5.8 Supervised fine-tuning
1. Learn
Section titled “1. Learn”- Stanford CS336: Language Modeling from Scratchlecture 15 "Alignment - SFT/RLHF" (May 20, slides), the SFT part
- Karpathy, nanochatREADME and the SFT stage scripts
2. Bridge
Section titled “2. Bridge”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.
3. Explore
Section titled “3. Explore”stubs/m5_8_sft.pyLoading 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.
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.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
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.