On this page
Two models with the same number of parameters can need wildly different amounts of memory to serve, and the main reason is how their attention stores keys and values. Here are the four designs you'll meet, what each costs per token, and how to work it out for any model from its config file.
#Why attention design matters for serving
Every token in the context keeps its keys and values in the KV cache, in every layer (KV cache explained). During decode, each step reads the whole cache. So the per-token cache size decides:
- How many users fit in GPU memory at once.
- How fast decode is at long context, because reading the cache becomes most of each step.
- What long context costs. At 128K tokens, the cache can be much larger than the model.
#A quick refresher: heads
Attention runs as several heads in parallel. Each head has its own query, key and value projections, so it can attend to different things: one head might track the subject of a sentence, another nearby punctuation. The outputs of all heads are concatenated and mixed.
The query heads have to be computed fresh for each new token, but the keys and values of past tokens are cached. So the number of key and value heads, not query heads, decides the cache size. That's the lever every design below pulls.
#The four designs
Take a 70B-class model with 80 layers, 64 query heads and head dimension 128, in BF16, and change only the attention.
#MHA: multi-head attention
Every query head has its own key and value head. Per token: bytes, which is 2.5 MiB. At 8K tokens, that's 20 GiB per sequence. The original transformer and early LLMs used MHA.
#MQA: multi-query attention
All query heads share one key head and one value head. Per token: bytes, which is 40 KiB: 64 times smaller. Decode gets much faster at long context, but sharing one KV head across all queries can cost quality, and with tensor parallelism the single KV head must be replicated on every GPU.
#GQA: grouped-query attention
The middle ground. Query heads are split into groups, and each group shares one KV head. With 8 KV heads (8 query heads per group), per token: bytes, which is 320 KiB: 8 times smaller than MHA. Quality stays close to MHA. This is what Llama 3, Qwen, Mistral and most current dense models use.
The GQA paper also showed that an existing MHA model can be converted: average the key and value heads within each group, then continue training briefly (a few percent of the original pre-training compute) to recover quality.
#MLA: multi-head latent attention
DeepSeek's design stores a single compressed latent vector per token per layer (plus a small part for rotary position information), and reconstructs keys and values for every head from it. The per-token cache is close to MQA's size, while quality stays close to MHA, because each head still gets its own keys and values.
The trick that makes MLA efficient at decode is absorbing the up-projections into the query and output projections, so the model can attend directly over the compressed latents without ever expanding them.
Cache (8 tokens × 576)
Work per layer at 32K
–
FLOPs per cache byte
–
The cache holds one latent per token
Instead of keys and values for 128 heads, MLA stores a 512-number latent (plus a 64-number positional key) per token. Here: 8 cached tokens.
Blue rows: cached latents. Amber: expanded per-head keys (naive path). Violet: the absorbed query.
Here's the idea in one line. A head's key for a past token is , where is the cached latent and the up-projection. The attention score is . So instead of expanding every cached latent into a full key, the model transforms the one new query once, then scores it directly against the small latents. The value side works the same way through the output projection.
#Side by side
| Design | KV heads | Per token (80 layers, BF16) | 8K tokens | 128K tokens |
|---|---|---|---|---|
| MHA | 64 | 2.5 MiB | 20 GiB | 320 GiB |
| GQA (8 groups) | 8 | 320 KiB | 2.5 GiB | 40 GiB |
| MQA | 1 | 40 KiB | 320 MiB | 5 GiB |
| MLA | latent, roughly MQA-sized | depends on latent size | close to MQA | close to MQA |
MLA's exact size depends on the latent dimension a model chooses. Check the model's configuration for it.
#Worked example: users per node
Put the same 70B-class model in FP8 on a node of eight H100s. After the weights and working memory, about 505 GB is left for the KV cache. With an FP8 cache (half the BF16 sizes above) and every user at a 32,768-token context:
| Design | KV per user at 32K tokens | Users that fit |
|---|---|---|
| MHA | about 43 GB | about 11 |
| GQA (8 KV heads) | about 5.4 GB | about 94 |
| MQA | about 0.7 GB | about 750 |
Same model size, same hardware, a 70-fold difference in how many long conversations you can serve at once. In practice other limits (compute, latency targets) cap the MQA case well below 750, but memory stops being the constraint.
#Real models
| Model | Layers | Query heads | KV heads × head dim | KV per token (16-bit) |
|---|---|---|---|---|
| Llama 3.1 8B | 32 | 32 | 8 × 128 | 128 KiB |
| Mistral 7B | 32 | 32 | 8 × 128 | 128 KiB |
| Llama 3.1 70B | 80 | 64 | 8 × 128 | 320 KiB |
| Qwen2.5 72B | 80 | 64 | 8 × 128 | 320 KiB |
| Phi-3 mini (MHA) | 32 | 32 | 32 × 96 | 384 KiB |
| DeepSeek-V3 (MLA) | 61 | 128 | one latent of 512 + 64 | about 69 KiB |
DeepSeek-V3 is a 671B-parameter model with 128 query heads, yet its cache per token is about half of an 8B GQA model's. That's the effect of MLA.
#Sliding windows and hybrid models
Two more ways to shrink the cache, often combined with GQA:
- Sliding-window attention. Some layers attend only to the last tokens, so their cache never grows beyond . Gemma 3, for example, alternates five local layers with a 1,024-token window and one global layer, so most layers' caches stay small at any context length.
- Hybrid models. Some layers are replaced with linear-attention or state-space layers that keep a fixed-size state instead of a growing cache. Only the remaining full-attention layers keep a per-token cache.
For both, compute the cache per layer type and add them up; the simple formula overstates the cache if you count every layer as global.
#Reading a model's config
In a Hugging Face config.json:
num_attention_heads: query heads.num_key_value_heads: KV heads. Equal to the query heads means MHA, 1 means MQA, anything in between means GQA.head_dim(orhidden_sizedivided bynum_attention_heads).num_hidden_layers: layers.- MLA models have fields such as
kv_lora_rankfor the latent size andqk_rope_head_dimfor the position part. - Sliding-window models have fields such as
sliding_windowand a pattern of which layers use it.
Then apply the formula: . For MLA, it's , since keys and values share one latent. The VRAM calculator does this for common models.
#What it changes for decode speed
At long context, each decode step reads more KV cache than weights. For a GQA 70B model at 32K tokens, one sequence's cache is 10 GiB, against 70 GB of FP8 weights shared by the whole batch. With 32 such sequences, the cache is 320 GiB per step, far more than the weights. Attention design is then the main factor in decode speed. That's why long-context serving leans so hard on GQA, MLA, FP8 KV caches and, increasingly, hybrid models that replace some attention layers with linear-attention or state-space layers whose state doesn't grow with context.
#Tensor parallelism and KV heads
Tensor parallelism splits attention heads across GPUs (parallelism explained). With GQA, the KV heads are split too, as long as the tensor-parallel size divides the number of KV heads: 8 KV heads split cleanly over 2, 4 or 8 GPUs. Beyond that (say TP=16 with 8 KV heads), KV heads are replicated, and per-GPU cache savings stop. MQA's single KV head is replicated on every GPU. MLA's single latent is also shared by all heads, so MLA models are often served with data parallelism for attention rather than tensor parallelism. Check this when choosing a TP size.
#Common mistakes
- Estimating KV cache with query heads for a GQA model, overstating it 4 to 8 times.
- Assuming MQA is free. Quality and TP replication both cost something.
- Ignoring the latent dimension when sizing MLA models.
- Counting sliding-window layers as global, overstating the cache of models such as Gemma.
- Forgetting layers. The per-token figure multiplies by every layer.
#FAQ
Does GQA make a model worse? Slightly, in principle; in practice the difference from MHA is small, and the memory saving lets you serve far more users, which is why almost every current model uses it.
Does FlashAttention shrink the KV cache? No. It changes how attention is computed, not what's stored (FlashAttention explained).
Can I quantise the cache as well? Yes. An FP8 or INT8 cache halves any of the sizes above, usually with little quality loss.
#Go deeper
Attention optimisations covers GQA, sliding windows and paged KV, and attention kernels for serving explains MLA's absorbed form and decode kernels. Linear attention and hybrid models covers the designs that keep the cache from growing at all.
attentionkv-cachegqamlaarchitectureinference-engineering
Found this useful?
Send it to someone who is learning the same thing.
Preparing for an interview?
Next: learn it step by step, free
Prefill, decode and the roofline
Why decode is memory-bound, with worked numbers for real GPUs. The idea the rest builds on. A free lesson with step-through diagrams and a quiz.