Inside the Transformer - A Better Illustrated Transformer
This is a plain decoder-only transformer, the kind behind GPT-style language models, drawn with GPT-2 small's sizes. Scroll and a short sentence moves through it one stage at a time. Tap any part of the diagram, or a card further down, to go inside it: the tokenizer, the residual stream, every attention head with its QK and OV circuits, the MLP and the sampler.
Go inside any part
Notes
- The sizes are GPT-2 small's (124 million parameters): 12 layers, 12 heads of 64 numbers each, a width of 768, an MLP of 3,072, a vocabulary of 50,257 tokens and a context of 1,024. Like GPT-2, it learns its position vectors, puts a LayerNorm before each sub-layer and one more at the end, uses GELU, and reuses the embedding matrix for the output. Sources: the GPT-2 paper and its model page.
- The numbers inside the drawings (vectors, scores, probabilities and the logit-lens readings) are made up so that each part behaves the way it really does. The formulas are real and are computed live in your browser.
- Head numbers such as 2.2, 4.11 (previous token), 0.1, 3.0 (duplicate token) and 5.5, 6.9 (induction) are heads that Wang et al. (2022) found doing these jobs in GPT-2 small. The other heads' patterns are drawn for illustration.
- The QK and OV circuit view comes from Elhage et al. (2021), and induction heads from Olsson et al. (2022).