The bottleneck nobody sees
You're waiting for a response from ChatGPT. The model generates word by word, each token takes about 50 to 100 ms. Why so slow? You might think it's a compute power problem, that the GPU is grinding away at full capacity to produce each word.
In reality, it's the opposite. During token-by-token generation, the GPU is often half idle. It's waiting. The real bottleneck is memory: for each new token generated, the model must re-read from GPU RAM the key (K) and value (V) vectors of all previous tokens, that's the famous KV cache. And reading this memory is slow compared to the GPU's compute speed.
Imagine a brilliant student (the GPU) who can solve equations very fast, but who at each step has to go fetch a book from a huge library. It's not the computation that slows them down, it's the trip to the book.
For a 70-billion parameter model with 80 layers, 64 heads, and a context of 8,000 tokens, the KV cache exceeds 20 gigabytes (the detailed arithmetic is just below). Re-reading over 20 GB of memory for each generated token, that's the real bottleneck.
Multi-Query Attention (MQA), proposed by Noam Shazeer in 2019, is an elegant and radical solution to this problem.
Fast Transformer Decoding: One Write-Head is All You NeedRefresher: standard Multi-Head Attention
In standard MHA (Multi-Head Attention), each attention head i has its own projections for queries, keys, and values:
In plain terms: is the text being processed (one vector per token), and , , are the three weight matrices of head , parameters learned during training, frozen afterwards. Applying them to the text, each head computes its own queries , keys and values , results, recomputed for every new text.
For H heads with per-head dimension , the KV cache stores elements per layer: is the number of heads, the number of tokens in the context, the size of one key (or value) vector, and the factor of 2 counts the two things being stored, the key and the value. No factor of 3: the current token's query is used once and never again, so it is never stored, the full explanation is in the multi-head attention lesson. With H = 64 heads, L = 8,000 tokens, = 128: that gives 2 x 64 x 8,000 x 128 = 131 million elements per layer. Multiply by 80 layers = 10.5 billion elements, or about 21 GB in float16 (2 bytes per element).
During autoregressive generation, each new token must "look at" all previous tokens via the attention mechanism. Without a cache, you'd have to recompute the K and V projections of the entire context at each new token, a colossal waste. The KV cache stores these projections once and for all: you cache what gets re-read at every step (the keys and values of the whole past), not what is used once (the current token's query). It's essential for speed, but it keeps growing with context length. For a 70B model with 128K tokens of context, the same arithmetic gives hundreds of GB.
The MQA mechanism: a single K/V pair for all heads
Shazeer's idea is disarmingly simple: instead of giving each head its own K and V projections, share a single K/V pair across all heads.
Reading these three lines: each head keeps its own query matrix , but there is now only one key matrix and one value matrix for everyone, hence a single and a single .
MQA can be described two ways: "the heads share the weight matrices and " (the learned parameter) or "the heads share the keys and values and " (the result computed on the text). The two formulations are equivalent: the same matrix applied to the same input necessarily produces the same result. The savings, however, are on the result side: what grows with the conversation and saturates memory is the cache of computed and , not the weight matrices, whose size never changes.
Each head still computes its own attention, with its own query:
In words: head compares its queries to the shared keys , divides by to keep the scores in a reasonable range, turns those scores into weights that sum to 1 (the softmax), and uses them to take a weighted average of the shared values .
So all heads query the same keys and values. The diversity between heads comes solely from the queries, they determine "what each head is looking for" in the context.

KV cache reduction: a factor of H
The KV cache goes from to . That's a reduction by a factor of H (the number of heads).
| Configuration | KV cache per layer | Reduction |
|---|---|---|
| MHA (H = 64 heads) | 2 x 64 x L x | Reference |
| MQA (1 K/V pair) | 2 x 1 x L x | 64 times less |
For our 70B parameter example with 8K tokens: the KV cache goes from ~21 GB to ~330 MB (21 GB ÷ 64). That's a radical change, we go from a major memory problem to something negligible.

Autoregressive LLM inference is typically memory-bandwidth bound: the GPU waits for data to arrive from HBM (High Bandwidth Memory, the graphics card's main memory), not for it to finish computations. The KV cache must be read in its entirety for each generated token. Reducing its size by a factor of 64 means 64 times fewer memory reads per token, which translates directly into much higher generation throughput.
Impact on quality: surprisingly small
The natural question: by sharing K and V across all heads, you inevitably lose expressiveness. What's the quality cost?
Research shows a modest degradation. In the original paper, translation quality stays nearly identical to the baseline; later work (notably the GQA paper) does measure a clearer drop on some demanding tasks. Why so little, given such radical sharing?
The intuition: the diversity of attention patterns comes mainly from the queries, not from keys and values. Think of attention heads as investigators querying a database. Each investigator (head) asks different questions (distinct queries), but they all consult the same files (shared K/V). Each investigator's specialization lies in what they're looking for, not in the files themselves.
Keys and values capture the same semantic information from the context regardless of the head. Sharing this context representation therefore doesn't lose much expressiveness.
Who uses MQA in production?
MQA has been adopted by several major models:
- PaLM (Google): one of the first large models to adopt it at scale
- Falcon 7B (TII): pure MQA; Falcon 40B already uses the grouped variant
- StarCoder: code generation model with MQA
Converting MHA to MQA after training
You have an already-trained MHA model and want MQA's benefits without starting from scratch? It's possible:
- Take the H K and V projection matrices from the H heads
- Average them into a single shared projection
- Briefly retrain the model, at about 5% of the original training budget

Google demonstrated (in the GQA paper, which also tests conversion to MQA) that this weight averaging, followed by a short additional training run at 5% of the original pre-training compute, recovers nearly all of the original MHA model's performance. You get MQA's inference gains without paying the full training cost again, an excellent investment/benefit ratio.
Advantages and limitations
What MQA brings:
- KV cache reduction by a factor of H, the number of heads (32 to 128 depending on the model)
- Significant acceleration of token-by-token generation
- Minimal architectural modification, easy to implement
- Compatible with Flash Attention and other inference optimizations
What MQA sacrifices:
- Heads can no longer specialize their K/V representations independently
- Slight measurable quality degradation, especially on tasks requiring very fine attention
- Doesn't reduce training cost (queries remain distinct, computation is the same)
For frontier models where every tenth of a benchmark point matters, MQA's quality loss can be unacceptable. The middle-ground solution: Grouped Query Attention (GQA), which shares K/V among groups of heads (for example 8 groups of 8 heads) rather than across all heads. GQA offers a tunable trade-off between MHA's maximum quality and MQA's maximum efficiency. It has become the de facto standard in recent models (LLaMA 2/3, Mistral, etc.).
Final comparison: MHA vs MQA
| Aspect | MHA | MQA |
|---|---|---|
| K, V projections | H distinct (H = 32 to 128) | 1 single shared |
| KV cache per layer | 2 × H × L × | 2 × L × |
| Quality | Reference | Slightly lower, mostly on fine-grained tasks |
| Memory reads per token | Reference | Divided by H |
| Latency per token | Reference | Significantly reduced |
MQA is an elegant architectural optimization that directly tackles the real bottleneck of LLM inference: memory bandwidth. Its idea (sharing K/V across all heads) paved the way for GQA, which refines this quality/efficiency trade-off to make it acceptable even for the most demanding models.
1. During token-by-token generation, the GPU is often half idle. Why?
2. MQA is sometimes described as the heads sharing the matrices W_K and W_V, sometimes as the heads sharing the keys and values K and V. What should you make of this?
3. Despite such radical sharing, quality drops surprisingly little. What intuition does the page give?
4. You have an already-trained MHA model and want MQA's benefits. What do you do?
5. For a frontier model where every tenth of a benchmark point matters, MQA's quality loss can be unacceptable. What is the middle-ground solution?