Quant Memo
Advanced

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 hh heads, each head ii has its own projection matrices WiK,WiVW_i^K, W_i^V, producing distinct keys KiK_i and values ViV_i per head — the cache stores hh separate K,VK, V pairs.

Multi-query attention (MQA) keeps hh separate query projections WiQW_i^Q (each head still asks its own questions) but uses a single shared WK,WVW^K, W^V for all heads:

headi=softmax ⁣(QiKdk)V\text{head}_i = \text{softmax}\!\left(\frac{Q_i K^\top}{\sqrt{d_k}}\right) V

In words: every head computes its own query QiQ_i and its own attention weights, but KK and VV — the thing that has to be cached across every generation step — are the same for all hh heads. That shrinks the KV cache by a factor of hh.

Grouped-query attention (GQA) splits the hh heads into gg groups (with 1<g<h1 < g < h), and each group shares one K,VK, V pair. MQA is the special case g=1g=1; ordinary multi-head attention is the special case g=hg=h.

Worked example 1: cache size for a real-sized model

Take a model with h=32h = 32 heads, head dimension dk=128d_k = 128, sequence length n=4,096n = 4{,}096, in 16-bit precision, across L=32L = 32 layers, caching both KK and VV.

Standard multi-head cache: 2×L×h×n×dk×2 bytes=2×32×32×4096×128×2=2,147,483,6482 \times L \times h \times n \times d_k \times 2\text{ bytes} = 2 \times 32 \times 32 \times 4096 \times 128 \times 2 = 2{,}147{,}483{,}648 bytes 2.0\approx 2.0 GB.

Multi-query cache (h=1h=1 shared instead of 32): divide by 32 → 67\approx 67 MB.

Grouped-query with g=8g = 8 groups: divide by 32/8=432/8 = 4512\approx 512 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 8 GB/(2.0 GB/4096 tokens)16,3848\text{ GB} / (2.0\text{ GB} / 4096\text{ tokens}) \approx 16{,}384 tokens before running out. Under MQA, the same 8 GB stretches to about 8/(0.067/4096)490,0008 / (0.067/4096) \approx 490{,}000 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.

MHA (h=32) 2.0 GB GQA (g=8) 0.5 GB MQA (g=1) 67 MB
KV cache size falls sharply from full multi-head attention to grouped-query to multi-query, for identical sequence length and model size.

Function explorer
-224.4
x = 1.00f(x) = 1.000

Drag the exponent above — cache size as a function of the number of shared groups gg shrinks in roughly this shape, falling fastest as gg first drops from hh and flattening once gg 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 gg 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

  1. With h=16h=16 heads and g=4g=4 groups, by what factor does the KV cache shrink relative to full multi-head attention?
  2. 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)
ShareTwitterLinkedIn