All courses

Distillation from a stronger model

Train a smaller student on a teacher's outputs, either its full token distributions (soft targets via KL) or its sampled text. Explain why reasoning traces from a strong model transfer well to small models.

Builds on: Supervised fine-tuning on demonstrations, Chain of thought as extra computation

▶ Watch this lesson Start the whole course

What the animation says

Big teacher, small student

You have a big, strong model, and you want a small one that is nearly as good but cheap to run. Ordinary training shows the small model one thing per position: the token that actually came next. But the big model knows more. At every position it can give a probability for every token in the vocabulary. Can that richer signal teach the small model better? And what if all you can get from the big model is text?

Soft targets carry more

Ordinary training uses a one-hot target: all the weight on cat, zero on everything else. The teacher's distribution says more: cat point six, kitten point two five, dog point one. It shows which alternatives are nearly right, and how uncertain this context is. So train the student to match the teacher's whole distribution. The gap is measured with KL divergence, averaged over positions. Every position now teaches something about every token, not just one. The catch: you need the teacher's logits and the same vocabulary.

The gradient: student minus teacher

For each token, the soft-target gradient on the student's logit is simply student probability minus teacher probability. With hard labels, the teacher's side is replaced by one for the observed token and zero elsewhere. That's the usual rule. Training steps against the gradient. A negative entry raises that logit; a positive entry lowers it. Three tokens, a, b, c. The teacher says point seven, point two, point one. The student says point five, point two five, point two five. Which logit will soft training push down the hardest? Tap it.

Hard label vs soft target

Now the hard label: observed token a, so the target is one, zero, zero. Student minus one-hot: minus point five, plus point two five, plus point two five. Soft: minus point two, plus point zero five, plus point one five. Hard pushes b and c down equally. Only soft says c is more overrated than b. Each entry compares one token, so a token where both agree gets zero. Now the teacher only returns text, no probabilities. What can you still do?

Text only: train on teacher samples

You can still distill. Send prompts, sample the teacher's responses, and run ordinary supervised fine-tuning on them. The loss is the usual one: minus log probability of each teacher token, given everything before it. This also works when the tokenizers differ, because only text crosses over. Averaged over samples, this equals the sequence-level KL from teacher to student, plus a constant: the teacher's entropy. The signal is thinner, though: one sampled token per position, instead of a full distribution.

Reasoning traces fit the budget

Teacher reasoning traces work especially well as text to train on. Each token the student produces gets a fixed amount of compute: about two operations per parameter, through a fixed number of layers. A hard answer can need more work than one token allows. Blurting it out in one step overloads that budget. A trace splits the work into small steps, and each step is a next-token prediction the student can afford. But teachers make mistakes. Keep only traces whose final answer checks out. Tap the one to throw away.

Can a big model teach a small one better?

You have a strong, large model and want a small one that is nearly as good but cheap to run. Ordinary training gives the small model one observed next token per position. The large model can do more: at every position it can say how likely each of the $V$ tokens is. How do you turn that into a better training signal? And what if all you can get from it is text?

Soft targets carry more than the answer

A one-hot target says only "the next token was *cat*". A teacher distribution says "*cat* 0.6, *kitten* 0.25, *dog* 0.1, …". It shows which alternatives are nearly right and how uncertain the context is. Training the student to match the whole distribution passes on information about all $V$ tokens at every position, not one sample. The student learns more from each token. This route needs the teacher's logits and a shared vocabulary.

Text only: SFT on teacher samples

Without logits, for example with text-only API access or a different tokenizer, sample responses from the teacher and run ordinary SFT on them. Reasoning traces from a strong teacher work especially well. A hard answer may need more computation than the student's ~$2N$ FLOPs and $L$ layers per token allow. A trace splits that work into small steps, and each step is a next-token prediction that fits the student's budget. Keep only traces whose final answer checks out.

The two distillation losses

Teacher $q_t=q(\cdot\mid x_{\le t})$, student $p_t=p_\theta(\cdot\mid x_{\le t})$. Soft targets: $$\mathcal L_{KD}=\frac1T\sum_t\sum_v q_t(v)\ln\frac{q_t(v)}{p_t(v)}$$ The gradient on the student's logits at position $t$ is $(p_t-q_t)/T$. This is the usual $p-\text{onehot}$ with the one-hot replaced by $q_t$. It is often mixed with the hard-label loss: $\alpha\mathcal L_{KD}+(1-\alpha)\mathcal L$. Sampled text: for prompt $x$, draw a response $y\sim q(\cdot\mid x)$ from the teacher and minimize $-\sum_t\ln p_\theta(y_t\mid x,y_{<t})$. In expectation this equals sequence-level $\mathrm{KL}(q\|p_\theta)$ plus a constant (the teacher's entropy).

Three tokens, hard vs soft

Teacher $q=(0.7,0.2,0.1)$ and student $p=(0.5,0.25,0.25)$ over tokens a, b, c. The observed token is a. - Hard loss: $-\ln0.5=0.693$. - Soft cross-entropy: $-(0.7\ln0.5+0.3\ln0.25)=0.485+0.416=0.901$. - Teacher entropy is $0.802$, so $\mathrm{KL}=0.901-0.802=0.099$. Logit gradients: - Hard: $p-\text{onehot}=(-0.5,0.25,0.25)$. - Soft: $p-q=(-0.2,0.05,0.15)$. Only the soft target tells the student that c is overrated more than b.

Remember

Distillation trains a small student to imitate a strong teacher. Either match its full next-token distributions with KL (a rich signal that needs logits and a shared vocabulary), or run SFT on its sampled text (needs only text). Teacher reasoning traces transfer well because they break hard answers into steps that each fit the student's fixed per-token compute.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.