Transformer Decoder Explained

/about

About this project

This is a graphical, interactive explainer of how a decoder-only transformer runs, and how it is trained, in two sites of the Foundations track. Decoder (/learn): type your own tokens, watch every operation execute, and sample the next token end-to-end. Training (/training, at its own address): train that same model, from the same initial weights, on a public-domain book in your browser, and follow the loss, the gradients and the optimiser step by step. Every matrix you see is the actual value the model computed — the visualisations are not a separate model.

Decoder’s TypeScript transformer is checked against PyTorch fixtures, operation by operation, to within 1e-5, and the animations’ frames against a float64 PyTorch run of the same weights: by default Training’s trained weights, with the seeded random initialiser one click away. Training’s trainer has a hand-written backward pass for every operation. A NumPy reference of it matches PyTorch autograd and finite differences, its optimisers match torch.optim, and the TypeScript trainer matches the reference step for step (the checks and their tolerances are in results.md).

It exists for two readers. End users learning the Transformer get a step-by-step walk-through with three layers of depth (concept, maths, code) they can toggle inside any section. Source-readers get a real-world Next.js full-stack codebase that reads like a textbook, with shapes, formulas, and references in the JSDoc of every operation.

Runs to come

Every number in the Training chapters comes from the model that runs in your browser, or from its Python reference. Six chapters also link a notebook that repeats the experiment at a larger scale on a GPU. Those runs are planned; when one is done, its output goes into the repository and its numbers into the chapter.

Built with

References

Licence

MIT. Use the code as a learning resource, fork it, ship it. See LICENSE for the text.

Terms across the sites

A few terms (goodput, ρ, the pipeline bubble) mean different things on different companion sites; the shared definitions give each one definition and one formula, and say where a site differs.

→ Decoder · Training

Built by Brendan Lynskey