New Masterclass:How to Build a Large Language Model

Read Now →

5 PPO Variants for Enhancing RLHF Performance

By Andreas T. on May 23, 2025

Guest Author

Reinforcement Learning from Human Feedback (RLHF) has become a central technique for aligning large language models (LLMs) with human preferences and intentions. Within the RLHF framework, Proximal Policy Optimization (PPO) is a widely adopted algorithm for fine-tuning the LLM based on a learned reward model. While standard PPO offers stability and good performance, its application in RLHF is not without challenges, leading to the development and use of several PPO variants.

These adaptations aim to refine the learning process, address specific issues like reward over-optimization or policy divergence, and ultimately produce models that are more helpful, harmless, and honest. Understanding these variants is valuable for engineers looking to optimize their RLHF pipelines and achieve superior model alignment.

Understanding PPO in the Context of RLHF

Before examining the variants, it's helpful to recap PPO's role and the specific demands RLHF places on it.

What is PPO?

Proximal Policy Optimization is an on-policy, actor-critic reinforcement learning algorithm. It aims to take the biggest possible improvement step on a policy using the data currently available, without stepping so far that it causes performance collapse. PPO achieves this by optimizing a clipped surrogate objective function:

LCLIP(θ)=E^t[min(rt(θ)A^t,clip(rt(θ),1ϵ,1+ϵ)A^t)]L^{CLIP}(\theta) = \hat{\mathbb{E}}_t [ \min(r_t(\theta)\hat{A}_t, \text{clip}(r_t(\theta), 1 - \epsilon, 1 + \epsilon)\hat{A}_t) ]

Here, rt(θ)=πθ(atst)πθold(atst)r_t(\theta) = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\theta_{old}}(a_t|s_t)} is the probability ratio of the current policy πθ\pi_{\theta} to the old policy πθold\pi_{\theta_{old}}, A^t\hat{A}_t is the estimated advantage function, and ϵ\epsilon is a small hyperparameter defining the clipping range. This clipping discourages excessively large policy updates.

Why PPO for RLHF?

PPO's relative simplicity, stability, and sample efficiency (compared to some other policy gradient methods) make it a good fit for fine-tuning massive LLMs. In RLHF, the LLM acts as the policy, generating text (actions) based on prompts (states). A separate reward model (RM), trained on human preference data, provides scalar reward signals. PPO then updates the LLM policy to maximize these rewards.

Challenges of Standard PPO in RLHF

Despite its strengths, applying standard PPO to RLHF presents hurdles:

  1. Reward Over-optimization (Reward Hacking): The policy might find ways to maximize the reward model's score in ways that don't align with true human preferences, exploiting loopholes in the RM.
  2. Policy Divergence: The fine-tuned policy can drift too far from the initial supervised fine-tuned (SFT) model, leading to a loss of coherence, style, or factual knowledge, or generating outputs that are stylistically different in undesirable ways.
  3. Mode Collapse: The policy might learn to generate repetitive or very similar high-reward responses, lacking diversity.
  4. Sample Efficiency: While better than some, training LLMs is expensive, so further improvements in sample efficiency are always welcome.

These challenges motivate the use of PPO variants specifically adapted for the RLHF setting.

5 PPO Variants for Enhanced RLHF

Several modifications to the standard PPO algorithm help address the aforementioned challenges, leading to more effective and controlled LLM alignment.

1. PPO with KL-Penalty (PPO-KL)

This is perhaps the most common and impactful variant for RLHF.

  • Problem Addressed: Prevents the policy from diverging too far from a reference model, typically the SFT model. This helps maintain desirable characteristics of the SFT model (e.g., instruction following, general knowledge) and mitigates catastrophic forgetting or generating out-of-distribution text that the reward model might not accurately score.
  • Modification: A Kullback-Leibler (KL) divergence penalty term is added to the reward signal. The agent is penalized if its output distribution strays too far from the reference model's distribution. The per-token reward is often structured as: Rtotal(st,at)=RM(st,at)βKL[πref(st)πθ(st)]R_{total}(s_t, a_t) = R_M(s_t, a_t) - \beta \cdot KL[ \pi_{ref}(\cdot|s_t) || \pi_{\theta}(\cdot|s_t) ] Where RM(st,at)R_M(s_t, a_t) is the score from the reward model, πref\pi_{ref} is the reference (SFT) policy, πθ\pi_{\theta} is the current policy being trained, and β\beta is the KL coefficient controlling the strength of the penalty. This modified reward is then used to compute advantages for the PPO objective.
  • Benefits for RLHF: Better control over policy deviation, maintains alignment with the SFT model's learned behaviors, improves generation quality and stability.
  • Considerations: The β\beta coefficient is a critical hyperparameter. Too low, and the policy might still diverge significantly. Too high, and it might overly constrain the policy, preventing it from learning to maximize the RM score effectively.

2. PPO with Clipped Value Function

This variant aims to stabilize the training of the value function, which is part of the actor-critic setup in PPO.

  • Problem Addressed: Large updates to the value function can lead to instability in the policy updates because advantage estimates depend on the value function. This is especially true if the reward landscape is noisy or changes rapidly.
  • Modification: Similar to how the policy objective is clipped, the value function loss can also be clipped. The value loss is typically Mean Squared Error (MSE) between the predicted value Vϕ(st)V_{\phi}(s_t) and the target value VttargV_t^{targ} (often computed using GAE). With clipping, the update to Vϕ(st)V_{\phi}(s_t) is restricted: Let Vϕold(st)V_{\phi_{old}}(s_t) be the value estimate from before the update. The new value estimate Vϕ(st)V_{\phi}(s_t) is used to calculate the loss. An alternative clipped value Vϕclipped(st)=Vϕold(st)+clip(Vϕ(st)Vϕold(st),ϵv,ϵv)V_{\phi}^{clipped}(s_t) = V_{\phi_{old}}(s_t) + \text{clip}(V_{\phi}(s_t) - V_{\phi_{old}}(s_t), -\epsilon_v, \epsilon_v) is also computed. The final value loss becomes: LVFCLIP(ϕ)=E^t[max((Vϕ(st)Vttarg)2,(Vϕclipped(st)Vttarg)2)]L^{VF-CLIP}(\phi) = \hat{\mathbb{E}}_t [ \max((V_{\phi}(s_t) - V_t^{targ})^2, (V_{\phi}^{clipped}(s_t) - V_t^{targ})^2) ] where ϵv\epsilon_v is the clipping range for the value function.
  • Benefits for RLHF: Leads to more stable value function learning, which in turn can stabilize policy updates and improve overall training dynamics.
  • Considerations: Adds another hyperparameter ϵv\epsilon_v to tune. The impact might be more noticeable in settings with high reward variance.

3. PPO with Adaptive KL Penalty (Target KL)

Instead of a fixed β\beta for the KL penalty, this approach adjusts β\beta dynamically.

  • Problem Addressed: Finding the right fixed β\beta for PPO-KL can be difficult. A fixed β\beta might be too restrictive early on or too loose later in training (or vice-versa).
  • Modification: The PPO objective is augmented with a KL term, and β\beta is adjusted at each iteration to keep the KL divergence between the policy and the reference model near a target value, KLtargetKL_{target}. If KLactual>KLtargetKL_{actual} > KL_{target}, β\beta is increased. If KLactual<KLtargetKL_{actual} < KL_{target}, β\beta is decreased. This is similar to the adaptive KL penalty in the original PPO paper, but here it's typically applied against the SFT model in RLHF.
  • Benefits for RLHF: More robust training as β\beta adapts to the learning dynamics. Can prevent the policy from deviating too quickly or getting stuck due to an inappropriate fixed β\beta.
  • Considerations: Requires careful tuning of KLtargetKL_{target} and the rate of β\beta adjustment. Implementation can be slightly more complex.

4. PPO with Entropy Bonus Annealing

Standard PPO often includes an entropy bonus to encourage exploration. Annealing this bonus can refine the exploration-exploitation balance.

  • Problem Addressed: A fixed entropy bonus might over-encourage exploration when exploitation is needed, or vice-versa. In RLHF, initial exploration can be good, but later on, the policy should converge to high-quality, human-aligned responses.
  • Modification: The entropy bonus term c2S(πθ,st)c_2 S(\pi_{\theta}, s_t) is added to the PPO objective. The coefficient c2c_2 is not fixed but annealed (e.g., linearly or exponentially decayed) over training steps. It starts higher to encourage exploration and gradually reduces to favor exploitation of learned high-reward regions. Objective: L(θ)=LCLIP(θ)c1LVF(ϕ)+c2(t)S(πθ,st)L(\theta) = L^{CLIP}(\theta) - c_1 L^{VF}(\phi) + c_2(t) S(\pi_{\theta}, s_t)
  • Benefits for RLHF: Can improve the policy's ability to discover diverse, high-reward generation strategies early on, then refine them effectively. May help escape local optima in the reward landscape.
  • Considerations: The annealing schedule (initial value, final value, decay rate/type) needs to be designed and tuned. Incorrect annealing can prematurely stop exploration or over-explore.

5. Multi-Objective PPO / Reward Shaping PPO

While not a PPO algorithm variant in the strictest sense, this involves modifying the reward signal that PPO optimizes, which is a common and effective practice in RLHF.

  • Problem Addressed: The scalar reward from a single RM might not capture all desired attributes (e.g., helpfulness, harmlessness, conciseness, specific style) or might not sufficiently penalize undesired behaviors (e.g., toxicity, repetitiveness).
  • Modification: The total reward function is composed of multiple components: Rtotal=wRMRRMβKLw1P1w2P2...+wkBk+...R_{total} = w_{RM}R_{RM} - \beta KL - w_1 P_1 - w_2 P_2 - ... + w_k B_k + ... Where RRMR_{RM} is the reward model score, KLKL is the KL divergence penalty, PiP_i are penalties for undesirable attributes (e.g., PlengthP_{length} for excessive length, PtoxicityP_{toxicity} for toxic content), and BkB_k are bonuses for desirable attributes not fully captured by RRMR_{RM}. The weights wiw_i control the importance of each component.
  • Benefits for RLHF: Allows for more granular control over the LLM's behavior by explicitly rewarding/penalizing specific characteristics. This can lead to models that are better aligned with complex, multi-faceted human preferences.
  • Considerations: Designing the reward components and tuning their weights (wi,βw_i, \beta) can be complex and requires significant experimentation. Over-penalizing can stifle the model or lead to degenerate solutions.

Implementation Details and Code Snippets

Successfully implementing these PPO variants involves careful hyperparameter tuning and structuring the RLHF training loop correctly.

Important Hyperparameters

Common hyperparameters across PPO variants include:

  • Learning rate (for policy and value networks)
  • Clip range (epsilonepsilon for policy, epsilonvepsilon_v for value if used)
  • KL coefficient (eta for PPO-KL, or target KL for adaptive KL)
  • GAE parameters (lambdalambda, gammagamma)
  • Number of PPO epochs per data batch
  • Minibatch size
  • Entropy coefficient (c2c_2, and its annealing schedule if used)
  • Value function loss coefficient (c1c_1)

Example: PPO-KL Reward Calculation and Policy Update

This snippet focuses on how the KL-penalized reward influences advantage calculation, which then feeds into the standard PPO clipped objective.

# Assuming: 
# policy_model: current LLM policy
# sft_model: reference SFT model (fixed)
# reward_model: learned reward model (fixed)
# prompts: batch of input prompts
# kl_beta: coefficient for KL penalty

# 1. Generate responses and get log_probs
# (Simplified: actual generation involves sampling, etc.)
# actions_token_ids are sequences of tokens for each prompt
# log_probs_policy are log_probs from policy_model for generated actions
# log_probs_sft are log_probs from sft_model for same actions
responses, log_probs_policy = policy_model.generate_and_log_probs(prompts)
with torch.no_grad():
    log_probs_sft = sft_model.log_probs_for_actions(prompts, responses)
    rm_scores = reward_model.score(prompts, responses) # Per-sequence

# 2. Calculate KL divergence (per token, then summed/averaged per seq)
# Note: Ensure log_probs are for the same vocabulary and tokenization
kl_div_per_token = log_probs_policy - log_probs_sft # (B, L)
kl_div_per_sequence = kl_div_per_token.sum(dim=-1) # (B)

# 3. Calculate the RLHF-specific reward
# This reward is used for advantage calculation
# rm_scores might be a single value per sequence.
# kl_div needs to be appropriately scaled if rm_scores are per sequence.
# For simplicity, assume rewards are aligned (e.g., at sequence end)
rewards = rm_scores - kl_beta * kl_div_per_sequence

# 4. Collect other PPO experience (values, old_log_probs)
# During PPO iteration, rollouts are generated:
# old_log_probs_policy, values = collect_rollouts(...) 

# 5. Compute advantages (e.g., using GAE)
# advantages = calculate_gae(rewards, values, dones, gamma, lambda_gae)

# 6. PPO Policy Loss (using advantages based on KL-penalized reward)
# ratio = torch.exp(new_log_probs_policy - old_log_probs_policy)
# surr1 = ratio * advantages
# surr2 = torch.clamp(ratio, 1.0 - clip_eps, 1.0 + clip_eps) * advantages
# policy_loss = -torch.min(surr1, surr2).mean()

# 7. Value Loss & Entropy Bonus (as in standard PPO)
# value_loss = ...
# entropy_bonus = ...
# total_loss = policy_loss + c1 * value_loss - c2 * entropy_bonus

Diagram: RLHF Loop with PPO Variant

Flow of data and model interactions in an RLHF process utilizing a PPO variant for policy optimization. The KL penalty is shown integrated into the reward calculation before PPO updates.

Choosing the Right Variant

There's no single "best" PPO variant for all RLHF tasks. The choice depends on several factors:

  • Specific Alignment Goals: If strict adherence to the SFT model's style or knowledge base is critical, PPO-KL or Adaptive KL PPO are strong candidates. If exploring novel capabilities is more important (while still being safe), a more lenient KL penalty or careful entropy annealing might be preferred.
  • Reward Model Quality: If the RM is prone to being exploited (reward hacking), stronger regularization via KL penalty or carefully shaped rewards is more important.
  • Computational Budget: Some variants (like adaptive KL or complex reward shaping) might require more experimentation and hyperparameter tuning.
  • Stability Needs: If training is unstable, PPO with clipped value function or adaptive KL could help.
  • Task Complexity: For tasks with multi-faceted success criteria, multi-objective PPO/reward shaping is almost essential.

In practice, PPO-KL (with a fixed, well-tuned β\beta) is a very common and effective starting point for RLHF. Many successful RLHF implementations build upon this foundation, possibly adding other refinements like value function clipping or careful reward shaping. Experimentation, guided by thorough evaluation of the resulting LLM's behavior, is indispensable.

Conclusion

Standard PPO provides a solid foundation for the reinforcement learning phase of RLHF, but its variants offer valuable tools for addressing the unique challenges of aligning LLMs. By incorporating mechanisms like KL divergence penalties, adaptive coefficients, value function clipping, and sophisticated reward shaping, engineers can exert finer control over the learning process.

These adaptations help to prevent undesirable policy drift, stabilize training, and steer the LLM more precisely towards complex human preferences. As research in RLHF continues, we can expect further refinements and novel approaches to PPO and other RL algorithms, pushing the boundaries of LLM alignment and capability.

© 2025 ApX Machine Learning. All rights reserved.