🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Knowledge Distillation for LLMs

Advanced TrainingModel CompressionđŸŸĸ Free Lesson

Advanced Training

Knowledge Distillation for LLMs — Compressing Intelligence

Knowledge distillation transfers the capabilities of large, expensive models into smaller, faster ones. This guide covers the theory and practice of distilling LLM knowledge for deployment.

  • Response-Based Distillation — Train student to match teacher outputs
  • Feature-Based Distillation — Transfer internal representations
  • Chain-of-Thought Distillation — Distill reasoning capabilities specifically

The goal is not a smaller model — it is a smaller model that thinks like a larger one.

Knowledge Distillation for LLMs

Knowledge distillation enables deploying capable models at a fraction of the cost. A 70B parameter model distilled into a 7B model can retain 80-95% of the teacher's performance on many tasks while being 10x faster and cheaper to run.

The Distillation Framework

Temperature-Scaled Softmax

Distillation Loss

python
import torch
import torch.nn as nn
import torch.nn.functional as F

class DistillationLoss(nn.Module):
    def __init__(self, temperature=4.0, alpha=0.5):
        super().__init__()
        self.temperature = temperature
        self.alpha = alpha
    
    def forward(self, student_logits, teacher_logits, labels):
        # Soft loss (KL divergence between softened distributions)
        student_soft = F.log_softmax(student_logits / self.temperature, dim=-1)
        teacher_soft = F.softmax(teacher_logits / self.temperature, dim=-1)
        soft_loss = F.kl_div(
            student_soft, teacher_soft,
            reduction="batchmean"
        ) * (self.temperature ** 2)
        
        # Hard loss (standard cross-entropy with true labels)
        hard_loss = F.cross_entropy(student_logits, labels)
        
        return self.alpha * hard_loss + (1 - self.alpha) * soft_loss

Types of Knowledge Distillation

Response-Based Distillation

python
def response_distillation_step(teacher, student, batch, loss_fn):
    with torch.no_grad():
        teacher_outputs = teacher(batch["input_ids"])
    student_outputs = student(batch["input_ids"])
    loss = loss_fn(student_outputs.logits, teacher_outputs.logits, batch["labels"])
    return loss

Feature-Based Distillation

python
class FeatureDistillationLoss(nn.Module):
    def __init__(self, teacher_dim, student_dim):
        super().__init__()
        self.projection = nn.Linear(student_dim, teacher_dim)
    
    def forward(self, student_hidden, teacher_hidden):
        projected = self.projection(student_hidden)
        return F.mse_loss(projected, teacher_hidden)

Attention-Based Distillation

python
def attention_distillation_loss(teacher_attn, student_attn):
    """Match attention distributions between teacher and student."""
    teacher_attn = teacher_attn.mean(dim=1)  # Average over heads
    student_attn = student_attn.mean(dim=1)
    
    teacher_attn = F.softmax(teacher_attn, dim=-1)
    student_attn = F.softmax(student_attn, dim=-1)
    
    return F.kl_div(
        student_attn.log(), teacher_attn,
        reduction="batchmean"
    )

Chain-of-Thought Distillation

Distilling Reasoning Capabilities

python
def cot_distillation(teacher, student, problems, loss_fn):
    """Distill chain-of-thought reasoning from teacher to student."""
    # Teacher generates reasoning traces
    teacher_traces = []
    for problem in problems:
        trace = teacher.generate(
            problem["question"],
            max_new_tokens=1024,
            temperature=0.3
        )
        teacher_traces.append(trace)
    
    # Student learns to produce same reasoning
    total_loss = 0
    for problem, trace in zip(problems, teacher_traces):
        student_output = student(problem["question"], labels=trace)
        total_loss += student_output.loss
    
    return total_loss / len(problems)

Selective Distillation

python
def selective_distillation(teacher, student, dataset, confidence_threshold=0.9):
    selective_loss = 0
    count = 0
    
    for batch in dataset:
        with torch.no_grad():
            teacher_probs = F.softmax(teacher(batch["input_ids"]).logits, dim=-1)
            max_probs, _ = teacher_probs.max(dim=-1)
            confidence = max_probs.mean().item()
        
        if confidence > confidence_threshold:
            student_out = student(batch["input_ids"])
            loss = distillation_loss(student_out.logits, teacher(batch["input_ids"]).logits, batch["labels"])
            selective_loss += loss
            count += 1
    
    return selective_loss / max(count, 1)

Multi-Teacher Distillation

python
def multi_teacher_distillation(teachers, student, batch, weights):
    combined_teacher_loss = 0
    for teacher, weight in zip(teachers, weights):
        with torch.no_grad():
            teacher_logits = teacher(batch["input_ids"])
        student_logits = student(batch["input_ids"])
        loss = F.kl_div(
            F.log_softmax(student_logits / 4.0, dim=-1),
            F.softmax(teacher_logits / 4.0, dim=-1),
            reduction="batchmean"
        )
        combined_teacher_loss += weight * loss
    return combined_teacher_loss

Practical Distillation Pipeline

Full Training Loop

python
def distill(teacher, student, train_dataloader, val_dataloader, epochs=3, lr=5e-5):
    optimizer = torch.optim.AdamW(student.parameters(), lr=lr)
    loss_fn = DistillationLoss(temperature=4.0, alpha=0.3)
    
    teacher.eval()
    student.train()
    
    for epoch in range(epochs):
        total_loss = 0
        for batch in train_dataloader:
            with torch.no_grad():
                teacher_outputs = teacher(batch["input_ids"])
            
            student_outputs = student(batch["input_ids"])
            loss = loss_fn(
                student_outputs.logits,
                teacher_outputs.logits,
                batch["labels"]
            )
            
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            total_loss += loss.item()
        
        avg_loss = total_loss / len(train_dataloader)
        print(f"Epoch {epoch + 1}: Loss = {avg_loss:.4f}")
        
        # Evaluate
        evaluate(student, val_dataloader)

Distillation vs Other Compression Methods

MethodAccuracy RetentionSpeedupMemory ReductionTraining Cost
Distillation80-95%3-10x3-10xHigh (need teacher)
Quantization95-99%2-4x2-4xLow
Pruning90-98%2-5x2-5xMedium
Low-rank85-95%2-3x2-3xMedium

Practice Exercises

  1. Distillation Design: Design a distillation pipeline to compress a 70B teacher into a 7B student. What temperature, alpha, and training data would you use?

  2. CoT Distillation: Implement chain-of-thought distillation for mathematical reasoning. How would you measure whether reasoning capabilities transfer effectively?

  3. Multi-Teacher Ensemble: If you have a general-purpose teacher and a code-specialized teacher, how would you combine their knowledge for a student that needs both capabilities?

  4. Distillation Analysis: Compare distillation from a 70B model vs. training a 7B model from scratch on the same data. What are the tradeoffs?

Key Takeaways


What to Learn Next

-> QLoRA and Quantization Reducing model size through quantization techniques.

-> LoRA and PEFT Parameter-efficient fine-tuning for large models.

-> Distributed Training for LLMs Scaling training across hundreds of GPUs.

-> Curriculum Learning for LLMs Strategic ordering of training data.

-> LLM Inference Optimization Making LLM inference faster and cheaper.

-> Open-Source LLM Ecosystem Pre-trained and distilled models available today.


Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. Forgetting constraints — Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert LLM Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement