18.4 Distillation with forward and reverse KL
A deployment can often use a smaller student model, provided it preserves the useful behavior of a larger teacher. Distillation trains the student to match the teacher’s output distribution token by token, which gives a measurable alternative to the preference- and reward-based training of the previous sections. The objective is a KL divergence between the two distributions, and the one real design decision is which direction of the KL to use: the two directions want different things from a student that cannot represent everything the teacher does.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- KL divergence: , the excess cost of predicting with ; nonnegative and asymmetric. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Temperature scaling: dividing logits by one fitted scalar to fix over- or underconfidence without changing the argmax. Read more: 13.8 Logistic regression (IRLS), ROC-AUC, calibration (ECE) in Mathematics
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Entropy: , the average surprise of a distribution. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- On-policy / off-policy: learning about the policy being followed versus learning about a different policy. Read more: 4.4 World models, multiple agents, and RLHF
- DPO: minimizing over preference pairs, equivalent to fitting the KL-regularized reward objective. Read more: 18.2 Direct preference optimization and IPO
- GRPO: a PPO-style RL method that replaces the critic with the group-normalized reward of several samples of the same prompt. Read more: 19.7 The training step and validation
This section defines both divergences, computes them by hand, and implements them in log space with temperature scaling. It builds on Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics, and for on-policy distillation on the samplers of Sampling and logit processors. The solution is distill.py (forward_kl, reverse_kl, and the shared _logsoftmax), and the contract is distill.pyi.
Learning objectives
- Compute forward and reverse KL between a teacher and a student by hand and say which is mass-covering and which mode-seeking.
- Implement both divergences stably from logits with a max-subtracted log-softmax.
- Explain why temperature is applied to both models and why the loss is multiplied by .
- Choose a direction for a given student and training setup.
Two divergences
Section titled “Two divergences”Write for the teacher’s probabilities, for the student’s, and for the distillation temperature. Forward KL is
and reverse KL is
Both are nonnegative and zero when the distributions agree. At temperature , the softmax uses logits divided by , and the loss is commonly multiplied by to compensate for the gradient scaling that temperature introduces.
The direction decides whose probabilities weight the log ratio. Forward KL weights by the teacher : wherever the teacher puts mass and the student does not, is large, so the student is pushed to cover every teacher mode (mass-covering). Reverse KL weights by the student : regions the student ignores cost nothing, so it can settle on one teacher mode (mode-seeking), emphasizing the modes it already selects.
Temperature and the factor
Section titled “Temperature and the T2T^2T2 factor”Temperature applies to both models before the divergence. Dividing both sets of logits by flattens both distributions, exposing the teacher’s ranking of wrong answers, the so-called dark knowledge. Temperature therefore changes the entropy of the target.
It also changes the gradient. The gradient of forward KL with respect to the student’s logits is , and for large the difference itself shrinks like . In that high-temperature regime the gradient therefore scales approximately like . Multiplying the loss by compensates for this scale effect; it does not guarantee that one learning rate works across all temperatures or students.
Working it by hand
Section titled “Working it by hand”For and , forward KL is . Reverse KL is . The two directions disagree on the same pair, and both become zero for equal distributions.
Designing the API
Section titled “Designing the API”forward_kl(student_logits, teacher_logits, temperature=1.0) and reverse_kl(...) take logits of the same shape, [..., V] with the vocabulary last, and return the mean divergence over all leading positions, multiplied by , as a float. In on-policy distillation the caller samples prefixes from the student, runs the teacher on those same prefixes, and passes both models’ logits at each position.
Building it step by step
Section titled “Building it step by step”Both divergences need log probabilities, not probabilities, because . Computing softmax and then log underflows a tiny probability to 0 and returns -inf; computing the log-softmax directly keeps every term finite. Log-softmax is stable after subtracting the max: for any , and choosing makes the largest exponent , so the sum cannot overflow and is at least 1.
def _logsoftmax(x, temperature): if temperature <= 0: raise ValueError("temperature must be positive") y = np.asarray(x, dtype=np.float64) / temperature # temperature first, on the logits m = np.max(y, axis=-1, keepdims=True) # per position, over the vocabulary # y - m - log Σ exp(y - m): exact, and exp never sees a positive argument return y - m - np.log(np.exp(y - m).sum(axis=-1, keepdims=True))Forward KL weights the log ratio by the teacher:
def forward_kl(student_logits, teacher_logits, temperature=1.0): log_s, log_t = _logsoftmax(student_logits, temperature), _logsoftmax(teacher_logits, temperature) if log_s.shape != log_t.shape: raise ValueError("student and teacher shapes differ") p = np.exp(log_t) # teacher probabilities are the weights # Σ_i p_i (log p_i - log q_i) per position, mean over positions, times T² return float(np.mean(np.sum(p * (log_t - log_s), axis=-1)) * temperature**2)Reverse KL is the same computation with the roles of the weights and the ratio swapped:
def reverse_kl(student_logits, teacher_logits, temperature=1.0): log_s, log_t = _logsoftmax(student_logits, temperature), _logsoftmax(teacher_logits, temperature) if log_s.shape != log_t.shape: raise ValueError("student and teacher shapes differ") q = np.exp(log_s) # student probabilities are the weights return float(np.mean(np.sum(q * (log_s - log_t), axis=-1)) * temperature**2)On [1000, -1000], _logsoftmax gives [0, -2000], so and every term is finite: the divergence of the input against itself is exactly 0. The full solution is distill.py.
Why this design
Section titled “Why this design”Each direction costs , dominated by the exponentials. The teacher forward pass is the real cost: for off-policy distillation its logits can be cached, while for on-policy distillation they must be recomputed on each batch of student samples.
Log space beats probability space because computing p * log(p/q) from probabilities fails exactly when distillation matters most: peaked teachers whose minority probabilities underflow. Log-softmax keeps the arithmetic exact over the full range of logits.
As for direction, forward KL is what classic logit distillation uses and is the right default when the student should hedge like the teacher. Reverse KL can favor concentrating on teacher-supported modes under capacity constraints. On-policy distillation specifies student-generated prefixes; it can use forward KL, reverse KL, or another justified discrepancy. Prefix source and divergence direction are independent choices, as explained in distillation data and deployment. The factor changes no minimizer, only the gradient scale; leaving it out silently shrinks the effective learning rate by .
What can go wrong
Section titled “What can go wrong”Three checks pin the implementation. The worked forward KL must be 0.19274..., which fixes both the arithmetic and the direction. Reverse KL on the same pair must be 0.22314..., different from forward KL, and zero for identical inputs. And extreme logits must stay finite: real logits can be in the hundreds, and [1000, -1000] against itself must give 0, not NaN.
The two direction bugs are easy to make and hard to notice, because both still produce a nonnegative number that goes down during training. Swapping teacher and student in forward KL silently computes reverse KL; forward_kl weights by p = exp(log_t), the teacher’s log-softmax. Weighting reverse KL by the teacher instead of the student computes forward KL; reverse_kl weights by q = exp(log_s), the student’s log-softmax.
Warning: Leaving out the max subtraction in
_logsoftmaxmakesexp(1000)overflow toinf, and the extreme-logit case returns NaN instead of 0.
Where this leads
Section titled “Where this leads”The post-trained capstone may use distillation instead of DPO or GRPO; it evaluates on the same held-out suite and records the teacher’s identity. In production, on-policy sequence-level distillation transfers behavior on student-generated prefixes (see the Hugging Face TRL distillation recipes), and teacher-student calibration tunes temperature, which changes both target entropy and gradient magnitude.
Further reading
Section titled “Further reading”- Hinton, Vinyals, and Dean, Distilling the Knowledge in a Neural Network (2015).
- Agarwal et al., On-Policy Distillation of Language Models: Learning from Self-Generated Mistakes (2024).
- Hugging Face TRL, the distillation trainer recipes.
Key terms
Section titled “Key terms”- distillation: training a student model to match a teacher model’s output distribution.
- forward KL: , weighted by the teacher; mass-covering.
- reverse KL: , weighted by the student; mode-seeking.
- distillation temperature: a divisor applied to both models’ logits before the softmax, flattening both distributions.
- on-policy distillation: distillation in which the teacher is evaluated on prefixes the student generated.
Summary
Section titled “Summary”- Forward KL penalizes teacher-supported modes the student misses.
- Reverse KL emphasizes modes the student already selects.
- Both are computed from a max-subtracted log-softmax, which stays finite for extreme logits.
- Temperature changes both target entropy and gradient scale; the factor restores the scale.
- On-policy distillation evaluates the teacher on student-generated prefixes.
Review questions
Section titled “Review questions”-
Why does forward KL punish a student that misses a teacher mode, while reverse KL does not?
Answer
In forward KL the term for token is weighted by . If the teacher gives mass and the student gives it almost none, is huge and weighted by a nonzero . In reverse KL the same term is weighted by , so the miss costs almost nothing.
-
What exactly would break if
_logsoftmaxdid not subtract the max?Answer
exp(1000)overflows toinf, the normalizer becomesinf, and the log probabilities become-infor NaN (inf - inf). The extreme-logit case would return NaN instead of 0. -
Why is temperature applied to the teacher as well as the student?
Answer
The divergence compares two distributions at the same temperature. Softening only the teacher would ask the student, at , to match a flatter target than the one it is evaluated against; softening both compares the two models’ rankings on equal terms.