From MHA and MQA to Grouped-Query and Latent Attention
Long-context serving is bounded by how many bytes each token appends to the KV cache. Standard Multi-Head Attention (MHA) stores distinct key and value vectors for every attention head. Multi-Query Attention (MQA) and Grouped-Query Attention (GQA) reduce that footprint by sharing K and V heads across query groups, trading away some head-level modeling capacity when pushed too aggressively.
| Attention mechanism | Cached values per token per layer | How queries attend during decode |
|---|---|---|
| Multi-Head Attention (MHA) | 2 × n_heads × d_head | Each query head reads its own cached K_i and V_i |
| Grouped-Query Attention (GQA) | 2 × n_kv_heads × d_head | Groups of n_heads / n_kv_heads queries share one cached K_g, V_g pair |
| Multi-Head Latent Attention (MLA) | d_c + d_rope (c_KV latent + shared RoPE key) | Head-specific up-projections are absorbed into query and output matrices |
Introduced in DeepSeek-V2 and carried forward into DeepSeek-V3, MLA projects hidden states h_t down into a compact latent vector c_t^{KV} of dimension d_c (for example 512, compared with 128 × 128 × 2 = 32,768 uncompressed MHA elements). Only that latent vector—plus a small shared rotary key—sits in the paged KV cache.
Compare per-token KV cache memory: MHA vs GQA vs MLA
Run this standalone calculation using DeepSeek-V3’s 61-layer geometry (128 attention heads, d_c = 512 latent KV width, d_rope = 64 decoupled rotary width) against equivalent 61-layer MHA and 8-head GQA layouts over a 32,768-token sequence.
layers = 61
seq_len = 32768
layouts = {
"MHA (128 KV heads, d=128, BF16)": (2 * 128 * 128, 2),
"GQA-8 (8 KV heads, d=128, BF16)": (2 * 8 * 128, 2),
"MLA (d_c=512 + d_rope=64, BF16)": (512 + 64, 2),
"MLA (d_c=512 + d_rope=64, FP8) ": (512 + 64, 1),
}
for name, (elems_per_layer, bytes_per_elem) in layouts.items():
per_token_kib = (layers * elems_per_layer * bytes_per_elem) / 1024
seq_gib = (layers * elems_per_layer * bytes_per_elem * seq_len) / 2**30
print(f"{name} -> {per_token_kib:6.1f} KiB/tok | 32K context: {seq_gib:6.2f} GiB")MHA (128 KV heads, d=128, BF16) -> 3904.0 KiB/tok | 32K context: 122.00 GiB
GQA-8 (8 KV heads, d=128, BF16) -> 244.0 KiB/tok | 32K context: 7.62 GiB
MLA (d_c=512 + d_rope=64, BF16) -> 68.6 KiB/tok | 32K context: 2.14 GiB
MLA (d_c=512 + d_rope=64, FP8) -> 34.3 KiB/tok | 32K context: 1.07 GiBAt 32K context, MLA uses 3.5× less KV memory than 8-head GQA and 57× less than full MHA while keeping 128 distinct attention heads. Test multi-sequence capacity for MLA and GQA presets directly in our GPU memory calculator, or implement GQA cache indexing in Grouped-Query KV Storage.
What DeepSeek-V3 MLA stores per token and layer
- Compressed KV latent (c_KV)512 dims
- Shared decoupled RoPE key (k_R)64 dims
576 values vs. 2,048 in 8-head GQA (d=128) or 32,768 in 128-head MHA.
Why matrix absorption and decoupled RoPE make MLA fast at decode
If an inference engine had to multiply c_t^{KV} by up-projection matrices W^{UK} and W^{UV} for every historical token on every decode step, expanding the latent cache back into 128 heads would wreck HBM bandwidth and FLOPs. MLA avoids materializing uncompressed keys and values through matrix absorption:
- Absorb `W^{UK}` into the query side. In
q_t^T K_j = q_t^T (W^{UK} c_j^{KV}) = ((W^{UK})^T q_t)^T c_j^{KV}, the engine projects the single current decode query into the512-dim latent space once and computes dot products directly against cachedc_j^{KV}. - Absorb `W^{UV}` into the output projection. Instead of expanding every cached token into per-head values
V_j = W^{UV} c_j^{KV}, attention weights aggregatec_j^{KV}in latent space first, then applyW^{UV}andW^Oto the single aggregated context vector. - Why RoPE must be decoupled. Rotary position embeddings (
R_t) sit betweenq_tandW^{UK}, breaking associative absorption (R_t^T R_jdepends on relative positiont - j). DeepSeek solves this with decoupled RoPE: queries and keys carry a separate narrow64-dim rotary sub-vector (k_t^R, shared across heads) whose scores add to the512-dim absorbed latent score.
Serving MLA checkpoints in vLLM, SGLang, and TensorRT-LLM
Because absorbed decode treats all 128 query heads as attending to a single shared c_t^{KV} latent per token, standard tensor parallelism across KV heads cannot shard c_t^{KV} without replicating it across TP ranks—one reason large MLA + MoE deployments combine data-parallel attention with expert parallelism (see Mixture of Experts inference).
Check supported engine flags, FlashMLA backends, and quantization recipes for DeepSeek-V3 and DeepSeek-R1 in our compatibility directory, and review vLLM’s MLA documentation and SGLang’s DeepSeek optimizations. Build attention and KV mechanics from scratch in our free KV cache lesson.
Multi-Head Latent Attention FAQ
Can you retrofit MLA onto an existing GQA checkpoint without retraining?
Not lossless out of the box—MLA learns its joint W^{DKV}, W^{UK}, W^{UV}, and decoupled RoPE projections during pretraining, though research methods explore SVD initialization followed by continued training.
Why does tensor parallelism replicate the MLA KV cache across ranks?
Because c_t^{KV} and k_t^R are shared across all query heads on a layer (like MQA with head width 576); sharding query heads across TP GPUs still requires each GPU to read the shared latent vector unless data-parallel attention is used.
Does MLA still benefit from PagedAttention and prefix caching?
Yes. Engines page the 576-element latent vectors into fixed token blocks and hash shared prompt prefixes exactly as they do for GQA caches, fitting several times more cached prefix tokens per GPU.
Sources and further reading
Primary documentation and research behind this guide.
- DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model (2024)
- DeepSeek-V3 Technical Report (2024)
- DeepSeek FlashMLA: Efficient MLA decoding kernels for Hopper GPUs
- vLLM v0.30.0: Multi-Head Latent Attention (MLA) design
- LMSYS Org: SGLang v0.3 DeepSeek MLA and weight absorption optimizations
