News / Learn / Fine-tuning
What is model distillation?
You'll be able to explain how distillation transfers a large model's behavior into a smaller model, and when that trade is worth making.
In plain English
Distillation trains a small model to copy what a large model does. The large model is the teacher. The small model is the student. The student learns from the teacher's outputs, so it can handle a narrow task with far less compute.
Why it matters
Large models are often slow and costly to run at scale. A distilled student can answer the same kinds of requests faster and cheaper, and it can run on hardware you control. For a product with a repeated task, this can cut latency and cost while keeping most of the quality.
How it works
The basic loop is simple. You pick a task. You send many inputs to the teacher and record its outputs. You then train the student on those input-output pairs, so it learns to produce similar answers.
Some versions use more than final answers. The student can also learn from the teacher's probability distribution over possible next tokens. These soft targets carry more information than a single correct label, so the student often learns faster.
The student is usually much smaller than the teacher. It has fewer parameters, which means less memory and faster inference. The tradeoff is that the student is only as good as the examples it saw. It tends to be strong on the task it was trained for and weaker outside it.
Distillation also has limits set by the teacher's license and terms of use. Check whether the provider allows using outputs to train another model before you start.
A concrete example
A simple data-generation step. You ask a teacher model to label inputs, then save the pairs for training a student. The model name here is a placeholder.
import json
def call_teacher(text: str) -> str:
# Replace with a call to your chosen provider's API.
raise NotImplementedError
inputs = ["Refund not received after 10 days", "How do I change my email?"]
with open("train.jsonl", "w") as f:
for text in inputs:
label = call_teacher(
f"Classify this support ticket as billing, account, or other. Reply with one word.\n\n{text}"
)
row = {"messages": [
{"role": "user", "content": text},
{"role": "assistant", "content": label.strip()},
]}
f.write(json.dumps(row) + "\n")A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Distilling a task you have not defined Write down the exact inputs and expected outputs first. A vague target produces a student that is vague.
- Using too few or unrepresentative inputs Sample real traffic, including edge cases. The student will only learn patterns it sees in training data.
- Skipping evaluation against the teacher Hold out examples and measure how often the student matches the teacher or the correct answer before you ship.
- Ignoring the teacher's terms of use Confirm that the provider allows training other models on its outputs. Keep records of the source and license.
Going deeper
Study knowledge distillation from the original research on soft targets. Then learn how to build evaluation sets, since they decide whether a distilled student is good enough.
FAQ
Is distillation the same as fine-tuning?
Not exactly. Fine-tuning adjusts an existing model on examples. Distillation trains a new, usually smaller model to imitate a larger teacher. Distillation often uses fine-tuning as the training method.
Will the student be as good as the teacher?
Usually not across all tasks. It can match or come close on the narrow task it was trained on. Measure it on your own test set.
Do I need a lot of data?
It depends on the task. Narrow tasks often need far fewer examples than broad ones. Start small, evaluate, then add data where the student fails.
Why is the student faster?
It has fewer parameters, so each request needs less computation and memory. That lowers inference latency and cost.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.