When we fine-tune a Large Language Model using standard next-token prediction, we teach it grammar, syntax, and basic pattern matching. But raw text generation alone doesn't guarantee that an LLM will be helpful. To truly align a model with human intent, we need a mechanism to reward desirable behavior and discourage toxic or unhelpful outputs.
With Reinforcement Learning with Human Feedback (RLHF) and its optimization algorithm, Direct Policy Optimization (DPO), we can help align the model with human preferences with the help of Reward Modeling.
In the previous blog, Proximal Policy Optimization: Reinforcement Learning with Human Feedback, we explored the complete mathematical and architectural pipeline behind RLHF with PPO.
In this blog, we will explore the complete mathematical and architectural pipeline behind RLHF with DPO, and how it differs from PPO.
As we have already seen in previous blog, we need 4 models for alignemtn in PPO, namely, Reference Model (SFT LLM), Actor (Policy LLM), Reward Model (adapted from SFT), and , Critic Model(Value Function LLM). Training all of these models can be computationally expensive and complex.
DPO simplify this process by directly optimizing the policy model directly on human preferences without the need for a separate reward model or critic LLM.
Direct Policy Optimization (DPO) offers several advantages over traditional Reinforcement Learning with Human Feedback (RLHF) approaches like Proximal Policy Optimization (PPO):
Simpler Implementation: DPO eliminates the need for complex reward modeling and policy gradient estimation.
Reduced Variance: By directly optimizing the policy, DPO can reduce the variance in the gradient estimates.
Better Sample Efficiency: DPO can achieve better performance with fewer samples compared to PPO.
We can rewrite the Reward Model's scoring function directly in terms of the LLM's own token probabilities. Therefore, instead of using a separate reward model to evaluate the response, DPO looks at the LLM itself, and evaluates, how likely is the policy llm to generate a choosen response compared to our reference LLM model, and similarly, how likely is the policy llm to generate a rejected response compared to the reference LLM model.
Therefore, we only need two models: the policy model and the reference model.
┌─────┐
Prompt ───► │ LLM │───► Response ───► DPO Loss Function ───► Parameter Update
└─────┘
The DPO Loss Function mathematically forces the model to do two things at once:
Maximize the probability of the Chosen response relative to the Reference model.
Minimize the probability of the Rejected response relative to the Reference model.
The standard naming conventions are the same as in the previous blog on PPO:
Quick Nomenclature Note of all the different LLMs used (Total 4):
πSFT) / Reference Model: The model fine-tuned on supervised chat conversations. Generally denoted as πSFT or πref. It serves as our baseline anchor.πθ): The language model trained to generate text, typically denoted as πθ (parameterized by weights θ).To train the reward model, we use pairwise human preference data, like we did for reward model training in PPO:
Collect Prompts :The instruction or question given to the model.
Chosen Response (yw) : The preferred, high-quality answer (written by a human or a better model).
Rejected Response (yl) : The dispreferred, lower-quality answer (unhelpful response generated by the SFT model).
{
"prompt": "x: What is 2 + 2?",
"chosen": "y_w: The answer to 2 + 2 is 4.",
"rejected": "y_l: 2 + 2 is 5, but sometimes math is relative."
}
In this section, we will explore how the Policy LLM is fine-tuned using Direct Policy Optimization (DPO) to align it with human preferences.
Here, we combine everything into DPO training step to update LLM weights (θ).
For every prompt in your batch,
┌──────────┐
y_w ───►│Policy LLM│ ───► log probabilities
\ /│ (π_θ) │
\/ └──────────┘
/\
/ \┌──────────┐
y_l ───►│ SFT LLM │ ───► log probabilities
│ (π_sft) │
└──────────┘
The Policy LLM isn't built from scratch. It is initialized directly from our trained SFT checkpoint (πSFT) without any modifications.
It checks how confident the policy model πθ is, about this response compared to the reference model πSFT.
The DPO algorithm uses a simple standard sigmoid cross-entropy loss function to update the policy parameters:
The DPO loss function is derived from the principle of maximizing the likelihood of the chosen response while minimizing the likelihood of the rejected response. The sigmoid function is used to convert the difference in scores into a probability, which is then used in the cross-entropy loss.
The Weighting Factor: If the model is already doing a good job ranking chosen over rejected, the sigmoid term approaches 1, making the weighting factor approach 0 (stopping unnecessary updates). If the model gets it wrong, the gradient spikes, forcing a heavy correction.
Lets say, we pass our prompt and responses through both models, πSFT and πθ, yielding the following sequence log-probabilities log π:
If the model gets it right: As σ approaches 1.0 (meaning the model strongly prefers the chosen response), the term (1 - σ) approaches 0, No unnecessary gradient updates.
If the model gets it wrong: The gradient scales up, forcefully tweaking the internal weights (θ) to increase the probability of generating yw while decreasing the probability of generating yl.