Muqeeth commited on
Commit
ece2fb1
·
verified ·
1 Parent(s): aadfc96

Add files using upload-large-folder tool

Browse files
Files changed (50) hide show
  1. .hydra/config.yaml +240 -0
  2. .hydra/hydra.yaml +154 -0
  3. .hydra/overrides.yaml +1 -0
  4. seed_42/Qwen/Qwen2.5-7B-Instruct/adapters/README.md +207 -0
  5. seed_42/Qwen/Qwen2.5-7B-Instruct/adapters/agent_adapter/adapter_config.json +42 -0
  6. seed_42/Qwen/Qwen2.5-7B-Instruct/adapters/critic_adapter/adapter_config.json +42 -0
  7. src_code_for_reproducibility/__init__.py +4 -0
  8. src_code_for_reproducibility/chat_utils/__pycache__/apply_template.cpython-312.pyc +0 -0
  9. src_code_for_reproducibility/chat_utils/__pycache__/chat_turn.cpython-312.pyc +0 -0
  10. src_code_for_reproducibility/chat_utils/__pycache__/template_specific.cpython-312.pyc +0 -0
  11. src_code_for_reproducibility/markov_games/__pycache__/__init__.cpython-312.pyc +0 -0
  12. src_code_for_reproducibility/markov_games/__pycache__/agent.cpython-312.pyc +0 -0
  13. src_code_for_reproducibility/markov_games/__pycache__/alternative_actions_runner.cpython-312.pyc +0 -0
  14. src_code_for_reproducibility/markov_games/__pycache__/mg_utils.cpython-312.pyc +0 -0
  15. src_code_for_reproducibility/markov_games/ipd/__pycache__/Ipd_hard_coded_agents.cpython-312.pyc +0 -0
  16. src_code_for_reproducibility/markov_games/ipd/__pycache__/__init__.cpython-312.pyc +0 -0
  17. src_code_for_reproducibility/markov_games/ipd/__pycache__/ipd_simulation.cpython-312.pyc +0 -0
  18. src_code_for_reproducibility/markov_games/ipd/__pycache__/ipd_statistics.cpython-312.pyc +0 -0
  19. src_code_for_reproducibility/markov_games/negotiation/README.md +27 -0
  20. src_code_for_reproducibility/markov_games/negotiation/__pycache__/nego_agent.cpython-312.pyc +0 -0
  21. src_code_for_reproducibility/markov_games/negotiation/__pycache__/no_press_nego_agent.cpython-312.pyc +0 -0
  22. src_code_for_reproducibility/markov_games/negotiation/__pycache__/tas_rps_agent.cpython-312.pyc +0 -0
  23. src_code_for_reproducibility/markov_games/negotiation/__pycache__/tas_rps_simulation.cpython-312.pyc +0 -0
  24. src_code_for_reproducibility/markov_games/negotiation/nego_hard_coded_policies.py +70 -0
  25. src_code_for_reproducibility/markov_games/negotiation/negotiation_statistics.py +249 -0
  26. src_code_for_reproducibility/markov_games/negotiation/no_press_nego_agent.py +108 -0
  27. src_code_for_reproducibility/markov_games/negotiation/no_press_nego_simulation.py +182 -0
  28. src_code_for_reproducibility/models/__init__.py +4 -0
  29. src_code_for_reproducibility/models/adapter_training_wrapper.py +104 -0
  30. src_code_for_reproducibility/models/inference_backend.py +44 -0
  31. src_code_for_reproducibility/models/inference_backend_dummy.py +59 -0
  32. src_code_for_reproducibility/models/inference_backend_vllm.py +111 -0
  33. src_code_for_reproducibility/models/large_language_model_local.py +361 -0
  34. src_code_for_reproducibility/models/scalar_critic.py +59 -0
  35. src_code_for_reproducibility/training/tally_rollout.py +116 -0
  36. src_code_for_reproducibility/training/tally_tokenwise.py +278 -0
  37. src_code_for_reproducibility/training/tokenize_chats.py +128 -0
  38. src_code_for_reproducibility/training/trainer_common.py +1032 -0
  39. src_code_for_reproducibility/utils/__pycache__/__init__.cpython-312.pyc +0 -0
  40. src_code_for_reproducibility/utils/__pycache__/dict_get_path.cpython-312.pyc +0 -0
  41. src_code_for_reproducibility/utils/__pycache__/gather_training_stats.cpython-312.pyc +0 -0
  42. src_code_for_reproducibility/utils/__pycache__/get_coagent_id.cpython-312.pyc +0 -0
  43. src_code_for_reproducibility/utils/__pycache__/resource_context.cpython-312.pyc +0 -0
  44. src_code_for_reproducibility/utils/__pycache__/rollout_tree_chat_htmls.cpython-312.pyc +0 -0
  45. src_code_for_reproducibility/utils/__pycache__/rollout_tree_gather_utils.cpython-312.pyc +0 -0
  46. src_code_for_reproducibility/utils/__pycache__/rollout_tree_stats.cpython-312.pyc +0 -0
  47. src_code_for_reproducibility/utils/__pycache__/short_id_gen.cpython-312.pyc +0 -0
  48. src_code_for_reproducibility/utils/__pycache__/stat_pack.cpython-312.pyc +0 -0
  49. src_code_for_reproducibility/utils/__pycache__/update_start_epoch.cpython-312.pyc +0 -0
  50. src_code_for_reproducibility/utils/__pycache__/wandb_utils.cpython-312.pyc +0 -0
.hydra/config.yaml ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ experiment:
2
+ wandb_enabled: true
3
+ nb_epochs: 1000
4
+ nb_matches_per_iteration: 128
5
+ reinit_matches_each_it: true
6
+ checkpoint_every_n_iterations: 50
7
+ start_epoch: 0
8
+ resume_experiment: true
9
+ base_seed: 42
10
+ seed_group_size: 1
11
+ train: true
12
+ stat_methods_for_live_wandb: mllm.markov_games.ipd.ipd_statistics
13
+ name: ipd_vanilla_ad_align_no_agent_buffer_seed42
14
+ agent_buffer: false
15
+ keep_agent_buffer_count: ${lora_count}
16
+ agent_buffer_recent_k: -1
17
+ logging:
18
+ wandb:
19
+ enabled: false
20
+ project: llm-negotiation
21
+ entity: null
22
+ mode: online
23
+ name: null
24
+ group: null
25
+ tags: []
26
+ notes: null
27
+ temperature: 1.0
28
+ markov_games:
29
+ runner_method_name: LinearRunner
30
+ runner_kwargs: {}
31
+ simulation_class_name: IPD
32
+ simulation_init_args:
33
+ rounds_per_game: 10
34
+ cooperate_actions:
35
+ - <A>
36
+ defect_actions:
37
+ - <B>
38
+ reward: 3
39
+ punishment: 1
40
+ temptation: 5
41
+ sucker: 0
42
+ agents:
43
+ 0:
44
+ agent_id: ${agent_0_id}
45
+ agent_name: Alice
46
+ agent_class_name: IPDAgent
47
+ policy_id: base_llm/agent_adapter
48
+ init_kwargs:
49
+ max_errors: 0
50
+ allow_reasoning: false
51
+ max_reasoning_chars: 0
52
+ cooperate_string: <A>
53
+ defect_string: <B>
54
+ strategy_prompt: ''
55
+ intro_prompt: "Welcome to an iterated game. You are playing against another\
56
+ \ agent.\n\nGame Rules:\n1. In each round, both agents simultaneously choose\
57
+ \ to either play A or B.\n2. The payoffs for each round are as follows:\n\
58
+ \ - If both agents play A, both receive 3 points.\n - If both agents\
59
+ \ play B, both receive 1 points.\n - If one agent plays A and the other\
60
+ \ plays B, the player who plays A receives 0 points and the player who plays\
61
+ \ B receives 5 points.\n3. The goal is to maximize your total points over\
62
+ \ all rounds.\n4. You will see the previous play of the other player after\
63
+ \ the completion of a round.\n\nIn order to play A, you must write \"<A>\"\
64
+ \ and nothing else.\nIn order to play B, you must write \"<B>\" and nothing\
65
+ \ else.\n\nDuring the entire game, you must only write \"<A>\" or \"<B>\"\
66
+ . If you write anything else, your response will be considered as an error.\n\
67
+ \nThe game starts now:\n"
68
+ goal_prompt: 'Your goal is to maximize your utility.
69
+
70
+ '
71
+ 1:
72
+ agent_id: ${agent_1_id}
73
+ agent_name: Bob
74
+ agent_class_name: IPDAgent
75
+ policy_id: base_llm/agent_adapter
76
+ init_kwargs:
77
+ max_errors: 0
78
+ allow_reasoning: false
79
+ max_reasoning_chars: 0
80
+ cooperate_string: <A>
81
+ defect_string: <B>
82
+ strategy_prompt: ''
83
+ intro_prompt: "Welcome to an iterated game. You are playing against another\
84
+ \ agent.\n\nGame Rules:\n1. In each round, both agents simultaneously choose\
85
+ \ to either play A or B.\n2. The payoffs for each round are as follows:\n\
86
+ \ - If both agents play A, both receive 3 points.\n - If both agents\
87
+ \ play B, both receive 1 points.\n - If one agent plays A and the other\
88
+ \ plays B, the player who plays A receives 0 points and the player who plays\
89
+ \ B receives 5 points.\n3. The goal is to maximize your total points over\
90
+ \ all rounds.\n4. You will see the previous play of the other player after\
91
+ \ the completion of a round.\n\nIn order to play A, you must write \"<A>\"\
92
+ \ and nothing else.\nIn order to play B, you must write \"<B>\" and nothing\
93
+ \ else.\n\nDuring the entire game, you must only write \"<A>\" or \"<B>\"\
94
+ . If you write anything else, your response will be considered as an error.\n\
95
+ \nThe game starts now:\n"
96
+ goal_prompt: 'Your goal is to maximize your utility.
97
+
98
+ '
99
+ models:
100
+ base_llm:
101
+ class: LeanLocalLLM
102
+ init_args:
103
+ llm_id: base_llm
104
+ model_name: Qwen/Qwen2.5-7B-Instruct
105
+ inference_backend: vllm
106
+ hf_kwargs:
107
+ device_map: auto
108
+ torch_dtype: bfloat16
109
+ max_memory:
110
+ 0: 20GiB
111
+ attn_implementation: flash_attention_2
112
+ inference_backend_init_kwargs:
113
+ enable_lora: true
114
+ seed: ${experiment.base_seed}
115
+ enable_prefix_caching: true
116
+ max_model_len: 10000.0
117
+ gpu_memory_utilization: 0.5
118
+ dtype: bfloat16
119
+ trust_remote_code: true
120
+ max_lora_rank: 32
121
+ enforce_eager: false
122
+ max_loras: ${lora_count}
123
+ max_cpu_loras: ${lora_count}
124
+ enable_sleep_mode: false
125
+ inference_backend_sampling_params:
126
+ temperature: ${temperature}
127
+ top_p: 1.0
128
+ max_tokens: 400
129
+ top_k: -1
130
+ logprobs: 0
131
+ adapter_configs:
132
+ agent_adapter:
133
+ task_type: CAUSAL_LM
134
+ r: 32
135
+ lora_alpha: 64
136
+ lora_dropout: 0.0
137
+ target_modules: all-linear
138
+ critic_adapter:
139
+ task_type: CAUSAL_LM
140
+ r: 32
141
+ lora_alpha: 64
142
+ lora_dropout: 0.0
143
+ target_modules: all-linear
144
+ enable_thinking: null
145
+ regex_max_attempts: 1
146
+ critics:
147
+ agent_critic:
148
+ module_pointer:
149
+ - base_llm
150
+ - critic_adapter
151
+ optimizers:
152
+ agent_optimizer:
153
+ module_pointer:
154
+ - base_llm
155
+ - agent_adapter
156
+ optimizer_class_name: torch.optim.Adam
157
+ init_args:
158
+ lr: 3.0e-06
159
+ weight_decay: 0.0
160
+ critic_optimizer:
161
+ module_pointer: agent_critic
162
+ optimizer_class_name: torch.optim.Adam
163
+ init_args:
164
+ lr: 3.0e-06
165
+ weight_decay: 0.0
166
+ trainers:
167
+ agent_trainer:
168
+ class: TrainerAdAlign
169
+ module_pointers:
170
+ policy:
171
+ - base_llm
172
+ - agent_adapter
173
+ policy_optimizer: agent_optimizer
174
+ critic: agent_critic
175
+ critic_optimizer: critic_optimizer
176
+ kwargs:
177
+ entropy_coeff: 0.01
178
+ entropy_topk: null
179
+ entropy_mask_regex: null
180
+ kl_coeff: 0.0
181
+ gradient_clipping: 1.0
182
+ restrict_tokens: null
183
+ mini_batch_size: 4
184
+ use_gradient_checkpointing: true
185
+ temperature: ${temperature}
186
+ device: cuda:0
187
+ use_gae: false
188
+ whiten_advantages: false
189
+ whiten_advantages_time_step_wise: false
190
+ skip_discounted_state_visitation: true
191
+ use_gae_lambda_annealing: false
192
+ gae_lambda_annealing_method: None
193
+ gae_lambda_annealing_method_params: None
194
+ gae_lambda_annealing_limit: 0.95
195
+ discount_factor: 0.9
196
+ use_rloo: true
197
+ enable_tokenwise_logging: false
198
+ pg_loss_normalization: nb_tokens
199
+ truncated_importance_sampling_ratio_cap: 2.0
200
+ reward_normalizing_constant: 5.0
201
+ ad_align_force_coop_first_step: false
202
+ ad_align_clipping: null
203
+ ad_align_gamma: 0.9
204
+ ad_align_exclude_k_equals_t: true
205
+ ad_align_use_sign: false
206
+ ad_align_beta: 0.5
207
+ use_old_ad_align: true
208
+ use_time_regularization: false
209
+ rloo_branch: false
210
+ reuse_baseline: false
211
+ train_on_which_data:
212
+ agent_trainer: ${agent_ids}
213
+ lora_count: 30
214
+ common_agent_kwargs:
215
+ max_errors: 0
216
+ allow_reasoning: false
217
+ max_reasoning_chars: 0
218
+ cooperate_string: <A>
219
+ defect_string: <B>
220
+ strategy_prompt: ''
221
+ intro_prompt: "Welcome to an iterated game. You are playing against another agent.\n\
222
+ \nGame Rules:\n1. In each round, both agents simultaneously choose to either play\
223
+ \ A or B.\n2. The payoffs for each round are as follows:\n - If both agents\
224
+ \ play A, both receive 3 points.\n - If both agents play B, both receive 1 points.\n\
225
+ \ - If one agent plays A and the other plays B, the player who plays A receives\
226
+ \ 0 points and the player who plays B receives 5 points.\n3. The goal is to maximize\
227
+ \ your total points over all rounds.\n4. You will see the previous play of the\
228
+ \ other player after the completion of a round.\n\nIn order to play A, you must\
229
+ \ write \"<A>\" and nothing else.\nIn order to play B, you must write \"<B>\"\
230
+ \ and nothing else.\n\nDuring the entire game, you must only write \"<A>\" or\
231
+ \ \"<B>\". If you write anything else, your response will be considered as an\
232
+ \ error.\n\nThe game starts now:\n"
233
+ goal_prompt: 'Your goal is to maximize your utility.
234
+
235
+ '
236
+ agent_0_id: Alice
237
+ agent_1_id: Bob
238
+ agent_ids:
239
+ - Alice
240
+ - Bob
.hydra/hydra.yaml ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ hydra:
2
+ run:
3
+ dir: ${oc.env:SCRATCH}/llm_negotiation/${now:%Y_%m}/${experiment.name}
4
+ sweep:
5
+ dir: multirun/${now:%Y-%m-%d}/${now:%H-%M-%S}
6
+ subdir: ${hydra.job.num}
7
+ launcher:
8
+ _target_: hydra._internal.core_plugins.basic_launcher.BasicLauncher
9
+ sweeper:
10
+ _target_: hydra._internal.core_plugins.basic_sweeper.BasicSweeper
11
+ max_batch_size: null
12
+ params: null
13
+ help:
14
+ app_name: ${hydra.job.name}
15
+ header: '${hydra.help.app_name} is powered by Hydra.
16
+
17
+ '
18
+ footer: 'Powered by Hydra (https://hydra.cc)
19
+
20
+ Use --hydra-help to view Hydra specific help
21
+
22
+ '
23
+ template: '${hydra.help.header}
24
+
25
+ == Configuration groups ==
26
+
27
+ Compose your configuration from those groups (group=option)
28
+
29
+
30
+ $APP_CONFIG_GROUPS
31
+
32
+
33
+ == Config ==
34
+
35
+ Override anything in the config (foo.bar=value)
36
+
37
+
38
+ $CONFIG
39
+
40
+
41
+ ${hydra.help.footer}
42
+
43
+ '
44
+ hydra_help:
45
+ template: 'Hydra (${hydra.runtime.version})
46
+
47
+ See https://hydra.cc for more info.
48
+
49
+
50
+ == Flags ==
51
+
52
+ $FLAGS_HELP
53
+
54
+
55
+ == Configuration groups ==
56
+
57
+ Compose your configuration from those groups (For example, append hydra/job_logging=disabled
58
+ to command line)
59
+
60
+
61
+ $HYDRA_CONFIG_GROUPS
62
+
63
+
64
+ Use ''--cfg hydra'' to Show the Hydra config.
65
+
66
+ '
67
+ hydra_help: ???
68
+ hydra_logging:
69
+ version: 1
70
+ formatters:
71
+ simple:
72
+ format: '[%(asctime)s][HYDRA] %(message)s'
73
+ handlers:
74
+ console:
75
+ class: logging.StreamHandler
76
+ formatter: simple
77
+ stream: ext://sys.stdout
78
+ root:
79
+ level: INFO
80
+ handlers:
81
+ - console
82
+ loggers:
83
+ logging_example:
84
+ level: DEBUG
85
+ disable_existing_loggers: false
86
+ job_logging:
87
+ version: 1
88
+ formatters:
89
+ simple:
90
+ format: '[%(asctime)s][%(name)s][%(levelname)s] - %(message)s'
91
+ handlers:
92
+ console:
93
+ class: logging.StreamHandler
94
+ formatter: simple
95
+ stream: ext://sys.stdout
96
+ file:
97
+ class: logging.FileHandler
98
+ formatter: simple
99
+ filename: ${hydra.runtime.output_dir}/${hydra.job.name}.log
100
+ root:
101
+ level: INFO
102
+ handlers:
103
+ - console
104
+ - file
105
+ disable_existing_loggers: false
106
+ env: {}
107
+ mode: RUN
108
+ searchpath: []
109
+ callbacks: {}
110
+ output_subdir: .hydra
111
+ overrides:
112
+ hydra:
113
+ - hydra.mode=RUN
114
+ task: []
115
+ job:
116
+ name: run
117
+ chdir: false
118
+ override_dirname: ''
119
+ id: ???
120
+ num: ???
121
+ config_name: ipd_vanilla_ad_align_no_agent_buffer_seed42.yaml
122
+ env_set: {}
123
+ env_copy: []
124
+ config:
125
+ override_dirname:
126
+ kv_sep: '='
127
+ item_sep: ','
128
+ exclude_keys: []
129
+ runtime:
130
+ version: 1.3.2
131
+ version_base: '1.1'
132
+ cwd: /home/mila/m/mohammed.muqeeth/AdAlignLLM
133
+ config_sources:
134
+ - path: hydra.conf
135
+ schema: pkg
136
+ provider: hydra
137
+ - path: /home/mila/m/mohammed.muqeeth/AdAlignLLM/configs
138
+ schema: file
139
+ provider: main
140
+ - path: ''
141
+ schema: structured
142
+ provider: schema
143
+ output_dir: /network/scratch/m/mohammed.muqeeth/llm_negotiation/2026_03/ipd_vanilla_ad_align_no_agent_buffer_seed42
144
+ choices:
145
+ hydra/env: default
146
+ hydra/callbacks: null
147
+ hydra/job_logging: default
148
+ hydra/hydra_logging: default
149
+ hydra/hydra_help: default
150
+ hydra/help: default
151
+ hydra/sweeper: basic
152
+ hydra/launcher: basic
153
+ hydra/output: default
154
+ verbose: false
.hydra/overrides.yaml ADDED
@@ -0,0 +1 @@
 
 
1
+ []
seed_42/Qwen/Qwen2.5-7B-Instruct/adapters/README.md ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: Qwen/Qwen2.5-7B-Instruct
3
+ library_name: peft
4
+ pipeline_tag: text-generation
5
+ tags:
6
+ - base_model:adapter:Qwen/Qwen2.5-7B-Instruct
7
+ - lora
8
+ - transformers
9
+ ---
10
+
11
+ # Model Card for Model ID
12
+
13
+ <!-- Provide a quick summary of what the model is/does. -->
14
+
15
+
16
+
17
+ ## Model Details
18
+
19
+ ### Model Description
20
+
21
+ <!-- Provide a longer summary of what this model is. -->
22
+
23
+
24
+
25
+ - **Developed by:** [More Information Needed]
26
+ - **Funded by [optional]:** [More Information Needed]
27
+ - **Shared by [optional]:** [More Information Needed]
28
+ - **Model type:** [More Information Needed]
29
+ - **Language(s) (NLP):** [More Information Needed]
30
+ - **License:** [More Information Needed]
31
+ - **Finetuned from model [optional]:** [More Information Needed]
32
+
33
+ ### Model Sources [optional]
34
+
35
+ <!-- Provide the basic links for the model. -->
36
+
37
+ - **Repository:** [More Information Needed]
38
+ - **Paper [optional]:** [More Information Needed]
39
+ - **Demo [optional]:** [More Information Needed]
40
+
41
+ ## Uses
42
+
43
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
44
+
45
+ ### Direct Use
46
+
47
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
48
+
49
+ [More Information Needed]
50
+
51
+ ### Downstream Use [optional]
52
+
53
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
54
+
55
+ [More Information Needed]
56
+
57
+ ### Out-of-Scope Use
58
+
59
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
60
+
61
+ [More Information Needed]
62
+
63
+ ## Bias, Risks, and Limitations
64
+
65
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
66
+
67
+ [More Information Needed]
68
+
69
+ ### Recommendations
70
+
71
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
72
+
73
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
74
+
75
+ ## How to Get Started with the Model
76
+
77
+ Use the code below to get started with the model.
78
+
79
+ [More Information Needed]
80
+
81
+ ## Training Details
82
+
83
+ ### Training Data
84
+
85
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
86
+
87
+ [More Information Needed]
88
+
89
+ ### Training Procedure
90
+
91
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
92
+
93
+ #### Preprocessing [optional]
94
+
95
+ [More Information Needed]
96
+
97
+
98
+ #### Training Hyperparameters
99
+
100
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
101
+
102
+ #### Speeds, Sizes, Times [optional]
103
+
104
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
105
+
106
+ [More Information Needed]
107
+
108
+ ## Evaluation
109
+
110
+ <!-- This section describes the evaluation protocols and provides the results. -->
111
+
112
+ ### Testing Data, Factors & Metrics
113
+
114
+ #### Testing Data
115
+
116
+ <!-- This should link to a Dataset Card if possible. -->
117
+
118
+ [More Information Needed]
119
+
120
+ #### Factors
121
+
122
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
123
+
124
+ [More Information Needed]
125
+
126
+ #### Metrics
127
+
128
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
129
+
130
+ [More Information Needed]
131
+
132
+ ### Results
133
+
134
+ [More Information Needed]
135
+
136
+ #### Summary
137
+
138
+
139
+
140
+ ## Model Examination [optional]
141
+
142
+ <!-- Relevant interpretability work for the model goes here -->
143
+
144
+ [More Information Needed]
145
+
146
+ ## Environmental Impact
147
+
148
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
149
+
150
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
151
+
152
+ - **Hardware Type:** [More Information Needed]
153
+ - **Hours used:** [More Information Needed]
154
+ - **Cloud Provider:** [More Information Needed]
155
+ - **Compute Region:** [More Information Needed]
156
+ - **Carbon Emitted:** [More Information Needed]
157
+
158
+ ## Technical Specifications [optional]
159
+
160
+ ### Model Architecture and Objective
161
+
162
+ [More Information Needed]
163
+
164
+ ### Compute Infrastructure
165
+
166
+ [More Information Needed]
167
+
168
+ #### Hardware
169
+
170
+ [More Information Needed]
171
+
172
+ #### Software
173
+
174
+ [More Information Needed]
175
+
176
+ ## Citation [optional]
177
+
178
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
179
+
180
+ **BibTeX:**
181
+
182
+ [More Information Needed]
183
+
184
+ **APA:**
185
+
186
+ [More Information Needed]
187
+
188
+ ## Glossary [optional]
189
+
190
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
191
+
192
+ [More Information Needed]
193
+
194
+ ## More Information [optional]
195
+
196
+ [More Information Needed]
197
+
198
+ ## Model Card Authors [optional]
199
+
200
+ [More Information Needed]
201
+
202
+ ## Model Card Contact
203
+
204
+ [More Information Needed]
205
+ ### Framework versions
206
+
207
+ - PEFT 0.17.1
seed_42/Qwen/Qwen2.5-7B-Instruct/adapters/agent_adapter/adapter_config.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alpha_pattern": {},
3
+ "auto_mapping": null,
4
+ "base_model_name_or_path": "Qwen/Qwen2.5-7B-Instruct",
5
+ "bias": "none",
6
+ "corda_config": null,
7
+ "eva_config": null,
8
+ "exclude_modules": null,
9
+ "fan_in_fan_out": false,
10
+ "inference_mode": true,
11
+ "init_lora_weights": true,
12
+ "layer_replication": null,
13
+ "layers_pattern": null,
14
+ "layers_to_transform": null,
15
+ "loftq_config": {},
16
+ "lora_alpha": 64,
17
+ "lora_bias": false,
18
+ "lora_dropout": 0.0,
19
+ "megatron_config": null,
20
+ "megatron_core": "megatron.core",
21
+ "modules_to_save": null,
22
+ "peft_type": "LORA",
23
+ "qalora_group_size": 16,
24
+ "r": 32,
25
+ "rank_pattern": {},
26
+ "revision": null,
27
+ "target_modules": [
28
+ "q_proj",
29
+ "down_proj",
30
+ "v_proj",
31
+ "k_proj",
32
+ "up_proj",
33
+ "gate_proj",
34
+ "o_proj"
35
+ ],
36
+ "target_parameters": null,
37
+ "task_type": "CAUSAL_LM",
38
+ "trainable_token_indices": null,
39
+ "use_dora": false,
40
+ "use_qalora": false,
41
+ "use_rslora": false
42
+ }
seed_42/Qwen/Qwen2.5-7B-Instruct/adapters/critic_adapter/adapter_config.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alpha_pattern": {},
3
+ "auto_mapping": null,
4
+ "base_model_name_or_path": "Qwen/Qwen2.5-7B-Instruct",
5
+ "bias": "none",
6
+ "corda_config": null,
7
+ "eva_config": null,
8
+ "exclude_modules": null,
9
+ "fan_in_fan_out": false,
10
+ "inference_mode": true,
11
+ "init_lora_weights": true,
12
+ "layer_replication": null,
13
+ "layers_pattern": null,
14
+ "layers_to_transform": null,
15
+ "loftq_config": {},
16
+ "lora_alpha": 64,
17
+ "lora_bias": false,
18
+ "lora_dropout": 0.0,
19
+ "megatron_config": null,
20
+ "megatron_core": "megatron.core",
21
+ "modules_to_save": null,
22
+ "peft_type": "LORA",
23
+ "qalora_group_size": 16,
24
+ "r": 32,
25
+ "rank_pattern": {},
26
+ "revision": null,
27
+ "target_modules": [
28
+ "q_proj",
29
+ "down_proj",
30
+ "v_proj",
31
+ "k_proj",
32
+ "up_proj",
33
+ "gate_proj",
34
+ "o_proj"
35
+ ],
36
+ "target_parameters": null,
37
+ "task_type": "CAUSAL_LM",
38
+ "trainable_token_indices": null,
39
+ "use_dora": false,
40
+ "use_qalora": false,
41
+ "use_rslora": false
42
+ }
src_code_for_reproducibility/__init__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ """
2
+ File: mllm/__init__.py
3
+ Summary: Initializes the multi-agent large language model package namespace.
4
+ """
src_code_for_reproducibility/chat_utils/__pycache__/apply_template.cpython-312.pyc ADDED
Binary file (4.13 kB). View file
 
src_code_for_reproducibility/chat_utils/__pycache__/chat_turn.cpython-312.pyc ADDED
Binary file (1.47 kB). View file
 
src_code_for_reproducibility/chat_utils/__pycache__/template_specific.cpython-312.pyc ADDED
Binary file (4.4 kB). View file
 
src_code_for_reproducibility/markov_games/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (301 Bytes). View file
 
src_code_for_reproducibility/markov_games/__pycache__/agent.cpython-312.pyc ADDED
Binary file (3.17 kB). View file
 
src_code_for_reproducibility/markov_games/__pycache__/alternative_actions_runner.cpython-312.pyc ADDED
Binary file (5.43 kB). View file
 
src_code_for_reproducibility/markov_games/__pycache__/mg_utils.cpython-312.pyc ADDED
Binary file (4.07 kB). View file
 
src_code_for_reproducibility/markov_games/ipd/__pycache__/Ipd_hard_coded_agents.cpython-312.pyc ADDED
Binary file (3.06 kB). View file
 
src_code_for_reproducibility/markov_games/ipd/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (439 Bytes). View file
 
src_code_for_reproducibility/markov_games/ipd/__pycache__/ipd_simulation.cpython-312.pyc ADDED
Binary file (6.87 kB). View file
 
src_code_for_reproducibility/markov_games/ipd/__pycache__/ipd_statistics.cpython-312.pyc ADDED
Binary file (1.42 kB). View file
 
src_code_for_reproducibility/markov_games/negotiation/README.md ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Negotiation Games: core mechanics and variants
2
+
3
+ This family of games feature two agents who, in each round, may briefly communicate and then simultaneously propose how to split a fixed resource (most commonly 10 coins). Rewards are the amount kept multiplied by an agent’s per-unit value. The starting speaker alternates deterministically across rounds.
4
+
5
+ Communication is optional and variant-dependent: some settings encourage rich messaging to share private information, while others remove messaging entirely to focus on allocation behavior.
6
+
7
+ Proportional splitting is used when the two proposals exceed the available total: allocations are scaled proportionally rather than discarded. This preserves a useful learning signal even when agents over-claim.
8
+
9
+ ### Variants (in increasing difficulty)
10
+
11
+ - No‑Press Split
12
+ - Multiple item types (e.g., hats, balls, books)
13
+ - The item values for each agent are public.
14
+ - No communication; agents go straight to making split proposals.
15
+ - Motivation: mirrors no‑communication setups (e.g., Advantage Alignment) while keeping the split decision nontrivial.
16
+
17
+ - Trust-and-Split RPS (TAS-RPS)
18
+ - Single item type (coins)
19
+ - Each round, a rock–paper–scissors hand draw creates a strong asymmetry: the winner’s per-coin value is 10, the loser’s is 1.
20
+ - Each agent initially sees only their own hand and must communicate to coordinate an optimal split.
21
+ - Motivation: enforce large value disparity so one’s own value reveals little about the other’s (avoiding ceiling effects) and incentivize meaningful communication.
22
+
23
+
24
+
25
+
26
+
27
+
src_code_for_reproducibility/markov_games/negotiation/__pycache__/nego_agent.cpython-312.pyc ADDED
Binary file (11.8 kB). View file
 
src_code_for_reproducibility/markov_games/negotiation/__pycache__/no_press_nego_agent.cpython-312.pyc ADDED
Binary file (6.11 kB). View file
 
src_code_for_reproducibility/markov_games/negotiation/__pycache__/tas_rps_agent.cpython-312.pyc ADDED
Binary file (6.05 kB). View file
 
src_code_for_reproducibility/markov_games/negotiation/__pycache__/tas_rps_simulation.cpython-312.pyc ADDED
Binary file (11.7 kB). View file
 
src_code_for_reproducibility/markov_games/negotiation/nego_hard_coded_policies.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/markov_games/negotiation/nego_hard_coded_policies.py
3
+ Summary: Provides deterministic negotiation policies for testing and baselines.
4
+ """
5
+
6
+ import asyncio
7
+ from typing import Any, Optional, Tuple
8
+
9
+ from mllm.markov_games.negotiation.nego_agent import NegotiationAgent
10
+ from mllm.markov_games.negotiation.nego_simulation import Split
11
+ from mllm.markov_games.negotiation.no_press_nego_agent import NoPressAgent
12
+ from mllm.markov_games.negotiation.no_press_nego_simulation import NoPressObs
13
+ from mllm.markov_games.rollout_tree import AgentActLog, ChatTurn
14
+
15
+
16
+ class HardCodedNegoWelfareMaximizingPolicy(NoPressAgent):
17
+ async def act(self, observation: NoPressObs) -> Tuple[Any, AgentActLog]:
18
+ """
19
+ Policy that gives all of the items to the agent who values them more.
20
+ If the items are equally valued, give them to the agent who values them more.
21
+ """
22
+ quantities = observation.quantities
23
+ my_values = observation.value
24
+ other_values = observation.other_value
25
+
26
+ items_given_to_self = {}
27
+ for item, qty in quantities.items():
28
+ my_v = float(my_values.get(item, 0))
29
+ other_v = float(other_values.get(item, 0))
30
+ if my_v == other_v:
31
+ items_given_to_self[item] = int(qty) / 2
32
+ else:
33
+ items_given_to_self[item] = int(qty if my_v > other_v else 0)
34
+
35
+ action = Split(items_given_to_self=items_given_to_self)
36
+ act_log = AgentActLog(
37
+ chat_turns=[
38
+ ChatTurn(
39
+ agent_id=self.agent_id,
40
+ role="assistant",
41
+ content="Using welfare-maximizing split (all to higher-value agent).",
42
+ is_state_end=True,
43
+ )
44
+ ],
45
+ info=None,
46
+ )
47
+ return action, act_log
48
+
49
+
50
+ class HardCodedNegoGreedyPolicy(NoPressAgent):
51
+ async def act(self, observation: NoPressObs) -> Tuple[Any, AgentActLog]:
52
+ """
53
+ Always gives itself all of the items.
54
+ """
55
+ quantities = observation.quantities
56
+ items_given_to_self = {item: int(qty) for item, qty in quantities.items()}
57
+
58
+ action = Split(items_given_to_self=items_given_to_self)
59
+ act_log = AgentActLog(
60
+ chat_turns=[
61
+ ChatTurn(
62
+ agent_id=self.agent_id,
63
+ role="assistant",
64
+ content="Using greedy split (keep all items).",
65
+ is_state_end=True,
66
+ )
67
+ ],
68
+ info=None,
69
+ )
70
+ return action, act_log
src_code_for_reproducibility/markov_games/negotiation/negotiation_statistics.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/markov_games/negotiation/negotiation_statistics.py
3
+ Summary: Aggregates and reports statistics for negotiation experiments.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from typing import Callable, Dict, List, Tuple
9
+
10
+ from mllm.markov_games.negotiation.nego_simulation import Split
11
+ from mllm.markov_games.rollout_tree import SimulationStepLog
12
+
13
+
14
+ def avg_reward(sl: SimulationStepLog) -> List[Tuple[str, float]]:
15
+ """Average (per-step) reward for each agent and overall.
16
+
17
+ What it computes:
18
+ - Returns the raw reward for every (non-buffer) agent at the current
19
+ simulation step.
20
+ - Adds an aggregate key ``all_agents`` which is the simple arithmetic
21
+ mean across the agents present in ``sl.rewards``.
22
+
23
+ Rationale / motivation:
24
+ Monitoring the reward stream at each step helps:
25
+ * Diagnose reward shaping issues (e.g., unintended negative drift).
26
+ * Provide a fairness snapshot (are rewards systematically skewed?).
27
+ * Supply a ubiquitous baseline metric used by other higher‑level
28
+ summaries (efficiency, surplus allocation, etc.).
29
+
30
+ Return shape:
31
+ { agent_id: float, ..., "all_agents": float }
32
+ If any agent id contains the substring "buffer" we treat this step as
33
+ an implementation artifact (e.g., rollout buffer) and return ``None``
34
+ to avoid polluting aggregates.
35
+ """
36
+ for aid in sl.rewards.keys():
37
+ if "buffer" in str(aid) and "live" not in str(aid):
38
+ return None
39
+ # One value per agent at each step
40
+ rewards_dict = {f"reward-{aid}": float(v) for aid, v in (sl.rewards or {}).items()}
41
+ return [(key, value) for key, value in rewards_dict.items() if value is not None]
42
+
43
+
44
+ def split_efficiency(sl: SimulationStepLog) -> List[Tuple[str, float]] | None:
45
+ """Final‑round allocation efficiency relative to an upper bound.
46
+
47
+ What it computes (only on the last timestep of a negotiation round):
48
+ - Uses ``info['values']`` (per‑agent per‑item valuations) and
49
+ ``info['quantities']`` (available item counts) to form a greedy
50
+ *upper bound* on achievable total reward: allocate each unit of an
51
+ item to the single agent who values that item most.
52
+ - Compares the actually realized sum of rewards at that final
53
+ timestep to this constructed maximum.
54
+ - Emits a single scalar under key ``"all_agents"`` equal to
55
+ achieved / theoretical_max.
56
+
57
+ Motivation:
58
+ Efficiency (a core welfare notion) distinguishes between coordination
59
+ failures (low efficiency) versus strategic distributional disputes
60
+ (high efficiency but uneven splits). Tracking this per round helps
61
+ evaluate whether models learn to identify and realize joint surplus.
62
+
63
+ Notes / caveats:
64
+ - Only defined for 2+ non‑buffer agents; if a buffer agent is present
65
+ returns ``None`` to exclude spurious steps.
66
+ - Requires the environment to have populated ``values`` and
67
+ ``quantities``; otherwise returns ``None``.
68
+ - This is an optimistic bound (not necessarily reachable under
69
+ protocol constraints) but is simple, fast, and comparable across
70
+ runs.
71
+ """
72
+ info = sl.info or {}
73
+ if not info or not info.get("is_last_timestep_in_round"):
74
+ return None
75
+ quantities = info.get("quantities") or {}
76
+ values = info.get("values") or {}
77
+ if not values or not quantities:
78
+ return None
79
+ agent_ids = list(sl.rewards.keys())
80
+ if type(values[agent_ids[0]]) is dict:
81
+ item_keys = list(values.values())[0].keys()
82
+ max_vals, max_quantities = [], []
83
+ for item in item_keys:
84
+ max_val = max(float(agent_vals[item]) for agent_vals in values.values())
85
+ max_vals.append(max_val)
86
+ max_quantities.append(quantities[item])
87
+ else:
88
+ max_vals = [max(float(v) for v in values.values())]
89
+ max_quantities = [quantities[item] for item in quantities.keys()]
90
+ for aid in sl.rewards.keys():
91
+ if "buffer" in str(aid) and "live" not in str(aid):
92
+ return None
93
+ achieved = sum(float(v) for v in sl.rewards.values())
94
+ max_reward = sum(d * v for d, v in zip(max_quantities, max_vals))
95
+ # Efficiency is a global metric; emit same value for a special key "all"
96
+ return [("split_efficiency", achieved / max_reward)]
97
+
98
+
99
+ def _extract_items_from_split(raw_split: Dict) -> Dict[str, float] | None:
100
+ """Return a mapping item->proposal amount from a split structure.
101
+
102
+ Supports both generic negotiation splits with nested structure
103
+ { 'items_given_to_self': {item: qty, ...}}
104
+ and TAS coin-only variants which may already be a flat mapping {'coins': qty}.
105
+ """
106
+
107
+ if raw_split is None:
108
+ return {}
109
+ elif isinstance(raw_split, Split):
110
+ return {k: float(v) for k, v in raw_split.items_given_to_self.items()}
111
+ elif isinstance(raw_split, dict):
112
+ if "items_given_to_self" in raw_split and isinstance(
113
+ raw_split["items_given_to_self"], dict
114
+ ):
115
+ return {k: float(v) for k, v in raw_split["items_given_to_self"].items()}
116
+ # Fallback: assume already flat mapping of items
117
+ elif hasattr(raw_split, "items_given_to_self"):
118
+ return {k: float(v) for k, v in raw_split["items_given_to_self"].items()}
119
+ return {
120
+ k: float(v) for k, v in raw_split.items() if isinstance(v, (int, float))
121
+ }
122
+ return {}
123
+
124
+
125
+ def _average_proposal_relative_value(
126
+ sl: SimulationStepLog,
127
+ metric_name: str,
128
+ comparator: Callable[[float, float], bool],
129
+ opposite_comparator: Callable[[float, float], bool],
130
+ ) -> Dict[str, float | None] | None:
131
+ """Shared implementation for proposal size conditioned on relative value.
132
+
133
+ Parameters:
134
+ comparator: returns True when agent_0's value relation (e.g. < or >)
135
+ to agent_1 holds for an item and we should collect agent_0's
136
+ proposed quantity for that item.
137
+ opposite_comparator: inverse relation used to collect agent_1's items.
138
+
139
+ Behavior:
140
+ - Executes only on final timestep of a round (where the definitive
141
+ proposal / allocation is known via ``info['splits']``).
142
+ - For each item, classifies which agent's value satisfies the chosen
143
+ relation and records that agent's proposed quantity from the split.
144
+ - Averages (mean) across all qualifying items per agent; if no items
145
+ qualify for an agent returns ``None`` for that agent id.
146
+ - Adds ``all_agents`` mean across the numeric (non-None) agent values.
147
+
148
+ Why this matters:
149
+ Distinguishing how much an agent *asks for* when it subjectively
150
+ values items more (or less) than its counterpart reveals patterns of
151
+ opportunism vs. concession. This is especially useful when raw reward
152
+ differences are subtle but allocation *intent* differs.
153
+ """
154
+ info = sl.info or {}
155
+ if not info or not info.get("is_last_timestep_in_round"):
156
+ return None
157
+ quantities = info.get("quantities") or {}
158
+ splits = info.get("splits") or {}
159
+ values = info.get("values") or {}
160
+ agent_ids: List[str] = list(sl.rewards.keys())
161
+ if len(agent_ids) != 2:
162
+ return None # Only defined for 2-agent case.
163
+ for aid in agent_ids:
164
+ if "buffer" in str(aid) and "live" not in str(aid):
165
+ return None
166
+ # Extract per-agent item proposals robustly
167
+ split_items = {aid: _extract_items_from_split(splits.get(aid)) for aid in agent_ids}
168
+ agent_0_vals: List[float] = []
169
+ agent_1_vals: List[float] = []
170
+ for item in quantities.keys():
171
+ # Values may be either a float (same for all items) or dict per item
172
+ v0_raw = values[agent_ids[0]]
173
+ v1_raw = values[agent_ids[1]]
174
+ v0 = float(v0_raw[item]) if isinstance(v0_raw, dict) else float(v0_raw)
175
+ v1 = float(v1_raw[item]) if isinstance(v1_raw, dict) else float(v1_raw)
176
+ if comparator(v0, v1):
177
+ agent_0_vals.append(split_items[agent_ids[0]].get(item, 0.0))
178
+ elif opposite_comparator(v0, v1):
179
+ agent_1_vals.append(split_items[agent_ids[1]].get(item, 0.0))
180
+ out: Dict[str, float | None] = {}
181
+ out[f"{metric_name}-{agent_ids[0]}"] = (
182
+ sum(agent_0_vals) / len(agent_0_vals) if agent_0_vals else None
183
+ )
184
+ out[f"{metric_name}-{agent_ids[1]}"] = (
185
+ sum(agent_1_vals) / len(agent_1_vals) if agent_1_vals else None
186
+ )
187
+
188
+ return [(key, value) for key, value in out.items() if value is not None]
189
+
190
+
191
+ def average_proposal_when_agent_values_item_lower(
192
+ sl: SimulationStepLog,
193
+ ) -> List[Tuple[str, float | None]] | None:
194
+ """Mean quantity an agent proposes for items it values *less* than opponent.
195
+
196
+ Interpretation:
197
+ A higher value implies the agent still claims (or is allocated) a
198
+ notable share of items where it has a comparative *disadvantage* in
199
+ valuation, signaling either strategic over-claiming or protocol-driven
200
+ egalitarian splits. Conversely, very low numbers can indicate
201
+ efficient specialization or excessive concession.
202
+
203
+ Returns:
204
+ Mapping { agent_id: float | None, "all_agents": float | None } where
205
+ None indicates no qualifying items for that agent in the round.
206
+ """
207
+ return _average_proposal_relative_value(
208
+ sl,
209
+ "average_proposal_when_agent_values_item_lower",
210
+ lambda a, b: a < b,
211
+ lambda a, b: a > b,
212
+ )
213
+
214
+
215
+ def average_proposal_when_agent_values_item_higher(
216
+ sl: SimulationStepLog,
217
+ ) -> List[Tuple[str, float | None]] | None:
218
+ """Mean quantity an agent proposes for items it values *more* than opponent.
219
+
220
+ Interpretation:
221
+ Captures how aggressively an agent claims items where it holds a
222
+ comparative *advantage*. Elevated values can reflect rational
223
+ specialization (efficient exploitation of comparative advantage) or
224
+ potentially unfair grabs if paired with low concession in the lower
225
+ valuation metric. Comparing this with the 'lower' counterpart helps
226
+ profile negotiation style (cooperative vs. exploitative).
227
+
228
+ Returns:
229
+ Mapping { agent_id: float | None, "all_agents": float | None } where
230
+ None indicates no qualifying items.
231
+ """
232
+ return _average_proposal_relative_value(
233
+ sl,
234
+ "average_proposal_when_agent_values_item_higher",
235
+ lambda a, b: a > b,
236
+ lambda a, b: a < b,
237
+ )
238
+
239
+
240
+ # Explicit list of metric functions exported for rendering. Helper functions
241
+ # starting with '_' are intentionally excluded. Update this list when adding
242
+ # new public statistics so render.py can rely on it instead of introspecting
243
+ # every callable in the module.
244
+ stat_functs: list[Callable[[SimulationStepLog], List[Tuple[str, float]]]] = [
245
+ avg_reward,
246
+ average_proposal_when_agent_values_item_lower,
247
+ average_proposal_when_agent_values_item_higher,
248
+ split_efficiency,
249
+ ]
src_code_for_reproducibility/markov_games/negotiation/no_press_nego_agent.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/markov_games/negotiation/no_press_nego_agent.py
3
+ Summary: Agent variant for no-press negotiations without explicit messaging.
4
+ """
5
+
6
+ from typing import Any, Dict, List, Tuple
7
+
8
+ from mllm.markov_games.negotiation.nego_agent import (
9
+ NegotiationAgent,
10
+ NegotiationAgentState,
11
+ )
12
+ from mllm.markov_games.negotiation.nego_simulation import Split
13
+ from mllm.markov_games.negotiation.no_press_nego_simulation import NoPressObs
14
+ from mllm.markov_games.rollout_tree import AgentActLog, ChatTurn
15
+
16
+
17
+ class NoPressAgent(NegotiationAgent):
18
+ def __init__(self, *args, **kwargs):
19
+ super().__init__(*args, **kwargs)
20
+ # No communication in this variant
21
+ self.intro_prompt = (
22
+ "Welcome to an iterated game. You are {agent}. The other agent is {other_agent}.\n"
23
+ "Setup:\n"
24
+ "1. The game consists of multiple independent rounds.\n"
25
+ "2. In each round, there are multiple items to split between the two agents.\n"
26
+ "3. Both agents are assigned a per-item value between 1 and 20 (inclusive) in each round.\n"
27
+ "4. You can observe per-item values of both agents.\n"
28
+ "5. Because assignments are random, both agents are equally likely to have same expected per-item value.\n"
29
+ "\n"
30
+ "Protocol:\n"
31
+ "1. Both agents simultaneously propose the amount of each item they will keep.\n"
32
+ "2. If the total sum of proposals is less than or equal to the item quantity, both agents receive their proposed amounts.\n"
33
+ "3. If the total sum of proposals exceeds the item quantity, they are allocated proportionally.\n"
34
+ "4. Your points for the round = (amount you receive per item) x (your per-item value for that round), added across all items.\n"
35
+ "5. Points are accumulated across rounds.\n"
36
+ "Your goal: {goal}\n"
37
+ )
38
+ self.new_round_prompt = (
39
+ "A New Round Begins\n"
40
+ "The items to split are {quantities}.\n"
41
+ "Your per-item values are {value} and {other_agent}'s per-item values are {other_value}."
42
+ )
43
+ self.last_round_prompt = (
44
+ "Last Round Summary:\n"
45
+ " - Items to split: {last_quantities}\n"
46
+ " - Your per-item values: {last_value_agent}\n"
47
+ " - {other_agent}'s per-item values: {last_value_coagent}\n"
48
+ " - You proposed: {last_split_agent}\n"
49
+ " - You earned: {last_points_agent} points\n"
50
+ " - {other_agent} proposed: {last_split_coagent}\n"
51
+ " - {other_agent} earned: {last_points_coagent} points\n"
52
+ " - Round Complete.\n"
53
+ )
54
+ self.send_split_prompt = "Submit Your Proposal\n" "Respond as {proposal_style}"
55
+
56
+ def get_message_regex(self, observation: NoPressObs) -> str:
57
+ """Return an empty pattern because the no-press variant forbids chat."""
58
+ return r"^$" # No messages allowed
59
+
60
+ def get_split_regex(self, observation: NoPressObs) -> str:
61
+ """Match proposals like ``Proposal: 4 coins, 6 apples`` case-insensitively."""
62
+ items = list(observation.quantities.keys())
63
+ # Accept both singular and plural forms
64
+ item_pattern = "|".join(
65
+ [f"{item[:-1]}s?" if item.endswith("s") else f"{item}s?" for item in items]
66
+ )
67
+ regex = rf"(?i)Proposal:\s*((?:\s*(?P<num>(10|[0-9]))\s*(?P<item>{item_pattern})\s*,?)+)"
68
+ return regex
69
+
70
+ def get_split_action(self, policy_output: str, observation: NoPressObs) -> Split:
71
+ """
72
+ Parse the LLM proposal into a normalized ``Split`` structure.
73
+
74
+ The regex-based parser is lenient (accepts pluralization variants) so that
75
+ prompt tweaks do not require re-training the extraction logic.
76
+ """
77
+ items = list(observation.quantities.keys())
78
+ import re as _re
79
+
80
+ split_regex = self.get_split_regex(observation)
81
+ items_given_to_self = {item: 0 for item in items}
82
+ m = _re.match(split_regex, policy_output.strip())
83
+ if m:
84
+ # Find all (number, item) pairs
85
+ item_pattern = "|".join(
86
+ [
87
+ f"{item[:-1]}s?" if item.endswith("s") else f"{item}s?"
88
+ for item in items
89
+ ]
90
+ )
91
+ inner_regex = rf"(?i)(10|[0-9])\s*({item_pattern})"
92
+
93
+ def normalize_item_name(item_str):
94
+ """Canonicalize plural/singular user text back to the config item id."""
95
+ for orig in items:
96
+ if item_str.lower() == orig.lower():
97
+ return orig
98
+ if orig.endswith("s") and item_str.lower() == orig[:-1].lower():
99
+ return orig
100
+ if (
101
+ not orig.endswith("s")
102
+ and item_str.lower() == orig.lower() + "s"
103
+ ):
104
+ return orig
105
+
106
+ for num, item in _re.findall(inner_regex, m.group(1)):
107
+ items_given_to_self[normalize_item_name(item)] = int(num)
108
+ return Split(items_given_to_self=items_given_to_self)
src_code_for_reproducibility/markov_games/negotiation/no_press_nego_simulation.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/markov_games/negotiation/no_press_nego_simulation.py
3
+ Summary: Simulation driver for no-press negotiation scenarios.
4
+ """
5
+
6
+ import copy
7
+ from collections import defaultdict
8
+ from dataclasses import dataclass
9
+ from typing import Any, Dict, List, Literal, Tuple
10
+
11
+ from mllm.markov_games.negotiation.nego_simulation import (
12
+ NegotiationObs,
13
+ NegotiationSimulation,
14
+ NegotiationState,
15
+ Split,
16
+ compute_tas_style_rewards,
17
+ )
18
+
19
+ AgentId = str
20
+
21
+
22
+ @dataclass
23
+ class NoPressState(NegotiationState):
24
+ """NegotiationState alias used to clarify we run in always-split phase."""
25
+
26
+ pass
27
+
28
+
29
+ @dataclass
30
+ class NoPressObs(NegotiationObs):
31
+ """Observation that includes both agents' values (since there is no messaging)."""
32
+
33
+ other_value: Dict[str, float]
34
+
35
+
36
+ class NoPressSimulation(NegotiationSimulation):
37
+ def __init__(
38
+ self,
39
+ game_type: Literal["10-1-exclusive", "10-1-ties", "1-to-20"] = "1-to-20",
40
+ same_round_value: bool = True,
41
+ atleast_one_conflict: bool = False,
42
+ *args,
43
+ **kwargs,
44
+ ):
45
+ self.game_type = game_type
46
+ self.same_round_value = same_round_value
47
+ self.atleast_one_conflict = atleast_one_conflict
48
+ super().__init__(*args, **kwargs)
49
+
50
+ def _sample_values(self) -> Dict[AgentId, dict]:
51
+ """Sample per-item valuations according to the configured template."""
52
+ values = defaultdict(dict)
53
+ if self.state is None:
54
+ item_types = self.item_types
55
+ else:
56
+ item_types = list(self.state.quantities.keys())
57
+ while True:
58
+ for item in item_types:
59
+ if self.game_type == "10-1-exclusive":
60
+ v = int(self.rng.choice([1, 10]))
61
+ values[self.agent_ids[0]][item] = v
62
+ values[self.agent_ids[1]][item] = 10 if v == 1 else 1
63
+ elif self.game_type == "10-1-ties":
64
+ for aid in self.agent_ids:
65
+ values[aid][item] = int(self.rng.choice([1, 10]))
66
+ elif self.game_type == "1-to-20":
67
+ for aid in self.agent_ids:
68
+ values[aid][item] = int(self.rng.integers(1, 21))
69
+ if self.atleast_one_conflict:
70
+ has_conflict = False
71
+ for item in item_types:
72
+ agent_values_for_item = [
73
+ values[aid][item] for aid in self.agent_ids
74
+ ]
75
+ if len(set(agent_values_for_item)) > 1:
76
+ has_conflict = True
77
+ break
78
+ if not has_conflict:
79
+ continue
80
+ agent_values = [sum(v.values()) for v in values.values()]
81
+ if len(set(agent_values)) == 1 or not self.same_round_value:
82
+ break
83
+ return values
84
+
85
+ def _sample_quantities(self) -> Dict[str, int]:
86
+ """No-press setups use symmetric 10-unit stocks for every item."""
87
+ return {item.lower(): 10 for item in self.item_types}
88
+
89
+ def set_new_round_of_variant(self):
90
+ """Refresh quantities/values and jump directly into the simultaneous split."""
91
+ self.state.quantities = self._sample_quantities()
92
+ self.state.values = self._sample_values()
93
+ self.state.split_phase = True
94
+
95
+ def get_info_of_variant(
96
+ self, state: NegotiationState, actions: Dict[AgentId, Any]
97
+ ) -> Dict[str, Any]:
98
+ """Surface quantities/values/splits so statistics modules can read them."""
99
+ return {
100
+ "quantities": copy.deepcopy(state.quantities),
101
+ "values": copy.deepcopy(state.values),
102
+ "splits": copy.deepcopy(state.splits),
103
+ }
104
+
105
+ def get_rewards(self, splits: Dict[AgentId, Split]) -> Dict[AgentId, float]:
106
+ """Reuse TAS reward logic because the split arbitration is identical."""
107
+ return compute_tas_style_rewards(
108
+ self.agent_ids, self.state.values, splits, self.state.quantities
109
+ )
110
+
111
+ def get_obs(self):
112
+ return {agent_id: self.get_obs_agent(agent_id) for agent_id in self.agent_ids}
113
+
114
+ def get_obs_agent(self, agent_id):
115
+ other_id = self._other(agent_id)
116
+ last_value_coagent = (
117
+ None
118
+ if self.state.previous_values is None
119
+ else self.state.previous_values.get(other_id)
120
+ )
121
+ last_points_coagent = (
122
+ None
123
+ if self.state.previous_points is None
124
+ else round(self.state.previous_points.get(other_id), 1)
125
+ )
126
+ last_value_agent = (
127
+ None
128
+ if self.state.previous_values is None
129
+ else self.state.previous_values.get(agent_id)
130
+ )
131
+ last_points_agent = (
132
+ None
133
+ if self.state.previous_points is None
134
+ else round(self.state.previous_points.get(agent_id), 1)
135
+ )
136
+ last_split_coagent = None
137
+ last_split_agent = None
138
+ if self.state.previous_splits is not None:
139
+ last_split_coagent = self.state.previous_splits[
140
+ other_id
141
+ ].items_given_to_self
142
+ last_split_agent = self.state.previous_splits[agent_id].items_given_to_self
143
+ obs = NoPressObs(
144
+ round_nb=self.state.round_nb,
145
+ last_message="",
146
+ quota_messages_per_agent_per_round=self.quota_messages_per_agent_per_round,
147
+ current_agent=self.state.current_agent,
148
+ other_agent=self.agent_id_to_name[other_id],
149
+ quantities=self.state.quantities,
150
+ item_types=self.item_types,
151
+ value=self.state.values[agent_id],
152
+ split_phase=self.state.split_phase,
153
+ last_split_agent=last_split_agent,
154
+ last_value_agent=last_value_agent,
155
+ last_points_agent=last_points_agent,
156
+ last_split_coagent=last_split_coagent,
157
+ last_value_coagent=last_value_coagent,
158
+ last_points_coagent=last_points_coagent,
159
+ other_value=self.state.values[other_id],
160
+ last_quantities=self.state.previous_quantities,
161
+ )
162
+ return obs
163
+
164
+ def reset(self):
165
+ start_agent = self.agent_ids[self._starting_agent_index]
166
+ quantities = self._sample_quantities()
167
+ values = self._sample_values()
168
+ self.state = NoPressState(
169
+ round_nb=0,
170
+ last_message="",
171
+ current_agent=start_agent,
172
+ quantities=quantities,
173
+ values=values,
174
+ previous_values=None,
175
+ splits={aid: None for aid in self.agent_ids},
176
+ nb_messages_sent={aid: 0 for aid in self.agent_ids},
177
+ split_phase=True,
178
+ previous_splits=None,
179
+ previous_points=None,
180
+ previous_quantities=None,
181
+ )
182
+ return self.get_obs()
src_code_for_reproducibility/models/__init__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ """
2
+ File: mllm/models/__init__.py
3
+ Summary: Exports model-layer utilities from the models package.
4
+ """
src_code_for_reproducibility/models/adapter_training_wrapper.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/models/adapter_training_wrapper.py
3
+ Summary: Wraps a shared LLM with adapter-specific PEFT handling for training.
4
+ """
5
+
6
+ import logging
7
+ from typing import Union
8
+
9
+ import torch
10
+ import torch.nn as nn
11
+ from peft import LoraConfig, get_peft_model
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+
16
+ class AdapterWrapper(nn.Module):
17
+ """
18
+ A thin façade that
19
+ • keeps a reference to a *shared* PEFT-wrapped model,
20
+ • ensures `set_adapter(adapter)` is called on every forward,
21
+ • exposes only the parameters that should be trained for that adapter
22
+ (plus whatever extra modules you name).
23
+ """
24
+
25
+ def __init__(
26
+ self,
27
+ shared_llm: nn.Module,
28
+ adapter_id: str,
29
+ lora_config: dict,
30
+ path: Union[str, None] = None,
31
+ ):
32
+ super().__init__()
33
+ self.shared_llm = shared_llm
34
+ self.adapter_id = adapter_id
35
+ lora_config = LoraConfig(**lora_config)
36
+ # this modifies the shared llm in place, adding a lora adapter inside
37
+ self.shared_llm = get_peft_model(
38
+ model=shared_llm,
39
+ peft_config=lora_config,
40
+ adapter_name=adapter_id,
41
+ )
42
+ self.shared_llm.train()
43
+ # Load external adapter weights if provided
44
+ loaded_from: str | None = None
45
+ if path:
46
+ try:
47
+ # Supports both local filesystem paths and HF Hub repo IDs
48
+ self.shared_llm.load_adapter(
49
+ is_trainable=True,
50
+ model_id=path,
51
+ adapter_name=adapter_id,
52
+ )
53
+ loaded_from = path
54
+ except (
55
+ Exception
56
+ ) as exc: # noqa: BLE001 - want to log any load failure context
57
+ logger.warning(
58
+ f"Adapter '{adapter_id}': failed to load from '{path}': {exc}"
59
+ )
60
+
61
+ if loaded_from:
62
+ logger.info(
63
+ f"Adapter '{adapter_id}': loaded initial weights from '{loaded_from}'."
64
+ )
65
+ else:
66
+ logger.info(
67
+ f"Adapter '{adapter_id}': initialized with fresh weights (no initial weights found)."
68
+ )
69
+
70
+ def parameters(self, recurse: bool = True):
71
+ """
72
+ "recurse" is just for pytorch compatibility
73
+ """
74
+ self.shared_llm.set_adapter(self.adapter_id)
75
+ params = [p for p in self.shared_llm.parameters() if p.requires_grad]
76
+
77
+ return params
78
+
79
+ def get_base_model_logits(self, contexts):
80
+ """
81
+ Run the base model (without adapter) in inference mode, without tracking gradients.
82
+ This is useful to get reference logits for KL-divergence computation.
83
+ """
84
+ with torch.no_grad():
85
+ with self.shared_llm.disable_adapter():
86
+ return self.shared_llm(input_ids=contexts)[0]
87
+
88
+ def forward(self, *args, **kwargs):
89
+ self.shared_llm.set_adapter(self.adapter_id)
90
+ return self.shared_llm(*args, **kwargs)
91
+
92
+ def save_pretrained(self, save_path):
93
+ self.shared_llm.save_pretrained(save_path)
94
+
95
+ def gradient_checkpointing_enable(self, *args, **kwargs):
96
+ self.shared_llm.gradient_checkpointing_enable(*args, **kwargs)
97
+
98
+ @property
99
+ def dtype(self):
100
+ return self.shared_llm.dtype
101
+
102
+ @property
103
+ def device(self):
104
+ return self.shared_llm.device
src_code_for_reproducibility/models/inference_backend.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/models/inference_backend.py
3
+ Summary: Declares the inference backend interface and shared dataclasses.
4
+ """
5
+
6
+ from abc import ABC, abstractmethod
7
+ from dataclasses import dataclass
8
+ from typing import Any, Optional
9
+
10
+
11
+ @dataclass
12
+ class LLMInferenceOutput:
13
+ content: str
14
+ reasoning_content: str | None = None
15
+ log_probs: list[float] | None = None
16
+ out_token_ids: list[int] | None = None
17
+
18
+
19
+ class LLMInferenceBackend(ABC):
20
+ @abstractmethod
21
+ def __init__(self, **kwargs):
22
+ ...
23
+
24
+ @abstractmethod
25
+ def prepare_adapter(
26
+ self, adapter_id: str, weights_got_updated: bool = False
27
+ ) -> None:
28
+ """Ensure adapter is ready/loaded for next generation call."""
29
+
30
+ @abstractmethod
31
+ async def generate(self, prompt: list[dict], regex: Optional[str] = None) -> str:
32
+ ...
33
+
34
+ @abstractmethod
35
+ def toggle_training_mode(self) -> None:
36
+ ...
37
+
38
+ @abstractmethod
39
+ def toggle_eval_mode(self) -> None:
40
+ ...
41
+
42
+ @abstractmethod
43
+ def shutdown(self) -> None:
44
+ ...
src_code_for_reproducibility/models/inference_backend_dummy.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/models/inference_backend_dummy.py
3
+ Summary: Stub inference backend that returns synthetic completions for tests.
4
+ """
5
+
6
+ import asyncio
7
+ from typing import Optional
8
+
9
+ import rstr
10
+ from transformers import AutoTokenizer
11
+
12
+ from mllm.models.inference_backend import LLMInferenceBackend, LLMInferenceOutput
13
+ from mllm.utils.short_id_gen import generate_short_id
14
+
15
+
16
+ class DummyInferenceBackend(LLMInferenceBackend):
17
+ def __init__(
18
+ self,
19
+ *args,
20
+ **kwargs,
21
+ ):
22
+ pass
23
+
24
+ def prepare_adapter(
25
+ self,
26
+ adapter_id: Optional[str],
27
+ weights_got_updated: bool,
28
+ adapter_path: Optional[str] = None,
29
+ ) -> None:
30
+ pass
31
+
32
+ async def toggle_training_mode(self) -> None:
33
+ await asyncio.sleep(0)
34
+ pass
35
+
36
+ async def toggle_eval_mode(self) -> None:
37
+ await asyncio.sleep(0)
38
+ pass
39
+
40
+ def shutdown(self) -> None:
41
+ pass
42
+
43
+ async def generate(
44
+ self,
45
+ prompt_text: str,
46
+ regex: Optional[str] = None,
47
+ extract_thinking: bool = False,
48
+ ) -> LLMInferenceOutput:
49
+ if regex:
50
+ # Create random string that respects the regex
51
+ return LLMInferenceOutput(
52
+ content=rstr.xeger(regex),
53
+ reasoning_content="I don't think, I am a dummy backend.",
54
+ )
55
+ else:
56
+ return LLMInferenceOutput(
57
+ content="I am a dummy backend without a regex.",
58
+ reasoning_content="I don't think, I am a dummy backend.",
59
+ )
src_code_for_reproducibility/models/inference_backend_vllm.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/models/inference_backend_vllm.py
3
+ Summary: Connects to in-process vLLM instances for batched generation.
4
+ """
5
+
6
+ import asyncio
7
+ import re
8
+ from typing import Optional
9
+
10
+ import torch
11
+ from transformers import AutoTokenizer
12
+ from vllm import AsyncEngineArgs, AsyncLLMEngine, SamplingParams
13
+ from vllm.inputs import TokensPrompt
14
+ from vllm.lora.request import LoRARequest
15
+ from vllm.sampling_params import GuidedDecodingParams, RequestOutputKind
16
+
17
+ from mllm.models.inference_backend import LLMInferenceBackend, LLMInferenceOutput
18
+ from mllm.utils.short_id_gen import generate_short_id
19
+
20
+
21
+ class VLLMAsyncBackend(LLMInferenceBackend):
22
+ def __init__(
23
+ self,
24
+ model_name: str,
25
+ tokenizer: AutoTokenizer,
26
+ # adapter_paths: dict[str, str],
27
+ engine_init_kwargs: dict = {},
28
+ sampling_params: dict = {},
29
+ ):
30
+ self.model_name = model_name
31
+ self.vllm_adapter_ids = {}
32
+ ea = dict(model=model_name, **engine_init_kwargs)
33
+ self.engine = AsyncLLMEngine.from_engine_args(AsyncEngineArgs(**ea))
34
+
35
+ self.sampling_params = sampling_params
36
+ self.tokenizer = tokenizer
37
+
38
+ def prepare_adapter(
39
+ self,
40
+ adapter_id: Optional[str],
41
+ adapter_path: Optional[str],
42
+ weights_got_updated: bool,
43
+ ) -> None:
44
+ if weights_got_updated:
45
+ self.vllm_adapter_ids[adapter_id] = generate_short_id()
46
+ self.current_lora_request = LoRARequest(
47
+ adapter_id,
48
+ self.vllm_adapter_ids[adapter_id],
49
+ adapter_path,
50
+ )
51
+
52
+ async def toggle_training_mode(self) -> None:
53
+ await self.engine.sleep(level=1)
54
+
55
+ async def toggle_eval_mode(self) -> None:
56
+ await self.engine.wake_up()
57
+
58
+ def shutdown(self) -> None:
59
+ # No explicit close call; engine stops when process exits.
60
+ pass
61
+
62
+ async def generate(
63
+ self,
64
+ input_token_ids: list[int],
65
+ regex: Optional[str] = None,
66
+ extract_thinking: bool = False,
67
+ ) -> LLMInferenceOutput:
68
+ # Build SamplingParams correctly
69
+ guided = GuidedDecodingParams(regex=regex) if regex else None
70
+ sp = SamplingParams(
71
+ **self.sampling_params,
72
+ guided_decoding=guided,
73
+ output_kind=RequestOutputKind.FINAL_ONLY,
74
+ )
75
+
76
+ prompt = TokensPrompt(prompt_token_ids=input_token_ids)
77
+ request_id = f"req-{asyncio.get_running_loop().time()}"
78
+ result_generator = self.engine.generate(
79
+ prompt,
80
+ sp, # SamplingParams(...)
81
+ request_id,
82
+ lora_request=self.current_lora_request,
83
+ )
84
+
85
+ async for out in result_generator: # with FINAL_ONLY this runs once
86
+ res = out
87
+
88
+ raw_text = res.outputs[0].text
89
+ out_token_ids = res.outputs[0].token_ids
90
+ log_probs = [
91
+ logprob_dict[token_id].logprob
92
+ for token_id, logprob_dict in zip(out_token_ids, res.outputs[0].logprobs)
93
+ ]
94
+ log_probs = torch.tensor(log_probs)
95
+ out_token_ids = torch.tensor(out_token_ids, dtype=torch.long)
96
+ content = raw_text
97
+ reasoning_content = None
98
+
99
+ if extract_thinking:
100
+ m = re.match(
101
+ r"^\n<think>\n([\s\S]*?)</think>\n\n(.*)$", raw_text, flags=re.DOTALL
102
+ )
103
+ if m:
104
+ reasoning_content = m.group(1)
105
+ content = m.group(2)
106
+ return LLMInferenceOutput(
107
+ content=content,
108
+ reasoning_content=reasoning_content,
109
+ log_probs=log_probs,
110
+ out_token_ids=out_token_ids,
111
+ )
src_code_for_reproducibility/models/large_language_model_local.py ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/models/large_language_model_local.py
3
+ Summary: Provides a local large language model wrapper over inference backends.
4
+ """
5
+
6
+ import logging
7
+ import os
8
+ import re
9
+ import sys
10
+ import uuid
11
+ from collections.abc import Callable
12
+ from copy import deepcopy
13
+ from datetime import datetime
14
+ from typing import Literal
15
+
16
+ import httpx
17
+ import requests
18
+ import torch
19
+ import torch.nn as nn
20
+ from torch.optim import SGD, Adam, AdamW, RMSprop
21
+ from transformers import AutoModelForCausalLM, AutoTokenizer
22
+
23
+ from mllm.chat_utils.apply_template import chat_turns_to_token_ids
24
+ from mllm.markov_games.rollout_tree import ChatTurn
25
+ from mllm.models.adapter_training_wrapper import AdapterWrapper
26
+ from mllm.models.inference_backend import LLMInferenceOutput
27
+ from mllm.models.inference_backend_dummy import DummyInferenceBackend
28
+ from mllm.models.inference_backend_vllm import VLLMAsyncBackend
29
+
30
+ logger = logging.getLogger(__name__)
31
+ logger.addHandler(logging.StreamHandler(sys.stdout))
32
+
33
+ AdapterID = str
34
+ PolicyID = str
35
+
36
+
37
+ class LeanLocalLLM:
38
+ """
39
+ Wrapper that manages local HuggingFace models, adapters, and inference backends.
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ llm_id: str = "base_llm",
45
+ model_name: str = "Qwen/Qwen3-4B-Instruct-2507",
46
+ device: str = "cuda",
47
+ hf_kwargs: dict = {},
48
+ adapter_configs: dict = {},
49
+ output_directory: str = "./models/",
50
+ inference_backend: Literal["vllm", "dummy"] = "vllm",
51
+ inference_backend_sampling_params: dict = {},
52
+ inference_backend_init_kwargs: dict = {},
53
+ initial_adapter_paths: dict[str, str] | None = None,
54
+ initial_buffer_paths: list[str] | None = None,
55
+ enable_thinking: bool = None,
56
+ regex_max_attempts: int = -1,
57
+ max_thinking_characters: int = 0,
58
+ ):
59
+ self.inference_backend_name = inference_backend
60
+ self.output_directory = output_directory
61
+ self.llm_id = llm_id
62
+ self.device = torch.device(device) if device else torch.device("cuda")
63
+ self.model_name = model_name
64
+ self.adapter_configs = adapter_configs
65
+ self.adapter_ids = list(adapter_configs.keys())
66
+ self.enable_thinking = enable_thinking
67
+ self.regex_max_attempts = regex_max_attempts
68
+ self.initial_buffer_paths = initial_buffer_paths
69
+ self.max_thinking_characters = max_thinking_characters
70
+ self.regex_retries_count = 0
71
+
72
+ # Optional user-specified initial adapter weight locations (local or HF Hub)
73
+ # Format: {adapter_id: path_or_repo_id}
74
+ self.initial_adapter_paths: dict[str, str] | None = initial_adapter_paths
75
+
76
+ # Path management / imports
77
+ self.save_path = str(os.path.join(output_directory, model_name, "adapters"))
78
+ self.adapter_paths = {
79
+ adapter_id: os.path.join(self.save_path, adapter_id)
80
+ for adapter_id in self.adapter_ids
81
+ }
82
+ checkpoints_dir = os.path.join(self.output_directory, "checkpoints")
83
+ self.past_agent_adapter_paths = {}
84
+ if os.path.isdir(checkpoints_dir):
85
+ for dirname in os.listdir(checkpoints_dir):
86
+ dirpath = os.path.join(checkpoints_dir, dirname)
87
+ if os.path.isdir(dirpath):
88
+ self.past_agent_adapter_paths[f"{dirname}_buffer"] = os.path.join(
89
+ dirpath, "agent_adapter"
90
+ )
91
+ logger.info(
92
+ f"Loaded {len(self.past_agent_adapter_paths)} past agent adapters from checkpoints directory."
93
+ )
94
+ if self.initial_buffer_paths is not None:
95
+ previous_count = len(self.past_agent_adapter_paths)
96
+ for path in self.initial_buffer_paths:
97
+ if os.path.isdir(path):
98
+ for dirname in os.listdir(path):
99
+ dirpath = os.path.join(path, dirname)
100
+ if os.path.isdir(dirpath):
101
+ self.past_agent_adapter_paths[
102
+ f"{dirname}_buffer"
103
+ ] = os.path.join(dirpath, "agent_adapter")
104
+ else:
105
+ logger.warning(
106
+ f"Initial buffer path {path} does not exist or is not a directory."
107
+ )
108
+ logger.info(
109
+ f"Loaded {len(self.past_agent_adapter_paths) - previous_count} past agent adapters from user-specified initial buffer paths."
110
+ )
111
+ self.past_agent_adapter_ids = list(self.past_agent_adapter_paths.keys())
112
+
113
+ # ID management for tracking adapter versions
114
+ self.adapter_train_ids = {
115
+ adapter_id: self.short_id_generator() for adapter_id in self.adapter_ids
116
+ }
117
+ # Initialize tokenizer
118
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
119
+ # Setup padding token to be same as EOS token
120
+ self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
121
+ self.tokenizer.pad_token = self.tokenizer.eos_token
122
+
123
+ self.weights_got_updated: dict[AdapterID, bool] = {
124
+ adapter_id: False for adapter_id in self.adapter_ids
125
+ }
126
+ self.weights_got_updated.update(
127
+ {adapter_id: False for adapter_id in self.past_agent_adapter_ids}
128
+ )
129
+ self.current_lora_request = None
130
+ self.currently_loaded_adapter_id = None
131
+
132
+ # ---------------------------------------------------------
133
+ # Init HF model, peft adapters
134
+ # ---------------------------------------------------------
135
+ self.shared_hf_llm = AutoModelForCausalLM.from_pretrained(
136
+ pretrained_model_name_or_path=model_name,
137
+ **hf_kwargs,
138
+ )
139
+ self.hf_adapters = {}
140
+ self.optimizers = {}
141
+ for adapter_id in self.adapter_ids:
142
+ # Prefer output-folder path if it exists; else fall back to user-specified initial path if provided
143
+ output_path = os.path.join(self.save_path, adapter_id)
144
+ chosen_path: str | None = None
145
+ if os.path.isdir(output_path) and os.listdir(output_path):
146
+ chosen_path = output_path
147
+ logger.info(
148
+ f"Initializing adapter '{adapter_id}': using existing weights from output folder '{chosen_path}'."
149
+ )
150
+ elif (
151
+ self.initial_adapter_paths and adapter_id in self.initial_adapter_paths
152
+ ):
153
+ chosen_path = self.initial_adapter_paths[adapter_id]
154
+ logger.info(
155
+ f"Initializing adapter '{adapter_id}': using provided initial path '{chosen_path}'."
156
+ )
157
+ else:
158
+ logger.info(
159
+ f"Initializing adapter '{adapter_id}': no initial weights provided or found; starting from scratch."
160
+ )
161
+ hf_adapter = AdapterWrapper(
162
+ shared_llm=self.shared_hf_llm,
163
+ adapter_id=adapter_id,
164
+ lora_config=adapter_configs[adapter_id],
165
+ path=chosen_path,
166
+ ).to(device)
167
+ self.hf_adapters[adapter_id] = hf_adapter
168
+ # Persist current state of all adapters (ensures remote loads are cached to disk)
169
+ self.export_adapters()
170
+
171
+ # ---------------------------------------------------------
172
+ # Init inference inference_backend
173
+ # ---------------------------------------------------------
174
+
175
+ if inference_backend == "vllm":
176
+ self.inference_backend = VLLMAsyncBackend(
177
+ model_name=self.model_name,
178
+ # adapter_paths=self.adapter_paths,
179
+ tokenizer=self.tokenizer,
180
+ engine_init_kwargs=inference_backend_init_kwargs,
181
+ sampling_params=inference_backend_sampling_params,
182
+ )
183
+ elif inference_backend == "dummy":
184
+ self.inference_backend = DummyInferenceBackend()
185
+ else:
186
+ raise ValueError(f"Unknown inference_backend: {inference_backend}")
187
+
188
+ def reset_regex_retries_count(self) -> None:
189
+ self.regex_retries_count = 0
190
+
191
+ def get_inference_policies(self) -> dict[PolicyID, Callable]:
192
+ """
193
+ Build async policy callables keyed by adapter id for inference-only usage.
194
+ """
195
+ policies = {}
196
+ for adapter_id in self.adapter_ids:
197
+ # define policy func
198
+ async def policy(
199
+ state: list[ChatTurn],
200
+ agent_id: str,
201
+ regex: str | None = None,
202
+ _adapter_id=adapter_id,
203
+ ):
204
+ self.prepare_adapter_for_inference(adapter_id=_adapter_id)
205
+ response = await self.get_action(state, agent_id, regex)
206
+ return response
207
+
208
+ policies[self.llm_id + "/" + adapter_id] = policy
209
+
210
+ for adapter_id in self.past_agent_adapter_ids:
211
+ # define policy func
212
+ async def policy(
213
+ state: list[ChatTurn],
214
+ agent_id: str,
215
+ regex: str | None = None,
216
+ _adapter_id=adapter_id,
217
+ ):
218
+ self.prepare_adapter_for_inference(adapter_id=_adapter_id)
219
+ response = await self.get_action(state, agent_id, regex)
220
+ return response
221
+
222
+ policies[self.llm_id + "/" + adapter_id] = policy
223
+ return policies
224
+
225
+ def get_adapter_modules(self) -> dict[PolicyID, nn.Module]:
226
+ """
227
+ Returns wrappers over the adapters which allows them be
228
+ interfaced like regular PyTorch models.
229
+ AdapterWrapper lives in adapter_wrapper.py; the huggingface modules already wrap
230
+ parameters here, so we surface them directly until an extra shim is required.
231
+ """
232
+ trainable_objects = {an: self.hf_adapters[an] for an in self.adapter_ids}
233
+ return trainable_objects
234
+
235
+ async def toggle_training_mode(self) -> None:
236
+ for adn in self.adapter_ids:
237
+ self.adapter_train_ids[adn] = self.short_id_generator()
238
+ await self.inference_backend.toggle_training_mode()
239
+
240
+ async def toggle_eval_mode(self) -> None:
241
+ await self.inference_backend.toggle_eval_mode()
242
+
243
+ def prepare_adapter_for_inference(self, adapter_id: AdapterID) -> None:
244
+ self.inference_backend.prepare_adapter(
245
+ adapter_id,
246
+ adapter_path=self.adapter_paths.get(
247
+ adapter_id, self.past_agent_adapter_paths.get(adapter_id, None)
248
+ ),
249
+ weights_got_updated=self.weights_got_updated[adapter_id],
250
+ )
251
+ self.currently_loaded_adapter_id = adapter_id
252
+ self.weights_got_updated[adapter_id] = False
253
+
254
+ # def _make_prompt_text(self, prompt: list[dict]) -> str:
255
+ # if self.enable_thinking is not None:
256
+ # prompt_text = self.tokenizer.apply_chat_template(
257
+ # prompt,
258
+ # tokenize=False,
259
+ # add_generation_prompt=True,
260
+ # enable_thinking=self.enable_thinking,
261
+ # )
262
+ # else:
263
+ # prompt_text = self.tokenizer.apply_chat_template(
264
+ # prompt,
265
+ # tokenize=False,
266
+ # add_generation_prompt=True,
267
+ # )
268
+
269
+ # return prompt_text
270
+
271
+ async def get_action(
272
+ self, state: list[ChatTurn], agent_id: str, regex: str | None = None
273
+ ) -> ChatTurn:
274
+ current_regex = regex if self.regex_max_attempts == -1 else None
275
+ pattern = re.compile(regex) if regex else None
276
+ nb_attempts = 0
277
+ state = state[:]
278
+ while True:
279
+ context_token_ids = chat_turns_to_token_ids(
280
+ chats=state,
281
+ tokenizer=self.tokenizer,
282
+ enable_thinking=self.enable_thinking,
283
+ )
284
+ policy_output = await self.inference_backend.generate(
285
+ input_token_ids=context_token_ids.tolist(),
286
+ extract_thinking=(self.max_thinking_characters > 0),
287
+ regex=current_regex,
288
+ )
289
+ if (
290
+ pattern is None
291
+ or (pattern.fullmatch(policy_output.content))
292
+ or (nb_attempts >= self.regex_max_attempts)
293
+ ):
294
+ return ChatTurn(
295
+ agent_id=agent_id,
296
+ role="assistant",
297
+ content=policy_output.content,
298
+ reasoning_content=policy_output.reasoning_content,
299
+ out_token_ids=policy_output.out_token_ids,
300
+ log_probs=policy_output.log_probs,
301
+ is_state_end=False,
302
+ )
303
+ else:
304
+ self.regex_retries_count += 1
305
+ nb_attempts += 1
306
+ logger.warning(
307
+ f"Response {policy_output.content} did not match regex: {regex}, retry {nb_attempts}/{self.regex_max_attempts}"
308
+ )
309
+ if nb_attempts == self.regex_max_attempts:
310
+ current_regex = regex
311
+ # regex_prompt = ChatTurn(
312
+ # role="user",
313
+ # content=f"Invalid response format. Expected format (regex): {current_regex}\n Please try again and provide ONLY a response that matches this regex.",
314
+ # reasoning_content=None,
315
+ # log_probs=None,
316
+ # out_token_ids=None,
317
+ # is_state_end=False,
318
+ # )
319
+ # state.append(regex_prompt)
320
+
321
+ def export_adapters(self) -> None:
322
+ """
323
+ Any peft wrapper, by default, saves all adapters, not just the one currently loaded.
324
+ """
325
+
326
+ # New version of the adapters available
327
+ for adapter_id in self.adapter_ids:
328
+ self.weights_got_updated[adapter_id] = True
329
+ for adapter_id in self.past_agent_adapter_ids:
330
+ self.weights_got_updated[adapter_id] = True
331
+
332
+ adapter_id = self.adapter_ids[0]
333
+ self.hf_adapters[adapter_id].save_pretrained(self.save_path)
334
+
335
+ def checkpoint_all_adapters(self, checkpoint_indicator: str) -> None:
336
+ """
337
+ Checkpoints all adapters to the configured output directory.
338
+ """
339
+ adapter_id = self.adapter_ids[0]
340
+ output_dir = os.path.join(self.output_directory, "checkpoints")
341
+ os.makedirs(output_dir, exist_ok=True)
342
+ date_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
343
+ agent_adapter_dir = f"{adapter_id}-{checkpoint_indicator}-{date_str}"
344
+ export_path = os.path.join(output_dir, agent_adapter_dir)
345
+ for adapter_id in self.adapter_ids:
346
+ if "agent" in adapter_id:
347
+ self.past_agent_adapter_paths[
348
+ f"{agent_adapter_dir}_buffer"
349
+ ] = os.path.join(export_path, adapter_id)
350
+ self.past_agent_adapter_ids.append(f"{agent_adapter_dir}_buffer")
351
+ self.weights_got_updated[f"{agent_adapter_dir}_buffer"] = False
352
+ self.hf_adapters[adapter_id].save_pretrained(export_path)
353
+
354
+ def short_id_generator(self) -> str:
355
+ """
356
+ Generates a short unique ID for tracking adapter versions.
357
+
358
+ Returns:
359
+ int: An 8-digit integer ID.
360
+ """
361
+ return str(uuid.uuid4().int)[:8]
src_code_for_reproducibility/models/scalar_critic.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/models/scalar_critic.py
3
+ Summary: Defines a scalar critic network and helper utilities.
4
+ """
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.optim as optim
9
+ from peft import LoraConfig, get_peft_model
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer
11
+
12
+ from mllm.models.adapter_training_wrapper import AdapterWrapper
13
+
14
+
15
+ class ScalarCritic(nn.Module):
16
+ """
17
+ A causal-LM critic_adapter + a scalar value head:
18
+ V_φ(s) = wᵀ h_last + b
19
+ Only LoRA adapters (inside critic_adapter) and the value head are trainable.
20
+ """
21
+
22
+ def __init__(self, critic_adapter: AdapterWrapper):
23
+ super().__init__()
24
+ self.critic_adapter = critic_adapter
25
+ hidden_size = self.critic_adapter.shared_llm.config.hidden_size
26
+ self.value_head = nn.Linear(hidden_size, 1).to(
27
+ dtype=critic_adapter.dtype, device=critic_adapter.device
28
+ )
29
+
30
+ def forward(self, input_ids, attention_mask=None, **kwargs):
31
+ # AdapterWrapper activates its own adapter internally
32
+ outputs = self.critic_adapter(
33
+ input_ids=input_ids,
34
+ attention_mask=attention_mask,
35
+ output_hidden_states=True,
36
+ **kwargs,
37
+ )
38
+ h_last = outputs.hidden_states[-1] # (B, S, H)
39
+ values = self.value_head(h_last).squeeze(-1) # (B, S)
40
+ return values
41
+
42
+ def parameters(self, recurse: bool = True):
43
+ """Iterator over *trainable* parameters for this critic."""
44
+ # 1) LoRA params for *this* adapter
45
+ for p in self.critic_adapter.parameters():
46
+ yield p
47
+ # 2) scalar head
48
+ yield from self.value_head.parameters()
49
+
50
+ def gradient_checkpointing_enable(self, *args, **kwargs):
51
+ self.critic_adapter.gradient_checkpointing_enable(*args, **kwargs)
52
+
53
+ @property
54
+ def dtype(self):
55
+ return self.critic_adapter.dtype
56
+
57
+ @property
58
+ def device(self):
59
+ return self.critic_adapter.device
src_code_for_reproducibility/training/tally_rollout.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/training/tally_rollout.py
3
+ Summary: Serializes rollout data into tallies for downstream processing.
4
+ """
5
+
6
+ import json
7
+ import os
8
+ from copy import deepcopy
9
+ from typing import Union
10
+
11
+ import numpy as np
12
+ import pandas as pd
13
+ import torch
14
+ from transformers import AutoTokenizer
15
+
16
+
17
+ class RolloutTallyItem:
18
+ def __init__(
19
+ self,
20
+ crn_ids: list[str],
21
+ rollout_ids: list[str],
22
+ agent_ids: list[str],
23
+ metric_matrix: torch.Tensor,
24
+ ):
25
+ """Lightweight data container that keeps rollout-aligned metric matrices."""
26
+ if isinstance(crn_ids, torch.Tensor):
27
+ crn_ids = crn_ids.detach().cpu().numpy()
28
+ if isinstance(rollout_ids, torch.Tensor):
29
+ rollout_ids = rollout_ids.detach().cpu().numpy()
30
+ if isinstance(agent_ids, torch.Tensor):
31
+ agent_ids = agent_ids.detach().cpu().numpy()
32
+ self.crn_ids = crn_ids
33
+ self.rollout_ids = rollout_ids
34
+ self.agent_ids = agent_ids
35
+ metric_matrix = metric_matrix.detach().cpu()
36
+ assert (
37
+ 0 < metric_matrix.ndim <= 2
38
+ ), "Metric matrix must have less than or equal to 2 dimensions"
39
+ if metric_matrix.ndim == 1:
40
+ metric_matrix = metric_matrix.reshape(1, -1)
41
+ # Convert to float32 if tensor is in BFloat16 format (not supported by numpy)
42
+ if metric_matrix.dtype == torch.bfloat16:
43
+ metric_matrix = metric_matrix.float()
44
+ self.metric_matrix = metric_matrix.numpy()
45
+
46
+
47
+ class RolloutTally:
48
+ """
49
+ Tally is a utility class for collecting and storing training metrics.
50
+ It supports adding metrics at specified paths and saving them to disk.
51
+ """
52
+
53
+ def __init__(self):
54
+ """
55
+ Initializes the RolloutTally object.
56
+
57
+ Args:
58
+ tokenizer (AutoTokenizer): Tokenizer for converting token IDs to strings.
59
+ max_context_length (int, optional): Maximum context length for contextualized metrics. Defaults to 30.
60
+ """
61
+ # Array-preserving structure (leaf lists hold numpy arrays / scalars)
62
+ self.metrics = {}
63
+ # Global ordered list of sample identifiers (crn_id, rollout_id) added in the order samples are processed
64
+
65
+ def reset(self):
66
+ """Reset the tally to an empty dict."""
67
+ self.metrics = {}
68
+
69
+ def get_from_nested_dict(self, dictio: dict, path: str):
70
+ """Retrieve a nested entry, creating intermediate dicts as needed."""
71
+ assert isinstance(path, list), "Path must be list."
72
+ for sp in path[:-1]:
73
+ dictio = dictio.setdefault(sp, {})
74
+ return dictio.get(path[-1], None)
75
+
76
+ def set_at_path(self, dictio: dict, path: str, value):
77
+ """Store ``value`` at ``path``; helper used by ``add_metric``."""
78
+ for sp in path[:-1]:
79
+ dictio = dictio.setdefault(sp, {})
80
+ dictio[path[-1]] = value
81
+
82
+ def add_metric(self, path: list[str], rollout_tally_item: RolloutTallyItem):
83
+ """
84
+ Adds a metric to the base tally at the specified path.
85
+
86
+ Args:
87
+ path (list): List of keys representing the path in the base tally.
88
+ rollout_tally_item (RolloutTallyItem): The rollout tally item to add.
89
+ """
90
+ rollout_tally_item = deepcopy(rollout_tally_item)
91
+
92
+ # Update array-preserving tally
93
+ array_list = self.get_from_nested_dict(dictio=self.metrics, path=path)
94
+ if array_list is None:
95
+ self.set_at_path(dictio=self.metrics, path=path, value=[rollout_tally_item])
96
+ else:
97
+ array_list.append(rollout_tally_item)
98
+
99
+ def save(self, identifier: str, folder: str):
100
+ """Persist the tally as a pickle (metrics only) under ``folder``."""
101
+ os.makedirs(name=folder, exist_ok=True)
102
+
103
+ from datetime import datetime
104
+
105
+ now = datetime.now()
106
+
107
+ # Pickle only (fastest, exact structure with numpy/scalars at leaves)
108
+ try:
109
+ import pickle
110
+
111
+ pkl_path = os.path.join(folder, f"{identifier}.rt_tally.pkl")
112
+ payload = {"metrics": self.metrics}
113
+ with open(pkl_path, "wb") as f:
114
+ pickle.dump(payload, f, protocol=pickle.HIGHEST_PROTOCOL)
115
+ except Exception:
116
+ pass
src_code_for_reproducibility/training/tally_tokenwise.py ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/training/tally_tokenwise.py
3
+ Summary: Converts token-level tallies into per-token statistics.
4
+ """
5
+
6
+ import json
7
+ import os
8
+ from typing import Any, Dict, List, Tuple, Union
9
+
10
+ import numpy as np
11
+ import pandas as pd
12
+ import torch
13
+ from transformers import AutoTokenizer
14
+
15
+
16
+ class ContextualizedTokenwiseTally:
17
+ """
18
+ Collect, store, and save token-level metrics per rollout.
19
+
20
+ - One DataFrame per rollout_id in `paths`
21
+ - Index = timestep (int)
22
+ - Columns are added incrementally via `add_contexts()` and `add_data()`
23
+ - Cells may contain scalars, strings, or lists (dtype=object)
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ tokenizer: AutoTokenizer,
29
+ paths: List[str],
30
+ max_context_length: int = 30,
31
+ ):
32
+ """
33
+ Args:
34
+ tokenizer: HuggingFace tokenizer used to convert tids -> tokens
35
+ paths: rollout identifiers (parallel to batch dimension)
36
+ max_context_length: truncate context token lists to this length
37
+ """
38
+ self.tokenizer = tokenizer
39
+ self.paths = paths
40
+ self.max_context_length = max_context_length
41
+ self.tally: Dict[str, pd.DataFrame] = {path: pd.DataFrame() for path in paths}
42
+
43
+ # set later by setters
44
+ self.contexts: torch.Tensor | None = None
45
+ self.action_mask: torch.Tensor | None = None
46
+ self.range: Tuple[int, int] | None = None
47
+
48
+ # --------- Utilities ---------
49
+
50
+ def tids_to_str(self, tids: List[int]) -> List[str]:
51
+ """Convert a list of token IDs to a list of token strings."""
52
+ return self.tokenizer.convert_ids_to_tokens(tids)
53
+
54
+ def _ensure_ready(self):
55
+ """Validate that action mask and range are configured prior to writes."""
56
+ assert self.action_mask is not None, "call set_action_mask(mask) first"
57
+ assert self.range is not None, "call set_range((start, end)) first"
58
+
59
+ @staticmethod
60
+ def _sanitize_filename(name: Any) -> str:
61
+ """Make a safe filename from any rollout_id."""
62
+ s = str(name)
63
+ bad = {os.sep, " ", ":", "|", "<", ">", '"', "'"}
64
+ if os.altsep is not None:
65
+ bad.add(os.altsep)
66
+ for ch in bad:
67
+ s = s.replace(ch, "_")
68
+ return s
69
+
70
+ @staticmethod
71
+ def _pad_left(seq: List[Any], length: int, pad_val: Any = "") -> List[Any]:
72
+ """Left-pad a sequence to `length` with `pad_val`."""
73
+ if len(seq) >= length:
74
+ return seq[-length:]
75
+ return [pad_val] * (length - len(seq)) + list(seq)
76
+
77
+ # --------- Setters ---------
78
+
79
+ def set_action_mask(self, action_mask: torch.Tensor):
80
+ """Register the (B, S) mask indicating which tokens correspond to actions."""
81
+ self.action_mask = action_mask
82
+
83
+ def set_range(self, range: Tuple[int, int]):
84
+ """Record which subset of ``paths`` the current mini-batch corresponds to."""
85
+ self.range = range
86
+
87
+ # --------- Column builders ---------
88
+
89
+ def add_contexts(self, contexts: torch.Tensor):
90
+ """
91
+ Add a single 'context' column (list[str]) for valid steps.
92
+
93
+ Expects `contexts` with shape (B, S): token id at each timestep.
94
+ For each valid timestep t, we use the last N tokens up to and including t:
95
+ window = contexts[i, max(0, t - N + 1) : t + 1]
96
+ The list is left-padded with "" to always be length N.
97
+ """
98
+ self._ensure_ready()
99
+
100
+ current_paths = self.paths[self.range[0] : self.range[1]]
101
+ B, S = contexts.shape
102
+ N = self.max_context_length
103
+
104
+ # to CPU ints once
105
+ contexts_cpu = contexts.detach().to("cpu")
106
+
107
+ for i in range(B):
108
+ rollout_id = current_paths[i]
109
+ df = self.tally.get(rollout_id, pd.DataFrame())
110
+
111
+ valid_idx = torch.nonzero(
112
+ self.action_mask[i].bool(), as_tuple=False
113
+ ).squeeze(-1)
114
+ if valid_idx.numel() == 0:
115
+ self.tally[rollout_id] = df
116
+ continue
117
+
118
+ idx_list = valid_idx.tolist()
119
+
120
+ # ensure index contains valid steps
121
+ if df.empty:
122
+ df = pd.DataFrame(index=idx_list)
123
+ else:
124
+ new_index = sorted(set(df.index.tolist()) | set(idx_list))
125
+ if list(df.index) != new_index:
126
+ df = df.reindex(new_index)
127
+
128
+ # build context windows
129
+ ctx_token_lists = []
130
+ for t in idx_list:
131
+ start = max(0, t - N + 1)
132
+ window_ids = contexts_cpu[i, start : t + 1].tolist()
133
+ window_toks = self.tids_to_str([int(x) for x in window_ids])
134
+ if len(window_toks) < N:
135
+ window_toks = [""] * (N - len(window_toks)) + window_toks
136
+ else:
137
+ window_toks = window_toks[-N:]
138
+ ctx_token_lists.append(window_toks)
139
+
140
+ # single 'context' column
141
+ if "context" not in df.columns:
142
+ df["context"] = pd.Series(index=df.index, dtype=object)
143
+ df.loc[idx_list, "context"] = pd.Series(
144
+ ctx_token_lists, index=idx_list, dtype=object
145
+ )
146
+
147
+ self.tally[rollout_id] = df
148
+
149
+ def add_data(
150
+ self,
151
+ metric_id: str,
152
+ metrics: torch.Tensor,
153
+ to_tids: bool = False,
154
+ ):
155
+ """
156
+ Add a metric column for valid steps.
157
+
158
+ Args:
159
+ metric_id: column name
160
+ metrics: shape (B, S) for scalars/ids or (B, S, K) for top-k vectors
161
+ to_tids: if True, treat ints/lists of ints as tids and convert to tokens
162
+ """
163
+ self._ensure_ready()
164
+ current_paths = self.paths[self.range[0] : self.range[1]]
165
+
166
+ if metrics.dim() == 2:
167
+ B, S = metrics.shape
168
+ elif metrics.dim() == 3:
169
+ B, S, _ = metrics.shape
170
+ else:
171
+ raise ValueError("metrics must be (B, S) or (B, S, K)")
172
+
173
+ for i in range(B):
174
+ rollout_id = current_paths[i]
175
+ df = self.tally.get(rollout_id, pd.DataFrame())
176
+
177
+ valid_idx = torch.nonzero(
178
+ self.action_mask[i].bool(), as_tuple=False
179
+ ).squeeze(-1)
180
+ if valid_idx.numel() == 0:
181
+ self.tally[rollout_id] = df
182
+ continue
183
+
184
+ idx_list = valid_idx.detach().cpu().tolist()
185
+
186
+ # Ensure index contains valid steps
187
+ if df.empty:
188
+ df = pd.DataFrame(index=idx_list)
189
+ else:
190
+ new_index = sorted(set(df.index.tolist()) | set(idx_list))
191
+ if list(df.index) != new_index:
192
+ df = df.reindex(new_index)
193
+
194
+ # Slice metrics at valid steps
195
+ m_valid = metrics[i][valid_idx]
196
+
197
+ # -> pure python lists (1D list or list-of-lists)
198
+ values = m_valid.detach().cpu().tolist()
199
+
200
+ # optional tids -> tokens
201
+ if to_tids:
202
+
203
+ def _to_tokish(x):
204
+ if isinstance(x, list):
205
+ return self.tids_to_str([int(v) for v in x])
206
+ else:
207
+ return self.tids_to_str([int(x)])[0]
208
+
209
+ values = [_to_tokish(v) for v in values]
210
+
211
+ # Ensure column exists with object dtype, then assign via aligned Series
212
+ if metric_id not in df.columns:
213
+ df[metric_id] = pd.Series(index=df.index, dtype=object)
214
+
215
+ if isinstance(values, np.ndarray):
216
+ values = values.tolist()
217
+
218
+ if len(values) != len(idx_list):
219
+ raise ValueError(
220
+ f"Length mismatch for '{metric_id}': values={len(values)} vs idx_list={len(idx_list)}"
221
+ )
222
+
223
+ df.loc[idx_list, metric_id] = pd.Series(
224
+ values, index=idx_list, dtype=object
225
+ )
226
+ self.tally[rollout_id] = df
227
+
228
+ # --------- Saving ---------
229
+
230
+ def save(self, path: str):
231
+ """
232
+ Write a manifest JSON and one CSV per rollout.
233
+
234
+ - Manifest includes metadata only (safe to JSON).
235
+ - Each rollout CSV is written with index label 'timestep'.
236
+ - Only a single 'context' column (list[str]).
237
+ """
238
+ if not self.tally or all(df.empty for df in self.tally.values()):
239
+ return
240
+
241
+ os.makedirs(path, exist_ok=True)
242
+ from datetime import datetime
243
+
244
+ now = datetime.now()
245
+
246
+ manifest = {
247
+ "created_at": f"{now:%Y-%m-%d %H:%M:%S}",
248
+ "max_context_length": self.max_context_length,
249
+ "num_rollouts": len(self.tally),
250
+ "rollouts": [],
251
+ }
252
+
253
+ for rid, df in self.tally.items():
254
+ rid_str = str(rid)
255
+ safe_name = self._sanitize_filename(rid_str)
256
+ csv_path = os.path.join(path, f"{safe_name}_tokenwise.csv")
257
+
258
+ # Put 'context' first, then the rest
259
+ cols = ["context"] + [c for c in df.columns if c != "context"]
260
+ try:
261
+ df[cols].to_csv(csv_path, index=True, index_label="timestep")
262
+ except Exception as e:
263
+ continue
264
+
265
+ manifest["rollouts"].append(
266
+ {
267
+ "rollout_id": rid_str,
268
+ "csv": csv_path,
269
+ "num_rows": int(df.shape[0]),
270
+ "columns": cols,
271
+ }
272
+ )
273
+
274
+ manifest_path = os.path.join(
275
+ path, f"tokenwise_manifest_{now:%Y-%m-%d___%H-%M-%S}.json"
276
+ )
277
+ with open(manifest_path, "w") as fp:
278
+ json.dump(manifest, fp, indent=2)
src_code_for_reproducibility/training/tokenize_chats.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/training/tokenize_chats.py
3
+ Summary: Tokenizes chat datasets and prepares tensors for training.
4
+ """
5
+
6
+ import logging
7
+ import sys
8
+
9
+ import regex
10
+ import torch
11
+ from transformers import AutoTokenizer
12
+
13
+ from mllm.training.training_data_utils import TrainingChatTurn, TrajectoryBatch
14
+
15
+ logger = logging.getLogger(__name__)
16
+ logger.addHandler(logging.StreamHandler(sys.stdout))
17
+
18
+
19
+ def process_training_chat(
20
+ tokenizer: AutoTokenizer,
21
+ chat_history: list[TrainingChatTurn],
22
+ entropy_mask_regex: str | None = None,
23
+ exploration_prompts_to_remove: list[str] = [],
24
+ use_engine_out_token_ids: bool = False,
25
+ ) -> tuple[torch.IntTensor, torch.BoolTensor, torch.IntTensor, torch.BoolTensor]:
26
+ """Tokenize a single training chat and build aligned per-token masks.
27
+
28
+ Given an ordered list of `TrainingChatTurn`, this function tokenizes each
29
+ turn independently using the tokenizer's chat template, then concatenates
30
+ all resulting token sequences. It also constructs three parallel 1D masks
31
+ that align with the concatenated tokens:
32
+
33
+ - input_ids: token ids for the entire chat, turn by turn
34
+ - action_mask: True for tokens that belong to assistant turns (i.e., model
35
+ actions), False for tokens from other roles
36
+ - timesteps: per-token time step copied from the originating turn's
37
+ `time_step`
38
+ - state_ends_mask: True for the last token of any turn where
39
+ `is_state_end` is True, otherwise False
40
+
41
+ Important details:
42
+ - Each turn is passed as a single-message list to
43
+ `tokenizer.apply_chat_template` and flattened; the per-turn outputs are
44
+ then concatenated in the original order.
45
+ - Turn boundaries are not explicitly encoded beyond what the chat template
46
+ inserts; masks provide alignment for learning signals and state endings.
47
+ - No truncation or padding is performed here; downstream code should handle
48
+ batching/padding as needed.
49
+ - Note on dtypes: `input_ids` will be a LongTensor (int64). `action_mask`
50
+ and `state_ends_mask` are BoolTensors. `timesteps` is currently created
51
+ as a float tensor; adjust the implementation if integer dtype is
52
+ required downstream.
53
+
54
+ Args:
55
+ tokenizer: A Hugging Face tokenizer supporting `apply_chat_template`.
56
+ chat_history: Ordered list of `TrainingChatTurn` forming one dialogue.
57
+
58
+ Returns:
59
+ A tuple of four 1D tensors, all of equal length N (the total number of
60
+ tokens across all turns), in the following order:
61
+ - input_ids (LongTensor)
62
+ - action_mask (BoolTensor)
63
+ - timesteps (FloatTensor as implemented; see note above)
64
+ - state_ends_mask (BoolTensor)
65
+ """
66
+ state_ends_mask = []
67
+ input_ids = []
68
+ action_mask = []
69
+ timesteps = []
70
+ entropy_mask = []
71
+ engine_log_probs = []
72
+ for train_chat_turn in chat_history:
73
+ is_state_end = train_chat_turn.is_state_end
74
+ time_step = train_chat_turn.time_step
75
+ is_action = train_chat_turn.role == "assistant"
76
+
77
+ # Remove exploration prompts from training data
78
+ for exploration_prompt in exploration_prompts_to_remove:
79
+ if exploration_prompt in train_chat_turn.content:
80
+ train_chat_turn.content = train_chat_turn.content.replace(
81
+ exploration_prompt, ""
82
+ )
83
+
84
+ chat_turn = {
85
+ "role": train_chat_turn.role,
86
+ "content": train_chat_turn.content,
87
+ }
88
+ if entropy_mask_regex is not None:
89
+ is_entropy_mask_true = (
90
+ regex.search(entropy_mask_regex, train_chat_turn.content) is not None
91
+ )
92
+ else:
93
+ is_entropy_mask_true = True
94
+ if is_action:
95
+ chat_turn_ids = train_chat_turn.out_token_ids
96
+ nb_chat_turns_ids = chat_turn_ids.numel()
97
+ action_mask.append(torch.ones(nb_chat_turns_ids, dtype=torch.bool))
98
+ engine_log_probs.append(train_chat_turn.log_probs)
99
+ else:
100
+ chat_turn_ids = train_chat_turn.chat_template_token_ids
101
+ nb_chat_turns_ids = chat_turn_ids.numel()
102
+ action_mask.append(torch.zeros(nb_chat_turns_ids, dtype=torch.bool))
103
+ engine_log_probs.append(torch.zeros(nb_chat_turns_ids, dtype=torch.float))
104
+ nb_chat_turns_ids = chat_turn_ids.numel()
105
+ state_ends_mask.append(torch.zeros(nb_chat_turns_ids, dtype=torch.bool))
106
+ if is_state_end:
107
+ state_ends_mask[-1][-1] = True # last token is state end
108
+ input_ids.append(chat_turn_ids)
109
+ entropy_mask.append(torch.ones(nb_chat_turns_ids, dtype=torch.bool))
110
+ if not is_entropy_mask_true:
111
+ entropy_mask[-1] = entropy_mask[-1] * False
112
+ timesteps.append(torch.ones(nb_chat_turns_ids) * time_step)
113
+ input_ids = torch.cat(input_ids)
114
+ action_mask = torch.cat(action_mask)
115
+ entropy_mask = torch.cat(entropy_mask)
116
+ timesteps = torch.cat(timesteps)
117
+ timesteps = timesteps.to(torch.long)
118
+ state_ends_mask = torch.cat(state_ends_mask)
119
+ engine_log_probs = torch.cat(engine_log_probs)
120
+
121
+ return (
122
+ input_ids,
123
+ action_mask,
124
+ entropy_mask,
125
+ timesteps,
126
+ state_ends_mask,
127
+ engine_log_probs,
128
+ )
src_code_for_reproducibility/training/trainer_common.py ADDED
@@ -0,0 +1,1032 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: mllm/training/trainer_common.py
3
+ Summary: Shared trainer utilities, base classes, and gradient helpers.
4
+ """
5
+
6
+ import logging
7
+ import os
8
+ import pickle
9
+ import sys
10
+ from abc import ABC, abstractmethod
11
+ from typing import Callable, Literal, Union
12
+
13
+ import numpy as np
14
+ import torch
15
+ import torch.nn.functional as F
16
+ from accelerate import Accelerator
17
+ from pandas._libs.tslibs.offsets import CBMonthBegin
18
+ from peft import LoraConfig
19
+ from torch.nn.utils.rnn import pad_sequence
20
+ from transformers import AutoModelForCausalLM, AutoTokenizer
21
+
22
+ from mllm.markov_games.rollout_tree import *
23
+ from mllm.markov_games.rollout_tree import RolloutTreeRootNode
24
+ from mllm.training.annealing_methods import sigmoid_annealing
25
+ from mllm.training.credit_methods import (
26
+ get_discounted_returns,
27
+ get_generalized_advantage_estimates,
28
+ get_rloo_credits,
29
+ whiten_advantages,
30
+ whiten_advantages_time_step_wise,
31
+ )
32
+ from mllm.training.tally_metrics import Tally
33
+ from mllm.training.tally_rollout import RolloutTally, RolloutTallyItem
34
+ from mllm.training.tally_tokenwise import ContextualizedTokenwiseTally
35
+ from mllm.training.tokenize_chats import *
36
+ from mllm.training.tokenize_chats import process_training_chat
37
+ from mllm.training.training_data_utils import *
38
+ from mllm.training.training_data_utils import (
39
+ TrainingBatch,
40
+ TrajectoryBatch,
41
+ get_tokenwise_credits,
42
+ )
43
+ from mllm.utils.resource_context import resource_logger_context
44
+
45
+ logger = logging.getLogger(__name__)
46
+ logger.addHandler(logging.StreamHandler(sys.stdout))
47
+
48
+
49
+ @dataclass
50
+ class TrainerAnnealingState:
51
+ annealing_step_counter: int = 0
52
+
53
+
54
+ class BaseTrainer(ABC):
55
+ """
56
+ Shared scaffolding for policy-gradient trainers (optimizer wiring, logging, etc.).
57
+
58
+ Subclasses implement `set_agent_trajectory_data` / `share_advantage_data`
59
+ to plug in algorithm-specific behavior.
60
+ """
61
+
62
+ def __init__(
63
+ self,
64
+ policy: AutoModelForCausalLM,
65
+ policy_optimizer: torch.optim.Optimizer,
66
+ critic: Union[AutoModelForCausalLM, None],
67
+ critic_optimizer: Union[torch.optim.Optimizer, None],
68
+ tokenizer: AutoTokenizer,
69
+ lr_scheduler: torch.optim.lr_scheduler.LRScheduler,
70
+ critic_lr_scheduler: Union[torch.optim.lr_scheduler.LRScheduler, None],
71
+ ######################################################################
72
+ entropy_coeff: float,
73
+ entropy_topk: int,
74
+ entropy_mask_regex: Union[str, None],
75
+ kl_coeff: float,
76
+ gradient_clipping: Union[float, None],
77
+ restrict_tokens: Union[list[str], None],
78
+ mini_batch_size: int,
79
+ use_gradient_checkpointing: bool,
80
+ temperature: float,
81
+ device: str,
82
+ whiten_advantages: bool,
83
+ whiten_advantages_time_step_wise: bool,
84
+ use_gae: bool,
85
+ use_gae_lambda_annealing: bool,
86
+ gae_lambda_annealing_limit: float,
87
+ gae_lambda_annealing_method: Literal["sigmoid_annealing"],
88
+ gae_lambda_annealing_method_params: dict,
89
+ pg_loss_normalization: Literal["batch", "nb_tokens"],
90
+ use_rloo: bool,
91
+ skip_discounted_state_visitation: bool,
92
+ discount_factor: float,
93
+ enable_tokenwise_logging: bool,
94
+ save_path: str,
95
+ reward_normalizing_constant: float = 1.0,
96
+ critic_loss_type: Literal["mse", "huber"] = "huber",
97
+ exploration_prompts_to_remove: list[str] = [],
98
+ filter_higher_refprob_tokens_kl: bool = False,
99
+ truncated_importance_sampling_ratio_cap: float = 0.0,
100
+ importance_sampling_strategy: Literal[
101
+ "per_token", "per_sequence"
102
+ ] = "per_token",
103
+ no_rloo_grouping: bool = False,
104
+ ):
105
+ """
106
+ Initialize the REINFORCE trainer with reward shaping for multi-agent or single-agent training.
107
+
108
+ Args:
109
+ model (AutoModelForCausalLM): The main policy model.
110
+ tokenizer (AutoTokenizer): Tokenizer for the model.
111
+ optimizer (torch.optim.Optimizer): Optimizer for the policy model.
112
+ lr_scheduler (torch.optim.lr_scheduler.LRScheduler): Learning rate scheduler for the policy model.
113
+ critic (AutoModelForCausalLM or None): Critic model for value estimation (optional).
114
+ critic_optimizer (torch.optim.Optimizer or None): Optimizer for the critic model (optional).
115
+ critic_lr_scheduler (torch.optim.lr_scheduler.LRScheduler or None): LR scheduler for the critic (optional).
116
+ config (RtConfig): Configuration object for training.
117
+ """
118
+ self.tokenizer = tokenizer
119
+ # self.tokenizer.padding_side = "left" # needed for flash attention
120
+ if self.tokenizer.pad_token_id is None:
121
+ self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
122
+ self.lr_scheduler = lr_scheduler
123
+ self.accelerator = Accelerator()
124
+ (
125
+ self.policy,
126
+ self.policy_optimizer,
127
+ self.critic,
128
+ self.critic_optimizer,
129
+ ) = self.accelerator.prepare(policy, policy_optimizer, critic, critic_optimizer)
130
+
131
+ self.critic_lr_scheduler = critic_lr_scheduler
132
+ self.tally = Tally()
133
+
134
+ if use_gradient_checkpointing == True:
135
+ self.policy.gradient_checkpointing_enable(dict(use_reentrant=False))
136
+ if critic is not None:
137
+ self.critic.gradient_checkpointing_enable(dict(use_reentrant=False))
138
+
139
+ self.save_path = save_path
140
+
141
+ # Load trainer state if it exists
142
+ self.trainer_annealing_state_path = os.path.join(
143
+ self.save_path, "trainer_annealing_state.pkl"
144
+ )
145
+ if os.path.exists(self.trainer_annealing_state_path):
146
+ logger.info(
147
+ f"Loading trainer state from {self.trainer_annealing_state_path}"
148
+ )
149
+ self.trainer_annealing_state = pickle.load(
150
+ open(self.trainer_annealing_state_path, "rb")
151
+ )
152
+ else:
153
+ self.trainer_annealing_state = TrainerAnnealingState()
154
+
155
+ # Load policy optimizer state if it exists
156
+ self.policy_optimizer_path = os.path.join(
157
+ self.save_path, "policy_optimizer_state.pt"
158
+ )
159
+ if os.path.exists(self.policy_optimizer_path):
160
+ logger.info(
161
+ f"Loading policy optimizer state from {self.policy_optimizer_path}"
162
+ )
163
+ self.policy_optimizer.load_state_dict(
164
+ torch.load(self.policy_optimizer_path)
165
+ )
166
+
167
+ # Load critic optimizer state if it exists
168
+ self.critic_optimizer_path = os.path.join(
169
+ self.save_path, "critic_optimizer_state.pt"
170
+ )
171
+ if (
172
+ os.path.exists(self.critic_optimizer_path)
173
+ and self.critic_optimizer is not None
174
+ ):
175
+ logger.info(
176
+ f"Loading critic optimizer state from {self.critic_optimizer_path}"
177
+ )
178
+ self.critic_optimizer.load_state_dict(
179
+ torch.load(self.critic_optimizer_path)
180
+ )
181
+ self.device = self.accelerator.device
182
+ self.entropy_coeff = entropy_coeff
183
+ self.entropy_topk = entropy_topk
184
+ self.entropy_mask_regex = entropy_mask_regex
185
+ self.kl_coeff = kl_coeff
186
+ self.gradient_clipping = gradient_clipping
187
+ self.restrict_tokens = restrict_tokens
188
+ self.mini_batch_size = mini_batch_size
189
+ self.use_gradient_checkpointing = use_gradient_checkpointing
190
+ self.temperature = temperature
191
+ self.use_gae = use_gae
192
+ self.whiten_advantages = whiten_advantages
193
+ self.whiten_advantages_time_step_wise = whiten_advantages_time_step_wise
194
+ self.use_rloo = use_rloo
195
+ self.skip_discounted_state_visitation = skip_discounted_state_visitation
196
+ self.use_gae_lambda_annealing = use_gae_lambda_annealing
197
+ self.gae_lambda_annealing_limit = gae_lambda_annealing_limit
198
+ if use_gae_lambda_annealing:
199
+ self.gae_lambda_annealing_method: Callable[
200
+ [int], float
201
+ ] = lambda step: eval(gae_lambda_annealing_method)(
202
+ step=step, **gae_lambda_annealing_method_params
203
+ )
204
+ self.discount_factor = discount_factor
205
+ self.enable_tokenwise_logging = enable_tokenwise_logging
206
+ self.reward_normalizing_constant = reward_normalizing_constant
207
+ self.pg_loss_normalization = pg_loss_normalization
208
+ self.critic_loss_type = critic_loss_type
209
+ self.exploration_prompts_to_remove = exploration_prompts_to_remove
210
+ # Common containers used by all trainers
211
+ self.training_data: dict = {}
212
+ self.debug_path_list: list[str] = []
213
+ self.policy_gradient_data = None
214
+ self.tally = Tally()
215
+ self.rollout_tally = RolloutTally()
216
+ self.tokenwise_tally: Union[ContextualizedTokenwiseTally, None] = None
217
+ self.filter_higher_refprob_tokens_kl = filter_higher_refprob_tokens_kl
218
+ self.truncated_importance_sampling_ratio_cap = (
219
+ truncated_importance_sampling_ratio_cap
220
+ )
221
+ self.importance_sampling_strategy = importance_sampling_strategy
222
+ self.no_rloo_grouping = no_rloo_grouping
223
+
224
+ def mask_non_restricted_token_logits(self, logits: torch.Tensor) -> torch.Tensor:
225
+ """
226
+ Masks logits so that only allowed tokens (as specified in config.restrict_tokens)
227
+ and the EOS token are active.
228
+ All other logits are set to -inf, effectively removing them from the softmax.
229
+
230
+ Args:
231
+ logits (torch.Tensor): The logits tensor of shape (B, S, V).
232
+
233
+ Returns:
234
+ torch.Tensor: The masked logits tensor.
235
+ """
236
+ # Gradients flow only through the kept logits; masking is recomputed per batch for clarity.
237
+
238
+ if self.restrict_tokens is not None:
239
+ allowed_token_ids = []
240
+ for token in self.restrict_tokens:
241
+ token_ids = self.tokenizer(token, add_special_tokens=False)["input_ids"]
242
+ allowed_token_ids.append(token_ids[0])
243
+ allowed_token_ids.append(
244
+ self.tokenizer.eos_token_id
245
+ ) # This token should always be active
246
+ allowed_token_ids = torch.tensor(allowed_token_ids, device=logits.device)
247
+ # Mask log_probs and probs to only allowed tokens
248
+ mask = torch.zeros_like(logits).bool() # (B, S, V)
249
+ mask[..., allowed_token_ids] = True
250
+ logits = torch.where(
251
+ mask,
252
+ logits,
253
+ torch.tensor(-float("inf"), device=logits.device),
254
+ )
255
+
256
+ return logits
257
+
258
+ def apply_reinforce_step(
259
+ self,
260
+ training_batch: TrainingBatch,
261
+ ) -> None:
262
+ """
263
+ Applies a single REINFORCE policy gradient step using the provided batch of rollouts.
264
+ Handles batching, loss computation (including entropy and KL regularization), gradient accumulation, and optimizer step.
265
+ Optionally logs various metrics and statistics.
266
+
267
+ Args:
268
+ paths (list[str]): List of game complete file paths for each rollout.
269
+ contexts (list[torch.Tensor]): List of context tensors for each rollout.
270
+ credits (list[torch.Tensor]): List of credit tensors (rewards/advantages) for each rollout.
271
+ action_masks (list[torch.Tensor]): List of action mask tensors for each rollout.
272
+ """
273
+ with resource_logger_context(logger, "Apply reinforce step"):
274
+ self.policy.train()
275
+ mb_size = self.mini_batch_size
276
+ nb_rollouts = len(training_batch)
277
+
278
+ # Initialize running mean logs
279
+ running_mean_logs = {
280
+ "rl_objective": 0.0,
281
+ "policy_gradient_loss": 0.0,
282
+ "policy_gradient_norm": 0.0,
283
+ "log_probs": 0.0,
284
+ "credits": 0.0,
285
+ "entropy": 0.0,
286
+ "engine_log_probs_diff_clampfrac": 0.0,
287
+ "tis_imp_ratio": 0.0,
288
+ "ref_log_probs_diff_clampfrac": 0.0,
289
+ "higher_refprob_frac": 0.0,
290
+ "tis_imp_ratio_clampfrac": 0.0,
291
+ }
292
+ if self.entropy_coeff != 0.0:
293
+ running_mean_logs["entropy"] = 0.0
294
+ if self.kl_coeff != 0.0:
295
+ running_mean_logs["kl_divergence"] = 0.0
296
+
297
+ # Get total number of tokens generated
298
+ total_tokens_generated = 0
299
+ for att_mask in training_batch.batch_action_mask:
300
+ total_tokens_generated += att_mask.sum()
301
+
302
+ # Obtain loss normalization
303
+ if self.pg_loss_normalization == "nb_tokens":
304
+ normalization_factor = total_tokens_generated
305
+ elif self.pg_loss_normalization == "batch":
306
+ normalization_factor = np.ceil(nb_rollouts / mb_size).astype(int)
307
+ else:
308
+ raise ValueError(
309
+ f"Invalid pg_loss_normalization: {self.pg_loss_normalization}"
310
+ )
311
+
312
+ # Gradient accumulation for each mini-batch
313
+ for mb in range(0, nb_rollouts, mb_size):
314
+ logger.info(f"Processing mini-batch {mb} of {nb_rollouts}")
315
+ loss = 0.0
316
+ training_mb = training_batch[mb : mb + mb_size]
317
+ training_mb = training_mb.get_padded_tensors()
318
+ training_mb.to(self.device)
319
+ (
320
+ tokens_mb,
321
+ action_mask_mb,
322
+ entropy_mask_mb,
323
+ credits_mb,
324
+ engine_log_probs_mb,
325
+ timesteps_mb,
326
+ ) = (
327
+ training_mb.batch_input_ids,
328
+ training_mb.batch_action_mask,
329
+ training_mb.batch_entropy_mask,
330
+ training_mb.batch_credits,
331
+ training_mb.batch_engine_log_probs,
332
+ training_mb.batch_timesteps,
333
+ )
334
+
335
+ # Next token prediction
336
+ contexts_mb = tokens_mb[:, :-1]
337
+ shifted_contexts_mb = tokens_mb[:, 1:]
338
+ action_mask_mb = action_mask_mb[:, 1:]
339
+ entropy_mask_mb = entropy_mask_mb[:, 1:]
340
+ credits_mb = credits_mb[:, 1:]
341
+ engine_log_probs_mb = engine_log_probs_mb[:, 1:]
342
+ timesteps_mb = timesteps_mb[:, 1:]
343
+
344
+ if self.enable_tokenwise_logging:
345
+ self.tokenwise_tally.set_action_mask(action_mask=action_mask_mb)
346
+ self.tokenwise_tally.set_range(range=(mb, mb + mb_size))
347
+ self.tokenwise_tally.add_contexts(contexts=contexts_mb)
348
+ self.tokenwise_tally.add_data(
349
+ metric_id="next_token",
350
+ metrics=shifted_contexts_mb,
351
+ to_tids=True,
352
+ )
353
+ self.tokenwise_tally.add_data(
354
+ metric_id="entropy_mask",
355
+ metrics=entropy_mask_mb,
356
+ )
357
+
358
+ if self.enable_tokenwise_logging:
359
+ self.tokenwise_tally.add_data(
360
+ metric_id="next_token_credit", metrics=credits_mb
361
+ )
362
+
363
+ # Forward pass + cast to FP-32 for higher prec. Causal LM attention masks are implicit;
364
+ # wire up a custom mask here only if the policy deviates from standard autoregressive behavior.
365
+ logits = self.policy(input_ids=contexts_mb)[0] # (B, S, V)
366
+
367
+ # Mask non-restricted tokens
368
+ if self.restrict_tokens is not None:
369
+ logits = self.mask_non_restricted_token_logits(logits)
370
+
371
+ logits /= self.temperature # (B, S, V)
372
+
373
+ # Compute new log probabilities
374
+ log_probs = F.log_softmax(logits, dim=-1) # (B, S, V)
375
+
376
+ # Get log probabilities of actions taken during rollouts
377
+ action_log_probs = log_probs.gather(
378
+ dim=-1, index=shifted_contexts_mb.unsqueeze(-1)
379
+ ).squeeze(
380
+ -1
381
+ ) # (B, S)
382
+ if self.pg_loss_normalization == "batch":
383
+ den_running_mean = action_mask_mb.sum() * normalization_factor
384
+ else:
385
+ den_running_mean = normalization_factor
386
+ running_mean_logs["log_probs"] += (
387
+ action_log_probs * action_mask_mb
388
+ ).sum().item() / den_running_mean
389
+ running_mean_logs["credits"] += (
390
+ credits_mb * action_mask_mb
391
+ ).sum().item() / den_running_mean
392
+
393
+ if self.enable_tokenwise_logging:
394
+ self.tokenwise_tally.add_data(
395
+ metric_id="next_token_log_prob",
396
+ metrics=action_log_probs,
397
+ )
398
+ self.tokenwise_tally.add_data(
399
+ metric_id="engine_next_token_log_prob",
400
+ metrics=engine_log_probs_mb,
401
+ )
402
+ self.tokenwise_tally.add_data(
403
+ metric_id="next_token_prob",
404
+ metrics=torch.exp(action_log_probs),
405
+ )
406
+ top_k_indices = torch.topk(logits, k=5, dim=-1).indices
407
+ self.tokenwise_tally.add_data(
408
+ metric_id=f"top_{5}_tids",
409
+ metrics=top_k_indices,
410
+ to_tids=True,
411
+ )
412
+ self.tokenwise_tally.add_data(
413
+ metric_id=f"top_{5}_probs",
414
+ metrics=torch.exp(log_probs).gather(
415
+ dim=-1, index=top_k_indices
416
+ ),
417
+ )
418
+
419
+ rewarded_action_log_probs = (
420
+ action_mask_mb * credits_mb * action_log_probs
421
+ )
422
+ # (B, S)
423
+ INVALID_LOGPROB = 1.0
424
+ CLAMP_VALUE = 40.0
425
+ masked_action_log_probs = torch.masked_fill(
426
+ action_log_probs, ~action_mask_mb, INVALID_LOGPROB
427
+ )
428
+ masked_engine_log_probs = torch.masked_fill(
429
+ engine_log_probs_mb, ~action_mask_mb, INVALID_LOGPROB
430
+ )
431
+ with torch.no_grad():
432
+ action_engine_log_probs_diff = (
433
+ masked_action_log_probs - masked_engine_log_probs
434
+ ).clamp(-CLAMP_VALUE, CLAMP_VALUE)
435
+ running_mean_logs["engine_log_probs_diff_clampfrac"] += (
436
+ action_engine_log_probs_diff.abs()
437
+ .eq(CLAMP_VALUE)
438
+ .float()
439
+ .sum()
440
+ .item()
441
+ / den_running_mean
442
+ )
443
+ if self.importance_sampling_strategy == "per_sequence":
444
+ tis_imp_ratio = torch.zeros_like(action_engine_log_probs_diff)
445
+ for mb_idx in range(action_engine_log_probs_diff.shape[0]):
446
+ valid_token_mask = action_mask_mb[mb_idx]
447
+ timestep_ids = timesteps_mb[mb_idx][valid_token_mask]
448
+ timestep_logprob_diffs = action_engine_log_probs_diff[mb_idx][
449
+ valid_token_mask
450
+ ]
451
+ max_timestep = int(timestep_ids.max().item()) + 1
452
+ timestep_sums = torch.zeros(
453
+ max_timestep,
454
+ device=action_engine_log_probs_diff.device,
455
+ dtype=action_engine_log_probs_diff.dtype,
456
+ )
457
+ timestep_sums.scatter_add_(
458
+ 0, timestep_ids, timestep_logprob_diffs
459
+ )
460
+ timestep_ratios = torch.exp(timestep_sums)
461
+ tis_imp_ratio[
462
+ mb_idx, valid_token_mask
463
+ ] = timestep_ratios.gather(0, timestep_ids)
464
+ else:
465
+ tis_imp_ratio = torch.exp(action_engine_log_probs_diff)
466
+ running_mean_logs["tis_imp_ratio"] += (
467
+ tis_imp_ratio * action_mask_mb
468
+ ).sum().item() / den_running_mean
469
+ if self.truncated_importance_sampling_ratio_cap > 0.0:
470
+ tis_imp_ratio = torch.clamp(
471
+ tis_imp_ratio, max=self.truncated_importance_sampling_ratio_cap
472
+ )
473
+ running_mean_logs["tis_imp_ratio_clampfrac"] += (
474
+ tis_imp_ratio.eq(self.truncated_importance_sampling_ratio_cap)
475
+ .float()
476
+ .sum()
477
+ .item()
478
+ ) / den_running_mean
479
+ rewarded_action_log_probs = (
480
+ rewarded_action_log_probs * tis_imp_ratio
481
+ )
482
+
483
+ if self.enable_tokenwise_logging:
484
+ self.tokenwise_tally.add_data(
485
+ metric_id="next_token_clogπ",
486
+ metrics=rewarded_action_log_probs,
487
+ )
488
+
489
+ # Add value term to loss
490
+ if self.pg_loss_normalization == "batch":
491
+ nb_act_tokens = action_mask_mb.sum()
492
+ mb_value = -rewarded_action_log_probs.sum() / nb_act_tokens
493
+ else:
494
+ mb_value = -rewarded_action_log_probs.sum()
495
+
496
+ loss += mb_value
497
+ running_mean_logs["rl_objective"] += mb_value.item() / den_running_mean
498
+
499
+ # -------------------------------------------------
500
+ # Entropy Regularization
501
+ # -------------------------------------------------
502
+ # Only apply entropy on distribution defined over most probable tokens
503
+ if self.entropy_topk is not None:
504
+ top_k_indices = torch.topk(
505
+ logits, k=self.entropy_topk, dim=-1
506
+ ).indices
507
+ entropy_logits = logits.gather(dim=-1, index=top_k_indices)
508
+ else:
509
+ entropy_logits = logits
510
+
511
+ token_entropy_terms = -F.softmax(
512
+ entropy_logits, dim=-1
513
+ ) * F.log_softmax(
514
+ entropy_logits, dim=-1
515
+ ) # (B, S, T)
516
+ token_entropy_terms *= (
517
+ action_mask_mb[:, :, None] * entropy_mask_mb[:, :, None]
518
+ ) # only get loss on specific action tokens
519
+
520
+ mb_entropy = token_entropy_terms.sum(dim=-1)
521
+
522
+ if self.enable_tokenwise_logging:
523
+ self.tokenwise_tally.add_data(
524
+ metric_id="entropy",
525
+ metrics=mb_entropy,
526
+ )
527
+ if self.pg_loss_normalization == "batch":
528
+ nb_act_tokens = action_mask_mb.sum()
529
+ mb_entropy = -mb_entropy.sum() / nb_act_tokens
530
+ else:
531
+ mb_entropy = -mb_entropy.sum()
532
+ running_mean_logs["entropy"] += -mb_entropy.item() / den_running_mean
533
+ if self.entropy_coeff != 0.0:
534
+ mb_entropy *= self.entropy_coeff
535
+ loss += mb_entropy
536
+
537
+ # -------------------------------------------------
538
+ # KL-DIVERGENCE
539
+ # -------------------------------------------------
540
+ if self.kl_coeff != 0.0:
541
+ ref_model_logits = self.policy.get_base_model_logits(contexts_mb)
542
+ ref_model_logits = ref_model_logits / self.temperature
543
+ # (B, S, V)
544
+ ref_model_logits = self.mask_non_restricted_token_logits(
545
+ logits=ref_model_logits
546
+ )
547
+ # (B, S, V)
548
+ ref_model_log_probs = F.log_softmax(ref_model_logits, dim=-1)
549
+ # (B, S, V)
550
+ ref_model_action_log_probs = ref_model_log_probs.gather(
551
+ dim=-1, index=shifted_contexts_mb.unsqueeze(-1)
552
+ ).squeeze(
553
+ -1
554
+ ) # (B,S)
555
+ # Approximating KL Divergence (see refs in docstring)
556
+ # Ref 1: http://joschu.net/blog/kl-approx.html
557
+ # Ref 2: https://github.dev/huggingface/trl/blob/main/trl/trainer/grpo_trainer.py#L1332
558
+ masked_ref_model_action_log_probs = torch.masked_fill(
559
+ ref_model_action_log_probs, ~action_mask_mb, INVALID_LOGPROB
560
+ )
561
+ action_log_probs_diff = (
562
+ masked_ref_model_action_log_probs - masked_action_log_probs
563
+ ).clamp(-CLAMP_VALUE, CLAMP_VALUE)
564
+ running_mean_logs["ref_log_probs_diff_clampfrac"] += (
565
+ action_log_probs_diff.abs().eq(CLAMP_VALUE).float().sum().item()
566
+ / den_running_mean
567
+ )
568
+ if self.filter_higher_refprob_tokens_kl:
569
+ higher_refprob_tokens_mask = action_log_probs_diff > 0.0
570
+ running_mean_logs["higher_refprob_frac"] += (
571
+ higher_refprob_tokens_mask.sum().item() / den_running_mean
572
+ )
573
+ action_log_probs_diff = action_log_probs_diff * (
574
+ ~higher_refprob_tokens_mask
575
+ )
576
+ kl_div = torch.expm1(action_log_probs_diff) - action_log_probs_diff
577
+ kl_div *= action_mask_mb # We only care about KLD of action tokens
578
+ if self.truncated_importance_sampling_ratio_cap > 0.0:
579
+ kl_div = kl_div * tis_imp_ratio
580
+ kl_div *= self.kl_coeff
581
+ if self.enable_tokenwise_logging:
582
+ self.tokenwise_tally.add_data(
583
+ metric_id="ref_model_next_token_log_prob",
584
+ metrics=ref_model_action_log_probs,
585
+ )
586
+ self.tokenwise_tally.add_data(
587
+ metric_id="kl_divergence",
588
+ metrics=kl_div,
589
+ )
590
+
591
+ if self.pg_loss_normalization == "batch":
592
+ nb_act_tokens = action_mask_mb.sum()
593
+ mb_kl = kl_div.sum() / nb_act_tokens
594
+ else:
595
+ mb_kl = kl_div.sum()
596
+ running_mean_logs["kl_divergence"] += (
597
+ mb_kl.item() / den_running_mean
598
+ )
599
+ loss += mb_kl
600
+
601
+ # Accumulate gradient
602
+ running_mean_logs["policy_gradient_loss"] += (
603
+ loss.item() / den_running_mean
604
+ )
605
+ loss /= normalization_factor
606
+ self.accelerator.backward(loss)
607
+
608
+ # ensure gpu memory is freed
609
+ del training_mb
610
+ del log_probs
611
+ del logits
612
+ del loss
613
+ del action_log_probs
614
+ del rewarded_action_log_probs
615
+
616
+ logger.info(
617
+ f"Accumulated the policy gradient loss for {total_tokens_generated} tokens."
618
+ )
619
+
620
+ # Clip gradients and take step
621
+ if self.gradient_clipping is not None:
622
+ grad_norm = self.accelerator.clip_grad_norm_(
623
+ self.policy.parameters(), self.gradient_clipping
624
+ )
625
+ running_mean_logs["policy_gradient_norm"] += grad_norm.item()
626
+
627
+ # Take step
628
+ self.policy_optimizer.step()
629
+ self.policy_optimizer.zero_grad()
630
+
631
+ # Store logs
632
+ for key, value in running_mean_logs.items():
633
+ self.tally.add_metric(path=key, metric=value)
634
+
635
+ # Clear accelerator state so we do not accumulate references between optimizer steps.
636
+ self.accelerator.clear(self.policy, self.policy_optimizer)
637
+ import gc
638
+
639
+ gc.collect()
640
+ torch.cuda.empty_cache()
641
+ return running_mean_logs
642
+
643
+ def get_advantages_with_critic_gradient_accumulation(
644
+ self, trajectories: TrajectoryBatch, critic_loss_scaling_factor: float = 2.0
645
+ ) -> torch.FloatTensor:
646
+ """
647
+ Compute (and optionally whiten) advantages while training the critic in mini-batches.
648
+ Uses GAE if enabled, otherwise uses Monte Carlo returns.
649
+ Optionally trains the critic if GAE is used.
650
+ Returns:
651
+ advantages: NestedFloatTensors
652
+ """
653
+
654
+ mb_size = self.mini_batch_size
655
+ batch_size = trajectories.rollout_ids.shape[0]
656
+ agent_id = trajectories.agent_ids[0]
657
+ batch_rewards = trajectories.batch_rewards
658
+
659
+ ######################################
660
+ # use critic for advantage estimation
661
+ ######################################
662
+ if self.use_gae:
663
+ if "buffer" in agent_id:
664
+ self.critic.eval()
665
+ training = False
666
+ else:
667
+ self.critic.train()
668
+ training = True
669
+ advantages = []
670
+ # critic_loss_scaling_factor comes learning single critic for two agents
671
+ normalization_factor = (
672
+ np.ceil(batch_size / mb_size).astype(int) * critic_loss_scaling_factor
673
+ )
674
+ # For each minibatch
675
+ for mb in range(0, batch_size, mb_size):
676
+ trajectory_mb = trajectories[mb : mb + mb_size]
677
+ trajectory_mb.to(self.device)
678
+ rewards_mb = trajectory_mb.batch_rewards
679
+ (
680
+ tokens_mb,
681
+ state_ends_mask_mb,
682
+ timestep_counts,
683
+ ) = trajectory_mb.get_padded_tensors_for_critic()
684
+ # critic causal attention up to end flags
685
+ if training:
686
+ vals_estimate_full = self.critic(tokens_mb)
687
+ else:
688
+ with torch.no_grad():
689
+ vals_estimate_full = self.critic(tokens_mb)
690
+
691
+ # if vals_estimate_full.dim() == 3:
692
+ # vals_estimate_full = vals_estimate_full.squeeze(-1)
693
+
694
+ # Select only positions where states end, per sample → list of (jT,)
695
+ B = tokens_mb.shape[0]
696
+ vals_list = [
697
+ vals_estimate_full[b][state_ends_mask_mb[b]] for b in range(B)
698
+ ]
699
+
700
+ # Pad to (B, max_jT) = (B, S)
701
+ vals_estimate_mb = pad_sequence(
702
+ vals_list, batch_first=True, padding_value=0.0
703
+ )
704
+ dtype = vals_estimate_mb.dtype
705
+ rewards_mb = pad_sequence(
706
+ rewards_mb, batch_first=True, padding_value=0.0
707
+ ).to(
708
+ dtype=dtype
709
+ ) # (B, S)
710
+ self.rollout_tally.add_metric(
711
+ path=["batch_rewards"],
712
+ rollout_tally_item=RolloutTallyItem(
713
+ crn_ids=trajectory_mb.crn_ids,
714
+ rollout_ids=trajectory_mb.rollout_ids,
715
+ agent_ids=trajectory_mb.agent_ids,
716
+ metric_matrix=rewards_mb,
717
+ ),
718
+ )
719
+ if self.reward_normalizing_constant != 1.0:
720
+ rewards_mb /= self.reward_normalizing_constant
721
+
722
+ det_vals_estimate_mb = vals_estimate_mb.detach() # (B, max_jT)
723
+ self.rollout_tally.add_metric(
724
+ path=["mb_value_estimates_critic"],
725
+ rollout_tally_item=RolloutTallyItem(
726
+ crn_ids=trajectory_mb.crn_ids,
727
+ rollout_ids=trajectory_mb.rollout_ids,
728
+ agent_ids=trajectory_mb.agent_ids,
729
+ metric_matrix=det_vals_estimate_mb,
730
+ ),
731
+ )
732
+
733
+ # Append a 0 value to the end of the value estimates
734
+ if det_vals_estimate_mb.shape[1] == rewards_mb.shape[1]:
735
+ Bsize = det_vals_estimate_mb.shape[0]
736
+ device = det_vals_estimate_mb.device
737
+ dtype = det_vals_estimate_mb.dtype
738
+ det_vals_estimate_mb = torch.cat(
739
+ [
740
+ det_vals_estimate_mb,
741
+ torch.zeros((Bsize, 1), device=device, dtype=dtype),
742
+ ],
743
+ dim=1,
744
+ ) # (B, max_jT+1)
745
+ else:
746
+ raise ValueError(
747
+ "Incompatible shapes for value estimates and rewards."
748
+ )
749
+
750
+ # Get annealed lambda
751
+ if self.use_gae_lambda_annealing:
752
+ annealing_constant = self.gae_lambda_annealing_method(
753
+ step=self.trainer_annealing_state.annealing_step_counter
754
+ )
755
+ annealed_lambda = (
756
+ self.gae_lambda_annealing_limit * annealing_constant
757
+ )
758
+ self.tally.add_metric(
759
+ path="annealed_lambda", metric=annealed_lambda
760
+ )
761
+ else:
762
+ annealed_lambda = self.gae_lambda_annealing_limit
763
+
764
+ # Get GAE advantages
765
+ gae_advantages = get_generalized_advantage_estimates(
766
+ rewards=rewards_mb,
767
+ value_estimates=det_vals_estimate_mb,
768
+ discount_factor=self.discount_factor,
769
+ lambda_coef=annealed_lambda,
770
+ ) # (B, max_jT)
771
+ self.rollout_tally.add_metric(
772
+ path=["mb_gae_advantages"],
773
+ rollout_tally_item=RolloutTallyItem(
774
+ crn_ids=trajectory_mb.crn_ids,
775
+ rollout_ids=trajectory_mb.rollout_ids,
776
+ agent_ids=trajectory_mb.agent_ids,
777
+ metric_matrix=gae_advantages,
778
+ ),
779
+ )
780
+ if training:
781
+ targets = (
782
+ gae_advantages.to(dtype=dtype) + det_vals_estimate_mb[:, :-1]
783
+ ) # (B, max_jT) # A(s, a, b) + V(s) = Q(s, a, b)
784
+ self.rollout_tally.add_metric(
785
+ path=["mb_targets_critic"],
786
+ rollout_tally_item=RolloutTallyItem(
787
+ crn_ids=trajectory_mb.crn_ids,
788
+ rollout_ids=trajectory_mb.rollout_ids,
789
+ agent_ids=trajectory_mb.agent_ids,
790
+ metric_matrix=targets,
791
+ ),
792
+ )
793
+ if self.critic_loss_type == "mse":
794
+ loss = F.mse_loss(
795
+ input=vals_estimate_mb,
796
+ target=targets,
797
+ )
798
+ elif self.critic_loss_type == "huber":
799
+ loss = F.huber_loss(
800
+ input=vals_estimate_mb,
801
+ target=targets,
802
+ )
803
+ self.tally.add_metric(path=["mb_critic_loss"], metric=loss.item())
804
+ # Accumulate gradient
805
+ loss /= normalization_factor
806
+ self.accelerator.backward(loss)
807
+ del loss
808
+ del targets
809
+ del vals_estimate_mb
810
+ del trajectory_mb
811
+ del vals_estimate_full
812
+
813
+ # Get jagged back using timestep_counts
814
+ advantages.extend(
815
+ [gae_advantages[i, : timestep_counts[i]] for i in range(B)]
816
+ )
817
+
818
+ ######################################
819
+ # use exclusively Monte Carlo returns & rloo for advantage estimation
820
+ ######################################
821
+ else:
822
+ lengths = [len(c) for c in batch_rewards]
823
+ padded_rewards = pad_sequence(
824
+ batch_rewards, batch_first=True, padding_value=0.0
825
+ )
826
+ self.rollout_tally.add_metric(
827
+ path=["mb_rewards"],
828
+ rollout_tally_item=RolloutTallyItem(
829
+ crn_ids=trajectories.crn_ids,
830
+ rollout_ids=trajectories.rollout_ids,
831
+ agent_ids=trajectories.agent_ids,
832
+ metric_matrix=padded_rewards,
833
+ ),
834
+ )
835
+ if self.reward_normalizing_constant != 1.0:
836
+ padded_rewards /= self.reward_normalizing_constant
837
+ padded_advantages = get_discounted_returns(
838
+ rewards=padded_rewards,
839
+ discount_factor=self.discount_factor,
840
+ ) # no baseline for now
841
+ if self.use_rloo:
842
+ is_grouped_by_rng = (
843
+ trajectories.crn_ids.unique().shape[0]
844
+ != trajectories.crn_ids.shape[0]
845
+ )
846
+ if is_grouped_by_rng and not self.no_rloo_grouping:
847
+ for crn_id in trajectories.crn_ids.unique():
848
+ rng_mask = trajectories.crn_ids == crn_id
849
+ rng_advantages = padded_advantages[rng_mask]
850
+ rng_advantages, _ = get_rloo_credits(credits=rng_advantages)
851
+ padded_advantages[rng_mask] = rng_advantages
852
+ else:
853
+ padded_advantages, _ = get_rloo_credits(credits=padded_advantages)
854
+ self.rollout_tally.add_metric(
855
+ path=["mb_rloo_advantages"],
856
+ rollout_tally_item=RolloutTallyItem(
857
+ crn_ids=trajectories.crn_ids,
858
+ rollout_ids=trajectories.rollout_ids,
859
+ agent_ids=trajectories.agent_ids,
860
+ metric_matrix=padded_advantages,
861
+ ),
862
+ )
863
+ advantages = [
864
+ padded_advantages[i, : lengths[i]]
865
+ for i in range(padded_advantages.shape[0])
866
+ ]
867
+
868
+ if self.whiten_advantages_time_step_wise or self.whiten_advantages:
869
+ lengths = [len(c) for c in advantages]
870
+ padded_advantages = pad_sequence(
871
+ advantages, batch_first=True, padding_value=0.0
872
+ )
873
+ if self.whiten_advantages_time_step_wise:
874
+ whitened_padded_advantages = whiten_advantages_time_step_wise(
875
+ padded_advantages
876
+ )
877
+ path = ["mb_whitened_advantages_time_step_wise"]
878
+ elif self.whiten_advantages:
879
+ whitened_padded_advantages = whiten_advantages(padded_advantages)
880
+ path = ["mb_whitened_advantages"]
881
+ self.rollout_tally.add_metric(
882
+ path=path,
883
+ rollout_tally_item=RolloutTallyItem(
884
+ crn_ids=trajectories.crn_ids,
885
+ rollout_ids=trajectories.rollout_ids,
886
+ agent_ids=trajectories.agent_ids,
887
+ metric_matrix=whitened_padded_advantages,
888
+ ),
889
+ )
890
+ advantages = [
891
+ whitened_padded_advantages[i, : lengths[i]]
892
+ for i in range(whitened_padded_advantages.shape[0])
893
+ ]
894
+
895
+ self.trainer_annealing_state.annealing_step_counter += 1
896
+
897
+ return advantages
898
+
899
+ @abstractmethod
900
+ def set_agent_trajectory_data(
901
+ self, agent_id: str, roots: list[RolloutTreeRootNode]
902
+ ) -> None:
903
+ """
904
+ Populate self.training_data for a single agent using the provided rollout trees.
905
+ """
906
+ pass
907
+
908
+ def set_trajectory_data(
909
+ self, roots: list[RolloutTreeRootNode], agent_ids: list[str]
910
+ ) -> None:
911
+ """
912
+ Convenience wrapper to ingest trajectory data for every training agent.
913
+ """
914
+ for agent_id in agent_ids:
915
+ self.set_agent_trajectory_data(agent_id, roots)
916
+
917
+ @abstractmethod
918
+ def share_advantage_data(self) -> list[AdvantagePacket]:
919
+ pass
920
+
921
+ @abstractmethod
922
+ def receive_advantage_data(self, advantage_packets: list[AdvantagePacket]) -> None:
923
+ pass
924
+
925
+ def set_policy_gradient_data(self, agent_ids: list[str]) -> None:
926
+ """
927
+ Reset and rebuild the policy-gradient minibatches before iterating through agents.
928
+ """
929
+ self.policy_gradient_data = None
930
+ for agent_id in agent_ids:
931
+ assert "buffer" not in agent_id, "Buffer agents do not train policy"
932
+ trajectory_batch = self.training_data[agent_id]
933
+ tokenwise_batch_credits = get_tokenwise_credits(
934
+ batch_timesteps=trajectory_batch.batch_timesteps,
935
+ batch_credits=trajectory_batch.batch_credits,
936
+ )
937
+ policy_gradient_data = TrainingBatch(
938
+ rollout_ids=trajectory_batch.rollout_ids,
939
+ batch_input_ids=trajectory_batch.batch_input_ids,
940
+ batch_action_mask=trajectory_batch.batch_action_mask,
941
+ batch_entropy_mask=trajectory_batch.batch_entropy_mask,
942
+ batch_credits=tokenwise_batch_credits,
943
+ batch_engine_log_probs=trajectory_batch.batch_engine_log_probs,
944
+ batch_timesteps=trajectory_batch.batch_timesteps,
945
+ )
946
+ if self.policy_gradient_data is None:
947
+ self.policy_gradient_data = policy_gradient_data
948
+ else:
949
+ self.policy_gradient_data.append(policy_gradient_data)
950
+
951
+ self.training_data = {}
952
+ self.tokenwise_tally = ContextualizedTokenwiseTally(
953
+ tokenizer=self.tokenizer,
954
+ paths=self.debug_path_list,
955
+ )
956
+
957
+ def train(self) -> None:
958
+ """
959
+ Entry point for policy updates: prepare batches, compute gradients, and update parameters.
960
+ """
961
+ assert self.policy_gradient_data is not None, "Policy gradient data is not set"
962
+ if self.critic_optimizer is not None:
963
+ if self.gradient_clipping is not None:
964
+ grad_norm = self.accelerator.clip_grad_norm_(
965
+ self.critic.parameters(), self.gradient_clipping
966
+ )
967
+ self.tally.add_metric(
968
+ path="gradient_norm_critic", metric=grad_norm.item()
969
+ )
970
+ # Take step
971
+ self.critic_optimizer.step()
972
+ self.critic_optimizer.zero_grad()
973
+ self.accelerator.clear(self.critic, self.critic_optimizer)
974
+ import gc
975
+
976
+ gc.collect()
977
+ torch.cuda.empty_cache()
978
+ running_mean_logs = self.apply_reinforce_step(
979
+ training_batch=self.policy_gradient_data
980
+ )
981
+ return running_mean_logs
982
+
983
+ def export_training_tally(self, identifier: str, folder: str) -> None:
984
+ """
985
+ Saves and resets the collected training metrics using the tally object.
986
+ """
987
+ os.makedirs(folder, exist_ok=True)
988
+ self.tally.save(identifier=identifier, folder=folder)
989
+ self.tokenwise_tally.save(
990
+ path=os.path.join(folder, f"{identifier}_tokenwise.csv")
991
+ )
992
+ self.rollout_tally.save(identifier=identifier, folder=folder)
993
+ self.tally.reset()
994
+ self.tokenwise_tally = None
995
+ self.rollout_tally.reset()
996
+ self.debug_path_list = []
997
+
998
+ def export_optimizer_states(self) -> None:
999
+ """
1000
+ Saves the optimizer states for both the main model and critic (if it exists).
1001
+ """
1002
+ try:
1003
+ os.makedirs(self.save_path, exist_ok=True)
1004
+
1005
+ torch.save(self.policy_optimizer.state_dict(), self.policy_optimizer_path)
1006
+ logger.info(f"Saved main optimizer state to {self.policy_optimizer_path}")
1007
+
1008
+ if self.critic_optimizer is not None:
1009
+ torch.save(
1010
+ self.critic_optimizer.state_dict(), self.critic_optimizer_path
1011
+ )
1012
+ logger.info(
1013
+ f"Saved critic optimizer state to {self.critic_optimizer_path}"
1014
+ )
1015
+ except Exception as e:
1016
+ logger.error(f"Error saving optimizer states: {str(e)}")
1017
+ raise
1018
+
1019
+ def export_trainer_annealing_state(self) -> None:
1020
+ """
1021
+ Saves the trainer state.
1022
+ """
1023
+ with open(self.trainer_annealing_state_path, "wb") as f:
1024
+ pickle.dump(self.trainer_annealing_state, f)
1025
+ logger.info(f"Saved trainer state to {self.trainer_annealing_state_path}")
1026
+
1027
+ def export_trainer_states(self) -> None:
1028
+ """
1029
+ Saves the trainer states.
1030
+ """
1031
+ self.export_optimizer_states()
1032
+ self.export_trainer_annealing_state()
src_code_for_reproducibility/utils/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (257 Bytes). View file
 
src_code_for_reproducibility/utils/__pycache__/dict_get_path.cpython-312.pyc ADDED
Binary file (669 Bytes). View file
 
src_code_for_reproducibility/utils/__pycache__/gather_training_stats.cpython-312.pyc ADDED
Binary file (12 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/get_coagent_id.cpython-312.pyc ADDED
Binary file (560 Bytes). View file
 
src_code_for_reproducibility/utils/__pycache__/resource_context.cpython-312.pyc ADDED
Binary file (4.68 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/rollout_tree_chat_htmls.cpython-312.pyc ADDED
Binary file (60.2 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/rollout_tree_gather_utils.cpython-312.pyc ADDED
Binary file (12.7 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/rollout_tree_stats.cpython-312.pyc ADDED
Binary file (2.38 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/short_id_gen.cpython-312.pyc ADDED
Binary file (722 Bytes). View file
 
src_code_for_reproducibility/utils/__pycache__/stat_pack.cpython-312.pyc ADDED
Binary file (7.76 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/update_start_epoch.cpython-312.pyc ADDED
Binary file (1.01 kB). View file
 
src_code_for_reproducibility/utils/__pycache__/wandb_utils.cpython-312.pyc ADDED
Binary file (6.66 kB). View file