4.3.10 · D1Pretraining & Fine-Tuning LLMs

Foundations — Instruction tuning

2,245 words10 min readBack to topic

Before you can read the parent note, you need to be fluent in a small pile of symbols and pictures. This page builds every single one of them from zero, in the order they depend on each other. Nothing here assumes you have seen probability, logarithms, or subscripts before.


1. A token — the atom of everything

Think of a sentence being chopped into LEGO bricks. "Add 2 and 3" might become the bricks Add, 2, and, 3. Each brick is a token.

Figure — Instruction tuning

Why do we need this idea? Because every later symbol (, the product, the loss) is defined per token. If you don't picture the sentence as a row of numbered bricks, none of the subscripts will mean anything.


2. The subscript — "the token at position "

is short for "time step" — it moves left to right like a clock ticking once per brick. So is the first brick, the second, and the last (we use a capital for the total number of tokens in the sequence — the final tick).


3. The shorthand — "everything before slot , in order"

Order matters here — "dog bites man" and "man bites dog" are the same bricks in a different order and mean opposite things, so is a sequence, never a shuffled pile. Picture a cursor blinking between two bricks. Everything to the left of the cursor, read in order, is ; the very next brick you are about to guess is .

Figure — Instruction tuning

Why the topic needs it: the whole job of a language model is "guess given ." That phrase — the next brick given all previous bricks in order — is the seed of every formula on the parent page.


4. Probability , and conditional probability

Picture the model as a weather forecaster. It doesn't say "the next word IS the." It says "given everything so far, the has a chance, a has , cheese has …" — a whole spread of guesses summing to .

Figure — Instruction tuning

The parameters (Greek "theta") are just all the tunable knobs inside the model — the billions of numbers that decide those probabilities. Writing reminds us "this forecast depends on the current knob settings." Training = turning the knobs.


5. The big sum and the big product

We need both: builds the probability of a whole sentence (multiply the guesses), and builds the loss (add up the surprises). Introducing them together now means no summation symbol will surprise you later.

The parent writes the sentence probability with :

Read it in plain words: "the chance of the whole sentence = (chance of brick 1) × (chance of brick 2 given brick 1) × (chance of brick 3 given bricks 1–2) × …". You build the sentence one guess at a time and multiply the guesses.


6. The logarithm — turning products into sums

So, using the magic property term-by-term: The scary product became a tidy sum.


7. Negative log-likelihood — the loss (and cross-entropy)

We said is always . If the model is confident and correct, and — tiny loss. If it's confident and wrong, and — huge loss.

We want a number that is big when wrong, small when right, so we flip the sign. First the raw quantity, the total negative log-likelihood (NLL) — literally the summed surprise over every token:

This total grows just because the sentence is long, which makes long and short examples hard to compare. So we usually report the average NLL — divide by the number of tokens counted:


8. The mask — grade only the answer

Now the one idea that makes instruction tuning different from plain pretraining.

Picture a highlighter running over only the answer half of each example. Highlighted = graded. Un-highlighted = ignored.

Figure — Instruction tuning

The parent's boxed formula is now fully readable — it is the average NLL from Section 7, but averaged over only the highlighted tokens:

  • — multiplying by deletes prompt tokens from the sum; multiplying by keeps response tokens.
  • — just counts how many tokens got highlighted (how many response tokens). Dividing by it gives the average surprise per answer-token, so a long prompt can't drown out a short answer.

How these feed the topic

Token = text brick

Position t and x sub t

x before t = ordered context

Probability p with given-bar

Product over positions = sentence prob

Log turns product into sum

Negative log sum = Cross-Entropy loss

Mask m = grade response only

Instruction Tuning objective

Everything on the left is a prerequisite for the parent topic, which lives at node I. The base guessing machine (nodes A–F) is exactly the Pretraining Objective (Next-token prediction); the mask (node H) is what turns that pretraining loss into instruction tuning.


Equipment checklist

Cover the right side and test yourself. You are ready when you can answer every line instantly.

What is a token?
One small chunk of text (a word or word-piece) the model reads/writes.
What does the subscript in point to?
The position/slot number of a token, counting left to right.
Read in plain words.
The ordered list of all tokens in slots before position (the context so far, order preserved).
What does the bar in mean?
"Given" — the probability of assuming the earlier tokens.
What is ?
All the tunable numbers (knobs/weights) inside the model.
What does tell you to do, and why here?
Multiply the per-token probabilities; because the sentence is right only if every token is right (chain rule).
What does tell you to do?
Add the listed terms from up to .
Why take the (base ) of the probabilities?
It turns a tiny product into a stable sum, since ; the loss is then in nats.
Why is the loss the negative log-probability?
So the number is small when the model is right (p near 1) and large when wrong (p near 0).
Total NLL vs. average NLL?
Total is the plain of surprises (grows with length); average divides by the token count for a comparable per-token number.
What is the mask ?
An on/off switch: for response tokens (graded), for prompt tokens (ignored).
What does count?
The number of response tokens — used to average the loss over just the answer.
In one line, what does instruction tuning change vs pretraining?
Same next-token loss, but masked so only the response tokens are graded.