532. GRPO: group-relative advantages with the PPO clipped objective
GRPO, used to train DeepSeek-R1, samples a group of answers per prompt and scores each against its own group, so no value network is needed. It then uses PPO's clipped objective.
For one prompt with G sampled answers:
- advantage A_i = (r_i - mean(r)) / (std(r) + 1e-8), population std
- ratio_i = exp(logp_new_i - logp_old_i)
- per-answer objective = min(ratio_i * A_i, clip(ratio_i, 1 - eps, 1 + eps) * A_i)
- return the mean objective over the group (to be maximised)
Write grpo_objective(rewards, logp_new, logp_old, eps=0.2). Return (objective, advantages).
def grpo_objective(rewards, logp_new, logp_old, eps=0.2)Example 1
Input: rewards = [1.0, 0.0, 0.0, 1.0], logp_new = [-1.0, -2.0, -1.5, -0.5], logp_old = [-1.2, -1.8, -1.5, -1.0]
Output: (0.145317, [1, -1, -1, 1])
Example 2
Input: rewards = [1.0, 1.0], logp_new = [-1.0, -2.0], logp_old = [-1.5, -1.0]
Output: (0.0, [0.0, 0.0])
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1
Compute advantages first.
Hint 2
Then the min of the unclipped and clipped terms, elementwise.
Common mistakes
- Using only the clipped term (the buggy version) is wrong when the ratio moves in the harmful direction.
Understand the idea
GRPO samples several answers to the same prompt and scores each against its own group, so it needs no value network. Advantage = (reward - group mean) / group std, meaning better or worse than the average answer for this prompt. Then it uses PPO's clipped objective: the probability ratio new / old is clipped to [1 - eps, 1 + eps], and you take the smaller of the clipped and unclipped terms, a cautious choice.
rewards [1, 0, 0, 1], mean 0.5, population std 0.5, so advantages [1, -1, -1, 1]. Say ratios are [1.5, 1.0, 0.7, 1.0], eps = 0.2. Answer 1: min(1.5 x 1, 1.2 x 1) = 1.2. Answer 2: -1. Answer 3: min(0.7 x -1, 0.8 x -1) = min(-0.7, -0.8) = -0.8. Answer 4: 1. Mean = (1.2 - 1 - 0.8 + 1) / 4 = 0.1.
A_i = (r_i - mean) / (std + 1e-8); ratio = exp(new - old); objective = mean(min(ratio A, clip(ratio) A))
DeepSeek-R1 style reasoning training uses GRPO.
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.