Abstract

Derive the closed-form Direct Preference Optimization (DPO) objective from the KL-constrained RLHF reward, eliminate the Value Critic via Group Relative Policy Optimization (GRPO), and examine how HybridFlow (veRL) orchestrates distributed RL dataflows.

1.The systems crisis of four-model PPO in RLHF#

Pretraining minimizes next-token cross-entropy over internet text, producing a base model that completes documents rather than solving user instructions or verifying multi-step proofs. Ouyang et al. (InstructGPT, arXiv:2203.02155[1][1] · InstructGPT (RLHF) · 2022Training language models to follow instructions with human feedbackOpen arXiv:2203.02155 ↗) established the three-stage RLHF pipeline: Supervised Fine-Tuning (SFT), fitting a Bradley-Terry Reward Model rϕ(x,y)r_\phi(x, y) on human preference pairs (yw≻yl∣x)(y_w \succ y_l \mid x), and optimizing the policy πθ\pi_\theta via Proximal Policy Optimization (PPO) under a reverse-KL penalty from the reference model πref\pi_{\text{ref}}:

max⁡πθ  Ex∼D, y∼πθ(⋅∣x) ⁣[rϕ(x,y)−β DKL ⁣(πθ(y∣x) ∥ πref(y∣x))]\max_{\pi_\theta}\; \mathbb{E}_{x \sim \mathcal{D},\, y \sim \pi_\theta(\cdot \mid x)}\!\Big[ r_\phi(x, y) - \beta\, \mathbb{D}_{\text{KL}}\!\big(\pi_\theta(y \mid x)\,\|\,\pi_{\text{ref}}(y \mid x)\big) \Big]
(1)

While effective, online PPO creates a severe AI Infrastructure bottleneck: four 70B-class models must reside and execute concurrently in GPU memory:

  1. Actor Policy πθ\pi_\theta (trainable: weights + gradients + FP32 AdamW state).
  2. Value Critic VψV_\psi (trainable: another full model of weights + gradients + AdamW state to estimate token-level Generalized Advantage Estimation baselines).
  3. Reference Policy πref\pi_{\text{ref}} (frozen: forward-only log-probabilities).
  4. Reward Model rϕr_\phi (frozen: forward-only scalar scoring).

At 70B70\text{B} scale, holding two trainable models (πθ,Vψ\pi_\theta, V_\psi) and two inference models (πref,rϕ\pi_{\text{ref}}, r_\phi) requires over 2.5 TB2.5\text{ TB} of aggregate VRAM before activation buffers, while alternating between memory-bound autoregressive rollout generation and compute-bound backward passes.

Figure 1: GPU memory footprint comparison across four-model PPO, DeepSeek-R1 GRPO, and closed-form DPO alongside the emergent chain-of-thought length and accuracy trajectory under GRPO.
Figure 1: GPU memory footprint comparison across four-model PPO, DeepSeek-R1 GRPO, and closed-form DPO alongside the emergent chain-of-thought length and accuracy trajectory under GRPO.

2.Direct Preference Optimization (DPO): Your LLM is secretly a reward model#

Rafailov et al. (DPO, arXiv:2305.18290[2][2] · DPO · 2023Direct Preference Optimization: Your Language Model is Secretly a Reward ModelOpen arXiv:2305.18290 ↗) asked whether we can optimize the exact KL-constrained RLHF objective in closed form without ever training an explicit reward model or running online RL sampling.

Start with the KL-constrained reward maximization objective at fixed prompt xx:

max⁡π  ∑yπ(y∣x)r(x,y)−β∑yπ(y∣x)log⁡π(y∣x)πref(y∣x)=−β DKL ⁣(π(y∣x) ∥ 1Z(x)πref(y∣x)exp⁡ ⁣(1βr(x,y)))+βlog⁡Z(x)\max_{\pi}\; \sum_y \pi(y \mid x) r(x, y) - \beta \sum_y \pi(y \mid x) \log \frac{\pi(y \mid x)}{\pi_{\text{ref}}(y \mid x)} = -\beta\, \mathbb{D}_{\text{KL}}\!\left( \pi(y \mid x) \,\middle\|\, \frac{1}{Z(x)} \pi_{\text{ref}}(y \mid x) \exp\!\Big(\frac{1}{\beta} r(x, y)\Big) \right) + \beta \log Z(x)
(2)

where Z(x)=∑yπref(y∣x)exp⁡(1βr(x,y))Z(x) = \sum_y \pi_{\text{ref}}(y \mid x) \exp\big(\frac{1}{\beta} r(x, y)\big) is the partition function independent of π\pi. Because the KL divergence is minimized at 00 when the two distributions are identical, the unique global optimal policy π∗(y∣x)\pi^*(y \mid x) is the Gibbs distribution:

π∗(y∣x)=1Z(x)πref(y∣x)exp⁡ ⁣(1βr(x,y))\pi^*(y \mid x) = \frac{1}{Z(x)} \pi_{\text{ref}}(y \mid x) \exp\!\left(\frac{1}{\beta} r(x, y)\right)
(3)

Take the logarithm and solve algebraically for the ground-truth reward r(x,y)r(x, y) in terms of π∗\pi^*:

r(x,y)=βlog⁡π∗(y∣x)πref(y∣x)+βlog⁡Z(x)r(x, y) = \beta \log \frac{\pi^*(y \mid x)}{\pi_{\text{ref}}(y \mid x)} + \beta \log Z(x)
(4)

Now substitute this reparameterized reward into the Bradley-Terry preference model P(yw≻yl∣x)=σ(r(x,yw)−r(x,yl))P(y_w \succ y_l \mid x) = \sigma\big(r(x, y_w) - r(x, y_l)\big). Because the intractable partition term βlog⁡Z(x)\beta \log Z(x) depends only on the prompt xx, it cancels out identically in the difference r(x,yw)−r(x,yl)r(x, y_w) - r(x, y_l)! Replacing π∗\pi^* with our parameterized policy πθ\pi_\theta yields the exact DPO Loss:

LDPO(πθ;πref)=−E(x,yw,yl)∼D ⁣[log⁡σ ⁣(βlog⁡πθ(yw∣x)πref(yw∣x)−βlog⁡πθ(yl∣x)πref(yl∣x))]\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]
(5)

Furthermore, taking the gradient with respect to θ\theta reveals why DPO behaves like a self-normalizing contrastive update:

∇θLDPO=−β E(x,yw,yl) ⁣[σ(r^θ(x,yl)−r^θ(x,yw))⏟Higher weight when implicit reward is wrong(∇θlog⁡πθ(yw∣x)−∇θlog⁡πθ(yl∣x))]\nabla_\theta \mathcal{L}_{\text{DPO}} = -\beta\, \mathbb{E}_{(x, y_w, y_l)}\!\Big[ \underbrace{\sigma\big(\hat{r}_\theta(x, y_l) - \hat{r}_\theta(x, y_w)\big)}_{\text{Higher weight when implicit reward is wrong}} \Big( \nabla_\theta \log \pi_\theta(y_w \mid x) - \nabla_\theta \log \pi_\theta(y_l \mid x) \Big) \Big]
(6)

where r^θ(x,y)=βlog⁡πθ(y∣x)πref(y∣x)\hat{r}_\theta(x, y) = \beta \log \frac{\pi_\theta(y \mid x)}{\pi_{\text{ref}}(y \mid x)} is the implicit reward margin.

3.DeepSeek-R1 and Group Relative Policy Optimization (GRPO)#

While offline DPO excels at stylistic and instruction alignment, unlocking frontier multi-step mathematical, coding, and scientific reasoning requires online exploration where the model generates its own chain-of-thought trajectories and learns from verifiable outcomes.

To make large-scale online RL economically tractable, DeepSeek-R1 (arXiv:2501.12948[4][4] · DeepSeek-R1 (GRPO) · 2025DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement LearningOpen arXiv:2501.12948 ↗) replaces PPO with Group Relative Policy Optimization (GRPO) and replaces neural reward models (which suffer from reward hacking on long reasoning chains) with deterministic rule-based verifiers (exact answer matching, compiler/unit-test execution, and format checks).

Instead of maintaining a 70B70\text{B}/671B671\text{B} Value Critic VψV_\psi to predict baseline returns, GRPO samples a group of GG completions {o1,o2,…,oG}\{o_1, o_2, \dots, o_G\} from the old policy πθold\pi_{\theta_{\text{old}}} for each prompt qq, scores them to obtain scalar rewards {r1,r2,…,rG}\{r_1, r_2, \dots, r_G\}, and uses the group z-score directly as the advantage A^i,t\hat{A}_{i,t} for every token tt in completion oio_i:

A^i,t=ri−mean⁡(r1,…,rG)std⁡(r1,…,rG)+ϵ\hat{A}_{i,t} = \frac{r_i - \operatorname{mean}(r_1, \dots, r_G)}{\operatorname{std}(r_1, \dots, r_G) + \epsilon}
(7)

The GRPO objective combines PPO's clipped importance-sampling ratio with Schulman's unbiased, strictly non-negative k3k_3 KL divergence estimator placed directly outside the clip operator:

JGRPO(θ)=Eq, {oi}i=1G∼πθold ⁣[1G∑i=1G1∣oi∣∑t=1∣oi∣(min⁡ ⁣(ρi,t(θ)A^i,t, clip⁡(ρi,t(θ),1−ϵ,1+ϵ)A^i,t)−β DKL(πθ ∥ πref))]\mathcal{J}_{\text{GRPO}}(\theta) = \mathbb{E}_{q,\, \{o_i\}_{i=1}^G \sim \pi_{\theta_{\text{old}}}}\!\left[ \frac{1}{G} \sum_{i=1}^G \frac{1}{|o_i|} \sum_{t=1}^{|o_i|} \left( \min\!\Big( \rho_{i,t}(\theta) \hat{A}_{i,t},\, \operatorname{clip}(\rho_{i,t}(\theta), 1-\epsilon, 1+\epsilon) \hat{A}_{i,t} \Big) - \beta\, \mathbb{D}_{\text{KL}}(\pi_\theta \,\|\, \pi_{\text{ref}}) \right) \right]
(8)

where ρi,t(θ)=πθ(oi,t∣q,oi,<t)πθold(oi,t∣q,oi,<t)\rho_{i,t}(\theta) = \frac{\pi_\theta(o_{i,t} \mid q, o_{i,<t})}{\pi_{\theta_{\text{old}}}(o_{i,t} \mid q, o_{i,<t})} and the per-token KL penalty is:

DKL(πθ ∥ πref)=πref(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)−log⁡πref(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)−1≥0\mathbb{D}_{\text{KL}}\big(\pi_\theta \,\|\, \pi_{\text{ref}}\big) = \frac{\pi_{\text{ref}}(o_{i,t} \mid q, o_{i,<t})}{\pi_\theta(o_{i,t} \mid q, o_{i,<t})} - \log \frac{\pi_{\text{ref}}(o_{i,t} \mid q, o_{i,<t})}{\pi_\theta(o_{i,t} \mid q, o_{i,<t})} - 1 \ge 0
(9)
python
def grpo_step_loss(
    logp_new: Tensor,
    logp_old: Tensor,
    logp_ref: Tensor,
    rewards: Tensor,
    eps_clip: float = 0.2,
    beta_kl: float = 0.04,
) -> tuple[Tensor, Tensor]:
    """
    Compute DeepSeek-R1 Group Relative Policy Optimization (GRPO) loss over a group of G completions.
    Shapes: logp_* [G, T], rewards [G].
    """
    # 1. Group-relative normalized advantage (eliminates the separate Value Critic network)
    mean_r = rewards.mean()
    std_r = rewards.std(unbiased=False)
    advantages = ((rewards - mean_r) / (std_r + 1e-8)).unsqueeze(-1)  # [G, 1] broadcast across tokens

    # 2. Importance sampling ratio & PPO clipped surrogate objective
    ratio = torch.exp(logp_new - logp_old.detach())
    surr1 = ratio * advantages
    surr2 = torch.clamp(ratio, 1.0 - eps_clip, 1.0 + eps_clip) * advantages
    policy_objective = torch.minimum(surr1, surr2)

    # 3. Unbiased non-negative Schulman k3 KL penalty estimator: exp(log_ref - log_new) - (log_ref - log_new) - 1
    log_diff = logp_ref.detach() - logp_new
    kl_penalty = torch.exp(log_diff) - log_diff - 1.0

    loss = -(policy_objective - beta_kl * kl_penalty).mean()
    return loss, advantages.squeeze(-1)


def dpo_preference_loss(
    policy_chosen_logp: Tensor,
    policy_rejected_logp: Tensor,
    ref_chosen_logp: Tensor,
    ref_rejected_logp: Tensor,
    beta: float = 0.1,
) -> tuple[Tensor, Tensor]:
    """Closed-form Direct Preference Optimization (DPO) loss and implicit reward margin."""
    chosen_logratios = policy_chosen_logp - ref_chosen_logp
    rejected_logratios = policy_rejected_logp - ref_rejected_logp
    logits = beta * (chosen_logratios - rejected_logratios)
    loss = -F.logsigmoid(logits).mean()
    reward_margin = (beta * chosen_logratios - beta * rejected_logratios).detach()
    return loss, reward_margin

In DeepSeek-R1-Zero, applying pure GRPO directly to the base model without any supervised warm-start triggered an emergent phase transition: average thinking length scaled naturally from hundreds to thousands of tokens as the model autonomously discovered self-verification, backtracking, and reflection ("aha moments"). Adding a small cold-start SFT stage followed by reasoning RL, rejection sampling SFT, and a second all-scenario RL pass produced the final DeepSeek-R1.

4.HybridFlow (veRL): 3D-HybridEngine for RLHF dataflows#

Even after GRPO deletes the Value Critic, an online RL step alternates between two conflicting parallel execution layouts for the Actor πθ\pi_\theta:

  • Rollout Generation (y∼πθoldy \sim \pi_{\theta_{\text{old}}}) is memory-bound autoregressive decoding: it demands smaller Tensor Parallel groups (e.g., TP=2\text{TP}=2 or 44 with vLLM PagedAttention) and large Data Parallelism across prompts.
  • Policy Training (∇θJGRPO\nabla_\theta \mathcal{J}_{\text{GRPO}}) is compute-bound and memory-capacity-bound: it requires wide 3D parallelism (TP×PP×ZeRO/FSDP\text{TP} \times \text{PP} \times \text{ZeRO/FSDP}) across nodes.

Sheng et al. (HybridFlow / veRL, arXiv:2409.19256[3][3] · HybridFlow / veRL · 2024HybridFlow: A Flexible and Efficient RLHF FrameworkOpen arXiv:2409.19256 ↗) solved this mismatch by decoupling a single-controller control plane (coordinating inter-node RL dataflow graphs) from a multi-controller SPMD computation plane powered by the 3D-HybridEngine. Between the training step and the rollout step, 3D-HybridEngine gathers and reshards Actor weights across heterogeneous parallelism topologies (Micro-AllGather over TP/PP groups) with zero redundant weight copies, cutting transition overhead by up to 89%89\% and boosting end-to-end RL throughput by 1.5×–3.7×1.5\times\text{--}3.7\times.

5.References and next chapter#

References

  1. [1]
  2. [2]
  3. [3]
  4. [4]

Share