TL;DR
Multi-query attention keeps many query heads but gives them all one shared key head and one shared value head. Each query head still has its own query projection, so it can ask a different question of the same context. The saving is in memory: the keys and values that must be stored during generation shrink by the number of heads.
It was proposed to make text generation faster. Decoding reads the whole cache for every new token, so a smaller cache means less memory traffic per step.
Where you'll see it: in the attention layers of large language models that are served at scale, where the KV Cache is the main limit on how many requests a GPU can hold.
Intuition
Picture a meeting where eight analysts each want to question the same set of documents. In Multi-Head Attention, every analyst gets their own copy of the documents, indexed the way they like. Eight copies of the index sit on the desk. In multi-query attention, the documents are indexed once and everyone uses that one index, but each analyst still brings their own question.
The questions are where the diversity lives. Each head's query decides what it looks for, so the heads can still focus on different things. What they lose is the freedom to index the context differently. Every head sees the same keys and the same values.
Why does this matter? During generation the model produces one token at a time. For each new token, every attention layer must read the stored keys and values of all earlier tokens. That cache sits in GPU memory, and reading it is often the slowest part of the step, not the arithmetic. If the cache is eight times smaller, there is eight times less to read per token and eight times more room to hold other requests.
flowchart LR
subgraph MHA[Multi-head attention]
Q1[Query 1] --- K1[Key 1]
Q2[Query 2] --- K2[Key 2]
Q3[Query 3] --- K3[Key 3]
end
subgraph MQA[Multi-query attention]
QA[Query 1] --- KS[Shared key]
QB[Query 2] --- KS
QC[Query 3] --- KS
end
The left side stores one key per head. The right side stores one key for all heads. The values are shared the same way, and the picture is the same for them.
The math
Use the notation from Multi-Head Attention. The input is , where is the sequence length and is the model width. There are query heads, and each head works in width . The symbol is the number of query heads, and is the width of each query and key.
Each query head has its own projection . All heads share one key projection and one value projection :
The heads are concatenated and mixed by , exactly as in multi-head attention:
The only change from multi-head attention is that and lose their head index . The score and softmax steps are the same. Each head still computes its own score matrix, because each head has its own query.
Parameters
In multi-head attention, the key and value projections together have weights, since . In multi-query attention they have weights. The query projections and the output matrix are unchanged. The saving is therefore a factor of on the key and value projections, which are a minor part of the layer's size.
The cache
The part that matters is the KV Cache. For one layer and one token, the cache stores one key and one value for each key-value head:
The ratio between the two cases is exactly . If a model has layers and each number takes bytes, the total cache for a sequence of tokens is times the numbers per token per layer. Multi-query attention divides this total by .
Cost of the score matrices
Multi-query attention does not change the number of score entries. Each query head still computes an matrix during training or prompt processing, so the arithmetic for the scores is the same. The gain is in what has to be stored and read between tokens during generation.
Worked examples
Take the two-token setup from Multi-Head Attention: , , . The tokens are and . The output projection is the identity. Head 1 uses , and head 2 uses . In multi-query attention, both heads share and .
- Shared keys: and .
- Shared values: and .
- Head 1 query for token : . Scores: .
- Softmax of is .
- Head 1 output: .
- Head 2 query for token : . Scores: .
- Softmax of is .
- Head 2 output: .
- Concatenated output for token : .
Compare with multi-head attention on the same input, where head 2 had its own keys and values and returned . With one shared key, head 2 can no longer favour by itself. Its query is zero, so it spreads its focus evenly over both tokens. The two heads still differ, but only through their queries.
The cache tells the same story. Multi-head attention stores four numbers per token here: a key and value for each head. Multi-query attention stores two: one key and one value. The ratio is .
Interpretation: sharing keys and values forces all heads to look at the same index. The heads can still choose different focus patterns, but the choice is limited to what the shared keys allow.
Take a hypothetical model with layers, width , and heads, so . Store values in 16-bit floats, so bytes. Serve a single sequence of tokens.
- Multi-head attention stores numbers per token per layer.
- Bytes per token across all layers: bytes, which is 512 KiB.
- For tokens: bytes, which is 4 GiB.
- Multi-query attention stores numbers per token per layer.
- Bytes per token across all layers: bytes, which is 16 KiB.
- For tokens: bytes, which is 128 MiB.
- The ratio is GiB MiB .
Interpretation: the one sequence drops from 4 GiB of cache to 128 MiB. The weights of the model are the same size either way, so the cache is the part that multi-query attention shrinks. A server that keeps many sequences in memory can hold roughly times as many. These figures are arithmetic for the hypothetical model above, not measurements from a real system.
Tradeoffs and failure modes
Multi-query attention trades a little model quality for a much smaller cache. Sharing one key and value head removes the model's ability to index the context in several ways at once. The quality loss is usually small, but it is a loss, and it grows with how aggressively the heads are shared.
The main practical question is whether to train with multi-query attention from the start, or to convert an existing multi-head model. Ainslie and colleagues showed in 2023 that an existing multi-head checkpoint can be converted ("uptrained") using about 5% of the original pre-training compute. The conversion keeps most of the quality at a much lower cost than training from scratch.
| Situation | Problem | Better choice |
|---|---|---|
| Long contexts or many concurrent requests | The KV Cache fills GPU memory | Multi-query attention, or Grouped-Query Attention to keep more key-value heads |
| Quality is the top priority and memory is not tight | Sharing keys and values costs some accuracy | Plain Multi-Head Attention |
| Existing multi-head checkpoint | Retraining from scratch is expensive | Convert the checkpoint and train briefly |
| Very long sequences in training | Score matrices grow with the square of the length | Memory-efficient exact kernels such as [[flashattention-paper |
Failure modes to watch for. A model trained with multi-head attention and then switched to multi-query attention without further training can lose more quality than expected. The shared key and value projections are new, and the model has to learn to use them. Also, multi-query attention saves memory and bandwidth during generation, but it does not reduce the score computation, which is unchanged. Judging the saving by training memory alone will give a misleading picture.
History
Noam Shazeer published Fast Transformer Decoding: One Write-Head is All You Need in 2019. The paper addressed decoding speed. The paper argued that incremental decoding is limited by the memory bandwidth needed to reload the keys and values at every step. Shazeer showed that sharing one key and value head across all query heads shrinks those tensors, and reported much faster decoding with only a slight quality loss on translation and language-modelling benchmarks.
In 2023 Ainslie and colleagues proposed grouped-query attention as a middle path. It keeps a small number of key-value groups instead of one shared head, and they showed how to uptrain multi-head checkpoints into this form, and that the grouped version reaches quality close to multi-head attention at speed close to multi-query. Multi-query attention is the extreme case of that idea, with one group.
Check yourself
QuestionIn multi-query attention, which part of the layer is shared across heads, and which part stays separate?Show answer
The key projection and the value projection are shared, so every query head reads the same keys and values. Each head keeps its own query projection , and the output matrix still mixes the concatenated heads. The shared keys and values are what reduce the cache.
QuestionA model has query heads and width , so . How many key and value numbers per token per layer does multi-head attention store, and how many does multi-query attention store?Show answer
Multi-head attention stores numbers per token per layer, which equals . Multi-query attention stores numbers. The ratio is .
QuestionWhy does multi-query attention leave the number of score entries unchanged, even though it shrinks the cache?Show answer
Each query head still has its own query, so each head still computes a score for every pair of positions, which is an matrix per head. The shared key only changes which keys those scores are computed against. The saving is in storage and reads of keys and values between generation steps, not in the number of scores.
QuestionApplied: you are serving a model with 64 query heads, and the GPU runs out of memory when many long requests run at once. Which change would you consider first, and what would you check before deploying it?Show answer
The likely cause is the key-value cache, which grows with the number of heads and the length of each request. Switching to multi-query attention would cut the cache by up to 64 times. Before deploying, check that the model was trained with this design, or that a converted checkpoint has been tested on your task. Switching an existing multi-head model without retraining can lose quality. Grouped-query attention is a milder option if the loss is too large.
Sources
- Shazeer, Fast Transformer Decoding: One Write-Head is All You Need (2019): https://arxiv.org/abs/1911.02150
- Ainslie et al., GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints (2023): https://arxiv.org/abs/2305.13245
- Vaswani et al., Attention Is All You Need (2017): https://arxiv.org/abs/1706.03762