Inference-time methods spend extra tokens on every question and leave the weights alone. This module updates the weights. The reward is the verifier from Module 3: one if the boxed answer checks out, zero otherwise. No human ranking and no learned reward model.

The algorithm is Group Relative Policy Optimization (GRPO). The form implemented first is the smallest one that is still a policy gradient: sample a group of answers, center their rewards, and increase the probability of the above-average answers. Clipping and a KL penalty wait until Module 7.

From preferences to checks

RLHF trains a reward model on human comparisons, then optimizes the policy against that model’s scalar. The reward model is a second network, about as expensive as the policy, and it can be gamed because it is only a proxy.

Definition 6.1 (RLVR).

Reinforcement learning with verifiable rewards replaces the learned reward with a program. On a math item the reward of a completion is

Definition 6.1 is only available when the task has a checker. Math is the running example because the checker already exists. The same pattern applies to unit tests for code. It does not apply to “write a clearer paragraph” unless you are willing to go back to a proxy.

The training questions are the MATH items that do not overlap MATH-500, about 12,000 problems. The evaluation set stays held out.

One GRPO step

Fix a question . Sample completions from the current policy. is small in these experiments, often 4 to 8, because each completion is a full generation.

Edit the four rewards. The advantage row recenters them. The loss line uses those advantages as fixed weights on the sequence log probabilities. When every reward is the same, every advantage is zero and the update vanishes.

Advantages

Definition 6.2 (Group-relative advantage).

Let be the rewards of the group, their mean, and their unbiased sample standard deviation. The advantage of rollout is

Definition 6.2 compares a rollout to the other draws of the same question, not to a global baseline. A reward of is good only if the group was not already all correct.

For the mean is and the unbiased standard deviation is . Then

up to the in the denominator, which changes the value in the fourth digit. The constant is there so a group with does not divide by zero. In that case every numerator is zero too, so .

Lemma 6.1 (Zero group, zero update).

If , then for every , and the policy-gradient loss below is identically zero.

Lemma 6.1 is desirable. A question the model always gets right supplies no contrast. A question it always gets wrong supplies none either. Spending a gradient step on either group cannot tell the optimizer which tokens to reinforce. The practical response is to draw a fresh question, or to raise the temperature so the group is mixed.

The mean of is zero by construction, up to the shared denominator. Tracking that mean during training is a sanity check on the implementation, not a measure of progress. The informative companion is the fraction of groups whose advantage standard deviation is zero.

Sequence log probability

Each rollout also has a log probability under the policy that produced it. As in Definition 5.2, a forward pass yields a log probability per generated token. GRPO sums them:

The sum is itself. Dividing by would multiply the advantage by and quietly change the objective. Longer and shorter answers of equal reward would no longer pull equally hard. The sum does the opposite: because each is negative, a longer answer has a more negative log probability, so an incorrect long answer is pushed down over more tokens, and a correct short answer is an easier likelihood to raise. That length bias is real. Module 7 returns to it.

The prompt tokens are not in the sum. The model is not credited or blamed for the question.

The forward pass that produces \eqnref{equ:seq-logp} must build a computation graph. A log-probability helper wrapped in inference mode will give the right number and a gradient of zero.

The loss

Definition 6.3 (Sequence-level policy gradient).

With advantages treated as constants,

Definition 6.3 is the quantity PyTorch minimizes. The minus sign is there because the optimizer descends. Minimizing \eqnref{equ:pg-loss} is the same as ascending . A positive advantage increases the probability of that rollout. A negative advantage decreases it.

Detach before the multiplication. Advantages depend on rewards, and rewards depend on the generated text, but they are not a differentiable function of in this implementation. If the graph is left attached, the backward pass is either an error or a nonsense path through the sampling step. The only tensors that should require gradients are the log probabilities.

This loss is on-policy and it has no importance ratio. The completions were sampled from , and the log probabilities are computed at the same . As soon as you take several optimizer steps on a frozen group of rollouts, that stops being true. The ratio in Module 7 is the correction.

The training loop

One iteration:

  1. Sample a question from the training split.
  2. Generate completions with the current weights. Use a nonzero temperature, or Lemma 6.1 fires on most items.
  3. Grade each completion with the shared verifier. Reward is or .
  4. Compute advantages from \eqnref{equ:advantage} and sequence log probabilities from \eqnref{equ:seq-logp}.
  5. Descend on \eqnref{equ:pg-loss} for one or a few steps.
  6. Every so often, run the greedy MATH-500 evaluation from Module 3 and write the accuracy next to the average reward.

The average reward on the training groups and the accuracy on MATH-500 are different numbers. The first is the fraction of sampled rollouts that were correct, at the training temperature. The second is greedy accuracy on held-out items. Both should be logged. A rising training reward with a flat evaluation accuracy is a warning that the model is fitting the training questions, or that the training temperature and the evaluation decoder have come apart.

Save checkpoints. A later collapse is much easier to understand if you can reload the model from before it.

What this objective is

\eqnref{equ:pg-loss} is REINFORCE with a group baseline, written as a loss. It is not yet the clipped surrogate from the GRPO papers, and it is not PPO. Those additions are the next module. Starting here makes the credit assignment readable: one scalar per rollout, shared by every token in that rollout. Tokens are not individually rewarded. If a correct solution contains a wrong intermediate step that happened to cancel, every token in it is reinforced together.

Exercises: Homework 6.

Code