All courses

RL with verifiable rewards (GRPO)

Train on problems with automatically checkable answers by sampling a group of responses and using each one's reward, normalized by the group mean and std, as its advantage in a PPO-style clipped update. Explain why this needs no learned value model or reward model.

Builds on: PPO: clipped updates and a learned value baseline, Reward models from human comparisons

▶ Watch this lesson Start the whole course

What the animation says

Two extra models

PPO trains a second network, the value model, often as big as the policy itself. Its job is to guess the expected reward from each prefix, giving updates a baseline. RLHF adds a third: a learned reward model that only approximates what people want. But for math with a known answer, or code with unit tests, a program can check correctness exactly. So a checker replaces the reward model. What could replace the value model's baseline?

Siblings as the baseline

The answer: ask the same prompt several times. Say G equals four, so four sampled responses. The checker scores each one: one if correct, zero if not. Their average reward estimates how well the policy does on this prompt. That's what the value model was trained to guess. Responses that beat their siblings get pushed up. Worse ones get pushed down. Here, one of four is correct. What happens to the three wrong ones?

Working the numbers

They fall below the average, so they're pushed down. Now let's find out how hard. The mean is zero point two five. Subtract it: the success is plus zero point seven five, each miss minus zero point two five. Then divide by the group's spread. Square the deviations, average over all four, and take the square root: about zero point four three three. Advantages: one point seven three for the success, minus zero point five eight for each miss. They sum to zero, and the rare success gets the big push.

When the group agrees

Now suppose all four answers are correct. Every reward equals the mean, so every deviation is zero, and so is the spread. The tiny delta prevents dividing by zero, and every advantage comes out zero. The same happens if all four fail. Prompts that are too easy or too hard teach nothing. Your turn: tap every group that gives no learning signal.

Plugging into PPO's clip

Everything else is PPO's clipped update. Each response gets one advantage, and every token in that response shares it. For each token, take the ratio of new to old probability, clip it near one, and keep the smaller term. Average over tokens and responses. Some recipes also add a KL penalty to stay near the SFT model. The price is G samples per prompt, and one advantage for all of a response's tokens. Tap the line that replaces the value model.

Can we drop the value and reward models?

PPO trains a second network, the **value model**, usually as large as the policy, to predict expected reward from each prefix. RLHF also needs a learned **reward model**, which only approximates what people want. For math with a known answer, or code with unit tests, a program can check correctness exactly. Can we get rid of both learned models and still have a good baseline?

Let siblings be the baseline

Give the model the same prompt $G$ times (say 8) and sample $G$ responses. A **checker** scores each one, for example 1 if correct and 0 if not. The group's average score estimates how well the current policy does on this prompt. That is the number a value model was trained to guess. Responses that beat their siblings get pushed up, and worse ones get pushed down. Every token in a response shares that response's advantage.

GRPO's advantage and objective

For prompt $x$, sample $y_1..y_G\sim\pi_{old}$ and score them with rewards $r_i$: $$A_i=\frac{r_i-\text{mean}(r)}{\text{std}(r)+\delta}$$ Here $\delta$ is tiny and avoids dividing by 0. With the per-token ratio $\rho_{i,t}=\frac{\pi_\theta(y_{i,t}\mid x,y_{i,<t})}{\pi_{old}(y_{i,t}\mid x,y_{i,<t})}$, maximize $$\frac1G\sum_i\frac{1}{|y_i|}\sum_t\min\big(\rho_{i,t}A_i,\,\text{clip}(\rho_{i,t},1\!-\!\epsilon,1\!+\!\epsilon)A_i\big)$$ $|y_i|$ is the response length and $\epsilon$ is PPO's clip range. The original GRPO also subtracts $\beta\,\mathrm{KL}(\pi_\theta\|\pi_{ref})$ to stay near the SFT model. Some recipes drop this term.

One success out of four

Take $G=4$ with rewards $[1,0,0,0]$. - Mean $=0.25$. Deviations: $0.75,-0.25,-0.25,-0.25$. - Variance $=(0.5625+3\cdot0.0625)/4=0.1875$, so std $\approx0.433$. - $A=[1.73,-0.58,-0.58,-0.58]$, which sums to 0. The rare success gets a big push up. If all 4 responses were correct, std would be 0 and every $A=0$: prompts that are too easy or too hard teach nothing.

Group advantages + clipped loss

Per-token ratios have shape [G, T], where T is the response length. Each response's single advantage is broadcast across its tokens. All responses here have the same length, so one overall mean equals the formula's per-response average.
import numpy as np
rng = np.random.default_rng(0)
r = np.array([1., 0., 0., 0.])                # checker rewards, G=4
A = (r - r.mean()) / (r.std() + 1e-6)         # one advantage per response
logp_old = rng.normal(-1.5, 0.3, size=(4, 3))  # [G, T] token log-probs
logp_new = logp_old + rng.normal(0, 0.2, size=(4, 3))
ratio = np.exp(logp_new - logp_old)           # pi_theta / pi_old per token
eps = 0.2
surr = np.minimum(ratio * A[:, None], np.clip(ratio, 1-eps, 1+eps) * A[:, None])
loss = -surr.mean()                           # minimizing = maximizing surrogate
print(A.round(2), loss)

Remember this

GRPO is PPO's clipped update with no value model. Each response's advantage is its reward minus the group's mean reward, divided by the group's std. The reward comes from a program that checks the answer, so no learned reward model is needed. The price is $G$ samples per prompt and one shared advantage for all tokens of a response.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.