4. Multi-Head Attention

Why one attention head isn't enough

In the previous article, we walked through how a single attention head works: you project your tokens into queries, keys, and values, compute scaled dot-product attention, and get back a weighted mix of the value vectors. It's elegant — and it genuinely works. But it has a fundamental limitation.

A single head can only look for one kind of pattern at a time.

The Problem With One Head

Think about what a model needs to understand in a sentence like "The bank near the river flooded after heavy rain."

To resolve that "bank" means a riverbank here, the model needs to track several things at once:

  • Syntax: "bank" is the subject; "flooded" is its verb
  • Semantics: "river" and "rain" are strong contextual clues
  • Coreference: nothing here, but in longer text you'd need to track pronoun referents
  • Local structure: "near the river" is a modifying phrase attached to "bank"

A single attention head produces one set of attention weights per token. Those weights are a single distribution — one number per other token. You can't use the same distribution to simultaneously say "pay attention to adjacent words for phrase structure" AND "pay attention to semantically similar words" AND "pay attention to my pronoun's referent." You're stuck with one weighted sum.

A single attention head is like looking at a sentence through one lens at a time. Multi-head attention is like having several specialists looking simultaneously — one for grammar, one for meaning, one for references — and then pooling their observations.

The Multi-Head Solution

The fix is straightforward: run several attention heads in parallel, each with its own independent weight matrices W_Q, W_K, and W_V. Each head gets to learn a completely different projection of the input, so each one ends up specializing in a different kind of relationship.

After each head computes its own attention output, you concatenate all the outputs and run them through one final linear projection (W_O) to mix them back into the model's standard dimension.

The formula is:

MultiHead(Q, K, V) = Concat(head_1, ..., head_h) · W_O

where head_i = Attention(Q·W_Q_i, K·W_K_i, V·W_V_i)

That's really it. The magic is entirely in the learned weight matrices — each head's W_Q, W_K, W_V are trained independently, so gradient descent pushes different heads toward different specializations.

Dimension Splitting

You might wonder: if we're running eight heads instead of one, doesn't this multiply our compute by eight?

Not quite. The trick is dimension splitting. If the model dimension is d_model = 512 and you have h = 8 heads, each head doesn't work with all 512 dimensions. Instead, each head operates on d_k = d_model / h = 64 dimensions.

  1. The input (shape: seq_len × d_model) arrives at the multi-head block.
  2. Each head applies its own W_Q, W_K, W_V matrices of shape d_model × d_k, projecting down to 64 dimensions.
  3. Each head computes scaled dot-product attention in that 64-dimensional space.
  4. Each head outputs a matrix of shape seq_len × d_k.
  5. All h outputs are concatenated along the last dimension: seq_len × (h · d_k) = seq_len × d_model.
  6. The concatenated result is projected through W_O (shape: d_model × d_model) to produce the final output.

The total parameter count ends up comparable to a single full-dimensional head. You get the diversity of multiple heads without a proportional compute cost increase.

What Different Heads Actually Learn

This isn't just theory. Researchers have probed attention heads in trained transformers and found real, interpretable specializations. Some consistent findings:

Positional heads — Some heads in early layers develop strong attention to adjacent tokens or tokens at fixed offsets (e.g., always attend one position to the left). These seem to help with local phrase structure.

Syntactic heads — Certain heads reliably track grammatical dependencies: determiners attending to their nouns, verbs attending to their subjects, prepositions attending to their objects. These patterns match what a dependency parser would produce.

Coreference heads — In models trained on longer documents, some heads specialize in linking pronouns to their antecedents. "It" learns to attend to the noun it refers to.

Broad semantic heads — Some heads spread attention more diffusely, weighting tokens by semantic relatedness rather than syntactic role. "Doctor" and "hospital" get high mutual attention even without a direct grammatical link.

The model isn't explicitly told to develop these specializations — they emerge because they're useful for predicting the next token accurately.

The Split-Compute-Concat Flow

Here's a diagram of the full multi-head mechanism. The input is split into h parallel streams, each stream runs its own attention, and the results are concatenated and projected:

Diagram

Split-compute-concat flow

Input (seq_len × d_model) X Head 1 W_Q¹ W_K¹ W_V¹ d_k = 64 Head 2 W_Q² W_K² W_V² d_k = 64 Head 3 W_Q³ W_K³ W_V³ d_k = 64 Head 4 W_Q⁴ W_K⁴ W_V⁴ d_k = 64 ··· Concat → seq_len × d_model × W_O (linear) Output (seq_len × d_model)

Each head independently attends to different aspects of the input. The concatenation step simply stacks their outputs side by side. The W_O projection then lets the model learn how to combine information from all heads into a coherent output.

Why This Generalizes So Well

Multi-head attention is one of the main reasons transformers transfer so effectively across tasks. When you fine-tune a pre-trained model, many of the head specializations are already in place — the syntactic heads and coreference heads learned during pre-training don't need to be relearned from scratch. Fine-tuning mostly adjusts the higher-level task-specific patterns.

  • A single attention head can only produce one attention distribution per token per layer — it can't simultaneously track syntax, semantics, and coreference.
  • Multi-head attention runs h independent heads in parallel, each with its own W_Q, W_K, W_V matrices.
  • Dimension splitting keeps compute manageable: each head works in d_model/h dimensions, not d_model.
  • Outputs are concatenated and projected through W_O to mix information across heads.
  • In practice, different heads genuinely specialize: some track adjacent words, some track syntactic dependencies, some track pronoun-antecedent links.

With multi-head attention in hand, we have the core computational primitive of the transformer. The next article puts it all together: we'll see how attention is wrapped with residual connections, layer normalization, and feed-forward layers to build a full transformer block — and how those blocks stack into a complete model.

Multi-Head Attention Viewer

Enter a sentence, click "Compute Heads", then click any word to see how each head attends to it.