$ attention.sh

knowledge base · entry 16

Your language model is secretly a reward model

A read-through of Direct Preference Optimization: Your Language Model is Secretly a Reward Model. Entry 15 built a four-model pipeline to learn from human preferences. This paper does the algebra that makes half of it vanish — same objective, no reward model, no RL loop, one supervised loss. It is why a two-GPU team can now afford preference tuning.

paper Direct Preference Optimization: Your Language Model is Secretly a Reward Model
authors Rafailov, Sharma, Mitchell, Ermon, Manning, Finn — Stanford
source arXiv:2305.18290
why we read it it is the current default for preference tuning — the algebraic step from InstructGPT’s pipeline to a loss you can run in an afternoon, and the root of the family (IPO, KTO, ORPO, SimPO) you will actually choose from

every figure zooms — click it, scroll to magnify, drag to pan

part one

The idea, before any algebra

figure 1 · two routes to the same place

Delete the middleman

RLHF’s route to “prefer the better answer” is long: fit a reward model to human comparisons, then run PPO — with a value network and a frozen reference copy along for the ride — to chase that reward. Four models in memory, a sampling loop, and PPO’s famous instability. DPO’s observation: the destination of that whole journey can be written down directly as a loss on the preference pairs themselves.

click any figure to zoom · scroll to magnify · drag to pan

Nothing about the goal changes — it is the same KL-leashed reward maximization as entry 15. What changes is that the reward model and the RL loop turn out to be scaffolding you can fold into the loss and throw away.

part two · the trick

The algebra that deletes the pipeline

figure 2 · the change of variables

Solve for the policy, then read it backwards

The KL-constrained reward objective has a known closed-form optimum: π*(y|x) ∝ πref(y|x)·exp(r(x,y)/β) — boost the reference model’s probabilities wherever reward is high. DPO’s move is to read that equation backwards: solve it for the reward, and every reward function becomes expressible through the policy it would produce: r(x,y) = β·log π(y|x)/πref(y|x) + β·log Z(x). The awkward term is Z(x), a sum over every possible answer — uncomputable. And here is the gift: human preferences under the Bradley–Terry model depend only on reward differences, and in a difference, Z(x) cancels.

That cancellation is the entire paper. Preferences compare two answers to the same prompt, so the one intractable object never needs to exist. What remains is written purely in probabilities the language model already computes.

figure 3 · the loss

One line, four probabilities

Substitute the reparameterized reward into the Bradley–Terry likelihood and out falls the DPO loss — a binary cross-entropy over preference pairs. For each triple (prompt, chosen answer yw, rejected answer yl), gather four numbers: the policy’s and the reference model’s log-probabilities of both answers. Push the chosen margin up, the rejected margin down, through a sigmoid, scaled by β.

Training is: freeze a copy of your SFT model as the reference, run both models over a batch of pairs, compute the loss, backpropagate. No sampling during training, no reward model, no value network — a preference stage that behaves like ordinary fine-tuning.

figure 4 · worked by hand

The loss on one toy pair

To feel the mechanics, run one pair through by hand with β = 0.5, in per-token-averaged log-probabilities. The model slightly prefers the rejected answer — so the implicit reward margin comes out negative, the sigmoid weight comes out large, and this pair teaches a strong lesson.

Run the same arithmetic on a pair the model already orders correctly and the weight shrinks toward zero — DPO spends its gradient where the model is wrong, which is precisely what keeps it from over-optimizing pairs it has already learned.

figure 5 · the gradient

A gradient that knows how wrong it is

Differentiate the loss and the structure is legible: increase the likelihood of the chosen answer, decrease the rejected one — with the whole update scaled by σ(r̂l − r̂w), the model’s own estimate of how badly its implicit reward mis-orders the pair. Confidently-correct pairs contribute almost nothing; confidently-wrong pairs dominate the batch.

part three · the evidence

Does the shortcut lose anything?

results · summarization

Matches PPO at its best — and doesn’t fall over

On Reddit TL;DR summarization, judged by GPT-4 against human-written references, DPO reaches about a 61% win rate at temperature 0 — above PPO’s 57% at its best temperature. The sharper finding is robustness: sweep the sampling temperature and PPO’s win rate collapses toward the base model’s, while DPO’s barely moves. On Anthropic helpful-harmless dialogue, DPO is the only computationally cheap method that improves on the dataset’s own preferred completions — roughly matching a 128-sample best-of-n that costs two orders of magnitude more compute at inference.

methodmachineryTL;DR win rateacross temperatures
DPOone loss, two model copies≈61%stays flat
PPO (RLHF)reward model + value net + sampling≈57% at its bestdegrades sharply
best-of-128128 samples + reward model, per querycompetitive— (unaffordable)
SFT onlyimitationwell below

part four · the descendants

The family it spawned

figure 7 · the DPO family

Pick by the data you can actually collect

DPO’s simplicity made it a foundation to iterate on, and the descendants are best told apart by one question: what feedback can you actually gather? Paired comparisons → DPO (or IPO if it overfits). Only independent thumbs-up/thumbs-down → KTO. Want to skip the separate SFT stage → ORPO. Memory too tight for a reference copy → SimPO. All of them are one-line trainer swaps in today’s tooling.

the finding that outranks the choice across this whole family, the quality of the preference pairs dominates the choice of loss. A better dataset beats a fancier objective almost every time — which is entry 14’s lesson, again, one stage later.

the math · in the paper's notation

The derivation, in three equations

The three lines the prose above walked around — the optimum, the loss, and the gradient, numbered as the paper numbers them. With these decoded, section 4 of the PDF reads in one pass.

the equations · worked

Solve, substitute, differentiate

eqs. (4)–(5) · the optimum, read backwards

πr(y|x)  =  1Z(x) πref(y|x) · exp( r(x,y) / β )

⟹    r(x,y)  =  β·log πr(y|x)πref(y|x)  +  β·log Z(x)

read aloud: the best KL-leashed policy boosts the reference model wherever reward is high — so, backwards, any reward is β times the log of how much a policy boosts the reference, plus a term that depends only on the prompt. That prompt-only term Z(x) sums over every possible answer and cannot be computed — and in the next line it will never need to be.

  • πrefthe frozen SFT model — InstructGPT's πSFT, renamed
  • βthe same KL leash strength as in entry 15's equation (2)
  • Z(x)the partition function: the normalizer over all answers to x — intractable, and about to cancel

eq. (7) · the DPO loss

DPO = −E [ log σ( β·log πθ(yw|x)πref(yw|x)β·log πθ(yl|x)πref(yl|x) ) ]

read aloud: substitute the backwards-read reward into the Bradley–Terry preference model, watch Z(x) cancel in the difference, and what remains is a binary cross-entropy on the margin between two log-ratios. Four log-probabilities per pair — policy and reference, chosen and rejected — and nothing else.

  • ywthe chosen answer of the pair…
  • yl…and the rejected one
  • β·log π/πrefthe implicit reward r̂ — the language model scoring answers without a reward model existing
plug in → figure 4's toy, β = 0.5, per-token-averaged log-probs: chosen log-ratio = −1.2 − (−1.0) = −0.2; rejected = −0.9 − (−1.0) = +0.1. margin = 0.5·(−0.2) − 0.5·(0.1) = −0.15 → σ(−0.15) ≈ 0.46 → loss = −ln 0.46 ≈ 0.77. the model mis-orders this pair, so it teaches hard.

eq. (8) · the gradient — where the intuition lives

∇ℒ = −β·E [ σ( r̂l − r̂w ) · ( ∇log πθ(yw|x) − ∇log πθ(yl|x) ) ]

read aloud: raise the chosen answer's likelihood, lower the rejected one's — with the whole step scaled by how badly the implicit reward mis-orders the pair. The green factor is figure 5's "knows how wrong it is" weight: near 0 on pairs already ordered correctly, near 1 on confident mistakes.

  • w, r̂lthe implicit rewards of the chosen and rejected answers, from the card above
plug in → the toy pair's weight: σ(+0.15) ≈ 0.54 — a strong lesson. a pair the model orders correctly by the same margin would weigh σ(−0.15) ≈ 0.46… and one it orders correctly by a margin of 2 would weigh σ(−2) ≈ 0.12 — the batch's gradient concentrates where the model is wrong.

part five

What we take from it

applied · our reading

Preference tuning for teams without an RL bench

Next in the arc: the other half of affordability. Preference tuning became one loss; entry 17 makes the hardware fit on one GPU.

source

Rafael Rafailov, Archit Sharma, Eric Mitchell, Stefano Ermon, Christopher D. Manning, Chelsea Finn. Direct Preference Optimization: Your Language Model is Secretly a Reward Model. arXiv:2305.18290. Figures on this page are our own; the derivation, win rates and robustness finding are the paper’s, the worked example uses toy numbers, and the family entries cite their respective papers (IPO, KTO, ORPO, SimPO).

← back to the knowledge base  ·  ← entry 15 · InstructGPT  ·  entry 17 · QLoRA →

attention.sh — a division of Jinacode Systems. Transformers, fine-tuning, and sovereign LLMs.
we're hiring the curious — hello@attention.sh