GRPO teaches the student by trial and error. Distillation teaches it by example. A stronger model, the teacher, writes a reasoning trace and a final answer for each training question. The student is trained to produce that text under ordinary next-token loss. At student training time there is no rollout, no advantage, and no verifier in the loss. The verifier comes back only at evaluation.
For small models this is often the better of the two. The DeepSeek-R1 report found that distilling a strong teacher into a small student beat running reinforcement learning on that small student. The teacher has already done the search. The student only has to imitate a trace that ends at the right answer.
Hard and soft targets
Definition 8.1 (Hard distillation).Hard distillation is supervised fine-tuning on text the teacher generated. The target at each position is a token id. The student never sees the teacher’s distribution, only the chosen token.
Definition 8.2 (Soft distillation).Soft distillation trains the student to match the teacher’s distribution over the vocabulary, typically by a KL divergence at each position. It needs the teacher’s logits.
Definition 8.1 is the method this module implements. Definition 8.2 uses more information per token and costs more: the teacher and the student must share a vocabulary, and a full logit vector per position of a long trace is expensive to store. Public reasoning APIs usually return text, not logits. Hard distillation is the setup that matches those constraints, and it is how the smaller R1-distilled models were trained. A hybrid adds a KL term on top of the token targets when logits happen to be available. That is the classical Hinton distillation objective. It is optional here.
The dataset
Use the same 12,000 MATH training problems as in Module 6, held out from MATH-500. Send each problem to a teacher such as DeepSeek-R1. Store the trace and the final answer. The teacher in the book’s run is far above the student: on the order of 91 percent MATH-500 accuracy, against about 15 percent for the Qwen3 0.6B base model and about 51 percent for the official Qwen3 0.6B reasoning checkpoint. Those numbers are a ceiling and a reference, not a promise for a short training run.
Filter before training.
- Drop items whose teacher answer fails the verifier. Imitating a wrong trace is supervised learning of a mistake.
- Drop items that do not fit the context length after tokenization.
- Split a validation slice off the filtered set. The training loss will fall even if the student is memorizing traces. Validation loss is the check.
Wrap the stored fields in one string. A clear convention is the think span from Definition 7.2, then the answer:
<think>
...teacher trace...
</think>
...final answer with a boxed result...The tags are optional for the loss. They make the trace easy to hide in a user interface, and they match the format a GRPO model may already have been asked to produce. Use the reasoning tokenizer so the tags are single ids.
Figure 8.1 is the whole dataset decision. The question stays in the context so the student predicts the trace given the problem. It does not contribute terms to the loss.
The loss
Let the token ids be
Definition 8.3 (Masked cross-entropy).The distillation loss is the next-token loss on the teacher tokens only,
Definition 8.3 is standard supervised fine-tuning. The prompt tokens are masked out of the average, usually by setting their labels to an ignore index. Including them trains the model to reproduce the question, which it was not asked to do, and it dilutes the gradient on the trace.
The factor
A batch is a set of these pairs, padded to a common length. Padding positions carry the same ignore index as the prompt. The gradient is then an average over real teacher tokens.
Training and evaluation
Load the Qwen3 0.6B base weights. Train with a modest learning rate and more than one epoch over the filtered set. Watch three numbers:
- training loss, which should fall,
- validation loss, which should fall and then flatten; a widening gap is overfitting to the traces,
- greedy MATH-500 accuracy with the same grader as every other module.
Accuracy is the number that can be compared to the GRPO runs and to the base model. A student that copies the teacher’s style and misses the box has learned the wrong part of the string. Spot-check a few generations for the tags, the box, and whether the boxed value actually follows from the trace.
Distillation transfers whatever the teacher wrote. If the teacher’s trace is correct and padded with a ritual phrase, the student learns the ritual too. If the teacher is wrong on a class of problems and those items were not filtered, the student inherits the error. The filter in the previous section is part of the method, not a preprocessing detail.
Where this sits
Figure 1.1 now has all three branches filled in. Sampling more, at modules 4 and 5, does not change
Exercises: Homework 8.
Code
- Notebook: ch08_main.ipynb
- Generating teacher traces: ch08/02_generate_distillation_data
- Training scripts: ch08/04_train_with_distillation
- Loading the resulting checkpoints in Hugging Face: ch08/06_use_via_huggingface