forked from rasbt/reasoning-from-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch06.py
More file actions
337 lines (282 loc) · 9.54 KB
/
Copy pathch06.py
File metadata and controls
337 lines (282 loc) · 9.54 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
# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt)
# Source for "Build a Reasoning Model (From Scratch)": https://mng.bz/lZ5B
# Code repository: https://github.com/rasbt/reasoning-from-scratch
from .qwen3 import KVCache
from .ch03 import (
extract_final_candidate, grade_answer, render_prompt
)
from .ch04 import top_p_filter
import json
import time
from pathlib import Path
import requests
import torch
def load_math_train(local_path="math_train.json", save_copy=True):
local_path = Path(local_path)
url = (
"https://raw.githubusercontent.com/rasbt/"
"math_full_minus_math500/refs/heads/main/"
"math_full_minus_math500.json"
)
backup_url = (
"https://f001.backblazeb2.com/file/reasoning-from-scratch/"
"MATH/math_full_minus_math500.json"
)
if local_path.exists():
with local_path.open("r", encoding="utf-8") as f:
data = json.load(f)
else:
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
except requests.RequestException:
print("Using backup URL.")
r = requests.get(backup_url, timeout=30)
r.raise_for_status()
data = r.json()
if save_copy:
with local_path.open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
return data
@torch.no_grad()
def sample_response(
model,
tokenizer,
prompt,
device,
max_new_tokens=512,
temperature=0.8,
top_p=0.9,
):
input_ids = torch.tensor(
tokenizer.encode(prompt),
device=device
)
cache = KVCache(n_layers=model.cfg["n_layers"])
model.reset_kv_cache()
logits = model(input_ids.unsqueeze(0), cache=cache)[:, -1]
generated = []
for _ in range(max_new_tokens):
if temperature and temperature != 1.0:
logits = logits / temperature
probas = torch.softmax(logits, dim=-1)
probas = top_p_filter(probas, top_p)
next_token = torch.multinomial(
probas.cpu(), num_samples=1
).to(device)
token_id = next_token.item()
generated.append(token_id)
if (
tokenizer.eos_token_id is not None
and token_id == tokenizer.eos_token_id
):
break
logits = model(next_token, cache=cache)[:, -1]
full_token_ids = torch.cat(
[input_ids,
torch.tensor(generated, device=device, dtype=input_ids.dtype),]
)
return full_token_ids, input_ids.numel(), tokenizer.decode(generated)
def reward_rlvr(answer_text, ground_truth):
extracted = extract_final_candidate(
answer_text, fallback=None # Require \boxed{}
)
if not extracted:
return 0.0
correct = grade_answer(extracted, ground_truth)
return float(correct)
def sequence_logprob(model, token_ids, prompt_len):
logits = model(token_ids.unsqueeze(0)).squeeze(0).float()
logprobs = torch.log_softmax(logits, dim=-1)
selected = logprobs[:-1].gather(
1, token_ids[1:].unsqueeze(-1)
).squeeze(-1)
return torch.sum(selected[prompt_len - 1:])
def compute_grpo_loss(
model,
tokenizer,
example,
device,
num_rollouts=2,
max_new_tokens=256,
temperature=0.8,
top_p=0.9,
):
assert num_rollouts >= 2
roll_logps, roll_rewards, samples = [], [], []
prompt = render_prompt(example["problem"])
was_training = model.training
model.eval()
for _ in range(num_rollouts):
# Stage 1: generate rollouts
token_ids, prompt_len, text = sample_response(
model=model,
tokenizer=tokenizer,
prompt=prompt,
device=device,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
)
# Stage 2: compute rewards
reward = reward_rlvr(text, example["answer"])
# Stage 4: compute logprobs
logp = sequence_logprob(model, token_ids, prompt_len)
roll_logps.append(logp)
roll_rewards.append(reward)
samples.append(
{
"text": text,
"reward": reward,
"gen_len": token_ids.numel() - prompt_len,
}
)
if was_training:
model.train()
# Stage 2: collect all rewards
rewards = torch.tensor(roll_rewards, device=device)
# Stage 3: compute advantages
advantages = (rewards - rewards.mean()) / (rewards.std() + 1e-4)
# Stage 4: collect all logprobs
logps = torch.stack(roll_logps)
# Stage 5: compute policy gradient loss
pg_loss = -(advantages.detach() * logps).mean()
loss = pg_loss # In the next chapter we add a KL term here
return {
"loss": loss.item(),
"pg_loss": pg_loss.item(),
"rewards": roll_rewards,
"advantages": advantages.detach().cpu().tolist(),
"samples": samples,
"loss_tensor": loss,
}
def save_checkpoint(model, checkpoint_dir, step, suffix=""):
checkpoint_dir = Path(checkpoint_dir)
checkpoint_dir.mkdir(parents=True, exist_ok=True)
suffix = f"-{suffix}" if suffix else ""
ckpt_path = (
checkpoint_dir /
f"qwen3-0.6B-rlvr-grpo-step{step:05d}{suffix}.pth"
)
torch.save(model.state_dict(), ckpt_path)
return ckpt_path
def append_csv_metrics(
csv_log_path,
step_idx,
total_steps,
loss,
reward_avg,
avg_response_len,
):
if not csv_log_path.exists():
csv_log_path.write_text(
"step,total_steps,loss,reward_avg,avg_response_len\n",
encoding="utf-8",
)
with csv_log_path.open("a", encoding="utf-8") as f:
f.write(
f"{step_idx},{total_steps},{loss:.6f},{reward_avg:.6f},"
f"{avg_response_len:.6f}\n"
)
def train_rlvr_grpo(
model,
tokenizer,
math_data,
device,
steps=None,
num_rollouts=2,
max_new_tokens=256,
temperature=0.8,
top_p=0.9,
lr=1e-5,
checkpoint_every=50,
checkpoint_dir=".",
csv_log_path=None,
):
if steps is None:
steps = len(math_data)
# Stage 1: initialize optimize
# (the model was already initialized outside the function)
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
model.train()
current_step = 0
if csv_log_path is None:
timestamp = time.strftime("%Y%m%d_%H%M%S")
csv_log_path = f"train_rlvr_grpo_metrics_{timestamp}.csv"
csv_log_path = Path(csv_log_path)
try:
# Stage 2: Iterate over training steps
for step in range(steps):
# Stage 3: Reset loss gradient
# (it's best practice to do this at the beginning of each step)
optimizer.zero_grad()
current_step = step + 1
example = math_data[step % len(math_data)]
# Stage 4: calculate GRPO loss
stats = compute_grpo_loss(
model=model,
tokenizer=tokenizer,
example=example,
device=device,
num_rollouts=num_rollouts,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
)
# Stage 5: Backward pass to calculate loss gradients
stats["loss_tensor"].backward()
# Clip large gradients to improve training stability
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
# Stage 6: Update model weights using loss gradients
optimizer.step()
# Stage 7: Collect rewards, losses, and response lengths
reward_avg = torch.tensor(stats["rewards"]).mean().item()
step_tokens = sum(sample["gen_len"] for sample in stats["samples"])
avg_response_len = (
step_tokens / len(stats["samples"]) if stats["samples"] else 0.0
)
append_csv_metrics(
csv_log_path,
current_step,
steps,
stats["loss"],
reward_avg,
avg_response_len,
)
# Print step metrics
print(
f"[Step {current_step}/{steps}] "
f"loss={stats['loss']:.4f} "
f"reward_avg={reward_avg:.3f} "
f"avg_resp_len={avg_response_len:.1f}"
)
# Sample outputs (every 10 steps) to check if model
# generates coherent text
if current_step % 10 == 0:
print(f"[Step {current_step}] sample outputs")
for i, sample in enumerate(stats["samples"][:3]):
text = sample["text"].replace("\n", "\\n")
print(
f" {i+1}) reward={sample['reward']:.3f} "
f"len={sample['gen_len']}: {text}"
)
print()
# Stage 8: Save model checkpoint
if checkpoint_every and current_step % checkpoint_every == 0:
ckpt_path = save_checkpoint(
model=model,
checkpoint_dir=checkpoint_dir,
step=current_step,
)
print(f"Saved checkpoint to {ckpt_path}")
# Save a model checkpoint if we interrupt the training early
except KeyboardInterrupt:
ckpt_path = save_checkpoint(
model=model,
checkpoint_dir=checkpoint_dir,
step=max(1, current_step),
suffix="interrupt",
)
print(f"\nKeyboardInterrupt. Saved checkpoint to {ckpt_path}")
return model
return model