Skip to content
ImagineWorld

Distilling reasoning models for efficient reasoning

node_idnode-book_build-a-reasoning-model_chapter-08-distilling-reasoning-models-for-efficient-reasoning
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num8
chapter_titleDistilling reasoning models for efficient reasoning
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 8 — Distilling reasoning models for efficient reasoning

Section titled “Chapter 8 — Distilling reasoning models for efficient reasoning”

Distilling reasoning models for efficient reasoning

This chapter covers

  • Hard and soft distillation for reasoning models
  • Creating and preparing a teacher-generated reasoning dataset
  • Training and evaluating a distilled student model using cross-entropy loss

Reasoning performance can be improved not only through inference-time scaling and reinforcement learning, but also through distillation. In distillation, a smaller student model is trained on the reasoning traces and answers generated by a larger teacher model. As shown in the overview in figure 8.1, this chapter focuses on this training-time technique.

We’ll look at model distillation in general before discussing the individual steps in the process.

graph LR
    subgraph G1 ["1 - Conventional LLM"]
        direction TB
        A1["Loading pretrained weights"]
        A2["Text generation"]
    end
    subgraph G2 ["2 - Evaluation"]
        direction TB
        B1["Benchmark-based evaluation"]
        B2["Judgment-based evaluation"]
    end
    subgraph G3 ["3 - Inference techniques"]
        direction TB
        C1["Advanced text generation and voting"]
        C2["Self-refinement"]
    end
    subgraph G4 ["4 - Training techniques"]
        direction TB
        D1["Reinforcement learning"]
        D2["Distillation"]
    end
    G1 --> G2 --> G3 --> G4

[!visual-conversion] Figure 8.1 — A model of the topics covered in this book (printed p. 268, scan page-280.png). This chapter focuses on distillation, where a smaller student model is trained on reasoning traces generated by a larger teacher model. Stage 4 is highlighted, annotated “This chapter focuses on distillation, where we train an LLM on the reasoning traces of a larger model.” Print stage headings: Stage 1: LLM basics (chapter 2) · Stage 2: Measuring performance (chapter 3) · Stage 3: Reasoning without training (chapters 4 and 5) · Stage 4: Reasoning with training (chapters 6 to 8).

1 Introducing model distillation for reasoning tasks

Model distillation involves training a smaller LLM, the student, on outputs produced by a larger LLM, the teacher. For reasoning models, these outputs usually include not only the final answer but also the intermediate reasoning trace that leads to it.

Distillation is important because the strongest reasoning models are often too large and expensive to work with directly. For example, DeepSeek-R1 has 671 billion parameters. Systems at that scale are expensive to develop and deploy, and they are far beyond what most practitioners can run on local hardware. The team behind DeepSeek-R1 created smaller model variants by distilling the 671-billion-parameter teacher model. We will follow the same basic idea in this chapter, but at a scale that is practical for this book.

Throughout this book, we have deliberately worked with small models rather than training a very large LLM from scratch, but the workflow in this chapter is the same as for larger models. We’ll use a stronger teacher to generate reasoning traces and then train a smaller student to reproduce them. In our setup, this distillation stage will take only a fraction of the time as for larger models. Our distillation training run will take about 3 hours on a DGX Spark and use about 15 GB of RAM, whereas even a few GRPO rounds in chapter 6 took around 12 hours and 70 GB of RAM on the same hardware.

Distillation can also be more effective than training a small model using reinforcement learning with verifiable rewards (RLVR). For example, the DeepSeek-R1 paper reported that its smaller distilled variants outperformed comparable models trained with reinforcement learning alone. In that setup, the largest DeepSeek-R1 model, with 671 billion parameters, acted as the teacher and generated the supervision used to train smaller student models.

There are two main types of distillation: hard distillation and soft distillation. In hard distillation, the student is trained on text generated by the teacher, so the teacher’s

tokens are treated as the targets. In soft distillation, the student is trained to match the teacher’s probability distribution over the vocabulary by minimizing the KL divergence, a measure of how different two probability distributions are. These two approaches are illustrated in figure 8.2.

“Multiply both sides by 2: 3x - 9 = 2x + 74 Subtract 2x from both sides…”

Hard distillation trains a smaller student LLM on the tokens generated by the teacher LLM.

[151644, 872, 198, 95155, 2176, 11067, 553…]

Teacher LLM

[42627, 279, 897, 315, 220, …]

“Half the value of 3x - 9 is x + 37. What is the value of x? Write the final result in \boxed{}”

graph LR
    P["Prompt:<br/>'Half the value of 3x − 9 is x + 37.<br/>What is the value of x? Write the<br/>final result in \\boxed{}'"] --> T["Teacher LLM"]
    T --> H["Hard distillation:<br/>token IDs [151644, 872, 198, 95155, 2176, 11067, 553...]<br/>→ 'Multiply both sides by 2:<br/>3x − 9 = 2x + 74<br/>Subtract 2x from both sides...'"]
    T --> S["Soft distillation:<br/>full logit / probability distribution<br/>over the whole vocabulary at each token"]

[!visual-conversion] Figure 8.2 — Hard distillation trains the student on teacher-generated tokens; soft distillation trains the student on the teacher’s full output distribution (printed p. 269, scan page-281.png). Print annotations: “Hard distillation trains a smaller student LLM on the tokens generated by the teacher LLM” and “Soft distillation trains the smaller student LLM on the logit values (or probability distribution) over the whole vocabulary at each token generation step.” The right-hand panel is a bar chart of logit value against vocabulary index across the full 151,666-token vocabulary; individual bar heights are not recoverable at scan resolution.

As illustrated in figure 8.2, one option is pure hard distillation. Here, we use only the teacher-generated text as the training target. If you’re familiar with the typical LLM training pipeline, which is covered in more detail in my previous book, Build A Large Language Model (From Scratch), hard distillation is just supervised fine-tuning on synthetic data. This is also the setup used for the smaller DeepSeek-R1 distilled models. A large teacher model generates reasoning traces and answers, and the student model is fine-tuned to reproduce them. According to the DeepSeek-R1 paper, for small models this distillation approach can result in higher accuracy than training with reinforcement learning. The main practical advantage of hard distillation is that we only need access to the teacher’s generated text, not its logits.

The second option is pure soft distillation. Instead of matching only the teacher’s chosen tokens, the student is trained to match the teacher’s full output distribution of each token over the whole vocabulary. This gives the student richer information about which alternative tokens the teacher considered plausible, but it requires access to teacher logits or log probabilities at training time.

A third option combines hard and soft distillation. This is the classic knowledge-distillation setup popularized earlier in computer vision, such as in the paper “Distilling the Knowledge in a Neural Network,” by Geoffrey Hinton, Oriol Vinyals, and Jeff Dean. In this case, we train on the teacher’s actual output tokens while also encouraging the student to match the teacher’s full distribution.

In practice, hard distillation is much more common for LLMs, partly because full teacher logits are usually inaccessible. Proprietary systems such as ChatGPT or Claude may expose generated text, but they generally do not expose the full vocabulary distribution needed for classical soft distillation.

WARNING Reusing generated text for distillation may be restricted by provider-specific usage policies, including the OpenAI and Anthropic terms of service, so review those policies carefully before using such outputs for training.

Even when logits are available, soft distillation is more cumbersome than hard distillation. The student and teacher usually require the same tokenizers so that their vocabulary distributions line up, so this approach is easier within the same model family. It is also much more expensive to store and use full token distributions for long reasoning traces. By comparison, storing plain text outputs is cheap and simple.

In this chapter, we’ll focus on hard distillation in the style of DeepSeek-R1 because it is the more practical setup for most readers. The steps we’ll discuss in this chapter are summarized in figure 8.3.

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.3 — This chapter covers distillation in four main steps: (1) distillation introduction and overview; (2) dataset preparation (steps 2a–2c); (3) training via distillation (steps 3a–3c); and (4) evaluation (printed p. 270, scan page-282.png). Step 1 is ticked and step 2 is highlighted. Print annotations: “In this section, we discussed the broad ideas and concepts behind distillation” and “Next, we will prepare the dataset for distillation.”

With this overview in place, we can now move from the high-level ideas behind distillation to the practical steps required to implement it. In the next section, we’ll begin by preparing a dataset for distillation, which will serve as the foundation for training the student model.

8.2 Generating a dataset for reasoning distillation

Section titled “8.2 Generating a dataset for reasoning distillation”

The first practical step in hard distillation is to create a dataset for training the student model. For us, the student will be the Qwen3 0.6B base model that we used in earlier chapters.

To build the dataset, we’ll use the 12,000 math problems from the MATH split that do not overlap with the MATH-500 evaluation set. These are the same 12,000 problems we used in chapters 6 and 7 for RLVR. Instead of sampling multiple student responses and computing rewards, we’ll now feed these problems to an existing reasoning model, DeepSeek-R1 (the teacher model), and collect its responses as training targets. This setup is illustrated in figure 8.4.

graph TD
    M["Mathematics Aptitude Test of Heuristics (MATH) dataset<br/>12,500 math problems"] -->|Training| TR["12,000 math problems"]
    M -->|Testing| TE["500 problems and solutions (MATH-500)"]
    TR --> D["DeepSeek-R1 (Teacher LLM)"]
    D -->|Generate solutions with a teacher LLM| SY["12,000 synthetic solutions"]
    SY --> DI["Distillation"]
    Q["Qwen3 0.6B (Student LLM)"] --> DI
    DI --> QD["Qwen3 0.6B (Student LLM)"]
    QD --> EV["Evaluation"]
    TE --> EV

[!visual-conversion] Figure 8.4 — The distillation setup used in this chapter (printed p. 271, scan page-283.png). We’ll use the 12,000 non-overlapping MATH training problems to obtain synthetic solutions from DeepSeek-R1, and we’ll later evaluate the distilled Qwen3 student on the separate MATH-500 test set. Print annotations: “Use the 12,000 math problems introduced in chapter 6”, “This section is focused on generating the 12,000 solutions”, “Train the student LLM on math problems and solutions generated by the teacher LLM”, and “Evaluate the distilled student LLM on a separate math dataset.”

In the RLVR setup in chapters 6 and 7, we trained the Qwen3 base model to produce the correct solution, and we then used a verifier to compare the model’s final answer with the reference answer. The verifier produced the reward signal.

In distillation, the supervision is more direct. Instead of comparing the student’s answer with the reference solution using a reward function, we’ll compare the student’s generated tokens with the teacher’s generated tokens, as shown in figure 8.4. The teacher’s response becomes the target sequence. This contrast with RLVR is illustrated in more detail in figure 8.5.

Chapters 6 and 7 (RLVR)

graph LR
    D1["2,000 math problems"] --> Q["Qwen3 0.6B"]
    Q --> G["Generated solution:<br/>'The degree of a polynomial is the largest exponent... \\boxed{4}'"]
    R["Ground-truth reference solution"] --> C1{"Compare"}
    G --> C1

This chapter (distillation)

graph LR
    D2["12,000 math problems"] --> T["DeepSeek-R1 (Teacher LLM)"]
    D2 --> S["Qwen3 0.6B (Student LLM)"]
    T --> TT["Teacher solution:<br/>'Okay, so I need to find the center of the circle given... \\boxed{4}'"]
    S --> ST["Student solution:<br/>'Okay, so to solve this I need to calculate the center...'"]
    TT --> C2{"Compare"}
    ST --> C2

[!visual-conversion] Figure 8.5 — 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) (printed p. 272, scan page-284.png). Print annotations: “In RLVR, we compare the generated solution to the ground-truth reference solution to compute the training signal” and “In distillation, we compare the teacher’s solution to the student’s solution to compute the training signal.”

A practical advantage of distillation is that we can generate the teacher dataset ahead of training the student.

Because generating teacher answers for all 12,000 math problems can be time and resource intensive, I created the dataset using the 671-billion-parameter DeepSeek-R1 model hosted via OpenRouter. The full data generation cost was approximately $50

in API usage. We’ll load this pregenerated dataset, so you do not need to generate it yourself to follow along. If you are curious about the data-generation process, the code and usage instructions are available in the supplementary materials at https://mng.bz/vZvx.

8.3 Loading the MATH training dataset for distillation

Section titled “8.3 Loading the MATH training dataset for distillation”

We’ll now load the distilled MATH dataset generated by DeepSeek-R1 in the previous section. Each example contains a math problem together with a reasoning trace and final answer, which we can use as targets for supervised fine-tuning. This step is highlighted in figure 8.6.

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.6 — In this section, we’ll load the DeepSeek-R1-generated dataset from a JSON file before preparing it for training (printed p. 273, scan page-285.png). Step 1 and step 2a are ticked and step 2b is highlighted, annotated “We’ll now download and load the dataset generated by DeepSeek-R1.”

I made the dataset available via the Hugging Face Hub rather than GitHub because the JSON file is approximately 107 MB, which exceeds GitHub’s 100 MB file-size limit: https://huggingface.co/datasets/rasbt/math_distill. The following helper function downloads the selected partition if it is not already cached locally and returns it as a Python object.

Listing 8.1 Loading the distilled MATH training split

import json import requests
from pathlib import Path
def load_distill_data(
local_path=None,
partition="deepseek-r1-math-train",
save_copy=True,
):
if local_path is None:
local_path = f"{partition}.json"
local_path = Path(local_path)
url = (
"https://huggingface.co/datasets/rasbt/math_distill"
"/resolve/main/data/"
f"{partition}.json"
)
backup_url = (
"https://f001.backblaze2.com/file/reasoning-from-scratch/"
f"MATH/{partition}.json"
)
if local_path.exists():
with local_path.open("r", encoding="utf-8") as f:
data = json.load(f)
size_kb = local_path.stat().st_size / 1e3
print(f"{local_path}: {size_kb:.1f} KB (cached)")
return data
assert partition in (
"deepseek-r1-math-train",
"deepseek-r1-math500",
"qwen3-235b-a22b-math-train",
"qwen3-235b-a22b-math500",
)
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
except requests.RequestException:
print("Using backup URL.")
r = requests.get(backup_url, timeout=30)
r.raise_for_status()
data = r.json()
if save_copy:
with local_path.open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
size_kb = local_path.stat().st_size / 1e3
print(f"{local_path}: {size_kb:.1f} KB")
return data

Reuse a cached copy if the dataset was already downloaded.

Try downloading from Hugging Face first.

Save a local copy so later runs can skip the download.

The output is as follows:

deepseek-r1-math-train.json: 107538.0 KB Dataset size: 12000

Let’s inspect one of the training examples so we can understand the dataset structure and see exactly which fields the teacher-generated data contains. For illustration, we’ll use the fifth training example:

from pprint import pprint pprint(math_train[4])

The printed output is as follows:

{‘gtruth_answer’: ‘6’, ‘message_content’: ‘Sam worked \( x \) days and did…’, ‘message_thinking’: “Okay, let’s see. Sam was hired for 20 days…”, ‘problem’: ‘Sam is hired for a 20-day period…’}

Each dataset entry contains the math problem itself (problem), the ground-truth answer (gtruth_answer), the teacher’s reasoning trace in message_thinking, and the final answer in message_content. For distillation, the two most important fields are the reasoning trace and the final answer, because together they form the target text that the student should learn to reproduce.

The format_distilled_answer function in the following listing combines these two fields into a single training target by placing the reasoning trace inside <think>… </think> tags and then appending the final answer.

Listing 8.2 Formatting teacher responses for distillation

def format_distilled_answer(entry):
content = str(entry["message_content"]).strip()
if not content:
raise ValueError("Missing non-empty 'message_content' field.")
thinking = str(entry["message_thinking"]).strip()
return f"<think>{thinking}</think>\n\n{content}"
print(format_distilled_answer(math_train[4]))

This is the printed output:

<think>Okay, let’s see. Sam was hired for 20 days. Each day he works, he earns $60…So answer is 6 days not worked.</think> Sam worked \( x \) days and did not work \( y \) days. We know:… Sam did not work (\boxed{6}) days.

As discussed in the previous chapter, the <think>``</think> tokens are optional. They are not required for distillation itself, though they can be useful for clearly separating the reasoning trace from the final answer. This separation becomes helpful when implementing user interfaces that hide the verbose reasoning trace from end users. Some systems, including products such as ChatGPT, may display only the final answer while hiding portions of the internal reasoning. Teaching the model to use explicit <think> tags makes these traces easier to parse and handle.

Since the dataset also contains ground-truth labels, we can measure how accurate the teacher model is on this set. For convenience, we’ll use the evaluate_json.py script from chapter 3’s supplementary materials, which compares generated answers with the reference answers using the verifier implemented in that chapter. This will give us a quick estimate of DeepSeek-R1’s performance on the distillation dataset:

from reasoning_from_scratch.ch07 import download_from_github
_ = download_from_github(
"ch03/02_math500-verifier-scripts/evaluate_json.py"
)

After downloading the script via the preceding Python code, we can run it in a code terminal as follows:

Terminal window
uv run evaluate_json.py \
--json_path "deepseek-r1-math-train.json" \
--gtruth_answer gtruth_answer \
--generated_text message_content

(If you don’t use uv, replace uv run with python.)

This is the output:

Accuracy: 90.6% (10871/12000)

While the model is not perfect, 90.6% is still a relatively high level of accuracy. On the MATH-500 test set from chapter 3, it achieved 91.2% accuracy, which is much higher than the Qwen3 0.6B base model we are going to train (15.2% accuracy on MATH-500) and the official Qwen3 0.6B reasoning reference model (50.8% on MATH-500).

Next, we’ll convert the raw dataset entries into training examples that can be consumed by the model. At a high level, this means formatting the prompts and answers consistently, tokenizing them, and storing the resulting token IDs together with the prompt length. This preprocessing stage is shown in figure 8.7.

Most of the work here is straightforward preprocessing. In the RLVR chapters, we performed similar preparation on the fly because each training example was sampled once and then discarded. Distillation is different, and we usually loop over the

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.7 — Bringing the loaded dataset into a format suitable for model training by understanding the tokenizer, tokenizing the examples, and filtering and splitting the dataset (printed p. 277, scan page-289.png). Steps 1, 2a and 2b are ticked and step 2c is highlighted, annotated “Previously, we loaded the DeepSeek-R1 answers from a JSON file” and “Now, we’ll bring the dataset into a format suitable for model training, which we will do in three stages.” Step 2c expands into three stages:

graph LR
    T1["2c stage 1 - Load and understand tokenizer"] --> T2["2c stage 2 - Tokenize dataset"]
    T2 --> T3["2c stage 3 - Filter and split dataset"]

same examples for multiple training epochs. It is therefore more efficient to format and tokenize the dataset once, store the processed examples, and reuse them during training. This is also one reason distillation is often easier to iterate on than RLVR, once the teacher data has been collected.

What are training epochs? A training epoch, or epoch for short, is a standard machine learning and deep learning term. An epoch is one complete pass through the full training dataset. For example, if we train for three epochs, the model sees each training example three times, usually in a different order each time. Multiple epochs help the model gradually improve by revisiting the same data more than once.

8.4.1 Loading and understanding the tokenizer

Section titled “8.4.1 Loading and understanding the tokenizer”

We’ll begin with the tokenizer. Here, we are using the Qwen3 reasoning tokenizer because it supports the <think>...</think> tokens introduced in chapter 7.

Listing 8.3 Loading the Qwen3 reasoning tokenizer

from reasoning_from_scratch.qwen3 import (
download_qwen3_small,
Qwen3Tokenizer,
)
def load_reasoning_tokenizer(local_dir="qwen3"):
download_qwen3_small(
kind="reasoning", tokenizer_only=True, out_dir=local_dir
)
tokenizer_path = Path(local_dir) / "tokenizer-reasoning.json"
tokenizer = Qwen3Tokenizer(
tokenizer_file_path=tokenizer_path,
apply_chat_template=True,
add_generation_prompt=True,
add_thinking=True,
)
return tokenizer
tokenizer = load_reasoning_tokenizer()

We set apply_chat_template=True and add_generation_prompt=True so that the tokenizer applies the same prompt-formatting style used for Qwen3’s chat and reasoning models. The following example shows the additional wrapper tokens that are inserted automatically.

prompt = "Sam is hired for a 20-day period..."
prompt_ids = tokenizer.encode(prompt)
decoded_prompt = tokenizer.decode(prompt_ids)
print(decoded_prompt)

The formatted text is as follows:

<|im_start|>user Sam is hired for a 20-day period…<|im_end|> <|im_start|>assistant

The <|im_start|>user marks the start of the user prompt, <|im_end|> marks the end of the prompt, and <|im_start|>assistant marks the start of the model response. This chat-style wrapping is optional, but it is a common convention for instruction and chat fine-tuning.

For the target answer, we’ll disable this wrapping by setting chat_wrapped=False. Otherwise, both the prompt and the answer would introduce their own assistant-start tokens, which is not what we want when concatenating them into a single training sequence:

answer = (
"`<think>`Okay, let me try to solve "
"this problem...<\/think> \boxed{4}"
)
answer_ids = tokenizer.encode(answer, chat_wrapped=False)
decoded_answer = tokenizer.decode(answer_ids)
print(decoded_answer)

As shown, the chat_wrapped=False setting suppressed the chat template for the answer tokens:

<think>Okay, let me try to solve this problem…</think> \boxed{4}

For training, we need the full sequence of token IDs: the wrapped prompt, followed by the teacher answer, followed by an end-of-sequence token. This is the sequence the model will see during next-token prediction:

token_ids = prompt_ids + answer_ids + [tokenizer.eos_token_id]
decoded_token_ids = tokenizer.decode(token_ids)
print(decoded_token_ids)

The formatted string is as follows:

<|im_start|>user Sam is hired for a 20-day period…<|im_end|> <|im_start|>assistant <think>Okay, let me try to solve this problem…</think>\boxed{4}<|im_end|>

It is worth emphasizing that the reasoning tokenizer is mainly convenient because it already includes the <think>``</think> tokens. In principle, though, we could also use the base tokenizer. Likewise, the chat template is not strictly required. We will keep it here because it matches the formatting used by Qwen3’s own reasoning models and helps keep the training and evaluation setup consistent.

If you later evaluate the model with the scripts from earlier chapters, remember to use the —which_model “reasoning” setting so that the evaluation uses the same tokenizer variant.

8.4.2 Formatting and tokenizing the dataset

Section titled “8.4.2 Formatting and tokenizing the dataset”

Now that we’ve loaded the tokenizer, we can apply the formatting and tokenization steps to the whole dataset, as illustrated in figure 8.8.

With the tokenizer in place, we can now apply the formatting and tokenization steps consistently across the whole dataset via a build_examples function. The full formatting and tokenization pipeline for a single training sample is illustrated in figure 8.9.

Here, the build_examples function performs three steps. First, it renders and tokenizes the prompt. Second, it formats and tokenizes the teacher answer. Third, it concatenates both parts and records the prompt length so that we can later compute the loss only on the answer tokens. The following listing shows how we can implement that in code.

graph LR
    T1["2c stage 1 - Load and understand tokenizer"] --> T2["2c stage 2 - Tokenize dataset"]
    T2 --> T3["2c stage 3 - Filter and split dataset"]

[!visual-conversion] Figure 8.8 — With tokenization complete, we can now apply the formatting and tokenization steps to the whole dataset (printed p. 280, scan page-292.png). Stages 1 and 2 are ticked and stage 3 is highlighted, under the parent step “2c — Format dataset for training”. Print annotation: “Next, we’ll apply the prompt and answer formatting, including tokenization, to the whole dataset.”

graph TD
    D["12,000 math problems and solutions (via DeepSeek-R1)"] --> MP["Math problem statement:<br/>'You are a helpful math assistant...<br/>Sam is hired for a 20-day period...<br/>Answer:'"]
    D --> RT["DeepSeek-R1 reasoning trace:<br/>'Okay, let's see. Sam was hired for 20 days.'"]
    D --> AT["DeepSeek-R1 answer text:<br/>'Sam did not work \\boxed{6} days.'"]
    MP -->|"1 - render_prompt(...)"| P1["&lt;|im_start|&gt;user<br/>You are a helpful math assistant...<br/>&lt;|im_end|&gt;<br/>&lt;|im_start|&gt;assistant"]
    RT -->|"2 - format_distilled_answer(...)"| P2["&lt;think&gt;Okay, let's see. Sam was<br/>hired for 20 days.&lt;/think&gt;<br/>Sam did not work \\boxed{6} days."]
    AT --> P2
    P1 -->|"encode(...)"| E1["Tokenizer → [151644, 872, 198, ...]"]
    P2 -->|"encode(..., chat_wrapped=False)"| E2["Tokenizer → [151667, 32313, 11, ...]"]
    E1 --> C["3 - Concatenated token sequence:<br/>[151644, 872, 198, ... 151667, 32313, 11, ... eos_token_id]"]
    E2 --> C

[!visual-conversion] Figure 8.9 — Example of the tokenization pipeline for one training sample (printed p. 280, scan page-292.png). 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. Print annotations: “Example of the encoded math problem prompt and answer that we need for training”, “Example of the encoded math problem prompt”, and “Formats answer without adding chat tokens (<|im_start|>, etc.)”. All token IDs are transcribed from the figure.

Listing 8.4 Building and inspecting tokenized distillation examples

from reasoning_from_scratch.ch03 import render_prompt
def build_examples(data, tokenizer):
examples = []
skipped = 0
for entry in data:
try:
prompt = render_prompt(entry["problem"]) # Step 1: Render the problem in the chat format
prompt_ids = tokenizer.encode(prompt)
target_answer = format_distilled_answer(entry) # Step 2: Tokenize the teacher reasoning trace and final answer
answer_ids = tokenizer.encode(
target_answer, chat_wrapped=False
)
token_ids = (
prompt_ids + answer_ids + [tokenizer.eos_token_id]
) # Step 3: Combine prompt and answer for training
if len(token_ids) < 2:
skipped += 1
continue
examples.append({
"token_ids": token_ids,
"prompt_len": len(prompt_ids), # Store the prompt length so we can ignore prompt tokens in the loss later.
})
except (KeyError, TypeError, ValueError):
skipped += 1
return examples, skipped
examples, skipped = build_examples(math_train, tokenizer)
print("Number of examples:", len(examples))
print("Number of skipped examples:", skipped)

The resulting numbers after running the code in listing 8.4 are as follows:

Number of examples: 12000
Number of skipped examples: 0

Next, let’s decode one of the training examples to inspect it further:

print(tokenizer.decode(examples[4]["token_ids"]))

This is the output:

<im_start>user You are a helpful math assistant. Answer the question and write the final result on a new line as: \boxed{ANSWER}

Question: Sam is hired for a 20-day period…

Answer:<im_end> <im_start>assistant <think>Okay, let’s see. Sam was hired for 20 days… So answer is 6 days not worked.</think> …Sam did not work (\boxed{6}) days.<im_end>

Looking at the preceding output, we can confirm that it meets all the formatting requirements. For instance, it uses the chat template correctly, and the answer’s reasoning trace is correctly enclosed in <think>``</think> tags. Finally, the answer ends with an end-of-sequence token, <im_end>.

Once the examples are tokenized, we still need to filter long sequences and split the dataset into training and validation subsets. This step is highlighted in figure 8.10.

graph LR
    T1["2c stage 1 - Load and understand tokenizer"] --> T2["2c stage 2 - Tokenize dataset"]
    T2 --> T3["2c stage 3 - Filter and split dataset"]

[!visual-conversion] Figure 8.10 — After tokenization, we filter out long sequences and split the remaining examples into training and validation subsets (printed p. 282, scan page-294.png). Stages 1 and 2 are ticked and stage 3 is highlighted, under the parent step “2c — Format dataset for training”. Print annotation: “Now, we filter the dataset by length and split it into training and validation subsets.”

After tokenization, it’s useful to inspect the sequence lengths. This tells us how long the examples are on average, which samples are extreme outliers, and how aggressive our filtering needs to be. We can then remove examples above the chosen maximum length, shuffle the remaining data with a fixed random seed, and split off a small validation set.

Let’s begin by analyzing the sequence lengths.

Listing 8.5 Computing lengths and filtering long examples

def compute_length(examples, answer_only=False):
lengths = []
for ex in examples:
total = len(ex["token_ids"])
length = total - ex["prompt_len"] if answer_only else total
lengths.append(length)
avg_len = round(sum(lengths) / len(lengths))
shortest_len = min(lengths)
longest_len = max(lengths)
shortest_idx = lengths.index(shortest_len)
longest_idx = lengths.index(longest_len)
print(f"Average: {avg_len} tokens")
print(f"Shortest: {shortest_len} tokens (index {shortest_idx})")
print(f"Longest: {longest_len} tokens (index {longest_idx})")
compute_length(examples)

This is the resulting output:

Average: 2946 tokens Shortest: 236 tokens (index 10846) Longest: 42005 tokens (index 2529)

As you can see, the average response length is 2,946 tokens, which is typical for reasoning models. There are outliers, though. For instance, the longest answer is 42,005 tokens, which is excessive. The index positions (index 10846 and index 2529) denote the positions of the shortest and longest examples in the dataset, respectively, in case we want to inspect them.

To keep the computational costs reasonable for this distillation example, we’ll filter the dataset to include only entries of up to 2,048 tokens using the code in listing 8.6. In practice, controlling sequence length is one of the main steps that makes distillation feasible on smaller hardware.

Listing 8.6 Filtering long examples

def filter_examples_by_max_len(examples, max_len=2048):
filtered_examples = [
s for s in examples
if len(s["token_ids"]) <= max_len
]
print("Original:", len(examples))
print("Filtered:", len(filtered_examples))
print("Removed:", len(examples) - len(filtered_examples))
return filtered_examples
filtered_examples = filter_examples_by_max_len(examples, max_len=2048)

The filtering code in listing 8.6 removes 5,305 training examples:

Original: 12000 Filtered: 6695 Removed: 5305

Let’s compute the dataset lengths on this new subset:

compute_length(filtered_examples)

As you can see, the average token length is now down to 1,180 tokens, and none of the formatted training examples exceed 2,048 tokens:

Average: 1180 tokens Shortest: 236 tokens (index 5971) Longest: 2048 tokens (index 5587)

Lastly, we’ll split the dataset into training and validation sets, where the latter are used for quick evaluations throughout the training run.

Listing 8.7 Partitioning into training and validation sets

import random
rng = random.Random(123)
rng.shuffle(filtered_examples)
train_examples = filtered_examples[25:]
val_examples = filtered_examples[:25]
print("Number of train examples:", len(train_examples))
print("Number of validation examples:", len(val_examples))

The resulting numbers of training and validation examples are as follows:

Number of train examples: 6670 Number of validation examples: 25

We are keeping the validation set purposefully small so that we don’t unnecessarily slow down the training loop. In addition, after training is completed, we will use the 500 samples from the MATH-500 set to evaluate the model’s performance.

Exercise 8.1: Training and validation set lengths Apply the compute_length function to the new train_examples and val_examples partitions to check whether they are balanced by sample length.

With the dataset preparation complete, we can turn to the actual distillation training. We’ll begin by loading the pretrained Qwen3 base model, as shown in figure 8.11.

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.11 — With the dataset preparation complete, we begin the distillation training by loading the pretrained Qwen3 base model (printed p. 285, scan page-297.png). Steps 1, 2, 2a, 2b and 2c are ticked and step 3a is highlighted, annotated “With the dataset preparation completed, we’ll now focus on the distillation training, which begins with loading our pretrained base model.” Qwen3 base model.

We’ll start with the pretrained Qwen3 base model, not from an RL-trained model, because distillation itself is the training stage we want to study here. This keeps our setup clean and makes it easier to attribute any improvement to the distilled reasoning traces. It also mirrors a common practical setup in which a general base model is adapted using teacher-generated data.

Listing 8.8 Loading the Qwen3 base model for distillation import torch from reasoning_from_scratch.ch02 import get_device

from reasoning_from_scratch.ch03 import (
load_model_and_tokenizer,
)
device = get_device()
model, _ = load_model_and_tokenizer(
which_model="base",
device=device,
use_compile=False,
)

We can ignore the loaded base tokenizer in listing 8.8 because we’ll be using the tokenizer with <think>``</think> token support that we loaded in section 8.4.1.

8.6 Computing the training and validation losses

Section titled “8.6 Computing the training and validation losses”

Next, we implement the cross-entropy loss that will serve as the training signal during distillation when we implement the training loop later. We will also reuse the same computation on the validation set to monitor progress during training.

If you have a deep learning background, you may already know cross-entropy from classification tasks. The idea is the same here, except that the target class we want to predict at each position is the next token in the teacher-generated sequence.

We can connect the cross-entropy loss directly to the log-probability computations from chapters 5 and 6. As discussed there, log probability measures how much probability the model assigns to the correct next token. Higher log probability means the model is more confident in the correct token, and lower log probability means the opposite. Reusing our “The capital of Germany is Berlin” example from previous chapters, figure 8.12 recaps this.

Cross-entropy is simply the negative average of the token log probabilities shown in figure 8.12. For instance, if −16.6250 is the log probability, the negative log probability is 16.6250, and the negative average log probability (or cross-entropy) is 16.6250/5 = 3.325. Note that the 5 is there because the sequence has 5 target predictions whose log probabilities are being averaged: “capital”, “of”, “Germany”, “is”, and “Berlin”. Similar to log probability, the closer the cross-entropy value is to 0, the better, because the model is more confident in the correct target token.

The sequence_logprob function from chapter 6 performs the computation shown in figure 8.12, which makes it a useful starting point for understanding how the distillation loss works. In this case, we’ll use the training example at index position 5730 because it is the shortest example in train_examples, as we can determine via compute_length(train_examples), and therefore computes a bit more quickly than the other examples:

token_ids = train_examples[5730]["token_ids"] prompt_len = train_examples[5730]["prompt_len"]
graph BT
A["1 - Convert input text into token IDs and pass them to the LLM<br/>'The capital of Germany is' → 785, 6722, 315, 9856, 374"] --> L["LLM"]
L --> B["2 - Get scores for next token"]
B --> C["Conditional log probability of 'Berlin'<br/>given 'The capital of Germany is' = −1.7812"]
Text input to the LLMNext tokenLog probability
The capital—−9.6875
The capital ofof−0.7695
The capital of GermanyGermany−4.0938
The capital of Germany isis−0.3008
The capital of Germany is BerlinBerlin−1.7812
−9.6875+(−0.7695)+(−4.0938)+(−0.3008)+(−1.7812)=−16.6250-9.6875 + (-0.7695) + (-4.0938) + (-0.3008) + (-1.7812) = -16.6250

[!visual-conversion] Figure 8.12 — Illustration of token and sequence log probabilities (printed p. 287, scan page-299.png). 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. Print annotations: “Illustration showing how the value in this row was obtained (we are reusing the example from chapter 5)”, “Joint log probability of the sequence ‘The capital of Germany is Berlin’”, and “Conditional log probability of ‘Berlin’ given ‘The capital of Germany is’”. Values match figure 5.16. The accompanying full-vocabulary bar plot’s individual heights are not recoverable at scan resolution. Instead of reporting the summed log probabilities returned by sequence_logprob, we’ll average them over the number of answer tokens. Summed log probabilities grow with sequence length, so they are not directly comparable across examples with shorter or longer answers. By dividing by the number of answer tokens, we’ll obtain a per-token quantity, which matches the form used by cross-entropy loss. We can compute the number of answer tokens by subtracting prompt_len from the total sequence length.

Listing 8.9 Computing average negative log probabilities

from reasoning_from_scratch.ch06 import sequence_logprob
tok = torch.tensor(token_ids, dtype=torch.long, device=device)
with torch.no_grad():
seq_logprob = sequence_logprob(model, tok, prompt_len)
num_answer_tokens = tok.numel() - prompt_len
avg_logprob = -seq_logprob / num_answer_tokens
print(f"Average logprob: {avg_logprob:.2f}")

The resulting negative average log probability is 1.68.

We can now compute the same quantity using PyTorch’s cross_entropy function. Although the function is usually introduced for classification, it’s a natural choice here. For example, the model logits provide the predicted class distribution over the vocabulary, and the target sequence provides the correct class label at each position.

As in the previous average-logprob calculation, we’ll compute the loss only over the answer tokens and ignore the prompt tokens (the prompt is the context provided as input and not something we want the model to be penalized for reproducing). This is illustrated in figure 8.13.

1 Inputs to model (prompt and answer, minus the last token):
<|im_start|> Compute -49/7 ... The answer is \boxed{-7} <|im_end|>
|
2 shifted by one token
<|im_start|> Compute -49/7 ... The answer is \boxed{-7}
|
v
3 Model logits: [ ][ ][ ][ ][ ][ ] ... [ ][ ][ ][ ][ ][ ]
(the last logit tensor holds the
151,000 logits for <|im_end|>)
|
v cross-entropy
4 Reference answer: The answer is \boxed{-7} <|im_end|>

[!visual-conversion] Figure 8.13 — Input for the cross-entropy loss over the answer tokens (printed p. 288, scan page-300.png). The model receives the prompt and answer, shifted by one token, as input, and the answer token logits are compared with the reference answer tokens. Print annotations: “The model is given the prompt and answer, minus the last token”, “The last logit tensor contains the (151,000) logits corresponding to the last token, <|im_end|>”, “Pass the answer logits and reference answer token IDs to the cross-entropy function”, and “The reference answer is the answer portion from the first row (training example).” <im_start> Compute -49/7 … The answer is \boxed{-7} <im_end>

Inputs to model: <im_start> Compute -49/7 … The answer is \boxed{-7} <im_end>

Model logits: <im_start> Compute -49/7 … The answer is \boxed{-7} <im_end>

Reference answer: The answer is \boxed{-7} <im_end>

The model is given the prompt and answer, minus the last token.

The last logit tensor contains the (151,000) logits corresponding to the last token, <|im_end|>.

Pass the answer logits and reference answer token IDs to the cross-entropy function.

The reference answer is the answer portion from the first row (training example).

Figure 8.13 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 with the reference answer tokens.

During distillation, we want the student to learn the teacher’s reasoning trace and final answer conditioned on the prompt. So, as shown in figure 8.13, we discard the logits and targets that correspond only to the prompt portion of the sequence when computing the cross-entropy.

Listing 8.10 Computing cross-entropy loss directly

input_ids = tok[:-1].unsqueeze(0) # Shift the sequence by one token so each input predicts the next token target.
target_ids = tok[1:]
logits = model(input_ids).squeeze(0)
first_answer_logit_idx = max(prompt_len - 1, 0) # Drop the prompt positions so the loss only covers the teacher answer.
answer_logits = logits[first_answer_logit_idx:]
answer_targets = target_ids[first_answer_logit_idx:]
with torch.no_grad():
ce_mean_direct = torch.nn.functional.cross_entropy(
answer_logits, answer_targets
) # Compute the cross-entropy loss.
print(f"Cross-entropy: {ce_mean_direct:.2f}")

The resulting cross-entropy loss is 1.68, which is similar to that in listing 8.9, where we used the sequence_logprob function. In other words, cross_entropy implements the same core calculation in a more optimized way. For training, we’ll therefore use the built-in cross_entropy function rather than our custom log-probability function.

The compute_example_loss helper in listing 8.11 wraps this logic into a convenient function that calculates the answer-only loss for a single example. “Answer-only loss” means the training loss is computed only on the teacher’s answer tokens, not on the prompt tokens. The following listing puts it all together into a function that applies the whole logic, from target preparation to cross-entropy computation.

Listing 8.11 Defining the loss for one distillation example

def compute_example_loss(model, example, device):
token_ids = example["token_ids"]
prompt_len = example["prompt_len"]
input_ids = torch.tensor(
token_ids[:-1], dtype=torch.long, device=device
).unsqueeze(0)
target_ids = torch.tensor(
token_ids[1:], dtype=torch.long, device=device
)
logits = model(input_ids).squeeze(0)
answer_start = max(prompt_len - 1, 0) # Ignore prompt tokens so the loss is computed on the distilled answer only.
answer_logits = logits[answer_start:]
answer_targets = target_ids[answer_start:]
<!-- page: 302 (printed 290) -->
loss = torch.nn.functional.cross_entropy(
answer_logits, answer_targets
)
return loss
with torch.no_grad():
loss = compute_example_loss(
model, train_examples[5730], device
)
print(f"Loss: {loss:.2f}")

Compute the cross-entropy loss.

Used to verify that the helper returns the same loss as before

The resulting loss is 1.68 again, which indicates that the function in listing 8.11 works as intended.

It is possible to process multiple examples in parallel by batching them together. I’ve omitted batching here to keep the implementation compact and lower the resource requirements. Appendix E discusses batching and throughput-oriented execution in more detail for loss computation and training in general.

Next, we’ll define a small wrapper that iteratively computes the average loss across multiple examples. This will be useful both for quick sanity checks and for tracking the validation loss during training.

Listing 8.12 Evaluating loss across multiple examples

@torch.no_grad()
def evaluate_examples(model, examples, device):
was_training = model.training
model.eval() # Temporarily switch to evaluation mode while scoring the examples.
total_loss = 0.0
num_examples = 0
for example in examples:
loss = compute_example_loss(model, example, device) # Sum the loss over all examples.
total_loss += loss.item()
num_examples += 1
if was_training: # Restore training mode so this helper is safe to call during training.
model.train()
return total_loss / num_examples # Average the loss over all examples.
train_loss = evaluate_examples(model, train_examples[:3], device)
print(f"Train loss (3 examples): {train_loss:.2f}")
val_loss = evaluate_examples(model, val_examples[:3], device)
print(f"Validation loss (3 examples): {val_loss:.2f}")

This is the output:

Train loss (3 examples): 0.98 Validation loss (3 examples): 1.02

We’ll reuse this evaluation function during training. Ideally, both quantities should decrease over time, which would indicate that the student model is becoming better at matching the teacher-generated target sequences.

In practice, the training loss, which is the loss computed on the training examples used for optimization, is often noisier because it is measured on the examples currently being optimized and can fluctuate depending on the sample order and recent parameter updates.

The validation loss, which is measured on a separate held-out validation set rather than on the examples currently being used for optimization, is computed without updating the model weights. As a result, it usually provides a cleaner signal of whether the student is improving in a way that generalizes beyond the training set. For this reason, the validation loss is often the more reliable metric to watch.

8.7 Implementing the training loop for distillation

Section titled “8.7 Implementing the training loop for distillation”

With the dataset prepared and the loss computation in place, we can now implement the training loop for distillation. This stage is highlighted in figure 8.14.

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.14 — With the dataset preparation and loss computation complete, we can now turn to the training loop for distillation (printed p. 291, scan page-303.png). Steps 1, 2, 2a–2c, 3a and 3b are ticked and step 3c is highlighted, annotated “We’ll now focus on the distillation training, which begins with loading our pretrained base model.”

The training loop we’ll use here is very similar to the one in chapter 6. The main difference is that here we’ll revisit the same training set multiple times across epochs and optimize the cross-entropy distillation loss instead of the GRPO objective (GRPO loss) used in RLVR. The detailed steps are shown in figure 8.15.

graph TD
    S1["1 - Initialize pretrained model and optimizer"] --> S2["2 - For each training epoch"]
    S2 --> S3["3 - Shuffle dataset"]
    S3 --> S4["4 - For each training step"]
    S4 --> S5["5 - Reset loss gradients from previous step"]
    S5 --> S6["6 - Calculate cross-entropy loss"]
    S6 --> S7["7 - Backward pass to calculate loss gradients"]
    S7 --> S8["8 - Update model weights using loss gradients"]
    S8 --> S9["9 - Calculate validation loss and report metrics"]
    S9 --> S10["10 - Save a model checkpoint"]
    S10 --> S4
    S4 --> S2

[!visual-conversion] Figure 8.15 — The distillation training loop (printed p. 292, scan page-304.png). 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 at certain intervals to track progress. Print annotations: “Similar to the training loop in chapter 6, except we now iterate over the training set multiple times via epochs”, “Inner loop over each training example in epoch”, “Stages 4–8 are identical to the training loop in chapter 6, except that we use a cross-entropy loss instead of GRPO loss”, and “Optional steps for tracking and saving the training progress.”

The following train_distillation function implements the loop shown in figure 8.15. It shuffles the training examples at the start of each epoch, computes the loss for each example, applies an optimizer step, optionally clips large gradients, and periodically evaluates on the validation set. The metrics are also written to a CSV file so we can inspect the learning curves later.

Listing 8.13 Implementing the distillation training loop

import time
def train_distillation(
model,
train_examples,
val_examples,
device,
epochs=2,
lr=5e-6,
grad_clip_norm=None,
seed=123,
log_every=50,
checkpoint_dir="checkpoints",
csv_log_path=None,
):
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
model.train()
total_steps = epochs * len(train_examples)
global_step = 0
rng = random.Random(seed)
if csv_log_path is None:
timestamp = time.strftime("%Y%m%d_%H%M%S")
csv_log_path = f"train_distill_metrics_{timestamp}.csv"
csv_log_path = Path(csv_log_path)
for epoch in range(1, epochs + 1): # Step 2: Iterate over training epochs
epoch_examples = list(train_examples) # Step 3: Shuffle the training examples at the start of the epoch
rng.shuffle(epoch_examples)
for example in epoch_examples: # Step 4: Iterate over training examples in epoch
global_step += 1
optimizer.zero_grad() # Stage 5: Reset loss gradient
loss = compute_example_loss(model, example, device) # Step 6: Compute the cross-entropy
loss.backward() # Step 7: Backpropagate gradients
if grad_clip_norm is not None:
torch.nn.utils.clip_grad_norm_( # Optionally clip large gradients to improve training stability
model.parameters(), grad_clip_norm
)
<!-- page: 306 (printed 294) -->
optimizer.step() # Step 8: Update the model weights
if log_every and global_step % log_every == 0:
val_loss = evaluate_examples(
model=model,
examples=val_examples,
device=device,
)
model.train()
print(
f"[Epoch {epoch}/{epochs} "
f"Step {global_step}/{total_steps}] "
f"train_loss={loss.item():.4f} "
f"val_loss={val_loss:.4f}"
)
append_csv_metrics(
csv_log_path=csv_log_path,
epoch_idx=epoch,
total_steps=global_step,
train_loss=loss.item(),
val_loss=val_loss,
)
ckpt_path = save_checkpoint(
model=model,
checkpoint_dir=checkpoint_dir,
step=global_step,
suffix=f"epoch{epoch}",
)
print(f"Saved checkpoint to {ckpt_path}")
return model
def save_checkpoint(model, checkpoint_dir, step, suffix=""):
checkpoint_dir = Path(checkpoint_dir)
checkpoint_dir.mkdir(parents=True, exist_ok=True)
suffix = f"-{suffix}" if suffix else ""
ckpt_path = (
checkpoint_dir /
f"qwen3-0.6B-distill-step{step:05d}{suffix}.pth"
)
torch.save(model.state_dict(), ckpt_path)
return ckpt_path
def append_csv_metrics(
csv_log_path,
epoch_idx,
total_steps,
train_loss,
val_loss,
):
if not csv_log_path.exists():
csv_log_path.write_text(
"epoch,total_steps,train_loss,val_loss\n",
encoding="utf-8"
)
def append_csv_metrics(
csv_log_path, epoch_idx, total_steps, train_loss, val_loss,
):
if not csv_log_path.exists():
csv_log_path.write_text(
"epoch,total_steps,train_loss,val_loss\n",
encoding="utf-8",
)
with csv_log_path.open("a", encoding="utf-8") as f:
f.write(
f"{epoch_idx},{total_steps},{train_loss:.6f},"
f"{val_loss:.6f}\n"
)

With a maximum sequence length of 2,048, the full training run requires about 15 GB of GPU memory. If this is too high for your hardware, you can lower the resource requirements by filtering out longer sequences earlier in the notebook, such as by changing max_len from 2048 to 1024 or 512 in the filter_examples_by_max_len step in listing 8.6.

Let’s execute a short training run.

Listing 8.14 Training the model

torch.manual_seed(0) # Seed PyTorch so the short demo is reproducible.
train_distillation(
model,
train_examples=train_examples[:10], # Train on a tiny subset so this notebook run stays lightweight.
val_examples=val_examples[:10],
device=device,
epochs=2,
lr=5e-6,
grad_clip_norm=1.0, # Same as in chapter 6
seed=123,
log_every=5,
csv_log_path="train_distill_metrics.csv",
)

Let’s briefly talk about the settings before we inspect the results. We are keeping the training run intentionally short. For instance, we use only the first 10 training examples and 10 validation examples, and we train for just 2 epochs. This is purely to keep the notebook run lightweight and fast enough for experimentation. For a real distillation run, we would of course train on many more examples, ideally, the whole training set.

The learning rate (lr=5e-6) is in a reasonable range for fine-tuning a pretrained model and worked well for this setup, as reflected in the loss curves discussed shortly.

The gradient clipping setting (grad_clip_norm=1.0) is the same as in chapter 6 and helps prevent unstable updates when a particular example produces unusually large gradients.

The log_every=5 setting means that validation loss is measured every 5 training steps. Since this demo uses only a handful of examples, this produces frequent progress updates so that we can quickly verify that the training loop behaves as expected. In a larger run, we would usually increase this interval to reduce evaluation overhead.

Finally, the csv_log_path="train_distill_metrics.csv" argument stores the training and validation losses in a CSV file so that we can inspect and plot them later.

The main goal of this run is not to achieve the best possible reasoning performance, but to confirm that the distillation pipeline works from end to end. Once that is established, we can move on to larger runs and inspect the resulting learning curves and checkpoints in more detail.

Let’s now take a brief look at the run’s output:

[Epoch 1/2 Step 5/20] train_loss=0.9648 val_loss=0.9082 [Epoch 1/2 Step 10/20] train_loss=0.9844 val_loss=0.8871 Saved checkpoint to checkpoints/qwen3-0.6B-distill-step00010-epoch1.pth [Epoch 2/2 Step 15/20] train_loss=0.8008 val_loss=0.8707 [Epoch 2/2 Step 20/20] train_loss=0.7148 val_loss=0.8586 Saved checkpoint to checkpoints/qwen3-0.6B-distill-step00020-epoch2.pth

Even though this is only a tiny demonstration run, the output already shows the expected overall behavior. Both the training loss and the validation loss decrease over time, which indicates that the student model is becoming better at matching the teacher-generated target sequences. The validation loss is especially useful here because it is computed on held-out examples and therefore provides a cleaner signal that the improvement is not limited to the training samples alone.

We can also see that checkpoints are saved at the end of each epoch. This is useful because it lets us resume training later or evaluate intermediate versions of the distilled model using the MATH-500 test set.

Now that we’ve implemented the training loop, the final stage is evaluating the distilled model, as shown in figure 8.16.

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.16 — Now we’ll evaluate the distilled model on the MATH-500 test set (printed p. 296, scan page-308.png). Steps 1 to 3c are all ticked and step 4 is highlighted, annotated “Lastly, we evaluate the distilled model on the MATH-500 test set.”

Instead of running the full distillation process inside the notebook, you can download a convenience script from the chapter’s supplementary materials. As in the previous chapter on RLVR, it is often convenient to keep the notebook focused on the core ideas and move the longer-running training job into a standalone script:

from reasoning_from_scratch.ch07 import download_from_github
download_from_github(
"ch08/04_train_with_distillation/distill.py"
)

After downloading the script with the preceding code, you can run it as follows in a code terminal (if you are not a uv user, replace uv run with python):

Terminal window
uv run distill.py \
--data_path deepseek-r1-math-train.json \
--validation_size 25 \
--epochs 3 \
--lr 1e-5 \
--max_seq_len 2048 \
--use_think_tokens \
--grad_clip 1.0

Using these settings, the full training run takes about 3 hours and 5 minutes on a DGX Spark and uses roughly 15.02 GB of GPU memory. (This is relatively modest compared with the earlier RLVR runs because most of the expensive work has already been moved into the one-time teacher data generation step.) If you do not want to run the training yourself, you can download the resulting metrics file and inspect it directly:

download_from_github(
"ch08/03_logs/deepseek-r1-2048_distill_metrics.csv"
)

The following listing implements a utility function to visualize the training metrics stored in the CSV file.

Listing 8.15 Plotting distillation losses from a CSV log

import csv
import matplotlib.pyplot as plt
def plot_distill_metrics(csv_path="train_distill_metrics.csv"):
total_steps, train_losses, val_losses, epoch_bounds = [], [], [], {}
with open(csv_path, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
step = int(row["total_steps"])
epoch = int(row["epoch"])

Load and plot the logged losses.

total_steps.append(step)
train_losses.append(float(row["train_loss"]))
val_losses.append(float(row["val_loss"]))
epoch_bounds.setdefault(epoch, [step, step])[1] = step

fig, ax = plt.subplots(figsize=(7, 4))

ax.plot(total_steps, train_losses, label="train_loss", alpha=0.3)
ax.plot(total_steps, val_losses, label="val_loss")
ax.set_xlabel("Total Steps")
ax.set_ylabel("Loss")
ax.legend()
epoch_axis = ax.secondary_xaxis("bottom")

epoch_axis.spines[“bottom”].set_position((“outward”, 45))

epochs = sorted(epoch_bounds)
epoch_axis.set_xticks(
[
(epoch_bounds[epoch][0] + epoch_bounds[epoch][1]) / 2
for epoch in epochs
]
)
epoch_axis.set_xticklabels(epochs)
epoch_axis.set_xlabel("Epoch")
plt.tight_layout()
plt.show()
plot_distill_metrics("deepseek-r1-2048_distill_metrics.csv")

The resulting plot is shown in figure 8.17.

Loss
1.2 |\ |
| \ || | --- train_loss
1.0 | \|||| | | --- val_loss
| \|||||||| | | |
0.8 | \||||||||||||||||| | | |
| ~~--_____________________________________
0.6 | ||||||||||||||||||||||||||||||||||||||
|
0.4 |
+--------------------------------------------------
0 2500 5000 7500 10000 12500 15000 17500 20000
Total Steps
|---------- 1 ----------|------ 2 ------|--- 3 ---|
Epoch

[!visual-conversion] Figure 8.17 — The training and validation losses of a 3-epoch distillation training run on DeepSeek-R1 reasoning traces (printed p. 298, scan page-310.png). Print annotation: “We can see an initial sharp and then gradual decline of the validation loss.” The x-axis carries a secondary epoch axis below the step axis. The body text supplies exact readings from the corresponding run log: train_loss=0.9648 val_loss=0.9082 at epoch 1/2 step 5/20, falling to train_loss=0.7148 val_loss=0.8586 by epoch 2/2 step 20/20. Per-step values across the full run are not recoverable at scan resolution.

The training-loss curve and validation-loss curve shown in figure 8.17 should be interpreted slightly differently. The training loss is computed for each training example during training at the current step, whereas the validation loss is computed on a small, fixed, held-out set. The validation curve is therefore less noisy and is the more informative signal here. We could also compute the training loss on a subset of examples, similar to the validation loss, but this would slow down training. The training loss is much less important than the validation loss for estimating training progress.

As we would hope, the validation loss drops sharply at first and then begins to flatten out, indicating that the model is learning from the distillation data but that additional training yields diminishing returns. We could experiment with a larger learning rate or a different schedule to train more aggressively, but overall the curve looks healthy.

Just as in chapter 7, we can evaluate saved checkpoints with the verifier-based utilities from chapter 3. The following command downloads the evaluation script from the supplementary materials:

download_from_github( "ch03/02_math500-verifier-scripts/evaluate_math500.py" )

Evaluate the distilled checkpoint on MATH-500 using the reasoning tokenizer in a code terminal, as follows:

uv run evaluate_math500.py
—dataset_size 500
—which_model reasoning
—max_new_tokens 4096
—checkpoint_path
“run_11/checkpoints/distill/qwen3-0.6B-distill-step06682-epoch1.pth”

For the later checkpoints from the same DeepSeek-R1 run, replace …step06682-epoch1.pth with …step13364-epoch2.pth, …step20046-epoch3.pth, and so on.

The evaluation results for the DeepSeek-R1 run used in this chapter are summarized in table 8.1. For reference, I’ve also included a second run trained on Qwen3 235B-A22B teacher outputs (a 235-billion-parameter Qwen3 model).

Table 8.1 MATH-500 task accuracy for different model checkpoints

MethodEpochFinal validation lossMATH-500 accuracy
1Base Qwen3 0.6B (chapter 3)--15.2%
2Reasoning Qwen3 0.6B (chapter 3)--48.2%
3DeepSeek-R110.543630.6%
4DeepSeek-R120.534932.4%
5DeepSeek-R130.534333.6%

Table 8.1 MATH-500 task accuracy for different model checkpoints (continued)

MethodEpochFinal validation lossMATH-500 accuracy
6Qwen3 235B-A22B10.404345.0%
7Qwen3 235B-A22B20.396343.8%
8Qwen3 235B-A22B30.394844.2%

Based on the results for the DeepSeek-R1 run shown in table 8.1, the MATH-500 accuracy improves from 30.6% after the first epoch to 33.6% after the third epoch, while the validation loss decreases from 0.5436 to 0.5343. This matches the earlier learning curve in figure 8.17, where the student clearly learns from the teacher-generated reasoning traces, but the gains begin to taper off after the initial improvement.

The Qwen3 235B-A22B run performs noticeably better in this setup. One likely reason is that the teacher and student come from the same model family, which means that the tokenizer, prompting conventions, and overall response style are more closely aligned. This can make the teacher targets easier for the smaller Qwen3 student to imitate.

Considering the MATH-500 accuracy of 45% (row 6), our distillation recipe reaches nearly the same performance as the reasoning reference model (48.2%, row 2), which is itself a distilled model but was trained on a much larger dataset generated by Qwen3 235B-A22B. This is the main tradeoff of distillation.

In general, we do not expect the smaller student to match the teacher exactly. For instance, Qwen3 235B-A22B has a MATH-500 accuracy of 92.4%, and DeepSeek-R1 has a MATH-500 accuracy of 91.2%. Still, we can recover a useful portion of the teacher’s reasoning behavior in a much cheaper model. Our distilled model accuracy could also be higher if we used a larger model (e.g., a 4- or 30-billion-parameter version of Qwen3 instead of Qwen3 0.6B), but this would increase the computational cost during training.

Let’s step back and connect the individual pieces we implemented into one complete workflow. Figure 8.18 summarizes the full distillation pipeline, starting with the introductory setup, followed by dataset generation and preprocessing, then the distillation training loop itself, and finally the evaluation of the distilled model on MATH-500.

This workflow completes the core technical recipe for distilling a smaller reasoning model from a stronger teacher. In practice, each stage offers room for variation, such as changing how the teacher data is generated and modifying the training settings.

Exercise 8.2: Distilling without <think> tokens

Repeat the distillation experiment without —use_think_tokens and compare the results against the version trained with reasoning traces wrapped in <think>… </think>. Inspect the validation loss and, if possible, evaluate the saved checkpoint on MATH-500. Then compare the results with those listed in table 8.1. How much do the explicit reasoning tags matter in this setup?

graph LR
    S1["1 - Distillation introduction and overview"] --> S2
    subgraph S2 ["2 - Prepare a dataset for distillation"]
        direction TB
        A["2a - Generate dataset"] --> B["2b - Load dataset"] --> C["2c - Format dataset for training"]
    end
    S2 --> S3
    subgraph S3 ["3 - Train via distillation"]
        direction TB
        D["3a - Load pretrained model"] --> E["3b - Implement loss computation"] --> F["3c - Implement the training loop"]
    end
    S3 --> S4["4 - Evaluate the distilled model"]

[!visual-conversion] Figure 8.18 — The evaluation of the distilled model completes the technical content of this chapter (printed p. 301, scan page-313.png). All steps, 1 through 4, are ticked.

8.9 Future directions for reasoning models

Before closing the chapter, let’s discuss where reasoning models are headed next.

For the foreseeable future, the broad pattern remains the same. For instance, the general strategy is to develop a stronger reasoning teacher, collect high-quality reasoning traces, and distill them into smaller student models.

One obvious direction is the continued refinement of the training recipe popularized by the DeepSeek-R1 paper, which includes both RLVR (chapters 6 and 7) for the flagship models and distillation for the smaller models focused on computational efficiency.

A second direction is optimization at inference time. In practice, a reasoning model should not always produce answers of equal length. Some tasks benefit from short, direct responses, whereas others benefit from more detailed, multistep reasoning. This creates room for more automatic and flexible inference scaling at the application layer, where the surrounding system decides when to ask for a short answer, when to allocate more reasoning budget, and when to stop early. For example, OpenAI implemented such a system with the launch of GPT-5 in 2025, adding an “auto” mode to steer the reasoning effort and the reasoning trace generation length.

A third direction for improvement is reward generation for RLVR. Much of the current work still relies heavily on rewards based on final answers, especially in math and code. But process rewards that check intermediate reasoning steps, not just the final result, may provide a richer training signal and help models learn more reliable reasoning strategies. For example, the DeepSeek-Math-V2 paper (https://arxiv.org/abs/2511.22570) recently demonstrated that judging the whole answer during training can meaningfully improve reasoning performance.

A fourth direction is the growing role of reasoning models as the engine inside larger agent applications such as OpenAI Codex, Claude Code, and OpenClaw. In these settings, the model must not only solve a simple math or coding problem, but also plan, call tools, recover from failures, and coordinate longer workflows. This naturally pushes reward design beyond math and code correctness. We may want rewards for successful tool use, information retrieval, policy compliance, and more. In turn, this leads to multi-reward training, where several objectives are optimized together instead of relying on a single correctness score.

Distillation will likely remain important in this setting because it offers a practical way to transfer these richer behaviors from larger and more expensive teacher systems into smaller models that are easier to deploy, may run locally, and are more cost effective for users.

This completes the main technical material of the book. We’ll now look at some brief pointers on what to try next, how to keep up with this fast-moving field, and where to find additional material.

A practical next step is to start combining the methods from this book instead of treating them as isolated techniques. For example, you could distill a smaller model from a strong teacher, continue training it with RLVR, and then apply inference-time scaling methods such as self-consistency or self-refinement at deployment time. Running these kinds of comparisons is often the fastest way to develop intuition for which method helps most in a given setting.

The appendices are also a good place to continue. They cover additional topics such as LLM architecture details, batched execution for higher throughput, and alternative evaluation approaches.

Lastly, the supplementary code repository (https://github.com/rasbt/reasoning-from-scratch) includes bonus material and standalone scripts that are better suited for longer runs and larger experiments than a notebook.

8.10.2 Staying up to date in a fast-moving field

Section titled “8.10.2 Staying up to date in a fast-moving field”

I hope this book gave you a clearer picture of how modern reasoning models work in practice.

Reasoning-model research is moving quickly, and specific algorithms, datasets, and best practices will continue to change, but the core ideas in this book will remain relevant and useful. These include careful model evaluation, distinguishing between

inference-time and training-time methods, and understanding how the training losses and reward signals are computed.

When new methods appear, it helps to map them back to these fundamentals. Many new techniques are best understood as variations or combinations of the building blocks we have implemented here, even when the surrounding training recipe becomes more complex.

In practice, I recommend several resources. First, you can skim recent machine learning and AI papers on arXiv (https://arxiv.org/list/cs.LG/recent) to spot new training and evaluation ideas early. But be aware that the volume of new papers is now so large that keeping up comprehensively is nearly impossible, so it is usually better to treat arXiv as a way to scan for themes and promising papers than to try to read everything.

Second, read technical summaries and reports for newly released models, since they often contain the most useful details about prompting conventions, benchmarks, and limitations. These are usually shared directly by the developer on the Hugging Face model hub, announcement blog articles, and social media.

Third, keep an eye on practitioner discussions on the social media platform X and in communities such as r/LocalLLaMA (https://www.reddit.com/r/LocalLLaMA/), where implementation details, replications, and failure cases often show up before they make it into polished papers.

AI-assistant or “deep research” tools can also be useful for monitoring these sources and summarizing new developments, but they work best as filters rather than substitutes for reading the original papers and model cards.

I also regularly write about AI and LLM topics on my blog at https://magazine.sebastianraschka.com.

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.

  • A distillation training loop is a standard supervised learning loop that 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. Additionally, it is useful and recommended that you evaluate checkpoints periodically on benchmark datasets.