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 LL layers with HH heads of dimension dhd_h, 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 — L=95L = 95, H=64H = 64, dh=128d_h = 128, fp16 — the cache cost is 2LHdh2 B3.1 MB per token2 \cdot L \cdot H \cdot d_h \cdot 2\text{ B} \approx 3.1\text{ MB per token}. 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 L=60L = 60, H=128H = 128, dh=128d_h = 128 (DeepSeek-V2, Table 1).

The three predecessors all chase this number, with diminishing returns.

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 htRd\mathbf{h}_t \in \mathbb{R}^{d} be the residual-stream vector at position tt. A standard MHA layer with HH heads of dimension dhd_h computes, for head i{1,,H}i \in \{1, \dots, H\}:

qt(i)=WQ(i)ht,kt(i)=WK(i)ht,vt(i)=WV(i)ht,\mathbf{q}^{(i)}_t = W^{(i)}_Q \mathbf{h}_t, \quad \mathbf{k}^{(i)}_t = W^{(i)}_K \mathbf{h}_t, \quad \mathbf{v}^{(i)}_t = W^{(i)}_V \mathbf{h}_t,

with WQ(i),WK(i),WV(i)Rdh×dW^{(i)}_Q, W^{(i)}_K, W^{(i)}_V \in \mathbb{R}^{d_h \times d}. The per-token cache footprint is 2Hdh2 \cdot H \cdot d_h scalars per layer — the two vectors kt(i)\mathbf{k}^{(i)}_t and vt(i)\mathbf{v}^{(i)}_t stacked across heads. For H=128H = 128, dh=128d_h = 128, L=60L = 60 (DeepSeek-V2) this is 1,966,0801{,}966{,}080 scalars or about 3.84 MB at fp16 per token.

Low-rank latent. MLA replaces the per-head WK(i),WV(i)W^{(i)}_K, W^{(i)}_V with a shared down-projection to a single latent ctKV\mathbf{c}^{KV}_t of dimension dcHdhd_c \ll H \cdot d_h, and per-head up-projections that reconstruct K and V at attention time (DeepSeek-V2, eq. 9–11):

ctKV=WDKVhtRdc,ktC,(i)=WUK(i)ctKV,vt(i)=WUV(i)ctKV,\mathbf{c}^{KV}_t = W_{DKV} \mathbf{h}_t \in \mathbb{R}^{d_c}, \qquad \mathbf{k}^{C,(i)}_t = W^{(i)}_{UK} \mathbf{c}^{KV}_t, \qquad \mathbf{v}^{(i)}_t = W^{(i)}_{UV} \mathbf{c}^{KV}_t,

with WDKVRdc×dW_{DKV} \in \mathbb{R}^{d_c \times d} and WUK(i),WUV(i)Rdh×dcW^{(i)}_{UK}, W^{(i)}_{UV} \in \mathbb{R}^{d_h \times d_c}. The superscript CC marks the “content” part of the key, distinguished below from a position-aware part. Only ctKV\mathbf{c}^{KV}_t enters the cache: dcd_c scalars per token per layer instead of 2Hdh2 H d_h. DeepSeek-V2 picks dc=512d_c = 512, so the latent is 164\tfrac{1}{64} the size of the stacked per-head K and V.

Why this is not just GQA with G=1G = 1. MQA also caches one K and one V per layer, of dimension dhd_h each — 2dh=2562 d_h = 256 scalars at DeepSeek-V2’s dh=128d_h = 128. The MLA latent is 512, twice as large in raw scalars, but expresses a dcd_c-dimensional subspace from which every head’s K and V are independently reconstructed by its own WUK(i),WUV(i)W^{(i)}_{UK}, W^{(i)}_{UV}. 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 ctQ=WDQhtRdc\mathbf{c}^{Q}_t = W_{DQ} \mathbf{h}_t \in \mathbb{R}^{d_c'} with dc=1536d_c' = 1536, then up-projects per head: qtC,(i)=WUQ(i)ctQ\mathbf{q}^{C,(i)}_t = W^{(i)}_{UQ} \mathbf{c}^{Q}_t (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 dc=3dcd_c' = 3 d_c is reported without a sensitivity study.

The RoPE obstruction. Rotary position embeddings (Su et al., 2021) multiply qt(i)\mathbf{q}^{(i)}_t and kt(i)\mathbf{k}^{(i)}_t by a position-dependent rotation RtRdh×dhR_t \in \mathbb{R}^{d_h \times d_h} before the inner product. With latent compression, the natural attention score for head ii between positions tt (query) and ss (key) is

qtC,(i)WUK(i)csKV.\mathbf{q}^{C,(i)}_t{}^{\top} W^{(i)}_{UK}{}^{\top} \, \mathbf{c}^{KV}_s.

The trick that makes MLA cheap at inference is to absorb WUK(i)W^{(i)}_{UK}{}^{\top} into the query-side matrix WUQ(i)W^{(i)}_{UQ} once, offline: define W~Q(i)=WUK(i)WUQ(i)\tilde{W}^{(i)}_Q = W^{(i)}_{UK} W^{(i)}_{UQ} and read the cached csKV\mathbf{c}^{KV}_s directly. But if RoPE rotates the per-head K after the up-projection, the score becomes (RtqtC,(i))RsWUK(i)csKV(R_t \mathbf{q}^{C,(i)}_t)^{\top} R_s W^{(i)}_{UK} \mathbf{c}^{KV}_s — the RsR_s depends on ss, so the up-projection cannot be precomposed: every cached token would need its WUK(i)W^{(i)}_{UK} 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):

kt(i)=[WUK(i)ctKVcontent, dh;  RtWKRhtshared RoPE, dR],\mathbf{k}^{(i)}_t = \big[\, \underbrace{W^{(i)}_{UK} \mathbf{c}^{KV}_t}_{\text{content, } d_h}; \; \underbrace{R_t \, W_{KR} \mathbf{h}_t}_{\text{shared RoPE, } d_R} \,\big],

where WKRRdR×dW_{KR} \in \mathbb{R}^{d_R \times d} produces a single dRd_R-dimensional rotary key shared across all heads, with dR=64d_R = 64 in DeepSeek-V2. The query is split symmetrically: qt(i)=[WUQ(i)ctQ;RtWQR(i)ctQ]\mathbf{q}^{(i)}_t = [W^{(i)}_{UQ} \mathbf{c}^{Q}_t ;\, R_t W^{(i)}_{QR} \mathbf{c}^{Q}_t]. 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 ctKV\mathbf{c}^{KV}_t and the single shared ktRRdR\mathbf{k}^{R}_t \in \mathbb{R}^{d_R}, i.e., dc+dR=512+64=576d_c + d_R = 512 + 64 = 576 scalars per layer per token. Across 60 layers that is 34,56034{,}560 scalars or ~67.5 KB per token at fp16, versus 3.84 MB for MHA on the same geometry — a 57×\sim 57\times 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

WDKV+i=1H(WUK(i)+WUV(i))+WKR=ddc+2Hdhdc+ddR.|W_{DKV}| + \sum_{i=1}^{H} \big( |W^{(i)}_{UK}| + |W^{(i)}_{UV}| \big) + |W_{KR}| = d \cdot d_c + 2 H \cdot d_h \cdot d_c + d \cdot d_R.

For DeepSeek-V2 (d=5120d = 5120, H=128H = 128, dh=128d_h = 128, dc=512d_c = 512, dR=64d_R = 64) this is 5120512+2128128512+51206419.4M5120 \cdot 512 + 2 \cdot 128 \cdot 128 \cdot 512 + 5120 \cdot 64 \approx 19.4\text{M} parameters per layer for the K/V path. Standard MHA at the same geometry uses 2dHdh=25120128128167.8M2 \cdot d \cdot H \cdot d_h = 2 \cdot 5120 \cdot 128 \cdot 128 \approx 167.8\text{M}, so MLA’s KV-side weight count is ~12% of MHA’s. The query-side compression adds another ddc+Hdhdc33.6Md \cdot d_c' + H \cdot d_h \cdot d_c' \approx 33.6\text{M} parameters per layer.

Computational complexity. Per layer, MLA’s attention compute for sequence length TT is O(T2Hdh)\mathcal{O}(T^2 H d_h) for the score and value-application — identical to MHA in the asymptotic sense — plus O(Tddc)\mathcal{O}(T \cdot d \cdot d_c) for the latent down-projection and O(THdhdc)\mathcal{O}(T \cdot H \cdot d_h \cdot d_c) for the per-head up-projections. The absorption trick hides the up-projection behind the query: at inference, the cached csKV\mathbf{c}^{KV}_s is read directly as a dcd_c-vector and the merged W~Q(i)\tilde{W}^{(i)}_Q produces head-ii queries that score against it without ever materializing ksC,(i)\mathbf{k}^{C,(i)}_s. 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]
KV cache layout: query heads on top, key/value groups on bottom, connecting lines show which queries read from which KV store.GQA: 16 query heads → 8 KV groupsQueriesQ0Q1Q2Q3Q4Q5Q6Q7Q8Q9Q10Q11Q12Q13Q14Q15KV groupsK,V0K,V1K,V2K,V3K,V4K,V5K,V6K,V7KV bytes / token / layer (fp16):4.0 KB(2.0× smaller than MHA)
G = 16 is MHA (no sharing). G = 1 is MQA (every head shares one K, V). Intermediate G values are GQA. Switch to MLA to compress K, V into a small per-token latent.

§ 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 G=8G = 8) 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 dc=512d_c = 512, dR=64d_R = 64, dc=1536d_c' = 1536, H=128H = 128, dh=128d_h = 128 across 61 layers with d=7168d = 7168 (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 dc+dRd_c + d_R vector rather than HH 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 dc=512,dR=64d_c = 512, d_R = 64 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 O(T2Hdh)\mathcal{O}(T^2 H d_h)) and adds a low-rank projection that costs a fraction of a percent of total FLOPs at HdhdcH d_h \gg d_c. 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 dcd_c (would dc=256d_c = 256 work? dc=1024d_c = 1024?) is not published. The sensitivity to dRd_R — 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 WUK(i)W^{(i)}_{UK} into W~Q(i)\tilde{W}^{(i)}_Q 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 dc=3dcd_c' = 3 d_c 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  -