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.
def group_advantages(rewards, scale=True, eps=1e-6)Example
group_advantages([[1, 0, 1, 0], [1, 1, 1, 1]]) gives adv [[0.999998, -0.999998, 0.999998, -0.999998], [0, 0, 0, 0]] and keep [True, False].
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.
Example: 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.
Rule: 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,)