Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
260 lines (222 loc) · 8.4 KB
/
Copy pathtrain.py
File metadata and controls
260 lines (222 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import asyncio
import logging
import random
from datetime import datetime
from typing import Any
import chz
import numpy as np
import torch
from tinker_cookbook import cli_utils, model_info
import rl_env as dermatology_env
from tinker_cookbook.rl.train import AsyncConfig, Config, main
from tinker_cookbook.rl.types import RLDatasetBuilder
from tinker.types import LossFnType
# Load dotenv
import dotenv
dotenv.load_dotenv()
logger = logging.getLogger(__name__)
def seed_everything(seed: int) -> None:
if seed is None:
return
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
if hasattr(torch, "use_deterministic_algorithms"):
try:
torch.use_deterministic_algorithms(True, warn_only=True)
except TypeError:
torch.use_deterministic_algorithms(True)
if torch.backends.cudnn.is_available():
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
@chz.chz
class CLIConfig:
"""Simple command-line configuration for RL training."""
# Model configuration
model_name: str = "Qwen/Qwen3-VL-30B-A3B-Instruct"
lora_rank: int = 32
renderer_name: str | None = None
load_checkpoint_path: str | None = None
# Environment configuration
env: str = "dermatology"
seed: int = 0 # Random seed for data shuffling
enable_tool_calling: bool = True
enable_explanation_reward: bool = True
enable_multiturn: bool = True # Enable multiturn training, where a simulated user can ask follow-up questions
gamma: float = 0.9 # Discount factor for multiturn rewards
reorder_manifest_path: str | None = None
reorder_mode: str = "easy_first"
reorder_missing_score: float = 0.5
# Training hyperparameters
group_size: int = 4
groups_per_batch: int = 100
learning_rate: float = 5e-5
max_tokens: int = 1024
temperature: float = 1.0
kl_penalty_coef: float = 0.0#0.005
# Number of optimizer steps per training iteration.
# Useful for very large batch sizes.
num_substeps: int = 4
# Number of times to repeat the images in the training set
epochs: int = 2
# Logging configuration
log_path: str | None = None
wandb_project: str | None = "dermatology"
wandb_name: str | None = None
compute_post_kl: bool = False
# Evals
eval_every: int = 60
eval_group_size: int = 4
eval_max_examples: int | None = 256
compute_mcnemar_eval: bool = True
mcnemar_baseline_model_name: str | None = "Qwen/Qwen3-VL-30B-A3B-Instruct"
# Checkpointing
save_every: int = 60
# Service configuration
base_url: str | None = None
behavior_if_log_dir_exists: cli_utils.LogdirBehavior = "ask"
max_steps_off_policy: int | None = None
# Loss function and configuration.
# See https://tinker-docs.thinkingmachines.ai/losses
loss_fn: LossFnType = "ppo"
loss_fn_config: dict[str, Any] | None = None
clip_low_threshold: float = 0.8
clip_high_threshold: float | None = None
def resolve_loss_fn_config(cli_config: CLIConfig) -> dict[str, Any] | None:
"""
Resolve loss_fn_config with sensible defaults for clip-based losses.
Priority:
1) Explicit `loss_fn_config` from CLI.
2) For `ppo`/`cispo`, derive config from clip threshold CLI fields.
"""
if cli_config.loss_fn_config is not None:
return cli_config.loss_fn_config
if cli_config.loss_fn not in ("ppo", "cispo"):
return None
if cli_config.clip_high_threshold is not None:
clip_high = cli_config.clip_high_threshold
else:
# Keep CISPO default behavior, tighten PPO by default.
clip_high = 1.2 if cli_config.loss_fn == "ppo" else 1.35
return {
"clip_low_threshold": cli_config.clip_low_threshold,
"clip_high_threshold": clip_high,
}
def get_dataset_builder(
env: str,
batch_size: int,
model_name: str,
renderer_name: str,
group_size: int,
eval_group_size: int,
eval_max_examples: int | None,
enable_tool_calling: bool,
enable_explanation_reward: bool,
enable_multiturn: bool,
gamma: float,
reorder_manifest_path: str | None,
reorder_mode: str,
reorder_missing_score: float,
seed: int = 0,
epochs: int = 1,
) -> RLDatasetBuilder:
return dermatology_env.DermatologyDatasetBuilder(
batch_size=batch_size,
model_name_for_tokenizer=model_name,
renderer_name=renderer_name,
# n_batches=100,
# include_fewshot=True,
group_size=group_size,
test_group_size=eval_group_size,
test_max_examples=eval_max_examples,
enable_tool_calling=enable_tool_calling,
enable_explanation_reward=enable_explanation_reward,
enable_multiturn=enable_multiturn,
gamma=gamma,
reorder_manifest_path=reorder_manifest_path,
reorder_mode=reorder_mode,
reorder_missing_score=reorder_missing_score,
seed=seed,
num_epochs=epochs,
)
async def cli_main(cli_config: CLIConfig):
"""Convert CLI config to full config and run training."""
seed_everything(cli_config.seed)
# Get tokenizer for stop sequences
renderer_name = cli_config.renderer_name or model_info.get_recommended_renderer_name(
cli_config.model_name
)
model_name = cli_config.model_name.replace("/", "-")
run_name = f"{cli_config.env}-{model_name}-{cli_config.lora_rank}rank-{cli_config.learning_rate}lr-{cli_config.group_size}group-{cli_config.groups_per_batch}batch-{cli_config.loss_fn}-seed{cli_config.seed}-{datetime.now().strftime('%Y-%m-%d-%H-%M')}"
# create log path if it doesn't exist
if cli_config.log_path is not None:
log_path = cli_config.log_path
else:
log_path = f"/tmp/tinker-examples/math_rl/{run_name}"
if cli_config.wandb_name is not None:
wandb_name = cli_config.wandb_name
else:
wandb_name = run_name
resolved_loss_fn_config = resolve_loss_fn_config(cli_config)
logger.info(
"Using loss_fn=%s loss_fn_config=%s",
cli_config.loss_fn,
resolved_loss_fn_config,
)
# Create full config
config = Config(
learning_rate=cli_config.learning_rate,
dataset_builder=get_dataset_builder(
env=cli_config.env,
batch_size=cli_config.groups_per_batch,
model_name=cli_config.model_name,
renderer_name=renderer_name,
group_size=cli_config.group_size,
eval_group_size=cli_config.eval_group_size,
eval_max_examples=cli_config.eval_max_examples,
enable_tool_calling=cli_config.enable_tool_calling,
enable_explanation_reward=cli_config.enable_explanation_reward,
enable_multiturn=cli_config.enable_multiturn,
gamma=cli_config.gamma,
reorder_manifest_path=cli_config.reorder_manifest_path,
reorder_mode=cli_config.reorder_mode,
reorder_missing_score=cli_config.reorder_missing_score,
seed=cli_config.seed,
epochs=cli_config.epochs
),
model_name=cli_config.model_name,
lora_rank=cli_config.lora_rank,
max_tokens=cli_config.max_tokens,
temperature=cli_config.temperature,
wandb_project=cli_config.wandb_project,
wandb_name=wandb_name,
log_path=log_path,
base_url=cli_config.base_url,
load_checkpoint_path=cli_config.load_checkpoint_path,
compute_post_kl=cli_config.compute_post_kl,
kl_penalty_coef=cli_config.kl_penalty_coef,
num_substeps=cli_config.num_substeps,
eval_every=cli_config.eval_every,
save_every=cli_config.save_every,
mcnemar_baseline_model_name=(
cli_config.mcnemar_baseline_model_name or cli_config.model_name
)
if cli_config.compute_mcnemar_eval
else None,
async_config=AsyncConfig(
max_steps_off_policy=cli_config.max_steps_off_policy,
groups_per_batch=cli_config.groups_per_batch,
)
if cli_config.max_steps_off_policy is not None
else None,
loss_fn=cli_config.loss_fn,
loss_fn_config=resolved_loss_fn_config,
)
cli_utils.check_log_dir(log_path, behavior_if_exists=cli_config.behavior_if_log_dir_exists)
# Run training
await main(config)
if __name__ == "__main__":
cli_config = chz.entrypoint(CLIConfig)
asyncio.run(cli_main(cli_config))