TL;DR
Multi-head attention is attention run several times in parallel, with each run (a head) working on its own learned projection of the input. The head outputs are concatenated and passed through one more matrix that mixes them back to the model's width. Each head gets its own attention weights, so different heads can pick out different relations between positions. The total parameter count is about the same as one attention operation at full width.
Where you'll see it: every layer of the Transformer, in both its encoder and decoder, and in most large language models and vision transformers built on it.
Intuition
Imagine reading a sentence with several people at once, each with a different question. One is tracking who the subject is, another is looking for the verb that agrees with it, and a third is finding the matching bracket or the word that the pronoun refers to. Each reader writes down what they found, and the notes are then combined into one summary.
A single attention operation gives each query one set of weights over the other positions. That is one distribution, so one query has to commit to one pattern of focus. It can be a mixture of patterns, but the mixture is shared by all its uses. Several heads let the same query hold several separate focus patterns at once, each shaped by its own learned projections.
Splitting the work is also cheap. If the model width is and there are heads, each head works in a smaller space of size . Each head is cheaper than one full-width head, and the total cost stays close to a single full-width attention. The model gets diversity without paying for it in size.
flowchart LR X[Input x, width d] --> H1[Head 1: own Q, K, V] X --> H2[Head 2: own Q, K, V] X --> HN[Head h: own Q, K, V] H1 --> C[Concatenate, width d] H2 --> C HN --> C C --> O[Output projection W O]
The math
Let the input be a matrix with one row per position, where is the sequence length and is the model width. There are heads. Each head has three learned projection matrices, , and , with by convention. The symbol is the width of each query and key, and is the width of each value.
Each head is an ordinary scaled dot-product attention on projected inputs:
The heads are concatenated along the width and mixed by an output matrix :
The result has shape , the same as the input, so the layer can be stacked.
The softmax is applied row by row, so each position has its own distribution over the other positions, separately in each head. The scaling is the one from single-head attention and has the same purpose there: it keeps the scores from growing with and saturating the softmax.
Shapes and cost
Each head's score matrix has size , so the total score memory is values per layer. The projections cost and the score products cost , the same order as single-head attention with full width. The price of extra heads is in the number of score matrices held in memory, not in the projection arithmetic.
Parameter count
With the base-model values from the original paper, and , each head has . Each head has three projections of size , and the output matrix has size . Splitting into heads gives the same total as one head of full width, because the eight narrow projections together have the same number of weights as one wide one. That is why the heads are essentially free.
Worked examples
Use , and .
- Each head's query projection has size weights. Keys and values add two more of the same size, so one head has projection weights.
- Eight heads give projection weights.
- The output matrix has size weights.
- Total per layer, ignoring biases: .
- A single head of full width would use three projections of size , which is weights, plus an output matrix of if one is used. The total is the same as the eight-head version.
Interpretation: the heads add separate patterns of focus without adding parameters. The extra cost is the eight separate score matrices, which grows with sequence length.
Let , , so . The query token is and the other token is . The scaling factor is . The output projection is the identity, so the concatenated heads are the output.
Head 1 uses , and .
- Query: .
- Keys: and . Scores: .
- Softmax: .
- Values: and .
- Head 1 output: .
Head 2 uses , and .
- Query: .
- Keys: and . Scores: .
- Softmax: .
- Values: and .
- Head 2 output: .
Concatenated output for the query token: .
Interpretation: head 1 is mostly looking at and returns a value near 2, but it also gives a weight, which pulls the result toward 4. Head 2 looks mainly at , whose value is 0, so it returns only a small amount. The two heads disagree about where to look, and both views reach the output.
Tradeoffs and failure modes
More heads are not always better. With a fixed width, each head is narrower, so each head's query and key have fewer dimensions to match on. Very many narrow heads can each learn a weak, noisy pattern. In practice the head count is a tuning choice, and it is often set by what the hardware handles efficiently.
The main cost in deployment is memory for keys and values during generation. Each head keeps its own keys and values for every past position, so the cache grows with the number of heads.
| Situation | Problem | Better choice |
|---|---|---|
| Serving long contexts with many heads | The key-value cache grows with heads times length | [[multi-query-attention |
| Long sequences in training or inference | Score matrices grow with the square of the length | Memory-efficient exact kernels such as [[flashattention-paper |
| Small model or short sequences | Heads are narrow and each learns a weak pattern | Fewer, wider heads |
| A task with one clear relation | Extra heads add little beyond one good head | A single head is enough |
Analysis of trained models has found heads that track the previous token, heads that follow syntactic links, and heads that look for repeated content. These patterns are observed after training. The architecture does not assign them in advance, and not every head has a clear role.
History
Attention in neural networks came first in sequence-to-sequence translation. Bahdanau, Cho and Bengio introduced additive attention in 2014 so that a decoder could look back at the encoder's states. Luong and colleagues in 2015 studied simpler scoring functions. Vaswani and colleagues introduced multi-head attention in 2017 in Attention Is All You Need, using it with self-attention and removing recurrence entirely. Their base model used eight heads. The design became the core of the transformer and of most large language models that followed.
Check yourself
QuestionIn multi-head attention, what does the output matrix do after the heads are concatenated?Show answer
It mixes the concatenated head outputs, which together have width , back into the model width . This lets information from different heads combine in any proportion. It also makes the output the same shape as the input, so the layer can be stacked.
QuestionA model has width and heads, with . How many projection weights does one head's query projection have, and what is the total for all heads of the query projection?Show answer
Each head's query projection maps width to width , so it has weights. Across 12 heads the total is , which equals . The total is the same as one full-width query projection, so splitting into heads does not change the count.
QuestionWhy can one attention head with one softmax find it hard to capture two different relations for the same query?Show answer
Each head gives a query a single set of weights that sum to 1. If the query should attend strongly to two unrelated positions, the weight has to be split between them, and each relation gets less than it would alone. Separate heads each produce their own set of weights, so each can sharpen on one pattern without giving up the other.
QuestionApplied: you are serving a model with 32 heads and very long prompts, and the GPU runs out of memory during generation. Which part of the attention layer is the likely cause, and what design change targets it?Show answer
The likely cause is the key-value cache, which stores keys and values for every head and every past position. With 32 heads, that is 32 sets of keys and values per position. Multi-query attention targets this by sharing a single key and value across all query heads, which cuts the cache by roughly the number of heads. A memory-efficient kernel such as FlashAttention targets the score matrix instead, which is a different cost.
Sources
- Vaswani et al., Attention Is All You Need (2017): https://arxiv.org/abs/1706.03762
- Attention (machine learning), Wikipedia: https://en.wikipedia.org/wiki/Attention_(machine_learning)