← Attention, Demystified: From Dot Products to Explaining Transformers
Week 2, Part 2: The Full Model and the Explanation

Rehearsing the Explanation

Deliver a five-minute whiteboard explanation of attention using one worked example sentence, and answer three likely follow-up questions (why not RNNs, what the heads learn, why it scales quadratically with sequence length).

Rehearsing the Explanation

You already know the mechanics. This lesson is about compression and delivery: turning what you know into five minutes of speech and one static whiteboard, then holding up under questions.

Two rules for the whole exercise:

  1. One example sentence, carried all the way through. Never introduce a second one. Every abstraction you mention must point back to the same nine words.
  2. The board is a destination, not a slideshow. By minute five the board should contain one finished picture your colleague can photograph. Don't erase.

The board layout

Draw this skeleton first, in about 20 seconds, before you say anything mathematical. Fill in the numbers as you talk.

  The  animal  crossed  the  street  because  IT  was  tired
                 |                 |                  ^
                 |                 |                  |
   ┌─────────────┴─────────────────┴──────────────────┘
   │
   │   token  →  x  →  ┌ q = x W_Q ┐
   │                    │ k = x W_K │   (3 learned projections)
   │                    └ v = x W_V ┘
   │
   │   score = q · k        weight = softmax(score / √d)
   │
   │   out(IT) = Σ weight_j · v_j
   │
   └─  [ scores table ]        [ softmax weights ]

Leave the right third of the board empty. That is where multi-head and the stack of layers go at minute four.


The script

Sentence: The animal crossed the street because it was tired.

0:00 – 0:40 — The problem

"To process the word it, the model has to know it refers to animal, not street. Nothing about the token it itself tells you that — the information is somewhere else in the sentence. So we need a mechanism that lets a token pull in information from other positions. That's all attention is: a learned, differentiable way for one position to look at other positions and copy in a blend of what it finds."

Circle IT and animal on the board. Draw the arrow.

0:40 – 1:30 — Three vectors per token

"Every token starts as a vector — say 4 numbers here, 768 or 4096 in a real model. From that one vector we make three, by multiplying by three learned matrices:

  • query — what am I looking for?
  • key — what do I advertise about myself?
  • value — what do I actually hand over if you pick me?

Same input vector, three different learned projections. Think of a room of people: your query is the question you're shouting, your key is the name tag on your chest, your value is the thing you say if someone walks over."

Write q=xWQq = xW_Q, k=xWKk = xW_K, v=xWVv = xW_V. Say the word learned out loud — it is the thing people forget.

1:30 – 3:00 — The worked numbers

Now pretend the four dimensions mean something. Say explicitly: "I'm making these numbers up so the arithmetic fits on a board; the real ones aren't interpretable like this."

Label the axes [animate, subject-ish, place, action].

qit=[ 1, 1, 0, 0 ]"I’m a pronoun hunting for an animate subject"q_{\text{it}} = [\,1,\ 1,\ 0,\ 0\,] \quad \text{"I'm a pronoun hunting for an animate subject"}

kanimal=[ 4, 4, 0, 0 ]kstreet=[ 1, 3, 3, 0 ]kcrossed=[ 0, 2, 0, 4 ]k_{\text{animal}} = [\,4,\ 4,\ 0,\ 0\,] \qquad k_{\text{street}} = [\,1,\ 3,\ 3,\ 0\,] \qquad k_{\text{crossed}} = [\,0,\ 2,\ 0,\ 4\,]

Dot products (do this arithmetic out loud, slowly — it is the single most convincing moment of the talk):

q⋅kanimal=4+4=8,q⋅kstreet=1+3=4,q⋅kcrossed=0+2=2q\cdot k_{\text{animal}} = 4+4 = 8, \qquad q\cdot k_{\text{street}} = 1+3 = 4, \qquad q\cdot k_{\text{crossed}} = 0+2 = 2

"The dot product is just an alignment score — big when two vectors point the same way. Now divide by d=4=2\sqrt{d} = \sqrt{4} = 2, because with big dd these sums grow and shove the softmax into a corner where gradients vanish."

4,2,14,\qquad 2,\qquad 1

Softmax:

e4≈54.6,e2≈7.4,e1≈2.7⇒sum≈64.7e^4 \approx 54.6,\quad e^2 \approx 7.4,\quad e^1 \approx 2.7 \quad\Rightarrow\quad \text{sum} \approx 64.7

0.84  animal,0.11  street,0.04  crossed\boxed{0.84\ \ \text{animal}, \qquad 0.11\ \ \text{street}, \qquad 0.04\ \ \text{crossed}}

"Softmax turns scores into a probability distribution over positions — everything positive, summing to one. It's soft: not 'fetch record #2' but 'fetch 84% of that one and a bit of the others.' That softness is why the whole thing is differentiable and why we can train it."

Finally the values. Let vanimal=[1,0]v_{\text{animal}} = [1,0], vstreet=[0,1]v_{\text{street}} = [0,1], vcrossed=[0,0]v_{\text{crossed}} = [0,0] on axes [animate-thing, place]:

outit=0.84 [1,0]+0.11 [0,1]+0.04 [0,0]=[ 0.84, 0.11 ]\text{out}_{\text{it}} = 0.84\,[1,0] + 0.11\,[0,1] + 0.04\,[0,0] = [\,0.84,\ 0.11\,]

"The vector sitting at position it after this layer is now 84% 'animal-like'. The pronoun has absorbed its referent. That's one attention head, one layer."

3:00 – 3:40 — All at once

"Every token does this simultaneously — each one is a query against every key. Stack the queries into a matrix QQ, keys into KK, values into VV, and the whole layer is one line:"

Attention(Q,K,V)=softmax ⁣(QK⊤dk)V\text{Attention}(Q,K,V) = \text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

"QK⊤QK^\top is the n×nn \times n table of every-token-against-every-token. Softmax runs along each row. In a language model that generates text we mask the upper triangle to −∞-\infty before the softmax, so position ii can't see the future — otherwise predicting the next word is cheating."

3:40 – 5:00 — Heads, blocks, stack

Use the empty right third now.

"One problem: that whole layer is one weighted average, so it can only chase one kind of relationship at a time. Fix: run hh copies in parallel with different WQ,WK,WVW_Q, W_K, W_V — heads — each at a smaller dimension, then concatenate and project back. One head can track coreference, another can just look at the previous word.

Wrap that with a residual connection and a per-token MLP, and you have a transformer block. GPT-style models are that block stacked 12 to 100+ times, with position information added at the bottom since attention itself is order-blind — a permuted sentence gives permuted outputs. Final vector at the last position → linear layer → softmax over the vocabulary → next token."

Stop. Five minutes.


Deliberately cut

Say these only if asked: layer norm placement, the exact positional scheme (RoPE/ALiBi/learned), why WOW_O exists, cross-attention in encoder–decoder models, dropout, the 4× MLP width. If pressed on something you've cut: "That's an implementation detail that doesn't change the picture — happy to go into it after."


The three follow-ups

"Why did this replace RNNs?"

Ten-second answer: Parallel training and a constant-length path between any two tokens.

The real answer: An RNN's hidden state at step tt requires step t−1t-1, so a length-nn sequence is nn sequential steps — you can't saturate a GPU no matter how much data you have. Attention computes all positions at once during training; it's a couple of big matmuls. Second, in an RNN, information from token 1 reaching token 100 passes through 99 multiplications, which is where the classic vanishing/exploding gradient trouble comes from. In attention, every pair is one dot product apart — path length O(1)O(1) instead of O(n)O(n).

If they push: "Wasn't the win just scale?" — Largely yes, and that's the point: the architecture is the one that lets you spend the compute. And note RNNs aren't dead: state-space models (Mamba, etc.) are a modern recurrent family designed to be trainable in parallel, motivated exactly by attention's quadratic cost. Also be honest that generation is still sequential in a transformer — one token at a time, which is why KV caching exists.

"What do the individual heads learn?"

Ten-second answer: Nobody assigns them; specialization emerges. Some are cleanly interpretable, many aren't.

The real answer: Interpretability work has found reliably recurring types — previous-token heads that attend one position back, positional/local heads, syntactic heads that link verbs to their subjects, and induction heads: a two-head circuit implementing "last time I saw A, it was followed by B, so predict B." That circuit is a big part of in-context learning. In the well-studied indirect-object task, specific "name mover" heads move the name from earlier in the sentence to the final position — which is essentially my it → animal cartoon, but discovered rather than designed.

The caveat to volunteer yourself (it makes you credible): most heads are messy and polysemantic, many are redundant and can be pruned, and a high attention weight is not proof of causal importance — "attention is not explanation" is a real literature. Attention weights are a hypothesis to test with ablations, not an explanation.

"Why is it quadratic in sequence length?"

Ten-second answer: nn queries times nn keys.

The real answer: QK⊤QK^\top has n2n^2 entries and each costs dd multiply-adds, so attention is O(n2d)O(n^2 d) FLOPs and — naively — O(n2)O(n^2) memory per head for the score matrix. At n=1000n = 1000 that's a million scores per head per layer; at n=100,000n = 100{,}000 it's ten billion. Doubling context quadruples the attention work.

The nuance that gets you respect: attention is not always the bottleneck. Per layer, attention is roughly ∝n2d\propto n^2 d while the MLP is roughly ∝nd2\propto n d^2, so attention only dominates once nn approaches dd — for a 4096-dim model at 512 tokens, the MLPs cost more. It's long contexts that hurt. Also, the n2n^2 memory is avoidable: FlashAttention tiles the computation so the full matrix is never materialized (same FLOPs, far less HBM traffic). The FLOPs are attacked separately by sliding-window/local attention, sparse patterns, linear-attention approximations, and non-attention sequence models. And at inference, the KV cache grows linearly with context — each new token attends to all previous ones, so generating nn tokens is again O(n2)O(n^2) total.


Phrases that will get you in trouble

| Don't say | Say instead | |---|---| | "Q, K, V are three different embeddings" | "Three learned projections of the same vector" | | "Softmax picks the most relevant token" | "Softmax produces a weighted blend, usually peaked" | | "We divide by d\sqrt{d} to normalize" | "To keep the scores' variance stable so softmax doesn't saturate" | | "Attention shows you what the model is thinking" | "Attention shows where information could flow; proving it matters takes ablation" | | "Transformers have no recurrence, so they're order-invariant" (and stopping there) | "...which is why positional information is injected explicitly" |


Try it yourself

Set a phone timer for five minutes and record audio while you talk at a real wall or a sheet of paper. Then check three things:

  1. Did you finish? If you ran past 5:00, you almost certainly over-explained WQ,WK,WVW_Q, W_K, W_V. Cut minute two to 40 seconds.
  2. Did you say the numbers out loud? Play back the 8 / 4 / 2 → 4 / 2 / 1 → 0.84 / 0.11 / 0.04 segment. If you mumbled it or wrote it silently, redo it — this is the part that converts.
  3. Cold-open a follow-up. Without re-reading this page, speak a 30-second answer to "why does it scale quadratically?" Then compare against the section above and note exactly which fact you dropped.

Then do the hardest version: give it to someone who will actually interrupt, and write down the one question you couldn't answer. That question is your next lesson.

Confused by anything? Highlight it and hit “Go deeper”.

Watch

Test yourself