← ai lobe

LLM from Scratch #0: The Roadmap

2026-11-212 min read#llm#rust#llm-from-scratch#machine-learning

LLM from Scratch · part 0 · all parts
  1. 0LLM from Scratch #0: The Roadmap
  2. 1LLM from Scratch #1: Knobs and Nudges

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.

One step of an LLM
1 · text"the cat sat on the"
2 · tokens
the cat sat on the
each becomes an id like 262
3 · modelA big math expression with ~124M knobs
4 · next-token probabilities
mat0.41
floor0.17
sofa0.12
roof0.06
illustrative numbers
5 · loopPick " mat", append it, go back to 1.
  • 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:

  1. The idea in plain words, with no symbols you haven’t met yet.
  2. An interactive to play with until the idea feels obvious.
  3. A quiz, with answers hidden until you click.
  4. The Rust: we write it, test it against the math, and the next part builds on it.

The lesson plan

Lesson plan
partlessonconceptswhat we build in Rust
1Knobs and Nudgesderivative, chain rule, backprop, gradient descentautograd.rs: a tape of values with + and ×, gradient checks
2A Brain Cellloss, neuron, nonlinearity, MLP, training loopmore ops (tanh, pow, neg), Neuron / Layer / MLP, a full training loop
3Counting Lettersprobability, softmax, cross-entropy, samplingour own random number generator; a bigram model that writes made-up names
4Tensorsshape, strides, matmul, broadcastingtensor.rs: an n-d array on one flat buffer
5Tensor Autogradbackward of matmul / sum / softmax, batchingthe tape again, where every node is a whole tensor
6Embeddingsembedding table, context window, initialisation, numerical stabilityan MLP language model that reads the last few characters
7Tokenizerbytes, BPE merges, encode / decodebpe.rs: learn merges from our own text
8Attentionquery / key / value, causal mask, multi-heada self-attention layer, checked against the nudge test
9Transformerpositions, residuals, LayerNorm, the block, GPTgpt.rs: the full model
10Train ItAdamW, learning-rate schedule, validation, generationtrain on tiny-shakespeare and generate text
11Make It Fastcache-friendly loops, SIMD, threads, KV cachea 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.

Concept map · click a concept
covered so farneeded firstunlocked by it

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.

Connected thoughts

neurons this one fires into: shared ideas first, then neighbours in time.