-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathrun_seed_fn.py
163 lines (134 loc) · 6.05 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
import os
import pickle
import gc
import logging
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 c2farm_lingunet_bc
from agents import peract_bc
from agents import arm
from agents.baselines import bc_lang, vit_bc_lang
def run_seed(rank,
cfg: DictConfig,
obs_config: ObservationConfig,
cams,
multi_task,
seed,
world_size) -> None:
dist.init_process_group("gloo",
rank=rank,
world_size=world_size)
task = cfg.rlbench.tasks[0]
tasks = cfg.rlbench.tasks
task_folder = task if not multi_task else 'multi'
replay_path = os.path.join(cfg.replay.path, task_folder, cfg.method.name, 'seed%d' % seed)
if cfg.method.name == 'ARM':
raise NotImplementedError("ARM is not supported yet")
elif cfg.method.name == '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)
agent = bc_lang.launch_utils.create_agent(
cams[0], cfg.method.activation, cfg.method.lr,
cfg.method.weight_decay, cfg.rlbench.camera_resolution,
cfg.method.grad_clip)
elif cfg.method.name == '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)
agent = vit_bc_lang.launch_utils.create_agent(
cams[0], cfg.method.activation, cfg.method.lr,
cfg.method.weight_decay, cfg.rlbench.camera_resolution,
cfg.method.grad_clip)
elif cfg.method.name == '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)
agent = c2farm_lingunet_bc.launch_utils.create_agent(cfg)
elif cfg.method.name == 'PERACT_BC':
replay_buffer = peract_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)
peract_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)
agent = peract_bc.launch_utils.create_agent(cfg)
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.start()
del train_runner
del agent
gc.collect()
torch.cuda.empty_cache()