Multi-Head Attention

Complete derivation of multi-head attention: why multiple heads increase expressiveness, the projection geometry, concatenation and linear mixing, computational complexity analysis, and the connection to ensemble kernel methods.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. From Single-Head to Multi-Head
  5. Mathematical Formulation
  6. The Projection Geometry
  7. Why Multiple Heads Help
  8. Computational Complexity
  9. Attention Patterns Across Heads
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive multi-head attention from single-head by partitioning the embedding space.
  2. Show that multi-head attention with HH heads and dimension dk=d/Hd_k = d/H has the same FLOPs as single-head.
  3. Prove that multi-head attention is strictly more expressive than single-head of the same dimension.
  4. Analyze the rank of attention matrices and how heads increase effective rank.
  5. Explain Grouped-Query Attention (GQA) and Multi-Query Attention (MQA) as efficiency variants.

Notation

  • HH — number of attention heads
  • dd — model dimension (embedding size)
  • dk=d/Hd_k = d/H — dimension per head
  • WiQ,WiKRd×dk\mathbf{W}_i^Q, \mathbf{W}_i^K \in \mathbb{R}^{d \times d_k} — query/key projections for head ii
  • WiVRd×dk\mathbf{W}_i^V \in \mathbb{R}^{d \times d_k} — value projection for head ii
  • WORd×d\mathbf{W}^O \in \mathbb{R}^{d \times d} — output projection
  • headi\text{head}_i — output of the ii-th attention head

Core Intuition

A single attention head computes one weighted average of values based on one notion of "relevance" (one Q-K dot product). But language has multiple simultaneous relationships: syntactic (subject-verb), semantic (noun-modifier), positional (adjacent words). Multi-head attention runs multiple attention operations in parallel, each attending to different types of relationships, then combines the results.

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.

From Single-Head to Multi-Head

Single-head attention with dimension dd:

Attention(Q,K,V)=softmax(QKTd)V.(1)\text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{d}}\right)\mathbf{V}. \tag{1}

Problem: The softmax produces a rank-1 (per query) attention pattern. A single head can focus on only one "aspect" at a time.

Solution: Use HH independent attention operations in lower-dimensional subspaces:

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

where each head operates in Rdk\mathbb{R}^{d_k}:

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

Mathematical Formulation

Given input XRT×d\mathbf{X} \in \mathbb{R}^{T \times d} (sequence of TT tokens):

For each head i{1,,H}i \in \{1, \ldots, H\}:

Qi=XWiQ,Ki=XWiK,Vi=XWiV.(4)\mathbf{Q}_i = \mathbf{X}\mathbf{W}_i^Q, \quad \mathbf{K}_i = \mathbf{X}\mathbf{W}_i^K, \quad \mathbf{V}_i = \mathbf{X}\mathbf{W}_i^V. \tag{4} Ai=softmax(QiKiTdk)RT×T.(5)\mathbf{A}_i = \text{softmax}\left(\frac{\mathbf{Q}_i\mathbf{K}_i^T}{\sqrt{d_k}}\right) \in \mathbb{R}^{T \times T}. \tag{5} headi=AiViRT×dk.(6)\text{head}_i = \mathbf{A}_i\mathbf{V}_i \in \mathbb{R}^{T \times d_k}. \tag{6}

Final output:

MHA(X)=[head1;;headH]WORT×d.(7)\text{MHA}(\mathbf{X}) = [\text{head}_1; \ldots; \text{head}_H]\mathbf{W}^O \in \mathbb{R}^{T \times d}. \tag{7}

The Projection Geometry

Each head projects the dd-dimensional input into a dkd_k-dimensional subspace. The projections WiQ,WiK\mathbf{W}_i^Q, \mathbf{W}_i^K define the "similarity metric" for head ii:

scorei(q,k)=(xqWiQ)(xkWiK)Tdk=xqWiQ(WiK)TxkTdk.(8)\text{score}_{i}(q, k) = \frac{(\mathbf{x}_q\mathbf{W}_i^Q)(\mathbf{x}_k\mathbf{W}_i^K)^T}{\sqrt{d_k}} = \frac{\mathbf{x}_q\mathbf{W}_i^Q(\mathbf{W}_i^K)^T\mathbf{x}_k^T}{\sqrt{d_k}}. \tag{8}

The matrix WiQ(WiK)TRd×d\mathbf{W}_i^Q(\mathbf{W}_i^K)^T \in \mathbb{R}^{d \times d} defines a learned bilinear form — each head learns a different notion of "similarity."


Why Multiple Heads Help

Theorem. Multi-head attention is strictly more expressive than single-head attention of the same total dimension.

Argument. Single-head with dimension dd computes one attention matrix ART×T\mathbf{A} \in \mathbb{R}^{T \times T}. Multi-head computes HH different matrices A1,,AH\mathbf{A}_1, \ldots, \mathbf{A}_H, each potentially attending to different positions. The output combines HH different weighted averages of HH different value projections — a richer representation.

Rank argument. A single softmax row has rank 1 (it's a probability vector applied to the value matrix). With HH heads contributing rank-dkd_k outputs each, the total output can have rank up to d=Hdkd = H \cdot d_k, enabling richer representations.

Empirical observation: Different heads learn different functions:

  • Some heads attend to adjacent tokens (local patterns).
  • Some attend to syntactic dependencies (long-range).
  • Some attend to specific positions (positional heads).

Computational Complexity

Single-head (dd dimensions): Computing QKT\mathbf{Q}\mathbf{K}^T costs O(T2d)O(T^2 d). Total: O(T2d)O(T^2 d).

Multi-head (HH heads, dk=d/Hd_k = d/H each): Each head costs O(T2dk)O(T^2 d_k). Over HH heads: O(HT2d/H)=O(T2d)O(H \cdot T^2 \cdot d/H) = O(T^2 d).

Same total cost — multi-head attention does not increase FLOPs compared to single-head of the same dimension. The parallelism across heads maps naturally to GPU tensor operations.

Parameters: WQ,WK,WV\mathbf{W}^Q, \mathbf{W}^K, \mathbf{W}^V each Rd×d\in \mathbb{R}^{d \times d}, plus WORd×d\mathbf{W}^O \in \mathbb{R}^{d \times d}. Total: 4d24d^2.


Attention Patterns Across Heads

Multi-Query Attention (MQA): Share K,V\mathbf{K}, \mathbf{V} projections across all heads; only Q\mathbf{Q} is per-head. Reduces KV-cache by factor HH.

Grouped-Query Attention (GQA): Group heads into GG groups; each group shares K,V\mathbf{K}, \mathbf{V}. Interpolates between MHA (G=HG=H) and MQA (G=1G=1). Used in LLaMA-2, Mistral.

Parameter comparison:

  • MHA: 4d24d^2 parameters
  • MQA: 2d2+2ddk=2d2(1+1/H)2d^2 + 2d \cdot d_k = 2d^2(1 + 1/H)
  • GQA: 2d2+2d2/G2d^2 + 2d^2/G

Common Pitfalls

Pitfall 1. Thinking heads are independent after training. While initialized independently, heads communicate through the residual stream and WO\mathbf{W}^O — they learn complementary roles.

Pitfall 2. Using too many heads with too small dkd_k. If dk<16d_k < 16, individual heads lack capacity to compute meaningful attention. GPT-3 uses d=12288,H=96dk=128d=12288, H=96 \Rightarrow d_k=128.

Pitfall 3. Not scaling by 1/dk1/\sqrt{d_k}. Without this, dot products grow with dimension, pushing softmax into saturation (near-one-hot outputs).


Summary

  • Multi-head attention runs HH parallel attention operations in dkd_k-dimensional subspaces.
  • Each head learns a different "similarity function" via its Q, K projections.
  • Same computational cost as single-head, but strictly more expressive.
  • The output projection WO\mathbf{W}^O mixes information across heads.
  • GQA/MQA trade off expressiveness for KV-cache efficiency.

Exercises

Exercise 1. Verify that multi-head attention has the same FLOP count as single-head attention of dimension dd.

Exercise 2. Show that if all heads use identical projections (WiQ=WjQ\mathbf{W}_i^Q = \mathbf{W}_j^Q for all i,ji,j), multi-head reduces to single-head.

Exercise 3. Compute the total parameter count for a multi-head attention layer with d=512,H=8d=512, H=8.

Exercise 4. Derive the gradient L/WiQ\partial\mathcal{L}/\partial\mathbf{W}_i^Q through the softmax attention.

Exercise 5. For GQA with G=4G=4 groups and H=32H=32 heads, how many independent K/V projections are there? What is the KV-cache size relative to full MHA?