蒸馏(Distillation)

上一章让模型自己去试,学到的是它本来就偶尔做得到的事。这一章换一个来源:找一个比它强的模型当老师,让老师写回答,学生照着学。这就是蒸馏(Distillation)。
老师是第一本第十三章用过的Qwen3-0.6B,比我们的模型大二十多倍。我们从第七章的smol-smoltalk里挑了两万个问题,让它在T4上回答,花了六个多小时。然后做一个对照实验:同一批问题,一份数据用老师的回答,一份用smol-smoltalk原来的回答,各训一个学生,都从第六章的基座模型出发。
结果有一半在意料之中:老师的回答好学得多,学它的学生在验证集上的loss低了一大截,首都、打招呼这样的问题,一句话答完就停。另一半不太好看:学生把老师的格式学得惟妙惟肖,加粗、小标题、LaTeX公式一样不落,内容却还是原地打转。学生能从老师那里拿走多少,先要看学生自己有多大。
这一章的代码在code/book/ch10/下面。config.py、model.py、adamw.py、schedule.py、export.py、chat_template.py和chat.py从第九章原样复制,sft.py从第七章原样复制。新写的有:pick_prompts.py挑问题;teacher.py让老师写回答;prepare_distill.py过滤老师的回答,装成训练数据;answers.py和compare.py比较两份数据和训出的学生;plot_log.py画loss曲线。colab.ipynb是在Colab上运行它们的笔记本。
为什么要蒸馏
一个小模型,自己从网页里学,学到的是网页的样子:什么都有,好坏参半,同一件事有一百种说法。让一个大模型先把要学的东西整理一遍,用统一的口径写出来,小模型再照着学,就省力得多。大模型已经替它从海量的数据里提炼过一遍了。
今天的小模型,几乎都是这么训出来的。Qwen3的小尺寸模型,后训练就是拿大尺寸模型当老师蒸馏出来的;第二章提到的Cosmopedia,是大模型写的教科书和故事,SmolLM2的预训练数据里就有它。
第七章其实已经蒸馏过了
回头看第七章的SFT数据。smol-smoltalk里的回答,几乎都不是人写的:占了一大半的smol-magpie-ultra-short,回答出自Llama-3.1-405B-Instruct;写代码的、写摘要的、改写文字的,也都是大模型合成的。第七章的SFT,本质上就是一次蒸馏,老师是好几个比我们大几百到上万倍的模型。
所以这一章要做的,不是从零开始讲一件新事,而是把第七章里藏着的那件事摆到明处:换一个老师,同一批问题,看学生学到的有什么不同。
两种蒸馏
学生从老师那里学,有两种学法,区别在于学生能看到老师的多少东西:是老师在每个位置上的完整概率分布,还是老师最后写出来的那一串词元。
logit蒸馏
“蒸馏”这个名字出自《Distilling the Knowledge in a Neural Network》(arXiv:1503.02531)。它让学生学老师在每个位置上的完整概率分布:老师预测下一个词元时,给词表里每个词元都算了一个概率,学生的loss是它的分布和老师的分布之间的KL散度:
其中
序列级蒸馏
另一种学法更朴素:让老师把回答写出来,学生在这些回答上做SFT,和第七章一模一样。这叫序列级蒸馏,出自《Sequence-Level Knowledge Distillation》(arXiv:1606.07947)。学生看不到老师的概率分布,只看到老师抽出来的那一个词元,学到的东西少一些;好处是什么都不用改,老师写完了,数据就有了,训练循环还是原来那个。
分词器不同怎么办
用logit蒸馏有一个前提:老师和学生的词表得一样。老师在某个位置上给出的是15万多个词元上的分布,Qwen3的词表有151,936个词元;我们的学生只认识8192个,两个分布对不上号。词元的切法也不一样,同一句话,老师切成20个词元,学生可能切成26个,连“第
有一些办法可以把两个词表对齐,但都很复杂。所以这一章用序列级蒸馏:老师写出的是文字,学生用自己的分词器重新切一遍就行。这也是用大模型合成数据的常见做法,smol-smoltalk就是这么来的。
老师:Qwen3-0.6B
老师是第一本第十三章用过的Qwen3-0.6B,6亿参数,许可证Apache 2.0。第一本用自己写的引擎跑过它,第十三章也逐章讲过它和SmolLM2有哪些不同,这里不再重复。
这一章不用第一本的引擎,而是用transformers跑它。原因是速度:我们要它写两万多个回答,第一本的引擎批处理时不会补齐,长短不一的问题只能一个一个写,在CPU上要写好几天。transformers能把几十个问题放在一起写,在T4上快得多。老师在这里只是一个出数据的工具,用谁家的代码跑都一样。
还有一件事:Qwen3有思考模式,回答之前先写一大段<think>。我们不要它思考:一来学生学不会,二来太慢。对话模板有一个开关enable_thinking,把它设成False,模板会替老师写好一段空的<think></think>,老师就直接作答。采样参数用Qwen3给非思考模式推荐的:温度0.7,top-p 0.8,top-k 20。
生成蒸馏数据
蒸馏数据分两步做:先挑问题,再让老师回答。两步分开存,问题挑好就不再变,老师写到一半断了,接着写剩下的问题就行;原来的回答也跟着问题存下来,后面的对照实验要用。
问题从哪来
问题从第七章的smol-smoltalk里挑。只挑单轮的:每段对话只取第一问一答,用户提问、助手回答;第一轮是系统提示的跳过。问题不超过256个词元,原来的回答拼进对话以后,要能放进一行,也就是1025个词元。一字不差的重复问题只留一个,训练集也不能和验证集重复。pick_prompts.py按随机的顺序看对话,挑够2万个训练用的、500个验证用的就停:
uv run python book/ch10/pick_prompts.py输出先是验证集,再是训练集。每一份打印看了多少段对话、挑中多少个,没挑中的按原因分开数,最后是挑中的问题出自smol-smoltalk的哪一部分:
val:24,229段对话,看了698段,挑中500个
挑中 500 71.6%
开头不是一问一答 152 21.8%
问题太长 26 3.7%
重复的问题 18 2.6%
回答太长 2 0.3%
来源:smol-magpie-ultra-short 355,self-oss-instruct 58
smol-contraints 46,openhermes-50k 35
everyday-conversations 3,explore-instruct-rewrite 3
train:460,341段对话,看了26,222段,挑中20,000个
挑中 20,000 76.3%
开头不是一问一答 4,151 15.8%
问题太长 1,077 4.1%
重复的问题 854 3.3%
回答太长 140 0.5%
来源:smol-magpie-ultra-short 14,252,self-oss-instruct 2,339
smol-contraints 1,967,openhermes-50k 1,349
explore-instruct-rewrite 92,everyday-conversations 1
写到 data/distill/prompts.jsonl大约四分之三的对话都合格,没挑中的大多是因为第一轮是系统提示。来源和第七章那张表差不多,七成出自smol-magpie-ultra-short;第七章表里的smollm-rewrite-30k和smol-summarize这两类,这里一个也没有,它们的对话都以系统提示开头。挑出来的问题连同原来的回答,存在data/distill/prompts.jsonl里,每行一个。
让老师写
脚本teacher.py把问题按长度排好,每次取64个,左边补齐:几十个问题长短不一,在左边补上空位,让所有问题的最后一个词元对齐,回答就从同一列开始往后写。问题按长度排过,一批里的长短差不多,补齐的就少。写的那一段是这样的:
texts = [tok.apply_chat_template(
[{"role": "user", "content": p["prompt"]}], tokenize=False,
add_generation_prompt=True, enable_thinking=False)
for p in batch]
enc = tok(texts, return_tensors="pt", padding=True)
enc = enc.to(args.device)
with torch.no_grad():
out = model.generate(
**enc, max_new_tokens=args.max_new, do_sample=True,
temperature=args.temperature, top_p=args.top_p,
top_k=args.top_k, pad_token_id=tok.pad_token_id)
new = out[:, enc["input_ids"].shape[1]:].tolist()每个回答最多512个词元,这是老师的词元,换成我们的分词器,同一段文字要多两成左右,大约是六百来个,再加上问题,正好放得进一行。写完的回答连同“是否自己停下”“写了几个词元”,每批追加到teacher.jsonl里。
老师写得比我预想的慢得多。在T4上,每秒只写一个回答左右,大约290个词元;越往后问题越长,降到每秒0.8个回答、200个词元。一批64个回答,要等最长的那个写完,先写完的那些就陪着空等。20,500个回答一共写了六个多小时,免费的Colab一次会话撑不下来,断了两次,靠--backup每隔10批备份到Drive的文件接着写。数据是蒸馏里最贵的部分,大模型合成数据的成本,大半花在这里。
这20,500个回答,老师一共写了552万个词元,中位数是243个。有83%的回答在512个词元之内自己停下了,剩下的17%写到上限还没写完。
数据质量
老师也会出错。prepare_distill.py给老师的每个回答过一遍筛子:
def check(chat, p, t):
"""老师的回答能不能用。返回(状态, 拼好的ids, mask)。"""
if not t["finished"]:
return "没写完", None, None
if not t["answer"]:
return "空回答", None, None
if CJK.search(t["answer"]) and not CJK.search(p["prompt"]):
return "中文", None, None
body = chat.tok.encode(t["answer"]).ids
if repeated(body) > 0.5:
return "重复", None, None
ids, mask = chat.render([{"role": "user", "content": p["prompt"]},
{"role": "assistant", "content": t["answer"]}])
if len(ids) > ROW:
return "太长", None, None
return "留下", ids, mask没写完的不要:学了它们,学生就学会写着写着戛然而止。Qwen3是中英双语的模型,问题是英文,回答里却出现了中日韩文字的,不要。repeated数回答里连续四个词元一组的片段,有一半以上是前面出现过的,说明老师在原地打转,也不要。最后是拼进对话以后放不进一行的。
留下的问题,装成三份数据。三份的问题一模一样,都是老师那一份能用的问题:teacher用老师的回答,orig用smol-smoltalk原来的回答,teacher-5k用老师的回答,训练集只取前5000个,对照实验用。每份都和第七章一样,装进1025个词元的行:
uv run python book/ch10/prepare_distill.py输出先是筛子的结果,再是两份回答的长度,最后是装好的每份数据有多少段、多少行:
20,500个问题,老师写了20,500个回答
val:500个回答
留下 424 84.8%
没写完 72 14.4%
空回答 0 0.0%
中文 0 0.0%
重复 2 0.4%
太长 2 0.4%
train:20,000个回答
留下 16,387 81.9%
没写完 3,397 17.0%
空回答 0 0.0%
中文 9 0.0%
重复 177 0.9%
太长 30 0.1%
回答的长度(我们的分词器,词元数):
train 老师的中位数 297 原来的 285 老师写得更长的占53.0%
val 老师的中位数 275 原来的 283 老师写得更长的占50.7%
teacher/train:16,387段,装成6,443行,算loss的词元5,187,845个
teacher-5k/train:5,000段,装成1,957行,算loss的词元1,568,742个
teacher/val:424段,装成161行,算loss的词元128,103个
teacher-5k/val:424段,装成161行,算loss的词元128,103个
orig/train:16,387段,装成5,983行,算loss的词元4,689,623个
orig/val:424段,装成154行,算loss的词元121,131个训练集留下了81.9%,扔掉的几乎都是没写完的。答成中文的只有9个,原地打转的177个,都很少:Qwen3-0.6B虽然小,写英文回答还是很稳的。
这里有一个容易忽略的偏差:扔掉的是老师写得长的那些问题,三份数据又都只留剩下的问题,所以留下来的问题,偏向能简短回答的那一类。好在两边的回答差不多长,中位数297对285,老师写得更长的刚过一半。后面两个学生要是不一样,主要不是因为长短。
两份回答长什么样
长短差不多,样子却很不一样。answers.py挑一个问题,把两份回答并排打印出来,再数一数两份训练集里各种格式出现得多频繁:
uv run python book/ch10/answers.py val-18输出分三块:最上面是问题;[老师]和[原来]下面各是一份回答,太长的行折到76列;最后是格式的统计,每种格式一行,两列分别是老师和原来的回答里带这种格式的比例:
问:A bat and a ball together cost $1.10. The bat costs $1.00 more than the
ball. How much does the ball cost?
[老师]
Let's solve this step by step.
Let the **cost of the ball** be $ x $.
Then the **cost of the bat** is $ x + 1 $ (since the bat costs $1.00 more
than the ball).
The total cost of the bat and the ball is:
$$
x + (x + 1) = 2x + 1
$$
We are told that the total cost is $1.10.
So:
$$
2x + 1 = 1.10
$$
Subtract 1 from both sides:
$$
2x = 0.10
$$
Divide by 2:
$$
x = 0.05
$$
### ✅ Final Answer:
The ball costs **$0.05**.
[原来]
Let's break down the given information. We know the total cost of the bat
and the ball is $1.10. We also know that the bat costs $1.00 more than the
ball.
If we let the cost of the ball be x cents, then the cost of the bat would
be x + $1.00. Since the total cost of the bat and the ball together is
$1.10, we can write the equation x + (x + $1.00) = $1.10.
Simplifying this equation, we get 2x + $1.00 = $1.10, which can be further
simplified to 2x = $0.10.
Dividing both sides of the equation by 2, we get x = $0.05.
So, the ball costs 5 cents.
训练集里老师写完了的16,603个问题,带这些格式的回答占多少:
格式 老师 原来
加粗 ** 58.0% 4.0%
小标题 ### 53.2% 0.8%
LaTeX $ 7.4% 0.4%
分隔线 --- 13.8% 0.0%
表情符号 9.4% 0.0%两份回答都做对了,球是5美分。原来的回答是几段平铺直叙的文字;老师的回答是Qwen3的招牌样子:加粗的关键词,单独一行的公式,带对勾表情的小标题。输出最后那张表说明这不是个例:老师的回答一半以上带加粗和小标题,原来的回答几乎没有。学生学老师,首先学到的就会是这些。表头的16,603个,是老师写完了的全部训练问题;再筛掉空的、答成中文的、原地打转的和放不进一行的,训练时用的是其中16,387个。
代码实现
数据有了,训练就是一次SFT,sft.py从第七章原样复制,一行没改。三个学生都从第六章的基座模型出发,用同样的超参数,只换数据:
| 学生 | 数据 | 训练集 | 步数 |
|---|---|---|---|
distill | 老师的回答,16,387个问题 | 6,443行 | 303 |
distill-orig | 原来的回答,16,387个问题 | 5,983行 | 281 |
distill-5k | 老师的回答,5,000个问题 | 1,957行 | 92 |
学习率和第七章一样,最大 --name和--data:
python book/ch10/sft.py --name distill --init .../runs/pretrain/model \
--data /content/data/distill/teacher --runs .../runs --resume \
--epochs 3 --warmup 30 --eval-every 30 --eval-batches 5 \
--sample-every 100 --ckpt-every 50 --export代码效果
三个学生训完,先看训练过程中loss怎么变,再比较两份数据各自有多好学,最后看学生自己写的回答。
loss曲线
训练日志下载到runs/下以后,用uv run book/ch10/plot_log.py画成两张图。左边是训练loss,20步的滑动平均;右边是验证loss,每30步一次,每个学生用的是自己那一份的验证集:

学老师的两个学生,loss从一开始就降得快。训完时,distill的验证loss是1.350,distill-orig是1.886,差了0.5还多。还没开始训,基座模型在老师的验证集上是2.684,在原来的验证集上是2.842,老师的回答本来就更好猜一些;训练把这个差距拉大了好几倍。
训练loss比验证loss低,distill最后是1.10上下,比验证loss低0.25。数据过了3遍,后两遍见到的都是看过的回答,有一点过拟合。不过验证loss一直在降,没有掉头往上走,3遍还不算多。distill-5k每31步就过完一遍数据,训练loss降得最快,验证loss却停在1.553,比distill高0.2。
交叉比较
各自验证集上的loss,比的是各自的数据好不好学。还可以交叉着比:每个学生在两份验证集上各算一次loss。compare.py做这件事,再让每个学生回答验证集的前100个问题,像第八章的answer_len.py那样,量平均长度、自己停下的比例和重复。第七章的SFT模型也放进来,当个参照:
uv run python book/ch10/compare.py runs/distill/model \
runs/distill-orig/model runs/distill-5k/model runs/sft/model表里每个学生一行,sft就是第七章的模型。前两列是它在两份验证集上的loss,只算回答的词元;后三列是它自己写回答时的平均长度、自己停下的比例和重复,重复和第八章一样,是回答里每4个相连的词元有多少在前面已经出现过:
验证集的前100个问题,温度0.8,top-k 50,每个最多300个词元
学生 老师的loss 原来的loss 平均长度 自己停下 重复
distill 1.351 2.241 259 26% 23%
distill-orig 1.858 1.886 264 43% 20%
distill-5k 1.554 2.347 268 19% 31%
sft 1.482 1.448 268 36% 17%前两列就是交叉比较。distill在老师的验证集上是1.351(这是compare.py重算的,和训练日志里的1.350差在最后一位),在原来的验证集上是2.241;distill-orig在两份上差不多,1.858和1.886。学了老师的回答,再看原来的回答就很意外;学了原来的回答,看老师的回答却不怎么意外,和看自己那一份差不多。老师的回答不只对学过它的学生好猜,对没专门学过原来那种写法的模型,都更好猜;第七章的sft是个例外,它就是在原来那种回答上训的。原因不难想:老师只有0.6B,采样又用了温度0.7和top-p 0.8,它写出的东西用词集中、句式固定,每段回答都长得差不多;原来的回答出自好几个大得多的模型,各有各的写法。
第七章的SFT模型是一个有意思的参照。它在46万段对话上训过,数据是这里的几十倍,所以在原来的验证集上最好,1.448;在老师的验证集上是1.482,也比distill-orig好,只输给专门学老师的distill。数据量的差距,比老师和原来回答的差别大得多。
后三列是学生自己写的回答,采样有随机性,这几个数要谨慎看待。自己停下的比例,distill是26%,distill-orig是43%;可是同样的三个学生,同样的命令,在Colab的T4上跑,是34%和35%,几乎一样。采样用的随机数在CPU和GPU上不同,100个问题的结果就能差出这么多。比较稳的结论有两条,两次运行都一样:distill-5k停得最少、重复最多;distill的重复比distill-orig多一点。第七章的SFT模型只在Mac上跑了这一次,重复最少。loss低了0.5的distill,自己写起回答来,并没有比distill-orig好。
回答对比
最后用chat.py问三个学生第七章那五个问题,贪婪解码:
uv run python book/ch10/chat.py --model runs/distill/model \
runs/distill-orig/model runs/distill-5k/model输出按问题分组,每个问题下面依次是distill、distill-orig和distill-5k的回答,每个最多200个词元,写到上限的就截在那里:
问:What is the capital of France?
[distill]
The capital of France is Paris.
[distill-orig]
The capital of France is Paris, which is the capital of the United States.
It is the capital of the United States, and it is the capital of the
United States.
The capital of the United States is Paris, which is the capital of the
United States. It is the capital of the United States, and it is the
capital of the United States.
The capital of the United States is Paris, which is the capital of the
United States. It is the capital of the United States, and it is the
capital of the United States.
The capital of the United States is Paris, which is the capital of the
United States. It is the capital of the United States, and it is the
capital of the United States.
The capital of the United States is Paris, which is the capital of the
United States. It is the capital of the United States.
The capital of the United States is Paris, which is the capital of the
United States
[distill-5k]
The capital of France is Paris. It is a country where the **population**
is divided into **100%** of the population. It is a country where the
**population** is divided into **100%** of the population.
### **Conclusion:**
The capital of France is **100%** of the population. It is a country where
the **population is divided into **100%** of the population.
### **Conclusion:**
The capital of France is **100%** of the population. It is a country where
the **population is divided into **100%** of the population.
### **Conclusion:**
The capital of France is **100%*
问:Give me three tips for staying healthy.
[distill]
1. **Use a healthy diet**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
- **Salary**:
[distill-orig]
When staying healthy, it's essential to approach the situation with
empathy and understanding. When we're feeling overwhelmed, it's essential
to acknowledge that it's okay to take a step back and assess your own
needs. This can be achieved through empathy, understanding, and self-
reflection, which can help you identify areas for improvement and make
more informed decisions.
Consider your own lifestyle and lifestyle choices. Consider your
lifestyle, diet, and lifestyle choices, as these can help you stay healthy
and motivated. Consider your lifestyle choices, lifestyle choices, and
lifestyle choices that align with your overall well-being.
It's also essential to recognize that you're not alone in the process of
staying healthy, but rather focusing on the positive aspects of your
lifestyle. By focusing on the positive aspects of your lifestyle, you can
develop a more balanced and balanced lifestyle that aligns with your
overall well-being
[distill-5k]
Give me a few tips to stay healthy and healthy. Here are some tips to stay
healthy:
1. **Take a break**: Avoid over-diagnosing a condition that requires
immediate attention.
2. **Take a break**: Avoid over-diagnosing a condition that requires
immediate attention.
3. **Take a break**: Avoid over-diagnosing a condition that requires
immediate attention.
4. **Take a break**: Avoid over-diagnosing a condition that requires
immediate attention.
5. **Take a break**: Avoid over-diagnosing a condition that requires
immediate attention.
6. **Take a break**: Avoid over-diagnosing a condition that requires
immediate attention.
7. **Take a break**: Avoid over-d
问:Write a short poem about the sea.
[distill]
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place of peace.**
**The sea is a place of peace, and the sea is a place
[distill-orig]
The sea's been a constant, yet still a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah, the sea's a place of mystery,
Ah
[distill-5k]
**Setting:**
Ah, the sea is a place of peace, and the sea is a place of peace. The sea
is a place of peace, and the sea is a place of peace.
I am the sea, and the sea is a place of peace. I am the sea’s leader, and
I am the sea’s leader.
问:What is 12 plus 7?
[distill]
12 plus 7 is a **single number**, and it is a **single number**.
### 1. **Single number**:
- 7 = 12
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
[distill-orig]
12 plus 7 is a fundamental concept in mathematics, and it is a fundamental
concept in number theory. It is a fundamental concept in number theory,
which states that the number of integers in a given range is equal to the
number of integers in a given range.
The concept of 12 plus 7 is a fundamental concept in number theory, which
states that the number of integers in a given range is equal to the number
of integers in a given range. This concept is fundamental in number
theory, which states that the number of integers in a given range is equal
to the number of integers in a given range.
The concept of 12 plus 7 is a fundamental concept in number theory, which
states that the number of integers in a given range is equal to the number
of integers in a given range. This concept is fundamental in number
theory, which states that the number of integers in a given range is equal
to
[distill-5k]
12 plus 7 = 12 $
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
### **12 plus 7 = 12 $**
问:Hi! How are you today?
[distill]
Hi! How are you today? I'm glad to meet you! 🌍✨
[distill-orig]
I'm a kid in the heart of the world. I'm a part of my life, and I'm a part
of my life. My name is Jack Harris, and I'm a part of my career. I've been
working in this world for over 20 years, and I've been working in this
world for over 20 years.
I'm a part of my career, and I'm a part of my career. I'm a part of my
family's history, and I'm a part of my family's history. I'm a part of my
family's history, and I'm a part of my family's history.
My career is a part of my family's history, and I'm a part of my family's
history. I'm a part of my family's history, and I'm a part of my family's
history. I'm a part of my family's history, and I'm a part of my family's
[distill-5k]
Hi! How are you today? I'm here to help you with your journey. Here's a
structured approach to help you navigate this journey:
### 1. **Set Clear Goals**
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **Start with a Plan**:
- **贪婪解码下,三个学生的毛病都很明显,但毛病不一样。学老师的distill,有两题答得最好:首都那题一句“The capital of France is Paris.”就停了,打招呼也是一句话加两个表情符号,正是老师的样子,前面那张表里,老师的回答有9.4%带表情符号。可另外三题,它学来的格式成了打转的工具:健康建议是一个越缩越深的列表,每一项都是加粗的“Salary”;12加7写出一列LaTeX的美元符号;诗是同一行加粗的句子重复到上限。
学原来回答的distill-orig,几乎不用格式,一大段一大段地写,打转的是整句话:把巴黎说成美国的首都,说了一遍又一遍;打招呼时,自我介绍成了一个叫Jack Harris的人。只学了5000个问题的distill-5k,两边的毛病都有,格式比distill还多,“### Conclusion:”写了一遍又一遍。
算12加7,三个学生没有一个算对。Qwen3-0.6B答这道题不在话下,可学生只学到了它答题的样子:加粗、小标题、公式,没学到算出19的本事。序列级蒸馏只能让学生模仿老师写出来的东西,老师会的东西,学生要自己装得下,才学得去。
对照实验
对照实验只有一个:数据量。distill-5k和distill用的是同一份老师的回答,只是训练集只取前5000个问题,其余设置一样,也过3遍,所以只训了92步。
结果前面都看到了:验证loss是1.553,比distill的1.350高0.2;自己写回答时,停下得最少、重复最多,两次运行都是这样;回答对比里,格式的毛病比distill更重。数据少了,学生最先学会的是最显眼的东西,也就是老师的格式;内容要靠更多的数据才学得到。
往另一个方向看也一样:16,387个问题比5000个好了不少,可是第七章的46万段对话还要好得多,第七章的SFT模型在两份验证集上的loss都很低,重复也最少。在我们这个尺寸上,这一章的蒸馏数据远远不够。老师写一个回答要一秒钟左右,46万个回答就是五天多的T4,这就是为什么大家宁愿用别人蒸馏好的现成数据,比如smol-smoltalk。
本章小结
这一章用Qwen3-0.6B当老师,做了一次序列级蒸馏。蒸馏是让小模型跟着大模型学。logit蒸馏学老师在每个位置上的完整分布,学得多,但要求两边的词表一样;序列级蒸馏让老师把回答写出来,学生在上面做SFT,词表不同也能用。我们的学生只认识8192个词元,老师有151,936个,所以用后者。第七章的smol-smoltalk本来就是大模型写的,那一次SFT其实就是蒸馏。
我们从smol-smoltalk挑了两万个单轮问题,让老师用非思考模式在T4上写回答,每秒一个左右,一共六个多小时。筛掉没写完的、答成中文的、原地打转的,留下16,387个问题。同一批问题,一份用老师的回答,一份用原来的回答,各训一个学生。
老师的回答好学得多:学它的学生验证loss是1.350,学原来回答的是1.886。老师的回答用词集中、口径统一,对谁都好猜。可是loss低,不等于回答好。学生学到的首先是老师的格式,加粗、小标题、公式;首都和打招呼答得干净利落,算术和列表照样原地打转,12加7没有一个学生算对。数据只有5000个问题时,学到的几乎只剩格式。
学生能从老师那里拿走多少,先要看学生自己有多大,以及给它看了多少回答。这一章的学生只见过一两万个回答,比不上第七章那个见过46万段对话的模型。下一章换一个问题:模型已经训好了,想让它学一件新本事,能不能不动原来的参数,只训一小部分新加的参数?这就是LoRA。