4.3.10 · D3Pretraining & Fine-Tuning LLMs

Worked examples — Instruction tuning

3,226 words15 min readBack to topic

This page is a drill. We take the response-masked cross-entropy loss from the parent note (Instruction tuning) and push it through every kind of situation it can face: normal cases, all-zero-mask degeneracies, tiny-probability blow-ups, multi-example batches, and a couple of exam-style traps. Nothing here contradicts the parent — we just go slower and cover the corners.

Before anything, two plain-words conventions we lean on the whole page.

Why and not the raw probability? Two reasons we will see repeatedly below: (1) multiplying many probabilities under-flows to zero, and adding logs is the same thing but numerically safe — this is the logarithm-of-a-product identity , which turns a fragile product into a stable sum (used all through Cross-Entropy Loss); (2) turns "probability = perfect" into "loss = perfect", and "probability = disaster" into "loss ", which is exactly the pain signal we want.

Look at Figure s01 below to lock in that shape. Its horizontal axis is the model's probability for the correct token (running ); its vertical axis is the per-token loss in nats. Follow the cyan curve from right to left: at the right edge () it touches the floor at — a perfect, unsurprised model; as you move left () it climbs without bound. The two amber dots mark exactly those extremes, and the small white squares sit on the checked numbers we compute in Example 4.

Figure — Instruction tuning
Figure s01 — the loss curve : flat floor of at (right), blow-up toward at (left).


The scenario matrix

Every case class this loss can throw at you, and which worked example covers it:

# Case class What's tricky about it Covered by
A Normal response, several tokens the everyday case; compute + average Ex 1
B Confident vs. unsure model (high vs low ) how punishes low probability Ex 2
C Degenerate: mask all zero () division by zero — an undefined loss Ex 3
D Limiting value: loss ; why we clip Ex 4
E Limiting value: loss ; the "already perfect" corner Ex 4
F Masked vs unmasked, same sequence the number actually changes; by how much Ex 5
G Batch of multiple examples, unequal lengths how to average across examples fairly Ex 6
H Real-world word problem (perplexity of an assistant) translate loss ↔ perplexity Ex 7
I Exam twist: multi-token target word one "word" = several tokens; product identity Ex 8
J Exam twist: which tokens get gradient? conceptual sign check, no arithmetic Ex 9

We now hit all ten cells.


Worked examples


Recall

Recall Cover the answers
  • Does "log" in the loss mean base-10 or base-? ⟶ base- (natural log); the unit is the nat.
  • What is a nat? ⟶ the surprise-unit from using ; a uniform -way guess costs nat.
  • Mask is all-zero — what's the loss? ⟶ undefined (); skip the example.
  • As the per-token loss does what? ⟶ diverges to (so we clip at a tiny ).
  • As ? ⟶ approaches , the floor.
  • Unmasked loss vs masked on the same sequence — which is usually lower and is that good? ⟶ unmasked is lower (easy prompt tokens deflate it); it's the wrong metric.
  • Perplexity from a natural-log loss of ? ⟶ .
  • Word "Paris" = tokens Par·is with — word probability? ⟶ .

Connections

  • Instruction tuning — parent; this page drills its masked-loss formula.
  • Cross-Entropy Loss — the we averaged everywhere.
  • Pretraining Objective (Next-token prediction) — source of the chain-rule product in Ex 8.
  • Supervised Fine-Tuning (SFT) — where the masking convention lives.
  • Prompt Templates and Chat Formats — the ### Response: header that defines the mask boundary.
  • Self-Instruct and Alpaca — auto-built examples that must contain a non-empty response.
  • FLAN and Zero-shot Generalization — lower loss/perplexity → more decisive assistant.