The all-or-nothing problem
MQA (Multi-Query Attention, previous lesson) is great for efficiency, but sharing a single K/V pair across all heads might be too radical. It's like having one waiter for an entire restaurant, quick to coordinate, but the service suffers when tables have different needs.
MHA (Multi-Head Attention, the standard mechanism) is perfect for quality, but its gigantic KV cache, the memory of keys and values of every token seen so far, re-read at every new token (why K and V are cached, and Q never is) limits what you can do in practice. Each head has its own set of keys and values, it's like a dedicated waiter per table, impeccable service, but the payroll explodes.
What if you didn't have to choose? What if you could have as many K/V groups as you want, from 1 (MQA) to H (MHA), including any value in between?
That's Grouped Query Attention (GQA), introduced by the Google team, and now adopted by virtually all modern open-source LLMs.
GQA: Training Generalized Multi-Query Models from Multi-Head CheckpointsThe GQA principle
Grouping the heads
Instead of one K/V pair per head (MHA) or one K/V pair for everyone (MQA), GQA creates G groups, each group sharing a K/V pair:
Reading these three lines: each head keeps its own query matrix (a learned parameter), but there are now only G key matrices and G value matrices , one per group. The H heads are divided into G groups of H/G heads each. All heads within the same group therefore use the same K and V, but keep distinct queries. Each head asks its own "question" to the context, but the context is represented by a reduced number of K/V perspectives.
Note in passing that saying "the heads of a group share the matrix " or "they share the keys " is saying the same thing: one matrix applied to one input gives one result. The distinction that matters is elsewhere: the matrix is a learned parameter with a fixed size; the keys and values are computed results on the current text, piling up in the cache with every token. That cache (and it alone) is what GQA shrinks.

The MHA -> GQA -> MQA continuum
GQA elegantly unifies MHA and MQA as extreme cases of the same mechanism:
- G = H: each group contains exactly one head -> this is standard MHA
- G = 1: a single group contains all heads -> this is MQA
- 1 < G < H: intermediate configuration -> this is GQA
You turn a single knob (the number of groups G) to navigate the entire quality-efficiency spectrum.

The dominant configuration in modern models is G = H/8, that is, 8 groups for 64 heads (each group contains 8 heads sharing the same K/V). The KV cache is reduced by a factor of 8 compared to MHA, while maintaining 8 distinct K/V sets to capture different aspects of the context. This is the choice of LLaMA 2 70B, Mistral 7B, and many other reference models.
KV cache reduction: the real numbers
For a model with H = 64 heads, d_k = 128, and L context tokens:
| Configuration | G | KV cache per layer per token | Reduction vs MHA |
|---|---|---|---|
| MHA | 64 | 2 x 64 x 128 = 16,384 elements | 1x |
| GQA-8 | 8 | 2 x 8 x 128 = 2,048 elements | 8x |
| GQA-4 | 4 | 2 x 4 x 128 = 1,024 elements | 16x |
| MQA | 1 | 2 x 1 x 128 = 256 elements | 64x |
In the middle column, the factor of 2 counts the two objects stored per K/V head and per token: the key and the value (never the query, which is used only once).
Let's put this in concrete perspective by finishing the arithmetic: 16,384 elements × 128,000 tokens × 80 layers × 2 bytes ≈ 335 GB of GPU memory for the MHA cache of a single request. With GQA-8, the same arithmetic gives ~42 GB. The difference (nearly 300 GB) is several 80 GB graphics cards saved, a direct impact on deployment cost.
In practice, G = 8 offers an excellent balance for large models: 8x KV cache reduction with nearly imperceptible quality loss on benchmarks. For very memory-constrained deployments (mobile, edge), G = 4 or G = 2 are viable with moderate degradation. G = 1 (MQA) remains useful for the most extreme cases where every megabyte counts. The rule of thumb: start with G = 8 and reduce only if memory requires it.
Converting MHA -> GQA: reusing existing models
One of GQA's most important practical advantages: you can convert an existing MHA model to GQA without retraining from scratch. This is crucial when you've invested millions of dollars in an MHA training run.
The uptrained conversion process
Uptrained conversion refers to converting an already-trained MHA model to GQA, followed by a short retraining, you don't start from scratch.
- Group the heads: distribute the H heads into G groups of consecutive heads
- Average the K/V: within each group, compute the average of the K and V weight matrices of the H/G constituent heads
- Retrain briefly: continue training the converted model at ~5% of the original pre-training compute to recover performance

In the paper, the conversion is done on Google's T5-XXL model (not on LLaMA): converted to GQA-8 and then retrained at 5% of the original compute, it recovers quality close to the original MHA model, at inference speed close to MQA's. The return on investment is enormous: a fraction of the training run versus months of full training.
Averaging vs selection
The paper compares three ways of forming a group's K/V:
- Averaging: the group's K projection is the average of the K projections of the constituent heads. This approach preserves information from all original heads and gives the best results.
- Selection: keep the first head of the group, discard the others. Simpler, slightly worse.
- Random reinitialization: start from fresh matrices. Clearly worse, the knowledge in the original heads is worth keeping.
The industry standard: who uses GQA?
GQA has become the de facto standard for open-source LLMs since mid-2023. The list speaks for itself:
- LLaMA 2 70B (Meta): first major model to popularize GQA (G = 8 for H = 64)
- LLaMA 3 (Meta): GQA across the entire family, from 8B to 405B
- Mistral 7B: GQA with G = 8 for H = 32 (8 K/V heads for 32 query heads, i.e. groups of 4)
- Gemma (Google): GQA across all sizes
- Qwen 2 (Alibaba): GQA as the standard configuration
- Command R (Cohere): GQA for inference efficiency
GQA is the first mechanism to offer a quality-efficiency trade-off good enough to be adopted without hesitation by teams targeting top performance. MQA was too aggressive, the quality loss became visible on demanding tasks. GQA-8 is practically indistinguishable from MHA on benchmarks, while dividing the KV cache by 8. It's the rare case where you can have your cake and (almost) eat it too.
Why does GQA work so well?
K/V redundancy
The usual intuition: the keys and values produced by different heads overlap substantially, they capture essentially the same semantic information with slight variations. Grouping them by averaging therefore only destroys a small fraction of useful information. This is consistent with an observation made in early studies of multi-head attention: entire heads can be removed from a trained model without a collapse in performance.
Queries carry the diversity
The real diversity of attention patterns comes from the queries: different heads ask different questions about the same content. One head asks "what's the grammatical subject?", another "what's the main verb?", a third "is there a cause-and-effect relationship?". These questions remain entirely distinct per head in GQA, only the K/V "dictionary" they search is shared within a group.
Sufficient residual capacity
With G = 8 K/V groups, the model still has 8 distinct representations of the context. That's enough to capture the different aspects needed: syntax, semantics, coreference, logical relationships, etc. The real loss only becomes noticeable below G = 4, on tasks that require multiple simultaneous perspectives on the context.
Combinations with other optimizations
GQA isn't an isolated technique, it combines naturally with other optimizations, and the gains multiply:
GQA + Flash Attention: perfectly compatible. Reducing the number of K/V heads reduces the size of manipulated tensors, which amplifies Flash Attention's gains by reducing memory transfers.
GQA + KV cache quantization: quantizing the KV cache in INT8 or INT4 further multiplies the reduction. The two techniques are orthogonal: GQA reduces the number of K/V groups, quantization reduces the precision of each value. Together, you can achieve 32x or more reduction (8 from GQA-8 × 4 from INT4).
GQA + Sliding Window Attention: Mistral combines GQA with a sliding context window. Double lever: fewer K/V groups and K/V history truncated to the window size.
With G = 1 or G = 2, the loss of K/V diversity can become significant, particularly for tasks requiring fine attention on multiple distinct aspects of the context simultaneously, complex multi-step reasoning, questions about long documents with many varied details, or translation between structurally very different languages.
Comparative performance
The paper's measurements on converted T5-XXL sum up as follows:
| Model | Attention | Quality | Inference speed |
|---|---|---|---|
| T5-XXL (MHA) | MHA | Reference | Slowest |
| T5-XXL -> GQA-8 | GQA-8 | Close to MHA | Close to MQA |
| T5-XXL -> MQA | MQA | Behind on several tasks | Fastest |
GQA-8's trade-off is crystal clear: a barely measurable quality loss for speed nearly equal to MQA's. For the vast majority of applications, that's a highly profitable exchange, you serve far more users with the same hardware.
Conclusion
Grouped Query Attention represents a pragmatic and decisive advance for LLM inference optimization. By offering a continuum between MHA (maximum quality) and MQA (maximum efficiency), it allows practitioners to choose exactly the optimal trade-off for their use case. Its near-universal adoption confirms that GQA has become a standard component of every modern Transformer architecture, as fundamental as layer normalization or residual connections.
1. In GQA you set the number of groups G. What happens at the two ends of the dial?
2. A group's matrix W_K versus the keys K_g computed from it: what is the difference, and which of the two does GQA shrink?
3. To convert an already-trained MHA model to GQA, which recipe gives the best results?
4. Why can K/V be shared within a group without a large quality loss?
5. When does GQA's quality loss become truly noticeable?