-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppo_gymnax.py
1508 lines (1272 loc) · 88.5 KB
/
ppo_gymnax.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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Code mostly taken from https://github.com/vwxyzjn/cleanrl/blob/master/cleanrl/ppo_atari_envpool_xla_jax.py
import gymnasium as gym
import numpy as np
import jax
import jax.numpy as jnp
import flax
from distrax import Normal, MultivariateNormalDiag
from flax import linen as nn
from functools import partial
from flax.training.train_state import TrainState
import optax
import optuna
import functools
import os
import random
from typing import Any
#from torch.utils.tensorboard import SummaryWriter
from PIL import Image, ImageDraw, ImageFont
from moviepy.editor import ImageSequenceClip
from matplotlib import pyplot as plt
import datetime
from utils import build_env, ActorTrainState, EpisodeStatistics, Storage, ObservationActionBuffer, convert_to_discrete_tree, plot_decision_tree, plot_decision_tree_soft, NormalizeObservationWrapper, OBSERVATION_LABELS
from args import get_args
import configs
from sympol import SYMPOL_RL
from mlp import Critic_MLP, Actor_MLP, Actor_MLP_Continuous
from sdt import Critic_SDT, Actor_SDT
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor, plot_tree
import distrax
import graphviz
import wandb
import pickle
import gymnax
import time
import jax
import jax.numpy as jnp
from jax import lax
from gymnax.wrappers import gym as wrappers
from gymnax.wrappers.purerl import FlattenObservationWrapper, GymnaxWrapper
from gymnasium.wrappers import FlattenObservation
#os.environ['MUJOCO_GL'] = 'egl'
# Fix weird OOM https://github.com/google/jax/discussions/6332#discussioncomment-1279991
#os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "0.1"
os.environ["XLA_PYTHON_CLIENT_PREALLOCATE"] = "0"
#os.environ["XLA_FLAGS"] = "--xla_dump_to=~/tmp/foo"
# 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"
def train_agent(args, trial=None, queue=None):
start_time = time.time()
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_number)
print('CUDA_VISIBLE_DEVICES', os.environ['CUDA_VISIBLE_DEVICES'])
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
if trial is not None:
timestamp = 'TRIAL_NO' + str(trial.number) + '_' + timestamp
if args.actor == 'mlp':
suggested_params = configs.suggest_config_mlp(trial, args.env_id)
elif args.actor == 'sympol':
suggested_params = configs.suggest_config_sympol(trial, args.env_id)
elif args.actor == 'sdt':
suggested_params = configs.suggest_config_sdt(trial, args.env_id)
elif args.actor == 'd-sdt':
suggested_params = configs.suggest_config_dsdt(trial, args.env_id)
elif args.actor == 'stateActionDT':
suggested_params = configs.suggest_config_stateActionDT(trial, args.env_id)
else:
suggested_params = {}
args.__dict__.update(suggested_params)
n_steps = args.n_steps
elif not args.use_best_config:
if False:
if args.env_id == 'Hopper-v4':
n_steps = 512
args.n_envs = 2
elif args.env_id == 'CartPole-v1':
n_steps = 32
args.n_envs = 8
elif args.env_id == 'Pendulum-v1':
n_steps = 1024
args.n_envs = 4
elif args.env_id == 'BipedalWalker-v3':
n_steps = 512
args.n_envs = 16
elif args.env_id == 'LunarLander-v2':
n_steps = 512
args.n_envs = 8
else:
n_steps = 512
args.n_envs = 8
else:
n_steps = args.n_steps
if args.dynamic_buffer:
n_steps = max(16, n_steps // 8)
accumulate_gradients_every = args.accumulate_gradients_every
accumulate_gradients_every_initial = accumulate_gradients_every
initial_steps = n_steps
# these parameters are defined dynamically
batch_size = int(args.n_envs * n_steps)
#minibatch_size = int(batch_size // args.n_minibatches)
minibatch_size = args.minibatch_size
while batch_size // minibatch_size < 2:
minibatch_size = minibatch_size // 2
#n_iterations = args.total_steps // batch_size
#eval_freq = max(args.eval_freq // batch_size, 1)
trial_scores = []
for random_trial_number in range(1, args.random_trials+1):
if args.actor == 'sympol':
model_identifier = '-'.join([str(args.depth), str(args.n_estimators), str(args.seed)])
elif args.actor != 'mlp':
model_identifier = '-'.join([str(args.depth), str(args.seed)])
else:
model_identifier = str(args.seed)
run_name = '-'.join([args.run_name, args.actor, str(np.round(args.learning_rate_actor, 6)), model_identifier, timestamp])
group_name = run_name
run_name = run_name + '_' + str(random_trial_number)
#envs = build_env(args.env_id, n_env=args.n_envs)
#args.n_envs = 1
if not args.env_id in gymnax.registered_envs:
import sys
sys.exit("Environment not implemented in gymnax")
envs, env_params = gymnax.make(args.env_id)
if args.normEnv:
print('NORMALIZE')
envs = NormalizeObservationWrapper(envs, env_params)
vmap_reset = jax.vmap(envs.reset, in_axes=(0, None))
vmap_step = jax.vmap(envs.step, in_axes=(0, 0, 0, None))
obs_dim = envs.observation_space(env_params).shape[-1]#envs.obs_shape[-1] #envs.single_observation_space.shape[-1]
obs_shape = envs.observation_space(env_params).shape
print('Observations:', obs_dim)
if isinstance(envs.action_space(), gymnax.environments.spaces.Discrete):
action_dim = envs.action_space().n
elif isinstance(envs.action_space(), gymnax.environments.spaces.Box):
action_dim = envs.action_space().shape[-1]
print('Actions:', action_dim)
if args.track:
wandb_run = wandb.init(
project=f"{args.exp_name}_{args.env_id}",
group=group_name,
tags=[args.run_name],
#sync_tensorboard=True,
config=vars(args),
name=run_name,
monitor_gym=True,
save_code=True,
)
env_seed = args.seed + (random_trial_number * 100)
env_key = jax.random.PRNGKey(env_seed)
if True:
seed_training = args.seed + (random_trial_number * 100)
else:
seed_training = args.seed
key = jax.random.PRNGKey(seed_training)
model_key = jax.random.PRNGKey(args.seed)
model_key, actor_key, critic_key = jax.random.split(model_key, 3)
# agent setup
if args.critic == "mlp":
critic = Critic_MLP(num_layers=args.num_layers, neurons_per_layer=args.neurons_per_layer)
elif args.critic == "sdt":
critic = Critic_SDT(depth=args.depth, temperature=args.temperature)#, temp=1)
critic.apply = jax.jit(critic.apply)
if args.actor == "mlp" or args.actor == "stateActionDT":
if args.action_type == "discrete":
actor = Actor_MLP(action_dim=action_dim, num_layers=args.num_layers, neurons_per_layer=args.neurons_per_layer)
else:
actor = Actor_MLP_Continuous(action_dim=action_dim, num_layers=args.num_layers, neurons_per_layer=args.neurons_per_layer)
actor.apply = jax.jit(actor.apply)
#args.learning_rate_actor = args.learning_rate_critic # same lr for MLP's
args.accumulate_gradients_every = 1 # do not accumulate gradients for MLP's
learning_rate_actor = args.learning_rate_actor
elif args.actor == "sympol":
learning_rate_actor_weights = args.learning_rate_actor_weights
learning_rate_actor_split_values = args.learning_rate_actor_split_values
learning_rate_actor_split_idx_array = args.learning_rate_actor_split_idx_array
learning_rate_actor_leaf_array = args.learning_rate_actor_leaf_array
learning_rate_actor_log_std = args.learning_rate_actor_log_std
actor = SYMPOL_RL(
obs_dim=obs_dim,
action_dim=action_dim,
action_type=args.action_type,
depth=args.depth,
n_estimators=args.n_estimators,
)
elif args.actor == "sdt" or args.actor == "d-sdt":
actor = Actor_SDT(action_dim=action_dim, depth=args.depth, temperature=args.temperature, action_type=args.action_type)#, temp=1)
actor.apply = jax.jit(actor.apply)
#args.learning_rate_actor = args.learning_rate_critic # same lr for SDT's
learning_rate_actor = args.learning_rate_actor
args.accumulate_gradients_every = 1 # do not accumulate gradients for SDT's
if args.adamW:
critic_state = TrainState.create(
apply_fn=None,
params=critic.init(critic_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm), optax.adamw(learning_rate=args.learning_rate_critic)
),
)
else:
critic_state = TrainState.create(
apply_fn=None,
params=critic.init(critic_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm), optax.adam(learning_rate=args.learning_rate_critic)
),
)
if args.actor == "sympol":
def map_nested_fn(fn):
'''Recursively apply `fn` to key-value pairs of a nested dict.'''
def map_fn(nested_dict):
return {k: (map_fn(v) if isinstance(v, dict) else fn(k, v))
for k, v in nested_dict.items()}
return map_fn
if args.SWA:
from optax_swag import swag
if args.adamW:
actor_state = ActorTrainState.create(
apply_fn=None,
params=actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.multi_transform(
{'estimator_weights': optax.inject_hyperparams(optax.adam)(learning_rate_actor_weights),
'split_values': optax.inject_hyperparams(optax.adam)(learning_rate_actor_split_values),
'split_idx_array': optax.inject_hyperparams(optax.adamw)(learning_rate_actor_split_idx_array),
'leaf_array': optax.inject_hyperparams(optax.adamw)(learning_rate_actor_leaf_array),
'log_std': optax.inject_hyperparams(optax.adamw)(learning_rate_actor_log_std),},
map_nested_fn(lambda k, _: k)),
swag(10, 2),
),
grad_accum=jax.tree.map(
jnp.zeros_like, actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)]))
),
indices=actor.init_indices(actor_key) if args.actor == "sympol" else None,
)
else:
actor_state = ActorTrainState.create(
apply_fn=None,
params=actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.multi_transform(
{'estimator_weights': optax.inject_hyperparams(optax.adam)(learning_rate_actor_weights),
'split_values': optax.inject_hyperparams(optax.adam)(learning_rate_actor_split_values),
'split_idx_array': optax.inject_hyperparams(optax.adam)(learning_rate_actor_split_idx_array),
'leaf_array': optax.inject_hyperparams(optax.adam)(learning_rate_actor_leaf_array),
'log_std': optax.inject_hyperparams(optax.adam)(learning_rate_actor_log_std),},
map_nested_fn(lambda k, _: k)),
swag(10, 2),
),
grad_accum=jax.tree.map(
jnp.zeros_like, actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)]))
),
indices=actor.init_indices(actor_key) if args.actor == "sympol" else None,
)
else:
if args.adamW:
actor_state = ActorTrainState.create(
apply_fn=None,
params=actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.multi_transform(
{'estimator_weights': optax.inject_hyperparams(optax.adam)(learning_rate_actor_weights),
'split_values': optax.inject_hyperparams(optax.adam)(learning_rate_actor_split_values),
'split_idx_array': optax.inject_hyperparams(optax.adamw)(learning_rate_actor_split_idx_array),
'leaf_array': optax.inject_hyperparams(optax.adamw)(learning_rate_actor_leaf_array),
'log_std': optax.inject_hyperparams(optax.adamw)(learning_rate_actor_log_std),},
map_nested_fn(lambda k, _: k)),
),
grad_accum=jax.tree.map(
jnp.zeros_like, actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)]))
),
indices=actor.init_indices(actor_key) if args.actor == "sympol" else None,
)
else:
actor_state = ActorTrainState.create(
apply_fn=None,
params=actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.multi_transform(
{'estimator_weights': optax.inject_hyperparams(optax.adam)(learning_rate_actor_weights),
'split_values': optax.inject_hyperparams(optax.adam)(learning_rate_actor_split_values),
'split_idx_array': optax.inject_hyperparams(optax.adam)(learning_rate_actor_split_idx_array),
'leaf_array': optax.inject_hyperparams(optax.adam)(learning_rate_actor_leaf_array),
'log_std': optax.inject_hyperparams(optax.adam)(learning_rate_actor_log_std),},
map_nested_fn(lambda k, _: k)),
),
grad_accum=jax.tree.map(
jnp.zeros_like, actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)]))
),
indices=actor.init_indices(actor_key) if args.actor == "sympol" else None,
)
else:
if args.adamW:
actor_state = ActorTrainState.create(
apply_fn=None,
params=actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.inject_hyperparams(optax.adamw)(learning_rate_actor),
),
grad_accum=jax.tree.map(
jnp.zeros_like, actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)]))
),
indices=actor.init_indices(actor_key) if args.actor == "sympol" else None,
)
else:
actor_state = ActorTrainState.create(
apply_fn=None,
params=actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)])),
tx=optax.chain(
optax.clip_by_global_norm(args.max_grad_norm),
optax.inject_hyperparams(optax.adam)(learning_rate_actor),
),
grad_accum=jax.tree.map(
jnp.zeros_like, actor.init(actor_key, jnp.array([envs.observation_space(env_params).sample(key)]))
),
indices=actor.init_indices(actor_key) if args.actor == "sympol" else None,
)
lr_scheduler = optax.contrib.reduce_on_plateau(patience=3, factor=0.5)
lr_scheduler_state = lr_scheduler.init(actor_state.params)
#actor.apply = jax.jit(actor.apply)
#critic.apply = jax.jit(critic.apply)
episode_stats = EpisodeStatistics(
episode_returns=jnp.zeros(args.n_envs, dtype=jnp.float32),
episode_lengths=jnp.zeros(args.n_envs, dtype=jnp.int32),
returned_episode_returns=jnp.zeros(args.n_envs, dtype=jnp.float32),
returned_episode_lengths=jnp.zeros(args.n_envs, dtype=jnp.int32),
)
@jax.jit
def get_action_and_value(
actor_state: TrainState,
critic_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"""
#jax.debug.print("next_obs: {}", next_obs.shape)
if args.action_type == "discrete":
action_logits = actor.apply(actor_state.params, next_obs, indices=actor_state.indices)
action_distribution = distrax.Categorical(logits=action_logits)
value = critic.apply(critic_state.params, next_obs)
# Sample discrete actions from Normal distribution
key, subkey = jax.random.split(key)
action = action_distribution.sample(seed=subkey)
logprob = action_distribution.log_prob(action)#.sum(-1)
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()),
)
else:
result = actor.apply(actor_state.params, next_obs, indices=actor_state.indices)
action_distribution = distrax.MultivariateNormalDiag(result[0], jnp.exp(result[1]))
value = critic.apply(critic_state.params, next_obs)
# Sample continuous actions from Normal distribution
key, subkey = jax.random.split(key)
action = action_distribution.sample(seed=subkey)
#jax.debug.print("action: {}", action.shape)
logprob = action_distribution.log_prob(action)#.sum(-1)
#jax.debug.print("action_distribution: {}", action_distribution)
#jax.debug.print("action: {}", action)
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(
actor_state_params: flax.core.FrozenDict,
critic_state_params: flax.core.FrozenDict,
x: np.ndarray,
action: np.ndarray,
):
"""calculate value, logprob of supplied `action`, and entropy"""
if args.action_type == "discrete":
logits = actor.apply(actor_state_params, x, indices=actor_state.indices)
value = critic.apply(critic_state_params, x).squeeze()
action_distribution = distrax.Categorical(logits=logits)
logprob = action_distribution.log_prob(action)
entropy = action_distribution.entropy()
else:
result = actor.apply(actor_state_params, x, indices=actor_state.indices)
action_distribution = distrax.MultivariateNormalDiag(result[0], jnp.exp(result[1]))
value = critic.apply(critic_state_params, x).squeeze()
logprob = action_distribution.log_prob(action)
entropy = action_distribution.entropy()
return logprob, entropy, value
def compute_gae_once(carry, inp, gamma, gae_lambda):
advantages = carry
nextdone, nextvalues, curvalues, reward = inp
nextnonterminal = 1.0 - nextdone
delta = reward + gamma * nextvalues * nextnonterminal - curvalues
advantages = delta + gamma * gae_lambda * nextnonterminal * advantages
return advantages, advantages
compute_gae_once = partial(compute_gae_once, gamma=args.gamma, gae_lambda=args.gae_lambda)
def compute_gae(
critic_state: TrainState,
next_obs: np.ndarray,
next_done: np.ndarray,
storage: Storage,
):
next_value = critic.apply(critic_state.params, next_obs).squeeze()
advantages = jnp.zeros((args.n_envs,))
dones = jnp.concatenate([storage.dones, next_done[None, :]], axis=0)
values = jnp.concatenate([storage.values, next_value[None, :]], axis=0)
_, advantages = jax.lax.scan(
compute_gae_once, advantages, (dones[1:], values[1:], values[:-1], storage.rewards), reverse=True
)
storage = storage.replace(
advantages=advantages,
returns=advantages + storage.values,
)
return storage
@jax.jit
def ppo_loss_base(actor_state_params, critic_state_params, x, a, logp, mb_advantages, mb_returns):
newlogprob, entropy, newvalue = get_action_and_value2(actor_state_params, critic_state_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_base_grad_fn = jax.value_and_grad(ppo_loss_base, argnums=(0, 1), has_aux=True)
@jax.jit
def update_ppo(
actor_state: TrainState,
critic_state: TrainState,
storage: Storage,
key: jax.random.PRNGKey,
accumulate_gradients_every: int,
):
def update_epoch(carry, unused_inp):
actor_state, critic_state, key = carry
key, subkey = jax.random.split(key)
def flatten(x):
return x.reshape((-1,) + x.shape[2:])
# taken from: https://github.com/google/brax/blob/main/brax/training/agents/ppo/train.py
def convert_data(x: jnp.ndarray):
num_minibatches = int(np.floor(x.shape[0] / minibatch_size))
size = num_minibatches * minibatch_size
x = jax.random.permutation(subkey, x)[:size]
x = jnp.reshape(x, (num_minibatches, -1) + x.shape[1:])
return x
flatten_storage = jax.tree_map(flatten, storage)
shuffled_storage = jax.tree_map(convert_data, flatten_storage)
def update_minibatch(carry, minibatch):
actor_state, critic_state = carry
(loss, (pg_loss, v_loss, entropy_loss, approx_kl)), (actor_grads, critic_grads) = ppo_loss_base_grad_fn(
actor_state.params,
critic_state.params,
minibatch.obs,
minibatch.actions,
minibatch.logprobs,
minibatch.advantages,
minibatch.returns,
)
critic_state = critic_state.apply_gradients(grads=critic_grads)
actor_grad_accum = jax.tree_util.tree_map(lambda x, y: x + y, actor_grads, actor_state.grad_accum)
actor_state = actor_state.apply_gradients(grads=actor_grads)
def update_fn():
grads = jax.tree_util.tree_map(lambda x: x / accumulate_gradients_every, actor_grad_accum)
new_state = actor_state.apply_gradients(
grads=grads,
grad_accum=jax.tree_util.tree_map(jnp.zeros_like, grads),
)
return new_state
actor_state = jax.lax.cond(
actor_state.step % accumulate_gradients_every == 0,
lambda _: update_fn(),
lambda _: actor_state.replace(grad_accum=actor_grad_accum, step=actor_state.step + 1),
None,
)
return (actor_state, critic_state), (loss, pg_loss, v_loss, entropy_loss, approx_kl, actor_grad_accum)
(actor_state, critic_state), (loss, pg_loss, v_loss, entropy_loss, approx_kl, actor_grad_accum) = jax.lax.scan(
update_minibatch, (actor_state, critic_state), shuffled_storage
)
return (actor_state, critic_state, key), (loss, pg_loss, v_loss, entropy_loss, approx_kl, actor_grad_accum)
(actor_state, critic_state, key), (loss, pg_loss, v_loss, entropy_loss, approx_kl, actor_grad_accum) = jax.lax.scan(
update_epoch, (actor_state, critic_state, key), (), length=args.n_update_epochs
)
return actor_state, critic_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key
#@jax.jit
global_step = 0
env_key, key_reset = jax.random.split(env_key, 2)
#next_obs, env_state = envs.reset(key_reset, env_params)
vmap_keys = jax.random.split(key_reset, args.n_envs)
next_obs, env_state = vmap_reset(vmap_keys, env_params)
next_done = np.zeros(args.n_envs).astype(bool)
#####jax.debug.print("next_obs INIT {}: {}", next_obs.shape, next_obs)
#env_state, env_params,
#@jax.jit
# based on https://github.dev/google/evojax/blob/0625d875262011d8e1b6aa32566b236f44b4da66/evojax/sim_mgr.py
#@jax.jit
def create_rollout_gymnax(n_steps):
def rollout_gymnax_(actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, global_step):
#for step in range(0, n_steps):
step = 0
key, rng_step = jax.random.split(key, 2)
step_keys = jax.random.split(rng_step, args.n_envs)
def policy_step(state_input, tmp):
actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, step_keys, global_step, step = state_input
global_step += args.n_envs
storage, action, key = get_action_and_value(
actor_state, critic_state, next_obs, next_done, storage, step, key,
)
# TRY NOT TO MODIFY: execute the game and log data.
#print('ACTION', jax.device_get(action))
#next_obs, reward, next_done, trunc, info = envs.step(jax.device_get(action))
#next_obs, env_state, reward, next_done, _ = envs.step(
# rng_step, env_state, action, env_params
#)
next_obs, env_state, reward, next_done, _ = vmap_step(step_keys, env_state, action, env_params)
#print('STEP', next_obs, reward, next_done, trunc, info)
new_episode_return = episode_stats.episode_returns + reward
new_episode_length = episode_stats.episode_lengths + 1
episode_stats = episode_stats.replace(
episode_returns=(new_episode_return) * (1 - next_done), #* (1 - trunc),
episode_lengths=(new_episode_length) * (1 - next_done), #* (1 - trunc),
# only update the `returned_episode_returns` if the episode is done
returned_episode_returns=jnp.where(
next_done,# + trunc,
new_episode_return,
episode_stats.returned_episode_returns,
),
returned_episode_lengths=jnp.where(
next_done,# + trunc,
new_episode_length,
episode_stats.returned_episode_lengths,
),
)
storage = storage.replace(rewards=storage.rewards.at[step].set(reward))
step += 1
return [actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, step_keys, global_step, step], [actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, step_keys, global_step, step]
#####jax.debug.print("next_obs END {}: {}", next_obs.shape, next_obs)
#####jax.debug.print("action END {}: {}", action.shape, action)
scan_out_single, scan_out = jax.lax.scan(
policy_step,
[actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, step_keys, global_step, step],
(),
n_steps
)
# Return masked sum of rewards accumulated by agent in episode
actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, step_keys, global_step, step = scan_out_single
return actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, global_step
return jax.jit(rollout_gymnax_)
#hyperparameters = {key: value for key, value in vars(args).items()}
# Save hyperparameters to wandb
#wandb.config.update(hyperparameters)
avg_score_list = []
iteration = 1
last_eval = 0
n_steps_old = 0
avg_episodic_return_list = []
total_time_cleaned = 0
while global_step < args.total_steps:
#for iteration in range(1, n_iterations + 1):
wandb_log = {}
# ALGO Logic: Storage setup
#increase_index = global_step // (args.total_steps//len(increase_factor_list))
if args.dynamic_buffer or not args.static_batch:
#increase_index = global_step // (args.total_steps//sum(increase_factor_list))
#increase_factor = int(increase_factor_list_long[increase_index])
increase_factor = int(2**(np.ceil((((global_step+1)*8)/(1+args.total_steps)))-1)) # int(increase_factor_list_long[increase_index])
increase_factor_batch = int(2**(np.ceil((((global_step+1)*8)/(1+args.total_steps)))-1)) # int(increase_factor_list_long[increase_index])
if args.dynamic_buffer:
n_steps = initial_steps * increase_factor
else:
n_steps = initial_steps
if not args.static_batch:
accumulate_gradients_every = int(accumulate_gradients_every_initial * increase_factor_batch)
else:
accumulate_gradients_every = int(accumulate_gradients_every_initial)
batch_size = int(args.n_envs * n_steps)
#n_iterations = args.total_steps // batch_size
#eval_freq = max(args.eval_freq // batch_size, 1)
current_eval = global_step // args.eval_freq
if n_steps != n_steps_old:
rollout_gymnax = create_rollout_gymnax(n_steps)
#compute_gae = create_compute_gae(n_steps)
#update_ppo = create_update_ppo(batch_size, minibatch_size, accumulate_gradients_every)
n_steps_old = n_steps
else:
if global_step == 0:
rollout_gymnax = create_rollout_gymnax(n_steps)
#compute_gae = create_compute_gae(n_steps)
#update_ppo = create_update_ppo(batch_size, minibatch_size, accumulate_gradients_every)
current_eval = global_step // args.eval_freq
start_time_cleaned = time.time()
storage = Storage(
obs=jnp.zeros((n_steps, args.n_envs) + obs_shape),
actions=jnp.zeros((n_steps, args.n_envs) + envs.action_space().shape, dtype=jnp.int32),
logprobs=jnp.zeros((n_steps, args.n_envs)),
dones=jnp.zeros((n_steps, args.n_envs)),
values=jnp.zeros((n_steps, args.n_envs)),
advantages=jnp.zeros((n_steps, args.n_envs)),
returns=jnp.zeros((n_steps, args.n_envs)),
rewards=jnp.zeros((n_steps, args.n_envs)),
)
#actor_state, critic_state, episode_stats, next_obs, next_done, storage, key, global_step = rollout(
# actor_state, critic_state, episode_stats, next_obs, next_done, storage, key, global_step
#)
actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, global_step = rollout_gymnax(
actor_state, critic_state, env_state, env_params, episode_stats, next_obs, next_done, storage, key, global_step
)
#jax.debug.print("next_obs END {}: {}", next_obs.shape, next_obs)
storage = compute_gae(critic_state, next_obs, next_done, storage)
actor_state, critic_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key = update_ppo(
actor_state,
critic_state,
storage,
key,
accumulate_gradients_every,
)
elapsed_time_cleaned = time.time() - start_time_cleaned
total_time_cleaned += elapsed_time_cleaned
avg_episodic_return = np.mean(np.array(episode_stats.returned_episode_returns))
avg_episodic_return_list.append(avg_episodic_return)
#writer.add_scalar("charts/avg_train_episodic_return", avg_episodic_return, global_step)
wandb_log['charts/avg_train_episodic_return'] = avg_episodic_return
if iteration == 1 or current_eval > last_eval or global_step + batch_size >= args.total_steps:
last_eval = current_eval
render_now = True if args.render_each_eval else True if global_step + batch_size >= args.total_steps else False
def fit_stateActionDT(actor_state, env_id, n_episodes, name_appendix, seed=1_000):
action_obs_store = ObservationActionBuffer(
#obs=jnp.zeros((n_steps, args.n_envs) + envs.single_observation_space.shape),
obs=jnp.zeros((n_steps, n_episodes) + obs_shape),
#actions=jnp.zeros((n_steps, args.n_envs) + envs.single_action_space.shape,
actions=jnp.zeros((n_steps, n_episodes) + envs.action_space().shape,
dtype=jnp.int32)
)
total_eval_steps = 0
for episode_index in range(n_episodes):
#temp_env = build_env(env_id, n_env=1)
env_gymnax, env_params_eval = gymnax.make(args.env_id)
temp_env = wrappers.GymnaxToGymWrapper(env_gymnax, env_params_eval, 0)
done, trunc = False, False
obs, info = temp_env.reset(seed=seed + episode_index)#random.randint(0, 1000))
step_counter = 0
while not done and not trunc:
actor_params = actor_state.params
if args.action_type == 'discrete':
action_logits = actor.apply(actor_params, np.array([obs]), indices=actor_state.indices)
action = jnp.argmax(action_logits, axis=1)
action = jnp.squeeze(action, axis=0) #jnp.squeeze(action, axis=0) if action.shape[0] == 1 else action #action[0]
else:
result = actor.apply(actor_params, np.array([obs]), indices=actor_state.indices)
action_distribution = distrax.MultivariateNormalDiag(result[0], jnp.exp(result[1]))
action = action_distribution.mean()
action = jnp.squeeze(action, axis=0)
action_obs_store = action_obs_store.replace(
obs=storage.obs.at[total_eval_steps].set(obs),
actions=storage.actions.at[total_eval_steps].set(action)
)
next_obs, rewards, done, trunc, info = temp_env.step(jax.device_get(action))
obs = next_obs
step_counter += 1
total_eval_steps += 1
temp_env.close()
# Initialize decision tree
if args.action_type == 'discrete':
decision_tree = DecisionTreeClassifier(max_depth=args.depth)
else:
if action_dim == 1:
decision_tree = DecisionTreeRegressor(max_depth=args.depth)
else:
decision_tree = [DecisionTreeRegressor(max_depth=args.depth) for _ in range(action_dim)]
# Train the decision tree
X = np.array(action_obs_store.obs).reshape(-1, temp_env.observation_space.shape[-1])
if args.action_type == 'discrete' or action_dim == 1:
y = np.array(action_obs_store.actions).reshape(-1)
decision_tree.fit(X, y)
else:
for i in range(action_dim):
y = np.array(action_obs_store.actions).reshape(-1, action_dim)
decision_tree[i].fit(X, y[:,i])
return decision_tree
def evaluate_agent(actor_state, env_id, n_episodes, name_appendix, seed=100, decision_tree=None):
video_folder = 'videos/wandb'
if not os.path.exists(video_folder):
os.makedirs(video_folder)
#temp_env = Monitor(temp_env, video_folder) #, force=True
score = []
score_interpretable = []
node_count = 0
for episode_index in range(n_episodes):
if args.actor == "stateActionDT":
#temp_env = build_env(env_id, n_env=1)
env_gymnax, env_params_eval = gymnax.make(args.env_id)
temp_env = wrappers.GymnaxToGymWrapper(env_gymnax, env_params_eval, 0)
video_path = os.path.join(video_folder, run_name + "-" + "-" + env_id + str(episode_index) + ".mp4")
image_path = os.path.join(video_folder, run_name + "-" + "-" + env_id)# + str(episode_index))
done, trunc = False, False
obs, info = temp_env.reset(seed=seed + episode_index)#random.randint(0, 1000))
running_reward = 0
frames = []
dones = False
step_counter = 0
while not done and not trunc:
if args.render_env and render_now:
if False:#frame is not None:
frame = temp_env.render()
frame = frame[0]
# Draw the figure on the canvas
frame.canvas.draw()
# Get the RGBA buffer from the figure
w, h = frame.canvas.get_width_height()
buf = np.frombuffer(frame.canvas.tostring_argb(), dtype=np.uint8)
buf.shape = (h, w, 4)
# Convert from ARGB to RGBA
buf = np.roll(buf, 3, axis=2)
# Remove the alpha channel
frame = buf[:, :, :3]
image = Image.fromarray(frame)
draw = ImageDraw.Draw(image)
text_step = f'Step: {step_counter}'
font_size = frame.shape[0] // 20
draw.text((font_size, font_size*0.5), text_step, (200, 200, 200), font=ImageFont.truetype("DejaVuSansMono-Bold.ttf", font_size))
text_reward = f'Reward: {running_reward}'
draw.text((font_size, font_size*2.0), text_reward, (200, 200, 200), font=ImageFont.truetype("DejaVuSansMono-Bold.ttf", font_size))
frames.append(np.array(image))
actor_params = actor_state.params
flat_obs = obs.reshape(1, -1)
if args.action_type == 'discrete':
action = decision_tree.predict(flat_obs)[0]
#print('decision_tree.predict(flat_obs)', decision_tree.predict(flat_obs))
#print('action', action)
else:
if action_dim == 1:
action = decision_tree.predict(flat_obs)
else:
action_list = []
for i in range(action_dim):
action_by_tree = decision_tree[i].predict(flat_obs)[0]
action_list.append(action_by_tree)
action = np.array(action_list)
next_obs, rewards, done, trunc, info = temp_env.step(action)
running_reward += rewards
#if "final_info" in info:
# episode_reward = info["final_info"][0]["episode"]["r"]
# score.append(episode_reward)
obs = next_obs
step_counter += 1
score_interpretable.append(running_reward)
if (args.render_env and render_now):
if False:#frame is not None:
frame = temp_env.render()
frame = frame[0]
# Draw the figure on the canvas
frame.canvas.draw()
# Get the RGBA buffer from the figure
w, h = frame.canvas.get_width_height()
buf = np.frombuffer(frame.canvas.tostring_argb(), dtype=np.uint8)
buf.shape = (h, w, 4)
# Convert from ARGB to RGBA
buf = np.roll(buf, 3, axis=2)
# Remove the alpha channel
frame = buf[:, :, :3]
image = Image.fromarray(frame)
draw = ImageDraw.Draw(image)
text_step = f'Step: {step_counter}'
font_size = frame.shape[0] // 20
draw.text((font_size, font_size*0.5), text_step, (200, 200, 200), font=ImageFont.truetype("DejaVuSansMono-Bold.ttf", font_size))
text_reward = f'Reward: {running_reward}'
draw.text((font_size, font_size*2.0), text_reward, (200, 200, 200), font=ImageFont.truetype("DejaVuSansMono-Bold.ttf", font_size))
frames.append(np.array(image))
numpy_clip = np.transpose(np.array(frames), (0, 3, 1, 2))
fps = 5 if 'MiniGrid' in env_id else 25
if args.track:
wandb.log({"gameplay_" + name_appendix + '_trial' + str(episode_index): wandb.Video(numpy_clip, fps=fps, format="mp4")}, commit=False)
#wandb_log["gameplay_" + name_appendix + '_trial' + str(episode_index)] = wandb.Video(numpy_clip, fps=fps, format="mp4")
if episode_index==0:
if args.action_type == 'discrete' or action_dim == 1:
# Plot the decision tree
plt.figure(figsize=(20, 10))
plot_tree(decision_tree, filled=True)
plt.title("Decision Tree")
# Save the plot to a file
video_folder = 'videos/wandb'
image_path = os.path.join(video_folder, run_name + "-" + "-" + args.env_id)
plot_filename = image_path + "state_action_DT.png"
plt.savefig(plot_filename)
plt.close()
# Log the image to wandb
if args.track:
wandb.log({"state_action_DT": wandb.Image(plot_filename)})
node_count += decision_tree.tree_.node_count
else:
node_count = 0
for i in range(action_dim):
# Plot the decision tree
plt.figure(figsize=(20, 10))
plot_tree(decision_tree[i], filled=True)
plt.title("Decision Tree " + str(i))
# Save the plot to a file
video_folder = 'videos/wandb'
image_path = os.path.join(video_folder, run_name + "-" + "-" + args.env_id)
plot_filename = image_path + "state_action_DT_reg" + str(i) + ".png"
plt.savefig(plot_filename)
plt.close()
node_count += decision_tree[i].tree_.node_count
if args.track:
# Log the image to wandb
wandb.log({"state_action_DT_" + str(i): wandb.Image(plot_filename)})
temp_env.close()
elif args.actor == "d-sdt":
#temp_env = build_env(env_id, n_env=1)
env_gymnax, env_params_eval = gymnax.make(args.env_id)
temp_env = wrappers.GymnaxToGymWrapper(env_gymnax, env_params_eval, 0)
video_path = os.path.join(video_folder, run_name + "-" + "-" + env_id + str(episode_index) + ".mp4")
image_path = os.path.join(video_folder, run_name + "-" + "-" + env_id)# + str(episode_index))
done, trunc = False, False
obs, info = temp_env.reset(seed=seed + episode_index)#random.randint(0, 1000))
running_reward = 0
frames = []
dones = False
step_counter = 0
while not done and not trunc:
if args.render_env and render_now:
if False:#frame is not None:
frame = temp_env.render()
frame = frame[0]
# Draw the figure on the canvas
frame.canvas.draw()
# Get the RGBA buffer from the figure
w, h = frame.canvas.get_width_height()
buf = np.frombuffer(frame.canvas.tostring_argb(), dtype=np.uint8)
buf.shape = (h, w, 4)
# Convert from ARGB to RGBA
buf = np.roll(buf, 3, axis=2)