Mathematical Comparison of DPO and GRPO in RL for LLMs
Focus: Optimization objectives, gradient derivations, and underlying probabilistic models.
1. Core Problem Setup
Both methods align LLMs with human preferences using a dataset (\mathcal{D} = {(x, y_w, y_l)}), where:
- (x): Input prompt.
- (y_w): Preferred ("winning") response.
- (y_l): Dispreferred ("losing") response.
The goal is to maximize (P(y_w \succ y_l \mid x)), the probability that (y_w) is preferred over (y_l).
2. Probabilistic Foundation: Bradley-Terry (BT) Model
Both methods use the BT model to formalize preferences:
[
P(y_w \succ y_l \mid x) = \sigma\left( \beta \left( \log \pi(y_w \mid x) - \log \pi(y_l \mid x) \right) \right)
]
where (\sigma) is the sigmoid function, (\beta > 0) is a temperature, and (\pi) is the policy (LLM).
3. Optimization Objectives
3.1. DPO: Direct Preference Optimization
Objective: Maximize the log-likelihood of observed preferences under the BT model.
[
\mathcal{L}{\text{DPO}}(\pi) = \mathbb{E}{(x,y_w,y_l) \sim \mathcal{D}} \left[ \log \sigma\left( \beta \left( \log \pi(y_w \mid x) - \log \pi(y_l \mid x) \right) \right) \right]
]
Key Insight:
- DPO eliminates the need for a separate reward model by directly optimizing (\pi) using the BT likelihood.
- Gradient derivation (with (\beta=1) for simplicity):
[
\nabla_\theta \mathcal{L}{\text{DPO}} = \mathbb{E} \left[ \sigma\left( \log \frac{\pi(y_l \mid x)}{\pi(y_w \mid x)} \right) \nabla\theta \log \pi(y_w \mid x) - \sigma\left( \log \frac{\pi(y_w \mid x)}{\pi(y_l \mid x)} \right) \nabla_\theta \log \pi(y_l \mid x) \right]
]
This simplifies to:
[
\nabla_\theta \mathcal{L}{\text{DPO}} = \mathbb{E} \left[ (1 - P{BT}) \nabla_\theta \log \pi(y_w \mid x) - P_{BT} \nabla_\theta \log \pi(y_l \mid x) \right]
]
where (P_{BT} = \sigma\left( \log \pi(y_w \mid x) - \log \pi(y_l \mid x) \right)).
Advantages:
- Closed-form gradients; no RL sampling or reward modeling.
- Theoretically avoids overoptimization via implicit KL regularization (via the BT denominator).
3.2. GRPO: Guided Preference Optimization
Objective: Maximize expected reward (from a learned reward model) with a KL-divergence constraint.
[
\mathcal{L}{\text{GRPO}}(\pi) = \mathbb{E}{x \sim \mathcal{D}, y \sim \pi(\cdot \mid x)} \left[ r_\phi(y \mid x) \right] - \beta \cdot \text{KL}\left( \pi(\cdot \mid x) \parallel \pi_{\text{ref}}(\cdot \mid x) \right)
]
Key Components:
- Reward Model (r_\phi):
- Trained on (\mathcal{D}) to predict preference scores:
[
\mathcal{L}{\text{RM}}(\phi) = -\mathbb{E} \left[ \log \sigma\left( r\phi(y_w \mid x) - r_\phi(y_l \mid x) \right) \right]
]
- (r_\phi(y \mid x)) approximates (\log \pi^*(y \mid x)) (optimal policy).
- KL Constraint:
- (\pi_{\text{ref}}): Reference policy (e.g., initial SFT model).
- Prevents deviation from (\pi_{\text{ref}}) to avoid reward hacking.
Gradient Derivation (using PPO-style clipping for stability):
[
\nabla_\theta \mathcal{L}{\text{GRPO}} \approx \mathbb{E} \left[ \min\left( r_t(\theta) \nabla\theta \log \pi_\theta(y \mid x), \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon) \nabla_\theta \log \pi_\theta(y \mid x) \right) - \beta \nabla_\theta \text{KL} \right]
]
where (r_t(\theta) = \frac{\pi_\theta(y \mid x)}{\pi_{\text{ref}}(y \mid x)} \cdot (r_\phi(y \mid x) - b(x))) and (b(x)) is a baseline.
Advantages:
- Decouples reward learning ((r_\phi)) from policy optimization.
- Explicit KL constraint enhances stability and control.
4. Critical Mathematical Differences
| Aspect | DPO | GRPO |
|---|
| Reward Model | Not used; direct BT likelihood. | Required; pre-trained on preferences. |
| Optimization | Single-stage; gradients from BT model. | Two-stage: reward learning + RL fine-tuning. |
| Constraint Handling | Implicit (via BT denominator). | Explicit KL penalty. |
| Gradient Computation | Closed-form; no importance sampling. | Requires PPO clipping/importance sampling. |
| Objective Form | Pairwise log-likelihood. | Expected reward + KL penalty. |
| Risk of Overoptimization | Lower (theoretically bounded). | Higher (mitigated by KL penalty). |
5. Theoretical Implications
- DPO:
- Equivalence to RLHF: Maximizing (\mathcal{L}_{\text{DPO}}) is equivalent to maximizing reward (r(y \mid x) = \log \pi(y \mid x)) under the BT model, but without explicitly learning (r).
- Bias-Variance Tradeoff: Avoids reward model misspecification but assumes BT perfectly captures preferences.
- GRPO:
- Reward Hacking: KL constraint (\text{KL}(\pi \parallel \pi_{\text{ref}})) bounds deviation but may limit exploration.
- Sample Efficiency: Reward model enables off-policy training but requires careful tuning of (\beta) and PPO hyperparameters.
6. Practical Tradeoffs
| Factor | DPO | GRPO |
|---|
| Implementation | Simpler (no RL, no reward model). | More complex (two-stage, PPO, KL tuning). |
| Compute | Lower (single optimization loop). | Higher (reward model + RL fine-tuning). |
| Stability | Theoretically stable; avoids KL tuning. | Sensitive to (\beta), (\epsilon), and reward scaling. |
| Flexibility | Limited to pairwise preferences. | Adapts to additional rewards (e.g., safety). |
| Performance | Competitive with PPO in many benchmarks. | State-of-the-art when reward model is high-quality. |
7. Conclusion
- DPO provides an elegant, unified framework that directly optimizes preferences via the BT likelihood, reducing complexity but tying optimization tightly to the pairwise preference structure.
- GRPO (as a PPO-based approach) offers modularity and control via a learned reward model and explicit constraints, at the cost of increased complexity and tuning.
When to use:
- DPO: For efficiency and simplicity; when preferences are cleanly pairwise.
- GRPO: When fine-grained control over policy updates (via KL) or integration of auxiliary rewards is needed.
Both methods represent milestones in aligning LLMs with human preferences, with DPO emphasizing mathematical elegance and GRPO emphasizing flexibility and robustness.