link: https://x.com/rasbt/status/2111448141778256161 category: x-tips source: X @rasbt author: Jay date: 2026-10-18 repository: github.com/rasbt/reasoning-from-scratch title: "用蒸馏把大推理模型能力迁移到小模型:Sebastian Raschka 第8章实战"
这是什么
Sebastian Raschka(@rasbt)在 2026 年 10 月 5 日发布油管视频 Build A Reasoning Model From Scratch 8: Distillation(约 79 分钟),是《Build a Reasoning Model (From Scratch)》一书的配套视频第 8 章。
核心理念:用知识蒸馏(Knowledge Distillation)替代强化学习(RL),把 DeepSeek R3 等大推理模型的能力迁移到 Qwen3-0.6B 这样的小模型。训练更稳定、不需要奖励函数设计,效果比 RL 更高效。
原帖:https://x.com/rasbt/status/2111448141778256161
视频:https://www.youtube.com/watch?v=msp69FR5NZs
仓库:github.com/rasbt/reasoning-from-scratch → ch08/
书籍:manning.com/books/build-a-reasoning-model-from-scratch Chapter 8
核验来源
| 项目 | 值 |
|---|---|
| X 原帖 | https://x.com/rasbt/status/2111448141778256161(@rasbt,ML/AI research engineer,前统计学教授,著有 LLMs-from-scratch,527K 粉丝) |
| YouTube 视频 | https://www.youtube.com/watch?v=msp69FR5NZs,2026-10-05 发布,@SebastianRaschka 频道,99K 订阅 |
| 配套仓库 | github.com/rasbt/reasoning-from-scratch,ch08 目录含 6 个子模块 |
| 书籍官方页 | manning.com/books/build-a-reasoning-model-from-scratch,Chapter 8: "Distilling reasoning models and generating reasoning distillation data" |
| 官方章节预览 | sebastianraschka.com/reasoning-from-scratch(Study Guide 第8行) |
上手步骤
准备工作
-
克隆仓库
bash git clone https://github.com/rasbt/reasoning-from-scratch.git cd reasoning-from-scratch/ch08 -
环境依赖 - Python + PyTorch - Hugging Face
transformers-transformers版本需支持 Qwen3 模型(书籍用 Qwen3-0.6B 作为学生模型) - Ollama 或 OpenRouter(用于生成教师模型的蒸馏数据)
蒸馏数据生成(ch08/02_generate_distillation_data)
这一步用教师模型(DeepSeek R3 或同类强推理模型)在 MATH 数据集上批量推理,保存推理轨迹(reasoning traces)作为训练数据。
- 数据来源:DeepSeek R1 MATH dataset(
sebastianraschka.com/reasoning-from-scratch) - 生成方式:Ollama 或 OpenRouter API 调用教师模型
- 输出:包含
<reasoning>...<reasoning/> <answer>...格式的 JSONL 数据
推理轨迹处理(ch08 主notebook)
- 解析结构:从教师输出中提取
<reasoning>块和<answer>块 - Tokenization:使用 Qwen3 chat template 进行 tokenize
- 长度控制:过滤超过 2,048 token 的样本(序列长度上限)
- 数据划分:生成训练集和验证集拆分
蒸馏训练(ch08/04_train_with_distillation)
核心是计算 answer token 的 log probabilities(非 reasoning 部分):
# 加载 Qwen3-0.6B 学生模型
model = load_qwen3_0_6b()
# 教师模型前向传播,计算 answer token log probabilities
with torch.no_grad():
teacher_logits = teacher_model(input_ids).logits
teacher_log_probs = F.log_softmax(teacher_logits, dim=-1)
# 学生模型前向传播
student_logits = model(input_ids).logits
student_log_probs = F.log_softmax(student_logits, dim=-1)
# Cross-entropy loss for distillation(仅在 answer token 上)
loss = F.cross_entropy(
student_log_probs[:, :-1].reshape(-1, vocab_size),
teacher_log_probs[:, 1:].argmax(dim=-1) # 软标签
)
训练脚本支持: - 单样本蒸馏(single-example distillation) - 批处理蒸馏(batched distillation)
评估
- 用 MATH-500 基准(sebastianraschka.com/reasoning-from-scratch,Chapter 8 evaluation)
- 评估工具:verifiers + parsing(Chapter 3 构建的解析器)
- 书籍数据显示:蒸馏 checkpoint 在 MATH-500 上的表现与 RL 方法可比,且训练更稳定
软蒸馏 vs 硬蒸馏
软蒸馏(Soft Distillation):
- 用教师模型输出的 logits / log probabilities 作为监督信号
- KL Divergence 损失:L = KL(student || teacher)
- 保留教师模型的置信度分布信息,学生学到的不仅是"正确答案"还有"为什么"
硬蒸馏(Hard Distillation): - 用 one-hot 形式的正确答案作为标签(hard labels) - 等同于普通监督学习
Raschka 推荐软蒸馏作为默认方案(视频 02:51 处),理由是推理模型的 reasoning token 空间大,软标签能传递更丰富的结构信息。
On-policy vs Off-policy 蒸馏
| 模式 | 含义 | 特点 |
|---|---|---|
| On-policy | 学生生成数据后立即用同一学生蒸馏 | 高方差,训练慢 |
| Off-policy | 提前用教师模型生成好数据,批量训练 | 更稳定、效率高,书籍和视频默认方案 |
适用边界
适合的场景: - 有现成的强推理模型(DeepSeek R1/R3、o1 等)作为教师 - 需要在小模型(<1B 参数)上部署推理能力 - 不想设计复杂的奖励函数或 RL pipeline
不适合的场景: - 没有教师模型或教师模型不可用 → 只能选 RL(Chapter 6-7) - 追求极致的 MATH/Code 基准分数 → RL + 格式奖励可能更高 - 需要实时持续学习 → 需要定期重新蒸馏
硬件要求(参考仓库 hardware-requirements):
- 学生模型 Qwen3-0.6B:单卡可训练(~4GB GPU 显存)
- 教师模型(DeepSeek R3 等):建议 A100 或等效卡
仓库结构速查
ch08/
├── 01_main-chapter-code/
│ ├── ch08_main.ipynb # 主章节代码
│ └── ch08_exercise-solutions.ipynb # 习题解答
├── 02_generate_distillation_data/ # 蒸馏数据生成脚本
├── 04_train_with_distillation/ # 蒸馏训练脚本
├── 05_download_training_checkpoints/ # 预训练 checkpoint 下载说明
└── 06_use_via_huggingface/ # 通过 HF transformers 使用模型
视频时间戳(来源:YouTube 视频描述)
| 时间点 | 内容 |
|---|---|
| 00:00 | Introduction and series recap |
| 01:07 | Why distill a reasoning model? |
| 02:51 | Soft vs. hard distillation |
| 11:06 | On-policy vs. off-policy distillation |
| 18:09 | Preparing data for reasoning distillation |
| 22:35 | Loading the DeepSeek R1 MATH dataset |
| 28:04 | Inspecting reasoning traces and final answers |
| 31:27 | Tokenization and chat templates |
| 35:58 | Pre-tokenizing the training dataset |
| 39:01 | Sequence lengths and the 2,048-token limit |
| 42:38 | Creating training and validation splits |
| 45:32 | Loading the Qwen3-0.6B base model |
| 46:24 | Computing answer-token log probabilities |
| 54:10 | Cross-entropy loss for distillation |
| 57:25 | Training and validation loss helpers |
| 1:02:03 | Implementing the distillation training loop |
| 1:07:26 | Running the standalone training script |
| 1:09:41 | Plotting training and validation losses |
| 1:11:31 | Evaluating checkpoints on MATH-500 |
| 1:14:09 | Comparing teachers and distillation results |
| 1:16:34 | Future directions for reasoning models |
| 1:19:41 | Bonus exercises and next steps |
不确定处
- 教师模型具体版本:视频描述提到 DeepSeek R1 MATH dataset,但教师模型是 R1 还是 R3 未明确说明;仓库 README 提到"DeepSeek-V3.2-"等变体,具体用哪个需参考 ch08/02 目录脚本中的配置。
- 蒸馏温度参数:KL divergence 损失中是否使用 temperature scaling(即 distill by softened probabilities)未在公开描述中确认,需读 notebook 源码确认。
- alpha 混合权重:若同时使用硬标签和软标签,损失函数是否有加权混合(
L = α * L_soft + (1-α) * L_hard),未在可检索信息中确认。 - 视频发布于 2026-10-05:早于本任务执行时间(2026-10-18),但 X 原帖仅为链接转发,内容需从视频提取,非直接引用。