PPO Optimization

Given the KL-regularized objective, we want a policy that maximizes reward while staying close to the reference model:

max⁡ϕEx,y∼πϕ[rθ(x,y)−λDKL(πϕ(⋅∣x) ∥ πref(⋅∣x))]\max_\phi \mathbb{E}_{x,y\sim\pi_\phi}\left[r_\theta(x,y)-\lambda D_{KL}(\pi_\phi(\cdot\mid x)\,\Vert\,\pi_{ref}(\cdot\mid x))\right]

We cannot enumerate every possible completion, and reward is only defined on sampled trajectories. So RLHF turns the objective into stochastic optimization: sample outputs, estimate reward minus KL, and take gradient steps.

Advantage equation

Turn a scored response into an update signal

PPO does not just ask whether a response has a high reward. It asks whether the response is better than expected after subtracting drift from the reference policy.

Sampled response

"KL control matters because reward optimization can otherwise move the policy toward brittle shortcuts."

7.4
1.6
5.2
A(x,y)=rθ(x,y)-λDKL-baseline=0.6

reward

7.4

KL cost

-1.6

baseline

-5.2

advantage

+0.6

This response is better than expected after accounting for KL, so the policy-gradient step should increase its probability.
Positive advantage means make this sampled answer more likely. Negative advantage means make it less likely.

A simple policy-gradient view is:

∇ϕJ=E[A(x,y)∇ϕlog⁡πϕ(y∣x)]\nabla_\phi J=\mathbb{E}\left[A(x,y)\nabla_\phi\log\pi_\phi(y\mid x)\right]

where

A(x,y)=rθ(x,y)−λDKL−baselineA(x,y)=r_\theta(x,y)-\lambda D_{KL}-\text{baseline}

This is the REINFORCE idea: increase the probability of sampled outputs with positive advantage and decrease the probability of outputs with negative advantage. But for large language models, large reward signals can cause massive policy shifts, KL explosion, reward hacking, or collapse.

Policy-gradient intuition

Advantage tells log probability which way to move

Reinforce sampled outputs that beat the baseline, and suppress sampled outputs that underperform.

∇φJ=E[A(x,y)·∇φ log πφ(y | x)]
1.4
24%
0.35

Before update

Probability of this sampled answer

πφ(y | x)24%

Push

up

After one small step

Illustrative next probability

πφ(y | x)32%
The log-probability term points at the sampled output. The advantage decides the sign and strength of the push.

PPO stabilizes that update by comparing the current policy to the policy that produced the sampled data:

rt=πϕ(a∣s)πold(a∣s)r_t=\frac{\pi_\phi(a\mid s)}{\pi_{old}(a\mid s)}

Then it maximizes the clipped surrogate:

LPPO=E[min⁡(rtA,clip⁡(rt,1−ϵ,1+ϵ)A)]L_{PPO}=\mathbb{E}\left[\min(r_tA,\operatorname{clip}(r_t,1-\epsilon,1+\epsilon)A)\right]

The ratio rtr_t asks how much more likely the new policy makes the sampled action. The clip prevents one batch from pushing probabilities too far in a single update.

PPO

Walk through the clipped objective

PPO compares the new policy to the old policy using a probability ratio, then clips updates that move too far.

=E[(,)]

min(...)

PPO takes the more conservative of the unclipped and clipped terms. That is the safety catch in the equation.

1.18
1.2

Unclipped term

rtA = 1.18 × 1.2 = 1.42

Clipped term

clip(1.18, 0.80, 1.20)A = 1.18 × 1.2 = 1.42

clip range

0.80-1.20

clipped r_t

1.18

unclipped

1.42

PPO term

1.42

The ratio is inside the trust range, so PPO uses the normal policy-gradient update for this sample.
PPO is still policy gradient learning. The clipping term makes the gradient less eager to take huge steps from one batch of samples.

The policy-optimization loop has four conceptual steps:

  1. Sample responses from the current policy for a batch of prompts.
  2. Score each response with the reward model, plus any safety or KL penalties.
  3. Estimate which sampled actions were better than expected.
  4. Update the policy carefully so good samples become more likely without causing a large distribution shift.

This is still reinforcement learning. The "environment" is the prompt-and-response process, the action is the generated text, and the reward is learned from human or AI feedback.

GRPO, or Group Relative Policy Optimization, makes the baseline local to the prompt. Instead of asking whether one response has a high score in absolute terms, it samples a small group of responses to the same prompt and asks which ones beat the group's average.

This is useful for language models because reward scales can vary across prompts. A score of 7 might be excellent for a difficult safety prompt but mediocre for an easy summary prompt. The group baseline turns the update into a within-prompt comparison.

Grouped optimization

Sample a response group

GRPO-style updates compare several sampled responses to the same prompt, using the group as the baseline.

Sample 1

2.1

-2.47

Sample 2

8.6

+4.03

Sample 3

3.2

-1.37

Sample 4

4.4

-0.17

group mean

4.57

strongest update

Sample 2

The useful signal is relative: which sampled responses beat the group expectation for this exact prompt?

A simple group-relative advantage can be written as

Ai=ri−1K∑j=1KrjA_i=r_i-\frac{1}{K}\sum_{j=1}^{K}r_j

Each response is compared to the average reward for the sampled group. Responses above the group mean receive positive advantage; responses below it receive negative advantage. This makes the training signal prompt-local: the model asks which sampled answer was better for this prompt, not merely which answer had a high global score.

⚠

Why PPO Instead of Raw REINFORCE?

  • Raw updates can produce massive policy shifts
  • KL can explode when reward pressure dominates
  • The policy may discover reward hacks or collapse into narrow behavior
  • PPO clips overly large probability-ratio changes before they dominate training
Checkpoint

What is the purpose of comparing multiple sampled responses to the same prompt in a group-relative update?