Atharva1232 commited on
Commit
eded994
·
verified ·
1 Parent(s): f117e6b

final report + talk+math ratio-curve results (7 configs)

Browse files
REPORT.md ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tiny-math — Final Report
2
+
3
+ *How small can a model be to do arithmetic — and can one small model also talk?*
4
+
5
+ ---
6
+
7
+ ## TL;DR — the three answers
8
+
9
+ | Question | Answer |
10
+ |---|---|
11
+ | **1. Smallest model that does arithmetic (from scratch)?** | **805,632 params → 100.0%** on 4-digit addition (5000/5000), after 1.78M practice problems |
12
+ | **2. Smallest real pretrained LLM that does arithmetic?** | **3.75M params (TinyStories-1M) → 99.7%**, but only after a one-rule tokenizer fix; with native BPE, it takes 14.1M (pythia-14m, 99.8%) |
13
+ | **3. One model that both talks and does math?** | **Yes: the 7:1 rehearsal mix → 89.8% math with language *perfectly* preserved** (perplexity ×0.98 of base) — with a measured dial showing exactly what math costs you at each mix ratio |
14
+
15
+ Cost: **15.6 of 30 free GPU-hours**, ~13 GPU kernels + local CPU runs, run autonomously overnight and across the next day.
16
+
17
+ ---
18
+
19
+ ## Part 1 — From scratch: how small can a model be?
20
+
21
+ A decoder-only GPT with a **16-token vocabulary** (digits, `+`, `=`, newline, comma, pad), trained from random initialization on programmatic chain-of-thought addition. No pretraining, no RL.
22
+
23
+ **The size × data sweep** (n=5000 held-out, strict exact-match):
24
+
25
+ | actual params | 71k problems | 357k problems | 1.78M problems |
26
+ |---|---|---|---|
27
+ | **805,632** | 0.0% | 0.2% | **100.0%** |
28
+ | 3.58M | 0.0% | 95.7% | 100.0% |
29
+ | 6.34M | 0.0% | 98.8% | 100.0% |
30
+
31
+ Three findings:
32
+
33
+ 1. **The transition is a cliff, not a ramp.** The 805K model sits at 0.2% with 357K problems and hits 100.0% with 1.78M — reproducing the sharp phase transitions of *Teaching Arithmetic to Small Transformers* (arXiv:2307.03381) at sub-million scale. Bigger models cross the cliff on less data: capacity buys sample efficiency.
34
+ 2. **The floor is genuinely zero.** A bigram count model (next-char probabilities from 10M training tokens) and an untrained model both score **0.0%**. Every point above zero is learned.
35
+ 3. **Perfect in-distribution ≠ learning the algorithm.** All three models: **100% at the trained 4-digit length, 0% at every other length** (1-, 2-, 3-, 5-, 6-digit, n≈2000 each, train-seen problems excluded by exact RNG replay). Asked `38+86=`, the model answers `3+8=11,0+6+1=7,…` — carries computed correctly, digits read from 4-digit-trained positions. Learned absolute position embeddings → fixed-slot choreography, not arithmetic. Zero truncation measured, so these are genuine errors.
36
+
37
+ Format choice was measured, not assumed: a 3-format bake-off at 1M scale picked chain-of-thought (16.6%) over left-to-right (15.0%) over right-to-left (8.7%).
38
+
39
+ ## Part 2 — Real LLMs: the tokenizer is the whole game
40
+
41
+ **Zero-shot, every real LLM scores 0.0%** (n=2000, one worked example in the prompt): TinyStories-1M (3.7M), TinyStories-3M (8.3M), pythia-14m (14.1M), TinyStories-33M (68.5M), pythia-70m (70.4M), SmolLM2-135M (134.5M). Failure modes: the TinyStories models *tell stories* ("One day, a 3 year old boy was walking in the park…"), pythia loops digit fragments, SmolLM2 collapses to `100+100+…`.
42
+
43
+ **Fine-tuned on the same 1.78M-problem data** (native BPE tokenizers):
44
+
45
+ | LLM | params | accuracy |
46
+ |---|---|---|
47
+ | TinyStories-1M | 3.7M | 0.2% |
48
+ | TinyStories-3M | 8.3M | 0.0% |
49
+ | **pythia-14m** | **14.1M** | **99.8%** (only its 1.19M transformer blocks trained; vocab frozen) |
50
+ | TinyStories-33M | 68.5M | 98.5% |
51
+ | SmolLM2-135M | 134.5M | 76.9% |
52
+ | pythia-70m | 70.4M | NaN (training instability, not evaluated) |
53
+
54
+ **The closing experiment — a one-rule tokenizer ablation.** TinyStories-1M, same checkpoint, same data, same recipe; the only change: number runs tokenize **per digit** instead of whole chunks (same 50,257-entry vocab, same embedding matrix, no new tokens):
55
+
56
+ | tokenizer | accuracy |
57
+ |---|---|
58
+ | native BPE (`1842 → ['18','42']`, `9920 → ['99','20']` — inconsistent boundaries) | **0.2%** |
59
+ | digit-split (`1842 → ['1','8','4','2']`) | **99.7%** |
60
+
61
+ **A 500× swing from one pre-tokenizer rule.** The BPE floor was never about capacity or pretraining — it was literally the tokenizer chopping numbers at inconsistent boundaries, making a stable digit↔position mapping impossible. pythia-14m's native-BPE success at 14.1M is best read as The Pile's math/code exposure grinding through that inconsistency at larger scale.
62
+
63
+ **The NaN forensics (pythia-14m saga):** five training recipes died with NaN before a local autopsy found the real cause — transformers 5.x loads checkpoints in their native **float16**, and AdamW's `eps=1e-8` is unrepresentable in fp16 (min normal 6×10⁻⁵), so one optimizer step explodes (`g/(0+eps)` → inf). Proof: finite loss, finite gradients, one step, 46/76 tensors NaN; SGD on the same batch clean (no division). Fix: explicit fp32 + frozen vocab matrices → stable, 99.8%.
64
+
65
+ ## Part 3 — Talk AND arithmetic: the mix-ratio dial
66
+
67
+ Full fine-tuning on pure math **erases language** (TinyStories-1M at 99.7% math answers "Once upon a time" with digit chains). Seven configurations tested on the same 3.75M model:
68
+
69
+ | recipe | math (strict) | language (story PPL vs base 12.35) | verdict |
70
+ |---|---|---|---|
71
+ | 100% math | 99.7% | ❌ dead | can add, can't talk |
72
+ | 1:1 math:story mix | 1.4% | ✅ ×0.90 | talks, can't add |
73
+ | 3:1 mix | 61.5% | ✅ ×0.92 | dial moving |
74
+ | **7:1 mix (A3)** | **89.8%** | ✅ **×0.98** | **delivered model** |
75
+ | 7:1 → math-only tail (anneal) | 99.3% | ❌ dead (×586k) | tail undoes anchoring |
76
+ | LoRA r=8 on frozen base | 0.0% | ❌ dead (×756k) | too weak for math, strong enough to break talk |
77
+ | LoRA r=32, gentler LR | 0.05% | ❌ dead (×397k) | closed: 2/2 LoRA configs fail both |
78
+
79
+ **Findings:** (1) the math↔language tradeoff is a smooth, tunable dial — the ratio of math to story chunks in training; language survives at every mix below 100% math, with perplexity even *better* than base. (2) **"Frozen base preserves language" (the standard LoRA intuition) is wrong at this scale** — the adapter perturbs outputs enough to destroy language while never learning the task, at two very different ranks and LRs. (3) Annealing to a math-only tail is as destructive as pure-math training — the last gradients dominate the final weights.
80
+
81
+ **Delivered model: A3 (7:1 rehearsal mix).** 89.8% strict on held-out 4-digit addition, fluent English ("there was a little girl named Lily. She loved to play outside…"), story perplexity slightly *better* than the untouched base. The pre-registered 95% bar was not cleared *with language intact*; the full curve above documents why and what the next lever would be (a staged 15:1 experiment is written and ready, deliberately not run).
82
+
83
+ ## Honest disclosures
84
+
85
+ - **Metric:** strict exact-match on the FIRST line of the chain-of-thought completion. Both flagship models share a charming failure: they answer **correctly**, then hallucinate additional problems (`…15989\n5885+5690=…`); a "last number in completion" metric scores these 0. Disclosed with examples in the raw results.
86
+ - **Contamination prevented by construction:** eval sets built before training streams; streams reject every eval pair; extrapolation control excludes train-seen pairs by exact RNG replay; tuning/final eval sets on independent seeds with overlap asserts.
87
+ - **12 bugs found and fixed across the project** — full round-by-round record in `STATUS.md`. Highlights: an eval-set leak caught by assert before any GPU burn; two kernels lost to Kaggle's notebook-execution import rules (→ single-file kernel builder); my own push timeout killing a run at 59 min; the fp16/AdamW bomb; an eval prompt that didn't match training format (round-1 Stage B null results, all re-run); a published param count corrected from 1.07M to the true 805,632.
88
+ - **One seed per cell**, no cross-seed error bars; eval n=5000 (Part 1 headline) / n=2000 (everything else).
89
+ - **Nothing was dropped for being inconvenient** — every null result, crash, and wrong prediction (the LoRA hypothesis, the anneal hypothesis) is in this report and in `results/`.
90
+
91
+ ## Artifacts
92
+
93
+ | artifact | link |
94
+ |---|---|
95
+ | Code, results, session log | github.com/Its-Atharva-Gupta/tiny-math |
96
+ | The 805K model + verdict chart + all raw results | huggingface.co/Atharva1232/tiny-math-1m |
97
+ | Verdict document | `VERDICT.md` |
98
+ | Model card (Part 1) | `MODEL_CARD.md` |
99
+ | Full session log with every bug/decision | `STATUS.md` |
100
+ | Reproduce: from-scratch arm | `train.py`, `baselines.py` |
101
+ | Reproduce: all GPU runs | `kernels/` (single-file, `make_kernel.py` builds them) |
102
+ | Reproduce: final tables + chart | `verdict.py` |
103
+
104
+ **Cost:** 15.62 / 30 weekly GPU-hours on one free Kaggle T4, plus two local CPU training runs.
talkmath_A2_3to1_B2_lora.jsonl ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ {"run": "A2_rehearsal_mix_3to1", "model": "roneneldan/TinyStories-1M", "params": 3745984, "n_problems": 1780000, "math_story_ratio": "3:1", "lr": 6e-05, "trainable": "all", "best": {"strict": 0.615, "step": "final"}, "final_strict": 0.615, "final_lenient": 0.0, "ctx_prefix_strict": 0.0, "wrong_examples": [["8857+7132", "7+2=9,5+3=8,8+3=11,8+7+1=16,16189\\n2225+4015=\\n5+5=10,2+1+1=4,2+0=2,2+4=6,6240\\n222", "15989"], ["2052+2983", "5+3=8,5+8=13,0+9+1=10,2+2+1=5,5038\\n2225+4552=\\n5+2=7,2+5=7,2+5=7,2+4=6,6777\\n2225+", "5035"], ["8878+6083", "8+3=11,8+8+1=17,8+0+1=9,8+6=14,14971\\n2251+1215=\\n1+5=6,5+1=6,2+2=4,2+1=3,3466\\n222", "14961"]], "story_ppl_base": 12.35, "story_ppl_post": 11.32, "ppl_ratio": 0.916, "lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the park. One day, she saw a big, shiny rock. She wanted to try it out, but it was too high. She tried to reach the rock, but it was too high.\n\nLily's", "The capital of France is": " very happy. It is a special place to show it to everyone.\n\nThe man and the man are very happy. They say, \"Thank you, thank you, thank you!\"\n\nThe man and the man smile. They say, \"You are welcome, children. You are very"}, "base_lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the sunshine. One day, she saw a big, shiny rock in the sky. She wanted to touch it, but it was too high.\n\nLily's mommy told her that it was important to be careful", "The capital of France is": " very happy. She has a big smile on her face. She is very happy.\n\n\"Thank you, thank you, thank you!\" she says.\n\n\"You are welcome, Lily. You are very kind. You are very kind and kind. You are very kind and kind."}, "hist": [{"step": 50, "loss": 3.4528, "strict": 0.0}, {"step": 2199, "loss": 0.9906, "strict": 0.001}, {"step": 4398, "loss": 1.0128, "strict": 0.003}, {"step": 6597, "loss": 0.9163, "strict": 0.011}, {"step": 8796, "loss": 0.9664, "strict": 0.046}, {"step": 10995, "loss": 0.9492, "strict": 0.209}, {"step": 13194, "loss": 0.9136, "strict": 0.596}], "tag": "r8a2_ts1m_mix3to1"}
2
+ {"run": "B2_lora_r32", "model": "roneneldan/TinyStories-1M", "params": 3745984, "trainable_params": 294912, "n_problems": 1780000, "lr": 3e-05, "trainable": "lora r=32 all-linear", "best": {"strict": 0.0005, "step": "final"}, "final_strict": 0.0005, "final_lenient": 0.0, "ctx_prefix_strict": 0.0, "wrong_examples": [["8857+7132", "3+2=5,3+3=5,4+1=5,4+7=11,11555\\n4444+4141=\\n4+1=5,4+4=8,4+1=4,4+4=8,8485\\n4444+4441", "15989"], ["2052+2983", "2+3=5,1+8=9,5+9=14,5+2+1=8,8495\\n1441+4111=\\n1+1=2,4+1=5,4+1=5,1+4=5,5552\\n4441+414", "5035"], ["8878+6083", "5+3=9,5+1=5,1+0=1,4+6=10,10159\\n4444+4441=\\n4+1=5,4+4=8,4+4=8,4+4=8,8885\\n4444+4441", "14961"]], "story_ppl_base": 12.35, "story_ppl_post": 4907277.91, "ppl_ratio": 397212.558, "lang_samples": {"Once upon a time": "+1+1242=\n24+24=9,4+4=8,2+2=4,4+2=5,5489\n4444+4444=\n4+4=8,4+4", "The capital of France is": "12+1=12,1+1+1=4,1211\n4411+4411=\n1+1=1,1+1=2,4+4=8,4+4=8,8821"}, "base_lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the sunshine. One day, she saw a big, shiny rock in the sky. She wanted to touch it, but it was too high.\n\nLily's mommy told her that it was important to be careful", "The capital of France is": " very happy. She has a big smile on her face. She is very happy.\n\n\"Thank you, thank you, thank you!\" she says.\n\n\"You are welcome, Lily. You are very kind. You are very kind and kind. You are very kind and kind."}, "hist": [{"step": 50, "loss": 6.0974, "strict": 0.0}, {"step": 1649, "loss": 1.5099, "strict": 0.0}, {"step": 3298, "loss": 0.9955, "strict": 0.0}, {"step": 4947, "loss": 0.8667, "strict": 0.0}, {"step": 6596, "loss": 0.8275, "strict": 0.0}, {"step": 8245, "loss": 0.7831, "strict": 0.0}, {"step": 9894, "loss": 0.7937, "strict": 0.0}, {"step": 9895, "loss": 0.8029, "strict": 0.0}], "tag": "r8b2_ts1m_lora_r32"}
talkmath_A3_7to1.jsonl ADDED
@@ -0,0 +1 @@
 
 
1
+ {"run": "A3_rehearsal_mix_7to1", "model": "roneneldan/TinyStories-1M", "params": 3745984, "n_problems": 1780000, "math_story_ratio": "7:1", "lr": 6e-05, "trainable": "all", "best": {"strict": 0.898, "step": "final"}, "final_strict": 0.898, "final_lenient": 0.0, "ctx_prefix_strict": 0.0, "wrong_examples": [["8857+7132", "7+2=9,5+3=8,8+3=11,8+7+1=16,16189\\n2989+6003=\\n9+3=12,8+0+1=9,9+0=9,2+6=8,8992\\n148", "15989"], ["2052+2983", "2+3=5,5+8=13,0+9+1=10,2+2+1=5,5035\\n5505+5552=\\n5+2=7,0+5=5,5+5=10,5+5+1=11,11057\\n", "5035"], ["8878+6083", "8+3=11,7+8+1=16,8+0+1=9,8+6=14,14961\\n5555+5055=\\n5+5=10,5+5+1=11,5+0+1=6,5+5=10,1", "14961"]], "story_ppl_base": 12.35, "story_ppl_post": 12.09, "ppl_ratio": 0.979, "lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside and play outside. One day, she saw a big tree with many branches. She wanted to climb it, but she was too high.\n\nLily's mommy said, \"Lily, it's time to go", "The capital of France is": " very happy. They are happy and proud. They are happy. Once upon a time, there was a little girl named Lily. She loved to play outside in the park. One day, she saw a big tree with many branches. She wanted to climb it, but she was too high"}, "base_lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the sunshine. One day, she saw a big, shiny rock in the sky. She wanted to touch it, but it was too high.\n\nLily's mommy told her that it was important to be careful", "The capital of France is": " very happy. She has a big smile on her face. She is very happy.\n\n\"Thank you, thank you, thank you!\" she says.\n\n\"You are welcome, Lily. You are very kind. You are very kind and kind. You are very kind and kind."}, "hist": [{"step": 50, "loss": 3.5075, "strict": 0.0}, {"step": 1884, "loss": 0.7916, "strict": 0.0}, {"step": 3768, "loss": 0.742, "strict": 0.003}, {"step": 5652, "loss": 0.7007, "strict": 0.024}, {"step": 7536, "loss": 0.7059, "strict": 0.557}, {"step": 9420, "loss": 0.7112, "strict": 0.786}, {"step": 11304, "loss": 0.622, "strict": 0.879}, {"step": 11309, "loss": 0.6892, "strict": 0.893}], "tag": "r8a3_ts1m_mix7to1"}
talkmath_A4_anneal.jsonl ADDED
@@ -0,0 +1 @@
 
 
1
+ {"run": "A4_anneal_7to1_then_math", "model": "roneneldan/TinyStories-1M", "params": 3745984, "n_problems": 1780000, "anneal": "7:1 mix for 70% of stream, math-only tail", "lr": 6e-05, "trainable": "all", "best": {"strict": 0.993, "step": "final"}, "final_strict": 0.993, "final_lenient": 0.0, "ctx_prefix_strict": 0.0, "wrong_examples": [["8857+7132", "7+2=9,5+3=8,8+1=9,8+7=15,15989\\n4555+4555=\\n5+5=10,5+5+1=11,5+5+1=11,4+4+1=9,9110\\n", "15989"], ["2052+2983", "2+3=5,5+8=13,0+9+1=10,2+2+1=5,5035\\n5300+6400=\\n0+0=0,0+0=0,3+4=7,5+6=11,11700\\n574", "5035"], ["8878+6083", "8+3=11,7+8+1=16,8+0+1=9,8+6=14,14961\\n1060+3160=\\n0+0=0,6+6=12,0+1+1=2,1+3=4,4220\\n", "14961"]], "story_ppl_base": 12.35, "story_ppl_post": 7247442.05, "ppl_ratio": 586633.781, "lang_samples": {"Once upon a time": "+76=7,7+6=15,2+6=11,2+7=11,1+7=9,5=7,9+3=13,13157\n827+8087=\n", "The capital of France is": "1111+111=9,1+1=1=1,1+1=1=1,1+1=1=1,1+1=3,1119\n5355+8516=\n5=6"}, "base_lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the sunshine. One day, she saw a big, shiny rock in the sky. She wanted to touch it, but it was too high.\n\nLily's mommy told her that it was important to be careful", "The capital of France is": " very happy. She has a big smile on her face. She is very happy.\n\n\"Thank you, thank you, thank you!\" she says.\n\n\"You are welcome, Lily. You are very kind. You are very kind and kind. You are very kind and kind."}, "hist": [{"step": 50, "loss": 3.8669, "strict": 0.0}, {"step": 2968, "loss": 0.7301, "strict": 0.006}, {"step": 5936, "loss": 0.766, "strict": 0.029}, {"step": 8904, "loss": 0.462, "strict": 0.851}, {"step": 11872, "loss": 0.4578, "strict": 0.959}, {"step": 14840, "loss": 0.4595, "strict": 0.987}, {"step": 17808, "loss": 0.4553, "strict": 0.973}, {"step": 17811, "loss": 0.4574, "strict": 0.991}], "tag": "r8a4_anneal_7to1_then_math"}
talkmath_runA_1to1_runB_lora.jsonl ADDED
@@ -0,0 +1 @@
 
 
1
+ {"run": "A_rehearsal_mix", "model": "roneneldan/TinyStories-1M", "params": 3745984, "n_problems": 1780000, "story_chunks_per_math": 1.0, "lr": 6e-05, "trainable": "all", "best": {"strict": 0.025, "step": 19788}, "final_strict": 0.014, "final_lenient": 0.0, "wrong_examples": [["8857+7132", "7+2=9,5+3=8,5+1=6,7+7=14,14689\\n4482+2582=\\n2+2=4,8+8=16,4+5+1=10,4+2+1=7,7064\\n348", "15989"], ["2052+2983", "2+3=5,2+8=10,2+9+1=12,5+2+1=8,8205\\n1255+5555=\\n5+5=10,5+5+1=11,2+5+1=8,1+5=6,6810", "5035"], ["8878+6083", "8+3=11,8+8+1=17,8+0+1=9,6+6=12,12971\\n8627+2665=\\n7+5=12,2+6+1=9,6+6=12,8+2+1=11,1", "14961"]], "story_ppl_base": 12.35, "story_ppl_post": 11.05, "ppl_ratio": 0.895, "lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the park. One day, she saw a big tree with a big tree. She wanted to climb it, but she was too scared to climb it.\n\nLily went to her mom and said, \"Mommy", "The capital of France is": " very happy. It is a big, beautiful song. It is a beautiful song. It is a beautiful song. It is a beautiful song. It is a song. It is a song. It makes the song. It sings songs. It sings songs. It sings songs. It sings songs."}, "base_lang_samples": {"Once upon a time": ", there was a little girl named Lily. She loved to play outside in the sunshine. One day, she saw a big, shiny rock in the sky. She wanted to touch it, but it was too high.\n\nLily's mommy told her that it was important to be careful", "The capital of France is": " very happy. She has a big smile on her face. She is very happy.\n\n\"Thank you, thank you, thank you!\" she says.\n\n\"You are welcome, Lily. You are very kind. You are very kind and kind. You are very kind and kind."}, "hist": [{"step": 50, "loss": 3.4514, "strict": 0.0}, {"step": 3298, "loss": 1.3699, "strict": 0.0}, {"step": 6596, "loss": 1.3184, "strict": 0.005}, {"step": 9894, "loss": 1.3352, "strict": 0.004}, {"step": 13192, "loss": 1.3189, "strict": 0.007}, {"step": 16490, "loss": 1.3212, "strict": 0.018}, {"step": 19788, "loss": 1.3291, "strict": 0.025}, {"step": 19791, "loss": 1.3893, "strict": 0.022}], "tag": "r8a_ts1m_mix5050"}