Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A small model taught by a big one
A large model can teach a smaller one. Instead of learning only the right answers, the student copies the teacher’s full sense of likelihood: “probably engine, maybe motor, certainly not banana”. DistilBERT is BERT with half its layers, trained this way. It keeps almost all of BERT’s accuracy while running much faster. The research exercise shows that much of the benefit comes from the teacher labelling extra, unlabelled examples. The demo shows the attention each remaining layer computes.
Distilled models run on phones, cars and drones where large models cannot. Distillation is one of the most practical ways to deploy a capable model within a latency budget.
- 1. In distillation the student learns from…
- 2. DistilBERT has…
- 3. DistilBERT keeps about…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Softened outputs: p_T = softmax(z/T). For logits (2, 0): T = 1 gives (0.881, 0.119), and large T approaches (0.5, 0.5). Higher T reveals how the teacher ranks wrong answers (“dark knowledge”).
- Distillation loss (Hinton et al.):
- L = α T² KL(p_T^teacher ‖ p_T^student) + (1 − α) CE(student, labels);
- the T² factor keeps the soft term’s gradient comparable across temperatures, since ∂(T² KL)/∂z_student = T (p_T^student − p_T^teacher).
- DistilBERT’s training:
- masked-LM distillation loss, masked-LM label loss, and a cosine loss aligning student and teacher hidden states (1 − cos; 0 for parallel vectors);
- the student keeps BERT’s width (768) and 6 of its 12 layers, initialised from teacher layers 0, 2, …, 10;
- token-type embeddings and the pooler are dropped, leaving 66 M parameters.
- Speed: halving the layers gives at most 2×. With fixed overheads equal to 3 layer-times, the speedup is 15/9 = 1.67, close to the reported 60% faster.
- 1. softmax((2, 0)/1) first entry:
- 2. The T² factor keeps…
- 3. DistilBERT's parameters:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Distillation in code
Exercise 1.
- The distillation loss was zero when student equalled teacher.
- Its gradient matched T(p_s − p_t) numerically.
- A higher temperature raised the teacher’s probability for non-top classes.
Practice.
- Hugging Face
DistilBertForSequenceClassificationis a drop-in replacement for BERT classifiers. - To distil a task-specific model, fine-tune the teacher first, then train the student on teacher outputs over the training data plus extra unlabelled in-domain text (the transfer set). Exercise 2 shows the extra data is crucial.
- Temperatures of 2–5 and α around 0.5 are common. Tune on validation data.
- Related methods: TinyBERT distils attention maps and embeddings layer by layer; MiniLM distils the last layer’s self-attention relations.
with torch.no_grad():
t_logits = teacher(**batch).logits
s_logits = student(**batch).logits
loss = alpha * T * T * F.kl_div(F.log_softmax(s_logits / T, -1), F.softmax(t_logits / T, -1), reduction="batchmean") \
+ (1 - alpha) * F.cross_entropy(s_logits, labels)- 1. A transfer set is…
- 2. TinyBERT also distils…
- 3. PyTorch KL term uses student…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
What the teacher adds.
- Soft targets carry more information per example than a one-hot label: they say which wrong answers are plausible.
- Hinton et al. argued this “dark knowledge” lets students learn more from each example.
- Just as important, a teacher can label any input, so the student can train on unlimited unlabelled data drawn from the input distribution.
When soft targets on the same data do not help.
- If the task is deterministic and the teacher is confident, its soft targets on a training example are nearly one-hot. They then carry little beyond the hard label.
- Training on the same few inputs then overfits just as with labels.
- The gain in exercise 2 came almost entirely from evaluating the teacher on fresh inputs, which supplies the missing data.
Student design. Depth reduction with layer-skipping initialisation keeps the teacher’s width, so weights can be copied. Width reduction needs layer-wise alignment losses or projection layers.
- 1. 'Dark knowledge' refers to…
- 2. Soft targets of a confident teacher on a deterministic task are…
- 3. Initialising from every other teacher layer requires…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Where does distillation’s benefit come from?
History.
- C. Buciluă, R. Caruana and A. Niculescu-Mizil compressed ensembles into small models (2006).
- G. Hinton, O. Vinyals and J. Dean, “Distilling the knowledge in a neural network” (2015), introduced temperature-softened targets.
- V. Sanh, L. Debut, J. Chaumond and T. Wolf introduced DistilBERT (2019).
Exercise 2.
- Teacher: a 2-layer Transformer (d = 32, 4 heads) trained on three-hop pointer chasing (E12-6), reaching 0.992.
- Students: the same shape, each with only 128 labelled examples, trained for 500 steps:
- hard: the labels;
- soft: the teacher’s T = 2 outputs on the same 128 inputs;
- transfer: the teacher’s T = 2 outputs on fresh random inputs (an unlabelled transfer set).
| model | three-hop accuracy |
|---|---|
| teacher | 0.992 |
| student, hard labels (128 examples) | 0.343 |
| student, soft targets on the same 128 inputs | 0.338 |
| student, soft targets on a fresh transfer set | 0.976 |
A second student seed gave 0.372, 0.351 and 0.897.
- Soft targets alone did not help. On this deterministic task the confident teacher’s soft labels carry almost nothing beyond the hard labels, and 128 examples are too few either way.
- The transfer set did: labelling fresh inputs with the teacher let the student nearly match it.
- In practice, distillation’s power is the teacher as a labeller of unlabelled data. Soft targets matter more when the teacher is uncertain or the task ambiguous.
- A narrower student (d = 16) could not learn the task even with the transfer set (0.47): distillation cannot exceed the student’s capacity.
Distillation in AI, robotics and aerospace
- On-board models: distil large perception or language models into small ones for flight computers and robots.
- Unlabelled logs as transfer sets: fleets generate huge unlabelled sensor and text logs. A teacher can label them for student training.
- Capacity limits: check that the student can represent the task; distillation cannot exceed the student’s capacity.
References
- G. Hinton, O. Vinyals, J. Dean (2015). “Distilling the knowledge in a neural network.” arXiv:1503.02531.
- V. Sanh, L. Debut, J. Chaumond, T. Wolf (2019). “DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter.” arXiv:1910.01108.
- C. Buciluă, R. Caruana, A. Niculescu-Mizil (2006). “Model compression.” Proc. KDD.
- X. Jiao et al. (2020). “TinyBERT: distilling BERT for natural language understanding.” Findings of EMNLP.