Overview

4 Building smaller and faster LLMs with depth pruning

This chapter introduces depth pruning as a practical way to make large language models smaller and faster by removing whole Transformer blocks. It explains that the technique is aggressive but effective, especially because inference is often limited less by raw computation than by memory movement. By cutting blocks, a model reduces parameter load, KV-cache growth, and the repeated read/write overhead that slows GPU inference, while also shrinking latency and memory use.

The chapter then compares simple static pruning rules with data-driven selection. Static methods choose blocks by position or weight magnitude, and they are cheap and easy to apply, but they can miss task-specific structure and remove blocks that still matter for the target domain. To overcome that limitation, the chapter builds a dynamic method using PyTorch hooks to capture each block’s input and output activations and cosine similarity to estimate how much a block transforms information. A block with high similarity is treated as less important, and the process is calibrated on WinoGrande so the pruning decision reflects real task behavior rather than assumptions about model architecture.

Finally, the chapter shows how to turn these scores into pruning decisions and evaluate the results on benchmark tasks and inference speed. It highlights that the best block-removal pattern depends on the calibration data, that protecting the first and last blocks is often sensible, and that avoiding consecutive removals can sometimes improve retention. The experiments show clear speed gains from pruning, and the paper discussion connects these ideas to Shortened LLaMA and ShortGPT, reinforcing that cosine-based block influence is a lightweight but powerful alternative to more expensive importance measures such as perplexity or gradient-based Taylor estimates.

Structure of the transformer blocks of the DistilGPT2 and Qwen3-06B models. Each block contains two functional modules: Attention and MLP. In turn, each module is composed of individual computational layers such as linear projections, normalizations, and activation functions.
Structure of the transformer blocks of the DistilGPT2 and Qwen3-06B models. Each block contains two functional modules: Attention and MLP. In turn, each module is composed of individual computational layers such as linear projections, normalizations, and activation functions.
Impact of block selection on depth pruning. The gain in inference (+15%) is identical as we are removing four blocks in both scenarios. The performance cost varies drastically depending on which ones are removed.
Impact of block selection on depth pruning. The gain in inference (+15%) is identical as we are removing four blocks in both scenarios. The performance cost varies drastically depending on which ones are removed.
Comparison of block selection strategies for depth pruning. From left to right: The full base model. Static strategies that remove blocks based on their position: the first, the last, or the middle ones. A data-driven strategy, where the removed blocks are scattered according to their actual contribution to the task. The data-driven strategy combined with a protection heuristic, which preserves the initial and final blocks.
Comparison of block selection strategies for depth pruning. From left to right: The full base model. Static strategies that remove blocks based on their position: the first, the last, or the middle ones. A data-driven strategy, where the removed blocks are scattered according to their actual contribution to the task. The data-driven strategy combined with a protection heuristic, which preserves the initial and final blocks.
Three pairs of vectors from left to right: Vectors with similar meaning point in similar directions, unrelated vectors in different directions, vectors with opposite meanings in opposite directions.
Three pairs of vectors from left to right: Vectors with similar meaning point in similar directions, unrelated vectors in different directions, vectors with opposite meanings in opposite directions.
In the block evaluation pipeline, hooks are registered, a forward pass of the model is executed using the dataset, and the activations are captured to calculate the block importance. The result is a list containing the block identifier with the assigned importance.
In the block evaluation pipeline, hooks are registered, a forward pass of the model is executed using the dataset, and the activations are captured to calculate the block importance. The result is a list containing the block identifier with the assigned importance.
Layer importance across all 28 blocks of Qwen3-0.6B: static magnitude (weight-based) versus data-driven cosine similarity calibrated on Winogrande. Both metrics are normalized to the [0,1] range. Block 0 dominates the cosine ranking by a wide margin.
Layer importance across all 28 blocks of Qwen3-0.6B: static magnitude (weight-based) versus data-driven cosine similarity calibrated on Winogrande. Both metrics are normalized to the [0,1] range. Block 0 dominates the cosine ranking by a wide margin.

Summary

  • Depth pruning eliminates entire Transformer blocks from a model. While aggressive, this approach targets the real bottleneck in LLM inference: data movement between memory and compute units, not just computational operations.
  • The primary benefit of depth pruning is reducing memory-bound operations. Each removed block eliminates a complete round of loading data from VRAM, processing it, and writing results back, directly improving inference latency and throughput.
  • Static block selection strategies (position-based or magnitude-based) are simple and computationally free but ignore how the model actually processes your specific data. The common heuristic of preserving the first four and last two blocks provides some safety but remains a general rule.
  • PyTorch hooks allow you to intercept and capture the input and output activations of any module during inference without modifying the model's code. Use register_forward_hook() to attach a function that receives the module, input, and output tensors.
  • Block importance depends on the calibration data and task. In this small model, data-driven methods converge toward the final blocks, but the calibration data shapes the selection at the margins, enough to produce measurable differences in task performance and an effect that grows with model size.
  • Research papers "Shortened LLaMA" (Kim et al., 2024) and "ShortGPT" (Men et al., 2024) validate depth pruning as superior to width pruning for latency reduction and confirm that cosine-based importance metrics are effective across models up to 70B parameters.

FAQ

What is depth pruning in an LLM?Depth pruning is the process of removing entire Transformer blocks from a model. It is a structured pruning method that reduces model depth rather than individual weights or neurons.
Why is depth pruning useful for building smaller and faster language models?It can reduce latency and lower memory consumption. By removing full blocks, it also cuts down memory movement and KV-cache growth, which are major bottlenecks in inference.
What is the difference between static and data-driven block selection?Static selection uses architectural rules or weight statistics without looking at real data, while data-driven selection measures how blocks behave on task-specific inputs to estimate their importance.
Why do many pruning heuristics preserve the first and last Transformer blocks?The first blocks often build foundational representations, and the last blocks often perform task-specific refinements. Preserving them is a common heuristic because they are frequently more important than middle blocks.
How do you select blocks based on weight magnitude?You compute an aggregate magnitude score for each block, usually by summing parameter norms, then remove the blocks with the smallest scores. This assumes smaller weights imply less contribution.
What are the limitations of magnitude-based or position-based pruning?They ignore the actual data and task. A block with small weights can still be crucial for a specific domain, and positional assumptions such as “middle blocks are redundant” do not always hold.
How do PyTorch hooks help analyze block importance?Hooks let you capture the input and output activations of each block during a forward pass without changing the model code. This makes it possible to compare how much each block transforms the information.
Why is cosine similarity used to measure block contribution?Cosine similarity compares the direction of vectors rather than their magnitude. If a block barely changes the representation, its input and output vectors will be very similar, giving a score close to 1.
How is block importance computed from cosine similarity?The chapter defines importance as 1 minus the mean cosine similarity between a block’s input and output activations. Higher transformation means lower similarity and therefore higher importance.
What did the WinoGrande experiments show about data-driven pruning?The analysis showed that important blocks depend on the calibration data. On WinoGrande, the last blocks were often the least important, while protected data-driven pruning produced better overall retention than simple static rules in many cases.

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