forked from rasbt/reasoning-from-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch08.py
More file actions
362 lines (287 loc) · 10.1 KB
/
Copy pathch08.py
File metadata and controls
362 lines (287 loc) · 10.1 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
# 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
import csv
import json
import random
import time
from pathlib import Path
import matplotlib.pyplot as plt
import requests
import torch
from .ch03 import render_prompt
from .qwen3 import (
Qwen3Tokenizer,
download_qwen3_small,
)
def load_distill_data(
local_path=None,
partition="deepseek-r1-math-train",
save_copy=True,
):
if local_path is None:
local_path = f"{partition}.json"
local_path = Path(local_path)
url = (
"https://huggingface.co/datasets/rasbt/math_distill"
"/resolve/main/data/"
f"{partition}.json"
)
backup_url = (
"https://f001.backblazeb2.com/file/reasoning-from-scratch/"
f"MATH/{partition}.json"
)
if local_path.exists():
with local_path.open("r", encoding="utf-8") as f:
data = json.load(f)
size_kb = local_path.stat().st_size / 1e3
print(f"{local_path}: {size_kb:.1f} KB (cached)")
return data
assert partition in (
"deepseek-r1-math-train",
"deepseek-r1-math500",
"qwen3-235b-a22b-math-train",
"qwen3-235b-a22b-math500",
)
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)
size_kb = local_path.stat().st_size / 1e3
print(f"{local_path}: {size_kb:.1f} KB")
return data
def format_distilled_answer(entry):
content = str(entry["message_content"]).strip()
if not content:
raise ValueError("Missing non-empty 'message_content' field.")
thinking = str(entry["message_thinking"]).strip()
return f"<think>{thinking}</think>\n\n{content}"
def load_reasoning_tokenizer(local_dir="qwen3"):
download_qwen3_small(
kind="reasoning", tokenizer_only=True, out_dir=local_dir
)
tokenizer_path = Path(local_dir) / "tokenizer-reasoning.json"
tokenizer = Qwen3Tokenizer(
tokenizer_file_path=tokenizer_path,
apply_chat_template=True,
add_generation_prompt=True,
add_thinking=True,
)
return tokenizer
def build_examples(data, tokenizer):
examples = []
skipped = 0
for entry in data:
try:
# Step 1: encode prompt
prompt = render_prompt(entry["problem"])
prompt_ids = tokenizer.encode(prompt)
# Step 2: encode answer
target_answer = format_distilled_answer(entry)
answer_ids = tokenizer.encode(
target_answer, chat_wrapped=False
)
# Step 3: Combine prompt and answer
token_ids = (
prompt_ids + answer_ids + [tokenizer.eos_token_id]
)
if len(token_ids) < 2:
skipped += 1
continue
examples.append({
"token_ids": token_ids,
"prompt_len": len(prompt_ids),
})
except (KeyError, TypeError, ValueError):
skipped += 1
return examples, skipped
def compute_length(examples, answer_only=False):
lengths = []
for ex in examples:
total = len(ex["token_ids"])
length = total - ex["prompt_len"] if answer_only else total
lengths.append(length)
avg_len = round(sum(lengths) / len(lengths))
shortest_len = min(lengths)
longest_len = max(lengths)
shortest_idx = lengths.index(shortest_len)
longest_idx = lengths.index(longest_len)
print(f"Average: {avg_len} tokens")
print(f"Shortest: {shortest_len} tokens (index {shortest_idx})")
print(f"Longest: {longest_len} tokens (index {longest_idx})")
def filter_examples_by_max_len(examples, max_len=2048):
filtered_examples = [
s for s in examples
if len(s["token_ids"]) <= max_len
]
print("Original:", len(examples))
print("Filtered:", len(filtered_examples))
print("Removed:", len(examples) - len(filtered_examples))
return filtered_examples
def compute_example_loss(model, example, device):
token_ids = example["token_ids"]
prompt_len = example["prompt_len"]
input_ids = torch.tensor(
token_ids[:-1], dtype=torch.long, device=device
).unsqueeze(0)
target_ids = torch.tensor(
token_ids[1:], dtype=torch.long, device=device
)
logits = model(input_ids).squeeze(0)
answer_start = max(prompt_len - 1, 0)
answer_logits = logits[answer_start:]
answer_targets = target_ids[answer_start:]
loss = torch.nn.functional.cross_entropy(
answer_logits, answer_targets
)
return loss
@torch.no_grad()
def evaluate_examples(model, examples, device):
was_training = model.training
model.eval()
total_loss = 0.0
num_examples = 0
for example in examples:
loss = compute_example_loss(model, example, device)
total_loss += loss.item()
num_examples += 1
if was_training:
model.train()
return total_loss / num_examples
def train_distillation(
model,
train_examples,
val_examples,
device,
epochs=2,
lr=5e-6,
grad_clip_norm=None,
seed=123,
log_every=50,
checkpoint_dir="checkpoints",
csv_log_path=None,
):
# Step 1: initialize optimizer (model is already loaded)
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
model.train()
total_steps = epochs * len(train_examples)
global_step = 0
rng = random.Random(seed)
if csv_log_path is None:
timestamp = time.strftime("%Y%m%d_%H%M%S")
csv_log_path = f"train_distill_metrics_{timestamp}.csv"
csv_log_path = Path(csv_log_path)
# Step 2: iterate over training epochs
for epoch in range(1, epochs + 1):
# Step 3: shuffle the training examples at the start of the epoch
epoch_examples = list(train_examples)
rng.shuffle(epoch_examples)
# Step 4: iterate over training examples in epoch
for example in epoch_examples:
global_step += 1
# Stage 5: reset loss gradient
# (it's best practice to do this at the beginning of each step)
optimizer.zero_grad()
# Step 6: compute the cross-entropy loss for the current example
loss = compute_example_loss(model, example, device)
# Step 7: backpropagate gradients
loss.backward()
# Optionally clip large gradients to improve training stability
if grad_clip_norm is not None:
torch.nn.utils.clip_grad_norm_(
model.parameters(), grad_clip_norm
)
# Step 8: update the model weights
optimizer.step()
# Step 9: periodically evaluate the current model on the validation set
if log_every and global_step % log_every == 0:
val_loss = evaluate_examples(
model=model,
examples=val_examples,
device=device,
)
model.train()
print(
f"[Epoch {epoch}/{epochs} "
f"Step {global_step}/{total_steps}] "
f"train_loss={loss.item():.4f} "
f"val_loss={val_loss:.4f}"
)
append_csv_metrics(
csv_log_path=csv_log_path,
epoch_idx=epoch,
total_steps=global_step,
train_loss=loss.item(),
val_loss=val_loss,
)
# Step 10: save a checkpoint for this epoch
ckpt_path = save_checkpoint(
model=model,
checkpoint_dir=checkpoint_dir,
step=global_step,
suffix=f"epoch{epoch}",
)
print(f"Saved checkpoint to {ckpt_path}")
return model
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-distill-step{step:05d}{suffix}.pth"
)
torch.save(model.state_dict(), ckpt_path)
return ckpt_path
def append_csv_metrics(
csv_log_path,
epoch_idx,
total_steps,
train_loss,
val_loss,
):
if not csv_log_path.exists():
csv_log_path.write_text(
"epoch,total_steps,train_loss,val_loss\n",
encoding="utf-8",
)
with csv_log_path.open("a", encoding="utf-8") as f:
f.write(
f"{epoch_idx},{total_steps},{train_loss:.6f},"
f"{val_loss:.6f}\n"
)
def plot_distill_metrics(csv_path="train_distill_metrics.csv"):
total_steps, train_losses, val_losses, epoch_bounds = [], [], [], {}
with open(csv_path, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
step = int(row["total_steps"])
epoch = int(row["epoch"])
total_steps.append(step)
train_losses.append(float(row["train_loss"]))
val_losses.append(float(row["val_loss"]))
epoch_bounds.setdefault(epoch, [step, step])[1] = step
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(total_steps, train_losses, label="train_loss", alpha=0.3)
ax.plot(total_steps, val_losses, label="val_loss")
ax.set_xlabel("Total Steps")
ax.set_ylabel("Loss")
ax.legend()
# Epoch axis
epoch_axis = ax.secondary_xaxis("bottom")
epoch_axis.spines["bottom"].set_position(("outward", 45))
epochs = sorted(epoch_bounds)
epoch_axis.set_xticks(
[(epoch_bounds[epoch][0] + epoch_bounds[epoch][1]) / 2
for epoch in epochs]
)
epoch_axis.set_xticklabels(epochs)
epoch_axis.set_xlabel("Epoch")
plt.tight_layout()
plt.show()