Multi-Query and Grouped-Query Attention
Serving a transformer fast means keeping a growing cache of keys and values in memory for every generated token — multi-query and grouped-query attention shrink that cache by sharing keys and values across heads instead of giving every head its own.
Prerequisites: Multi-Head Attention, The KV Cache and Incremental Decoding
Generating text one token at a time means a transformer must remember, for every previous token, the key and value vectors every head computed for it — the The KV Cache and Incremental Decoding — since recomputing them at every step would be wasteful. The problem: standard multi-head attention gives every head its own keys and values, so the cache grows with the number of heads as well as sequence length. For a large model generating long outputs, that cache can dwarf the model's own weights and become the real bottleneck on how fast text streams out. Multi-query and grouped-query attention are two related fixes: stop giving every head its own keys and values, and share them instead.
The analogy: one shared reference desk, many readers
Picture a library reading room with 32 researchers (the attention heads), each keeping a personal, photocopied set of every reference book they might need (the keys and values) under standard multi-head attention. Photocopying 32 full sets costs shelf space. Multi-query attention keeps just one shared master set at a central desk, and lets all 32 researchers consult it — each still asks their own personal questions (queries stay separate), but all read from the same shelf. Grouped-query attention is the middle ground: split the 32 researchers into, say, 8 groups of 4, each group with its own shared shelf — less duplication than 32 personal copies, more flexibility than one shared shelf.
The mechanics
In standard multi-head attention with heads, each head has its own projection matrices , producing distinct keys and values per head — the cache stores separate pairs.
Multi-query attention (MQA) keeps separate query projections (each head still asks its own questions) but uses a single shared for all heads:
In words: every head computes its own query and its own attention weights, but and — the thing that has to be cached across every generation step — are the same for all heads. That shrinks the KV cache by a factor of .
Grouped-query attention (GQA) splits the heads into groups (with ), and each group shares one pair. MQA is the special case ; ordinary multi-head attention is the special case .
Worked example 1: cache size for a real-sized model
Take a model with heads, head dimension , sequence length , in 16-bit precision, across layers, caching both and .
Standard multi-head cache: bytes GB.
Multi-query cache ( shared instead of 32): divide by 32 → MB.
Grouped-query with groups: divide by → MB.
The KV cache — the part of memory that scales with how many tokens you have generated so far — shrinks by roughly 32× under MQA and 4× under GQA with 8 groups, for the exact same sequence length.
Worked example 2: how many tokens fit in a fixed memory budget
Suppose a GPU has 8 GB free for KV cache after loading weights, with the cache costs from above. MHA lets you cache roughly tokens before running out. Under MQA, the same 8 GB stretches to about tokens — far beyond what's needed, meaning the real win is serving far more simultaneous requests with the same memory, not just longer single contexts.
Drag the exponent above — cache size as a function of the number of shared groups shrinks in roughly this shape, falling fastest as first drops from and flattening once is already small.
MQA and GQA cut the KV cache — the actual inference-time memory bottleneck — by sharing key and value projections across heads while keeping each head's queries separate, trading a small amount of representational flexibility for a large, direct reduction in memory and memory-bandwidth cost per generated token.
What this means in practice
Almost every large language model deployed for fast serving today (Llama 2 70B, Mistral, and others) uses GQA rather than full multi-head attention, because inference speed at long context is bottlenecked by moving the KV cache through memory, not raw compute — GQA directly shrinks the data that has to move. The choice of is a knob teams tune explicitly: more groups preserves quality closer to full MHA, fewer groups saves more memory and bandwidth.
The saving is in cache size and memory bandwidth, not in the number of floating-point operations for a single query — MQA and GQA do not meaningfully change compute cost for a single forward pass over a short sequence. The benefit compounds specifically during autoregressive generation over many steps, and specifically when memory bandwidth, not raw FLOPs, is the bottleneck — which it usually is at long context and high batch size.
Practice
- With heads and groups, by what factor does the KV cache shrink relative to full multi-head attention?
- Why does MQA save cache memory but not reduce the number of query projections computed per head?
Related concepts
Practice in interviews
Further reading
- Shazeer, Fast Transformer Decoding: One Write-Head is All You Need (2019)
- Ainslie et al., GQA: Training Generalized Multi-Query Transformer Models (2023)