Part XVII — LLM Post-Training: SFT, DPO, GRPO & Reasoning / Test-Time Compute · Chapter 4

Direct Preference Optimization (DPO)

Hook

Part VII's RLHF — short for Reinforcement Learning from Human Feedback — chapter fit a reward model from preference comparisons, then ran a separate policy-gradient loop against it — two stages, two different update rules. What if the algebra let both stages collapse into one?

Intuition

Apply each comparison. The policy shifts after every single one — no reward model sits in between the preference data and the policy update at all.

Formalize

For an optimal policy under a KL (Kullback–Leibler)-regularized reward objective, the reward and the policy are related in closed form: r(y)=βlog(π(y)/πref(y))+constr(y) = \beta \log\big(\pi(y)/\pi_{\text{ref}}(y)\big) + \text{const}. Substitute that directly into the Bradley-Terry preference loss from Part VII, and the reward model cancels out algebraically, leaving a loss purely in terms of the policy:

L=logσ(β[(logπ(yw)logπref(yw))(logπ(yl)logπref(yl))])\mathcal{L} = -\log\sigma\Big(\beta\big[(\log\pi(y_w) - \log\pi_{\text{ref}}(y_w)) - (\log\pi(y_l) - \log\pi_{\text{ref}}(y_l))\big]\Big)
  • L\mathcal{L} — the DPO loss being minimized for a single preference pair.
  • β\beta — the KL-penalty strength inherited from the RLHF objective; controls how far the policy is allowed to drift from the reference.
  • π\pi — the policy currently being trained.
  • πref\pi_{\text{ref}} — the frozen reference policy (typically the pre-RLHF model) the trained policy is measured against.
  • ywy_w — the winning (preferred) response in the comparison.
  • yly_l — the losing (dispreferred) response in the comparison.
  1. The reward model's term cancels in the gradient

    Differentiating this with respect to the policy's own parameters, the softmax's π(a)\pi(a) term cancels exactly between the winner and loser gradients.

  2. What's left is a single-stage update

    One gradient step per preference pair, straight on the policy — no reward model and no separate RL update rule required, simpler than RLHF's two-stage procedure.

Play

Two completely different training procedures — fit-a-reward-model-then-run-RL versus one direct gradient loop — landing on the same ranking from the same three comparisons. Neither path is "more correct"; DPO — short for Direct Preference Optimization — is just a shortcut through algebra that happens to skip a stage RLHF needs.

Worked example

The very first comparison, "B beats A", starting from a uniform policy:

  1. At initialization, the policy IS the reference

    π=πref\pi = \pi_{\text{ref}} everywhere, so logπ(yw)logπref(yw)=0\log\pi(y_w)-\log\pi_{\text{ref}}(y_w) = 0 for every arm. The bracketed term is 00=00 - 0 = 0, so h=β0=0h=\beta \cdot 0 = 0 and σ(0)=0.5\sigma(0)=0.5.

  2. The gradient step

    The update is θθ+η(1σ(h))β(1[a=yw]1[a=yl])\theta \to \theta + \eta\,(1-\sigma(h))\,\beta\,(\mathbb{1}[a{=}y_w]-\mathbb{1}[a{=}y_l]), with learning rate η=0.5\eta=0.5 and β=0.5\beta=0.5. With σ(h)=0.5\sigma(h)=0.5, the update factor (1σ(h))=0.5(1-\sigma(h))=0.5 pushes θB\theta_B up and θA\theta_A down by the same amount:

    • θB0+0.5η(0.5)1σ(h)(0.5)β(1)=0.125\theta_B \to 0 + \underbrace{0.5}_{\eta}\underbrace{(0.5)}_{1-\sigma(h)}\underbrace{(0.5)}_{\beta}(1) = 0.125
    • θA00.125=0.125\theta_A \to 0 - 0.125 = -0.125
  3. Nothing else moved

    θC\theta_C is untouched — this comparison never mentioned C, and DPO's update (like RLHF's) only touches the two arms actually being compared.

Checkpoint

Find the arm, among the three, with the lowest preference parameter after DPO training.

Pick an arm to try it
Summary
θL=(1σ(h))β(1[a=yw]1[a=yl])\nabla_\theta \mathcal{L} = -\big(1-\sigma(h)\big)\,\beta\,\big(\mathbb{1}[a{=}y_w] - \mathbb{1}[a{=}y_l]\big)

DPO isn't a different objective from RLHF — it's the same Bradley-Terry preference objective, with the reward model's role folded into the policy's own log-ratio against a reference. That's the entire appeal: one training loop instead of two, no separate reward model to overfit or drift out of sync with the policy being trained against it. The next chapter asks where the preference data itself comes from in the first place — what happens when a model critiques its own answers instead of a human supplying every comparison.