# 模型蒸馏

模型蒸馏是一种训练技术：训练一个小型“学生”模型去复现一个更大的“教师”模型或集成模型的输出，使学生以一小部分规模和推理成本接近教师的精度。教师完整的类别概率分布，而不只是其概率最高的预测，作为训练信号；在手机、无人机和边缘服务器上部署，要求模型的前向传播开销低廉。

| 关键事实 | 值 |
| --- | --- |
| 训练信号 | 教师经温度软化的类别概率作为软目标，可选地还包括隐藏特征与样本间关系 |
| 组合损失 | \( \mathcal{L}_{\mathrm{KD}} = (1-\lambda)\,H(y,q) + \lambda\,H(\tilde{p},\tilde{q}) \)，高温下的软目标项，\( \tau=1 \) 时的硬标签项 |
| 温度效应 | logits [5.0, 2.0, 0.5] 在 \( \tau=1 \) 时约为 [0.94, 0.05, 0.01]，在 \( \tau=2 \) 时为 [0.75, 0.17, 0.08]，在 \( \tau=4 \) 时为 [0.56, 0.26, 0.18] |
| 典型保留率 | 在 GLUE、SuperGLUE 和 MMLU 上，学生通常保留教师 95% 以上的性能 |
| DistilBERT 结果 | 6600 万对比 1.1 亿参数（小 40%），达到 BERT 语言理解能力的 97%，推理快 60% |
| 主要失效模式 | 容量差距：大得多的教师可能监督得更差；中间教师—助教模型可缓解这一问题 |

## 工作原理

核心机制是软目标匹配。教师的 logits \( Z_i \) 经过温度缩放的 softmax，

\[ p(Z_i, \tau) = \frac{\exp(Z_i/\tau)}{\sum_i \exp(Z_i/\tau)} \]

学生被训练去匹配这一分布，通常通过最小化教师分布与学生分布之间的 KL 散度（衡量一个概率分布与另一个概率分布差异的统计量）。<sup>[1](https://arxiv.org/pdf/2503.12067v2.pdf)</sup> 将温度 \( \tau \) 提高到 1 以上会使分布变平：对于 logits [5.0, 2.0, 0.5]，概率从 \( \tau=1 \) 时约 [0.94, 0.05, 0.01] 变为 \( \tau=2 \) 时的 [0.75, 0.17, 0.08]，以及 \( \tau=4 \) 时的 [0.56, 0.26, 0.18]，从而暴露出教师如何对错误答案排序。<sup>[2](http://llmbook.icsgen-ai.org/part-4-training-adaptation/module-17-peft/section-17.5.html)</sup> [杰弗里·辛顿](https://www.edgepedia.org/zh/geoffrey-hinton)（Geoffrey Hinton）、奥里奥尔·维尼亚尔斯（Oriol Vinyals）和[杰夫·迪恩](https://www.edgepedia.org/zh/jeff-dean)（Jeff Dean）将这种次级信息称为“暗知识”，并认为高熵软目标每个训练样本携带的信息远多于硬标签，且梯度方差小得多，因此学生可以用更少的数据、更高的学习率训练。<sup>[3](https://doi.org/10.48550/arxiv.1503.02531)</sup>

标准目标组合两个交叉熵项，

\[ \mathcal{L}_{\mathrm{KD}} = (1-\lambda)\,H(y,q) + \lambda\,H(\tilde{p},\tilde{q}) \]

其中 \( y \) 是硬标签，\( q \) 是学生的普通预测，\( \tilde{p} \)、\( \tilde{q} \) 是经温度软化的教师和学生预测，\( \lambda \in [0,1] \)。<sup>[4](https://arxiv.org/pdf/2002.03532v2.pdf)</sup> 由于软目标梯度按 \( 1/\tau^{2} \) 缩放，当两项同时使用时，它们要乘以 \( \tau^{2} \)。<sup>[3](https://doi.org/10.48550/arxiv.1503.02531)</sup> 在零均值 logits 的高温极限下，蒸馏退化为最小化 \( \tfrac{1}{2}(z_i - v_i)^2 \)，因此 logit 匹配是一种特例。<sup>[3](https://doi.org/10.48550/arxiv.1503.02531)</sup> 当 \( \tau=1 \) 且教师分布为均匀分布时，蒸馏损失的梯度等于标签平滑的梯度，因此 KD 起到自适应标签平滑的作用，按教师对正确类别的置信度重新缩放梯度。<sup>[4](https://arxiv.org/pdf/2002.03532v2.pdf)</sup>

## 如何实施

实践者工作流有五步。第一，训练或选择一个强教师；PyTorch 教程将目标表述为：在不改变学生前向传播成本的前提下，在普通交叉熵之上加入蒸馏损失，以便部署到无人机或手机等硬件上。<sup>[5](https://docs.pytorch.org/tutorials/beginner/knowledge_distillation_tutorial.html)</sup> 第二，选择并初始化学生：[DistilBERT](https://www.edgepedia.org/zh/distilbert) 将层数减半，去掉 token 类型嵌入和池化器，并通过从每两层中取一层来从教师初始化；在固定参数预算下，减半层数比缩小隐藏维度带来更高效率。<sup>[6](https://arxiv.org/abs/1910.01108)</sup> 第三，设置温度和损失权重。辛顿、维尼亚尔斯和迪恩尝试了从 1 到 20 的温度，并指出当学生非常小时，较低温度效果更好。<sup>[7](https://intellabs.github.io/distiller/knowledge_distillation.html)</sup> 一项在温度 {1, 2, 4, 8} 上的基准发现，温度高于 2 在小型下游数据集上更好，较低温度在较大数据集上更好，而较小的硬标签权重（0.1 或 0.2）总是有帮助。<sup>[8](https://arxiv.org/pdf/2201.00558)</sup> 第四，训练；DistilBERT 使用三重损失（掩码语言建模、温度缩放蒸馏和余弦距离），其中两个蒸馏损失贡献了大部分性能。<sup>[6](https://arxiv.org/abs/1910.01108)</sup> 第五，在目标指标和延迟预算上对照教师进行评估。

## 起源

将集成模型压缩为更小模型的一般思想，早先由克里斯蒂安·布奇卢（Cristian Buciluǎ）、里奇·卡鲁阿纳（Rich Caruana）和亚历山德鲁·尼古列斯库-米齐尔（Alexandru Niculescu-Mizil）在 KDD 2006 上描述为“模型压缩”，<sup>[9](https://dl.acm.org/doi/10.1145/1150402.1150464)</sup> 后由辛顿、维尼亚尔斯和迪恩在 2015 年发表于 arXiv 的论文《在神经网络中蒸馏知识》（Distilling the Knowledge in a Neural Network）中表述并推广为软目标蒸馏。<sup>[3](https://doi.org/10.48550/arxiv.1503.02531)</sup> 将集成知识迁移到单个小模型可以通过匹配 logits 完成。<sup>[3](https://doi.org/10.48550/arxiv.1503.02531)</sup> 综述记载，“模型压缩”将来自大模型或集成的信息迁移到小模型的训练中，而不会造成显著精度损失，而辛顿、维尼亚尔斯和迪恩后来将其推广为知识蒸馏。<sup>[10](https://arxiv.org/abs/2006.05525)</sup> 后来的理论工作包括将蒸馏与万普尼克（Vapnik）的利用特权信息学习统一为“广义蒸馏”，<sup>[11](https://arxiv.org/pdf/1511.03643)</sup> 以及恩里克·博伊克斯-阿德塞拉（Enric Boix-Adserà）在 2024 年 arXiv 上的结果，证明蒸馏可以比从头学习便宜得多。<sup>[12](https://doi.org/10.48550/arxiv.2403.09053)</sup>

## 变体

综述按迁移内容和训练调度来组织这一领域。**知识类型。** 基于响应的蒸馏匹配最终输出或 logits；基于特征的蒸馏匹配中间表示，逐层对齐特征图；基于关系的蒸馏迁移样本之间的关系，如朴元杓（Wonpyo Park）、金东柱（Dongju Kim）、卢岩（Yan Lu）和赵敏秀（Minsu Cho）的《关系知识蒸馏》论文。<sup>[10](https://arxiv.org/abs/2006.05525)</sup><sup> • </sup><sup>[13](https://arxiv.org/pdf/2306.10687)</sup><sup> • </sup><sup>[14](https://doi.org/10.48550/arxiv.1904.05068)</sup> **调度。** 离线蒸馏使用冻结教师；在线蒸馏同时训练教师和学生；自蒸馏让同一网络同时扮演两种角色。<sup>[10](https://arxiv.org/abs/2006.05525)</sup><sup> • </sup><sup>[15](https://doi.org/10.48550/arxiv.2002.03936)</sup> **任务特定设计。** 焦小奇（Xiaoqi Jiao）及其同事的 TinyBERT 分两阶段蒸馏：预训练时的通用蒸馏和微调时的任务特定蒸馏，损失作用于嵌入层、隐藏状态、注意力矩阵和预测 logits。<sup>[16](https://doi.org/10.48550/arxiv.1909.10351)</sup> 来自金允（Yoon Kim）和亚历山大·M·拉什（Alexander M. Rush）2016 年 arXiv 论文的[序列级知识蒸馏](https://www.edgepedia.org/zh/sequence-level-knowledge-distillation)，在序列级迁移机器翻译行为。<sup>[17](https://doi.org/10.48550/arxiv.1606.07947)</sup> 思维链蒸馏用教师推理过程训练学生。<sup>[18](https://arxiv.org/abs/2306.14050)</sup> 另一条路线是王同舟（Tongzhou Wang）、朱俊彦（Jun-Yan Zhu）、安东尼奥·托拉尔巴（Antonio Torralba）和[阿列克谢·A·叶夫罗斯](https://www.edgepedia.org/zh/alexei-a-efros)（Alexei A. Efros）2018 年双层元学习论文中的数据集蒸馏，它压缩数据集而非模型。<sup>[19](https://doi.org/10.48550/arxiv.1811.10959)</sup> 迟志豪（Zhihao Chi）及其同事的 NormKD 为每个样本定制温度。<sup>[20](https://doi.org/10.48550/arxiv.2308.00520)</sup> 对于生成式 LLM，MiniLLM 将前向 KL 替换为反向 KLD，用于 1.2 亿到 130 亿参数的学生；而同策略蒸馏作为 GKD 建立的框架，让学生生成自己的序列，教师给出逐 token 反馈，将累积误差从 \( O(\epsilon \cdot T^{2}) \) 降至 \( O(\epsilon \cdot T) \)。<sup>[21](https://export.arxiv.org/pdf/2306.08543)</sup><sup> • </sup><sup>[22](https://arxiv.org/html/2604.00626)</sup>

## 应用

**语音。** 在原始论文的 Android 语音搜索实验中，一个 8500 万参数的声学模型用 3% 的数据以硬目标训练时严重过拟合，而软目标无需早停便收敛到 57%，比全数据结果低约 2%；一个 10 模型集成的改进有超过 80% 被迁移。<sup>[3](https://doi.org/10.48550/arxiv.1503.02531)</sup> **NLP。** DistilBERT 比 BERT-base 快 60%，达到其语言理解能力的 97%，该模型家族还包括 DistilGPT2、DistilRoBERTa 和 DistilmBERT。<sup>[6](https://arxiv.org/abs/1910.01108)</sup><sup> • </sup><sup>[23](https://github.com/huggingface/transformers-research-projects/blob/main/distillation/README.md)</sup> 将 BERT 蒸馏到 BiLSTM 得到的模型规模为 BERT 的 3%，在 CPU 上运行快 22 倍；TinyBERT 在 BERT-base GLUE 的 96.8% 以上性能下提供 7.5–9.4 倍更快的推理。<sup>[8](https://arxiv.org/pdf/2201.00558)</sup><sup> • </sup><sup>[24](https://dl.acm.org/doi/10.1145/3699518)</sup> **视觉。** 最佳蒸馏 ResNet-20 的参数量和 FLOPs 仅为其 ResNet-56 教师的三分之一，性能下降 0.1%。<sup>[13](https://arxiv.org/pdf/2306.10687)</sup> **LLM。** [DeepSeek-R1](https://www.edgepedia.org/zh/deepseek-r1) 将一个 6710 亿参数的专家混合教师蒸馏为 1.5B 到 70B 参数的稠密学生，同时保留长思维链推理，且 DeepSeek-R1-Distill-Qwen-32B 在数学、代码和科学问答上优于 OpenAI-o1-mini。<sup>[22](https://arxiv.org/html/2604.00626)</sup><sup> • </sup><sup>[25](https://arxiv.org/pdf/2602.12172)</sup>

## 局限与替代方案

**容量与保真度限制。** 更大的模型并不总是更好的教师：容量差距可能产生不利监督，教师—助教方案通过插入一个中间模型来缓解。<sup>[10](https://arxiv.org/abs/2006.05525)</sup><sup> • </sup><sup>[13](https://arxiv.org/pdf/2306.10687)</sup> 一项覆盖 18 项研究的元分析报告了幂律蒸馏差距，\( \mathrm{DED}(r) \propto r^{-\beta} \)，其中 \( \beta \approx 0.38 \pm 0.06 \)，因此规模低于教师 6% 的语言模型学生通常会损失超过 20% 的相对性能。<sup>[26](https://coale.science/storage/pdfs/642539f6-ad93-4ce4-a79d-9650c3a9db35.pdf)</sup> **教师错误会传播。** 学生继承教师超出准确率的属性，包括决策边界相似性、对抗脆弱性和不变性；更小、准确率更低的教师往往对学生更好。<sup>[27](https://papers.nips.cc/paper_files/paper/2023/file/2433fec2144ccf5fea1c9c5ebdbc3924-Paper-Conference.pdf)</sup> 对于生成式 LLM，前向 KL 最小化会导致模式覆盖，把概率质量分配给低概率的教师 token，并可能产生幻觉；反向 KL 是模式寻求的，会以多样性为代价减少低质量输出，前向 KL 适合机器翻译，反向 KL 适合对话和指令微调。<sup>[28](https://arxiv.org/html/2402.13116)</sup> 在某些任务上，教师在超过一半的情况下给错误答案轨迹分配的概率高于正确答案轨迹。<sup>[29](https://aclanthology.org/2026.acl-long.908.pdf)</sup> 丹·巴斯布里奇（Dan Busbridge）及其同事的蒸馏缩放定律表明，学生损失随教师质量、学生规模和数据量呈幂律变化，并存在 U 形容量区间，教师能力过强会降低效率。<sup>[30](https://doi.org/10.48550/arxiv.2502.08606)</sup> **替代方案。** 与剪枝（移除参数）和低秩分解（矩阵分解）不同，蒸馏不直接变换教师权重，而是用教师输出或表示作为单独选择并训练的学生模型的训练信号；量化和剪枝直接改变权重，当教师参数不可访问时更难应用。<sup>[1](https://arxiv.org/pdf/2503.12067v2.pdf)</sup><sup> • </sup><sup>[28](https://arxiv.org/html/2402.13116)</sup> 这些方法可以很好地结合：已发表的工作将蒸馏与量化、蒸馏与剪枝配对。<sup>[7](https://intellabs.github.io/distiller/knowledge_distillation.html)</sup> 直接对比的基准确实存在：COLING 2022 的一项研究在六种 BERT 规模和八个 GLUE 任务上比较了量化感知训练、知识蒸馏和幅度剪枝，发现量化和蒸馏始终优于剪枝，<sup>[31](https://aclanthology.org/2022.coling-1.252.pdf)</sup> 此外还有后来的统一评估，如《剪枝与量化：哪个更好？》（NeurIPS 2023）和 UniComp。由于分词器不匹配和封闭权重阻碍了从 GPT-4 和 OpenAI o1 等专有模型进行 logit 迁移，用教师生成的合成数据训练成为 LLM [蒸馏](https://www.edgepedia.org/zh/distillation)的实际默认做法。<sup>[25](https://arxiv.org/pdf/2602.12172)</sup>

## References

1. [A Comprehensive Survey on Knowledge Distillation](https://arxiv.org/pdf/2503.12067v2.pdf)
2. [Section 17.5: Knowledge Distillation: Foundations & Pipelines (LLM textbook)](http://llmbook.icsgen-ai.org/part-4-training-adaptation/module-17-peft/section-17.5.html)
3. [Hinton, Geoffrey, Vinyals, Oriol, Dean, Jeff (2015). Distilling the Knowledge in a Neural Network. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1503.02531)
4. [Understanding and Improving Knowledge Distillation](https://arxiv.org/pdf/2002.03532v2.pdf)
5. [Knowledge Distillation Tutorial (PyTorch official documentation)](https://docs.pytorch.org/tutorials/beginner/knowledge_distillation_tutorial.html)
6. [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108)
7. [Knowledge Distillation - Neural Network Distiller (Intel Labs documentation)](https://intellabs.github.io/distiller/knowledge_distillation.html)
8. [Knowledge Distillation of Transformer-based Language Models Revisited](https://arxiv.org/pdf/2201.00558)
9. [Model compression](https://dl.acm.org/doi/10.1145/1150402.1150464)
10. [Knowledge Distillation: A Survey (Gou et al.)](https://arxiv.org/abs/2006.05525)
11. [Unifying Distillation and Privileged Information (Lopez-Paz et al.)](https://arxiv.org/pdf/1511.03643)
12. [Boix-Adsera, Enric (2024). Towards a theory of model distillation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2403.09053)
13. [Knowledge Distillation from A Stronger Teacher (survey with empirical comparison)](https://arxiv.org/pdf/2306.10687)
14. [Park, Wonpyo and colleagues (2019). Relational Knowledge Distillation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1904.05068)
15. [Müller, Rafael, Kornblith, Simon, Hinton, Geoffrey (2020). Subclass Distillation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2002.03936)
16. [Jiao, Xiaoqi and colleagues (2019). TinyBERT: Distilling BERT for Natural Language Understanding. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1909.10351)
17. [Kim, Yoon, Rush, Alexander M. (2016). Sequence-Level Knowledge Distillation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1606.07947)
18. [Symbolic Chain-of-Thought Distillation: Small Models Can Also 'Think' Step-by-Step](https://arxiv.org/abs/2306.14050)
19. [Wang, Tongzhou and colleagues (2018). Dataset Distillation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1811.10959)
20. [Chi, Zhihao and colleagues (2023). NormKD: Normalized Logits for Knowledge Distillation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2308.00520)
21. [MiniLLM: Knowledge Distillation of Large Language Models](https://export.arxiv.org/pdf/2306.08543)
22. [A Survey of On-Policy Distillation for Large Language Models](https://arxiv.org/html/2604.00626)
23. [Hugging Face distillation README (Distil* model family)](https://github.com/huggingface/transformers-research-projects/blob/main/distillation/README.md)
24. [Survey on Knowledge Distillation for Large Language Models (ACM TIST)](https://dl.acm.org/doi/10.1145/3699518)
25. [Pedagogically-inspired black-box distillation framework (Identifier–Organizer–Adapter pipeline)](https://arxiv.org/pdf/2602.12172)
26. [The Distillation Gap Is a Power Law: Capacity Ratios, Scaling, and the Limits of Knowledge Transfer](https://coale.science/storage/pdfs/642539f6-ad93-4ce4-a79d-9650c3a9db35.pdf)
27. [What Knowledge Gets Distilled in Knowledge Distillation?](https://papers.nips.cc/paper_files/paper/2023/file/2433fec2144ccf5fea1c9c5ebdbc3924-Paper-Conference.pdf)
28. [A Survey on Knowledge Distillation of Large Language Models](https://arxiv.org/html/2402.13116)
29. [Distillation Traps and Guards: A Calibration Knob for LLM Distillability](https://aclanthology.org/2026.acl-long.908.pdf)
30. [Busbridge, Dan and colleagues (2025). Distillation Scaling Laws. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2502.08606)
31. [Combining Compressions for Multiplicative Size Scaling on Natural Language Tasks](https://aclanthology.org/2022.coling-1.252.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods › Ensemble, boosting, and transfer methods › Transfer learning and domain adaptation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/model-distillation

English original: https://www.edgepedia.org/model-distillation
