4.3.14 · D1Pretraining & Fine-Tuning LLMs

Foundations — Knowledge distillation

2,431 words11 min readBack to topic

This page assumes you have seen nothing. Before you can read the parent note Knowledge Distillation, every letter and squiggle it uses is built here, one at a time, each earning its place before the next arrives.


0. The very first picture: what is a "score" and a "probability"?

A model looking at a photo of {cat, dog, car} produces three raw numbers — one per option. These raw, unbounded numbers are called logits.

Figure — Knowledge distillation

Figure s01 — reading the logits. Three bars, one per option: magenta = cat, violet = dog, orange = car. Their heights are the logit values (), printed above each bar. Notice the car bar dips below the zero line drawn in navy — logits are allowed to be negative, as the arrow annotation points out. But we cannot use raw scores as probabilities, because probabilities must (a) never be negative and (b) add up to exactly . That is the job of the next tool.


1. Why we need (the exponential) — the "make-it-positive" machine

To turn any score into something never-negative, we need a function that:

  1. takes any real number in, and
  2. always spits out a positive number.

The exponential function does exactly this. Here is a fixed constant (just a number, like ).

Figure — Knowledge distillation

Figure s02 — the exponential curve. The magenta curve is ; the violet dots mark the sample values above. Read two things off it: (1) the curve never touches the navy zero line — output is always positive, highlighted by the orange "always > 0" arrow; (2) it climbs steeply on the right — small increases in cause large increases in .


2. The sum symbol — "add up all of them"

The parent note writes . That big Greek letter (sigma) is just a compact way to say "add up a list".

Why do we need it? Because to make probabilities add to , we must divide each positive score by the total of all positive scores — and is how we write "the total".


3. Putting it together: the softmax — the "make-it-a-probability" machine

Now we can read the parent note's central formula:

Read it left to right in plain words:

  • = the probability the model assigns to option (a number between and ).
  • Top (): make this option's score positive.
  • Bottom (): the total of all options' positive scores.
  • Divide → each option's share of the total. By construction they now add to .
Figure — Knowledge distillation

Figure s03 — the softmax pipeline. For each option (cat, dog, car) three bars stand side by side: orange = the raw logit , violet = its exponential (now all positive), magenta = the final probability (scaled ×60 just so the tiny values are visible). Follow one option left-to-right and you see the machine work: a possibly-negative score becomes positive, then the navy arrow "divide by the sum → adds to 1" turns the violet bars into the magenta shares printed above them (). That exponential middle step is why a trained model looks overconfident — the tiny probabilities on "dog" and "car" get crushed. Distillation's whole trick, temperature, is about un-crushing them.

Recall Why is the biggest logit still the biggest probability?

Because is increasing: a bigger input always gives a bigger output. So softmax preserves the ranking of the logits — it only re-scales how confident each one looks, never who wins. ::: Softmax preserves ranking; only the confidence spread changes.


3b. A practical edge case: numerical stability (log-sum-exp)


4. The word "distribution", and = how many options

We keep saying distribution. It just means "the full list of probabilities across all options", e.g. . The parent uses for how many options there are (3 for {cat,dog,car}; ~50,000 for an LLM's vocabulary). When the parent writes for the uniform case, that is "everyone gets an equal slice".


5. The and "cross-entropy" — measuring "how wrong"

To train, we need a single number that says how far apart two distributions are. The building block is the logarithm (specifically , the natural log — the inverse of ).

Now the parent's loss line reads cleanly. Recall from §4 that is the student's probability for option : means: for every option , take the teacher's opinion , multiply by the student's surprise , and add them up. Small when the student's distribution matches where the teacher put its weight.


6. The leftover symbols: , , , and

  • — the hard target / true label as a list with a single : for a real cat, . This is a "one-hot" vector.
  • (alpha) — a mixing weight between and : how much to trust soft targets vs. hard labels. means "half and half".
  • (temperature) — the star of the show, defined and pictured in Softmax and Temperature. Here just know: is a positive number you divide every logit by before softmax, to flatten the distribution.
  • — the gradient: "if I nudge logit up a tiny bit, how much does the loss change?" This is what training uses to improve the student. The curly just means "rate of change with respect to one variable while holding the others still".

Prerequisite map

logit z_i - raw score

exponential e^x - make positive

summation - add all up

softmax - list of probabilities

distribution p and q

natural log ln

cross-entropy and KL - how wrong

hard label y - one hot

temperature T - flatten it

distillation loss

mixing weight alpha

gradient partial L

Knowledge Distillation

Every node above is a symbol the parent uses. Follow the arrows: scores become probabilities, probabilities feed a "how-wrong" measure, temperature reshapes them, and the mixing weight and gradient assemble the final distillation loss.


Connections

  • Next tool up: Softmax and Temperature — the knob in full.
  • The loss's measuring stick: Cross-Entropy and KL Divergence.
  • Where distillation sits among cousins: Model Compression, Pruning and Quantization.
  • Real distilled model: DistilBERT.
  • How teachers generate training data: Instruction Tuning, and the reuse-of-knowledge idea in Transfer Learning.

Equipment checklist