Reinforcement Learning2024

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 insight

For each prompt, sample GG completions, grade them with a deterministic verifier, and compute the group-normalized advantage Ai=ri−mean⁡(r)max⁡(std⁡(r),σmin⁡)A_i = \frac{r_i - \operatorname{mean}(r)}{\max(\operatorname{std}(r), \sigma_{\min})} Combine AiA_i 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

  1. 01

    Import a verifiable prompt + reference answer dataset

    Import GSM8K server-side with client.datasets.from_hf() (or grad datasets import hf) so each record carries the user prompt and ground-truth expected_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")
  2. 02

    Configure managed GRPORecipe with built-in math validation

    GRPORecipe runs server-side group rollouts (group_size=8, top_p=1.0), grades every completion with built-in validator='math', computes group_std advantages, 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)
  3. 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, GRPOKL computes Schulman k3 KL against frozen initial weights on the same GPU (no second model), steps with calibrated lr='auto' (2.5e-5 on Qwen3-8B), and tracks held-out eval_pass_at_1.

    client.runs.create(dataset=ds, name="grpo-gsm8k", recipe=recipe, max_cost_usd=10.0).wait()
  4. 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, and eval_pass_at_1 from the unified grad runs watch TUI, then download the exported peft_lora artifact.

    grad runs watch grpo-gsm8k

Implementation

train.py
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.

Hendrycks MATH pass@1DeepSeekMath 7B (RL vs. Instruct SFT baseline)
+4.9 pts
DeepSeekMath-Instruct 7B46.8%
DeepSeekMath-RL 7B (GRPO)51.7%
GSM8K pass@1DeepSeekMath 7B (RL vs. Instruct SFT baseline)
+5.3 pts
DeepSeekMath-Instruct 7B82.9%
DeepSeekMath-RL 7B (GRPO)88.2%

Models & datasets

ModelDurationEst. 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
DatasetSourceSize
GSM8K MathHugging Face7,473 train / 1,319 test
MATH BenchmarkHugging Face7,500 train / 5,000 test

Practical gotchas

  • GRPORecipe sets skip_zero_var=True and top_p=1.0 automatically so zero-variance groups never waste GPU steps and importance ratios stay exact.
  • Keep kl_coef small (0.01–0.040.01\text{–}0.04) 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, and regex; use the interactive Trainer API when you need custom Python code execution or multi-turn tool environments.

grad primitives