Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 59

PPO's Role in SFT Model Optimization and Regularization

Recommended resources

Welcome to the next stage of our journey into LLM alignment. In the previous lesson, we demystified the process of training a reward model. You learned how we convert subjective human preferences into a concrete, differentiable scoring function, , using the Bradley-Terry model. We now have a "judge" that can assign a numerical score to any given completion, acting as a scalable proxy for human feedback.

Today, we address the main event: Stage 3 of the RLHF pipeline. We will investigate how to use this reward model to actively steer our Supervised Fine-Tuned (SFT) model towards generating outputs that the judge would score highly. This is where Reinforcement Learning (RL) enters the picture. Our goal for this lesson is to describe the role of Proximal Policy Optimization (PPO) in optimizing the SFT model against the reward model, focusing on how it achieves this goal while maintaining stability. You will understand why a simple "maximize the reward" approach is doomed to fail and how PPO's engineered safeguards overcome these challenges.

From Supervised Learning to Reinforcement Learning

Our objective is to fine-tune our existing SFT model so that it produces responses that earn a high score from our reward model. At first glance, this might seem straightforward. Why not just generate a response, get the reward score, and use gradient ascent to update the model to maximize that score?

The problem lies in a fundamental step of the generation process: sampling. When an LLM generates text, it samples the next token from a probability distribution. This act of sampling is a discrete, random event—like rolling a die. You cannot calculate a smooth gradient through a die roll. This "die roll problem" makes direct backpropagation from the reward signal to the model weights mathematically impossible.

To solve this, we shift our perspective from supervised learning to reinforcement learning.

The RL Framework for LLMs

Let's map our LLM alignment problem to the standard RL framework. This analogy will be our guide for the rest of the lesson.

  • Agent: The model we are training (initialized from the SFT model). In RL, this is also called the policy, denoted as .
  • Action: Generating the next token in a sequence. A full response is a sequence of actions.
  • Environment: The context provided by the prompt, which sets the stage for the agent's actions.
  • Reward: The scalar score produced by our frozen reward model, , for a complete generated response .

The goal is no longer to maximize the reward of one specific output but to update the policy to maximize the expected reward over all possible outputs it could generate. This objective is written as:

Miraculously, there's an elegant mathematical "trick" to compute the gradient of this objective, bypassing the non-differentiable sampling step. It's called the Policy Gradient Theorem.

Reinforcement Learning | RLHF Book by Nathan Lambert

To understand the mathematical foundation of PPO, we first need to grasp the core policy gradient theorem. This chapter from the RLHF Book provides a clear, formal derivation.

Please read the section Policy Gradient Algorithms. Given your background, you should find the derivation using the log-derivative trick quite accessible. Focus on understanding how we arrive at the final policy gradient formulation, which allows us to estimate the gradient using samples from the policy: \nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta} \left[ \sum_{t=0}^\infty \nabla_\theta \log \pi_\theta(a_t|s_t) \Psi_t \right] Here, \Psi_t is a term representing the reward, which we will refine later.

The policy gradient formula essentially tells us to:

  1. Sample a response from our current policy.
  2. For each token (action) in the response, calculate how to adjust the model's weights to make that token more likely (the part, the "steering wheel").
  3. Weight this adjustment by the reward received for the entire response (the part, the "gas pedal"). If the reward was high, we "press the gas" on the updates to make that response more probable. If it was low, we effectively "put it in reverse."

The Runaway Train: Why Naive RL Fails

While the policy gradient theorem gives us a mathematically sound way to learn from a reward signal, applying it naively to LLMs leads to spectacular failures. The process is like a powerful train engine with no brakes, steering, or shock absorbers.

RLHF in 90 min

The video "RLHF in 90 min" provides a fantastic and intuitive explanation of the two primary failure modes of naive RL for LLMs.

Please watch the segment from Failure Modes. The speaker vividly describes the problems of reward hacking and training collapse.

As the video explains, there are two main failure modes:

  1. Reward Hacking (Goodhart's Law): The reward model is only a proxy for true human preference; it has flaws and biases. A powerful optimization algorithm, when told to maximize the RM score at all costs, will not learn to be more helpful. It will learn to ruthlessly exploit the judge's weaknesses. For example, it might discover the RM has a slight bias for longer answers and start producing verbose, unhelpful text to get a higher score. This is "cheating the test."

  2. Training Instability & Collapse: The learning process itself is inherently unstable.

    • High Variance: The reward signal is noisy. A single lucky generation that gets an unusually high reward can cause a massive, violent gradient update, throwing the model's carefully learned linguistic abilities into chaos. This can lead to "catastrophic forgetting," where the model forgets basic grammar or facts.
    • On-Policy Inefficiency: The standard policy gradient algorithm is on-policy, meaning the data generated by a policy is only valid for a single gradient update. After the weights are updated to , all that expensive data must be thrown away. For multi-billion parameter LLMs, this is prohibitively inefficient.

The solution to on-policy inefficiency is to move to an off-policy method using importance sampling. This allows us to reuse a batch of data for multiple update steps. We correct for the fact that the data came from an old policy () by multiplying the objective by a probability ratio: . However, this introduces its own instability: if the new policy becomes very different from the old one, this ratio can explode or vanish, leading back to destructive updates.


PPO: An Engineered System of Safeguards

Proximal Policy Optimization is not just a single formula; it's a carefully engineered system designed to solve all the problems we just outlined. It introduces a series of safeguards that act as the brakes, steering, and suspension for our runaway train.

Let's look at the overall architecture.

This diagram illustrates the PPO training loop in RLHF. A prompt (x) is fed to both the policy we are training (Trained LLM) and a frozen copy of the original SFT model. The policy generates text, which is scored by the Reward Model. The KL Loss regularizes the policy, preventing it from straying too far from the frozen reference. The combined signal is used in the PPO update, which iteratively improves the policy.

The PPO algorithm can be best understood by breaking down its objective function into three key safeguards.

Safeguard 1: The KL Penalty (Steering)

To prevent reward hacking and catastrophic forgetting, we must stop our policy from drifting too far from the sane, well-behaved SFT model we started with. PPO does this by adding a penalty term to the reward. This penalty is the Kullback-Leibler (KL) divergence between the current policy's output distribution and that of a frozen, reference SFT model.

The final reward used for the update is:

Think of this as a "mathematical rubber band." The SFT model is the anchor. If our learning policy () tries to drift too far away to chase a weird reward signal, the KL penalty increases, pulling it back towards the sane anchor. The hyperparameter controls the strength of this rubber band.

Safeguard 2: The Advantage Function (Shock Absorbers)

To combat the high variance of the raw reward signal, PPO doesn't use the reward directly. Instead, it uses the advantage function, which measures how much better or worse an action was compared to the average expectation.

To do this, we introduce another model: the Critic or Value Model, . This is a copy of the LLM with a scalar head (like the reward model) that is trained to predict the expected future reward from a given state .

The advantage is then calculated as:

This is the "surprise factor." A high reward isn't necessarily good if an even higher reward was expected. Conversely, a low reward can still lead to a positive update if a large negative reward was expected. This relative signal is far more stable than the raw, noisy reward. The canonical method for calculating this is Generalized Advantage Estimation (GAE), which cleverly blends surprises over multiple timesteps to get a low-variance estimate.

Safeguard 3: The Clipped Surrogate Objective (Brakes)

This is the signature innovation of PPO. It directly tackles the problem of the exploding importance sampling ratio (). The policy objective is replaced with a "clipped" version:

Let's break this down:

  • is the probability ratio .
  • is the advantage calculated in the previous step.
  • The clip function clamps the ratio within a small window, typically if .
  • The min function takes the smaller of the normal objective and the clipped objective.

This seemingly complex formula has a simple, brilliant effect: it creates a "trust region" and removes the incentive for the optimizer to make excessively large updates.

Reinforcement Learning | RLHF Book by Nathan Lambert

The RLHF Book provides a concise but mathematically precise explanation of the PPO objective and, crucially, walks through exactly how the clipping mechanism behaves in different scenarios.

First, read the brief introduction to the PPO objective in the section Proximal Policy Optimization. Then, carefully study the next section, Understanding the PPO Objective. This walks through the cases for positive and negative advantage and is key to building a deep intuition for why PPO is stable.

The key takeaway from the reading is that the clipping effectively acts as a governor on the policy update.

  • If an action was good (), the objective "clips" the update, preventing the policy from becoming overconfident in that action too quickly.
  • If an action was bad (), the objective ensures the policy is penalized, but again, the update is bounded to prevent over-correction.

The Complete PPO System

Finally, we assemble all the pieces. The full PPO training process involves four models:

  1. The Actor (the policy , which is learning).
  2. The Critic (the value model , which is also learning).
  3. The Reward Model (the judge, which is frozen).
  4. The Reference Model (the SFT anchor for the KL penalty, which is frozen).
A detailed view of the PPO training architecture. Text data is passed to the learning 'Trained LM' (Actor) and a 'Frozen LM' (Reference). The Actor's output is scored by the 'Reward (Preference) Model' and also compared to the Frozen LM to compute a KL penalty. This 'Final reward' is combined with predictions from a Critic (part of the Actor model) to compute the PPO loss, which then updates the Actor's weights.

The total loss function that is minimized has three components:

  • is the clipped policy loss we just discussed, which updates the Actor.
  • is a value function loss (typically mean squared error) that updates the Critic to make better predictions.
  • is an entropy bonus that encourages the policy to maintain some randomness and not become too deterministic, aiding exploration.

The training alternates between a rollout phase, where the current policy generates a batch of data, and a learning phase, where the actor and critic are updated for several epochs on that same batch of data using this composite loss function.

RLHF in 90 min

The "RLHF in 90 min" video provides an incredibly detailed, step-by-step walkthrough of this entire assembly line, complete with numerical examples that make the abstract concepts concrete.

This is a longer watch, but it directly maps to the theory we've discussed and will solidify your understanding. Start with Augmented Reward to see how the KL penalty is calculated token-by-token. Next, watch Calculating the Advantage to understand the role of the critic and GAE. Then, see the core clipping mechanism in action in The Final Loss (Policy Loss). Finally, see how all components, including the value loss and entropy bonus, come together in Assembling the full PPO Loss.


Conclusion

In this lesson, we have taken a deep dive into Proximal Policy Optimization, the engine that powers the final stage of RLHF. You've seen that PPO is not just one algorithm but a robust system of interlocking components, each designed to counteract a specific failure mode of naive reinforcement learning.

Here are the key takeaways:

  • Directly optimizing an LLM against a reward signal is impossible due to the non-differentiable sampling step and is unstable due to reward hacking and high variance.
  • PPO solves this by using the policy gradient theorem to learn from the expected reward, avoiding direct backpropagation through sampling.
  • It introduces three crucial safeguards:
    1. A KL penalty to regularize against policy drift and prevent the model from deviating too far from its sane SFT initialization.
    2. A value function (critic) and advantage estimation to create a stable, low-variance learning signal based on "surprise" rather than raw reward.
    3. A clipped surrogate objective to constrain the size of policy updates, preventing the destructive instability associated with off-policy learning.
  • The result is a stable, efficient process that fine-tunes an LLM to produce outputs that align with a learned model of human preferences.

PPO has been foundational to the success of models like ChatGPT. However, as you've seen, it is a complex system involving four separate models and a sophisticated RL training loop. This complexity has motivated researchers to find simpler alternatives.

In our next lesson, we will explore one such alternative: Direct Preference Optimization (DPO). You will learn how DPO cleverly reframes the problem to directly optimize the policy on preference data, completely bypassing the need for an explicit reward model and the entire RL optimization loop.