Memory & attention4 min read

Multi-Head Latent Attention (MLA) explained: KV compression

Grouped-Query Attention shrank the KV cache by forcing multiple query heads to share identical keys and values. Multi-Head Latent Attention (MLA), introduced in DeepSeek-V2 and scaled in DeepSeek-V3 and R1, goes further: it caches a single compressed latent vector per token and folds decompression into surrounding weights so decode stays compact without sacrificing per-head expressiveness.

Conceptual illustration of wide attention manifolds compressing into a compact latent core with a separate rotary track.
Multi-Head Latent Attention stores a compact latent vector plus a narrow rotary key instead of full per-head KV tensors.

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.

What each attention family stores in GPU memory per token and layer.
Attention mechanismCached values per token per layerHow queries attend during decode
Multi-Head Attention (MHA)2 × n_heads × d_headEach query head reads its own cached K_i and V_i
Grouped-Query Attention (GQA)2 × n_kv_heads × d_headGroups 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.

Run with Python 3; no GPU or packages required
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")
Calculated output
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 GiB

At 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.

Visual explanation

What DeepSeek-V3 MLA stores per token and layer

576 cached values per token · layer
  1. Compressed KV latent (c_KV)512 dims
  2. 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.

Exact DeepSeek-V2/V3 per-layer latent cache geometry (d_c = 512, d_rope = 64). During decode, absorbed projections read this 576-element state directly without expanding per-head K and V in HBM.

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 the 512-dim latent space once and computes dot products directly against cached c_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 aggregate c_j^{KV} in latent space first, then apply W^{UV} and W^O to the single aggregated context vector.
  • Why RoPE must be decoupled. Rotary position embeddings (R_t) sit between q_t and W^{UK}, breaking associative absorption (R_t^T R_j depends on relative position t - j). DeepSeek solves this with decoupled RoPE: queries and keys carry a separate narrow 64-dim rotary sub-vector (k_t^R, shared across heads) whose scores add to the 512-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.

  1. DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model (2024)
  2. DeepSeek-V3 Technical Report (2024)
  3. DeepSeek FlashMLA: Efficient MLA decoding kernels for Hopper GPUs
  4. vLLM v0.30.0: Multi-Head Latent Attention (MLA) design
  5. LMSYS Org: SGLang v0.3 DeepSeek MLA and weight absorption optimizations

Keep learning

Related guide · 6 min readMixture of Experts (MoE) inference: memory, routing & GPUsRelated guide · 5 min readFlashAttention vs PagedAttention: what each optimizesRelated guide · 5 min readGPU memory for LLM inference: how to size and choose a GPURelated guide · 7 min readKV cache explained: formula, diagram, and memory sizingAcademy course · 9 unitsInference Engineering FoundationsAcademy course · 13 unitsvLLMAcademy course · 13 unitsSGLangAcademy course · 20 unitsHugging Face Transformers
Back to all articles