Sub topic 12
From Base Model to Assistant: Instruction Tuning and Preference Tuning
This chapter assumes you know the pretraining objective (next-token cross-entropy), how a decoder-only model produces a probability distribution over tokens, and how sampling picks the next token. Here we cover what happens after pretraining to turn a text continuer into an assistant.
Why a base model is not an assistant
A pretrained base model is trained to continue text that looks like its training data. Given the prompt "What is the capital of France?", it may answer, but it may also continue with more quiz questions, because lists of questions appear often on the web. Nothing in the pretraining loss rewards being helpful, following a request, or refusing a harmful one.
Post-training changes that. It starts from the pretrained weights and trains further on much smaller, curated data. Two stages are common: instruction tuning, then preference tuning.
Instruction tuning
Instruction tuning, also called supervised fine-tuning (SFT), trains on pairs of a prompt and a good response written by people or curated from other sources. The format is usually a conversation with role markers, such as system, user, and assistant, flattened into one token sequence by a chat template.
The loss is the same next-token cross-entropy as in pretraining. Two details differ:
- The loss is usually computed only on the response tokens. Prompt tokens are masked out, so the model learns to produce answers and not to imitate user messages.
- The dataset is small, often thousands to a few hundred thousand examples. The model mostly learns a format and a behavior. Most of its knowledge was already acquired in pretraining.
A common error is to treat SFT as a way to add large amounts of new facts. It works poorly for that. It mainly shapes style, format, and which existing capabilities the model uses.
Preference tuning
Demonstrations are expensive, and for many tasks it is easier to judge two answers than to write a perfect one. Preference tuning uses that. For each prompt, the data holds a chosen response and a rejected response, ranked by human raters or by another model.
There are two main approaches:
| Approach | How it works | Trade-off |
|---|---|---|
| RLHF (reinforcement learning from human feedback) | Train a reward model on the comparisons, then optimize the language model to score highly on it with a reinforcement learning algorithm such as PPO | Flexible, but has more moving parts and can be unstable |
| DPO (direct preference optimization) | Skip the reward model and train directly on the pairs with a classification-style loss | Simpler and cheaper, but offline, so it only learns from the fixed pairs |
Both keep the tuned model close to the starting model. RLHF typically adds a KL penalty, which measures how far the new token distributions drift from the SFT model's. Without it, the model can find odd outputs that exploit weaknesses in the reward model, a failure called reward hacking.
A minimal DPO-style loss
The DPO loss rewards raising the chosen response's log-probability relative to a frozen reference model, and lowering the rejected one's.
import numpy as np
def dpo_loss(logp_chosen, logp_rejected, ref_chosen, ref_rejected, beta=0.1):
# Each argument is the summed log-probability of a full response.
margin = (logp_chosen - ref_chosen) - (logp_rejected - ref_rejected)
return float(np.log1p(np.exp(-beta * margin)))
# The model already favors the chosen answer more than the reference does.
print(dpo_loss(-10.0, -14.0, -11.0, -12.0))
# The model favors the rejected answer more than the reference does.
print(dpo_loss(-14.0, -10.0, -12.0, -11.0))The first call prints a lower loss than the second. The parameter beta controls how strongly the model is allowed to move away from the reference.
Common errors and side effects
- Overfitting to the format. Too many epochs on a small SFT set can make the model repeat the training style or lose general ability.
- Sycophancy. If raters prefer agreeable answers, the model learns to agree with the user even when the user is wrong.
- Verbosity bias. Raters and reward models often favor longer answers, so tuned models drift toward length.
- Reduced calibration. Preference-tuned models are often more confident in their stated answers than their underlying token probabilities justify.
- Alignment is shallow in places. Post-training shifts behavior, but it does not remove knowledge learned in pretraining.
Exercise
In instruction tuning, why are the loss values usually computed only on the response tokens and not on the prompt tokens?
Show answer
The goal is to teach the model to produce good answers given a prompt. Including prompt tokens in the loss would also train the model to predict user messages, which is not the behavior we want from an assistant.
Exercise
What does the KL penalty in RLHF protect against, and what is that failure called?
Show answer
It keeps the tuned model's output distributions close to the starting model. Without it, the model can exploit weaknesses in the reward model to get high scores with low-quality outputs, which is called reward hacking.