Lab#LL01-1: Distilling a reasoning model

Lab

📘 Lab#LL01-1: Distilling a reasoning model

A large reasoning model can teach a smaller one by writing solutions out loud. We first ask why that works, and how hard targets differ from soft ones, then we walk through the chapter 8 recipe: traces, a filter, a 0.6B student, and a published GPU run.

This lab follows chapter 8 of Sebastian Raschka’s Build a Reasoning Model (From Scratch). The tiny cells on this page run in the browser. The real training of a Qwen3 0.6B student does not; that needs a GPU or Colab, and we point you at Raschka’s distill.py. Small helpers live in labs/ll01-1/ in this wiki repo.

The two live cells below are deliberately tiny, and are labelled as such: a cross-entropy versus KL toy on three tokens, and one canned <think> trace. There is no Qwen3 here, and no 12,000-problem download.

1 Why distill a reasoner?

A good reasoning model is often large, and large is expensive to run. The tempting idea is simple: let the large model teach a smaller one.

The smaller model is the student. The large one is the teacher. The student practises on the teacher’s work, instead of having to invent the skill from a blank page. That is distillation, and the short reference What is distillation? is the place to sit with the idea. This lab is the walk-through.

Everyday example

A marking scheme only says whether the final number is right. A tutor who thinks aloud also shows how they got there. Distillation is the second kind of practice: the student learns from the tutor’s written walk-through.

Reinforcement learning with a verifier (RLVR) is closer to the marking scheme. Both can work. For a small model, learning from a strong teacher’s traces can be the more effective of the two — that is a claim from the DeepSeek-R1 work, where the 671-billion-parameter model was trained first and the smaller variants were then distilled from it.

Diagram of a Teacher box, a middle box of reasoning traces, and a Student box, with arrows from teacher to traces to student.

The teacher writes traces; the student trains on that text.

Diagram of a Teacher box, a middle box of reasoning traces, and a Student box on a dark background, with arrows from teacher to traces to student.

The teacher writes traces; the student trains on that text.

In this lab the teacher traces come from a large DeepSeek-R1 model, already generated. The student is a Qwen3 0.6B base model. We will not load that student in the browser.

2 Hard teaching and soft teaching

Before any dataset, we need to be clear about the loss. There are two basic signals, and a third that adds them together.

Hard distillation treats the teacher’s tokens as the target. The loss is ordinary cross-entropy. You only need the text, not the teacher’s logits. That is what this chapter implements, and what you can do when the teacher is a closed model that will not give you its distribution.

Soft distillation asks the student to match the teacher’s probabilities, usually with \(\mathrm{KL}(p_{\text{teacher}} \,\Vert\, p_{\text{student}})\). That needs logits (or log-probabilities) and a shared tokenizer.

Combined distillation is the 2015 knowledge-distillation recipe: \(L = \mathrm{CE} + \lambda\,\mathrm{KL}\).

Hard and soft are not two brand names for the same thing. Hard only looks at the winning token. Soft looks at the whole shape of the teacher’s guess, including the near misses.

A three-token toy is enough to feel the difference. The teacher is fairly sure the next token is yes. The student currently leans toward no. Cross-entropy only asks how much mass the student put on yes. KL also punishes the student for missing the rest of the teacher’s shape. This cell is a toy, not a language-model training run.

Ex#LL01-1

The same script lives at labs/ll01-1/toy_ce_kl.py if you want to run it locally. On a typical run you should see yes as the hard target, a hard CE of about \(1.08\), and a soft KL of about \(0.48\).

3 A reasoning trace is a written solution

Soft labels pass on a distribution. A reasoning teacher can also pass on a trace: the written walk-through, then the answer. In the chapter 8 dataset each row has four fields: the problem, a ground-truth answer, the teacher’s message_thinking, and the teacher’s final message_content. For training, the thinking and the answer are wrapped like this:

<think>…the walk-through…</think>

the final answer

The tags are a convenience, not a law of nature. They make it easier to show or hide the monologue later, the way some chat interfaces hide a model’s chain of thought. The student still has to write the words in between.

The cell below uses one short, canned shop problem, written for this lab. It is not a row from the 12,000-problem dataset, and it does not download anything.

Ex#LL01-2

That block of text is what a hard-distillation student would be asked to predict, token by token, given the problem. The next sections describe how chapter 8 does this at scale. We stay with numbers and a diagram; we do not pull the full dataset into the browser.

4 The dataset, without downloading it

Chapter 8 starts from 12,000 MATH problems that do not overlap the MATH-500 test set — the same pool used earlier in the book for RLVR. Those problems were given to a large DeepSeek-R1 teacher, and the traces were saved ahead of time (the published generation used a hosted 671B model). Readers of the book are not asked to regenerate that set. It is published as rasbt/math_distill and is about 108 MB of JSON. A second partition, from a Qwen3 235B-A22B teacher, is there too.

On that training set, the DeepSeek-R1 teacher matches the ground-truth answer on 90.6% of the problems (10,871 / 12,000). A teacher can still be wrong; the number is a useful check, not a guarantee.

The traces are then tokenized with Qwen3’s reasoning tokenizer, which knows the <think> tags, and long sequences are dropped. With a maximum length of 2,048 tokens, 12,000 examples shrink to 6,695 (the average length of those that remain is about 1,180 tokens). A 25-example validation split is taken from the shuffled remainder; the rest is the training set.

Five-step pipeline: MATH problems, teacher answers, format with think tags, filter to 6,695 examples of at most 2,048 tokens, and train a Qwen3 0.6B student on a GPU or Colab.

The chapter 8 pipeline: 12,000 MATH problems, teacher traces, think-tag formatting, a 2,048-token filter, then GPU training of the 0.6B student.

Five-step pipeline on a dark background: MATH problems, teacher answers, format with think tags, filter to 6,695 examples, and train a Qwen3 0.6B student on a GPU or Colab.

The chapter 8 pipeline: 12,000 MATH problems, teacher traces, think-tag formatting, a 2,048-token filter, then GPU training of the 0.6B student.

5 The student, and the loss

The student is the Qwen3 0.6B base model used throughout the book, loaded onto whatever device is available (cpu, a CUDA GPU, or Apple MPS). The loss is hard distillation: next-token cross-entropy, comparing the student’s distribution with the teacher’s tokens. The training loop is the book’s earlier loop, with this cross-entropy in place of the GRPO loss, and with more than one pass over the data (epochs).

In the notebook, a short demonstration trains on ten examples for two epochs, with a learning rate of \(5\times 10^{-6}\). That is only to show that the loop runs. The real command uses the full filtered set, three epochs, and a learning rate of \(10^{-5}\), at a maximum sequence length of 2,048.

That full run needs about 15 GB of memory. The published timing is about 3 hours 5 minutes on a DGX Spark, using 15.02 GB of GPU RAM. If memory is tight, the filter can be tightened to 1,024 or 512 tokens. None of this belongs in a browser tab.

The script to use is Raschka’s, in the book repository, not a copy kept here:

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

Details, including how to install the package and where to find checkpoints, are in labs/ll01-1/README.md in this repo and in ch08/04_train_with_distillation/distill.py.

6 What the published run found

After training, chapter 8 evaluates checkpoints on MATH-500. The figures below are the published results from that chapter, not a run made for this wiki.

Teacher data Epoch MATH-500 accuracy Final validation loss
Base (book, chapter 3) — 15.2% —
Reasoning checkpoint (book, chapter 3) — 48.2% —
DeepSeek-R1 distillation 1 30.6% 0.5436
DeepSeek-R1 distillation 2 32.4% 0.5349
DeepSeek-R1 distillation 3 33.6% 0.5343
Qwen3 235B-A22B distillation 1 45.0% 0.4043
Qwen3 235B-A22B distillation 2 43.8% 0.3963
Qwen3 235B-A22B distillation 3 44.2% 0.3948

A few things are worth noticing, without over-reading a single table. The 0.6B base model starts at 15.2%. Distilling from DeepSeek-R1 traces lifts it into the low thirties. Distilling from a same-family Qwen3 235B teacher gets closer to the book’s earlier “reasoning” checkpoint (48.2%), with a lower validation loss. More epochs help only a little on this run; the validation loss flattens. The choice of teacher data matters at least as much as the number of epochs.

7 On a GPU or Colab

To reproduce the training, use a machine with a GPU, or a Colab notebook with a GPU runtime. Install reasoning_from_scratch, torch, and tokenizers, download the published traces, and run distill.py as above. Optional extras in the book repo include generating new traces (Ollama or OpenRouter; the original 12,000 DeepSeek-R1 traces cost about $50 in API time), a batched training script, and Hugging Face wrappers.

8 Summary

Idea In one sentence
Distillation here A 0.6B student learns from a large teacher’s written traces
Hard versus soft CE on the winning tokens, or KL on the whole distribution
A trace <think> walk-through, then the answer
The dataset 12,000 MATH problems, already answered; filter to 6,695 sequences of at most 2,048 tokens
The student Qwen3 0.6B base, trained with next-token cross-entropy
In the browser Only the three-token toy and one canned trace
On a GPU or Colab distill.py, about 15 GB and a few hours for the full run
Published MATH-500 15.2% base; about 34% after DeepSeek-R1 distillation; about 45% after a Qwen3 235B teacher

9 FAQ

Why not train the 0.6B model in this page? Because it is a real language-model run: a 0.6B parameter student, long sequences, about 15 GB of memory, and hours of GPU time. A browser cell cannot host that honestly. The two cells that do run here are toys, and they are labelled as such.

Why hard distillation, if soft can be better? Soft distillation needs teacher logits and a shared tokenizer. The DeepSeek-R1-style recipe, and most work with a closed teacher, only has the text. Hard distillation is the practical default. The first live cell is there so the two losses do not stay abstract.

Do I need to generate the 12,000 traces myself? No. They are published. Regenerating them is optional, slow, and can cost money. The bonus scripts are in the book repository if you want to try a smaller sample.

Is a higher MATH-500 number the same as “better reasoning”? It is one maths benchmark, on one student size, with two teacher sources. It is evidence that the student picked up useful behaviour from the traces. It is not a full verdict on reasoning.

Where is this in the book? Chapter 8 of Build a Reasoning Model (From Scratch): the hard/soft distinction, the MATH traces, the 0.6B student, the training loop, and the evaluation table. There is also a video walk-through.

10 See also

11 References

Back to top