← All posts

Model development

Post 14 of 26

Supervised fine-tuning: teaching a base model to answer

Posts in this series · 26

A pre-trained model has read trillions of tokens, and it has learned to continue text. Give it “What is the capital of France?” and it may answer, or it may continue with “What is the capital of Germany?”, because lists of questions are common on the web. Nothing in pre-training told it that a question is a request.

Supervised fine-tuning (SFT) teaches the missing behaviour with examples: conversations in which a user asks and an assistant answers well. The surprising part is how little changes mechanically. It is the same next-token loss as the first post, on a different kind of text, with a few decisions about which tokens count. Those decisions are what this post is about. You need only the first post to follow it; nothing from the distributed-training posts is required.

A conversation becomes text: the chat template

A model reads one sequence of tokens, so a conversation must be flattened into one. A chat template does it: it wraps each message in role markers and ends each assistant turn with a stop token. The released Tülu 3 tokenizer renders a two-message conversation as

<|user|>
What is 2+3?
<|assistant|>
2+3 = 5.<|end_of_text|>

Three consequences follow:

Which tokens are trained: the loss mask

Usually the model is trained only to produce the assistant’s replies, not to predict the user’s messages. The labels for every other token are set to −100, the ignore value from the first post, so they contribute no loss. For the conversation above, rendering it with the Tülu 3 tokenizer gives 25 tokens; the first 17 (the user’s message and the <|assistant|> header) are masked, and the 8 tokens of 2+3 = 5.<|end_of_text|> are trained. open-instruct, the code behind Tülu 3, builds exactly this kind of mask by rendering each prefix of the conversation and masking every non-assistant span, including the header that announces the assistant’s turn.

Two points from the first post carry over directly:

Try it: trace the labels. A conversation has a system message of 10 tokens, a user message of 20, an assistant reply of 30, another user message of 15 and a final assistant reply of 40. Each assistant reply ends with one stop token. How many tokens are trained with assistant-only masking? And if only the last turn is trained?

Answer. All assistant turns: (30+1)+(40+1)=(30 + 1) + (40 + 1) = 72 tokens, the replies and their stop tokens; the 45 tokens of system and user messages and every role header are masked. Last turn only: 41. Training only on the last turn treats earlier replies as context, which is useful when they came from a weaker model; training on all turns uses more of the data.

How the loss is averaged

Conversations have very different reply lengths, so averaging matters as much here as it did for packed pre-training rows. Suppose one conversation has a 100-token reply with mean loss 1.0 and another a 10-token reply with mean loss 2.0:

The Tülu 3 team ran into exactly this. Their models trained on GPUs underperformed others, and the cause was loss aggregation in Hugging Face Transformers: with gradient accumulation each sample was averaged separately, so samples, not tokens, were weighted equally, and changing the accumulation steps changed the weighting. They switched to a summed loss, which weights every token equally, retuned the learning rate, and found it generally better (§4.3.2). It is the same trap as mean-then-divide accumulation in the first post and per-rank averaging in the data-parallelism post.

What the data teaches

The mechanism is simple; the data is where the work is. Tülu 3 is unusually informative because it published ablations of its own mix for Llama 3.1 8B (§4.2, Table 10):

Removed from the mixAverageMost affected
nothing (full mix)60.1—
WildChat (diverse real conversations)58.9AlpacaEval 2: 12.4 → 7.5
safety data58.0safety: 93.1 → 74.7, others nearly unchanged
math data58.2GSM8K: 76.2 → 64.1; MATH: 31.5 → 23.5

Three lessons generalize:

Fine-tuning is short and sensitive. Tülu 3’s 8B model trained for 2 epochs at a learning rate of 5×10−65 \times 10^{-6}, with an effective batch of 128 sequences of up to 4,096 tokens (Table 11). Changing only the random seed moved the average between 59.8 and 59.9, and averaging the weights of two runs (“model soup”) gave 60.2, not reliably better than the best single run (Table 14). Differences smaller than a few tenths of a point are within noise.

The base model still sets the ceiling. The same SFT mix gave 31.5 on MATH from Llama 3.1 8B but 56.4 from Qwen 2.5 Math 7B, a model whose pre-training emphasized mathematics (Table 12). SFT draws out and formats what pre-training learned; it adds less than it seems.

Everyday picture: SFT is an apprenticeship by example. The apprentice already knows a great deal; watching how answers are given teaches them the shop’s format and manners, not the trade itself.

What can go wrong

What to remember

Sources

Further reading