MiniReasoner / README.md
sampath017's picture
Make modeling_minireasoner.py compatible with standalone importlib loading and update README snippet
ccb909e verified
|
Raw History Blame Contribute Delete
6.52 kB
---
language:
- en
library_name: pytorch
pipeline_tag: text-generation
tags:
- pytorch
- rope
- transformer
- from-scratch
- chain-of-thought
- arithmetic
- reasoning
- sft
- kv-cache
- rlvr
- grpo
datasets:
- SimpleStories/SimpleStories
- sampath017/MiniReasoner-arith-v3
---
# MiniReasoner (32M RoPE CoT SFT Release)
A **32.05M-parameter RoPE Transformer** built from scratch in PyTorch—pre-trained on `853.5M` tokens from [`SimpleStories/SimpleStories`](https://huggingface.co/datasets/SimpleStories/SimpleStories) (`26,047` steps) and fine-tuned on [`sampath017/MiniReasoner-arith-v3`](https://huggingface.co/datasets/sampath017/MiniReasoner-arith-v3) (`10,000` steps) with `10%` story replay.
This checkpoint (`e57bf52bb95842dba6628092261584c4`, Step `10,000`) is the **Phase 6 release winner**, achieving **`97.35%` strict trace pass (`97.42%` exact match)** across all `6,002` rows of the sealed `arith_v3` test split while preserving English story fluency (`3.31` `SimpleStories` test PPL vs. `3.12` base).
- **GitHub Repository:** [sampath017/MiniReasoner](https://github.com/sampath017/MiniReasoner)
- **Full Build Playbook:** [COOKBOOK.md](https://github.com/sampath017/MiniReasoner/blob/main/COOKBOOK.md)
- **SFT/Eval Dataset:** [`sampath017/MiniReasoner-arith-v3`](https://huggingface.co/datasets/sampath017/MiniReasoner-arith-v3)
![MiniReasoner Phase 6 Release Comparison](assets/phase6_release_comparison.png)
## Model Details
| Property | Value |
| --- | --- |
| **Architecture** | Decoder-only GPT with RoPE (`theta = 10,000`) & tied embeddings |
| **Parameters** | `32,049,152` (`10` layers, `8` heads, `head_dim = 64`, `n_embd = 512`) |
| **Context Window** | `1,024` tokens |
| **Tokenizer** | Custom digit-isolated `1,024`-token byte-level BPE (`<|eos|>` ID `0`) |
| **Pre-Training** | `SimpleStories` (`2,094,643` train stories / `853,498,695` tokens, `26,047` steps, run `23a72f3a...`) |
| **CoT SFT** | `arith_v3` (`10,000` steps, `bs=32`, `-100` prompt masking, `10%` story replay, run `e57bf52b...`) |
| **Artifacts Included** | `model.pt` (`122.4 MiB` inference weights + metadata), `latest_step_10000_077e9ad1.pt` (`367.0 MiB` full checkpoint), `config.json`, `tokenizer.json`, `modeling_minireasoner.py` |
## Evaluation Summary (Phase 6 Sealed Test)
Evaluated once on the sealed `arith_v3` `test` split (`6,002` rows) and held-out `SimpleStories` `test` stories (`250` stories / `101,970` tokens):
| Stage / Checkpoint | Run ID | Arithmetic Accuracy (`6,002` sealed test) | `SimpleStories` Test | Notes |
| --- | --- | --- | --- | --- |
| **Base** (Step `26,047`) | `23a72f3a` | `0.0%` (`0/10` zero-shot) | **PPL `3.12`** · BPB `0.5426` · Cloze `86.0%` | 1 epoch over `853.5M` tokens |
| **Direct SFT** (Step `8,000`) | `ef3cc27c` | `88.8%` val (`213/240`) | `1.2021` story val loss | Bottlenecked on `mul_3d` (`3/10`) |
| **CoT Scratchpad SFT** (Step `10,000`) 🏆 | `e57bf52b` | **`97.35%` strict / `97.42%` exact** (`5,843/6,002`) | **PPL `3.31`** · BPB `0.5697` · Cloze `83.0%` | **Release winner** · `100.0%` wording gen |
| **GRPO RL `best`** (Step `100`) | `486683da` | `97.15%` strict / `97.23%` exact (`5,831/6,002`) | **PPL `3.32`** · BPB `0.5708` · Cloze `83.0%` | `fixed=56, broke=68` (`net=-12`) |
| **GRPO RL `latest`** (Step `300`) | `486683da` | `96.93%` strict / `97.03%` exact (`5,818/6,002`) | **PPL `3.32`** · BPB `0.5709` · Cloze `83.0%` | `fixed=58, broke=83` (`net=-25`) |
**Per-Operation Breakdown (CoT SFT Step `10,000`, `strict_trace=True`):**
`add`: `99.75%` (`1,197/1,200`) · `sub`: `99.58%` (`1,197/1,202`) · `div`: `96.92%` (`1,163/1,200`) · `mul`: `95.50%` (`1,146/1,200`) · `multi`: `95.00%` (`1,140/1,200`) · **Valid Work Trace:** `98.33%` · **Format Rate:** `100.00%`
| Base Pre-Training (`853.5M` tokens) | CoT Scratchpad SFT (`arith_v3` + 10% replay) |
| :---: | :---: |
| ![Base Pre-Training Curves](assets/phase3_pretrain_curves.png) | ![CoT SFT Training Curves](assets/phase4b_cot_sft_curves.png) |
## Quickstart (Standalone Inference, No Repo Clone Required)
Install dependencies (`pip install torch tokenizers huggingface_hub`), then run:
```python
from huggingface_hub import hf_hub_download
import importlib.util
import sys
mod_path = hf_hub_download("sampath017/MiniReasoner", "modeling_minireasoner.py")
spec = importlib.util.spec_from_file_location("modeling_minireasoner", mod_path)
mr = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = mr
spec.loader.exec_module(mr)
reasoner = mr.MiniReasoner.from_pretrained("sampath017/MiniReasoner")
# 1. Arithmetic CoT Scratchpad (always end prompt with "\n" or use .solve())
print(reasoner.solve("What is -95 * 672?"))
# {'prompt': 'What is -95 * 672?\n',
# 'output': 'Work: 672*90=60480; 672*5=3360; 60480+3360=63840; neg=-63840\nAnswer: -63840',
# 'work': '672*90=60480; 672*5=3360; 60480+3360=63840; neg=-63840',
# 'answer': -63840}
print(reasoner.solve("Compute 15 * 4 and 92 - 17, then subtract the second result from the first."))
# {'prompt': 'Compute 15 * 4 and 92 - 17, then subtract the second result from the first.\n',
# 'output': 'Work: 4*10=40; 4*5=20; 40+20=60; 92-10=82; 82-7=75; 75-60=15; neg=-15\nAnswer: -15',
# 'work': '4*10=40; 4*5=20; 40+20=60; 92-10=82; 82-7=75; 75-60=15; neg=-15',
# 'answer': -15}
# 2. SimpleStories Narrative Continuation
print(reasoner.generate("Once upon a time, there was a little", max_new_tokens=120, temperature=0.8, top_p=0.9))
```
### Reproducing Evaluations with the GitHub Repository
```bash
git clone https://github.com/sampath017/MiniReasoner.git
cd MiniReasoner && uv sync
```
Download the exact full checkpoint (`latest_step_10000_077e9ad1.pt`, SHA-256 `077e9ad172eaf5c4...`) via `hf_hub_download("sampath017/MiniReasoner", "latest_step_10000_077e9ad1.pt")` and pass it to `src/core/generate.py` (`complete`) or `notebooks/phase6_final_release.ipynb`.
## Prompt Format & Limitations
- **Arithmetic prompt format:** Prompts in `arith_v3` end with a trailing newline (`"What is 47 + 8?\n"`), and the model emits `"Work: <semicolon-separated steps>\nAnswer: <integer><|eos|>"`.
- **Scope & known failure modes:** Trained on signed integers up to 3 digits (`-999..999`), exact integer division, and 2-step PEMDAS/word chains. Harder 3-digit multiplications (e.g., `177 * 515`) and certain 3-digit signed divisions (`~3%–9%` slice error rate) can still slip; out-of-distribution `4+`-digit problems or non-story open-domain factual QA are outside training scope.