-
Notifications
You must be signed in to change notification settings - Fork 657
/
ppo_atari_envpool_xla_jax.py
452 lines (407 loc) · 18 KB
/
ppo_atari_envpool_xla_jax.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
# docs and experiment results can be found at https://docs.cleanrl.dev/rl-algorithms/ppo/#ppo_atari_envpool_xla_jaxpy
import os
import random
import time
from dataclasses import dataclass
from typing import Sequence
import envpool
import flax
import flax.linen as nn
import gym
import jax
import jax.numpy as jnp
import numpy as np
import optax
import tyro
from flax.linen.initializers import constant, orthogonal
from flax.training.train_state import TrainState
from torch.utils.tensorboard import SummaryWriter
# Fix weird OOM https://github.com/google/jax/discussions/6332#discussioncomment-1279991
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "0.6"
# Fix CUDNN non-determinisim; https://github.com/google/jax/issues/4823#issuecomment-952835771
os.environ["TF_XLA_FLAGS"] = "--xla_gpu_autotune_level=2 --xla_gpu_deterministic_reductions"
os.environ["TF_CUDNN DETERMINISTIC"] = "1"
@dataclass
class Args:
exp_name: str = os.path.basename(__file__)[: -len(".py")]
"""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 = False
"""if toggled, this experiment will be tracked with Weights and Biases"""
wandb_project_name: str = "cleanRL"
"""the wandb's project name"""
wandb_entity: str = None
"""the entity (team) of wandb's project"""
capture_video: bool = False
"""whether to capture videos of the agent performances (check out `videos` folder)"""
# Algorithm specific arguments
env_id: str = "Breakout-v5"
"""the id of the environment"""
total_timesteps: int = 10000000
"""total timesteps of the experiments"""
learning_rate: float = 2.5e-4
"""the learning rate of the optimizer"""
num_envs: int = 8
"""the number of parallel game environments"""
num_steps: int = 128
"""the number of steps to run in each environment per policy rollout"""
anneal_lr: bool = True
"""Toggle learning rate annealing for policy and value networks"""
gamma: float = 0.99
"""the discount factor gamma"""
gae_lambda: float = 0.95
"""the lambda for the general advantage estimation"""
num_minibatches: int = 4
"""the number of mini-batches"""
update_epochs: int = 4
"""the K epochs to update the policy"""
norm_adv: bool = True
"""Toggles advantages normalization"""
clip_coef: float = 0.1
"""the surrogate clipping coefficient"""
clip_vloss: bool = True
"""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 = None
"""the target KL divergence threshold"""
# 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)"""
class Network(nn.Module):
@nn.compact
def __call__(self, x):
x = jnp.transpose(x, (0, 2, 3, 1))
x = x / (255.0)
x = nn.Conv(
32,
kernel_size=(8, 8),
strides=(4, 4),
padding="VALID",
kernel_init=orthogonal(np.sqrt(2)),
bias_init=constant(0.0),
)(x)
x = nn.relu(x)
x = nn.Conv(
64,
kernel_size=(4, 4),
strides=(2, 2),
padding="VALID",
kernel_init=orthogonal(np.sqrt(2)),
bias_init=constant(0.0),
)(x)
x = nn.relu(x)
x = nn.Conv(
64,
kernel_size=(3, 3),
strides=(1, 1),
padding="VALID",
kernel_init=orthogonal(np.sqrt(2)),
bias_init=constant(0.0),
)(x)
x = nn.relu(x)
x = x.reshape((x.shape[0], -1))
x = nn.Dense(512, kernel_init=orthogonal(np.sqrt(2)), bias_init=constant(0.0))(x)
x = nn.relu(x)
return x
class Critic(nn.Module):
@nn.compact
def __call__(self, x):
return nn.Dense(1, kernel_init=orthogonal(1), bias_init=constant(0.0))(x)
class Actor(nn.Module):
action_dim: Sequence[int]
@nn.compact
def __call__(self, x):
return nn.Dense(self.action_dim, kernel_init=orthogonal(0.01), bias_init=constant(0.0))(x)
@flax.struct.dataclass
class AgentParams:
network_params: flax.core.FrozenDict
actor_params: flax.core.FrozenDict
critic_params: flax.core.FrozenDict
@flax.struct.dataclass
class Storage:
obs: jnp.array
actions: jnp.array
logprobs: jnp.array
dones: jnp.array
values: jnp.array
advantages: jnp.array
returns: jnp.array
rewards: jnp.array
@flax.struct.dataclass
class EpisodeStatistics:
episode_returns: jnp.array
episode_lengths: jnp.array
returned_episode_returns: jnp.array
returned_episode_lengths: jnp.array
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
run_name = f"{args.env_id}__{args.exp_name}__{args.seed}__{int(time.time())}"
if args.track:
import wandb
wandb.init(
project=args.wandb_project_name,
entity=args.wandb_entity,
sync_tensorboard=True,
config=vars(args),
name=run_name,
monitor_gym=True,
save_code=True,
)
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()])),
)
# TRY NOT TO MODIFY: seeding
random.seed(args.seed)
np.random.seed(args.seed)
key = jax.random.PRNGKey(args.seed)
key, network_key, actor_key, critic_key = jax.random.split(key, 4)
# env setup
envs = envpool.make(
args.env_id,
env_type="gym",
num_envs=args.num_envs,
episodic_life=True,
reward_clip=True,
seed=args.seed,
)
envs.num_envs = args.num_envs
envs.single_action_space = envs.action_space
envs.single_observation_space = envs.observation_space
envs.is_vector_env = True
episode_stats = EpisodeStatistics(
episode_returns=jnp.zeros(args.num_envs, dtype=jnp.float32),
episode_lengths=jnp.zeros(args.num_envs, dtype=jnp.int32),
returned_episode_returns=jnp.zeros(args.num_envs, dtype=jnp.float32),
returned_episode_lengths=jnp.zeros(args.num_envs, dtype=jnp.int32),
)
handle, recv, send, step_env = envs.xla()
def step_env_wrappeed(episode_stats, handle, action):
handle, (next_obs, reward, next_done, info) = step_env(handle, action)
new_episode_return = episode_stats.episode_returns + info["reward"]
new_episode_length = episode_stats.episode_lengths + 1
episode_stats = episode_stats.replace(
episode_returns=(new_episode_return) * (1 - info["terminated"]) * (1 - info["TimeLimit.truncated"]),
episode_lengths=(new_episode_length) * (1 - info["terminated"]) * (1 - info["TimeLimit.truncated"]),
# only update the `returned_episode_returns` if the episode is done
returned_episode_returns=jnp.where(
info["terminated"] + info["TimeLimit.truncated"], new_episode_return, episode_stats.returned_episode_returns
),
returned_episode_lengths=jnp.where(
info["terminated"] + info["TimeLimit.truncated"], new_episode_length, episode_stats.returned_episode_lengths
),
)
return episode_stats, handle, (next_obs, reward, next_done, info)
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"
def linear_schedule(count):
# anneal learning rate linearly after one training iteration which contains
# (args.num_minibatches * args.update_epochs) gradient updates
frac = 1.0 - (count // (args.num_minibatches * args.update_epochs)) / args.num_iterations
return args.learning_rate * frac
network = Network()
actor = Actor(action_dim=envs.single_action_space.n)
critic = Critic()
network_params = network.init(network_key, np.array([envs.single_observation_space.sample()]))
agent_state = TrainState.create(
apply_fn=None,
params=AgentParams(
network_params,
actor.init(actor_key, network.apply(network_params, np.array([envs.single_observation_space.sample()]))),
critic.init(critic_key, network.apply(network_params, np.array([envs.single_observation_space.sample()]))),
),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.inject_hyperparams(optax.adam)(
learning_rate=linear_schedule if args.anneal_lr else args.learning_rate, eps=1e-5
),
),
)
network.apply = jax.jit(network.apply)
actor.apply = jax.jit(actor.apply)
critic.apply = jax.jit(critic.apply)
# ALGO Logic: Storage setup
storage = Storage(
obs=jnp.zeros((args.num_steps, args.num_envs) + envs.single_observation_space.shape),
actions=jnp.zeros((args.num_steps, args.num_envs) + envs.single_action_space.shape, dtype=jnp.int32),
logprobs=jnp.zeros((args.num_steps, args.num_envs)),
dones=jnp.zeros((args.num_steps, args.num_envs)),
values=jnp.zeros((args.num_steps, args.num_envs)),
advantages=jnp.zeros((args.num_steps, args.num_envs)),
returns=jnp.zeros((args.num_steps, args.num_envs)),
rewards=jnp.zeros((args.num_steps, args.num_envs)),
)
@jax.jit
def get_action_and_value(
agent_state: TrainState,
next_obs: np.ndarray,
next_done: np.ndarray,
storage: Storage,
step: int,
key: jax.random.PRNGKey,
):
"""sample action, calculate value, logprob, entropy, and update storage"""
hidden = network.apply(agent_state.params.network_params, next_obs)
logits = actor.apply(agent_state.params.actor_params, hidden)
# sample action: Gumbel-softmax trick
# see https://stats.stackexchange.com/questions/359442/sampling-from-a-categorical-distribution
key, subkey = jax.random.split(key)
u = jax.random.uniform(subkey, shape=logits.shape)
action = jnp.argmax(logits - jnp.log(-jnp.log(u)), axis=1)
logprob = jax.nn.log_softmax(logits)[jnp.arange(action.shape[0]), action]
value = critic.apply(agent_state.params.critic_params, hidden)
storage = storage.replace(
obs=storage.obs.at[step].set(next_obs),
dones=storage.dones.at[step].set(next_done),
actions=storage.actions.at[step].set(action),
logprobs=storage.logprobs.at[step].set(logprob),
values=storage.values.at[step].set(value.squeeze()),
)
return storage, action, key
@jax.jit
def get_action_and_value2(
params: flax.core.FrozenDict,
x: np.ndarray,
action: np.ndarray,
):
"""calculate value, logprob of supplied `action`, and entropy"""
hidden = network.apply(params.network_params, x)
logits = actor.apply(params.actor_params, hidden)
logprob = jax.nn.log_softmax(logits)[jnp.arange(action.shape[0]), action]
# normalize the logits https://gregorygundersen.com/blog/2020/02/09/log-sum-exp/
logits = logits - jax.scipy.special.logsumexp(logits, axis=-1, keepdims=True)
logits = logits.clip(min=jnp.finfo(logits.dtype).min)
p_log_p = logits * jax.nn.softmax(logits)
entropy = -p_log_p.sum(-1)
value = critic.apply(params.critic_params, hidden).squeeze()
return logprob, entropy, value
@jax.jit
def compute_gae(
agent_state: TrainState,
next_obs: np.ndarray,
next_done: np.ndarray,
storage: Storage,
):
storage = storage.replace(advantages=storage.advantages.at[:].set(0.0))
next_value = critic.apply(
agent_state.params.critic_params, network.apply(agent_state.params.network_params, next_obs)
).squeeze()
lastgaelam = 0
for t in reversed(range(args.num_steps)):
if t == args.num_steps - 1:
nextnonterminal = 1.0 - next_done
nextvalues = next_value
else:
nextnonterminal = 1.0 - storage.dones[t + 1]
nextvalues = storage.values[t + 1]
delta = storage.rewards[t] + args.gamma * nextvalues * nextnonterminal - storage.values[t]
lastgaelam = delta + args.gamma * args.gae_lambda * nextnonterminal * lastgaelam
storage = storage.replace(advantages=storage.advantages.at[t].set(lastgaelam))
storage = storage.replace(returns=storage.advantages + storage.values)
return storage
@jax.jit
def update_ppo(
agent_state: TrainState,
storage: Storage,
key: jax.random.PRNGKey,
):
b_obs = storage.obs.reshape((-1,) + envs.single_observation_space.shape)
b_logprobs = storage.logprobs.reshape(-1)
b_actions = storage.actions.reshape((-1,) + envs.single_action_space.shape)
b_advantages = storage.advantages.reshape(-1)
b_returns = storage.returns.reshape(-1)
def ppo_loss(params, x, a, logp, mb_advantages, mb_returns):
newlogprob, entropy, newvalue = get_action_and_value2(params, x, a)
logratio = newlogprob - logp
ratio = jnp.exp(logratio)
approx_kl = ((ratio - 1) - logratio).mean()
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 * jnp.clip(ratio, 1 - args.clip_coef, 1 + args.clip_coef)
pg_loss = jnp.maximum(pg_loss1, pg_loss2).mean()
# Value loss
v_loss = 0.5 * ((newvalue - mb_returns) ** 2).mean()
entropy_loss = entropy.mean()
loss = pg_loss - args.ent_coef * entropy_loss + v_loss * args.vf_coef
return loss, (pg_loss, v_loss, entropy_loss, jax.lax.stop_gradient(approx_kl))
ppo_loss_grad_fn = jax.value_and_grad(ppo_loss, has_aux=True)
for _ in range(args.update_epochs):
key, subkey = jax.random.split(key)
b_inds = jax.random.permutation(subkey, args.batch_size, independent=True)
for start in range(0, args.batch_size, args.minibatch_size):
end = start + args.minibatch_size
mb_inds = b_inds[start:end]
(loss, (pg_loss, v_loss, entropy_loss, approx_kl)), grads = ppo_loss_grad_fn(
agent_state.params,
b_obs[mb_inds],
b_actions[mb_inds],
b_logprobs[mb_inds],
b_advantages[mb_inds],
b_returns[mb_inds],
)
agent_state = agent_state.apply_gradients(grads=grads)
return agent_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key
# TRY NOT TO MODIFY: start the game
global_step = 0
start_time = time.time()
next_obs = envs.reset()
next_done = np.zeros(args.num_envs)
@jax.jit
def rollout(agent_state, episode_stats, next_obs, next_done, storage, key, handle, global_step):
for step in range(0, args.num_steps):
global_step += args.num_envs
storage, action, key = get_action_and_value(agent_state, next_obs, next_done, storage, step, key)
# TRY NOT TO MODIFY: execute the game and log data.
episode_stats, handle, (next_obs, reward, next_done, _) = step_env_wrappeed(episode_stats, handle, action)
storage = storage.replace(rewards=storage.rewards.at[step].set(reward))
return agent_state, episode_stats, next_obs, next_done, storage, key, handle, global_step
for iteration in range(1, args.num_iterations + 1):
iteration_time_start = time.time()
agent_state, episode_stats, next_obs, next_done, storage, key, handle, global_step = rollout(
agent_state, episode_stats, next_obs, next_done, storage, key, handle, global_step
)
storage = compute_gae(agent_state, next_obs, next_done, storage)
agent_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key = update_ppo(
agent_state,
storage,
key,
)
avg_episodic_return = np.mean(jax.device_get(episode_stats.returned_episode_returns))
print(f"global_step={global_step}, avg_episodic_return={avg_episodic_return}")
# TRY NOT TO MODIFY: record rewards for plotting purposes
writer.add_scalar("charts/avg_episodic_return", avg_episodic_return, global_step)
writer.add_scalar(
"charts/avg_episodic_length", np.mean(jax.device_get(episode_stats.returned_episode_lengths)), global_step
)
writer.add_scalar("charts/learning_rate", agent_state.opt_state[1].hyperparams["learning_rate"].item(), global_step)
writer.add_scalar("losses/value_loss", v_loss.item(), global_step)
writer.add_scalar("losses/policy_loss", pg_loss.item(), global_step)
writer.add_scalar("losses/entropy", entropy_loss.item(), global_step)
writer.add_scalar("losses/approx_kl", approx_kl.item(), global_step)
writer.add_scalar("losses/loss", loss.item(), global_step)
print("SPS:", int(global_step / (time.time() - start_time)))
writer.add_scalar("charts/SPS", int(global_step / (time.time() - start_time)), global_step)
writer.add_scalar(
"charts/SPS_update", int(args.num_envs * args.num_steps / (time.time() - iteration_time_start)), global_step
)
envs.close()
writer.close()