Paper commentary
DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter
- Problem Large pre-trained Transformer language models are costly to train, store and run, which makes on-device use difficult.
- Method Pre-trains a smaller student with a triple loss that combines masked language modeling, a distillation term over teacher soft targets and a cosine term on hidden states, and initializes the student from alternating teacher layers.
Why shrink a pre-trained language model
Pre-trained language models with several hundred million parameters have become a default starting point for many NLP tasks, and larger models tend to keep improving downstream results. The figure of parameter counts across recently released pretrained language models makes the growth in model size visible at a glance. Two costs follow from this growth: the energy needed to train ever-larger models, and the memory and computation that prevent running them on a device in real time.
The study asks whether a much smaller general-purpose model can be pre-trained so that it stays useful after fine-tuning. Most earlier distillation work targeted individual downstream tasks, whereas here distillation is applied while the model itself is pre-trained. The resulting model, DistilBERT, is reported as 40% smaller than BERT, keeping 97% of its language understanding and running 60% faster.
The later sections test the same model on benchmark accuracy, on speed and size, and on a mobile question answering application.
Learning from a teacher's full output distribution
A standard supervised objective pushes the predicted distribution toward one-hot gold labels, but the small probabilities that the model assigns to the wrong classes carry information about how well it generalizes.
The distillation term is written as \( L_{ce} = \sum_i t_i \log(s_i) \), where \( t_i \) is the probability the teacher assigns to class i and \( s_i \) the probability the student assigns to it. Both probabilities come from a softmax with temperature, \[ p_i = \frac{\exp(z_i/T)}{\sum_j \exp(z_j/T)} \] where \( z_i \) is the model score for class i and a larger T smooths the distribution.
The same temperature is used for teacher and student during training, and T is set to 1 at inference, which gives a standard softmax again.
The final objective is a linear combination of the distillation term with the masked language modeling loss, \( L_{mlm} \), which is the supervised part. A cosine embedding loss, \( L_{cos} \), is added because it pulls the directions of the student and teacher hidden-state vectors toward each other. Together these three terms form the triple loss used for pre-training.
Halving the depth and starting from the teacher
Linear layers and layer normalisation are heavily optimized in current linear algebra frameworks, so changing the hidden size costs less computation time than changing the number of layers at a fixed parameter budget, according to the authors' investigation. Reducing depth is therefore the lever chosen to shrink the model.
Initialization matters for the sub-network to converge, and the student shares its dimensionality with the teacher. The assumption behind this step is the common dimensionality of the two networks.
Pre-training data and compute
Training follows recent best practices for BERT pre-training: very large batches formed by gradient accumulation, up to 4K examples per batch, dynamic masking, and no next sentence prediction objective.
Pre-training ran on 8 16GB V100 GPUs for approximately 90 hours. The authors set this against RoBERTa, which they report required 1 day of training on 1024 32GB V100 GPUs, but the two runs use different hardware and budgets, so this is a reference point rather than a controlled comparison.
GLUE development set results
The evaluation uses the GLUE development sets, with each task fine-tuned without ensembling or multi-task fine-tuning. The ELMo baseline is an ELMo encoder followed by two BiLSTMs, with scores reported by the GLUE authors, whereas BERT-base and DistilBERT are medians of 5 runs with different seeds.
DistilBERT reaches a macro-score of 77.0 on the nine tasks, against 79.5 for BERT-base and 68.7 for ELMo. Table 1 lists the per-task scores behind these macro-scores.
Against ELMo, DistilBERT scores higher on seven of the nine tasks and ties on WNLI at 56.3. It is slightly lower on SST-2, at 91.3 against 91.5, and the largest margin is on STS-B, where the text puts the gap at up to 19 points of accuracy. Because the ELMo numbers are the GLUE authors' reported values rather than re-runs under the same seeds, the ELMo comparison is less controlled than the BERT-base one.
IMDb and SQuAD after fine-tuning
Table 2 reports IMDb test accuracy: DistilBERT reaches 92.82, against 93.46 for BERT-base.
On SQuAD 1.1 dev EM/F1, DistilBERT reaches 77.7/85.8, against 81.2/88.5 for BERT-base. The text places DistilBERT within 3.9 points of the full BERT model.
A second distillation step is added during fine-tuning, in which a BERT model already fine-tuned on SQuAD acts as an extra teacher term in the loss. This variant is reported as DistilBERT (D) at 79.1/86.9 EM/F1 in Table 2, while the text gives 79.8 F1 and 70.4 EM for the same setting, so the two reports do not agree.
Inference speed, size and on-device latency
Parameter counts are 180 million for ELMo, 110 million for BERT-base and 66 million for DistilBERT. The corresponding times are 895 seconds for ELMo, 668 seconds for BERT-base and 410 seconds for DistilBERT.
The timing runs on an Intel Xeon E5-2690 v3 Haswell CPU at 2.9GHz. DistilBERT has 40% fewer parameters than BERT and is 60% faster at inference under this protocol.
For on-device use, the authors built a question answering application for an iPhone 7 Plus and compared it with their previously trained BERT-base question answering model. The authors note that the size could be reduced further with quantization, which they did not apply in this comparison.
Which loss terms and initialization matter
Table 4 reports changes relative to the full triple loss with teacher weights initialization, measured as the GLUE macro-score. Removing the distillation loss changes the macro-score by -2.96, removing the cosine embedding loss changes it by -1.46, and removing the masked language modeling loss changes it by -0.31. Because a higher macro-score is better, each of these numbers is a decline when the component is removed.
The authors describe the masked language modeling term as having little impact, while the two distillation terms account for a large part of the performance. Replacing the teacher-based initialization with random weights changes the macro-score by -3.69 relative to the full triple loss. Each axis is reported only as a single macro-score change, so the table says nothing about individual GLUE tasks.
Position against other distillation work
Several earlier studies distill a fine-tuned BERT into a task-specific network, for example into an LSTM-based classifier or a smaller Transformer for reading comprehension. The authors found it beneficial to use general-purpose pre-training distillation instead, and to let the student learn from the teacher during pre-training.
Multi-distillation work combines an ensemble of teachers through multi-task learning, and a multilingual model has been pre-trained solely through distillation. The authors report that, in their ablation, using the teacher through initialization and additional losses gives substantial gains. Pruning attention heads and quantization are treated as orthogonal to this work.
Limitations
The GLUE comparison uses development sets, and BERT-base and DistilBERT scores are medians of 5 runs with different seeds, while ELMo scores are taken from the GLUE authors. Downstream results are single tables of IMDb test accuracy and SQuAD dev EM/F1 without per-run spread.
Speed is measured on one CPU setting with batch size 1 and on one iPhone 7 Plus question answering comparison. Quantization and pruning are not combined with DistilBERT in these experiments.
The SQuAD second-distillation result has two values in the paper, 79.1/86.9 EM/F1 in Table 2 and 79.8 F1 and 70.4 EM in the text. The RoBERTa training cost is a reference from another hardware setup and is not measured against DistilBERT under the same conditions.
Paper info
- Authors
- Victor Sanh, Lysandre Debut, Julien Chaumond, Thomas Wolf
- Venue
- arXiv · Preprint
- First published
- 2019-10-2
- Added
- 2026-10-20
- Updated
- 2020-3-1
- Version
- v4
- arXiv
- 1910.01108
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Victor Sanh, Lysandre Debut, Julien Chaumond, Thomas Wolf (2019). DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter. arXiv:1910.01108v4. https://arxiv.org/abs/1910.01108v4
BibTeX
@misc{sanh2019distilbert,
title = {{DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter}},
author = {Victor Sanh and Lysandre Debut and Julien Chaumond and Thomas Wolf},
year = {2019},
eprint = {1910.01108},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1910.01108v4}
}