Multi-Head Attention

One attention head can only focus one way at a time. Running several heads in parallel — each with its own queries, keys and values — lets a token attend to different things at once, then combine what they found.

Large Language Models: From Transformers to Frontier Models

A single attention head computes one set of weights per token — one way of deciding what is relevant. But a token usually needs several kinds of context simultaneously. Take the verb sat in "the cat sat on the mat": to be understood well it wants to know who sat ( cat, its subject), where it sat ( mat, via on), and the grammatical shape of the phrase. A single softmax has to spread its one budget of attention across all of these and inevitably compromises.

Multi-head attention removes the compromise by running several attention operations — heads — in parallel, each free to focus on a different kind of relationship.

The idea: several attentions at once

Instead of one set of projection matrices WQ,WK,WVW_Q, W_K, W_V, the layer has hh independent sets, one per head. Head number ii has its own WQ(i),WK(i),WV(i)W_Q^{(i)}, W_K^{(i)}, W_V^{(i)}, and therefore computes its own queries, keys, values, scores and blended output — the entire scaled, masked attention from Module 3, run start to finish, hh times over on the same input.

Because each head has its own learned projections, each can specialise:

  • one head might track subject–verb links,
  • another might follow adjacent-word syntax,
  • another might reach back to a distant referent (our it → cat),
  • another might attend to punctuation or sentence boundaries.

They all look at the same tokens, but through different learned lenses, at the same time.

Splitting the budget, not multiplying the cost

Here is the elegant part. Multi-head attention does not make each head as wide as the whole model. Instead the model's dimension dd is divided among the heads. With hh heads, each head works in a smaller dimension

dk=dh.d_k = \frac{d}{h}.

So a model of width d=512d = 512 with h=8h = 8 heads gives each head a 64-dimensional query, key and value. Each head attends in its own 64-dimensional subspace. The total amount of computation is about the same as one full-width head — we have re-spent the same budget as several narrow, specialised views rather than one wide, unfocused one.

Why splitting helps rather than hurts

You might expect smaller heads to be weaker. In practice several narrow heads beat one wide head, because attention's bottleneck is not dimension but focus: one softmax can only emphasise one pattern of relevance at a time. Eight softmaxes can emphasise eight patterns at once. Diversity of attention matters more here than the width of any single head.

Combine what the heads found

After all hh heads produce their blended outputs (each a dkd_k-dimensional vector per token), the layer must fold them back into a single dd-dimensional vector to return to the residual stream. It does this in two steps:

  1. Concatenate the heads' outputs, stacking the hh vectors of size dkd_k back into one vector of size h⋅dk=dh \cdot d_k = d.
  2. Mix them with a final learned output matrix WOW_O, so that information discovered by different heads can interact rather than sit in separate slots.
MultiHead=WO [ head1 ; head2 ; … ; headh ].\text{MultiHead} = W_O \,\big[\,\text{head}_1 \,;\, \text{head}_2 \,;\, \dots \,;\, \text{head}_h\,\big].

The output projection WOW_O is essential: without it the heads' findings would remain in disjoint sub-blocks of the vector, never combined. With it, the layer produces a single enriched vector that reflects all the heads at once.

The input projected into several parallel attention heads each in a smaller dimension, their outputs concatenated and passed through an output projection
Multi-head attention: the same input feeds h parallel heads, each attending in a d/h-dimensional subspace. Their outputs are concatenated and mixed by W_O into one vector.

The shape to remember

Multi-head attention is one layer that internally fans out into hh small attentions and fans back in:

  • Fan out: project the input into hh sets of (query, key, value), each of dimension dk=d/hd_k = d/h.
  • Attend: run scaled, masked attention independently in each head.
  • Fan in: concatenate the hh outputs and mix them with WOW_O.

The next chapter looks more closely at the bookkeeping of these shapes, and the one after asks what the heads actually end up doing — and whether we need all of them.

Try it yourself
Transformer Lab: multi-head attention →

Compare four heads with different jobs, and check that the parameter count does not depend on the number of heads.

EasyMulti-head

Why run multiple attention heads instead of one?

MediumMulti-head

With h heads in a model of width d, what dimension does each head work in, and how does that keep the cost in check?

MediumMulti-head

Why is the output projection W_O needed after concatenating the heads?