Overview

6 Knowledge recovery through distillation

Knowledge recovery after structural pruning is the central challenge in rebuilding a smaller LLM, because removing depth or width inevitably strips away knowledge and capability. The chapter argues that recovery begins before training, when you decide what to prune: data-driven layer importance analysis tends to preserve more recoverable knowledge than simple heuristics like dropping the last blocks or blindly protecting the final layers. In the experiments, pruning four of Gemma-3-270M’s 18 blocks with cosine-similarity-based importance scoring produced the strongest starting point for distillation, showing that the choice of removed blocks strongly shapes both the immediate damage and the eventual recovery ceiling.

To restore performance, the chapter moves beyond classic logits-only distillation and builds a compound training setup that combines hard labels, soft labels, and hidden-state alignment. The student and teacher are aligned through mapping strategies that handle unequal depth, including uniform, last-block, and original-index mappings, and the loss blends task loss, KL-based logit loss, and feature alignment. The results show that logits distillation alone can recover a pruned model effectively when enough data is available, while feature alignment becomes more useful when the hidden states themselves matter or when pruning is more aggressive. The chapter also introduces advanced recovery techniques: Skew KLD smooths overly sharp mismatches between teacher and student distributions, and Feature Dynamics Distillation aligns how representations change from one layer to the next rather than only matching static activations.

Across the recovery experiments, more data consistently improved the outcome, with 2K samples giving only modest gains, 15K enabling strong recovery, and 40K allowing the student to match or even surpass the teacher on perplexity while keeping most benchmark capability. Advanced methods delivered their best value at larger data scales, where they slightly improved perplexity at a small cost in compute and, sometimes, a tiny capability tradeoff. The overall guidance is practical: use data-driven pruning, start with a balanced compound loss, keep feature losses low or off for purely generative models unless needed, and reserve advanced techniques for cases with limited data, aggressive pruning, or tasks where intermediate representations are the end product. The chapter closes by emphasizing that this pruning-plus-distillation pipeline is not just theoretical, but reflects real industry practice for building efficient small language models.

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.

FAQ

Why does structural pruning reduce model knowledge, and why is recovery so important?Structural pruning removes Transformer blocks or neurons, which usually causes the model to lose knowledge and capabilities. Recovery is critical because it determines whether the smaller model can match, or even surpass, the original base model after pruning.
What should be considered before pruning a model for knowledge recovery?The most important decision is which parts of the model to keep or remove. The choice of removed blocks strongly affects how much knowledge is lost and how quickly the model can recover during distillation.
What are the main depth pruning strategies compared in this chapter?The chapter compares data-driven individual pruning, data-driven consecutive pruning, removing the last blocks, and preserving the final blocks. Data-driven methods use importance analysis, while heuristic methods rely on architectural assumptions.
Why did the data-driven individual pruning strategy perform best?It removed the least important blocks based on cosine-similarity importance analysis, so it preserved more useful knowledge than heuristic strategies. As a result, it degraded less before recovery and reached the best final performance after distillation.
Why is the dataset used for distillation so important?The distillation dataset defines what kind of knowledge the student model will recover. A high-quality, diverse dataset like Cosmopedia helps the student regain general capabilities more effectively and avoids noisy supervision that could hurt recovery.
How do teacher and student hidden states get aligned when they have different numbers of layers?The chapter uses layer mapping strategies. The main options are uniform mapping, last-block mapping, and original-indices mapping, each deciding how student layers correspond to teacher layers for feature alignment.
What is included in the compound distillation loss?The compound loss combines task loss from hard labels, logits loss from soft labels, and hidden-state alignment loss from intermediate layers. These components are weighted by alpha, beta, and gamma.
Why are temperature and KL divergence used in logits distillation?Temperature softens the teacher’s output distribution so the student can learn not only the top answer but also the alternatives the teacher considered. KL divergence measures the difference between the student and teacher probability distributions.
What do Feature Alignment and Feature Dynamics Distillation add beyond standard distillation?Feature Alignment aligns hidden states between teacher and student layers, while Feature Dynamics Distillation aligns how those hidden states change across layers. These techniques help when intermediate representations matter or when the student is highly compressed.
When should advanced techniques like Skew KLD and FDD be used?They are most useful when pruning is aggressive or the recovery dataset is large enough to benefit from more refined supervision. For small datasets, basic distillation is often enough; for larger datasets, advanced techniques can provide a modest but consistent perplexity advantage.

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


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 ebook for free
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 ebook for free