-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathppo_atari.py
300 lines (259 loc) · 10.6 KB
/
ppo_atari.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
# 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.
"""
This script reproduces the Proximal Policy Optimization (PPO) Algorithm
results from Schulman et al. 2017 for the Atari Environments.
"""
from __future__ import annotations
import warnings
import hydra
from torchrl._utils import compile_with_warmup
@hydra.main(config_path="", config_name="config_atari", version_base="1.1")
def main(cfg: DictConfig): # noqa: F821
import torch.optim
import tqdm
from tensordict import TensorDict
from tensordict.nn import CudaGraphModule
from torchrl._utils import timeit
from torchrl.collectors import SyncDataCollector
from torchrl.data import LazyTensorStorage, TensorDictReplayBuffer
from torchrl.data.replay_buffers.samplers import SamplerWithoutReplacement
from torchrl.envs import ExplorationType, set_exploration_type
from torchrl.objectives import ClipPPOLoss
from torchrl.objectives.value.advantages import GAE
from torchrl.record import VideoRecorder
from torchrl.record.loggers import generate_exp_name, get_logger
from utils_atari import eval_model, make_parallel_env, make_ppo_models
torch.set_float32_matmul_precision("high")
device = cfg.optim.device
if device in ("", None):
if torch.cuda.is_available():
device = "cuda:0"
else:
device = "cpu"
device = torch.device(device)
# Correct for frame_skip
frame_skip = 4
total_frames = cfg.collector.total_frames // frame_skip
frames_per_batch = cfg.collector.frames_per_batch // frame_skip
mini_batch_size = cfg.loss.mini_batch_size // frame_skip
test_interval = cfg.logger.test_interval // frame_skip
compile_mode = None
if cfg.compile.compile:
compile_mode = cfg.compile.compile_mode
if compile_mode in ("", None):
if cfg.compile.cudagraphs:
compile_mode = "default"
else:
compile_mode = "reduce-overhead"
# Create models (check utils_atari.py)
actor, critic = make_ppo_models(cfg.env.env_name, device=device)
# Create collector
collector = SyncDataCollector(
create_env_fn=make_parallel_env(cfg.env.env_name, cfg.env.num_envs, device),
policy=actor,
frames_per_batch=frames_per_batch,
total_frames=total_frames,
device=device,
max_frames_per_traj=-1,
compile_policy={"mode": compile_mode, "warmup": 1} if compile_mode else False,
cudagraph_policy=cfg.compile.cudagraphs,
)
# Create data buffer
sampler = SamplerWithoutReplacement()
data_buffer = TensorDictReplayBuffer(
storage=LazyTensorStorage(
frames_per_batch, compilable=cfg.compile.compile, device=device
),
sampler=sampler,
batch_size=mini_batch_size,
compilable=cfg.compile.compile,
)
# Create loss and adv modules
adv_module = GAE(
gamma=cfg.loss.gamma,
lmbda=cfg.loss.gae_lambda,
value_network=critic,
average_gae=False,
device=device,
vectorized=not cfg.compile.compile,
)
loss_module = ClipPPOLoss(
actor_network=actor,
critic_network=critic,
clip_epsilon=cfg.loss.clip_epsilon,
loss_critic_type=cfg.loss.loss_critic_type,
entropy_coef=cfg.loss.entropy_coef,
critic_coef=cfg.loss.critic_coef,
normalize_advantage=True,
)
# use end-of-life as done key
adv_module.set_keys(done="end-of-life", terminated="end-of-life")
loss_module.set_keys(done="end-of-life", terminated="end-of-life")
# Create optimizer
optim = torch.optim.Adam(
loss_module.parameters(),
lr=cfg.optim.lr,
weight_decay=cfg.optim.weight_decay,
eps=cfg.optim.eps,
)
# Create logger
logger = None
if cfg.logger.backend:
exp_name = generate_exp_name("PPO", f"{cfg.logger.exp_name}_{cfg.env.env_name}")
logger = get_logger(
cfg.logger.backend,
logger_name="ppo",
experiment_name=exp_name,
wandb_kwargs={
"config": dict(cfg),
"project": cfg.logger.project_name,
"group": cfg.logger.group_name,
},
)
logger_video = cfg.logger.video
else:
logger_video = False
# Create test environment
test_env = make_parallel_env(cfg.env.env_name, 1, device, is_test=True)
if logger_video:
test_env = test_env.append_transform(
VideoRecorder(logger, tag="rendering/test", in_keys=["pixels_int"])
)
test_env.eval()
# Main loop
collected_frames = 0
num_network_updates = torch.zeros((), dtype=torch.int64, device=device)
pbar = tqdm.tqdm(total=total_frames)
num_mini_batches = frames_per_batch // mini_batch_size
total_network_updates = (
(total_frames // frames_per_batch) * cfg.loss.ppo_epochs * num_mini_batches
)
def update(batch, num_network_updates):
optim.zero_grad(set_to_none=True)
# Linearly decrease the learning rate and clip epsilon
alpha = torch.ones((), device=device)
if cfg_optim_anneal_lr:
alpha = 1 - (num_network_updates / total_network_updates)
for group in optim.param_groups:
group["lr"] = cfg_optim_lr * alpha
if cfg_loss_anneal_clip_eps:
loss_module.clip_epsilon.copy_(cfg_loss_clip_epsilon * alpha)
num_network_updates = num_network_updates + 1
# Get a data batch
batch = batch.to(device, non_blocking=True)
# Forward pass PPO loss
loss = loss_module(batch)
loss_sum = loss["loss_critic"] + loss["loss_objective"] + loss["loss_entropy"]
# Backward pass
loss_sum.backward()
torch.nn.utils.clip_grad_norm_(
loss_module.parameters(), max_norm=cfg_optim_max_grad_norm
)
# Update the networks
optim.step()
return loss.detach().set("alpha", alpha), num_network_updates
if cfg.compile.compile:
update = compile_with_warmup(update, mode=compile_mode, warmup=1)
adv_module = compile_with_warmup(adv_module, mode=compile_mode, warmup=1)
if cfg.compile.cudagraphs:
warnings.warn(
"CudaGraphModule is experimental and may lead to silently wrong results. Use with caution.",
category=UserWarning,
)
update = CudaGraphModule(update, in_keys=[], out_keys=[], warmup=5)
adv_module = CudaGraphModule(adv_module)
# extract cfg variables
cfg_loss_ppo_epochs = cfg.loss.ppo_epochs
cfg_optim_anneal_lr = cfg.optim.anneal_lr
cfg_optim_lr = cfg.optim.lr
cfg_loss_anneal_clip_eps = cfg.loss.anneal_clip_epsilon
cfg_loss_clip_epsilon = cfg.loss.clip_epsilon
cfg_logger_num_test_episodes = cfg.logger.num_test_episodes
cfg_optim_max_grad_norm = cfg.optim.max_grad_norm
cfg.loss.clip_epsilon = cfg_loss_clip_epsilon
losses = TensorDict(batch_size=[cfg_loss_ppo_epochs, num_mini_batches])
collector_iter = iter(collector)
total_iter = len(collector)
for i in range(total_iter):
timeit.printevery(1000, total_iter, erase=True)
with timeit("collecting"):
data = next(collector_iter)
metrics_to_log = {}
frames_in_batch = data.numel()
collected_frames += frames_in_batch * frame_skip
pbar.update(frames_in_batch)
# Get training rewards and episode lengths
episode_rewards = data["next", "episode_reward"][data["next", "terminated"]]
if len(episode_rewards) > 0:
episode_length = data["next", "step_count"][data["next", "terminated"]]
metrics_to_log.update(
{
"train/reward": episode_rewards.mean().item(),
"train/episode_length": episode_length.sum().item()
/ len(episode_length),
}
)
with timeit("training"):
for j in range(cfg_loss_ppo_epochs):
# Compute GAE
with torch.no_grad(), timeit("adv"):
torch.compiler.cudagraph_mark_step_begin()
data = adv_module(data)
if compile_mode:
data = data.clone()
with timeit("rb - extend"):
# Update the data buffer
data_reshape = data.reshape(-1)
data_buffer.extend(data_reshape)
for k, batch in enumerate(data_buffer):
with timeit("update"):
torch.compiler.cudagraph_mark_step_begin()
loss, num_network_updates = update(
batch, num_network_updates=num_network_updates
)
loss = loss.clone()
num_network_updates = num_network_updates.clone()
losses[j, k] = loss.select(
"loss_critic", "loss_entropy", "loss_objective"
)
# Get training losses and times
losses_mean = losses.apply(lambda x: x.float().mean(), batch_size=[])
for key, value in losses_mean.items():
metrics_to_log.update({f"train/{key}": value.item()})
metrics_to_log.update(
{
"train/lr": loss["alpha"] * cfg_optim_lr,
"train/clip_epsilon": loss["alpha"] * cfg_loss_clip_epsilon,
}
)
# Get test rewards
with torch.no_grad(), set_exploration_type(
ExplorationType.DETERMINISTIC
), timeit("eval"):
if ((i - 1) * frames_in_batch * frame_skip) // test_interval < (
i * frames_in_batch * frame_skip
) // test_interval:
actor.eval()
test_rewards = eval_model(
actor, test_env, num_episodes=cfg_logger_num_test_episodes
)
metrics_to_log.update(
{
"eval/reward": test_rewards.mean(),
}
)
actor.train()
if logger:
metrics_to_log.update(timeit.todict(prefix="time"))
metrics_to_log["time/speed"] = pbar.format_dict["rate"]
for key, value in metrics_to_log.items():
logger.log_scalar(key, value, collected_frames)
collector.update_policy_weights_()
collector.shutdown()
if not test_env.is_closed:
test_env.close()
if __name__ == "__main__":
main()