Multi-Head Attention: Full Mechanics

Complete derivation of multi-head attention: why multiple heads, the projection matrices, concatenation, parameter count, and how heads specialize — positional, syntactic, and semantic attention patterns.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Multi-Head Formulation
  5. Parameter Count
  6. Why Multiple Heads?
  7. Head Specialization
  8. The Output Projection
  9. Relation to Tensor Products
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive multi-head attention from single-head attention.
  2. Compute the exact parameter count for MHA.
  3. Prove that MHA with one head of dimension dd is more expressive than single-head attention.
  4. Explain empirically observed head specialization patterns.
  5. Derive the computational equivalence of parallel heads vs sequential computation.

Notation

  • dd — model dimension
  • HH — number of heads
  • dk=dv=d/Hd_k = d_v = d/H — per-head dimension
  • WiQ,WiKRd×dk\mathbf{W}_i^Q, \mathbf{W}_i^K \in \mathbb{R}^{d \times d_k} — per-head projection matrices
  • WORd×d\mathbf{W}^O \in \mathbb{R}^{d \times d} — output projection

Core Intuition

A single attention head can only compute one type of similarity between positions. But understanding language requires attending to multiple relationships simultaneously: syntactic structure, semantic similarity, positional proximity, coreference, etc. Multi-head attention runs HH independent attention operations in parallel, each in a lower-dimensional subspace, allowing the model to jointly attend to information from different representation subspaces.

Multi-Head Attention

ThecatsatmatHead 1: syntax patternsConcat heads → Linear projection → Output
Head
0
Head 1Head 2Head 3
Explore: Each head learns different attention patterns. Arc thickness = attention weight. Heads are concatenated and projected to mix information.

The Multi-Head Formulation

MultiHead(Q,K,V)=Concat(head1,,headH)WO,(1)\text{MultiHead}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{Concat}(\text{head}_1, \ldots, \text{head}_H)\mathbf{W}^O, \tag{1}

where each head:

headi=Attention(QWiQ,KWiK,VWiV).(2)\text{head}_i = \text{Attention}(\mathbf{Q}\mathbf{W}_i^Q, \mathbf{K}\mathbf{W}_i^K, \mathbf{V}\mathbf{W}_i^V). \tag{2}

Step by step for input XRT×d\mathbf{X} \in \mathbb{R}^{T \times d}:

  1. Project: Qi=XWiQRT×dk\mathbf{Q}_i = \mathbf{X}\mathbf{W}_i^Q \in \mathbb{R}^{T \times d_k} (similarly for K, V).
  2. Attend: headi=softmax(QiKiT/dk)ViRT×dv\text{head}_i = \text{softmax}(\mathbf{Q}_i\mathbf{K}_i^T/\sqrt{d_k})\mathbf{V}_i \in \mathbb{R}^{T \times d_v}.
  3. Concatenate: [head1;;headH]RT×d[\text{head}_1; \ldots; \text{head}_H] \in \mathbb{R}^{T \times d}.
  4. Output project: Multiply by WORd×d\mathbf{W}^O \in \mathbb{R}^{d \times d}.

Parameter Count

For each head:

  • WiQ\mathbf{W}_i^Q: d×dkd \times d_k parameters
  • WiK\mathbf{W}_i^K: d×dkd \times d_k parameters
  • WiV\mathbf{W}_i^V: d×dvd \times d_v parameters

Across HH heads:

  • Q projections: H×d×dk=d×dH \times d \times d_k = d \times d (since Hdk=dH \cdot d_k = d)
  • K projections: d×dd \times d
  • V projections: d×dd \times d
  • Output projection: d×dd \times d
Total MHA parameters=4d2.(3)\boxed{\text{Total MHA parameters} = 4d^2.} \tag{3}

This is identical to a single head with full dimension dd (same compute budget, split across heads).


Why Multiple Heads?

Theorem (Expressiveness). Multi-head attention with HH heads is strictly more expressive than single-head attention of the same total dimension.

Intuitive argument: Each head produces a different attention pattern Ai\mathbf{A}_i. The output is a weighted combination of values retrieved using different similarity metrics. A single head can only produce one attention pattern per layer.

Formal argument: Consider the output at position tt:

ot=WO[jαtj(1)vj(1)jαtj(H)vj(H)]=iWiOjαtj(i)vj(i),(4)\mathbf{o}_t = \mathbf{W}^O\begin{bmatrix}\sum_j\alpha_{tj}^{(1)}\mathbf{v}_j^{(1)} \\ \vdots \\ \sum_j\alpha_{tj}^{(H)}\mathbf{v}_j^{(H)}\end{bmatrix} = \sum_i\mathbf{W}^O_i\sum_j\alpha_{tj}^{(i)}\mathbf{v}_j^{(i)}, \tag{4}

where WiO\mathbf{W}^O_i is the ii-th column block of WO\mathbf{W}^O. Each term uses a different attention distribution α(i)\alpha^{(i)} — this cannot be replicated by a single softmax.


Head Specialization

Empirical analysis of trained transformers reveals systematic specialization:

Positional heads: Attend to fixed relative positions (e.g., always attend to the previous token, or token 2 positions back). These implement a form of learned n-gram patterns.

Syntactic heads: Track grammatical structure — subject-verb agreement, modifier-noun relationships. Identifiable by attention aligning with dependency parse trees.

Induction heads: Two-head circuits that implement in-context learning:

  1. Head A: copies previous token identity.
  2. Head B: attends to positions where Head A's output matches current context → "if A follows B before, then A follows B again."

Retrieval heads: In later layers, attend to semantically relevant tokens regardless of position (long-range factual recall).


The Output Projection

Why WO\mathbf{W}^O? Without it, each head's contribution is restricted to its dvd_v-dimensional subspace. The output projection allows:

  • Mixing information across heads.
  • Projecting the concatenated output back to model dimension.
  • Learning which head combinations are useful.

Alternative view: The full MHA operation can be written as:

MHA(X)=i=1Hsoftmax(XWiQ(XWiK)Tdk)XWiVWiO,(5)\text{MHA}(\mathbf{X}) = \sum_{i=1}^H \text{softmax}\left(\frac{\mathbf{XW}_i^Q(\mathbf{XW}_i^K)^T}{\sqrt{d_k}}\right)\mathbf{XW}_i^V\mathbf{W}_i^{O}, \tag{5}

where WiORdv×d\mathbf{W}_i^O \in \mathbb{R}^{d_v \times d} is the ii-th row block of WO\mathbf{W}^O.


Relation to Tensor Products

Multi-head attention can be viewed through the lens of tensor decomposition. The attention operation for all heads simultaneously:

MHA(X)=i=1H(AiWiVO)X,(6)\text{MHA}(\mathbf{X}) = \sum_{i=1}^H (\mathbf{A}_i \otimes \mathbf{W}_i^{VO})\mathbf{X}, \tag{6}

where \otimes denotes the outer product in the token dimension, and WiVO=WiVWiO\mathbf{W}_i^{VO} = \mathbf{W}_i^V\mathbf{W}_i^O is the combined value-output projection.

This is a rank-HH decomposition of the full attention tensor — each head contributes one rank-1 component in the "attention pattern × value mixing" space.


Common Pitfalls

Pitfall 1. Assuming more heads = better. Beyond a threshold, increasing HH while keeping dd fixed makes each head dimension d/Hd/H too small for meaningful attention patterns. Typical sweet spot: dk=64d_k = 64128128.

Pitfall 2. Ignoring the output projection. Removing WO\mathbf{W}^O (using raw concatenation) significantly hurts performance because heads cannot communicate.

Pitfall 3. Treating heads as independent. While computed in parallel, heads interact through the residual stream — later layers read the combined output of all heads from previous layers.


Summary

  • Multi-head attention runs HH parallel attention operations in dkd_k-dimensional subspaces.
  • Total parameters: 4d24d^2 (same budget as single-head, but more expressive).
  • Heads specialize: positional, syntactic, induction, retrieval patterns emerge from training.
  • Output projection WO\mathbf{W}^O enables cross-head information mixing.
  • MHA as rank-HH tensor decomposition of the full attention operation.

Exercises

Exercise 1. For a model with d=1024,H=16d=1024, H=16, compute dkd_k and the total MHA parameters (including output projection).

Exercise 2. Prove that Concat(head1,,headH)WO\text{Concat}(\text{head}_1, \ldots, \text{head}_H)\mathbf{W}^O cannot be replicated by a single attention head of dimension dd (hint: different attention patterns per subspace).

Exercise 3. Compute the FLOPs for multi-head attention with T=2048,d=4096,H=32T=2048, d=4096, H=32.

Exercise 4. Describe how an "induction head" circuit works using two attention heads. What attention patterns must each head learn?

Exercise 5. If we reduce HH from 32 to 8 while keeping d=4096d=4096 constant, what changes in terms of per-head expressiveness and total parameter count?