Part III · RL for LLMs · Chapter 13
Direct preference optimization
Skip the reward model: derive DPO from the KL-regularized objective, and meet its variants.
Chapter 12 built the RLHF pipeline. You collected preference pairs, fit a Bradley–Terry reward model, and ran PPO against it, with four networks in memory (policy, reference, reward model, value function), a sampling loop, per-token KL penalties, GAE, and a long list of implementation details. It works, but it’s a lot of machinery for a simple goal: make the model produce more of what people prefer.
In 2023, Rafailov and colleagues pointed out that most of the machinery is unnecessary, at least on paper. The RL step in RLHF has a known answer, a closed-form formula you met in Chapter 10 and Chapter 11. If you know the answer’s form, you can turn the problem around: instead of learning a reward and then searching for the policy that maximizes it, write the reward in terms of the policy, and fit the policy to the preferences directly. The result is direct preference optimization (DPO): one loss, computed from four log-probabilities per preference pair, trained like supervised learning. No reward model, no sampling, no PPO. The paper’s subtitle says what is going on: your language model is secretly a reward model.
This chapter derives DPO slowly, with numbers you can check, then looks hard at what it gives up. DPO is offline, so it never sees its own mistakes. It can push the probability of the preferred answers down. With few labels it ignores its own leash. Then come the variants people built to fix these problems (IPO, KTO, ORPO, SimPO, online DPO), and the contested question of whether DPO or PPO works better.
Two facts you already have
DPO is built from two results from earlier chapters. Here they are side by side.
Fact 1: the KL-regularized optimum (Chapter 10 derived it, Chapter 11 applied it to language models). For a prompt , the RLHF objective is
and its maximizer is the reference policy tilted by the exponentiated reward:
is a partition function: a sum over every possible response, astronomically many for a language model. Nobody can compute it. That’s why RLHF doesn’t just write down and stop; it runs PPO to approximate it by sampling.
Fact 2: the Bradley–Terry model (Chapter 12). The probability that an annotator prefers to for prompt is
with the logistic sigmoid. A reward model is fit by maximizing this likelihood on the preference data, which is the loss . Notice that only reward differences between two responses to the same prompt enter.
RLHF uses these facts in sequence: fit with Fact 2, then chase the of Fact 1 with RL. DPO’s idea is to use Fact 1 backwards.
The trick, with numbers
Take Chapter 10’s worked example and pretend it’s a language model with a single prompt and only three possible responses A, B, C. The reference policy is , the rewards are , and . Chapter 10 computed the tilted optimum: the weights are , so , , and .
Now go the other way. Suppose someone hands you and but not the reward. Compute for each response:
| Response | true | ||||
|---|---|---|---|---|---|
| A | 0.5 | 0.179 | 0 | ||
| B | 0.3 | 0.292 | 1 | ||
| C | 0.2 | 0.529 | 2 |
The log-ratio column is the reward minus one constant, , the same for every response. So you don’t recover the reward exactly, but you recover every reward difference exactly: B − A , C − A , C − B .
And reward differences are all Bradley–Terry needs. The probability an annotator prefers C to A is , and you get the same number from the policy alone: . The unknowable never came into it, because it cancelled in the subtraction.
So here is DPO in one sentence. If the policy is the tilted optimum for some reward, that reward is (up to a per-prompt constant), so fit the policy so that this implied reward explains the preference data under Bradley–Terry.
Deriving the DPO loss
Now the general version, in four steps.
Step 1: invert the optimum. Take the log of Fact 1 and solve for the reward:
Step 2: substitute into Bradley–Terry. For two responses to the same prompt, the terms are identical and cancel:
The preference probability is now written entirely in terms of the optimal policy and the reference. No reward function appears.
Step 3: make the policy the thing you learn. Replace by the trainable model . Each policy now defines a reward model, its implicit reward
and is exactly the KL-regularized optimum for (plug into Fact 1 and you get and ).
Step 4: maximum likelihood. Fit to the preference data with the ordinary Bradley–Terry loss, as you would fit a reward model in Chapter 12. That’s the DPO loss:
Each term is a sequence log-probability, and (Chapter 11) a sequence log-probability is a sum of token log-probs: . So for each pair you need exactly four numbers: , , , . That takes one forward pass through each model for each of the two responses. The reference numbers don’t change during training, so you can compute them once in advance.
∂ The fine print: is fitting the policy really the same as fitting a reward and then optimizing it?
Step 3 quietly swapped “learn a reward, then find its optimal policy” for “learn a policy”. Here is why nothing is lost, in an idealized setting where both and can be any function.
Rewards come in equivalence classes. Call two rewards equivalent if they differ by a function of the prompt only: . Equivalent rewards
- give the same Bradley–Terry probabilities, because cancels in (this is the shift invariance of Chapter 12), and
- give the same optimal policy, because and the factor cancels when you normalize over .
(The DPO paper states these two facts as its Lemmas 1 and 2.)
Each class corresponds to exactly one policy, and vice versa. Going from rewards to policies, each class maps to one optimal policy , by the second bullet. Going back, take any policy that is positive wherever is. The reward has , so its optimal policy is itself. And any reward whose optimal policy is satisfies , so it is in the class of . Classes and policies are in one-to-one correspondence.
So the two problems are the same problem. RLHF solves and then outputs . DPO solves . Because the map between classes and policies is a bijection, and the likelihood depends only on the class, the DPO optimum is , where is the maximum-likelihood reward. That is exactly what RLHF would return if its RL step found the exact optimum. (The DPO paper’s Theorem 1 states the representation result.)
Where the idealization breaks. In practice the reward model and the policy are different neural networks trained by different procedures on finite data. The Bradley–Terry maximum-likelihood solution is not unique on responses the data never mentions, and the two pipelines fill in that gap differently: PPO’s KL penalty is measured on responses the policy actually samples, while DPO’s loss says nothing at all about responses outside the pairs. “Equivalent in the limit” does not mean “equivalent in practice”. The section on what DPO gives up returns to this.
Why the pair must share a prompt
The cancellation in Step 2 is the whole trick, and it needs both responses to be answers to the same . Suppose you tried to compare for prompt with for prompt . Then
and the last term, a ratio of two intractable sums, refuses to go away. DPO can only learn from comparisons within a prompt. That matches how preference data is collected anyway: annotators see one prompt and two answers.
A worked example of the loss
Take and one pair with these sequence log-probs:
| log-ratio | implicit reward log-ratio | |||
|---|---|---|---|---|
| chosen | ||||
| rejected |
The policy has made the chosen response times more likely than the reference does, and the rejected one times less likely. The implicit reward margin is , the implicit reward model says , and the loss is .
Two numbers to remember. At the start of training , every log-ratio is 0, every margin is 0, and the loss is for every pair. Any other value at step 0 means a bug. And notice that the raw log-probs (, ) are large and negative, as they are for any real response of a few dozen tokens, but only the differences from the reference matter.
Quick check At the very first DPO step (policy initialized to the reference), what is the DPO loss on every pair, and what is the implicit reward of every response?
With π_θ = π_ref every log-ratio log(π_θ/π_ref) is 0, so every implicit reward β·log-ratio is 0, every margin is 0, and −log σ(0) = log 2. This is a handy sanity check for any DPO implementation.
What the gradient does
The DPO loss looks like a classification loss. To see what it does to the policy, differentiate it. Write for the margin. Since and (the reference terms are constants),
Gradient descent on the loss moves along the negative of this, so each pair does three things:
- Raise the winner. is the supervised fine-tuning gradient on .
- Lower the loser. pushes the rejected response down. This “negative gradient” is something SFT can’t do: SFT only ever learns from good examples.
- Weight by surprise. The factor is the probability the implicit reward model assigns to the wrong ordering. Pairs it already ranks confidently get almost no weight; pairs it gets wrong get almost full weight.
Here is the weight as the margin grows:
| implicit margin | ||||||
|---|---|---|---|---|---|---|
| loss | 2.127 | 1.313 | 0.693 | 0.313 | 0.127 | 0.018 |
| weight | 0.881 | 0.731 | 0.500 | 0.269 | 0.119 | 0.018 |
The weight is what keeps DPO from running away. Without it, “push the winner up and the loser down with equal force forever” has no stopping point. The DPO paper reports that a naive version without the weighting coefficient can make the language model degenerate. With it, each pair contributes less and less once the implicit reward model is confident about it. (Less and less, but never zero: with labels that never disagree, the push fades without stopping, which is the problem the section on deterministic preferences takes up.)
Quick check Two pairs in a batch: for pair 1 the implicit reward margin is +3 (ranked confidently right), for pair 2 it is −1 (ranked wrong). Roughly how do their gradient weights compare?
The weight is σ(−margin): σ(−3) ≈ 0.047 for pair 1 and σ(1) ≈ 0.731 for pair 2, a ratio of about 15. DPO concentrates its effort on pairs its implicit reward model currently gets wrong, though no pair's weight is ever exactly zero.
The algorithm in practice
DPO
input: preference pairs D = {(x, y_w, y_l)}, SFT model π_SFT, β (e.g. 0.1)
1. π_ref ← copy of π_SFT, frozen; π_θ ← π_SFT, trainable
2. (optional) precompute log π_ref(y_w|x) and log π_ref(y_l|x) for every pair
3. for each minibatch of pairs:
4. lw ← Σ_t log π_θ(y_w,t | x, y_w,<t) # masked sum over response tokens
5. ll ← Σ_t log π_θ(y_l,t | x, y_l,<t)
6. u ← β·[(lw − log π_ref(y_w|x)) − (ll − log π_ref(y_l|x))]
7. loss ← mean(−log σ(u)) # use a stable log-sigmoid
8. gradient step on loss
9. log: reward accuracy mean(u > 0), mean margin, mean lw and ll
Some practical points, all of which you will meet in the lab:
- The reference is the SFT model. DPO assumes is a reasonable model of the distribution the preference data came from. The DPO paper recommends, when no SFT model is available, first fine-tuning on the preferred responses to get one.
- Masking. Prompt tokens and padding are excluded from the sums, exactly as in Chapter 11. Llama 3’s DPO also masked out special formatting tokens (headers and end-of-turn tokens) that appear in both responses.
- Hyperparameters. around 0.1 is the common default; Llama 3 reports with learning rate . Learning rates are small and training is short, usually one to a few epochs over the pairs, because DPO overfits (more below).
- What to watch. The reward accuracy (fraction of pairs with ) and the margin tell you whether the implicit reward model is learning. The chosen and rejected log-probs, and , tell you how it is learning. You would hope the chosen one goes up. Often it doesn’t.
Here is how this compares with the pipeline of Chapter 12:
| RLHF with PPO (Chapter 12) | DPO | |
|---|---|---|
| Stages after SFT | train a reward model, then run RL | one training run |
| Networks in memory | policy, reference, reward model, value function | policy and reference (reference log-probs can be precomputed) |
| Generation during training | every step: sample responses, score them | none |
| Data | preference pairs (for the RM) plus prompts (for RL) | preference pairs only |
| Where the KL leash acts | explicit penalty, on responses the policy samples | built into the loss through the derivation |
| Learns from its own outputs? | yes: on-policy | no: offline |
| Typical failure | the policy exploits reward-model errors | overfitting the pairs, likelihood displacement, drift on unseen responses |
DPO is cheaper and simpler. The last two rows are the price.
Watch DPO train
The demo below is a whole “language model” for a single prompt with a handful of possible responses, small enough to see everything. Each scenario fixes , a hidden true reward (used only to draw the RLHF target , the thick horizontal ticks on the bars), and a preference dataset. A pair that annotators label "" with probability contributes its average loss, times the loss with as winner plus times the loss with as winner. The demo minimizes this with Adam on all pairs at once (no sampling), so the curves are smooth and repeatable. The readout below the table shows the step, the loss, the mean implicit-reward margin on the pairs, and two KL divergences: how far has moved from , and how far it still is from .
In scenarios 1 and 2 the policy is tabular: one free logit per response. In scenario 3 the four responses are described by 2-dimensional feature vectors and the policy is . That is a cartoon of a real network, where similar responses have similar internal representations and therefore move together.
πref πθ (trained) π* (RLHF optimum)
solid: log πθ(y) dashed: log π*(y)
| response | in the data | true r | πref | πθ | π* | implicit reward β·log(πθ/πref) |
|---|
What to try: (1) Scenario 1, DPO, β = 1: press Train. π_θ converges onto the π* ticks (the thick horizontal lines), (0.179, 0.292, 0.529) as in the worked example, the implicit rewards become (−1.027, −0.027, 0.973), and KL(π_θ ‖ π*) goes to 0. Change β to 0.3 and train again: π* changes, but the implicit rewards still differ by exactly 1 and 2, the true reward gaps. (2) Scenario 2: response B is the winner of one pair, yet its log-probability falls (the curve labeled B). D, the best response, appears in no pair and is never learned. The margin grows without limit, and the final policy is the same for β = 0.1 and β = 1. Switch the loss to IPO: the margin stops at 0.5. (3) Scenario 3: the data says only 'No.' ≻ 'Never.', yet 'Yes.' takes over and 'No.' first rises, then collapses. Switch to DPO + NLL and train again.
Scenario 1 confirms the derivation: when every pair is labeled according to Bradley–Terry with the true reward (think: infinitely many annotators per pair), DPO’s minimizer is exactly the RLHF optimum , without ever seeing the reward or . Scenarios 2 and 3 are what happens with realistic data, and they lead straight to the next section.
What DPO gives up
It’s offline
DPO trains on a fixed dataset of pairs that some other policy generated (often a mixture of older models, or the SFT model before training). It never samples from , so it never finds out what actually does. This is Chapter 10’s offline RL problem in a new form.
Look at what the loss constrains. It involves only through the log-probabilities of the responses in the pairs. Every other response, which is nearly all of them, is unconstrained, except that probabilities must sum to one. If raising relative to happens to also raise some response that appears nowhere in the data, the loss doesn’t notice, and nothing in DPO will ever sample to find out it’s bad.
Xu et al. (2024) make this precise. They prove that every policy PPO-based RLHF can produce also minimizes the DPO loss, but not the reverse: DPO’s set of solutions is strictly larger. Their toy example has three responses with and one preference, . The policy minimizes the DPO loss (the loss only needs ) while putting 90% of its mass on , a response the reference never produces and no annotator ever saw. A KL-penalized RL policy can’t do this, because would have infinite KL from the reference. Scenario 2 in the demo shows the milder version: the best response D gets nothing, because nobody ever compared it with anything.
PPO-based RLHF has the opposite profile. It samples from the current policy, so it finds out what the policy does, and the reward model judges those very samples. Its weakness is that the reward model may be wrong about them (reward hacking, Chapter 19). DPO trades “a wrong judge of new samples” for “no judge of new samples”.
Deterministic preferences: the leash goes slack
In the derivation, is the strength of the KL leash: large keeps near . But look at what the loss does with a pair whose label is always the same. If a fraction of annotators prefer , the expected loss on that pair, as a function of the margin , is . It is minimized where , at
(The second term is there because a fraction of the time the label is flipped, and then the margin enters the loss with the opposite sign.) With that’s a finite margin, . With there is no minimum: the loss keeps decreasing as . Since , the chosen log-ratio can never exceed , so the only way to make infinite is : the rejected response is driven out completely. Whatever is, the optimizer drives the log-ratios to infinity. only sets the speed, not the destination, and the KL regularization that the derivation promised has no effect.
And is the normal case, not an edge case. In a real dataset each pair is labeled once, so the empirical preference probability of every pair is exactly 1, even when the true probability is 0.6. Azar et al. (2023) used this argument to motivate IPO (identity preference optimization), which replaces the log-sigmoid with a squared loss that has a finite target. Scenario 2 in the demo shows both behaviors: DPO’s margin grows without limit, while IPO’s stops at a fixed value.
In practice the damage is limited by early stopping: DPO is trained for one or a few epochs with a small learning rate, so it never gets close to the infinite optimum. That works, but it means the real regularizer is the training schedule, not .
Likelihood displacement: the chosen response can go down
The DPO loss depends on the difference of two log-ratios. Nothing in it says has to go up. If pushing down is “cheaper” than pushing up, the optimizer can widen the margin while both log-probabilities fall, as long as falls faster. The probability mass has to go somewhere, and it goes to responses outside the pair.
This is observed in real training runs, not only in toys. Pal et al. (2024) reported that DPO can lower the likelihood of preferred responses, especially when the two responses in a pair differ by only a few tokens. Razin et al. (2025) studied it under the name likelihood displacement. Their examples are striking. Training a model to prefer “No” over “Never” sharply raised the probability of “Yes”. In a safety setting, DPO on refusal data lowered Llama-3-8B-Instruct’s refusal rate from 74.4% to 33.4%. They trace the effect to pairs whose two responses have very similar hidden representations.
Why similarity matters is easy to see in the demo’s feature model. With and one pair, the gradient of the margin is
because the normalizer’s gradient cancels in the difference. So moves along . When and are nearly identical (“No.” and “Never.”), is a short vector that encodes only the small way they differ, and it can point anywhere, including toward a response with the opposite meaning. Each response’s logit changes by per unit step. If “Yes.” has a larger than “No.” does, “Yes.” grows fastest and eventually takes mass from everything, including the “chosen” “No.”. That’s scenario 3. A real network’s representations aren’t 2-D, but the mechanism (the update direction is a difference of two similar representations) is the one Razin et al. describe.
You’ll see the same thing in the lab with a real (tiny) transformer. On correct-versus-wrong arithmetic answers, plain DPO with separates chosen from rejected on more than 90% of training pairs, while the held-out log-probability of the correct answers falls from to about and the model gets fewer problems right than before training. The fixes are simple:
- Add a likelihood term on the chosen response. . Llama 3 used this with . Pang et al. (2024), training reasoning with iterated DPO on correct versus incorrect chains of thought, found the NLL term crucial.
- Penalize any drop of the chosen log-ratio below zero (DPOP, the “DPO-Positive” loss of Pal et al.).
- Clean the data: Razin et al. filter out pairs whose responses are too similar.
- Sample from the policy (online DPO, below), so the mass that leaks to unexpected responses gets noticed and pushed back.
Quick check During DPO training you see reward accuracy rise to 95% and the margin grow, while the average log π_θ(chosen) falls steadily. What is the best reading?
The DPO loss only constrains the gap between chosen and rejected. Both can fall. Whether that is harmful depends on where the freed probability goes, which the loss doesn't control. Check generations, and consider an NLL term on the chosen, a larger β, or fewer epochs.
Length and other shortcuts
Because the implicit reward is a sum of per-token log-ratios, DPO can be influenced by response length. Preference datasets often favor longer answers (annotators and LLM judges have a verbosity bias, Chapter 14), and Park et al. (2024) found that DPO strongly exploits this, producing longer responses without more quality. They tie it to the same out-of-distribution problem: DPO extrapolates the “longer is preferred” pattern beyond the data. Their fix adds a length regularizer. Length-normalized variants (SimPO, and the length-normalized DPO of Tülu 3, below) attack the same issue.
The variant family
DPO started a flood of “direct alignment” losses. They differ in four choices: what data they need (pairs, or single responses labeled good or bad), whether they need a reference model, how they normalize for length, and what shape of loss they put on the margin. Here are the ones you will see named in papers, in our notation, with for the raw log-ratio.
| Method | Data | Reference? | Loss, per example | Main idea |
|---|---|---|---|---|
| DPO (Rafailov et al. 2023) | pairs | yes | Bradley–Terry on the implicit reward | |
| IPO (Azar et al. 2023) | pairs | yes | finite target margin, so deterministic labels can’t push it to infinity | |
| KTO (Ethayarajh et al. 2024) | single responses labeled desirable / undesirable | yes | , see below | no pairs needed; loss shaped by prospect theory |
| ORPO (Hong et al. 2024) | pairs | no | SFT and preference learning in one stage | |
| SimPO (Meng et al. 2024) | pairs | no | length-normalized reward, target margin | |
| DPO + NLL (Pang et al. 2024; used in Llama 3) | pairs | yes | keep the chosen response likely | |
| Online / iterative DPO (e.g. Guo et al. 2024) | pairs sampled from the current policy | yes | DPO | fresh on-policy data each round |
A few words on each.
IPO. Azar et al. show that DPO and RLHF both rest on the Bradley–Terry assumption, which turns a preference probability into a reward difference through the logit function . That function is infinite at , and that’s the source of the infinite margins. IPO optimizes preference probabilities directly (their is the identity) and arrives at a squared loss that regresses the log-ratio margin to . With label probability the expected IPO loss is minimized at margin : finite even at . Notice that IPO’s (the paper writes ) is a different knob from DPO’s: the target is a margin in raw log-ratio units, and the optimum is not the RLHF (the demo’s scenario 1 with IPO shows this).
KTO. Pairs are expensive. Often you only have thumbs up or thumbs down on single responses. KTO (named for Kahneman and Tversky’s prospect theory, a model of how people value gains and losses relative to a reference point) works with that. With the implicit log-ratio and a reference point (an estimate of computed from the batch and not backpropagated through), the “value” of a response is if it is desirable and if not, and the loss is . A desirable response is pushed up until it beats the reference point, an undesirable one down until it is below it. The weights , (default 1) let you correct for unbalanced numbers of good and bad examples. The paper reports matching or exceeding pair-based methods from 1B to 30B parameters.
ORPO. It removes the reference model and the separate SFT stage. Training is SFT on plus a penalty on the odds ratio, where is the per-token geometric mean probability and . With no there is no explicit leash; the SFT term keeps the model anchored to the chosen responses. The paper used between 0.1 and 0.25 for its main models.
SimPO. The implicit reward is the average per-token log-probability, scaled by , with no reference model, and the loss asks the chosen to beat the rejected by a margin (the paper’s , renamed here because is our discount factor). The average removes the advantage long responses get from summing more tokens. The paper suggests around 2–2.5 and between 0.5 and 1.5, finds that removing the length normalization hurts most, and reports gains over DPO on AlpacaEval 2 and Arena-Hard. Tülu 3 used a related length-normalized DPO, which keeps the reference but divides each log-ratio by the response length (with , since per-token log-ratios are small).
Which is best? Each paper reports wins on its own benchmarks, and independent comparisons disagree. In the Tülu 3 report, length-normalized DPO beat both standard DPO and SimPO on their evaluation suite. Treat the table as a menu of fixes for specific failure modes: IPO for overfitting to deterministic labels, KTO for unpaired data, ORPO and SimPO for dropping the reference model’s memory and compute, length normalization for verbosity, NLL for displacement.
Online and iterative DPO
Most of DPO’s problems come from one fact: the pairs were not sampled from the model being trained. The obvious fix is to sample them from it.
Online DPO (Guo et al. 2024, “Direct Language Model Alignment from Online AI Feedback”) samples two responses from the current policy for each prompt, asks an LLM annotator which is better, and takes a DPO step on that fresh pair. The paper reports that this beats offline DPO, IPO and SLiC (another pairwise loss, a hinge on the log-probability margin), and RLHF, in human evaluations. Iterative DPO does the same in a few large rounds instead of every step: train with DPO, sample new responses from the trained model, label them (with humans, a reward model, a judge, or a verifier), train again. Llama 3 ran several such rounds, re-collecting preferences on the latest model each time. Self-rewarding language models (Yuan et al. 2024) let the model judge its own samples between rounds. Iterative reasoning preference optimization (Pang et al. 2024) builds the pairs from correct and incorrect chains of thought checked against the known answer.
Online DPO is no longer offline RL, and it has stopped being “RL-free” in the sense that mattered: you now need a generation loop and a source of labels for new samples, which is most of the machinery DPO set out to remove. What remains different from PPO is the loss: a contrastive loss on pairs, with no value function and no explicit KL penalty.
DPO or PPO? The empirical picture
This is contested, and the honest answer is “it depends on the data, the tuning, and what you measure.” Here are the main data points, with what each one actually claims.
- Xu et al. (2024), “Is DPO Superior to PPO for LLM Alignment?” Beyond the theorem above, they find that a carefully tuned PPO surpasses the other alignment methods they test in all their settings, from dialogue to competitive programming. They identify three things that made PPO work: advantage normalization, large batches, and an exponential moving average of the reference model.
- Ivison et al. (2024), “Unpacking DPO and PPO”. A controlled comparison across many preference datasets. Their ranking of what matters: the quality of the preference data first, then the learning algorithm, then reward model quality, then extra unlabeled prompts. PPO beat DPO, by up to 2.5% in math and 1.2% in general domains, while better data was worth up to 8% on instruction following and truthfulness.
- Llama 3 (Grattafiori et al. 2024). Meta’s post-training used SFT, rejection sampling and DPO over several rounds. The report says the team explored on-policy algorithms such as PPO but found that DPO needed less compute for large models and performed better, especially on instruction-following benchmarks like IFEval.
- Tülu 3 (Lambert et al. 2024). AI2’s open recipe is SFT, then length-normalized DPO on largely on-policy preference pairs (responses from the SFT model and other models, judged by an LLM), then RL with verifiable rewards. In their controlled comparison, PPO came out comparable to DPO, slightly lower, at a much higher compute cost, so they kept DPO for preference tuning and used PPO-style RL for the verifiable-reward stage.
- On-policy data matters more than the loss. Tajwar et al. (2024) argue that methods using on-policy samples or a “negative gradient” (pushing some responses down) do better than offline maximum-likelihood objectives, because they move probability mass between responses faster. Online DPO’s results (above) point the same way.
The pattern most practitioners take from this: with a fixed offline dataset and limited compute, DPO is a strong, cheap default. With the ability to sample from the policy and score the samples, on-policy methods tend to win, and online DPO closes much of the gap.
Where things stand in late 2026. DPO remains a standard step in open recipes: Olmo 3 still runs SFT, then DPO, then RL with verifiable rewards, building its preference pairs with the “delta learning” idea of Geng et al. (2025) (pair a stronger model’s response with a weaker model’s; what matters is the quality gap). Qwen2.5 used offline DPO on pairs built from execution feedback and answer matching before its online RL stage. But for reasoning, the center of gravity moved to on-policy RL with verifiable rewards and GRPO-style algorithms (Chapter 15). In a June 2026 Interconnects conversation, Nathan Lambert and Finbarr Timbers described DPO as largely disappearing from leading frontier recipes, while noting it can still be a compute-efficient choice and that the shift may owe something to fashion rather than careful ablations. No published study settles the question.
Quick check Which statement about DPO vs. PPO is best supported by the evidence above?
The equivalence holds only in an idealized limit (exact optimization over all functions). In practice, controlled studies (Xu et al., Ivison et al.) found tuned PPO ahead, while Llama 3 and Tülu 3 chose DPO for cost and found it competitive. Online/iterative DPO, which samples from the policy, narrows the gap.
Summary
- The trick: the KL-regularized optimum can be inverted, . In Bradley–Terry only reward differences within a prompt appear, so cancels.
- The loss: with the implicit reward . It needs four sequence log-probs per pair and no sampling. At initialization it equals .
- The gradient raises the winner, lowers the loser, and weights each pair by , the implicit reward model’s probability of getting it wrong.
- In the ideal limit (all pairs, Bradley–Terry labels, an expressive model), DPO’s optimum is exactly the RLHF optimum. In practice it is not.
- What it gives up: it is offline (no exploration; unseen responses are unconstrained); with deterministic labels the margin is pushed to infinity and stops acting as a leash (IPO’s motivation); and likelihood displacement can lower the chosen response’s probability and move mass to unrelated, sometimes opposite, responses.
- Variants fix specific failures: IPO (finite target), KTO (unpaired labels), ORPO and SimPO (no reference model; SimPO length-normalizes), DPO + NLL (displacement), online/iterative DPO (on-policy data).
- Empirically it’s contested: tuned PPO often wins in controlled studies, DPO is far cheaper and has been used by Llama 3, Tülu 3 and Olmo 3, and on-policy data matters as much as the choice of loss.
Exercises
(a) List everything an RLHF-with-PPO run needs during training that a DPO run does not (models in memory, computation per step, data).
(b) DPO still needs a reference model. What role does it play, and could you drop it? Name a method that does.
(c) A colleague says: “DPO is RL without the RL.” In what sense is that right, and in what important sense is DPO not reinforcement learning as Chapter 1 defined it?
Solution
(a) PPO-based RLHF needs a reward model and a value function in memory (on top of the policy and the reference), a generation step every iteration (sample responses from the current policy, then score them with the reward model), per-token rewards with a KL penalty, GAE advantages, and the PPO clipped update over several epochs. It needs prompts for the RL stage in addition to the preference data used to train the reward model. DPO needs only the preference pairs, the policy, and the reference, and the reference’s log-probs can be computed once before training.
(b) The reference anchors the implicit reward: measures how much the policy has changed a response’s probability, and the derivation’s KL leash is a leash to . Without it, the “reward” of a response would be its raw log-probability, which mostly reflects length and fluency rather than quality. You can drop it if you replace the anchor with something else: SimPO uses the length-normalized log-probability with a margin; ORPO adds an SFT term on the chosen response. Both save the memory and compute of the reference model, at the cost of the derivation’s guarantee.
(c) It is right that DPO optimizes, in the ideal limit, the same KL-regularized reward objective as RLHF; that’s the derivation. It is not RL in Chapter 1’s sense because the agent never acts. It never samples a response, never receives evaluative feedback on its own behavior, and its data does not depend on its policy. It is supervised learning on a fixed dataset with a cleverly chosen loss. That is exactly why it is offline, can’t explore, and can’t correct mistakes on responses the dataset doesn’t contain.
Take and a pair with , , , .
(a) Compute both implicit rewards and the margin. Does the implicit reward model rank the pair correctly, and with what probability?
(b) Compute the loss and the gradient weight .
(c) Compute and . Which way does a gradient-descent step move each log-prob?
Solution
(a) . . Margin . It ranks the pair correctly, with : correct, but not confident.
(b) Loss . Weight .
(c) with . So , and . Gradient descent moves opposite to the gradient: goes up, goes down, with equal force. (How the parameters change, and so what happens to other responses, depends on the network; see Exercise 13.4.)
(a) Starting from the objective and its solution (take it as given from Chapter 10), derive the DPO loss. Show every step, and say exactly where you use the fact that the two responses share a prompt.
(b) Suppose a dataset also contained comparisons of the form “response to prompt is better than response to prompt ”. Write the Bradley–Terry probability of such a comparison in terms of and , and explain why DPO can’t use it.
(c) Bradley–Terry generalizes to rankings of responses (the Plackett–Luce model): the probability of the ranking is . Show that also cancels here, and write the resulting loss.
Hint
Solution
(a) Take the log of the solution: . Multiply by and rearrange:
Bradley–Terry says . Substitute:
Here we use the shared prompt: both terms are evaluated at the same , so they cancel. Now parameterize the policy, , which makes a reward model whose optimal policy is itself. The log-likelihood of the dataset under Bradley–Terry is . Maximizing it is minimizing
(b) . The last term is a ratio of sums over all responses to two different prompts. It is not a function of anything you can compute from the policy’s log-probs on the two given responses, so the loss can’t be evaluated. (A separately trained reward model can in principle use cross-prompt comparisons, although Bradley–Terry rewards are only identified up to a per-prompt shift, so those comparisons carry information the within-prompt model doesn’t even represent.)
(c) Write and , :
The common factor cancels in every term of the product because all responses share . The loss is with . For this is the DPO loss, since and the term is . (The DPO paper derives this Plackett–Luce version in its appendix.)
(a) Derive for one pair and show it equals .
(b) Now take a tabular policy over a finite set of responses (the demo’s scenarios 1 and 2): with one logit per response, and fixed. Show that , where is the one-hot vector for response . Which logits move under gradient descent?
(c) In that tabular model with a single pair, show that a gradient step always increases , and that the ratio of any two responses outside the pair doesn’t change. So in this model, displacement needs something more. What? (Exercise 13.7 and the demo’s scenario 3 give two answers.)
Hint
Solution
(a) Let and . Then . The reference terms don’t depend on , so . By the chain rule, , and .
(b) : the normalizer’s gradient cancels in the difference. So . Only two logits move: up and down, by equal amounts. All other logits stay put.
(c) After the step, , with . Then , and
since . So always rises. For outside the pair, , and neither logit moves, so the ratio is unchanged (both probabilities shrink by the same factor as grows). Displacement needs either several pairs that share responses, so one response is pushed up by one pair and down by another (Exercise 13.7, demo scenario 2), or shared parameters, so moving one response’s logit moves others (demo scenario 3, a real network).
Fix one pair and let be the fraction of annotators who prefer . Write for the raw log-ratio margin, where .
(a) Write the expected DPO loss on this pair as a function of and find its minimizer. What happens as ? Does stop this?
(b) If the labels come from Bradley–Terry with true reward , so , show the minimizer satisfies . What does that say about the policy DPO converges to when every pair of responses is labeled this way (demo scenario 1)?
(c) The expected IPO loss is . Find its minimizer and evaluate it at .
(d) A real dataset labels each pair once. Explain why (a) is the relevant case, even when annotators disagree a lot.
Hint
Solution
(a) With : . Then . It vanishes at , i.e. , and , so this is the minimum. So . As , . At , for every : the loss decreases forever and has no minimizer. doesn’t help: for any , must go to infinity. A larger makes the loss saturate sooner in , but the infimum is still only approached as . Because , the chosen log-ratio is capped at , so forces .
(b) With , where , the minimizer is . So . If this holds for every pair of responses, then is the same constant for all , i.e. : the policy is the RLHF optimum . (In a tabular model all pairwise conditions can be met at once, and that’s what the demo’s scenario 1 shows.)
(c) : with . Setting it to zero gives , a finite value for every (the loss is a convex quadratic). At , : the larger , the smaller the target margin, so acts as a leash again. In the demo’s implicit-reward units, , the value where IPO’s margin stops.
(d) With one label per pair, the empirical of every pair is exactly 1 (or 0). Minimizing the empirical loss is minimizing a sum of terms, each of which wants an infinite margin, whatever the true annotator agreement was. Only early stopping, a small learning rate, and the model’s limited capacity keep the margins finite. This is Azar et al.’s argument for IPO.
(a) For fixed , expand the DPO loss of one pair to first order in around . What does the gradient become, and why might training on it for a long time be dangerous? (The DPO paper reports what happens.)
(b) As , what happens to the gradient weight once the policy has moved even slightly in the right direction, and what policy does training end at?
(c) Connect both answers to the optimum : which policy does DPO target in each limit?
Hint
Solution
(a) With and , the loss is . Its gradient is : every pair gets the same weight , forever, regardless of how well the pair is already ranked. This is the “naive” objective (raise the winner, lower the loser, with no stopping point) that the DPO paper reports can make the model degenerate: the log-probability of the loser can be pushed toward , dragging shared tokens and probability mass with it.
(b) The weight is . For any fixed , as this goes to . So as soon as the policy ranks a pair correctly by any tiny log-ratio margin, the pair stops contributing. Training ends with the policy almost exactly at , just nudged enough to get the orderings right.
(c) In the target : gives (the leash is infinitely tight), matching (b). gives concentrated on the highest-reward response (no leash), which requires infinite log-ratios, matching the unbounded push in (a). In both limits, the loss’s behavior and the target’s behavior agree.
A tabular policy over three responses A, B, C starts at the uniform reference, , with logits . The dataset has two pairs, each labeled once: B ≻ A and C ≻ B. B is the chosen response in the first pair.
(a) Using Exercise 13.4(b), write the gradient of the total loss (the mean over the two pairs) with respect to at the start. Which logits move?
(b) Argue that throughout gradient descent the two margins stay equal, so for some growing . Write as a function of and evaluate it at .
(c) Training reports 100% reward accuracy and growing margins. What happened to the “chosen” response B, and where did its probability go?
Hint
Solution
(a) Both margins are 0 at the start, so both weights are . The gradient of the mean loss is . Gradient descent raises , lowers by the same amount, and leaves unchanged: the push up from pair 1 and the push down from pair 2 cancel.
(b) Suppose . The two margins are and , equal, so the two weights are equal, and by the same calculation as in (a) the gradient is again proportional to . So the form is preserved, with increasing. Then
At : . At : . At : . It decreases monotonically in .
(c) Both pairs are ranked correctly, with growing margins, yet B, the winner of the first pair, falls from to at , and on toward 0. Its probability went to C (and A’s too): at . A response can be “chosen” in the data and still lose probability, because the loss only sees margins. In this example the outcome is reasonable (C beat B, so C is the better answer). With shared parameters the mass can go somewhere unreasonable instead, which is scenario 3 in the demo.
(a) Scenario 1, DPO, β = 1. Train to the end. Check π_θ, the implicit rewards and the mean margin against the worked example in “The trick, with numbers”. Then set β = 0.3 and train again. What changes, and what stays the same?
(b) Scenario 2, DPO. Compare β = 0.1 and β = 1. Where does each end up? Then switch to IPO with β = 1 and compare with π*. Which response is never learned about, and why?
(c) Scenario 3, DPO, β = 1. Describe the curve of log π_θ(“No.”). Which response wins, and how does its true reward compare with the others’? Now try DPO + NLL, then IPO. Which one fixes the problem?
Solution
(a) At β = 1, π_θ converges to (0.179, 0.292, 0.529), exactly the π* ticks. The implicit rewards converge to (−1.027, −0.027, 0.973), which is with , the table in the text. The mean margin over the three pairs converges to (1 + 1 + 2)/3 = 1.333, the mean true reward gap, and KL(π_θ ‖ π*) goes to 0. At β = 0.3, π* is much sharper (C gets most of the mass), and DPO follows it. But the implicit rewards still differ by 1 and 2, and the mean margin is still 1.333. DPO learns the reward differences, whatever β, and β only decides how strongly the policy acts on them.
(b) Both go to “all mass on C”, with B and D falling too. With deterministic labels, β changes the speed (how long the curves take) but not the destination, as Exercise 13.5 showed. The margin keeps growing. With IPO at β = 1 the margin stops at 0.5 and the policy stays close to the reference: about (0.25, 0.31, 0.34, 0.10). In both cases D, which has the highest true reward, is never learned about: it appears in no pair, so no gradient ever targets its logit, and it only loses probability through normalization. π* gives D the most mass. The data simply never asked about it: this is DPO being offline.
(c) log π_θ(“No.”) first rises for a few dozen steps, then falls steadily, while “Yes.” (true reward −3, by far the worst) rises to over 90% of the mass. DPO + NLL fixes it: “No.” goes toward probability 1, because the NLL term pulls θ toward the chosen response’s features while DPO separates it from “Never.”. IPO limits the damage because the margin target is finite, but at β = 1 “Yes.” still about doubles its probability (0.05 → 0.10): with a feature-based policy, any movement along the direction φ(No.) − φ(Never.) helps “Yes.”.
In dpo.py, implement sequence_logprob (Task 1), implicit_reward and dpo_loss (Task 2), then ipo_loss and simpo_loss (Task 3). The tests check the worked example from this chapter (, log-probs , loss ) and the gradient , IPO’s zero at margin , and numerical stability at a margin of .
Hint
torch.nn.functional.logsigmoid; torch.log(torch.sigmoid(u)) underflows to -inf for very negative u.Solution
The reference solution (solutions/dpo.py) is a few lines per function:
def sequence_logprob(token_logp, mask, average=False):
total = (token_logp * mask).sum(dim=-1)
return total / mask.sum(dim=-1).clamp(min=1.0) if average else total
def implicit_reward(policy_logp, ref_logp, beta):
return beta * (policy_logp - ref_logp)
def dpo_loss(pw, pl, rw, rl, beta):
r_w, r_l = implicit_reward(pw, rw, beta), implicit_reward(pl, rl, beta)
return -F.logsigmoid(r_w - r_l).mean(), r_w.detach(), r_l.detach()
def ipo_loss(pw, pl, rw, rl, beta):
h = (pw - rw) - (pl - rl)
return ((h - 1.0 / (2.0 * beta)) ** 2).mean()
def simpo_loss(avg_w, avg_l, beta, margin):
return -F.logsigmoid(beta * avg_w - beta * avg_l - margin).mean()Key points: the mask multiplication comes before the sum (padding positions can hold anything); IPO’s has no in front, while its target does; F.logsigmoid is computed stably as ; and the implicit rewards are returned detached because they are for logging only.
Implement dpo_step (Task 4): policy log-probs with gradients, reference log-probs under torch.no_grad(), the chosen loss, an optional NLL term on the chosen response, one optimizer step, and the diagnostics dict. Then run python run.py, which builds about 2,200 offline pairs (a correct answer preferred to a wrong one, both sampled from the SFT TinyLM) and trains four variants on them.
(a) Which variant lowers answer accuracy? What happens to its held-out , and how is that possible when its reward accuracy on training pairs is above 90%?
(b) Explain why β = 1 behaves so differently from β = 0.1 here, using the gradient weight .
(c) Which of the three fixes in the run correspond to which section of the chapter?
Hint
Solution
The core of the reference dpo_step:
tok_w, mask_w = response_logprobs(policy, prompts, chosen)
tok_l, mask_l = response_logprobs(policy, prompts, rejected)
pi_w, pi_l = sequence_logprob(tok_w, mask_w), sequence_logprob(tok_l, mask_l)
with torch.no_grad():
ref_w = sequence_logprob(*response_logprobs(ref, prompts, chosen))
ref_l = sequence_logprob(*response_logprobs(ref, prompts, rejected))
loss, r_w, r_l = dpo_loss(pi_w, pi_l, ref_w, ref_l, cfg.beta) # or ipo / simpo
if cfg.nll_coef > 0:
loss = loss - cfg.nll_coef * sequence_logprob(tok_w, mask_w, average=True).mean()
opt.zero_grad(); loss.backward(); opt.step()followed by the stats: (r_w > r_l).float().mean(), (r_w - r_l).mean(), pi_w.mean(), pi_l.mean() as Python floats.
(a) With the reference solution, DPO β = 0.1 drops greedy accuracy from 0.76 to about 0.38, and its held-out falls from −0.67 to about −2.5, while falls much further (to about −9.8). The margin, which is all the loss sees, keeps growing, so the ranking is right; but both responses lose probability, and the freed mass goes to other wrong answers that appear in no pair (for example, the model starts answering 41+19= with 71). This is likelihood displacement: the right answer is “preferred” and less likely at the same time.
(b) A pair stops contributing once is small, say (weight 0.05). With β = 1 that needs a raw log-ratio margin nats (natural-log units), which the model reaches quickly; then the gradients fade and the policy stays near the reference. With β = 0.1 it needs nats. The optimizer keeps pushing hard on every pair for many epochs over the same small dataset, and the cheapest way to grow is to crush the rejected answers and everything that shares parameters with them, which includes the correct answers’ neighbors and the correct answers themselves. (On a dataset this small, with this many epochs, a small β is a weak leash, as the “deterministic preferences” section predicts.)
(c) DPO β = 1: a tighter leash, the “deterministic preferences” section (and Exercise 13.6). DPO β = 0.1 + NLL: the likelihood-displacement fix (Llama 3, Pang et al.); it does best here, about 0.91 greedy accuracy, with rising to −0.18. IPO β = 0.1: the finite-target fix from the variants section; about 0.84 greedy accuracy.
Files: README.md, dpo.py, prefs.py, test_dpo.py, run.py
cd 13-dpo
python -m pytest -q # your code: tests fail until you fill in the TODOs
RL_SOLUTIONS=1 python -m pytest -q # reference solutions: all passFirst time? See Exercises & setup.
Further reading
- Rafailov et al. (2023), Direct Preference Optimization: Your Language Model is Secretly a Reward Model. The original paper. Section 4 is the derivation, section 5 the theory (equivalence classes), and the appendix has the Plackett–Luce version and the gradient analysis.
- Azar et al. (2023), A General Theoretical Paradigm to Understand Learning from Human Preferences. The ΨPO framework and IPO; the clearest statement of why deterministic preferences break DPO’s regularization.
- Razin et al. (2025), Unintentional Unalignment: Likelihood Displacement in Direct Preference Optimization. Why chosen log-probs fall, the “No”/“Never”/“Yes” example, and a data-filtering fix.
- Xu et al. (2024), Is DPO Superior to PPO for LLM Alignment? A Comprehensive Study. The theorem that DPO’s solutions include PPO’s and more, plus the PPO tuning that made it win.
- Ivison et al. (2024), Unpacking DPO and PPO: Disentangling Best Practices for Learning from Preference Feedback. A careful controlled comparison; the lesson that data quality matters most.
- Meng, Xia & Chen (2024), SimPO: Simple Preference Optimization with a Reference-Free Reward, and Ethayarajh et al. (2024), KTO: Model Alignment as Prospect Theoretic Optimization. The two most widely used variants, each well written.
- Guo et al. (2024), Direct Language Model Alignment from Online AI Feedback, and Tajwar et al. (2024), Preference Fine-Tuning of LLMs Should Leverage Suboptimal, On-Policy Data. Why on-policy data matters, from two angles.
- Lambert et al. (2024), Tülu 3: Pushing Frontiers in Open Language Model Post-Training. A fully open recipe with a DPO stage, its hyperparameters, and DPO-vs-PPO-vs-SimPO comparisons on the same data.
Next, Chapter 14 asks where the preferences and rewards come from in the first place: human labelers, AI feedback and constitutions, LLM judges, rubrics, and verifiers.