DeepSeek-V2 是 236B 总参 / 21B 激活的 MoE 模型,核心创新为 Multi-head Latent Attention (MLA,KVCache 压缩 93.3%) 和 DeepSeekMoE(细粒度专家 + 共享专家隔离)。在 8.1T token 上预训练后经 SFT+GRPO 对齐,以 21B 激活参数达到开源 SOTA 水平,推理吞吐 5.76× DeepSeek 67B。
扩大 LLM 参数量可提升智能但带来训练成本和推理效率的双重挑战。KVCache 是推理瓶颈——标准 MHA 需缓存 $2n_h d_h$ 元素/token/层。GQA/MQA 压缩 KVCache 但损失模型质量。需要一种 既压缩 KVCache 又不损失甚至提升质量 的 attention 机制。
低秩 KV 联合压缩:
$$\mathbf{c}_t^{KV} = W^{DKV} \mathbf{h}_t, \quad \mathbf{c}_t^{KV} \in \mathbb{R}^{d_c}$$
推理时仅缓存 $\mathbf{c}_t^{KV}$($d_c$ 维),而非完整 K 和 V($2n_h d_h$ 维)。还原 K/V 的上投影矩阵 $W^{UK}$、$W^{UV}$ 可分别吸收进 $W^Q$ 和 $W^O$——推理时 无需显式重建 K/V。
Decoupled RoPE:RoPE 与低秩压缩不兼容(RoPE 矩阵插入 $W^Q$ 和 $W^{UK}$ 之间使吸收失败)。解决方案:引入额外的 decoupled query $\mathbf{q}_t^R$ 和共享 key $\mathbf{k}_t^R$ 专门承载位置信息:
$$\mathbf{k}_t^R = \text{RoPE}(W^{KR} \mathbf{h}_t), \quad \mathbf{k}_t^R \in \mathbb{R}^{d_h^R}$$
最终 key = $[\mathbf{k}_t^C; \mathbf{k}_t^R]$,总 KVCache = $(d_c + d_h^R) \times l$ 元素。
KVCache 对比:
| Mechanism | KV Cache / Token | Equivalent GQA Groups | Quality |
|---|---|---|---|
| MHA | $2n_h d_h$ | $n_h$ | Strong |
| GQA | $2n_g d_h$ | $n_g$ | Moderate |
| MQA | $2d_h$ | 1 | Weak |
| MLA | $(d_c + d_h^R) \approx 4.5 d_h$ | 2.25 | Stronger than MHA |
DeepSeek-V2 设置 $d_c = 4d_h = 512$, $d_h^R = d_h/2 = 64$。KVCache = GQA-2.25 水平但质量超 MHA——通过低秩瓶颈的正则化效应或跨头结构捕获实现。
$$\mathbf{h}_t' = \mathbf{u}_t + \sum_{i=1}^{N_s} \text{FFN}_i^{(s)}(\mathbf{u}_t) + \sum_{i=1}^{N_r} g_{i,t} \text{FFN}_i^{(r)}(\mathbf{u}_t)$$
核心技术壁垒:MLA 中 decoupled RoPE 的设计——$\mathbf{k}_t^R$ 在所有 head 间 共享 位置信息,仅增加 $d_h^R = 64$ 维 cache,但打破了 per-head 独立性假设却不损失质量。这一 insight 使 RoPE 与低秩压缩兼容,是 MLA 能实际部署的关键。
推理时关键优化:$W^{UK}$ 吸收进 $W^{UQ}$(precompute $W^{UQ} \cdot W^{UK\top}$),$W^{UV}$ 吸收进 $W^O$。因此推理时不需要从 $\mathbf{c}_t^{KV}$ 恢复完整的 K/V 向量。
| 符号 | 值 (DeepSeek-V2) | 含义 |
|---|---|---|
| $d$ | 5120 | Hidden dimension |
| $n_h$ | 128 | Attention heads |
| $d_h$ | 128 | Per-head dimension |
| $d_c$ | 512 = $4d_h$ | KV compression dimension |
| $d_c'$ | 1536 | Query compression dimension |
| $d_h^R$ | 64 = $d_h/2$ | Decoupled RoPE dimension |
| $N_s$ | 2 | Shared experts |
| $N_r$ | 160 | Routed experts |
| $K_r$ | 6 | Activated routed experts per token |
| $M$ | 3 | Device-limited routing max devices |
| Model Scale | MHA KV Cache / Token | MLA KV Cache / Token | Compression Ratio | Quality |
|---|---|---|---|---|
| Small (15.7B total) | 110.6K elements | 15.6K elements | 7.1× | MLA better (BBH 39 vs 37.9, MMLU 50 vs 48.7) |
| Large (247B total) | 860.2K elements | 34.6K elements | 24.9× | MLA better (BBH 50.7 vs 46.6, MMLU 59 vs 57.5) |
MLA 在两个 scale 上都 严格优于 MHA(质量更好 + cache 更小)——是论文最强的验证结果。
| Benchmark | DeepSeek-V2 (21B act.) | LLaMA3-70B (70B act.) | Gap |
|---|---|---|---|
| MMLU | 78.5 | 78.9 | -0.4 (≈持平) |
| BBH | 78.9 | 81.0 | -2.1 |
| MATH | 43.6 | 42.2 | +1.4 |
| GSM8K | 79.2 | 83.0 | -3.8 |
| HumanEval | 48.8 | 48.2 | +0.6 |
| C-Eval | 81.7 | 67.5 | +14.2 |
| CMMLU | 84.0 | 69.3 | +14.7 |
以 21B activated(LLaMA3 的 30%)达到接近 70B dense 的英文水平,中文大幅领先。
| Model | MT-Bench | AlpacaEval 2.0 LC | AlignBench |
|---|---|---|---|
| DeepSeek-V2 Chat (RL) | 8.97 | 38.9 | 7.91 |
| LLaMA3-70B Instruct | 8.95 | 34.4 | — |
| GPT-4-0613 | — | — | 7.53 |
DeepSeek-V2 Chat (RL) 在 MT-Bench 上微超 LLaMA3-70B Instruct,AlignBench 上超越 GPT-4-0613。
| Benchmark | SFT | RL | Delta |
|---|---|---|---|
| BBH | 81.3 | 79.7 | -1.6 |
| HumanEval | 76.8 | 81.1 | +4.3 |
| MATH | 52.7 | 53.9 | +1.2 |
| MT-Bench | 8.62 | 8.97 | +0.35 |
| AlpacaEval 2.0 | 30.0 | 38.9 | +8.9 |
RL 显著提升 code/math/open-ended 但损失 BBH(-1.6)——选择性 alignment tax。
| Step | Claim | Evidence | Strength |
|---|---|---|---|
| 1 | MHA KVCache 是推理瓶颈 | Table 1 (KV cache comparison); 定量分析 $2n_h d_h$ | Strong |
| 2 | GQA/MQA 压缩 KVCache 但损失质量 | Table 8 (MQA < GQA < MHA ablation at 7B) | Strong |
| 3 | MLA 通过低秩联合压缩实现 cache < GQA-2.25 且质量 > MHA | Table 9 (MLA vs MHA at two scales) | Strong — 核心消融 |
| 4 | Decoupled RoPE 使 MLA 兼容位置编码 | 逻辑论证(矩阵不可交换性) | Medium — 无消融 |
| 5 | DeepSeekMoE + device-limited routing 实现经济训练 | 42.5% cost savings; 定性论证 $M=3$ 足够 | Medium — routing 无消融 |
| 6 | 端到端:21B activated 达到 70B dense 水平 | Table 2, 3, 4, 5 | Strong — 全面 benchmark |
关键实现细节:
| Stage | Goal | Data | LR Schedule | Context | Techniques |
|---|---|---|---|---|---|
| Pre-training | 基础能力 | 8.1T tokens (Chinese > English ~12%) | Warmup 2K steps → step-decay (×0.316 at 60%, 90%) | 4K | Max LR $2.4 \times 10^{-4}$, batch 2304→9216 |
| Context extension | 128K context | YaRN on decoupled $\mathbf{k}_t^R$ | — | 32K (train) → 128K (eval) | 1000 steps, batch 576, $s=40$ |
| SFT | Chat 能力 | 1.5M instances (1.2M helpful + 0.3M safety) | $5 \times 10^{-6}$ | 128K | 2 epochs |
| RL Stage 1 | 推理对齐 | Code/math reasoning reward | — | — | GRPO, code compiler + math ground-truth RM |
| RL Stage 2 | 偏好对齐 | Helpful + safety + rule-based reward | — | — | GRPO, multi-reward: $c_1 \cdot RM_{helpful} + c_2 \cdot RM_{safety} + c_3 \cdot RM_{rule}$ |
最难复制的训练 trick:两阶段 RL 策略——先用代码编译器/数学标准答案做客观 reward 训练推理能力(这些能力随训练步数持续提升),再用人类偏好 reward 对齐通用能力。这个分阶段策略避免了推理和偏好目标的冲突。
| 精度 | 权重 | KV Cache/Token | 最小 GPU |
|---|---|---|---|
| BF16 | ~472 GB | $(512+64) \times 2 \times 60 = 69,120$ bytes | 8×H800 (640 GB) |
| FP8 | ~236 GB | ~6-bit avg → ~43K bytes | 8×H800 (with headroom) |