In this series we build a GPT from nothing, in Rust, with zero dependencies. No PyTorch, no NumPy, no ndarray. We write the random number generator, the tensors, the autograd engine and the transformer ourselves.
Why Rust instead of Python? In Python, one line of PyTorch hides most of what this series is about. Here nothing is hidden. If a gradient flows somewhere, it’s because we wrote the line that sends it there.
Every part is interactive. You can drag the knobs, step through backprop one box at a time, and check every gradient by hand.
What an LLM actually does
An LLM does exactly one thing: given the text so far, give a probability for every possible next token.
the cat sat on the- Writing is that, in a loop: pick a token, append it, ask again.
- Training is that, plus a nudge. Say the real next token was “ mat”, and the model gave it 0.41. The loss is
−log(0.41) = 0.89. Backprop works out which way each of the 124 million knobs should turn to push 0.41 up, and every knob takes a small step.
Everything in this series serves one of those two loops.
How each part works
Each part follows the same four steps:
- The idea in plain words, with no symbols you haven’t met yet.
- An interactive to play with until the idea feels obvious.
- A quiz, with answers hidden until you click.
- The Rust: we write it, test it against the math, and the next part builds on it.
The lesson plan
| part | lesson | concepts | what we build in Rust |
|---|---|---|---|
| 1 | Knobs and Nudges | derivative, chain rule, backprop, gradient descent | autograd.rs: a tape of values with + and ×, gradient checks |
| 2 | A Brain Cell | loss, neuron, nonlinearity, MLP, training loop | more ops (tanh, pow, neg), Neuron / Layer / MLP, a full training loop |
| 3 | Counting Letters | probability, softmax, cross-entropy, sampling | our own random number generator; a bigram model that writes made-up names |
| 4 | Tensors | shape, strides, matmul, broadcasting | tensor.rs: an n-d array on one flat buffer |
| 5 | Tensor Autograd | backward of matmul / sum / softmax, batching | the tape again, where every node is a whole tensor |
| 6 | Embeddings | embedding table, context window, initialisation, numerical stability | an MLP language model that reads the last few characters |
| 7 | Tokenizer | bytes, BPE merges, encode / decode | bpe.rs: learn merges from our own text |
| 8 | Attention | query / key / value, causal mask, multi-head | a self-attention layer, checked against the nudge test |
| 9 | Transformer | positions, residuals, LayerNorm, the block, GPT | gpt.rs: the full model |
| 10 | Train It | AdamW, learning-rate schedule, validation, generation | train on tiny-shakespeare and generate text |
| 11 | Make It Fast | cache-friendly loops, SIMD, threads, KV cache | a fast matmul, benchmarks, quick generation |
Every concept, and what depends on what
There are 32 concepts between “what is a derivative” and “a GPT that writes Shakespeare”. Click any of them below. Amber lights up everything you need to know first, and blue lights up everything it unlocks. GPT starts out selected, so the amber you see first is the whole series.
The only maths you need before starting is school maths: the dot product, matrix multiplication, exp and log, and what a probability is. Everything else gets built from the ground up.
The code lives at github.com/spy16/llm-from-scratch. Start with Part 1: Knobs and Nudges.