tinyzero-countdown-19m
A ~18.9M-parameter decoder-only transformer, pretrained from scratch and
post-trained with GRPO (Group Relative Policy Optimization) to solve
Countdown-style arithmetic puzzles: given a set of numbers and a target,
find an equation using each number exactly once that reaches the target.
Architecture
RoPE positional embeddings, RMSNorm, grouped-query attention (via
F.scaled_dot_product_attention), SwiGLU MLP, tied embeddings. Custom
8192-token BPE vocabulary trained on the pretraining corpus (not GPT-2's
tokenizer -- see rationale below).
- Parameters: ~18.88M (verified exactly, not estimated)
- Context length: 256
- Vocab size: 8192 (custom-trained BPE)
- d_model: 384, layers: 10, heads: 6 (2 KV heads, GQA)
Training pipeline and what I learned building it
Pretraining: ~380M tokens on FineWeb-Edu + synthetic arithmetic
text, at a ~20:1 token:parameter ratio (Chinchilla-optimal). An earlier
attempt at 116M params / 150M tokens (1.3:1 ratio) showed the failure mode
directly: healthy train/val loss gap but weak generalization. This version
also fixes a subtler issue -- at small model scale, a standard 50k-token
vocabulary's embedding table dominates the parameter budget (60-75% of
total params); training a small custom vocab instead keeps embedding
overhead to ~17%, leaving actual capacity for reasoning.
SFT: an instruction-format fine-tune initially looked successful by
loss (train 1.02->0.34) but generation accuracy was 0% -- a real
loss/accuracy divergence caused by a response template that was mostly
easy-to-predict boilerplate, diluting the loss signal on the tokens that
actually mattered (the numbers/operators). Root-caused to a large,
un-bridged distribution shift between the pretraining corpus's format and
the instruction phrasing; fixed by skipping the instruction wrapper and
running GRPO directly on the pretrained checkpoint's native prompt format
instead.
GRPO: trained directly on the pretrained checkpoint, using the
verifier (exact equation checker) as a binary+partial-credit reward, group-
relative advantage normalization, PPO-style clipping, and a KL penalty
against a frozen reference to prevent collapse. Result: 31.6% ->
34.4% accuracy on a held-out 250-problem set (+2.8pp), with stable
KL throughout (no collapse). This is a modest, honestly-reported effect --
run at only ~500 steps on an 18.9M model, not a large or highly significant
result, and reported with that caveat intentionally.
Usage
1import torch
2from tokenizers import ByteLevelBPETokenizer
3# adapt these imports to wherever you place model.py / config.py from this repo
4from model import TinyTransformer
5from config import ModelConfig
6
7mcfg = ModelConfig()
8model = TinyTransformer(mcfg)
9ckpt = torch.load("pytorch_model.pt", map_location="cpu")
10model.load_state_dict(ckpt["model_state_dict"])
11model.eval()
12
13tokenizer = ByteLevelBPETokenizer("vocab.json", "merges.txt")
14prompt = "Numbers: [12, 45, 7, 3], Target: 88, Equation:"
15ids = tokenizer.encode(prompt).ids
16x = torch.tensor([ids])
17out = model.generate(x, max_new_tokens=40, temperature=1.0, top_k=1)
18print(tokenizer.decode(out[0, len(ids):].tolist()))
Limitations
- Small model (~19M params) -- general text fluency is weak; this is
specialized for the countdown arithmetic task, not general-purpose use.
- Only handles the raw prompt format shown above; natural-language
instruction phrasing was found to significantly degrade output quality
(see training notes above) and was not used for the released checkpoint.
- Evaluated on synthetically generated countdown problems only.