524. Compute batched GRPO advantages and filter zero-signal groups
Why it matters Group-relative advantages replace a learned value function in GRPO-style RL for reasoning models.
GRPO samples G answers per prompt and scores each answer against the other answers to the SAME prompt. If every answer got the same reward, the group teaches nothing and is often filtered out.
Write group_advantages(rewards, scale=True, eps=1e-6). rewards is (P, G). For each row: adv = r - mean(r); if scale, divide by (population std of the row + eps). Return (adv, keep) where adv is (P, G) float and keep is a length-P bool array that is True when the row's std is greater than 0.
Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.
def group_advantages(rewards, scale=True, eps=1e-6)Example 1
Input: group_advantages([[1, 0, 1, 0], [1, 1, 1, 1]])
Output: adv [[0.999998, -0.999998, 0.999998, -0.999998], [0, 0, 0, 0]] and keep [True, False]
Example 2
Input: rewards = [[0.2, 0.5, 0.8], [0.0, 0.0, 0.9]]
Output: ([[-1.22474, 0.0, 1.22474], [-0.707105, -0.707105, 1.41421]], [True, True])
Example 3
Input: rewards = [[3, 1, 2], [5, 5, 5]], scale = False
Output: ([[1.0, -1.0, 0.0], [0.0, 0.0, 0.0]], [True, False])
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
Hint 1
Use axis=1 with keepdims=True for the mean and std.
Hint 2
Per-row statistics with keepdims.
Hint 3
Population std is np.std's default.
Common mistakes
- Normalising across the whole batch.
- Dividing by std without eps and getting NaN for uniform groups.
Understand the idea
In GRPO the model answers the same question several times, and each answer is graded against its siblings, like grading on a curve within one classroom instead of across the whole school. That removes the need for a separate value network.
one prompt gets rewards 1, 0, 1, 0. The mean is 0.5 and the std is 0.5, so the advantages are about +1 and -1: correct answers are pushed up, wrong ones down. Another prompt gets 1, 1, 1, 1. Every advantage is 0: the model already solves it, so this group gives no learning signal.
A[p, i] = (r[p, i] - mean_p) / (std_p + eps), or without the division when scale is off; keep[p] = std_p > 0
Some later recipes drop the std division to avoid over-weighting nearly uniform groups, and some resample prompts whose groups have no spread so every batch contributes gradient.
Shapes and data flow
rewards (P, G) -> mean, std (P, 1) -> adv (P, G); keep (P,)
Topics
Companies1
- Anthropic a candidate reported this question Anthropic: GRPO trainer debugging (CircleCoder)
From public interview reports and guides. Not affiliated with or endorsed by these companies.