From Pretraining to Chat Model: SFT and RLHF
By the Katkat team · Updated · 8 min read
Quick overview
How a base model becomes a chat model: next-token pretraining, supervised instruction tuning, then RLHF or DPO, and what each stage changes.
After this lesson you should be able to explain how a raw text-predicting network becomes an assistant that follows instructions and behaves the way people prefer. You should be able to name the three stages (pretraining, supervised fine-tuning, preference optimisation), state the training objective of each, and say what each one does and does not change. You should also be able to compare RLHF with DPO and discuss their trade-offs.
Stage 1: Pretraining
Pretraining teaches the model language, facts and patterns by predicting the next token over a very large text corpus. For a sequence of T tokens, the loss is the average negative log-likelihood:
L = -(1/T) * sum over t of log p(x_t | x_1 ... x_{t-1})
This is cross-entropy against the true next token. Every position in every document gives a training signal, and no human labels are needed. That is why this stage can use trillions of tokens.
A useful sanity check is the starting loss. A randomly initialised model with a 32,000-token vocabulary predicts roughly uniformly, so its loss is about ln(32,000) ≈ 10.37 nats per token. Well-trained models end far below that, but the number shows what the loss measures.
The output is a base model. It is a very good document continuer, not an assistant. If you prompt it with "What is the capital of France?", it may answer, or it may continue with five more quiz questions, because quiz lists are common in web text. It has no notion of "the user asked, so I should reply". See Tokenization for how the text becomes the tokens being predicted.
Stage 2: Supervised fine-tuning (instruction tuning)
Supervised fine-tuning (SFT), also called instruction tuning, continues training the same network on a much smaller dataset of (prompt, ideal response) pairs. The objective is still next-token cross-entropy. Two details change.
- Format. Conversations are serialised with a chat template: special tokens that mark system, user and assistant turns, plus an end-of-turn token. The model learns to produce the end-of-turn token, which is how it knows when to stop.
- Loss masking. The loss is computed only on the assistant's tokens. The prompt tokens are context, not targets. If a prompt is 200 tokens and the response is 300, the loss is averaged over those 300 tokens only.
The data comes from human-written demonstrations, curated public datasets, or responses generated by another model and then filtered. Quality matters much more than volume at this stage. A few thousand to a few hundred thousand clean examples can change behaviour a lot. The full recipe is in Supervised Fine-Tuning: Data and Recipe.
SFT is imitation learning. The model copies the demonstrated behaviour. It cannot learn that response A is better than response B, because it only ever sees one "good" answer per prompt.
Stage 3: Preference optimisation
SFT can make a model follow instructions, but demonstrations are expensive and hard to write at the quality you want. It is usually easier for a person to say which of two answers is better than to write a perfect answer. Preference optimisation uses that cheaper signal.
The data is a set of triples: a prompt, a chosen response and a rejected response. Labelling and Preference Data covers how these are collected. There are two main ways to use them.
RLHF
Reinforcement learning from human feedback (RLHF) has two steps:
- Train a reward model. It is usually initialised from the SFT model, with a scalar output head. It is trained so the chosen response scores higher than the rejected one, using a pairwise (Bradley-Terry style) loss:
loss = -ln sigmoid(r_chosen - r_rejected). - Optimise the policy against the reward model. The model generates responses, the reward model scores them, and a reinforcement learning algorithm (PPO is the classic choice) updates the model to raise the score. A KL penalty keeps the policy close to a frozen reference copy, typically the SFT model:
objective = r(response) - beta * KL(policy || reference).
DPO
Direct preference optimisation (DPO) removes the explicit reward model and the sampling loop. It rewrites the RLHF objective so the policy can be trained directly on the preference pairs with a classification-style loss. It needs log-probabilities from both the policy and a frozen reference model. The implicit reward is beta * log(policy / reference).
Worked example: scale and loss values
How small is SFT compared with pretraining? A common rule of thumb for training compute is about 6 × parameters × tokens FLOPs. It is approximate, but fine for comparing stages. Take a 7B-parameter model.
- Pretraining on 2 trillion tokens: 6 × 7×10⁹ × 2×10¹² = 8.4×10²² FLOPs.
- SFT on 50,000 examples averaging 500 tokens: 25 million tokens, so 6 × 7×10⁹ × 2.5×10⁷ = 1.05×10¹⁸ FLOPs per epoch.
- Ratio: 1.05×10¹⁸ / 8.4×10²² = 1.25×10⁻⁵, about 0.001% of pretraining compute per epoch.
SFT is cheap, but it still changes behaviour dramatically. This is why people say it mostly elicits ability that pretraining already built.
Reward model loss. Suppose the chosen response gets reward 2.0 and the rejected gets 0.5. The margin is 1.5. sigmoid(1.5) = 1 / (1 + e^-1.5) = 1 / 1.2231 ≈ 0.818, so the loss is -ln(0.818) ≈ 0.20. If the model had them reversed (margin -1.5), the loss would be about 1.70, so it is pushed hard to fix the ordering.
DPO loss. Let beta = 0.1. Suppose that, relative to the reference model, the policy has raised the chosen response's log-probability by 2.0 and lowered the rejected one's by 1.0. The margin is 0.1 × (2.0 − (−1.0)) = 0.3. sigmoid(0.3) ≈ 0.574, so the loss is -ln(0.574) ≈ 0.55. Training pushes the margin up, but beta controls how strongly deviation from the reference is rewarded.
What each stage changes
| Stage | Data | Objective | Main thing it changes |
|---|---|---|---|
| Pretraining | Trillions of raw tokens, no labels | Next-token cross-entropy on all tokens | Knowledge, language ability, reasoning patterns |
| SFT | Thousands to hundreds of thousands of prompt-response pairs | Next-token cross-entropy on response tokens only | Format, instruction following, turn-taking, tone |
| Preference optimisation | Prompt plus chosen/rejected pairs | Pairwise reward (RLHF) or direct preference loss (DPO) | Which of several plausible answers the model favours: helpfulness, style, refusals, safety |
The key idea is that knowledge mostly comes from pretraining, while behaviour comes from the later stages. Later stages shift the output distribution rather than add large amounts of new knowledge.
RLHF versus DPO
| Aspect | RLHF (reward model + PPO) | DPO |
|---|---|---|
| Models in memory | Policy, reference, reward model, plus a value model in typical PPO setups | Policy and reference |
| Online sampling | Yes, generates from the current policy | No, trains on a fixed dataset |
| Stability | Sensitive to hyperparameters; reward hacking risk | Simpler and more stable, but limited to the offline data |
| Reusable artefact | Reward model can also rank or filter outputs | No reward model produced |
| Engineering cost | High | Lower |
Neither is strictly better. Offline methods are attractive when you have a good preference dataset and a small team. Online RL is attractive when you have a reliable reward signal and want the model to explore beyond the data. Details are in Preference Tuning: RLHF and DPO.
Trade-offs and failure modes
- Reward hacking. The policy finds outputs that score high on the reward model but are not actually good, such as longer answers or confident-sounding ones. The KL penalty limits this but does not remove it.
- Sycophancy and verbosity. If human raters tend to prefer agreeable or long answers, the model learns that. The reward reflects rater bias.
- Alignment tax and forgetting. Heavy fine-tuning can degrade some pretrained abilities. Evaluate on general benchmarks as well as your target task.
- Hallucination. If SFT data contains facts the base model never learned, the model may learn to produce confident answers anyway. This is one commonly discussed contributor, covered in Why Language Models Hallucinate.
- Diversity loss. Preference tuning tends to narrow the output distribution, so the same prompt gives more similar answers. This interacts with decoding settings.
- Cost control. Parameter-efficient methods such as LoRA and QLoRA are often used for SFT and DPO when full fine-tuning is too expensive.
How does this come up in interviews?
1. Why can't you just prompt a base model like a chat model? A base model was trained only to continue text, so it has no learned notion of a user turn, when to stop, or what a helpful answer looks like. SFT teaches the chat format and the end-of-turn behaviour. Few-shot prompting can partly imitate this, but it costs context tokens and is less reliable.
2. Why use preference data on top of SFT instead of just more demonstrations? SFT only imitates one answer per prompt and cannot express "A is better than B". Comparing answers is easier and cheaper for humans than writing ideal ones. Preference methods use the contrast to push the model toward better behaviour and away from worse behaviour, including things demonstrations rarely cover, like refusals and tone.
3. What is the KL penalty in RLHF for? Without it, the policy can drift far from the reference model and exploit weaknesses in the reward model, producing high-scoring nonsense. The KL term keeps the policy's distribution close to the SFT model, trading some reward for fluency and robustness. Beta sets that trade-off.
4. When would you choose DPO over PPO-based RLHF? When you have a good static preference dataset and want a simpler, more stable pipeline with fewer models in memory. I would choose PPO-style RLHF if I needed online exploration or had a reliable reward signal such as a verifier. In either case I would evaluate for length bias and regressions in general capability.
5. Your SFT model follows instructions but gives rambling, over-agreeable answers. What do you check? Look at the SFT data first: response lengths, tone and whether it contains sycophantic patterns. If there is a preference stage, check whether the preference labels reward length or agreement. Then audit the reward or evaluation setup, and consider adding preference pairs where the concise or correct answer is chosen.
Common mistakes
- Saying SFT "teaches the model new knowledge" at scale. It mostly changes format and behaviour; large knowledge gains come from pretraining or retrieval.
- Forgetting loss masking, or implying the model is trained to predict the user's prompt during SFT.
- Describing RLHF as one step. It has a reward-model step and a policy-optimisation step.
- Claiming DPO has no reference model. It does, and the reference log-probabilities are part of the loss.
- Ignoring the KL penalty, or explaining it as only a regulariser without mentioning reward hacking.
- Treating the stages as independent. Each starts from the previous checkpoint, and the preference data should come from outputs resembling what the current model produces.
- Presenting preference tuning as purely a safety step. It also shapes helpfulness, style and formatting.