↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What DistilBERT is

Knowledge distillation trains a small student model to imitate a large teacher, so that the student keeps much of the teacher's accuracy at a fraction of its size and cost. DistilBERT is the student of BERT: a 6-layer model trained to reproduce what the 12-layer BERT-base predicts. Its authors report that it is 40 % smaller and 60 % faster than BERT while keeping 97 % of its language-understanding performance.

This page loads both released checkpoints, google-bert/bert-base-uncased (109.5 M parameters, 12 layers) and distilbert/distilbert-base-uncased (67.0 M, 6 layers). It gives them five sentences from the WikiText-2 test set, each with one word masked. It records how the student was built and the teacher's and student's full predictions at the mask, and computes DistilBERT's three training losses on these sentences.

12 layers → 6

The student keeps BERT's width (768), heads (12) and 30,522-word vocabulary and halves the depth. It also drops the token-type embeddings and the pooler. Before distillation, the student's layers were initialised by copying teacher layers. The DistilBERT paper says "one layer out of two"; Hugging Face's extract_distilbert.py, the script that built the released model, copies teacher layers 0, 2, 4, 7, 9 and 11 into student layers 0–5, together with the embeddings.

The 12 layers → 6 panel shows that this start is still visible in the released weights. The cosine similarity between each student layer's weights and each teacher layer's is highest, by far, for the layer it was copied from, and near 0 for every other. The word embeddings are still close to BERT's (similarity 0.956), and the position embeddings are BERT's to within . The hidden-states view compares the activations after every layer instead. Only the last pair is trained to match (see below), and the rest follows from the shared start.

Soft targets and temperature

A trained model's output says more than its top answer. For "After retiring from [MASK] he settled in Buffalo", the teacher gives "football" 25 % and "baseball" 19 %, which tells the student which words are interchangeable here, something a one-hot label cannot. To bring this out, both models' logits are divided by a temperature before the softmax:

At these are the models' own predictions; a higher flattens them and spreads probability over the runners-up. The Soft targets at [MASK] · T = … panel draws the teacher's and the student's top words at the selected temperature, with each distribution's entropy. At , "Robert Boulter is an English film, television and theatre [MASK]." gets "actor" from the teacher at 78 % and from the student at 85 %. At both spread out to an entropy above 6 nats. Every token shades each token of the sentence by the teacher–student KL divergence at that temperature.

The three losses

DistilBERT is trained with a weighted sum of three losses, shown in The three losses panel:

  • , the distillation loss: the student matches the teacher's soft targets at , averaged here over every token of the sentence.
  • , the masked-language-model loss: BERT's own objective, of the true word at . During training 15 % of tokens are picked for it; here one word is masked.
  • , the cosine loss: between the student's and the teacher's last hidden states (768 numbers each), averaged over tokens, which keeps the two representations aligned.

The weights 5, 2 and 1 and come from Hugging Face's distillation README and train.py, and the losses are those of its distiller.py. The factor comes from Hinton et al.: the gradients of the soft-target loss shrink as , so multiplying by keeps them the same size as changes. On all five sentences as one batch of 72 tokens, , , and .

Where the student differs

The five sentences table lists each sentence with the teacher's and the student's top word at and the per-sentence losses. The student agrees with the teacher where the teacher is sure ("actor", "homeless"). It also inherits the teacher's mistakes: for "guitar strings, [MASK] lines and vocals" both prefer "bass" to the true "piano", the teacher at 66 % and the student at 46 %. Where the student goes its own way the losses show it. For "Winds also tore the [MASK] off an ice plant", the teacher gives "roof" 65 %, while the student's top word is "snow" at 7 %, its distribution nearly flat (entropy 6.30 nats at ), and at that sentence has the largest KL at the mask.

Where distillation came from

Buciluǎ, Caruana and Niculescu-Mizil (2006) compressed large ensembles into single small networks by training them on the ensemble's labels. Hinton, Vinyals and Dean (2015) introduced the temperature and the soft-target loss, and named the method distillation. DistilBERT (Sanh et al., 2019) applied it during pretraining with the triple loss above, and the distilled model was trained on the same English Wikipedia and Toronto Book Corpus data as BERT. TinyBERT went further, also matching the teacher's attention maps and intermediate hidden states layer by layer. Large language models are now often distilled through their sampled text instead, as the R1-Distill tab shows.

Papers

  1. DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter Victor Sanh, Lysandre Debut, Julien Chaumond, Thomas Wolf · arXiv 2019 Introduces DistilBERT: a 6-layer student initialised from BERT’s layers and trained with distillation, MLM and cosine losses.
  2. Distilling the Knowledge in a Neural Network Geoffrey Hinton, Oriol Vinyals, Jeff Dean · arXiv 2015 Soft targets at a temperature T and the T² factor, the distillation loss DistilBERT uses.
  3. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova · NAACL 2019 BERT, the teacher, and its masked-language-model objective.
  4. Model compression Cristian Buciluǎ, Rich Caruana, Alexandru Niculescu-Mizil · KDD 2006 Trains a small network to reproduce a large ensemble’s outputs, the idea distillation grew from.
  5. Pointer Sentinel Mixture Models Stephen Merity, Caiming Xiong, James Bradbury, Richard Socher · ICLR 2017 Introduces the WikiText data sets; the five sentences come from WikiText-2’s test set.
  6. TinyBERT: Distilling BERT for Natural Language Understanding Xiaoqi Jiao et al. · Findings of EMNLP 2020 Distils BERT layer by layer, matching attention maps and hidden states as well as outputs.

See also