Knowledge Distillation
August 2026 – Vladislav KruglikovKnowledge distillation is a teacher to student paradigm where a smaller student model learns not only from ground truth labels but from the full output distribution of a larger teacher effectively performing a lossy compression of the teacher’s behavior. This allows model compression and faster inference, often improves the quality of small models beyond standard training, and serves as a form of transfer learning by transferring uncertainty and structure learned by the teacher
Loss formulation
Distillation uses:
The weighting coefficient controls the trade off between factual correctness matching ground truth and imitation of the behavior of the teacher.
Hard labels
Hard labels are the standard ground truth answers the teacher model was originally trained on. They provide only the single correct target and do not encode uncertainty or relationships between alternative outputs.
Soft labels
Soft labels are the full probability distributions produced by the teacher model. They are typically obtained using temperature scaling which smooths the distribution and makes it less peaky exposing additional information. soft labels are more informative. Unlike hard labels soft labels convey:
- Uncertainty of the teacher.
- Relative similarity between tokens or classes
- Structured knowledge about which alternatives are plausible and which are unlikely.
For example if teacher outputs 70% for cat and 25% for dog and 5% for bird this soft distribution represents distilled knowledge. It tells the student that the input is somewhat similar to both a cat and a dog but clearly not a bird. Information that is lost when training only on the hard label cat.
Temperature scaling
Teacher distribution is too peaky. Collapses to hard labels because low probability classes have almost no weights into gradient. Use temperature greater then 1 usually around 2 to 4 to smooth distribution. If model was overfitted to something we can use this to reveal other hidden knowledge model has to learn from.
Implementations
There are multiple implementations:
- Offline – the teacher LLM is run ahead of time to generate completions or next token logits for a fixed prompt corpus. These teacher outputs are stored and reused during student training. This mode is compute efficient for training and stable but requires significant storage.
- Online – the teacher LLM is queried on the fly during student training to produce next token distributions or completions. This avoids storing teacher outputs and allows adaptive supervision but requires running the teacher in the training loop which is expensive.
- Hybrid – offline data is generated before the student exists. We can use student later to filter examples with high loss and stop wasting signal on easy tokens. Hybrid distillation uses the evolving student to decide where high precision teacher supervision is needed. Teacher compute is expensive. Student compute is cheap. Use student to route teacher attention. Loss based routing. Run student forward pass. Compute CE loss on available target. Select examples with highest loss. Query teacher only for those. Interpretation is to ask teacher for those where it is only wrong.
Granulariry
- Sequence level. The teacher is used as data generator. The student is trained to reproduce sequences generated by teacher using CE treating them as ground truth. There is no matching of token distributions only matching of tokens. Used for correctness and low variance. Sometimes generate multiple completions per prompt and later keep all for diversity purpose or select best via filtering. Limitation is that it looses teacher uncertainty and alternative reasoning paths. In sequence level distillation we still use standard one hot cross entropy but the targets are sequences generated by the teacher. At each token position we treat the teacher selected token as having probability 1 and implicitly assign probability 0 to all alternative tokens rather than matching the full probability distribution of the teacher.
- Token level. The student is trained to match the next token distribution of the teacher at each position rather than only copying the chosen token of the teacher. Instead of assuming probability 1 for the generated token the student learns from the entire soft distribution produced by the teacher.
Vocabulary compatibility
Token-level logit matching is straightforward only when the teacher and student output spaces align. If they use different tokenizers or vocabularies, their logits refer to different tokens and cannot be compared directly. Distillation then requires token mapping, sequence-level supervision, or another method for aligning the two output spaces.
Logit truncation
Teacher outputs are usually stored as logits and not probabilities because logits more numeraiclly stable and temperature scaling is applied to logits thus softmax later can be recomputed with different temperatures.
Storing full logits over the entire vocabulary is prohibitively expensive. Most probability mass is covered by top probable tokens (that is also called popularity bias). Tail tokens contribute little to gradients. KL over top tokens is a good approximation of full KL.
- Top k
- Top p
- Sampled tokens
Quality based selection
Filtering step applied to teacher generated data. It decides which teacher outputs are kept for training and which are discarded. Distillation blindly imitates the teacher. That means hallucinations, incorrect reasoning, low quality or inconsistent outputs will be copied by the student unless filtered out. Quality based selection exists to remove junk before training.
- Teacher generates one or more candidate completions for a prompt.
- Each candidate is scored or evaluated.
- Low quality candidates are rejected.
- Only accepted samples are used for distillation.
The student never sees rejected outputs.
Common signals:
- Teacher self consistency. Generate multiple answers. Keep answers that agree with each other.
- Teacher confidence. Low entropy or high likelihood under teacher. Reject uncertain or unstable outputs.
- Reward model or classifier. Score outputs for correctness, helpfulness and safety. Keep top scoring samples.
- Rule based filters. Length constraints. Format checks. Known failure patterns.
On policy distillation of language models
Suppose the prompt contains tokens and the completion contains tokens from positions through . If the teacher generates the completion
then, when the student computes its distribution at position , it is conditioned on the prompt and the teacher's preceding completion tokens:
The student is therefore trained to answer the question «What would you predict after the teacher's previous tokens?» At inference, however, the student is conditioned on its own preceding tokens:
There is no mismatch at the first completion position because both models are conditioned only on the prompt. The mismatch begins after the student chooses a different token from the teacher. From that point onward, ordinary distillation trains the student on prefixes it may not encounter when generating by itself.
On-policy distillation instead generates a student completion first and compares the teacher and student distributions under the same student prefix:
The student is thus trained under its own preceding tokens, matching the situation it faces during inference.
In paper authors suggest:
- Student generates a response
- Feed that response to the teacher using teacher forcing
- At every response position , obtain both models’ full next-token distributions conditioned on the same student prefix.
- Compute KL over the vocabulary
Industry
Gemma 3 Technical Report:
- Gemma 3 27B is distilled from a larger non released internal Gemini teacher not from another Gemma model.
- 256 logits per token weighted by teacher probabilities. All other tokens are set to zero probability. Distribution is renormalized.