What Is Grouped Query Attention? Cheaper AI Memory
Grouped query attention is a small change to the attention layer that cuts the memory a long conversation needs. The arithmetic shows how much.
Grouped query attention (GQA) is a variation of the transformer attention layer in which several query heads share a single set of key and value heads. It sits between multi-head attention, where every query head has its own keys and values, and multi-query attention, where all query heads share just one. The result is a much smaller KV cache for nearly the same quality.
If you have wondered why newer models handle long conversations without needing enormous amounts of memory, this layer is one of the reasons. The idea is simple enough to do the arithmetic on a napkin, which is what the rest of this post does.
Grouped query attention vs multi-head vs multi-query
Attention works with three things per head: queries, keys and values. In standard multi-head attention (MHA), a model with 32 heads has 32 sets of queries, 32 sets of keys and 32 sets of values.
Multi-query attention (MQA) keeps 32 query heads but shares a single key head and a single value head across all of them. It makes decoding faster because there is far less to load from memory for each new token.
Grouped-query attention (GQA) splits the difference. With 32 query heads and 8 key-value heads, each key-value head serves a group of 4 query heads.
The paper that introduced GQA by Ainslie and colleagues, accepted at EMNLP 2023, states the tradeoff directly: MQA speeds up decoder inference but "can lead to quality degradation", while GQA uses an intermediate number of key-value heads and "achieves quality close to multi-head attention with comparable speed to MQA". The same paper shows how to convert an existing multi-head checkpoint into a multi-query model using about 5 percent of the original pre-training compute.
Why the key-value heads matter: the cache
When a model generates text, it keeps the keys and values of every earlier token in memory so it does not recompute them. That store is the KV cache, and it grows with every token in the conversation. Queries are not cached, only keys and values. So shrinking the number of key-value heads directly shrinks the cache.
The cache size per token is:
2 (keys and values) x layers x key-value heads x head dimension x bytes per number
The arithmetic for one illustrative model
Take a hypothetical model with 32 layers, 32 query heads, a head dimension of 128 and 16-bit numbers (2 bytes). This is a configuration in the range of a 7 billion parameter model, chosen for round numbers, not a description of any particular release.
Variant | Key-value heads | Cache per token | At 8,192 tokens | At 32,768 tokens |
|---|---|---|---|---|
Multi-head (MHA) | 32 | 512 KiB | 4.0 GiB | 16.0 GiB |
Grouped-query (GQA) | 8 | 128 KiB | 1.0 GiB | 4.0 GiB |
Multi-query (MQA) | 1 | 16 KiB | 0.125 GiB | 0.5 GiB |
Going from 32 key-value heads to 8 cuts the cache to a quarter. At 32,000 tokens, that is the difference between a cache that alone needs 16 GiB and one that needs 4 GiB. That is per conversation, so a server handling many users at once feels it multiplied.
The table counts only the cache, not the model weights, so this saving is separate from compressing the weights. The two stack: quantization shrinks the weights, GQA shrinks the cache.
Why not just use MQA?
Fewer key-value heads means less capacity to attend to different aspects of the text in parallel. MQA pushes that to the extreme and the GQA paper reports that it can hurt quality. GQA keeps several key-value heads, so each group can specialize a little, while still getting most of the memory saving. It is a dial, not a switch: the number of groups is a design choice a model's builders make.
What this means when you choose or run a model
Long context becomes affordable. A model using GQA can serve a long context window with less memory per user, which shows up as lower cost or more simultaneous conversations on the same hardware.
It is a published design decision, not a setting you control. You cannot switch it on for a model that was not built with it. You can read a model's configuration or technical report to see how many key-value heads it uses, when the publisher shares that.
It complements other efficiency work. FlashAttention changes how attention is computed on the chip. Mixture of experts changes how much of the network runs per token. GQA changes how much has to be remembered. They solve different bottlenecks.
For the surrounding machinery, how AI models work is the place to start.
A caution on the numbers
The table is arithmetic on a hypothetical configuration, and real systems add overhead: some store the cache at lower precision, some share or compress it further, and serving software reserves extra space. Use the table for the ratios between the variants, which hold regardless of the particular model, and use the publisher's figures for any absolute memory number.
FAQ
What is the difference between GQA and multi-head attention?
Multi-head attention gives every query head its own key and value heads. GQA lets groups of query heads share key and value heads, so there are fewer of them to store.
Does grouped query attention reduce quality?
The GQA paper reports quality close to multi-head attention with speed comparable to multi-query attention. Some loss is possible compared with full multi-head attention, depending on the model and the number of groups.
Is GQA the same as multi-query attention?
No. Multi-query attention is the special case with exactly one key-value head. GQA uses an intermediate number, more than one but fewer than the number of query heads.
Does GQA make training or inference faster?
The paper's main claim concerns decoder inference, where loading the cache from memory is a bottleneck. Fewer key-value heads means less to load per generated token.
How did this land?
About the author

Senior Editor, AI & Product
Cecilia leads the Swarmz editorial desk. She has spent a decade turning complex AI and product topics into writing people actually finish, and she owns the blog's quality bar.


