← All notes
Course chapters
On this page

Lesson 28 · Large language models

Model distillation

Teach a student from model probabilities or generated text. Explore temperature, soft and hard losses, then examine what transfers and how upfront teaching costs affect deployment.

1. Can a smaller model learn from a teacher?

A capable model may require more memory, latency or compute than an application can afford. A smaller model can be easier to deploy, but its behaviour may fall short on the tasks that matter.

Distillation uses a teacher's behaviour as a training signal for a student. The student is often smaller, but the broader idea also includes learning from ensembles or teaching a different architecture.

It is not simply copying a parameter file. The student is trained to reproduce selected aspects of the teacher's behaviour on data.

Pause and think

If a teacher generates answers for a training set, is training a student on that text useful? What information might it leave out?

I've thought about it — show the answerHide answer

It can be useful: the generated answers provide demonstrations of the desired behaviour. But one generated sequence does not expose the teacher's full distribution over alternatives at every prediction step.

This distinction leads to two common routes: matching probability distributions and learning from generated sequences.

2. Hard labels and soft targets

Consider a four-class question: “What is the capital of France?” The class order is Paris, London, Rome and Berlin. A one-hot hard label assigns probability one to Paris and zero to the other classes.

A teacher can also provide a probability distribution. Besides the top answer, it tells the student how that teacher distributes probability among alternatives.

Hard target · correct class Paris

q=[1,0,0,0]q=[1,0,0,0]

Names the desired class without describing how the teacher scores alternatives.

Soft target · teaching logits at T = 4

[64.51%, 14.39%, 11.21%, 9.89%]

Class order: Paris, London, Rome, Berlin. Probabilities describe this hypothetical teacher, not objective similarity or truth.

Figure 28-1. Hard and soft targets provide different supervision. Non-winning probabilities can be informative, but can also reflect teacher errors.

These relative probabilities are sometimes called dark knowledge. They may reflect useful learned similarities or uncertainty. They may also contain biases, calibration errors or irrelevant associations.

A larger probability for London than Rome in our example does not prove that London is objectively a “more capital-like” city. These are hypothetical model scores for this question, not verified semantic distances.

A hard label is also not literally one bit of information in every problem: selecting one class out of many differs from a binary decision. The important distinction here is the supervision provided, not a fixed information-bit count.

Classical distribution distillation asks the student to match more than just the winning output. The teacher can remain fixed while optimisation changes the student's parameters.

3. Temperature reveals the distribution's softer shape

A very peaked teacher distribution puts little probability on alternatives. Temperature changes how its logits become probabilities:

pi(T)=exp⁡(zi/T)∑jexp⁡(zj/T),T>0.p_i^{(T)}=\frac{\exp(z_i/T)}{\sum_j\exp(z_j/T)},\qquad T>0.

For teacher logits [8,2,1,0.5][8,2,1,0.5], raising TT from 11 to 44 makes the distribution flatter. The top class remains Paris, but the other classes receive more probability mass.

Dividing by a larger positive temperature preserves the ordering of logits. It reduces logit gaps; it does not literally make the gaps larger. What becomes easier to inspect is the probability mass assigned to less likely alternatives. Higher temperature is not automatically a better training choice.

A common loss combines soft-target matching with ground-truth supervision:

L=αT2DKL ⁣(pteacher(T)∥pstudent(T))+(1−α)Lhard,\mathcal L =\alpha T^2D_{\mathrm{KL}}\!\left(p_{\mathrm{teacher}}^{(T)}\parallel p_{\mathrm{student}}^{(T)}\right) +(1-\alpha)\mathcal L_{\mathrm{hard}}, Lhard=−log⁡pstudent(1)(c),0≤α≤1,\mathcal L_{\mathrm{hard}}=-\log p_{\mathrm{student}}^{(1)}(c), \qquad 0\leq\alpha\leq1,

where cc is the correct class. Both models use the same temperature in the soft term, while the hard term uses temperature one in this convention.

The KL direction matters:

DKL(pt∥ps)=∑ipt,ilog⁡pt,ips,i.D_{\mathrm{KL}}(p_t\parallel p_s) =\sum_i p_{t,i}\log\frac{p_{t,i}}{p_{s,i}}.

When teacher targets are fixed, minimising this KL has the same student-dependent part as cross-entropy with the teacher's soft targets. The T2T^2 factor is commonly used to compensate for how temperature changes the gradient scale; it is not an extra guarantee about model quality.

Paris

Teacher 99.607% · student 82.283%

London

Teacher 0.247% · student 11.136%

Rome

Teacher 0.091% · student 4.097%

Berlin

Teacher 0.055% · student 2.485%

KL(teacher ∥ student): 0.175358 nats

T² × KL: 0.175358

Hard-label loss at T = 1 (Paris): 0.195007

Combined loss: 0.185183

Teacher logits [8, 2, 1, 0.5]; student logits [4, 2, 1, 0.5]. These are hand-picked numbers, not measured model predictions. Controls recalculate losses without updating weights. Teal bars show the teacher; grey bars show the student.

Experiment 28-A. Compare softened distributions and calculate the temperature-scaled soft loss plus hard-label loss.

The experiment recalculates losses for fixed examples without training a student. Choosing the matching student makes the soft KL zero, but its hard-label loss is still positive because its probability of Paris is not exactly one.

Changing temperature can change both the raw KL and its scaling. Loss values at different temperatures should not be read as a direct ranking of model quality. Changing α\alpha changes the training objective, not the underlying predictions in this fixed-logit demonstration.

At deployment, the student ordinarily produces its own predictions without a teacher alongside it. A distillation training temperature is not automatically the same setting as a text-generation sampling temperature.

4. Two teaching signals in practice

Distribution distillation

Input / shared prefix → teacher probabilities

Same output classes → student probabilities

Distribution loss → update student

Sequence distillation

Prompts → teacher generates text

Validate and filter → training examples

Response-token loss → update student

Figure 28-2. Two common teaching signals. Access to teacher probabilities is different from access only to generated sequences; neither requires the student to copy the teacher architecture.

Distribution distillation

For language models, a teacher distribution can supervise the student's next-token probabilities at a shared prefix. Full-vocabulary probabilities or logits expose many alternatives beyond the sampled token.

This requires access to those scores, but not necessarily ownership of the teacher or its internal activations. An interface can expose scores, a subset of log probabilities or only text; those access patterns provide different information. A few returned top-token scores are not automatically the complete distribution.

Direct token-level KL assumes aligned output classes and compatible prefixes. If teacher and student use different tokenizers, their vocabulary entries and sequence positions do not directly match. Additional alignment methods or text-level supervision are needed.

Sequence distillation

The teacher generates responses to prompts, and the student trains on the resulting text, often using an SFT-style response loss:

Lseq=−∑tlog⁡ps(y^t∣x,y^<t),\mathcal L_{\mathrm{seq}} =-\sum_t\log p_s(\widehat y_t\mid x,\widehat y_{<t}),

where y^\widehat y is a teacher-generated response. One selected sequence contains less explicit information about alternative next tokens than the full distributions along it, but it can still provide rich demonstrations.

Data filtering, deduplication and validation matter. A teacher can generate mistakes, unsupported claims or patterns that the student then imitates. Multiple samples, verified answers and a suitable prompt distribution can improve the teaching data, without guaranteeing success.

Reasoning-oriented distillation may train on teacher-generated solutions containing intermediate explanatory steps. These are generated sequences, not guaranteed access to the teacher's complete internal reasoning process. A fluent explanation is not proof that the solution is correct.

This text-level route can also accommodate different teacher and student architectures or tokenizers, because the student tokenizes the teaching text with its own tokenizer.

5. What transfers, what does not, and what does it cost?

DistilBERT is a published example of training a smaller BERT-style representation model with a combination of language-model, distillation and representation-alignment losses. Its reported size, speed and benchmark results describe a particular experiment; a single “percentage of intelligence retained” is not a general measure.

The DeepSeek-R1 work also describes distilling generated reasoning data into smaller student models. This illustrates sequence-based teaching; it does not establish that a student retains every teacher capability or that all tasks benefit equally.

A teacher is not an absolute performance ceiling

A student trained to exactly match one teacher distribution on the same inputs would reproduce that distribution. That narrow statement is different from claiming that a real student can never outperform its teacher on any evaluation.

Different training data, ground-truth supervision, regularisation, teacher ensembles or task specialisation can let a student outperform a teacher on some measures. It can also lose capabilities elsewhere. Neither superiority nor a fixed ceiling follows merely from the word “distillation”.

Capacity, data coverage and optimisation constrain what a student learns. It is not guaranteed to fit the teacher's entire behaviour, and any evaluation should include held-out cases and the application's actual requirements.

Pay for teaching before saving at deployment

Teacher inference, data validation, student training and evaluation have an upfront cost. A cheaper deployed student can offset that cost over enough requests, if it meets the required quality and service constraints.

A simplified cost comparison is:

Cteacher=Qct,Cstudent=C0+Qcs.C_{\mathrm{teacher}}=Qc_t,\qquad C_{\mathrm{student}}=C_0+Qc_s.

If ct>csc_t>c_s, equality occurs at:

Q∗=C0ct−cs.Q^*=\frac{C_0}{c_t-c_s}.

Hypothetical costs: teacher request 100 units; student request 20 units; one-time data generation and training 10,000 units.

Teacher-only cost: 10,000 units

Student cost including setup: 12,000 units

Cost equality at 125 requests under these assumptions

Illustrative arithmetic only, excluding quality differences, hosting, maintenance and variable response lengths. These are not prices, measured savings or a deployment recommendation.

Experiment 28-B. Explore how a fixed upfront distillation cost can change the deployment cost comparison.

The displayed costs are deliberately hypothetical units. Real cost and memory depend on precision, model shape, context length, batching, hardware and response length. There is no universal claim that every seven-billion-parameter student fits on one particular GPU or saves a fixed factor.

Distillation can be combined with quantisation or pruning, but these are different techniques. A compact model's useful performance must be measured rather than inferred from its parameter count or training label.

6. What you have discovered

  • Distribution distillation teaches from probabilities over alternatives; sequence distillation teaches from generated outputs.
  • Temperature softens both teacher and student distributions in the soft-loss term, while the hard-label term can retain ordinary ground-truth supervision.
  • Access to text, partial scores, full distributions or internal representations supports different distillation methods.
  • Student capacity, data quality and evaluation determine what transfers; a teacher is not a universal performance ceiling.
  • Upfront teaching costs and later deployment costs need to be considered together.

The main technical path is now in place: attention, Transformer components, tokenization, architectures, training and efficiency techniques. Next, we will revisit the journey from N-grams to Transformers, connecting the problems that motivated each step.

Check your understanding

1. What extra target information does classical distribution distillation provide compared with a one-hot label?
2. Why use a temperature above one in classical distillation?