TL;DR
DistilBERT is a small language model trained to imitate BERT, a larger one. It has the same general design as BERT but half as many layers, and it is trained with three losses at once: match the teacher's soft word probabilities, predict the masked words as usual, and align the direction of the hidden states with the teacher's.
The result has about 40 percent fewer parameters, runs about 60 percent faster at inference, and keeps about 97 percent of BERT's score on the GLUE benchmark.
Builds on: BERT: Pre-training of Deep Bidirectional Transformers, Knowledge Distillation · Leads to: smaller pre-trained encoders trained with distillation, and the on-device question answering the authors demonstrated
Why it mattered
Pre-trained transformers such as BERT became the default starting point for language tasks, but they are large and slow to run. The paper's question was whether a much smaller model could keep most of that quality when used the same way: pre-trained once, then fine-tuned on each task.
The authors built a general-purpose student, not a task-specific one, so a single distilled checkpoint can be fine-tuned for classification and question answering. They also ran a proof-of-concept on-device question-answering experiment, which the abstract reports as a demonstration rather than a benchmark.
The problem
BERT-base has 12 transformer layers and about 110 million parameters. Running it costs memory and time on each query, and that cost matters on phones and in any service with a tight latency budget. Simpler compression, such as training a small model from scratch on the same labels, loses a lot of accuracy, because the labels say only which word was hidden, not which other words were plausible.
Knowledge distillation addresses the second point. A teacher assigns a probability to every vocabulary word at each masked position, and that full distribution carries information about which words are close in meaning. The student learns from the whole distribution rather than from a single correct word. The paper's job was to apply this to pre-training, so the student is a general model, and to choose the student's architecture and starting point so it trains well.
How it works
Here is one tiny instance of the distillation signal. The teacher has three candidate words for a masked slot, with raw scores (logits) . The student produces its own distribution from its own scores.
- Turn the teacher's scores into probabilities with a temperature : divide each score by and apply the softmax. The scores become .
- Exponentiate: , , . Sum: .
- Divide: the teacher's soft targets are . Check: they sum to .
- Without the temperature the same scores give , which is sharper. The temperature spreads probability onto the less likely words, which is the information the student needs.
- The student, at the same temperature, produces . Its distillation loss is the cross-entropy between the two: . Training lowers this number by moving the student's probabilities toward .
- At inference the temperature is set to 1, which recovers the ordinary softmax. The student's output is then used as a normal classifier.
- The full objective adds two more terms, described in the math section, and the student's layers are initialised from the teacher's by taking one layer out of two.
flowchart LR T[BERT teacher, 12 layers] --> SOFT[Soft targets at temperature T] T --> HID[Teacher hidden states] S[DistilBERT student, 6 layers, initialised from every other teacher layer] --> SL[Student predictions at temperature T] S --> SH[Student hidden states] SOFT --> LCE[Distillation loss] SL --> LCE D[Masked text] --> LMLM[Masked language modelling loss] S --> LMLM HID --> LCOS[Cosine loss on hidden states] SH --> LCOS LCE --> TOT[Total loss] LMLM --> TOT LCOS --> TOT
The math
The softmax with temperature is
where is the model's score for class and controls how smooth the output is. The same is used for the teacher and the student during training, and at inference.
The distillation loss
With teacher probabilities and student probabilities , the distillation loss is
This is the Cross-Entropy Loss between the teacher's distribution and the student's. It is minimised when for every word.
The masked language modelling loss
The student is also trained on the original task, predicting the hidden words. Its loss is the usual Masked Language Modeling loss against the true words. The paper's ablation study finds that this term adds little on top of the two distillation losses, so the teacher's signal does most of the work.
The cosine loss and the total
The cosine loss pushes the direction of the student's hidden vector toward the teacher's at each token. Writing for the Cosine Similarity of the two vectors, a loss that is zero when they point the same way is
The final objective is a linear combination of the three terms,
with weights chosen by the authors. Because the student uses Transformer layers, its hidden vectors have the same size as BERT's, which is what makes the direct comparison of and possible.
Results
The headline results are from the abstract: the student has about 40 percent fewer parameters than BERT, keeps about 97 percent of BERT's score across the language understanding benchmark (GLUE), and runs about 60 percent faster at inference. The ratio of 97 percent is an average over tasks, so the per-task tables in the paper matter for any single use.
The main design evidence is the ablation study, which removes one training loss at a time. Removing the distillation loss hurts most, and the masked language modelling loss matters least. Read the paper's tables for the exact per-task and ablation numbers rather than relying on a summary.
Limits the paper states or implies:
- The student is a general model, so its gains come from pre-training. A task-specific distillation step can reduce the gap further but adds training cost.
- The 97 percent figure hides differences between tasks, so a team with one task should measure that task directly.
- The speed numbers depend on the hardware and batch size, and the on-device result is one proof-of-concept app.
Reading guide
Read BERT: Pre-training of Deep Bidirectional Transformers first for the teacher's architecture and masked training, and Knowledge Distillation for the soft-target idea. Then read the paper in this order.
- Introduction: the goal of a small general-purpose model. Needs Transformer.
- Knowledge distillation section: the teacher, the student, and the soft-target loss. Needs Softmax Temperature and Cross-Entropy Loss.
- DistilBERT section: the student architecture, its initialisation from the teacher, and the three losses. Needs Masked Language Modeling, Layer Normalization, Positional Encoding and Word Embedding.
- Experiments, GLUE benchmark: the per-task table. Needs Fine-Tuning for how each task is trained on top of the pre-trained model.
- Ablation study: which loss matters. Read it after the DistilBERT section.
- Related work: other compression methods. Optional on a first read.
Check yourself
QuestionThe teacher's raw scores are (2, 1, 0). Using T = 2, which word gets the largest soft target, and roughly what probability does it get?Show answer
Dividing by gives . Exponentials are , and , which sum to . The first word gets .
QuestionWhy does the paper use soft teacher probabilities rather than only the correct word as the training target?Show answer
The full distribution shows which other words the teacher considers plausible. Those small probabilities encode similarity between words, so the student learns more per example than from a one-hot label. The temperature makes these small probabilities larger, which is why it is used during training.
QuestionThe ablation study finds that dropping the masked language modelling loss changes results much less than dropping the distillation loss. What does that suggest about where the gain comes from?Show answer
Most of the gain comes from copying the teacher's output distribution, not from the original masked prediction objective. The masked loss still helps a little, but the teacher's soft targets carry most of the information the student uses.
QuestionSuppose a team needs a model for one task. The paper reports 97 percent of BERT's score as an average over tasks. What should they check before adopting DistilBERT?Show answer
They should measure DistilBERT on their own task rather than trust the average, since an average hides differences between tasks. They should also check the speed numbers on their own hardware and batch size, because the paper's 60 percent figure depends on that setup.