A base model just continues
A freshly pretrained model (a base model) has learned only one thing: to continue texts plausibly. Write “Why is ice cold?” and it may carry on with another question, a forum post or an explanation, whatever was typical after such a line in its training texts. It doesn't know we expect a helpful answer.
That is what supervised fine-tuning, SFT for short, is for: we keep training, this time with examples of a question and the desired answer. That is roughly how GPT-3 became ChatGPT: with many carefully written examples and afterwards training with human ratings (more on that in chapter 28).
Realistic expectations: SFT teaches the model format, tone and answering behavior. It doesn't conjure up knowledge the model didn't learn in pretraining. From a hundred question-answer pairs, a small model mainly learns to repeat those hundred answers. That is why we always also test with newly phrased questions.
The chat template
So the model knows who is speaking, every conversation gets a fixed structure of special tokens:
BOS USER Frage EOS ASSISTANT Antwort EOSOptionally a system instruction comes first: SYSTEM Text EOS right after BOS. The role markers are inserted during data preparation, as IDs of their own that can't occur in normal text (chapter 10).
When using the model, the prompt ends after ASSISTANT. The next token the model produces is then the first word of the answer. And because it has learned to end answers with EOS, it also knows when to stop. Important: training and use must use exactly the same template; otherwise a small model in particular gets confused quickly.
Only the answer is learned
Now the most important trick of the chapter. The model should read the question but not learn to write questions. So only the answer counts for the error. The positions of the question and the role markers get the target −100, “ignore”, which you know from chapter 14. The answer and the final EOS stay active.
The question doesn't disappear: attention can read it as normal. It just doesn't produce any error of its own. Like a student who gets exercises with solutions for the exam but is graded only on their own solutions.
One detail: the mask also has to be shifted by one along with the target shift. The output at the ASSISTANT marker predicts the first answer token, so it has to be active.
targets = ids[1:].copy()
active = mask[1:]
targets[~active] = -100A concrete format
{"messages":[{"role":"user","content":"Why does ice melt?"},{"role":"assistant","content":"Ice melts when enough heat is added to it."}]}Big companies use tens of thousands of such examples, often written by people. In step 7 of part VII you build your conversations yourself, from stories. The tokenizer from pretraining stays unchanged.
Your SFT training in part VII adjusts all weights. With large models people often use LoRA: instead of all weights, only small extra tables are trained, which saves an enormous amount of memory. For our small model that isn't necessary.
Note the smaller learning rate: we want to fine-tune, not throw everything learned overboard.
Don't cut off examples that are too long
If an example is longer than the context window, you could simply shorten it. But then the entire answer might fall away, and there is nothing left to learn. That is why our project throws such examples out and reports how many. If nothing is left at all, it stops with an error message.
Padding and fair weighting
SFT examples have different lengths. Shorter ones are padded with PAD, and their targets are −100. Thanks to the causal mask, the padding at the end bothers no one.
During evaluation, every answer token counts the same. Otherwise a one-word answer would count as much as a whole paragraph.
The examples shape the behavior
What the model sees, it imitates. If you want short answers in a certain language, you need short example answers in that language. Include different phrasings of questions, explanations, small text tasks, and cases where the right answer is “I don't know”. Proofread the answers yourself. Contradictory examples quickly confuse a small model.
Tailoring your model to a narrow topic is perfectly fine; then call it a topic model, though, and check how it reacts to questions outside that topic.
Comparing before and after
Ask the base model and the SFT model the same questions, fixed in advance. Rate format, readability, correctness and honest not-knowing separately. A nicer answer format with just as many factual errors is real progress, but only in format.
Key points
- A base model continues text. SFT trains it with question-and-answer examples to answer.
- A chat template with role markers shows who is speaking. Training and use need the same template.
- Learning happens only on the answer. The question is read but excluded from the error with −100.