Overview

2 An end-to-end rearchitecting project

This chapter presents a practical end-to-end model rearchitecting exercise: start with a working language model, shrink it without losing too much capability, and then recover much of the lost performance through distillation. The process is framed as a hands-on workflow rather than a theoretical deep dive, using a compact Gemma model that can run on modest Colab hardware. The chapter emphasizes that model quality is judged along two axes at once: capability on standard benchmarks and inference efficiency such as speed and latency.

The first part of the workflow establishes a clear baseline. After loading the model, checking its structure, and confirming it can generate coherent text, the chapter measures performance with several common benchmarks and a simple inference-time test. Those baseline measurements provide the reference point for all later comparisons. With that foundation in place, the model is then structurally modified by pruning off the last two transformer layers, reducing depth and making the model lighter and faster while keeping the original model intact for later use.

After pruning, the chapter evaluates the impact and shows the expected trade-off: faster inference and fewer parameters, but noticeable drops in benchmark scores and some factual degradation in free-form generation. The final stage uses knowledge distillation to train the smaller student model to imitate the original teacher’s output distributions on a subset of general text data, recovering much of the lost ability without changing the architecture. The result is a smaller model that retains most of the original capabilities while preserving the speed gains, illustrating the central idea that efficient systems can be created by redesigning models intelligently rather than simply making them larger.

The rearchitecting cycle followed in this chapter. We start with an original model, make it smaller through pruning, and finally recover its capabilities through knowledge distillation.
The rearchitecting cycle followed in this chapter. We start with an original model, make it smaller through pruning, and finally recover its capabilities through knowledge distillation.
The original gemma-3-270m model is reduced from 18 to 16 layers. The capabilities results shown are achieved via the knowledge recovery phase.
The original gemma-3-270m model is reduced from 18 to 16 layers. The capabilities results shown are achieved via the knowledge recovery phase.
In the knowledge distillation cycle, the Teacher model generates its reasoning (logits) for a given text. The Pruned model (our student) does the same. The KL Divergence loss function measures the difference between both "reasonings," and this error is used to adjust the student's weights, making it think more like the Teacher over time.
In the knowledge distillation cycle, the Teacher model generates its reasoning (logits) for a given text. The Pruned model (our student) does the same. The KL Divergence loss function measures the difference between both "reasonings," and this error is used to adjust the student's weights, making it think more like the Teacher over time.

Summary

  • Establishing a quantitative baseline is an indispensable first step in any optimization project because it provides an objective metric against which the success or failure of interventions is measured.
  • Depth pruning removes entire Transformer layers to reduce model size and speed up inference, typically targeting the final layers that hold refinement details rather than core knowledge.
  • Knowledge distillation lets smaller “student” models recover lost capabilities by learning to mimic the reasoning patterns of larger “teacher” models.
  • Combining structural pruning and knowledge distillation lets you build more efficient models that retain most of the capabilities of their larger, original versions.

FAQ

What is the main goal of the end-to-end rearchitecting project in this chapter?

The goal is to reduce the size of a base model while preserving as much of its capability as possible. The chapter does this by first pruning the model structure to make it smaller and faster, then recovering lost knowledge through distillation.

What two phases of the rearchitecting pipeline are used in this chapter?

The chapter uses two phases: Structural Optimization and Knowledge Recovery. Structural Optimization modifies the model architecture to make it lighter and faster, and Knowledge Recovery transfers knowledge from the original model to the pruned model.

Why is gemma-3-270m chosen for the hands-on example?

It is compact enough to run on a T4 GPU in Google Colab, yet still advanced enough to demonstrate real optimization techniques. It also reflects current architectural trends and is practical for hands-on experimentation.

What is meant by “baseline” in this chapter?

The baseline is the starting performance of the original model before any modification. It includes capability benchmarks and inference efficiency measurements so later changes can be compared against a clear reference point.

Which benchmarks are used to measure model capabilities?

The chapter uses four benchmarks: ARC-Easy, Winogrande, HellaSwag, and LAMBADA. Together, they measure reasoning, common sense, and language understanding across different types of tasks.

What is depth pruning?

Depth pruning is a structural optimization technique that removes entire layers from a model. In this chapter, the last two transformer blocks are removed to reduce depth and improve inference speed.

Why does the chapter remove the last two layers instead of other layers?

The chapter uses the last two layers as a simple, direct starting point for learning the process. This is a practical choice for demonstration, though the book notes that it is not necessarily the optimal pruning strategy.

Why must the pruned layers be wrapped in nn.ModuleList?

Wrapping the layers in nn.ModuleList ensures PyTorch properly registers them as submodules. This is necessary so the model’s parameters are tracked correctly and can be moved, trained, and managed properly.

What is knowledge distillation in this chapter’s workflow?

Knowledge distillation is the process of teaching the smaller student model to imitate the teacher model’s output distributions. The student learns not just the final answer, but the teacher’s confidence across possible answers, using KL divergence as the loss.

What is the final result of pruning and knowledge recovery?

The final model is smaller and faster than the original while recovering most of its performance. In the chapter’s example, the student remains 4.16% smaller, runs about 10% faster, and regains most of the original model’s benchmark capability.

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