LLM distillation
Knowledge distillation 是一种 model compression 技术,通过训练较小的 student model 来复现较大 teacher model 的行为。由此得到的模型更小、更快,执行 inference 的成本也更低。更重要的是,它保留了 teacher model 的大部分能力。
可以用一种简单的方式来理解:
- Teacher model 已经非常擅长解决这项任务
- Student model 通过模仿 teacher model 的输出进行学习
Quantization 会降低现有 weights 的 precision,而 distillation 与之不同,它会生成一个全新的模型。这两种技术可以互补:distilled model 还可以继续进行 quantization,以进一步提升效率。
DeepSeek-R1(671B parameters)很好地展示了 distillation 能够实现什么。研究人员利用它将 reasoning 能力迁移到更小的模型中,包括 1.5B、7B、8B、14B、32B 和 70B 版本。这些模型的 reasoning 风格与 teacher model 相似,但 inference 成本仅为后者的一小部分。
Distillation 如何工作
标准 training 使用 hard label 教导模型,也就是给定输入所对应的正确答案。
Distillation 则使用 teacher model 的 soft label 训练 student model,也就是所有可能 next Token 的完整 probability distribution。
Soft label 包含的信息比单个正确答案更丰富。例如,如果 teacher model 为“car”分配 40% 的概率,为“vehicle”分配 30% 的概率,那么 student model 学到的不仅是答案,还包括哪些替代答案是合理的,以及 teacher model 对判断有多大把握。这有助于 student model 获得更好的 generalization 能力。
训练 student model 时,通常会结合使用:
- Distillation loss:衡量 student model 与 teacher model 的 output distribution 有多接近。
- Cross-entropy loss:在 labeled data 上执行标准的 next-token prediction。
这些训练目标会组合使用,以平衡 knowledge transfer 与任务性能。
Distillation 的类型
Response distillation
训练 student model,使其匹配 teacher model 的 output Token probability。这是最常见的形式,因为它只需要访问 teacher model 的输出。它具有良好的可扩展性,被广泛用于生成更小的通用模型。
Feature distillation
训练 student model,使其模仿 intermediate representation,例如 hidden state、attention pattern 或 layer activation。这需要访问 teacher model 的内部结构,实现成本更高,但对于 architecture 相似的模型,可以获得更好的 knowledge transfer 效果。
Chain-of-thought distillation
Teacher model 会生成完整的 reasoning trace,也就是逐步解决问题的过程,再使用这些 trace 对 student model 进行 fine-tuning。DeepSeek-R1 的 distilled version 正是以这种方式创建的。它显著提升了小型模型的 reasoning 能力,尤其是在数学、编程和逻辑问题求解等任务上。
什么时候使用 distillation
在以下情况下,distillation 是一个不错的选择:
- 你需要一个更小、更快的模型,用于 latency-sensitive 或资源受限的部署环境。
- 大型模型的 quantized version 仍然超出你的 VRAM 或 latency budget。
- 你可以使用性能强大的 teacher model,并且能够承担 training 成本。
- 你希望将特定能力(例如 reasoning 或编程)迁移到更小的模型中。
在以下情况下,distillation 可能并不适合:
- Student model 与 teacher model 之间的能力差距过大,student architecture 无法吸收这些能力。
- 你没有足够的 compute 在 training 期间以 inference 方式运行 teacher model。
- 现有的 quantized model 或 fine-tuned model 已经能满足你的需求。
- 你需要尽可能高的准确率。
并非所有模型都可以进行 distillation。某些许可证禁止使用模型输出来训练其他模型。
Distillation 如何影响 inference
Distillation 发生在 training 期间,但其优势会在 inference 期间体现出来。
通过创建更小的模型,它可以直接提高 serving efficiency:
- 更低的 latency:Token 生成速度更快
- 更低的内存占用:所需 GPU memory 更少
- 更高的 throughput:同一硬件上可以并行处理更多请求
- 更低的成本:每个请求所需的 compute 更少
换句话说,在应用任何 runtime optimization 之前,distillation 就能降低 inference 的基础成本。
可以这样理解:
- Distillation 让模型变得更小
- Quantization 让模型变得更轻量
- Inference optimization(例如 prefix caching)让 serving 更高效
你可以在 inference system 中组合使用这些技术:大型模型 → Distill → 小型模型 → Quantize(如果需要)→ 优化 serving → 部署
FAQ
Distillation 面临哪些主要挑战?
Distillation 可能非常有效,但也存在以下权衡:
- 需要额外的 training compute
- 依赖 teacher model 的质量
- 在复杂任务上可能出现性能损失
- 需要谨慎执行 data generation 和数据过滤
因此,许多团队会从 quantization 开始,只在需要时才使用 distillation。
Distillation 和 quantization 有什么区别?
下面是两者的并列对比:
| Distillation | Quantization | |
|---|---|---|
| 改变的内容 | 训练一个更小的新模型 | 降低现有模型的 precision |
| 输出 | 不同的模型,即 student model | 同一个模型,但 weights 的 precision 更低 |
| 发生时间 | Training 期间 | Training 之后 |
| 所需 compute | 高,需要进行 training | 低,可以快速应用 |
| 对大小的影响 | 减少 parameter 数量 | 减少每个 parameter 占用的内存 |
| 易用性 | 更复杂 | 更容易应用 |
| 典型使用场景 | 构建更小的生产模型 | 优化现有模型以便部署 |