Inferensys

Glossary

Knowledge Distillation

A model compression technique where a smaller, more efficient student model is trained to mimic the output scores or internal representations of a larger, more powerful teacher model.
ML engineer working on model compression and quantization, laptop showing performance benchmarks, technical workspace.
MODEL COMPRESSION

What is Knowledge Distillation?

Knowledge distillation is a model compression technique where a smaller, efficient 'student' model is trained to replicate the behavior of a larger, complex 'teacher' model, transferring dark knowledge through soft probability distributions.

Knowledge distillation is a machine learning paradigm where a compact student model is trained to mimic the output distribution of a larger, pre-trained teacher model. Rather than training the student on hard labels alone, the student learns from the teacher's softened logits, capturing inter-class similarities and nuanced patterns that a one-hot label cannot provide. This process transfers the teacher's generalization capability into a significantly smaller architecture.

The technique is critical for deploying models in latency-sensitive or resource-constrained environments, such as distilling a cross-encoder into a bi-encoder for fast retrieval. The student minimizes a composite loss function combining the standard task loss with a distillation loss, typically Kullback-Leibler divergence, weighted by a temperature parameter that controls the softness of the teacher's output distribution.

MODEL COMPRESSION

Key Characteristics of Knowledge Distillation

The core mechanisms and architectural patterns that enable a compact student model to approximate the generalization capabilities of a computationally expensive teacher model.

01

Teacher-Student Architecture

The foundational setup where a large, pre-trained teacher model generates soft probability distributions over outputs. A smaller student model is trained not just on the hard labels but on these soft targets, which capture the teacher's nuanced decision boundaries and inter-class similarities. This transfers dark knowledge that is not present in the one-hot ground truth.

02

Response-Based Distillation

The most common form of distillation, focusing solely on the final output layer. The student minimizes the divergence between its predicted probability distribution and the teacher's distribution. A temperature parameter (T) in the softmax function softens the probabilities, revealing the relative confidence the teacher assigns to incorrect classes. Key loss components include:

  • Kullback-Leibler divergence between softened student and teacher outputs
  • Cross-entropy loss against the ground truth hard labels
03

Feature-Based Distillation

Instead of matching only final outputs, the student learns to mimic the intermediate feature representations of the teacher. A regression loss minimizes the distance between the student's hidden layer activations and the teacher's corresponding layers. This is critical when the student has a different depth or width, requiring a projection layer to map the student's feature space to the teacher's dimensionality.

04

Relation-Based Distillation

Transfers the structural relationships learned by the teacher. Instead of matching individual examples, the student learns to preserve the mutual relations between data samples in the teacher's embedding space. Techniques include:

  • Similarity-preserving loss: Matching the Gram matrices of activations within a batch
  • Relational Knowledge Distillation (RKD): Preserving distance-wise and angle-wise relations between triplet samples
05

Distillation for Bi-Encoder Efficiency

A critical application in retrieval systems where a computationally heavy cross-encoder (which processes query-document pairs jointly) acts as the teacher. The student is a bi-encoder that encodes queries and documents independently for fast ANN search. The bi-encoder is trained to mimic the cross-encoder's relevance scores, enabling low-latency semantic search without sacrificing the precision of joint attention mechanisms.

06

Online vs. Offline Distillation

Offline distillation uses a frozen, pre-trained teacher to guide a student in a standard two-phase process. Online distillation trains both models simultaneously in a single phase, where the teacher and student co-evolve. A variant, self-distillation, uses the same architecture for both roles, where a deeper network's predictions guide a shallower instance of itself, often improving generalization without an external teacher.

KNOWLEDGE DISTILLATION

Frequently Asked Questions

Clear, technically precise answers to the most common questions about transferring knowledge from large teacher models to compact, efficient student models.

Knowledge distillation is a model compression technique where a compact student model is trained to replicate the behavior of a larger, more powerful teacher model. Instead of training the student solely on hard labels from a dataset, the student learns from the teacher's output probability distribution—often called soft targets. These soft targets contain rich information about inter-class similarities that hard labels lack. For example, a teacher classifying an image of a car might assign a 0.7 probability to 'car,' 0.2 to 'truck,' and 0.1 to 'bus,' revealing the semantic relationship between these vehicles. The student is trained to minimize the Kullback-Leibler (KL) divergence between its own softened output distribution and the teacher's, using a temperature parameter to control the softness of the probability distribution. This process effectively transfers the teacher's generalization capabilities into a model that is often orders of magnitude smaller and faster at inference time.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.