/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.
- Chapter 7: a µP width sweep, widths 128 to 1,024 under both parameterisations (notebook, about an hour on an A100).
- Chapter 8: baseline, QK-norm and z-loss on a 6-layer, width-512 model (notebook, about 45 minutes on an A100).
- Chapter 9: FP32, BF16, FP16 with and without a GradScaler, and FP8 emulation on a 60M-parameter model (notebook, about 40 minutes on an A100).
- Chapter 10: peak memory and step time with and without activation checkpointing (notebook, about 10 minutes on any GPU).
- Chapter 14: an IsoFLOP sweep on GPTs of 1–40M parameters (notebook, about an hour on an A100).
- Chapter 15: throughput, MFU and an exact checkpoint resume (notebook, about 10 minutes on any GPU).
Built with
- Next.js 14 (App Router)
- TypeScript (strict)
- Tailwind CSS
- D3.js
- Drizzle ORM + Postgres
- Auth.js v5
- next-mdx-remote
- Vitest + Playwright
References
- Vaswani et al., 2017 — Attention Is All You Need. The original Transformer paper. The pre-norm decoder block, sinusoidal positional encoding, and scaled dot-product attention in this codebase all follow this paper directly.
- Radford et al., 2019 — Language Models are Unsupervised Multitask Learners (GPT-2). The decoder-only architecture and pre-LayerNorm pattern follow this work.
- Hendrycks & Gimpel, 2016 — Gaussian Error Linear Units (GELUs). The tanh approximation used in `lib/transformer/gelu.ts`.
- Andrej Karpathy — nanoGPT and “Let's build GPT”. Reference implementations checked against during development.
- Kingma & Ba, 2014 — Adam: A Method for Stochastic Optimization, and Loshchilov & Hutter, 2017 — Decoupled Weight Decay Regularization (AdamW). The optimisers in `lib/training/optimizers.ts`.
- Sennrich, Haddow & Birch, 2015 — Neural Machine Translation of Rare Words with Subword Units. Byte-pair encoding, in `lib/training/bpe.ts`.
- Lewis Carroll, 1865 — Alice’s Adventures in Wonderland (Project Gutenberg eBook #11, public domain). The Training site’s text, lower-cased to the 64-character alphabet.
- Narayanan et al., 2021 — Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM, and Grattafiori et al., 2024 — The Llama 3 Herd of Models. The published runs the training-systems model (`lib/training/systems.ts`) is checked against.
- Peng et al., 2024 — DeMo: Decoupled Momentum Optimization, and Douillard et al., 2023 — DiLoCo: Distributed Low-Communication Training of Language Models. Training over slow links, in `lib/training/slowlinks.ts`.
- 3blue1brown — Neural Networks series. Pedagogical inspiration for the visual intuition layer.
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.
Built by Brendan Lynskey