Highlights

Contents

Part IX
09

Multi-Head Attention

One attention pattern may mix incompatible relationships. Several heads form separate comparisons. Their outputs combine afterward, and shared KV heads reduce the history they retain.

YOU ARE HERE"raw text"tokenizerembeddings+ positionslearnedwavesRoPEDECODER BLOCK× NnormattentionQKVsoftmaxmaskheadsn × n+normMLP+residual streamKV cachefinal normLM headsoftmaxP(next)training: the loss sends a correction back to every weight

#Section 48One Softmax, Two Needs

Look at it in The cat sat because it was tired once more. To build a good vector for it, the model wants at least two things. It wants the antecedent, cat, to know what it refers to. It also wants the word because, to know that it is the subject of a new clause explaining something. Those are two different reasons to look at two different tokens.

A single attention computation produces one set of weights per token, and the weights must sum to one. If one head tries to serve both needs, it has to split its budget: roughly half on cat and half on because. The output is then a fifty-fifty blend of cat’s value and because’s value, a muddy average that is neither “the cat” nor “a reason clause” and is harder for later layers to use than either clean signal.

“IT” NEEDS TWO THINGS AT ONCE: ITS ANTECEDENT AND THE CLAUSE WORD BEFORE ITone head: one budget, splitThecatsatbec.ittwo heads: one job eachThecatsatbec.itoutput: a muddy average of "cat" and "because"head 1 gets "cat", head 2 gets "because", both sharp
Figure 1.65One head must split a single budget between two needs, giving a blurred average. Two heads can each take one job and give two sharp answers.

Multi-head attention fixes this by running several smaller attention computations side by side, each with its own queries, keys and values. Each one is called a head. One head can learn to resolve pronouns while another tracks clause structure, and a third tracks something else entirely. Their outputs are combined at the end. Every real Transformer uses this: GPT-2 small has 12 heads per layer, Llama 3 8B has 32, Finch-19 has 8.

#Section 49Split the Width, Do Not Multiply It

Heads do not make the model bigger. Finch-19’s query for one token is 512 numbers long. With 8 heads, those 512 numbers are simply cut into 8 slices of 64. Head 0 uses numbers 0 to 63, head 1 uses 64 to 127, and so on. The same happens for keys and values. Each head runs the whole Part VII formula on its own slice, with dk=64d_k = 64.

dhead=dmodelH=5128=64d_{\text{head}} = \frac{d_{\text{model}}}{H} = \frac{512}{8} = 64

In code, the split is two reshape operations. After the projection, q has shape (B, T, 512). Viewing it as (B, T, 8, 64) cuts the last dimension into 8 heads of 64. Transposing to (B, 8, T, 64) moves the head dimension before the token dimension, so that each head's T×64T \times 64 block sits together and the attention formula can run on all 8 heads in one batched matrix multiplication.

FINCH-19: ONE 512-NUMBER QUERY CUT INTO 8 HEADS OF 64head 00 to 63head 164 to 127head 2128 to 191head 3192 to 255head 4256 to 319head 5320 to 383head 6384 to 447head 7448 to 511d_model = 512 = 8 heads × d_head 64q = x WQ(B, T, 512).view(B, T, 8, 64)(B, T, 8, 64).transpose(1, 2)(B, 8, T, 64)more heads, same total width: splitting is free
Figure 1.66Splitting the width. A 512-number query is cut into eight 64-number heads, and two reshapes rearrange the tensor so all heads run in parallel.
python
q = q.view(B, T, H, d_head).transpose(1, 2)   # (B, T, 512) -> (B, 8, T, 64)
k = k.view(B, T, H, d_head).transpose(1, 2)   # same for keys
v = v.view(B, T, H, d_head).transpose(1, 2)   # and values
att = softmax(q @ k.transpose(-2, -1) / 8.0 + mask, dim=-1)   # (B, 8, T, T); sqrt(64) = 8
out = att @ v                                  # (B, 8, T, 64)

So with 8 heads you get 8 independent attention patterns for about the same cost as one. There is one cost that does grow: each head has its own T×TT \times T weight grid, so 8 heads means 8 grids. Part X takes that seriously.

#Section 50What Different Heads Learn

Nobody tells a head what to focus on. Each head's WQW_Q, WKW_K and WVW_V slices start random, and training sorts out a division of labour on its own. Researchers who open up trained models find the same habits again and again.

A previous-token head always looks one step back. It is one of the simplest patterns and one of the most useful: knowing "what came right before me" is the raw material for spotting phrases. An antecedent head, or more generally a coreference head, links words like it back to what they refer to. A sink head dumps most of its weight on the very first token when it has nothing useful to do, which works like a "no-op" setting, because the first token's value can learn to be harmless. A broad head spreads its weight evenly and produces a general summary of the context so far.

FOUR HABITS HEADS LEARN IN ALMOST EVERY MODEL (ARC THICKNESS = WEIGHT)previous-token headThecatsatbecauseitwastiredantecedent headThecatsatbecauseitwastiredsink headThecatsatbecauseitwastiredbroad headThecatsatbecauseitwastiredone step back, alwayspronoun → its nounparks weight on token 0even spread: a summary
Figure 1.67Four habits that heads fall into in trained models. Arc thickness shows attention weight. These patterns are illustrative, but each is documented in real models.
THE SAME FOUR HABITS AS 7 × 7 WEIGHT GRIDS (ROWS = QUERIES)previous-tokenantecedentsinkbroada line under the diagonal, a bright column at "cat", a bright first column, an even wash
Figure 1.68The same four habits as weight grids. A line just under the diagonal, a bright column at the antecedent, a bright first column, and an even wash. Once you know these shapes, you can spot them in real attention maps.

A famous combination is the induction head, described by researchers at Anthropic in 2022. It works with a previous-token head in an earlier layer to implement the rule "if A B appeared earlier and I am now at A, predict B". That simple copy-the-pattern trick is a large part of how models learn from examples placed in their prompt.

#Section 51Putting Heads Back Together

Each head produces its own output: for Finch-19, 8 outputs of 64 numbers per token. They have to become one 512-number vector again, so the block can add it back to the token's vector. Two steps do this.

First, concatenate: place the 8 outputs end to end, giving 512 numbers. That only stacks them side by side; head 0's findings sit in numbers 0 to 63, head 1's in 64 to 127, and they do not interact. Second, multiply by one more learned matrix, the output projection WOW_O, which is 512×512512 \times 512. Each of its output numbers is a weighted sum over all 512 concatenated numbers, so it can combine what the antecedent head found with what the clause head found.

MultiHead(x)=concat(head1,…,headH) WO\text{MultiHead}(x) = \text{concat}(\text{head}_1, \dots, \text{head}_H)\, W_O
CONCATENATE THE HEADS, THEN MIX THEM WITH W_O (FINCH-19, ONE TOKEN)h0h1h2h3h4h5h6h78 head outputs, 64 numbers eachconcat512WO512 × 512512W_O lets what one head foundcombine with what another foundconcat alone just stacks the heads side by side
Figure 1.69Concatenation stacks the eight head outputs into one 512-number vector; the output projection then mixes information across heads.

More heads, same parameter bill

Splitting a fixed model width into heads does not enlarge the projection matrices. More heads change the division of features and add distinct attention maps, rather than automatically adding projection weights.

MORE HEADS, SAME PARAMETER BILL: 4 × 512² IN FINCH-19 WHATEVER H ISH = 1, d_head = 512WQ: 262,144H = 2, d_head = 256WQ: 262,144H = 8, d_head = 64WQ: 262,144H = 16, d_head = 32WQ: 262,144each projection is cut into H column blocks; the total is 4 × 262,144 = 1,048,576 weights for any H
Figure 1.70The number of heads only changes how the projection matrices are cut, not their size. Four 512 × 512 matrices hold 1,048,576 weights for any head count.

#Section 52Heads Can Share Keys and Values

Unit II extends this idea. In standard multi-head attention, called MHA, every query head has its own keys and values. But the keys and values have to be stored during generation, in the KV cache of Part XI, and that storage is often what limits how long a conversation can be or how many users a server can handle.

So people asked: what if several query heads shared one set of keys and values? Each head still asks its own question with its own query, but they all look it up in the same keys and read the same values. With 8 query heads sharing 2 key/value heads in groups of 4, that is grouped-query attention, GQA. With all 8 sharing a single key/value head, it is multi-query attention, MQA. Noam Shazeer proposed MQA in 2019; Joshua Ainslie and colleagues at Google introduced GQA in 2023. Nearly every modern model uses GQA: Llama 3 8B has 32 query heads and 8 key/value heads. Unit II covers it in depth, and Finch-24 will use 8 query heads with 2 key/value heads.

MHAevery query head has its own K/V8 query heads8 K/V headsGQAgroups of 4 share one K/V8 query heads2 K/V headsMQAall heads share one K/V8 query heads1 K/V headKV cache size is proportional to the number of K/V heads: 8 : 2 : 1
Figure 1.71Multi-head, grouped-query and multi-query attention. Query heads always stay separate; what changes is how many key/value heads they share. The KV cache shrinks in proportion.

Sharp edges of multi-head attention

dmodeld_{\text{model}} must divide evenly by HH. 512 with 8 heads gives 64 per head; 768 with 12 gives 64. 768 with 10 heads would give 76.8, which does not work.

Heads that are too narrow stop working well. With too many heads, each slice is so thin it cannot represent much, and the dk\sqrt{d_k}-scaled scores get noisy. Almost every model keeps dheadd_{\text{head}} between 64 and 128, and adds more heads as the model gets wider rather than making heads thinner.

Many heads are redundant, but you still need them in training. Paul Michel and colleagues showed in 2019, in Are Sixteen Heads Really Better than One?, that many heads can be removed from a trained model with little loss. But training with fewer heads from the start usually does worse. Extra heads seem to help training find good solutions.

The reshape bug. Splitting into heads is a reshape, and one wrong axis order, such as view(B, H, T, d_head) instead of view(B, T, H, d_head) followed by a transpose, mixes numbers from different heads and different tokens. The shapes come out correct, the code runs, and the output is garbage. Check one head’s output against a simple single-head reference implementation.