Step 13 Β· Teaching it to follow instructions
A base model completes text. It does not answer questions. Supervised fine-tuning is the step that turns one into the other.
In 60 seconds
Step 13 Β· Teaching it to follow instructions
A base model completes text. It does not answer questions. Supervised fine-tuning is the step that turns one into the other.
The data format
example = {
"messages": [
{"role": "system", "content": "You are a careful assistant."},
{"role": "user", "content": "Why is the sky blue?"},
{"role": "assistant", "content": "Sunlight contains all colours. Air "
"scatters short wavelengths more than long ones, so blue light bounces "
"around the sky and reaches your eyes from every direction."}
]
}
# Rendered with the model's chat template, then tokenised. Each role turn
# is wrapped in special tokens, and the newlines matter:
#
# <|im_start|>system + newline + You are a careful assistant. + <|im_end|>
# <|im_start|>user + newline + Why is the sky blue? + <|im_end|>
# <|im_start|>assistant + newline + Sunlight contains... + <|im_end|>
#
# Use tokenizer.apply_chat_template() rather than building this by hand --
# every model family differs, and a mismatch degrades quality silently.def build_labels(input_ids, assistant_spans, ignore=-100):
"""Only the assistant's own tokens contribute to the loss."""
labels = torch.full_like(input_ids, ignore)
for start, end in assistant_spans:
labels[start:end] = input_ids[start:end]
return labels
# cross_entropy ignores -100 by default, so the model is never
# trained to generate the user's questions back at you.Quality beats quantity, dramatically
| Dataset | Size | Result |
|---|---|---|
| Scraped chat logs | 500,000 | Mediocre β inconsistent style, factual errors learned as targets |
| Carefully curated | 1,000β10,000 | Often better on every axis |
| Curated + verified synthetic | 20,000β100,000 | The current mainstream recipe |
| Your own domain examples | 200β2,000 | Excellent for narrow, format-heavy tasks |
Hyperparameters that differ from pretraining
- Learning rate 10β100x lower β typically 1e-5 to 2e-5 for full fine-tuning, 1e-4 to 2e-4 for LoRA.
- 1β3 epochs only. More and you get memorisation and a sharp drop in general ability.
- Small batches are fine β 32 to 128 sequences is normal.
- Watch for catastrophic forgetting. Evaluate on general benchmarks, not only your task. A model that got great at your format and forgot how to reason is a bad trade.
What SFT can and cannot fix
- Output format and structure.
- Tone, persona and length.
- Domain-specific phrasing and jargon.
- Reliable tool-call formatting.
- Refusal behaviour on a defined policy.
- Adding facts β use retrieval instead.
- Fixing reasoning β that comes from pretraining scale and Module 51.
- Keeping up with changing information.
- Anything you could achieve with a better prompt, which you should try first.
Watch and read more
Lab
An SFT run where the loss mask is the whole lesson.
The problem
labels = input_ids.clone()
labels[~assistant_mask] = -100 # cross_entropy ignores -100
# Run once without this line. Read what the model generates. That is the lesson.You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Your fine-tune is excellent on your task and much worse at general reasoning. Diagnose and fix.Reveal
Questions people ask
How many examples do I need?
For a narrow format task, 200β1,000 well-made examples often get you most of the way. For a general assistant, tens of thousands. Start small, evaluate, and only add data where evaluation shows a gap.
Full fine-tune or LoRA?
LoRA for nearly everyone β Module 52. Full fine-tuning is worth it when you have a lot of data, need a large behaviour shift, and have the memory for it.
How do I know it worked?
A held-out set of your real task, scored by a rubric or a human, plus a general benchmark to detect forgetting. Training loss going down tells you almost nothing about whether the model got better at the job.
Can I fine-tune on my company documents?
You can, and it usually disappoints. The model picks up style but recalls facts unreliably and cannot be updated when documents change. Retrieval is the right tool for knowledge; fine-tuning is the right tool for behaviour.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.
Sign in with your phone number to take the test and save your progress