-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
422 lines (354 loc) · 13.2 KB
/
Copy pathtrain.py
File metadata and controls
422 lines (354 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
from collections.abc import Callable
import json
from pathlib import Path
import random
import re
from typing import Any, Iterator, Optional, Literal
import wandb
import torch
import torch.optim as optim
import torch.nn.functional as F
from torch.nn.utils import clip_grad_norm_
from torch.utils.data import DataLoader
from transformers import (
AutoTokenizer,
PreTrainedTokenizer,
AutoModelForCausalLM,
GenerationConfig,
)
from loss import approx_kl_divergence, GRPOLoss
from reward import default_reward, rule_based_reward, soft_overlong_punishment
from replay_buffer import ReplayBuffer, Experience, join_experience_batch
def load_model(
model_name_or_path: str,
trust_remote_code: bool = False,
bf16: bool = True,
device_map=None,
) -> tuple[AutoModelForCausalLM, PreTrainedTokenizer]:
"""load model with flash attention"""
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
tokenizer.pad_token = tokenizer.eos_token # ending sentence token
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
trust_remote_code=trust_remote_code,
attn_implementation="flash_attention_2",
torch_dtype=torch.bfloat16 if bf16 else "auto",
device_map=device_map,
)
return model, tokenizer
# DeepSeek Zero system prompt
system_prompt = """A conversation between User and Assistant. The user asks a question, and the Assistant solves it.
The assistant first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively, i.e., <think> reasoning process here </think>
<answer> answer here </answer>
"""
@torch.no_grad()
def rollout(
model: AutoModelForCausalLM,
tokenizer: PreTrainedTokenizer,
task: str,
oracle_answer: str, # reference or ground truth for eval
num_rollouts: int,
max_resp_len: int = 1024,
overlong_buffer_len: int = 256,
temperature: float = 1.0,
# nucleus sampling (top-p sampling)
# = sampling only most probable tokens whose cumulative probability adds up to up to top_p
top_p: float = 1.0,
strategy: Literal["grpo", "dr.grpo", "dapo"] = "grpo",
) -> tuple[torch.Tensor, torch.Tensor, list[str]]:
"""
Generate a number of rollout sequences from the model given a prompt and task
Return:
[log_probs, rewards, outputs]
"""
model.eval()
# 1. format prompt
chat_messages = [
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": task,
},
]
chat_prompt = tokenizer.apply_chat_template(
chat_messages, tokenize=False, add_generation_prompt=True
)
model_inputs = tokenizer(
[chat_prompt],
return_tensors="pt",
padding=True,
padding_side="left",
return_attention_mask=True,
).to("cuda")
# duplicate prompt num_rollouts times
model_inputs["attention_mask"] = model_inputs["attention_mask"].repeat(
num_rollouts, 1
)
input_ids = model_inputs["input_ids"].repeat(num_rollouts, 1)
model_inputs["input_ids"] = input_ids
# 2. sample completions
pad_token_id = tokenizer.eos_token_id
generation_config = GenerationConfig(
do_sample=True,
top_p=top_p,
temperature=temperature,
max_length=max_resp_len,
pad_token_id=pad_token_id,
)
sequence_ids = model.generate(**model_inputs, generation_config=generation_config)
completions = tokenizer.batch_decode(
sequence_ids[:, input_ids.shape[1] :], skip_special_tokens=True
)
action_mask = torch.zeros_like(sequence_ids, dtype=torch.bool)
action_mask[:, input_ids.shape[1] :] = True
action_mask[sequence_ids == pad_token_id] = False
action_mask = action_mask[:, 1:]
# 3. determine rewards
returns = torch.zeros(num_rollouts, 1, dtype=torch.float)
for i, completion in enumerate(completions):
# search answer tag
answer_match = re.search(
r"<answer>(.*?)</answer>",
completion,
flags=re.DOTALL,
)
answer = answer_match.group(1) if answer_match else None
# reward modeling goes here
reward = 0
if strategy == "dapo":
reward = rule_based_reward(answer, oracle_answer)
overlong_reward = soft_overlong_punishment(answer, max_resp_len, overlong_buffer_len)
reward += overlong_reward
else:
reward = default_reward(answer, oracle_answer)
returns[i] = reward
return sequence_ids, returns.to(sequence_ids.device), action_mask, completions
def init_rng(seed: int) -> torch.Generator:
random.seed(seed)
return torch.manual_seed(seed)
def group_advantages(
returns: torch.Tensor,
eps: float = 1e-8,
strategy: Literal["grpo", "dr.grpo", "dapo"] = "grpo",
f_norm: float = 1.0,
num_samples: int = 16,
n_valid_samples: int = 0,
num_generations: int = 32,
) -> torch.Tensor:
"""support different advantages grouping"""
assert strategy in ("grpo", "dr.grpo", "dapo")
if strategy == "grpo":
return (returns - returns.mean()) / (returns.std() + eps)
if strategy == "dr.grpo":
# Dr.GRPO modification 2: remove difficulty bias by just computing the MC advantage without dividing by std
return returns - returns.mean()
if strategy == "dapo":
mean_grouped_rewards = returns.mean()
std_grouped_rewards = returns.mean()
g_mean_grouped_rewards = mean_grouped_rewards
g_std_grouped_rewards = std_grouped_rewards
advantages = returns - mean_grouped_rewards
# inverse alpha and f_norm
inverse_alpha = n_valid_samples / num_samples
inverse_alpha = min(1.0, inverse_alpha)
return
# default: use grpo version
return (returns - returns.mean()) / (returns.std() + eps)
def sequence_log_probs_from_logits(
logits: torch.tensor, output_ids: torch.tensor
) -> torch.Tensor:
log_prob = F.log_softmax(logits, dim=-1)
return log_prob.gather(dim=-1, index=output_ids.unsqueeze(-1)).squeeze(-1)
def sequences_log_probs(
model: AutoModelForCausalLM,
sequence_ids: torch.Tensor,
attention_mask: torch.Tensor,
) -> torch.Tensor:
position_ids = attention_mask.long().cumsum(dim=-1) - 1
position_ids.masked_fill_(mask=(attention_mask == 0), value=1)
output = model.forward(
input_ids=sequence_ids,
attention_mask=attention_mask,
position_ids=position_ids,
use_cache=False,
)
logits = output["logits"]
log_probs = sequence_log_probs_from_logits(
logits=logits[:, :-1].to(torch.float32),
output_ids=sequence_ids[:, 1:],
)
return log_probs
def read_jsonl(file_name: str | Path) -> Iterator:
file_path = Path(file_name)
with file_path.open(mode="r", encoding="utf-8") as f:
for line in f:
yield json.loads(line)
def read_prompts(
file_name: str,
predicate: Optional[Callable[[Any], bool]] = None,
max_rows: Optional[int] = None,
) -> list:
rows = []
for x in read_jsonl(file_name):
if predicate is None or predicate(x):
rows.append(x)
if max_rows is not None and len(rows) >= max_rows:
break
return rows
def main():
seed = 42
wandb_project = "tiny_grpo"
device_index = 0
model_name = "meta-llama/Llama-3.2-1B-Instruct"
checkpoint_path = Path("./output")
checkpoint_interval = 20
# grpo
policy_ops = "gpg" # policy optimization strategy
max_resp_len = 1024 # max response length (aka L_max in DAPO)
overlong_buffer_len = 256 # overlong buffer length (aka L_cache in DAPO)
train_batch_size = 16
lr = 5e-6
kl_weight = 0.01
clip_eps_low = 0.2
clip_eps_high = 0.28 if policy_ops == "dapo" else 0.2 # DAPO recommends clip_eps_high = 0.28
group_size = 12
rollouts_per_step = 32
epochs_per_step = 1
max_norm = 1.0 # gradient clipping
# rollout params
top_p = 1.0
temperature = 1.0
device = torch.device("cuda", device_index)
cpu_device = torch.device("cpu")
init_rng(seed)
reference_model, _ = load_model(model_name, device_map=device)
model, tokenizer = load_model(model_name, device_map=device)
optimizer = optim.Adam(model.parameters(), lr=lr)
reference_model.eval()
model.gradient_checkpointing_enable(
gradient_checkpointing_kwargs={"use_reentrant": False}
)
pad_token_id = tokenizer.eos_token_id
prompts = read_prompts(
"data/math_tasks.jsonl",
predicate=lambda x: len(x["question"]) < 128
and x["num_terms"] <= 3
and x["num_digits"] <= 3,
max_rows=64 * 1024,
)
print(f"found {len(prompts)} matching prompts")
prompt_loader = DataLoader(
prompts,
batch_size=rollouts_per_step,
shuffle=True,
drop_last=True,
pin_memory=False,
)
replay_buffer = ReplayBuffer()
objective = GRPOLoss(
clip_eps_low=clip_eps_low,
clip_eps_high=clip_eps_high,
kl_weight=kl_weight,
policy_ops=policy_ops,
max_resp_len=max_resp_len,
)
if wandb_project is None:
wandb.init(mode="disabled")
else:
wandb.init(project=wandb_project)
for k, prompt_batch in enumerate(prompt_loader):
rollout_returns = []
replay_buffer.clear()
questions = prompt_batch["question"]
answers = prompt_batch["answer"]
with torch.no_grad():
for q, a in zip(questions, answers):
sequence_ids, returns, action_mask, completions = rollout(
model,
tokenizer,
q,
a,
num_rollouts=group_size,
max_resp_len=max_resp_len,
overlong_buffer_len=overlong_buffer_len,
temperature=temperature,
top_p=top_p,
strategy=policy_ops,
)
print(
f"rollout q='{q}', a='{a}', returns={returns.sum().item():.2f}, replay_buffer_size={len(replay_buffer)}, sequence_ids={sequence_ids.shape}"
)
rollout_returns.append(returns.cpu())
advantages = group_advantages(returns, strategy=policy_ops)
attention_mask = sequence_ids != pad_token_id
log_probs = sequences_log_probs(
model=model,
sequence_ids=sequence_ids,
attention_mask=attention_mask,
)
log_probs_ref = sequences_log_probs(
model=reference_model,
sequence_ids=sequence_ids,
attention_mask=attention_mask,
)
kl = approx_kl_divergence(
log_probs=log_probs,
log_probs_ref=log_probs_ref,
action_mask=action_mask,
)
experience = Experience(
sequences=sequence_ids,
action_log_probs=log_probs,
log_probs_ref=log_probs_ref,
returns=returns,
advantages=advantages,
attention_mask=attention_mask,
action_mask=action_mask,
kl=kl,
)
replay_buffer.append(experience.to(cpu_device))
torch.cuda.empty_cache()
episode_return_sum = torch.stack(rollout_returns).sum()
print(f"returns of step {k}: {episode_return_sum:.4f}")
wandb.log({"returns": episode_return_sum})
experience_sampler = DataLoader(
replay_buffer,
batch_size=train_batch_size,
shuffle=True,
drop_last=True,
collate_fn=join_experience_batch,
)
for step_epoch in range(epochs_per_step):
model.train()
for exp in experience_sampler:
exp: Experience
exp = exp.to(device)
optimizer.zero_grad()
log_probs = sequences_log_probs(
model=model,
sequence_ids=exp.sequences,
attention_mask=exp.attention_mask,
)
loss, kl = objective(log_probs=log_probs, experience=exp)
if not loss.isfinite():
print(f"Loss not finite, skipping backward, loss={loss}")
print(f"experience.advantages={experience.advantages}")
continue
loss.backward()
grad_norm = clip_grad_norm_(model.parameters(), max_norm=max_norm)
print(f"{step_epoch}: kl={kl: .4f}, grad_norm={grad_norm: .4f}")
wandb.log({"kl": kl, "grad_norm": grad_norm})
optimizer.step()
if (
checkpoint_path is not None
and checkpoint_interval is not None
and (k + 1) % checkpoint_interval == 0
):
model.save_pretrained(checkpoint_path / f"step_{k}")
if checkpoint_path is not None:
model.save_pretrained(checkpoint_path / f"step_{k}")
if __name__ == "__main__":
main()