tiny-gpt: a transformer written from scratch

A decoder-only transformer in numpy with hand-written backpropagation, trained on a laptop-class CPU. This page runs the same weights in plain JavaScript so you can watch what it attends to and try to break it.

Loading model...

01Generate and see how sure the model was

Each token is shaded by the probability the model gave it: pale means it expected it, strong colour means it was surprised. Hover a token to see where it looked; click to pin it.

Nothing generated yet. Press Generate to continue the prompt, or pick an example. The model is small, so expect text that sounds like Python documentation but wanders.

02Inside the model

Left: the attention pattern of one head. Arcs show where the hovered token reads from; the grey triangle is the causal mask, the part of the sequence a token is never allowed to see. Right: what the model predicted at that position.

Layer
Head
No attention to show yet. Generate some text above and every layer and head will appear here.
Next-token distribution
model, temperature 1what the sampler uses
Hover or click a token to see the model's top guesses at that position.

03Try to break it

The model learned positions 0 to 127 and nothing else. Ask it to go further and choose what the code does. Then compare temperature 0 with a hot sampler on the same prompt and seed.

A. Run past the trained context

Choose a policy and press Run. Try unguarded, then refuse.
B. Temperature 0 against temperature 2

Same prompt, same seed, same 60 tokens. Only the sampler changes.

Press Compare to run both.

04Evidence

Every number here is read from docs/results.json, written by python -m tiny_gpt.train, or computed live in this page.

Validation loss during training, against two baselines
Held-out documents

JavaScript against Python
Checking...