Reinforcement Learning: Direct Preference Optimization (DPO)
A classification-style loss that trains directly on human preference pairs.
With RLHF the story has become both powerful and slightly unsatisfying at the same time.
Powerful, because we finally stopped pretending that every good behavior can be captured by a neat hand-written reward function. Instead, we admitted something much closer to reality: humans are often far better at saying “this answer is better than that one” than at writing down an exact scalar reward for every possible response.
But also slightly unsatisfying, because the full RLHF pipeline is heavy.
We start with supervised fine-tuning so the model learns how to speak in a generally sensible way. Then we collect preference comparisons. Then we train a separate reward model. Then we run PPO to optimize the policy against that learned reward, while keeping it close to a reference model through a KL penalty.
Conceptually, it all makes sense.
And yet a natural question keeps lingering in the background.
If our dataset already tells us which response humans preferred and which one they rejected, why do we need to build so much machinery in the middle? Why train a separate reward model just to convert comparisons into scores, and then run a reinforcement-learning algorithm just to make the policy prefer the chosen response?
Why not learn that preference more directly?
That question is exactly where Direct Preference Optimization (DPO) enters.
To really understand why DPO feels so elegant, it helps to begin with a simple situation.
Imagine a prompt
Now suppose we have two candidate responses.
The first is
The second is
A human reads both and prefers the first one.
This is the raw data we actually have in preference learning:
The prompt, a chosen response, and a rejected response.
Now think carefully about what PPO-based RLHF is trying to achieve after all its intermediate steps. The reward model is trained so that the chosen response gets a higher score than the rejected one. Then PPO updates the policy so that responses with higher reward become more likely.
If we compress those two stages into one sentence, the final goal is simply this:
for a given prompt, make the policy assign higher probability to the chosen response than to the rejected one.
That sounds almost embarrassingly straightforward.
And once it is said that plainly, it becomes hard not to ask: can we just train that directly?
From a Naive Idea to a Precise One
At first, this looks too naive. If we write something like
then yes, it captures the direction we want. But it misses an important ingredient from RLHF. In RLHF, we never optimize preference in isolation. We always keep the new policy close to a reference policy, often the supervised fine-tuned model. That reference matters, because without it the model may drift into strange regions of behavior. It may exploit the preference signal in odd ways, become overly verbose, unnaturally confident, or collapse into stylistic quirks that happen to fool the objective.
So if DPO is going to replace reward-model training plus PPO, it must somehow preserve the stabilizing role of that reference policy.
This is where the idea becomes elegant.
DPO does not simply say “increase the probability of chosen responses.” It says something more precise:
increase the probability of chosen responses relative to the reference policy, and decrease the probability of rejected responses relative to the reference policy.
That “relative to the reference” idea is the key.
To see why, consider the KL-regularized RLHF objective:
This objective says: achieve high reward, but do not drift too far from the reference policy.
Now comes a key mathematical insight. The optimal policy for this objective has a closed form (see Appendix):
This equation says that the optimal policy is simply the reference policy tilted by the exponential of the reward.
If the reward is high, the probability increases relative to the reference.
If the reward is low, the probability decreases.
Taking logs (ignoring normalization constants), we obtain
Now something remarkable happens.
The reward can be written in terms of a log-probability difference between the policy and the reference policy.
This means the reward model is no longer strictly necessary. It is implicitly encoded in how the policy deviates from the reference.
Now consider a preference pair where the human prefers over . That implies
Substituting the expression above, we get
This expression is no longer about rewards at all.
It compares two preference gaps:
- how strongly the new policy prefers over
- how strongly the reference policy prefers over
And the condition is simple:
the new policy should prefer the chosen response more strongly than the reference policy does.
Turning the Idea Into a Loss
Now we can turn this into a learning objective.
Define a score:
Then the preference condition becomes
We convert this into a probability:
where is the sigmoid function.
Finally, we optimize the likelihood of the human preference:
And just like that, we have replaced:
- reward model training
- PPO optimization
with a single supervised-style loss. No rollouts. No value functions. No advantage estimates. Just pairs, comparisons, and gradients.
But there is a small detail we quietly brushed aside earlier.
When we derived the connection between reward and log-probabilities, there was a parameter sitting there the whole time:
We ignored it to keep the story simple. Now it is time to bring it back. back into the score:
Now look at the difference:
and hence out loss become:
That is DPO.
Making It Concrete
It helps to make this concrete with numbers. Imagine for a moment that for the same prompt , the reference policy assigns the following log-probabilities:
This means that, under the reference model, the rejected response is actually slightly more likely.
Now suppose our current policy has shifted a bit:
Let us compute the score difference step by step.
First, measure how much the new policy has moved relative to the reference.
For the chosen response,
For the rejected response,
Now take the difference:
If we choose , then the argument to the sigmoid becomes . That gives
So the model is already strongly aligned with the human preference. The loss
is small, which means only a gentle update is needed.
Another elegant piece of DPO is hidden in the gradient. If we define the preference margin
then the loss for one example is simply
Now consider how this behaves.
If is already very large and positive, then is close to , the loss is tiny, and the gradient is tiny. This means DPO does not keep pushing a preference pair that the model already gets right with a comfortable margin.
But if is negative, then is small, the loss is large, and the gradient is large. That means DPO automatically focuses more strongly on examples where the model still implicitly favors the rejected response.
This is a beautiful kind of self-weighting. No critic is needed to tell the algorithm where it is wrong. The pairwise margin itself tells us how wrong the current policy is. The original paper emphasizes exactly this intuition: the DPO update increases the likelihood of preferred completions, decreases the likelihood of dispreferred completions, and weights examples according to how incorrectly the model’s implicit reward ordering still ranks them.
What This Really Replaces
At this point, the relationship to the earlier RLHF discussion becomes much clearer.
In RLHF, the chain looked like this:
human preference reward model PPO improved policy.
In DPO, the chain collapses into
human preference direct policy optimization.
But it would be a mistake to think DPO “abandons” the RLHF objective. It actually keeps the same spirit. The KL-constrained reward-maximization picture is still there in the background. The difference is that DPO notices something PPO-based RLHF did not exploit cleanly enough: once the optimal KL-regularized policy can be written in closed form, and once pairwise preference models care only about reward differences, the reward model can be analytically folded into the policy itself.
There is also something very satisfying about this from the perspective of language modeling.
In ordinary reinforcement learning, the agent must interact with an environment, observe consequences, estimate returns, bootstrap values, and so on. Language-model alignment is a bit stranger. We already have an offline dataset of comparisons. We are not trying to discover the consequences of actions in some unknown world. We are trying to align a conditional sequence model with pairwise judgments. Once that is the problem, a classification-style objective begins to feel much more natural than a full actor-critic loop.
And this is why DPO feels like such a natural next step after RLHF. In the previous blog, PPO was the engine that carried us from learned rewards to aligned behavior. In this blog, we discover that maybe the engine was not the essential part after all. Maybe the essential part was always the preference comparison itself, and the rest of the machinery was just one way of expressing it.
Appendix
Deriving the Optimal Policy Under a KL-Regularized Reward
Fix a prompt . We want to choose the distribution over the response space to maximize
subject to the constraint that is a valid probability distribution:
This is a constrained optimization over a function, with one variable per , so we use Lagrange multipliers.
We start by forming the Lagrangian. We introduce a multiplier for the normalization constraint, and we'll check that the nonnegativity constraint is automatically satisfied, so no multiplier is needed for it:
Next, we take the functional derivative. Treating each , for each fixed , as an independent variable, we differentiate with respect to it. There are three terms involving .
The first term gives us:
The second term, using the product rule on , gives us:
using .
The third term gives us:
Putting it together and setting the total derivative to zero:
Now we solve for . Rearranging:
Exponentiating both sides:
The last factor doesn't depend on , so we call it :
To fix this constant, we apply the normalization constraint :
where
is the partition function. It normalizes the distribution and doesn't depend on .
This brings us to the final closed form:
Finally, we verify that this is a maximum rather than a saddle point. Taking the second derivative of with respect to :
Since and , this is strictly negative everywhere. The objective is strictly concave in , because is concave (negative entropy is convex, so its negative is concave), and adding the linear reward term preserves concavity. A strictly concave function on a convex constraint set, has a unique global maximum, which is exactly the critical point we found. This also confirms automatically.