← All writing

Technical note

MHA, MQA, and GQA: the cache is part of the architecture

A compact comparison of attention variants through the lens of inference-time memory.

Attention architecture is often introduced as a modeling choice. At inference time, it is also a memory-traffic choice.

The shared question

For a sequence length TT, each generated token attends to cached keys and values from preceding tokens. Multi-head attention gives every query head its own key and value head. Multi-query attention shares one key/value head across all query heads. Grouped-query attention sits between them, sharing key/value heads within groups.

VariantQuery headsKV headsInference intuition
MHAhhhhHighest KV-cache footprint
MQAhh11Maximum sharing
GQAhhggControlled sharing

If each head has dimension dhd_h, KV-cache storage is proportional to 2Tnkvdh2Tn_{kv}d_h per layer. The number of KV heads, not the number of query heads, is the term that changes across these variants.

The systems consequence

The useful engineering question is not which acronym is best in isolation. It is whether the model is compute-bound or bandwidth-bound at the intended batch size, context length, hardware, and serving setup. The KV cache is a first-class part of that answer.

This is a seed note: a place to extend with benchmarks, tensor shapes, and a small implementation walkthrough.

Notes by Zaid Yusuf