链接: https://x.com/rasbt/status/2111448141119856866 分类: x-tips 来源: X 账号 @rasbt(Sebastian Raschka, PhD) 作者: Jay 更新日期: 2026-10-21 仓库: https://github.com/rasbt/reasoning-from-scratch 主仓库: https://sebastianraschka.com/reasoning-from-scratch
推理解码模型蒸馏实战:从 RL 到高效小模型
这是什么
Sebastian Raschka(@rasbt)在其 "Build A Reasoning Model From Scratch" 视频系列第 8 集中,系统讲解了如何用蒸馏(Distillation)替代强化学习(RL)来训练小型推理模型。这是该系列的最终集,时长约 1 小时 20 分钟。
核心思路:用大模型(teacher)生成高质量推理轨迹(reasoning traces),以此数据训练小模型(student),比跑 RL 训练更高效。蒸馏代码位于 GitHub 仓库 rasbt/reasoning-from-scratch 的 ch08 目录。
核验来源
| 维度 | 内容 |
|---|---|
| 原帖 | https://x.com/rasbt/status/2111448141119856866 |
| YouTube 视频 | Build A Reasoning Model From Scratch 8: Distillation(2026-10-17 发布) |
| 配套书籍 | Build A Reasoning Model (From Scratch), Manning, 2025, ISBN 9781633434677 |
| 代码仓库 | https://github.com/rasbt/reasoning-from-scratch/tree/main/ch08 |
| 书籍主页 | https://sebastianraschka.com/reasoning-from-scratch |
关于 teacher/student 模型名称和 benchmark 数字的说明:原帖正文未提及 DeepSeek R1、Qwen3-0.6B 或 MATH-500 准确率等具体数字;这些信息来自 YouTube 视频描述和 GitHub README,不属于本条原帖覆盖范围,下文仅引用有出处的内容。
视频章节结构(原帖正文记录)
| 时间戳 | 主题 |
|---|---|
| 00:00 | 开场与系列回顾 |
| 01:07 | 为什么用蒸馏训练推理模型? |
| 02:51 | Soft distillation vs. hard distillation |
| 11:06 | On-policy vs. off-policy 蒸馏 |
| 18:09 | 准备推理蒸馏数据 |
| 22:35 | 加载 DeepSeek R1 MATH 数据集 |
| 28:04 | 检查推理轨迹与最终答案 |
| 31:27 | Tokenization 与 chat templates |
| 35:58 | 预分词训练数据集 |
| 39:01 | 序列长度与 2,048 token 限制 |
| 42:38 | 创建训练/验证 split |
| 45:32 | 加载 Qwen3-0.6B 基础模型 |
| 46:24 | 计算 answer-token log probabilities |
| 54:10 | 蒸馏的交叉熵损失函数 |
| 57:25 | 训练/验证损失辅助函数 |
| 1:02:03 | 实现蒸馏训练循环 |
| 1:07:26 | 运行独立训练脚本 |
| 1:09:41 | 绘制训练/验证 loss 曲线 |
| 1:11:31 | 在 MATH-500 上评估 checkpoint |
| 1:14:09 | Teacher 与蒸馏结果对比 |
| 1:16:34 | 推理模型的未来方向 |
| 1:19:41 | bonus 练习与后续步骤 |
核心概念解析
为什么蒸馏比 RL 更高效
RL 训练推理模型(如 GRPO)需要反复采样、奖励评估和策略更新,每次迭代的计算成本很高。蒸馏把这一过程拆成两步: 1. 生成阶段:用强大的 teacher 模型一次性生成大量推理轨迹数据(只需一次推理开销)。 2. 训练阶段:用这些轨迹数据做监督学习(supervised learning),用普通梯度下降训练 student 模型。
Soft vs. Hard Distillation
- Hard distillation:用 teacher 生成的 hard 标签(最终答案)做标准交叉熵训练。
- Soft distillation:利用 teacher 输出的 token-level log probabilities( logits),结合 KL 散度或带温度的 softmax 让学生学习更丰富的分布信息,比 hard 标签包含更多"暗知识"。
视频中 46:24 章节标题为"Computing answer-token log probabilities",说明该实现采用 soft 蒸馏路线,在 answer token 上计算 teacher 的 log probabilities。
On-policy vs. Off-policy
- Off-policy:用 teacher 一次生成所有数据(提前准备好数据池),student 在此固定数据集上训练,无需实时调用 teacher。
- On-policy:student 每轮训练后重新用自己采样,teacher 的分布随 student 能力变化而更新,训练更动态但计算开销更大。
上手步骤
环境准备
-
安装 uv(项目推荐的工具):
bash curl -LsSf https://astral.sh/uv/install.sh | sh -
克隆仓库并安装依赖:
bash git clone https://github.com/rasbt/reasoning-from-scratch.git cd reasoning-from-scratch uv sync
整体 pipeline
蒸馏工作流分三个阶段,对应仓库三个子目录:
ch08/
├── 02_generate_distillation_data/ # 阶段1:生成 teacher 输出
├── 04_train_with_distillation/ # 阶段2:蒸馏训练
└── 05_download_training_checkpoints/ # 阶段3:下载已训练好的 checkpoint
阶段1:生成蒸馏数据
两种生成方式,见 ch08/02_generate_distillation_data/README.md:
方式A - 本地 Ollama(适合 Qwen3 4B、DeepSeek R1 32B 等可本地运行的模型):
uv run generate_with_ollama.py \
--math_json math_train_sample.json \
--output_path teacher_outputs.json
方式B - OpenRouter API(适合 DeepSeek R1 671B、Kimi K2.5 等超大模型):
uv run generate_with_openrouter.py \
--math_json math_train_sample.json \
--output_path teacher_outputs.json \
--api_key $OPENROUTER_API_KEY
输出格式为 JSON,每条记录包含:
{
"problem": "Compute 1/2 + 1/6.",
"gtruth_answer": "2/3",
"message_thinking": "I will rewrite the fractions with a common denominator.",
"message_content": "The final answer is \\boxed{\\tfrac{2}{3}}."
}
其中 message_thinking 是推理过程(可作为 <think> 内容),message_content 是最终答案(作为监督目标)。
阶段2:蒸馏训练
推荐使用 ch08/04_train_with_distillation/distill.py。数据需满足:
- 字段
problem:数学问题,插入 math prompt template - 字段
message_content:必需,作为监督目标答案 - 字段
message_thinking:可选,若存在则在message_content前拼入<think>区块 - 超过
--max_seq_len的样本会被自动过滤,再做 train/validation split
最小化试跑(用 5 条样本验证流程):
uv run distill.py \
--data_path ../02_generate_distillation_data/sample_openrouter_outputs.json \
--dataset_size 5 \
--validation_size 1 \
--epochs 2 \
--log_every 1
正式训练(需 GPU,Qwen3-0.6B 约需 6GB+ VRAM):
uv run distill.py \
--data_path /path/to/teacher_outputs.json \
--max_seq_len 2048 \
--epochs 3
脚本行为:
- 从 Qwen3-0.6B 基础权重初始化
- 每 epoch 结束后在 checkpoints/distill/ 保存 checkpoint
- 向 logs/distill_metrics.csv 追加训练指标
另有 distill_batched.py 支持多样本批处理(需更大显存),checkpoint 目录为 checkpoints/distill_batched/。
阶段3:使用 checkpoint
ch08/05_download_training_checkpoints/ 提供了下载预训练 checkpoint 的说明。ch08/06_use_via_huggingface/ 说明如何通过 Hugging Face 加载模型和 checkpoint。
适用边界
适合的场景: - 有充足 GPU 资源(约 6GB+ VRAM 跑 Qwen3-0.6B) - 需要把大模型推理能力迁移到可部署的小模型 - 已有高质量 teacher 模型(本地 Ollama 或 OpenRouter API)
不适合的场景: - 无法访问 GPU 或显存不足(可用 Ollama 本地跑小模型做 teacher,但 student 训练仍需 GPU) - 追求最高推理能力(蒸馏的 student 通常弱于 teacher,直接用 teacher 推理效果更好) - 数据质量无法保证(teacher 输出质量直接决定 student 上限)
已知限制:
- 原帖正文未提及具体 benchmark 数字,无法验证 student 模型相对 teacher 的精度损失比例。
- 视频中提到的 MATH-500 评估结果需查看 GitHub ch08/03_logs 目录的日志文件,本条原帖未提供。
- 2,048 token 序列长度限制来自 Qwen3-0.6B 架构配置,超长推理轨迹会被截断或过滤。
关键参考
- 原帖:https://x.com/rasbt/status/2111448141119856866
- 视频:https://www.youtube.com/watch?v=msp69FR5NZs
- GitHub ch08:https://github.com/rasbt/reasoning-from-scratch/tree/main/ch08
- 书籍主页:https://sebastianraschka.com/reasoning-from-scratch
- 书籍 Amazon:https://amzn.to/4aAKiFY