From REINFORCE to GRPO: three estimates of one gradient

This language model has four tokens, a b c d, and writes exactly three of them, so all 64 of its completions fit on the screen. It starts from a pretrained model πref and is rewarded for some completions. The goal is to maximize expected reward while staying close to πref: E[r] − β·KL(π‖πref). Three learners train side by side on the same budget of samples, and differ only in how they weight each sample. Click a reward cell to change the reward.

reward:
β0.25 add to every reward0
speed samples per step learning rate
show:

1. The policy: a next-token distribution at every prefix

The tiny language model as a treeThe root holds the distribution over the first token, the next row the distributions over the second token after each first token, and the third row the distributions over the third token. Below, the 64 completions with their rewards and their probabilities under the policy, the pretrained model, and the KL-regularized optimum.

2. The last batch: each completion’s reward and the weight it gets

The last batch of samplesEach sampled completion with its reward and its advantage, the weight on the gradient of its log-probability.

3. All three, trained side by side

Training curvesExpected reward, KL divergence from the pretrained model, and how well each step's gradient estimate points along the true gradient, for the three learners.
The model. A language model is a policy: at each prefix it gives a distribution over the next token, and a completion is a path through this tree. Here every prefix has its own softmax over four logits, 21 prefixes in all; a real model computes the same softmaxes with one shared network, which is what lets it generalize. Its log-probability of a completion is the sum along the path, log π(y) = log π(y1) + log π(y2|y1) + log π(y3|y1y2). The reward arrives only at the end, so each completion is treated as one action. The lower plot uses a square-root scale so that rare completions stay visible.
REINFORCE. You cannot backpropagate through sampling, but ∇Ey~π[r(y)] = Ey~π[r(y) ∇log π(y)], so sampling a batch and stepping along Σi ri ∇log π(yi) climbs the expected reward. That is supervised fine-tuning on the model’s own samples, each weighted by its reward. With rewards of 0 and 1 it is exactly “keep the samples that worked and fine-tune on them.” Every sampled token is pushed up at its prefix, by the log-softmax gradient, one-hot minus probabilities.
Why a baseline. Subtracting any b that does not depend on the sample leaves the gradient unbiased, because E[∇log π] = 0. It changes the noise. Raise add to every reward: the objective’s gradient is unchanged, but REINFORCE now pushes every sample up and relies on small differences between big pushes, and the right-hand chart shows its steps pointing in nearly random directions. The baseline learner subtracts a running average of past rewards and does not notice the shift. Weight r − b is the advantage: better than usual goes up, worse goes down.
GRPO. DeepSeek’s GRPO uses the batch itself as the baseline: sample a group of completions for the same prompt and give each the advantage (ri − mean)/std. Two consequences are visible here. A group whose rewards are all equal teaches nothing; the status line counts those steps. And dividing by the std rescales the reward by 1/std, which amplifies rare successes (try one rare completion) but changes the objective: with more a’s, GRPO overshoots the dashed target, and unchecking the box brings it back. Real GRPO also takes several steps per batch with PPO’s clipped probability ratio; with one step per batch, as here, the ratio is 1 and the clipping does nothing. The KL term’s gradient is computed exactly in this demo; real implementations estimate it from the samples. PPO, the original RLHF algorithm, replaces the group baseline with a learned value network, a second model as big as the first.
The dashed target. The objective E[r] − β·KL(π‖πref) has a closed-form maximizer, π*(y) ∝ πref(y) exp(r(y)/β): the pretrained model, reweighted toward reward. Every learner here is a noisy way to climb to it. Lower β and π* piles onto the rewarded completions, in the proportions πref already gave them; raise it and π* stays near πref. At β = 0 nothing holds the policy back, and it collapses onto whichever rewarded completion it happens to find. Inverting this formula is how DPO gets rid of the sampling.