AI Model Distillation Explained: Small Models, Big Smarts

In 2019, Hugging Face faced a familiar problem. BERT worked well, but it was too heavy for most production setups. Running it on a single CPU took too long, and running it on a GPU cost too much for a small team. The engineers did not train a new model from scratch. They taught a smaller model to copy BERT, and the result was DistilBERT: 40 percent smaller, 60 percent faster, and still keeping 97 percent of BERT language understanding, according to the paper that introduced it. That process has a name, and it now sits behind many of the fast, cheap AI tools you use every day.

The name is model distillation. A large model, called the teacher, passes its knowledge to a smaller model, called the student. The student never sees the full training data. It learns from the teacher output instead, and it ends up far smaller and far faster while keeping most of the teacher skill. I find it one of the most practical ideas in modern AI, because it turns a model you cannot afford to run into one you can ship.

Abstract visualization of artificial intelligence with glowing neural network nodes over a dark background

What Model Distillation Actually Does

Distillation compresses knowledge, not files. A teacher model holds patterns it learned from vast amounts of text. A student model studies the teacher answers and absorbs the patterns in a compact form. The student has fewer parameters, needs less memory, and answers faster. The goal is a model that behaves like the teacher on the tasks you care about, at a fraction of the running cost.

Think of a senior engineer mentoring a junior hire. The senior engineer does not hand over every book they ever read. They show the junior hire how they solve real tickets, which shortcuts work, and which mistakes to avoid. The junior hire learns the judgment, not the library. Distillation works the same way. The teacher shows the student how it answers, and the student learns the judgment.

The paper that started it all came from Geoffrey Hinton and his coauthors in 2015. Their paper, “Distilling the Knowledge in a Neural Network,” showed that a small model trained on the soft output of a large model can perform far better than the same small model trained on hard labels alone. Hinton argued that the probabilities a large model assigns to wrong answers carry useful information. A picture of a dog might get a 0.01 chance of being a cat and a 0.000001 chance of being a car. That gap tells the student that dogs resemble cats more than cars. Hard labels erase that detail. Soft targets preserve it.

How a Teacher Model Trains a Student

The training loop has three parts: a teacher, a student, and a set of examples. You feed the same input to both models. The teacher produces a probability distribution across possible outputs. The student tries to match that distribution while also learning from the correct label. A temperature setting softens the teacher distribution so the student can see the fine differences between second and third choices.

Temperature is the key control. At a low temperature, the teacher output looks like a hard label, with almost all probability on one answer. At a higher temperature, the distribution spreads out and reveals the teacher ranking of every option. The student learns more from a softened distribution, so researchers often train with a temperature above 1 and then test the student at a normal temperature of 1.

The loss function mixes two signals. One part measures how close the student output sits to the teacher soft targets. The other part measures how close the student output sits to the true label. A weight balances the two. In practice, teams tune that weight on a small validation set and keep the setting that holds accuracy while the model stays small.

Data matters as much as the math. The student needs examples that cover the task you want it to do. If you distill a summarization model, feed it the kind of documents your users send. If you distill a classifier, feed it the kind of messages your support queue receives. The teacher can label data you already have, which means you can build a training set without paying humans to label thousands of examples.

Why DistilBERT Still Matters

DistilBERT remains the clearest proof that distillation works in production. The Hugging Face team used a technique called knowledge distillation during pretraining, as described in the DistilBERT paper. They trained the student on the masked language modeling task, with BERT as the teacher, and added a cosine embedding loss to align the hidden states of the two models. The result kept 97 percent of BERT performance on the GLUE benchmark while cutting the parameter count from 110 million to 66 million.

The speed gain changed what teams could ship. A model that runs 60 percent faster fits on cheaper hardware, answers more requests per second, and costs less per request. For a support chatbot or a search feature, that difference decides whether the feature ships at all. I have seen teams drop a feature because the large model cost too much per query, then ship the same feature a week later with a distilled model.

DistilBERT also showed that distillation works before fine-tuning. You distill once, then fine-tune the student for your task like any other pretrained model. That pattern now appears across the field. Teams distill a general model, then adapt the small version for classification, extraction, or ranking. The student stays small through every step.

Distillation vs Fine-Tuning vs Quantization

People often mix up three ways to make a model cheaper to run. They solve different problems, and the table below shows where each one fits. Many teams combine them: distill first, then fine-tune, then quantize the result.

MethodWhat it changesModel sizeAccuracy costBest for
DistillationTrains a new, smaller model from a teacherMuch smallerSmall, often 1 to 3 percentShipping a fast model on limited hardware
Fine-tuningAdapts an existing model to one taskSame sizeNone on the target task, often betterTeaching a model your domain or style
QuantizationStores weights with fewer bitsSmaller on disk and in memorySmall with good methodsFitting a model into limited VRAM
PruningRemoves weights or neuronsSmaller, with sparse structureVaries, needs retrainingCutting a model that is already trained

Read the table as a menu, not a ranking. Fine-tuning teaches a model new behavior. Quantization shrinks the model you already have. Distillation builds a new model that behaves like a big one. When your problem is cost per request, distillation usually wins. When your problem is a model that does not know your product, fine-tuning wins. For background on fine-tuning a small model after distillation, read our guide to LoRA fine-tuning, which adapts large models with a fraction of the training cost.

Where Distillation Works Best

Distillation shines on narrow, repeated tasks. Classification, spam detection, intent routing, sentiment scoring, and document ranking all fit. These tasks have a clear input and a bounded output, so a student can match the teacher with ease. A support team that routes ten thousand tickets a day can replace a large model with a distilled classifier and cut its inference bill by half or more.

It also works well for on-device AI. A phone cannot run a 70 billion parameter model, but it can run a distilled model with a few hundred million parameters. That is why voice assistants, keyboards, and camera apps use distilled models for their on-device features. The teacher stays in the cloud, and the student lives on the device.

Retrieval pipelines benefit too. A distilled reranker can score search results almost as well as a large model, at a fraction of the latency. If your search stack already uses retrieval augmented generation, a distilled reranker slots in without changing the rest of the system. Our guide to reranking for RAG explains how that scoring step shapes result quality.

Distillation struggles with open-ended generation. A student can learn to answer short questions, but it rarely matches a teacher on long, creative writing or multi-step reasoning. The student has less capacity, so it forgets edge cases first. If your task needs deep reasoning across a long context, keep the large model and distill only the narrow parts around it.

How to Distill a Model Without a Research Team

You do not need a research lab to try distillation. Open tools cover the whole loop, and a small team can run a first experiment in a weekend. The steps below give you a path from a teacher you already use to a student you can afford to run.

First, pick one task and define success. Choose a task with a clear metric, such as classification accuracy or ranking quality. Collect a few hundred examples from real usage, and hold out a test set the student never sees during training. Without a held-out test set, you cannot tell whether the student learned the task or memorized the examples.

Second, pick a teacher and a student. The teacher can be any model that performs well on your task, including an API model if you have the right to use its output for training. Check the provider terms first, since some providers restrict training on model output. The student should be a small open model in the same family as the teacher when possible, such as a DistilBERT-style model for a BERT teacher, or a small decoder for a large decoder.

Third, generate teacher labels. Run the teacher over your examples and save the full output distribution, not just the top answer. If you use an API, log the logprobs when the provider exposes them. For open models, the Hugging Face distillation guide shows how to set up the teacher and student in the same training loop.

Fourth, train with a mix of soft and hard targets. Start with a temperature of 2 to 4 and a loss weight that favors the teacher signal. Train for a few epochs and check the test set after each one. If the student stalls, raise the temperature or add more examples. If it overfits, add data or lower the training rate. Watch the gap between teacher and student accuracy, since that gap tells you how much headroom remains.

Fifth, measure cost, not just accuracy. A student that matches the teacher on 97 percent of cases but runs ten times cheaper is a win for most products. Measure latency on your actual hardware and price per thousand requests. Those numbers decide whether the distilled model ships. Keep an eye on the settings that shape cost at inference time, since sampling controls such as temperature and top-p change both output quality and speed. Our explainer on temperature vs top-p covers those controls in detail.

Finally, test the student on the cases where the teacher fails. Distillation copies teacher behavior, including teacher mistakes. If the teacher shows bias or errors on a slice of your data, the student inherits them. Run a small error review before you ship, and keep the teacher around for the cases where the student confidence drops.

What to Watch Before You Commit

Distillation has limits worth knowing before you invest. The student cannot exceed the teacher on the task it learned from. If the teacher scores 90 percent, the student lands below that number. Your ceiling is the teacher, so pick a teacher that already performs well on your data.

Legal terms deserve a careful read. Some model providers forbid using their output to train a competing model. Others allow it for internal use but not for redistribution. Read the terms of the teacher you plan to use, and keep a record of the license. A distilled model trained on a restricted teacher can create a compliance problem that costs more than the inference savings.

Claims about benchmarks need the same care. A vendor that reports a distilled model at 97 percent of teacher performance may have tested on a narrow benchmark. Run your own test set before you believe a headline number. If you want a method for reading vendor charts with a skeptical eye, our guide on how to read AI benchmarks walks through the common tricks.

Token costs add another wrinkle. Teacher models charge per token, and labeling a large dataset through an API can cost more than you expect. Estimate the labeling bill before you start, and sample your data if the full set costs too much. Tools such as the OpenAI tokenizer and the Gemini token guide help you count tokens before you send a request.

Size also has a floor. A student that is too small cannot hold the teacher patterns, no matter how good the training. If your first student loses too much accuracy, try a larger student before you change the method. A model with twice the parameters still costs far less than the teacher, and the accuracy gain often pays for the size.

Conclusion

Model distillation turns a model you cannot afford into one you can ship. A teacher passes its judgment to a student through soft targets, and the student keeps most of the skill at a fraction of the size and cost. DistilBERT proved the idea in 2019, and the pattern now runs behind search, support, and on-device features across the industry.

Start small. Pick one repeated task, measure the teacher on your own data, and distill a student for that task alone. Compare accuracy, latency, and cost per request, and ship the student when the numbers justify it. I suggest you keep the teacher for the hard cases and let the student handle the routine ones. That split gives you the best of both models: teacher quality where it counts, and student speed everywhere else. If you want to shrink a model you already run, our guide to LLM quantization shows how to cut memory use without retraining at all.

Irfan is a Creative Tech Strategist and the founder of Grafisify. He spends his days testing the latest AI design tools and breaking down complex tech into actionable guides for creators. When he’s not writing, he’s experimenting with generative art or optimizing digital workflows.

Leave a Reply

Your email address will not be published. Required fields are marked *

You might also like
Speculative Decoding Explained: Faster LLM Output

Speculative Decoding Explained: Faster LLM Output

LLM Batch APIs: Cut Your AI Bill in Half

LLM Batch APIs: Cut Your AI Bill in Half

Reasoning Models Explained: How LLMs Think First

Reasoning Models Explained: How LLMs Think First

KV Cache Explained: The Hidden Memory Behind Every Chat

KV Cache Explained: The Hidden Memory Behind Every Chat

How to Read AI Benchmarks Without Being Fooled

How to Read AI Benchmarks Without Being Fooled

Temperature vs Top-P: Which LLM Knobs Still Matter?

Temperature vs Top-P: Which LLM Knobs Still Matter?