Skip to content

5.9 Preference learning: RLHF and DPO

ready≈ 14 hoursNeeds: 5.8, 1.5Unlocks: 5.10
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (DPO explorer)
  4. 4 Do
  5. 5 Check

Comparisons. Writing the ideal answer is hard; saying which of two answers is better is easier and more consistent. The Bradley–Terry model turns comparisons into a scalar score: P(a preferred to b) = σ(r(a) − r(b)), module 2.3’s logistic regression, trained with 1.5’s cross-entropy, on score differences. A reward model is a language model with a scalar head, trained on human comparisons with this loss (Christiano et al. 2017 introduced the approach for deep RL).

RLHF. InstructGPT then optimised the SFT model with PPO to maximise the reward model’s score, minus β times the KL divergence from the SFT model, so that the policy cannot drift into text that fools the reward model (Ouyang et al. 2022, section 3). This needs four models in memory (policy, reference, reward model and a value function) and sampling during training.

DPO. That objective has a closed-form optimum: the reference reweighted by er/β. Solving it for r expresses the reward through the policy itself, and substituting into Bradley–Terry gives a classification loss on preference pairs. No reward model, no sampling: just two forward passes per model per pair (Rafailov et al. 2023, sections 4–5). The gradient weights each pair by how wrongly the policy currently ranks it.

Caveats. DPO is trained on fixed pairs, usually collected from other models; pairs sampled from the policy being trained (on-policy data) tend to help, as do repeated rounds. With few or deterministic preferences it can push probabilities to extremes, which is what β and early stopping restrain. CS336 Assignment 5 implements the per-pair DPO loss (assignment5-alignment).

From the RLHF objective to the DPO lossstep 1 of 6
  1. max⁡π Ey∼π[r(y)]−β KL(π ∥ πref)\max_{\pi}\ \mathbb{E}_{y \sim \pi}[r(y)] - \beta\, \mathrm{KL}(\pi \,\|\, \pi_{\text{ref}})

DPO explorer

What preference optimization pushes

Static view on small screens. Open this page on a laptop to interact.

User: My code throws KeyError: "name". What should I check?

preference pairs:
0 steps
Check that the dict has the key "name" before reading it, or use d.get("name"). reward +2.0
18%18%62%
The key "name" is missing. Print the dict's keys to see what is there. reward +1.6
22%22%34%
KeyError means a key is missing. reward +0.4
30%30%4%
Try restarting your computer. reward -1.5
10%10%0%
Use a try/except around everything and ignore errors. reward -1.0
12%12%0%
I cannot help with that. reward -2.0
8%8%0%
SFT model (reference) DPO policy RLHF optimum ref·er/β/Z
DPO loss 0.693; pairs ranked correctly by the implicit reward: 0% expected true reward: SFT 0.40 → policy 0.40 (optimum 1.79); KL(policy ‖ SFT) = 0.000

Pairs of replies are sampled from the SFT model and labelled by a judge whose preferences follow the hidden rewards (Bradley–Terry). DPO raises the log-probability of each chosen reply relative to the reference and lowers the rejected one, weighting pairs the policy still gets wrong. With many pairs it converges to the policy RLHF would find, the reference reweighted by er/β; with only a few pairs it overfits to the ones it saw. Small β lets the policy move far from the SFT model; large β keeps it close.

Six candidate replies to one coding question, each with a hidden reward and three bars: its probability under the SFT reference model, under the policy being trained with DPO, and under the RLHF optimum, the reference reweighted by e^(r/β). Buttons choose the number of preference pairs (10, 200 or 2,000, sampled from the reference and labelled by a Bradley–Terry judge) and train DPO for 25 or 500 steps or reset; a slider sets β. The readout gives the DPO loss, the fraction of pairs ranked correctly, the expected true reward and the KL divergence from the reference. Keyboard: Tab to the slider and buttons.
Bradley-Terry rewards, the KL-regularized optimum and the DPO loss stubs/m5_9_dpo.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m5_9_dpo.py and run mlc check 5.9. The tests are identical; import the results on the progress page.

Hints · fit_bradley_terry 0/3 revealed
Hints · dpo_loss 0/3 revealed

Then run DPO on your model. Open notebooks/5.9-dpo.ipynb on Kaggle with a GPU and paste in your 4.6, 5.8 and 5.9 files. It pretrains and fine-tunes your GPT as in 5.8, collects on-policy pairs (the correct answer against the model’s own wrong one), checks a PyTorch DPO loss against your functions, trains with it, and compares accuracy on held-out words before and after.

1. A Bradley–Terry reward model is trained on…
2. In RLHF and DPO, a smaller β…
3. Why does the partition function Z not appear in the DPO loss?
4. What does DPO avoid that PPO-based RLHF needs?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.