GRPO and RLOO
PPO for language models, as RLHF runs it, keeps four networks in memory, the policy being trained, a frozen reference copy, a reward model, and a value model (§16.4). The value model is the awkward one. It is usually as large as the policy, it must be trained alongside it, and its job in reasoning RL is close to impossible. After every prefix of a long solution, it must predict the probability that the final answer will turn out correct. A bad value model makes bad advantages, and bad advantages make a bad policy.
In early 2024 the DeepSeekMath paper proposed throwing the value model away (Shao et al. 2024). For each prompt, sample a group of responses, score each one, and use the group's own average reward as the baseline. A response is good if it beat its siblings. They called it Group Relative Policy Optimization (GRPO), and a year later it trained DeepSeek-R1. Around the same time, Ahmadian et al. (2024) argued from the RLHF side that PPO's machinery was unnecessary for language models and that a REINFORCE estimator with a leave-one-out baseline, RLOO, did better. The two methods are closer than their names suggest, and understanding exactly how close is the fastest way into this whole family of algorithms.
This section derives the group baseline from the policy gradient, writes the GRPO objective term by term, then looks hard at the details that later papers found to matter, the normalizations that introduce biases (Dr. GRPO), the tricks that make long-chain RL stable (DAPO), and the move from token-level to sequence-level ratios (GSPO). It ends with a complete implementation.
The policy gradient with a group baseline
Treat a whole response as one action, as §15.7 does for terminal rewards. With prompt , response , and a scalar reward , the policy gradient theorem with a baseline (§15.4) is
for any baseline that does not depend on . The natural baseline is the prompt's value, the expected reward , which with 0/1 rewards is the success probability of §18.2. PPO learns with a network. GRPO and RLOO estimate it from other samples of the same prompt.
The leave-one-out baseline
Draw responses independently from with rewards . For response , use the mean reward of the other responses,
In plain words, each response is judged against the average of its siblings, and pushed up if it did better and down if it did worse.
This is unbiased, and the proof is the baseline lemma of §15.4 with one extra observation. Given , the baseline is a function of for only, and those are independent of . So
because the expected score is zero. Each term of is therefore an unbiased estimate of , and so is their average. The baseline costs nothing beyond the extra samples, which RLOO needs anyway, and it is an unbiased estimate of built from data the policy just produced. This is the estimator of Kool, van Hoof, and Welling (2019), "Buy 4 REINFORCE Samples, Get a Baseline for Free!", applied to language models by Ahmadian et al.
The group mean is the leave-one-out baseline, rescaled
Now use the plain group mean as the baseline instead, which includes response 's own reward. §15.4 warned that this makes the baseline depend on the action. Here is exactly how much. Since ,
So . In plain words, subtracting the group mean gives exactly the leave-one-out advantages shrunk by the constant factor . The resulting gradient estimate points in an unbiased direction and is times too short on average, a "bias" that any learning rate absorbs. With , the factor is 0.94. The worry in §15.4 was correct in principle and harmless in practice.
The leave-one-out estimator is unbiased (ratio 1), the group-mean estimator is unbiased up to the factor , and both have less variance than plain REINFORCE. The group mean's variance looks lower still, but only because its estimates are shrunk by . Multiply the leave-one-out variance by and you get the group-mean number, so the two are the same estimator at different scales.
RLOO advantages for a batch of groups
rewards has shape (P, G), one row per prompt and responses per prompt. Implement rloo_advantages(rewards) returning the leave-one-out advantages for every entry, shape (P, G), with no Python loops.
GRPO's advantage
GRPO uses the group mean and also divides by the group's standard deviation,
with a small to avoid dividing by zero. For outcome rewards, every token of response gets this same advantage, . The paper does not say which standard deviation. At the time of writing, TRL and verl both use the sample standard deviation, with Bessel's correction (dividing by ), and we use that convention. With the two conventions differ by only 3%, but the difference is enough to fail an exact test.
A worked example makes the numbers concrete. Take responses of which are correct, with rewards 1 and 0. The mean is . The sample standard deviation is . So each correct response gets and each wrong one gets . The advantages sum to zero, as they always do. The two correct responses share a push up of and the six wrong ones share a push down of the same total.
Notice what happens when all responses get the same reward. The numerator is zero for everyone, and the group contributes no gradient at all. With 0/1 rewards, a prompt that the policy solves with probability produces a mixed group, one with any signal, with probability
For , a prompt at is informative only 34% of the time, and one at the same. Prompts that are too easy or too hard for the current policy waste their rollouts. This fact drives much of the engineering that follows, from DAPO's dynamic sampling to curricula that keep the training set near the frontier of what the model can do.
Group-relative advantages, GRPO and Dr. GRPO
Implement group_advantages(rewards, normalize_std=True, eps=1e-6) for rewards of shape (P, G). Return for each group, divided by the group's sample standard deviation (ddof=1) plus eps when normalize_std is True (GRPO), and not divided when it is False (Dr. GRPO). A group whose rewards are all equal must get exactly zero advantages.
The GRPO objective
DeepSeekMath writes the objective at the token level, borrowing PPO's clipped surrogate (§15.6). Sample the group from the policy as it was before the update, , and define each token's probability ratio
Then GRPO maximizes
where is the number of tokens in response , is the clip range, and is a per-token estimate of the KL divergence to the reference policy,
In plain words, for every token of every response, take PPO's clipped surrogate with the response's group-relative advantage, subtract a penalty for drifting from the reference model, average the tokens within each response, and average the responses.
Each piece has a reason, and two of them have consequences that took the field a year to notice.
- The ratio and the clip are PPO's. They let the same batch of samples be reused for several gradient steps without the policy running away from the distribution that produced them. When the batch is used for a single step, as in DeepSeekMath's own runs, the ratio is exactly 1 at the update and the clip never binds. The gradient of at is , so GRPO reduces to REINFORCE with group-normalized advantages plus the KL gradient.
- The KL term is the estimator of §0.5, with and , whose value is a never negative estimate of , unbiased when the tokens are sampled from itself (here they come from , so it is a close approximation). GRPO adds it to the loss. RLHF with PPO instead subtracts a per-token KL from the reward (§16.4), which then flows through the advantage estimate. DeepSeekMath gives the reason, that putting it in the loss avoids complicating the calculation of . The choice has a cost that §15.7 works out in its table of , , and . Differentiated at the sampled tokens, 's gradient is , whose expectation is the gradient of the KL with its arguments swapped (the forward KL), , not of the reverse KL that the objective writes down and the estimate reports. GRPO's penalty still pulls the policy toward the reference, but through a different divergence. (Used as a reward instead, would be wrong too. Only in the reward gives the reverse KL's gradient.)
- The average weights each response equally regardless of its length. This is the first of the two consequences.
- The std in rescales each prompt's gradient by how varied its group's rewards were. This is the second.
DeepSeekMath trained its 7B model with samples per question, a learning rate of , , a maximum length of 1,024 tokens, a batch of 1,024, and a single policy update per batch, on about 144K chain-of-thought questions from GSM8K and MATH. Its rewards came from a learned reward model trained on DeepSeekMath-Base, not from a program, since the rule-based rewards of RLVR (§18.3) became the norm only with R1. GRPO took the instruction-tuned model from 82.9% to 88.2% on GSM8K and from 46.8% to 51.7% on MATH. The paper also defines a process supervision variant, in which a process reward model (§18.6) scores each step, the step rewards are normalized across the whole group, and each token's advantage is the sum of the normalized rewards of the steps from its own onward. And it describes iterative GRPO, which periodically retrains the reward model on fresh policy samples (with 10% of historical data replayed) and resets the reference model to the current policy.
The GRPO loss with clipping, KL, masking, and three aggregations
Implement grpo_loss(logp, logp_old, logp_ref, adv, mask, eps_low=0.2, eps_high=0.2, beta=0.04, agg="seq_mean", max_len=None). The first three arguments and mask have shape (B, T) for responses padded to tokens (mask is 1 at real tokens and 0 at padding, where the log-probabilities may be garbage), and adv has shape (B,), one advantage per response. Compute, per token, with and , then aggregate with weights to a scalar loss .
"seq_mean"(GRPO), , where ."token_mean"(DAPO), ."constant"(Dr. GRPO), with =max_len, or ifmax_lenis None.
Return (loss, grad), where grad is , shape (B, T), treating logp_old, logp_ref, and adv as constants. At a ratio exactly on a clip boundary, use the unclipped gradient.
Two biases, and Dr. GRPO
Liu et al. (2025) looked at R1-Zero-style training closely and argued that two of GRPO's normalizations bias the optimization in ways nobody intended. Their fix, which they called Dr. GRPO ("GRPO done right"), removes both.
The length bias
Under the average, the total weight of response on the gradient is , whatever its length, so each of its tokens gets . Follow that through for the two signs of the advantage.
- Correct responses (). A short one gets a larger push per token than a long one. The update favors brevity among correct answers.
- Wrong responses (). A long one gets a smaller push down per token than a short one. Take two wrong responses with in a group of , one of 100 tokens and one of 1,000. Each token of the short one is pushed down by and each token of the long one by , ten times less. The update penalizes long wrong answers less, so wrong answers drift longer.
The second effect is the troubling one, because response length grows during reasoning RL for good reasons too (§18.5), and the bias makes some of that growth an artifact of the loss rather than useful thinking. Dr. GRPO replaces the per-response average with a sum over tokens divided by a constant, the maximum generation length (any constant works, it just rescales the gradient). DAPO, independently, divides by the total number of tokens in the group, , so every token in the group counts equally, and many implementations average over all tokens in the batch. Either way, every token in a group (or batch) gets the same weight, so a long response is no longer underweighted per token relative to a short one beside it.
The same issue appeared in §15.3 in a different guise. Dividing each episode's sum by its own length changes the objective, while dividing by a constant or by the batch's total token count only rescales the step, and the analysis above says precisely how the first one changes it.
The difficulty bias
The std normalization multiplies each prompt's contribution by . With 0/1 rewards and large , the sample std approaches , where is the prompt's success probability. The mean-centered estimator's expected gradient is , from the identity above. So in the large- limit,
using . In plain words, GRPO does not maximize the average accuracy . It maximizes , which weights a unit of progress on a prompt by , smallest at and growing without bound as approaches 0 or 1. Prompts the model almost always gets right, and prompts it almost always gets wrong, are upweighted relative to the ones in the middle.
Is that bad? It is a different objective, and not necessarily a worse one. Upweighting hard prompts can be useful, and the finite- effect partly cancels it, since a prompt near usually produces an all-wrong group with no signal at all. But it is not the objective anybody wrote down, and Liu et al. call it a question-level difficulty bias. Dr. GRPO uses the plain mean-centered advantage . REINFORCE++ (Hu 2025) keeps a normalization but computes the mean and std over the whole global batch instead of per prompt, which removes the per-prompt reweighting.
The mean-only column is exactly , the true gradient up to the constant. With the std and , the expected gradient is close to (somewhat below it at , where of groups are still all wrong), which at is about six times the true gradient and at only twice it, so the hard prompt gets about three times the relative weight. With the hard prompt rarely produces a mixed group, and its expected gradient falls.
Liu et al. also studied the base models that this kind of RL starts from. While most base models need a template to answer a question rather than keep writing text, Qwen2.5 base models answered best with no template at all, scoring about 60% higher on average than with the usual 4-shot prompt, which the authors suspect reflects pretraining on concatenated question-answer text. (Their other finding about base models, on where the famous "aha moment" comes from, belongs with the recipe it questions, in §18.5.) Their minimal recipe, Dr. GRPO on Qwen2.5-Math-7B with MATH problems of levels 3 to 5, reached 43.3% on AIME 2024 with 27 hours on 8 A100 GPUs.
DAPO's four fixes
ByteDance's DAPO paper (Yu et al. 2025) set out to reproduce R1-Zero-scale results openly and found that naive GRPO on Qwen2.5-32B reached only 30 points on AIME 2024. Four changes, plus dropping the KL term, took it to 50, beating the 47 that DeepSeek reported for R1-Zero-style training of the same base model, in half the training steps. The ablation adds them one at a time.
| change | AIME 2024 (avg@32) |
|---|---|
| naive GRPO | 30 |
| + overlong filtering | 36 |
| + clip-higher | 38 |
| + soft overlong punishment | 41 |
| + token-level loss | 42 |
| + dynamic sampling (the full DAPO) | 50 |
Clip-higher. PPO's symmetric clip limits how far one update can raise a token's probability, to times its old value. For a token the policy already likes, , that is no constraint, since exceeds 1. For an unlikely token, , it caps the new probability at 0.012, an absolute gain of 0.002, so the rare tokens that start a new line of reasoning can grow only slowly while the already-likely ones are effectively unconstrained. The authors linked this asymmetry to entropy collapse, the policy becoming nearly deterministic early and exploring nothing new. They decoupled the clip range, keeping for decreases and raising for increases.
Dynamic sampling. Groups in which every response is right, or every response is wrong, have zero advantage and contribute nothing except noise in the batch statistics. DAPO over-samples prompts and discards those whose groups are all correct or all wrong, refilling until the batch is full of mixed groups. Formally, the objective is taken only over prompts with . This cost more generation per step and paid for itself in steps, the largest single gain in the ablation.
Token-level loss. As discussed above, normalize by the total number of tokens, , so long responses are not underweighted per token.
Overlong reward shaping. Responses cut off at the length limit have no answer and get a wrong-answer reward even when the reasoning was going well. Overlong filtering masks their loss entirely. Soft overlong punishment (implemented in §18.3's PyTorch block) adds a length penalty that ramps in over the last 4,096 tokens before the 20,480-token limit.
DAPO also used a rule-based reward of for a correct answer and otherwise, samples per prompt, a constant learning rate of , 512 prompts per rollout step, and 16 gradient updates per rollout step, so its ratios do move away from 1 and its clipping does bind. It removed the KL penalty, arguing that a model trained to produce long chains of thought is supposed to move far from its starting distribution.
GSPO, ratios at the sequence level
Qwen's GSPO paper (Zheng et al. 2025) attacked a different part of the objective, the token-level ratio itself. Importance sampling corrects for a mismatch between two distributions by reweighting samples, and it works when you average over many samples from the same distribution. A token-level ratio reweights a single sampled token at a single context, which cannot perform a meaningful distribution correction and adds noise. The noise accumulates over long responses, the clip interacts with it, and in their experiments it led to training collapse, especially for mixture-of-experts models, whose routing changes between the old and new policy make individual token ratios jump around (§13.6). GRPO on MoE models needed routing replay, recording which experts each token used during sampling and forcing the same experts when computing the new probabilities.
GSPO replaces the token ratio with a length-normalized sequence ratio,
the geometric mean of the token ratios, and clips at the sequence level,
In plain words, the unit of reward is a whole response, so the unit of importance weighting and clipping should be a whole response too. The length normalization keeps near 1 for long responses, which is why GSPO's clip ranges are tiny, below and above in their experiments (their GRPO baseline used 0.2 and 0.27). GSPO clipped about two orders of magnitude more tokens than GRPO and still trained more efficiently, it made routing replay unnecessary for MoE models, and Qwen credits it for the improvements in its Qwen3 models. A variant, GSPO-token, allows per-token advantages while keeping the sequence-level ratio's value.
The wild token shifts the mean log-ratio by only , so the length-normalized ratio stays within a fraction of a percent of 1. The raw sequence ratio, the product of all thousand token ratios, is at the mercy of a thousand small fluctuations. It came out at 4.97 here, but its logarithm has standard deviation , so a factor of 20 either way is typical, and the spread grows with length, which makes it useless as an importance weight.
The family at a glance
| method | baseline | advantage normalization | ratio and clip | loss aggregation | KL to reference |
|---|---|---|---|---|---|
| PPO for RLHF (§16.4) | learned value model, GAE | batch whitening (common) | token | per token | per-token penalty in the reward |
| RLOO | mean of the other | none | none (on-policy REINFORCE) | per sequence | usually in the reward |
| GRPO | group mean | divide by group std | token, symmetric | mean per response, then over responses | in the loss |
| Dr. GRPO | group mean | none | token | sum divided by a constant | optional |
| DAPO | group mean | divide by group std | token, , | mean over all tokens | none |
| GSPO | group mean | divide by group std | sequence (geometric mean), tiny | per sequence | optional |
| REINFORCE++ | none per prompt | global batch mean and std | token | per token | in the reward |
| ReMax | reward of the greedy response | none | none | per sequence | optional |
ReMax (Li et al. 2023) is the one row not met above. It uses the reward of the policy's greedy response as the baseline, which costs one extra generation per prompt instead of a group.
Libraries such as TRL, verl, and OpenRLHF expose most of these choices as configuration flags, so in practice "GRPO" names a family, and a paper that reports "GRPO" results needs to say which members of the family it used.
A toy training run
Here is the whole idea on a problem small enough to watch. A "reasoning task" has prompt types, and for each type the model chooses among strategies, each with its own probability of producing a correct answer. The policy is a table of logits, one row per prompt type. Each step samples a group of strategies per prompt, observes 0/1 rewards, and takes one step of REINFORCE with the group-mean baseline, the Dr. GRPO advantage. For a softmax policy the score of action is (§15.3), so the update for one prompt's logits is
REINFORCE with a group baseline on a toy reasoning task
Implement two functions.
group_update(z, actions, rewards, lr) takes one prompt's logits z (shape (A,)), the sampled actions (ints, shape (G,)), and their rewards (shape (G,)), and returns the updated logits by the equation above.
train(S, steps, G, lr, seed) takes success probabilities S of shape (P, A) and trains a policy that starts from all-zero logits. Use rng = np.random.default_rng(seed). At each step, for each prompt x = 0, ..., P-1 in order, draw actions = rng.choice(A, size=G, p=pi) from the current policy, then rewards = (rng.random(G) < S[x, actions]).astype(float), then update that prompt's logits with group_update. Return (Z, acc) where Z is the final (P, A) logits and acc is a list of length steps + 1 holding the expected accuracy before each step and after the last.
In PyTorch
Now the real thing at toy scale, a complete GRPO loop on an autoregressive policy with variable-length responses, masking, a reference model, the KL penalty, DAPO's asymmetric clip and token-level loss, and two gradient steps per batch so the ratio and the clip actually do something. The task is a tiny verifiable one. The prompt is a number from 1 to 4, the vocabulary is the tokens 1, 0, and an end token, and the reward is 1 exactly when the response is copies of 1 followed by the end token. A uniformly random policy solves with probability and with probability . (The randomly initialized network is not exactly uniform, and the printed starting accuracies are estimates from 256 samples each.)
Every line maps to a piece of the objective. The group statistics are computed per prompt with view(-1, G). The mask stops at the first end token, so padding contributes nothing. The old and reference log-probabilities are computed once, without gradients, before the updates. And the hardest prompts, solved a few times in a thousand at the start, are learned because the few groups that contain a success give that success a large positive advantage.
What a GRPO step costs at scale
At real scale the gradient is the cheap part. Take DAPO's batch, 512 prompts per rollout step with responses each, on a 32B-parameter model, and suppose the responses average 5,000 tokens (reasoning RL often runs longer). Count FLOPs with the per generated token and per trained token of §9.2, where is now the parameter count.
Generation is only a sixth of the FLOPs, yet practitioners report that it takes most of the wall-clock time, because decoding is memory-bound. Each decode step reads every weight and the whole KV cache to produce one token per sequence (§6.5, §11.2), while the training passes process all 41M tokens as large matrix multiplications. And the step cannot finish until the longest response does, so a few 20,000-token responses can hold up a batch of 5,000-token ones. Three engineering consequences follow, and interviewers like to ask about all three.
- Separate engines. Rollouts run in a fast inference engine (vLLM or SGLang) and the loss in a training framework, with fresh weights copied to the sampler after each update. The two compute slightly different probabilities for the same token, because their kernels, precision, and batching differ, so samples are a little off-policy even when the batch is used for a single update. Yao et al. (2025) measured this mismatch and corrected it with truncated importance sampling, weighting each token's loss by for a cap .
- Asynchronous rollouts. Generating batch while training on batch keeps the GPUs busy, at the price of samples that are one or more policy versions stale. The ratio and clip in GRPO's objective were designed for exactly this kind of staleness.
- No critic. The value model would be another 32B network to train, 512 GB of weights, gradients, and Adam state at the 16 bytes per parameter of §10.1, plus its forward and backward passes. Dropping it is a large part of GRPO's appeal.
Read more
- Shao et al. (2024). DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models. GRPO's origin, Section 4, with the outcome and process variants and the analysis of why RL works in Section 5.2.
- Ahmadian et al. (2024). Back to Basics: Revisiting REINFORCE Style Optimization for Learning from Human Feedback in LLMs. RLOO, and the argument that PPO's machinery is unnecessary for language models.
- Kool, van Hoof & Welling (2019). Buy 4 REINFORCE Samples, Get a Baseline for Free!. The leave-one-out baseline.
- Liu et al. (2025). Understanding R1-Zero-Like Training: A Critical Perspective. Dr. GRPO, the length and difficulty biases, and what base models already do before RL.
- Yu et al. (2025). DAPO: An Open-Source LLM Reinforcement Learning System at Scale. Clip-higher, dynamic sampling, token-level loss, and overlong shaping, with a full ablation.
- Zheng et al. (2025). Group Sequence Policy Optimization. Sequence-level ratios and clipping, and why token-level ratios destabilize MoE training.
- Li et al. (2023). ReMax: A Simple, Effective, and Efficient Reinforcement Learning Method for Aligning Large Language Models. The greedy-response baseline.
- Hu (2025). REINFORCE++: Stabilizing Critic-Free Policy Optimization with Global Advantage Normalization. Normalizing advantages over the global batch instead of per prompt.