-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_seed_fn.py
218 lines (184 loc) · 6.59 KB
/
run_seed_fn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
import pickle
import gc
from typing import List
import hydra
import numpy as np
import torch
from omegaconf import DictConfig
from rlbench import CameraConfig, ObservationConfig
from yarr.replay_buffer.wrappers.pytorch_replay_buffer import PyTorchReplayBuffer
from yarr.runners.offline_train_runner import OfflineTrainRunner
from yarr.utils.stat_accumulator import SimpleAccumulator
from helpers.custom_rlbench_env import CustomRLBenchEnv, CustomMultiTaskRLBenchEnv
import torch.distributed as dist
from agents import agent_factory
from agents import replay_utils
import peract_config
from functools import partial
def run_seed(
rank,
cfg: DictConfig,
obs_config: ObservationConfig,
seed,
world_size,
) -> None:
peract_config.config_logging()
dist.init_process_group("gloo", rank=rank, world_size=world_size)
tasks = cfg.rlbench.tasks
cams = cfg.rlbench.cameras
task_folder = "multi" if len(tasks) > 1 else tasks[0]
replay_path = os.path.join(
cfg.replay.path, task_folder, cfg.method.name, "seed%d" % seed
)
agent = agent_factory.create_agent(cfg)
if not agent:
print("Unable to create agent")
return
if cfg.method.name == "ARM":
raise NotImplementedError("ARM is not supported yet")
elif cfg.method.name == "BC_LANG":
from agents.baselines import bc_lang
assert cfg.ddp.num_devices == 1, "BC_LANG only supports single GPU training"
replay_buffer = bc_lang.launch_utils.create_replay(
cfg.replay.batch_size,
cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams,
cfg.rlbench.camera_resolution,
)
bc_lang.launch_utils.fill_multi_task_replay(
cfg,
obs_config,
rank,
replay_buffer,
tasks,
cfg.rlbench.demos,
cfg.method.demo_augmentation,
cfg.method.demo_augmentation_every_n,
cams,
)
elif cfg.method.name == "VIT_BC_LANG":
from agents.baselines import vit_bc_lang
assert cfg.ddp.num_devices == 1, "VIT_BC_LANG only supports single GPU training"
replay_buffer = vit_bc_lang.launch_utils.create_replay(
cfg.replay.batch_size,
cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams,
cfg.rlbench.camera_resolution,
)
vit_bc_lang.launch_utils.fill_multi_task_replay(
cfg,
obs_config,
rank,
replay_buffer,
tasks,
cfg.rlbench.demos,
cfg.method.demo_augmentation,
cfg.method.demo_augmentation_every_n,
cams,
)
elif cfg.method.name.startswith("ACT_BC_LANG"):
from agents import act_bc_lang
assert cfg.ddp.num_devices == 1, "ACT_BC_LANG only supports single GPU training"
replay_buffer = act_bc_lang.launch_utils.create_replay(
cfg.replay.batch_size,
cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams,
cfg.rlbench.camera_resolution,
replay_size=3e5,
prev_action_horizon=cfg.method.prev_action_horizon,
next_action_horizon=cfg.method.next_action_horizon,
)
act_bc_lang.launch_utils.fill_multi_task_replay(
cfg,
obs_config,
rank,
replay_buffer,
tasks,
cfg.rlbench.demos,
cfg.method.demo_augmentation,
cfg.method.demo_augmentation_every_n,
cams,
)
elif cfg.method.name == "C2FARM_LINGUNET_BC":
from agents import c2farm_lingunet_bc
replay_buffer = c2farm_lingunet_bc.launch_utils.create_replay(
cfg.replay.batch_size,
cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams,
cfg.method.voxel_sizes,
cfg.rlbench.camera_resolution,
)
c2farm_lingunet_bc.launch_utils.fill_multi_task_replay(
cfg,
obs_config,
rank,
replay_buffer,
tasks,
cfg.rlbench.demos,
cfg.method.demo_augmentation,
cfg.method.demo_augmentation_every_n,
cams,
cfg.rlbench.scene_bounds,
cfg.method.voxel_sizes,
cfg.method.bounds_offset,
cfg.method.rotation_resolution,
cfg.method.crop_augmentation,
keypoint_method=cfg.method.keypoint_method,
)
elif (
cfg.method.name.startswith("BIMANUAL_PERACT")
or cfg.method.name.startswith("RVT")
or cfg.method.name.startswith("PERACT_BC")
):
replay_buffer = replay_utils.create_replay(cfg, replay_path)
replay_utils.fill_multi_task_replay(cfg, obs_config, rank, replay_buffer, tasks)
elif cfg.method.name == "PERACT_RL":
raise NotImplementedError("PERACT_RL is not supported yet")
else:
raise ValueError("Method %s does not exists." % cfg.method.name)
wrapped_replay = PyTorchReplayBuffer(
replay_buffer, num_workers=cfg.framework.num_workers
)
stat_accum = SimpleAccumulator(eval_video_fps=30)
cwd = os.getcwd()
weightsdir = os.path.join(cwd, "seed%d" % seed, "weights")
logdir = os.path.join(cwd, "seed%d" % seed)
train_runner = OfflineTrainRunner(
agent=agent,
wrapped_replay_buffer=wrapped_replay,
train_device=rank,
stat_accumulator=stat_accum,
iterations=cfg.framework.training_iterations,
logdir=logdir,
logging_level=cfg.framework.logging_level,
log_freq=cfg.framework.log_freq,
weightsdir=weightsdir,
num_weights_to_keep=cfg.framework.num_weights_to_keep,
save_freq=cfg.framework.save_freq,
tensorboard_logging=cfg.framework.tensorboard_logging,
csv_logging=cfg.framework.csv_logging,
load_existing_weights=cfg.framework.load_existing_weights,
rank=rank,
world_size=world_size,
)
train_runner._on_thread_start = partial(
peract_config.config_logging, cfg.framework.logging_level
)
train_runner.start()
del train_runner
del agent
gc.collect()
torch.cuda.empty_cache()