Skip to content
mlmentorship

Multi-head attention: why one head is not enough

Run h independent attention computations in parallel, then concatenate. Each head specializes in a different relation. The mechanism most senior candidates can write but few can motivate.

Published · 8 min read ·Core ·Intermediate

Summary

Multi-head attention projects , , into lower-dimensional subspaces, runs scaled dot-product attention independently in each, and concatenates the results before a final output projection. Same FLOPs as one large head; very different inductive bias.

Single-head attention computes one weighted average per position. That single distribution has to encode every relation the model needs: syntactic, positional, semantic, coreferential. In practice it cannot, and ablations show that single-head transformers underperform multi-head transformers at matched parameter count (Vaswani et al., 2017).

Multiple heads let different attention patterns coexist. One head learns “previous token,” another “matching bracket,” another “this noun’s modifier.” Probing studies on BERT show many heads fire on syntactic dependencies that linguists recognize (Clark et al., 2019).

The mechanism

Given input and head count with per-head dimension :

  1. Project: , , , each shape . Reshape to .
  2. Per-head attention: for each head ,
  1. Concatenate: stack the heads back into shape .
  2. Output projection: .

Ignoring optional biases, total parameters are (the four projection matrices). FLOPs are , the same asymptotic cost as single-head attention; the heads share the dimension budget.

Tensor flow

Split features into heads; every head still sees all tokens

Heads split features, not the token sequenceEvery head can attend over all tokens. Concatenating head outputs restores the feature dimension before the output projection.Every head keeps every token rowInput: three tokens, four featuresSplit columns into two headsConcatenate feature columns againThe split changes features, notsequence membership.
Derivation
Q, K, and V projection into parallel attention heads and recombination A full input sequence X with n tokens and d features is projected into Q, K, and V, each n by d. The feature axis is split across h parallel heads. Representative head 1 and head h each receive all n tokens through Q i, K i, and V i tensors of shape n by d sub h, and each produces an n by d sub h output. The h head outputs concatenate into n by h d sub h, equal to n by d, then a d by d output matrix W O produces the final n by d tensor. The diagram explicitly states that heads do not receive disjoint token subsets. 1 · SHARED SEQUENCE X all n tokens × d features 2 · PROJECT Q, K, V Qn × d Kn × d Vn × d HEAD 1 · all n tokens HEAD h · all n tokens Q₁n×dₕ K₁n×dₕ V₁n×dₕ Qₕn×dₕ Kₕn×dₕ Vₕn×dₕ scaled dot-product attention scaled dot-product attention head₁ · n×dₕ headₕ · n×dₕ ⋯ 3 · CONCATENATE h FEATURE SLICES [ head₁ | ··· | headₕ ] n × (h dₕ) = n × d WO · d × d output · n × d
every head receives Q, K, and V for all n tokens; only the d-wide feature axis is split into dₕ = d/h slices. The h outputs concatenate back to n × d, then WO mixes information across heads.
Check: Does each head attend over all n tokens? Which dimension is split before concatenation?
Read it this way: Every head can attend over all tokens. Concatenating head outputs restores the feature dimension before the output projection.
Which head loses access to token two after this feature split?

Why split the dimension

If you keep per head and run heads, you multiply parameters and compute by . Splitting across heads keeps the cost matched to a single-head baseline, so any gain is attributable to the multiplicity itself, not extra capacity. This is the design choice that makes the comparison meaningful.

Variants

  • Multi-query attention (MQA): share and across all heads; only is per-head. KV-cache shrinks by x. See GQA and MQA.
  • Grouped-query attention (GQA): share across groups of heads. Compromise between full MHA and MQA. The Llama 2/3 default.
  • Cross-attention: from one sequence, from another. See self-attention vs cross-attention.
  • Sliding-window / sparse: restrict each head to a local window or learned sparse pattern.

Tradeoffs

  • Head count: 8 to 32 is typical. More heads with smaller can hurt expressiveness; fewer heads with larger loses specialization. to is the modern sweet spot.
  • KV-cache memory scales linearly with in vanilla MHA. The motivation for MQA and GQA at long context.

Common pitfalls

  • Equating “more heads” with “more capacity.” Splitting fixes the parameter budget; it is a structural choice, not a scale-up.
  • Reading the post-softmax weights as “what the model attends to.” Heads are mixed in . Single-head probes can be misleading.
  • Treating MHA as the bottleneck. In long-context LLMs, the FFN is usually larger; attention compute scales with but FFN compute scales with .