Skip to content

5.8 Supervised fine-tuning

ready≈ 14 hoursNeeds: 4.6Unlocks: 5.9, 5.12
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Loss-mask viewer)
  4. 4 Do
  5. 5 Check

From continuation to conversation. A pretrained model continues whatever text it is given. Asked a question, it may answer, or write five more questions. Supervised fine-tuning trains it on example conversations so that after a user’s message it produces a helpful reply and then stops. InstructGPT began this way, with demonstrations written by labellers (Ouyang et al. 2022, section 3).

Templates. A conversation becomes one token sequence, with special tokens marking each turn. nanochat uses <|user_start|>, <|assistant_start|> and their end tokens (nanochat); other model families use their own, and a model only works with the template it was trained on (Hugging Face chat templates). At inference the prompt ends with <|assistant_start|>, and generation stops at <|assistant_end|>.

Masking. The model reads the whole conversation, but the loss counts only the assistant’s tokens, including the end token: otherwise it would learn to imitate users, and might never learn to stop. Masked targets are set to −1 and skipped, and so are padded positions. Averaging over the counted tokens of a batch weights long replies more than short ones, which some implementations correct (CS336 Assignment 5 builds this response mask and loss).

Small, clean data. SFT changes style and format more than knowledge. A thousand carefully chosen examples can produce a capable assistant from a strong base model (Zhou et al. 2023, LIMA). nanochat’s SFT stage mixes conversations, multiple-choice questions and arithmetic in a few thousand steps.

What the loss mask does to the gradientstep 1 of 4
  1. L=1n∑t∈M−log⁡softmax⁡(zt)yt,n=∣M∣\mathcal{L} = \frac{1}{n}\sum_{t \in M} -\log \operatorname{softmax}(z_t)_{y_t}, \qquad n = |M|

Loss-mask viewer

How SFT data is formatted

Static view on small screens. Open this page on a laptop to interact.

conversation:
loss on:
bos ↓ user_start
user_start ↓ B
B ↓ e
e ↓ ␣
␣ ↓ b
b ↓ r
r ↓ i
i ↓ e
e ↓ f
f ↓ .
. ↓ ⏎
⏎ ↓ ⏎
⏎ ↓ C
C ↓ a
a ↓ p
p ↓ i
i ↓ t
t ↓ a
a ↓ l
l ↓ ␣
␣ ↓ o
o ↓ f
f ↓ ␣
␣ ↓ F
F ↓ r
r ↓ a
a ↓ n
n ↓ c
c ↓ e
e ↓ ?
? ↓ user_end
user_end ↓ asst_start
asst_start ↓ P
P ↓ a
a ↓ r
r ↓ i
i ↓ s
s ↓ .
. ↓ asst_end
asst_end ↓ user_start
user_start ↓ A
A ↓ n
n ↓ d
d ↓ ␣
␣ ↓ J
J ↓ a
a ↓ p
p ↓ a
a ↓ n
n ↓ ?
? ↓ user_end
user_end ↓ asst_start
asst_start ↓ T
T ↓ o
o ↓ k
k ↓ y
y ↓ o
o ↓ .
. ↓ asst_end
60 tokens rendered; the loss counts 14 of 59 targets (24%)
tmpl template token u user a assistant t target counted t target ignored (−1)
one turnsystem + two turnsnon-ASCII
batch: 24 of 177 target positions count; padding (hatched) is always ignored

The model reads the whole conversation but is graded only on what the assistant should say. Each input token (top) is used to predict the next token (bottom); targets outside the assistant's replies are set to −1 and skipped by the loss. Training on every token also teaches the model to write the user's questions and the template, wasting capacity. Leaving out the end token is a classic bug: the model never learns to stop and keeps writing after its answer. In a batch, shorter conversations are padded on the right; padded targets are ignored too.

A conversation rendered as tokens: each input token, coloured by role (template, user or assistant), above the next token it must predict, which is outlined if the loss counts it and struck through if its target is −1. Buttons choose one of three conversations (one turn, a system message with two turns, and one with non-ASCII bytes) and what the loss covers: only the assistant’s replies, every token, or the replies without their end token. A readout counts the trained targets. Below, the three conversations collated into one padded batch, with counted, ignored and padded positions. Keyboard: Tab to the buttons and press Enter.
Chat template, loss mask and masked cross-entropy stubs/m5_8_sft.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m5_8_sft.py and run mlc check 5.8. The tests are identical; import the results on the progress page.

Hints · render_chat 0/3 revealed
Hints · sft_targets 0/3 revealed

Then fine-tune your own GPT. Open notebooks/5.8-sft.ipynb on Kaggle with a GPU and paste in your 4.6 GPT and your 5.8 file. It pretrains your GPT on TinyStories bytes, then fine-tunes it on generated tasks with checkable answers (reverse a word, uppercase it, count its letters, add two numbers) and requests for stories, rendered with your template. It compares the base model, SFT with your mask and SFT on every token: how often each answers correctly on words it never saw, and how often it stops.

1. Why mask the user's tokens out of the SFT loss?
2. What goes wrong if the <|assistant_end|> token is masked out?
3. Two conversations in a batch have 4 and 40 counted tokens. With the loss averaged over all counted tokens, the long one gets…
4. What does SFT mostly change?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.