-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_ppo.py
More file actions
747 lines (646 loc) · 32.7 KB
/
stack_ppo.py
File metadata and controls
747 lines (646 loc) · 32.7 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
from collections import defaultdict
import gc
import os
import random
import time
from dataclasses import dataclass
from typing import Optional
import gymnasium as gym
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import tyro
from torch.distributions.normal import Normal
from torch.utils.tensorboard import SummaryWriter
# ManiSkill specific imports
import mani_skill.envs
from mani_skill.utils import gym_utils
from mani_skill.utils.wrappers.flatten import FlattenActionSpaceWrapper, FlattenRGBDObservationWrapper
from mani_skill.utils.wrappers.record import RecordEpisode
from mani_skill.vector.wrappers.gymnasium import ManiSkillVectorEnv
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions.normal import Normal
from so101_stack_cube import StackCubeSO101Env
@dataclass
class Args:
exp_name: Optional[str] = 'StackCubeCamCkpt'
"""the name of this experiment"""
seed: int = 1
"""seed of the experiment"""
torch_deterministic: bool = True
"""if toggled, `torch.backends.cudnn.deterministic=False`"""
cuda: bool = True
"""if toggled, cuda will be enabled by default"""
track: bool = True
"""if toggled, this experiment will be tracked with Weights and Biases"""
wandb_project_name: str = "ManiSkill"
"""the wandb's project name"""
wandb_entity: Optional[str] = None
"""the entity (team) of wandb's project"""
wandb_group: str = "PPO"
"""the group of the run for wandb"""
capture_video: bool = True
"""whether to capture videos of the agent performances (check out `videos` folder)"""
save_model: bool = True
"""whether to save model into the `runs/{run_name}` folder"""
evaluate: bool = False
"""if toggled, only runs evaluation with the given model checkpoint and saves the evaluation trajectories"""
checkpoint: Optional[str] = 'runs/ckpt_621.pt'
"""path to a pretrained checkpoint file to start evaluation/training from"""
render_mode: str = "sensors"
"""the environment rendering mode"""
# Algorithm specific arguments
env_id: str = "StackCubeSO101-v0"
"""the id of the environment"""
include_state: bool = True
"""whether to include state information in observations"""
obs_mode: str = 'rgb'
"""whether to include privileged states, if yes use 'rgb+state'"""
total_timesteps: int = 60000000
"""total timesteps of the experiments"""
learning_rate: float = 3e-4
"""the learning rate of the optimizer"""
num_envs: int = 400
"""the number of parallel environments"""
num_eval_envs: int = 4
"""the number of parallel evaluation environments"""
partial_reset: bool = True
"""whether to let parallel environments reset upon termination instead of truncation"""
eval_partial_reset: bool = False
"""whether to let parallel evaluation environments reset upon termination instead of truncation"""
num_steps: int = 80
"""the number of steps to run in each environment per policy rollout"""
num_eval_steps: int = 100
"""the number of steps to run in each evaluation environment during evaluation"""
reconfiguration_freq: Optional[int] = None
"""how often to reconfigure the environment during training"""
eval_reconfiguration_freq: Optional[int] = 1
"""for benchmarking purposes we want to reconfigure the eval environment each reset to ensure objects are randomized in some tasks"""
control_mode: Optional[str] = "pd_ee_delta_pose"
"""the control mode to use for the environment"""
anneal_lr: bool = False
"""Toggle learning rate annealing for policy and value networks"""
gamma: float = 0.95
"""the discount factor gamma"""
gae_lambda: float = 0.9
"""the lambda for the general advantage estimation"""
num_minibatches: int = 32
"""the number of mini-batches"""
update_epochs: int = 5
"""the K epochs to update the policy"""
norm_adv: bool = True
"""Toggles advantages normalization"""
clip_coef: float = 0.2
"""the surrogate clipping coefficient"""
clip_vloss: bool = False
"""Toggles whether or not to use a clipped loss for the value function, as per the paper."""
ent_coef: float = 0.01
"""coefficient of the entropy"""
vf_coef: float = 0.5
"""coefficient of the value function"""
max_grad_norm: float = 0.5
"""the maximum norm for the gradient clipping"""
target_kl: float = 0.1
"""the target KL divergence threshold"""
reward_scale: float = 1.0
"""Scale the reward by this factor"""
eval_freq: int = 10
"""evaluation frequency in terms of iterations"""
save_train_video_freq: Optional[int] = None
"""frequency to save training videos in terms of iterations"""
finite_horizon_gae: bool = False
# to be filled in runtime
batch_size: int = 0
"""the batch size (computed in runtime)"""
minibatch_size: int = 0
"""the mini-batch size (computed in runtime)"""
num_iterations: int = 0
"""the number of iterations (computed in runtime)"""
def layer_init(layer, std=np.sqrt(2), bias_const=0.0):
"""
Parameter initialization helper function
"""
torch.nn.init.orthogonal_(layer.weight, std)
torch.nn.init.constant_(layer.bias, bias_const)
return layer
class DictArray(object):
def __init__(self, buffer_shape, element_space, data_dict=None, device=None):
self.buffer_shape = buffer_shape
if data_dict:
self.data = data_dict
else:
assert isinstance(element_space, gym.spaces.dict.Dict)
self.data = {}
for k, v in element_space.items():
if isinstance(v, gym.spaces.dict.Dict):
self.data[k] = DictArray(buffer_shape, v, device=device)
else:
dtype = (torch.float32 if v.dtype in (np.float32, np.float64) else
torch.uint8 if v.dtype == np.uint8 else
torch.int16 if v.dtype == np.int16 else
torch.int32 if v.dtype == np.int32 else
v.dtype)
print(buffer_shape + v.shape, dtype)
self.data[k] = torch.zeros(buffer_shape + v.shape, dtype=dtype, device=device)
def keys(self):
return self.data.keys()
def __getitem__(self, index):
if isinstance(index, str):
return self.data[index]
return {
k: v[index] for k, v in self.data.items()
}
def __setitem__(self, index, value):
if isinstance(index, str):
self.data[index] = value
for k, v in value.items():
self.data[k][index] = v
@property
def shape(self):
return self.buffer_shape
def reshape(self, shape):
t = len(self.buffer_shape)
new_dict = {}
for k,v in self.data.items():
if isinstance(v, DictArray):
new_dict[k] = v.reshape(shape)
else:
new_dict[k] = v.reshape(shape + v.shape[t:])
new_buffer_shape = next(iter(new_dict.values())).shape[:len(shape)]
return DictArray(new_buffer_shape, None, data_dict=new_dict)
class NatureCNN(nn.Module):
def __init__(self, sample_obs, feature_size=512, epsilon=1e-8, clip_obs=5.0):
super().__init__()
self.epsilon = epsilon
self.clip_obs = clip_obs
self.out_features = 0
if "state" in sample_obs:
state_dim = sample_obs["state"].shape[-1]
print(f'State Dimension: {state_dim}')
self.register_buffer("running_mean", torch.zeros(state_dim))
self.register_buffer("running_var", torch.ones(state_dim))
self.register_buffer("count", torch.tensor(1e-4))
self.state_net = nn.Sequential(
nn.Linear(state_dim, 256),
nn.ReLU()
)
self.out_features += 256
if "rgb" in sample_obs:
img_h = sample_obs["rgb"].shape[1]
img_w = sample_obs["rgb"].shape[2]
def make_cnn():
return nn.Sequential(
nn.Conv2d(3, 32, kernel_size=8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.Flatten(),
)
self.cnn_v1 = make_cnn()
self.cnn_v2 = make_cnn()
with torch.no_grad():
dummy_img = torch.zeros(1, 3, img_h, img_w)
n_flatten = self.cnn_v1(dummy_img).shape[1]
half_feature = feature_size // 2
def make_visual_head():
return nn.Sequential(
nn.Linear(n_flatten, half_feature),
nn.LayerNorm(half_feature),
nn.ReLU()
)
self.rgb_final1 = make_visual_head()
self.rgb_final2 = make_visual_head()
self.out_features += (half_feature * 2)
def forward(self, observations) -> torch.Tensor:
encoded_tensor_list = []
if "state" in observations:
state = observations["state"].float().to(self.running_mean.device)
if self.training:
with torch.no_grad():
batch_mean = state.mean(dim=0)
batch_var = state.var(dim=0, unbiased=False)
batch_count = state.shape[0]
# Welford 算法更新 Running Mean/Var
delta = batch_mean - self.running_mean
tot_count = self.count + batch_count
new_mean = self.running_mean + delta * batch_count / tot_count
m_a = self.running_var * self.count
m_b = batch_var * batch_count
M2 = m_a + m_b + delta**2 * self.count * batch_count / tot_count
self.running_mean.copy_(new_mean)
self.running_var.copy_(M2 / tot_count)
self.count.copy_(tot_count)
# 执行标准化和 Clipping
state = (state - self.running_mean) / torch.sqrt(self.running_var + self.epsilon)
state = torch.clamp(state, -self.clip_obs, self.clip_obs)
encoded_tensor_list.append(self.state_net(state))
if "rgb" in observations:
obs_rgb = observations["rgb"].float().permute(0, 3, 1, 2) / 255.0
rgb_v1 = obs_rgb[:, :3, :, :]
rgb_v2 = obs_rgb[:, 3:, :, :]
feat_v1 = self.rgb_final1(self.cnn_v1(rgb_v1))
feat_v2 = self.rgb_final2(self.cnn_v2(rgb_v2))
rgb_features = torch.cat([feat_v1, feat_v2], dim=1)
encoded_tensor_list.append(rgb_features)
return torch.cat(encoded_tensor_list, dim=1)
class Agent(nn.Module):
def __init__(self, envs, sample_obs, **kwargs):
super().__init__()
self.feature_net = NatureCNN(sample_obs=sample_obs)
latent_size = self.feature_net.out_features
action_dim = np.prod(envs.single_action_space.shape)
# Critic Network
self.critic = nn.Sequential(
layer_init(nn.Linear(latent_size, 512)),
nn.ReLU(inplace=True),
layer_init(nn.Linear(512, 1)),
)
# Actor Network
self.actor_mean = nn.Sequential(
layer_init(nn.Linear(latent_size, 512)),
nn.ReLU(inplace=True),
layer_init(nn.Linear(512, action_dim), std=0.01*np.sqrt(2)),
)
self.actor_logstd = nn.Parameter(torch.ones(1, action_dim) * -0.5)
# Find the range of actions
assert isinstance(envs.single_action_space, gym.spaces.Box)
action_low = envs.single_action_space.low
action_high = envs.single_action_space.high
self.register_buffer(
"action_low", torch.from_numpy(action_low).float()
)
self.register_buffer(
"action_high", torch.from_numpy(action_high).float()
)
def get_features(self, x):
return self.feature_net(x)
def get_value(self, x):
features = self.feature_net(x)
return self.critic(features)
def denormalize_action(self, a):
"""
Maps action from [-1, 1] to [low, high]
"""
return (a + 1) / 2 * (self.action_high - self.action_low) + self.action_low
def _squashed_normal(self, mean):
logstd = self.actor_logstd.expand_as(mean)
std = torch.exp(logstd)
base_dist = Normal(mean, std)
# reparameterization trick
raw = base_dist.rsample()
squashed = torch.tanh(raw)
# log-prob with tanh correction
logprob = base_dist.log_prob(raw)
logprob -= torch.log(1 - squashed.pow(2) + 1e-6)
logprob = logprob.sum(1)
entropy = base_dist.entropy().sum(1)
return squashed, logprob, entropy
def _logprob_squashed(self, mean: torch.Tensor, action_squashed: torch.Tensor):
# Inverse tanh in a numerically stable way
atanh = 0.5 * (torch.log1p(action_squashed + 1e-6) - torch.log1p(-action_squashed + 1e-6))
std = torch.exp(self.actor_logstd.expand_as(mean))
base_dist = Normal(mean, std)
logprob = base_dist.log_prob(atanh) - torch.log(1 - action_squashed.pow(2) + 1e-6)
return logprob.sum(1)
def get_action(self, x, deterministic=False):
features = self.feature_net(x)
action_mean = self.actor_mean(features)
if deterministic:
squashed = torch.tanh(action_mean)
else:
squashed, _, _ = self._squashed_normal(action_mean)
action = self.denormalize_action(squashed)
return action
def get_action_and_value(self, x, action=None):
x = self.feature_net(x)
action_mean = self.actor_mean(x)
if action is None:
squashed, logprob, entropy = self._squashed_normal(action_mean)
else:
squashed = action
logprob = self._logprob_squashed(action_mean, squashed)
# entropy of the base Gaussian (independent of the specific action)
entropy = Normal(action_mean, torch.exp(self.actor_logstd.expand_as(action_mean))).entropy().sum(1)
action_real = self.denormalize_action(squashed)
return squashed, logprob, entropy, self.critic(x), action_real
class Logger:
def __init__(self, log_wandb=False, tensorboard: SummaryWriter = None) -> None:
self.writer = tensorboard
self.log_wandb = log_wandb
def add_scalar(self, tag, scalar_value, step):
if self.log_wandb:
wandb.log({tag: scalar_value}, step=step)
self.writer.add_scalar(tag, scalar_value, step)
def close(self):
self.writer.close()
if __name__ == "__main__":
args = tyro.cli(Args)
args.batch_size = int(args.num_envs * args.num_steps)
args.minibatch_size = int(args.batch_size // args.num_minibatches)
args.num_iterations = args.total_timesteps // args.batch_size
if args.exp_name is None:
args.exp_name = os.path.basename(__file__)[: -len(".py")]
run_name = f"{args.env_id}__{args.exp_name}__{args.seed}__{int(time.time())}"
else:
run_name = args.exp_name
# TRY NOT TO MODIFY: seeding
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = args.torch_deterministic
device = torch.device("cuda" if torch.cuda.is_available() and args.cuda else "cpu")
# env setup
env_kwargs = dict(obs_mode=args.obs_mode, robot_uids="so101", render_mode=args.render_mode, sim_backend="physx_cuda")
if args.control_mode is not None:
env_kwargs["control_mode"] = args.control_mode
eval_envs = gym.make(args.env_id, num_envs=args.num_eval_envs, reconfiguration_freq=args.eval_reconfiguration_freq, **env_kwargs)
envs = gym.make(args.env_id, num_envs=args.num_envs if not args.evaluate else 1, reconfiguration_freq=args.reconfiguration_freq, **env_kwargs)
# rgbd obs mode returns a dict of data, we flatten it so there is just a rgbd key and state key
envs = FlattenRGBDObservationWrapper(envs, rgb=True, depth=False, state=args.include_state)
eval_envs = FlattenRGBDObservationWrapper(eval_envs, rgb=True, depth=False, state=args.include_state)
if isinstance(envs.action_space, gym.spaces.Dict):
envs = FlattenActionSpaceWrapper(envs)
eval_envs = FlattenActionSpaceWrapper(eval_envs)
if args.capture_video:
eval_output_dir = f"runs/{run_name}/videos"
if args.evaluate:
eval_output_dir = f"{os.path.dirname(args.checkpoint)}/test_videos"
print(f"Saving eval videos to {eval_output_dir}")
if args.save_train_video_freq is not None:
save_video_trigger = lambda x : (x // args.num_steps) % args.save_train_video_freq == 0
envs = RecordEpisode(envs, output_dir=f"runs/{run_name}/train_videos", save_trajectory=False, save_video_trigger=save_video_trigger, max_steps_per_video=args.num_steps, video_fps=30)
eval_envs = RecordEpisode(eval_envs, output_dir=eval_output_dir, save_trajectory=args.evaluate, trajectory_name="trajectory", max_steps_per_video=args.num_eval_steps, video_fps=30)
envs = ManiSkillVectorEnv(envs, args.num_envs, ignore_terminations=not args.partial_reset, record_metrics=True)
eval_envs = ManiSkillVectorEnv(eval_envs, args.num_eval_envs, ignore_terminations=not args.eval_partial_reset, record_metrics=True)
assert isinstance(envs.single_action_space, gym.spaces.Box), "only continuous action space is supported"
max_episode_steps = gym_utils.find_max_episode_steps_value(envs._env)
logger = None
if not args.evaluate:
print("Running training")
if args.track:
import wandb
config = vars(args)
config["env_cfg"] = dict(**env_kwargs, num_envs=args.num_envs, env_id=args.env_id, reward_mode="normalized_dense", env_horizon=max_episode_steps, partial_reset=args.partial_reset)
config["eval_env_cfg"] = dict(**env_kwargs, num_envs=args.num_eval_envs, env_id=args.env_id, reward_mode="normalized_dense", env_horizon=max_episode_steps, partial_reset=args.partial_reset)
wandb.init(
project=args.wandb_project_name,
entity=args.wandb_entity,
sync_tensorboard=False,
config=config,
name=run_name,
save_code=True,
group=args.wandb_group,
tags=["ppo", "walltime_efficient"]
)
writer = SummaryWriter(f"runs/{run_name}")
writer.add_text(
"hyperparameters",
"|param|value|\n|-|-|\n%s" % ("\n".join([f"|{key}|{value}|" for key, value in vars(args).items()])),
)
logger = Logger(log_wandb=args.track, tensorboard=writer)
else:
print("Running evaluation")
# ALGO Logic: Storage setup
obs = DictArray((args.num_steps, args.num_envs), envs.single_observation_space, device='cpu')
# obs = DictArray((args.num_steps, args.num_envs), envs.single_observation_space, device='cuda')
actions = torch.zeros((args.num_steps, args.num_envs) + envs.single_action_space.shape).to(device)
logprobs = torch.zeros((args.num_steps, args.num_envs)).to(device)
rewards = torch.zeros((args.num_steps, args.num_envs)).to(device)
dones = torch.zeros((args.num_steps, args.num_envs)).to(device)
values = torch.zeros((args.num_steps, args.num_envs)).to(device)
# TRY NOT TO MODIFY: start the game
global_step = 0
start_time = time.time()
next_obs, _ = envs.reset(seed=args.seed)
# import cv2
# next_obs_rgb = next_obs["rgb"]
# pic_a = next_obs_rgb[0, :, :, :3].cpu().numpy()
# pic_b = next_obs_rgb[0, :, :, 3:].cpu().numpy()
# # uint8
# pic_a_bgr = cv2.cvtColor(pic_a, cv2.COLOR_RGB2BGR)
# pic_b_bgr = cv2.cvtColor(pic_b, cv2.COLOR_RGB2BGR)
# path_a = f"./obs_view/main.png"
# path_b = f"./obs_view/wrist.png"
# cv2.imwrite(path_a, pic_a_bgr)
# cv2.imwrite(path_b, pic_b_bgr)
eval_obs, _ = eval_envs.reset(seed=args.seed)
next_done = torch.zeros(args.num_envs, device=device)
print(f"####")
print(f"args.num_iterations={args.num_iterations} args.num_envs={args.num_envs} args.num_eval_envs={args.num_eval_envs}")
print(f"args.minibatch_size={args.minibatch_size} args.batch_size={args.batch_size} args.update_epochs={args.update_epochs}")
print(f"####")
agent = Agent(envs, sample_obs=next_obs).to(device)
optimizer = optim.Adam(agent.parameters(), lr=args.learning_rate, eps=1e-5)
if args.checkpoint:
agent.load_state_dict(torch.load(args.checkpoint))
cumulative_times = defaultdict(float)
for iteration in range(1, args.num_iterations + 1):
print(f"Epoch: {iteration}, global_step={global_step}")
final_values = torch.zeros((args.num_steps, args.num_envs), device=device)
agent.eval()
if iteration % args.eval_freq == 1:
print("Evaluating")
stime = time.perf_counter()
eval_obs, _ = eval_envs.reset()
eval_metrics = defaultdict(list)
num_episodes = 0
for _ in range(args.num_eval_steps):
with torch.no_grad():
eval_obs, eval_rew, eval_terminations, eval_truncations, eval_infos = eval_envs.step(agent.get_action(eval_obs, deterministic=True))
if "final_info" in eval_infos:
mask = eval_infos["_final_info"]
num_episodes += mask.sum()
for k, v in eval_infos["final_info"]["episode"].items():
eval_metrics[k].append(v)
print(f"Evaluated {args.num_eval_steps * args.num_eval_envs} steps resulting in {num_episodes} episodes")
for k, v in eval_metrics.items():
mean = torch.stack(v).float().mean()
if logger is not None:
logger.add_scalar(f"eval/{k}", mean, global_step)
print(f"eval_{k}_mean={mean}")
if logger is not None:
eval_time = time.perf_counter() - stime
cumulative_times["eval_time"] += eval_time
logger.add_scalar("time/eval_time", eval_time, global_step)
if args.evaluate:
break
if args.save_model and iteration % args.eval_freq == 1:
model_path = f"runs/{run_name}/ckpt_{iteration}.pt"
torch.save(agent.state_dict(), model_path)
print(f"model saved to {model_path}")
# Annealing the rate if instructed to do so.
if args.anneal_lr:
frac = 1.0 - (iteration - 1.0) / args.num_iterations
lrnow = frac * args.learning_rate
optimizer.param_groups[0]["lr"] = lrnow
rollout_time = time.perf_counter()
for step in range(0, args.num_steps):
global_step += args.num_envs
obs[step] = next_obs
dones[step] = next_done
# ALGO LOGIC: action logic
with torch.no_grad():
squashed, logprob, _, value, action_real = agent.get_action_and_value(next_obs)
values[step] = value.flatten()
actions[step] = squashed
logprobs[step] = logprob
# TRY NOT TO MODIFY: execute the game and log data.
next_obs, reward, terminations, truncations, infos = envs.step(action_real)
next_done = torch.logical_or(terminations, truncations).to(torch.float32)
rewards[step] = reward.view(-1) * args.reward_scale
if "final_info" in infos:
final_info = infos["final_info"]
done_mask = infos["_final_info"]
for k, v in final_info["episode"].items():
logger.add_scalar(f"train/{k}", v[done_mask].float().mean(), global_step)
for k in infos["final_observation"]:
infos["final_observation"][k] = infos["final_observation"][k][done_mask]
with torch.no_grad():
final_values[step, torch.arange(args.num_envs, device=device)[done_mask]] = agent.get_value(infos["final_observation"]).view(-1)
# del infos
# if "final_info" in locals(): del final_info
rollout_time = time.perf_counter() - rollout_time
cumulative_times["rollout_time"] += rollout_time
# bootstrap value according to termination and truncation
with torch.no_grad():
next_value = agent.get_value(next_obs).reshape(1, -1)
advantages = torch.zeros_like(rewards).to(device)
lastgaelam = 0
for t in reversed(range(args.num_steps)):
if t == args.num_steps - 1:
next_not_done = 1.0 - next_done
nextvalues = next_value
else:
next_not_done = 1.0 - dones[t + 1]
nextvalues = values[t + 1]
real_next_values = next_not_done * nextvalues + final_values[t] # t instead of t+1
# next_not_done means nextvalues is computed from the correct next_obs
# if next_not_done is 1, final_values is always 0
# if next_not_done is 0, then use final_values, which is computed according to bootstrap_at_done
if args.finite_horizon_gae:
"""
See GAE paper equation(16) line 1, we will compute the GAE based on this line only
1 *( -V(s_t) + r_t + gamma * V(s_{t+1}) )
lambda *( -V(s_t) + r_t + gamma * r_{t+1} + gamma^2 * V(s_{t+2}) )
lambda^2 *( -V(s_t) + r_t + gamma * r_{t+1} + gamma^2 * r_{t+2} + ... )
lambda^3 *( -V(s_t) + r_t + gamma * r_{t+1} + gamma^2 * r_{t+2} + gamma^3 * r_{t+3}
We then normalize it by the sum of the lambda^i (instead of 1-lambda)
"""
if t == args.num_steps - 1: # initialize
lam_coef_sum = 0.
reward_term_sum = 0. # the sum of the second term
value_term_sum = 0. # the sum of the third term
lam_coef_sum = lam_coef_sum * next_not_done
reward_term_sum = reward_term_sum * next_not_done
value_term_sum = value_term_sum * next_not_done
lam_coef_sum = 1 + args.gae_lambda * lam_coef_sum
reward_term_sum = args.gae_lambda * args.gamma * reward_term_sum + lam_coef_sum * rewards[t]
value_term_sum = args.gae_lambda * args.gamma * value_term_sum + args.gamma * real_next_values
advantages[t] = (reward_term_sum + value_term_sum) / lam_coef_sum - values[t]
else:
delta = rewards[t] + args.gamma * real_next_values - values[t]
advantages[t] = lastgaelam = delta + args.gamma * args.gae_lambda * next_not_done * lastgaelam # Here actually we should use next_not_terminated, but we don't have lastgamlam if terminated
returns = advantages + values
# flatten the batch
b_obs = obs.reshape((-1,))
b_logprobs = logprobs.reshape(-1)
b_actions = actions.reshape((-1,) + envs.single_action_space.shape)
b_advantages = advantages.reshape(-1)
b_returns = returns.reshape(-1)
b_values = values.reshape(-1)
# Optimizing the policy and value network
# gc.collect()
# torch.cuda.empty_cache()
agent.train()
b_inds = np.arange(args.batch_size)
clipfracs = []
update_time = time.perf_counter()
for epoch in range(args.update_epochs):
np.random.shuffle(b_inds)
for start in range(0, args.batch_size, args.minibatch_size):
end = start + args.minibatch_size
mb_inds = b_inds[start:end]
# _, newlogprob, entropy, newvalue, _ = agent.get_action_and_value(b_obs[mb_inds], b_actions[mb_inds])
obs_batch = b_obs[mb_inds]
obs_batch_gpu = {}
for k, v in obs_batch.items():
if isinstance(v, torch.Tensor):
obs_batch_gpu[k] = v.to(device)
else:
obs_batch_gpu[k] = v
_, newlogprob, entropy, newvalue, _ = agent.get_action_and_value(
obs_batch_gpu,
b_actions[mb_inds]
)
logratio = newlogprob - b_logprobs[mb_inds]
ratio = logratio.exp()
with torch.no_grad():
# calculate approx_kl http://joschu.net/blog/kl-approx.html
old_approx_kl = (-logratio).mean()
approx_kl = ((ratio - 1) - logratio).mean()
clipfracs += [((ratio - 1.0).abs() > args.clip_coef).float().mean().item()]
if args.target_kl is not None and approx_kl > args.target_kl:
break
mb_advantages = b_advantages[mb_inds]
if args.norm_adv:
mb_advantages = (mb_advantages - mb_advantages.mean()) / (mb_advantages.std() + 1e-8)
# Policy loss
pg_loss1 = -mb_advantages * ratio
pg_loss2 = -mb_advantages * torch.clamp(ratio, 1 - args.clip_coef, 1 + args.clip_coef)
pg_loss = torch.max(pg_loss1, pg_loss2).mean()
# Value loss
newvalue = newvalue.view(-1)
if args.clip_vloss:
v_loss_unclipped = (newvalue - b_returns[mb_inds]) ** 2
v_clipped = b_values[mb_inds] + torch.clamp(
newvalue - b_values[mb_inds],
-args.clip_coef,
args.clip_coef,
)
v_loss_clipped = (v_clipped - b_returns[mb_inds]) ** 2
v_loss_max = torch.max(v_loss_unclipped, v_loss_clipped)
v_loss = 0.5 * v_loss_max.mean()
else:
v_loss = 0.5 * ((newvalue - b_returns[mb_inds]) ** 2).mean()
entropy_loss = entropy.mean()
loss = pg_loss - args.ent_coef * entropy_loss + v_loss * args.vf_coef
optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(agent.parameters(), args.max_grad_norm)
optimizer.step()
if args.target_kl is not None and approx_kl > args.target_kl:
break
update_time = time.perf_counter() - update_time
cumulative_times["update_time"] += update_time
y_pred, y_true = b_values.cpu().numpy(), b_returns.cpu().numpy()
var_y = np.var(y_true)
explained_var = np.nan if var_y == 0 else 1 - np.var(y_true - y_pred) / var_y
logger.add_scalar("charts/learning_rate", optimizer.param_groups[0]["lr"], global_step)
logger.add_scalar("losses/value_loss", v_loss.item(), global_step)
logger.add_scalar("losses/policy_loss", pg_loss.item(), global_step)
logger.add_scalar("losses/entropy", entropy_loss.item(), global_step)
logger.add_scalar("losses/old_approx_kl", old_approx_kl.item(), global_step)
logger.add_scalar("losses/approx_kl", approx_kl.item(), global_step)
logger.add_scalar("losses/clipfrac", np.mean(clipfracs), global_step)
logger.add_scalar("losses/explained_variance", explained_var, global_step)
print("SPS:", int(global_step / (time.time() - start_time)))
logger.add_scalar("charts/SPS", int(global_step / (time.time() - start_time)), global_step)
logger.add_scalar("time/step", global_step, global_step)
logger.add_scalar("time/update_time", update_time, global_step)
logger.add_scalar("time/rollout_time", rollout_time, global_step)
logger.add_scalar("time/rollout_fps", args.num_envs * args.num_steps / rollout_time, global_step)
for k, v in cumulative_times.items():
logger.add_scalar(f"time/total_{k}", v, global_step)
logger.add_scalar("time/total_rollout+update_time", cumulative_times["rollout_time"] + cumulative_times["update_time"], global_step)
if args.save_model and not args.evaluate:
model_path = f"runs/{run_name}/final_ckpt.pt"
torch.save(agent.state_dict(), model_path)
print(f"model saved to {model_path}")
envs.close()
if logger is not None: logger.close()