- 1Forward the policy on (prompt + rejected) to get `logπ_θ(rejected)`
- 2Sample a batch of (prompt, chosen_response, rejected_response) triples
- 3Forward the frozen reference on (prompt + rejected) to get `logπ_ref(rejected)` (no grad)
- 4Compute the DPO loss `−log σ(β · log-ratio difference)` and backprop into the policy only
- 5Forward the frozen reference on (prompt + chosen) to get `logπ_ref(chosen)` (no grad)
- 6Form the log-ratio difference: `[logπ_θ(c) − logπ_ref(c)] − [logπ_θ(r) − logπ_ref(r)]`
- 7Forward the policy on (prompt + chosen) to get `logπ_θ(chosen)`
Sample a (prompt, chosen, rejected) triple, run four forwards (two policy, two frozen reference), form the log-ratio gap on chosen minus the same gap on rejected, then backprop the sigmoid loss into the policy only.
Imagine grading two essays a student wrote on the same prompt, one good, one bad. You also keep last week's version of the student to use as a reference baseline. You ask both students to score each essay and write down four scores. Then you check: did the new student get more excited about the good essay relative to the old student than they did about the bad essay relative to the old student? If yes, great, keep nudging in that direction. If not, push harder so the gap grows. DPO is just that comparison made math: four scores, one preference signal, gentle updates only to the new student so they do not forget who they were.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: four forwards (two policy, two frozen reference) + sequence log-prob sum + log-ratio gap difference + sigmoid loss with β + policy-only backprop + memory trade-off.
| Step | Model | Input | Gradient? |
|---|---|---|---|
| 1 | n/a | Sample (prompt, chosen, rejected) triple | n/a |
| 2 | Policy θ | prompt + chosen | yes |
| 3 | Policy θ | prompt + rejected | yes |
| 4 | Reference (frozen) | prompt + chosen | no |
| 5 | Reference (frozen) | prompt + rejected | no |
| 6 | n/a | Form log-ratio gap difference | n/a |
| 7 | Policy θ only | Backprop sigmoid loss | yes |
Real products, models, and research that use this idea.
- Hugging Face TRL's `DPOTrainer` is the canonical 2026 implementation; it runs the four forwards, masks the prompt, and supports sharing the reference across DP ranks.
- Llama 4 and DeepSeek V4 chat variants use DPO or its IPO / KTO descendants as the preference-alignment stage after SFT.
- Axolotl and LLaMA-Factory expose DPO recipes that load a reference model alongside the policy and stream four-forward batches.
- Zephyr 7B (HuggingFaceH4) was an early high-profile demonstration that DPO on UltraFeedback could match PPO-RLHF quality with simpler infra.
- Anthropic and Mistral report internal preference-alignment pipelines that ablate DPO, IPO, and KTO on top of their SFT checkpoints, all sharing the four-forward shape.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat does β actually control, and what happens at very large or very small β?
QWhy does DPO need no explicit reward model where PPO-RLHF does?
QHow do IPO and KTO modify the DPO loss, and what failure modes do they target?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Forgetting that the reference model needs both a chosen and a rejected forward, not one shared call. Without both reference log-probs you cannot form the gap difference DPO is built on.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.