Reinforcement learning from human feedback works, but it is a complicated machine: train a reward model on human comparisons, then run a reinforcement learning algorithm (PPO) that samples from the model, scores the samples, and updates the policy, all while keeping several large models in memory and fighting training instability.
Direct Preference Optimization (DPO; Rafailov et al., 2023) reaches the same goal with a single, simple loss function on the preference data. Its subtitle summarizes the key insight: your language model is secretly a reward model. This reading walks through the derivation step by step and builds intuition for what the DPO loss does.
1From RLHF to DPO
Both methods start from the same preference data: prompts $x$, each with a preferred response $y_w$ (“winning”) and a dispreferred response $y_l$ (“losing”). They differ in what they do with it:
$(x, y_w, y_l)$→reward modelmaximum likelihood (Bradley–Terry)⇄LM policyreinforcement learning: sample, score, update
$(x, y_w, y_l)$→final LMmaximum likelihood: one classification loss
Notation. Following the DPO paper, we write $\pi_\theta(y\mid x)$ for the model being trained (the policy), $\pi_{\text{ref}}(y\mid x)$ for the frozen reference model it starts from (usually the supervised fine-tuned model), and $r(x,y)$ for a reward function. In the notation sometimes used for RLHF, these correspond to $p^{RL}_\theta$, $p^{PT}$, and $RM_\phi$.
Recall the two ingredients of RLHF:
- A reward model trained with the Bradley–Terry loss, so that preferred responses score higher: $$\mathcal{L}_R(r,\mathcal{D}) = -\,\mathbb{E}_{(x,y_w,y_l)\sim\mathcal{D}}\Big[\log\sigma\big(r(x,y_w)-r(x,y_l)\big)\Big].$$
- A KL-regularized objective for the policy: get high reward, but stay close to the reference model. $$\max_\pi\ \mathbb{E}_{x\sim\mathcal{D},\,y\sim\pi(y\mid x)}\big[r(x,y)\big] - \beta\,\mathbb{D}_{\text{KL}}\big(\pi(\cdot\mid x)\,\|\,\pi_{\text{ref}}(\cdot\mid x)\big).$$
2Step 1: the optimal policy has a closed form
The KL-regularized objective can be solved exactly. Write the KL as an expectation and rearrange:
$$\begin{aligned} &\max_\pi\ \mathbb{E}_{x}\,\mathbb{E}_{y\sim\pi(y\mid x)}\Big[r(x,y) - \beta\log\frac{\pi(y\mid x)}{\pi_{\text{ref}}(y\mid x)}\Big]\\ =\ &\min_\pi\ \mathbb{E}_{x}\,\mathbb{E}_{y\sim\pi(y\mid x)}\Big[\log\frac{\pi(y\mid x)}{\pi_{\text{ref}}(y\mid x)} - \frac{1}{\beta}r(x,y)\Big] &&\text{(divide by } -\beta\text{)}\\ =\ &\min_\pi\ \mathbb{E}_{x}\,\mathbb{E}_{y\sim\pi(y\mid x)}\Big[\log\frac{\pi(y\mid x)}{\frac{1}{Z(x)}\pi_{\text{ref}}(y\mid x)\exp\!\big(\tfrac{1}{\beta}r(x,y)\big)} - \log Z(x)\Big], &&\text{(add and subtract } \log Z(x)\text{)} \end{aligned}$$where the partition function
$$Z(x)=\sum_y \pi_{\text{ref}}(y\mid x)\exp\!\Big(\frac{1}{\beta}r(x,y)\Big)$$makes the denominator a proper probability distribution. Call that distribution
$$\pi^*(y\mid x) = \frac{1}{Z(x)}\,\pi_{\text{ref}}(y\mid x)\,\exp\!\Big(\frac{1}{\beta}r(x,y)\Big).$$The objective becomes $\min_\pi \mathbb{E}_x\big[\mathbb{D}_{\text{KL}}(\pi(\cdot\mid x)\,\|\,\pi^*(\cdot\mid x)) - \log Z(x)\big]$. Since $Z(x)$ does not depend on $\pi$, and a KL divergence is always $\ge 0$ with equality only when the two distributions are equal, the optimum is exactly
$$\pi = \pi^*.$$In words: the best policy reweights the reference model by $\exp(r/\beta)$. Responses with higher reward get more probability; $\beta$ controls how strongly.
Why not just use this formula directly? Because $Z(x)$ sums over every possible response, an astronomically large number of sequences. We cannot compute it.
3Step 2: write the reward in terms of the policy
Take the logarithm of the closed form and solve for the reward:
$$r(x,y) = \beta\log\frac{\pi^*(y\mid x)}{\pi_{\text{ref}}(y\mid x)} + \beta\log Z(x).$$This flips the relationship around: instead of “a reward defines a policy”, a policy defines a reward. For any policy $\pi_\theta$, we can define its implicit reward
$$\hat r_\theta(x,y) = \beta\log\frac{\pi_\theta(y\mid x)}{\pi_{\text{ref}}(y\mid x)}.$$It is positive when the policy likes a response more than the reference model does, and negative when it likes it less. (Figure 1's last column shows exactly this quantity.)
4Step 3: the DPO loss
Now combine the two pieces:
- a loss function on reward functions (Bradley–Terry), and
- a transformation between reward functions and policies (Step 2).
Substitute $r(x,y)=\beta\log\frac{\pi_\theta(y\mid x)}{\pi_{\text{ref}}(y\mid x)}+\beta\log Z(x)$ into the Bradley–Terry loss. The loss only uses the difference $r(x,y_w)-r(x,y_l)$, and both responses share the same prompt $x$, so
$$r(x,y_w)-r(x,y_l) = \beta\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)} + \cancel{\beta\log Z(x)} - \beta\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)} - \cancel{\beta\log Z(x)}.$$The intractable $Z(x)$ cancels. What remains is a loss function on the policy itself:
$$\mathcal{L}_{\text{DPO}}(\pi_\theta;\pi_{\text{ref}}) = -\,\mathbb{E}_{(x,y_w,y_l)\sim\mathcal{D}}\left[\log\sigma\left(\beta\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)} - \beta\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}\right)\right].$$Everything in this loss is easy to compute: the log-probability of a response under a language model is just the sum of its token log-probabilities. No reward model, no sampling, no reinforcement learning. Training the policy with this loss is the same as training a Bradley–Terry reward model whose “reward” is the implicit reward $\hat r_\theta$; that is why the language model is “secretly a reward model”.
5What the gradient does
Differentiating the DPO loss gives
$$\nabla_\theta\mathcal{L}_{\text{DPO}} = -\beta\,\mathbb{E}\Big[\underbrace{\sigma\big(\hat r_\theta(x,y_l)-\hat r_\theta(x,y_w)\big)}_{\text{higher weight when the implicit reward is wrong}}\Big(\underbrace{\nabla_\theta\log\pi_\theta(y_w\mid x)}_{\text{increase likelihood of }y_w} - \underbrace{\nabla_\theta\log\pi_\theta(y_l\mid x)}_{\text{decrease likelihood of }y_l}\Big)\Big].$$- Each step makes the preferred response more likely and the dispreferred response less likely.
- The weight $\sigma(\hat r_\theta(x,y_l)-\hat r_\theta(x,y_w))$ is large when the model currently ranks the pair the wrong way, and small once the pair is ranked correctly by a clear margin. Pairs the model already gets right contribute little.
6How well does DPO work?
A controlled experiment. The DPO paper first tests the methods where the true reward is known. The task is to generate positive movie reviews (IMDb) with a GPT-2 model. A pretrained sentiment classifier serves as the “gold” reward, preference pairs are created from it, and the policy is optimized with PPO and with DPO. Plotting the achieved reward against the KL divergence from the reference model shows that DPO reaches higher reward at every level of KL: it finds a better trade-off between “get high reward” and “stay close to the reference”.
Real tasks. On summarization and single-turn dialogue, the paper reports that DPO matches or exceeds PPO-based RLHF in win rates judged by GPT-4, while being much simpler to train.
At scale. DPO and its variants are now widely used in open models. For example, Meta's Llama 3 models are post-trained in several rounds, each consisting of supervised fine-tuning, rejection sampling, and DPO.
PPO vs. DPO
| RLHF (PPO) | DPO | |
|---|---|---|
| Reward model | trained separately | not needed (the policy is an implicit reward model) |
| Sampling during training | yes (online, from the current policy) | no (offline, fixed preference data) |
| Models in memory | policy, reference, reward, value | policy, reference |
| Training objective | maximize reward − β·KL with RL | classification loss on preference pairs |
| Implementation | complex, sensitive to hyperparameters | simple, similar to SFT |
| Stability | can be unstable | stable |
| Exploration | yes: learns from its own new outputs | no: limited to responses in the dataset |
DPO in practice
- The reference model $\pi_{\text{ref}}$ is usually the SFT model, kept frozen; the policy $\pi_\theta$ is initialized from the same model.
- $\beta$ controls how far $\pi_\theta$ can move from $\pi_{\text{ref}}$ (a common value is about 0.1). Small $\beta$ fits the preferences more aggressively; large $\beta$ stays closer to the reference.
- Each training example needs four log-probabilities: $\log\pi_\theta(y_w\mid x)$ and $\log\pi_\theta(y_l\mid x)$ from the trainable policy, and the same two from the frozen reference (which can be precomputed).
- Limitations: DPO only sees the responses in its dataset and can overfit them; carefully tuned online RL can still do better in some settings. Many variants (e.g., IPO, KTO, SimPO, online DPO) try to address these issues.
7Summary
- The KL-regularized RLHF objective has a closed-form optimum: $\pi^*\propto\pi_{\text{ref}}\exp(r/\beta)$, but its normalizer $Z(x)$ is intractable.
- Rearranging gives the reward in terms of the policy: $r=\beta\log(\pi^*/\pi_{\text{ref}})+\beta\log Z(x)$.
- Plugging this into the Bradley–Terry loss, $Z(x)$ cancels, giving the DPO loss on the policy directly.
- The gradient raises $\log\pi_\theta(y_w)$, lowers $\log\pi_\theta(y_l)$, and focuses on pairs the model currently ranks wrongly.
- DPO needs no reward model, no sampling, and only two models in memory; it is now a standard part of post-training.
8Practice
Exercise 1: the optimal policy
A prompt has two possible responses with $\pi_{\text{ref}}=(0.5, 0.5)$ and rewards $r=(2, 0)$. With $\beta=1$, what is $\pi^*$? (Use $e^2\approx 7.39$.)
Exercise 2: the DPO loss by hand
With $\beta=0.5$, a pair has $\log\frac{\pi_\theta(y_w)}{\pi_{\text{ref}}(y_w)}=1$ and $\log\frac{\pi_\theta(y_l)}{\pi_{\text{ref}}(y_l)}=-1$. Compute the implicit rewards, the loss, and the gradient weight. (Use $\sigma(1)\approx 0.73$.)
Exercise 3: why does $Z(x)$ cancel?
Would $Z(x)$ still cancel if the two responses in a pair came from different prompts $x_1$ and $x_2$?
Quick quiz
9Further reading
- Rafailov, Sharma, Mitchell, Ermon, Manning & Finn (2023). Direct Preference Optimization: Your Language Model is Secretly a Reward Model. NeurIPS. (Section 4 contains the derivation.)
- Hugging Face. TRL DPO Trainer documentation. (How DPO is implemented in practice.)
- Lambert. Reinforcement Learning from Human Feedback (online book). (See the chapter on direct alignment algorithms.)