TL;DR
Grouped-query attention, or GQA, splits the query heads of a multi-head layer into groups. Every head in a group shares one key head and one value head, while each head keeps its own query. The model stores fewer keys and values per token, so the KV Cache shrinks by the sharing factor, and the quality loss is much smaller than when all heads share one key and value.
Where you'll see it: in the attention layers of large language models that need a small KV cache, where it cuts the memory needed for long contexts and large batches during generation.
Intuition
Teams sharing a filing cabinet
Picture a support centre with 32 agents. Each agent has a different question to ask about a customer's history, which is the query. The history itself is stored in a filing cabinet, the keys and values.
Full multi-head attention gives every agent a private cabinet. Each cabinet is an exact copy of the customer's history, and the storage grows with every message in the conversation. Multi-query attention gives all 32 agents one shared cabinet. That is cheap to store, but agents who need different parts of the history now compete for the same organisation of files. Grouped-query attention puts the agents into teams, say eight teams of four, and gives each team one cabinet. Each team's cabinet is organised for the questions its members tend to ask, and the storage is a quarter of the private-cabinet version (8 cabinets instead of 32).
In the model, the agents are query heads, the cabinets are key and value heads, and the teams are groups.
Why the cache is the thing to shrink
During generation the model produces one token at a time. Each new token reads every stored key and value from GPU memory. The GQA paper describes this directly: autoregressive decoder inference is a severe bottleneck because of the memory bandwidth needed to load the attention keys and values at every decoding step (Ainslie et al., 2023, section 1). The cache also takes up space, so it limits how many requests a GPU can hold at once and how long a context can get.
Reducing the number of key and value heads attacks both problems. The arithmetic work for the query heads is unchanged, but there is far less data to move and store.
flowchart LR Q1["Query head 1"] --> A["Key and value, group 1"] Q2["Query head 2"] --> A Q3["Query head 3"] --> B["Key and value, group 2"] Q4["Query head 4"] --> B
The math
Let be the number of query heads, the number of groups, and the width of one head. The sharing factor is , the number of query heads per group. Grouped-query attention requires to divide . Let be the number of layers, and the number of bytes per stored number, so for 16-bit floats.
Let be the group that query head belongs to. For an input with one row per position, each query head has its own projection , while the keys and values come from the projections of its group:
The heads are concatenated and mixed by an output matrix, exactly as in Multi-Head Attention.
Only the key and value projections are shared. Each query head still computes its own attention scores over the positions, so the layer keeps separate score matrices.
Special cases
Two settings recover the older designs.
- If , every group has one head, so each query head has its own key and value. This is Multi-Head Attention.
- If , all query heads share one key and one value head. This is multi-query attention, introduced by Shazeer in 2019, see Multi-Query Attention.
Grouped-query attention is the family in between. The paper writes GQA- for the version with groups.
The cache size
For each token, each layer stores keys and values, each of width . That is numbers per layer, so the cache per token is
Compared with multi-head attention, which uses , the cache is smaller by the factor . For a sequence of tokens, multiply by . This is the same formula as in KV Cache with the number of key and value heads substituted for the model width.
What does not change
The score computation still uses score matrices. Training compute therefore changes little, and the saving shows up mainly in memory traffic and cache size during decoding. The paper makes the same point when it notes that larger models suffer relatively less from the attention memory overhead, because the cache scales with the model dimension while the parameters scale with its square.
Converting an existing checkpoint
A GQA model does not have to be trained from scratch. The paper converts a multi-head checkpoint by mean-pooling the original key and value projections within each group. Each group key head is the average of the heads it replaces. The converted model then needs a short extra pre-training run, called uptraining. In the paper's experiments this is a fraction of the original pre-training steps, and the main results use . For the T5-XXL model in the paper, that took about 600 TPUv3 chip-days.
Worked examples
Use head width so the scale is 1. Four query heads form two groups: heads 1 and 2 share group A, and heads 3 and 4 share group B.
- Count the cached numbers for one token in one layer. Multi-head attention stores 4 keys and 4 values, so 8 numbers. GQA with 2 groups stores 2 keys and 2 values, so 4 numbers. Multi-query attention stores 1 key and 1 value, so 2 numbers.
- Ratios against multi-head attention: is a factor of , and is a factor of . These match .
- Now compute two heads that share group A. The cache holds two positions for group A: keys and values .
- Head 1 has query . Scores are . The softmax weights are and .
- Head 1 output .
- Head 2 has query . Scores are , so both weights are . Head 2 output .
Interpretation: both heads read the same two cached entries. They still produce different outputs because their queries differ, and the cache holds half the entries that multi-head attention would hold for these two heads.
Take an illustrative model with layers, query heads of width , 16-bit storage (), and a sequence of tokens. These are not the paper's numbers; they show the scale of the effect.
- Multi-head attention, : bytes per token , which is 512 KiB. For 4096 tokens, that is 2 GiB.
- GQA with : bytes per token , which is 128 KiB. For 4096 tokens, that is 512 MiB.
- Multi-query attention, : bytes per token KiB. For 4096 tokens, that is 64 MiB.
- At a batch of 16 sequences, the GQA cache is MiB GiB, down from 32 GiB with multi-head attention.
Interpretation: the choice of sets the cache directly. Going from 32 groups to 8 frees three quarters of the cache memory, which is space the server can use for more concurrent requests or longer contexts.
The GQA paper reports these values in its Table 1, for per-sample inference time on TPUv4 and average dev-set score across its summarisation, translation and question-answering tasks.
| Model | Inference time (s) | Average score |
|---|---|---|
| MHA-XXL | 1.51 | 47.2 |
| MQA-XXL, 5% uptrained | 0.24 | 46.6 |
| GQA-8-XXL, 5% uptrained | 0.28 | 47.1 |
- Speed-up of GQA-8 over multi-head attention: .
- Speed-up of multi-query attention over multi-head attention: .
- Score gap of GQA-8 to multi-head attention: points.
- Score gap of multi-query attention: points.
Interpretation: GQA-8 gives most of the speed of multi-query attention, about 5.4 times faster than multi-head attention, while keeping almost all of its quality. These numbers come from one T5 setup in the paper and should not be read as a universal speed-up for other models or hardware.
Tradeoffs and failure modes
Grouped-query attention is a memory decision. It helps most when the cache is the bottleneck.
| Situation | Problem | Better choice |
|---|---|---|
| Long contexts or large batches on a fixed GPU | The key-value cache fills memory | GQA with fewer groups, which cuts the cache by |
| Short contexts, small batches | The cache is small, so there is little to save | Multi-head attention keeps the most capacity |
| Fastest possible decoding, quality can drop a little | One shared key and value (multi-query) saves the most memory | Multi-query attention, ideally after uptraining |
| Existing multi-head checkpoint, limited training budget | Training from scratch is expensive | Convert by mean-pooling and uptrain a small fraction of the steps |
| Encoder self-attention | Encoder states are computed in parallel, so memory bandwidth is rarely the limit | Keep full multi-head attention there; the paper applies GQA only to decoder self-attention and cross-attention |
Failure modes to watch for:
- Too few groups. Quality falls as shrinks. The paper found that multi-query attention, with one group, needs uptraining before it is useful, while GQA already performs reasonably right after conversion. It chose 8 groups for its T5-XXL experiments as a middle ground.
- Converting without uptraining. A converted checkpoint is a damaged model until it has been trained a little further. Skipping that step costs quality.
- Expecting a training speed-up. Every query head still computes its own scores, so training compute barely changes. Plan around memory and decoding speed.
- Confusing with the number of query heads. Serving code has to know both numbers. Mismatched shapes produce wrong outputs or crashes, not just slowdowns.
History
Multi-query attention came first. In 2019 Shazeer proposed multi-query attention, which uses one key head and one value head for all query heads, to speed up decoding in transformer models. The GQA paper cites this work for the observation that decoder inference is limited by the memory bandwidth needed for keys and values.
GQA came in 2023. Joshua Ainslie and colleagues proposed grouping the query heads so that each group shares a key and value head. They also gave a recipe for converting existing multi-head checkpoints and uptraining them for a small share of the original compute. Their argument, in the paper's words, is that uptrained GQA reaches quality close to multi-head attention with speed comparable to multi-query attention.
Check yourself
QuestionWhat does the number count in grouped-query attention?Show answer
It counts the groups of query heads, and so the number of key and value heads stored per layer. Each group has one key head and one value head, shared by the query heads in it.
QuestionA model has query heads, width , layers, 16-bit storage, and uses GQA with . How big is the cache for one 2048-token sequence?Show answer
Bytes per token , which is 64 KiB. For 2048 tokens, KiB MiB.
QuestionWhy does grouped-query attention save memory even though every query head still computes its own attention scores?Show answer
The memory saving comes from the keys and values, not from the scores. The cache stores key and value heads per token rather than . The score matrices exist only during the computation for the current step and are not stored across steps, so reducing the cached keys and values is what cuts the cache. The arithmetic for the scores is unchanged, which is why training speed barely moves.
QuestionYou are serving a model with multi-head attention. At batch size 16 and long contexts, the GPU runs out of memory for the cache. Why can you not just change the attention layer's group count at serving time, and what would you do?Show answer
The weights were trained with separate key and value projections. Changing the group count changes the shape of those projections, so the existing checkpoint would give wrong results. You would need to convert the checkpoint by mean-pooling the key and value heads within each group, and then uptrain it for a small fraction of the original steps to recover quality. This is a training task, not a configuration change.
Sources
- Ainslie et al., GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints (2023).
- Shazeer, Fast Transformer Decoding: One Write-Head is All You Need (2019).
- Vaswani et al., Attention Is All You Need (2017).
- Attention (machine learning) on Wikipedia.