Skip to content

3Blue1Brown: Transformers and attention, visually

optionalextendedfundamentalvideorubric 8/122024 · ~55 min

Grant Sanderson (3Blue1Brown)

Status

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).

Attention is how context changes a word’s meaning. Each token starts with a context-free embedding E⃗\vec E. A head computes a change ΔE⃗\Delta\vec E and adds it, giving a refined E⃗′\vec E':

ΔE⃗j=∑isoftmax⁡i ⁣(K⃗i⋅Q⃗jdk) W↑W↓E⃗i.\Delta \vec E_j=\sum_i \operatorname{softmax}_i\!\left(\frac{\vec K_i\cdot\vec Q_j}{\sqrt{d_k}}\right)\, W_{\uparrow}W_{\downarrow}\vec E_i .

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 timetopicstudy page
Ch5 0:00predict, sample, repeatGPT-3
Ch5 3:03inside a transformerIllustrated Transformer
Ch5 12:27word embeddingsBPE for the tokens being embedded
Ch5 22:22softmax with temperatureSelf-consistency, which samples with temperature
Ch6 4:29the attention patternAttention Is All You Need
Ch6 12:42context size grows quadraticallyFlashAttention, Lost in the Middle
Ch6 15:44counting parametersKaplan scaling laws
Ch6 19:19multiple headsGQA, MLA

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.

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.

  1. 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.
  2. 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.
  3. 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.

Done with this page?

Status