Section 06

The SFT stage in practice

Demonstrations, chat templates, and data quality

Paper: Training language models to follow instructions with human feedback (InstructGPT) — Ouyang et al., 2022

The previous chapter established why instruction tuning works. This one is about the unglamorous machinery of actually doing it in a production pipeline, the part labeled “Step 1: SFT” in every modern post-training diagram. There is very little new math here. The interesting questions turn out to be about data and plumbing: what format you feed the model, which tokens you compute loss on, and how few examples you can get away with.

SFT is just next-token training, on curated data

When InstructGPT (Ouyang et al., 2022) laid out the influential three-step recipe (SFT → reward model → RL optimization), the first step was deliberately ordinary. Supervised fine-tuning takes the pre-trained base modelbase modelA model straight out of pre-training — a powerful text continuator that has not yet been taught to follow instructions, hold a conversation, or refuse harmful requests.See in glossary → and continues training it with the same basic autoregressive next-token predictionnext-token predictionThe pre-training objective for GPT-style models: given the tokens so far, predict a probability distribution over the next token. Also called causal or autoregressive language modeling.See in glossary → machinery, maximizing the likelihoodlikelihoodThe probability a model assigns to observed data. Supervised fine-tuning maximizes the likelihood of human-written target responses given their prompts.See in glossary → of target text.

The only thing that changes is the diet. Instead of raw web text, the model sees curated (prompt, response) demonstrations: high-quality examples of a prompt followed by the ideal answer, often written or edited by human labelers. For a demonstration with prompt xx and response y=(y1,,yT)y = (y_1, \ldots, y_T), the loss is the familiar negative log-likelihood:

LSFT=t=1Tlogpθ(ytx,y<t)\mathcal{L}_{\text{SFT}} = -\sum_{t=1}^{T} \log p_\theta\big(y_t \mid x,\, y_{<t}\big)

That is it. No reward, no sampling, no RL. SFT is teaching by demonstration: here is a good answer; make answers like this more probable.

Look closely at what that sum runs over, though: only the response tokens y1,,yTy_1, \ldots, y_T, never the prompt. In practice this is enforced by loss masking. The full sequence (system + user + assistant turns) is fed through the model so it can condition on the whole context, but the prompt positions are excluded from the loss. The model is graded only on the tokens it is supposed to produce (the assistant’s reply), not on the tokens it was given. We want it to learn to answer, not to predict the user’s question; computing loss on the prompt would spend capacity modeling user inputs and reward the model for memorizing prompts instead of responding well. Masking points the entire training signal at the behavior we actually care about.

The chat template: turning a document model into a conversation

A base model only knows how to continue a document. To make it act like a turn-taking assistant, we need a fixed convention for marking who is speaking: a chat templatechat templateThe fixed formatting (with special tokens marking roles like system/user/assistant) that turns a multi-turn conversation into the single token stream a model is trained and served on.See in glossary →. The template wraps each message in special tokensspecial tokensReserved tokens (e.g. role markers and end-of-turn markers) added to the vocabulary to delimit structure that ordinary text tokens cannot express.See in glossary →: reserved symbols, added to the vocabulary, that act as role and turn markers the model learns to recognize and emit.

A typical exchange, serialized, looks like this:

That opening <|system|> block is the system promptsystem promptA special leading instruction that sets the assistant’s persona, rules, and constraints for a conversation, separate from the user’s turns.See in glossary →: an instruction, often hidden from the end user, that sets persona, rules, and constraints for the conversation. Training and template conventions can make models more likely to follow it over user instructions, but this is a learned behavior rather than a hard security boundary; product-level controls may also be needed.

Loss masking: only learn the assistant’s words

One important practical detail is loss masking. The serialized example above contains the system prompt, the user’s question, and the assistant’s reply. Computing language-modeling loss over all tokens trains the model to model the entire transcript; many chat-SFT pipelines instead focus the loss on text the assistant should produce.

The fix, as we explained briefly above, is loss masking (sometimes “prompt masking”): you compute the next-token loss only on the assistant’s response tokens, and mask out (zero the loss on) the system and user tokens. The prompt tokens are still fed in as context (the model conditions on them), but they contribute nothing to the gradient. Concretely, the masked objective trains only on the spans the assistant is responsible for producing:

LSFT=tassistantlogpθ(ytcontext<t)\mathcal{L}_{\text{SFT}} = -\sum_{t \in \text{assistant}} \log p_\theta\big(y_t \mid \text{context}_{<t}\big)

Assistant-only masking is common rather than universal: the best choice depends on the data format, objectives, and model. It concentrates the supervised signal on assistant behavior while retaining the prompt as conditioning context. This masking is also one concrete line between SFT and mid-trainingmid-trainingA phase between the main pre-training run and post-training, used to inject specialized data or capabilities (e.g. long context, code-from-execution) while still training the base model on a next-token-style objective.See in glossary →: mid-training is still full-sequence language modeling (loss on every token), whereas SFT typically masks the loss down to just the assistant’s reply.

Data quality over quantity

The instinct from pre-training (more tokens is better) does not carry over cleanly to SFT. The landmark demonstration was LIMA (“Less Is More for Alignment,” Zhou et al., 2023): the authors fine-tuned a strong base model on just 1,000 carefully curated prompt–response pairs and got a model competitive with ones tuned on hundreds of thousands of examples.

LIMA supports the hypothesis that a strong base model can benefit greatly from a small, carefully curated alignment set. It does not establish a universal data law: SFT can also teach new facts, skills, tools, and formats, and the useful data scale depends on the base model and target behavior. In practice, high-quality examples often outperform a comparable amount of noisy data, but larger diverse datasets can also help.

With the supervised stage understood, the next question is where all those high-quality demonstrations come from when you don’t have an army of labelers. That is the story of synthetic and self-generated data, the next chapter.