Before you move a control in the demo, say what you expect to change.
Foundation reference
Grouped-Query Attention (GQA)
Generating each new token means reading the whole key-value cache from memory, so a smaller cache means less memory traffic and room for longer contexts or larger batches; MQA was introduced to cut exactly this cost (Shazeer, 2019).
Study guide
Where to start
Why can several query heads share one key head and one value head with little loss of quality, when each query head still computes its own attention pattern?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
Use the prerequisites and related concepts at the end of the page to fill the next gap.
Why It Matters for Modern Models
- Generating each new token means reading the whole key-value cache from memory, so a smaller cache means less memory traffic and room for longer contexts or larger batches; MQA was introduced to cut exactly this cost (Shazeer, 2019).
- Ainslie et al. (2023) converted existing multi-head checkpoints to GQA using about 5% of the original pre-training compute and found quality close to MHA at a speed close to MQA.
- Llama 2 70B, the largest released Llama 2 model, uses GQA; the paper also reports a 34B model with GQA that was trained but not released (Touvron et al., 2023).
Questions to Think About
- GQA leaves the attention arithmetic per query head unchanged; why does token-by-token generation still get faster, and why is the gain smaller when processing the prompt?
- How should the number of key-value heads g be chosen, and what is lost in going from MHA to GQA and from GQA to MQA (Ainslie et al., 2023)?
Interactive Demo
Core Math
If you prefer intuition first, try the demo above, then come back for the math.
In multi-head attention (MHA) each of the query heads has its own key head and value head. During generation the keys and values of all earlier tokens are cached, so for one sequence the cache holds
numbers, with layers, key-value heads, head dimension and tokens; the factor 2 counts keys and values. For MHA, . The demo on this page calls these two numbers and .
Multi-query attention (MQA) keeps all query heads but gives them a single shared key-value head, (Shazeer, 2019).
Grouped-query attention (GQA) sits in between: the query heads are divided into equal groups, and the heads in a group share one key head and one value head (Ainslie et al., 2023):
Memory is reduced by a factor of ; equivalently, GQA retains the fraction of the MHA KV cache. recovers MHA and recovers MQA.