-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathunizero.py
More file actions
executable file
·1963 lines (1735 loc) · 105 KB
/
unizero.py
File metadata and controls
executable file
·1963 lines (1735 loc) · 105 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
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
import copy
import logging
from collections import defaultdict
from typing import Any, Dict, List, Tuple, Union
import numpy as np
import torch
import torch.nn.functional as F
import wandb
from ding.model import model_wrap
from ding.utils import POLICY_REGISTRY
from lzero.mcts import UniZeroMCTSCtree as MCTSCtree
from lzero.model import ImageTransforms
from lzero.policy import (DiscreteSupport, InverseScalarTransform,
mz_network_output_unpack, phi_transform, prepare_obs,
prepare_obs_stack_for_unizero, scalar_transform,
select_action, to_torch_float_tensor)
from lzero.policy.head_clip_manager import (HeadClipConfig, HeadClipManager,
create_head_clip_manager_from_dict)
from lzero.policy.muzero import MuZeroPolicy
from lzero.policy.utils import initialize_pad_batch
from torch.nn.utils.convert_parameters import (parameters_to_vector,
vector_to_parameters)
from .utils import configure_optimizers_nanogpt
def scale_module_weights_vectorized(module: torch.nn.Module, scale_factor: float):
"""
Efficiently scale all weights of a module using vectorized operations.
"""
if not (0.0 < scale_factor < 1.0):
return # Do nothing if the scaling factor is invalid
# 1. Flatten all parameters of the module into a single vector
params_vec = parameters_to_vector(module.parameters())
# 2. Perform multiplication operation on this vector
params_vec.data.mul_(scale_factor)
# 3. Copy the scaled vector back to the individual parameters of the module
vector_to_parameters(params_vec, module.parameters())
def configure_optimizer_unizero(model, learning_rate, weight_decay, device_type, betas):
"""
Configure optimizer with differentiated learning rates and weight decay for encoder/backbone/head of UniZero model.
"""
# 1. Define parameters that need special handling
param_dict = {pn: p for pn, p in model.named_parameters() if p.requires_grad}
# 2. Divide parameters into three groups: Transformer backbone, Tokenizer, and Heads
transformer_params = {pn: p for pn, p in param_dict.items() if 'transformer' in pn}
tokenizer_params = {pn: p for pn, p in param_dict.items() if 'tokenizer' in pn}
# Head parameters are those that belong to neither transformer nor tokenizer
head_params = {
pn: p for pn, p in param_dict.items()
if 'transformer' not in pn and 'tokenizer' not in pn
}
# 3. Set different optimizer parameters for each group (especially learning rate)
# We still use AdamW here, but with more reasonable learning rate settings
optim_groups = [
{
'params': list(tokenizer_params.values()),
'lr': learning_rate, # Tokenizer uses base learning rate, e.g., 1e-4
'weight_decay': weight_decay
},
{
'params': list(transformer_params.values()),
'lr': learning_rate, # Tokenizer uses base learning rate, e.g., 1e-4
'weight_decay': weight_decay
},
{
'params': list(head_params.values()),
'lr': learning_rate, # Heads also use base learning rate, e.g., 1e-4
'weight_decay': weight_decay
}
]
logging.info("--- Optimizer Groups ---")
logging.info(f"Transformer LR: {learning_rate}")
logging.info(f"Tokenizer/Heads LR: {learning_rate}")
optimizer = torch.optim.AdamW(optim_groups, betas=betas)
return optimizer
@POLICY_REGISTRY.register('unizero')
class UniZeroPolicy(MuZeroPolicy):
"""
Overview:
The policy class for UniZero, official implementation for paper UniZero: Generalized and Efficient Planning
with Scalable LatentWorld Models. UniZero aims to enhance the planning capabilities of reinforcement learning agents
by addressing the limitations found in MuZero-style algorithms, particularly in environments requiring the
capture of long-term dependencies. More details can be found in https://arxiv.org/abs/2406.10667.
"""
# The default_config for UniZero policy.
config = dict(
type='unizero',
model=dict(
# (str) The model type. For 1-dimensional vector obs, we use mlp model. For the image obs, we use conv model.
model_type='conv', # options={'mlp', 'conv'}
# (bool) If True, the action space of the environment is continuous, otherwise discrete.
continuous_action_space=False,
# (tuple) The obs shape.
observation_shape=(3, 64, 64),
# (bool) Whether to use the self-supervised learning loss.
self_supervised_learning_loss=True,
# (bool) Whether to use discrete support to represent categorical distribution for value/reward/value_prefix.
categorical_distribution=True,
# (int) The image channel in image observation.
image_channel=3,
# (int) The number of frames to stack together.
frame_stack_num=1,
# (int) The number of res blocks in MuZero model.
num_res_blocks=1,
# (int) The number of channels of hidden states in MuZero model.
num_channels=64,
# (tuple) The range of supports used in categorical distribution.
# These variables are only effective when ``model.categorical_distribution=True``.
reward_support_range=(-50., 51., 1.),
value_support_range=(-50., 51., 1.),
# (bool) whether to learn bias in the last linear layer in value and policy head.
bias=True,
# (bool) whether to use res connection in dynamics.
res_connection_in_dynamics=True,
# (str) The type of normalization in MuZero model. Options are ['BN', 'LN']. Default to 'BN'.
norm_type='BN',
# (bool) Whether to analyze simulation normalization.
analysis_sim_norm=False,
# (int) The save interval of the model.
learn=dict(learner=dict(hook=dict(save_ckpt_after_iter=10000, ), ), ),
world_model_cfg=dict(
# (str) The encoder type, e.g., 'resnet' or 'vit'.
encoder_type='resnet',
# (bool) If True, the action space of the environment is continuous, otherwise discrete.
continuous_action_space=False,
# (int) The number of tokens per block.
tokens_per_block=2,
# (int) The maximum number of blocks.
max_blocks=10,
# (int) The maximum number of tokens, calculated as tokens per block multiplied by max blocks.
max_tokens=2 * 10,
# (int) The context length, usually calculated as twice the number of some base unit.
context_length=2 * 4,
# (bool) Whether to use GRU gating mechanism.
gru_gating=False,
# (str) The device to be used for computation, e.g., 'cpu' or 'cuda'.
device='cpu',
# (bool) Whether to analyze simulation normalization.
analysis_sim_norm=False,
# (bool) Whether to analyze dormant ratio, average_weight_magnitude of net, effective_rank of latent.
analysis_dormant_ratio_weight_rank=False,
# (int) The shape of the action space.
action_space_size=6,
# (int) The size of the group, related to simulation normalization.
group_size=8, # NOTE: sim_norm
# (str) The type of attention mechanism used. Options could be ['causal'].
attention='causal',
# (int) The number of layers in the model.
num_layers=2,
# (int) The number of attention heads.
num_heads=8,
# (int) The dimension of the embedding.
embed_dim=768,
# (float) The dropout probability for the embedding layer.
embed_pdrop=0.1,
# (float) The dropout probability for the residual connections.
resid_pdrop=0.1,
# (float) The dropout probability for the attention mechanism.
attn_pdrop=0.1,
# (int) The size of the support set for value and reward heads.
support_size=101,
# (int) The maximum size of the cache.
max_cache_size=5000,
# (int) The number of environments.
env_num=8,
# (float) The weight of the latent reconstruction loss.
latent_recon_loss_weight=0.,
# (float) The weight of the perceptual loss.
perceptual_loss_weight=0.,
# (float) The weight of the policy entropy loss.
policy_entropy_weight=0,
# (str) The normalization type for the final layer in both the head and the encoder.
# This option must be the same for both 'final_norm_option_in_head' and 'final_norm_option_in_encoder'.
# Valid options are 'LayerNorm' and 'SimNorm'.
# When set to 'LayerNorm', the 'predict_latent_loss_type' should be 'mse'.
# When set to 'SimNorm', the 'predict_latent_loss_type' should be 'group_kl'.
final_norm_option_in_head="LayerNorm",
final_norm_option_in_encoder="LayerNorm",
# (str) The type of loss function for predicting latent variables.
# Options are 'mse' (Mean Squared Error) or 'group_kl' (Group Kullback-Leibler divergence).
# This choice is dependent on the normalization method selected above.
predict_latent_loss_type='mse',
# (str) The type of observation. Options are ['image', 'vector'].
obs_type='image',
# (float) The discount factor for future rewards.
gamma=1,
# (float) The threshold for a dormant neuron.
dormant_threshold=0.025,
# (bool) Whether to use Rotary Position Embedding (RoPE) for relative position encoding.
# If False, nn.Embedding is used for absolute position encoding.
# For more details on RoPE, refer to the author's blog: https://spaces.ac.cn/archives/8265/
# TODO: If you want to use rotary_emb in an environment, you need to include the timestep as a return key from the environment.
rotary_emb=False,
# (int) The base value for calculating RoPE angles. Commonly set to 10000.
rope_theta=10000,
# (int) The maximum sequence length for position encoding.
max_seq_len=8192,
# (int) The rank parameter for LoRA (Low-Rank Adaptation). Set to 0 to disable LoRA.
lora_r=0,
# (float) The alpha parameter for LoRA scaling.
lora_alpha=1,
# (float) The dropout probability for LoRA layers.
lora_dropout=0.0,
# Controls where to compute reconstruction loss: 'after_backbone', 'before_backbone', or None.
# - after_backbone: The reconstruction loss is computed after the encoded representation passes through the backbone.
# - before_backbone: The reconstruction loss is computed directly on the encoded representation, without the backbone.
decode_loss_mode=None,
# (str/None) Task embedding option. Set to None to disable task-specific embeddings. Options are ['concat_task_embed', 'add_task_embed', 'register_task_embed'].
# Please note that "register_task_embed" has not yet been fully tested.
task_embed_option=None,
# (bool) Whether to use task embeddings.
use_task_embed=False,
# TODO: optimize the following configs.
# (bool) Whether to use normal head (standard prediction heads).
use_normal_head=True,
# (bool) Whether to use Soft Mixture-of-Experts (MoE) head.
use_softmoe_head=False,
# (bool) Whether to use Mixture-of-Experts (MoE) head.
use_moe_head=False,
# (int) Number of experts in the MoE head.
num_experts_in_moe_head=4,
# (bool) Whether to use MoE in the transformer layers.
moe_in_transformer=False,
# (bool) Whether to use multiplicative MoE in the transformer layers.
multiplication_moe_in_transformer=False,
# (int) Number of shared experts in MoE.
n_shared_experts=1,
# (int) Number of experts to use per token in MoE.
num_experts_per_tok=1,
# (int) Total number of experts in the transformer MoE.
num_experts_of_moe_in_transformer=8,
# ****** Priority ******
# (bool) Whether to use priority when sampling training data from the buffer.
use_priority=False,
),
),
# ****** common ******
# (bool) Whether to enable adaptive policy entropy weight (alpha)
use_adaptive_entropy_weight=True,
# (float) Learning rate for adaptive alpha optimizer
adaptive_entropy_alpha_lr=1e-3,
# (float) Target entropy ratio at the start of training (higher = more exploration)
target_entropy_start_ratio=0.98,
# (float) Target entropy ratio at the end of training (lower = more exploitation)
target_entropy_end_ratio=0.05,
# (int) Number of training steps to decay target entropy from start to end ratio
target_entropy_decay_steps=500000,
# ==================== START: Encoder-Clip Annealing Config ====================
# (bool) Whether to enable annealing for encoder-clip values.
use_encoder_clip_annealing=True,
# (str) Annealing type. Options: 'linear' or 'cosine'.
encoder_clip_anneal_type='cosine',
# (float) Starting clip value for annealing (looser in early training).
encoder_clip_start_value=30.0,
# (float) Ending clip value for annealing (stricter in later training).
encoder_clip_end_value=10.0,
# (int) Training iteration steps required to complete annealing from start to end value.
encoder_clip_anneal_steps=100000, # e.g., reach final value after 100k iterations
# (float) Fixed latent norm clip threshold (used when encoder_clip_annealing is disabled)
latent_norm_clip_threshold=20.0,
# ===================== END: Encoder-Clip Annealing Config =====================
# ==================== START: Head-Clip Annealing Config ====================
# NOTE: The usage and implementation of Head-Clip may need to be optimized
# (bool) Whether to enable head-clip (dynamically clip head output range)
use_head_clip=False, # Disabled by default
# Detailed Head-Clip configuration
head_clip_config=dict(
enabled=False,
# Specify heads that need clipping (optional, defaults to empty list)
enabled_heads=[], # Example: ['policy', 'value', 'rewards']
# Detailed configuration for each head (optional)
head_configs={
# 'policy': {
# 'use_annealing': True,
# 'anneal_type': 'cosine', # 'cosine' or 'linear'
# 'start_value': 30.0, # Loose in early phase
# 'end_value': 10.0, # Strict in later phase
# 'anneal_steps': 500000,
# },
# 'value': {
# 'clip_threshold': 20.0,
# 'use_annealing': False,
# },
},
# Monitoring configuration
monitor_freq=1, # Check every iteration
log_freq=1000, # Print log every 1000 iterations
),
# ===================== END: Head-Clip Annealing Config =====================
# ==================== START: Policy Label Smoothing Config ====================
# (float) Starting epsilon value for policy label smoothing (higher = more smoothing)
policy_ls_eps_start=0.05,
# (float) Ending epsilon value for policy label smoothing (lower = less smoothing)
policy_ls_eps_end=0.01,
# (int) Number of training steps to decay label smoothing epsilon from start to end
policy_ls_eps_decay_steps=50000,
label_smoothing_eps=0.1, # TODO: For value
# (bool) Whether to use continuous (fixed) label smoothing throughout training
use_continuous_label_smoothing=False,
# (float) Fixed epsilon value for continuous label smoothing (only used when use_continuous_label_smoothing=True)
continuous_ls_eps=0.05,
# ===================== END: Policy Label Smoothing Config =====================
# ==================== START: Learning Rate Scheduler Config ====================
# (int) Total training iterations for cosine annealing LR scheduler (only used when cos_lr_scheduler=True)
total_iterations=500000,
# (float) Final learning rate for cosine annealing LR scheduler (only used when cos_lr_scheduler=True)
final_learning_rate=4e-5,
# ===================== END: Learning Rate Scheduler Config =====================
# ==================== START: Monitoring Config ====================
# (int) Frequency of monitoring model parameter and gradient norms (in training iterations). Set to 0 to disable.
monitor_norm_freq=5000,
# (bool) Whether to enable enhanced policy monitoring (logits statistics, target policy entropy, etc.)
use_enhanced_policy_monitoring=False,
# ===================== END: Monitoring Config =====================
# (bool) whether to use rnd model.
use_rnd_model=False,
# (bool) Whether to use multi-gpu training.
multi_gpu=False,
# (bool) Whether to enable the sampled-based algorithm (e.g. Sampled EfficientZero)
# this variable is used in ``collector``.
sampled_algo=False,
# (bool) Whether to enable the gumbel-based algorithm (e.g. Gumbel Muzero)
gumbel_algo=False,
# (bool) Whether to use C++ MCTS in policy. If False, use Python implementation.
mcts_ctree=True,
# (bool) Whether to use cuda for network.
cuda=True,
# (int) The number of environments used in collecting data.
collector_env_num=8,
# (int) The number of environments used in evaluating policy.
evaluator_env_num=3,
# (str) The type of environment. Options are ['not_board_games', 'board_games'].
env_type='not_board_games',
# (str) The type of action space. Options are ['fixed_action_space', 'varied_action_space'].
action_type='fixed_action_space',
# (str) The type of battle mode. Options are ['play_with_bot_mode', 'self_play_mode'].
battle_mode='play_with_bot_mode',
# (bool) Whether to monitor extra statistics in tensorboard.
monitor_extra_statistics=True,
# (int) The transition number of one ``GameSegment``.
game_segment_length=400,
# (bool) Whether to analyze simulation normalization.
analysis_sim_norm=False,
# (bool) Whether to use the pure policy to collect data.
collect_with_pure_policy=False,
# (int) The evaluation frequency.
eval_freq=int(5e3),
# (str) The sample type. Options are ['episode', 'transition'].
sample_type='transition',
# ****** observation ******
# (bool) Whether to transform image to string to save memory.
transform2string=False,
# (bool) Whether to use gray scale image.
gray_scale=False,
# (bool) Whether to use data augmentation.
use_augmentation=False,
# (list) The style of augmentation.
augmentation=['shift', 'intensity'],
# ******* learn ******
# (bool) Whether to ignore the done flag in the training data. Typically, this value is set to False.
# However, for some environments with a fixed episode length, to ensure the accuracy of Q-value calculations,
# we should set it to True to avoid the influence of the done flag.
ignore_done=False,
# (int) How many updates(iterations) to train after collector's one collection.
# Bigger "update_per_collect" means bigger off-policy.
# collect data -> update policy-> collect data -> ...
# For different env, we have different episode_length,
# we usually set update_per_collect = collector_env_num * episode_length / batch_size * reuse_factor.
# If we set update_per_collect=None, we will set update_per_collect = collected_transitions_num * cfg.policy.replay_ratio automatically.
update_per_collect=None,
# (float) The ratio of the collected data used for training. Only effective when ``update_per_collect`` is not None.
replay_ratio=0.25,
# (int) Minibatch size for one gradient descent.
batch_size=256,
# (str) Optimizer for training policy network.
optim_type='AdamW',
# (float) Learning rate for training policy network. Initial lr for manually decay schedule.
learning_rate=0.0001,
# (int) Frequency of hard target network update.
target_update_freq=100,
# (int) Frequency of soft target network update.
target_update_theta=0.05,
# (int) Frequency of target network update.
target_update_freq_for_intrinsic_reward=1000,
# (float) Weight decay for training policy network.
weight_decay=1e-4,
# (float) One-order Momentum in optimizer, which stabilizes the training process (gradient direction).
momentum=0.9,
# (float) The maximum constraint value of gradient norm clipping.
grad_clip_value=20,
# (int) The number of episodes in each collecting stage when use muzero_collector.
n_episode=8,
# (int) The number of num_segments in each collecting stage when use muzero_segment_collector.
num_segments=8,
# (int) the number of simulations in MCTS for renalyze.
num_simulations=50,
# (int) The number of simulations in MCTS for the collect phase.
collect_num_simulations=25,
# (int) The number of simulations in MCTS for the eval phase.
eval_num_simulations=50,
# (float) Discount factor (gamma) for returns.
discount_factor=0.997,
# (int) The number of steps for calculating target q_value.
td_steps=5,
# (int) The number of unroll steps in dynamics network.
num_unroll_steps=10,
# (float) The weight of reward loss.
reward_loss_weight=1,
# (float) The weight of value loss.
value_loss_weight=0.25,
# (float) The weight of policy loss.
policy_loss_weight=1,
# (float) The weight of ssl (self-supervised learning) loss.
ssl_loss_weight=0,
# (bool) Whether to use the cosine learning rate decay.
cos_lr_scheduler=False,
# (bool) Whether to use piecewise constant learning rate decay.
# i.e. lr: 0.2 -> 0.02 -> 0.002
piecewise_decay_lr_scheduler=False,
# (int) The number of final training iterations to control lr decay, which is only used for manually decay.
threshold_training_steps_for_final_lr=int(5e4),
# (bool) Whether to use manually decayed temperature.
manual_temperature_decay=False,
# (int) The number of final training iterations to control temperature, which is only used for manually decay.
threshold_training_steps_for_final_temperature=int(5e4),
# (float) The fixed temperature value for MCTS action selection, which is used to control the exploration.
# The larger the value, the more exploration. This value is only used when manual_temperature_decay=False.
fixed_temperature_value=0.25,
# (bool) Whether to use the true chance in MCTS in some environments with stochastic dynamics, such as 2048.
use_ture_chance_label_in_chance_encoder=False,
# (int) The number of steps to accumulate gradients before performing an optimization step.
accumulation_steps=1,
# ****** Priority ******
# (bool) Whether to use priority when sampling training data from the buffer.
use_priority=False,
# (float) The degree of prioritization to use. A value of 0 means no prioritization,
# while a value of 1 means full prioritization.
priority_prob_alpha=0.6,
# (float) The degree of correction to use. A value of 0 means no correction,
# while a value of 1 means full correction.
priority_prob_beta=0.4,
# (int) The initial Env Steps for training.
train_start_after_envsteps=int(0),
# (bool) Whether to use task_exploitation_weight.
use_task_exploitation_weight=False,
# ****** UCB ******
# (float) The alpha value used in the Dirichlet distribution for exploration at the root node of search tree.
root_dirichlet_alpha=0.3,
# (float) The noise weight at the root node of the search tree.
root_noise_weight=0.25,
# ****** Explore by random collect ******
# (int) The number of episodes to collect data randomly before training.
random_collect_episode_num=0,
# ****** Explore by eps greedy ******
eps=dict(
# (bool) Whether to use eps greedy exploration in collecting data.
eps_greedy_exploration_in_collect=False,
# (str) The type of decaying epsilon. Options are 'linear', 'exp'.
type='linear',
# (float) The start value of eps.
start=1.,
# (float) The end value of eps.
end=0.05,
# (int) The decay steps from start to end eps.
decay=int(1e5),
),
)
def default_model(self) -> Tuple[str, List[str]]:
"""
Overview:
Return this algorithm default model setting for demonstration.
Returns:
- model_info (:obj:`Tuple[str, List[str]]`): model name and model import_names.
- model_type (:obj:`str`): The model type used in this algorithm, which is registered in ModelRegistry.
- import_names (:obj:`List[str]`): The model class path list used in this algorithm.
.. note::
The user can define and use customized network model but must obey the same interface definition indicated \
by import_names path. For MuZero, ``lzero.model.unizero_model.MuZeroModel``
"""
return 'UniZeroModel', ['lzero.model.unizero_model']
# ==================== Model Norm Monitoring Function ====================
def _monitor_model_norms(self) -> Dict[str, float]:
"""
Overview:
Calculate and return parameter matrix norms for key model components (Encoder, Transformer, Heads).
This function should be called within a torch.no_grad() context for efficiency.
Returns:
- norm_metrics (:obj:`Dict[str, float]`): Dictionary containing all norm metrics for logging.
"""
world_model = self._learn_model.world_model
norm_metrics = {}
# Define module groups to monitor
module_groups = {
'encoder': world_model.tokenizer.encoder,
'transformer': world_model.transformer,
'head_value': world_model.head_value,
'head_reward': world_model.head_rewards,
'head_policy': world_model.head_policy,
}
for group_name, group_module in module_groups.items():
total_norm_sq = 0.0
for param_name, param in group_module.named_parameters():
if param.requires_grad:
# Calculate L2 norm for single layer parameters
param_norm = param.data.norm(2).item()
# Replace dots to display correctly as hierarchy in TensorBoard
log_name = f'norm/{group_name}/{param_name.replace(".", "/")}'
norm_metrics[log_name] = param_norm
total_norm_sq += param_norm ** 2
# Calculate total norm for entire module
total_group_norm = np.sqrt(total_norm_sq)
norm_metrics[f'norm/{group_name}/_total_norm'] = total_group_norm
return norm_metrics
def _monitor_gradient_norms(self) -> Dict[str, float]:
"""
Overview:
Calculate and return gradient norms for key model components.
This function should be called after gradient computation and before parameter updates.
Returns:
- grad_metrics (:obj:`Dict[str, float]`): Dictionary containing all gradient norm metrics for logging.
"""
world_model = self._learn_model.world_model
grad_metrics = {}
# Define module groups to monitor
module_groups = {
'encoder': world_model.tokenizer.encoder,
'transformer': world_model.transformer,
'head_value': world_model.head_value,
'head_reward': world_model.head_rewards,
'head_policy': world_model.head_policy,
}
for group_name, group_module in module_groups.items():
total_grad_norm_sq = 0.0
num_params_with_grad = 0
for param_name, param in group_module.named_parameters():
if param.requires_grad and param.grad is not None:
# Calculate L2 norm for single layer parameter gradients
grad_norm = param.grad.data.norm(2).item()
# Replace dots to display correctly as hierarchy in TensorBoard
log_name = f'grad/{group_name}/{param_name.replace(".", "/")}'
grad_metrics[log_name] = grad_norm
total_grad_norm_sq += grad_norm ** 2
num_params_with_grad += 1
# Calculate total gradient norm for entire module
if num_params_with_grad > 0:
total_group_grad_norm = np.sqrt(total_grad_norm_sq)
grad_metrics[f'grad/{group_name}/_total_norm'] = total_group_grad_norm
else:
grad_metrics[f'grad/{group_name}/_total_norm'] = 0.0
return grad_metrics
# =================================================================
def _init_learn(self) -> None:
"""
Overview:
Learn mode init method. Called by ``self.__init__``. Initialize the learn model, optimizer and MCTS utils.
"""
if self._cfg.optim_type == 'SGD':
# Configure SGD optimizer
self._optimizer_world_model = torch.optim.SGD(
self._model.world_model.parameters(),
lr=self._cfg.learning_rate,
momentum=self._cfg.momentum,
weight_decay=self._cfg.weight_decay
)
elif self._cfg.optim_type == 'AdamW':
# NOTE: nanoGPT optimizer
self._optimizer_world_model = configure_optimizers_nanogpt(
model=self._model.world_model,
learning_rate=self._cfg.learning_rate,
weight_decay=self._cfg.weight_decay,
device_type=self._cfg.device,
betas=(0.9, 0.95),
)
elif self._cfg.optim_type == 'AdamW_mix_lr_wdecay':
self._optimizer_world_model = configure_optimizer_unizero(
model=self._model.world_model,
learning_rate=self._cfg.learning_rate,
weight_decay=self._cfg.weight_decay,
device_type=self._cfg.device,
betas=(0.9, 0.95),
)
if self._cfg.cos_lr_scheduler:
from torch.optim.lr_scheduler import CosineAnnealingLR
total_iters = self._cfg.total_iterations
final_lr = self._cfg.final_learning_rate
self.lr_scheduler = CosineAnnealingLR(
self._optimizer_world_model,
T_max=total_iters,
eta_min=final_lr
)
logging.info(f"CosineAnnealingLR enabled: T_max={total_iters}, eta_min={final_lr}")
if self._cfg.piecewise_decay_lr_scheduler:
from torch.optim.lr_scheduler import LambdaLR
max_step = self._cfg.threshold_training_steps_for_final_lr
# NOTE: the 1, 0.1, 0.01 is the decay rate, not the lr.
lr_lambda = lambda step: 1 if step < max_step * 0.5 else (0.1 if step < max_step else 0.01) # noqa
self.lr_scheduler = LambdaLR(self._optimizer_world_model, lr_lambda=lr_lambda)
# use model_wrapper for specialized demands of different modes
self._target_model = copy.deepcopy(self._model)
# Ensure that the installed torch version is greater than or equal to 2.0
assert int(''.join(filter(str.isdigit, torch.__version__))) >= 200, "We need torch version >= 2.0"
self._model = torch.compile(self._model)
self._target_model = torch.compile(self._target_model)
# NOTE: soft target
self._target_model = model_wrap(
self._target_model,
wrapper_name='target',
update_type='momentum',
update_kwargs={'theta': self._cfg.target_update_theta}
)
self._learn_model = self._model
if self._cfg.use_augmentation:
self.image_transforms = ImageTransforms(
self._cfg.augmentation,
image_shape=(self._cfg.model.observation_shape[1], self._cfg.model.observation_shape[2])
)
self.value_support = DiscreteSupport(*self._cfg.model.value_support_range, self._cfg.device)
self.reward_support = DiscreteSupport(*self._cfg.model.reward_support_range, self._cfg.device)
self.value_inverse_scalar_transform_handle = InverseScalarTransform(self.value_support, self._cfg.model.categorical_distribution)
self.reward_inverse_scalar_transform_handle = InverseScalarTransform(self.reward_support, self._cfg.model.categorical_distribution)
self.intermediate_losses = defaultdict(float)
self.l2_norm_before = 0.
self.l2_norm_after = 0.
self.grad_norm_before = 0.
self.grad_norm_after = 0.
if self._cfg.model.model_type == 'conv':
# for image-input env
self.pad_token_id = -1
else:
# for text-input env and vector-input env
# Retrieve the tokenizer from the encoder module if it exists
encoder_tokenizer = getattr(self._model.tokenizer.encoder, 'tokenizer', None)
# Extract the padding token ID from the tokenizer if available, otherwise use 0 as default. Used in _reset_collect()
# The pad_token_id is used to identify padding tokens in sequences, which is essential for:
# 1. Masking padded positions during attention computation to prevent them from affecting the output
# 2. Properly handling variable-length sequences in batch processing
# 3. Distinguishing between actual tokens and padding in loss calculation
# Default value 0 is a common convention when no specific padding token is defined
self.pad_token_id = encoder_tokenizer.pad_token_id if encoder_tokenizer is not None else 0
if self._cfg.use_wandb:
# TODO: add the model to wandb
wandb.watch(self._learn_model.representation_network, log="all")
self.accumulation_steps = self._cfg.accumulation_steps
# ==================== START: Target Entropy Regularization Initialization ====================
# Read whether to enable adaptive alpha from config, and provide a default value
self.use_adaptive_entropy_weight = self._cfg.use_adaptive_entropy_weight
# Add configuration in _init_learn
self.target_entropy_start_ratio = self._cfg.target_entropy_start_ratio
self.target_entropy_end_ratio = self._cfg.target_entropy_end_ratio
self.target_entropy_decay_steps = self._cfg.target_entropy_decay_steps # e.g., complete annealing within 200k steps (2M envsteps)
if self.use_adaptive_entropy_weight:
# 1. Set target entropy. For discrete action spaces, a common heuristic is the negative logarithm
# of action space dimension multiplied by a coefficient.
# This coefficient (e.g., 0.98) can be used as a hyperparameter.
action_space_size = self._cfg.model.action_space_size
self.target_entropy = -np.log(1.0 / action_space_size) * 0.98
# 2. Initialize a learnable log_alpha parameter.
# Initialized to 0, meaning initial alpha = exp(0) = 1.0.
self.log_alpha = torch.nn.Parameter(torch.zeros(1, device=self._cfg.device), requires_grad=True)
# 3. Create a dedicated optimizer for log_alpha.
# Using a smaller learning rate (e.g., 1e-4) different from the main optimizer is usually more stable.
alpha_lr = self._cfg.adaptive_entropy_alpha_lr
self.alpha_optimizer = torch.optim.Adam([self.log_alpha], lr=alpha_lr)
logging.info("="*20)
logging.info(">>> Target Entropy Regularization (Adaptive Alpha) Enabled <<<")
logging.info(f" Target Entropy: {self.target_entropy:.4f}")
logging.info(f" Alpha Optimizer Learning Rate: {alpha_lr:.2e}")
logging.info("="*20)
# ===================== END: Target Entropy Regularization Initialization =====================
# ==================== START: Initialize Encoder-Clip Annealing Parameters ====================
self.use_encoder_clip_annealing = self._cfg.use_encoder_clip_annealing
self.latent_norm_clip_threshold = self._cfg.latent_norm_clip_threshold # TODO
if self.use_encoder_clip_annealing:
self.encoder_clip_anneal_type = self._cfg.encoder_clip_anneal_type
self.encoder_clip_start = self._cfg.encoder_clip_start_value
self.encoder_clip_end = self._cfg.encoder_clip_end_value
self.encoder_clip_anneal_steps = self._cfg.encoder_clip_anneal_steps
logging.info("="*20)
logging.info(">>> Encoder-Clip Annealing Enabled <<<")
logging.info(f" Type: {self.encoder_clip_anneal_type}")
logging.info(f" Range: {self.encoder_clip_start} -> {self.encoder_clip_end}")
logging.info(f" Steps: {self.encoder_clip_anneal_steps}")
logging.info("="*20)
else:
# If annealing is not enabled, use a fixed clip threshold
self.latent_norm_clip_threshold = self._cfg.latent_norm_clip_threshold
# ===================== END: Initialize Encoder-Clip Annealing Parameters =====================
# ==================== START: Initialize Head-Clip Manager ====================
self.use_head_clip = self._cfg.use_head_clip
if self.use_head_clip:
head_clip_config_dict = self._cfg.head_clip_config
# Ensure enabled is consistent with top-level configuration
head_clip_config_dict['enabled'] = self.use_head_clip
# Create HeadClipManager
self.head_clip_manager = create_head_clip_manager_from_dict(head_clip_config_dict)
logging.info("=" * 60)
logging.info(">>> Head-Clip Manager Initialized <<<")
logging.info(f" Enabled heads: {self.head_clip_manager.enabled_heads}")
for head_name in self.head_clip_manager.enabled_heads:
config = self.head_clip_manager.get_head_config(head_name)
if config.use_annealing:
logging.info(
f" {head_name}: annealing {config.start_value:.1f} → {config.end_value:.1f} "
f"over {config.anneal_steps} steps ({config.anneal_type})"
)
else:
logging.info(f" {head_name}: fixed threshold = {config.clip_threshold:.1f}")
logging.info("=" * 60)
else:
self.head_clip_manager = None
# ===================== END: Initialize Head-Clip Manager =====================
# Policy Label Smoothing Parameters
self.policy_ls_eps_start = self._cfg.policy_ls_eps_start
self.policy_ls_eps_end = self._cfg.policy_ls_eps_end
self.policy_ls_eps_decay_steps = self._cfg.policy_ls_eps_decay_steps
logging.info(f"self.policy_ls_eps_start: {self.policy_ls_eps_start}")
def _forward_learn(self, data: Tuple[torch.Tensor]) -> Dict[str, Union[float, int]]:
"""
Overview:
The forward function for learning policy in learn mode, which is the core of the learning process.
The data is sampled from replay buffer.
The loss is calculated by the loss function and the loss is backpropagated to update the model.
Arguments:
- data (:obj:`Tuple[torch.Tensor]`): The data sampled from replay buffer, which is a tuple of tensors.
The first tensor is the current_batch, the second tensor is the target_batch.
Returns:
- info_dict (:obj:`Dict[str, Union[float, int]]`): The information dict to be logged, which contains \
current learning loss and learning statistics.
"""
self._learn_model.train()
self._target_model.train()
current_batch, target_batch, train_iter = data
obs_batch_ori, action_batch, target_action_batch, mask_batch, indices, weights, make_time, timestep_batch = current_batch
target_reward, target_value, target_policy = target_batch
# Calculate current epsilon for policy label smoothing
# ==================== Continuous Label Smoothing ====================
use_continuous_label_smoothing = self._cfg.use_continuous_label_smoothing
if use_continuous_label_smoothing:
# Use fixed high epsilon throughout training
current_policy_label_eps = self._cfg.continuous_ls_eps
else:
# Use original decay schedule
if self.policy_ls_eps_start > 0:
progress = min(1.0, train_iter / self.policy_ls_eps_decay_steps)
current_policy_label_eps = self.policy_ls_eps_start * (1 - progress) + self.policy_ls_eps_end * progress
else:
current_policy_label_eps = 0.0
# ================================================================================
# Prepare observations based on frame stack number
if self._cfg.model.frame_stack_num > 1:
obs_batch, obs_target_batch = prepare_obs_stack_for_unizero(obs_batch_ori, self._cfg)
else:
obs_batch, obs_target_batch = prepare_obs(obs_batch_ori, self._cfg)
# Apply augmentations if needed
if self._cfg.use_augmentation:
obs_batch = self.image_transforms.transform(obs_batch)
if self._cfg.model.self_supervised_learning_loss:
obs_target_batch = self.image_transforms.transform(obs_target_batch)
# Prepare action batch and convert to torch tensor
action_batch = torch.from_numpy(action_batch).to(self._cfg.device).unsqueeze(
-1).long() # For discrete action space
timestep_batch = torch.from_numpy(timestep_batch).to(self._cfg.device).unsqueeze(
-1).long()
data_list = [mask_batch, target_reward, target_value, target_policy, weights]
mask_batch, target_reward, target_value, target_policy, weights = to_torch_float_tensor(data_list,
self._cfg.device)
target_reward = target_reward.view(self._cfg.batch_size, -1)
target_value = target_value.view(self._cfg.batch_size, -1)
# Transform rewards and values to their scaled forms
transformed_target_reward = scalar_transform(target_reward)
transformed_target_value = scalar_transform(target_value)
# Convert to categorical distributions
target_reward_categorical = phi_transform(self.reward_support, transformed_target_reward, label_smoothing_eps= self._cfg.label_smoothing_eps)
target_value_categorical = phi_transform(self.value_support, transformed_target_value, label_smoothing_eps=self._cfg.label_smoothing_eps)
# Prepare batch for GPT model
batch_for_gpt = {}
if isinstance(self._cfg.model.observation_shape, int) or len(self._cfg.model.observation_shape) == 1:
batch_for_gpt['observations'] = torch.cat((obs_batch, obs_target_batch), dim=1).reshape(
self._cfg.batch_size, -1, self._cfg.model.observation_shape)
elif len(self._cfg.model.observation_shape) == 3:
batch_for_gpt['observations'] = torch.cat((obs_batch, obs_target_batch), dim=1).reshape(
self._cfg.batch_size, -1, *self._cfg.model.observation_shape)
batch_for_gpt['actions'] = action_batch.squeeze(-1)
batch_for_gpt['timestep'] = timestep_batch.squeeze(-1)
batch_for_gpt['rewards'] = target_reward_categorical[:, :-1]
batch_for_gpt['mask_padding'] = mask_batch == 1.0 # 0 means invalid padding data
batch_for_gpt['mask_padding'] = batch_for_gpt['mask_padding'][:, :-1]
batch_for_gpt['observations'] = batch_for_gpt['observations'][:, :-1]
batch_for_gpt['ends'] = torch.zeros(batch_for_gpt['mask_padding'].shape, dtype=torch.long,
device=self._cfg.device)
batch_for_gpt['target_value'] = target_value_categorical[:, :-1]
# ==================== Apply Policy Label Smoothing ====================
# This was previously computed but never applied. Now we actually smooth the target_policy.
smoothed_target_policy = target_policy[:, :-1]
if current_policy_label_eps > 0:
num_actions = smoothed_target_policy.shape[-1]
uniform_dist = torch.ones_like(smoothed_target_policy) / num_actions
smoothed_target_policy = (1.0 - current_policy_label_eps) * smoothed_target_policy + \
current_policy_label_eps * uniform_dist
batch_for_gpt['target_policy'] = smoothed_target_policy
# ===================================================================================
batch_for_gpt['scalar_target_value'] = target_value
# Extract valid target policy data and compute entropy
valid_target_policy = batch_for_gpt['target_policy'][batch_for_gpt['mask_padding']]
target_policy_entropy = -torch.sum(valid_target_policy * torch.log(valid_target_policy + 1e-9), dim=-1)
average_target_policy_entropy = target_policy_entropy.mean()
# Update world model
losses = self._learn_model.world_model.compute_loss(
batch_for_gpt, self._target_model.world_model.tokenizer, self.value_inverse_scalar_transform_handle, global_step=train_iter, current_policy_label_eps=current_policy_label_eps,
)
# ==================== Integrate norm monitoring logic ====================
norm_log_dict = {}
# Check if monitoring frequency is reached
if self._cfg.monitor_norm_freq > 0 and (train_iter == 0 or (train_iter % self._cfg.monitor_norm_freq == 0)):
with torch.no_grad():
# 1. Monitor model parameter norms
param_norm_metrics = self._monitor_model_norms()
norm_log_dict.update(param_norm_metrics)
# 2. Monitor intermediate tensor x (Transformer output)
intermediate_x = losses.intermediate_losses.get('intermediate_tensor_x')
if intermediate_x is not None:
# x shape is (B, T, E)
# Calculate L2 norm for each token
token_norms = intermediate_x.norm(p=2, dim=-1)
# Record statistics of these norms
norm_log_dict['norm/x_token/mean'] = token_norms.mean().item()
norm_log_dict['norm/x_token/std'] = token_norms.std().item()
norm_log_dict['norm/x_token/max'] = token_norms.max().item()
norm_log_dict['norm/x_token/min'] = token_norms.min().item()
# 3. Monitor detailed statistics of logits (Value, Policy, Reward)
logits_value = losses.intermediate_losses.get('logits_value')
if logits_value is not None:
norm_log_dict['logits/value/mean'] = logits_value.mean().item()
norm_log_dict['logits/value/std'] = logits_value.std().item()
norm_log_dict['logits/value/max'] = logits_value.max().item()
norm_log_dict['logits/value/min'] = logits_value.min().item()
norm_log_dict['logits/value/abs_max'] = logits_value.abs().max().item()
logits_policy = losses.intermediate_losses.get('logits_policy')
if logits_policy is not None:
norm_log_dict['logits/policy/mean'] = logits_policy.mean().item()
norm_log_dict['logits/policy/std'] = logits_policy.std().item()
norm_log_dict['logits/policy/max'] = logits_policy.max().item()
norm_log_dict['logits/policy/min'] = logits_policy.min().item()
norm_log_dict['logits/policy/abs_max'] = logits_policy.abs().max().item()
logits_reward = losses.intermediate_losses.get('logits_reward')
if logits_reward is not None:
norm_log_dict['logits/reward/mean'] = logits_reward.mean().item()
norm_log_dict['logits/reward/std'] = logits_reward.std().item()
norm_log_dict['logits/reward/max'] = logits_reward.max().item()
norm_log_dict['logits/reward/min'] = logits_reward.min().item()
norm_log_dict['logits/reward/abs_max'] = logits_reward.abs().max().item()
# 4. Monitor obs_embeddings (Encoder output) statistics
obs_embeddings = losses.intermediate_losses.get('obs_embeddings')
if obs_embeddings is not None:
# Calculate L2 norm for each embedding
emb_norms = obs_embeddings.norm(p=2, dim=-1)
norm_log_dict['embeddings/obs/norm_mean'] = emb_norms.mean().item()
norm_log_dict['embeddings/obs/norm_std'] = emb_norms.std().item()
norm_log_dict['embeddings/obs/norm_max'] = emb_norms.max().item()
norm_log_dict['embeddings/obs/norm_min'] = emb_norms.min().item()
# ==================== Early Warning System ====================
# Detect potential training instability and issue warnings
warnings_issued = []
# Check 1: Policy logits explosion (should be caught by clip, but warn anyway)
if 'logits/policy/abs_max' in norm_log_dict:
policy_abs_max = norm_log_dict['logits/policy/abs_max']
if policy_abs_max > 8.0:
warnings_issued.append(f"⚠️ CRITICAL: Policy logits explosion detected! abs_max={policy_abs_max:.2f} (threshold: 8.0)")
elif policy_abs_max > 5.0:
warnings_issued.append(f"⚠️ WARNING: Policy logits getting large! abs_max={policy_abs_max:.2f} (threshold: 5.0)")
# Check 2: Embedding norm explosion
if 'embeddings/obs/norm_std' in norm_log_dict:
emb_norm_std = norm_log_dict['embeddings/obs/norm_std']
if emb_norm_std > 10.0:
warnings_issued.append(f"⚠️ CRITICAL: Embedding norm std explosion! std={emb_norm_std:.2f} (threshold: 10.0)")
elif emb_norm_std > 5.0:
warnings_issued.append(f"⚠️ WARNING: Embedding norm std increasing! std={emb_norm_std:.2f} (threshold: 5.0)")
# Check 3: X token norm collapse
if 'norm/x_token/std' in norm_log_dict:
x_token_std = norm_log_dict['norm/x_token/std']
if x_token_std < 0.1:
warnings_issued.append(f"⚠️ CRITICAL: X token norm collapse! std={x_token_std:.4f} (threshold: 0.1)")
elif x_token_std < 0.5:
warnings_issued.append(f"⚠️ WARNING: X token norm decreasing! std={x_token_std:.4f} (threshold: 0.5)")
# Log warnings if any
if warnings_issued:
logging.warning(f"\n{'='*80}\n[TRAINING STABILITY] Iteration {train_iter}:\n" + "\n".join(warnings_issued) + f"\n{'='*80}")
norm_log_dict['stability/warning_count'] = float(len(warnings_issued))
else:
norm_log_dict['stability/warning_count'] = 0.0
# ====================================================================
# =================================================================
# Extract the calculated value_priority from the returned losses.
value_priority_tensor = losses.intermediate_losses['value_priority']
# Convert to numpy array for the replay buffer, adding a small epsilon.
value_priority_np = value_priority_tensor.detach().cpu().numpy() + 1e-6
weighted_total_loss = (weights * losses.loss_total).mean()
for loss_name, loss_value in losses.intermediate_losses.items():
self.intermediate_losses[f"{loss_name}"] = loss_value
# Extract losses from intermediate_losses dictionary
obs_loss = self.intermediate_losses['loss_obs']
reward_loss = self.intermediate_losses['loss_rewards']
policy_loss = self.intermediate_losses['loss_policy']