Group Relative Policy Optimization (GRPO) trains a policy by comparing multiple completions of the same prompt. It is a PPO-inspired policy-gradient method used in RLHF and reasoning tasks. Instead of learning a value function, it estimates each completion’s advantage from its reward relative to the group, avoiding an additional value model and its memory cost. section 6.1 section 6.2.8
For a prompt s, sample G completions and score them with rewards r₁ through rG. The advantage of completion i is Aᵢ = (rᵢ − mean(r)) / std(r). A reward above the group mean gives a positive advantage; one below it gives a negative advantage. The same completion-level advantage is assigned to each token in that completion. section 6.2.8
Equation 6.34
\[\label{eq:GRPO_token} \begin{aligned}[b] J(\theta) &= \frac{1}{G}\sum_{i=1}^G \frac{1}{|a_i|}\sum_{t=1}^{|a_i|} \Biggl( \min\Biggl( \frac{\pi_\theta(a_{i,t}\mid s_i)} {\pi_{\theta_{\mathrm{old}}}(a_{i,t}\mid s_i)} A_{i,t}, \\[2.5pt] &\qquad\qquad \operatorname{clip}\Biggl( \frac{\pi_\theta(a_{i,t}\mid s_i)} {\pi_{\theta_{\mathrm{old}}}(a_{i,t}\mid s_i)}, 1-\varepsilon, 1+\varepsilon \Biggr) A_{i,t} \Biggr) \\[2.5pt] &\qquad -\beta \mathcal{D}_{\mathrm{KL}} \bigl( \pi_\theta(\cdot\mid s_i) \Vert \pi_{\mathrm{ref}}(\cdot\mid s_i) \bigr) \Biggr). \end{aligned}\]The token-level objective combines group-relative advantages with PPO-style clipping and a KL penalty against a reference policy. section 6.2.8
For each token, the objective compares the current policy probability with the probability under the policy that generated the samples. It takes the smaller of the advantage-weighted ratio and its clipped counterpart, limiting the incentive for large updates. The standard formulation also subtracts a KL penalty from the maximized objective; equivalently, the penalty is added to the minimized loss. This places KL directly in the loss rather than shaping the reward, though other implementations are possible. section 6.2.8 section 6.3.6
mean_grouped_rewards = rewards.view(
-1,
self.num_generations,
).mean(dim=1) 1
std_grouped_rewards = rewards.view(
-1,
self.num_generations,
).std(dim=1)
mean_grouped_rewards = mean_grouped_rewards.repeat_interleave(
self.num_generations,
dim=0,
)
std_grouped_rewards = std_grouped_rewards.repeat_interleave(
self.num_generations,
dim=0,
)
advantages = (
rewards - mean_grouped_rewards
) / (std_grouped_rewards + 1e-4)
advantages = advantages.unsqueeze(1) # Shape: (B*G, 1)
ratio = torch.exp(
new_per_token_logps - per_token_logps
) # Shape: (B*G, L) #1
eps = self.cliprange # e.g. 0.2
pg_losses1 = -advantages * ratio # Shape: (B*G, L)
pg_losses2 = -advantages * torch.clamp(
ratio,
1.0 - eps,
1.0 + eps,
) # Shape: (B*G, L)
pg_loss_max = torch.max(pg_losses1, pg_losses2) # Shape: (B*G, L)
per_token_loss = (
pg_loss_max + self.beta * per_token_kl
) # Shape: (B*G, L) #2
loss = (
(per_token_loss * completion_mask).sum(dim=1)
/ completion_mask.sum(dim=1)
).mean() #3
with torch.no_grad(): #4
clip_frac = (
(pg_losses2 > pg_losses1).float() * completion_mask
).sum() / completion_mask.sum()
approx_kl = (
0.5
* ((new_per_token_logps - per_token_logps) ** 2)
* completion_mask
).sum() / completion_mask.sum()
The implementation groups rewards by prompt, broadcasts normalized advantages across tokens, and computes the clipped per-token policy loss. A small denominator offset stabilizes normalization. section 6.3.6
Compared with PPO, GRPO removes the learned value network and its loss. Compared with RLOO, it uses the group mean and standard deviation rather than a leave-one-out reward baseline. Both learned reward models and verifiable rewards can supply scores. Loss aggregation, clipping and handling truncated generations remain consequential implementation choices. section 6.2.11 section 6.3.6 section 6.6


