Visual walkthrough — Supervised fine-tuning (SFT)
This is the visual companion to Supervised fine-tuning (SFT). If you have never seen probability multiplication or a logarithm used this way, start at line one — everything is earned.
Step 1 — A model is a machine that scores possible next words
WHAT. Feed the model the text "What is 2+2 ? ". It hands back a bar for every token in . The bar for "4" is tall; the bar for "banana" is a sliver.
WHY. Before we can say "learn to answer," we need to see what the model actually produces. It never produces a word directly — it produces a confidence for every possible word. That confidence is the only thing we can push on.
PICTURE. Each bar's height is — the model's probability for token . The heights sum to (they fill exactly one full unit of "belief").

In symbols, the distribution the model gives after prompt is written
- — the model (a function from text to a bar chart).
- — the model's parameters (all its tunable weights). Changing reshapes the bars.
- — a placeholder: "for every token ."
- — "given that the text so far is ." The bars depend on what came before.
Step 2 — "Good answer" means: make the right bar tall
WHAT. Point at the one bar the human's demonstration says is correct. Call its height — the probability the model assigned to the actual next token .
WHY. We are doing behavioral cloning: copy the human. The only way a next-token machine can "copy" is to route its probability mass onto the token the human actually chose. So the whole of SFT is: lift the correct bars.
PICTURE. One bar glows (the human's token). We want to grab its top and pull it toward ; the other bars shrink to compensate because the total is fixed at .

- — the human's chosen token at position .
- — its bar height, the number we push up.
Step 3 — An answer is a chain of these choices
WHAT. The response "4" then "<eos>" is two bar-charts in a row. Chart 1 (after the prompt) scores "4". Chart 2 (after prompt and "4") scores "<eos>" — the "I'm done" token.
WHY. We cannot judge an answer with a single bar; a good answer is a good path of choices. Every step's chart depends on the tokens already emitted — that's what "autoregressive" means.
PICTURE. A conveyor belt: each stop is a fresh bar chart, and the token chosen at one stop is fed forward as input to the next.

The probability of the whole response is the probability of taking every correct step. Independent-looking steps chained by "and" multiply — this is the chain rule of probability:
- — multiply over all positions of the answer.
- — "all response tokens before position " (what the belt already emitted).
- The product is the height of the correct bar at step 1, times step 2, times… — the chance the model walks the entire human path.
Step 4 — Multiplying tiny bars is dangerous; take a log
WHAT. Apply to both sides. The product becomes a sum:
WHY. Two payoffs. (1) Numerical safety: sums of manageable numbers instead of a vanishing product. (2) is monotonic (always increasing) — see the picture — so whatever makes the product biggest also makes its log biggest. Maximizing the log is the same problem, just easier.
PICTURE. The curve climbs forever without ever going back down. So "highest product" and "highest log of that product" sit at the exact same input — swapping to log changes the scale, never the winner.

- — add up the per-step scores.
- — the log-height of one correct bar. Since bar heights are between and , their logs are (negative or zero).
Step 5 — Flip the sign: from "reward" to "loss"
WHAT. Negate and average over the dataset :
WHY. is exactly the Cross-entropy loss between "the truth is token " (a single tall spike) and the model's bar chart. It is when the correct bar is at height (perfect) and shoots to as that bar drops to (surprised, wrong). Minimizing it is lifting the correct bars — the goal from Step 2.
PICTURE. The curve : near it kisses zero (no penalty for confident-correct); near it rockets up (huge penalty for confident-wrong). This asymmetry is what makes the model care about not missing the right token.

- — the flip: maximize-likelihood becomes minimize-loss.
- — average over all example pairs in the dataset.
- Inside the brackets: the negated log-height sum — one number measuring "how surprised the model was by this human answer."
Step 6 — The mask: grade only the answer, never the question
WHAT. Slot into the sum and divide by the number of graded tokens:
WHY. The prompt is given by the user — teaching the model to predict the question wastes capacity and can hurt answers. And dividing by (the answer length) means a long answer and a short answer are scored on the same scale, so the loss doesn't just reward brevity or verbosity.
PICTURE. A strip of tokens: prompt cells greyed (switch off, ), answer cells lit (switch on, ). Only the lit cells send gradient back into . The <eos> cell is lit too — that's how the model learns when to stop.

- — the on/off switch per token.
- — count of answer tokens (the divisor = the average).
- Everything else is identical to Pretraining of LLMs; only the data and this mask differ.
Step 7 — Edge cases: does the formula survive the corners?
Case A — the model is already perfect ( for every answer token). Then at every step, so . Nothing to fix — the valley floor. ✓
Case B — the model is catastrophically wrong (). Then . One confidently-missed token can dominate the whole loss — which is correct: assigning zero probability to what actually happened is the worst possible mistake.
Case C — a one-token answer (, e.g. just "Yes"). The sum has one term; ; the average is just that single . No division-by-zero, formula holds. ✓
Case D — the whole prompt is masked ( for prompt): those terms contribute . They vanish cleanly; the model never gets gradient for the question. ✓ (If you ever masked out every token, and the average is undefined — so a valid SFT example must contain at least one response token.)
PICTURE. The curve again, with A pinned at the far right (, loss ) and B sliding off the left cliff (, loss ). The middle worked example () sits between them.

Worked check — the parent's tiny example, on the curves
Response "4" with , then "<eos>" with :
Read it off Step 7's curve: lands at height , at ; their average is the model's surprise per answer token. Gradient descent will push both bars up, shrinking both numbers next time.
The one-picture summary

Left to right, this single figure is the whole derivation: bars → chain → log-sum → sign-flip → mask. A raw score per token, chained across the answer, log-summed for stability, negated into a loss, and masked to the answer only.
Recall Feynman: the walkthrough in plain words
The model is a machine that, at every moment, holds up a bar chart guessing the next word. To teach it to answer, we point at the bar the human actually wrote and say "make that one taller." A full answer is a chain of such choices, so we multiply the correct-bar heights together — that's the chance it walks the whole human path. Those numbers get microscopic, so we take a logarithm, which turns the multiply into an add without changing which answer wins. Trainers only ever slide downhill, so we flip the sign: now "climb toward the right answer" becomes "descend into a valley." Finally we tape over the question tokens — we grade only the answer and the "I'm done" tag — and we average so long answers aren't punished for length. That masked, averaged, negated log-sum is the SFT loss — the exact same cross-entropy as pretraining, aimed at curated demonstrations. Compare with RLHF and Direct Preference Optimization (DPO), which come after and optimize preferences instead of imitating single demonstrations.