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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Multi-Head Formulation
- Parameter Count
- Why Multiple Heads?
- Head Specialization
- The Output Projection
- Relation to Tensor Products
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive multi-head attention from single-head attention.
- Compute the exact parameter count for MHA.
- Prove that MHA with one head of dimension is more expressive than single-head attention.
- Explain empirically observed head specialization patterns.
- Derive the computational equivalence of parallel heads vs sequential computation.
Notation
- — model dimension
- — number of heads
- — per-head dimension
- — per-head projection matrices
- — 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 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
The Multi-Head Formulation
where each head:
Step by step for input :
- Project: (similarly for K, V).
- Attend: .
- Concatenate: .
- Output project: Multiply by .
Parameter Count
For each head:
- : parameters
- : parameters
- : parameters
Across heads:
- Q projections: (since )
- K projections:
- V projections:
- Output projection:
This is identical to a single head with full dimension (same compute budget, split across heads).
Why Multiple Heads?
Theorem (Expressiveness). Multi-head attention with heads is strictly more expressive than single-head attention of the same total dimension.
Intuitive argument: Each head produces a different attention pattern . 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 :
where is the -th column block of . Each term uses a different attention distribution — 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:
- Head A: copies previous token identity.
- 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 ? Without it, each head's contribution is restricted to its -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:
where is the -th row block of .
Relation to Tensor Products
Multi-head attention can be viewed through the lens of tensor decomposition. The attention operation for all heads simultaneously:
where denotes the outer product in the token dimension, and is the combined value-output projection.
This is a rank- 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 while keeping fixed makes each head dimension too small for meaningful attention patterns. Typical sweet spot: –.
Pitfall 2. Ignoring the output projection. Removing (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 parallel attention operations in -dimensional subspaces.
- Total parameters: (same budget as single-head, but more expressive).
- Heads specialize: positional, syntactic, induction, retrieval patterns emerge from training.
- Output projection enables cross-head information mixing.
- MHA as rank- tensor decomposition of the full attention operation.
Exercises
Exercise 1. For a model with , compute and the total MHA parameters (including output projection).
Exercise 2. Prove that cannot be replicated by a single attention head of dimension (hint: different attention patterns per subspace).
Exercise 3. Compute the FLOPs for multi-head attention with .
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 from 32 to 8 while keeping constant, what changes in terms of per-head expressiveness and total parameter count?