GQA, MQA and MLA: how attention decides the KV cache

Attention is where the KV cache comes from, and the way a model arranges its attention heads can change the cache by an order of magnitude at the same parameter count. Four arrangements cover today's open models.

Multi-head attention (MHA)

Every query head has key and value heads of its own, and the cache stores all of them: 2 × layers × heads × head dimension × bytes per token. It is the original Transformer design and the most expensive to cache. Phi-3 mini 4k still uses it — 32 heads over 32 layers, 384.0 KiB per token.

Multi-query attention (MQA)

All query heads share a single key head and a single value head (Shazeer, 2019, “Fast Transformer Decoding: One Write-Head is All You Need”). The cache shrinks by the full number of heads — the largest saving there is — but sharing one set of keys and values across every head can cost quality.

Grouped-query attention (GQA)

Query heads are split into groups, and each group shares one key-value head: more than one, fewer than the number of query heads. Ainslie et al. (2023) proposed it as the middle ground — quality close to multi-head attention, speed close to multi-query — and 28 of the 31 models LLMScale bundles use it. Llama 3.3 70B Instruct groups 64 query heads onto 8 key-value heads, so it caches 320.0 KiB per token where multi-head attention would need 2560.0 KiB: 8× less.

Multi-head latent attention (MLA)

DeepSeek-V2 took another route. Instead of keys and values per head, each layer caches one compressed latent vector per token, plus a small decoupled key that carries the rotary position encoding. The cache per token becomes layers × (kv_lora_rank + qk_rope_head_dim) × bytes, and the head count drops out. For its own dimensions, the DeepSeek-V2 paper (§2.1.3) puts that at the size of GQA with only 2.25 groups.

DeepSeek V3 caches 512 + 64 = 576 values per layer across 61 layers: 68.6 KiB per token, 24.9× less than the per-head formula gives. At 8k context that is 549 MiB instead of 13.34 GiB. A calculator that applies the per-head formula to DeepSeek models overstates the cache by that factor; LLMScale switches formulas whenever a config carries both fields.

Side by side

ModelQuery headsKV headsAttentionPer tokenvs MHA
Phi-3 mini 4k3232MHA384.0 KiB1×
Llama 3.2 1B Instruct328GQA32.0 KiB4×
Qwen2.5 7B Instruct284GQA56.0 KiB7×
Llama 3.1 8B Instruct328GQA128.0 KiB4×
Mistral Nemo 12B Instruct328GQA160.0 KiB4×
Mixtral 8x7B Instruct328GQA128.0 KiB4×
Qwen3 32B648GQA256.0 KiB8×
Llama 3.3 70B Instruct648GQA320.0 KiB8×
Qwen2.5 72B Instruct648GQA320.0 KiB8×
DeepSeek V3128—MLA68.6 KiB24.9×

FP16. “vs MHA”: how many times more a multi-head version of the same model would cache.

What it means when you choose a model