What it is used for

Distillation is used when the best model is too large, too slow, or too expensive to serve a large number of users. Its founding paper opens with a simple observation: training many models and averaging their predictions almost always improves accuracy, but using that whole ensemble to answer every question is cumbersome and computationally expensive. Distillation compresses the knowledge of the ensemble into a single small model that is easy to deploy.

Distillation therefore becomes a bridge between the training stage and the deployment stage. At the training stage, computation may be as large as needed and time as long as needed to extract patterns from data. At the deployment stage, answers must be fast and cheap, often on small devices. With distillation, what the giant model dug out during training can be enjoyed through a small model at deployment. The uses shown in the founding paper include handwriting recognition and the acoustic model of a heavily used commercial voice search system, and its best known later use is shrinking large language models so they can run on devices with limited capacity.

How to use it

The working of distillation can be read as three steps. In the first step, the teacher is trained the ordinary way until its performance is as good as possible; this teacher can be a single very large model or an ensemble of many models whose answers are averaged. In the second step, the teacher is asked to answer a set of examples, called the transfer set, and its whole probability spread for each answer is recorded as the soft target. This transfer set can be the original training set, which the paper found works well, or entirely unlabeled data. In the third step, the student is trained to match those soft targets, and usually also keeps training on the correct labels with a much lower weight.

One important setting in the second and third steps is temperature. The teacher's answers are processed by an output layer called the softmax, which converts the raw score of each answer, called the logit, into a probability. Temperature is a dial on that softmax: its normal value is 1, and raising it makes the probability spread softer, meaning the differences between similar answers become more visible. The paper uses the same high temperature when the teacher produces the soft targets and when the student learns to match them; after training is finished, the student goes back to using a temperature of 1 as usual. With high-temperature soft targets, each training example gives the student far more information than a single firm label, so the student can often be trained on much less data and with a much higher learning rate.

Real examples

The following examples are not invented for this article. All three are recorded in papers written by their own researchers, and their numbers are the results those papers reported in their time.

The first example is the MNIST handwritten digit recognition experiment in the 2015 paper. A large network made 67 errors on the test set. A smaller network, trained the ordinary way, made 146 errors. The same small network, when trained to match the large network's soft targets at a temperature of 20, made only 74 errors, nearly matching its teacher. A further experiment in the same paper even removed all examples of the digit 3 from the transfer set, so that for the student the 3 was a digit it had never seen; after one small adjustment, the student still recognized 98.6 percent of the test 3s correctly, because the teacher's soft targets had taught it that some handwritten 2s look like 3s and others look like 7s.

The second example is speech recognition. The same paper distilled an ensemble of 10 acoustic models, the models that turn speech sounds into language units, into a single model for a system based on the Android voice search acoustic model of its time. The single baseline model reached a frame accuracy of 58.9 percent, the 10 model ensemble reached 61.1 percent, and the distilled single model reached 60.8 percent, so almost the whole advantage of the ensemble moved into the single model. On the word error rate measure, the distilled model recorded 10.7 percent, the same as its teacher ensemble and better than the baseline's 10.9 percent.

The third example is DistilBERT, a distilled version of the BERT language model published by Victor Sanh and colleagues in 2019. Its abstract states, word for word: "it is possible to reduce the size of a BERT model by 40%, while retaining 97% of its language understanding capabilities and being 60% faster". Unlike most earlier work, which distilled models for one specific task, DistilBERT was distilled during the pre-training phase, the phase that builds general language ability, so the small model can still be fine-tuned for many tasks just like BERT.

Practitioner level

For practitioners, the first thing to set is the temperature and the mixture of the training objective. The founding paper reports that when the student is much smaller than the teacher, intermediate temperatures work best: in the MNIST experiment, a student network radically shrunk to 30 units per layer worked significantly better at temperatures of 2.5 to 4 than at higher or lower temperatures. The reason is that too high a temperature forces the student to also match very negative logits, whose values are poorly constrained and may carry noise. The student's training objective is a mixture of two cross entropies, the measure of the gap between the student's answer spread and the spread it should have: cross entropy against the soft targets at high temperature, plus cross entropy against the correct labels at a temperature of 1 with a considerably lower weight, with the soft target contribution multiplied by the square of the temperature so the balance between the two does not change when the temperature is changed.

The second point is that matching logits directly, the method used by the pioneering work of Rich Caruana and his collaborators, turns out to be a special case of distillation at a very high temperature. The third point concerns deployment: a trained student is an ordinary model standing on its own; the teacher is no longer needed when answering, so all the extra cost appears only once, at the training stage.

Another recorded practical application is TinyBERT, published by Xiaoqi Jiao and colleagues in 2019, which distills BERT using a method designed specifically for the Transformer design together with two-stage learning, at the pre-training stage and at the task-specific stage. Its abstract reports that the 4 layer TinyBERT achieves more than 96.8 percent of its teacher BERT base model's performance on the GLUE benchmark, while being 7.5 times smaller and 9.4 times faster at inference.

Expert level

At the expert level, the core of distillation is to view knowledge as a mapping from input vectors to output vectors, not as the teacher's parameter values. What is transferred is not the contents of the teacher's head, but the way the teacher generalizes: for similar inputs, which outputs are reasonable. The relative probabilities of the incorrect answers carry this knowledge. The paper illustrates that an image of a BMW may only have a very small chance of being mistaken for a garbage truck, but that mistake is still many times more probable than mistaking it for a carrot; a ranking of comparisons of that kind is what teaches the student the similarity structure in the data.

Mathematically, the derivative of the soft target cross entropy with respect to each of the student's logits takes the form of the difference between the student's probability and the teacher's probability, divided by the temperature. In the limit of a temperature very high compared with the size of the logits, that form shrinks to the difference between the student's logit and the teacher's logit itself, so distillation becomes equivalent to minimizing half the squared difference between the logits, exactly the logit matching of its pioneers, provided the logits of both models have been zero-meaned separately for each case. At lower temperatures, distillation pays less attention to logits that are much more negative than the average, which can be an advantage because such logits are almost completely unconstrained by the teacher's training objective and could be very noisy, although those logits may also convey useful information; which effect dominates is an empirical question, and the paper's experiments show intermediate temperatures winning when the student is too small.

The same paper also introduces a new kind of ensemble: one or more full models plus many specialist models, each of which learns to distinguish the fine-grained classes that the full models often confuse. Unlike a mixture of experts, these specialist models can be trained rapidly and in parallel, and the knowledge of the whole ensemble can then still be distilled into a single model for deployment, so the cost of the ensemble is paid only at training time.

Limits and risks

The first limit is the student's capacity. The founding paper states that the student generally cannot match the soft targets exactly; the finding that intermediate temperatures work best appears precisely when the student is too small to capture all of the teacher's knowledge. Distillation compresses rather than multiplies, so some of the teacher's knowledge can be lost on the way, and how much is lost depends on the size gap and on how the training is done.

The second limit is that the student inherits the way the teacher generalizes, including its mistakes. The whole value of distillation rests on a teacher that generalizes well; the student is trained to generalize in the same way as its teacher, so a teacher judgment that goes wrong on some region of the data will also be taught to the student as a correct pattern. Quality checks therefore still have to be run on the student independently, not treated as settled because the teacher is good.

The third limit concerns the standing of the numbers. The numbers in this article are results reported by each paper in its time, on the test sets and hardware of its day; they show what those experiments achieved, not a guarantee that the same gap will appear on another model, data set, or device. Distillation also does not remove the teacher's cost: the large model or ensemble still has to be trained first, so the saving is felt at the repeatedly used deployment stage, not across the whole process.