llm-from-scratch · stages 00–02

Code LLM Build Map

What has been built so far, drawn out: the memory budget, the data pipeline, the leaks we found in GitHub data, and a live tokenizer you can train by clicking.

target ~125M-param Python model GPU RTX 4060 · 8 GB tests 114 passing data 2.36B tokens ready updated 25 Oct 2026
Where the project is

3 of 25 stages done

done in progress not started
Stage 00 · setup

Where GPU memory goes during training

Training needs far more memory than the model itself. For every parameter the GPU holds the weight, its gradient, and two numbers the Adam optimizer keeps. That is 16 bytes per parameter before any activations.

We predicted 192 MB for a 12.6M-parameter test model and measured 208 MB. The 8.5% gap is activations held for the backward pass. That rule of thumb is why 125M parameters is our ceiling on 8 GB.

weights · 4 bytes/param48.0 MB gradients + activations64.3 MB Adam optimizer · 8 bytes/param96.0 MB predicted total192.0 MB
Measured on the 4060. Optimizer state is the biggest slice, so a budget that forgets it is off by 2×.
Stage 02 · data

From GitHub files to training batches

The model never sees files. It sees long rows of numbers. This pipeline turns 1.2 million GitHub Python files plus English articles into those numbers, removing anything we cannot legally share or that would let the model cheat on its exam.

GitHub Python 12 shards · 1.2M files 12 GB of text License filter keep MIT, Apache, BSD… 57.7% of bytes kept Split by repo 99.5% train 0.5% val (the exam) Near-dedup −28% train files −40% val files English articles fineweb-edu 726k documents Split by doc id 99.5% train 0.5% val BPE tokenizer 8,192 tokens digits split one by one 4 token files 2.36B tokens 4.4 GB on disk Batch loader 85% code · 15% English every token once/epoch The model Stage 03 onward 730k files ~520k files already quality-filtered text ids 140M tok/s batches (B, T)
Everything solid is built, run on the real data, and tested. The dashed box is the next stage. The loader serves 140M tokens a second, about 1,700 times what the first model will need, so the GPU will never wait for data.
Stage 02 · the main finding

GitHub is full of copies, and they leaked into the exam

We hold back 0.5% of repositories as an exam the model never studies. But people fork projects and copy files between them, so an exam file often had a near-twin in the study material under another project's name.

We measured it by checking whether the rare lines in each exam file (lines that appear in 5 files or fewer across all of GitHub) also show up in the study set. A rare line on both sides means a copy.

Splitting by repository helped a little. Removing near-duplicates with fingerprints (MinHash) helped a lot. We needed both, because each one catches a kind of copy the other misses.

Rare exam lines found in the study set

01020304050%
Two shards, ~119k files. Near-dedup also removed 28% of the study set, files the model would otherwise have read twice.
Stage 01 · tokenizer

How a tokenizer learns its vocabulary

A model can only read numbers, so text has to be cut into pieces called tokens, each with an id. The tokenizer learns which pieces to use by counting. It starts from raw bytes (256 possible values, so any text in any language works) and repeats one move thousands of times.

1 · Text → bytes "é" becomes C3 A9 256 starting tokens 2 · Cut at words "def" " add" "(" " self" merges never cross a cut 3 · Count pairs every two neighbours ("a","l") seen 9 times 4 · Merge the top pair "a"+"l" → new token "al" replace it everywhere 5 · Write it down merge #1: a + l → 256 this list IS the tokenizer repeat until 8,192 tokens (7,936 merges)
Steps 3 to 5 repeat. Each round, the most common neighbouring pair becomes one new token, so common pieces of code like " self" or "return" end up as single tokens.
Try it

Train a tokenizer by hand

This runs the same algorithm as stages/01_tokenizer/bpe.py. The highlighted pieces are the pair that will merge next. Press Merge once and watch them fuse into one blue token. It opens with 12 merges done.

Edit the training text

vocab256
tokens0
bytes / token1.00

Most common pairs

Merges learned, in order

Using the tokenizer

Encoding new text replays the merges in order

Once trained, the tokenizer handles text it has never seen. It starts from bytes and applies the merges it learned, always the earliest-learned one first. This rebuilds the pieces the same way training did.

The steps on the right use the merges from the lab above, so they change as you train more. Type anything.

Stage 01 · the decision

Three ways to cut numbers, and the one we kept

The step that cuts text before merging decides what can never be merged. For numbers there are three common choices. If 2024 is one token, the digit 2 gets a different id depending on which number it sits in, which makes numbers harder for a model to learn.

We trained all three on 36 MB of real Python and English and measured them on code they never saw. Splitting every digit costs only 1.3% in compression and gives every digit a stable id, so that is the one we kept.

Real output of the three patterns on x = 2024 + 1234567; port=8080. The kept rule is marked.
Stage 01 · result

Our small tokenizer reads code better than GPT-2's

A good tokenizer fits more text into each token, so the model sees more code per step. We measured bytes of held-out Python per token.

GPT-2's tokenizer has six times our vocabulary and still loses on code by a wide margin. It was trained on web pages and never learned tokens for runs of spaces, so every space of Python indentation costs it a whole token. On English it still wins, as you would expect.

Doubling our vocabulary to 16,384 would gain 6% more but add 4.2M parameters, about a sixth of the first model. We kept 8,192.

Held-out Python, bytes per token

012345
Measured on 3 MB of Python from repositories the tokenizers never trained on. Blue is the tokenizer the project uses.
Next

Stage 03: from token ids to vectors

  1. 1Give every one of the 8,196 token ids a learned vector, the model's first layer.
  2. 2Tell the model where each token sits in the sequence. Attention cannot see word order on its own, so we will build three ways of adding position and test each.
  3. 3Prove it matters: train a tiny model with no position information and watch it fail to learn word order.
  4. 4Later, before the big 125M run: download about 8 more shards. We have 1.33B code tokens and that model wants about 2.1B.