Skip to content
mentoor

Topic 3 of 6 · 6 min read

Grouped-query and latent attention

Goal: Calculate the KV-cache size of a model configuration with and without grouped-query attention.

Grouped-query attention (GQA) lets several query heads share one key head and one value head, which shrinks the KV cache by the sharing ratio with little loss in quality. Multi-head latent attention (MLA) goes further by caching a small compressed vector and rebuilding keys and values from it.

Check yourself · 1 of 3

A model has 32 query heads and 8 KV heads. How many query heads share each KV head?

Choose one answer

Why it matters

During generation, the model stores the keys and values of every earlier token so it does not recompute them: the KV cache. It grows with context length and with every concurrent request. For long contexts it often takes more memory than the weights, and reading it limits decoding speed. Cache size decides how many users one GPU can serve.

How to calculate it

bytes per token = 2 * layers * kv_heads * head_dim * bytes_per_value

The 2 is one key plus one value. Take a Llama-3-8B-style config: 32 layers, head_dim 128, 8 KV heads, 16-bit values (2 bytes).

per_token = 2 * 32 * 8 * 128 * 2        # 131,072 bytes = 128 KiB
at_8k = per_token * 8192               # 1 GiB
mha   = 2 * 32 * 32 * 128 * 2          # 524,288 bytes with 32 KV heads

With 32 query heads and 8 KV heads, each KV head serves 4 query heads: a 4x smaller cache, 128 KiB instead of 512 KiB per token. Multi-query attention (MQA) is the extreme: one KV head for all query heads.

How the variants differ

  • MHA: KV heads = query heads. Best quality, largest cache (GPT-2 style).
  • GQA: a few KV heads. The GQA paper shows quality near MHA with speed near MQA.
  • MQA: one KV head. Smallest cache, more quality risk.
  • MLA: caches a low-dimensional latent per token and projects it up to keys and values when needed. The DeepSeek-V2 paper reports a 93.3 % smaller KV cache than its previous dense model.

Before and after

Weak: "The 8B model needs 16 GB, so it fits on a 24 GB card for 32 concurrent users." The weights fit, but 32 users at 8k context need 32 GiB of cache. Better: add weights (16 GB) plus cache per user times users, then compare to free memory.

What goes wrong

  • Using the query head count instead of the KV head count in the formula and overestimating by 4x.
  • Forgetting that cache scales with batch size, not only with one request.
  • Forgetting the 2 for key and value, or the byte width (FP8 caches halve it).

Examples

Same model, two head layouts

Config: 32 layers, head_dim 128, 16-bit values, 8,192 tokens. With 32 KV heads the cache is 4 GiB per sequence. With 8 KV heads it is 1 GiB. On a GPU with 40 GiB free after weights, that is 10 sequences versus 40.

Reading the field names

{"num_attention_heads": 32, "num_key_value_heads": 8, "head_dim": 128}

Group size is 32 / 8 = 4 query heads per KV head. If num_key_value_heads is missing, the model is plain multi-head attention.

Key points

  • The KV cache holds keys and values for every earlier token; it grows with context length and batch size.
  • Cache bytes per token are 2 * layers * kv_heads * head_dim * bytes per value.
  • GQA shares each key and value head across a group of query heads, cutting the cache by the group size.
  • MLA caches a compressed latent instead of full keys and values, and rebuilds them when needed.
  • Capacity planning needs weights plus cache per user times concurrent users, not weights alone.

Common mistakes

  • Using num_attention_heads in the cache formula for a GQA model.
  • Planning memory from model weights alone.
  • Forgetting the factor of 2 for keys plus values.
  • Assuming GQA changes the number of query heads rather than the number of KV heads.

Check yourself · 2 of 3

A model has 24 layers, 4 KV heads, head_dim 64 and 16-bit values. How many bytes of KV cache per token?

Choose one answer

Check yourself · 3 of 3

Why can a long-context server run out of memory even though the weights fit on the GPU?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic