Distillation & Knowledge Transfer

AI & Machine Learning Foundations beginner How AI Works 12 min read

Overview

Knowledge distillation is a model compression technique where a smaller “student” model is trained to replicate the behavior of a larger, more capable “teacher” model. Rather than training the student from scratch on raw data, distillation transfers the teacher’s learned knowledge — including nuanced relationships between outputs that aren’t captured by simple labels — into a compact model suitable for production deployment.

This technique has become critical in the LLM era because frontier models are often too large and expensive to deploy directly. A 70B-parameter model might cost $1–2 per million tokens to serve, while a distilled 7B student could deliver 80–90% of the quality at 10% of the cost. Companies routinely distill capabilities from proprietary models like GPT-4 or Claude into smaller open-weight models optimized for specific tasks.

For AI engineers, distillation is a core tool for bridging the gap between what’s possible in research and what’s practical in production. It’s the primary technique behind most efficient open-source models and a key component of any cost-optimization strategy.

How It Works

The Teacher-Student Paradigm

The fundamental idea is straightforward: instead of training a model on hard labels (e.g., “this token is ‘cat’”), you train it on the teacher’s full probability distribution over all possible outputs. These “soft targets” contain far more information than hard labels because they encode the teacher’s uncertainty and the relationships between different outputs.

Key Concept: Soft Targets

When a teacher model predicts the next token, it doesn’t just output “cat” — it produces a probability distribution like {"cat": 0.7, "kitten": 0.15, "feline": 0.08, "dog": 0.02, ...}. These soft probabilities reveal that the teacher considers “kitten” and “feline” as reasonable alternatives, while “dog” is unlikely. This relational knowledge is the “dark knowledge” that distillation transfers — information that’s invisible in hard one-hot labels.

Temperature Scaling

A key insight in distillation is that raw softmax outputs are often too “peaked” — the top prediction dominates with very high probability, making the soft targets nearly indistinguishable from hard labels. Temperature scaling addresses this by dividing the logits by a temperature parameter T before applying softmax:

# Standard softmax (T=1): peaked distribution
softmax(logits / 1.0)  # → [0.95, 0.03, 0.01, 0.01, ...]

# High-temperature softmax (T=4): softer distribution
softmax(logits / 4.0)  # → [0.40, 0.20, 0.15, 0.12, ...]

# The softer distribution reveals more about the teacher's
# internal knowledge about relationships between outputs

Higher temperatures (typically T = 2–10) produce softer distributions that reveal more of the teacher’s knowledge. However, very high temperatures can over-flatten the distribution, making it harder for small students to learn. The optimal temperature depends on the capacity gap between teacher and student — smaller students benefit from lower temperatures.

The Distillation Loss Function

The student is trained with a combined loss that balances learning from the teacher (soft targets) with learning from the ground truth (hard labels):

# Distillation loss function
loss = alpha * KL_divergence(
    student_softmax(logits_s / T),
    teacher_softmax(logits_t / T)
) * T² + (1 - alpha) * cross_entropy(logits_s, true_labels)

# alpha: weight between soft and hard losses (typically 0.5–0.9)
# T: temperature (typically 2–10)
# T²: scaling factor to keep gradient magnitudes comparable

The T² factor is important: as temperature increases, the magnitudes of the softmax gradients decrease by 1/T², so multiplying by T² compensates and keeps the soft-target gradients on the same scale as the hard-label gradients.

Key Concept: Alpha Balancing

The alpha hyperparameter controls how much the student relies on the teacher’s knowledge vs. ground truth. Higher alpha (e.g., 0.9) trusts the teacher more, which works well when the teacher is highly accurate. Lower alpha (e.g., 0.5) gives more weight to ground truth labels, useful when the teacher may have systematic biases you don’t want to transfer.

Distillation Schemes

There are three primary approaches to when and how distillation happens:

Beyond Logit Matching

Modern distillation goes beyond just matching output distributions:

Practical Applications

LLM Distillation in Practice

Knowledge distillation is the primary technique behind many successful production models:

When to Use Distillation

Distillation Pipeline

A typical production distillation pipeline involves: (1) selecting a teacher model that performs well on your task, (2) generating a dataset of teacher outputs (either via API calls or by running inference on your data), (3) choosing a student architecture (usually the same family, just smaller), (4) training the student on the teacher’s outputs with temperature-scaled soft targets, and (5) evaluating the student against your quality benchmarks to ensure acceptable performance.

Key Takeaways

Further Reading