DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model
Read first: GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints · RoFormer: Enhanced Transformer with Rotary Position Embedding
TL;DR. Multi-head Latent Attention (MLA) compresses each token’s keys and values into one 512-dimensional latent vector and caches only that, plus a small 64-dimensional key that carries RoPE and is shared across heads. At inference, the up-projections are folded into the query and output matrices, so full keys and values are never rebuilt. DeepSeek-V2 (236B total, 21B active, 128K context) uses MLA with DeepSeekMoE. Compared with DeepSeek 67B, it cuts the KV cache by 93.3%.
Why it matters now. Long-context RAG readers and deep-research agents with long trajectories are limited by KV-cache memory, not FLOPs. The cache sets how many requests or rollouts fit on a GPU, which also matters for RL with long rollouts. MLA is the attention used in DeepSeek-V3. It is the strongest published answer to “how small can the cache get without the quality loss of GQA or MQA?”
Key idea
Section titled “Key idea”Standard MHA caches elements per token per layer. MLA caches a joint latent instead:
with . Since , the matrix can be absorbed into the query projection, and into the output projection. Queries are compressed too (, ), but only to save activation memory during training.
Decoupled RoPE. RoPE puts a position-dependent rotation between and , which blocks absorption. MLA therefore adds a separate RoPE part:
The softmax is scaled by . The cache per token is elements. With and , that is , the same as GQA with only 2.25 groups (Table 1).
Key results
Section titled “Key results”- Against DeepSeek 67B (§1, §3.2.3). Training cost falls by 42.5% (172.8K versus 300.6K H800 GPU hours per trillion tokens), the KV cache falls by 93.3%, and maximum generation throughput rises 5.76×. On one 8×H800 node, generation exceeds 50K tokens per second, with the cache quantised to about 6 bits per element.
- Cache formulas (Table 1). MHA , GQA , MQA , MLA .
- MHA, GQA, MQA on 7B dense models (Appendix D.1, 1.33T tokens). MMLU 45.2, 41.2, 37.9. BBH 37.0, 35.6, 33.2.
- MLA versus MHA in MoE models (Appendix D.2). Small (about 16B, 1.33T tokens): MMLU 48.7 → 50.0 with 14% of the cache. Large (about 250B, 420B tokens): MMLU 57.5 → 59.0 and BBH 46.6 → 50.7 with 4% of the cache (34.6K versus 860.2K elements per token).
- Long context (§3.1.4). After YaRN on and 1000 steps at 32K, needle-in-a-haystack holds up to 128K.
Limitations & open questions
Section titled “Limitations & open questions”- MLA must be built in during pretraining. Converting existing GQA or MHA checkpoints is not addressed.
- Absorbed attention works in a 512-dimensional latent per head, so compute per cached token rises even as bytes fall. The balance shifts with batch size and hardware.
- The evidence that MLA is “better than MHA” comes from one seed per setting and four benchmarks, with the large models trained for only 420B tokens.
- The low-rank bottleneck could limit tasks that need many independent key subspaces (for example, precise retrieval over many distractors). Harder long-context tests such as RULER would probe this.
Connections
Section titled “Connections”- Alternatives for shrinking the cache: GQA and MQA, paged KV memory, and sparse reads with NSA. Recurrent models with no KV cache: Mamba, Gated DeltaNet.
- Position: RoPE and YaRN.
- Same model’s FFN: DeepSeekMoE. Successor: DeepSeek-V3.
- IR analogue: token-vector compression in ColBERTv2 and PLAID.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
Use DeepSeek-V2's attention sizes: 60 layers, 128 heads, head dim 128, latent dim 512, decoupled RoPE dim 64. Compute the KV cache per token, in elements and in bfloat16 bytes, for full multi-head attention, for GQA with 8 groups at the same head size, and for MLA. Then give the cache for a single 128K-token context in each case. Finally, show algebraically why MLA with latent dim 4·d_h and RoPE dim d_h/2 equals GQA with 2.25 groups.
What a strong answer covers
- MHA: 2 × 128 × 128 × 60 = 1,966,080 elements, about 3.9 MB per token. At 131,072 tokens that is about 515 GB, more than six 80 GB GPUs for one request.
- GQA-8: 2 × 8 × 128 × 60 = 122,880 elements, about 246 KB per token, or about 32 GB at 128K.
- MLA: (512 + 64) × 60 = 34,560 elements, about 69 KB per token, or about 9.1 GB at 128K. That is 1.8% of MHA and 28% of GQA-8 here.
- Per layer, MLA stores d_c + d_hᴿ = 4d_h + d_h/2 = 4.5·d_h. GQA stores 2·n_g·d_h. Setting them equal gives n_g = 2.25.
- In deployment the paper also quantises the cache to about 6 bits per element, which shrinks these numbers by about another 2.7× relative to bfloat16.
- Mechanism
Show why MLA never needs to materialise per-head keys and values at inference, by writing one head's attention score in terms of the cached latent. Then explain exactly why applying RoPE to the up-projected keys would break this trick, and how the decoupled RoPE key fixes it.
What a strong answer covers
- Score for head i: qᵢᵀ kᵢ = (W^UQᵢ c^Q)ᵀ (W^UKᵢ c^KV) = c^Qᵀ (W^UQᵢᵀ W^UKᵢ) c^KV. The product W^UQᵢᵀ W^UKᵢ is fixed, so W^UK can be folded into the query side. Likewise W^UV folds into W^O. Attention runs directly against the cached 512-dimensional latent.
- RoPE multiplies keys by a rotation that depends on position. With RoPE on kᶜ, the score has a position-dependent matrix between W^UQ and W^UK. That matrix changes for every pair of positions, so it cannot be pre-multiplied once. Matrix products do not commute, so it cannot be moved out of the way.
- Fix: carry position in a separate small part. Use extra per-head RoPE queries qᴿ and one RoPE key kᴿ of dimension 64 shared by all heads, computed from hₜ and cached.
- Concatenate: q = [qᶜ; qᴿ] and k = [kᶜ; kᴿ], with softmax scale 1/√(d_h + d_hᴿ). The content part stays absorbable, and only 64 extra numbers per token per layer are cached.
- Side effect: YaRN context extension is applied only to kᴿ, which is the only part that carries RoPE.
- What if…
Appendix D compares MQA, GQA-8 and MHA on 7B dense models, and MLA against MHA on small (about 16B) and large (about 250B) MoE models. Summarise what each comparison shows, and explain why the first comparison is needed to make the second persuasive.
What a strong answer covers
- Dense 7B (D.1): quality orders MHA > GQA-8 > MQA. For example MMLU is 45.2, 41.2 and 37.9, and C-Eval is 42.9, 37.7 and 30.0. Sharing KV heads costs real quality at this scale.
- MoE (D.2): MLA matches or beats MHA. Small: BBH 37.9 → 39.0, MMLU 48.7 → 50.0, CMMLU 52.3 → 53.4, with C-Eval slightly lower (51.6 → 50.9). Large: all four improve, for example BBH 46.6 → 50.7.
- Cache: MLA uses 14% (small) and 4% (large) of MHA's KV cache.
- D.1 establishes that cheaper caches usually lose quality. Without it, "MLA ≈ MHA" could just mean the benchmarks are insensitive to attention.
- Caveats: one seed per setting, four benchmarks, and the large models are trained for only 420B tokens. The 250B MLA-versus-MHA gap may not hold at full training.
- Design
You are choosing the attention type for a 7B reader model that must handle 128K-token retrieved contexts for many concurrent users on one 8-GPU academic node. Compare GQA-8, MLA and MHA with KV quantisation, in terms of memory per request, decode arithmetic intensity, quality risk and engineering cost. Take the head count and head size as 32 × 128 to keep numbers concrete.
What a strong answer covers
- Memory per token per layer: MHA 2 × 32 × 128 = 8192 elements, GQA-8 2048, and MLA with d_c = 512 and d_hᴿ = 64 gives 576. Memory per request scales linearly with context length, so MLA allows about 3.6× more concurrent 128K requests than GQA-8.
- Decode intensity: in absorbed form, every head reads the same 576-number latent and computes a 576-dim score plus a 512-dim value sum. That gives much more compute per byte read than MHA. Decoding moves from memory-bound toward compute-bound, which suits GPUs.
- Quality risk: the paper's evidence says MLA ≈ MHA > GQA at its scales. But you cannot easily convert an existing GQA checkpoint, so MLA means pretraining or major continued training.
- Engineering: MLA needs custom kernels for the absorbed form. GQA plus 4- to 8-bit KV quantisation works with stock vLLM and is cheaper to adopt.
- Decision: for a new pretraining run, use MLA. For fine-tuning an existing open model, keep its attention and add KV quantisation and paged memory.
- Critique
DeepSeek-V2 reports a 93.3% KV-cache reduction and 5.76× higher maximum generation throughput compared with DeepSeek 67B. Critique how much of this is due to MLA, and design a comparison that isolates MLA's contribution.
What a strong answer covers
- The baseline differs in many ways at once: dense versus MoE (21B active versus 67B parameters), a different architecture and training run, and possibly different serving setups. V2 also quantises its KV cache to about 6 bits.
- Maximum throughput depends on how many sequences fit in memory, so cache size, weight memory and kernels all interact. The 5.76× cannot be attributed to MLA alone.
- Isolating experiment: take the same MoE backbone and serving stack, and swap only the attention (MHA, GQA-8, MLA) with the cache precision fixed. Measure max batch, tokens per second and quality at 4K, 32K and 128K.
- Also report per-token latency at batch 1, where MLA's extra compute per cached token may show up as a cost rather than a gain.
- Connect
MLA caches one compressed vector per token and reconstructs per-head keys on the fly. ColBERTv2 and PLAID store one compressed vector per document token and decompress or score them at query time. Compare the two designs: what is compressed, how scoring avoids full decompression, and what each gives up.
What a strong answer covers
- MLA: a learned linear down-projection to 512 dims, trained end-to-end. Scoring uses absorbed matrices, so the full keys are never rebuilt.
- ColBERTv2: centroid ID plus quantised residual per token, built after training. PLAID scores with centroids first and decompresses only candidates.
- Both rely on per-token representations having low intrinsic dimension and use structure (linearity, or centroids) to score in the compressed space.
- Trade-offs: MLA's compression is lossless with respect to its own trained model but must be learned during pretraining. ColBERT compression is post hoc and lossy, and quality depends on the codebook.
- A research angle: learned low-rank token compression trained jointly with a late-interaction retriever, in the style of MLA.