LeetLLM
My PlanLearnGlossaryTracksPracticeBlog
LeetLLM

Your go-to resource for mastering AI & LLM systems.

Product

  • Learn
  • Glossary
  • Tracks
  • Practice
  • Blog
  • RSS

Legal

  • Terms of Service
  • Privacy Policy

© 2026 LeetLLM. All rights reserved.

All Topics
Your Progress
0%

0 of 196 articles completed

🛠️Computing Foundations0/9
Git, Shell, Linux for AIDocker for Reproducible AIPython for AI EngineeringNumPy and Tensor ShapesCUDA for ML TrainingMPS & Metal for ML on MacData Structures for AISQL and Data ModelingAlgorithms for ML Engineers
📊Math & Statistics0/9
Gradients and BackpropVectors, Matrices & TensorsLinear Algebra for MLAdam, Momentum, SchedulersProbability for Machine LearningBayesian Inference from EvidenceStatistics and UncertaintyDistributions and SamplingHypothesis Tests, Intervals, and pass@k
📚Preparation & Prerequisites0/13
Neural Networks from ScratchCNNs from ScratchTraining & BackpropagationSoftmax, Cross-Entropy & OptimizationRNNs, LSTMs, GRUs, and Sequence ModelingAutoencoders and VAEsThe Transformer Architecture End-to-EndLanguage Modeling & Next TokensFrom GPT to Modern LLMsPrompt Engineering FundamentalsCalling LLM APIs in ProductionFirst AI App End-to-EndThe LLM Lifecycle
🧮ML Algorithms & Evaluation0/15
Linear Regression from ScratchLogistic Regression and MetricsDecision Trees, Forests, and BoostingReinforcement Learning BasicsValidation and LeakageStatistical Learning and GeneralizationConformal Prediction and UncertaintyClustering and PCAProbabilistic Generative ModelsCore Retrieval AlgorithmsDecoding AlgorithmsExperiment Design and A/B TestingCausal Inference and CounterfactualsPyTorch Training LoopsDataset Pipelines and Data Quality
📦Production ML Systems0/6
Feature Engineering for Production MLBatch and Streaming Feature PipelinesGradient Boosted Trees in ProductionRanking and Recommendation SystemsForecasting and Anomaly DetectionMonitoring Predictive Models
🧪Core LLM Foundations0/8
The Bitter Lesson & ComputeBPE, WordPiece, and SentencePieceStatic to Contextual EmbeddingsPerplexity & Model EvaluationFile Ingestion for AIChunking StrategiesLLM Benchmarks & LimitationsInstruction Tuning & Chat Templates
🧰Applied LLM Engineering0/25
Dimensionality Reduction for EmbeddingsCoT, ToT & Self-Consistency PromptingFunction Calling & Tool UseMCP & Tool Protocol StandardsContext EngineeringPrompt Injection DefenseResponsible AI GovernancePrivacy-Preserving Machine LearningData Labeling and Human FeedbackEvaluating AI AgentsProduction RAG PipelinesHybrid Search: Dense + SparseReranking and Cross-Encoders for RAGRAG Evaluation for Reliable AnswersLLM-as-a-Judge EvaluationBias & Fairness in LLMsHallucination Detection & MitigationLLM Observability & MonitoringExperiment Tracking with MLflow and W&BPrompt Optimization with DSPyModel Versioning & DeploymentSemantic Caching & Cost OptimizationLLM Cost Engineering & Token EconomicsModel Gateways, Routing, and FallbacksDesign an Automated Support Agent
🎓Portfolio Capstones0/9
Capstone: Delivery ETA PredictionCapstone: Product RankingCapstone: Demand ForecastingCapstone: Image Damage ClassifierCapstone: Production ML PipelineCapstone: Document QACapstone: Eval DashboardCapstone: Fine-Tuned ClassifierCapstone: Reproducible ML Study
🧠Transformer Deep Dives0/8
Sentence Embeddings & Contrastive LossEmbedding Similarity & QuantizationScaled Dot-Product AttentionVision Transformers and Image EncodersPositional Encoding: RoPE & ALiBiLayer Normalization: Pre-LN vs Post-LNMechanistic InterpretabilityDecoding Strategies: Greedy to Nucleus
🧬Advanced Training & Adaptation0/17
Scaling Laws & Compute-Optimal TrainingPre-training Data at ScaleBuild GPT from Scratch LabJAX for PyTorch ResearchersContinued Pretraining for Domain ShiftSynthetic Data PipelinesSupervised Fine-Tuning PipelineMixed Precision TrainingDistributed Training: FSDP & ZeROLoRA & Parameter-Efficient TuningTraining Run OperationsReward Modeling from Preference DataRLHF & DPO AlignmentConstitutional AI & Red TeamingRLVR & Verifiable RewardsKnowledge Distillation for LLMsModel Merging and Weight Interpolation
🤖Advanced Agents & Retrieval0/16
Vector DB Internals: HNSW & IVFAdvanced RAG: HyDE & Self-RAGGraphRAG & Knowledge GraphsRAG Security & Access ControlStructured Output GenerationReAct & Plan-and-ExecuteGuardrails & Safety FiltersCode Generation & SandboxingComputer-Use / GUI / Browser AgentsHuman-in-the-Loop Agent ArchitectureAI Coding Workflow with AgentsAgent Memory & PersistenceAgent Failure & RecoveryRecursive Language Models (RLM)Multi-Agent OrchestrationCapstone: Production Agent
⚡Inference & Production Scale0/31
Inference: TTFT, TPS & KV CacheMulti-Query & Grouped-Query AttentionKV Cache & PagedAttentionPrefix Caching and Prompt CachingFlashAttention & Memory EfficiencyContinuous Batching & SchedulingScaling LLM InferenceModel Parallelism for LLM InferenceAccelerator Architecture Field GuideCorrect GPU Profiling and BenchmarkingGPU Kernel Performance Engineering LabGPU Parallel Primitives LabGPU Compilers and Kernel LanguagesGEMM From Naive CUDA to Tensor CoresLow-Precision GPU ProgrammingGPU Runtime Execution LabAI-Generated Kernel EngineeringModel Quantization: GPTQ, AWQ & GGUFLocal LLM DeploymentSLM Specialization & Edge DeploymentSpeculative DecodingLong Context Window ManagementMixture of Experts ArchitectureMamba & State Space ModelsReasoning & Test-Time ComputeAdvanced MLOps & DevOps for AIGPU Serving & AutoscalingGPU Failure OperationsDistributed Inference Data PlaneServing Benchmark EngineeringA/B Testing for LLMs
🏗️System Design Capstones0/9
Content Moderation SystemCode Completion SystemMulti-Tenant LLM PlatformLLM-Powered Search EngineVision-Language Models & CLIPMultimodal LLM ArchitectureDiffusion Models: Images & TextReal-Time Voice AI AgentReasoning Agent System Design
🎤AI Lab Interviewing0/4
AI Lab Coding Interview: Python SystemsAI Lab System Design InterviewAI Lab Behavioral InterviewAI Lab Technical Presentation
🔬Project Deep Dives0/17
Deep Dive - vLLMDeep Dive - SkyRLDeep Dive - FlashAttentionDeep Dive - FlashInferDeep Dive - DeepGEMMDeep Dive - NCCLDeep Dive - MegatronDeep Dive - DeepSpeedDeep Dive - RayDeep Dive - MLflowDeep Dive - PyTorchDeep Dive - TransformersDeep Dive - SGLangDeep Dive - slimeDeep Dive - DeepEPDeep Dive - TinkerDeep Dive - Light-PEFT
Back to Topics
LearnAdvanced Training & AdaptationKnowledge Distillation for LLMs
⚡HardFine-Tuning & Training

Knowledge Distillation for LLMs

Understand the main forms of knowledge distillation for LLMs, from logit matching and response-based supervision to on-policy KD. Learn when distillation helps, where student capacity becomes the bottleneck, and how to implement a correct teacher-student training loop.

29 min read
Learning path
Step 118 of 196 in the full curriculum
RLVR & Verifiable RewardsModel Merging and Weight Interpolation

Personalize this lesson

Adapt explanations and teaching visuals to your background and preferred voice.

The previous lesson used Reinforcement Learning from Verifiable Rewards (RLVR): train a policy against checked outcomes where a verifier exists. Distillation starts after that useful teacher exists. At 09:00, a 7B student language model, a smaller large language model (LLM), passes staging's familiar restore-window cases. At 09:15, it answers a new expired-admin case with 7 days; trusted policy says not allowed. The student is fast enough for production, but copying that regression isn't a deployment win.

Knowledge distillation trains a smaller student to imitate useful behavior from a larger or otherwise more capable teacher. In this example, a 70B-class teacher answers "How many days can a disabled production API key stay restorable?" well enough to pass your checks, but you can't afford that model on every request. A 7B-class student is viable only if it keeps enough of that behavior under a latency and serving-cost budget.

That transfer signal depends on what the teacher exposes. In white-box settings, the student can match softened token probabilities or internal features. In black-box settings, it fine-tunes on selected teacher-written answers, solution traces, or synthetic corpora. The student doesn't inherit the teacher's judgment. It copies the behavior you can measure, then earns release through the same restore-window checks, latency target, and cost budget.

Before computing a token-level loss, compare teacher and student on the same token vocabulary, prompt formatting, and response positions. Two arrays can share a shape while assigning column 17 to different tokens. The loss still runs, but it trains the student toward the wrong words.

Why soft labels teach more than hard labels

The staging regression points at a weakness in hard labels. Hinton et al. popularized a practical formulation in 2015: train a student to mimic a teacher's behavior rather than relying only on ground-truth labels. A teacher's full probability distribution can carry more information than a one-hot label.[1]Reference 1Distilling the Knowledge in a Neural Network.https://arxiv.org/abs/1503.02531

Start with one response token. The trusted answer is 7d, so a hard label puts mass 1.00 on 7d and zero on every alternative. Predict what gets lost: the teacher's ranking among plausible wrong answers. Its output also contains dark knowledge. Here 3d is the strongest near-miss, with smaller mass on 30d and never. That extra signal is useful only if the teacher's ranking is itself useful. It isn't proof that the student learned a general access-policy rule.

Three bar charts of the same four restore-window answers. A hard label puts mass 1.00 on 7d and zero on 3d, 30d, and never. At temperature 1 the teacher keeps 0.818 on 7d and 0.111 on the near-miss 3d. At temperature 4 the ranking stays 7d then 3d then 30d then never, but the top gap shrinks from 0.708 to 0.156.
Hard labels hide alternatives. At T = 1 the teacher still ranks 7d above 3d by 0.708. Raising T to 4 keeps that order while shrinking the gap to 0.156, so near-misses contribute real gradient. A held-out check still has to decide whether the ranking is worth copying.

Those four bars are one simplified prediction. For a causal LLM, logit distillation applies the same idea at each predicted token position. Temperature TTT divides the logits before softmax. T=1T = 1T=1 is the model's ordinary distribution. T>1T > 1T>1 flattens it so second-place tokens aren't rounded into zero. Push TTT far enough and the ranking disappears.

Predict the top gap before running this check: it should shrink as TTT rises, then nearly vanish. The code uses the same logits as the figure, [4.0,2.0,1.0,0.7][4.0, 2.0, 1.0, 0.7][4.0,2.0,1.0,0.7], and prints the four probabilities plus p(7d)−p(3d)p(7d) - p(3d)p(7d)−p(3d) at T=1T = 1T=1, T=4T = 4T=4, and T=40T = 40T=40.

temperature-softening.py
1import math 2 3logits = {"7d": 4.0, "3d": 2.0, "30d": 1.0, "never": 0.7} 4 5def softened_probabilities(temperature: float) -> dict[str, float]: 6 scaled = {token: math.exp(logit / temperature) for token, logit in logits.items()} 7 total = sum(scaled.values()) 8 return {token: value / total for token, value in scaled.items()} 9 10for temperature in (1.0, 4.0, 40.0): 11 probs = softened_probabilities(temperature) 12 rounded = {token: round(probability, 3) for token, probability in probs.items()} 13 top_gap = probs["7d"] - probs["3d"] 14 print(f"T={temperature:g} probabilities:", rounded, "top_gap:", round(top_gap, 3))
Output
1T=1 probabilities: {'7d': 0.818, '3d': 0.111, '30d': 0.041, 'never': 0.03} top_gap: 0.708 2T=4 probabilities: {'7d': 0.397, '3d': 0.241, '30d': 0.188, 'never': 0.174} top_gap: 0.156 3T=40 probabilities: {'7d': 0.263, '3d': 0.25, '30d': 0.244, 'never': 0.242} top_gap: 0.013

Once alternatives carry signal, decide how much to trust that signal. A common training objective blends the teacher-matching loss with a ground-truth task loss:

L=α⋅Ldistill+(1−α)⋅Ltask\mathcal{L} = \alpha \cdot \mathcal{L}_{\text{distill}} + (1 - \alpha) \cdot \mathcal{L}_{\text{task}}L=α⋅Ldistill​+(1−α)⋅Ltask​

Ldistill\mathcal{L}_{\text{distill}}Ldistill​ matches teacher behavior, Ltask\mathcal{L}_{\text{task}}Ltask​ matches trusted labels, and α∈[0,1]\alpha \in [0, 1]α∈[0,1] sets the mix. Raising α\alphaα copies more of the teacher, including its mistakes. That makes α\alphaα a behavior-and-risk choice, not a knob to tune from training loss alone.

What happens when α\alphaα moves closer to 1?

Answer

The student listens more to the teacher distribution and less to the hard task labels. That can transfer richer teacher behavior, but it also makes teacher mistakes more influential.

That trade-off is why distillation isn't automatically better than training from scratch. It's most useful when you already have a strong teacher, legal access to its signal, and a clear smaller deployment target. The Gemma 2 report gives a controlled example: the authors train the 2B and 9B models with token-probability distillation, and a 2B ablation trained for 500B tokens scores 67.7 when distilled from a 7B teacher versus 60.3 from scratch on their three-benchmark average.[2]Reference 2Gemma 2: Improving Open Language Models at a Practical Sizehttps://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdf That 7B teacher is an ablation sized to mimic their 27B-to-9B gap, not a claim about the production teacher.

Gemma 2 writes the distillation objective as a token-level negative log-likelihood under the teacher distribution, which matches forward KL up to the teacher's entropy, a constant with respect to the student. Different recipes still expose different channels: Orca trains on explanation traces, phi-1.5 uses curated synthetic textbook-like data, and Gemma 2 uses teacher token probabilities for small models.[3]Reference 3Orca: Progressive Learning from Complex Explanation Traces of GPT-4.https://arxiv.org/abs/2306.02707[4]Reference 4Textbooks Are All You Need II: phi-1.5 technical report.https://arxiv.org/abs/2309.05463[2]Reference 2Gemma 2: Improving Open Language Models at a Practical Sizehttps://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdf These examples motivate careful data and signal selection. They don't establish one universally best recipe.

Soft labels explain why a teacher distribution can beat a one-hot target. Next, put that target on the causal language-model path and see what can go wrong.

Matching the teacher's probabilities: logit distillation

Logit KD needs the teacher's logits: raw, unnormalized scores from the final layer, before softmax. Training minimizes KL divergence (Kullback-Leibler divergence) so the student's token distribution tracks the teacher's.

Send the same restore-window prompt through both models. Predict the update: the teacher's output is a target, while only the student's weights move.

Diagram showing Shared prompt tokens, Frozen teacher, Trainable student, and Teacher probs q at T.
Shared prompt tokens, Frozen teacher, Trainable student, and Teacher probs q at T.

Start by applying temperature scaling, dividing both models' logits by T>1T > 1T>1 before softmax:

qi=exp⁡(zt,i/T)∑jexp⁡(zt,j/T),pi=exp⁡(zs,i/T)∑jexp⁡(zs,j/T)q_i = \frac{\exp(z_{t,i} / T)}{\sum_j \exp(z_{t,j} / T)}, \quad p_i = \frac{\exp(z_{s,i} / T)}{\sum_j \exp(z_{s,j} / T)}qi​=∑j​exp(zt,j​/T)exp(zt,i​/T)​,pi​=∑j​exp(zs,j​/T)exp(zs,i​/T)​

ztz_tzt​ are the teacher's logits, zsz_szs​ are the student's logits, and TTT is temperature. The KL term is then

LKL=T2∑iqilog⁡qipi\mathcal{L}_{\text{KL}} = T^2 \sum_i q_i \log \frac{q_i}{p_i}LKL​=T2∑i​qi​logpi​qi​​

Reading the formula

  • qiq_iqi​ is the teacher's softened probability for token iii (the target)
  • pip_ipi​ is the student's softened probability for token iii (the current prediction)
  • TTT is temperature (higher TTT is softer and closer to uniform)
  • The T2T^2T2 factor compensates for gradient scaling. Hinton et al. note that gradients from soft targets scale as about 1/T21/T^21/T2, so multiplying by T2T^2T2 keeps the soft term's relative weight stable while you tune temperature.[1]Reference 1Distilling the Knowledge in a Neural Network.https://arxiv.org/abs/1503.02531
  • The hard task loss, when you use one, is ordinary cross-entropy at T=1T = 1T=1
  • KL measures extra surprisal from coding samples from qqq with a code built for ppp

Why use temperature T>1T > 1T>1 during logit distillation?

Answer

Higher temperature softens the teacher distribution, exposing near-miss classes instead of only the top answer. The student learns the teacher's ranking over alternatives rather than a hard label alone.

Two implementation details can turn a plausible loss into bad supervision. First, next-token training needs a one-token shift: logits at position ttt train against the label at position t+1t+1t+1. Second, direct logit KD assumes teacher and student use the same token-to-id output mapping. Equal vocabulary sizes aren't enough: token id 42 must denote the same token in both models. If the output spaces differ, plain token-level KL no longer lines up and you usually fall back to response distillation or design an explicit mapping.

⚠️ Common mistake: Running logit distillation without verifying tokenizer alignment. Two models can share a vocabulary size and still disagree on token-id mappings. Compare the complete output mapping, not vocab_size alone, before training.

Use this stdlib toy loss to check both mechanics without a neural net. It shifts next-token labels, skips ignored positions, computes T2T^2T2 KL plus hard cross-entropy, and fails fast on a reordered vocabulary.

causal-kd-loss.py
1import math 2 3VOCAB = ("7d", "3d", "30d", "never") 4 5def softmax(logits: list[float], temperature: float) -> list[float]: 6 scaled = [logit / temperature for logit in logits] 7 offset = max(scaled) 8 exps = [math.exp(logit - offset) for logit in scaled] 9 total = sum(exps) 10 return [value / total for value in exps] 11 12def log_softmax(logits: list[float], temperature: float) -> list[float]: 13 return [math.log(prob) for prob in softmax(logits, temperature)] 14 15def kl_divergence(teacher_probs: list[float], student_log_probs: list[float]) -> float: 16 return sum( 17 teacher_prob * (math.log(teacher_prob) - student_log_prob) 18 for teacher_prob, student_log_prob in zip(teacher_probs, student_log_probs) 19 ) 20 21def cross_entropy(logits: list[float], label: int) -> float: 22 return -log_softmax(logits, 1.0)[label] 23 24def distillation_loss( 25 student_logits: list[list[float]], 26 teacher_logits: list[list[float]], 27 labels: list[int], 28 student_vocabulary: tuple[str, ...], 29 teacher_vocabulary: tuple[str, ...], 30 temperature: float = 3.0, 31 alpha: float = 0.5, 32 ignore_index: int = -100, 33) -> float: 34 if student_vocabulary != teacher_vocabulary: 35 raise ValueError( 36 "Logit KD requires identical token-to-id mappings. " 37 "Use response KD or design an explicit mapping when output spaces differ." 38 ) 39 width = len(student_vocabulary) 40 if any(len(row) != width for row in student_logits + teacher_logits): 41 raise ValueError("Logit rows and vocabulary dimensions must agree.") 42 43 # Causal LMs predict token t+1 from positions up to t. 44 shift_student = student_logits[:-1] 45 shift_teacher = teacher_logits[:-1] 46 shift_labels = labels[1:] 47 48 soft_terms: list[float] = [] 49 hard_terms: list[float] = [] 50 for student_row, teacher_row, label in zip(shift_student, shift_teacher, shift_labels): 51 if label == ignore_index: 52 continue 53 teacher_probs = softmax(teacher_row, temperature) 54 student_log_probs = log_softmax(student_row, temperature) 55 soft_terms.append(kl_divergence(teacher_probs, student_log_probs) * temperature**2) 56 hard_terms.append(cross_entropy(student_row, label)) 57 58 if not soft_terms: 59 return 0.0 60 soft_loss = sum(soft_terms) / len(soft_terms) 61 hard_loss = sum(hard_terms) / len(hard_terms) 62 return alpha * soft_loss + (1 - alpha) * hard_loss 63 64student_logits = [ 65 [0.20, 0.10, 0.00, -0.10], 66 [1.50, 0.40, 0.10, -0.20], 67 [0.30, 2.00, 0.20, 0.00], 68 [0.10, 0.20, 0.30, 0.40], 69] 70teacher_logits = [ 71 [2.00, 0.50, 0.10, 0.00], 72 [3.00, 1.00, 0.20, 0.10], 73 [0.20, 2.50, 0.40, 0.10], 74 [0.00, 0.10, 0.20, 1.50], 75] 76labels = [0, 0, 1, -100] 77 78loss = distillation_loss( 79 student_logits, 80 teacher_logits, 81 labels, 82 VOCAB, 83 VOCAB, 84 temperature=3.0, 85 alpha=0.6, 86) 87mismatch_failed = False 88try: 89 distillation_loss( 90 student_logits, 91 teacher_logits, 92 labels, 93 VOCAB, 94 tuple(reversed(VOCAB)), 95 ) 96except ValueError as exc: 97 mismatch_failed = "token-to-id mappings" in str(exc) 98 99print("loss:", round(loss, 4)) 100print("loss_is_finite:", math.isfinite(loss)) 101print("mismatch failed:", mismatch_failed)
Output
1loss: 0.72 2loss_is_finite: True 3mismatch failed: True

White-box logits are the richest token-level channel. Many teachers expose text only, so the supervision contract changes.

When you only have text: response distillation

Without weights or logits, response distillation is the available KD channel: the teacher writes answers, and the student fine-tunes on selected (prompt, response) pairs.

This is Supervised Fine-Tuning (SFT) on teacher-generated targets, not ground truth. An API commonly provides text rather than full token probabilities, so the channel is thinner than direct logit access. A teacher can provide worked solutions, decomposed subproblems, critiques, or multiple candidates, but those outputs should pass task-specific checks where you have them. For the restore-window assistant, ask which row you would train on before looking at the teacher's prose: only an answer that matches a trusted rule belongs in the target set. That is the same verifier boundary used in the RLVR chapter.

Student / recipeTeacherSignal transferredWhy it matters
Alpaca 7B[5]Reference 5Stanford Alpaca: An Instruction-following LLaMA Model.https://github.com/tatsu-lab/stanford_alpacatext-davinci-00352K generated instruction-response examplesThe repository reports preliminary instruction-following evaluation and clear non-commercial dataset terms.
Orca 13B[3]Reference 3Orca: Progressive Learning from Complex Explanation Traces of GPT-4.https://arxiv.org/abs/2306.02707GPT-4 + ChatGPTExplanation traces and task instructionsEvaluates a richer generated-trace recipe, rather than logit KD.
phi-1.5[4]Reference 4Textbooks Are All You Need II: phi-1.5 technical report.https://arxiv.org/abs/2309.05463Existing LLMs + curated synthetic dataTextbook-like synthetic corporaAdjacent synthetic-data recipe, not a teacher-distribution KD comparison.
Distilling Step-by-Step[6]Reference 6Distilling Step-by-Step! Outperforming Larger Language Models with Less Training Data.https://arxiv.org/abs/2305.02301Generated rationales plus labelsRationales as an extra supervised targetTests whether smaller students improve on the studied tasks with rationale supervision.
Gemma 2 2B / 9B[2]Reference 2Gemma 2: Improving Open Language Models at a Practical Sizehttps://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdfLarger Gemma teachersToken-probability distillation during pretrainingReports a controlled 2B distilled-versus-from-scratch ablation.
DeepSeek-R1-Distill[7]Reference 7DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learninghttps://arxiv.org/abs/2501.12948DeepSeek-R1About 800K selected SFT examplesFine-tunes Qwen2.5 and Llama bases (1.5B, 7B, 8B, 14B, 32B, 70B) with SFT only, no RL stage on those distilled checkpoints. The paper reports 55.5% on AIME 2024 for Distill-Qwen-7B and 72.6% for Distill-Qwen-32B.

Response distillation and synthetic-data training overlap when a stronger model generates the selected targets. The label "distillation" doesn't remove the need for evaluation. Generated traces can be wrong, stylistically misleading, contaminated, or out of scope for the intended student.

Use the restore-window gate as a small prediction test. The teacher is trusted on disabled prod key restore and wrong on expired admin restore, so predict which prompt the filter rejects.

teacher-output-gate.py
1generated = [ 2 {"prompt": "disabled prod key restore", "teacher": "7 days", "verified": "7 days"}, 3 {"prompt": "expired admin restore", "teacher": "7 days", "verified": "not allowed"}, 4 {"prompt": "rotate staging key window", "teacher": "not restorable", "verified": "not restorable"}, 5] 6 7accepted = [ 8 example for example in generated 9 if example["teacher"] == example["verified"] 10] 11rejected = [ 12 example["prompt"] for example in generated 13 if example["teacher"] != example["verified"] 14] 15 16print("generated:", len(generated)) 17print("accepted:", len(accepted)) 18print("rejected prompts:", rejected) 19print("teacher text is trusted label:", len(rejected) == 0)
Output
1generated: 3 2accepted: 2 3rejected prompts: ['expired admin restore'] 4teacher text is trusted label: False

Text and logits supervise the output. With white-box access you can also ask hidden states to match, which introduces a projection problem that output losses don't have.

Aligning internal layers: feature distillation

Logit distillation matches output distributions. White-box access also lets a training objective match selected student hidden states to selected teacher hidden states through a learned projection.

Lfeature=∑l∥flteacher−g(flstudent)∥2\mathcal{L}_{\text{feature}} = \sum_l \|f_l^{\text{teacher}} - g(f_l^{\text{student}})\|^2Lfeature​=∑l​∥flteacher​−g(flstudent​)∥2

flteacherf_l^{\text{teacher}}flteacher​ and flstudentf_l^{\text{student}}flstudent​ are hidden states at layer lll, and g(⋅)g(\cdot)g(⋅) projects student features into the teacher feature space before comparison.

Why does feature distillation need the projection g(⋅)g(\cdot)g(⋅)?

Answer

The student and teacher may have different hidden sizes. The projection maps student features into the teacher's feature space so the layer representations can be compared.

Feature matching adds three decisions before it adds value: which layers correspond, how to train the projection, and whether its extra compute improves held-out outcomes. Hidden-state access is a richer interface, not a guarantee of a better student.

MethodTeacher signalMain advantageMain constraint
Response KDSelected text outputsWorks without white-box accessTeacher errors become SFT targets unless filtered
Logit KDToken probabilitiesPreserves distribution informationRequires aligned output space or an explicit mapping
Feature KDSelected hidden statesExposes intermediate representationsNeeds layer/projection design and more storage or compute
On-policy KDTeacher scores on student samplesVisits prefixes the student producesRequires online sampling and teacher evaluation

So far KL was written as DKL(q∥p)D_{\mathrm{KL}}(q \| p)DKL​(q∥p), with the teacher on the left. For generation, that direction is a choice, not a law.

Forward versus reverse KL

When minimizing KL for language generation, direction matters. Classical distillation commonly minimizes forward KL (teacher ∣∣||∣∣ student), which penalizes a student for missing probability mass that the teacher assigns to continuations. When a small student can't model the teacher distribution well, that coverage pressure can be costly. Predict the consequence: a capacity-limited student may spread probability across many teacher modes instead of committing to a usable answer.

Reverse KL (student ∣∣||∣∣ teacher) places more pressure on probability mass the student assigns where the teacher assigns little. MiniLLM reports improvements over its studied standard-KD baselines using reverse KL with an on-policy optimization algorithm in instruction-following experiments.[8]Reference 8MiniLLM: On-Policy Distillation of Large Language Models.https://arxiv.org/abs/2306.08543 GKD evaluates multiple divergences and reports that the best divergence depends on task and the diversity-performance tradeoff.[9]Reference 9On-Policy Distillation for Language Models.https://arxiv.org/abs/2306.13649

DirectionFormulaBehaviorCommon fit
Forward KLDKL(Pteacher∥Pstudent)D_{KL}(P_{teacher} \| P_{student})DKL​(Pteacher​∥Pstudent​)Mean-seeking, covers more of the teacher distributionClassic KD when broad coverage matters
Reverse KLDKL(Pstudent∥Pteacher)D_{KL}(P_{student} \| P_{teacher})DKL​(Pstudent​∥Pteacher​)Penalizes student mass in teacher-low-probability regionsCandidate objective to evaluate for generation

Stay inside the restore-window story. At a later token the teacher still prefers 7d (0.58) over 3d (0.40) and almost never predicts never (0.02). Predict which student should trigger the larger reverse-KL penalty: one covers the ranking but leaks 0.10 onto never; another dumps 0.25 onto never. The latter is the stronger out-of-support mistake, and reverse KL more than doubles while forward KL rises less.

Two bar charts of KL on a shared 0 to 0.50 axis. Teacher mass is 0.58 on 7d, 0.40 on 3d, and 0.02 on never. When the student leaks 0.10 onto never, forward KL is 0.051 and reverse KL is 0.084. When never-mass rises to 0.25, forward KL is 0.218 and reverse KL is 0.436, so reverse KL reacts more strongly to student mass where the teacher is near zero.
Both charts use the same 0 to 0.50 KL axis. Reverse KL jumps from 0.084 to 0.436 when the student piles mass on never, the token the teacher almost never predicts. That makes reverse KL a relevant candidate, not an automatic winner.

No divergence wins every task. Measure task quality, diversity, calibration, and failure rates under the decoding setup you will ship.

kl-direction-diagnostic.py
1import math 2 3teacher = {"7d": 0.58, "3d": 0.40, "never": 0.02} 4students = { 5 "covers_teacher": {"7d": 0.54, "3d": 0.36, "never": 0.10}, 6 "dumps_on_never": {"7d": 0.40, "3d": 0.35, "never": 0.25}, 7} 8 9def kl(left: dict[str, float], right: dict[str, float]) -> float: 10 return sum(prob * math.log(prob / right[token]) for token, prob in left.items()) 11 12for name, student in students.items(): 13 forward = kl(teacher, student) 14 reverse = kl(student, teacher) 15 print(name, "forward:", round(forward, 3), "reverse:", round(reverse, 3)) 16 17print("choose objective from evaluation, not slogan")
Output
1covers_teacher forward: 0.051 reverse: 0.084 2dumps_on_never forward: 0.218 reverse: 0.436 3choose objective from evaluation, not slogan

KL direction decides how the student fits a distribution on a given prefix. It can't help with prefixes the distillation set never contains.

Off-policy versus on-policy distillation

Off-policy (standard) distillation trains the student on a fixed set of prefixes, which may come from ground-truth sequences or teacher-generated outputs. At inference the student generates its own tokens, so its prefix distribution shifts. Predict the failure: once one early token changes, later prefixes can leave the fixed training set, and errors compound. That's exposure bias.

On-policy methods such as Generalized Knowledge Distillation (GKD) sample sequences from the student, then compare student and teacher token distributions on the prefixes the student produced. GKD can mix fixed outputs and student-generated outputs through a student-data fraction λ\lambdaλ; it doesn't require a natural-language critique.[9]Reference 9On-Policy Distillation for Language Models.https://arxiv.org/abs/2306.13649 The cost is computational: both student sampling and teacher scoring run during training. This is useful when fixed teacher data misses prefixes that the deployed student commonly enters, but the benefit has to be measured per task.

Gemma 2's post-training recipe applies the same idea after SFT on predominantly synthetic teacher responses: the authors also distill from the teacher on the student's distribution, citing GKD and MiniLLM.[2]Reference 2Gemma 2: Improving Open Language Models at a Practical Sizehttps://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdf[9]Reference 9On-Policy Distillation for Language Models.https://arxiv.org/abs/2306.13649[8]Reference 8MiniLLM: On-Policy Distillation of Large Language Models.https://arxiv.org/abs/2306.08543

on-policy-prefix-coverage.py
1fixed_teacher_prefixes = { 2 "restore disabled prod key", 3 "rotate staging key", 4} 5student_generated_prefixes = { 6 "restore disabled prod key", 7 "restore expired admin key", 8 "restore key without approval", 9} 10 11unseen_in_fixed_data = student_generated_prefixes - fixed_teacher_prefixes 12teacher_scored_prefixes = fixed_teacher_prefixes | student_generated_prefixes 13 14print("fixed prefixes:", len(fixed_teacher_prefixes)) 15print("student prefixes needing new teacher scores:", sorted(unseen_in_fixed_data)) 16print("scored after on-policy collection:", len(teacher_scored_prefixes))
Output
1fixed prefixes: 2 2student prefixes needing new teacher scores: ['restore expired admin key', 'restore key without approval'] 3scored after on-policy collection: 4

The losses and sampling rules matter only if a training step updates the student and not the teacher. The next loop makes that invariant visible.

A practical distillation training loop

A typical loop freezes the teacher, sends the same input through both models, and updates only the student with the combined loss. The local example uses tiny positionwise PyTorch stand-ins rather than Transformers, so you can test the mechanics without downloading a real teacher.

tiny-kd-training-step.py
1import torch 2from torch import nn 3import torch.nn.functional as F 4 5class TinyLM(nn.Module): 6 def __init__(self, vocab_size: int, hidden_size: int): 7 super().__init__() 8 self.embedding = nn.Embedding(vocab_size, hidden_size) 9 self.output = nn.Linear(hidden_size, vocab_size) 10 11 def forward(self, input_ids: torch.Tensor) -> torch.Tensor: 12 return self.output(self.embedding(input_ids)) 13 14def kd_loss(student_logits: torch.Tensor, teacher_logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor: 15 temperature = 3.0 16 alpha = 0.7 17 18 shift_student = student_logits[:, :-1, :] 19 shift_teacher = teacher_logits[:, :-1, :].detach() 20 shift_labels = labels[:, 1:] 21 22 student_flat = shift_student.reshape(-1, shift_student.size(-1)) 23 teacher_flat = shift_teacher.reshape(-1, shift_teacher.size(-1)) 24 labels_flat = shift_labels.reshape(-1) 25 26 soft_teacher = F.softmax(teacher_flat / temperature, dim=-1) 27 soft_student = F.log_softmax(student_flat / temperature, dim=-1) 28 soft_loss = F.kl_div(soft_student, soft_teacher, reduction="batchmean") * temperature**2 29 hard_loss = F.cross_entropy(student_flat, labels_flat) 30 return alpha * soft_loss + (1 - alpha) * hard_loss 31 32torch.manual_seed(0) 33vocab_size = 12 34teacher = TinyLM(vocab_size=vocab_size, hidden_size=16) 35student = TinyLM(vocab_size=vocab_size, hidden_size=6) 36teacher.requires_grad_(False) 37teacher.eval() 38 39input_ids = torch.tensor([ 40 [1, 2, 3, 4, 5], 41 [1, 3, 5, 7, 9], 42]) 43labels = input_ids.clone() 44optimizer = torch.optim.AdamW(student.parameters(), lr=0.05) 45 46with torch.no_grad(): 47 teacher_logits = teacher(input_ids) 48 49before = kd_loss(student(input_ids), teacher_logits, labels) 50optimizer.zero_grad() 51before.backward() 52has_grad = any(parameter.grad is not None for parameter in student.parameters()) 53optimizer.step() 54 55after = kd_loss(student(input_ids), teacher_logits, labels) 56 57print("before:", round(float(before), 4)) 58print("after:", round(float(after), 4)) 59print("has_grad:", has_grad) 60print("after_is_finite:", bool(torch.isfinite(after))) 61print("improved:", bool(after < before))
Output
1before: 1.1513 2after: 0.9793 3has_grad: True 4after_is_finite: True 5improved: True

A deployed pipeline usually puts this pattern inside a framework trainer, with aligned-vocabulary models such as a larger Gemma teacher and smaller Gemma student when you need direct token-probability distillation. Teams may pre-compute some teacher signal offline to avoid running the teacher inside every student update. Dense next-token logits across a long corpus are costly to store, so a design may consider top-k logits, teacher responses, or online scoring, then measure the quality effect of compression. The payload-only estimate below ignores metadata and storage-format overhead, so treat it as a lower-bound sizing exercise.

logit-cache-budget.py
1tokens = 50_000_000 2vocab_size = 32_000 3bytes_per_logit = 2 # bf16 4top_k = 64 5bytes_per_topk_item = 2 + 4 # bf16 value plus int32 token id 6 7dense_bytes = tokens * vocab_size * bytes_per_logit 8topk_bytes = tokens * top_k * bytes_per_topk_item 9gib = 1024 ** 3 10 11print("dense cache GiB:", round(dense_bytes / gib, 1)) 12print("top-k cache GiB:", round(topk_bytes / gib, 1)) 13print("storage reduction:", round(dense_bytes / topk_bytes, 1), "x") 14print("quality must still be evaluated:", True)
Output
1dense cache GiB: 2980.2 2top-k cache GiB: 17.9 3storage reduction: 166.7 x 4quality must still be evaluated: True

If an online-distillation pilot is feasible, compare it with an offline baseline before committing to large-scale data generation. That comparison can show whether fresh teacher scoring earns enough held-out quality to justify its compute cost for this task.

The training step assumes a legal, filtered corpus. For response KD, corpus design is the main lever you control, alongside student capacity and the evaluation split.

Building a distillation dataset

When using response-based distillation, selected data, student capacity, and training budget set the ceiling. Build a generation and selection pipeline that can reject incorrect, duplicate, contaminated, or irrelevant examples before they become targets.

Seed-Expand-Filter pipeline

A Seed-Expand-Filter pipeline gives teacher-data generation a sequence of decisions. It doesn't prove quality by itself. Each filter needs a measurable contract and a separate evaluation split.

  1. Seed: Start with a small set of high-quality, human-written prompts (for example, 100 restore-window and key-rotation questions).
  2. Expand: Ask the teacher to generate new, diverse variations of those prompts.
  3. Generate: Have the teacher answer the new prompts, often with rationales or decomposed steps when richer supervision helps.
  4. Filter: Use checks, deduplication, safety screening, or reviewed scoring rules to reject unsuitable generations.

Use the three-row fixture from the teacher-output gate to see those filters fire:

PromptTeacher textGateDecision
disabled prod key restore7 daysmatches the trusted answerkeep
Disabled prod key restore (not generated)same normalized promptskip as duplicate
expired admin restore7 daystrusted answer is not allowedreject

This follows the broad shape of Alpaca's Self-Instruct-style pipeline and Orca's richer explanation-trace generation, while real systems add deduplication, safety filters, and task balancing.[5]Reference 5Stanford Alpaca: An Instruction-following LLaMA Model.https://github.com/tatsu-lab/stanford_alpaca[3]Reference 3Orca: Progressive Learning from Complex Explanation Traces of GPT-4.https://arxiv.org/abs/2306.02707

Two checks are easy to skip and expensive to recover from: deduplicate prompts before paying for generation, and verify teacher answers before they become student targets. The wrapper encodes both.

select-teacher-responses.py
1from collections.abc import Callable 2 3class DistillationDataGenerator: 4 def __init__( 5 self, 6 teacher_generate: Callable[[str], str], 7 verify_response: Callable[[str, str], bool], 8 ): 9 self.teacher_generate = teacher_generate 10 self.verify_response = verify_response 11 12 def generate_dataset(self, prompts: list[str]) -> list[dict[str, str]]: 13 selected: list[dict[str, str]] = [] 14 seen: set[str] = set() 15 for prompt in prompts: 16 normalized = " ".join(prompt.lower().split()) 17 if normalized in seen: 18 continue 19 seen.add(normalized) 20 response = self.teacher_generate(normalized).strip() 21 if self.verify_response(normalized, response): 22 selected.append({"prompt": normalized, "response": response}) 23 return selected 24 25def fake_teacher(prompt: str) -> str: 26 return "7 days" 27 28trusted_answers = { 29 "disabled prod key restore": "7 days", 30 "expired admin restore": "not allowed", 31} 32 33def verify_response(prompt: str, response: str) -> bool: 34 return trusted_answers[prompt] == response 35 36generator = DistillationDataGenerator(fake_teacher, verify_response) 37examples = generator.generate_dataset([ 38 "disabled prod key restore", 39 " Disabled prod key restore ", 40 "expired admin restore", 41]) 42 43print("selected prompts:", [example["prompt"] for example in examples]) 44print("selected count:", len(examples)) 45print("bad response retained:", any("expired admin" in example["prompt"] for example in examples))
Output
1selected prompts: ['disabled prod key restore'] 2selected count: 1 3bad response retained: False

Keep training data out of evaluation

Teacher generation can quietly contaminate a benchmark when prompts, reference solutions, or close rewrites enter the student training set. Block exact normalized overlap before training. For real releases, extend the gate with near-duplicate and reference-solution checks.

held-out-contamination-gate.py
1def normalize(prompt: str) -> str: 2 return " ".join(prompt.lower().replace("?", "").split()) 3 4candidate_training_prompts = [ 5 "Compute retry budget for a failed restore", 6 "Summarize failed restore logs", 7 "How many days can a disabled production API key stay restorable?", 8] 9held_out_prompts = [ 10 "how many days can a disabled production API key stay restorable", 11 "Estimate GPU queue delay for a remote region", 12] 13 14held_out_keys = {normalize(prompt) for prompt in held_out_prompts} 15accepted = [ 16 prompt for prompt in candidate_training_prompts 17 if normalize(prompt) not in held_out_keys 18] 19blocked = [ 20 prompt for prompt in candidate_training_prompts 21 if normalize(prompt) in held_out_keys 22] 23 24print("accepted training prompts:", len(accepted)) 25print("blocked overlap:", blocked) 26print("held-out exact overlap after gate:", any(normalize(p) in held_out_keys for p in accepted))
Output
1accepted training prompts: 2 2blocked overlap: ['How many days can a disabled production API key stay restorable?'] 3held-out exact overlap after gate: False

A clean corpus still can't make a small model remember behavior beyond its capacity or context window, and it can't settle license constraints.

Limitations and when not to distill

Distillation doesn't erase context-window or data-coverage limits. A student may beat its teacher on a narrow checked metric after filtering or task-specific training, while regressing on other behavior. Treat teacher and student as separate artifacts to evaluate.

Before investing in a distillation pipeline, write the behavior contract and its tests. If the student passes common restore cases but fails expired-admin or long-context cases, capacity, context length, prompt coverage, and teacher-target quality are competing explanations. Split those hypotheses with targeted slices instead of reading one aggregate score.

BehaviorRegression risk to testUseful held-out gate
Domain answersGenerated targets can repeat teacher errorsChecked answer accuracy and abstention rate
Instruction followingNarrow traces can miss new constraintsFresh constraint-following prompts
Multi-step solutionsFinal answers can hide invalid stepsStep checks where available plus final-answer accuracy
Long-context useStudent architecture or context limit may differRetrieval and long-context slices at deployment length
Safety and policy behaviorFiltered corpus may omit refusals or edge casesSafety-policy evaluation separate from task benchmark

Legal and ethical considerations

Because the student can mirror teacher outputs closely, the origins of that training data matter.

  • Provider terms matter: the Stanford Alpaca release was research-only and non-commercial, and the repo points to both the underlying LLaMA restrictions and the dataset's CC BY-NC 4.0 terms.[5]Reference 5Stanford Alpaca: An Instruction-following LLaMA Model.https://github.com/tatsu-lab/stanford_alpaca
  • Restrictions must be reviewed: before generating a corpus or shipping a student, review the teacher access terms, base-student license, generated-data license, and permitted use of outputs. Don't infer permission from technical access.
  • Imitation isn't capability proof: a student may reproduce style or familiar output patterns while failing new checked tasks. Held-out evaluation, not resemblance, establishes value.

Cost-quality tradeoff

Choose a distillation method from measured quality and economics. Richer teacher access enables different losses; it doesn't rank final models without evaluation.

ApproachRequired accessMain training costRelease gate
Use teacher directlyTeacher inferenceNo student trainingBaseline quality, latency, and cost
Response KDGenerated outputs and permitted useGeneration plus SFTOutput filtering and held-out task quality
Logit KDAligned teacher token probabilitiesTeacher scoring or cache storageTask quality plus cache/online cost
Feature KDHidden states and layer mappingExtra projections and state transferAblation against simpler KD baseline

Distillation loss is an optimization signal, not a release decision. A student can match teacher probabilities on training batches and still regress on held-out generation quality, long-context behavior, or latency targets. Measure task metrics, pairwise win rate, and real serving cost together.

deployment-gate.py
1teacher = {"checked_accuracy": 0.94, "policy_error_rate": 0.01, "latency_ms": 180, "monthly_cost": 42000} 2student = {"checked_accuracy": 0.92, "policy_error_rate": 0.04, "latency_ms": 42, "monthly_cost": 18000} 3requirements = { 4 "checked_accuracy": 0.90, 5 "max_policy_error_rate": 0.02, 6 "max_latency_ms": 60, 7 "max_monthly_cost": 20000, 8} 9 10checks = { 11 "quality": student["checked_accuracy"] >= requirements["checked_accuracy"], 12 "policy": student["policy_error_rate"] <= requirements["max_policy_error_rate"], 13 "latency": student["latency_ms"] <= requirements["max_latency_ms"], 14 "cost": student["monthly_cost"] <= requirements["max_monthly_cost"], 15} 16 17print("student faster:", student["latency_ms"] < teacher["latency_ms"]) 18print("student cheaper:", student["monthly_cost"] < teacher["monthly_cost"]) 19print("release checks:", checks) 20print("deploy student:", all(checks.values()))
Output
1student faster: True 2student cheaper: True 3release checks: {'quality': True, 'policy': False, 'latency': True, 'cost': True} 4deploy student: False

The gate makes that trade-off concrete: this student is faster and cheaper, and its checked accuracy clears the threshold, but its policy error rate doesn't. Keep the teacher or revise the student until every release check passes.

When distillation breaks

SymptomCauseFix
Validation loss barely changes as you raise temperature.The softened teacher distribution may be too flat or the soft-loss weight may be ineffective.Inspect teacher probabilities and tune temperature and loss weight on held-out tasks.
Student looks strong on training prompts but weak on held-out tasks.Distillation corpus is too narrow, repetitive, or too close to evaluation data.Broaden prompt coverage, filter duplicates, and keep a separate held-out evaluation slice.
Student predicts current token instead of next token during logit KD.Causal LM loss forgot the one-token shift.Shift logits at position ttt against labels at position t+1t+1t+1 before KL or cross-entropy.
Student copies teacher hallucinations and policy mistakes.Distillation blindly transferred bad teacher outputs.Filter teacher generations, add task loss, and evaluate against trusted labels or reward checks.
KL loss runs but student quality stays random.Teacher and student token-to-id mappings don't align, even if sizes match.Compare mappings exactly, use response distillation, or design an explicit output-space mapping.
Tiny student misses required checked behaviors.Student capacity, context, or data coverage is insufficient for this release target.Narrow task scope, increase student size, or revise training and evaluation design.
Offline metrics look great but production quality collapses.Distillation and evaluation data leaked into each other.Split generation, tuning, and evaluation sets cleanly before training starts.
Complete the lesson

Mastery Check

Answer every question, then check your score. Score 75% or higher to mark this lesson complete.

1.Why does the distillation recipe use a teacher temperature above 1?

Correct answer: It softens the teacher distribution so non-top alternatives carry informative relative probabilities.

A higher temperature exposes dark knowledge in the teacher's alternatives. The student can learn relative class or token preferences that disappear when supervision is reduced to a one-hot target.

2.A team raises distillation temperature but leaves the soft-loss magnitude unadjusted. What article-grounded correction is missing?

Correct answer: Multiply the temperature-scaled soft loss by T², then mix it with the hard loss using the chosen alpha.

Temperature changes gradient scale as well as probabilities. The article's objective compensates the soft branch with T² before combining it with hard-label supervision.

3.A causal KD implementation compares student_logits[:, :-1] with labels at the same positions. Which fix is required?

Correct answer: Pair student and teacher logits at [:, :-1] with hard labels at [:, 1:], using identical token-ID mappings.

For causal next-token prediction, logits at position t predict token t+1, so the soft teacher-student comparison uses aligned logits while the hard branch pairs logits[:, :-1] with labels[:, 1:]. Both models must assign the same meaning to each output ID.

4.When is text-only response distillation safest for an assistant dataset?

Correct answer: When teacher-generated responses are gated by trusted labels or task checks instead of being accepted blindly.

A teacher can be confidently wrong. Response KD is useful when trusted labels or validation gates constrain where synthetic teacher outputs enter the training set.

5.Teacher and student hidden states have different widths, but a feature-distillation loss is required. What is the principled adaptation?

Correct answer: Project one representation into the other's dimension with a learned alignment layer before comparing features.

Feature KD needs comparable spaces. A projection layer aligns widths while preserving a learnable mapping; zero padding or truncation silently changes the intended representation loss.

6.What distinction matters when choosing forward KL for distillation?

Correct answer: Forward KL, written teacher || student, penalizes missing teacher-supported modes differently from reverse KL.

KL direction is not cosmetic. Forward KL from teacher to student and reverse KL have different mode-covering behavior, so the direction must match the intended supervision rather than being swapped by habit.

7.Why does on-policy KD feed student-generated prefixes back into the teacher?

Correct answer: It evaluates teacher and student on states the student is likely to visit, exposing compounding-prefix errors.

Teacher-forced prefixes can hide errors that appear after the student deviates. On-policy prefixes make the distillation signal cover the student's own state distribution.

8.Which update boundary preserves the intended teacher-student roles during KD?

Correct answer: Keep the teacher frozen in evaluation mode and apply optimizer updates only to student parameters.

The teacher supplies a fixed target distribution or representation. Updating it during the same objective turns the target into a moving participant and no longer matches the stated distillation setup.

9.The deployment card reports student quality 0.92, policy error 0.04, latency 42 ms, and cost 18,000 against gates quality >= 0.90, policy <= 0.02, latency <= 60, cost <= 20,000. What decision follows?

Correct answer: Block promotion because the policy-error gate fails, despite quality, latency, and cost passing.

The card uses conjunctive gates. A policy error of 0.04 exceeds the allowed 0.02, so the candidate is blocked even though the other three metrics meet their thresholds.

9 questions remaining.

Next Step
Continue to Model Merging and Weight Interpolation

Distillation trains a new student from teacher signal. Model merging asks whether compatible checkpoints can be combined into one candidate without another gradient-training run.

PreviousRLVR & Verifiable Rewards
Share this article
XFacebookLinkedInBlueskyRedditHacker NewsEmail
References

Distilling the Knowledge in a Neural Network.

Hinton, G., Vinyals, O., & Dean, J. · 2015

https://arxiv.org/abs/1503.02531

Gemma 2: Improving Open Language Models at a Practical Size

Gemma Team, Google DeepMind · 2024

https://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdf

Orca: Progressive Learning from Complex Explanation Traces of GPT-4.

Mukherjee, S., et al. · 2023

https://arxiv.org/abs/2306.02707

Textbooks Are All You Need II: phi-1.5 technical report.

Li, Y., et al. · 2023

https://arxiv.org/abs/2309.05463

Stanford Alpaca: An Instruction-following LLaMA Model.

Taori, R., et al. · 2023 · GitHub

https://github.com/tatsu-lab/stanford_alpaca

Distilling Step-by-Step! Outperforming Larger Language Models with Less Training Data.

Hsieh, C., et al. · 2023

https://arxiv.org/abs/2305.02301

DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning

DeepSeek-AI · 2025

https://arxiv.org/abs/2501.12948

MiniLLM: On-Policy Distillation of Large Language Models.

Gu, Y., et al. · 2024

https://arxiv.org/abs/2306.08543

On-Policy Distillation for Language Models.

Agarwal, R., et al. · 2024

https://arxiv.org/abs/2306.13649

Discussion

Questions and insights from fellow learners.

Discussion loads when you reach this section.