Group-Based Policy Optimization › A Worked Example by Hand

Michael BrenndoerferNovember 29, 20264 min read
Language AI HandbookAll chapters
Part 40 of 67Reasoning Post-Training and Verifiable Rewards
Chapter 301Group-Based Policy Optimization › A Worked Example by Hand
Reading section 4 of 10 · A Worked Example by Hand · 4 minSection 4 of 10 · 4 min

Section 4 of 10 · 4 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Before writing code, work through a small group by hand. Suppose a prompt receives five sampled responses with rewards [1,0,1,1,0][1, 0, 1, 1, 0]. The group mean is 0.60.6 and the population standard deviation is 0.24≈0.49\sqrt{0.24} \approx 0.49. The normalization gives:

A^1=(1−0.6)/0.49≈+0.82,A^2=(0−0.6)/0.49≈−1.22,A^3=+0.82,A^4=+0.82,A^5=−1.22.\begin{aligned} \hat{A}_1 &= (1 - 0.6)/0.49 \approx +0.82, \\ \hat{A}_2 &= (0 - 0.6)/0.49 \approx -1.22, \\ \hat{A}_3 &= +0.82, \\ \hat{A}_4 &= +0.82, \\ \hat{A}_5 &= -1.22. \end{aligned}

The unrounded advantages sum to zero. The displayed values sum to 3(0.82)+2(−1.22)=0.023(0.82)+2(-1.22)=0.02 because of rounding. There are fewer incorrect answers in this group, so each has a larger absolute advantage than each correct one. A zero sum of advantage coefficients does not imply a zero update.

Return to the opening group with seven rewards of 1 and one reward of 0. Its mean is 7/87/8 and its population standard deviation is 7/8\sqrt{7}/8. Each correct completion gives its tokens advantage 1/7≈0.381/\sqrt{7}\approx 0.38. The incorrect completion gives its tokens advantage −7≈−2.65-\sqrt{7}\approx -2.65. At the start of an update with the learner equal to the behavior policy, every ratio is one. The clipped surrogate for each token then equals that token’s advantage before the KL term and the two averaging steps apply.

A useful mental model

The group mean plays a role similar to the reference point in grading on a curve. In the five-response group, the correct answers sit 0.4 above it and the incorrect answers sit 0.6 below it before scaling. If all answers receive the same score, every score equals the group mean and the guarded advantages are zero.

PythonIn [13]
CollapseExpand
import torch

rewards_example = torch.tensor([1.0, 0.0, 1.0, 1.0, 0.0])
mean_r = rewards_example.mean()
std_r = rewards_example.std(unbiased=False)
adv_example = (rewards_example - mean_r) / (std_r + 1e-4)

print(f"rewards:     {rewards_example.tolist()}")
print(f"group mean:  {mean_r.item():.4f}")
print(f"group std:   {std_r.item():.4f}")
print(f"advantages:  {[round(x, 4) for x in adv_example.tolist()]}")
print(f"adv mean:    {adv_example.mean().item():.6f}")
ConsoleOut [13]
CollapseExpand
rewards:     [1.0, 0.0, 1.0, 1.0, 0.0]
group mean:  0.6000
group std:   0.4899
advantages:  [0.8163, -1.2245, 0.8163, 0.8163, -1.2245]
adv mean:    -0.000000

The computed advantages match the hand calculation. The mean of the advantages is approximately zero, which is what the within-prompt normalization is designed to enforce. Note that the epsilon in the denominator has essentially no effect here, because the standard deviation is well above it; the epsilon exists for the case where the standard deviation collapses to zero.

The figure below visualizes the mapping from rewards to advantages for this group.

VisualizationOut [14]
CollapseExpand
Bar chart of GRPO group advantages for five responses; three positive bars and two negative bars, all summing to zero.
Within-group GRPO advantages for a group of five responses with rewards [1, 0, 1, 1, 0]. The three correct responses receive equal positive advantage of about +0.82, the two incorrect responses receive larger-magnitude negative advantage of about -1.22, and the advantages sum to zero within the group. The asymmetry arises because the group mean of 0.6 sits closer to 1 than to 0.

Two properties are worth noting. First, the correct responses are visually shorter than the incorrect ones: +0.82+0.82 versus −1.22-1.22. That asymmetry follows directly from the mean being 0.60.6, closer to 1 than to 0. Second, subtracting one shared mean makes the advantages sum to zero in exact arithmetic. Dividing by one shared positive number, including the standard deviation plus epsilon, preserves that arithmetic identity. The inclusive group mean still depends on each action, and the weighted token gradient can be nonzero.

If all five rewards were one, the group mean would be one and the standard deviation would be zero. The unguarded equation would be undefined. With the positive epsilon used in the example, every advantage is zero and this group contributes no reward-surrogate gradient.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy