6 Knowledge recovery through distillation
Knowledge recovery after pruning is presented as the decisive stage in rearchitecting an LLM, because removing depth or width inevitably erases some of the model’s learned capability. The chapter emphasizes that recovery begins even before distillation training starts: the way blocks are chosen for removal strongly affects how much knowledge is lost and how quickly the student can regain it. A small, practical experiment shows that importance-based pruning preserves more recoverable knowledge than simply cutting the last layers, making block selection a first-order design decision rather than an afterthought.
The core recovery workflow combines three signals: hard labels from the dataset, soft labels from the teacher’s output distribution, and hidden-state alignment between teacher and student layers. To make this possible when the student has fewer blocks than the teacher, the chapter compares different mapping strategies, such as uniform mapping, last-layer mapping, and original-index mapping. The loss is built as a weighted blend of task loss, logits loss, and hidden-state loss, with temperature scaling used to soften the teacher’s predictions and cosine similarity used to compare internal representations. The chapter also shows how to implement the training loop efficiently with frozen teacher weights, batched tokenization, and gradient accumulation.
Experiments demonstrate that the best pruning strategy is the data-driven one that removes the least important individual blocks, and that distillation can substantially restore performance, especially as training data increases. With limited data, basic label-and-logit distillation is already strong, while advanced techniques such as Skew KL Divergence and Feature Dynamics Distillation become more useful as the dataset grows or pruning becomes more aggressive. The chapter closes with practical guidance: choose loss weights based on the final use case, rely more on hidden-state alignment when representations are the product you care about, and remember that good pruning decisions and good data matter more than any fancy recovery objective.
Comparison of layer removal strategies. All experiments remove 4 blocks (striped) from the original 18-block model. EXP01-02 use data-driven importance analysis (Listing 6.5), while EXP03-04 follow heuristic rules.
The three mapping strategies: Uniform mapping distributes proportionally, last mapping aligns with the deepest layers, and original mapping preserves the original architectural connections, leaving gaps where the removed layers were.
The compound loss combines three signals: Hard Labels from the dataset (the ground truth "France"), Soft Labels capturing the teacher's probability distribution across candidates, and Hidden States alignment between corresponding Transformer blocks. The weights α, β, γ control each component's influence on the total loss.
Temperature softens the teacher's probability distribution. At T=1.0, the model assigns 94.5% to "hard". At T=2.0, probabilities spread across alternatives (23.9%, 4.7%, 1.3%, 1.2%), exposing the teacher's reasoning process to the student.
Token shifting aligns predictions with labels. Each logit position predicts the next token in the sequence. The first label (WORK) is discarded because no logit predicts it, and the last logit is discarded because there's no future token to predict after <EOS>. The arrows show how logits[0] compares against labels[1], logits[1] against labels[2], and so on.
Convergence analysis showing perplexity (bars, left axis) and capabilities retention (lines with markers, right axis) across three training data scales. Different bar patterns (diagonal hatching, dots, solid) and line markers (circles, squares, triangles) distinguish the three distillation approaches tested at each scale.
Target distribution comparison. Left panel: Standard KLD with teacher targets (solid) and student predictions (diagonal hatching). Right panel: Skew KLD adds hybrid targets (dotted) computed as 90% teacher + 10% student.
The rearchitecting pipeline is flexible and modular. You can apply Width Pruning to reduce MLP dimensions, Depth Pruning to remove blocks, or combine both as shown here for maximum efficiency. Regardless of the structural changes chosen, the final step uses the original Base Model as the Teacher to recover lost knowledge through Distillation.
Summary
- Efficient model recovery doesn't start at training, but in the design phase, and that intelligently selecting which blocks to keep (instead of pruning blindly) drastically reduces the computational cost later on.
- Compound loss functions combine three complementary signals: hard labels from the dataset (task loss), soft probability distributions from the teacher (logits loss), and intermediate representations (hidden loss). The balance between these signals should align your final objective. Generative models prioritize logits, while embedding models need higher hidden loss weights.
- You can design Layer Mapping strategies to align a large Teacher with a small Student, addressing the challenge of mismatched architectural depth.
- You've seen how to implement specialized loss functions like Skew KLD and Feature Dynamics, designed to improve convergence when data is scarce or pruning has been aggressive.
- This approach isn't theoretical, but the production standard used by Nvidia (Minitron) and Mistral (Ministral), which allows you to apply top-tier engineering techniques at an accessible scale.
Rearchitecting LLMs ebook for free