9.3 Direct Preference Optimization
Same pairs. Implicit r = β log(π/π_ref). Z(x) cancels. Offline.
These notes match the lecture slides. Use (slides) on the course hub for the deck.
Direct Preference Optimization (DPO) (Rafailov et al., 2023) fits a policy to chosen/rejected pairs without training an RM and without PPO. The reward is written in closed form from the policy and a frozen reference, then the Bradley–Terry model becomes a classification loss on logits.
1. What DPO is
RLHF solves with a learned RM and an on-policy loop. DPO uses the same preference pairs and skips both the RM and PPO. Under Bradley–Terry, the optimal reward can be written from the policy and a frozen reference,
Plug that into the RM logistic loss and the parameters disappear. You train directly, offline. The derived reward has a partition that cancels because Bradley–Terry only sees a difference. KTO and IPO sit in the same family; this hour is DPO.
If , both log-ratios are 0, the argument of is 0, and . Then . That is the start of training, not a bug. Lab 9 will compute this on toy scalar logits, not on an LLM.
2. Why we use it
RLHF’s PPO stage is heavy: sampling, a critic, a moving policy, stale rollouts. DPO is a classification loss on logits of and . You have pairs, a GPU, and no appetite for a PPO stack. Offline: the pair set is fixed, so you do not explore new unless you collect more.
That is also the limit. If the SFT model never proposed good , DPO cannot invent them from nothing. PPO can sample new that were not in the pair file; offline DPO cannot. Implicit reward can still overfit the pair set. Report , the reference checkpoint, and a held-out preference accuracy (how often assigns higher log-prob to than to ). That number is not “the model is aligned”; it is “the model copied this rater.”
3. Architecture
Two networks, one frozen: policy and reference (usually SFT). No critic. No sampling-in-the-loop required. You need log-probabilities of both completions under and under . The reference stays frozen. Dropping because “there is no RM” removes the leash; the reference is the leash.

The data path is the same pair file as note 9.1. The compute path is a forward on and a forward on for each of and (four logprob sums, or two if you cache the frozen reference). There is no RM head.
4. How it works, step by step
- Start from SFT. Freeze that checkpoint as . Copy it as .
- For each pair , compute log-prob of both completions under both models.
- Form the two log-ratios and subtract loser from winner. Scale by .
- Push that difference through and take . Gradient raises relative mass on versus , compared with the reference.
- Absolute log-prob can fall on both sequences; relative log-prob is what DPO scores. Write the argument of as if it helps.
At init, and the loss is . After a few steps the winner log-ratio should exceed the loser log-ratio. The frozen reference is still there: without it, can raise both log-probs or collapse. is the same kind of leash as in RLHF: it scales how hard you push away from .
5. Mathematical formulas
Closed-form reward, then DPO loss:
If , both log-ratios vanish and . After a few steps, toy log-probs (sums over tokens): , , , . Log-ratio winner: . Log-ratio loser: . Difference . , loss .
6. Positive points and negative points
Positive.
- Offline classification loss: no RM head, no PPO loop, no on-policy sampling.
- Same pair file as the RM lecture; the closed-form is why the RM can disappear.
- Init loss is a checkable number (Lab 9).
- is still an explicit leash toward .
Negative.
- Still needs pairs and a frozen reference. Dropping the reference is not a simplification.
- Can overfit the pair set. Held-out preference accuracy is “copied this rater,” not “aligned.”
- Not magic if the pairs are noisy. Two wrong answers still define an order.
- Offline: cannot invent replies the SFT model never wrote. PPO can sample new .
- Absolute log-prob can fall on both sequences; if you only watch that, you will think training failed.
When not to. If you need on-policy exploration of completions that are not in the file, stay with RLHF. If you have no SFT reference, you do not have DPO’s leash.
7. Teaching this note
About 35–40 minutes at the board: write implicit , plug into , compute . Play the Deep Dive preference / RLHF segment 2:48:26–3:09:39. Lab 9 section 2 is the toy DPO scalar.
8. Worked example
Let . Suppose at init . Then
so .
After a few steps, toy log-probs (sums over tokens): , , , . Log-ratio winner: . Log-ratio loser: . Difference . , loss .
The frozen reference is still there: without it, can raise both log-probs or collapse. PPO can sample new that were not in the pair file; offline DPO cannot.
9. Where students get stuck
- Forgetting when at step 0.
- Dropping because “there is no RM.” The reference is the leash.
- Expecting DPO to invent replies the SFT model never wrote.
10. Video
Karpathy — Deep Dive into LLMs like ChatGPT. Play the preference segment 2:48:26–3:09:39 (RLHF). Pause on comparison data: DPO uses those same pairs, without the PPO loop. The closed-form loss is the board.
11. Practice
-
If , what is ? (Compute .)
-
Why do you still keep a frozen reference if there is no RM?
-
Name one thing PPO can do that offline DPO cannot, given a fixed pair file.
-
, winner log-ratio , loser log-ratio . What is the argument of , and is the loss smaller or larger than ?
-
Both completions have log-prob under and under . What is the DPO loss, and what did the policy fail to learn yet?