3Blue1Brown: Transformers and attention, visually
TL;DR. Two chapters of 3Blue1Brown’s neural network series. Chapter 5, “Transformers, the tech behind LLMs” (1 April 2024), shows next-token prediction, embeddings, unembedding and softmax with temperature. Chapter 6, “Attention in transformers, step-by-step” (7 April 2024), builds a single attention head from a made-up “adjectives update nouns” example and then scales to multi-head attention. Both keep a running parameter count with GPT-3’s numbers. Watch Chapter 5 then Chapter 6. Each also has a written lesson on the 3Blue1Brown site (Ch5, Ch6).
Key idea
Section titled “Key idea”Attention is how context changes a word’s meaning. Each token starts with a context-free embedding . A head computes a change and adds it, giving a refined :
Queries ask a question (“any adjectives in front of me?”), keys offer answers, and the value map says what to add. Many heads run in parallel and their changes are summed.
How the chapters map onto this site (timestamps from the video descriptions):
| chapter and time | topic | study page |
|---|---|---|
| Ch5 0:00 | predict, sample, repeat | GPT-3 |
| Ch5 3:03 | inside a transformer | Illustrated Transformer |
| Ch5 12:27 | word embeddings | BPE for the tokens being embedded |
| Ch5 22:22 | softmax with temperature | Self-consistency, which samples with temperature |
| Ch6 4:29 | the attention pattern | Attention Is All You Need |
| Ch6 12:42 | context size grows quadratically | FlashAttention, Lost in the Middle |
| Ch6 15:44 | counting parameters | Kaplan scaling laws |
| Ch6 19:19 | multiple heads | GQA, MLA |
Key results
Section titled “Key results”There are no experiments; the payoff is intuition with exact shapes. GPT-3’s vocabulary of 50,257 tokens and width of 12,288 give 617,558,016 weights each for embedding and unembedding. A head has four 1,572,864-weight matrices, a block has 96 heads, and 96 blocks give just under 58B attention weights. The written lesson notes a correction to the value-down output size (128). Positional encoding is mentioned but not explained, and the MLP blocks are left for the next chapter.
Connections
Section titled “Connections”- Same mechanism, from the paper: Attention Is All You Need and the Illustrated Transformer.
- Positions, which the video skips: RoPE.
- Build it in code: Karpathy’s Let’s build GPT, which the video description recommends.
- The model whose numbers are used: GPT-3.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
Chapter 6 counts attention parameters with GPT-3's numbers: embedding size 12,288, key-query size 128, 96 heads per block and 96 blocks. Reproduce the count per head, per block and for the whole model, and say what share of 175B it is.
What a strong answer covers
- Query and key matrices are each 128 × 12,288 = 1,572,864 weights.
- The value map is factored into a down matrix (128 × 12,288) and an up matrix (12,288 × 128), the same size again, so a head has 4 × 1.57M ≈ 6.3M weights.
- Per block: 96 × 6.3M ≈ 604M. Over 96 blocks: about 58B, roughly a third of 175B.
- Embedding and unembedding add about 617M each (50,257 × 12,288); most of the rest sits in the MLP blocks.
- Mechanism
The video trains on every prefix of a passage at once and masks the attention pattern. Explain why both are needed, how masking is done, and why the video's grid is normalised by column while most code normalises by row.
What a strong answer covers
- Each position predicts its next token, so one passage gives as many training examples as it has tokens.
- If later tokens could influence earlier ones, they would give away the answers, so their scores are set to minus infinity before the softmax and become zero weight.
- The attention pattern has one row and one column per token, so its size grows with the square of the context length.
- The video writes K transpose Q with queries as columns, so softmax runs down each column. The paper and most code write Q K transpose with queries as rows, which is the same computation transposed.
- Connect
The video factors each head's value map into a down-projection and an up-projection and adds the heads' proposed changes. Show how this matches the paper's W^V and W^O, and why the low-rank view matters for later designs.
What a strong answer covers
- The down matrix is the paper's per-head W^V. The up matrices of all heads, side by side, form the output matrix W^O.
- Concatenating head outputs and multiplying by W^O equals summing each head's up-projected change, which is the video's picture.
- Each head's value map therefore has rank at most 128 (the head size), a view the video credits to Anthropic's Transformer Circuits work.
- Designs like grouped-query attention and multi-head latent attention also exploit low-rank or shared key and value maps to shrink the KV cache.