forked from rasbt/reasoning-from-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch03.py
More file actions
574 lines (468 loc) · 17 KB
/
Copy pathch03.py
File metadata and controls
574 lines (468 loc) · 17 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# 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 pathlib import Path
import json
import re
import time
import requests
from sympy import simplify
from sympy.parsing import sympy_parser as spp
from sympy.core.sympify import SympifyError
from sympy.polys.polyerrors import PolynomialError
from tokenize import TokenError
import torch
from .qwen3 import (
download_qwen3_small,
Qwen3Tokenizer,
Qwen3Model,
QWEN_CONFIG_06_B
)
from .ch02 import (
generate_text_basic_stream_cache
)
RE_NUMBER = re.compile(
r"-?(?:\d+/\d+|\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)"
)
LATEX_FIXES = [ # Latex formatting to be replaced
(r"\\left\s*", ""),
(r"\\right\s*", ""),
(r"\\,|\\!|\\;|\\:", ""),
(r"\\cdot", "*"),
(r"\u00B7|\u00D7", "*"),
(r"\\\^\\circ", ""),
(r"\\dfrac", r"\\frac"),
(r"\\tfrac", r"\\frac"),
(r"°", ""),
]
RE_SPECIAL = re.compile(r"<\|[^>]+?\|>") # strip chat special tokens like <|assistant|>
SUPERSCRIPT_MAP = {
"⁰": "0", "¹": "1", "²": "2", "³": "3", "⁴": "4",
"⁵": "5", "⁶": "6", "⁷": "7", "⁸": "8", "⁹": "9",
"⁺": "+", "⁻": "-", "⁽": "(", "⁾": ")",
}
def load_model_and_tokenizer(which_model, device, use_compile, local_dir="qwen3"):
if which_model == "base":
download_qwen3_small(
kind="base", tokenizer_only=False, out_dir=local_dir
)
tokenizer_path = Path(local_dir) / "tokenizer-base.json"
model_path = Path(local_dir) / "qwen3-0.6B-base.pth"
tokenizer = Qwen3Tokenizer(tokenizer_file_path=tokenizer_path)
elif which_model == "reasoning":
download_qwen3_small(
kind="reasoning", tokenizer_only=False, out_dir=local_dir
)
tokenizer_path = Path(local_dir) / "tokenizer-reasoning.json"
model_path = Path(local_dir) / "qwen3-0.6B-reasoning.pth"
tokenizer = Qwen3Tokenizer(
tokenizer_file_path=tokenizer_path,
apply_chat_template=True,
add_generation_prompt=True,
add_thinking=True,
)
else:
raise ValueError(f"Invalid choice: which_model={which_model}")
model = Qwen3Model(QWEN_CONFIG_06_B)
model.load_state_dict(torch.load(model_path))
model.to(device)
if use_compile:
torch._dynamo.config.allow_unspec_int_on_nn_module = True
model = torch.compile(model)
return model, tokenizer
def load_tokenizer_only(which_model, local_dir="qwen3"):
if which_model == "base":
download_qwen3_small(
kind="base", tokenizer_only=True, out_dir=local_dir
)
tokenizer_path = Path(local_dir) / "tokenizer-base.json"
tokenizer = Qwen3Tokenizer(tokenizer_file_path=tokenizer_path)
elif which_model == "reasoning":
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,
)
else:
raise ValueError(f"Invalid choice: which_model={which_model}")
return tokenizer
def generate_text_stream_concat(
model, tokenizer, prompt, device, max_new_tokens,
verbose=False,
):
input_ids = torch.tensor(
tokenizer.encode(prompt), device=device
).unsqueeze(0)
generated_ids = []
for token in generate_text_basic_stream_cache(
model=model,
token_ids=input_ids,
max_new_tokens=max_new_tokens,
eos_token_id=tokenizer.eos_token_id,
):
next_token_id = token.squeeze(0)
generated_ids.append(next_token_id.item())
if verbose:
print(
tokenizer.decode(next_token_id.tolist()),
end="",
flush=True
)
return tokenizer.decode(generated_ids)
def get_last_boxed(text):
# Find the last occurrence of "\boxed"
boxed_start_idx = text.rfind(r"\boxed")
if boxed_start_idx == -1:
return None
# Get position after "\boxed"
current_idx = boxed_start_idx + len(r"\boxed")
# Skip any whitespace after "\boxed"
while current_idx < len(text) and text[current_idx].isspace():
current_idx += 1
# Expect an opening brace "{"
if current_idx >= len(text) or text[current_idx] != "{":
return None
# Parse the braces with nesting
current_idx += 1
brace_depth = 1
content_start_idx = current_idx
while current_idx < len(text) and brace_depth > 0:
char = text[current_idx]
if char == "{":
brace_depth += 1
elif char == "}":
brace_depth -= 1
current_idx += 1
# Account for unbalanced braces
if brace_depth != 0:
return None
# Extract content inside the outermost braces
return text[content_start_idx:current_idx-1]
def extract_final_candidate(text, fallback="number_then_full"):
# Default return value if nothing matches
result = ""
if text:
# Prefer the last boxed expression if present
boxed = get_last_boxed(text.strip())
if boxed:
result = boxed.strip().strip("$ ")
# If no boxed expression, try fallback
elif fallback in ("number_then_full", "number_only"):
m = RE_NUMBER.findall(text)
if m:
# Use last number
result = m[-1]
elif fallback == "number_then_full":
# Else return full text if no number found
result = text
return result
def normalize_text(text):
if not text:
return ""
text = RE_SPECIAL.sub("", text).strip()
# Strip leading multiple-choice labels
# E.g., like "c. 3" -> 3, or "b: 2" -> 2
match = re.match(r"^[A-Za-z]\s*[.:]\s*(.+)$", text)
if match:
text = match.group(1)
# Remove angle-degree markers
text = re.sub(r"\^\s*\{\s*\\circ\s*\}", "", text) # ^{\circ}
text = re.sub(r"\^\s*\\circ", "", text) # ^\circ
text = text.replace("°", "") # Unicode degree
# unwrap \text{...} if the whole string is wrapped
match = re.match(r"^\\text\{(?P<x>.+?)\}$", text)
if match:
text = match.group("x")
# strip inline/display math wrappers \( \) \[ \]
text = re.sub(r"\\\(|\\\)|\\\[|\\\]", "", text)
# light LaTeX canonicalization
for pat, rep in LATEX_FIXES:
text = re.sub(pat, rep, text)
# convert unicode superscripts into exponent form (e.g., 2² -> 2**2)
def convert_superscripts(s, base=None):
converted = "".join(
SUPERSCRIPT_MAP[ch] if ch in SUPERSCRIPT_MAP else ch
for ch in s
)
if base is None:
return converted
return f"{base}**{converted}"
text = re.sub(
r"([0-9A-Za-z\)\]\}])([⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻]+)",
lambda m: convert_superscripts(m.group(2), base=m.group(1)),
text,
)
text = convert_superscripts(text)
# numbers/roots
text = text.replace("\\%", "%").replace("$", "").replace("%", "")
text = re.sub(
r"\\sqrt\s*\{([^}]*)\}",
lambda match: f"sqrt({match.group(1)})",
text,
)
text = re.sub(
r"\\sqrt\s+([^\\\s{}]+)",
lambda match: f"sqrt({match.group(1)})",
text,
)
# fractions
text = re.sub(
r"\\frac\s*\{([^{}]+)\}\s*\{([^{}]+)\}",
lambda match: f"({match.group(1)})/({match.group(2)})",
text,
)
text = re.sub(
r"\\frac\s+([^\s{}]+)\s+([^\s{}]+)",
lambda match: f"({match.group(1)})/({match.group(2)})",
text,
)
# exponent and mixed numbers
text = text.replace("^", "**")
text = re.sub(
r"(?<=\d)\s+(\d+/\d+)",
lambda match: "+" + match.group(1),
text,
)
# 1,234 -> 1234
text = re.sub(
r"(?<=\d),(?=\d\d\d(\D|$))",
"",
text,
)
return text.replace("{", "").replace("}", "").strip().lower()
def sympy_parser(expr):
# To avoid crashing on long garbage responses
# that some badly trained models (chapter 6) may emit
if expr is None or len(expr) > 2000:
return None
try:
return spp.parse_expr(
expr,
transformations=(
# Standard transformations like handling parentheses
*spp.standard_transformations,
# Allow omitted multiplication symbols (e.g., "2x" -> 2*x")
spp.implicit_multiplication_application,
),
# Evaluate during parsing so simple constants simplify (e.g., 2+3 -> 5)
evaluate=True,
)
except (SympifyError, SyntaxError, TypeError, AttributeError,
IndexError, TokenError, ValueError, PolynomialError):
return None
def equality_check(expr_gtruth, expr_pred):
# First, check if the two expressions are exactly the same string
if expr_gtruth == expr_pred:
return True
# Parse both expressions into SymPy objects (returns None if parsing fails)
gtruth, pred = sympy_parser(expr_gtruth), sympy_parser(expr_pred)
# If both expressions were parsed successfully, try symbolic comparison
if gtruth is not None and pred is not None:
try:
# If the difference is 0, they are equivalent
return simplify(gtruth - pred) == 0
except (SympifyError, TypeError):
pass
return False
def split_into_parts(text):
result = [text]
if text:
# Check if text looks like a tuple or list, e.g. "(a, b)" or "[a, b]"
if (
len(text) >= 2
and text[0] in "([" and text[-1] in ")]"
and "," in text[1:-1]
):
# Split on commas inside brackets and strip whitespace
items = [p.strip() for p in text[1:-1].split(",")]
if all(items):
result = items
else:
# If text is empty, return an empty list
result = []
return result
def grade_answer(pred_text, gt_text):
result = False # Default outcome if checks fail
# Only continue if both inputs are non-empty strings
if pred_text is not None and gt_text is not None:
gt_parts = split_into_parts(
normalize_text(gt_text)
) # Break ground truth into comparable parts
pred_parts = split_into_parts(
normalize_text(pred_text)
) # Break prediction into comparable parts
# Ensure both sides have same number of valid parts
if (gt_parts and pred_parts
and len(gt_parts) == len(pred_parts)):
result = all(
equality_check(gt, pred)
for gt, pred in zip(gt_parts, pred_parts)
) # Check each part for mathematical equivalence
return result # True only if all checks passed
def run_demos_table(tests):
header = ("Test", "Expect", "Got", "Status")
rows = []
for name, pred, gtruth, expect in tests:
got = grade_answer(pred, gtruth) # Run equality check
status = "PASS" if got == expect else "FAIL"
rows.append((name, str(expect), str(got), status))
data = [header] + rows
# Compute max width for each column to align table nicely
col_widths = [
max(len(row[i]) for row in data)
for i in range(len(header))
]
# Print table row by row
for row in data:
line = " | ".join(
row[i].ljust(col_widths[i])
for i in range(len(header))
)
print(line)
# Print summary of passed tests
passed = sum(r[3] == "PASS" for r in rows)
print(f"\nPassed {passed}/{len(rows)}")
def render_prompt(prompt):
template = (
"You are a helpful math assistant.\n"
"Answer the question and write the final result on a new line as:\n"
"\\boxed{ANSWER}\n\n"
f"Question:\n{prompt}\n\nAnswer:"
)
return template
def load_math500_test(local_path="math500_test.json", save_copy=True):
local_path = Path(local_path)
url = (
"https://raw.githubusercontent.com/rasbt/reasoning-from-scratch/"
"main/ch03/01_main-chapter-code/math500_test.json"
)
if local_path.exists():
with local_path.open("r", encoding="utf-8") as f:
data = json.load(f)
else:
r = requests.get(url, timeout=30)
r.raise_for_status()
data = r.json()
if save_copy: # Saves a local copy
with local_path.open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
return data
def mini_eval_demo(model, tokenizer, device):
ex = { # Test example with "problem" and "answer" fields
"problem": "Compute 1/2 + 1/6.",
"answer": "2/3"
}
prompt = render_prompt(ex["problem"]) # 1. Apply prompt template
gen_text = generate_text_stream_concat( # 2. Generate response
model, tokenizer, prompt, device,
max_new_tokens=64,
)
pred_answer = extract_final_candidate(gen_text) # 3. Extract and normalize answer
is_correct = grade_answer( # 4. Grade answer
pred_answer, ex["answer"]
)
print(f"Device: {device}")
print(f"Prediction: {pred_answer}")
print(f"Ground truth: {ex['answer']}")
print(f"Correct: {is_correct}")
def eta_progress_message(
processed,
total,
start_time,
show_eta=False,
label="Progress",
):
progress = f"{label}: {processed}/{total}"
pad_width = len(f"{label}: {total}/{total} | ETA: 00h 00m 00s")
if not show_eta or processed <= 0:
return progress.ljust(pad_width)
elapsed = time.time() - start_time
if elapsed <= 0:
return progress.ljust(pad_width)
remaining = max(total - processed, 0)
if processed:
avg_time = elapsed / processed
eta_seconds = avg_time * remaining
else:
eta_seconds = 0
eta_seconds = max(int(round(eta_seconds)), 0)
minutes, rem_seconds = divmod(eta_seconds, 60)
hours, minutes = divmod(minutes, 60)
if hours:
eta = f"{hours}h {minutes:02d}m {rem_seconds:02d}s"
elif minutes:
eta = f"{minutes:02d}m {rem_seconds:02d}s"
else:
eta = f"{rem_seconds:02d}s"
message = f"{progress} | ETA: {eta}"
return message.ljust(pad_width)
def evaluate_math500_stream(
model,
tokenizer,
device,
math_data,
out_path=None,
max_new_tokens=512,
verbose=False,
):
if out_path is None:
dev_name = str(device).replace(":", "-") # Make filename compatible with Windows
out_path = Path(f"math500-{dev_name}.jsonl")
num_examples = len(math_data)
num_correct = 0
total_len = 0 # Calculates the average response length (see exercise 3.2)
start_time = time.time()
with open(out_path, "w", encoding="utf-8") as f: # Save results for inspection
for i, row in enumerate(math_data, start=1):
prompt = render_prompt(row["problem"]) # 1. Apply prompt template
gen_text = generate_text_stream_concat( # 2. Generate response
model, tokenizer, prompt, device,
max_new_tokens=max_new_tokens,
verbose=verbose,
)
total_len += len(tokenizer.encode(gen_text))
extracted = extract_final_candidate( # 3. Extract and normalize answer
gen_text
)
is_correct = grade_answer( # 4. Grade answer
extracted, row["answer"]
)
num_correct += int(is_correct)
record = { # Record to be saved for inspection
"index": i,
"problem": row["problem"],
"gtruth_answer": row["answer"],
"generated_text": gen_text,
"extracted": extracted,
"correct": bool(is_correct),
}
f.write(json.dumps(record, ensure_ascii=False) + "\n")
progress_msg = eta_progress_message(
processed=i,
total=num_examples,
start_time=start_time,
show_eta=True,
label="MATH-500",
)
print(progress_msg, end="\r", flush=True)
if verbose: # Print responses during the generation process
print(
f"\n\n{'='*50}\n{progress_msg}\n"
f"{'='*50}\nExtracted: {extracted}\n"
f"Expected: {row['answer']}\n"
f"Correct so far: {num_correct}\n{'-'*50}"
)
# Print summary information
seconds_elapsed = time.time() - start_time
acc = num_correct / num_examples if num_examples else 0.0
print(f"\nAccuracy: {acc*100:.1f}% ({num_correct}/{num_examples})")
print(f"Total time: {seconds_elapsed/60:.1f} min")
avg_len = total_len / num_examples
print(f"Average response length: {avg_len:.2f} tokens")
print(f"Logs written to: {out_path}")
return num_correct, num_examples, acc