Overview

8 Distilling reasoning models for efficient reasoning

Distillation is presented as a practical training-time route to better reasoning performance: instead of relying only on inference-time scaling or reinforcement learning, a smaller student model learns from reasoning traces and final answers produced by a stronger teacher. The chapter contrasts hard distillation, where the student imitates teacher-generated text, with soft distillation, where it matches the teacher’s probability distribution; while soft distillation can carry richer information, hard distillation is far more practical for most LLM workflows because it only needs generated text and not logits.

The implementation focuses on a DeepSeek-R1-style hard distillation pipeline built around math problems. A teacher model generates reasoning traces and answers for a 12,000-example MATH subset, the data is formatted with optional thinking tags, tokenized with a reasoning-aware tokenizer, filtered for excessive length, and split into training and validation sets. The chapter emphasizes careful preprocessing because distillation reuses the same examples across epochs, making it worthwhile to cache the formatted sequences and keep only answer-token positions for the loss.

Training uses a pre-trained Qwen3 0.6B base model and a standard cross-entropy objective applied only to the teacher answer tokens, not the prompt. A small training run demonstrates that validation loss falls as the student learns the teacher’s outputs, and larger runs show meaningful gains on MATH-500, especially when teacher and student share the same model family and formatting conventions. The chapter closes by placing distillation in the broader reasoning-model landscape, where it remains a key method for transferring strong reasoning behavior into smaller, cheaper models that are easier to deploy locally or inside agentic systems.

A mental model of the topics covered in this book. This chapter focuses on distillation, where a smaller student model is trained on reasoning traces generated by a larger teacher model.
Hard distillation trains the student on teacher-generated tokens, soft distillation trains the student on the teacher's full output distribution.
The chapter has four main steps: (1) distillation introduction and overview; (2) dataset preparation (steps 2a-2c); (3) training via distillation (steps 3a-3c); and (4) evaluation.
Distillation setup used in this chapter. We use the 12,000 non-overlapping MATH training problems to obtain synthetic solutions from DeepSeek-R1 and later evaluate the distilled Qwen3 student on the separate MATH-500 test set.
In RLVR, the generated answer is compared against the ground-truth reference solution (top subpanel), whereas in distillation the student answer is compared against the teacher-generated solution (bottom subpanel).
Chapter overview with the current section highlighted. Here, we load the DeepSeek-R1-generated dataset from a JSON file before preparing it for training.
Bringing the loaded dataset into a format suitable for model training by understanding the tokenizer, tokenizing the examples, and filtering and splitting the dataset.
With the tokenizer step complete, we now move on to apply the formatting and tokenization steps to the whole dataset.
Example of the tokenization pipeline for one training sample. The math problem is rendered into the chat prompt format, the teacher reasoning trace and final answer are combined via format_distilled_answer, and both parts are concatenated into one token sequence.
After tokenization, we filter out long sequences and split the remaining examples into training and validation subsets.
With the dataset preparation complete, we begin the distillation training by loading the pre-trained Qwen3 base model.
Illustration of token and sequence log-probabilities. The log-probabilities of the correct next tokens are summed to obtain the sequence log-probability, which is the basis for the cross-entropy loss used later in this chapter.
Input for the cross-entropy loss over the answer tokens. The model receives the prompt and answer shifted by one token as input, and the answer-token logits are compared against the reference answer tokens.
With dataset preparation and loss computation complete, we now turn to the training loop for distillation.
Distillation training loop. In each epoch, the training examples are shuffled, the student model computes a cross-entropy loss for each example, gradients are backpropagated, and the model weights are updated. The validation loss is reported in certain intervals to track progress.
After implementing the training loop, we evaluate the distilled model on the MATH-500 test set.
The training and validation loss of a 3-epoch distillation training run on DeepSeek-R1 reasoning traces.
The evaluation of the distilled model completes the technical content of this chapter.

Summary

  • Distillation trains a smaller student LLM on outputs produced by a larger teacher LLM.
  • Hard distillation is usually more practical than soft distillation because teacher logits are often unavailable and teacher text outputs are much cheaper to store and reuse.
  • We used DeepSeek-R1 as the teacher model and Qwen3 0.6B as the student model
  • The distillation dataset was built from the 12,000 MATH training problems that do not overlap with MATH-500.
  • Each training sample combines a rendered prompt with the teacher reasoning trace and final answer, optionally separated via <think>...</think> tags.
  • For efficiency, we tokenize the dataset once, filter it by sequence length, and reuse the processed examples across multiple epochs.
  • The training objective is answer-only cross-entropy, which is equivalent to the negative average log-probability of the correct next tokens.
  • The distillation training loop is a standard supervised learning loop, which includes shuffling the training examples each epoch, computing the loss, backpropagating, updating the model weights, and tracking validation loss.
  • The validation loss is the main signal to watch during training, and saved checkpoints can later be evaluated on MATH-500 with the verifier from chapter 3.
  • The distillation approach in this chapter improves the Qwen3 0.6B base model from 15.2% accuracy on MATH-500 to 45.0% accuracy.

FAQ

What is model distillation in the context of reasoning models?Model distillation is a training-time method where a smaller student model learns from outputs produced by a larger teacher model. For reasoning tasks, the teacher typically provides both the intermediate reasoning trace and the final answer, which the student is trained to reproduce.
Why is distillation useful for reasoning models?Distillation is useful because strong reasoning models are often too large and expensive to run directly. By transferring their reasoning behavior into a smaller model, we can get much cheaper and more practical inference while retaining a useful portion of the teacher’s reasoning ability.
What is the difference between hard distillation and soft distillation?In hard distillation, the student is trained on the teacher’s generated text tokens as targets. In soft distillation, the student tries to match the teacher’s full probability distribution over the vocabulary, usually by minimizing KL divergence. Hard distillation is more common for LLMs because it only requires teacher text, not logits.
Why is hard distillation more practical for LLMs than soft distillation?Hard distillation is easier to use because many teacher models expose generated text but not logits or log-probabilities. It is also simpler and cheaper to store and train on plain text than on full token distributions, especially for long reasoning traces.
What dataset is used to train the distilled reasoning student in this chapter?The chapter uses 12,000 math problems from the MATH split that do not overlap with the MATH-500 evaluation set. These problems are sent to DeepSeek-R1 to generate teacher responses, which become the synthetic supervision for training the student model.
What information does each teacher-generated training example contain?Each example includes the original math problem, the ground-truth answer, the teacher’s reasoning trace in message_thinking, and the teacher’s final answer in message_content. The reasoning trace and final answer are combined into the distillation target.
Why are <think>...</think> tags used in the distillation targets?The <think> tags are optional, but they help clearly separate the reasoning trace from the final answer. This makes the output easier to parse and can be useful when building interfaces that want to hide or display the reasoning separately from the final response.
How are training examples prepared for distillation?The prompt is rendered into a chat-style format, the teacher answer is formatted with the reasoning trace and final answer, and both parts are tokenized and concatenated into one sequence. The prompt length is stored so the loss can later be computed only on the answer tokens.
Why is the loss computed only on the answer tokens and not on the prompt tokens?The prompt is already given as input, so the model should not be penalized for reproducing it. During distillation, the goal is to teach the student to generate the teacher’s reasoning and answer conditioned on the prompt, so only the answer portion contributes to the cross-entropy loss.
How is the distilled model evaluated after training?The distilled checkpoint is evaluated on the MATH-500 test set using the same reasoning tokenizer setup. The chapter tracks validation loss during training and then measures task accuracy on MATH-500 to see how well the student learned the teacher’s reasoning behavior.

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
  • Build a Reasoning Model (From Scratch) 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
  • Build a Reasoning Model (From Scratch) 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
  • Build a Reasoning Model (From Scratch) ebook for free