Multi-Head Attention › How Multi-Head Attention Works
Language AI HandbookAll chapters Multi-Head Attention › How Multi-Head Attention WorksOne section · Sections in this chapter3 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Multi-head attention divides the model's capacity across parallel attention heads. Each head operates on a reduced dimension, so the total computation remains comparable to single-head attention with the full dimension.
The process unfolds in four steps:
- Project the input into separate QKV representations
- Compute scaled dot-product attention independently for each head
- Concatenate all head outputs into a single tensor
- Project the concatenated output back to the model dimension
Let's trace through each step with concrete mathematics and then implement it in code.
Step 1: Parallel Projections
Given an input sequence with tokens and model dimension , we project it into QKV representations for each head. If we have heads, we typically set the head dimension to . This division ensures that the total computation across all heads remains comparable to a single attention operation over the full dimension.
For head , we compute the QKV matrices through learned linear projections:
where:
- : the input sequence with tokens, each represented as a -dimensional vector
- : the learned query projection matrix for head , transforming each token into a -dimensional query vector
- : the learned key projection matrix for head , transforming each token into a -dimensional key vector
- : the learned value projection matrix for head (often )
- : the resulting query and key matrices for head
- : the resulting value matrix for head
- : the per-head dimension, typically
Each head gets its own projection matrices. This allows it to learn different transformations of the input. Head 1 might project tokens to emphasize syntactic features, while head 2 emphasizes semantic similarity. different projection matrices create different "views" of the same input, each potentially highlighting different aspects of the token relationships.
Step 2: Independent Attention Computation
Each head computes standard scaled dot-product attention using its own projections. The attention mechanism compares queries against keys to determine relevance weights, then uses those weights to aggregate values:
where:
- : the output of attention head , containing contextual representations for all tokens
- : the matrix of raw attention scores, where entry measures how much query at position matches key at position
- : the scaling factor that prevents dot products from growing too large as increases (covered in the previous chapter on scaled dot-product attention)
- : applied row-wise to convert scores into attention weights that sum to 1 for each query position
- : the value matrix whose rows are aggregated according to the attention weights
This produces separate outputs, each of shape . The heads operate completely independently at this stage; there is no information sharing between them. This independence is what allows different heads to specialize in detecting different types of relationships.
Step 3: Concatenation
After computing all heads, we concatenate their outputs along the feature dimension. Each head produces an matrix, and stacking these side-by-side creates a larger matrix:
where:
- : the individual head outputs, each of shape
- : the concatenated feature dimension, combining all heads' outputs
If , then , and the concatenated output has the same dimension as the original input. This dimensional consistency is intentional: it allows multi-head attention to be used as a drop-in replacement for single-head attention. The concatenation preserves each head's contribution separately, letting the subsequent projection learn how to combine them.
Step 4: Output Projection
Finally, we apply a learned linear projection to the concatenated output. This projection maps the combined head outputs back to the model dimension:
where:
- : the concatenated outputs from all attention heads
- : the learned output projection matrix
- : the final output, matching the input dimension
This projection serves two purposes. First, it combines information from all heads, allowing the model to learn how to mix their contributions. A head specializing in syntax might contribute more heavily for certain tokens, while a semantic head dominates for others. Second, it ensures the output dimension matches the input dimension, enabling residual connections in the transformer architecture.