Overview

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.
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 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.
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.
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.
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.
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.
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.
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.

FAQ

Why is knowledge recovery so important after structural pruning?Structural pruning removes model depth or width, which usually causes a loss of knowledge and capability. Recovery is critical because it determines whether the pruned model can regain performance and possibly match or even surpass the original base model.
What decides how well a pruned model can recover its lost knowledge?Recovery depends on two main factors: which parts of the model were removed and how the model is trained afterward to recover the lost knowledge. The pruning choice itself can strongly affect how much the model degrades and how easily it can recover.
Why does importance-based pruning often recover better than removing the last blocks?Importance-based pruning removes the least useful blocks, so the student model starts from a stronger baseline after pruning. In the chapter’s experiments, this strategy recovered faster and achieved better final results than simply removing the last blocks.
Which pruning strategy performed best in the chapter’s comparison experiments?The data-driven individual strategy, EXP01, performed best. It removed the least important blocks identified by cosine similarity analysis, producing the lowest initial degradation and the strongest final recovery after distillation.
Why is the recovery dataset chosen so carefully?The dataset determines what kind of knowledge the model prioritizes during recovery. A high-quality, diverse dataset helps the student recover general capabilities more effectively and avoids noisy or biased training signals.
What is the purpose of aligning teacher and student hidden states?Hidden-state alignment helps the student learn not only the final output, but also the internal reasoning process of the teacher. This is especially useful when intermediate representations matter, such as in embedding models or RAG systems.
How do uniform, last-block, and original layer mappings differ?Uniform mapping distributes teacher layers proportionally across the student. Last-block mapping aligns student layers with the teacher’s deepest layers. Original mapping preserves the original architectural positions of the layers that were not pruned.
Why does the compound loss combine hard labels, soft labels, and hidden-state alignment?Hard labels teach the correct answer, soft labels transfer the teacher’s probability distribution and confidence, and hidden-state alignment stabilizes the student’s internal representations. Together, they provide a more complete recovery signal than any one component alone.
When should Skew KLD and Feature Dynamics Distillation be used?They are most useful when pruning is aggressive or the recovery dataset is limited. Skew KLD smooths training when student and teacher distributions differ greatly, while FDD helps align how representations evolve across layers.
Can width pruning and depth pruning be combined in the same recovery pipeline?Yes. Width pruning changes the MLP intermediate size, but the Transformer hidden dimensions remain compatible, so the same distillation workflow still applies. This means width pruning and depth pruning can be stacked to build a more efficient model.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs Rearchitecting LLMs ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs Rearchitecting LLMs ebook for free