-
Notifications
You must be signed in to change notification settings - Fork 325
/
utils.py
473 lines (390 loc) · 14.1 KB
/
utils.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# 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 torch.nn
import torch.optim
from tensordict.nn import NormalParamExtractor, TensorDictModule
from torchrl.collectors import SyncDataCollector
from torchrl.data import CompositeSpec, LazyMemmapStorage, TensorDictReplayBuffer
from torchrl.data.replay_buffers.samplers import SamplerWithoutReplacement
from torchrl.data.tensor_specs import DiscreteBox
from torchrl.envs import (
CatFrames,
CatTensors,
DoubleToFloat,
EnvCreator,
ExplorationType,
GrayScale,
NoopResetEnv,
ObservationNorm,
ParallelEnv,
Resize,
RewardScaling,
RewardSum,
StepCounter,
ToTensorImage,
TransformedEnv,
)
from torchrl.envs.libs.dm_control import DMControlEnv
from torchrl.modules import (
ActorValueOperator,
ConvNet,
MLP,
OneHotCategorical,
ProbabilisticActor,
TanhNormal,
ValueOperator,
)
from torchrl.objectives import ClipPPOLoss
from torchrl.objectives.value.advantages import GAE
from torchrl.record.loggers import generate_exp_name, get_logger
from torchrl.trainers.helpers.envs import LIBS
DEFAULT_REWARD_SCALING = {
"Hopper-v1": 5,
"Walker2d-v1": 5,
"HalfCheetah-v1": 5,
"cheetah": 5,
"Ant-v2": 5,
"Humanoid-v2": 20,
"humanoid": 100,
}
# ====================================================================
# Environment utils
# -----------------
def make_base_env(env_cfg, from_pixels=None):
env_library = LIBS[env_cfg.env_library]
env_kwargs = {
"env_name": env_cfg.env_name,
"frame_skip": env_cfg.frame_skip,
"from_pixels": env_cfg.from_pixels
if from_pixels is None
else from_pixels, # for rendering
"pixels_only": False,
"device": env_cfg.device,
}
if env_library is DMControlEnv:
env_task = env_cfg.env_task
env_kwargs.update({"task_name": env_task})
env = env_library(**env_kwargs)
return env
def make_transformed_env(base_env, env_cfg):
if env_cfg.noop > 1:
base_env = TransformedEnv(env=base_env, transform=NoopResetEnv(env_cfg.noop))
from_pixels = env_cfg.from_pixels
if from_pixels:
return make_transformed_env_pixels(base_env, env_cfg)
else:
return make_transformed_env_states(base_env, env_cfg)
def make_transformed_env_pixels(base_env, env_cfg):
if not isinstance(env_cfg.reward_scaling, float):
env_cfg.reward_scaling = DEFAULT_REWARD_SCALING.get(env_cfg.env_name, 5.0)
env = TransformedEnv(base_env)
reward_scaling = env_cfg.reward_scaling
env.append_transform(RewardScaling(0.0, reward_scaling))
env.append_transform(ToTensorImage())
env.append_transform(GrayScale())
env.append_transform(Resize(84, 84))
env.append_transform(CatFrames(N=4, dim=-3))
env.append_transform(RewardSum())
env.append_transform(StepCounter())
obs_norm = ObservationNorm(in_keys=["pixels"], standard_normal=True)
env.append_transform(obs_norm)
env.append_transform(DoubleToFloat())
return env
def make_transformed_env_states(base_env, env_cfg):
if not isinstance(env_cfg.reward_scaling, float):
env_cfg.reward_scaling = DEFAULT_REWARD_SCALING.get(env_cfg.env_name, 5.0)
env = TransformedEnv(base_env)
reward_scaling = env_cfg.reward_scaling
env.append_transform(RewardScaling(0.0, reward_scaling))
# we concatenate all the state vectors
# even if there is a single tensor, it'll be renamed in "observation_vector"
selected_keys = [
key for key in env.observation_spec.keys(True, True) if key != "pixels"
]
out_key = "observation_vector"
env.append_transform(CatTensors(in_keys=selected_keys, out_key=out_key))
env.append_transform(RewardSum())
env.append_transform(StepCounter())
# obs_norm = ObservationNorm(in_keys=[out_key])
# env.append_transform(obs_norm)
env.append_transform(DoubleToFloat())
return env
def make_parallel_env(env_cfg, state_dict):
num_envs = env_cfg.num_envs
env = make_transformed_env(
ParallelEnv(num_envs, EnvCreator(lambda: make_base_env(env_cfg))), env_cfg
)
init_stats(env, 3, env_cfg.from_pixels)
env.load_state_dict(state_dict, strict=False)
return env
def get_stats(env_cfg):
env = make_transformed_env(make_base_env(env_cfg), env_cfg)
init_stats(env, env_cfg.n_samples_stats, env_cfg.from_pixels)
state_dict = env.state_dict()
for key in list(state_dict.keys()):
if key.endswith("loc") or key.endswith("scale"):
continue
del state_dict[key]
return state_dict
def init_stats(env, n_samples_stats, from_pixels):
for t in env.transform:
if isinstance(t, ObservationNorm):
if from_pixels:
t.init_stats(
n_samples_stats,
cat_dim=-4,
reduce_dim=tuple(
-i for i in range(1, len(t.parent.batch_size) + 5)
),
keep_dims=(-1, -2, -3),
)
else:
t.init_stats(n_samples_stats)
def make_test_env(env_cfg, state_dict):
env_cfg.num_envs = 1
env = make_parallel_env(env_cfg, state_dict=state_dict)
return env
# ====================================================================
# Collector and replay buffer
# ---------------------------
def make_collector(cfg, policy):
env_cfg = cfg.env
collector_cfg = cfg.collector
collector_class = SyncDataCollector
state_dict = get_stats(env_cfg)
collector = collector_class(
make_parallel_env(env_cfg, state_dict=state_dict),
policy,
frames_per_batch=collector_cfg.frames_per_batch,
total_frames=collector_cfg.total_frames,
device=collector_cfg.collector_device,
storing_device="cpu",
max_frames_per_traj=collector_cfg.max_frames_per_traj,
)
return collector, state_dict
def make_data_buffer(cfg):
cfg_collector = cfg.collector
cfg_loss = cfg.loss
sampler = SamplerWithoutReplacement()
return TensorDictReplayBuffer(
storage=LazyMemmapStorage(cfg_collector.frames_per_batch),
sampler=sampler,
batch_size=cfg_loss.mini_batch_size,
)
# ====================================================================
# Model
# -----
#
# We give one version of the model for learning from pixels, and one for state.
# TorchRL comes in handy at this point, as the high-level interactions with
# these models is unchanged, regardless of the modality.
def make_ppo_models(cfg):
env_cfg = cfg.env
from_pixels = env_cfg.from_pixels
proof_environment = make_transformed_env(make_base_env(env_cfg), env_cfg)
init_stats(proof_environment, 3, env_cfg.from_pixels)
if not from_pixels:
# we must initialize the observation norm transform
# init_stats(
# proof_environment, n_samples_stats=3, from_pixels=env_cfg.from_pixels
# )
common_module, policy_module, value_module = make_ppo_modules_state(
proof_environment
)
else:
common_module, policy_module, value_module = make_ppo_modules_pixels(
proof_environment
)
# Wrap modules in a single ActorCritic operator
actor_critic = ActorValueOperator(
common_operator=common_module,
policy_operator=policy_module,
value_operator=value_module,
).to(cfg.optim.device)
with torch.no_grad():
td = proof_environment.rollout(max_steps=100, break_when_any_done=False)
td = actor_critic(td)
del td
actor = actor_critic.get_policy_operator()
critic = actor_critic.get_value_operator()
critic_head = actor_critic.get_value_head()
return actor, critic, critic_head
def make_ppo_modules_state(proof_environment):
# Define input shape
input_shape = proof_environment.observation_spec["observation_vector"].shape
# Define distribution class and kwargs
continuous_actions = False
if isinstance(proof_environment.action_spec.space, DiscreteBox):
num_outputs = proof_environment.action_spec.space.n
distribution_class = OneHotCategorical
distribution_kwargs = {}
else: # is ContinuousBox
continuous_actions = True
num_outputs = proof_environment.action_spec.shape[-1] * 2
distribution_class = TanhNormal
distribution_kwargs = {
"min": proof_environment.action_spec.space.low,
"max": proof_environment.action_spec.space.high,
"tanh_loc": False,
}
# Define input keys
in_keys = ["observation_vector"]
shared_features_size = 256
# Define a shared Module and TensorDictModule
common_mlp = MLP(
in_features=input_shape[-1],
activation_class=torch.nn.ReLU,
activate_last_layer=True,
out_features=shared_features_size,
num_cells=[64, 64],
)
common_module = TensorDictModule(
module=common_mlp,
in_keys=in_keys,
out_keys=["common_features"],
)
# Define on head for the policy
policy_net = MLP(
in_features=shared_features_size, out_features=num_outputs, num_cells=[]
)
if continuous_actions:
policy_net = torch.nn.Sequential(
policy_net, NormalParamExtractor(scale_lb=1e-2)
)
policy_module = TensorDictModule(
module=policy_net,
in_keys=["common_features"],
out_keys=["loc", "scale"] if continuous_actions else ["logits"],
)
# Add probabilistic sampling of the actions
policy_module = ProbabilisticActor(
policy_module,
in_keys=["loc", "scale"] if continuous_actions else ["logits"],
spec=CompositeSpec(action=proof_environment.action_spec),
safe=True,
distribution_class=distribution_class,
distribution_kwargs=distribution_kwargs,
return_log_prob=True,
default_interaction_type=ExplorationType.RANDOM,
)
# Define another head for the value
value_net = MLP(in_features=shared_features_size, out_features=1, num_cells=[])
value_module = ValueOperator(
value_net,
in_keys=["common_features"],
)
return common_module, policy_module, value_module
def make_ppo_modules_pixels(proof_environment):
# Define input shape
input_shape = proof_environment.observation_spec["pixels"].shape
# Define distribution class and kwargs
if isinstance(proof_environment.action_spec.space, DiscreteBox):
num_outputs = proof_environment.action_spec.space.n
distribution_class = OneHotCategorical
distribution_kwargs = {}
else: # is ContinuousBox
num_outputs = proof_environment.action_spec.shape
distribution_class = TanhNormal
distribution_kwargs = {
"min": proof_environment.action_spec.space.low,
"max": proof_environment.action_spec.space.high,
}
# Define input keys
in_keys = ["pixels"]
# Define a shared Module and TensorDictModule (CNN + MLP)
common_cnn = ConvNet(
activation_class=torch.nn.ReLU,
num_cells=[32, 64, 64],
kernel_sizes=[8, 4, 3],
strides=[4, 2, 1],
)
common_cnn_output = common_cnn(torch.ones(input_shape))
common_mlp = MLP(
in_features=common_cnn_output.shape[-1],
activation_class=torch.nn.ReLU,
activate_last_layer=True,
out_features=512,
num_cells=[],
)
common_mlp_output = common_mlp(common_cnn_output)
# Define shared net as TensorDictModule
common_module = TensorDictModule(
module=torch.nn.Sequential(common_cnn, common_mlp),
in_keys=in_keys,
out_keys=["common_features"],
)
# Define on head for the policy
policy_net = MLP(
in_features=common_mlp_output.shape[-1],
out_features=num_outputs,
activation_class=torch.nn.ReLU,
num_cells=[256],
)
policy_module = TensorDictModule(
module=policy_net,
in_keys=["common_features"],
out_keys=["logits"],
)
# Add probabilistic sampling of the actions
policy_module = ProbabilisticActor(
policy_module,
in_keys=["logits"],
spec=CompositeSpec(action=proof_environment.action_spec),
# safe=True,
distribution_class=distribution_class,
distribution_kwargs=distribution_kwargs,
return_log_prob=True,
default_interaction_type=ExplorationType.RANDOM,
)
# Define another head for the value
value_net = MLP(
activation_class=torch.nn.ReLU,
in_features=common_mlp_output.shape[-1],
out_features=1,
num_cells=[256],
)
value_module = ValueOperator(
value_net,
in_keys=["common_features"],
)
return common_module, policy_module, value_module
# ====================================================================
# PPO Loss
# ---------
def make_advantage_module(loss_cfg, value_network):
advantage_module = GAE(
gamma=loss_cfg.gamma,
lmbda=loss_cfg.gae_lamdda,
value_network=value_network,
average_gae=True,
)
return advantage_module
def make_loss(loss_cfg, actor_network, value_network, value_head):
advantage_module = make_advantage_module(loss_cfg, value_network)
loss_module = ClipPPOLoss(
actor=actor_network,
critic=value_head,
clip_epsilon=loss_cfg.clip_epsilon,
loss_critic_type=loss_cfg.loss_critic_type,
entropy_coef=loss_cfg.entropy_coef,
critic_coef=loss_cfg.critic_coef,
normalize_advantage=True,
)
return loss_module, advantage_module
def make_optim(optim_cfg, loss_module):
optim = torch.optim.Adam(
loss_module.parameters(),
lr=optim_cfg.lr,
weight_decay=optim_cfg.weight_decay,
)
return optim
# ====================================================================
# Logging and recording
# ---------------------
def make_logger(logger_cfg):
exp_name = generate_exp_name("PPO", logger_cfg.exp_name)
logger_cfg.exp_name = exp_name
logger = get_logger(logger_cfg.backend, logger_name="ppo", experiment_name=exp_name)
return logger