← All posts

Model development

Post 17 of 26

Reinforcement learning for language models: policy gradients, PPO, GRPO and verifiable rewards

Posts in this series · 26

The preference post ended with an objective, maximize reward while staying close to a reference policy, and one way to approach it, DPO, which learns from fixed pairs. Reinforcement learning (RL) attacks the objective directly: the model generates responses, a reward scores them, and the model is updated to make high-reward responses more likely. Because it learns from its own current outputs, it can improve beyond the examples it was given. This post builds the method from one equation, with only the SFT and preference posts as background.

The policy gradient

Treat the model as a policy πθ\pi_\theta: given a prompt, a probability distribution over responses. We want to maximize the expected reward J(θ)=Ey∼πθ[R(y)]J(\theta) = \mathbb{E}_{y \sim \pi_\theta}[R(y)]. The difficulty is that yy is sampled, and sampling has no gradient. The policy gradient theorem gets around it:

∇θJ=Ey∼πθ[R(y) ∇θlog⁡πθ(y)].\nabla_\theta J = \mathbb{E}_{y \sim \pi_\theta}\big[R(y)\,\nabla_\theta \log \pi_\theta(y)\big].

In words: sample responses, and do SFT on each one weighted by its reward. For a language model, log⁡πθ(y)\log \pi_\theta(y) is the sum of the log-probabilities of yy‘s tokens.

Make it concrete with a policy over four answers to one question, probabilities (0.4,0.3,0.2,0.1)(0.4, 0.3, 0.2, 0.1), where only answer 3 is correct (reward 1). On the logits, ∇log⁡π(y)\nabla \log \pi(y) is ey−πe_y - \pi, so the exact gradient is 0.2×(e3−π)=(−0.08,−0.06,+0.16,−0.02)0.2 \times (e_3 - \pi) = (-0.08, -0.06, +0.16, -0.02): raise the correct answer, lower the others in proportion to their probability. Averaging the one-sample estimate R(y)(ey−π)R(y)(e_y - \pi) over 20,000 sampled answers gives the same vector to within 0.001. The method is unbiased, but each single sample is noisy.

Baselines and advantages

Any constant bb can be subtracted from the reward without changing the expected gradient, because E[∇log⁡πθ(y)]=0\mathbb{E}[\nabla \log \pi_\theta(y)] = 0. Subtracting a good baseline reduces the noise. With bb set to the average reward, 0.2, the variance of the single-sample estimate for the correct answer’s logit falls from 0.102 to 0.057, with the same mean. The quantity A(y)=R(y)−bA(y) = R(y) - b is called the advantage: how much better this response was than expected. Correct answers get positive advantage, incorrect ones negative, and the gradient pushes each accordingly.

Choosing the baseline is where the main algorithms differ.

PPO: a value model and a clipped step

Proximal Policy Optimization (PPO), used in InstructGPT, adds two ideas (Schulman et al., 2017; Ouyang et al., 2022):

The KL term from the preference post enters as a per-token penalty on the reward, rt=r−βlog⁡(πθ/πref)r_t = r - \beta \log(\pi_\theta/\pi_{\text{ref}}), keeping the policy near the reference (DeepSeekMath, Eq. 2). PPO therefore runs four models: the policy, the frozen reference, the reward model and the value model.

GRPO: the group is the baseline

Group Relative Policy Optimization (GRPO) drops the value model (DeepSeekMath, §4.1). For each prompt it samples a group of GG responses, scores them, and uses the group itself as the baseline. Each response’s advantage is its reward normalized within the group, A^i=(ri−mean(r))/std(r)\hat A_i = (r_i - \text{mean}(r))/\text{std}(r), applied to every token of that response (§4.1.2). It keeps PPO’s clipping, and adds the KL divergence to the reference directly to the loss instead of to the reward.

With a group of 4:

RewardsAdvantages
1, 0, 0, 1+1, −1, −1, +1
1, 0, 0, 0+1.73, −0.58, −0.58, −0.58
0, 0, 0, 00, 0, 0, 0
1, 1, 1, 10, 0, 0, 0

A lone success among failures gets a large advantage, and groups where every response gets the same reward contribute nothing: no difference, no signal. DeepSeekMath used 64 samples per question, a KL coefficient of 0.04 and a learning rate of 10−610^{-6}, and GRPO raised its 7B model’s MATH accuracy from 46.8% to 51.7% (§4.2, Table 5). Without the value model, GRPO saves the memory and compute of a second large network; in exchange, it needs several samples per prompt.

On the four-answer toy, 60 GRPO steps with groups of 8 raise the probability of the correct answer from 0.20 to 0.99 without a KL penalty, and to 0.94 with one (β=0.5\beta = 0.5), which keeps the policy measurably closer to where it started.

Try it: where is the signal? You train with GRPO, groups of 8, on three kinds of prompt: ones the model never solves, ones it solves half the time, and ones it solves 95% of the time. What fraction of groups give any learning signal?

Answer. A group gives signal only if its rewards are mixed: 1−p8−(1−p)81 - p^8 - (1-p)^8. Never solved: 0, every group is all failures. Half the time: 0.99. Solved 95% of the time: 0.34, most groups are all successes. Learning concentrates on prompts at the edge of the model’s ability. This is the RL counterpart of the rejection-sampling bias in the previous post, and it is why RL datasets are often filtered to prompts of intermediate difficulty.

Verifiable rewards

Everything above depends on the reward. A learned reward model is an imperfect judge that the policy can learn to exploit, the subject of the next post. Where correctness can be checked by a program, the reward can come from the check instead:

Verifiable rewards are only as good as the verifier. A checker that accepts a correct final number with wrong reasoning rewards guessing; one that checks format can be satisfied without substance.

What RL costs

Most of the time in an RL step is generation: producing GG responses per prompt, often long ones, before any gradient is computed. Training systems therefore pair the trainer with a fast inference engine and move updated weights between them, and the inference post explains what makes generation fast. Because responses come from a slightly older policy than the one being trained, the probability ratio and clipping matter more as the gap grows.

Everyday picture: a student doing practice problems. Supervised learning is copying worked solutions; RL is attempting problems and being told only “right” or “wrong”, then doing more of whatever worked. The baseline is the student’s average score: a correct answer on a problem everyone gets right teaches little, a correct answer on a hard one teaches a lot.

What to remember

Sources

Further reading