The problem GQA solves
During generation a transformer must remember the keys and values of every token it has already processed, because the next attention step needs them. That store is the key-value cache, and its size is 2 x layers x kv_heads x head_dim x tokens x batch x bytes per element. The factor of two is for keys and values; everything else is what it sounds like.
The numbers get uncomfortable fast. A 70B model with 80 layers, 64 key-value heads of width 128, at 4,096 tokens in fp16 needs 2 x 80 x 64 x 128 x 4,096 x 2 bytes = 10 GiB for a single conversation. Four concurrent users exceed 40 GiB before any weights are loaded. Because decode reads the entire cache for every generated token, the cache is not just a memory problem - it is the bandwidth bottleneck that decides tokens per second.
GQA attacks both at once by not storing so much. Instead of one key and value per query head, several query heads share a single key-value head. With 64 query heads and 8 key-value heads, the cache shrinks eightfold: 1.25 GiB instead of 10 GiB for the same conversation, and eight times less memory traffic per decoding step.
The three points on the spectrum
Multi-head attention (MHA), the original design, gives every query head its own key and value projection. It is the most expressive option and the most expensive to cache: cache size scales linearly with the number of query heads.
Multi-query attention (MQA), proposed by Noam Shazeer in 2019, goes to the other extreme: all query heads share one key and one value head. The cache becomes tiny and decoding gets dramatically faster, especially at long context. The cost is quality and training stability - MQA models are more prone to loss spikes and typically lose a point or two on benchmarks, and the single key-value head becomes a bottleneck for what different heads can attend to.
Grouped-query attention (Ainslie et al., 2023) sits between them. With H query heads and G key-value groups, each group serves H/G query heads. G = H is MHA; G = 1 is MQA. The paper's useful practical finding was that an existing MHA checkpoint can be converted by mean-pooling the key and value projections within each group, then continuing pretraining for about 5% of the original pretraining compute - a cheap upgrade rather than a retrain from scratch.
The arithmetic, worked through
Take a 70B-class model: 80 layers, d_model 8,192, 64 query heads, head_dim 128, and 8 key-value groups. Cache per token = 2 x 80 x 8 x 128 = 163,840 elements, which is 320 KiB in fp16. At 4,096 tokens that is 1.25 GiB. The MHA equivalent with 64 key-value heads is eight times larger: 2.5 MiB per token, or 10 GiB per sequence.
Parameters shrink too, though less dramatically. The query projection is d_model x (H x d_head) = 8,192^2 ~ 67M; each key and value projection is d_model x (G x d_head) = 8,192 x 1,024 ~ 8.4M. With MHA those would each be 67M, so GQA saves about 117M parameters per layer - roughly 9.4B across 80 layers, a meaningful fraction of a 70B model that can be spent elsewhere.
Compute barely changes. The attention score and value products still involve all 64 query heads, so FLOPs per token are nearly identical. What changes is memory capacity and memory traffic, and in decode those are what you are actually waiting on. This is why GQA improves throughput far more than it improves raw arithmetic efficiency.
Where the cache formula comes from
It is worth deriving the formula once so you never have to look it up. For every layer, every token that has been processed leaves behind one key vector and one value vector for each key-value head. Each vector holds head_dim numbers. So elements per token per layer = 2 x kv_heads x head_dim, and across the whole model it is 2 x layers x kv_heads x head_dim.
Multiply by bytes per element to get bytes per token. In fp16 that is two bytes; fp8 or int8 halves it; 4-bit roughly quarters it. Then multiply by the number of tokens in the sequence and by the number of sequences being served concurrently, and you have your cache budget.
A 7B-class model with 32 layers, 8 key-value heads and head_dim 128 stores 2 x 32 x 8 x 128 = 65,536 elements per token - 128 KiB in fp16. A 32K-token conversation needs 4 GiB, so 32 concurrent conversations need 128 GiB. That single calculation explains why long-context products are expensive to serve, why providers charge more for large inputs, and why 4-bit cache quantisation is worth the engineering effort.
Why sharing keys and values costs so little quality
Query heads specialise: one tracks the previous token, another matches brackets, another aggregates sentiment. Key and value heads appear to specialise much less. The information a head needs to retrieve - what token is at position j, what its hidden state looks like - is broadly similar across heads, so pooling the retrieval side loses less than pooling the query side would.
Empirically the gap is small. The GQA paper reported quality close to multi-head attention on summarisation, translation and QA while running at multi-query speed, using uptrained T5-XXL checkpoints. Open models from 7B to 70B adopted the finding, with the caveat that aggressive grouping (G = 1 or 2) does degrade long-context retrieval more than short-answer benchmarks reveal.
The residual risk is not accuracy on average but tails: tasks that need several heads attending to different distant positions simultaneously can suffer when those heads share one key-value subspace. If your workload is long-document retrieval, evaluate at your real context length rather than trusting a short-prompt benchmark.
Interaction with tensor parallelism
Serving a model across GPUs splits attention heads across devices. With 8 key-value heads, a tensor-parallel degree above 8 forces replication of the key-value cache on several devices, which wastes exactly the memory GQA was meant to save. This is the practical reason 8 is such a common group count: it maps cleanly onto an 8-GPU node.
If you plan to serve on 16 or 32 GPUs, check the key-value head count before choosing a model. Some architectures use 2 or 4 groups, which pushes the replication threshold down to 2 or 4 devices and can make multi-node serving inefficient. Others, such as DeepSeek's latent attention, compress keys and values into a shared low-rank latent instead of grouping heads, which changes the arithmetic but solves the same problem.
Training a GQA model from scratch
Initialisation matters more than usual. Each key and value projection should start as the mean of the query-head projections it replaces, scaled so that the variance of the attention logits matches what the model would have seen under MHA. Random initialisation of a single shared head produces much larger logits and a loss spike in the first few hundred steps.
Grouping choice should follow the head dimension you want, not the other way round. Keep head_dim at 128 where possible, because that is what fused attention kernels are tuned for, and derive the query head count from d_model. Then choose groups so that query heads divide evenly and the group count divides the tensor-parallel degree you plan to serve on.
During training, evaluate at the context length you intend to ship. GQA's quality cost grows with sequence length, because shared key-value subspaces have to serve heads that want to look at very different places at once. A 4K evaluation will tell you the model is fine when the 64K behaviour is not.
Choosing a group count
For a new model, 8 groups with a head dimension of 128 is a sane default and matches most published architectures. If generation speed at long context is the dominant constraint and you can afford a small quality regression, 2-4 groups helps further; if you are training a small model where quality is fragile, stay closer to MHA.
The decision should be made with a measurement, not a vibe: pick your real prompt and output lengths, compute cache bytes per token for each option, multiply by concurrency, and compare against available memory after weights. Then benchmark tokens per second at the batch size you actually serve.
A worked sizing exercise: 14 GB of weights for a 7B model leaves roughly 60 GB on an 80 GB card. At 320 KiB per token, 20 concurrent users with 8K contexts need 20 x 8,192 x 320 KiB ~ 51 GiB, which fits. The MHA version at 2.5 MiB per token would need 400 GiB and would not fit on any single node. That is the entire argument for GQA in one line of arithmetic.
The wider family of cache reductions
- MQA: one key-value head, maximum cache reduction, largest quality risk.
- GQA: G groups, the default in most open models since 2023.
- Multi-head latent attention: store a compressed latent per token and project up when needed; DeepSeek-V2 reported a 90%+ cache reduction.
- Cross-layer sharing: reuse the same keys and values across adjacent layers, cutting the layer factor rather than the head factor.
- Quantised caches: store keys and values in fp8 or int8, roughly halving bytes with modest quality loss - orthogonal to GQA and multiplicative with it.
- Eviction and windowing: keep only recent or high-attention tokens, which bounds cache size but changes what the model can recall.
- Paged allocation: store the cache in fixed-size blocks so fragmented sequences do not waste memory; this is the core of vLLM's throughput advantage.
How to measure the benefit honestly
Benchmark at the batch size and sequence length you actually serve. GQA's advantage is nearly invisible at 512-token prompts with batch 1, because the cache is small and the weights dominate memory traffic. At 32K tokens with 16 concurrent users it is the difference between fitting on one node and needing four.
Report three numbers, not one. Time to first token tells you about prefill and is largely unaffected by GQA. Inter-token latency at your concurrency tells you about decode bandwidth, where GQA helps most. Tokens per second per GPU at a fixed quality bar tells you the actual unit economics. A model that is 30% faster per token but needs two extra points of quality recovery through retries is not faster.
Also measure cache occupancy, not just speed. Serving engines such as vLLM report cache block usage; if you are at 90% occupancy you are one long request away from preemption and a latency cliff. Headroom of 20-30% is normal practice, and it is cheaper than the incident review that follows a traffic spike.
Common mistakes
- Confusing query head count with key-value head count when sizing memory - the cache depends only on the latter.
- Assuming GQA reduces attention FLOPs. It does not; it reduces memory and bandwidth.
- Expecting GQA to extend usable context. It makes long context affordable, not more accurate.
- Ignoring tensor-parallel replication, then wondering why 16-way serving uses more memory per GPU than 8-way.
- Benchmarking only short prompts, where the cache is small and GQA's advantage is invisible.
- Forgetting that quantising the cache and shrinking it are independent levers that stack.
- Reading a paper's "64 heads" claim without checking its key-value head count, then mis-sizing the cluster by a factor of eight.
- Treating GQA and MHA checkpoints at the same parameter count as interchangeable - the projection shapes differ, so weights do not transfer.
What it means in practice
GQA is the clearest example of a change that costs almost nothing in quality and buys a large improvement in unit economics. It is why a modern 8B model can serve dozens of concurrent long conversations on one GPU, and why the 2023 generation of open models felt so much cheaper to run than the 2022 generation at similar parameter counts.
For a team building on top of these models, the practical consequence is that context length is priced in memory, not just in tokens. Long prompts with many concurrent users are a capacity decision as much as a cost decision, and the key-value head count is the number that tells you how bad it will be.