4.3.14 · D4Pretraining & Fine-Tuning LLMs

Exercises — Knowledge distillation

1,968 words9 min readBack to topic

Throughout, we reuse one running vocabulary of size (number of classes/tokens). A logit is just the raw, un-normalised score the network assigns to class before softmax. Nothing below uses a symbol we have not named.


Level 1 — Recognition

Exercise 1.1

State, in one sentence each, what hard targets, soft targets, and dark knowledge are.

Recall Solution
  • Hard targets: the one-hot ground-truth label, e.g. — it says only the correct class.
  • Soft targets: the teacher's full softened probability vector, e.g. — every class gets a share.
  • Dark knowledge: the relational information carried by the non-largest probabilities — "a cat looks more like a dog than like a car," which the hard label cannot express.

Exercise 1.2

Write the temperature-softmax formula and say what happens in the two limits and .

Recall Solution

  • : every logit is divided by a huge number, so all , so , and every class gets — the uniform distribution.
  • : the largest logit's exponent dominates everything else, so the distribution sharpens to a one-hot at the argmax.

Level 2 — Application

Exercise 2.1

Teacher logits over cat, dog, car. Compute the softmax at .

Recall Solution

. Sum . Cat dominates; dog and car are already partly visible because these logits are not extreme.

Exercise 2.2

Now soften the same logits at . Compare with 2.1 — did the ordering change? Did the gaps?

Recall Solution

Divide logits by : . . Sum . Ordering: cat > dog > car — unchanged (it always is; temperature never reorders). Gaps: hugely reduced — cat fell from to , car rose from to . The similarity structure is now loud.

Exercise 2.3

Given student softmax and true label = cat, compute the hard cross-entropy loss .

Recall Solution

The one-hot label is , so only the cat term survives:


Level 3 — Analysis

Exercise 3.1

Using the softened teacher from 2.2 and student soft probs , compute the soft cross-entropy .

Recall Solution

.

Exercise 3.2

With , , (Ex 3.1) and (Ex 2.3), compute the total distillation loss . Then recompute it wrongly without the and state how badly the soft term shrinks.

Recall Solution

Correct: Without (the bug): The soft contribution collapses from to — a factor of smaller. The optimiser would then almost ignore the teacher and just fit hard labels. This is exactly why the factor exists.

Figure — Knowledge distillation

Exercise 3.3

Explain why the soft-loss gradient scales like , referencing the softmax cross-entropy gradient .

Recall Solution

Two independent factors of appear:

  1. From the student softmax argument. Each logit enters as , so differentiating with respect to the raw logit brings down a chain-rule factor . This gives .
  2. From the difference itself. For small logits, Taylor-expand . Then where are teacher logits — a second . Multiplying the two: overall gradient . Scaling by cancels this shrinkage so the soft and hard gradients keep comparable magnitude. See Cross-Entropy and KL Divergence for the base gradient identity.

Level 4 — Synthesis

Exercise 4.1

A giant LLM predicts over a vocabulary of tokens at every position of a 512-token sequence. Argue, with numbers, why sequence-level logit distillation is enormously more informative per training example than hard next-token labels.

Recall Solution
  • Hard labels: each position gives one integer (the true next token) — scalars of signal per sequence.
  • Soft logit targets: each position gives a full distribution over tokens. That is probability values per sequence.
  • So the soft signal is richer per position (before compression). Even after the teacher concentrates most mass on a few tokens, the tail encodes which alternative continuations are plausible — grammatical, semantic, and stylistic neighbours. This is why DistilBERT and instruction-distilled models learn so efficiently: one forward pass of a teacher yields a dense target where a hard label yields one bit of "correct token."

Exercise 4.2

Design a distillation recipe for compressing a 7B-parameter chat model into a 1.3B student that must follow instructions. Which KD variant, temperature, and loss mix would you choose, and why? Name one prerequisite topic you rely on.

Recall Solution

A defensible recipe (many valid answers — grade on justification):

  1. Variant: sequence-level KD. Let the teacher generate high-quality instruction–response pairs, then train the student on that generated corpus (Alpaca-style). Rationale: instruction following is about producing coherent sequences, so imitating the teacher's actual outputs transfers behaviour, not just per-token marginals. Ties directly to Instruction Tuning.
  2. Optionally add response-based logit KD on the same data: match next-token distributions at so the student inherits calibrated token uncertainty, not just the greedy path.
  3. Loss mix: with , . Keep the hard term so the student stays anchored to real targets and doesn't drift into teacher hallucinations.
  4. Prerequisite leaned on: Transfer Learning — the student is warm-started from a pretrained checkpoint, then distilled, rather than trained from scratch.

Level 5 — Mastery

Exercise 5.1

Prove that as , the softened softmax of any finite logit vector converges to the uniform distribution , and interpret what this means for the usefulness of distillation at extreme temperature.

Recall Solution

Write . As , every , so . Hence the numerator and the denominator . Therefore Interpretation: at extreme the teacher's distribution is indistinguishable from a coin flip over all classes — it carries zero relative information. The student learns nothing about class structure. This bounds how high you may push : dark knowledge lives at moderate softening, then evaporates. Confirms the L3 trap's warning quantitatively.

Figure — Knowledge distillation

Exercise 5.2

Show that KD's soft target and label smoothing produce different targets even when they happen to share the same top-1 probability, and explain why only one of them transfers dark knowledge. Use logits (from Ex 2.1) at as the KD target and a label-smoothing target with smoothing over classes.

Recall Solution

KD soft target (Ex 2.1): — cat highest, then dog, then car. The ordering encodes "cat resembles dog more than car." Label smoothing target: . Both put most mass on cat, but label smoothing gives dog and car the identical — it is uniform over the wrong classes and says nothing about similarity. KD gives them vs — a structured split. Conclusion: only KD carries dark knowledge; label smoothing is a flat regulariser. Matching top-1 mass is not enough; the shape of the tail is what teaches relationships.


Recall Final self-check

Did ever change the ranking of classes in any exercise? ::: No — temperature only changes the spread/confidence, never the order. In Ex 3.2, by what factor did dropping shrink the soft loss? ::: By . What single property distinguishes KD's soft targets from label smoothing? ::: KD's tail is structured (encodes class similarity); label smoothing's tail is uniform.