forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redq.py
231 lines (201 loc) · 6.82 KB
/
redq.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
219
220
221
222
223
224
225
226
227
228
229
230
231
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import dataclasses
import os
import pathlib
import uuid
from datetime import datetime
import hydra
import torch.cuda
from hydra.core.config_store import ConfigStore
from torchrl.envs import ParallelEnv, EnvCreator
from torchrl.envs.transforms import RewardScaling, TransformedEnv
from torchrl.envs.utils import set_exploration_mode
from torchrl.modules import OrnsteinUhlenbeckProcessWrapper
from torchrl.record import VideoRecorder
from torchrl.trainers.helpers.collectors import (
make_collector_offpolicy,
OffPolicyCollectorConfig,
)
from torchrl.trainers.helpers.envs import (
correct_for_frame_skip,
get_stats_random_rollout,
parallel_env_constructor,
transformed_env_constructor,
EnvConfig,
)
from torchrl.trainers.helpers.logger import LoggerConfig
from torchrl.trainers.helpers.losses import make_redq_loss, LossConfig
from torchrl.trainers.helpers.models import (
make_redq_model,
REDQModelConfig,
)
from torchrl.trainers.helpers.replay_buffer import (
make_replay_buffer,
ReplayArgsConfig,
)
from torchrl.trainers.helpers.trainers import make_trainer, TrainerConfig
config_fields = [
(config_field.name, config_field.type, config_field)
for config_cls in (
TrainerConfig,
OffPolicyCollectorConfig,
EnvConfig,
LossConfig,
REDQModelConfig,
LoggerConfig,
ReplayArgsConfig,
)
for config_field in dataclasses.fields(config_cls)
]
Config = dataclasses.make_dataclass(cls_name="Config", fields=config_fields)
cs = ConfigStore.instance()
cs.store(name="config", node=Config)
DEFAULT_REWARD_SCALING = {
"Hopper-v1": 5,
"Walker2d-v1": 5,
"HalfCheetah-v1": 5,
"cheetah": 5,
"Ant-v2": 5,
"Humanoid-v2": 20,
"humanoid": 100,
}
@hydra.main(version_base=None, config_path=".", config_name="config")
def main(cfg: "DictConfig"): # noqa: F821
cfg = correct_for_frame_skip(cfg)
if not isinstance(cfg.reward_scaling, float):
cfg.reward_scaling = DEFAULT_REWARD_SCALING.get(cfg.env_name, 5.0)
device = (
torch.device("cpu")
if torch.cuda.device_count() == 0
else torch.device("cuda:0")
)
exp_name = "_".join(
[
"REDQ",
cfg.exp_name,
str(uuid.uuid4())[:8],
datetime.now().strftime("%y_%m_%d-%H_%M_%S"),
]
)
if cfg.logger == "tensorboard":
from torchrl.trainers.loggers.tensorboard import TensorboardLogger
logger = TensorboardLogger(log_dir="redq_logging", exp_name=exp_name)
elif cfg.logger == "csv":
from torchrl.trainers.loggers.csv import CSVLogger
logger = CSVLogger(log_dir="redq_logging", exp_name=exp_name)
elif cfg.logger == "wandb":
from torchrl.trainers.loggers.wandb import WandbLogger
logger = WandbLogger(log_dir="redq_logging", exp_name=exp_name)
elif cfg.logger == "mlflow":
from torchrl.trainers.loggers.mlflow import MLFlowLogger
logger = MLFlowLogger(
tracking_uri=pathlib.Path(os.path.abspath("redq_logging")).as_uri(),
exp_name=exp_name,
)
video_tag = exp_name if cfg.record_video else ""
stats = None
if not cfg.vecnorm and cfg.norm_stats:
proof_env = transformed_env_constructor(cfg=cfg, use_env_creator=False)()
stats = get_stats_random_rollout(
cfg,
proof_env,
key="next_pixels" if cfg.from_pixels else "next_observation_vector",
)
# make sure proof_env is closed
proof_env.close()
elif cfg.from_pixels:
stats = {"loc": 0.5, "scale": 0.5}
proof_env = transformed_env_constructor(
cfg=cfg, use_env_creator=False, stats=stats
)()
model = make_redq_model(
proof_env,
cfg=cfg,
device=device,
)
loss_module, target_net_updater = make_redq_loss(model, cfg)
actor_model_explore = model[0]
if cfg.ou_exploration:
if cfg.gSDE:
raise RuntimeError("gSDE and ou_exploration are incompatible")
actor_model_explore = OrnsteinUhlenbeckProcessWrapper(
actor_model_explore,
annealing_num_steps=cfg.annealing_frames,
sigma=cfg.ou_sigma,
theta=cfg.ou_theta,
).to(device)
if device == torch.device("cpu"):
# mostly for debugging
actor_model_explore.share_memory()
if cfg.gSDE:
with torch.no_grad(), set_exploration_mode("random"):
# get dimensions to build the parallel env
proof_td = actor_model_explore(proof_env.reset().to(device))
action_dim_gsde, state_dim_gsde = proof_td.get("_eps_gSDE").shape[-2:]
del proof_td
else:
action_dim_gsde, state_dim_gsde = None, None
proof_env.close()
create_env_fn = parallel_env_constructor(
cfg=cfg,
stats=stats,
action_dim_gsde=action_dim_gsde,
state_dim_gsde=state_dim_gsde,
)
collector = make_collector_offpolicy(
make_env=create_env_fn,
actor_model_explore=actor_model_explore,
cfg=cfg,
# make_env_kwargs=[
# {"device": device} if device >= 0 else {}
# for device in args.env_rendering_devices
# ],
)
replay_buffer = make_replay_buffer(device, cfg)
recorder = transformed_env_constructor(
cfg,
video_tag=video_tag,
norm_obs_only=True,
stats=stats,
logger=logger,
use_env_creator=False,
)()
# remove video recorder from recorder to have matching state_dict keys
if cfg.record_video:
recorder_rm = TransformedEnv(recorder.base_env)
for transform in recorder.transform:
if not isinstance(transform, VideoRecorder):
recorder_rm.append_transform(transform)
else:
recorder_rm = recorder
if isinstance(create_env_fn, ParallelEnv):
recorder_rm.load_state_dict(create_env_fn.state_dict()["worker0"])
create_env_fn.close()
elif isinstance(create_env_fn, EnvCreator):
recorder_rm.load_state_dict(create_env_fn().state_dict())
else:
recorder_rm.load_state_dict(create_env_fn.state_dict())
# reset reward scaling
for t in recorder.transform:
if isinstance(t, RewardScaling):
t.scale.fill_(1.0)
t.loc.fill_(0.0)
trainer = make_trainer(
collector,
loss_module,
recorder,
target_net_updater,
actor_model_explore,
replay_buffer,
logger,
cfg,
)
final_seed = collector.set_seed(cfg.seed)
print(f"init seed: {cfg.seed}, final seed: {final_seed}")
trainer.train()
return (logger.log_dir, trainer._log_dict)
if __name__ == "__main__":
main()