A pretrained language model does exactly one thing, which is to continue text plausibly, because that is what Equation (18.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 rather than 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 rather than what it knows. A large instruction set can teach facts of its own in passing, and that is a side effect rather than the point of the stage. That is why the stage is cheap relative to pretraining, and why 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 (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 (20.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 is worth remembering for later, because equal rewards give exactly and a loss of , which is the most a single comparison can cost when the model has no opinion at all.
The policy is then optimised against the reward model rather than against people, and those are different things. The objective keeps it on a leash:
where is the instruction-tuned model the policy started from. Take two candidate behaviours, an honest 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. That 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 (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 (20.2) already encodes a reward, readable straight off its own log-ratio against the reference:
Substitute that into Equation (20.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 . That 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.
Try it yourself.
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.
It is worth being precise 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 21 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 rather than removing 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, which is why each needs a system around the model rather than a fix inside it.
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.
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.
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.
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.
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.
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.
Why the difference and not the score. Equation (20.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.
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.
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.
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.
Each one reproduces the tables above. Open it in Colab, 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.
standard library only Run it in Colab Notebook SourceFrom Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.