[LLM 4/10] DPO: When a Language Model Becomes Its Own Reward Model
In the last chapter we did RLHF with PPO, and you saw how many moving parts it has — a separate reward model to train, four models resident in VRAM at once, a dozen-plus PPO hyperparameters to tune, and if the reward model is off, the policy finds a shortcut that games the score. In this chapter we'll accomplish the same thing with an ordinary supervised training loop — no reward model, no RL. And crucially, this is not an approximation. We'll prove algebraically that those two pieces genuinely cancel out.
Open in Colab04_dpo.ipynb
1. The Problem
Say you want your AI assistant to "always answer in Thai" — sounds simple. Now try writing that as a loss function. You can't.
This is the central problem of alignment: answer quality cannot be written as an equation. "More polite," "more natural," "doesn't drift into English" — there is no single correct answer key. There is only comparison: show a human two answers and ask which one they prefer. So the data comes in threes: a prompt , a preferred answer (chosen), and a dispreferred one (rejected).
PPO-style RLHF solves this by taking a two-step detour:
- Train a reward model to imitate human preferences
- Use RL to push the policy toward high scores from that reward model
The detour has a price:
| Problem with RLHF/PPO | What it costs you in practice |
|---|---|
| An extra model to train | More steps, more ways to fail, more time |
| Four models loaded at once | policy + ref + reward + value — VRAM explodes |
| Reward hacking | The model finds a loophole that scores well without humans liking it any more |
| PPO is hyperparameter-sensitive | Two runs with different seeds can tell completely different stories |
So this chapter asks one short question: can we skip steps 1 and 2 entirely?
2. What We're Going to Do
Yes, we can — and the reason is beautiful.
The starting point is the observation that the KL-constrained RLHF objective has a closed-form solution. We already know exactly what the optimal policy looks like, without running a single step of RL. Knowing that, we invert the equation — instead of asking "what policy does this reward produce?" we ask "what reward does this policy imply?"
Once you invert the equation, the language model already is a reward model, implicitly.
The reward model and the RL loop aren't "approximated away" — they cancel out algebraically.
What's left is an ordinary supervised loss function you can train with a single Trainer.
This is DPO (Direct Preference Optimization), proposed by Rafailov et al. (2023). The "Direct" comes from optimizing on preference data directly, with no intermediary.
3. The Equations
3.1 Setting up: the RLHF objective
In plain language: "maximize the reward, but don't wander too far from where you started."
- = the policy, i.e. the model we're training
- = the reference policy, i.e. the starting model (here, the post-SFT model from chapter 2)
- = the reward for answer given prompt
- = how tight the leash is; higher values pull harder back toward
The KL term is not decoration. Without it the model runs off to wherever reward is highest and the language falls apart.
3.2 Step 1 — the closed-form solution
That problem can be solved by hand (it amounts to finding the distribution that minimizes KL against a target distribution), giving
- is the partition function, the denominator that makes everything sum to 1
- Note that depends only on , not on — hold on to that sentence, it's about to become the hero of the story
The intuition: the optimal policy is the original model, reweighted by . High-reward answers get their probability amplified, low-reward answers get pushed down, but everything starts from the original shape of .
In practice we can't compute , because it requires summing over every possible answer in the universe. That's why people reach for RL — and it's exactly why DPO doesn't have to.
3.3 Step 2 — invert the equation to get the reward
Take the log of both sides and rearrange:
This line is the crux: any reward function can be rewritten in terms of the optimal policy and the starting policy. Which means that if we have two models, we can compute their implicit reward immediately, without ever training a reward model.
3.4 Step 3 — substitute into Bradley-Terry and vanishes
The standard model of preference is Bradley-Terry: the probability a human picks over is
where is the sigmoid. Notice that reward appears in this equation only as a difference. Substitute equation 3.3 — appears identically on both sides because it's the same — so it cancels out.
The uncomputable thing () disappears, because Bradley-Terry only cares about the difference of rewards. What remains is the log-probability of two models on text we already have, obtainable with an ordinary forward pass. No sampling of answers, no rollouts, no value function — DPO is supervised learning, all the way down.
3.5 The gradient — where the intuition lives
where is called the implicit reward.
Read it piece by piece:
- The right-hand parenthesis = the direction: push the log-prob of up and the log-prob of down, simultaneously
- = the weight: "how badly is the model ranking this pair?"
That weight is the most important teaching point here. If the model already ranks the pair correctly ( clearly greater than ), then approaches zero and the pair contributes almost no gradient. DPO therefore focuses on its own mistakes automatically, with nobody curating the data for it.
This post is roughly the first 30% of the chapter. The rest — environment setup, data preparation, the main code, measured results and the wrap-up — is in the free LLM Finetuning course. Sign in with Google to read it.
Read the full lesson in the course →