Supervised fine-tuning on demonstrations
Fine-tune a base model on (prompt, ideal response) pairs, computing the loss only on response tokens. Explain why a small, high-quality set mainly teaches format and persona rather than new knowledge.
Builds on: Base models continue documents, Shifted targets and parallel training, The LLM training loop
What the animation says
From continuer to assistant
A base model only continues documents. Hand it a question, and it may just write more questions. An assistant answers in a steady voice, follows a format, and stops at the right point. To get there, labs write maybe ten to a hundred thousand examples, after pretraining on trillions of tokens. Each example is a prompt and an ideal reply, glued into one document with a fixed layout: User, then Assistant. We train with the usual next-token loop. But which predictions should the model be graded on?
Only the reply is graded
Only the reply counts. Here is our tiny document: User, hi, Assistant, Hello, exclamation mark, end. Six tokens. Each position predicts the next one, so positions one to five have a target. The end token belongs to the reply. It teaches the model when to stop. A position is scored when its target is a reply token. Watch the target column, not the input. Your turn: drag the divider so only the scored positions sit below it.
Masked loss, by hand
So the mask is zero, zero, one, one, one. The model gives each true next token a probability: point one, point zero five, point five, point eight, point nine. Each position's loss is minus the log of that probability. Confident and right means a small loss. Average only the three scored positions: about point six nine, point two two and point one one. That's roughly point three four. Now remove the mask and average all five positions. What happens to the loss?
The mask in PyTorch
It nearly quadruples, to about one point two six. Without the mask, guessing the user's words dominates. In code, the mask is one line. Shift the tokens left to get the targets, as usual. Then this line sets the prompt targets to minus one hundred. Cross entropy skips every target equal to that ignore index and averages over the reply tokens only. Gradients still flow back through attention into the prompt. So the model learns to use the prompt, just never to predict it.
Why so few examples work
So supervised fine-tuning is plain next-token loss on demonstrations, with the prompt masked out. Why do so few examples change behaviour so much? Style, format and persona show up in every single example. Thousands of repeats push them hard. A new fact appears once or twice. That's too little signal to store it reliably. So fine-tuning mostly changes how the model answers, not what it knows. The knowledge came from pretraining.
Same loop, but only the reply is graded
The masked next-token loss
Masked vs unmasked loss, by hand
Loss masking in PyTorch
import torch, torch.nn.functional as F
V = 10
ids = torch.tensor([[3, 7, 1, 4, 9, 2]]) # [B=1, T=6]; first 3 = prompt
prompt_len = 3
inputs, targets = ids[:, :-1], ids[:, 1:].clone() # shift left
targets[:, :prompt_len - 1] = -100 # targets x2..x3 are prompt: no loss
logits = torch.randn(1, 5, V, requires_grad=True) # stand-in for model(inputs)
loss = F.cross_entropy(logits.reshape(-1, V), targets.reshape(-1),
ignore_index=-100) # mean over response tokens only
loss.backward()
print(loss.item())
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses