Inferensys

Glossary

Knowledge Distillation

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

What is Knowledge Distillation?

Knowledge distillation is a compression technique where a smaller 'student' model is trained to replicate the behavior and output distributions of a larger, more complex 'teacher' model.

Knowledge distillation transfers the generalization capabilities of a cumbersome teacher model to a compact student model by training the student to mimic the teacher's soft output probabilities, not just the hard ground-truth labels. The student learns the relative probabilities of incorrect classes, capturing dark knowledge about the data's structure.

In retrieval systems, cross-encoder distillation is critical: a computationally expensive cross-encoder teacher scores query-document pairs with full attention, and its relevance scores supervise a fast bi-encoder student. This yields a dense retriever that approximates the accuracy of the expensive model while maintaining the low latency required for searching billion-scale vector indexes.

MODEL COMPRESSION

Key Characteristics of Knowledge Distillation

Knowledge distillation transfers the generalization capabilities of a large, complex teacher model to a smaller, efficient student model by training the student to match the teacher's output distributions.

01

Teacher-Student Architecture

The core framework involves a teacher model (typically a large cross-encoder or ensemble) and a student model (a lightweight bi-encoder). The student is trained not on hard labels, but on the soft labels produced by the teacher. These soft labels—probability distributions over classes—contain rich information about inter-class similarities that one-hot labels lack.

  • Teacher: High-capacity model frozen during distillation
  • Student: Compact model trained to mimic teacher outputs
  • Soft Targets: Probability vectors capturing dark knowledge
02

Cross-Encoder to Bi-Encoder Distillation

A critical application in modern search systems. A cross-encoder processes the query and document concatenated together with full self-attention, yielding high accuracy but prohibitive latency. The distilled bi-encoder encodes query and document independently, enabling pre-computed passage embeddings and fast Maximum Inner Product Search (MIPS).

  • Teacher: Cross-encoder with full query-document attention
  • Student: Bi-encoder for independent encoding
  • Result: Cross-encoder precision at bi-encoder speed
03

Temperature Scaling

A temperature parameter (T) controls the softness of the teacher's output probabilities. Higher temperatures produce softer distributions, revealing more of the dark knowledge—the relative similarities between incorrect classes. The student is trained with the same high temperature, but evaluated at T=1.

  • T > 1: Softens probability distribution, exposes class relationships
  • T = 1: Standard softmax for inference
  • Loss Function: Typically Kullback-Leibler divergence between softened distributions
04

Training Data Strategies

Distillation can use the original labeled dataset, a large unlabeled corpus, or synthetically generated queries. Using unlabeled data allows the student to learn from the teacher's predictions on a much larger scale than the original training set, improving generalization.

  • Labeled Data: Original supervised training set
  • Unlabeled Corpus: Teacher generates pseudo-labels for massive datasets
  • Synthetic Queries: Augment training with generated query-passage pairs
05

Hard Negative Mining

To improve the student's discriminative power, training often incorporates hard negatives—passages that are top-ranked by a basic retriever but are actually irrelevant. The teacher model scores these negatives, and the student learns to push them away from the query in the embedding space, sharpening the decision boundary.

  • Hard Negatives: Superficially similar but irrelevant passages
  • Source: Retrieved from BM25 or a preliminary dense retriever
  • Impact: Significantly improves Recall@K metrics
06

Distillation Loss Functions

The student is optimized using a combination of losses. The distillation loss (KL divergence) aligns the student's softened outputs with the teacher's. An optional student loss (cross-entropy with hard labels) can be added to ground the model in true labels. A weighted sum balances imitation and ground truth.

  • L_KD: KL divergence between teacher and student soft labels
  • L_CE: Cross-entropy loss against ground truth labels
  • L_total = α * L_KD + (1-α) * L_CE
KNOWLEDGE DISTILLATION

Frequently Asked Questions

Explore the core concepts behind knowledge distillation, the critical compression technique that transfers expertise from large, computationally expensive teacher models to smaller, efficient student models for production deployment.

Knowledge distillation is a model compression technique where a compact student model is trained to replicate the behavior of a larger, more complex teacher model. Instead of learning directly from hard labels in a dataset, the student learns from the teacher's output probability distribution, known as soft targets. These soft targets contain rich information about inter-class similarities that hard labels lack, providing a more informative training signal. The process minimizes the divergence between the student's predictions and the teacher's predictions, effectively transferring the teacher's generalization capabilities to a model with a fraction of the parameters and latency.

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.