CSCE 471 Natural Language Processing · Spring 2027

Direct Preference Optimization

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

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.

After reading this, you should be able to state the KL-regularized RLHF objective · derive its closed-form optimal policy · rewrite a reward function in terms of a policy · derive the DPO loss and explain why the partition function cancels · interpret the DPO gradient · compare DPO with RLHF using PPO

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:

RLHF
preference data
$(x, y_w, y_l)$
→reward modelmaximum likelihood (Bradley–Terry)⇄LM policyreinforcement learning: sample, score, update
DPO
preference data
$(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:

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.

$\beta$:
Figure 1 (interactive). The optimal policy $\pi^*\propto\pi_{\text{ref}}\exp(r/\beta)$ for a toy prompt with four possible responses (illustrative numbers). Bars show $\pi^*$, gray ticks show $\pi_{\text{ref}}$. The last column, $\beta\log(\pi^*/\pi_{\text{ref}})$, equals the reward minus the same constant $\beta\log Z(x)$ for every response: the policy “contains” the reward.

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:

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].$$
$\log\frac{\pi_\theta(y_w)}{\pi_{\text{ref}}(y_w)}$:
$\log\frac{\pi_\theta(y_l)}{\pi_{\text{ref}}(y_l)}$:$\beta$ = 1
Figure 2 (interactive). One preference pair. Move the log-ratios of the preferred and dispreferred responses (how much more or less likely the policy makes them than the reference). The bars show the implicit rewards, the loss, and the gradient weight. At the start of training both log-ratios are 0, so the loss is $\log 2\approx 0.69$ and the weight is 0.5.
$\beta$:
Figure 3 (interactive). DPO on a toy policy over four responses. The preference data contains all six pairs from the human ranking A ≻ B ≻ C ≻ D. The reference model (gray ticks) prefers B. Each click runs 10 gradient steps on the DPO loss, with no reward model and no sampling. The policy learns to put A first. Note that the loss keeps decreasing as the probabilities become more extreme: with a fixed dataset, DPO keeps pushing the margins apart, which is why it is usually trained only briefly.

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 modeltrained separatelynot needed (the policy is an implicit reward model)
Sampling during trainingyes (online, from the current policy)no (offline, fixed preference data)
Models in memorypolicy, reference, reward, valuepolicy, reference
Training objectivemaximize reward − β·KL with RLclassification loss on preference pairs
Implementationcomplex, sensitive to hyperparameterssimple, similar to SFT
Stabilitycan be unstablestable
Explorationyes: learns from its own new outputsno: limited to responses in the dataset

DPO in practice

7Summary

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$.)

Unnormalized: $0.5e^{2}\approx 3.69$ and $0.5e^{0}=0.5$. $Z=4.19$, so $\pi^*\approx(0.88,\ 0.12)$. With a larger $\beta$, $\pi^*$ would stay closer to $(0.5,0.5)$.

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$.)

$\hat r(y_w)=0.5$, $\hat r(y_l)=-0.5$, margin $=1$. Loss $=-\log\sigma(1)\approx 0.31$. Weight $=\sigma(-1)\approx 0.27$: the pair is already ranked correctly, so it gets a smaller update than a fresh pair (weight 0.5).

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$?

No: the difference would contain $\beta\log Z(x_1)-\beta\log Z(x_2)$, which does not vanish. The cancellation relies on both responses answering the same prompt, which is exactly how preference data is collected.

Quick quiz

9Further reading