GRPO
DeepSeekMath / GRPO: Group Relative Policy Optimization Shao et al. arXiv
Generate several answers to the same question and reinforce the ones that score above the group average.
Why it mattered
Classic PPO for LLMs forces you to train and hold a second full-size critic network in GPU memory just to estimate per-token baselines, doubling compute and destabilizing training whenever the critic lags behind the policy. DeepSeekMath showed that for verifiable tasks like math and code, you can throw the critic away and use the prompt's own rollout group as a free, zero-parameter baseline.
Core insightFor each prompt, sample completions, grade them with a deterministic verifier, and compute the group-normalized advantage Combine with a clipped importance ratio and KL regularization against the frozen initial weights.
GRPORecipe is a first-class managed recipe in grad: point it at any prompt+answer dataset with validator='math', advantage='group_std', group_size=8, and kl_coef=0.04 for server-managed rollouts, single-GPU initial-weight Schulman k3 KL (GRPOKL), zero-variance group skipping, and live grad runs watch telemetry. Drop down to the interactive Trainer API whenever you need custom Python rubrics or multi-turn tool environments.
When to use
Use GRPO when you have a deterministic verifier (math equivalence, regex, JSON schema, exact match, or unit tests) and want to push reasoning accuracy past SFT without paying memory for a PPO critic. If you only need tone, formatting, or preference alignment without automated grading, start with SFT or DPO first.
Step-by-step walkthrough
- 01
Import a verifiable prompt + reference answer dataset
Import GSM8K server-side with
client.datasets.from_hf()(orgrad datasets import hf) so each record carries the user prompt and ground-truthexpected_output. Keeping prompts and answers in a versioned dataset guarantees reproducible train/eval splits.client.datasets.from_hf("openai/gsm8k", config="main", split="train", prompt="question", answer="answer", limit=2560, name="gsm8k-grpo") - 02
Configure managed GRPORecipe with built-in math validation
GRPOReciperuns server-side group rollouts (group_size=8,top_p=1.0), grades every completion with built-invalidator='math', computesgroup_stdadvantages, and drops zero-variance groups (skip_zero_var=True). No custom rollout loop or external math grader is required.GRPORecipe(model="qwen/qwen3-8b", validator="math", advantage="group_std", group_size=8, kl_coef=0.04, max_steps=80) - 03
Preview cost ($0) and run with single-GPU initial-weight KL
Call
client.runs.plan()to verify token budget and memory before launching. During training,GRPOKLcomputes Schulman k3 KL against frozen initial weights on the same GPU (no second model), steps with calibratedlr='auto'(2.5e-5on Qwen3-8B), and tracks held-outeval_pass_at_1.client.runs.create(dataset=ds, name="grpo-gsm8k", recipe=recipe, max_cost_usd=10.0).wait() - 04
Watch live rewards, entropy, and KL in grad runs watch
Monitor live mean reward, skipped zero-variance group share, true token-level Shannon entropy,
kl_ref, andeval_pass_at_1from the unifiedgrad runs watchTUI, then download the exportedpeft_loraartifact.grad runs watch grpo-gsm8k
Implementation
from grad import Grad, adapters
from grad.recipes import GRPORecipe
client = Grad()
ds = client.datasets.from_hf(
"openai/gsm8k", config="main", split="train", prompt="question", answer="answer", limit=2560, name="gsm8k-grpo"
)
recipe = GRPORecipe(
model="qwen/qwen3-8b",
validator="math",
advantage="group_std",
group_size=8,
max_tokens=1024,
kl_coef=0.04,
clip_low=0.2,
clip_high=0.2,
max_steps=80,
system_prompt="Solve the math problem step by step and put the final answer in \\boxed{...}.",
adapter=adapters.LoRA(rank=32),
)
print(client.runs.plan(dataset=ds, name="grpo-gsm8k", recipe=recipe))
run = client.runs.create(dataset=ds, name="grpo-gsm8k", recipe=recipe, max_cost_usd=10.0).wait()
print(run.artifact.ref, run.eval_report)Benchmarks
Reported results from the original paper compared against its baseline.
Models & datasets
| Model | Duration | Est. cost |
|---|---|---|
Qwen3 8Brecommendedqwen/qwen3-8b | ~28 min | ~$2.20 |
Qwen3 4Bfast smoke runqwen/qwen3-4b | ~14 min | ~$1.10 |
Qwen3.5 9Bqwen/qwen3.5-9b | ~32 min | ~$2.60 |
| Dataset | Source | Size |
|---|---|---|
| GSM8K Math | Hugging Face | 7,473 train / 1,319 test |
| MATH Benchmark | Hugging Face | 7,500 train / 5,000 test |
Practical gotchas
GRPORecipesetsskip_zero_var=Trueandtop_p=1.0automatically so zero-variance groups never waste GPU steps and importance ratios stay exact.- Keep
kl_coefsmall () on verifiable math tasks; overly large KL penalties suppress exploration of longer reasoning chains. - Built-in server validators cover
math,exact_match,contains,final_number,choice,json,non_empty, andregex; use the interactiveTrainerAPI when you need custom Python code execution or multi-turn tool environments.