<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>长上下文 on 北海</title>
    <link>https://blog-6sm.pages.dev/tags/%E9%95%BF%E4%B8%8A%E4%B8%8B%E6%96%87/</link>
    <description>Recent content in 长上下文 on 北海</description>
    <image>
      <title>北海</title>
      <url>https://blog-6sm.pages.dev/images/og-default.png</url>
      <link>https://blog-6sm.pages.dev/images/og-default.png</link>
    </image>
    <generator>Hugo</generator>
    <language>zh</language>
    <copyright>2026</copyright>
    <lastBuildDate>Sat, 25 Apr 2026 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://blog-6sm.pages.dev/tags/%E9%95%BF%E4%B8%8A%E4%B8%8B%E6%96%87/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>DeepSeek-V4：迈向高效的百万 Token 上下文智能</title>
      <link>https://blog-6sm.pages.dev/readings/deepseek-v4/</link>
      <pubDate>Sat, 25 Apr 2026 00:00:00 +0000</pubDate>
      <guid>https://blog-6sm.pages.dev/readings/deepseek-v4/</guid>
      <description>DeepSeek-V4 技术报告全文翻译与批注。V4 系列包含两个 MoE 模型（1.6T/284B 参数），通过混合压缩注意力（CSA&#43;HCA）、流形约束超连接（mHC）和 Muon 优化器，将百万 token 上下文的推理 FLOPs 降至 V3.2 的 27%、KV 缓存降至 10%。后训练采用领域专家独立训练 &#43; 在策略蒸馏（OPD）的新范式。</description>
      <content:encoded><![CDATA[<blockquote>
<p>原文：<a href="https://huggingface.co/deepseek-ai/DeepSeek-V4-Pro/blob/main/DeepSeek_V4.pdf">DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligence</a></p>
<p>作者：DeepSeek-AI</p>
<p>模型下载：<a href="https://huggingface.co/collections/deepseek-ai/deepseek-v4">https://huggingface.co/collections/deepseek-ai/deepseek-v4</a></p>
<p>开源实现：<a href="https://huggingface.co/deepseek-ai/DeepSeek-V4-Pro/tree/main/inference">https://huggingface.co/deepseek-ai/DeepSeek-V4-Pro/tree/main/inference</a></p>
</blockquote>
<hr>
<h1 id="1-引言">1. 引言</h1>
<p>推理模型（reasoning models）的出现 (DeepSeek-AI, 2025; OpenAI, 2024c) 开创了测试时扩展（test-time scaling）的新范式，为大语言模型（Large Language Models, LLMs）带来了显著的性能提升。然而，这一扩展范式从根本上受制于原始注意力机制（vanilla attention mechanism）的二次方计算复杂度 (Vaswani et al., 2017)，这为超长上下文和推理过程带来了难以逾越的瓶颈。与此同时，长程场景和任务的涌现——从复杂的智能体工作流（agentic workflows）到大规模跨文档分析——也使得对超长上下文的高效支持成为未来发展的关键需求。虽然近期的开源工作 (Bai et al., 2025a; DeepSeek-AI, 2024; MiniMax, 2025; Qwen, 2025) 在通用能力方面取得了进步，但在处理超长序列方面的核心架构低效问题仍然是一大阻碍，限制了测试时扩展的进一步收益，也阻碍了对长程场景和任务的深入探索。</p>
<p>为了突破超长上下文中的效率瓶颈，我们开发了 DeepSeek-V4 系列模型，包括预览版本 DeepSeek-V4-Pro（1.6T 参数，49B 激活）和 DeepSeek-V4-Flash（284B 参数，13B 激活）。通过架构创新，DeepSeek-V4 系列在处理超长序列的计算效率方面实现了质的飞跃。这一突破使得高效支持一百万 token 的上下文长度成为可能，开启了下一代大语言模型百万级上下文的新时代。我们相信，高效处理超长序列的能力将解锁测试时扩展的下一个前沿，为长程任务的深入研究铺平道路，并为探索在线学习（online learning）等未来范式奠定必要的基础。</p>
<p>与 DeepSeek-V3 架构 (DeepSeek-AI, 2024) 相比，DeepSeek-V4 系列保留了 DeepSeekMoE 框架 (Dai et al., 2024) 和多 token 预测（Multi-Token Prediction, MTP）策略，同时在架构和优化方面引入了若干关键创新。为增强长上下文效率，我们设计了一种混合注意力机制，结合了压缩稀疏注意力（Compressed Sparse Attention, CSA）和重度压缩注意力（Heavily Compressed Attention, HCA）。CSA 沿序列维度压缩键值缓存（KV caches），然后执行 DeepSeek 稀疏注意力（DeepSeek Sparse Attention, DSA）(DeepSeek-AI, 2025)；而 HCA 则对键值缓存施加更激进的压缩，但保持稠密注意力。为增强建模能力，我们引入了流形约束超连接（Manifold-Constrained Hyper-Connections, <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC）(Xie et al., 2026)，以升级传统的残差连接。此外，我们引入了 Muon 优化器 (Jordan et al., 2024; Liu et al., 2025)，用于 DeepSeek-V4 系列的训练，以实现更快的收敛和更高的训练稳定性。</p>
<p>为了实现 DeepSeek-V4 系列的高效训练与推理以及高效的开发流程，我们引入了若干基础设施优化。首先，我们为 MoE 模块设计并实现了一个统一的融合算子（fused kernel），完全重叠计算、通信和内存访问。其次，我们采用了 TileLang (Wang et al., 2026)，一种领域特定语言（Domain-Specific Language, DSL），以平衡开发效率和运行时性能。第三，我们提供了高效的批次不变（batch-invariant）和确定性（deterministic）算子库，以确保训练和推理之间的逐位可复现性（bitwise reproducibility）。第四，我们将 FP4 量化感知训练（quantization-aware training）应用于 MoE 专家权重和索引器 QK 路径，以减少内存和计算开销。第五，在训练框架方面，我们通过张量级检查点（tensor-level checkpointing）扩展了自动微分（autograd）框架，以实现细粒度的重计算控制；并通过混合 ZeRO 策略提升训练效率，以支持 Muon 优化器和低成本的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 实现（通过重计算和融合算子）、以及两阶段上下文并行（two-stage contextual parallelism）来管理压缩注意力。最后，在推理框架方面，我们设计了一种异构键值缓存（heterogeneous KV cache）结构，配合基于磁盘的存储策略，以实现高效的共享前缀复用（shared-prefix reuse）。</p>
<p>通过采用混合 CSA 和 HCA 以及计算和存储方面的精度优化，DeepSeek-V4 系列在推理浮点运算量（FLOPs）和键值缓存大小方面均较 DeepSeek-V3.2 实现了显著降低，尤其是在长上下文场景下。图 1 的右半部分展示了 DeepSeek-V3.2 和 DeepSeek-V4 系列的估计单 token 推理浮点运算量和累计键值缓存大小。在 1M token 上下文的场景下，即使拥有更多激活参数的 DeepSeek-V4-Pro，其单 token 浮点运算量（以等效 FP8 FLOPs 计算）仅为 DeepSeek-V3.2 的 27%，键值缓存大小仅为其 10%。此外，拥有更少激活参数的 DeepSeek-V4-Flash 将效率推向了更高水平：在 1M token 上下文场景下，其单 token 浮点运算量仅为 DeepSeek-V3.2 的 10%，键值缓存大小仅为其 7%。此外，对于 DeepSeek-V4 系列，被路由的专家参数采用 FP4 精度。虽然目前 FP4 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span> FP8 运算的峰值浮点运算量与 FP8 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span> FP8 在现有硬件上相同，但理论上在未来硬件上可以实现 1/3 的更高效率，这将进一步提升 DeepSeek-V4 系列的效率。</p>
<p>在预训练阶段，我们在 32T token 上训练 DeepSeek-V4-Flash，在 33T token 上训练 DeepSeek-V4-Pro。预训练完成后，这两个模型可以原生且高效地支持 1M 长度的上下文。在我们的内部评估中，DeepSeek-V4-Flash-Base 凭借其更高参数效率的设计，在大多数基准测试中已超越 DeepSeek-V3.2-Base。DeepSeek-V4-Pro-Base 进一步扩大了这一优势，在 DeepSeek 基座模型中树立了新的性能标杆，在推理、编码、长上下文和世界知识任务上展现出全面的优越性。</p>
<p>DeepSeek-V4 系列的后训练流程采用两阶段范式：先独立培养领域专家（domain-specific experts），再通过在策略蒸馏（on-policy distillation）进行统一模型整合。具体而言，对于每个目标领域——如数学、编码、智能体和指令遵循——各自独立训练一个专家模型。基座模型首先在高质量的领域特定数据上进行监督微调（Supervised Fine-Tuning, SFT），以建立基础能力。随后，应用基于群体相对策略优化（Group Relative Policy Optimization, GRPO）(DeepSeek-AI, 2025) 的强化学习（Reinforcement Learning, RL），通过针对特定成功标准定制的奖励模型进一步优化模型的领域对齐行为。这一阶段产生了一组多样化的专业专家，每个专家在各自领域表现卓越。最后，为了整合这些独立的专长，通过在策略蒸馏训练一个统一模型，其中统一模型作为学生，学习优化反向 KL 散度损失（reverse KL loss）。</p>
<p><strong>核心评估结果摘要</strong></p>
<ul>
<li>
<p><strong>知识</strong>：在广泛世界知识的评估中，DeepSeek-V4-Pro-Max（DeepSeek-V4-Pro 的最大推理力度模式）在 SimpleQA (OpenAI, 2024d) 和 Chinese-SimpleQA (He et al., 2024) 基准测试上显著优于领先的开源模型。在教育知识方面——通过 MMLU-Pro (Wang et al., 2024b)、HLE (Phan et al., 2025) 和 GPQA (Rein et al., 2023) 评估——DeepSeek-V4-Pro-Max 相较其开源同类模型展现出微弱的领先优势。DeepSeek-V4-Pro-Max 已大幅缩小了与领先闭源模型 Gemini-3.1-Pro 之间的差距，尽管在这些基于知识的评估中仍略有落后。</p>
</li>
<li>
<p><strong>推理</strong>：通过扩展推理 token，DeepSeek-V4-Pro-Max 在标准推理基准测试上展现出优于 GPT-5.2 和 Gemini-3.0-Pro 的性能。然而，其表现与 GPT-5.4 和 Gemini-3.1-Pro 相比仍有微小差距，表明其发展轨迹大约落后于最先进的前沿模型 3 到 6 个月。此外，DeepSeek-V4-Flash-Max 取得了与 GPT-5.2 和 Gemini-3.0-Pro 相当的表现，确立了其作为高性价比复杂推理架构的地位。</p>
</li>
</ul>
<p><img alt="DeepSeek-V4 系列基准测试结果" loading="lazy" src="/readings/deepseek-v4/images/fig1-benchmark.png"></p>
<ul>
<li>
<p><strong>智能体</strong>：在公开基准测试上，DeepSeek-V4-Pro-Max 与领先的开源模型（如 Kimi-K2.6 和 GLM-5.1）表现相当，但略逊于前沿闭源模型。在我们的内部评估中，DeepSeek-V4-Pro-Max 超越了 Claude Sonnet 4.5，接近 Opus 4.5 的水平。</p>
</li>
<li>
<p><strong>长上下文</strong>：DeepSeek-V4-Pro-Max 在百万 token 上下文窗口下，于合成和实际用例中均取得了出色的结果，在学术基准测试上甚至超越了 Gemini-3.1-Pro。</p>
</li>
<li>
<p><strong>DeepSeek-V4-Pro 与 DeepSeek-V4-Flash 的对比</strong>：DeepSeek-V4-Flash-Max 在知识评估中因参数规模较小而表现略低。然而，在分配更大思考预算时，其在推理任务上能取得可比的结果。在智能体评估中，虽然 DeepSeek-V4-Flash-Max 在若干基准测试上与 DeepSeek-V4-Pro-Max 表现相当，但在更复杂、更高难度的任务上仍落后于后者。</p>
</li>
</ul>
<h1 id="2-架构">2. 架构</h1>
<p>总体而言，DeepSeek-V4 系列保留了 Transformer (Vaswani et al., 2017) 架构和多 token 预测（Multi-Token Prediction, MTP）模块 (DeepSeek-AI, 2024; Gloeckle et al., 2024)，同时相比 DeepSeek-V3 引入了若干关键升级：(1) 首先，我们引入流形约束超连接（Manifold-Constrained Hyper-Connections, <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC）(Xie et al., 2026) 以增强传统残差连接；(2) 其次，我们设计了一种混合注意力架构，通过压缩稀疏注意力（Compressed Sparse Attention）和重度压缩注意力（Heavily Compressed Attention）大幅提升长上下文效率；(3) 第三，我们采用 Muon (Jordan et al., 2024; Liu et al., 2025) 作为优化器。对于混合专家（Mixture-of-Experts, MoE）组件，我们仍然沿用 DeepSeekMoE (Dai et al., 2024) 架构，仅做了少量调整。多 token 预测（MTP）(DeepSeek-AI, 2024; Gloeckle et al., 2024; Li et al., 2024; Qi et al., 2020) 的配置与 DeepSeek-V3 中建立的设置保持一致。所有其他未明确说明的细节均遵循 DeepSeek-V3 (DeepSeek-AI, 2024) 中的设定。图 2 展示了 DeepSeek-V4 的整体架构，具体细节如下所述。</p>
<p><img alt="DeepSeek-V4 系列的整体架构。我们使用混合 CSA（压缩稀疏注意力）和 HCA（重度压缩注意力）作为注意力层，使用 DeepSeekMoE 作为前馈层，并通过 <span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>m</mi></mrow><annotation encoding=\"application/x-tex\">m</annotation></semantics></math></span>HC 增强传统残差连接。" loading="lazy" src="/readings/deepseek-v4/images/fig2-architecture.png"></p>
<h2 id="21-继承自-deepseek-v3-的设计">2.1. 继承自 DeepSeek-V3 的设计</h2>
<p><strong>混合专家（Mixture-of-Experts）。</strong> 与此前的 DeepSeek 系列模型 (DeepSeek-AI, 2024; DeepSeek-AI, 2024) 一样，DeepSeek-V4 系列同样采用 DeepSeekMoE 范式 (Dai et al., 2024) 构建前馈网络（Feed-Forward Networks, FFNs），该范式设置了细粒度的路由专家和共享专家。与 DeepSeek-V3 不同的是，我们将计算亲和力分数（affinity scores）的激活函数从 Sigmoid(<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>⋅</mo></mrow><annotation encoding="application/x-tex">\cdot</annotation></semantics></math></span>) 改为 Sqrt(Softplus(<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>⋅</mo></mrow><annotation encoding="application/x-tex">\cdot</annotation></semantics></math></span>))。在负载均衡方面，我们同样采用无辅助损失（auxiliary-loss-free）策略 (DeepSeek-AI, 2024; Wang et al., 2024a)，并辅以轻量的序列级平衡损失（sequence-wise balance loss），以防止单个序列内出现极端不平衡。对于 DeepSeek-V4，我们移除了对路由目标节点数量的约束，并精心重新设计了并行策略以保持训练效率。此外，与 DeepSeek-V3 相比，我们将最初若干 Transformer 块中的稠密前馈网络（dense FFN）层替换为采用哈希路由（Hash routing）(Roller et al., 2021) 的 MoE 层。哈希路由策略根据预定义的哈希函数，依据输入 token ID 为每个 token 确定目标专家。</p>
<p><strong>多 token 预测（Multi-Token Prediction）。</strong> 与 DeepSeek-V3 一样，DeepSeek-V4 系列也设置了 MTP 模块和目标。鉴于 MTP 策略已在 DeepSeek-V3 中得到验证，我们在 DeepSeek-V4 系列中沿用了相同的策略，未做修改。</p>
<h2 id="22-流形约束超连接">2.2. 流形约束超连接</h2>
<p>如图 2 所示，DeepSeek-V4 系列引入了流形约束超连接（Manifold-Constrained Hyper-Connections, <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC）(Xie et al., 2026)，以增强相邻 Transformer 块之间的传统残差连接。与朴素超连接（naive Hyper-Connections, HC）(Zhu et al., 2025) 相比，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 的核心思想是将残差映射约束到一个特定流形上，从而增强跨层信号传播的稳定性，同时保持模型的表达能力。本小节将简要介绍标准 HC，并描述我们如何设计 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 以实现稳定训练。</p>
<p><strong>标准超连接（Standard Hyper-Connections）。</strong> 标准 HC 将残差流的宽度按 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{hc}}</annotation></semantics></math></span> 倍扩展。具体而言，残差流的形状从 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">\mathbb{R}^d</annotation></semantics></math></span> 扩展到 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">\mathbb{R}^{n_{\text{hc}} \times d}</annotation></semantics></math></span>，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span> 是实际层输入的隐藏维度大小。令 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>X</mi><mi>l</mi></msub><mo>=</mo><mo stretchy="false">[</mo><msub><mi mathvariant="bold">x</mi><mrow><mi>l</mi><mo separator="true">,</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msub><mi mathvariant="bold">x</mi><mrow><mi>l</mi><mo separator="true">,</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msub><msup><mo stretchy="false">]</mo><mi>T</mi></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">X_l = [\mathbf{x}_{l,1}; \ldots; \mathbf{x}_{l,n_{\text{hc}}}]^T \in \mathbb{R}^{n_{\text{hc}} \times d}</annotation></semantics></math></span> 为第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi></mrow><annotation encoding="application/x-tex">l</annotation></semantics></math></span> 层之前的残差状态。HC 引入三个线性映射：输入映射 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>A</mi><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mn>1</mn><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">A_l \in \mathbb{R}^{1 \times n_{\text{hc}}}</annotation></semantics></math></span>、残差变换 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">B_l \in \mathbb{R}^{n_{\text{hc}} \times n_{\text{hc}}}</annotation></semantics></math></span>，以及输出映射 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>C</mi><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><mn>1</mn></mrow></msup></mrow><annotation encoding="application/x-tex">C_l \in \mathbb{R}^{n_{\text{hc}} \times 1}</annotation></semantics></math></span>。残差状态的更新公式如下：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>X</mi><mrow><mi>l</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>=</mo><msub><mi>B</mi><mi>l</mi></msub><msub><mi>X</mi><mi>l</mi></msub><mo>+</mo><msub><mi>C</mi><mi>l</mi></msub><msub><mi mathvariant="script">F</mi><mi>l</mi></msub><mo stretchy="false">(</mo><msub><mi>A</mi><mi>l</mi></msub><msub><mi>X</mi><mi>l</mi></msub><mo stretchy="false">)</mo><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(1)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">X_{l+1} = B_l X_l + C_l \mathcal{F}_l(A_l X_l), \tag{1}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="script">F</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">\mathcal{F}_l</annotation></semantics></math></span> 表示第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi></mrow><annotation encoding="application/x-tex">l</annotation></semantics></math></span> 层（例如 MoE 层），其输入和输出形状均为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">\mathbb{R}^d</annotation></semantics></math></span>。注意实际的层输入 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>A</mi><mi>l</mi></msub><msub><mi>X</mi><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">A_l X_l \in \mathbb{R}^d</annotation></semantics></math></span> 也是 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span> 维的，因此扩展的残差宽度不影响内部层的设计。HC 将残差宽度与实际隐藏大小解耦，以最小的计算开销提供了一个互补的缩放轴，因为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{hc}}</annotation></semantics></math></span> 通常远小于隐藏大小 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span>。然而，尽管 HC 已展现出提升模型性能的潜力，我们发现在堆叠多层时训练会频繁出现数值不稳定现象，这阻碍了 HC 的扩展。</p>
<blockquote>
<p><strong>译注：公式 (1) 的直觉理解</strong></p>
<p>对比标准 Transformer 的残差连接 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>x</mi><mrow><mi>l</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>=</mo><msub><mi>x</mi><mi>l</mi></msub><mo>+</mo><msub><mi mathvariant="script">F</mi><mi>l</mi></msub><mo stretchy="false">(</mo><msub><mi>x</mi><mi>l</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">x_{l+1} = x_l + \mathcal{F}_l(x_l)</annotation></semantics></math></span>，HC 做了三件事：(a) 将残差流从一条&quot;管道&quot;扩展为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{hc}}</annotation></semantics></math></span> 条并行管道（DeepSeek-V4 中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>=</mo><mn>4</mn></mrow><annotation encoding="application/x-tex">n_{\text{hc}}=4</annotation></semantics></math></span>）；(b) 用矩阵 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>A</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">A_l</annotation></semantics></math></span> 将多条管道加权混合为一条，送入实际的层计算；(c) 层的输出通过 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>C</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">C_l</annotation></semantics></math></span> 分配回各管道，再加上残差流经 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span> 的线性变换。可以把它想象成一条四车道高速公路：车辆（信息）在四条车道间通过 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span> 换道和混合，而匝道（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>A</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">A_l</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>C</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">C_l</annotation></semantics></math></span>）连接高速公路和每一层的&quot;服务区&quot;（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="script">F</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">\mathcal{F}_l</annotation></semantics></math></span>）。问题在于：如果 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span> 的谱范数大于 1，信号在逐层传播时会指数级放大——经过 61 层后可能爆炸；小于 1 则会消失。这就是为什么需要 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 来约束 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span>。</p>
</blockquote>
<p><strong>流形约束残差映射（Manifold-Constrained Residual Mapping）。</strong> <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 的核心创新在于将残差映射矩阵 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span> 约束到双随机矩阵（doubly stochastic matrices）的流形上（即 Birkhoff 多胞形，Birkhoff polytope）<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">M</mi></mrow><annotation encoding="application/x-tex">\mathcal{M}</annotation></semantics></math></span>，从而增强跨层信号传播的稳定性：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>B</mi><mi>l</mi></msub><mo>∈</mo><mi mathvariant="script">M</mi><mo><mi mathvariant="normal">≔</mi></mo><mo stretchy="false">{</mo><mi>M</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>n</mi></mrow></msup><mo>∣</mo><mi>M</mi><msub><mn mathvariant="bold">1</mn><mi>n</mi></msub><mo>=</mo><msub><mn mathvariant="bold">1</mn><mi>n</mi></msub><mo separator="true">,</mo><mtext> </mtext><msubsup><mn mathvariant="bold">1</mn><mi>n</mi><mi>T</mi></msubsup><mi>M</mi><mo>=</mo><msubsup><mn mathvariant="bold">1</mn><mi>n</mi><mi>T</mi></msubsup><mo separator="true">,</mo><mtext> </mtext><mi>M</mi><mo>⩾</mo><mn>0</mn><mo stretchy="false">}</mo><mi mathvariant="normal">.</mi></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(2)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">B_l \in \mathcal{M} \coloneqq \{M \in \mathbb{R}^{n \times n} \mid M\mathbf{1}_n = \mathbf{1}_n,\ \mathbf{1}_n^T M = \mathbf{1}_n^T,\ M \geqslant 0\}. \tag{2}</annotation></semantics></math></span><blockquote>
<p><strong>译注：双随机矩阵约束的直觉</strong></p>
<p>双随机矩阵是什么？它是一个每行之和 = 1、每列之和 = 1、且所有元素 ≥ 0 的方阵。最简单的例子就是单位矩阵 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>I</mi></mrow><annotation encoding="application/x-tex">I</annotation></semantics></math></span>——它是一个双随机矩阵。置换矩阵（交换行的矩阵）也是。事实上，所有双随机矩阵都是置换矩阵的凸组合（这就是 Birkhoff 定理）。</p>
<p>为什么这个约束能稳定训练？关键性质是：双随机矩阵的谱范数（最大奇异值）恰好 ≤ 1。这意味着 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span> 作为线性变换<strong>永远不会放大信号</strong>。考虑一个极端的例子：如果 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">B_l</annotation></semantics></math></span> 的谱范数是 1.01，经过 61 层后信号被放大 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>1.01</mn><mn>61</mn></msup><mo>≈</mo><mn>1.83</mn></mrow><annotation encoding="application/x-tex">1.01^{61} \approx 1.83</annotation></semantics></math></span> 倍；如果是 1.1，则放大 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>1.1</mn><mn>61</mn></msup><mo>≈</mo><mn>339</mn></mrow><annotation encoding="application/x-tex">1.1^{61} \approx 339</annotation></semantics></math></span> 倍——这就是训练不稳定的根源。约束到双随机矩阵后，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">∥</mi><msub><mi>B</mi><mi>l</mi></msub><msub><mi mathvariant="normal">∥</mi><mn>2</mn></msub><mo>≤</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\|B_l\|_2 \leq 1</annotation></semantics></math></span> 被数学保证，信号永远不会爆炸。更妙的是，双随机矩阵在乘法下封闭（两个双随机矩阵的乘积仍然是双随机矩阵），所以无论堆叠多少层，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mn>61</mn></msub><msub><mi>B</mi><mn>60</mn></msub><mo>⋯</mo><msub><mi>B</mi><mn>1</mn></msub></mrow><annotation encoding="application/x-tex">B_{61} B_{60} \cdots B_1</annotation></semantics></math></span> 仍然是非扩张的。</p>
</blockquote>
<p>该约束确保映射矩阵 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">∥</mi><msub><mi>B</mi><mi>l</mi></msub><msub><mi mathvariant="normal">∥</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">\|B_l\|_2</annotation></semantics></math></span> 的谱范数不超过 1，因此残差变换是非扩张的（non-expansive），这增强了前向传播和反向传播过程中的数值稳定性。此外，集合 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">M</mi></mrow><annotation encoding="application/x-tex">\mathcal{M}</annotation></semantics></math></span> 在乘法下封闭，这保证了深层 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 堆叠场景下的稳定性。另外，输入变换 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>A</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">A_l</annotation></semantics></math></span> 和输出变换 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>C</mi><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">C_l</annotation></semantics></math></span> 也通过 Sigmoid 函数约束为非负且有界的，以避免信号抵消的风险。</p>
<p><strong>动态参数化（Dynamic Parameterization）。</strong> 三个线性映射的参数是动态生成的，被分解为一个动态（输入依赖）分量和一个静态（输入无关）分量。给定输入 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>X</mi><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">X_l \in \mathbb{R}^{n_{\text{hc}} \times d}</annotation></semantics></math></span>，首先将其展平并归一化：<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>X</mi><mo>^</mo></mover><mi>l</mi></msub><mo>=</mo><mtext>RMSNorm</mtext><mo stretchy="false">(</mo><mtext>vec</mtext><mo stretchy="false">(</mo><msub><mi>X</mi><mi>l</mi></msub><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mn>1</mn><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">\hat{X}_l = \text{RMSNorm}(\text{vec}(X_l)) \in \mathbb{R}^{1 \times n_{\text{hc}} d}</annotation></semantics></math></span>。然后，遵循传统 HC 的方式，生成无约束的原始参数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>A</mi><mo>~</mo></mover><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mn>1</mn><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">\tilde{A}_l \in \mathbb{R}^{1 \times n_{\text{hc}}}</annotation></semantics></math></span>、<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>B</mi><mo>~</mo></mover><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">\tilde{B}_l \in \mathbb{R}^{n_{\text{hc}} \times n_{\text{hc}}}</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>C</mi><mo>~</mo></mover><mi>l</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><mn>1</mn></mrow></msup></mrow><annotation encoding="application/x-tex">\tilde{C}_l \in \mathbb{R}^{n_{\text{hc}} \times 1}</annotation></semantics></math></span>：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mover accent="true"><mi>A</mi><mo>~</mo></mover><mi>l</mi></msub><mo>=</mo><msubsup><mi>α</mi><mi>l</mi><mtext>pre</mtext></msubsup><mo>⋅</mo><mo stretchy="false">(</mo><msub><mover accent="true"><mi>X</mi><mo>^</mo></mover><mi>l</mi></msub><msubsup><mi>W</mi><mi>l</mi><mtext>pre</mtext></msubsup><mo stretchy="false">)</mo><mo>+</mo><msubsup><mi>S</mi><mi>l</mi><mtext>pre</mtext></msubsup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(3)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\tilde{A}_l = \alpha_l^{\text{pre}} \cdot (\hat{X}_l W_l^{\text{pre}}) + S_l^{\text{pre}}, \tag{3}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mover accent="true"><mi>B</mi><mo>~</mo></mover><mi>l</mi></msub><mo>=</mo><msubsup><mi>α</mi><mi>l</mi><mtext>res</mtext></msubsup><mo>⋅</mo><mtext>Mat</mtext><mo stretchy="false">(</mo><msub><mover accent="true"><mi>X</mi><mo>^</mo></mover><mi>l</mi></msub><msubsup><mi>W</mi><mi>l</mi><mtext>res</mtext></msubsup><mo stretchy="false">)</mo><mo>+</mo><msubsup><mi>S</mi><mi>l</mi><mtext>res</mtext></msubsup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(4)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\tilde{B}_l = \alpha_l^{\text{res}} \cdot \text{Mat}(\hat{X}_l W_l^{\text{res}}) + S_l^{\text{res}}, \tag{4}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mover accent="true"><mi>C</mi><mo>~</mo></mover><mi>l</mi></msub><mo>=</mo><msubsup><mi>α</mi><mi>l</mi><mtext>post</mtext></msubsup><mo>⋅</mo><mo stretchy="false">(</mo><msub><mover accent="true"><mi>X</mi><mo>^</mo></mover><mi>l</mi></msub><msubsup><mi>W</mi><mi>l</mi><mtext>post</mtext></msubsup><msup><mo stretchy="false">)</mo><mi>T</mi></msup><mo>+</mo><msubsup><mi>S</mi><mi>l</mi><mtext>post</mtext></msubsup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(5)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\tilde{C}_l = \alpha_l^{\text{post}} \cdot (\hat{X}_l W_l^{\text{post}})^T + S_l^{\text{post}}, \tag{5}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>W</mi><mi>l</mi><mtext>pre</mtext></msubsup><mo separator="true">,</mo><msubsup><mi>W</mi><mi>l</mi><mtext>post</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mi>d</mi><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">W_l^{\text{pre}}, W_l^{\text{post}} \in \mathbb{R}^{n_{\text{hc}} d \times n_{\text{hc}}}</annotation></semantics></math></span>，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>W</mi><mi>l</mi><mtext>res</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mi>d</mi><mo>×</mo><msubsup><mi>n</mi><mtext>hc</mtext><mn>2</mn></msubsup></mrow></msup></mrow><annotation encoding="application/x-tex">W_l^{\text{res}} \in \mathbb{R}^{n_{\text{hc}} d \times n_{\text{hc}}^2}</annotation></semantics></math></span> 是用于生成动态分量的可学习参数；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Mat</mtext><mo stretchy="false">(</mo><mo>⋅</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Mat}(\cdot)</annotation></semantics></math></span> 将大小为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>×</mo><msubsup><mi>n</mi><mtext>hc</mtext><mn>2</mn></msubsup></mrow><annotation encoding="application/x-tex">1 \times n_{\text{hc}}^2</annotation></semantics></math></span> 的向量重塑为大小为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{hc}} \times n_{\text{hc}}</annotation></semantics></math></span> 的矩阵；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>S</mi><mi>l</mi><mtext>pre</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mn>1</mn><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">S_l^{\text{pre}} \in \mathbb{R}^{1 \times n_{\text{hc}}}</annotation></semantics></math></span>、<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>S</mi><mi>l</mi><mtext>post</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><mn>1</mn></mrow></msup></mrow><annotation encoding="application/x-tex">S_l^{\text{post}} \in \mathbb{R}^{n_{\text{hc}} \times 1}</annotation></semantics></math></span>、<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>S</mi><mi>l</mi><mtext>res</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>×</mo><msub><mi>n</mi><mtext>hc</mtext></msub></mrow></msup></mrow><annotation encoding="application/x-tex">S_l^{\text{res}} \in \mathbb{R}^{n_{\text{hc}} \times n_{\text{hc}}}</annotation></semantics></math></span> 是可学习的静态偏置；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>α</mi><mi>l</mi><mtext>pre</mtext></msubsup><mo separator="true">,</mo><msubsup><mi>α</mi><mi>l</mi><mtext>res</mtext></msubsup><mo separator="true">,</mo><msubsup><mi>α</mi><mi>l</mi><mtext>post</mtext></msubsup><mo>∈</mo><mi mathvariant="double-struck">R</mi></mrow><annotation encoding="application/x-tex">\alpha_l^{\text{pre}}, \alpha_l^{\text{res}}, \alpha_l^{\text{post}} \in \mathbb{R}</annotation></semantics></math></span> 是初始化为小值的可学习门控因子。</p>
<p><strong>施加参数约束（Applying Parameter Constraints）。</strong> 在获得无约束的原始参数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>A</mi><mo>~</mo></mover><mi>l</mi></msub><mo separator="true">,</mo><msub><mover accent="true"><mi>B</mi><mo>~</mo></mover><mi>l</mi></msub><mo separator="true">,</mo><msub><mover accent="true"><mi>C</mi><mo>~</mo></mover><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">\tilde{A}_l, \tilde{B}_l, \tilde{C}_l</annotation></semantics></math></span> 后，我们对其施加前述约束以增强数值稳定性。具体而言，对于输入和输出映射，我们使用 Sigmoid 函数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>σ</mi><mo stretchy="false">(</mo><mo>⋅</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\sigma(\cdot)</annotation></semantics></math></span> 以确保其非负性和有界性：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>A</mi><mi>l</mi></msub><mo>=</mo><mi>σ</mi><mo stretchy="false">(</mo><msub><mover accent="true"><mi>A</mi><mo>~</mo></mover><mi>l</mi></msub><mo stretchy="false">)</mo><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(6)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">A_l = \sigma(\tilde{A}_l), \tag{6}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>C</mi><mi>l</mi></msub><mo>=</mo><mn>2</mn><mi>σ</mi><mo stretchy="false">(</mo><msub><mover accent="true"><mi>C</mi><mo>~</mo></mover><mi>l</mi></msub><mo stretchy="false">)</mo><mi mathvariant="normal">.</mi></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(7)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">C_l = 2\sigma(\tilde{C}_l). \tag{7}</annotation></semantics></math></span><p>对于残差映射 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>B</mi><mo>~</mo></mover><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">\tilde{B}_l</annotation></semantics></math></span>，我们将其投影到双随机矩阵流形 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">M</mi></mrow><annotation encoding="application/x-tex">\mathcal{M}</annotation></semantics></math></span> 上。这通过 Sinkhorn-Knopp 算法实现，该算法首先对 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi>B</mi><mo>~</mo></mover><mi>l</mi></msub></mrow><annotation encoding="application/x-tex">\tilde{B}_l</annotation></semantics></math></span> 施加指数函数以确保正性，得到 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>M</mi><mrow><mo stretchy="false">(</mo><mn>0</mn><mo stretchy="false">)</mo></mrow></msup><mo>=</mo><mi>exp</mi><mo>⁡</mo><mo stretchy="false">(</mo><msub><mover accent="true"><mi>B</mi><mo>~</mo></mover><mi>l</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">M^{(0)} = \exp(\tilde{B}_l)</annotation></semantics></math></span>，然后交替进行列归一化和行归一化：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msup><mi>M</mi><mrow><mo stretchy="false">(</mo><mi>t</mi><mo stretchy="false">)</mo></mrow></msup><mo>=</mo><msub><mi mathvariant="script">T</mi><mi>r</mi></msub><mo stretchy="false">(</mo><msub><mi mathvariant="script">T</mi><mi>c</mi></msub><mo stretchy="false">(</mo><msup><mi>M</mi><mrow><mo stretchy="false">(</mo><mi>t</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></msup><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(8)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">M^{(t)} = \mathcal{T}_r(\mathcal{T}_c(M^{(t-1)})), \tag{8}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="script">T</mi><mi>r</mi></msub></mrow><annotation encoding="application/x-tex">\mathcal{T}_r</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="script">T</mi><mi>c</mi></msub></mrow><annotation encoding="application/x-tex">\mathcal{T}_c</annotation></semantics></math></span> 分别表示行归一化和列归一化。该迭代收敛至一个约束双随机矩阵 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>B</mi><mi>l</mi></msub><mo>=</mo><msup><mi>M</mi><mrow><mo stretchy="false">(</mo><msub><mi>t</mi><mi>max</mi><mo>⁡</mo></msub><mo stretchy="false">)</mo></mrow></msup></mrow><annotation encoding="application/x-tex">B_l = M^{(t_{\max})}</annotation></semantics></math></span>。我们选择 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mi>max</mi><mo>⁡</mo></msub><mo>=</mo><mn>20</mn></mrow><annotation encoding="application/x-tex">t_{\max} = 20</annotation></semantics></math></span> 作为实际值。</p>
<blockquote>
<p><strong>译注：Sinkhorn-Knopp 算法的直觉</strong></p>
<p>如何把一个任意的正矩阵&quot;投影&quot;到双随机矩阵？Sinkhorn-Knopp 算法非常简单：交替做行归一化和列归一化。想象你有一个 4×4 的正矩阵（因为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub><mo>=</mo><mn>4</mn></mrow><annotation encoding="application/x-tex">n_{\text{hc}}=4</annotation></semantics></math></span>）：(1) 先把每一行除以该行的和，使每行之和 = 1；(2) 再把每一列除以该列的和，使每列之和 = 1；(3) 此时行和可能不再等于 1 了，所以再做一次行归一化&hellip; 如此交替进行。Sinkhorn (1964) 证明了这个过程一定收敛到一个双随机矩阵。DeepSeek 选择迭代 20 次——对于 4×4 矩阵来说，这已经足够收敛到很高精度了。注意在反向传播时，梯度需要穿过这 20 次迭代，但由于矩阵很小（4×4），计算开销可以忽略。</p>
</blockquote>
<h2 id="23-基于-csa-和-hca-的混合注意力">2.3. 基于 CSA 和 HCA 的混合注意力</h2>
<p>当上下文长度达到极端规模时，注意力机制成为模型中主要的计算瓶颈。为此，DeepSeek-V4 设计了两种高效注意力架构——压缩稀疏注意力（Compressed Sparse Attention, CSA）和重度压缩注意力（Heavily Compressed Attention, HCA）——并采用交错混合配置，大幅降低了长文本场景下注意力的计算开销。CSA 整合了压缩和稀疏两种注意力策略：它首先将每 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个 token 的键值（Key-Value, KV）缓存压缩为一个条目，然后应用 DeepSeek 稀疏注意力（DeepSeek Sparse Attention, DSA）(DeepSeek-AI, 2025)，使每个查询 token 仅关注 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math></span> 个压缩后的键值条目。HCA 旨在通过将每 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span>（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>≫</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">\gg m</annotation></semantics></math></span>）个 token 的键值缓存合并为一个条目来实现极致压缩。CSA 和 HCA 的混合架构显著提升了 DeepSeek-V4 系列的长上下文效率，使百万 token 上下文在实践中变得可行。本小节描述了我们混合注意力架构的核心技术，我们还提供了一个开源实现<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mrow></mrow><mn>1</mn></msup></mrow><annotation encoding="application/x-tex">^1</annotation></semantics></math></span>以便明确地说明细节。</p>
<h3 id="231-压缩稀疏注意力">2.3.1. 压缩稀疏注意力</h3>
<p>CSA 的核心架构如图 3 所示，它首先将每 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个 token 的键值缓存压缩为一个条目，然后应用 DeepSeek 稀疏注意力进一步加速。</p>
<p><img alt="CSA 的核心架构。它将键值条目的数量压缩为 <span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mfrac><mn>1</mn><mi>m</mi></mfrac></mrow><annotation encoding=\"application/x-tex\">\\frac{1}{m}</annotation></semantics></math></span> 倍，然后应用 DeepSeek 稀疏注意力进一步加速。此外，一小组滑动窗口键值条目与选中的压缩键值条目拼接在一起，以增强局部细粒度依赖关系。" loading="lazy" src="/readings/deepseek-v4/images/fig3-csa.png"></p>
<p><strong>压缩键值条目（Compressed Key-Value Entries）。</strong> 令 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>H</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">H \in \mathbb{R}^{n \times d}</annotation></semantics></math></span> 为输入隐藏状态序列，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi></mrow><annotation encoding="application/x-tex">n</annotation></semantics></math></span> 为序列长度，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span> 为隐藏维度大小。CSA 首先计算两组键值条目 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>a</mi></msup><mo separator="true">,</mo><msup><mi>C</mi><mi>b</mi></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">C^a, C^b \in \mathbb{R}^{n \times c}</annotation></semantics></math></span> 及其对应的压缩权重 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>Z</mi><mi>a</mi></msup><mo separator="true">,</mo><msup><mi>Z</mi><mi>b</mi></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">Z^a, Z^b \in \mathbb{R}^{n \times c}</annotation></semantics></math></span>，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi></mrow><annotation encoding="application/x-tex">c</annotation></semantics></math></span> 是注意力头维度：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msup><mi>C</mi><mi>a</mi></msup><mo>=</mo><mi>H</mi><mo>⋅</mo><msup><mi>W</mi><mrow><mi>a</mi><mi>K</mi><mi>V</mi></mrow></msup><mo separator="true">,</mo><mspace width="1em"/><msup><mi>C</mi><mi>b</mi></msup><mo>=</mo><mi>H</mi><mo>⋅</mo><msup><mi>W</mi><mrow><mi>b</mi><mi>K</mi><mi>V</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(9)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">C^a = H \cdot W^{aKV}, \quad C^b = H \cdot W^{bKV}, \tag{9}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msup><mi>Z</mi><mi>a</mi></msup><mo>=</mo><mi>H</mi><mo>⋅</mo><msup><mi>W</mi><mrow><mi>a</mi><mi>Z</mi></mrow></msup><mo separator="true">,</mo><mspace width="1em"/><msup><mi>Z</mi><mi>b</mi></msup><mo>=</mo><mi>H</mi><mo>⋅</mo><msup><mi>W</mi><mrow><mi>b</mi><mi>Z</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(10)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">Z^a = H \cdot W^{aZ}, \quad Z^b = H \cdot W^{bZ}, \tag{10}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>a</mi><mi>K</mi><mi>V</mi></mrow></msup><mo separator="true">,</mo><msup><mi>W</mi><mrow><mi>b</mi><mi>K</mi><mi>V</mi></mrow></msup><mo separator="true">,</mo><msup><mi>W</mi><mrow><mi>a</mi><mi>Z</mi></mrow></msup><mo separator="true">,</mo><msup><mi>W</mi><mrow><mi>b</mi><mi>Z</mi></mrow></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>d</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">W^{aKV}, W^{bKV}, W^{aZ}, W^{bZ} \in \mathbb{R}^{d \times c}</annotation></semantics></math></span> 是可训练参数。接下来，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>a</mi></msup></mrow><annotation encoding="application/x-tex">C^a</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>b</mi></msup></mrow><annotation encoding="application/x-tex">C^b</annotation></semantics></math></span> 中每 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个键值条目将根据其压缩权重和可学习的位置偏置 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>B</mi><mi>a</mi></msup><mo separator="true">,</mo><msup><mi>B</mi><mi>b</mi></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>m</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">B^a, B^b \in \mathbb{R}^{m \times c}</annotation></semantics></math></span> 被压缩为一个条目，生成 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mtext>Comp</mtext></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mfrac><mi>n</mi><mi>m</mi></mfrac><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">C^{\text{Comp}} \in \mathbb{R}^{\frac{n}{m} \times c}</annotation></semantics></math></span>。每个压缩条目 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>i</mi><mtext>Comp</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>c</mi></msup></mrow><annotation encoding="application/x-tex">C_i^{\text{Comp}} \in \mathbb{R}^c</annotation></semantics></math></span> 的计算如下：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mo stretchy="false">[</mo><msubsup><mi>S</mi><mrow><mi>m</mi><mo>:</mo><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo>−</mo><mn>1</mn></mrow><mi>a</mi></msubsup><mo separator="true">;</mo><msubsup><mi>S</mi><mrow><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>:</mo><mi>m</mi><mi>i</mi><mo>−</mo><mn>1</mn></mrow><mi>b</mi></msubsup><mo stretchy="false">]</mo><mo>=</mo><msub><mtext>Softmax</mtext><mtext>row</mtext></msub><mo stretchy="false">(</mo><mo stretchy="false">[</mo><msubsup><mi>Z</mi><mrow><mi>m</mi><mo>:</mo><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo>−</mo><mn>1</mn></mrow><mi>a</mi></msubsup><mo>+</mo><msup><mi>B</mi><mi>a</mi></msup><mo separator="true">;</mo><msubsup><mi>Z</mi><mrow><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>:</mo><mi>m</mi><mi>i</mi><mo>−</mo><mn>1</mn></mrow><mi>b</mi></msubsup><mo>+</mo><msup><mi>B</mi><mi>b</mi></msup><mo stretchy="false">]</mo><mo stretchy="false">)</mo><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(11)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">[S^a_{m:m(i+1)-1}; S^b_{m(i-1):mi-1}] = \text{Softmax}_{\text{row}}([Z^a_{m:m(i+1)-1} + B^a; Z^b_{m(i-1):mi-1} + B^b]), \tag{11}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msubsup><mi>C</mi><mi>i</mi><mtext>Comp</mtext></msubsup><mo>=</mo><munderover><mo>∑</mo><mrow><mi>j</mi><mo>=</mo><mi>m</mi><mi>i</mi></mrow><mrow><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo>−</mo><mn>1</mn></mrow></munderover><msubsup><mi>S</mi><mi>j</mi><mi>a</mi></msubsup><mo>⊙</mo><msubsup><mi>C</mi><mi>j</mi><mi>a</mi></msubsup><mo>+</mo><munderover><mo>∑</mo><mrow><mi>j</mi><mo>=</mo><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></mrow><mrow><mi>m</mi><mi>i</mi><mo>−</mo><mn>1</mn></mrow></munderover><msubsup><mi>S</mi><mi>j</mi><mi>b</mi></msubsup><mo>⊙</mo><msubsup><mi>C</mi><mi>j</mi><mi>b</mi></msubsup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(12)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">C_i^{\text{Comp}} = \sum_{j=mi}^{m(i+1)-1} S_j^a \odot C_j^a + \sum_{j=m(i-1)}^{mi-1} S_j^b \odot C_j^b, \tag{12}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>⊙</mo></mrow><annotation encoding="application/x-tex">\odot</annotation></semantics></math></span> 表示 Hadamard 积；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>Softmax</mtext><mtext>row</mtext></msub><mo stretchy="false">(</mo><mo>⋅</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Softmax}_{\text{row}}(\cdot)</annotation></semantics></math></span> 表示沿行维度的 softmax 操作，对来自 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>Z</mi><mi>a</mi></msup></mrow><annotation encoding="application/x-tex">Z^a</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>Z</mi><mi>b</mi></msup></mrow><annotation encoding="application/x-tex">Z^b</annotation></semantics></math></span> 的共 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2</mn><mi>m</mi></mrow><annotation encoding="application/x-tex">2m</annotation></semantics></math></span> 个元素进行归一化。当 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">i = 0</annotation></semantics></math></span> 时，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>Z</mi><mrow><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>:</mo><mi>m</mi><mi>i</mi><mo>−</mo><mn>1</mn></mrow><mi>b</mi></msubsup></mrow><annotation encoding="application/x-tex">Z^b_{m(i-1):mi-1}</annotation></semantics></math></span> 填充负无穷，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mrow><mi>m</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>:</mo><mi>m</mi><mi>i</mi><mo>−</mo><mn>1</mn></mrow><mi>b</mi></msubsup></mrow><annotation encoding="application/x-tex">C^b_{m(i-1):mi-1}</annotation></semantics></math></span> 填充零。注意每个 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>i</mi><mtext>Comp</mtext></msubsup></mrow><annotation encoding="application/x-tex">C_i^{\text{Comp}}</annotation></semantics></math></span> 来自 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2</mn><mi>m</mi></mrow><annotation encoding="application/x-tex">2m</annotation></semantics></math></span> 个键值条目，但用于 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>i</mi><mtext>Comp</mtext></msubsup></mrow><annotation encoding="application/x-tex">C_i^{\text{Comp}}</annotation></semantics></math></span> 的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>b</mi></msup></mrow><annotation encoding="application/x-tex">C^b</annotation></semantics></math></span> 的索引与用于 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mrow><mi>i</mi><mo>−</mo><mn>1</mn></mrow><mtext>Comp</mtext></msubsup></mrow><annotation encoding="application/x-tex">C_{i-1}^{\text{Comp}}</annotation></semantics></math></span> 的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>a</mi></msup></mrow><annotation encoding="application/x-tex">C^a</annotation></semantics></math></span> 的索引是重叠的。因此，CSA 实际上将序列长度压缩为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>1</mn><mi>m</mi></mfrac></mrow><annotation encoding="application/x-tex">\frac{1}{m}</annotation></semantics></math></span> 倍。</p>
<blockquote>
<p><strong>译注：KV 压缩机制的直觉理解</strong></p>
<p>想象你有一本 1000 页的书（1000 个 token）。CSA 的压缩（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mo>=</mo><mn>4</mn></mrow><annotation encoding="application/x-tex">m=4</annotation></semantics></math></span>）相当于每 4 页做一个&quot;摘要卡片&quot;——但不是简单取平均，而是用学到的权重 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Z</mi></mrow><annotation encoding="application/x-tex">Z</annotation></semantics></math></span> 做加权求和，让模型自己决定每页的重要性。关键的&quot;重叠&quot;设计是：第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span> 张卡片不仅看第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>4</mn><mi>i</mi></mrow><annotation encoding="application/x-tex">4i</annotation></semantics></math></span> 到 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>4</mn><mi>i</mi><mo>+</mo><mn>3</mn></mrow><annotation encoding="application/x-tex">4i+3</annotation></semantics></math></span> 页（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>a</mi></msup></mrow><annotation encoding="application/x-tex">C^a</annotation></semantics></math></span> 部分），还看前一组的第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>4</mn><mi>i</mi><mo>−</mo><mn>4</mn></mrow><annotation encoding="application/x-tex">4i-4</annotation></semantics></math></span> 到 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>4</mn><mi>i</mi><mo>−</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">4i-1</annotation></semantics></math></span> 页（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mi>b</mi></msup></mrow><annotation encoding="application/x-tex">C^b</annotation></semantics></math></span> 部分）。这种重叠保证了相邻压缩块之间有信息交叉，不会在块边界处丢失上下文。最终 1000 个 KV 条目变成 250 个——序列长度压缩 4 倍。</p>
<p>然后闪电索引器再从这 250 个压缩条目中用 top-k 选择最相关的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math></span> 个（如 512 个），进一步降低注意力的计算量。这就是&quot;压缩 + 稀疏&quot;双重优化的含义。</p>
</blockquote>
<p><strong>用于稀疏选择的闪电索引器（Lightning Indexer for Sparse Selection）。</strong> 在获得压缩后的键值条目 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mtext>Comp</mtext></msup></mrow><annotation encoding="application/x-tex">C^{\text{Comp}}</annotation></semantics></math></span> 后，CSA 应用 DSA 策略选择 top-k 个压缩键值条目进行核心注意力计算。首先，CSA 对 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mtext>Comp</mtext></msup></mrow><annotation encoding="application/x-tex">C^{\text{Comp}}</annotation></semantics></math></span> 执行与上述相同的压缩操作，得到压缩后的索引器键（indexer keys）<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>K</mi><mtext>IComp</mtext></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mfrac><mi>n</mi><mi>m</mi></mfrac><mo>×</mo><msup><mi>c</mi><mi>I</mi></msup></mrow></msup></mrow><annotation encoding="application/x-tex">K^{\text{IComp}} \in \mathbb{R}^{\frac{n}{m} \times c^I}</annotation></semantics></math></span>，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>c</mi><mi>I</mi></msup></mrow><annotation encoding="application/x-tex">c^I</annotation></semantics></math></span> 是索引器头维度。然后，对于查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span>，以低秩方式生成索引器查询 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow><mi>I</mi></msubsup><mo separator="true">;</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow><mi>I</mi></msubsup><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow><mi>I</mi></msubsup><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\mathbf{q}^I_{t,1}; \mathbf{q}^I_{t,2}; \ldots; \mathbf{q}^I_{t,n^I_h}\}</annotation></semantics></math></span>：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup><mo>=</mo><msub><mi mathvariant="bold">h</mi><mi>t</mi></msub><mo>⋅</mo><msup><mi>W</mi><mrow><mi>D</mi><mi>Q</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(13)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\mathbf{c}_t^Q = \mathbf{h}_t \cdot W^{DQ}, \tag{13}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mo stretchy="false">[</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow><mi>I</mi></msubsup><mo separator="true">;</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow><mi>I</mi></msubsup><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow><mi>I</mi></msubsup><mo stretchy="false">]</mo><mo>=</mo><msubsup><mi mathvariant="bold">q</mi><mi>t</mi><mi>I</mi></msubsup><mo>=</mo><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup><mo>⋅</mo><msup><mi>W</mi><mrow><mi>I</mi><mi>U</mi><mi>Q</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(14)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">[\mathbf{q}^I_{t,1}; \mathbf{q}^I_{t,2}; \ldots; \mathbf{q}^I_{t,n^I_h}] = \mathbf{q}_t^I = \mathbf{c}_t^Q \cdot W^{IUQ}, \tag{14}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="bold">h</mi><mi>t</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">\mathbf{h}_t \in \mathbb{R}^d</annotation></semantics></math></span> 是查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 的输入隐藏状态；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><msub><mi>d</mi><mi>c</mi></msub></msup></mrow><annotation encoding="application/x-tex">\mathbf{c}_t^Q \in \mathbb{R}^{d_c}</annotation></semantics></math></span> 是查询的压缩潜向量；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>c</mi></msub></mrow><annotation encoding="application/x-tex">d_c</annotation></semantics></math></span> 是查询压缩维度；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow><annotation encoding="application/x-tex">n^I_h</annotation></semantics></math></span> 是索引器查询头的数量；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>D</mi><mi>Q</mi></mrow></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>d</mi><mo>×</mo><msub><mi>d</mi><mi>c</mi></msub></mrow></msup></mrow><annotation encoding="application/x-tex">W^{DQ} \in \mathbb{R}^{d \times d_c}</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>I</mi><mi>U</mi><mi>Q</mi></mrow></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>d</mi><mi>c</mi></msub><mo>×</mo><msup><mi>c</mi><mi>I</mi></msup><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow></msup></mrow><annotation encoding="application/x-tex">W^{IUQ} \in \mathbb{R}^{d_c \times c^I n^I_h}</annotation></semantics></math></span> 分别是索引器查询的下投影和上投影矩阵。接下来，查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 与其前方的压缩块 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi></mrow><annotation encoding="application/x-tex">s</annotation></semantics></math></span>（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mo>&lt;</mo><mtext>Floor</mtext><mo stretchy="false">(</mo><mfrac><mi>t</mi><mi>m</mi></mfrac><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">s &lt; \text{Floor}(\frac{t}{m})</annotation></semantics></math></span>）之间的索引分数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>I</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>s</mi></mrow></msub><mo>∈</mo><mi mathvariant="double-struck">R</mi></mrow><annotation encoding="application/x-tex">I_{t,s} \in \mathbb{R}</annotation></semantics></math></span> 计算如下：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mo stretchy="false">[</mo><msubsup><mi>w</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow><mi>I</mi></msubsup><mo separator="true">;</mo><msubsup><mi>w</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow><mi>I</mi></msubsup><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msubsup><mi>w</mi><mrow><mi>t</mi><mo separator="true">,</mo><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow><mi>I</mi></msubsup><mo stretchy="false">]</mo><mo>=</mo><msubsup><mi mathvariant="bold">w</mi><mi>t</mi><mi>I</mi></msubsup><mo>=</mo><msub><mi mathvariant="bold">h</mi><mi>t</mi></msub><mo>⋅</mo><msup><mi>W</mi><mi>w</mi></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(15)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">[w^I_{t,1}; w^I_{t,2}; \ldots; w^I_{t,n^I_h}] = \mathbf{w}_t^I = \mathbf{h}_t \cdot W^w, \tag{15}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>I</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>s</mi></mrow></msub><mo>=</mo><munderover><mo>∑</mo><mrow><mi>h</mi><mo>=</mo><mn>1</mn></mrow><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></munderover><msubsup><mi>w</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>h</mi></mrow><mi>I</mi></msubsup><mo>⋅</mo><mtext>ReLU</mtext><mrow><mo fence="true">(</mo><msubsup><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>h</mi></mrow><mi>I</mi></msubsup><mo>⋅</mo><msubsup><mi>K</mi><mi>s</mi><mtext>IComp</mtext></msubsup><mo fence="true">)</mo></mrow><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(16)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">I_{t,s} = \sum_{h=1}^{n^I_h} w^I_{t,h} \cdot \text{ReLU}\left(\mathbf{q}^I_{t,h} \cdot K_s^{\text{IComp}}\right), \tag{16}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mi>w</mi></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>d</mi><mo>×</mo><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow></msup></mrow><annotation encoding="application/x-tex">W^w \in \mathbb{R}^{d \times n^I_h}</annotation></semantics></math></span> 是可学习矩阵；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>w</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>h</mi></mrow><mi>I</mi></msubsup><mo>∈</mo><mi mathvariant="double-struck">R</mi></mrow><annotation encoding="application/x-tex">w^I_{t,h} \in \mathbb{R}</annotation></semantics></math></span> 是第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi></mrow><annotation encoding="application/x-tex">h</annotation></semantics></math></span> 个索引器头的权重。对于查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span>，给定其索引分数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>I</mi><mrow><mi>t</mi><mo separator="true">,</mo><mo>:</mo></mrow></msub></mrow><annotation encoding="application/x-tex">I_{t,:}</annotation></semantics></math></span>，我们使用 top-k 选择器有选择性地保留压缩键值条目的一个子集 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>t</mi><mtext>SprsComp</mtext></msubsup></mrow><annotation encoding="application/x-tex">C_t^{\text{SprsComp}}</annotation></semantics></math></span>，用于后续的核心注意力计算：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msubsup><mi>C</mi><mi>t</mi><mtext>SprsComp</mtext></msubsup><mo>=</mo><mrow><mo fence="true">{</mo><msubsup><mi>C</mi><mi>s</mi><mtext>Comp</mtext></msubsup><mo>∣</mo><msub><mi>I</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>s</mi></mrow></msub><mo>∈</mo><mtext>Top-k</mtext><mo stretchy="false">(</mo><msub><mi>I</mi><mrow><mi>t</mi><mo separator="true">,</mo><mo>:</mo></mrow></msub><mo stretchy="false">)</mo><mo fence="true">}</mo></mrow><mi mathvariant="normal">.</mi></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(17)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">C_t^{\text{SprsComp}} = \left\{C_s^{\text{Comp}} \mid I_{t,s} \in \text{Top-k}(I_{t,:})\right\}. \tag{17}</annotation></semantics></math></span><p><strong>共享键值多查询注意力（Shared Key-Value MQA）。</strong> 在选择稀疏键值条目之后，CSA 以多查询注意力（Multi-Query Attention, MQA）(Shazeer, 2019) 的方式执行核心注意力，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>t</mi><mtext>SprsComp</mtext></msubsup></mrow><annotation encoding="application/x-tex">C_t^{\text{SprsComp}}</annotation></semantics></math></span> 中的每个压缩键值条目同时充当注意力键和值。具体而言，对于查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span>，我们首先从压缩潜向量 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup></mrow><annotation encoding="application/x-tex">\mathbf{c}_t^Q</annotation></semantics></math></span> 生成注意力查询 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow></msub><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><msub><mi>n</mi><mi>h</mi></msub></mrow></msub><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\mathbf{q}_{t,1}; \mathbf{q}_{t,2}; \ldots; \mathbf{q}_{t,n_h}\}</annotation></semantics></math></span>：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mo stretchy="false">[</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow></msub><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><msub><mi>n</mi><mi>h</mi></msub></mrow></msub><mo stretchy="false">]</mo><mo>=</mo><msub><mi mathvariant="bold">q</mi><mi>t</mi></msub><mo>=</mo><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup><mo>⋅</mo><msup><mi>W</mi><mrow><mi>U</mi><mi>Q</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(18)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">[\mathbf{q}_{t,1}; \mathbf{q}_{t,2}; \ldots; \mathbf{q}_{t,n_h}] = \mathbf{q}_t = \mathbf{c}_t^Q \cdot W^{UQ}, \tag{18}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">n_h</annotation></semantics></math></span> 表示查询头的数量；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>U</mi><mi>Q</mi></mrow></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>d</mi><mi>c</mi></msub><mo>×</mo><mi>c</mi><msub><mi>n</mi><mi>h</mi></msub></mrow></msup></mrow><annotation encoding="application/x-tex">W^{UQ} \in \mathbb{R}^{d_c \times cn_h}</annotation></semantics></math></span> 是查询的上投影矩阵。注意潜向量 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup></mrow><annotation encoding="application/x-tex">\mathbf{c}_t^Q</annotation></semantics></math></span> 与用于索引器查询的潜向量是共享的。接下来，我们在 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\mathbf{q}_{t,i}\}</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>t</mi><mtext>SprsComp</mtext></msubsup></mrow><annotation encoding="application/x-tex">C_t^{\text{SprsComp}}</annotation></semantics></math></span> 上执行 MQA：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo>=</mo><mtext>CoreAttn</mtext><mrow><mo fence="true">(</mo><mtext>query</mtext><mo>=</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo separator="true">,</mo><mtext> key</mtext><mo>=</mo><msubsup><mi>C</mi><mi>t</mi><mtext>SprsComp</mtext></msubsup><mo separator="true">,</mo><mtext> value</mtext><mo>=</mo><msubsup><mi>C</mi><mi>t</mi><mtext>SprsComp</mtext></msubsup><mo fence="true">)</mo></mrow><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(19)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\mathbf{o}_{t,i} = \text{CoreAttn}\left(\text{query}=\mathbf{q}_{t,i},\ \text{key}=C_t^{\text{SprsComp}},\ \text{value}=C_t^{\text{SprsComp}}\right), \tag{19}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>c</mi></msup></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i} \in \mathbb{R}^c</annotation></semantics></math></span> 是第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 个 token 第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span> 个头的核心注意力输出；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>CoreAttn</mtext><mo stretchy="false">(</mo><mo>⋅</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{CoreAttn}(\cdot)</annotation></semantics></math></span> 表示核心注意力操作。</p>
<p><strong>分组输出投影（Grouped Output Projection）。</strong> 在 DeepSeek-V4 的配置中，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">cn_h</annotation></semantics></math></span> 的值相当大。因此，直接将核心注意力操作的输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow></msub><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><msub><mi>n</mi><mi>h</mi></msub></mrow></msub><mo stretchy="false">]</mo><mo>=</mo><msub><mi mathvariant="bold">o</mi><mi>t</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>c</mi><msub><mi>n</mi><mi>h</mi></msub></mrow></msup></mrow><annotation encoding="application/x-tex">[\mathbf{o}_{t,1}; \mathbf{o}_{t,2}; \ldots; \mathbf{o}_{t,n_h}] = \mathbf{o}_t \in \mathbb{R}^{cn_h}</annotation></semantics></math></span> 投影为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span> 维隐藏状态将产生巨大的计算负担。为缓解这一问题，我们设计了一种分组输出投影策略。具体而言，我们首先将 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">n_h</annotation></semantics></math></span> 个输出分为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi></mrow><annotation encoding="application/x-tex">g</annotation></semantics></math></span> 组，然后将每组输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow><mi>G</mi></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>c</mi><mfrac><msub><mi>n</mi><mi>h</mi></msub><mi>g</mi></mfrac></mrow></msup></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i}^G \in \mathbb{R}^{c\frac{n_h}{g}}</annotation></semantics></math></span> 投影为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>g</mi></msub></mrow><annotation encoding="application/x-tex">d_g</annotation></semantics></math></span> 维的中间输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><msub><mi>d</mi><mi>g</mi></msub></msup></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i}^{G&#x27;} \in \mathbb{R}^{d_g}</annotation></semantics></math></span>，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>g</mi></msub><mo>&lt;</mo><mi>c</mi><mfrac><msub><mi>n</mi><mi>h</mi></msub><mi>g</mi></mfrac></mrow><annotation encoding="application/x-tex">d_g &lt; c\frac{n_h}{g}</annotation></semantics></math></span>。最后，我们将中间输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo separator="true">;</mo><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>g</mi></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo stretchy="false">]</mo><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>d</mi><mi>g</mi></msub><mi>g</mi></mrow></msup></mrow><annotation encoding="application/x-tex">[\mathbf{o}_{t,1}^{G&#x27;}; \mathbf{o}_{t,2}^{G&#x27;}; \ldots; \mathbf{o}_{t,g}^{G&#x27;}] \in \mathbb{R}^{d_g g}</annotation></semantics></math></span> 投影为最终的注意力输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi mathvariant="bold">o</mi><mo>^</mo></mover><mi>t</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">\hat{\mathbf{o}}_t \in \mathbb{R}^d</annotation></semantics></math></span>。</p>
<h3 id="232-重度压缩注意力">2.3.2. 重度压缩注意力</h3>
<p>HCA 的核心架构如图 4 所示，它以更大的力度压缩键值缓存，但不采用稀疏注意力。</p>
<p><img alt="HCA 的核心架构。它执行更重的压缩，将 <span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mi>m</mi><mo mathvariant=\"normal\" lspace=\"0em\" rspace=\"0em\">′</mo></msup></mrow><annotation encoding=\"application/x-tex\">m&#x27;</annotation></semantics></math></span>（<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mo>≫</mo><mi>m</mi></mrow><annotation encoding=\"application/x-tex\">\\gg m</annotation></semantics></math></span>）个 token 的键值条目合并为一个。此外，我们还引入了少量滑动窗口键值条目，以增强局部细粒度依赖关系。" loading="lazy" src="/readings/deepseek-v4/images/fig4-hca.png"></p>
<p><strong>压缩键值条目（Compressed Key-Value Entries）。</strong> 总体而言，HCA 的压缩策略与 CSA 类似，但采用了更大的压缩率 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span>（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>≫</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">\gg m</annotation></semantics></math></span>），并且不执行重叠压缩。令 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>H</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>d</mi></mrow></msup></mrow><annotation encoding="application/x-tex">H \in \mathbb{R}^{n \times d}</annotation></semantics></math></span> 为输入隐藏状态序列，HCA 首先计算原始键值条目 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>C</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">C \in \mathbb{R}^{n \times c}</annotation></semantics></math></span> 及其对应的压缩权重 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Z</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">Z \in \mathbb{R}^{n \times c}</annotation></semantics></math></span>：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mi>C</mi><mo>=</mo><mi>H</mi><mo>⋅</mo><msup><mi>W</mi><mrow><mi>K</mi><mi>V</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(20)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">C = H \cdot W^{KV}, \tag{20}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mi>Z</mi><mo>=</mo><mi>H</mi><mo>⋅</mo><msup><mi>W</mi><mi>Z</mi></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(21)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">Z = H \cdot W^Z, \tag{21}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>K</mi><mi>V</mi></mrow></msup><mo separator="true">,</mo><msup><mi>W</mi><mi>Z</mi></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>d</mi><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">W^{KV}, W^Z \in \mathbb{R}^{d \times c}</annotation></semantics></math></span> 是可训练参数。接下来，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>C</mi></mrow><annotation encoding="application/x-tex">C</annotation></semantics></math></span> 中每 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span> 个键值条目将根据压缩权重和可学习的位置偏置 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">B \in \mathbb{R}^{m&#x27; \times c}</annotation></semantics></math></span> 被压缩为一个条目，生成 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mtext>Comp</mtext></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mfrac><mi>n</mi><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mfrac><mo>×</mo><mi>c</mi></mrow></msup></mrow><annotation encoding="application/x-tex">C^{\text{Comp}} \in \mathbb{R}^{\frac{n}{m&#x27;} \times c}</annotation></semantics></math></span>。每个压缩条目 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>C</mi><mi>i</mi><mtext>Comp</mtext></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>c</mi></msup></mrow><annotation encoding="application/x-tex">C_i^{\text{Comp}} \in \mathbb{R}^c</annotation></semantics></math></span> 的计算如下：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>S</mi><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>i</mi><mo>:</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo>−</mo><mn>1</mn></mrow></msub><mo>=</mo><msub><mtext>Softmax</mtext><mtext>row</mtext></msub><mo stretchy="false">(</mo><msub><mi>Z</mi><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>i</mi><mo>:</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo>−</mo><mn>1</mn></mrow></msub><mo>+</mo><mi>B</mi><mo stretchy="false">)</mo><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(22)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">S_{m&#x27;i:m&#x27;(i+1)-1} = \text{Softmax}_{\text{row}}(Z_{m&#x27;i:m&#x27;(i+1)-1} + B), \tag{22}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msubsup><mi>C</mi><mi>i</mi><mtext>Comp</mtext></msubsup><mo>=</mo><munderover><mo>∑</mo><mrow><mi>j</mi><mo>=</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>i</mi></mrow><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo>−</mo><mn>1</mn></mrow></munderover><msub><mi>S</mi><mi>j</mi></msub><mo>⊙</mo><msub><mi>C</mi><mi>j</mi></msub><mi mathvariant="normal">.</mi></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(23)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">C_i^{\text{Comp}} = \sum_{j=m&#x27;i}^{m&#x27;(i+1)-1} S_j \odot C_j. \tag{23}</annotation></semantics></math></span><p>通过该压缩操作，HCA 将序列长度压缩为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>1</mn><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mfrac></mrow><annotation encoding="application/x-tex">\frac{1}{m&#x27;}</annotation></semantics></math></span> 倍。</p>
<p><strong>共享键值多查询注意力与分组输出投影（Shared Key-Value MQA and Grouped Output Projection）。</strong> HCA 同样采用与 CSA 相同的共享键值 MQA 和分组输出投影策略。在键值压缩之后，对于查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span>，HCA 首先以低秩方式生成注意力查询 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow></msub><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><msub><mi>n</mi><mi>h</mi></msub></mrow></msub><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\mathbf{q}_{t,1}; \mathbf{q}_{t,2}; \ldots; \mathbf{q}_{t,n_h}\}</annotation></semantics></math></span>：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup><mo>=</mo><msub><mi mathvariant="bold">h</mi><mi>t</mi></msub><mo>⋅</mo><msup><mi>W</mi><mrow><mi>D</mi><mi>Q</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(24)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\mathbf{c}_t^Q = \mathbf{h}_t \cdot W^{DQ}, \tag{24}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><mo stretchy="false">[</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow></msub><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow></msub><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><msub><mi>n</mi><mi>h</mi></msub></mrow></msub><mo stretchy="false">]</mo><mo>=</mo><msub><mi mathvariant="bold">q</mi><mi>t</mi></msub><mo>=</mo><msubsup><mi mathvariant="bold">c</mi><mi>t</mi><mi>Q</mi></msubsup><mo>⋅</mo><msup><mi>W</mi><mrow><mi>U</mi><mi>Q</mi></mrow></msup><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(25)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">[\mathbf{q}_{t,1}; \mathbf{q}_{t,2}; \ldots; \mathbf{q}_{t,n_h}] = \mathbf{q}_t = \mathbf{c}_t^Q \cdot W^{UQ}, \tag{25}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="bold">h</mi><mi>t</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">\mathbf{h}_t \in \mathbb{R}^d</annotation></semantics></math></span> 是查询 token <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 的输入隐藏状态；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">n_h</annotation></semantics></math></span> 表示查询头的数量；<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>D</mi><mi>Q</mi></mrow></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>d</mi><mo>×</mo><msub><mi>d</mi><mi>c</mi></msub></mrow></msup></mrow><annotation encoding="application/x-tex">W^{DQ} \in \mathbb{R}^{d \times d_c}</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>W</mi><mrow><mi>U</mi><mi>Q</mi></mrow></msup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>d</mi><mi>c</mi></msub><mo>×</mo><mi>c</mi><msub><mi>n</mi><mi>h</mi></msub></mrow></msup></mrow><annotation encoding="application/x-tex">W^{UQ} \in \mathbb{R}^{d_c \times cn_h}</annotation></semantics></math></span> 分别是查询的下投影和上投影矩阵。接下来，在 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\mathbf{q}_{t,i}\}</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>C</mi><mtext>Comp</mtext></msup></mrow><annotation encoding="application/x-tex">C^{\text{Comp}}</annotation></semantics></math></span> 上执行 MQA：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo>=</mo><mtext>CoreAttn</mtext><mrow><mo fence="true">(</mo><mtext>query</mtext><mo>=</mo><msub><mi mathvariant="bold">q</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo separator="true">,</mo><mtext> key</mtext><mo>=</mo><msup><mi>C</mi><mtext>Comp</mtext></msup><mo separator="true">,</mo><mtext> value</mtext><mo>=</mo><msup><mi>C</mi><mtext>Comp</mtext></msup><mo fence="true">)</mo></mrow><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(26)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\mathbf{o}_{t,i} = \text{CoreAttn}\left(\text{query}=\mathbf{q}_{t,i},\ \text{key}=C^{\text{Comp}},\ \text{value}=C^{\text{Comp}}\right), \tag{26}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>c</mi></msup></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i} \in \mathbb{R}^c</annotation></semantics></math></span> 是第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 个 token 第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span> 个头的核心注意力输出。接下来，与 CSA 一样，HCA 将 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">n_h</annotation></semantics></math></span> 个输出分为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi></mrow><annotation encoding="application/x-tex">g</annotation></semantics></math></span> 组，对每组输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow><mi>G</mi></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>c</mi><mfrac><msub><mi>n</mi><mi>h</mi></msub><mi>g</mi></mfrac></mrow></msup></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i}^G \in \mathbb{R}^{c\frac{n_h}{g}}</annotation></semantics></math></span> 投影为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>g</mi></msub></mrow><annotation encoding="application/x-tex">d_g</annotation></semantics></math></span> 维的中间输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><msub><mi>d</mi><mi>g</mi></msub></msup></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i}^{G&#x27;} \in \mathbb{R}^{d_g}</annotation></semantics></math></span>，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>g</mi></msub><mo>&lt;</mo><mi>c</mi><mfrac><msub><mi>n</mi><mi>h</mi></msub><mi>g</mi></mfrac></mrow><annotation encoding="application/x-tex">d_g &lt; c\frac{n_h}{g}</annotation></semantics></math></span>。最后，HCA 将中间输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>1</mn></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo separator="true">;</mo><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mn>2</mn></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo separator="true">;</mo><mo>…</mo><mo separator="true">;</mo><msubsup><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>g</mi></mrow><msup><mi>G</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></msubsup><mo stretchy="false">]</mo><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><msub><mi>d</mi><mi>g</mi></msub><mi>g</mi></mrow></msup></mrow><annotation encoding="application/x-tex">[\mathbf{o}_{t,1}^{G&#x27;}; \mathbf{o}_{t,2}^{G&#x27;}; \ldots; \mathbf{o}_{t,g}^{G&#x27;}] \in \mathbb{R}^{d_g g}</annotation></semantics></math></span> 投影为最终的注意力输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mover accent="true"><mi mathvariant="bold">o</mi><mo>^</mo></mover><mi>t</mi></msub><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mi>d</mi></msup></mrow><annotation encoding="application/x-tex">\hat{\mathbf{o}}_t \in \mathbb{R}^d</annotation></semantics></math></span>。</p>
<h3 id="233-其他细节">2.3.3. 其他细节</h3>
<p>除了上述 CSA 和 HCA 的核心架构之外，我们的混合注意力还包含若干其他技术。为行文清晰，我们在前面的介绍中省略了这些额外技术，将在本小节中进行简要描述。此外，本小节仅聚焦于这些技术的核心思想，可能会为简洁起见省略一些细微细节。我们鼓励读者参考我们的开源实现以获取明确的细节。</p>
<p><strong>查询与键值条目归一化（Query and Key-Value Entry Normalization）。</strong> 对于 CSA 和 HCA，我们在核心注意力操作之前，对查询的每个头和压缩键值条目的唯一头执行额外的 RMSNorm 操作。这种归一化避免了注意力 logits 爆炸，并可能提升训练稳定性。</p>
<p><strong>部分旋转位置编码（Partial Rotary Positional Embedding）。</strong> 对于 CSA 和 HCA，我们对注意力查询、键值条目和核心注意力输出部分地应用旋转位置编码（Rotary Positional Embedding, RoPE）(Su et al., 2024)。具体而言，对于 CSA 和 HCA 中使用的每个查询向量和键值条目向量，我们将 RoPE 应用到其最后 64 个维度。由于键值条目同时充当注意力键和值，原始的核心注意力输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\mathbf{o}_{t,i}\}</annotation></semantics></math></span> 将携带绝对位置编码（因为它们来自键值条目的加权和）。作为一种对策，我们还对每个 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi mathvariant="bold">o</mi><mrow><mi>t</mi><mo separator="true">,</mo><mi>i</mi></mrow></msub></mrow><annotation encoding="application/x-tex">\mathbf{o}_{t,i}</annotation></semantics></math></span> 的最后 64 个维度应用位置 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>−</mo><mi>i</mi></mrow><annotation encoding="application/x-tex">-i</annotation></semantics></math></span> 的 RoPE。这样，核心注意力的输出也将携带相对位置编码——每个键值条目对核心注意力输出的贡献也将与查询和键值条目之间的距离相关。</p>
<p><strong>滑动窗口注意力的附加分支（Additional Branch of Sliding Window Attention）。</strong> 为了在 CSA 和 HCA 中严格保持因果性，每个查询只能关注其前方的压缩键值块。因此，查询无法访问自身所在压缩块内其他 token 的信息。同时，在语言建模中，近期的 token 通常与当前查询 token 有更强的相关性。出于这些原因，我们为 CSA 和 HCA 都引入了一个补充的滑动窗口注意力分支，以更好地建模局部依赖关系。具体而言，对于每个查询 token，我们额外生成 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个未压缩的键值条目，对应最近的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个 token。在 CSA 和 HCA 的核心注意力中，这些滑动窗口中的键值条目将与压缩键值条目一起使用。</p>
<p><strong>注意力汇聚（Attention Sink）。</strong> 在 CSA 和 HCA 的核心注意力中，我们采用了注意力汇聚（attention sink）技巧 (OpenAI, 2025; Xiao et al., 2024)。具体而言，我们设置一组可学习的汇聚 logits <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msubsup><mi>z</mi><mn>1</mn><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo separator="true">,</mo><msubsup><mi>z</mi><mn>2</mn><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo separator="true">,</mo><mo>…</mo><mo separator="true">,</mo><msubsup><mi>z</mi><msub><mi>n</mi><mi>h</mi></msub><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{z&#x27;_1, z&#x27;_2, \ldots, z&#x27;_{n_h}\}</annotation></semantics></math></span>。对于第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi></mrow><annotation encoding="application/x-tex">h</annotation></semantics></math></span> 个注意力头，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>Exp</mtext><mo stretchy="false">(</mo><msubsup><mi>z</mi><mi>h</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{Exp}(z&#x27;_h)</annotation></semantics></math></span> 将被加到注意力分数的分母中：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>s</mi><mrow><mi>h</mi><mo separator="true">,</mo><mi>i</mi><mo separator="true">,</mo><mi>j</mi></mrow></msub><mo>=</mo><mfrac><mrow><mtext>Exp</mtext><mo stretchy="false">(</mo><msub><mi>z</mi><mrow><mi>h</mi><mo separator="true">,</mo><mi>i</mi><mo separator="true">,</mo><mi>j</mi></mrow></msub><mo stretchy="false">)</mo></mrow><mrow><munder><mo>∑</mo><mi>k</mi></munder><mtext>Exp</mtext><mo stretchy="false">(</mo><msub><mi>z</mi><mrow><mi>h</mi><mo separator="true">,</mo><mi>i</mi><mo separator="true">,</mo><mi>k</mi></mrow></msub><mo stretchy="false">)</mo><mo>+</mo><mtext>Exp</mtext><mo stretchy="false">(</mo><msubsup><mi>z</mi><mi>h</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo stretchy="false">)</mo></mrow></mfrac><mo separator="true">,</mo></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(27)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">s_{h,i,j} = \frac{\text{Exp}(z_{h,i,j})}{\sum_k \text{Exp}(z_{h,i,k}) + \text{Exp}(z&#x27;_h)}, \tag{27}</annotation></semantics></math></span><p>其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>s</mi><mrow><mi>h</mi><mo separator="true">,</mo><mi>i</mi><mo separator="true">,</mo><mi>j</mi></mrow></msub><mo separator="true">,</mo><msub><mi>z</mi><mrow><mi>h</mi><mo separator="true">,</mo><mi>i</mi><mo separator="true">,</mo><mi>j</mi></mrow></msub><mo>∈</mo><mi mathvariant="double-struck">R</mi></mrow><annotation encoding="application/x-tex">s_{h,i,j}, z_{h,i,j} \in \mathbb{R}</annotation></semantics></math></span> 分别表示第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi></mrow><annotation encoding="application/x-tex">h</annotation></semantics></math></span> 个注意力头在第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span> 个查询 token 和第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi></mrow><annotation encoding="application/x-tex">j</annotation></semantics></math></span> 个前驱 token 或压缩块之间的注意力分数和注意力 logit。这一技巧允许每个查询头将其总注意力分数调整为不等于 1，甚至可以接近 0。</p>
<h3 id="234-效率讨论">2.3.4. 效率讨论</h3>
<p>由于采用了混合 CSA 和 HCA 以及低精度计算和存储，DeepSeek-V4 系列的注意力模块在注意力浮点运算量和键值缓存大小两方面均实现了卓越的效率，尤其是在长上下文场景下。首先，我们为键值条目采用混合存储格式：旋转位置编码（RoPE）维度使用 BF16 精度，其余维度使用 FP8 精度。这种混合表示方式使键值缓存大小相比纯 BF16 存储减少了近一半。其次，闪电索引器内的注意力计算以 FP4 精度执行，在极长上下文下加速注意力操作。第三，相较于 DeepSeek-V3.2，DeepSeek-V4 系列选择了更小的注意力 top-k 值，从而提升了短文本和中等长度文本的模型效率。最后，也是最重要的一点，压缩注意力和混合注意力技术大幅减少了键值缓存大小和计算浮点运算量。</p>
<p>以 BF16 GQA8 (Ainslie et al., 2023)（头维度 128）作为基线——这是大语言模型注意力的常见配置之一——DeepSeek-V4 系列的键值缓存大小在 1M 上下文场景下可以被大幅降低至该基线的约 2%。此外，即使与 DeepSeek-V3.2 (DeepSeek-AI, 2025)——已经是一个高效的基线——相比，DeepSeek-V4 系列在效率方面仍展现出显著优势。其推理浮点运算量和键值缓存大小的对比见图 1 的右半部分。</p>
<h2 id="24-muon-优化器">2.4. Muon 优化器</h2>
<p>我们在 DeepSeek-V4 系列的大部分模块中采用 Muon 优化器 (Jordan et al., 2024; Liu et al., 2025)，因为它具有更快的收敛速度和更高的训练稳定性。我们 Muon 优化的完整算法总结在算法 1 中。</p>
<blockquote>
<p><strong>算法 1</strong> 用于 DeepSeek-V4 的 Muon 优化器</p>
<p><strong>需要：</strong> 学习率 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>η</mi></mrow><annotation encoding="application/x-tex">\eta</annotation></semantics></math></span>，动量 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>μ</mi></mrow><annotation encoding="application/x-tex">\mu</annotation></semantics></math></span>，权重衰减 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>λ</mi></mrow><annotation encoding="application/x-tex">\lambda</annotation></semantics></math></span>，更新重缩放因子 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>γ</mi></mrow><annotation encoding="application/x-tex">\gamma</annotation></semantics></math></span></p>
<p>1: <strong>对于</strong> 每个训练步 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> <strong>执行</strong>
2:  <strong>对于</strong> 每个逻辑独立的权重 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>W</mi><mo>∈</mo><msup><mi mathvariant="double-struck">R</mi><mrow><mi>n</mi><mo>×</mo><mi>m</mi></mrow></msup></mrow><annotation encoding="application/x-tex">W \in \mathbb{R}^{n \times m}</annotation></semantics></math></span> <strong>执行</strong>
3:   <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>G</mi><mi>t</mi></msub><mo>=</mo><msub><mi mathvariant="normal">∇</mi><mi>W</mi></msub><msub><mi mathvariant="script">L</mi><mi>t</mi></msub><mo stretchy="false">(</mo><msub><mi>W</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">G_t = \nabla_W \mathcal{L}_t(W_{t-1})</annotation></semantics></math></span>           <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>▹</mo></mrow><annotation encoding="application/x-tex">\triangleright</annotation></semantics></math></span> 计算梯度
4:   <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>M</mi><mi>t</mi></msub><mo>=</mo><mi>μ</mi><msub><mi>M</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>+</mo><msub><mi>G</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">M_t = \mu M_{t-1} + G_t</annotation></semantics></math></span>          <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>▹</mo></mrow><annotation encoding="application/x-tex">\triangleright</annotation></semantics></math></span> 累积动量缓冲区
5:   <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>O</mi><mi>t</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo>=</mo><mtext>HybridNewtonSchulz</mtext><mo stretchy="false">(</mo><mi>μ</mi><msub><mi>M</mi><mi>t</mi></msub><mo>+</mo><msub><mi>G</mi><mi>t</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O&#x27;_t = \text{HybridNewtonSchulz}(\mu M_t + G_t)</annotation></semantics></math></span>   <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>▹</mo></mrow><annotation encoding="application/x-tex">\triangleright</annotation></semantics></math></span> Nesterov 技巧和混合 Newton-Schulz
6:   <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>O</mi><mi>t</mi></msub><mo>=</mo><msubsup><mi>O</mi><mi>t</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msubsup><mo>⋅</mo><msqrt><mrow><mi>max</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>n</mi><mo separator="true">,</mo><mi>m</mi><mo stretchy="false">)</mo></mrow></msqrt><mo>⋅</mo><mi>γ</mi></mrow><annotation encoding="application/x-tex">O_t = O&#x27;_t \cdot \sqrt{\max(n, m)} \cdot \gamma</annotation></semantics></math></span>       <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>▹</mo></mrow><annotation encoding="application/x-tex">\triangleright</annotation></semantics></math></span> 重缩放更新 RMS
7:   <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>W</mi><mi>t</mi></msub><mo>=</mo><msub><mi>W</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>⋅</mo><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><mi>η</mi><mi>λ</mi><mo stretchy="false">)</mo><mo>−</mo><mi>η</mi><msub><mi>O</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">W_t = W_{t-1} \cdot (1 - \eta\lambda) - \eta O_t</annotation></semantics></math></span>      <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>▹</mo></mrow><annotation encoding="application/x-tex">\triangleright</annotation></semantics></math></span> 执行权重衰减和更新
8:  <strong>结束</strong>
9: <strong>结束</strong></p>
</blockquote>
<p><strong>基本配置（Basic Configurations）。</strong> 我们对嵌入模块、预测头模块、<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 模块的静态偏置和门控因子，以及所有 RMSNorm 模块的权重保留使用 AdamW 优化器 (Loshchilov and Hutter, 2017)。所有其他模块均使用 Muon 更新。遵循 Liu et al. (2025)，我们还对 Muon 参数应用权重衰减，使用 Nesterov (Jordan et al., 2024; Nesterov, 1983) 技巧，并重缩放更新的均方根（Root Mean Square, RMS）以复用 AdamW 的超参数。与他们不同的是，我们使用混合 Newton-Schulz 迭代进行正交化。</p>
<p><strong>混合 Newton-Schulz 迭代（Hybrid Newton-Schulz Iterations）。</strong> 对于给定矩阵 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi></mrow><annotation encoding="application/x-tex">M</annotation></semantics></math></span>，令其奇异值分解（Singular Value Decomposition, SVD）为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi><mo>=</mo><mi>U</mi><mi mathvariant="normal">Σ</mi><msup><mi>V</mi><mi>T</mi></msup></mrow><annotation encoding="application/x-tex">M = U\Sigma V^T</annotation></semantics></math></span>。Newton-Schulz 迭代旨在近似地将 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi></mrow><annotation encoding="application/x-tex">M</annotation></semantics></math></span> 正交化为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>U</mi><msup><mi>V</mi><mi>T</mi></msup></mrow><annotation encoding="application/x-tex">UV^T</annotation></semantics></math></span>。通常，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>M</mi></mrow><annotation encoding="application/x-tex">M</annotation></semantics></math></span> 首先被归一化为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>M</mi><mn>0</mn></msub><mo>=</mo><mi>M</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">∣</mi><mi mathvariant="normal">∣</mi><mi>M</mi><mi mathvariant="normal">∣</mi><msub><mi mathvariant="normal">∣</mi><mi>F</mi></msub></mrow><annotation encoding="application/x-tex">M_0 = M/||M||_F</annotation></semantics></math></span> 以确保其最大奇异值不超过 1。然后，每次 Newton-Schulz 迭代执行如下操作：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi>M</mi><mi>k</mi></msub><mo>=</mo><mi>a</mi><msub><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>+</mo><mi>b</mi><mo stretchy="false">(</mo><msub><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></msub><msubsup><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow><mi>T</mi></msubsup><mo stretchy="false">)</mo><msub><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>+</mo><mi>c</mi><mo stretchy="false">(</mo><msub><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></msub><msubsup><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow><mi>T</mi></msubsup><msup><mo stretchy="false">)</mo><mn>2</mn></msup><msub><mi>M</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></msub><mi mathvariant="normal">.</mi></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(28)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">M_k = aM_{k-1} + b(M_{k-1}M_{k-1}^T)M_{k-1} + c(M_{k-1}M_{k-1}^T)^2 M_{k-1}. \tag{28}</annotation></semantics></math></span><p>我们的混合 Newton-Schulz 分两个不同阶段执行 10 次迭代。在前 8 步中，我们使用系数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>a</mi><mo separator="true">,</mo><mi>b</mi><mo separator="true">,</mo><mi>c</mi><mo stretchy="false">)</mo><mo>=</mo><mo stretchy="false">(</mo><mn>3.4445</mn><mo separator="true">,</mo><mo>−</mo><mn>4.7750</mn><mo separator="true">,</mo><mn>2.0315</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(a, b, c) = (3.4445, -4.7750, 2.0315)</annotation></semantics></math></span> 以驱动快速收敛，将奇异值拉近 1。在最后 2 步中，我们切换为系数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>a</mi><mo separator="true">,</mo><mi>b</mi><mo separator="true">,</mo><mi>c</mi><mo stretchy="false">)</mo><mo>=</mo><mo stretchy="false">(</mo><mn>2</mn><mo separator="true">,</mo><mo>−</mo><mn>1.5</mn><mo separator="true">,</mo><mn>0.5</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(a, b, c) = (2, -1.5, 0.5)</annotation></semantics></math></span>，将奇异值精确稳定在 1。</p>
<p><strong>避免注意力 logits 爆炸（Avoiding Exploding Attention Logits）。</strong> DeepSeek-V4 系列的注意力架构允许我们直接对注意力查询和键值条目应用 RMSNorm，从而有效防止注意力 logits 爆炸。因此，我们在 Muon 优化器中不使用 QK-Clip 技术 (Liu et al., 2025)。</p>
<h1 id="3-通用基础设施general-infrastructures">3. 通用基础设施（General Infrastructures）</h1>
<h2 id="31-专家并行中的细粒度通信-计算重叠">3.1. 专家并行中的细粒度通信-计算重叠</h2>
<p>混合专家模型（Mixture-of-Experts, MoE）可以通过专家并行（Expert Parallelism, EP）进行加速。然而，EP 需要复杂的跨节点通信，并对互联带宽和延迟提出了很高的要求。为了缓解 EP 中的通信瓶颈，并在更低的互联带宽需求下实现更高的端到端性能，我们提出了一种细粒度的 EP 方案，该方案将通信和计算融合到单一的流水线化内核（pipelined kernel）中，以实现通信-计算重叠（communication-computation overlapping）。</p>
<p><strong>通信延迟可以被隐藏。</strong> 我们 EP 方案的核心洞察在于，通信延迟可以被有效地隐藏在 MoE 层的计算之下。如图 5 所示，在 DeepSeek-V4 系列中，每个 MoE 层主要可以分解为四个阶段：两个通信密集阶段——分发（Dispatch）和合并（Combine），以及两个计算密集阶段——线性层-1（Linear-1）和线性层-2（Linear-2）。我们的性能分析表明，在单个 MoE 层内，通信的总时间少于计算的总时间。因此，在将通信和计算融合为统一的流水线之后，计算仍然是主要瓶颈，这意味着系统可以容忍较低的互联带宽而不降低端到端性能。</p>
<p><img alt="EP方案与相关工作的对比示意图" loading="lazy" src="/readings/deepseek-v4/images/fig5-ep-scheme.png"></p>
<p>图 5 | 我们的 EP 方案与相关工作的对比示意图。Comet (Zhang et al., 2025b) 将分发（Dispatch）与线性层-1（Linear-1）重叠，将线性层-2（Linear-2）与合并（Combine）重叠。我们的 EP 方案通过将专家拆分并调度为多个波次（wave），实现了更细粒度的重叠。理论加速比是在 DeepSeek-V4-Flash 架构配置下评估的。</p>
<p><strong>细粒度 EP 方案。</strong> 为了进一步降低互联带宽需求并放大重叠的收益，我们引入了一种更细粒度的专家分区（expert partitioning）方案。受到多项相关工作的启发 (Aimuyo et al., 2025; Zhang et al., 2025b)，我们将专家拆分并调度为多个波次（wave）。每个波次由一小部分专家组成。一旦某个波次内的所有专家完成了通信，计算即可立即开始，无需等待其他专家。在稳态下，当前波次的计算、下一个波次的词元传输以及已完成专家的结果发送都在并行进行，如图 5 所示。这形成了一个专家间的细粒度流水线，使计算和通信在整个波次中持续进行。基于波次的调度将端到端性能提升至接近纯计算时间，尤其在强化学习（Reinforcement Learning, RL）展开（rollout）等极端场景中效果显著，这类场景通常会遇到长尾小批次问题。</p>
<p><strong>性能与开源大内核。</strong> 我们在 NVIDIA GPU 和华为昇腾（HUAWEI Ascend）NPU 平台上验证了该细粒度 EP 方案。与强基线的非融合实现相比，在通用推理工作负载上实现了 1.50 ~ 1.73 倍的加速，在 RL 展开和高速智能体服务等延迟敏感场景中实现了高达 1.96 倍的加速。我们已将基于 CUDA 的大内核实现开源，命名为 <strong>MegaMoE</strong>，作为 DeepGEMM 的一个组件。</p>
<p><strong>观察与建议。</strong> 我们分享内核开发中的观察与经验教训，并向硬件厂商提出一些建议，希望有助于高效硬件设计和更好的软硬件协同设计（software-hardware co-design）：</p>
<ul>
<li><strong>计算-通信比（Computation-Communication Ratio）。</strong> 完全的通信-计算重叠取决于计算-通信比，而非仅仅取决于带宽本身。设峰值计算吞吐量为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>C</mi></mrow><annotation encoding="application/x-tex">C</annotation></semantics></math></span>，互联带宽为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi></mrow><annotation encoding="application/x-tex">B</annotation></semantics></math></span>，则当 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>C</mi><mi mathvariant="normal">/</mi><mi>B</mi><mo>⩽</mo><msub><mi>V</mi><mtext>comp</mtext></msub><mi mathvariant="normal">/</mi><msub><mi>V</mi><mtext>comm</mtext></msub></mrow><annotation encoding="application/x-tex">C/B \leqslant V_{\text{comp}}/V_{\text{comm}}</annotation></semantics></math></span> 时，通信可以被完全隐藏，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>V</mi><mtext>comp</mtext></msub></mrow><annotation encoding="application/x-tex">V_{\text{comp}}</annotation></semantics></math></span> 表示计算量，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>V</mi><mtext>comm</mtext></msub></mrow><annotation encoding="application/x-tex">V_{\text{comm}}</annotation></semantics></math></span> 表示通信量。对于 DeepSeek-V4-Pro，其中每个词元-专家对需要 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>6</mn><mi>h</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">6hd</annotation></semantics></math></span> 次浮点运算（FLOPs）（SwiGLU 的门控、上投影和下投影），但仅需 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>3</mn><mi>h</mi></mrow><annotation encoding="application/x-tex">3h</annotation></semantics></math></span> 字节的通信量（FP8 分发 + BF16 合并），这可以简化为：</li>
</ul>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mfrac><mi>C</mi><mi>B</mi></mfrac><mo>⩽</mo><mn>2</mn><mi>d</mi><mo>=</mo><mn>6144</mn><mtext> FLOPs/Byte.</mtext></mrow><annotation encoding="application/x-tex">\frac{C}{B} \leqslant 2d = 6144 \text{ FLOPs/Byte.}</annotation></semantics></math></span><p>也就是说，每 GBps 的互联带宽足以隐藏 6.1 TFLOP/s 的计算对应的通信。一旦带宽达到此阈值，它就不再是瓶颈，投入额外的芯片面积来进一步提升带宽将面临收益递减。我们建议未来的硬件设计以这样的平衡点为目标，而非无条件地扩展带宽。</p>
<ul>
<li>
<p><strong>功耗预算（Power Budget）。</strong> 极致的内核融合同时驱动计算、内存和网络达到高负载，使得功耗限流（power throttling）成为关键的性能限制因素。我们建议未来的硬件设计为这种完全并发的工作负载提供充足的功耗余量。</p>
</li>
<li>
<p><strong>通信原语（Communication Primitives）。</strong> 我们采用基于拉取（pull-based）的方式，即每个 GPU 主动从远程 GPU 读取数据，以避免细粒度推送（push）所带来的高通知延迟。未来具有更低延迟跨 GPU 信令（signaling）的硬件将使推送模式变得可行，并支持更自然的通信模式。</p>
</li>
<li>
<p><strong>激活函数（Activation Function）。</strong> 我们建议用一种低成本的逐元素（element-wise）激活函数替换 SwiGLU，该函数不涉及指数运算或除法运算。这将减轻 GEMM 后处理的负担，并在相同的参数预算下移除门控投影（gate projection），扩大中间维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span>，从而进一步放宽带宽需求。</p>
</li>
</ul>
<h2 id="32-使用-tilelang-进行灵活高效的内核开发">3.2. 使用 TileLang 进行灵活高效的内核开发</h2>
<p>在实践中，我们精心设计的模型架构会产生数百个细粒度的 Torch ATen 算子。我们采用 TileLang (Wang et al., 2026) 来开发一组融合内核（fused kernel），以最小的工作量替换其中的绝大多数，同时提供最优的性能。它还允许我们在验证期间快速原型化注意力变体等算子。这些内核在模型架构开发、大规模训练以及最终的推理服务生产部署中发挥着关键作用。作为一种领域特定语言（Domain-Specific Language, DSL），TileLang 在开发生产力和运行时效率之间取得了平衡，在支持同一代码库内的深度迭代优化的同时，实现了快速开发。此外，我们与 TileLang 社区紧密合作，以促进更敏捷、更高效、更稳定的内核开发工作流。</p>
<p><strong>通过宿主代码生成降低调用开销（Reducing Invocation Overhead with Host Codegen）。</strong> 随着加速器性能的持续增长，CPU 端的编排开销（orchestration overhead）变得日益突出。对于小型、高度优化的内核，固定的宿主端开销很容易限制利用率和吞吐量。这种开销的一个常见来源是宿主端逻辑（如运行时契约检查）通常用 Python 编写，以获得灵活性，但这会产生固定的逐次调用成本。</p>
<p>我们通过宿主代码生成（Host Codegen）来缓解这一开销，将大部分宿主端逻辑移入生成的宿主代码中。具体而言，我们首先协同生成设备内核和一个轻量级的宿主启动器，在中间表示（Intermediate Representation, IR）层嵌入必要的元数据——例如数据类型、秩/形状约束以及步长/布局假设——这些信息从语言前端解析得到。然后，该启动器被降级为基于 TVM-FFI (Chen et al., 2018) 框架的宿主源代码，其紧凑的调用约定和零拷贝张量互操作共同最小化了宿主端开销。在运行时，生成的宿主代码执行验证和参数编组（argument marshaling），将所有逐次调用的检查移出 Python 执行路径。我们的测量表明，CPU 端的验证开销从数十或数百微秒降至每次调用不到一微秒。</p>
<p><strong>SMT 求解器辅助的形式化整数分析（SMT-Solver-Assisted Formal Integer Analysis）。</strong> TileLang 内核涉及复杂的张量索引运算，需要强大的形式化整数分析能力。在编译过程中，如布局推导（layout inference）、内存冲突检测（memory hazard detection）和边界分析（bound analysis）等阶段，编译器必须验证整数表达式是否满足特定属性，以启用相应的优化。因此，更强的形式化分析能力可以解锁更高级和更复杂的优化机会。</p>
<p>为此，我们将 Z3 SMT 求解器 (De Moura and Bjorner, 2008) 集成到 TileLang 的代数系统中，为张量程序中的大多数整数表达式提供形式化分析能力。我们通过将 TileLang 的整数表达式转换为 Z3 的无量词非线性整数算术（quantifier-free non-linear integer arithmetic, QF_NIA）来在计算开销和形式化表达能力之间取得平衡。基于整数线性规划（Integer Linear Programming, ILP）求解器，QF_NIA 可以无缝地解决内核中常见的标准线性整数表达式。此外，其固有的非线性推理能力有效地应对了诸如可变张量形状上的向量化（vectorization）等高级挑战。在合理的资源限制下，Z3 提升了整体优化性能，同时将编译时间开销限制在仅几秒钟。其影响在多个编译阶段中都很显著，包括向量化、屏障插入（barrier insertion）和代码简化（code simplification）。</p>
<p><strong>数值精度与逐比特可复现性（Numerical Precision and Bitwise Reproducibility）。</strong> 在生产环境中，数值正确性和可复现性（reproducibility）与原始吞吐量同等重要。因此，我们默认优先保证精度：快速数学优化（fast-math optimizations）在编译器层面被禁用，影响精度的近似运算仅作为显式的、可选择加入的前端算子提供（例如 <code>T.__exp</code>、<code>T.__log</code> 和 <code>T.__sin</code>）。相反，当需要严格的 IEEE-754 语义时，TileLang 提供带有显式舍入模式的 IEEE 兼容内建函数（例如 <code>T.ieee_fsqrt</code>、<code>T.ieee_fdiv</code> 和 <code>T.ieee_add</code>），使开发者能够精确指定数值行为。</p>
<p>我们还以逐比特可复现性为目标，用于验证内核与手写 CUDA 基线的一致性。我们将 TileLang 的代数简化和降级规则与主流 CUDA 工具链（例如 NVCC）对齐，以避免引入非预期的比特级差异。布局注解（Layout annotations）（例如 <code>T.annotate_layout</code>）进一步允许用户锁定布局相关的降级决策，保持求值和累加顺序与参考 CUDA 实现一致，从而在需要时实现逐比特一致的输出。</p>
<p>我们的评估表明，这些面向精度和可复现性的设计选择并不牺牲性能：在保守的默认设置下，TileLang 内核仍然具有竞争力，同时提供了可选择性放宽数值约束以获取更高速度的旋钮。</p>
<h2 id="33-高性能批次不变和确定性内核库">3.3. 高性能批次不变和确定性内核库</h2>
<p>为了实现高效的训练和推理，我们开发了一套全面的高性能计算内核。除了基本功能和最大化硬件利用率之外，另一个关键设计目标是确保预训练、后训练和推理流水线之间的训练可复现性和逐比特对齐（bitwise alignment）。因此，我们实现了端到端的、逐比特批次不变（bitwise batch-invariant）和确定性（deterministic）的内核，且性能开销极小。这些内核有助于调试、稳定性分析以及一致的后训练行为。</p>
<p><strong>批次不变性（Batch Invariance）。</strong> 批次不变性确保任何给定词元的输出在比特级别上保持一致，无论其在批次中的位置如何。要实现批次不变性，主要挑战如下：</p>
<ul>
<li>
<p><strong>注意力（Attention）。</strong> 为实现批次不变性，我们不能使用 split-KV 方法 (Dao et al., 2023)，该方法将单个序列的注意力计算分布到多个流式多处理器（Stream Multiprocessors, SM）上以平衡负载。然而，放弃这一技术会导致严重的波量化（wave-quantization）问题，从而不利地影响 GPU 利用率。为解决这个问题，我们为批次不变的解码（batch-invariant decoding）开发了一种双内核（dual-kernel）策略。第一个内核在单个 SM 内计算整个序列的注意力输出，确保完全占用的波次具有高吞吐量。第二个内核用于最小化最后一个部分填充波次的延迟，从而缓解波量化问题，它为单个序列使用多个 SM。为了保证这两个内核的逐比特一致性，我们精心设计了第二个内核的计算路径，确保其累加顺序与第一个内核相同。此外，第二个内核利用线程块簇（thread-block cluster）内的分布式共享内存（distributed shared memory），实现跨 SM 的高速数据交换。这种双内核方法有效地将批次不变解码的开销控制在可忽略不计的水平。</p>
</li>
<li>
<p><strong>矩阵乘法（Matrix Multiplication）。</strong> 传统的 cuBLAS 库 (NVIDIA Corporation, 2024) 无法实现批次不变性。因此，我们端到端地替换为 DeepGEMM (Zhao et al., 2025)。此外，对于非常小的批次大小，传统实现通常采用 split-k (Osama et al., 2023) 技术来提升性能。不幸的是，split-k 技术无法保证批次不变性，这是 DeepSeek-V4 的一个关键特性。因此，我们在大多数场景中放弃了 split-k，但这可能会导致性能下降。为解决这个问题，我们引入了一系列优化，使我们的矩阵乘法实现在大多数主要场景中能够匹配甚至超越标准 split-k 的性能。</p>
</li>
</ul>
<p><strong>确定性（Determinism）。</strong> 确定性训练对于调试硬件或软件问题非常有价值。此外，当训练出现异常（如损失尖峰）时，确定性使研究人员能够更容易地定位数值原因并进一步改进模型设计。训练中的非确定性通常源于非确定性的累加顺序，这往往是由于使用了原子加法（atomic addition）指令。这个问题主要出现在反向传播过程中，具体包括以下几个部分：</p>
<ul>
<li>
<p><strong>注意力反向传播（Attention Backward）。</strong> 在稀疏注意力（sparse attention）反向传播的传统实现中，使用 <code>atomicAdd</code> 来累加 KV 词元的梯度。这由于浮点加法的非结合性（non-associativity）而引入了非确定性。为解决这个问题，我们为每个 SM 分配独立的累加缓冲区，然后对所有缓冲区进行全局确定性求和。</p>
</li>
<li>
<p><strong>MoE 反向传播（MoE Backward）。</strong> 当来自不同秩（rank）的多个 SM 同时向接收秩上的同一缓冲区写入数据时，协商写入位置同样会引入非确定性。为解决这个问题，我们设计了一种词元顺序预处理机制，在每个单独的秩内进行预处理，并结合跨多个秩的缓冲区隔离。该策略确保了专家并行的发送结果和 MoE 反向传播中累加顺序的确定性。</p>
</li>
<li>
<p><strong><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 中的矩阵乘法（Matrix Multiplication in <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC）。</strong> <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 涉及一个输出维度仅为 24 的矩阵乘法。对于非常小的批次大小，我们不得不使用 split-k (Osama et al., 2023) 算法，但其朴素实现会导致非确定性。为克服这一问题，我们分别输出每个 split 部分，然后在后续内核中执行确定性的归约操作，从而同时保持了性能和确定性。</p>
</li>
</ul>
<h2 id="34-fp4-量化感知训练">3.4. FP4 量化感知训练</h2>
<p>为了在部署时实现推理加速和内存节省，我们在后训练阶段引入了量化感知训练（Quantization-Aware Training, QAT）(Jacob et al., 2018)，使模型适应量化引入的精度下降。我们将 FP4（MXFP4）量化 (Rouhani et al., 2023) 应用于两个组件：(1) MoE 专家权重，它们是 GPU 显存占用的主要来源 (OpenAI, 2025)；(2) CSA 索引器中的查询-键（Query-Key, QK）路径，其中 QK 激活值被缓存、加载并完全以 FP4 进行乘法运算，从而在长上下文场景中加速注意力分数计算。此外，我们在 QAT 过程中将索引分数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>I</mi><mrow><mo>:</mo><mo separator="true">,</mo><mo>:</mo></mrow></msub></mrow><annotation encoding="application/x-tex">I_{:,:}</annotation></semantics></math></span> 从 FP32 量化为 BF16。该优化为 top-k 选择器实现了 2 倍加速，同时保持了 99.7% 的 KV 条目召回率。</p>
<p>对于 MoE 专家权重，遵循 QAT 的通常做法，由优化器维护的 FP32 主权重首先被量化到 FP4，然后反量化回 FP8 用于计算。值得注意的是，我们的 FP4 到 FP8 反量化是无损的。这是因为 FP8（E4M3）相比 FP4（E2M1）多了 2 个指数位，提供了更大的动态范围。因此，只要 FP4 量化块（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>×</mo><mn>32</mn></mrow><annotation encoding="application/x-tex">1 \times 32</annotation></semantics></math></span> 个瓦片）内子块的最大和最小缩放因子之比在每个 FP8 量化块（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>128</mn><mo>×</mo><mn>128</mn></mrow><annotation encoding="application/x-tex">128 \times 128</annotation></semantics></math></span> 个瓦片）内不超过某个阈值，细粒度的缩放信息就可以被 FP8 的扩展动态范围完全吸收。我们通过实验验证了当前权重满足此条件。这使得整个 QAT 流水线可以完全复用现有的 FP8 训练框架，无需任何修改。在反向传播中，梯度相对于前向传播中使用的相同 FP8 权重计算，并直接传播回 FP32 主权重，等效于通过量化操作应用直通估计器（Straight-Through Estimator, STE）。这也避免了重新量化转置权重的需要。</p>
<p>在 RL 训练的推理和展开阶段（不涉及反向传播），我们直接使用真实的 FP4 量化权重，而非模拟量化。这确保了采样期间的模型行为与在线部署完全一致，同时也减少了内核内存加载，实现了实际的加速并显著降低了内存消耗。CSA 索引器中的 QK 路径也做了类似处理。</p>
<h2 id="35-训练框架">3.5. 训练框架</h2>
<p>我们的训练框架建立在为 DeepSeek-V3 (DeepSeek-AI, 2024) 开发的可扩展且高效的基础设施之上。在训练 DeepSeek-V4 时，我们继承了这一稳健的基础，同时引入了若干关键创新以适应其新颖的架构组件——具体包括 Muon 优化器、<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 以及混合注意力机制——同时保持高训练效率和稳定性。</p>
<h3 id="351-muon-的高效实现">3.5.1. Muon 的高效实现</h3>
<p>Muon 优化器需要完整的梯度矩阵来计算参数更新，这在与零冗余优化器（Zero Redundancy Optimizer, ZeRO）(Rajbhandari et al., 2020) 结合时带来了挑战。传统的 ZeRO 针对 AdamW 等逐元素优化器设计，其中单个参数矩阵可以被分区并跨多个秩进行更新。为解决这一冲突，我们为 Muon 设计了一种混合的 ZeRO 桶分配策略。</p>
<p>对于稠密参数（dense parameters），我们限制 ZeRO 并行的最大规模，并采用背包算法（knapsack algorithm）将参数矩阵分配到这些秩上，确保每个秩管理大致均衡的负载。每个秩上的桶被填充到与最大桶大小对齐，以便于高效的 reduce-scatter 操作。在我们的设置中，这种填充通常产生不到 10% 的内存开销，其中每个秩管理不超过五个参数矩阵。当数据并行的总规模超过 ZeRO 的限制时，我们跨额外的数据并行组冗余地计算 Muon 更新，以换取更少的总桶内存。</p>
<p>对于 MoE 参数，我们独立优化每个专家。我们首先将 SwiGLU (Shazeer, 2020) 中所有专家在所有层上的下投影矩阵展平，然后是展平的上投影矩阵和门控矩阵。接着，我们填充展平后的向量，以确保可以在所有秩上均匀分布，且不会拆分任何逻辑上独立的矩阵。鉴于专家数量众多，我们不对 MoE 参数施加 ZeRO 并行的限制，且填充开销也可以忽略不计。</p>
<p>此外，在每个秩上，具有相同形状的连续参数会自动合并，以支持 Newton-Schulz 迭代的批量执行，从而更好地利用硬件。此外，我们观察到 Muon 中的 Newton-Schulz 迭代在使用 BF16 矩阵乘法计算时仍然保持稳定。利用这一点，我们以随机舍入（stochastic rounding）的方式，将 MoE 梯度量化到 BF16 精度后再跨数据并行秩同步，从而将通信量减半。为避免低精度加法器引入的累加误差，我们用两阶段方案替换了传统的基于树或环的 reduce-scatter 集合通信：首先，一个 all-to-all 操作在各秩之间交换本地梯度，然后每个秩以 FP32 进行本地求和。该设计保持了数值稳健性。</p>
<h3 id="352-hc-的高性价比和内存高效实现">3.5.2. <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 的高性价比和内存高效实现</h3>
<p><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 的引入增加了流水线阶段之间的激活内存消耗和通信量，相比传统的残差连接而言。为缓解这些开销，我们实施了若干优化策略。</p>
<p>首先，我们为训练和推理精心设计并实现了 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 的融合内核。其次，我们引入了一种选择性检查点中间张量的重计算策略。具体而言，我们重计算层间的大部分隐藏状态和所有归一化的层输入，同时避免重计算计算密集型操作。这在内存节省和计算开销之间取得了平衡。第三，我们调整了 DualPipe 1F1B 重叠方案，以适应增加的流水线通信并支持 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 中某些操作的并行执行。</p>
<p>总体而言，这些优化将 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 的挂钟时间开销限制在重叠 1F1B 流水线阶段的仅 6.7%。更多工程优化的细节可参见专门的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC 论文 (Xie et al., 2026)。</p>
<h3 id="353-长上下文注意力的上下文并行">3.5.3. 长上下文注意力的上下文并行</h3>
<p>传统的上下文并行（Context Parallelism, CP）沿序列维度进行分区，每个秩维护连续的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi></mrow><annotation encoding="application/x-tex">s</annotation></semantics></math></span> 个词元。这为我们的压缩注意力机制（即 CSA 和 HCA）带来了两个挑战。一方面，训练样本由多个序列打包而成，每个序列以 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>（或 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span>）为因子独立压缩，尾部不足 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个词元的部分被丢弃。因此，压缩后的 KV 长度通常小于 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mi>s</mi><mi>m</mi></mfrac></mrow><annotation encoding="application/x-tex">\frac{s}{m}</annotation></semantics></math></span> 且在各秩之间存在差异。另一方面，压缩需要 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个连续的 KV 条目，这可能跨越两个相邻 CP 秩的边界。</p>
<p>为应对这些挑战，我们设计了一种两阶段通信方案。在第一阶段，每个秩 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span> 将其最后 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个未压缩的 KV 条目发送给秩 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">i+1</annotation></semantics></math></span>。然后秩 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">i+1</annotation></semantics></math></span> 将这些接收到的条目与其本地的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi></mrow><annotation encoding="application/x-tex">s</annotation></semantics></math></span> 个未压缩 KV 条目一起进行压缩，产生固定长度为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mi>s</mi><mi>m</mi></mfrac><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\frac{s}{m}+1</annotation></semantics></math></span> 的压缩条目（其中可能存在一些填充条目）。在第二阶段，一个 all-gather 操作收集所有 CP 秩上的本地压缩 KV 条目。然后一个融合的选择-填充（select-and-pad）算子将其重组为完整的压缩 KV 条目集合，总长度为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>cp_size</mtext><mo>⋅</mo><mfrac><mi>s</mi><mi>m</mi></mfrac></mrow><annotation encoding="application/x-tex">\text{cp\_size} \cdot \frac{s}{m}</annotation></semantics></math></span>。任何填充条目都被放置在末尾。对于 HCA 和 CSA 中的索引器，每个查询词元可见的压缩 KV 条目范围可通过规则预计算。对于 CSA 中的稀疏注意力，top-<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math></span> 选择器显式指定每个查询可见的压缩 KV 条目索引。</p>
<h3 id="354-灵活激活检查点的扩展自动微分">3.5.4. 灵活激活检查点的扩展自动微分</h3>
<p>传统的激活检查点（activation checkpointing）实现在整个模块的粒度上操作，决定是保留还是在反向传播期间重计算其输出激活值。这种粗粒度的方式往往导致重计算成本和激活内存占用之间的次优权衡。一种替代方法是手动实现整个层的前向和反向逻辑，显式管理张量检查点状态。虽然这种方法能够实现细粒度控制，但失去了自动微分（automatic differentiation）框架的便利性，大幅增加了开发复杂度。</p>
<p>为了在不牺牲编程效率的情况下实现细粒度控制，我们实现了一种张量级别的激活检查点机制，并支持自动微分。通过这种机制，开发者只需实现前向传播并选择性地标注需要自动检查点和重计算的个别张量。我们的框架利用 TorchFX (Reed et al., 2022) 来追踪完整的计算图。对于每个标注的张量，它执行反向遍历以识别其重计算所需的最小子图。我们将这些最小子图定义为重计算图，并将它们插入到反向逻辑中对应的梯度计算之前。</p>
<p>与手动实现相比，该设计在训练期间不引入额外开销。该框架中的重计算通过直接释放标注张量的 GPU 内存并复用重计算张量的存储指针来实现，无需任何 GPU 内存拷贝。此外，由于图追踪以具体方式执行模型，我们可以追踪每个张量的底层存储指针，从而实现对共享存储的张量（例如 reshape 操作的输入和输出）的自动重计算去重。这使开发者在标注重计算时无需推理底层的内存细节。</p>
<h2 id="36-推理框架">3.6. 推理框架</h2>
<p>我们的推理框架主要继承自 DeepSeek-V3，在 KV 缓存管理方面有一些差异。</p>
<h3 id="361-kv-缓存结构与管理">3.6.1. KV 缓存结构与管理</h3>
<p>为了高效管理 DeepSeek-V4 中混合注意力机制产生的异构 KV 缓存（heterogeneous KV cache），我们设计了一种定制化的 KV 缓存布局。该布局如图 6 所示，我们将在下文详细阐述。</p>
<p><img alt="DeepSeek-V4 的 KV 缓存布局示意图" loading="lazy" src="/readings/deepseek-v4/images/fig6-kv-cache.png"></p>
<p>图 6 | DeepSeek-V4 的 KV 缓存布局示意图。KV 缓存被组织为两个主要组成部分：用于 CSA/HCA 的经典 KV 缓存，以及用于 SWA 和 CSA/HCA 中待压缩词元的状态缓存（state cache）。在状态缓存中，每个请求被分配一个固定大小的缓存块。在该块中，SWA 段存储最近 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个词元对应的 KV 条目，而 CSA/HCA 段存储尚未准备好进行压缩的未压缩尾部状态。在经典 KV 缓存中，我们为每个请求分配多个块。每个缓存块覆盖 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>lcm</mtext><mo stretchy="false">(</mo><mi>m</mi><mo separator="true">,</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{lcm}(m, m&#x27;)</annotation></semantics></math></span> 个原始词元，产生 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>k</mi><mn>1</mn></msub><mo>=</mo><mfrac><mrow><mtext>lcm</mtext><mo stretchy="false">(</mo><mi>m</mi><mo separator="true">,</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">)</mo></mrow><mi>m</mi></mfrac></mrow><annotation encoding="application/x-tex">k_1 = \frac{\text{lcm}(m,m&#x27;)}{m}</annotation></semantics></math></span> 个 CSA 压缩词元和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>k</mi><mn>2</mn></msub><mo>=</mo><mfrac><mrow><mtext>lcm</mtext><mo stretchy="false">(</mo><mi>m</mi><mo separator="true">,</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">)</mo></mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mfrac></mrow><annotation encoding="application/x-tex">k_2 = \frac{\text{lcm}(m,m&#x27;)}{m&#x27;}</annotation></semantics></math></span> 个 HCA 压缩词元。</p>
<p><strong>DeepSeek-V4 中的异构 KV 条目。</strong> DeepSeek-V4 系列的混合注意力机制引入了多种类型的 KV 条目，具有不同的键值（Key-Value, KV）缓存大小和更新规则。用于稀疏选择的闪电索引器（lightning indexer）将额外维度引入 KV 缓存，这些维度的嵌入大小与主注意力（primary attention）中的不同。CSA 和 HCA 采用的压缩技术分别将序列长度缩减了 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>1</mn><mi>m</mi></mfrac></mrow><annotation encoding="application/x-tex">\frac{1}{m}</annotation></semantics></math></span> 和 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>1</mn><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mfrac></mrow><annotation encoding="application/x-tex">\frac{1}{m&#x27;}</annotation></semantics></math></span> 倍，从而降低了整体 KV 缓存大小。因此，KV 缓存大小在不同层之间存在差异。此外，滑动窗口注意力（Sliding Window Attention, SWA）层也以不同的 KV 缓存大小运行，并具有独立的缓存命中和淘汰策略。在压缩分支中，每 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 个词元生成一个 KV 条目。当剩余词元数量不足以进行压缩时，所有待处理的词元及其相关的隐藏状态必须保留在缓冲区中，直到可以执行压缩操作。这些被缓冲的词元代表了一种由位置上下文决定的序列状态，也在 KV 缓存框架内进行管理。</p>
<p><strong>管理混合注意力 KV 缓存的挑战。</strong> 混合注意力机制违反了 PagedAttention 及其变体背后的基本假设。虽然近期的混合 KV 缓存管理算法（例如 Jenga (Zhang et al., 2025a)、Hymba (Dong et al., 2025)）针对通用混合注意力模型或特定结构，但有两个主要障碍阻止了在 PagedAttention 框架下统一管理所有层的 KV 缓存：</p>
<ul>
<li>多样化的缓存策略，例如滑动窗口注意力中使用的策略。</li>
<li>高性能注意力内核施加的约束，包括对齐要求。</li>
</ul>
<p>为了高效管理 DeepSeek-V4 的 KV 缓存，我们设计了相应的策略来克服这两个挑战。</p>
<p><strong>SWA 和未压缩尾部词元的状态缓存（State Cache）。</strong> 为应对第一个障碍，我们采用了一种替代的缓存管理机制。由于 SWA 旨在有限的 KV 缓存大小下提升性能，将其连同压缩分支中未压缩的尾部词元一起视为一个状态空间模型（state-space model）是合理的。相应的 KV 缓存因此可以被视为仅取决于当前位置的序列特定状态。相应地，我们预分配一个固定且有限大小的状态缓存池，并将其动态分配给每个序列。</p>
<p><strong>稀疏注意力内核协同设计（Sparse Attention Kernel Co-Design）。</strong> 关于第二个障碍，传统的高性能注意力内核通常假设每个块有固定数量 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi></mrow><annotation encoding="application/x-tex">B</annotation></semantics></math></span> 个词元以优化性能，对应于 CSA 中的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>⋅</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">B \cdot m</annotation></semantics></math></span> 个原始词元和 HCA 中的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>⋅</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">B \cdot m&#x27;</annotation></semantics></math></span> 个原始词元。通过采用高性能稀疏注意力内核，不同层可以在不降低性能的情况下容纳每个块可变数量的词元。实现这一点需要 KV 缓存布局和稀疏注意力内核的协同设计。例如，将块填充到与缓存行对齐可以提高性能。因此，对于压缩比为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 的 CSA 和压缩比为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span> 的 HCA，每个块的原始词元数量可以是 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>lcm</mtext><mo stretchy="false">(</mo><mi>m</mi><mo separator="true">,</mo><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{lcm}(m, m&#x27;)</annotation></semantics></math></span> 的任意倍数，即这两个压缩比的最小公倍数。</p>
<h3 id="362-磁盘-kv-缓存存储">3.6.2. 磁盘 KV 缓存存储</h3>
<p>在服务 DeepSeek-V4 时，我们利用磁盘 KV 缓存（on-disk KV cache）存储机制来消除共享前缀请求的重复预填充。对于 CSA/HCA 中的压缩 KV 条目和滑动窗口注意力（SWA）中的未压缩 KV 条目，我们设计了不同的存储方案。</p>
<p>对于 CSA 和 HCA，我们简单地将所有压缩 KV 条目存储到磁盘。当请求命中已存储的前缀时，我们读取并复用对应于该前缀的压缩 KV 条目，直到最后一个完整的压缩块。特别地，对于尾部不完整块中的前缀词元，我们仍然需要重计算它们以恢复未压缩的 KV 条目，因为 CSA 和 HCA 中的未压缩 KV 条目不被存储。</p>
<p>对于 SWA 的 KV 条目，由于它们未经压缩且存在于每一层中，其体积大约是压缩后 CSA 和 HCA KV 条目的 8 倍。为高效处理这些大量的 SWA KV 条目，我们提出并实现了三种不同的策略来管理磁盘上的 SWA KV 条目，每种策略在存储开销和计算冗余之间提供不同的权衡：</p>
<ul>
<li>
<p><strong>完全 SWA 缓存（Full SWA Caching）。</strong> 该策略存储所有词元的完整 SWA KV 条目，确保零计算冗余。在该策略下，命中前缀的 SWA KV 条目可以通过仅读取磁盘缓存中最后 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个词元对应的条目来重建。尽管计算零冗余，但该策略对于现代基于 SSD 的存储系统而言效率不高——因为存储的 SWA KV 缓存中只有一小部分会被访问，这导致了不平衡的写密集型访问模式。</p>
</li>
<li>
<p><strong>周期性检查点（Periodic Checkpointing）。</strong> 该策略每隔 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span> 个词元对最后 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个词元的 SWA KV 条目进行检查点保存，其中 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span> 是一个可调参数。对于命中的前缀，我们加载最近的检查点状态，然后重计算剩余的尾部词元。通过调节 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span>，该策略可以按需在存储和计算之间进行权衡。</p>
</li>
<li>
<p><strong>零 SWA 缓存（Zero SWA Caching）。</strong> 该策略不存储任何 SWA KV 条目。对于命中的前缀，我们需要执行更多的重计算来恢复 SWA KV 条目。具体而言，在每个注意力层中，每个词元的 SWA KV 条目仅依赖于前一层最近 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个词元的 SWA KV 条目。因此，利用已缓存的 CSA 和 HCA KV 条目，重计算最后 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub><mo>⋅</mo><mi>L</mi></mrow><annotation encoding="application/x-tex">n_{\text{win}} \cdot L</annotation></semantics></math></span> 个词元就足以恢复一个 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>L</mi></mrow><annotation encoding="application/x-tex">L</annotation></semantics></math></span> 层模型的最后 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 个 SWA KV 条目。</p>
</li>
</ul>
<p>根据具体的部署场景，我们选择最合适的策略来实现存储与计算之间的最佳权衡。</p>
<h1 id="4-预训练pre-training">4. 预训练（Pre-Training）</h1>
<h2 id="41-数据构建data-construction">4.1. 数据构建（Data Construction）</h2>
<p>在 DeepSeek-V3 预训练数据的基础上，我们致力于构建一个更加多样化、更高质量、且具有更长有效上下文的训练语料库。我们持续优化数据构建流程。对于网络来源的数据，我们采用过滤策略来去除批量自动生成和模板化的内容，从而降低模型坍缩（model collapse）的风险（Zhu et al., 2024）。数学和编程语料仍然是我们训练数据的核心组成部分，我们还通过在中期训练阶段引入智能体数据（agentic data）来进一步增强 DeepSeek-V4 系列的编程能力。对于多语言数据，我们为 DeepSeek-V4 构建了更大规模的语料库，提升了其对不同文化中长尾知识的捕获能力。对于 DeepSeek-V4，我们特别重视长文档数据的筛选，优先收录科学论文、技术报告以及其他具有独特学术价值的材料。综合以上所有内容，我们的预训练语料库包含超过 32T 个 token，涵盖数学内容、代码、网页、长文档以及其他高质量类别。</p>
<p>对于预训练数据，我们基本沿用了 DeepSeek-V3 的预处理策略。在分词（tokenization）方面，我们在 DeepSeek-V3 分词器的基础上引入了少量特殊 token 用于上下文构建，词表大小仍保持为 128K。我们同样继承了 DeepSeek-V3 的 token 拆分（token-splitting）（DeepSeek-AI, 2024）和中间填充（Fill-in-Middle, FIM）（DeepSeek-AI, 2024）策略。受 Ding et al.（2024）的启发，我们将来自不同来源的文档打包（pack）到合适的序列中，以尽量减少样本截断。与 DeepSeek-V3 不同的是，我们在预训练期间采用了样本级注意力掩码（sample-level attention masking）。</p>
<h2 id="42-预训练设置pre-training-setups">4.2. 预训练设置（Pre-Training Setups）</h2>
<h3 id="421-模型设置model-setups">4.2.1. 模型设置（Model Setups）</h3>
<p><strong>DeepSeek-V4-Flash。</strong> 我们将 Transformer 层数设为 43，隐藏维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span> 设为 4096。前两层使用纯滑动窗口注意力（pure sliding window attention）。后续各层以交错方式使用 CSA 和 HCA。对于 CSA，我们将压缩率 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 设为 4，索引器查询头数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow><annotation encoding="application/x-tex">n_h^I</annotation></semantics></math></span> 设为 64，索引器头维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>c</mi><mi>I</mi></msup></mrow><annotation encoding="application/x-tex">c^I</annotation></semantics></math></span> 设为 128，为稀疏注意力选择的 KV 条目数（即注意力 top-k）设为 512。对于 HCA，我们将压缩率 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span> 设为 128。对于 CSA 和 HCA，我们将查询头数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">n_h</annotation></semantics></math></span> 设为 64，头维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi></mrow><annotation encoding="application/x-tex">c</annotation></semantics></math></span> 设为 512，查询压缩维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>c</mi></msub></mrow><annotation encoding="application/x-tex">d_c</annotation></semantics></math></span> 设为 1024。输出投影分组数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi></mrow><annotation encoding="application/x-tex">g</annotation></semantics></math></span> 设为 8，每个中间注意力输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>g</mi></msub></mrow><annotation encoding="application/x-tex">d_g</annotation></semantics></math></span> 的维度设为 1024。滑动窗口注意力附加分支的窗口大小 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 设为 128。我们在所有 Transformer 块中均使用 MoE 层，但前 3 个 MoE 层使用哈希路由（Hash routing）策略。每个 MoE 层由 1 个共享专家（shared expert）和 256 个路由专家（routed expert）组成，每个专家的中间隐藏维度为 2048。在路由专家中，每个 token 激活 6 个专家。多 token 预测（multi-token prediction）深度设为 1。对于 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC，扩展因子 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{hc}}</annotation></semantics></math></span> 设为 4，Sinkhorn-Knopp 迭代次数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mi>max</mi><mo>⁡</mo></msub></mrow><annotation encoding="application/x-tex">t_{\max}</annotation></semantics></math></span> 设为 20。在此配置下，DeepSeek-V4-Flash 共有 284B 总参数，其中每个 token 激活 13B 参数。</p>
<p><strong>DeepSeek-V4-Pro。</strong> 我们将 Transformer 层数设为 61，隐藏维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span> 设为 7168。前两层使用 HCA。后续各层以交错方式使用 CSA 和 HCA。对于 CSA，我们将压缩率 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span> 设为 4，索引器查询头数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msubsup><mi>n</mi><mi>h</mi><mi>I</mi></msubsup></mrow><annotation encoding="application/x-tex">n_h^I</annotation></semantics></math></span> 设为 64，索引器头维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>c</mi><mi>I</mi></msup></mrow><annotation encoding="application/x-tex">c^I</annotation></semantics></math></span> 设为 128，为稀疏注意力选择的 KV 条目数（即注意力 top-k）设为 1024。对于 HCA，我们将压缩率 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>m</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup></mrow><annotation encoding="application/x-tex">m&#x27;</annotation></semantics></math></span> 设为 128。对于 CSA 和 HCA，我们将查询头数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mi>h</mi></msub></mrow><annotation encoding="application/x-tex">n_h</annotation></semantics></math></span> 设为 128，头维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi></mrow><annotation encoding="application/x-tex">c</annotation></semantics></math></span> 设为 512，查询压缩维度 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>c</mi></msub></mrow><annotation encoding="application/x-tex">d_c</annotation></semantics></math></span> 设为 1536。输出投影分组数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi></mrow><annotation encoding="application/x-tex">g</annotation></semantics></math></span> 设为 16，每个中间注意力输出 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mi>g</mi></msub></mrow><annotation encoding="application/x-tex">d_g</annotation></semantics></math></span> 的维度设为 1024。滑动窗口注意力附加分支的窗口大小 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>win</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{win}}</annotation></semantics></math></span> 设为 128。我们在所有 Transformer 块中均使用 MoE 层，但前 3 个 MoE 层使用哈希路由策略。每个 MoE 层由 1 个共享专家和 384 个路由专家组成，每个专家的中间隐藏维度为 3072。在路由专家中，每个 token 激活 6 个专家。多 token 预测深度设为 1。对于 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi></mrow><annotation encoding="application/x-tex">m</annotation></semantics></math></span>HC，扩展因子 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>n</mi><mtext>hc</mtext></msub></mrow><annotation encoding="application/x-tex">n_{\text{hc}}</annotation></semantics></math></span> 设为 4，Sinkhorn-Knopp 迭代次数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>t</mi><mi>max</mi><mo>⁡</mo></msub></mrow><annotation encoding="application/x-tex">t_{\max}</annotation></semantics></math></span> 设为 20。在此配置下，DeepSeek-V4-Pro 共有 1.6T 总参数，其中每个 token 激活 49B 参数。</p>
<h3 id="422-训练设置training-setups">4.2.2. 训练设置（Training Setups）</h3>
<p><strong>DeepSeek-V4-Flash。</strong> 我们对大部分参数使用 Muon 优化器（Jordan et al., 2024; Liu et al., 2025），但对嵌入模块（embedding module）、预测头模块（prediction head module）以及所有 RMSNorm 模块的权重使用 AdamW 优化器（Loshchilov and Hutter, 2017）。对于 AdamW，我们将其超参数设为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>β</mi><mn>1</mn></msub><mo>=</mo><mn>0.9</mn></mrow><annotation encoding="application/x-tex">\beta_1 = 0.9</annotation></semantics></math></span>，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>β</mi><mn>2</mn></msub><mo>=</mo><mn>0.95</mn></mrow><annotation encoding="application/x-tex">\beta_2 = 0.95</annotation></semantics></math></span>，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>ε</mi><mo>=</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>20</mn></mrow></msup></mrow><annotation encoding="application/x-tex">\varepsilon = 10^{-20}</annotation></semantics></math></span>，weight_decay <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>=</mo><mn>0.1</mn></mrow><annotation encoding="application/x-tex">= 0.1</annotation></semantics></math></span>。对于 Muon，我们将动量设为 0.95，权重衰减设为 0.1，并将每次更新矩阵的 RMS 缩放至 0.18 以重新利用 AdamW 的学习率。我们在 32T 个 token 上训练 DeepSeek-V4-Flash，并与 DeepSeek-V3 一样，采用批量大小调度策略（batch size scheduling strategy），将批量大小（以 token 计）从较小值逐步增加到 75.5M，然后在大部分训练过程中保持在 75.5M。学习率在前 2000 步线性预热，在大部分训练过程中维持在 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2.7</mn><mo>×</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>4</mn></mrow></msup></mrow><annotation encoding="application/x-tex">2.7 \times 10^{-4}</annotation></semantics></math></span>。在训练接近尾声时，我们按照余弦调度（cosine schedule）将学习率衰减至 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2.7</mn><mo>×</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>5</mn></mrow></msup></mrow><annotation encoding="application/x-tex">2.7 \times 10^{-5}</annotation></semantics></math></span>。训练从序列长度 4K 开始，逐步扩展至 16K、64K 和 1M。关于稀疏注意力的设置，我们首先使用密集注意力对模型进行预热训练，处理前 1T 个 token，然后在序列长度 64K 时引入稀疏注意力，并在后续训练中保持稀疏注意力。在引入注意力稀疏性时，我们先设置一个短阶段来预热 CSA 中的闪电索引器（lightning indexer），然后在大部分训练过程中使用稀疏注意力训练模型。对于无辅助损失的负载均衡（auxiliary-loss-free load balancing），我们将偏置更新速度（bias update speed）设为 0.001。对于平衡损失（balance loss），我们将其损失权重设为 0.0001，以避免单个序列内出现极端不平衡。MTP 损失权重在大部分训练过程中设为 0.3，并在学习率衰减开始时设为 0.1。</p>
<p><strong>DeepSeek-V4-Pro。</strong> 除了超参数的具体数值外，DeepSeek-V4-Pro 的训练设置与 DeepSeek-V4-Flash 基本一致。我们对大部分参数使用 Muon 优化器，但对嵌入模块、预测头模块以及所有 RMSNorm 模块的权重使用 AdamW 优化器。AdamW 和 Muon 的超参数与 DeepSeek-V4-Flash 相同。我们在 33T 个 token 上训练 DeepSeek-V4-Pro，同样采用批量大小调度策略，最大批量大小为 94.4M 个 token。学习率调度策略与 DeepSeek-V4-Flash 基本相同，但峰值学习率设为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2.0</mn><mo>×</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>4</mn></mrow></msup></mrow><annotation encoding="application/x-tex">2.0 \times 10^{-4}</annotation></semantics></math></span>，末期学习率设为 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2.0</mn><mo>×</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>5</mn></mrow></msup></mrow><annotation encoding="application/x-tex">2.0 \times 10^{-5}</annotation></semantics></math></span>。训练同样从序列长度 4K 开始，逐步扩展至 16K、64K 和 1M。与 DeepSeek-V4-Flash 相比，DeepSeek-V4-Pro 在密集注意力阶段的持续时间更长，而引入稀疏注意力的策略与 DeepSeek-V4-Flash 相同，均采用两阶段训练方法。对于无辅助损失的负载均衡，我们将偏置更新速度设为 0.001。对于平衡损失，我们将其损失权重设为 0.0001，以避免单个序列内出现极端不平衡。MTP 损失权重在大部分训练过程中设为 0.3，并在学习率衰减开始时设为 0.1。</p>
<h3 id="423-缓解训练不稳定性mitigating-training-instability">4.2.3. 缓解训练不稳定性（Mitigating Training Instability）</h3>
<p>训练万亿参数的 MoE 模型面临巨大的稳定性挑战，DeepSeek-V4 系列也不例外。我们在训练过程中遇到了显著的不稳定性问题。虽然简单的回滚可以暂时恢复训练状态，但它们不足以作为长期解决方案，因为它们无法防止损失尖峰（loss spike）的再次出现。从经验上看，我们发现尖峰的发生始终与 MoE 层中的异常值（outlier）密切相关，而路由机制本身似乎会加剧这些异常值的产生。因此，我们从两个维度着手解决这一问题：打破路由引发的恶性循环，以及直接抑制异常值。幸运的是，我们发现了两种实用技术，能够有效地维持训练稳定性。尽管对其底层机制的全面理论理解仍是一个开放问题，我们仍然公开分享这些技术，以促进社区的进一步探索。</p>
<p><strong>预见性路由（Anticipatory Routing）。</strong> 我们发现，将主干网络（backbone network）和路由网络（routing network）的同步更新解耦，能够显著提高训练稳定性。具体而言，在第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 步，我们使用当前网络参数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>θ</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">\theta_t</annotation></semantics></math></span> 进行特征计算，但路由索引（routing indices）是基于历史网络参数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>θ</mi><mrow><mi>t</mi><mo>−</mo><mi mathvariant="normal">Δ</mi><mi>t</mi></mrow></msub></mrow><annotation encoding="application/x-tex">\theta_{t-\Delta t}</annotation></semantics></math></span> 计算并应用的。在实践中，为了避免两次加载模型参数的开销，我们提前获取第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 步的数据，在第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mo>−</mo><mi mathvariant="normal">Δ</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">t - \Delta t</annotation></semantics></math></span> 步&quot;预见性地&quot;计算并缓存路由索引，供第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi></mrow><annotation encoding="application/x-tex">t</annotation></semantics></math></span> 步后续使用，这就是我们将此方法命名为预见性路由的原因。我们还在基础设施层面对此进行了大量优化。首先，由于预计算路由索引仅需一次前向传播，我们精心编排了流水线执行和与专家并行（Expert Parallelism, EP）通信的计算重叠，成功地将预见性路由的额外墙钟时间开销限制在约 20%。其次，我们引入了一种自动检测机制，当损失尖峰发生时触发短暂回滚并仅在此时激活预见性路由；在该模式下运行一段时间后，系统恢复到标准训练。最终，这种动态应用使我们能够在几乎不增加整体训练开销的情况下避免损失尖峰，且不会影响模型性能。</p>
<blockquote>
<p><strong>译注：预见性路由为什么能稳定训练？</strong></p>
<p>这是一个极其精妙的工程发现。在 MoE 训练中存在一个恶性循环：某些 token 产生异常大的激活值 → 这些 token 被路由到特定专家（因为路由决策基于当前激活）→ 该专家的梯度被这些异常值主导 → 专家权重更新后产生更大的异常值 → 更极端的路由分配&hellip; 通过使用历史参数 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>θ</mi><mrow><mi>t</mi><mo>−</mo><mi mathvariant="normal">Δ</mi><mi>t</mi></mrow></msub></mrow><annotation encoding="application/x-tex">\theta_{t-\Delta t}</annotation></semantics></math></span> 计算路由（而非当前步的 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>θ</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex">\theta_t</annotation></semantics></math></span>），当前步的异常激活不会立即影响路由决策——这打断了上述恶性循环。</p>
<p>有趣的是，作者坦言&quot;对其底层机制的全面理论理解仍是一个开放问题&quot;。这在工业级大模型训练中非常典型：先用工程直觉找到有效的解法，理论解释留待后续。</p>
</blockquote>
<p><strong>SwiGLU 截断（SwiGLU Clamping）。</strong> 在此前的文献中（Bello et al., 2017; Riviere et al., 2024），截断（clamping）已被明确用于约束数值范围，从而增强训练稳定性。在我们的实际训练中，我们通过实验发现，对 SwiGLU 应用截断（OpenAI, 2025）能够有效消除异常值，并在很大程度上帮助稳定训练过程，同时不会损害模型性能。在 DeepSeek-V4-Flash 和 DeepSeek-V4-Pro 的整个训练过程中，我们将 SwiGLU 的线性分量截断在 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mo>−</mo><mn>10</mn><mo separator="true">,</mo><mn>10</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[-10, 10]</annotation></semantics></math></span> 的范围内，同时将门控分量的上界截断为 10。</p>
<h2 id="43-评估evaluations">4.3. 评估（Evaluations）</h2>
<h3 id="431-评估基准evaluation-benchmarks">4.3.1. 评估基准（Evaluation Benchmarks）</h3>
<p>对于基座模型（base model）的评估，我们考虑了四个关键维度的基准测试：世界知识、语言理解与推理、编程与数学、以及长上下文处理。</p>
<p><strong>世界知识</strong>基准测试包括 AGIEval（Zhong et al., 2023）、C-Eval（Huang et al., 2023）、CMMLU（Li et al., 2023）、MMLU（Hendrycks et al., 2020）、MMLU-Redux（Gema et al., 2024）、MMLU-Pro（Wang et al., 2024b）、MMMLU（OpenAI, 2024a）、MultiLoKo（Hupkes and Bogoychev, 2025）、Simple-QA verified（Haas et al., 2025）、SuperGPQA（Du et al., 2025）、FACTS Parametric（Cheng et al., 2025）和 TriviaQA（Joshi et al., 2017）。</p>
<p><strong>语言理解与推理</strong>基准测试包括 BigBench Hard (BBH)（Suzgun et al., 2022）、DROP（Dua et al., 2019）、HellaSwag（Zellers et al., 2019）、CLUEWSC（Xu et al., 2020）和 WinoGrande（Sakaguchi et al., 2019）。</p>
<p><strong>编程与数学</strong>基准测试包括 BigCodeBench（Zhuo et al., 2025）、HumanEval（Chen et al., 2021）、GSM8K（Cobbe et al., 2021）、MATH（Hendrycks et al., 2021）、MGSM（Shi et al., 2023）和 CMath（Wei et al., 2023）。</p>
<p><strong>长上下文</strong>基准测试包括 LongBench-V2（Bai et al., 2025b）。</p>
<h3 id="432-评估结果evaluation-results">4.3.2. 评估结果（Evaluation Results）</h3>
<p>表 1 | DeepSeek-V3.2-Base、DeepSeek-V4-Flash-Base 和 DeepSeek-V4-Pro-Base 的详细对比。所有模型均在我们的统一内部框架下使用相同的评估设置进行评估。差距不超过 0.3 的分数被视为处于同一水平。每行中的最高分以<strong>粗体</strong>标注，第二高分以<u>下划线</u>标注。</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th>基准测试 (指标)</th>
          <th># Shots</th>
          <th>DeepSeek-V3.2 Base</th>
          <th>DeepSeek-V4-Flash Base</th>
          <th>DeepSeek-V4-Pro Base</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td></td>
          <td>架构</td>
          <td>-</td>
          <td>MoE</td>
          <td>MoE</td>
          <td>MoE</td>
      </tr>
      <tr>
          <td></td>
          <td>激活参数量</td>
          <td>-</td>
          <td>37B</td>
          <td>13B</td>
          <td>49B</td>
      </tr>
      <tr>
          <td></td>
          <td>总参数量</td>
          <td>-</td>
          <td>671B</td>
          <td>284B</td>
          <td>1.6T</td>
      </tr>
      <tr>
          <td>世界知识</td>
          <td>AGIEval (EM)</td>
          <td>0-shot</td>
          <td>80.1</td>
          <td>82.6</td>
          <td><strong>83.1</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MMLU (EM)</td>
          <td>5-shot</td>
          <td>87.8</td>
          <td>88.7</td>
          <td><strong>90.1</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MMLU-Redux (EM)</td>
          <td>5-shot</td>
          <td>87.5</td>
          <td>89.4</td>
          <td><strong>90.8</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MMLU-Pro (EM)</td>
          <td>5-shot</td>
          <td>65.5</td>
          <td>68.3</td>
          <td><strong>73.5</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MMMLU (EM)</td>
          <td>5-shot</td>
          <td>87.9</td>
          <td>88.8</td>
          <td><strong>90.3</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>C-Eval (EM)</td>
          <td>5-shot</td>
          <td>90.4</td>
          <td>92.1</td>
          <td><strong>93.1</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>CMMLU (EM)</td>
          <td>5-shot</td>
          <td>88.9</td>
          <td>90.4</td>
          <td><strong>90.8</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MultiLoKo (EM)</td>
          <td>5-shot</td>
          <td>38.7</td>
          <td>42.2</td>
          <td><strong>51.1</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>Simple-QA verified (EM)</td>
          <td>25-shot</td>
          <td>28.3</td>
          <td>30.1</td>
          <td><strong>55.2</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>SuperGPQA (EM)</td>
          <td>5-shot</td>
          <td>45.0</td>
          <td>46.5</td>
          <td><strong>53.9</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>FACTS Parametric (EM)</td>
          <td>25-shot</td>
          <td>27.1</td>
          <td>33.9</td>
          <td><strong>62.6</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>TriviaQA (EM)</td>
          <td>5-shot</td>
          <td>83.3</td>
          <td>82.8</td>
          <td><strong>85.6</strong></td>
      </tr>
      <tr>
          <td>语言理解与推理</td>
          <td>BBH (EM)</td>
          <td>3-shot</td>
          <td><strong>87.6</strong></td>
          <td>86.9</td>
          <td>87.5</td>
      </tr>
      <tr>
          <td></td>
          <td>DROP (F1)</td>
          <td>1-shot</td>
          <td>88.2</td>
          <td>88.6</td>
          <td><strong>88.7</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>HellaSwag (EM)</td>
          <td>0-shot</td>
          <td>86.4</td>
          <td>85.7</td>
          <td><strong>88.0</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>WinoGrande (EM)</td>
          <td>0-shot</td>
          <td>78.9</td>
          <td>79.5</td>
          <td><strong>81.5</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>CLUEWSC (EM)</td>
          <td>5-shot</td>
          <td>83.5</td>
          <td>82.2</td>
          <td><strong>85.2</strong></td>
      </tr>
      <tr>
          <td>编程与数学</td>
          <td>BigCodeBench (Pass@1)</td>
          <td>3-shot</td>
          <td><strong>63.9</strong></td>
          <td>56.8</td>
          <td>59.2</td>
      </tr>
      <tr>
          <td></td>
          <td>HumanEval (Pass@1)</td>
          <td>0-shot</td>
          <td>62.8</td>
          <td>69.5</td>
          <td><strong>76.8</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>GSM8K (EM)</td>
          <td>8-shot</td>
          <td>91.1</td>
          <td>90.8</td>
          <td><strong>92.6</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MATH (EM)</td>
          <td>4-shot</td>
          <td>60.5</td>
          <td>57.4</td>
          <td><strong>64.5</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>MGSM (EM)</td>
          <td>8-shot</td>
          <td>81.3</td>
          <td><strong>85.7</strong></td>
          <td>84.4</td>
      </tr>
      <tr>
          <td></td>
          <td>CMath (EM)</td>
          <td>3-shot</td>
          <td>92.6</td>
          <td><strong>93.6</strong></td>
          <td>90.9</td>
      </tr>
      <tr>
          <td>长上下文</td>
          <td>LongBench-V2 (EM)</td>
          <td>1-shot</td>
          <td>40.2</td>
          <td>44.7</td>
          <td><strong>51.5</strong></td>
      </tr>
  </tbody>
</table>
<p>在表 1 中，我们提供了 DeepSeek-V3.2、DeepSeek-V4-Flash 和 DeepSeek-V4-Pro 三个基座模型的详细对比，所有模型均在统一的内部框架下以严格一致的设置进行评估。</p>
<p>将 DeepSeek-V4-Flash-Base 与 DeepSeek-V3.2-Base 进行比较，揭示了一个令人信服的效率故事。尽管使用了显著更少的激活参数和总参数，DeepSeek-V4-Flash-Base 在大量基准测试中的表现仍然优于 DeepSeek-V3.2-Base。这一优势在世界知识任务和具有挑战性的长上下文场景中尤为明显。这些结果表明，DeepSeek-V4-Flash-Base 中的架构改进、数据质量的提升以及训练优化，在更紧凑的参数预算下实现了更优越的性能，有效超越了更大的 DeepSeek-V3.2-Base，在大多数评估中取得了领先。</p>
<p>此外，DeepSeek-V4-Pro-Base 展现了进一步的、决定性的能力飞跃，在 DeepSeek-V3.2-Base 和 DeepSeek-V4-Flash-Base 之上确立了近乎全面的领先地位。在几乎所有类别上都取得了提升，DeepSeek-V4-Pro-Base 在最具挑战性的基准测试上刷新了 DeepSeek 基座模型的性能新高。在知识密集型评估中，它带来了显著的提升，同时也大幅推进了长上下文理解能力。在大多数推理和代码基准测试中，DeepSeek-V4-Pro-Base 同样超越了之前的两个模型。这一全面的提升确认了 DeepSeek-V4-Pro-Base 作为 DeepSeek 系列中最强基座模型的地位，在知识、推理、编程和长上下文能力的全方位上超越了其前代模型。</p>
<h1 id="5-后训练post-training">5. 后训练（Post-Training）</h1>
<h2 id="51-后训练流程post-training-pipeline">5.1. 后训练流程（Post-Training Pipeline）</h2>
<p>在预训练之后，我们进行了后训练阶段，以产出 DeepSeek-V4 系列的最终模型。虽然训练流程在很大程度上沿用了 DeepSeek-V3.2 的方案，但做出了一项关键的方法论替换：混合强化学习（Reinforcement Learning, RL）阶段被完全替换为在线策略蒸馏（On-Policy Distillation, OPD）。</p>
<h3 id="511-专家训练specialist-training">5.1.1. 专家训练（Specialist Training）</h3>
<p>领域专家的开发通过调整 DeepSeek-V3.2 的训练流程来完成。具体而言，每个模型通过初始的微调（fine-tuning）阶段和随后的强化学习（RL）阶段进行顺序优化，这些阶段由领域特定的提示词和奖励信号驱动。在 RL 阶段，我们实现了组相对策略优化（Group Relative Policy Optimization, GRPO）算法，其超参数与我们此前的研究保持高度一致（DeepSeek-AI, 2025; DeepSeek-AI, 2025）。</p>
<p><strong>推理力度（Reasoning Efforts）。</strong> 众所周知，模型在推理任务上的表现从根本上取决于所消耗的计算力度。因此，我们在不同的 RL 配置下训练了不同的专家模型，以促进开发针对不同推理能力的模型。如表 2 所示，DeepSeek-V4-Pro 和 DeepSeek-V4-Flash 均支持三种特定的推理力度模式。对于每种模式，我们在 RL 训练期间施加不同的长度惩罚和上下文窗口，从而导致不同的输出 token 长度。为了整合这些不同的推理模式，我们使用由 <code>&lt;think&gt;</code> 和 <code>&lt;/think&gt;</code> token 标记的专用响应格式。此外，对于&quot;Think Max&quot;模式，我们在系统提示词的开头添加一条特定指令来引导模型的推理过程，如表 3 所示。</p>
<p>表 2 | 三种推理模式的对比</p>
<table>
  <thead>
      <tr>
          <th>推理模式</th>
          <th>特征</th>
          <th>典型使用场景</th>
          <th>响应格式</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Non-think（无思考）</td>
          <td>快速、直觉式响应，基于习惯或简单规则</td>
          <td>日常常规任务、紧急反应、低风险决策</td>
          <td><code>&lt;/think&gt;</code> 摘要</td>
      </tr>
      <tr>
          <td>Think High（高强度思考）</td>
          <td>有意识的逻辑分析，较慢但更准确</td>
          <td>复杂问题求解、规划、中风险决策</td>
          <td><code>&lt;think&gt;</code> 思考 token <code>&lt;/think&gt;</code> 摘要</td>
      </tr>
      <tr>
          <td>Think Max（最大强度思考）</td>
          <td>将推理能力推向极致。慢但强大</td>
          <td>探索模型推理能力的边界</td>
          <td>1. 在开头添加特殊系统提示词。2. <code>&lt;think&gt;</code> 思考 token <code>&lt;/think&gt;</code> 摘要</td>
      </tr>
  </tbody>
</table>
<p>表 3 | 注入&quot;Think Max&quot;模式系统提示词的指令</p>
<table>
  <thead>
      <tr>
          <th>注入的指令</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Reasoning Effort: Absolute maximum with no shortcuts permitted. You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios. Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.</td>
      </tr>
  </tbody>
</table>
<p><strong>生成式奖励模型（Generative Reward Model）。</strong> 通常，易于验证的任务可以使用简单的基于规则的验证器或测试用例来有效优化。相比之下，难以验证的任务传统上依赖于基于人类反馈的强化学习（Reinforcement Learning from Human Feedback, RLHF），这需要大量的人工标注来训练一个标量奖励模型。在 DeepSeek-V4 系列的后训练阶段，我们摒弃了这些传统的基于标量的奖励模型。取而代之的是，为了处理难以验证的任务，我们策划了基于评分标准的 RL 数据，并采用生成式奖励模型（Generative Reward Model, GRM）来评估策略轨迹。至关重要的是，我们将 RL 优化直接应用于 GRM 本身。在这一范式中，actor 网络原生地充当 GRM，从而实现了模型评判（judging）能力与标准生成能力的联合优化。通过统一这些角色，模型内部的推理能力被自然地融入到其评判过程中，产生了高度稳健的评分。此外，这种方法仅需少量多样化的人工标注即可实现优越的性能，因为模型利用自身的逻辑在复杂任务中进行泛化。</p>
<p><strong>工具调用模式与特殊 Token（Tool-Call Schema and Special Token）。</strong> 与我们之前的版本一致，我们使用专用的 <code>&lt;think&gt;&lt;/think&gt;</code> 标签来界定推理路径。在 DeepSeek-V4 系列中，我们引入了一种新的工具调用模式（tool-call schema），该模式使用一个特殊的 <code>&quot;|DSML|&quot;</code> token 并采用基于 XML 的格式来进行工具调用，如表 4 所示。我们的实验表明，XML 格式有效地降低了转义失败的概率，减少了工具调用错误，为模型-工具交互提供了更稳健的接口。</p>
<p>表 4 | DeepSeek-V4 系列的工具调用模式</p>
<table>
  <thead>
      <tr>
          <th>Tool Call Schema</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>## Tools</code></td>
      </tr>
      <tr>
          <td><code>You have access to a set of tools to help answer the user's question. You can invoke tools by writing a &quot;&lt;|DSML|tool_calls&gt;&quot; block like the following:</code></td>
      </tr>
      <tr>
          <td><code>&lt;|DSML|tool_calls&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|DSML|invoke name=&quot;$TOOL_NAME&quot;&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|DSML|parameter name=&quot;$PARAMETER_NAME&quot; string=&quot;true|false&quot;&gt;$PARAMETER_VALUE&lt;/|DSML|parameter&gt;</code></td>
      </tr>
      <tr>
          <td><code>...</code></td>
      </tr>
      <tr>
          <td><code>&lt;/|DSML|invoke&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|DSML|invoke name=&quot;$TOOL_NAME2&quot;&gt;</code></td>
      </tr>
      <tr>
          <td><code>...</code></td>
      </tr>
      <tr>
          <td><code>&lt;/|DSML|invoke&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;/|DSML|tool_calls&gt;</code></td>
      </tr>
      <tr>
          <td></td>
      </tr>
      <tr>
          <td><code>String parameters should be specified as is and set 'string=&quot;true&quot;'. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set 'string=&quot;false&quot;'.</code></td>
      </tr>
      <tr>
          <td></td>
      </tr>
      <tr>
          <td><code>If thinking_mode is enabled (triggered by &lt;think&gt;), you MUST output your complete reasoning inside &lt;think&gt;...&lt;/think&gt; BEFORE any tool calls or final response.</code></td>
      </tr>
      <tr>
          <td></td>
      </tr>
      <tr>
          <td><code>Otherwise, output directly after &lt;/think&gt; with tool calls or final response.</code></td>
      </tr>
      <tr>
          <td></td>
      </tr>
      <tr>
          <td><code>### Available Tool Schemas</code></td>
      </tr>
      <tr>
          <td><code>{Tool Definition...}</code></td>
      </tr>
      <tr>
          <td></td>
      </tr>
      <tr>
          <td><code>You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.</code></td>
      </tr>
  </tbody>
</table>
<p><strong>交错思考（Interleaved Thinking）。</strong> DeepSeek-V3.2 引入了一种上下文管理策略，该策略在跨工具结果轮次中保留推理痕迹，但在新用户消息到达时将其丢弃。虽然这种方法有效，但在复杂的智能体工作流中仍然会造成不必要的 token 浪费——每当新的用户轮次到来，都会清除所有累积的推理内容，迫使模型从头重建其问题求解状态。利用 DeepSeek-V4 系列扩展的 1M token 上下文窗口，我们进一步优化了这一机制，以最大化交错思考在智能体环境中的效果：</p>
<ul>
<li><strong>工具调用场景（Tool-Calling Scenarios）。</strong> 如图 7(a) 所示，所有推理内容在整个对话过程中完整保留。与 DeepSeek-V3.2 在每次新用户轮次时丢弃思考痕迹不同，DeepSeek-V4 系列在所有轮次中保留完整的推理历史，包括跨用户消息边界。这使模型能够在长期智能体任务中维持一条连贯的、累积的思维链。</li>
<li><strong>通用对话场景（General Conversational Scenarios）。</strong> 如图 7(b) 所示，原有策略保持不变：当新用户消息到达时，前几轮的推理内容被丢弃，在持久化推理痕迹收益有限的场景中保持上下文简洁。</li>
</ul>
<p><img alt="DeepSeek-V4 系列的思考管理机制" loading="lazy" src="/readings/deepseek-v4/images/fig7-thinking.png"></p>
<p><img alt="DeepSeek-V4 系列带工具的思考管理机制" loading="lazy" src="/readings/deepseek-v4/images/fig7b-thinking-tools.png"></p>
<p>与 DeepSeek-V3.2 一样，通过用户消息模拟工具交互的智能体框架（如 Terminus）可能不会触发工具调用的上下文路径，因此可能无法从增强的推理持久化中获益。我们继续建议此类架构使用 non-think 模型。</p>
<p><strong>快速指令（Quick Instruction）。</strong> 在聊天机器人场景中，许多辅助任务（如判断是否触发网络搜索、意图识别等）必须在生成响应之前执行。传统上，这些任务由一个独立的小模型处理，需要冗余的预填充（prefilling），因为它无法复用已有的 KV 缓存。为了克服这一局限性，我们引入了快速指令（Quick Instruction）。我们将一组专用的特殊 token 直接附加到输入序列中，每个 token 对应一个特定的辅助任务。通过直接复用已经计算好的 KV 缓存，这一机制完全避免了冗余预填充，并允许某些任务（如生成搜索查询和判断权威性及领域）并行执行。因此，这种方法显著降低了用户感知的首 token 生成延迟（Time-to-First-Token, TTFT），并消除了维护和迭代额外小模型的工程开销。支持的快速指令 token 汇总在表 5 中。</p>
<p>表 5 | 用于辅助任务的快速指令特殊 token</p>
<table>
  <thead>
      <tr>
          <th>特殊 Token</th>
          <th>描述</th>
          <th>格式</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>&lt;|action|&gt;</code></td>
          <td>判断用户提示是否需要网络搜索或可以直接回答</td>
          <td><code>...&lt;|User|&gt;{prompt}&lt;|Assistant|&gt;&lt;think&gt;&lt;|action|&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|title|&gt;</code></td>
          <td>在第一次助手响应后生成简洁的对话标题</td>
          <td><code>...&lt;|Assistant|&gt;{response}&lt;|end_of_sentence|&gt;&lt;|title|&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|query|&gt;</code></td>
          <td>为用户提示生成搜索查询</td>
          <td><code>...&lt;|User|&gt;{prompt}&lt;|query|&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|authority|&gt;</code></td>
          <td>分类用户提示对来源权威性的需求</td>
          <td><code>...&lt;|User|&gt;{prompt}&lt;|authority|&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|domain|&gt;</code></td>
          <td>识别用户提示所属的领域</td>
          <td><code>...&lt;|User|&gt;{prompt}&lt;|domain|&gt;</code></td>
      </tr>
      <tr>
          <td><code>&lt;|extracted_url|&gt;</code> <code>&lt;|read_url|&gt;</code></td>
          <td>判断用户提示中的每个 URL 是否应被获取和阅读</td>
          <td><code>...&lt;|User|&gt;{prompt}&lt;|extracted_url|&gt;{url}&lt;|read_url|&gt;</code></td>
      </tr>
  </tbody>
</table>
<h3 id="512-在线策略蒸馏on-policy-distillation">5.1.2. 在线策略蒸馏（On-Policy Distillation）</h3>
<p>在通过专门的微调和强化学习训练出多个领域专家后，我们采用多教师在线策略蒸馏（On-Policy Distillation, OPD）作为将专家能力合并到最终模型的主要技术。OPD 已成为一种有效的后训练范式，能够将领域专家的知识和能力高效地迁移到一个统一模型中。这是通过让学生模型在自身生成的轨迹上学习教师模型的输出分布来实现的。形式化地，给定一组 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span> 个专家模型 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">{</mo><msub><mi>π</mi><msub><mi>E</mi><mn>1</mn></msub></msub><mo separator="true">,</mo><msub><mi>π</mi><msub><mi>E</mi><mn>2</mn></msub></msub><mo separator="true">,</mo><mo>…</mo><mo separator="true">,</mo><msub><mi>π</mi><msub><mi>E</mi><mi>N</mi></msub></msub><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">\{\pi_{E_1}, \pi_{E_2}, \ldots, \pi_{E_N}\}</annotation></semantics></math></span>，OPD 目标函数定义为：</p>
<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable width="100%"><mtr><mtd width="50%"></mtd><mtd><mrow><msub><mi mathvariant="script">L</mi><mtext>OPD</mtext></msub><mo stretchy="false">(</mo><mi>θ</mi><mo stretchy="false">)</mo><mo>=</mo><munderover><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>N</mi></munderover><msub><mi>w</mi><mi>i</mi></msub><mo>⋅</mo><msub><mtext>D</mtext><mtext>KL</mtext></msub><mrow><mo fence="true">(</mo><msub><mi>π</mi><mi>θ</mi></msub><mi mathvariant="normal">∥</mi><msub><mi>π</mi><msub><mi>E</mi><mi>i</mi></msub></msub><mo fence="true">)</mo></mrow><mi mathvariant="normal">.</mi></mrow></mtd><mtd width="50%"></mtd><mtd><mtext>(29)</mtext></mtd></mtr></mtable><annotation encoding="application/x-tex">\mathcal{L}_{\text{OPD}}(\theta) = \sum_{i=1}^{N} w_i \cdot \text{D}_{\text{KL}}\left(\pi_\theta \| \pi_{E_i}\right). \tag{29}</annotation></semantics></math></span><blockquote>
<p><strong>译注：OPD 公式的直觉理解</strong></p>
<p>这个公式说的是：学生模型 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>π</mi><mi>θ</mi></msub></mrow><annotation encoding="application/x-tex">\pi_\theta</annotation></semantics></math></span> 要同时&quot;模仿&quot;<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math></span> 个教师专家。<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>D</mtext><mtext>KL</mtext></msub><mo stretchy="false">(</mo><msub><mi>π</mi><mi>θ</mi></msub><mi mathvariant="normal">∥</mi><msub><mi>π</mi><msub><mi>E</mi><mi>i</mi></msub></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{D}_{\text{KL}}(\pi_\theta \| \pi_{E_i})</annotation></semantics></math></span> 衡量的是学生与第 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span> 个教师之间的输出分布差异。注意这是<strong>反向 KL 散度</strong>（student ‖ teacher，而非 teacher ‖ student）——这意味着学生倾向于&quot;覆盖&quot;教师认为可能的所有输出，而不是只集中在教师最偏好的输出上。</p>
<p>关键洞察是&quot;on-policy&quot;——训练数据从学生自己的策略 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>π</mi><mi>θ</mi></msub></mrow><annotation encoding="application/x-tex">\pi_\theta</annotation></semantics></math></span> 中采样。这意味着当学生面对一道数学题时，它会自己先生成一个答案，然后比较：数学教师会怎么回答？编程教师呢？通过加权 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>w</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">w_i</annotation></semantics></math></span>，学生自动从最相关的教师那里学习。这比传统的混合 RL（试图用一个 loss 同时优化所有目标）优雅得多——因为不同目标间的梯度干扰被消除了。</p>
<p>为什么用全词表蒸馏而非 token 级 KL 估计？因为 token 级估计相当于用单个样本估计一个高维分布的 KL 散度——方差极大。全词表蒸馏保留了完整的 128K 词表上的概率分布，梯度估计更稳定，但代价是需要物化完整的 logit——这就是为什么 5.2.2 节需要那么多工程优化。</p>
</blockquote>
<p>在此公式中，<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>w</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">w_i</annotation></semantics></math></span> 表示每个专家被赋予的权重，通常由该专家的相对重要性决定。计算反向 KL 损失 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mtext>D</mtext><mtext>KL</mtext></msub><mo stretchy="false">(</mo><msub><mi>π</mi><mi>θ</mi></msub><mi mathvariant="normal">∥</mi><msub><mi>π</mi><msub><mi>E</mi><mi>i</mi></msub></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\text{D}_{\text{KL}}(\pi_\theta \| \pi_{E_i})</annotation></semantics></math></span> 需要从学生策略 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>π</mi><mi>θ</mi></msub></mrow><annotation encoding="application/x-tex">\pi_\theta</annotation></semantics></math></span> 中采样训练轨迹，以维持在线策略学习。其底层逻辑确保统一策略 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>π</mi><mi>θ</mi></msub></mrow><annotation encoding="application/x-tex">\pi_\theta</annotation></semantics></math></span> 选择性地从与当前任务上下文相关的专业专家中学习（例如，对于数学推理任务从数学专家学习，对于编程任务从编程专家学习）。通过这一机制，来自物理上不同的专家权重的知识在 logit 层面被整合到统一的参数空间中，实际上避免了传统权重合并或混合 RL 技术中常见的性能退化。在此阶段，超过十个涵盖各领域的教师模型被用于蒸馏到单个学生模型中。</p>
<p>在处理上述 OPD 目标时，先前的工作通常将全词表 KL 损失简化为每个 token 位置上的 token 级 KL 估计，并复用 RL 框架，将 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>sg</mtext><mrow><mo fence="true">[</mo><mi>log</mi><mo>⁡</mo><mfrac><mrow><msub><mi>π</mi><msub><mi>E</mi><mi>i</mi></msub></msub><mo stretchy="false">(</mo><msub><mi>y</mi><mi>t</mi></msub><mi mathvariant="normal">∣</mi><mi>x</mi><mo separator="true">,</mo><msub><mi>y</mi><mrow><mo>&lt;</mo><mi>t</mi></mrow></msub><mo stretchy="false">)</mo></mrow><mrow><msub><mi>π</mi><mi>θ</mi></msub><mo stretchy="false">(</mo><msub><mi>y</mi><mi>t</mi></msub><mi mathvariant="normal">∣</mi><mi>x</mi><mo separator="true">,</mo><msub><mi>y</mi><mrow><mo>&lt;</mo><mi>t</mi></mrow></msub><mo stretchy="false">)</mo></mrow></mfrac><mo fence="true">]</mo></mrow></mrow><annotation encoding="application/x-tex">\text{sg}\left[\log \frac{\pi_{E_i}(y_t|x, y_{&lt;t})}{\pi_\theta(y_t|x, y_{&lt;t})}\right]</annotation></semantics></math></span>（sg 表示停止梯度操作）替换为策略损失计算中的每 token 优势估计。虽然这种方法在资源使用上是高效的，但它会导致梯度估计的高方差，并经常造成训练不稳定。因此，我们在 OPD 中采用了全词表 logit 蒸馏（full-vocabulary logit distillation）。在计算反向 KL 损失时保留完整的 logit 分布，能够产生更稳定的梯度估计，并确保对教师知识的忠实蒸馏。在下一小节中，我们将描述使全词表 OPD 在大规模场景下可行的工程实践。</p>
<h2 id="52-rl-与-opd-基础设施rl-and-opd-infrastructures">5.2. RL 与 OPD 基础设施（RL and OPD Infrastructures）</h2>
<p>我们的后训练基础设施构建于为 DeepSeek-V3.2 开发的可扩展框架之上。具体而言，我们整合了第 3.5 节描述的分布式训练栈以及先前引入的用于高效自回归采样的 rollout 引擎。在此基础上，我们在本工作中引入了以下主要增强。这些设计实现了超长上下文 RL 和涉及十多个不同教师模型的 OPD 合并任务的高效执行，从而大幅加速了模型发布的迭代周期。</p>
<h3 id="521-fp4-量化集成fp4-quantization-integration">5.2.1. FP4 量化集成（FP4 Quantization Integration）</h3>
<p>我们应用 FP4 (MXFP4) 量化来加速所有 rollout 和所有仅推理的前向传播，包括教师模型和参考模型的前向传播，从而降低内存占用和采样延迟。如第 3.4 节所述，我们在 rollout 和推理阶段直接使用原生 FP4 权重。对于训练步骤，FP4 量化通过无损的 FP4 转 FP8 反量化步骤进行模拟，从而无缝复用现有的 FP8 混合精度框架和 FP32 主权重，不需要对反向传播流程做任何修改。</p>
<h3 id="522-高效的全词表-opd-教师调度efficient-teacher-scheduling-for-full-vocabulary-opd">5.2.2. 高效的全词表 OPD 教师调度（Efficient Teacher Scheduling for Full-Vocabulary OPD）</h3>
<p>我们的框架支持全词表在线策略蒸馏（OPD），可有效处理数量不受限制的教师模型，每个教师模型可能包含数万亿参数。为实现这一点，所有教师权重被卸载到集中式分布式存储中，在教师前向传播期间按需加载，并通过类似 ZeRO 的参数分片来缓解 I/O 和 DRAM 压力。此外，对于词表大小 <span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">∣</mi><mi>V</mi><mi mathvariant="normal">∣</mi><mo>&gt;</mo><mn>100</mn><mtext>k</mtext></mrow><annotation encoding="application/x-tex">|V| &gt; 100\text{k}</annotation></semantics></math></span> 的情况，即使存储到磁盘，直接物化所有教师的 logit 也是不可行的。我们通过在前向传播期间仅缓存最后一层教师隐藏状态到集中式缓冲区来解决这一问题。在训练时，这些缓存的状态被检索并通过相应的预测头模块，在线重建完整的 logit。这种设计在完全避免显式 logit 物化带来的内存负担的同时，仅引入可忽略不计的重计算开销。为了降低教师预测头的 GPU 内存占用，我们在数据调度期间按教师索引对训练样本进行排序。这一安排确保每个不同的教师头在每个 mini-batch 中仅加载一次，且任何时刻设备内存中最多只有一个教师头。所有参数和隐藏状态的加载/卸载操作在后台异步进行，不会阻塞关键路径上的计算。最后，教师和学生 logit 之间的精确 KL 散度使用专门的 TileLang 内核计算，这加速了计算并减少了动态内存分配。</p>
<h3 id="523-可抢占和容错的-rollout-服务preemptible-and-fault-tolerant-rollout-service">5.2.3. 可抢占和容错的 Rollout 服务（Preemptible and Fault-Tolerant Rollout Service）</h3>
<p>为了在保障高优先级任务快速获取硬件资源的同时最大化 GPU 资源利用率，我们的 GPU 集群采用了集群级可抢占任务调度器（preemptive task scheduler），任何正在运行的任务都可能在任何时刻被抢占。此外，硬件故障在大规模 GPU 集群中十分常见。为此，我们为 RL/OPD rollout 实现了一个可抢占且容错的 LLM 生成服务。</p>
<p>具体而言，我们为每个生成请求实现了 token 粒度的预写日志（Write-Ahead Log, WAL）。每当为一个请求生成新 token 时，我们立即将其追加到该请求的 WAL 中。在抢占期间，我们暂停推理引擎并将未完成请求的 KV 缓存保存下来。恢复时，我们使用持久化的 WAL 和保存的 KV 缓存继续解码。即使发生致命的硬件错误，我们也可以使用 WAL 中持久化的 token 重新运行预填充阶段来重建 KV 缓存。</p>
<p>重要的是，从头重新生成未完成请求在数学上是不正确的，因为这会引入长度偏差（length bias）。由于较短的响应更容易幸免于中断，从头重新生成会使模型倾向于产生更短的序列。如果推理栈是批次不变（batch-invariant）且确定性的，这一正确性问题也可以通过为采样器使用一致的伪随机数种子来重新生成解决。然而，这种方法仍然会产生重新运行解码阶段的额外成本，使其远不如我们的 token 粒度 WAL 方法高效。</p>
<h3 id="524-面向百万-token-上下文的-rl-框架扩展scaling-rl-framework-for-million-token-context">5.2.4. 面向百万 Token 上下文的 RL 框架扩展（Scaling RL Framework for Million-Token Context）</h3>
<p>我们针对百万 token 序列上的高效 RL 和 OPD 引入了专门的优化。在 rollout 阶段，我们采用了第 5.2.3 节详述的可抢占且容错的 rollout 服务。对于推理和训练阶段，我们将 rollout 数据格式分解为轻量级元数据和重量级的逐 token 字段。在数据调度期间，整个 rollout 数据的元数据可以被加载以执行全局洗牌（global shuffling）和打包布局计算。重量级的逐 token 字段通过共享内存数据加载器加载，以消除节点内数据冗余，并在 mini-batch 粒度上消费后立即释放，从而大幅降低 CPU 和 GPU 内存压力。设备上 mini-batch 的数量根据工作负载动态确定，以实现计算吞吐量与 I/O 重叠之间的高效权衡。</p>
<h3 id="525-面向智能体-ai-的沙箱基础设施sandbox-infrastructure-for-agentic-ai">5.2.5. 面向智能体 AI 的沙箱基础设施（Sandbox Infrastructure for Agentic AI）</h3>
<p>为了满足后训练和评估期间智能体 AI 的多样化执行需求，我们构建了一个生产级沙箱平台——<strong>DeepSeek 弹性计算（DeepSeek Elastic Compute, DSec）</strong>。DSec 由三个 Rust 组件构成——API 网关（<code>Apiserver</code>）、每主机代理（<code>Edge</code>）和集群监控器（<code>Watcher</code>）——它们通过自定义 RPC 协议互联，并在 3FS 分布式文件系统（DeepSeek-AI, 2025）之上水平扩展。在生产环境中，单个 DSec 集群管理数十万个并发沙箱实例。</p>
<p>DSec 的设计源于四个观察：（1）智能体工作负载高度异构，从轻量级函数调用到具有多样化操作系统和安全需求的完整软件工程流水线；（2）环境镜像数量众多且体积庞大，但必须能快速加载并支持迭代式定制；（3）高密度部署要求高效的 CPU 和内存利用；（4）沙箱生命周期必须与 GPU 训练调度协调，包括抢占和基于检查点的恢复。基于这些观察，我们在以下内容中逐一阐述 DSec 的四个核心设计。</p>
<p><strong>统一接口下的四种执行基底（Four Execution Substrates Behind One Unified Interface）。</strong> DSec 暴露一个统一的 Python SDK（<code>libdsec</code>），抽象了四种执行基底。<strong>函数调用（Function Call）</strong> 将无状态调用分派到预热容器池，消除冷启动开销。<strong>容器（Container）</strong> 完全兼容 Docker，并利用 EROFS（Gao et al., 2019）实现按需加载以高效组装镜像。<strong>微型虚拟机（microVM）</strong> 基于 Firecracker（Agache et al., 2020）构建，增加了 VM 级别的隔离，适用于安全敏感的高密度部署。<strong>完整虚拟机（fullVM）</strong> 基于 QEMU（Bellard, 2005）构建，支持任意客户操作系统。这四种基底共享统一的 API 接口——命令执行、文件传输和 TTY 访问——它们之间的切换仅需修改一个参数。</p>
<p><strong>基于分层存储的快速镜像加载（Fast Image Loading via Layered Storage）。</strong> DSec 通过分层、按需加载来平衡快速启动与大规模且不断增长的环境镜像语料库。对于容器，基础镜像和文件系统提交以 3FS 支持的只读 EROFS 层存储，直接挂载到 overlay <code>lowerdirs</code> 中。我们将文件元数据保持在本地磁盘上以便随时访问；同时，数据块按需从 3FS 获取。对于微型虚拟机，DSec 使用 <code>overlaybd</code>（Li et al., 2020）磁盘格式：只读基础层存放在 3FS 上以实现跨实例共享，而写操作则进入本地的写时复制层。这种快照可以链式使用，便于高效的版本管理和毫秒级恢复。</p>
<p><strong>大规模并发下的密度优化（Density Optimizations Under Massive Concurrency）。</strong> 为了在每个集群上容纳数十万个沙箱，DSec 解决了两个资源瓶颈。首先，它通过减少虚拟化环境中的重复页面缓存占用并应用内存回收来实现安全的超额分配（overcommitment）。其次，它缓解了容器运行时中的自旋锁竞争（spinlock contention），从而降低了每个沙箱的 CPU 开销，显著提升了每台主机的打包密度。</p>
<p><strong>轨迹日志与抢占安全恢复（Trajectory Logging and Preemption-Safe Resumption）。</strong> DSec 为每个沙箱维护一个全局有序的轨迹日志（trajectory log），持久化地记录每个命令调用及其结果。该轨迹日志服务于三个目的：（1）<strong>客户端快进（client fast-forwarding）</strong>——当训练任务被抢占时，沙箱资源仍然保留；恢复时，DSec 重放先前已完成命令的缓存结果，加速任务恢复，同时防止非幂等操作被重复执行而导致的错误；（2）<strong>细粒度溯源（fine-grained provenance）</strong>——每次状态变更的来源及相应结果都可追溯；（3）<strong>确定性重放（deterministic replay）</strong>——任何历史会话都可以从其轨迹中忠实地复现。</p>
<h2 id="53-标准基准评估standard-benchmark-evaluation">5.3. 标准基准评估（Standard Benchmark Evaluation）</h2>
<h3 id="531-评估设置evaluation-setup">5.3.1. 评估设置（Evaluation Setup）</h3>
<p><strong>知识与推理（Knowledge and Reasoning）。</strong> 知识与推理数据集包括 MMLU-Pro（Wang et al., 2024b）、GPQA（Rein et al., 2023）、Human Last Exam（Phan et al., 2025）、Simple-QA Verified（Haas et al., 2025）、Chinese-SimpleQA（He et al., 2024）、LiveCodeBench-v6（Jain et al., 2024）、CodeForces（内部基准）、HMMT 2026 Feb、Apex（Balunovic et al., 2025）、Apex Shortlist（Balunovic et al., 2025）、IMOAnswerBench（Luong et al., 2025）和 PutnamBench（Tsoukalas et al., 2024）。</p>
<p>对于代码，我们在 LiveCodeBench-v6 和一个内部 Codeforces 基准上评估 DeepSeek-V4 系列。对于 Codeforces，我们收集了 14 场 Codeforces Division 1 竞赛，包含 114 道题目（2025 年 5 月至 11 月）。Elo 评分的计算方式如下：对于每场竞赛，我们为每道题独立生成 32 个候选解。对于每道题，我们从这些解中无放回地随机抽取 10 个，并以随机顺序排列组成提交序列。每个提交都由领域专家构建的测试套件评判。已解决问题的得分遵循 OpenAI（2025）的罚分方案：模型获得的分数等于在相同先前失败次数下解决同一问题的人类参与者的中位数得分。这产生了每个采样提交序列的竞赛总分，进而转化为竞赛排名，随后转化为估计评分（通过标准 Codeforces 评分系统）。竞赛级别的期望评分定义为该估计评分在所有可能的随机选择和排列方式（每题 10 个提交）上的期望值。模型的总体评分是这些竞赛级别期望评分在全部 14 场竞赛上的平均值。</p>
<p>对于推理和知识任务，我们将温度设为 1.0，并将 Non-think、High 和 Max 模式的上下文窗口分别设为 8K、128K 和 384K token。对于数学任务（如 HMMT、IMOAnswerBench、Apex 和 HLE），我们使用以下模板进行评估：<code>&quot;{question}\nPlease reason step by step, and put your final answer within \boxed{}.&quot;</code>。对于 DeepSeek-V4-Pro-Max 在数学任务上的评估，我们使用以下模板以引发更深入的推理：<code>&quot;Solve the following problem. The problem may ask you to prove a statement, or ask for an answer. If finding an answer is required, you should come up with the answer, and your final solution should also be a rigorous proof of that answer being valid.\n\n{question}&quot;</code>。</p>
<p>对于形式化数学任务，我们在 Lean v4.28.0-rc1（Moura and Ullrich, 2021）的智能体设置下进行评估，可使用 Lean 编译器和语义策略搜索引擎，以最大推理力度运行，工具调用上限为 500 次。此外，我们还评估了一种计算密集型更高的流程：先生成并过滤候选自然语言解，然后通过自我验证（self-verification）（Shao et al., 2025）筛选保留的解，再将其提供给形式化智能体作为引导，用于证明对应的 Lean 陈述。这种设计利用非形式化推理来拓展探索空间，同时通过形式化验证保证严格的正确性。提交仅在严格验证器 Comparator 同时接受两种设置下的结果时才被视为正确。</p>
<p>由于 K2.6 和 GLM-5.1 的 API 返回查询过于繁忙的响应，我们将其部分条目留空。</p>
<p><strong>1M Token 上下文。</strong> 由于 DeepSeek-V4 系列支持 1M token 上下文，我们选择 OpenAI MRCR（OpenAI, 2024b）和 CorpusQA（Lu et al., 2026）作为基准，评估模型在长上下文场景下的表现。我们在这些任务上重新评估了 Claude Opus 4.6 和 Gemini 3.1 Pro，以期在所有模型之间实现配置标准化。我们未评估 GPT-5.4，因为其 API 对我们的大部分查询返回了失败响应。</p>
<p><strong>智能体（Agent）。</strong> 智能体数据集包括 Terminal Bench 2.0（Merrill et al., 2026）、SWE-Verified（OpenAI, 2024e）、SWE Multilingual（Yang et al., 2025）、SWE-Pro（Deng et al., 2025）、BrowseComp（Wei et al., 2025）、MCPAtlas 公开评估集（Bandi et al., 2026）、GDPval-AA（AA, 2025; Patwardhan et al., 2025）和 Tool-Decathlon（Li et al., 2025）。</p>
<p>对于代码智能体任务（SWE-Verified、Terminal-Bench、SWE-Pro、SWE Multilingual），我们使用内部开发的评估框架评估 DeepSeek-V4 系列。该框架提供了一组最小化的工具——一个 bash 工具和一个文件编辑工具。最大交互步数设为 500，最大上下文长度设为 512K token。关于 Terminal-Bench 2.0，我们注意到 GLM-5.1 指出的环境相关问题。尽管如此，我们仍在原始 Terminal-Bench 2.0 数据集上报告性能以保持一致性。在 Terminal-Bench 2.0 Verified 子集上，DeepSeek-V4-Pro 达到了约 72.0 的分数。</p>
<p>对于搜索智能体任务（BrowseComp、HLE w/ tool），我们同样使用内部搭建的评测框架，配备网络搜索和 Python 工具，最大交互步数设为 500，最大上下文长度设为 512K token。对于 BrowseComp，我们使用与 DeepSeek-V3.2 相同的丢弃全部（discard-all）上下文管理策略（DeepSeek-AI, 2025）。</p>
<h3 id="532-评估结果evaluation-results">5.3.2. 评估结果（Evaluation Results）</h3>
<p>表 6 | DeepSeek-V4-Pro-Max 与闭源/开源模型的对比。&ldquo;Max&rdquo;、&ldquo;xHigh&quot;和&quot;High&quot;表示推理力度。最佳结果以<strong>粗体</strong>标注，第二佳结果以<u>下划线</u>标注。</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th>基准测试 (指标)</th>
          <th>Opus-4.6 Max</th>
          <th>GPT-5.4 xHigh</th>
          <th>Gemini-3.1-Pro High</th>
          <th>K2.6 Thinking</th>
          <th>GLM-5.1 Thinking</th>
          <th>DS-V4-Pro Max</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>知识与推理</td>
          <td>MMLU-Pro (EM)</td>
          <td>89.1</td>
          <td>87.5</td>
          <td>91.0</td>
          <td>87.1</td>
          <td>86.0</td>
          <td><strong>87.5</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>SimpleQA-Verified (Pass@1)</td>
          <td>46.2</td>
          <td>45.3</td>
          <td><strong>75.6</strong></td>
          <td>36.9</td>
          <td>38.1</td>
          <td>57.9</td>
      </tr>
      <tr>
          <td></td>
          <td>Chinese-SimpleQA (Pass@1)</td>
          <td>76.4</td>
          <td>76.8</td>
          <td><strong>85.9</strong></td>
          <td>75.9</td>
          <td>75.0</td>
          <td>84.4</td>
      </tr>
      <tr>
          <td></td>
          <td>GPQA Diamond (Pass@1)</td>
          <td>91.3</td>
          <td><strong>93.0</strong></td>
          <td>94.3</td>
          <td>90.5</td>
          <td>86.2</td>
          <td>90.1</td>
      </tr>
      <tr>
          <td></td>
          <td>HLE (Pass@1)</td>
          <td>40.0</td>
          <td>39.8</td>
          <td><strong>44.4</strong></td>
          <td>36.4</td>
          <td>34.7</td>
          <td>37.7</td>
      </tr>
      <tr>
          <td></td>
          <td>LiveCodeBench (Pass@1)</td>
          <td>88.8</td>
          <td>-</td>
          <td>91.7</td>
          <td>89.6</td>
          <td>-</td>
          <td><strong>93.5</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>Codeforces (Rating)</td>
          <td>-</td>
          <td>3168</td>
          <td>3052</td>
          <td>-</td>
          <td>-</td>
          <td><strong>3206</strong></td>
      </tr>
      <tr>
          <td></td>
          <td>HMMT 2026 Feb (Pass@1)</td>
          <td>96.2</td>
          <td><strong>97.7</strong></td>
          <td>94.7</td>
          <td>92.7</td>
          <td>89.4</td>
          <td>95.2</td>
      </tr>
      <tr>
          <td></td>
          <td>IMOAnswerBench (Pass@1)</td>
          <td>75.3</td>
          <td><strong>91.4</strong></td>
          <td>81.0</td>
          <td>86.0</td>
          <td>83.8</td>
          <td>89.8</td>
      </tr>
      <tr>
          <td></td>
          <td>Apex (Pass@1)</td>
          <td>34.5</td>
          <td>54.1</td>
          <td><strong>60.9</strong></td>
          <td>24.0</td>
          <td>11.5</td>
          <td>38.3</td>
      </tr>
      <tr>
          <td></td>
          <td>Apex Shortlist (Pass@1)</td>
          <td>85.9</td>
          <td>78.1</td>
          <td>89.1</td>
          <td>75.5</td>
          <td>72.4</td>
          <td><strong>90.2</strong></td>
      </tr>
      <tr>
          <td>长上下文</td>
          <td>MRCR 1M (MMR)</td>
          <td><strong>92.9</strong></td>
          <td>-</td>
          <td>76.3</td>
          <td>-</td>
          <td>-</td>
          <td>83.5</td>
      </tr>
      <tr>
          <td></td>
          <td>CorpusQA 1M (ACC)</td>
          <td><strong>71.7</strong></td>
          <td>-</td>
          <td>53.8</td>
          <td>-</td>
          <td>-</td>
          <td>62.0</td>
      </tr>
      <tr>
          <td>智能体</td>
          <td>Terminal Bench 2.0 (Acc)</td>
          <td>65.4</td>
          <td><strong>75.1</strong></td>
          <td>68.5</td>
          <td>66.7</td>
          <td>63.5</td>
          <td>67.9</td>
      </tr>
      <tr>
          <td></td>
          <td>SWE Verified (Resolved)</td>
          <td><strong>80.8</strong></td>
          <td>-</td>
          <td>80.6</td>
          <td>80.2</td>
          <td>-</td>
          <td>80.6</td>
      </tr>
      <tr>
          <td></td>
          <td>SWE Pro (Resolved)</td>
          <td>57.3</td>
          <td>57.7</td>
          <td>54.2</td>
          <td><strong>58.6</strong></td>
          <td>58.4</td>
          <td>55.4</td>
      </tr>
      <tr>
          <td></td>
          <td>SWE Multilingual (Resolved)</td>
          <td><strong>77.5</strong></td>
          <td>-</td>
          <td>-</td>
          <td>76.7</td>
          <td>73.3</td>
          <td>76.2</td>
      </tr>
      <tr>
          <td></td>
          <td>BrowseComp (Pass@1)</td>
          <td>83.7</td>
          <td>82.7</td>
          <td><strong>85.9</strong></td>
          <td>83.2</td>
          <td>79.3</td>
          <td>83.4</td>
      </tr>
      <tr>
          <td></td>
          <td>HLE w/ tools (Pass@1)</td>
          <td>53.1</td>
          <td>52.0</td>
          <td>51.6</td>
          <td><strong>54.0</strong></td>
          <td>50.4</td>
          <td>48.2</td>
      </tr>
      <tr>
          <td></td>
          <td>GDPval-AA (Elo)</td>
          <td>1619</td>
          <td><strong>1674</strong></td>
          <td>1314</td>
          <td>1482</td>
          <td>1535</td>
          <td>1554</td>
      </tr>
      <tr>
          <td></td>
          <td>MCPAtlas Public (Pass@1)</td>
          <td><strong>73.8</strong></td>
          <td>67.2</td>
          <td>69.2</td>
          <td>66.6</td>
          <td>71.8</td>
          <td>73.6</td>
      </tr>
      <tr>
          <td></td>
          <td>Toolathlon (Pass@1)</td>
          <td>47.2</td>
          <td><strong>54.6</strong></td>
          <td>48.8</td>
          <td>50.0</td>
          <td>40.7</td>
          <td>51.8</td>
      </tr>
  </tbody>
</table>
<p>DeepSeek-V4-Pro-Max 与其他闭源/开源模型的对比结果见表 6。同时，我们在表 7 中评估了 DeepSeek-V4-Flash 和 DeepSeek-V4-Pro 的不同模式，并展示了结果。</p>
<p>表 7 | DeepSeek-V4 系列不同规模和模式之间的对比。&ldquo;Non-Think&rdquo;、&ldquo;High&quot;和&quot;Max&quot;表示推理力度。</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th>基准测试 (指标)</th>
          <th>DeepSeek-V4-Flash</th>
          <th></th>
          <th></th>
          <th>DeepSeek-V4-Pro</th>
          <th></th>
          <th></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td></td>
          <td></td>
          <td>Non-Think</td>
          <td>High</td>
          <td>Max</td>
          <td>Non-Think</td>
          <td>High</td>
          <td>Max</td>
      </tr>
      <tr>
          <td>知识与推理</td>
          <td>MMLU-Pro (EM)</td>
          <td>83.0</td>
          <td>86.4</td>
          <td>86.2</td>
          <td>82.9</td>
          <td>87.1</td>
          <td>87.5</td>
      </tr>
      <tr>
          <td></td>
          <td>SimpleQA-Verified (Pass@1)</td>
          <td>23.1</td>
          <td>28.9</td>
          <td>34.1</td>
          <td>45.0</td>
          <td>46.2</td>
          <td>57.9</td>
      </tr>
      <tr>
          <td></td>
          <td>Chinese-SimpleQA (Pass@1)</td>
          <td>71.5</td>
          <td>73.2</td>
          <td>78.9</td>
          <td>75.8</td>
          <td>77.7</td>
          <td>84.4</td>
      </tr>
      <tr>
          <td></td>
          <td>GPQA Diamond (Pass@1)</td>
          <td>71.2</td>
          <td>87.4</td>
          <td>88.1</td>
          <td>72.9</td>
          <td>89.1</td>
          <td>90.1</td>
      </tr>
      <tr>
          <td></td>
          <td>HLE (Pass@1-COT)</td>
          <td>8.1</td>
          <td>29.4</td>
          <td>34.8</td>
          <td>7.7</td>
          <td>35.4</td>
          <td>37.7</td>
      </tr>
      <tr>
          <td></td>
          <td>LiveCodeBench (Pass@1-COT)</td>
          <td>55.2</td>
          <td>88.4</td>
          <td>91.6</td>
          <td>56.8</td>
          <td>89.8</td>
          <td>93.5</td>
      </tr>
      <tr>
          <td></td>
          <td>Codeforces (Rating)</td>
          <td>-</td>
          <td>2816</td>
          <td>3052</td>
          <td>-</td>
          <td>2919</td>
          <td>3206</td>
      </tr>
      <tr>
          <td></td>
          <td>HMMT 2026 Feb (Pass@1)</td>
          <td>40.8</td>
          <td>91.9</td>
          <td>94.8</td>
          <td>31.7</td>
          <td>94.0</td>
          <td>95.2</td>
      </tr>
      <tr>
          <td></td>
          <td>IMOAnswerBench (Pass@1)</td>
          <td>41.9</td>
          <td>85.1</td>
          <td>88.4</td>
          <td>35.3</td>
          <td>88.0</td>
          <td>89.8</td>
      </tr>
      <tr>
          <td></td>
          <td>Apex (Pass@1)</td>
          <td>1.0</td>
          <td>19.1</td>
          <td>33.0</td>
          <td>0.4</td>
          <td>27.4</td>
          <td>38.3</td>
      </tr>
      <tr>
          <td></td>
          <td>Apex Shortlist (Pass@1)</td>
          <td>9.3</td>
          <td>72.1</td>
          <td>85.7</td>
          <td>9.2</td>
          <td>85.5</td>
          <td>90.2</td>
      </tr>
      <tr>
          <td>长上下文</td>
          <td>MRCR 1M (MMR)</td>
          <td>37.5</td>
          <td>76.9</td>
          <td>78.7</td>
          <td>44.7</td>
          <td>83.3</td>
          <td>83.5</td>
      </tr>
      <tr>
          <td></td>
          <td>CorpusQA 1M (ACC)</td>
          <td>15.5</td>
          <td>59.3</td>
          <td>60.5</td>
          <td>35.6</td>
          <td>56.5</td>
          <td>62.0</td>
      </tr>
      <tr>
          <td>智能体</td>
          <td>Terminal Bench 2.0 (Acc)</td>
          <td>49.1</td>
          <td>56.6</td>
          <td>56.9</td>
          <td>59.1</td>
          <td>63.3</td>
          <td>67.9</td>
      </tr>
      <tr>
          <td></td>
          <td>SWE Verified (Resolved)</td>
          <td>73.7</td>
          <td>78.6</td>
          <td>79.0</td>
          <td>73.6</td>
          <td>79.4</td>
          <td>80.6</td>
      </tr>
      <tr>
          <td></td>
          <td>SWE Pro (Resolved)</td>
          <td>49.1</td>
          <td>52.3</td>
          <td>52.6</td>
          <td>52.1</td>
          <td>54.4</td>
          <td>55.4</td>
      </tr>
      <tr>
          <td></td>
          <td>SWE Multilingual (Resolved)</td>
          <td>69.7</td>
          <td>70.2</td>
          <td>73.3</td>
          <td>69.8</td>
          <td>74.1</td>
          <td>76.2</td>
      </tr>
      <tr>
          <td></td>
          <td>BrowseComp (Pass@1)</td>
          <td>-</td>
          <td>53.5</td>
          <td>73.2</td>
          <td>-</td>
          <td>80.4</td>
          <td>83.4</td>
      </tr>
      <tr>
          <td></td>
          <td>HLE w/ tools (Pass@1)</td>
          <td>-</td>
          <td>40.3</td>
          <td>45.1</td>
          <td>-</td>
          <td>44.7</td>
          <td>48.2</td>
      </tr>
      <tr>
          <td></td>
          <td>MCPAtlas Public (Pass@1)</td>
          <td>64.0</td>
          <td>67.4</td>
          <td>69.0</td>
          <td>69.4</td>
          <td>74.2</td>
          <td>73.6</td>
      </tr>
      <tr>
          <td></td>
          <td>GDPval-AA (Elo)</td>
          <td>-</td>
          <td>-</td>
          <td>1395</td>
          <td>-</td>
          <td>-</td>
          <td>1554</td>
      </tr>
      <tr>
          <td></td>
          <td>Toolathlon (Pass@1)</td>
          <td>40.7</td>
          <td>43.5</td>
          <td>47.8</td>
          <td>46.3</td>
          <td>49.0</td>
          <td>51.8</td>
      </tr>
  </tbody>
</table>
<p><strong>知识。</strong> 在通用世界知识的评估中，DeepSeek-V4-Pro 的最大推理力度模式（DeepSeek-V4-Pro-Max）在开源大语言模型中确立了新的最先进水平。如 SimpleQA-Verified 所示，DeepSeek-V4-Pro-Max 以 20 个绝对百分点的优势大幅超越所有现有开源基线。尽管取得了这些进展，它目前仍落后于领先的闭源模型 Gemini-3.1-Pro。在教育类知识和推理领域，DeepSeek-V4-Pro-Max 在 MMLU-Pro 上略微超越 Kimi 和 GLM，尽管在 GPQA 和 HLE 基准上仍落后于领先的闭源模型。总体而言，DeepSeek-V4-Pro-Max 在增强开源模型世界知识能力方面标志着一个重要里程碑。</p>
<p>此外，DeepSeek-V4-Flash 和 DeepSeek-V4-Pro 在知识类任务上存在显著的性能差距；这在预期之内，因为更大的参数规模有助于在预训练期间保留更多知识。值得注意的是，两个模型在分配更高推理力度时，在知识基准上都展现了更好的表现。</p>
<p><strong>推理。</strong> DeepSeek-V4-Pro-Max 在所有主要开源模型的推理基准上表现优异，并在许多指标上达到了与最先进闭源模型相当的水平，而较小的 DeepSeek-V4-Flash-Max 也超越了此前最佳的开源模型 K2.6-Thinking。同时，DeepSeek-V4-Pro 和 DeepSeek-V4-Flash 在代码和数学推理任务上表现出色。根据我们的评估，它们在编程竞赛中的表现可与 GPT-5.4 相媲美，这是开源模型首次在该任务上追平闭源模型。在 Codeforces 排行榜上，DeepSeek-V4-Pro-Max 目前位列人类参赛者第 23 名。DeepSeek-V4 在形式化数学任务上也展现了强劲的表现，无论是在智能体设置还是计算密集型设置下。在智能体设置下，它取得了最先进的结果，如图 8 所示，超越了 Seed Prover 等先前的模型（Chen et al., 2025）。在计算密集型更高的流程下，性能进一步提升，超越了包括 Aristotle（Achim et al., 2025）在内的系统，并达到了该设置下已知的最佳结果。</p>
<p><img alt="形式化推理在实用和前沿两种范式下的表现" loading="lazy" src="/readings/deepseek-v4/images/fig8-9-formal-mrcr.png"></p>
<p><strong>智能体。</strong> DeepSeek-V4 系列在智能体评估中展现了强劲的表现。对于代码智能体任务，DeepSeek-V4-Pro 取得了与 K2.6 和 GLM-5.1 相当的结果，尽管所有这些开源模型仍落后于闭源对手。DeepSeek-V4-Flash 在编程任务上的表现不及 DeepSeek-V4-Pro，尤其在 Terminal Bench 2.0 上差距明显。在其他智能体评估中也观察到类似趋势。值得注意的是，DeepSeek-V4-Pro 在 MCPAtlas 和 Toolathlon 这两个包含广泛工具和 MCP 服务的评测集上表现出色——这表明我们的模型具有优秀的泛化能力，并非仅在内部框架上表现良好。</p>
<p><strong>1M Token 上下文。</strong> DeepSeek-V4-Pro 在 MRCR 任务上超越了 Gemini-3.1-Pro（该任务衡量上下文内检索能力），但仍落后于 Claude Opus 4.6。如图 9 所示，在 128K 上下文窗口内，检索性能保持高度稳定。虽然超过 128K 标记后性能开始出现退化，但与闭源和开源对手相比，模型在 1M token 下的检索能力仍然表现出色。与 MRCR 不同，CorpusQA 更接近真实场景。评估结果同样表明 DeepSeek-V4-Pro 优于 Gemini-3.1-Pro。</p>
<p><img alt="DeepSeek-V4 系列在 MRCR 任务上的表现" loading="lazy" src="/readings/deepseek-v4/images/fig8-9-formal-mrcr.png"></p>
<p><strong>推理力度。</strong> 如表 7 所示，采用更长上下文和在 RL 中降低长度惩罚的 Max 模式，在最具挑战性的任务上优于 High 模式。图 10 展示了 DeepSeek-V4-Pro、DeepSeek-V4-Flash 和 DeepSeek-V3.2 在代表性推理和智能体任务上的性能与成本对比。通过扩展测试时计算（test-time compute），DeepSeek-V4 系列相较前代取得了显著提升。此外，在 HLE 等推理任务上，DeepSeek-V4-Pro 展现出高于 DeepSeek-V3.2 的 token 效率。</p>
<p><img alt="HLE 和 Terminal Bench 2.0 在不同推理力度下的表现" loading="lazy" src="/readings/deepseek-v4/images/fig10-hle-terminal.png"></p>
<h2 id="54-真实世界任务表现performance-on-real-world-tasks">5.4. 真实世界任务表现（Performance on Real-World Tasks）</h2>
<p>标准化基准测试往往难以捕捉多样化真实世界任务的复杂性，导致测试结果与实际用户体验之间存在差距。为弥合这一鸿沟，我们开发了专有的内部指标，优先关注真实世界的使用模式而非传统基准。这种方法确保我们的优化能够转化为切实的收益。我们的评估框架专门针对 DeepSeek API 和聊天机器人的主要使用场景，使模型性能与实际需求相匹配。</p>
<h3 id="541-中文写作chinese-writing">5.4.1. 中文写作（Chinese Writing）</h3>
<p>DeepSeek 的主要使用场景之一是中文写作。我们对功能性写作和创意写作进行了严格的评估。表 12 展示了 DeepSeek-V4-Pro 与 Gemini-3.1-Pro 在功能性写作任务上的成对对比。这些任务由常见的日常写作需求组成，提示通常简洁明了。Gemini-3.1-Pro 被选为基线，因为它在我们的评估中是中文写作表现最佳的外部模型。结果表明 DeepSeek-V4-Pro 以 62.7% 对 34.1% 的总体胜率超越了该基线；这主要是因为 Gemini 偶尔会让其固有的风格偏好覆盖用户的明确要求，尤其在中文写作场景中。</p>
<p>表 13 展示了创意写作对比，评估沿两个轴进行：指令遵循和写作质量。与 Gemini-3.1-Pro 相比，DeepSeek-V4-Pro 在指令遵循上的胜率为 60.0%，在写作质量上的胜率为 77.5%，表明在指令遵循方面有一定改善，在写作质量方面有大幅提升。虽然 DeepSeek-V4-Pro 在总体用户案例分析中表现更优，但将评估限制在最具挑战性的提示上——特别是涉及高复杂度约束或多轮对话的场景——则发现 Claude Opus 4.5 相比 DeepSeek-V4-Pro 仍保持性能优势。如表 14 所示，Claude Opus 4.5 以 52.0% 对 45.9% 的胜率领先。</p>
<h3 id="542-搜索search">5.4.2. 搜索（Search）</h3>
<p>搜索增强问答是 DeepSeek 聊天机器人的核心功能。在 DeepSeek 网页端和应用端，&ldquo;non-think&quot;模式采用检索增强生成（Retrieval-Augmented Search, RAG），而&quot;thinking&quot;模式则使用智能体搜索（agentic search）。</p>
<p><strong>检索增强搜索（Retrieval Augmented Search）。</strong> 我们对 DeepSeek-V4-Pro 和 DeepSeek-V3.2 在客观和主观问答类别上进行了成对评估。如表 11 所示，DeepSeek-V4-Pro 以较大优势超越了 DeepSeek-V3.2，在两个类别上都展现了一致的优势。最显著的提升出现在单值搜索和规划策略任务中，表明 DeepSeek-V4-Pro 在从检索上下文中定位精确的事实答案和合成结构化方案方面表现出色。然而，DeepSeek-V3.2 在对比和推荐任务上仍保持相当的竞争力，表明 DeepSeek-V4-Pro 在需要平衡多角度推理的搜索结果场景中仍有改进空间。</p>
<p><strong>智能体搜索（Agentic Search）。</strong> 与标准 RAG 不同，智能体搜索赋予模型迭代调用搜索和获取工具的能力，显著增强了整体搜索表现。在 DeepSeek-Chat 的 thinking 模式中，我们优化了智能体搜索功能，以在预定义的&quot;思考预算&quot;内最大化响应准确性。如表 9 所示，智能体搜索在复杂任务上持续优于 RAG。此外，其成本保持高效，智能体搜索仅比标准 RAG 略微昂贵（见表 10）。</p>
<h3 id="543-白领任务white-collar-task">5.4.3. 白领任务（White-Collar Task）</h3>
<p>为了严格评估模型在复杂企业生产力场景中的实用性，我们构建了一个包含 30 个高级中文专业任务的综合测试集。这些工作流涵盖了高级认知需求，包括深度信息分析、全面文档生成和精细文档编辑，覆盖了金融、教育、法律和科技等 13 个关键行业。评估在内部搭建的智能体框架中进行，配备了 Bash 和网络搜索等基本工具。</p>
<p>鉴于这些任务的开放性质，自动化指标通常无法捕捉高质量响应的细微差别。因此，我们进行了人工评估，将 DeepSeek-V4-Pro-Max 与 Opus-4.6-Max 的表现进行对比。标注员在以下四个维度上对模型输出进行了盲评：</p>
<ul>
<li><strong>任务完成度（Task Completion）：</strong> 核心问题是否被成功解决。</li>
<li><strong>指令遵循（Instruction Following）：</strong> 对具体约束和指令的遵守程度。</li>
<li><strong>内容质量（Content Quality）：</strong> 事实准确性、逻辑连贯性和专业语气。</li>
<li><strong>排版美观（Formatting Aesthetics）：</strong> 版面可读性和视觉呈现。</li>
</ul>
<p>如图 11 所示，DeepSeek-V4-Pro-Max 在多样化的中文白领任务中全面超越 Opus-4.6-Max，实现了 63% 的不败率（non-loss rate），并在分析、生成和编辑任务上展现了一致的优势。图 12 中的详细维度得分突出了模型在任务完成度和内容质量方面的主要优势。具体而言，DeepSeek-V4-Pro-Max 能够主动预见用户的隐含意图，频繁提供补充性见解和自我验证步骤。它在长文本生成方面也表现出色，提供深入、连贯的叙述，而非 Opus-4.6-Max 经常产出的过度简化的要点列表。此外，该模型严格遵循正式的专业规范，如标准化的中文层级编号。然而，在指令遵循方面，它偶尔会忽略特定的格式约束，略微落后于 Opus。此外，该模型在将大量文本输入精简为简洁摘要方面不够熟练。最后，其排版美观度仍有很大改进空间，特别是在演示文稿幻灯片的整体视觉设计方面。图 13、14 和 15 展示了若干测试案例；由于部分输出较长，仅展示了部分页面。</p>
<p><img alt="白领任务胜率对比与详细维度得分" loading="lazy" src="/readings/deepseek-v4/images/fig11-12-whitecollar.png"></p>
<h3 id="544-代码智能体code-agent">5.4.4. 代码智能体（Code Agent）</h3>
<p>为了评估我们的编码智能体能力，我们从真实的内部研发工作负载中策划任务。我们从 50 多位内部工程师处收集了约 200 个具有挑战性的任务，涵盖功能开发、bug 修复、代码重构和跨多种技术栈（包括 PyTorch、CUDA、Rust 和 C++）的故障诊断。每个任务都附有其原始代码仓库、对应的执行环境和人工标注的评分标准；经过严格的质量筛选后，保留了 30 个任务作为评估集。如表 8 所示，DeepSeek-V4-Pro 显著超越了 Claude Sonnet 4.5，并接近 Claude Opus 4.5 的水平。</p>
<p>表 8 | 研发编程基准对比（外部模型仅用于评估目的）</p>
<table>
  <thead>
      <tr>
          <th>Model</th>
          <th>Haiku 4.5</th>
          <th>Sonnet 4.5</th>
          <th>DeepSeek-V4-Pro-Max</th>
          <th>Opus 4.5</th>
          <th>Opus 4.5 Thinking</th>
          <th>Opus 4.6 Thinking</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Pass Rate (%)</td>
          <td>13</td>
          <td>47</td>
          <td>67</td>
          <td>70</td>
          <td>73</td>
          <td><strong>80</strong></td>
      </tr>
  </tbody>
</table>
<p>在一项针对 DeepSeek 开发者和研究人员（<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi><mo>=</mo><mn>85</mn></mrow><annotation encoding="application/x-tex">N = 85</annotation></semantics></math></span>）的调查中——所有受访者都有在日常工作中使用 DeepSeek-V4-Pro 进行智能体编程的经验——被问及 DeepSeek-V4-Pro 与其他前沿模型相比是否已可作为默认和主要编程模型使用时，52% 的人表示是，39% 的人倾向于是，不到 9% 的人表示否。受访者认为 DeepSeek-V4-Pro 在大多数任务中能够交付令人满意的结果，但也注意到存在一些小问题，包括对模糊提示的误解和偶尔的过度思考。</p>
<h1 id="6-结论局限性与未来方向">6. 结论、局限性与未来方向</h1>
<p>在本工作中，我们发布了 DeepSeek-V4 系列的预览版本，目标是打造突破超长上下文处理效率瓶颈的下一代大语言模型（Large Language Models）。通过结合 CSA 和 HCA 的混合注意力架构（Hybrid Attention Architecture），DeepSeek-V4 系列在长序列效率方面实现了巨大飞跃。这些架构创新与大量基础设施优化相结合，实现了对百万级 token 上下文的高效原生支持，并为未来的测试时扩展（Test-time Scaling）、长周期任务（Long-horizon Tasks）以及在线学习（Online Learning）等新兴范式奠定了必要基础。评测结果表明，DeepSeek-V4-Pro-Max 作为最大推理力度模式，重新定义了开源模型（Open Models）的技术前沿。它在知识类基准测试上大幅超越了此前的开源模型，在推理性能上接近前沿闭源模型（Frontier Proprietary Models），并展现出具有竞争力的智能体能力（Agent Capabilities）。与此同时，DeepSeek-V4-Flash-Max 在推理性能上达到了与领先闭源模型相当的水平，同时保持了高度成本高效的架构。我们相信，DeepSeek-V4 系列将开启开源模型百万级长上下文的新时代，并为实现更高的效率、规模和智能铺平道路。</p>
<p>在追求极致长上下文效率的过程中，DeepSeek-V4 系列采用了大胆的架构设计。为降低风险，我们保留了许多经过初步验证的组件和技巧，这些组件虽然有效，却使架构变得相对复杂。在未来的迭代中，我们将进行更全面、更系统的研究，将架构精简至最核心的设计要素，使其在不牺牲性能的前提下更加优雅。与此同时，尽管前瞻性路由（Anticipatory Routing）和 SwiGLU 钳制（SwiGLU Clamping）已被证明在缓解训练不稳定性方面行之有效，但其底层原理仍未被充分理解。我们将积极研究训练稳定性的基础问题，加强内部指标监控，力求以更系统化、更可预测的方式实现稳定的大规模训练。</p>
<p>此外，在混合专家模型（MoE）和稀疏注意力架构之外，我们还将沿着新的维度主动探索模型稀疏性——例如更稀疏的嵌入模块（Sparse Embedding Modules）(Cheng et al., 2026)——以在不损害能力的前提下进一步提升计算和内存效率。我们还将持续研究低延迟架构和系统技术，使长上下文部署和交互更加灵敏。此外，我们认识到长周期、多轮智能体任务（Long-horizon, Multi-round Agentic Tasks）的重要性和实用价值，并将持续在这一方向上进行迭代和探索。我们也正在致力于为模型引入多模态能力（Multimodal Capabilities）。最后，我们致力于开发更好的数据筛选和合成策略，以持续提升模型在日益广泛的场景和任务中的智能水平、鲁棒性和实用性。</p>
<h1 id="附录-b-评测详情">附录 B. 评测详情</h1>
<p><strong>表 9 | DeepSeek-V4-Pro 的智能体搜索（Agentic Search）与检索增强搜索（Retrieval Augmented Search）对比。</strong></p>
<table>
  <thead>
      <tr>
          <th>难度</th>
          <th>类别</th>
          <th style="text-align: right">#</th>
          <th style="text-align: right">Agent 胜出</th>
          <th style="text-align: right">RAG 胜出</th>
          <th style="text-align: right">平局</th>
          <th style="text-align: right">Agent%</th>
          <th style="text-align: right">RAG%</th>
          <th style="text-align: right">平局%</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Easy</td>
          <td>Objective Q&amp;A (客观问答)</td>
          <td style="text-align: right">196</td>
          <td style="text-align: right">110</td>
          <td style="text-align: right">43</td>
          <td style="text-align: right">43</td>
          <td style="text-align: right">56.1</td>
          <td style="text-align: right">21.9</td>
          <td style="text-align: right">21.9</td>
      </tr>
      <tr>
          <td>Easy</td>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td style="text-align: right">321</td>
          <td style="text-align: right">198</td>
          <td style="text-align: right">56</td>
          <td style="text-align: right">67</td>
          <td style="text-align: right">61.7</td>
          <td style="text-align: right">17.4</td>
          <td style="text-align: right">20.9</td>
      </tr>
      <tr>
          <td>Hard</td>
          <td>Objective Q&amp;A (客观问答)</td>
          <td style="text-align: right">168</td>
          <td style="text-align: right">102</td>
          <td style="text-align: right">33</td>
          <td style="text-align: right">33</td>
          <td style="text-align: right">60.7</td>
          <td style="text-align: right">19.6</td>
          <td style="text-align: right">19.6</td>
      </tr>
      <tr>
          <td>Hard</td>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td style="text-align: right">184</td>
          <td style="text-align: right">126</td>
          <td style="text-align: right">27</td>
          <td style="text-align: right">31</td>
          <td style="text-align: right">68.5</td>
          <td style="text-align: right">14.7</td>
          <td style="text-align: right">16.8</td>
      </tr>
      <tr>
          <td></td>
          <td>Total (总计)</td>
          <td style="text-align: right"><strong>869</strong></td>
          <td style="text-align: right"><strong>536</strong></td>
          <td style="text-align: right"><strong>159</strong></td>
          <td style="text-align: right"><strong>174</strong></td>
          <td style="text-align: right"><strong>61.7</strong></td>
          <td style="text-align: right"><strong>18.3</strong></td>
          <td style="text-align: right"><strong>20.0</strong></td>
      </tr>
  </tbody>
</table>
<p><strong>表 10 | DeepSeek-V4-Pro 的智能体搜索与检索增强搜索成本对比（均值）。智能体搜索的大部分工具调用为并行执行。</strong></p>
<table>
  <thead>
      <tr>
          <th>版本</th>
          <th style="text-align: right">工具调用次数</th>
          <th style="text-align: right">预填充 (tokens)</th>
          <th style="text-align: right">输出 (tokens)</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>V4 Agentic Search</td>
          <td style="text-align: right">16.2</td>
          <td style="text-align: right">13649</td>
          <td style="text-align: right">1526</td>
      </tr>
      <tr>
          <td>V4 Retrieval Augmented Search</td>
          <td style="text-align: right">—</td>
          <td style="text-align: right">10453</td>
          <td style="text-align: right">1308</td>
      </tr>
  </tbody>
</table>
<p><strong>表 11 | DeepSeek-V4-Pro 与 DeepSeek-V3.2 在搜索问答任务上的对比评测。</strong></p>
<table>
  <thead>
      <tr>
          <th></th>
          <th></th>
          <th style="text-align: right"></th>
          <th style="text-align: right">内部综合评估 (Internal Evaluation)</th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>类别</strong></td>
          <td><strong>子类别</strong></td>
          <td style="text-align: right"><strong>#</strong></td>
          <td style="text-align: right"><strong>V4 胜出</strong></td>
          <td style="text-align: right"><strong>V3.2 胜出</strong></td>
          <td style="text-align: right"><strong>平局</strong></td>
          <td style="text-align: right"><strong>V4%</strong></td>
          <td style="text-align: right"><strong>V3.2%</strong></td>
          <td style="text-align: right"><strong>平局%</strong></td>
      </tr>
      <tr>
          <td>Objective Q&amp;A (客观问答)</td>
          <td>Single-value Search (单值信息查找)</td>
          <td style="text-align: right">95</td>
          <td style="text-align: right">36</td>
          <td style="text-align: right">10</td>
          <td style="text-align: right">49</td>
          <td style="text-align: right">37.9</td>
          <td style="text-align: right">10.5</td>
          <td style="text-align: right">51.6</td>
      </tr>
      <tr>
          <td>Objective Q&amp;A (客观问答)</td>
          <td>Entity Search (实体信息查找)</td>
          <td style="text-align: right">99</td>
          <td style="text-align: right">24</td>
          <td style="text-align: right">7</td>
          <td style="text-align: right">68</td>
          <td style="text-align: right">24.2</td>
          <td style="text-align: right">7.1</td>
          <td style="text-align: right">68.7</td>
      </tr>
      <tr>
          <td>Objective Q&amp;A (客观问答)</td>
          <td>Enumerative Search (枚举型信息查找)</td>
          <td style="text-align: right">95</td>
          <td style="text-align: right">19</td>
          <td style="text-align: right">8</td>
          <td style="text-align: right">68</td>
          <td style="text-align: right">20.0</td>
          <td style="text-align: right">8.4</td>
          <td style="text-align: right">71.6</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>289</strong></td>
          <td style="text-align: right"><strong>79</strong></td>
          <td style="text-align: right"><strong>25</strong></td>
          <td style="text-align: right"><strong>185</strong></td>
          <td style="text-align: right"><strong>27.3</strong></td>
          <td style="text-align: right"><strong>8.7</strong></td>
          <td style="text-align: right"><strong>64.0</strong></td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Causal Analysis (原因分析)</td>
          <td style="text-align: right">100</td>
          <td style="text-align: right">28</td>
          <td style="text-align: right">5</td>
          <td style="text-align: right">67</td>
          <td style="text-align: right">28.0</td>
          <td style="text-align: right">5.0</td>
          <td style="text-align: right">67.0</td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Comparison (对比)</td>
          <td style="text-align: right">96</td>
          <td style="text-align: right">28</td>
          <td style="text-align: right">20</td>
          <td style="text-align: right">48</td>
          <td style="text-align: right">29.2</td>
          <td style="text-align: right">20.8</td>
          <td style="text-align: right">50.0</td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Advice Seeking (寻求建议)</td>
          <td style="text-align: right">92</td>
          <td style="text-align: right">23</td>
          <td style="text-align: right">8</td>
          <td style="text-align: right">61</td>
          <td style="text-align: right">25.0</td>
          <td style="text-align: right">8.7</td>
          <td style="text-align: right">66.3</td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Recommendation (推荐)</td>
          <td style="text-align: right">95</td>
          <td style="text-align: right">26</td>
          <td style="text-align: right">19</td>
          <td style="text-align: right">50</td>
          <td style="text-align: right">27.4</td>
          <td style="text-align: right">20.0</td>
          <td style="text-align: right">52.6</td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Planning &amp; Strategy (攻略计划)</td>
          <td style="text-align: right">92</td>
          <td style="text-align: right">32</td>
          <td style="text-align: right">11</td>
          <td style="text-align: right">49</td>
          <td style="text-align: right">34.8</td>
          <td style="text-align: right">12.0</td>
          <td style="text-align: right">53.3</td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Opinion &amp; Evaluation (评价看法)</td>
          <td style="text-align: right">96</td>
          <td style="text-align: right">30</td>
          <td style="text-align: right">8</td>
          <td style="text-align: right">58</td>
          <td style="text-align: right">31.2</td>
          <td style="text-align: right">8.3</td>
          <td style="text-align: right">60.4</td>
      </tr>
      <tr>
          <td>Subjective Q&amp;A (主观问答)</td>
          <td>Trend Analysis (趋势分析)</td>
          <td style="text-align: right">96</td>
          <td style="text-align: right">23</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">70</td>
          <td style="text-align: right">24.0</td>
          <td style="text-align: right">3.1</td>
          <td style="text-align: right">72.9</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>667</strong></td>
          <td style="text-align: right"><strong>190</strong></td>
          <td style="text-align: right"><strong>74</strong></td>
          <td style="text-align: right"><strong>403</strong></td>
          <td style="text-align: right"><strong>28.5</strong></td>
          <td style="text-align: right"><strong>11.1</strong></td>
          <td style="text-align: right"><strong>60.4</strong></td>
      </tr>
      <tr>
          <td></td>
          <td><strong>TOTAL (总计)</strong></td>
          <td style="text-align: right"><strong>956</strong></td>
          <td style="text-align: right"><strong>269</strong></td>
          <td style="text-align: right"><strong>99</strong></td>
          <td style="text-align: right"><strong>588</strong></td>
          <td style="text-align: right"><strong>28.1</strong></td>
          <td style="text-align: right"><strong>10.4</strong></td>
          <td style="text-align: right"><strong>61.5</strong></td>
      </tr>
  </tbody>
</table>
<p><strong>图 14 | 一项任务的示例输出，该任务要求对比纳斯达克（NASDAQ）的两种定投策略。</strong></p>
<p><strong>图 15 | 一项任务的示例输出，该任务要求研究 2020-2025 年诺贝尔科学奖并生成分析性 PDF 报告。</strong></p>
<p><strong>表 12 | DeepSeek-V4-Pro 与 Gemini-3.1-Pro 在中文功能性写作上的对比分析。</strong></p>
<table>
  <thead>
      <tr>
          <th></th>
          <th></th>
          <th style="text-align: right"></th>
          <th style="text-align: right">内部综合评估 (Internal Evaluation)</th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
          <th style="text-align: right"></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>类别</strong></td>
          <td><strong>子类别</strong></td>
          <td style="text-align: right"><strong>#</strong></td>
          <td style="text-align: right"><strong>DS 胜出</strong></td>
          <td style="text-align: right"><strong>Gem 胜出</strong></td>
          <td style="text-align: right"><strong>平局</strong></td>
          <td style="text-align: right"><strong>DS%</strong></td>
          <td style="text-align: right"><strong>Gem%</strong></td>
          <td style="text-align: right"><strong>平局%</strong></td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Report (报告)</td>
          <td style="text-align: right">527</td>
          <td style="text-align: right">350</td>
          <td style="text-align: right">162</td>
          <td style="text-align: right">15</td>
          <td style="text-align: right">66.41</td>
          <td style="text-align: right">30.74</td>
          <td style="text-align: right">2.85</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Proposal (方案策划)</td>
          <td style="text-align: right">291</td>
          <td style="text-align: right">181</td>
          <td style="text-align: right">103</td>
          <td style="text-align: right">7</td>
          <td style="text-align: right">62.20</td>
          <td style="text-align: right">35.40</td>
          <td style="text-align: right">2.41</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Education (教育培训)</td>
          <td style="text-align: right">159</td>
          <td style="text-align: right">100</td>
          <td style="text-align: right">56</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">62.89</td>
          <td style="text-align: right">35.22</td>
          <td style="text-align: right">1.89</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Email &amp; Letter (邮件书信)</td>
          <td style="text-align: right">146</td>
          <td style="text-align: right">107</td>
          <td style="text-align: right">37</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">73.29</td>
          <td style="text-align: right">25.34</td>
          <td style="text-align: right">1.37</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Notice (通知公告)</td>
          <td style="text-align: right">72</td>
          <td style="text-align: right">43</td>
          <td style="text-align: right">24</td>
          <td style="text-align: right">5</td>
          <td style="text-align: right">59.72</td>
          <td style="text-align: right">33.33</td>
          <td style="text-align: right">6.94</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Professional (专业文本)</td>
          <td style="text-align: right">63</td>
          <td style="text-align: right">34</td>
          <td style="text-align: right">27</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">53.97</td>
          <td style="text-align: right">42.86</td>
          <td style="text-align: right">3.17</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Recruitment (招聘求职)</td>
          <td style="text-align: right">42</td>
          <td style="text-align: right">27</td>
          <td style="text-align: right">15</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">64.29</td>
          <td style="text-align: right">35.71</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Technical (技术文本)</td>
          <td style="text-align: right">29</td>
          <td style="text-align: right">22</td>
          <td style="text-align: right">7</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">75.86</td>
          <td style="text-align: right">24.14</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td>Business Writing (办公文本)</td>
          <td>Review (介绍评价)</td>
          <td style="text-align: right">20</td>
          <td style="text-align: right">15</td>
          <td style="text-align: right">5</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">75.00</td>
          <td style="text-align: right">25.00</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>1349</strong></td>
          <td style="text-align: right"><strong>879</strong></td>
          <td style="text-align: right"><strong>436</strong></td>
          <td style="text-align: right"><strong>34</strong></td>
          <td style="text-align: right"><strong>65.16</strong></td>
          <td style="text-align: right"><strong>32.32</strong></td>
          <td style="text-align: right"><strong>2.52</strong></td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Social Media (社交媒体文案)</td>
          <td style="text-align: right">267</td>
          <td style="text-align: right">156</td>
          <td style="text-align: right">101</td>
          <td style="text-align: right">10</td>
          <td style="text-align: right">58.43</td>
          <td style="text-align: right">37.83</td>
          <td style="text-align: right">3.75</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Ad Copy (广告商品文案)</td>
          <td style="text-align: right">214</td>
          <td style="text-align: right">109</td>
          <td style="text-align: right">98</td>
          <td style="text-align: right">7</td>
          <td style="text-align: right">50.93</td>
          <td style="text-align: right">45.79</td>
          <td style="text-align: right">3.27</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Long-form Content (内容平台长文)</td>
          <td style="text-align: right">99</td>
          <td style="text-align: right">71</td>
          <td style="text-align: right">25</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">71.72</td>
          <td style="text-align: right">25.25</td>
          <td style="text-align: right">3.03</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>News Report (新闻报道)</td>
          <td style="text-align: right">51</td>
          <td style="text-align: right">27</td>
          <td style="text-align: right">22</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">52.94</td>
          <td style="text-align: right">43.14</td>
          <td style="text-align: right">3.92</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Advertorial (营销软文)</td>
          <td style="text-align: right">17</td>
          <td style="text-align: right">12</td>
          <td style="text-align: right">4</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">70.59</td>
          <td style="text-align: right">23.53</td>
          <td style="text-align: right">5.88</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Headline (标题)</td>
          <td style="text-align: right">11</td>
          <td style="text-align: right">7</td>
          <td style="text-align: right">4</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">63.64</td>
          <td style="text-align: right">36.36</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Narration Script (口播文案)</td>
          <td style="text-align: right">4</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">50.00</td>
          <td style="text-align: right">25.00</td>
          <td style="text-align: right">25.00</td>
      </tr>
      <tr>
          <td>Media Writing (媒体文本)</td>
          <td>Comment (评论)</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">66.67</td>
          <td style="text-align: right">33.33</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>666</strong></td>
          <td style="text-align: right"><strong>386</strong></td>
          <td style="text-align: right"><strong>256</strong></td>
          <td style="text-align: right"><strong>24</strong></td>
          <td style="text-align: right"><strong>57.96</strong></td>
          <td style="text-align: right"><strong>38.44</strong></td>
          <td style="text-align: right"><strong>3.60</strong></td>
      </tr>
      <tr>
          <td>Everyday Writing (生活文本)</td>
          <td>Congratulatory (祝贺文本)</td>
          <td style="text-align: right">101</td>
          <td style="text-align: right">54</td>
          <td style="text-align: right">41</td>
          <td style="text-align: right">6</td>
          <td style="text-align: right">53.47</td>
          <td style="text-align: right">40.59</td>
          <td style="text-align: right">5.94</td>
      </tr>
      <tr>
          <td>Everyday Writing (生活文本)</td>
          <td>Communication (沟通回复)</td>
          <td style="text-align: right">100</td>
          <td style="text-align: right">71</td>
          <td style="text-align: right">26</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">71.00</td>
          <td style="text-align: right">26.00</td>
          <td style="text-align: right">3.00</td>
      </tr>
      <tr>
          <td>Everyday Writing (生活文本)</td>
          <td>Reflection (心得感想)</td>
          <td style="text-align: right">90</td>
          <td style="text-align: right">68</td>
          <td style="text-align: right">17</td>
          <td style="text-align: right">5</td>
          <td style="text-align: right">75.56</td>
          <td style="text-align: right">18.89</td>
          <td style="text-align: right">5.56</td>
      </tr>
      <tr>
          <td>Everyday Writing (生活文本)</td>
          <td>Review (介绍评价)</td>
          <td style="text-align: right">55</td>
          <td style="text-align: right">44</td>
          <td style="text-align: right">9</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">80.00</td>
          <td style="text-align: right">16.36</td>
          <td style="text-align: right">3.64</td>
      </tr>
      <tr>
          <td>Everyday Writing (生活文本)</td>
          <td>Comment (评论)</td>
          <td style="text-align: right">44</td>
          <td style="text-align: right">34</td>
          <td style="text-align: right">8</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">77.27</td>
          <td style="text-align: right">18.18</td>
          <td style="text-align: right">4.55</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>390</strong></td>
          <td style="text-align: right"><strong>271</strong></td>
          <td style="text-align: right"><strong>101</strong></td>
          <td style="text-align: right"><strong>18</strong></td>
          <td style="text-align: right"><strong>69.49</strong></td>
          <td style="text-align: right"><strong>25.90</strong></td>
          <td style="text-align: right"><strong>4.62</strong></td>
      </tr>
      <tr>
          <td>Oral Writing (口头文本)</td>
          <td>Speech (发言稿)</td>
          <td style="text-align: right">226</td>
          <td style="text-align: right">135</td>
          <td style="text-align: right">85</td>
          <td style="text-align: right">6</td>
          <td style="text-align: right">59.73</td>
          <td style="text-align: right">37.61</td>
          <td style="text-align: right">2.65</td>
      </tr>
      <tr>
          <td>Oral Writing (口头文本)</td>
          <td>Narration Script (口播文案)</td>
          <td style="text-align: right">51</td>
          <td style="text-align: right">25</td>
          <td style="text-align: right">23</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">49.02</td>
          <td style="text-align: right">45.10</td>
          <td style="text-align: right">5.88</td>
      </tr>
      <tr>
          <td>Oral Writing (口头文本)</td>
          <td>Sales Script (话术)</td>
          <td style="text-align: right">31</td>
          <td style="text-align: right">22</td>
          <td style="text-align: right">6</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">70.97</td>
          <td style="text-align: right">19.35</td>
          <td style="text-align: right">9.68</td>
      </tr>
      <tr>
          <td>Oral Writing (口头文本)</td>
          <td>Dialogue (对话文本)</td>
          <td style="text-align: right">10</td>
          <td style="text-align: right">4</td>
          <td style="text-align: right">6</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">40.00</td>
          <td style="text-align: right">60.00</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td>Oral Writing (口头文本)</td>
          <td>Congratulatory (祝贺文本)</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">100.00</td>
          <td style="text-align: right">0.00</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>319</strong></td>
          <td style="text-align: right"><strong>187</strong></td>
          <td style="text-align: right"><strong>120</strong></td>
          <td style="text-align: right"><strong>12</strong></td>
          <td style="text-align: right"><strong>58.62</strong></td>
          <td style="text-align: right"><strong>37.62</strong></td>
          <td style="text-align: right"><strong>3.76</strong></td>
      </tr>
      <tr>
          <td>Official Document (公文文档)</td>
          <td>Administrative Doc (事务文书)</td>
          <td style="text-align: right">117</td>
          <td style="text-align: right">60</td>
          <td style="text-align: right">53</td>
          <td style="text-align: right">4</td>
          <td style="text-align: right">51.28</td>
          <td style="text-align: right">45.30</td>
          <td style="text-align: right">3.42</td>
      </tr>
      <tr>
          <td>Official Document (公文文档)</td>
          <td>Personal Doc (个人文书)</td>
          <td style="text-align: right">73</td>
          <td style="text-align: right">45</td>
          <td style="text-align: right">27</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">61.64</td>
          <td style="text-align: right">36.99</td>
          <td style="text-align: right">1.37</td>
      </tr>
      <tr>
          <td>Official Document (公文文档)</td>
          <td>Government Doc (行政公文)</td>
          <td style="text-align: right">34</td>
          <td style="text-align: right">19</td>
          <td style="text-align: right">14</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">55.88</td>
          <td style="text-align: right">41.18</td>
          <td style="text-align: right">2.94</td>
      </tr>
      <tr>
          <td>Official Document (公文文档)</td>
          <td>Speech (发言稿)</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">33.33</td>
          <td style="text-align: right">66.67</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td>Official Document (公文文档)</td>
          <td>Essay Writing (申论写作)</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">33.33</td>
          <td style="text-align: right">33.33</td>
          <td style="text-align: right">33.33</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>230</strong></td>
          <td style="text-align: right"><strong>126</strong></td>
          <td style="text-align: right"><strong>97</strong></td>
          <td style="text-align: right"><strong>7</strong></td>
          <td style="text-align: right"><strong>54.78</strong></td>
          <td style="text-align: right"><strong>42.17</strong></td>
          <td style="text-align: right"><strong>3.04</strong></td>
      </tr>
      <tr>
          <td>Academic Writing (学术文本)</td>
          <td>Research Paper (学术论文)</td>
          <td style="text-align: right">104</td>
          <td style="text-align: right">67</td>
          <td style="text-align: right">32</td>
          <td style="text-align: right">5</td>
          <td style="text-align: right">64.42</td>
          <td style="text-align: right">30.77</td>
          <td style="text-align: right">4.81</td>
      </tr>
      <tr>
          <td>Academic Writing (学术文本)</td>
          <td>Coursework (课程作业)</td>
          <td style="text-align: right">90</td>
          <td style="text-align: right">53</td>
          <td style="text-align: right">35</td>
          <td style="text-align: right">2</td>
          <td style="text-align: right">58.89</td>
          <td style="text-align: right">38.89</td>
          <td style="text-align: right">2.22</td>
      </tr>
      <tr>
          <td>Academic Writing (学术文本)</td>
          <td>Academic Support (学术辅助)</td>
          <td style="text-align: right">15</td>
          <td style="text-align: right">11</td>
          <td style="text-align: right">3</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">73.33</td>
          <td style="text-align: right">20.00</td>
          <td style="text-align: right">6.67</td>
      </tr>
      <tr>
          <td>Academic Writing (学术文本)</td>
          <td>Science Outreach (专业科普)</td>
          <td style="text-align: right">7</td>
          <td style="text-align: right">6</td>
          <td style="text-align: right">1</td>
          <td style="text-align: right">0</td>
          <td style="text-align: right">85.71</td>
          <td style="text-align: right">14.29</td>
          <td style="text-align: right">0.00</td>
      </tr>
      <tr>
          <td></td>
          <td>Subtotal (小计)</td>
          <td style="text-align: right"><strong>216</strong></td>
          <td style="text-align: right"><strong>137</strong></td>
          <td style="text-align: right"><strong>71</strong></td>
          <td style="text-align: right"><strong>8</strong></td>
          <td style="text-align: right"><strong>63.43</strong></td>
          <td style="text-align: right"><strong>32.87</strong></td>
          <td style="text-align: right"><strong>3.70</strong></td>
      </tr>
      <tr>
          <td><strong>Total (总计)</strong></td>
          <td></td>
          <td style="text-align: right"><strong>3170</strong></td>
          <td style="text-align: right"><strong>1986</strong></td>
          <td style="text-align: right"><strong>1081</strong></td>
          <td style="text-align: right"><strong>103</strong></td>
          <td style="text-align: right"><strong>62.65</strong></td>
          <td style="text-align: right"><strong>34.10</strong></td>
          <td style="text-align: right"><strong>3.25</strong></td>
      </tr>
  </tbody>
</table>
]]></content:encoded>
    </item>
  </channel>
</rss>
