Tiny GPT
A transformer with 1,818,048 parameters, trained from scratch on Pan Tadeusz. It writes the verse beside this text one letter at a time, in your browser, with inference code I wrote without a machine learning library.
Its logits match PyTorch to within 6.7e-6. Click any letter of the poem to see what the model did to produce it.
Loading weights, 0% of 3.6 MB
Look inside the modelUnderlined letters were long shots: the model picked them with under 25% probability.
The model weighs these for the next letter.
- characters per second (model time only, on this device)
One letter, start to finish
Every number below was computed by the model in this tab for the letter you picked in the poem. It shows what the model read, which of its 16 attention heads listened to which letters, and what each part pushed towards the answer.
The plate opens as soon as the model has read the first line.
Sixteen habits
Training gave each attention head its own way of reading. I measured them on 48 windows of the poem the model never saw: how far back each one looks, and how much worse the model gets when that head is switched off. The maps show what each head did for the letter you inspected.
zeroed: +0.016 nats per letter
zeroed: +0.004 nats per letter
zeroed: +0.017 nats per letter
zeroed: +0.004 nats per letter
zeroed: +0.006 nats per letter
zeroed: +0.004 nats per letter
zeroed: +0.006 nats per letter
zeroed: +0.089 nats per letter
zeroed: +0.009 nats per letter
zeroed: +0.009 nats per letter
zeroed: +0.008 nats per letter
zeroed: +0.007 nats per letter
zeroed: +0.004 nats per letter
zeroed: +0.007 nats per letter
zeroed: +0.005 nats per letter
zeroed: +0.006 nats per letter
Hover a map. Rows are the letter doing the reading, columns the letter it reads; the same cell lights up in all 16 heads.
Zeroing head 2.4 (word boundaries) raises the loss by 0.089 nats per letter, 5.3 times the damage of the next most important head.
Temperature
Temperature rescales the model's probabilities before it draws a letter. Low values make it take the favourite almost every time, high values let long shots through. Three writers read your opening line below and draw from the same random numbers, so the only difference between them is the temperature.
T = 0.3
- Uncertainty per letter
- -
- Repeated 4-letter runs
- -
T = 0.8
- Uncertainty per letter
- -
- Repeated 4-letter runs
- -
T = 1.4
- Uncertainty per letter
- -
- Repeated 4-letter runs
- -
The line under each poem is the model's uncertainty, in bits, for each letter it chose. The pace of writing is slowed to reading speed.
Break it
A test is only worth something if it fails when the code is wrong. Two classic transformer bugs are built into this model behind a switch. Dropping the 1/√d factor costs 0.25 nats per letter on held-out text; letting attention see one slot ahead costs 0.004, which no reader would notice in the verse. The parity check against PyTorch catches both.
Attention scores are not divided by the square root of the head size. Scores grow, softmax saturates, heads stop mixing.
Intact model
same dice, same opening, same temperature
- Loss on held-out text, 48 windows
- 1.620 nats
- Uncertainty per letter
- -
- Repeated 4-letter runs
- -
Drop the 1/√d factor
broken on purpose
- Loss on held-out text, 48 windows
- 1.866 nats (+0.246)
- Uncertainty per letter
- -
- Repeated 4-letter runs
- -
- fp32 weights from the test suite5.7e-6within the limit
- fp16 weights, test run from the test suite6.7e-6within the limit
- This page, intact not measured yet-
- Without the 1/√d factor from the test suite7.2e+0fails the limit
- Attention sees the future from the test suite3.1e+0fails the limit
The evidence
The training run, where the parameters sit, the test suite and the speed on this device.
Training
3,200 steps of 64 windows of 128 letters, AdamW with a cosine schedule, dropout 0.2, on the CPU of a Ryzen 5 7600X in 38 minutes. PyTorch is used for training and for nothing else.
At step 0 the loss is 4.33, which is guessing at random over 71 letters (ln 71 = 4.26). It ends at 1.666 on text it never trained on, as unsure as choosing between 5.3 equally likely letters.
Training loss ends at 1.434, so the model has started to memorise: the gap between the two lines is 0.23 nats and it widens at every checkpoint.
step 3,200 after 38 min of CPU time validation 1.666 training 1.434
Where 1,818,048 parameters sit
- MLP1,183,48865.1%
- Attention592,89632.6%
- Position table24,5761.4%
- Letter table, also the output layer13,6320.7%
- LayerNorm3,4560.2%
Counted from the shape of the network: width 192, 4 blocks, 4 heads, 71 letters, 128 positions. The sum is checked against PyTorch's own count in the tests.
56 of 56 tests pass
- parity.test.ts 5 tests
- TypeScript against PyTorch logits at every position of four inputs, and every attention map of one verse line, for fp32 and shipped fp16 weights. Two deliberately broken variants must fail the same check.
- inspect.test.ts 17 tests
- The traced step equals the plain step. The logit lens at the last stage is the real prediction. Every part adds up to the residual stream and to the logit. A reused cache gives what a fresh one gives, and a cache that lies is caught.
- lanes.test.ts 6 tests
- Writers with the same settings write the same text, another seed or a broken model does not, a cold writer repeats itself more than a hot one.
- lib.test.ts 20 tests
- CSS colours equal canvas colours and text contrast clears 4.5, head roles follow from the measured profiles, the parameter count equals the one PyTorch reported, captions agree with the raw arrays, and the fault losses quoted on the page are re-measured.
- units.test.ts 8 tests
- fp16 decoding, top-k and temperature, seeded sampling, the sliding context window, attention rows as probability distributions.
| Input | Letters | fp32 | fp16 shipped |
|---|---|---|---|
| One letter | 1 | 1.9e-6 | 1.2e-6 |
| A short prompt | 21 | 5.7e-6 | 4.8e-6 |
| One verse line | 50 | 5.7e-6 | 5.7e-6 |
| A full 128-letter window | 128 | 5.7e-6 | 6.7e-6 |
Speed
Writes 400 letters at full speed inside the worker and times it.
Reference run: 361 letters per second, headless Chrome on a Ryzen 5 7600X desktop (median of 7 runs, production build). The weights are 3.6 MB as fp16 and the model reads one position at a time from a key-value cache, so a new letter costs one pass instead of a pass over the whole window.