The attention mechanism
How a token decides which other tokens in the sequence actually matter to it.
Embeddings give every token a starting vector, but that vector alone can't know anything about context — the word "bank" needs a different meaning next to "river" than next to "loan". Attention is the mechanism that lets every token pull in information from every other token before deciding what it means.
Intuition
Think of attention as each token asking the sequence: "given who I am, which of you are relevant to me, and by how much?" — then blending in a weighted mix of everyone's information accordingly.
Query, key, value
Every token produces three vectors from its embedding, via three learned weight matrices:
- Query () — what this token is looking for
- Key () — what this token offers, for others to match against
- Value () — the actual information this token contributes if selected
A token's query is compared against every other token's key to produce a relevance score, which is then used to weight that token's value:
The term just keeps the dot products from growing too large as the vector dimension increases — without it, softmax would saturate and gradients would vanish during training.
One attention head: how much each token (row) pulls from each token (column)
attention("sat" → "cat") = 0.19 (score 0.14 → softmax)
toy vectors for illustration — a real head's pattern is learned, not designed
Two things to notice while playing with the grid. First, each row sums to exactly 1 — softmax always produces a probability distribution, so a token's attention is a budget it spends across the other tokens. Second, toggle the scaling off: the same scores produce much sharper weights, which is exactly the saturation problem the scaling prevents during training.
Careful
It's easy to read that formula and think attention is doing something exotic. Mechanically it's one matrix multiply, one softmax, and another matrix multiply. The power comes from doing this across many heads and many layers, not from any single step being complicated.
Why multiple heads
A single attention computation can only learn one notion of "relevance." Real models run many attention computations in parallel — heads — each with its own , , projections, so the model can track several kinds of relationships at once (e.g. one head tracking grammatical subject-verb agreement, another tracking coreference across a paragraph).
# simplified single-head attention
def attention(Q, K, V):
scores = Q @ K.T / sqrt(d_k)
weights = softmax(scores, axis=-1)
return weights @ VThe dimension arithmetic is worth internalizing, because it explains why heads are nearly free:
The model's dimension is split between heads, not duplicated: 12 heads at each operate on 64-dimensional sub-vectors, and their outputs are concatenated back to 768 before the output projection. Total QKV parameters stay the same as one big head would cost — you're trading "one high-capacity relevance map" for "12 specialists at lower resolution each." Empirically the specialists win — with one honest caveat: per-head ablations show many heads are redundant or prunable with negligible loss; only a handful per layer are load-bearing, but those carry relationships (coreference, positional copying, induction — a term the interpretability chapter defines) that no single high-capacity head provides. Head-level patterns were readable with simple probing years before heavyweight mechanistic-interpretability tooling existed.
Note
Each head's attention pattern (its heatmap) tends to be far sparser and more semantic than the all-averaged picture. The heads you just played with are one head's pattern; a real layer's output is the concatenation of a dozen such specialists, each spending its own attention budget.
Switch between four specialists on the same sentence:
One layer's output = the concatenation of specialist heads
each token looks one step back — a positional copying habit
Four heads, four completely different relevance maps over the same six tokens — all computed in parallel and concatenated. Switch heads and the pattern flips wholesale, because each has its own learned Q/K/V projections. (These rules are hand-designed for legibility; real heads' patterns are learned and frequently redundant — only a handful per layer are load-bearing.)
This lesson has exercises attached — working through the score computation by hand for a 3-token sequence — once the exercises layer ships.
Next: the transformer block — the repeatable unit these attention heads live inside.