Skip to content
GenAI Learn/Adapting Models: SFT, LoRA & Distillation
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Knowledge Distillation: Teacher and Student

4 min readChapter 4 of 4

You'll learn to

  • -Explain distillation as training a small student to imitate a large teacher
  • -Describe soft targets and what the temperature does to them
  • -Show why soft targets carry more information than a single right answer
  • -Decide when distillation, quantization or a smaller base model is the right way to cut cost

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

A big model is accurate and slow and expensive. A small model is fast and cheap and a bit worse. Distillation tries to get most of the accuracy of the first with the cost of the second. Instead of training the small model only on the correct answers, you train it to imitate the large one. Hinton and colleagues described the idea as compressing the knowledge of a large model, or a whole ensemble of models, into a single easier one to deploy.

An Analogy: A Master and a Trainee

A chess master asked for the best move says 'knight to f3'. A good teacher says more: 'knight to f3 is best, knight to c3 is nearly as good, and moving the queen is a blunder'. The trainee learns far more from that ranking than from the single best move. The master's full set of opinions, including how much worse each wrong answer is, is the extra information distillation passes on.

Soft Targets and Temperature

A classifier ends with a softmax that turns scores into probabilities. A confident teacher gives the right answer almost all the probability and the others almost none, so the small differences among the wrong answers are hidden. Dividing the scores by a temperature before the softmax spreads the probabilities out and reveals them. The student is trained to match this softened distribution, the soft targets, usually with a loss that measures the gap between the two distributions.

Press Run: soften a teacher, then train two students, one on hard labels and one on soft targets

At temperature 1 the teacher gives cat 0.81, dog 0.181, fox 0.005 and car 0.003. At temperature 4 the same scores become 0.449, 0.309, 0.129 and 0.114, so the preference for dog over fox over car is easy to see. The two students tell the story. Trained on the hard label alone, the student gives cat 0.998 and the three wrong answers exactly the same tiny share, so the spread among them is 0.000: it learned only which one is right. Trained on soft targets at temperature 2, it copies the teacher's whole ranking, with a spread of 0.252 among the wrong answers.

Distillation With Language Models

  • -Logit distillation matches the teacher's probabilities over the next token. It needs access to those probabilities, which hosted models often do not expose.
  • -Sequence-level distillation trains the student on text the teacher wrote. This is the synthetic data pipeline from the last chapter followed by supervised fine-tuning, and it is the common route when you only have the teacher's outputs.
  • -A student rarely beats its teacher. Measure it against the teacher on a held-out set of your own tasks, not on a general benchmark.
  • -Check the license of the teacher before you train on its outputs.

When is it the right lever? Distillation gives you a different, smaller model and costs a training project. Quantization shrinks the same model with no training and a small quality cost, so try it first. A smaller off-the-shelf model may already be good enough. Reach for distillation when you have a large, expensive model that does your task well, plenty of inputs to run it on, and a latency or cost target the large model cannot meet.

Decide the quality bar before you start: for example, 'within 2 points of the teacher on our held-out set at one fifth of the cost'. Without a bar, a distilled model is easy to declare a success.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!