-
Notifications
You must be signed in to change notification settings - Fork 55
/
ddppo_trainer.py
532 lines (464 loc) · 22.3 KB
/
ddppo_trainer.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import contextlib
import os
import random
import time
from collections import defaultdict, deque
import numpy as np
import torch
import torch.distributed as distrib
import torch.nn as nn
from torch.optim.lr_scheduler import LambdaLR
from habitat import Config, logger
from ss_baselines.common.baseline_registry import baseline_registry
from ss_baselines.common.env_utils import construct_envs
from ss_baselines.common.environments import get_env_class
from ss_baselines.savi.models.rollout_storage import RolloutStorage
from ss_baselines.common.tensorboard_utils import TensorboardWriter
from ss_baselines.common.utils import batch_obs, linear_decay
from ss_baselines.savi.ddppo.algo.ddp_utils import (
EXIT,
REQUEUE,
add_signal_handlers,
init_distrib_slurm,
load_interrupted_state,
requeue_job,
save_interrupted_state,
)
from ss_baselines.savi.ddppo.algo.ddppo import DDPPO
from ss_baselines.savi.models.belief_predictor import BeliefPredictor, BeliefPredictorDDP
from ss_baselines.savi.ppo.ppo_trainer import PPOTrainer
from ss_baselines.savi.ppo.policy import AudioNavSMTPolicy, AudioNavBaselinePolicy
@baseline_registry.register_trainer(name="ddppo")
class DDPPOTrainer(PPOTrainer):
# DD-PPO cuts rollouts short to mitigate the straggler effect
# This, in theory, can cause some rollouts to be very short.
# All rollouts contributed equally to the loss/model-update,
# thus very short rollouts can be problematic. This threshold
# limits the how short a short rollout can be as a fraction of the
# max rollout length
SHORT_ROLLOUT_THRESHOLD: float = 0.25
def __init__(self, config=None):
interrupted_state = load_interrupted_state()
if interrupted_state is not None:
config = interrupted_state["config"]
super().__init__(config)
def _setup_actor_critic_agent(self, ppo_cfg: Config, observation_space=None) -> None:
r"""Sets up actor critic and agent for DD-PPO.
Args:
ppo_cfg: config node with relevant params
Returns:
None
"""
logger.add_filehandler(self.config.LOG_FILE)
action_space = self.envs.action_spaces[0]
self.action_space = action_space
has_distractor_sound = self.config.TASK_CONFIG.SIMULATOR.AUDIO.HAS_DISTRACTOR_SOUND
if ppo_cfg.policy_type == 'rnn':
self.actor_critic = AudioNavBaselinePolicy(
observation_space=self.envs.observation_spaces[0],
action_space=self.action_space,
hidden_size=ppo_cfg.hidden_size,
goal_sensor_uuid=self.config.TASK_CONFIG.TASK.GOAL_SENSOR_UUID,
extra_rgb=self.config.EXTRA_RGB,
use_mlp_state_encoder=ppo_cfg.use_mlp_state_encoder
)
if ppo_cfg.use_belief_predictor:
belief_cfg = ppo_cfg.BELIEF_PREDICTOR
bp_class = BeliefPredictorDDP if belief_cfg.online_training else BeliefPredictor
self.belief_predictor = bp_class(belief_cfg, self.device, None, None,
ppo_cfg.hidden_size, self.envs.num_envs, has_distractor_sound
).to(device=self.device)
if belief_cfg.online_training:
params = list(self.belief_predictor.predictor.parameters())
if belief_cfg.train_encoder:
params += list(self.actor_critic.net.goal_encoder.parameters()) + \
list(self.actor_critic.net.visual_encoder.parameters()) + \
list(self.actor_critic.net.action_encoder.parameters())
self.belief_predictor.optimizer = torch.optim.Adam(params, lr=belief_cfg.lr)
self.belief_predictor.freeze_encoders()
elif ppo_cfg.policy_type == 'smt':
smt_cfg = ppo_cfg.SCENE_MEMORY_TRANSFORMER
belief_cfg = ppo_cfg.BELIEF_PREDICTOR
self.actor_critic = AudioNavSMTPolicy(
observation_space=self.envs.observation_spaces[0],
action_space=self.envs.action_spaces[0],
hidden_size=smt_cfg.hidden_size,
nhead=smt_cfg.nhead,
num_encoder_layers=smt_cfg.num_encoder_layers,
num_decoder_layers=smt_cfg.num_decoder_layers,
dropout=smt_cfg.dropout,
activation=smt_cfg.activation,
use_pretrained=smt_cfg.use_pretrained,
pretrained_path=smt_cfg.pretrained_path,
pretraining=smt_cfg.pretraining,
use_belief_encoding=smt_cfg.use_belief_encoding,
use_belief_as_goal=ppo_cfg.use_belief_predictor,
use_label_belief=belief_cfg.use_label_belief,
use_location_belief=belief_cfg.use_location_belief,
normalize_category_distribution=belief_cfg.normalize_category_distribution,
use_category_input=has_distractor_sound
)
if smt_cfg.freeze_encoders:
self._static_smt_encoder = True
self.actor_critic.net.freeze_encoders()
if ppo_cfg.use_belief_predictor:
smt = self.actor_critic.net.smt_state_encoder
bp_class = BeliefPredictorDDP if belief_cfg.online_training else BeliefPredictor
self.belief_predictor = bp_class(belief_cfg, self.device, smt._input_size, smt._pose_indices,
smt.hidden_state_size, self.envs.num_envs, has_distractor_sound
).to(device=self.device)
if belief_cfg.online_training:
params = list(self.belief_predictor.predictor.parameters())
if belief_cfg.train_encoder:
params += list(self.actor_critic.net.goal_encoder.parameters()) + \
list(self.actor_critic.net.visual_encoder.parameters()) + \
list(self.actor_critic.net.action_encoder.parameters())
self.belief_predictor.optimizer = torch.optim.Adam(params, lr=belief_cfg.lr)
self.belief_predictor.freeze_encoders()
else:
raise ValueError(f'Policy type {ppo_cfg.policy_type} is not defined!')
self.actor_critic.to(self.device)
if self.config.RL.DDPPO.pretrained:
# load weights for both actor critic and the encoder
pretrained_state = torch.load(self.config.RL.DDPPO.pretrained_weights, map_location="cpu")
self.actor_critic.load_state_dict(
{
k[len("actor_critic."):]: v
for k, v in pretrained_state["state_dict"].items()
if "actor_critic.net.visual_encoder" not in k and
"actor_critic.net.smt_state_encoder" not in k
},
strict=False
)
self.actor_critic.net.visual_encoder.rgb_encoder.load_state_dict(
{
k[len("actor_critic.net.visual_encoder.rgb_encoder."):]: v
for k, v in pretrained_state["state_dict"].items()
if "actor_critic.net.visual_encoder.rgb_encoder." in k
},
)
self.actor_critic.net.visual_encoder.depth_encoder.load_state_dict(
{
k[len("actor_critic.net.visual_encoder.depth_encoder."):]: v
for k, v in pretrained_state["state_dict"].items()
if "actor_critic.net.visual_encoder.depth_encoder." in k
},
)
if self.config.RL.DDPPO.reset_critic:
nn.init.orthogonal_(self.actor_critic.critic.fc.weight)
nn.init.constant_(self.actor_critic.critic.fc.bias, 0)
self.agent = DDPPO(
actor_critic=self.actor_critic,
clip_param=ppo_cfg.clip_param,
ppo_epoch=ppo_cfg.ppo_epoch,
num_mini_batch=ppo_cfg.num_mini_batch,
value_loss_coef=ppo_cfg.value_loss_coef,
entropy_coef=ppo_cfg.entropy_coef,
lr=ppo_cfg.lr,
eps=ppo_cfg.eps,
max_grad_norm=ppo_cfg.max_grad_norm,
use_normalized_advantage=ppo_cfg.use_normalized_advantage,
)
def train(self) -> None:
r"""Main method for DD-PPO.
Returns:
None
"""
self.local_rank, tcp_store = init_distrib_slurm(
self.config.RL.DDPPO.distrib_backend
)
add_signal_handlers()
# Stores the number of workers that have finished their rollout
num_rollouts_done_store = distrib.PrefixStore(
"rollout_tracker", tcp_store
)
num_rollouts_done_store.set("num_done", "0")
self.world_rank = distrib.get_rank()
self.world_size = distrib.get_world_size()
self.config.defrost()
self.config.TORCH_GPU_ID = self.local_rank
self.config.SIMULATOR_GPU_ID = self.local_rank
# Multiply by the number of simulators to make sure they also get unique seeds
self.config.TASK_CONFIG.SEED += (
self.world_rank * self.config.NUM_PROCESSES
)
self.config.freeze()
random.seed(self.config.TASK_CONFIG.SEED)
np.random.seed(self.config.TASK_CONFIG.SEED)
torch.manual_seed(self.config.TASK_CONFIG.SEED)
if torch.cuda.is_available():
self.device = torch.device("cuda", self.local_rank)
torch.cuda.set_device(self.device)
else:
self.device = torch.device("cpu")
self.envs = construct_envs(
self.config, get_env_class(self.config.ENV_NAME)
)
ppo_cfg = self.config.RL.PPO
if (
not os.path.isdir(self.config.CHECKPOINT_FOLDER)
and self.world_rank == 0
):
os.makedirs(self.config.CHECKPOINT_FOLDER)
self._setup_actor_critic_agent(ppo_cfg)
self.agent.init_distributed(find_unused_params=True)
if ppo_cfg.use_belief_predictor and ppo_cfg.BELIEF_PREDICTOR.online_training:
self.belief_predictor.init_distributed(find_unused_params=True)
if self.world_rank == 0:
logger.info(
"agent number of trainable parameters: {}".format(
sum(
param.numel()
for param in self.agent.parameters()
if param.requires_grad
)
)
)
if ppo_cfg.use_belief_predictor:
logger.info(
"belief predictor number of trainable parameters: {}".format(
sum(
param.numel()
for param in self.belief_predictor.parameters()
if param.requires_grad
)
)
)
logger.info(f"config: {self.config}")
observations = self.envs.reset()
batch = batch_obs(observations, device=self.device)
obs_space = self.envs.observation_spaces[0]
if ppo_cfg.use_external_memory:
memory_dim = self.actor_critic.net.memory_dim
else:
memory_dim = None
rollouts = RolloutStorage(
ppo_cfg.num_steps,
self.envs.num_envs,
obs_space,
self.action_space,
ppo_cfg.hidden_size,
ppo_cfg.use_external_memory,
ppo_cfg.SCENE_MEMORY_TRANSFORMER.memory_size + ppo_cfg.num_steps,
ppo_cfg.SCENE_MEMORY_TRANSFORMER.memory_size,
memory_dim,
num_recurrent_layers=self.actor_critic.net.num_recurrent_layers,
)
rollouts.to(self.device)
if self.config.RL.PPO.use_belief_predictor:
self.belief_predictor.update(batch, None)
for sensor in rollouts.observations:
rollouts.observations[sensor][0].copy_(batch[sensor])
# batch and observations may contain shared PyTorch CUDA
# tensors. We must explicitly clear them here otherwise
# they will be kept in memory for the entire duration of training!
batch = None
observations = None
current_episode_reward = torch.zeros(
self.envs.num_envs, 1, device=self.device
)
running_episode_stats = dict(
count=torch.zeros(self.envs.num_envs, 1, device=self.device),
reward=torch.zeros(self.envs.num_envs, 1, device=self.device),
)
window_episode_stats = defaultdict(
lambda: deque(maxlen=ppo_cfg.reward_window_size)
)
t_start = time.time()
env_time = 0
pth_time = 0
count_steps = 0
count_checkpoints = 0
start_update = 0
prev_time = 0
lr_scheduler = LambdaLR(
optimizer=self.agent.optimizer,
lr_lambda=lambda x: linear_decay(x, self.config.NUM_UPDATES),
)
# Try to resume at previous checkpoint (independent of interrupted states)
count_steps_start, count_checkpoints, start_update = self.try_to_resume_checkpoint()
count_steps = count_steps_start
interrupted_state = load_interrupted_state()
if interrupted_state is not None:
self.agent.load_state_dict(interrupted_state["state_dict"])
if self.config.RL.PPO.use_belief_predictor:
self.belief_predictor.load_state_dict(interrupted_state["belief_predictor"])
self.agent.optimizer.load_state_dict(
interrupted_state["optim_state"]
)
lr_scheduler.load_state_dict(interrupted_state["lr_sched_state"])
requeue_stats = interrupted_state["requeue_stats"]
env_time = requeue_stats["env_time"]
pth_time = requeue_stats["pth_time"]
count_steps = requeue_stats["count_steps"]
count_checkpoints = requeue_stats["count_checkpoints"]
start_update = requeue_stats["start_update"]
prev_time = requeue_stats["prev_time"]
with (
TensorboardWriter(
self.config.TENSORBOARD_DIR, flush_secs=self.flush_secs
)
if self.world_rank == 0
else contextlib.suppress()
) as writer:
for update in range(start_update, self.config.NUM_UPDATES):
if ppo_cfg.use_linear_lr_decay:
lr_scheduler.step()
if ppo_cfg.use_linear_clip_decay:
self.agent.clip_param = ppo_cfg.clip_param * linear_decay(
update, self.config.NUM_UPDATES
)
if EXIT.is_set():
self.envs.close()
if REQUEUE.is_set() and self.world_rank == 0:
requeue_stats = dict(
env_time=env_time,
pth_time=pth_time,
count_steps=count_steps,
count_checkpoints=count_checkpoints,
start_update=update,
prev_time=(time.time() - t_start) + prev_time,
)
state_dict = dict(
state_dict=self.agent.state_dict(),
optim_state=self.agent.optimizer.state_dict(),
lr_sched_state=lr_scheduler.state_dict(),
config=self.config,
requeue_stats=requeue_stats,
)
if self.config.RL.PPO.use_belief_predictor:
state_dict['belief_predictor'] = self.belief_predictor.state_dict()
save_interrupted_state(state_dict)
requeue_job()
return
count_steps_delta = 0
self.agent.eval()
if self.config.RL.PPO.use_belief_predictor:
self.belief_predictor.eval()
for step in range(ppo_cfg.num_steps):
(
delta_pth_time,
delta_env_time,
delta_steps,
) = self._collect_rollout_step(
rollouts, current_episode_reward, running_episode_stats
)
pth_time += delta_pth_time
env_time += delta_env_time
count_steps_delta += delta_steps
# This is where the preemption of workers happens. If a
# worker detects it will be a straggler, it preempts itself!
if (
step
>= ppo_cfg.num_steps * self.SHORT_ROLLOUT_THRESHOLD
) and int(num_rollouts_done_store.get("num_done")) > (
self.config.RL.DDPPO.sync_frac * self.world_size
):
break
num_rollouts_done_store.add("num_done", 1)
self.agent.train()
if self.config.RL.PPO.use_belief_predictor:
self.belief_predictor.train()
self.belief_predictor.set_eval_encoders()
if self._static_smt_encoder:
self.actor_critic.net.set_eval_encoders()
if ppo_cfg.use_belief_predictor and ppo_cfg.BELIEF_PREDICTOR.online_training:
location_predictor_loss, prediction_accuracy = self.train_belief_predictor(rollouts)
else:
location_predictor_loss = 0
prediction_accuracy = 0
(
delta_pth_time,
value_loss,
action_loss,
dist_entropy,
) = self._update_agent(ppo_cfg, rollouts)
pth_time += delta_pth_time
stats_ordering = list(sorted(running_episode_stats.keys()))
stats = torch.stack(
[running_episode_stats[k] for k in stats_ordering], 0
)
distrib.all_reduce(stats)
for i, k in enumerate(stats_ordering):
window_episode_stats[k].append(stats[i].clone())
stats = torch.tensor(
[value_loss, action_loss, dist_entropy, location_predictor_loss, prediction_accuracy, count_steps_delta],
device=self.device,
)
distrib.all_reduce(stats)
count_steps += stats[5].item()
if self.world_rank == 0:
num_rollouts_done_store.set("num_done", "0")
losses = [
stats[0].item() / self.world_size,
stats[1].item() / self.world_size,
stats[2].item() / self.world_size,
stats[3].item() / self.world_size,
stats[4].item() / self.world_size,
]
deltas = {
k: (
(v[-1] - v[0]).sum().item()
if len(v) > 1
else v[0].sum().item()
)
for k, v in window_episode_stats.items()
}
deltas["count"] = max(deltas["count"], 1.0)
writer.add_scalar(
"Metrics/reward", deltas["reward"] / deltas["count"], count_steps
)
# Check to see if there are any metrics
# that haven't been logged yet
metrics = {
k: v / deltas["count"]
for k, v in deltas.items()
if k not in {"reward", "count"}
}
if len(metrics) > 0:
for metric, value in metrics.items():
writer.add_scalar(f"Metrics/{metric}", value, count_steps)
writer.add_scalar("Policy/value_loss", losses[0], count_steps)
writer.add_scalar("Policy/policy_loss", losses[1], count_steps)
writer.add_scalar("Policy/entropy_loss", losses[2], count_steps)
writer.add_scalar("Policy/predictor_loss", losses[3], count_steps)
writer.add_scalar("Policy/predictor_accuracy", losses[4], count_steps)
writer.add_scalar('Policy/learning_rate', lr_scheduler.get_lr()[0], count_steps)
# log stats
if update > 0 and update % self.config.LOG_INTERVAL == 0:
logger.info(
"update: {}\tfps: {:.3f}\t".format(
update,
(count_steps - count_steps_start)
/ ((time.time() - t_start) + prev_time),
)
)
logger.info(
"update: {}\tenv-time: {:.3f}s\tpth-time: {:.3f}s\t"
"frames: {}".format(
update, env_time, pth_time, count_steps
)
)
logger.info(
"Average window size: {} {}".format(
len(window_episode_stats["count"]),
" ".join(
"{}: {:.3f}".format(k, v / deltas["count"])
for k, v in deltas.items()
if k != "count"
),
)
)
# checkpoint model
if update % self.config.CHECKPOINT_INTERVAL == 0:
self.save_checkpoint(
f"ckpt.{count_checkpoints}.pth",
dict(step=count_steps),
)
count_checkpoints += 1
self.envs.close()