🧠
Knowledge Distillation
Visual Summary β€” Post 37
Incorrect password
Overview › Teacher→Student › Temperature › Math › Types › Modes › Results › LLMs

Knowledge Distillation

Teaching a small, fast student model to think like a large, slow teacher β€” by learning not just the right answers, but the teacher's confidence patterns across all possible answers.

40%
DistilBERT size reduction
97%
Performance retained
7.5Γ—
TinyBERT compression
60%
Faster inference
2015
Hinton et al. paper
Core idea: A large teacher model has learned rich internal representations β€” not just which class is correct, but how similar different classes are to each other. A student model trained on hard labels (one-hot targets) misses all of this. Knowledge distillation lets the student learn from the teacher's soft probability distribution β€” capturing the full picture of what the teacher has learned.
The Problem
Large models achieve high accuracy but are too slow and memory-intensive for deployment. Smaller models trained from scratch fail to match their performance β€” they lack the rich representations the large model developed.
The Solution
Train the small student to mimic the large teacher β€” not by copying weights, but by learning from the teacher's output distribution. The teacher's soft probabilities carry far more information than hard one-hot labels.
The Result
Students that approach β€” and sometimes match β€” teacher performance at a fraction of the size. DistilBERT is 40% smaller, 60% faster, and retains 97% of BERT's language understanding. TinyBERT is 7.5Γ— smaller with 9.4Γ— faster inference.
The Human Analogy
Hard label learning is like a student who only learns the final exam answers β€” "the answer to Q3 is C." No understanding of why C is right or how close B was.

Knowledge distillation is like learning from an expert teacher who says "C is clearly right (70%), but B is also plausible (20%) because these two concepts overlap in this way." The student learns the structure of the problem, not just the answer.
Hinton's original example: An image of a "2" in MNIST. Hard label: [0,0,1,0,...]. Teacher's soft output: 2: 0.91, 3: 0.06, 7: 0.02...

The soft output reveals that 2s look somewhat like 3s and 7s β€” a structural relationship the student would never learn from one-hot labels alone. This is what Hinton calls "dark knowledge" β€” the information hidden in the near-zero probabilities.

Teacher β†’ Student

The fundamental setup: a trained teacher generates soft targets that the student learns from β€” alongside (or instead of) the original hard labels.

The Distillation Setup
Hard Labels (One-Hot)
Training signal: 1.0 for the correct class, 0.0 for all others. The student learns nothing about the relationship between classes β€” only which one is right.
Soft Targets (Teacher Output, T=4)
The teacher distributes probability across similar classes. Every near-zero value is a signal: "class 3 is somewhat plausible, class 7 less so." This is the dark knowledge that makes distillation work.
Why soft targets work better than hard labels for the student: Soft labels provide an O(CΒ²) information signal per example β€” encoding the full similarity structure between C classes β€” while hard labels provide only O(C) bits per example. For a 1000-class problem, that's potentially 1,000,000Γ— more information per training example.
Dark Knowledge β€” What Hides in Near-Zero Probabilities
Class similarity
A "2" is more likely to be confused with "3" than with "8". The teacher's soft probabilities encode this β€” and the student learns it implicitly.
Uncertainty
The teacher's confidence level on a given example tells the student how hard the example is β€” enabling the student to weight its attention appropriately.
Generalization structure
The patterns of confusion across the training set encode the geometry of the decision space β€” which the student absorbs as inductive bias without seeing the teacher's internals.

Temperature Scaling

Temperature T controls how "soft" the teacher's output probabilities are. Higher T spreads probability more evenly β€” revealing more dark knowledge. Lower T sharpens toward a hard prediction.

Soft softmax: p_i = exp(z_i / T) / Ξ£_j exp(z_j / T) T = 1 β†’ standard softmax (sharp, close to one-hot) T > 1 β†’ softer distribution (more information in near-zero classes) T β†’ ∞ β†’ uniform distribution (maximum entropy) T < 1 β†’ sharper than standard (more confident)
Interactive Temperature Explorer
Temperature T T = 1
Entropy: β€”
Top class confidence: β€”
Dark knowledge visible: β€”
T = 1
Standard Softmax
The teacher behaves as in inference β€” one class dominates. Near-zero probabilities carry almost no gradient signal. The student essentially learns from hard labels.
T = 3–7
Hinton's Sweet Spot
The original paper uses T=3–8 for most experiments. Probability spreads enough to make class similarities visible while maintaining useful signal. The gradient scaling tΒ² ensures proper weighting.
T β†’ ∞
Uniform Limit
At very high T, all classes approach equal probability. At this limit, distillation reduces to directly matching the teacher's logits β€” equivalent to the logit matching in Bucilua et al. (2006) model compression.
Important implementation note: When using temperature T during training, the distillation loss must be multiplied by TΒ² to compensate for the reduced gradient magnitude at higher temperatures. This keeps the effective gradient scale constant regardless of temperature choice.

The Math

Three equations define the full distillation framework. Each has an intuitive interpretation alongside the formalism.

Loss Weight Tuning β€” Ξ± controls the teacher/label balance
Ξ± (teacher weight) Ξ± = 0.7
The tΒ² Scaling Factor β€” Why It's Necessary
When the temperature T is raised, the soft softmax outputs become smaller in magnitude. This means the gradients flowing back to the student are also smaller β€” by a factor of 1/TΒ².

Multiplying the distillation loss by TΒ² compensates for this: it keeps the gradient magnitude consistent regardless of the temperature setting, letting you compare results across different T values fairly.
High-Temperature Limit β†’ Logit Matching
At very high T, the soft distillation loss approaches:

L_KD β‰ˆ (1/2N) Ξ£ (z_i βˆ’ αΊ‘_i)Β²

This is exactly the mean squared error between student and teacher logits β€” identical to the earlier model compression approach (Bucilua et al., 2006). KD at high T is therefore a principled generalization of direct logit matching.

Three Types of Knowledge

The Hinton paper focuses on response-based knowledge. But subsequent work identified two additional types β€” what the teacher's intermediate layers know, and how inputs relate to each other.

πŸ“€
Response-Based
Learn from the teacher's final output layer β€” soft probabilities or logits. This is Hinton's original formulation.
πŸ”¬
Feature-Based
Match intermediate layer activations β€” not just final outputs. TinyBERT uses this to distill attention maps and hidden states.
πŸ”—
Relation-Based
Match the relationships between different examples or layers β€” correlation matrices, flow of solution, inter-class distances.
Which Type to Use?
Property Response-Based Feature-Based Relation-Based
Teacher access neededOutput onlyInternal layersInternal layers
Architecture constraintNone β€” any studentSame/similar architecturePartial constraint
Information richnessMediumHighHigh
Implementation complexitySimpleComplexComplex
Best forAPI-only teachers, cross-architectureTransformer-to-Transformer (TinyBERT)Few-shot, graph, metric learning

Three Training Modes

How and when the teacher is used during student training defines three distinct distillation paradigms β€” each with different requirements, advantages, and use cases.

Offline Distillation
πŸ“š
Process: Train the teacher fully, freeze it, then train the student on teacher-generated soft labels.

Advantage: Simple, decoupled β€” teacher and student training are independent.

Disadvantage: Teacher's knowledge is static. Student cannot influence what the teacher attends to.

Used by: Original Hinton (2015), DistilBERT
Online Distillation
⚑
Process: Teacher and student train simultaneously. The teacher updates based on the student's progress, creating a dynamic curriculum.

Advantage: No pre-training overhead. Teacher adapts to student's learning state.

Disadvantage: More complex training setup. Unstable if teacher isn't significantly stronger than student.

Used by: Deep Mutual Learning (DML), online ensemble methods
Self-Distillation
πŸͺž
Process: The model teaches itself β€” either earlier layers teach later ones, or later epochs teach earlier ones. No separate teacher needed.

Advantage: No teacher model required. Can regularize and improve any network.

Disadvantage: Limited by the model's own knowledge β€” cannot exceed its capacity.

Used by: Born-Again Networks, Progressive Self-Distillation
Hinton's Specialist Networks β€” Ensemble Distillation at Scale
The original paper also introduces a novel ensemble type: one full generalist model + many small specialist models, each trained to distinguish fine-grained subsets of classes.

Example: For ImageNet with 1,000 classes, specialists each focus on confusable subsets β€” e.g., specialist for dog breeds, specialist for bird species. Each trains fast in parallel.

Distillation role: The ensemble of specialists is then distilled into a single student that captures the fine-grained discrimination of all specialists.
Generalist model
Trained on all 1,000 classes. Handles most inputs. Routes uncertain cases to specialists.
Specialist models (Γ—K)
Each trained on a confusable subset + a halved "dustbin" of other classes. Trains on 1/K of total data β€” rapid parallel training.
Distilled student
Single model trained on the combined soft targets of the full ensemble β€” capturing both generalist and specialist knowledge.

Real-World Results

From Hinton's original MNIST experiments to production speech models to BERT compression β€” knowledge distillation consistently delivers near-teacher performance at student-model cost.

MNIST β€” Hinton's Surprising Result β€Ί
Hinton's original experiment trained a student on MNIST with no knowledge that 3 exists as a digit β€” the student never saw a "3" in its training data. Yet after distillation from a teacher that had seen 3s, the student correctly classified most "3" test examples. The soft targets from the teacher β€” which showed that "2"s and "8"s look somewhat like "3"s β€” transferred enough information about the "3" manifold for the student to generalize. This is the clearest demonstration of dark knowledge transfer.
Speech Recognition β€” Commercial Scale β€Ί
Hinton et al. applied distillation to Google's commercial speech recognition system. A single distilled acoustic model matched the performance of an ensemble of 10 models β€” while being 10Γ— smaller. The distilled model achieved a Word Error Rate (WER) that was significantly better than any single model trained from scratch, and matched the ensemble on held-out test sets.
DistilBERT β€” 40% smaller, 97% performance β€Ί
Sanh et al. (2019) distilled BERT-base (110M params) into DistilBERT (66M params) using offline response-based distillation with a triple loss: language modeling + distillation + cosine distance. Result: 40% smaller, 60% faster at inference, 97% of BERT's GLUE benchmark performance retained. Crucially, distillation happened at pre-training time β€” not just fine-tuning β€” allowing knowledge transfer of the full language model, not just task-specific behavior.
TinyBERT β€” 7.5Γ— compression, 9.4Γ— faster β€Ί
Jiao et al. (2019) used feature-based distillation (matching attention matrices and hidden states layer-by-layer) plus a two-stage approach: general pre-training distillation + task-specific distillation. The 4-layer TinyBERT (14.5M params vs BERT-base 110M) achieves 96.8% of BERT-base performance on GLUE while being 7.5Γ— smaller and 9.4Γ— faster. Layer-wise distillation is key β€” the student learns the structure of each transformer layer, not just the final output.
MobileNet / EfficientNet β€” Vision Models β€Ί
Distillation has been central to deploying vision models on mobile devices. MobileNet variants trained with distillation from ResNet/EfficientNet teachers achieve near-teacher accuracy at 4–10Γ— compression. Google's use of distillation in production computer vision pipelines β€” image search, photo organization, OCR β€” represents billions of daily inference calls running on compressed student models.
Consistent pattern: Across domains (vision, NLP, speech), distillation students trained on soft targets consistently outperform students of the same size trained on hard labels β€” often matching teachers 3–10Γ— their size.

Knowledge Distillation in the LLM Era

Distillation has evolved significantly for large language models β€” where the "teacher" may be an API-only model, the student architecture differs substantially, and the knowledge to transfer is generative rather than classificatory.

LLM Distillation Results β€” Size vs. Performance
Challenges Unique to LLM Distillation
Vocabulary mismatch
Teacher and student may use different tokenizers β€” making direct token-level KD impossible. Requires mapping or using teacher-generated text as training data rather than raw logits.
API-only teachers
GPT-4, Claude, Gemini expose only sampled outputs β€” not logits. Students must learn from sequences rather than probability distributions, losing the soft target advantage.
Exposure bias
In generation tasks, the student is trained on teacher-generated sequences but must generate its own at inference β€” creating a train/test distribution mismatch that doesn't exist in classification KD.
Capacity gap
When the teacher is 100Γ— larger than the student, the student simply cannot represent everything the teacher knows. Too large a gap leads to "student confusion" β€” the soft targets are too spread to be useful signals.
Data provenance
Using a commercial API's outputs (e.g., GPT-4) to train a smaller model may violate the API's Terms of Use β€” exactly the issue identified in the Data Provenance blog. Alpaca was trained on GPT-3 outputs and carries this restriction.
Hallucination transfer
A student that faithfully learns from a teacher also learns the teacher's hallucinations and biases. If the teacher confabulates, the student learns to confabulate β€” often more confidently than the teacher did.

Paper Sources

This visual summary is based on the following papers.

Primary Reference
Hinton, Vinyals & Dean (Google Brain) β€” "Distilling the Knowledge in a Neural Network" β€” NIPS 2014 Deep Learning Workshop, arXiv: 1503.02531 (2015). Introduces temperature scaling, soft targets, dark knowledge, and specialist network ensembles. The foundational paper that named and formalized knowledge distillation.
β†— arXiv 1503.02531
Key Follow-on Papers
Sanh et al. β€” "DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter" β€” NeurIPS 2019 Workshop. arXiv: 1910.01108. 40% size reduction, 97% performance, 60% faster. Introduces pre-training-time distillation for language models.
Jiao et al. β€” "TinyBERT: Distilling BERT for Natural Language Understanding" β€” EMNLP 2020. arXiv: 1909.10351. Layer-wise Transformer distillation. 7.5Γ— smaller, 9.4Γ— faster, 96.8% of BERT-base GLUE performance.
Gou et al. β€” "Knowledge Distillation: A Survey" β€” IJCV 2021. arXiv: 2006.05525. Comprehensive taxonomy of response-based, feature-based, and relation-based distillation. Covers offline, online, and self-distillation training schemes.
Continue Learning
Next
Speculative Decoding β€” Fast Inference
Related
Data Provenance β€” Training Data Licensing
Related
Agent Harness Engineering
Related
DSPy β€” Programmatic LLM Pipelines
All Posts
Visual Summary Home