Course 01 / Free & open

Distilling Generative Models

Knowledge distillation for language models, from the objective function to a study you could publish. An 18-chapter textbook, 13 hands-on labs, and every line of source.

Most distillation write-ups stop at the temperature-scaled loss from 2015. This one keeps going: where teacher logits actually come from, how tokenizers get in the way, what a top-k cache costs, why the choice of divergence changes the model you end up with, and which failure modes only show up once you are training on real checkpoints.

Every claim in the labs is checked with an assertion against autograd or a closed form, so a notebook fails loudly the moment a claim stops being true. Where a result needs a real training run, the lab says so plainly rather than dressing up a toy run as evidence.

The whole course is free and open. No signup, no email gate. Read the textbook in the browser, clone the repo, run the labs.

Free · No signup 18 chapters 13 labs Worked solutions PyTorch
Start here

The textbook reads in the browser with a searchable table of contents. The repository holds the labs, solutions, PDF, and reference code.

FormatTextbook + labs
Labs13 notebooks
LevelIntermediate
PriceFree

Prerequisites: you can read PyTorch and have trained a transformer before. Nothing beyond that.

The textbook / 18 chapters

Five parts, built in dependency order.

Nothing is used before it is defined. Part I builds the vocabulary, Part II derives the objective, and the later parts only introduce a technique once the machinery it rests on is already on the table.

Part I
Foundations
01What Distillation Is, and What It Is Not
02Distributions Over Tokens, and the Numbers That Hold Them
03Measuring the Distance Between Two Distributions
04Estimating Divergences from Samples
Part II
The Objective
05The Classical Objective
06Choosing a Divergence
Part III
Making It Real
07Tokenizers, Templates, and Alignment
08The First Real Run
09The Economics of Teacher Compute
Part IV
The Method Space
10Off-Policy Distillation and the Logit Cache
11Sequence-Level and Black-Box Distillation
12On-Policy Distillation
13Student Initialization: Prune, Then Distill
14Cross-Tokenizer and Representation Distillation
Part V
Systems, Judgment, and Research
15Serving a Teacher and Measuring Your Machine
16Evaluating a Distilled Model
17Security: What Distillation Carries and What It Leaks
18Research You Can Defend
The labs / 13 notebooks

Every unit answers one concrete question.

Each notebook states its tier on the first line and opens with the question it exists to settle. Solutions live in the repository, one notebook per lab, with the approach explained before the code.

00
Distributions, divergences, and numerics
What is actually being minimized?
Tier 1
01
The objective, verified
Why T², why beta bites, why k is measured rather than assumed.
Tier 1
02
Tokenizers, alignment, and real model outputs
Where do teacher logits come from, and how do they line up?
Tier 1
03
Classical KD and the capacity gap
When does a soft target beat a hard label?
Tier 2
04
Off-policy distillation with cached logits
The cheapest real pipeline on a single box.
Tier 2
05
Divergence choice and mode behavior
Forward, reverse, JSD: what each one costs you.
Tier 2
06
Sequence-level, rationale, and black-box KD
Distilling when all you have is an API.
Tier 2
07
On-policy distillation
Exposure bias, GKD, lmbda, and the cold-start problem.
Tier 2
08
Engineering and scale
Top-k caching, teacher servers, and throughput math.
Tier 2
09
Student initialization and prune-then-distill
Stop starting from random.
Tier 2
10
Cross-tokenizer and representation distillation
ULD, GOLD, and hidden-state matching.
Tier 2
11
Evaluation, failure modes, and security
Entropy collapse, calibration, backdoor transfer, extraction.
Tier 1 + 2
12
Capstone: a reproducible distillation study
A multi-arm comparison you could defend in review.
Tier 2
How the labs work

Two tiers, and an honest line between them.

The split follows one rule: the material worth asserting everywhere is the material that needs no training, and the material that needs training can only be validated on the box that trains.

Tier 1 — runs anywhere

Synthetic logit tensors, real tokenizers, and small cached models. No GPU, finishes in minutes, fully asserted. The saved outputs come from real executions.

This is where the silent bugs actually live. A wrong beta or an unshifted mask trains the wrong thing without throwing an error or even visibly bending the loss curve.

Tier 2 — the real runs

Distillation on real checkpoints, written against cited library APIs. Each lab has three parts: a pre-flight that runs anywhere and is fully asserted, the training run itself behind a flag, and a verdict section with expected ranges and failure signatures.

If the pre-flight passes, the run cannot fail for a silent reason. A green check from a toy stand-in would just be false confidence, so Part B does not pretend.

Written against primary sources

Hinton on the temperature objective, Kim and Rush on sequence-level KD, Agarwal et al. on GKD, MiniLLM on reverse KL, Minitron and Sheared-LLaMA on prune-then-distill, T-MTB and DistillGuard on the security surface.

The reading happens before the chapter is drafted, and the labs introspect the installed library configs at runtime rather than trusting any document, including this one.

Get started

Open the book, or clone the repository.

The textbook is the narrative path through the material. The repository holds the executable version: labs, solutions, the reference implementation in code/kd_core.py, the full reading list, and the PDF edition.

Looking for the other courses? Mechanistic Interpretability of Large Language Models covers reverse-engineering transformers, from the residual stream to automated circuit discovery, or see all free courses.