Attention Mechanisms · May 2024
Multi-Head Latent Attention
advanced
kv-cacheefficiency
Cut KV cache memory below MQA/GQA while preserving or improving quality.
§ 1 · Premise
KV cache is the autoregressive tax
In a decoder Transformer of layers with heads of dimension , each generated token appends one key and one value vector per head per layer to a cache that the next step has to read. For DeepSeek-67B’s geometry — , , , fp16 — the cache cost is . A 32K context holds about 100 GB of cache before any model parameters are loaded, and the cache, not the FFN, dominates inference-time memory bandwidth at long context. DeepSeek-V2 quantifies the same problem on its 236B-parameter geometry: 1.6 MB of cache per token at , , (DeepSeek-V2, Table 1).
The three predecessors all chase this number, with diminishing returns.
- MHA (Vaswani et al., 2017) gives every query head its own K and V; the cache scales with . Quality is the reference, memory is the worst.
- MQA (Shazeer, 2019) keeps a single shared K, V across all heads. Cache shrinks by , but quality drops measurably on multi-task evals: GQA’s authors report MQA losing 0.7 points of average score vs. MHA at the 11B scale (Ainslie et al., 2023, Table 2).
- GQA (Ainslie et al., 2023) interpolates: groups of query heads share a K, V pair. At on Llama-2-70B the cache is smaller than MHA while quality is within noise.
The shared move in MQA and GQA is to throw away K, V diversity. MLA keeps the diversity but stores a compressed representation: at each token, cache one low-rank latent and reconstruct every head’s K and V on the fly. The reconstruction matrices are folded into adjacent projections at inference, so the per-token memory cost is the latent — not the per-head vectors.
Two complications keep this from being a one-line description. First, the natural place to apply RoPE — to each head’s K — is incompatible with the latent representation, because rotating after up-projection forces the up-projection to be re-applied per cached token. Second, the same trick that lets MLA evade that cost (absorbing the K up-projection into the query at inference) only works when the query side is also restructured to keep position out of the content channel. Both points are derived below.
§ 2 · Derivation
From MHA to a cached latent
MHA baseline. Let be the residual-stream vector at position . A standard MHA layer with heads of dimension computes, for head :
with . The per-token cache footprint is scalars per layer — the two vectors and stacked across heads. For , , (DeepSeek-V2) this is scalars or about 3.84 MB at fp16 per token.
Low-rank latent. MLA replaces the per-head with a shared down-projection to a single latent of dimension , and per-head up-projections that reconstruct K and V at attention time (DeepSeek-V2, eq. 9–11):
with and . The superscript marks the “content” part of the key, distinguished below from a position-aware part. Only enters the cache: scalars per token per layer instead of . DeepSeek-V2 picks , so the latent is the size of the stacked per-head K and V.
Why this is not just GQA with . MQA also caches one K and one V per layer, of dimension each — scalars at DeepSeek-V2’s . The MLA latent is 512, twice as large in raw scalars, but expresses a -dimensional subspace from which every head’s K and V are independently reconstructed by its own . MQA collapses head diversity at the cache level; MLA collapses only the rank, leaving each head free to read the latent through its own linear map. The empirical consequence is in § 4.
Query-side compression. DeepSeek-V2 also down-projects queries to with , then up-projects per head: (DeepSeek-V2, eq. 6–8). The query compression saves training-time activation memory; it is not cached, since queries are recomputed at every step. The choice is reported without a sensitivity study.
The RoPE obstruction. Rotary position embeddings (Su et al., 2021) multiply and by a position-dependent rotation before the inner product. With latent compression, the natural attention score for head between positions (query) and (key) is
The trick that makes MLA cheap at inference is to absorb into the query-side matrix once, offline: define and read the cached directly. But if RoPE rotates the per-head K after the up-projection, the score becomes — the depends on , so the up-projection cannot be precomposed: every cached token would need its reapplied on the fly, defeating the purpose. Rotating before the down-projection mixes positions into the latent in a way that cannot be unrotated per head.
Decoupled RoPE head. MLA’s resolution is to split each key into a content part (no RoPE, reconstructed from the latent) and a small position-aware part (RoPE applied, cached separately) (DeepSeek-V2, eq. 12–13):
where produces a single -dimensional rotary key shared across all heads, with in DeepSeek-V2. The query is split symmetrically: . The content channel carries the absorbable up-projection; the rotary channel carries position with a small extra cost.
Per-token cache after decoupling. The cache stores and the single shared , i.e., scalars per layer per token. Across 60 layers that is scalars or ~67.5 KB per token at fp16, versus 3.84 MB for MHA on the same geometry — a reduction at the layer-cache level (DeepSeek-V2, Table 1 reports the equivalent figure as 4.5% of MHA cache after grouping by representation precision).
Parameter count. Setting aside biases, an MLA layer’s KV-side weights total
For DeepSeek-V2 (, , , , ) this is parameters per layer for the K/V path. Standard MHA at the same geometry uses , so MLA’s KV-side weight count is ~12% of MHA’s. The query-side compression adds another parameters per layer.
Computational complexity. Per layer, MLA’s attention compute for sequence length is for the score and value-application — identical to MHA in the asymptotic sense — plus for the latent down-projection and for the per-head up-projections. The absorption trick hides the up-projection behind the query: at inference, the cached is read directly as a -vector and the merged produces head- queries that score against it without ever materializing . The flop count is unchanged; the memory traffic is what shrinks.
§ 3 · Reference implementation
Sketch
# Shapes: B batch, T tokens, d model dim, H heads, d_h head dim,
# d_c KV latent dim, d_c_q Q latent dim, d_R RoPE head dim.
# --- projections (per layer) ---
c_kv = x @ W_DKV # [B, T, d_c] <- cached
k_rope = rope(x @ W_KR, pos) # [B, T, d_R] <- cached
c_q = x @ W_DQ # [B, T, d_c_q] (not cached)
# Up-projections used at attention time. At inference these get folded:
# tilde_W_Q[i] = W_UK[i] @ W_UQ[i], so queries can score against c_kv directly.
k_content = c_kv @ W_UK # [B, T, H, d_h] (logical only)
v = c_kv @ W_UV # [B, T, H, d_h]
q_content = c_q @ W_UQ # [B, T, H, d_h]
q_rope = rope(c_q @ W_QR, pos) # [B, T, H, d_R]
# Stack the content + rope channels into a (d_h + d_R)-wide key/query.
k = concat([k_content, broadcast(k_rope, H)], dim=-1) # [B, T, H, d_h + d_R]
q = concat([q_content, q_rope], dim=-1) # [B, T, H, d_h + d_R]
# Standard scaled dot-product over the concatenated dims.
attn = softmax(q @ k.transpose(-1, -2) / sqrt(d_h + d_R)) # [B, H, T, T]
out = (attn @ v) @ W_O # [B, T, d]
§ 4 · Empirical evidence
Ablations and scaling
Quality vs. MHA, MQA, GQA at fixed scale. DeepSeek-V2’s Table 9 holds the model at 7B activated parameters and varies only the attention variant. MLA reports 50.7 average across the listed benchmarks vs. 50.0 for MHA, with MLA winning on BBH (+1.5), MMLU (+0.4), and C-Eval (+1.5) while losing 0.3 on the GSM8K subset; MQA at 47.5 average and GQA at 48.6 both trail MHA on this setup (DeepSeek-V2, Table 9). The cache reduction at the same table is from 110.6 KB/token (MHA) to 33.75 KB/token (MLA), or 4.5% of the MHA budget after DeepSeek’s representation-precision accounting — a 3.3× reduction at the comparable-quality point.
Throughput. DeepSeek-V2 reports a 5.76× maximum generation throughput improvement over DeepSeek-67B (which used GQA at ) on the same H800 cluster (DeepSeek-V2, §1 and Figure 1). The throughput gain conflates MLA with the DeepSeekMoE FFN; the paper does not isolate the MLA-only fraction.
Carry-over to V3 and V3.1. DeepSeek-V3 keeps , , , , across 61 layers with (DeepSeek-V3 Technical Report, §2.1.1). No re-ablation against MHA at V3 scale is published; the V3 report cites the V2 ablations as the basis for keeping MLA. DeepSeek-V3.1’s HF model card reports the same attention configuration carried over.
Scaling to V3.2 + sparse attention. DeepSeek-V3.2 (arXiv 2509.04559) keeps MLA intact and adds a DeepSeek Sparse Attention (DSA) module that uses a Lightning Indexer to select a top-K subset of the cached latents (k=2048) for each query. The V3.2 report’s Table 2 shows DSA on top of MLA matching V3.1’s quality on most benchmarks while cutting prefill cost materially at long context — the MLA cache is what makes the sparse indexer cheap to score, since each candidate is a single vector rather than per-head K vectors. This is the clearest scaling evidence: the same MLA shape extends from 236B to 671B parameters and composes with a sparse selector layered above it.
Reproductions and independent reports. Kimi-K2 (Moonshot AI repo) adopts the MLA pattern with the decoupled RoPE head; the public model card describes the same shape. Kimi-Linear-48B (arXiv 2510.26692, Table 1) uses MLA on 7 of 27 layers in a hybrid stack with KDA linear attention on the rest, reporting that those 7 MLA layers contribute the bulk of long-context recall accuracy on RULER. No independent academic reproduction at the DeepSeek-V2 scale is public as of 2026-05-12; the available evidence is the introducing labs and their successors.
Training-time vs. inference-time cost. The empirical reports emphasize per-token cache and generation throughput, both of which are inference-time quantities. The training-time picture is different: MLA’s per-step compute matches MHA asymptotically (still ) and adds a low-rank projection that costs a fraction of a percent of total FLOPs at . The DeepSeek-V2 report does not provide a training-time ablation isolating MLA’s wall-clock cost from the DeepSeekMoE FFN that runs alongside it; the V3 report similarly treats the two as a single joint design choice. Practitioners reproducing MLA from the equations should expect training-step time comparable to MHA on the same geometry; the wins are exclusively on the autoregressive side.
What is not in the public record. The sensitivity of quality to (would work? ?) is not published. The sensitivity to — and whether the decoupled head can be removed entirely with a NoPE-style positional scheme — is also not studied in the DeepSeek reports. At kernel level, the inference fusion of into that makes MLA cache-efficient is described in DeepSeek-V2 §2.1.2 as a recipe but without throughput numbers isolated from the rest of the inference stack. The choice of for the query latent is likewise unmotivated in print. “I don’t know” is the honest answer to all three.
Adopted by
- DeepSeek V2 · DeepSeek-AI — Original MLA introduction. 60 layers; d_c = 512, d_R = 64. [source]
- DeepSeek V3 · DeepSeek-AI — Same MLA design as V2 across 61 layers. [source]
- DeepSeek V3.1 · DeepSeek-AI — Same MLA design carried over from V3. [source]
- DeepSeek V3.2-Exp · DeepSeek-AI — MLA with the DeepSeek Sparse Attention layer added on top (Lightning Indexer + top-K selection). [source]
- Kimi K2 · Moonshot AI — MLA-style latent attention with a decoupled RoPE head — the same pattern introduced in DeepSeek-V2. [source]
- Kimi Linear 48B-A3B · Moonshot AI — MLA on 7 of 27 full-attention layers in the hybrid stack (kv_lora_rank 512, decoupled head dim 64); remaining 20 layers run KDA linear attention. [source]
Lineage
Cite
BibTeX entry for the original paper
@article{arxiv2405_04434,
title = {DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model},
author = {DeepSeek-AI},
year = {2024},
eprint = {2405.04434},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2405.04434}
} Or cite the paper directly: arXiv:2405.04434.
Export
BibTeX
@article{arxiv_2405_04434,
title = {DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model},
author = {DeepSeek-AI},
year = {2024},
eprint = {2405.04434},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2405.04434}
} CSL JSON
{
"id": "arxiv_2405_04434",
"type": "article-journal",
"title": "DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model",
"author": [
{
"literal": "DeepSeek-AI"
}
],
"issued": {
"date-parts": [
[
2024
]
]
},
"URL": "https://arxiv.org/abs/2405.04434",
"number": "2405.04434",
"source": "arXiv"
} RIS
TY - JOUR
TI - DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model
AU - DeepSeek-AI
PY - 2024
JO - arXiv
AN - arXiv:2405.04434
UR - https://arxiv.org/abs/2405.04434
ER -