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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- From Single-Head to Multi-Head
- Mathematical Formulation
- The Projection Geometry
- Why Multiple Heads Help
- Computational Complexity
- Attention Patterns Across Heads
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive multi-head attention from single-head by partitioning the embedding space.
- Show that multi-head attention with heads and dimension has the same FLOPs as single-head.
- Prove that multi-head attention is strictly more expressive than single-head of the same dimension.
- Analyze the rank of attention matrices and how heads increase effective rank.
- Explain Grouped-Query Attention (GQA) and Multi-Query Attention (MQA) as efficiency variants.
Notation
- — number of attention heads
- — model dimension (embedding size)
- — dimension per head
- — query/key projections for head
- — value projection for head
- — output projection
- — output of the -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
From Single-Head to Multi-Head
Single-head attention with dimension :
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 independent attention operations in lower-dimensional subspaces:
where each head operates in :
Mathematical Formulation
Given input (sequence of tokens):
For each head :
Final output:
The Projection Geometry
Each head projects the -dimensional input into a -dimensional subspace. The projections define the "similarity metric" for head :
The matrix 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 computes one attention matrix . Multi-head computes different matrices , each potentially attending to different positions. The output combines different weighted averages of 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 heads contributing rank- outputs each, the total output can have rank up to , 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 ( dimensions): Computing costs . Total: .
Multi-head ( heads, each): Each head costs . Over heads: .
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: each , plus . Total: .
Attention Patterns Across Heads
Multi-Query Attention (MQA): Share projections across all heads; only is per-head. Reduces KV-cache by factor .
Grouped-Query Attention (GQA): Group heads into groups; each group shares . Interpolates between MHA () and MQA (). Used in LLaMA-2, Mistral.
Parameter comparison:
- MHA: parameters
- MQA:
- GQA:
Common Pitfalls
Pitfall 1. Thinking heads are independent after training. While initialized independently, heads communicate through the residual stream and — they learn complementary roles.
Pitfall 2. Using too many heads with too small . If , individual heads lack capacity to compute meaningful attention. GPT-3 uses .
Pitfall 3. Not scaling by . Without this, dot products grow with dimension, pushing softmax into saturation (near-one-hot outputs).
Summary
- Multi-head attention runs parallel attention operations in -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 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 .
Exercise 2. Show that if all heads use identical projections ( for all ), multi-head reduces to single-head.
Exercise 3. Compute the total parameter count for a multi-head attention layer with .
Exercise 4. Derive the gradient through the softmax attention.
Exercise 5. For GQA with groups and heads, how many independent K/V projections are there? What is the KV-cache size relative to full MHA?