After this chapter you will be able to
say why a capable pretrained model is not yet useful, and what each alignment stage changes;
compute a Bradley-Terry probability and loss from two rewards;
show how the KL penalty prevents reward hacking, and find the at which the order flips;
read the DPO margin and say why it is watched and not the loss;
name the four failures that alignment leaves and the stage responsible for each.
This chapter builds on Chapter 19 and Chapter 20, which supply the pretrained model and the lever of prompting.
A pretrained language model does one thing, which is to continue text plausibly, because that is what Equation (19.1) asked of it. Ask a base model “What is the capital of France?” and a perfectly good continuation is:
What is the capital of Germany? What is the capital of Italy?
Nothing has gone wrong, because that is a very likely continuation of a list of questions and the model was trained to produce likely continuations. The model knows the answer, so nothing is missing from its capability. What is missing is that nobody ever told it a question should be followed by an answer and not by another question. Closing that gap is what this chapter is about, and it happens in stages.
| stage | data | teaches |
|---|---|---|
| pretraining | raw text, trillions of tokens | language and facts |
| instruction tuning | instruction and response pairs | the shape of a response |
| preference tuning | human rankings of responses | which response is better |
Pretraining is enormously expensive and happens once, while the other two are moderate and both are repeated. The stages are not alternatives, because each assumes the one before it and each teaches something the previous one could not.
Continue training on pairs of instructions and good responses, where the data is what matters. A few tens of thousands of well-written pairs turn a text continuer into something that answers questions, follows formats and stops when it is done.
Note what has not happened. The capital of France was already there, so what changed is the model’s default behaviour when it sees a question, not what it knows. A large instruction set can teach facts of its own in passing, and that is a side effect, not the point of the stage. For this reason the stage is cheap relative to pretraining, and data quality dominates data quantity here.
Instruction tuning teaches the shape of a response and cannot easily teach which of two well-shaped responses is better. Nobody can write a loss function for helpfulness, and yet people can reliably say which of two answers they prefer, and that comparison is enough.
Reinforcement learning from human feedback (RLHF, training a model by scoring what it produces) (Ouyang et al. 2022) turns those comparisons into a training signal in three steps. Collect pairs of model responses and have people rank them, train a reward model to predict those rankings, then optimise the language model against the reward model with a penalty for drifting too far from where it started.
The reward model gives a response a score , and no human ever supplies such a score. What a human supplies is an ordering, and the Bradley-Terry model is what converts one into the other. If a rater preferred over , the model’s probability of agreeing is
so only the difference of the two rewards matters and the scale is free. Training minimises the negative log of Equation (21.1) over the collected pairs. Four pairs make the behaviour concrete, with the rewards a partly trained model might give:
| pair | loss | ||||
|---|---|---|---|---|---|
| clear answer | |||||
| answer with steps | |||||
| confident bluff | |||||
| two paraphrases |
The third row is the one that teaches. There the model prefers the bluff, the pair costs , more than three times the first row, and training pushes up and down until the ordering flips. The fourth row should be remembered for later, because equal rewards give and a loss of , which is the most a single comparison can cost when the model has no opinion at all.
The policy, which is what RLHF calls the language model being tuned, is then optimised against the reward model instead of against people, and those are different things. The objective keeps it on a leash:
where is the instruction-tuned model the policy started from, and is the Kullback-Leibler divergence of Appendix A. It is zero when the policy equals the reference and grows as the two drift apart. Take two candidate behaviours, a plain answer that the reward model scores and stays close to the reference, and a hack that scores by exploiting the reward model while drifting far from it:
| candidate | KL | |||||
|---|---|---|---|---|---|---|
| honest answer | ||||||
| reward hack |
With the hack wins, because beats and the reward model is the only judge in the room. The ordering flips at , where the hack’s distance from the starting model finally costs more than the extra reward it buys. This is reward hacking, and the penalty is what prevents it. Too large a is its own failure, since the policy then stays so close to the reference that preference training changes nothing.
Direct preference optimisation (DPO) (Rafailov et al. 2023) reaches a similar destination by a shorter road, with no separate reward model and no reinforcement learning loop. Its insight is that a policy trained under Equation (21.2) already encodes a reward, readable straight off its own log-ratio against the reference:
Substitute that into Equation (21.1) and the reward model disappears, leaving a loss over preference pairs alone:
With , three stages of one pair show what the loss is measuring:
| stage | margin | loss | |||
|---|---|---|---|---|---|
| at the start | |||||
| after one step | |||||
| over-corrected |
At the start the policy is the reference, so both implicit rewards are zero, the margin is zero and the loss is . This is the same number the undecided pair gave in the Bradley-Terry table above, and for the same reason, which is that the model has no opinion yet.
One step raises the preferred response and lowers the rejected one, and the loss falls. The third row is the warning that exists to price, because pushing the margin further keeps shrinking the loss while the policy drifts further from where it began. DPO is simpler to implement and to debug, and the two methods are close enough in practice that the choice is usually made on engineering grounds.
Run it.
code/worked_examples/alignment.pyproduces every table in this section. Run--btfor the reward-model loss,--klfor the reward-hacking sweep and--dpofor the implicit rewards. Change one reward in the first table and watch which pair dominates the mean loss.
Precision matters here, because the word carries more weight than the technique does. Alignment changes which of the model’s existing behaviours are likely, and it neither adds knowledge nor removes capability.
A model that could write malware before alignment can still write malware after it, and what changed is that the aligned model’s most probable response to the request is a refusal. That distinction explains most of what follows, including why jailbreaks exist, since a behaviour made unlikely is not a behaviour removed.
Four failure modes survive all three stages, and each has a known cause. Hallucination is the model stating false things fluently, and the cause is structural, because it was trained to produce likely text and a plausible falsehood is likely text. Nothing in the objective distinguishes true from true-sounding, and Chapter 22 is the main practical response.
Sycophancy is the model agreeing with the user against the evidence, which is preference tuning working correctly on data where raters preferred agreeable answers. The signal was human approval, and approval and correctness are not the same thing.
Jailbreaks are careful phrasings that recover refused behaviour, and for the reason just given: alignment shifted probabilities and did not remove capabilities, so a sufficiently different framing can shift them back. Inherited bias is the last. Chapter 9 measured social regularities in static embeddings with WEAT. The same regularities sit in the pretraining corpus of every large model, and alignment reduces their expression without removing their source. Chapter 27 takes this up properly.
None of the four is a bug in the sense of a mistake in the code. Each is a predictable consequence of the objective, so each needs a system around the model, not a fix inside it.
Summary
A pretrained model continues text, so a question may be followed by another question. Instruction tuning on demonstrations teaches the format of an answer and is cheap, and quality matters more than quantity. Preference tuning ranks pairs of answers with a reward model trained by the Bradley-Terry loss, which depends only on the difference of the rewards. The policy is optimised against that reward model with a KL penalty that keeps it near the starting model, and the penalty is what prevents reward hacking. Direct preference optimisation reaches a similar result without a separate reward model, and its margin is more informative than its loss. Alignment changes which behaviours are likely and adds no knowledge, so models still invent facts, flatter, can be jailbroken and carry bias, and each of these needs a system around the model.
Key terms
instruction tuning. fine-tuning on demonstrations of the desired answer to a prompt.
reward model. a model trained on preference pairs to score an answer.
Bradley-Terry model. a model in which the chance of preferring one answer is a sigmoid of the reward difference.
KL penalty. a term that keeps the optimised policy close to a reference model.
reward hacking. raising the reward model’s score by exploiting its errors.
direct preference optimisation. training on preference pairs directly, without a separate reward model.
sycophancy. agreeing with the user because agreement was rewarded.
Ouyang et al. (2022) is the InstructGPT paper and the clearest description of the three-stage pipeline. Rafailov et al. (2023) introduce direct preference optimisation. Bai et al. (2022) replace much of the human labelling with a written set of principles, which is a different answer to the same problem.
21.1 Why the difference and not the score. Equation (21.1) depends on and never on either reward alone. Add to both rewards in every row of the first table and recompute the losses. Explain what this says about what a reward model can and cannot be asked to mean, and why two reward models trained on the same data can disagree wildly on absolute scores while ranking identically.
21.2 Find the flip point. Using the reward-hacking table, solve for the exact at which the honest answer overtakes the hack, rather than reading it off the four columns. Then redo it for a hack that scores at a KL of , and say what that implies about choosing when you do not know in advance how hard the reward model can be gamed.
21.3 The loss that means nothing yet. Both the undecided Bradley-Terry pair and the first row of the DPO table give a loss of exactly . Show why those two zeros are the same zero. Then say what a DPO loss above would tell you about the current policy.
21.4 Read the margin, not the loss. In the DPO table the loss falls from to while the margin grows from to . Compute the margin needed to reach a loss of , then of . Plot or tabulate the two and explain why practitioners watch the implicit reward margin rather than the loss when deciding to stop.
21.5 A pair by hand. A pair has rewards and . Compute and the loss. Then compute both when the rewards are and , and say which pair training pushes harder.
21.6 The penalty by hand. A candidate has reward and KL . Compute its penalised value for , and , and compare each with the honest answer of the table.
Build and measure
21.7 The continuation trap. Write three prompts on which a base language model and an instruction-tuned one would behave very differently. For each, describe the base model’s likely output and say why it is a correct continuation.
21.8 Why a reward model. Explain why preference tuning needs a learned reward model rather than direct human feedback in the training loop. Then explain what the penalty term protects against, and predict what its absence would produce.
21.9 Shape against substance. Instruction tuning changes behaviour without adding knowledge. Design an experiment that would distinguish the two, using only prompts and no access to the weights.
In practice
21.10 Diagnose the failure. For each symptom, name which stage is responsible and what you would change: (a) the model invents citations; (b) it reverses a correct answer when contradicted; (c) it refuses a legitimate medical question; (d) it produces a different quality of answer for names from different regions.
21.11 Sycophancy as a design consequence. Argue that sycophancy is preference tuning succeeding rather than failing. Then propose one change to the data collection that would reduce it, and name what your change costs.
21.12 A jailbreak. A refusal that works for a direct request fails when the request is framed as fiction. Use the claim that alignment shifts probabilities to explain why, and name one defence inside the model and one outside it.
Each one reproduces the tables above. Run it in the page, change an input and watch which numbers move.
alignment.pyPreference tuning, in three tables
Bradley-Terry turns a ranking into a loss, the KL penalty stops reward hacking, and DPO does it without a reward model.
runs in your browser Run itFrom Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.