Key takeaways
- RLHF with PPO needs four models in memory at once, and the value network is about as big as the policy and has to be trained alongside it. That's expensive.
- GRPO (Group Relative Policy Optimization) samples a group of answers for each question and computes advantages from the group's mean and standard deviation. The baseline isn't learned; it's measured, so the value network disappears. It's a great fit for verifiable rewards like math and code, and Dr. GRPO fixes two biases that come from the standard deviation and from answer length.
- DPO (Direct Preference Optimization) starts from the optimal solution under a KL constraint and writes the reward as a log probability ratio. Plug that into Bradley–Terry and the uncomputable Z cancels out.
- The DPO loss simply widens the gap in implicit reward between the good and the bad answer. It needs only the policy and the reference model: no reward model, no sampling, no reinforcement learning.
The bill for four models
At the end of last episode we counted them up: RLHF with PPO needs four models on the GPUs at the same time.
- Policy model: the language model we're training.
- Reference model: a frozen copy made before training starts, used for the KL penalty.
- Reward model: scores each answer; also frozen.
- Value network: from any point partway through an answer, predicts roughly what score we'll end up with.
The value network is usually about the same size as the policy, and it has to be trained too. A model in training needs more than its weights: it also stores gradients and optimizer states, which takes several times the memory of a frozen model.
So here's this episode's question: can we get away with fewer of these four models? We'll see two answers. GRPO simply gets rid of the value network. DPO goes further: no reward model and no RL sampling at all, leaving only the policy and the reference model.
Recap: what the value network does
In PPO, the model writes an answer and the reward comes just once, at the very end. Say it gets the question right: R=1. But PPO wants to know how good each individual token was, so at every position the value network estimates: if we keep writing from here, what score do we get on average?
The advantage is At=R−Vψ(st). Better than expected, the advantage is positive and that token's probability gets pushed up; worse than expected, it gets pushed down. PPO then multiplies the advantage by the new-to-old probability ratio rt and clips rt to stay between 1−ϵ and 1+ϵ, so no single step goes too far:
JCLIP=E[t∑min(rtAt, clip(rt,1−ϵ,1+ϵ)At)]
The trouble is V. It's a big model in its own right, trained alongside the policy. And guessing the final score from half an answer is genuinely hard; when V guesses wrong, the advantage is wrong too.
GRPO's idea is very direct. All V really gives us is an expected score to compare against. So instead of predicting it, why not answer the same question several times and simply measure it?
GRPO: answer the same question six times
Give the model a question: what is 17×24? The correct answer is 408. GRPO has the model write a group of answers to that same question, each independently. Here we take six, so the group size is G=6.
| Answer |
1 |
2 |
3 |
4 |
5 |
6 |
| Output |
408 |
418 |
398 |
408 |
428 |
388 |
| Reward |
1 |
0 |
0 |
1 |
0 |
0 |
The question has a known answer, so a short program can check it: 1 point if right, 0 if wrong.
- Average: two of six are right, so the group's average is 2/6≈0.33. That's exactly the expected score we wanted. No network guessed it; we measured it from the answers we actually got.
- Subtract the average: the average becomes the new zero. The two right answers become +0.67 and the four wrong ones −0.33.
- Divide by the standard deviation, which measures how spread out the scores are. For this group it's 61∑i(ri−0.33)2≈0.47.
That gives each answer's advantage: about +1.41 for the right ones and about −0.71 for the wrong ones. These six advantages add up to exactly zero: answers that beat the group average are encouraged, and answers below it are discouraged.
The group-relative advantage
As a formula: for a question q, sample a group of G answers {oi}i=1G and compute their rewards r={ri}i=1G. The group-relative advantage of answer i is
Ai=std(r)ri−mean(r)
Every token in an answer shares this one advantage. If the answer as a whole is right, each of its tokens gets a push upward.
Watch out: Strictly speaking this isn't a "baseline", because besides subtracting the mean it also divides by a normalization factor (the standard deviation). And it only cares about how good an answer is relative to the others: multiply every reward by 10, or add 5 to all of them, and the advantages don't change one bit.
Two extremes:
- A hard question where only one of six is right: its advantage shoots up to +2.24, while each wrong answer loses only 0.45. A rare right answer gets a big reward; a mistake almost everyone makes costs each of them only a little.
- An easy question where all six are right: every answer equals the average, every advantage is zero, and the question adds nothing to training. Same if all six are wrong.
So for GRPO, the most useful questions are the ones the model sometimes gets right and sometimes gets wrong. Only then are there differences within the group to learn from.
The GRPO objective
With the advantage in hand, GRPO's objective looks almost exactly like PPO's, with one change: the At from the value network is swapped for the group-relative advantage Ai. The probability ratio works as before. For token t of answer i, it's the probability under the new model divided by the probability under the old model that did the sampling:
ri,t=πθold(oi,t∣q,oi,<t)πθ(oi,t∣q,oi,<t)
(Don't mix up the reward ri with the ratio ri,t.) Then we average over the tokens inside each answer, average over the G answers in the group, and subtract β times the KL divergence to keep the model tethered to the reference model:
JGRPO(θ)=G1i=1∑G∣oi∣1t=1∑∣oi∣[min(ri,tAi, clip(ri,t,1−ϵ,1+ϵ)Ai)−βDKL(πθ∥πref)]
PPO folds the KL into the reward, but GRPO writes it straight into the objective. The KL is estimated token by token with a simple expression that can never come out negative:
πθπref−logπθπref−1≥0
Watch out: The notes write JGRPO-CLIP, which is just the clipped part (no KL term), with the length written as T: G1∑i=1GT1∑t=1Tmin(rtA(i),clip(rt,1−ϵ,1+ϵ)A(i)). That T1 is the video's ∣oi∣1: an average over the answer's length.
GRPO combines three ideas:
- the off-policy policy gradient, training on samples from the old policy πθold (episode 18);
- PPO's clipping (episode 19);
- group-normalized advantages: the mean and standard deviation of a group of samples stand in for the value network.
The training loop
Each round, copy the current model to serve as the old policy and use it to sample G answers for every question. A program scores them, we compute the advantages within each group, then take several gradient steps on this batch and move on to the next round. In more detail:
- Initialize the policy πθ←πθinit.
- For each outer step (
n_grpo_steps):
- sample a batch of questions Db from the dataset D;
- update the old policy πθold←πθ;
- for each question q, sample G outputs {oi}i=1G∼πθold(⋅∣q);
- score each output with R(q,oi) and compute the group-relative advantages Ai;
- for each inner training step (
n_train_steps_per_rollout_batch): update πθ by maximizing the GRPO objective.
The original notes' algorithm has one more line: keep training the reward model rφ through a replay mechanism. This episode's example scores answers with a program, so it doesn't need that.
Notice what's missing from the loop: there's no value network. The baseline isn't learned; it's measured.
Intuition: GRPO's biggest win is getting rid of the critic (the value network). PPO trains an extra network to predict the expected reward. GRPO asks why predict it at all: sample several answers to the same question, use their average as the baseline and their standard deviation to normalize, and you have an empirical baseline. It costs more compute (G completions per question) but less memory (no value network to store).
Why it suits math and code
GRPO is an especially good fit for math and code, and the reason is the reward: in these tasks you can directly check whether an answer is right. Compare the final answer to a math problem; run the unit tests for code. The reward comes from a program, so it's cheap and accurate, and very hard for the model to game. A reward model, by contrast, is itself a neural network whose scores are only an approximation, and the model might learn to please it instead of actually getting better.
These tasks also tend to involve a long chain of reasoning where you only learn at the very end whether it worked. Predicting success halfway through is really hard for a value network. GRPO doesn't even try; it just solves the problem several times.
The price is compute: every question needs G answers, and G is usually anywhere from a dozen to several dozen. In exchange, you save the memory and training of one whole big model.
GRPO first appeared in DeepSeekMath, DeepSeek's paper on a model for math. Later, in the DeepSeek-R1 paper, a model called R1-Zero was trained with nothing but simple rule-based rewards (is the answer right, and is the format followed?), and it taught itself to write longer and longer chains of reasoning.
Two small biases: Dr. GRPO
GRPO works well, but people later pointed out two small biases hidden in its formula. Dr. GRPO is the version that fixes them.
Bias 1: dividing by the standard deviation. In a group of six, the number of right answers changes the standard deviation, and with it the amplification factor 1/std(r):
| Right answers |
1 |
2 |
3 |
4 |
5 |
| 1/std |
2.68 |
2.12 |
2.00 |
2.12 |
2.68 |
Half right and half wrong: a factor of 2. Only one right, or only one wrong: a factor of 2.68. When std(r) is small (the question is too easy or too hard), dividing by it amplifies the reward, so questions that are too hard or too easy actually get more weight.
Bias 2: dividing by the answer's length. The ∣oi∣1 in the objective averages over each answer's length. Take two wrong answers, both with advantage −0.71: one 50 tokens long, the other 500. Spread over its tokens, each token of the short answer takes ten times the penalty of a token in the long one. Right answers work the same way: each token of a short answer gets pushed harder. The result:
- among correct answers, short ones get bigger gradients and are reinforced more strongly;
- among incorrect answers, long ones get smaller gradients: the penalty is spread thin, and they're barely punished.
So the model picks up a bad habit: when it can't solve something, the longer it writes, the better.
Dr. GRPO's fix is simple. Stop dividing by the standard deviation, stop dividing by each answer's own length, and divide everything by one fixed constant instead:
Ai=ri−mean(r),∣oi∣1 ⟶ fixed constant1
DPO: no reinforcement learning at all
Now let's switch direction and return to human preferences. Labelers usually don't score answers; they compare two and pick the better one. So one piece of data is a prompt x plus the winning answer yw and the losing answer yl: (x,yw,yl).
RLHF first uses these pairs to train a reward model, then uses PPO to maximize its score: two stages, with nonstop sampling along the way. DPO asks a bold question: could we skip the reward model and the reinforcement learning, and train the language model directly on the preference data?
The answer is yes. The derivation takes three steps:
- write down the optimal solution to the RLHF objective;
- flip it around to express the reward in terms of the policy;
- plug that back into the preference model (Bradley–Terry).
Intuition: RLHF works in two stages: learn a reward model, then use RL to maximize it. DPO notices that the optimal policy and the reward are tied by an explicit formula. So write the reward in terms of the policy itself and substitute it into the Bradley–Terry preference probability. The reward model "disappears", leaving a single classification loss computed directly on the policy.
Step 1: the optimal policy under a KL constraint
Write down the RLHF objective: make the expected reward as high as possible, minus β times the KL divergence between the model and the reference model:
πmax Ey∼π[R(x,y)]−βDKL(π∥πref)
If the model is allowed to be any probability distribution, this has a closed-form optimal solution: the reference model's probability, times e to the R/β, divided by a normalizing constant Z(x):
π∗(y∣x)=Z(x)1πref(y∣x)exp(β1R(x,y)),Z(x)=y∑πref(y∣x)eR(x,y)/β
A tiny example gives a feel for it. Say a prompt has only three possible answers, A, B and C:
| Answer |
πref |
Reward |
π∗ (β=1) |
| A |
0.50 |
1 |
0.36 |
| B |
0.30 |
2 |
0.59 |
| C |
0.20 |
0 |
0.05 |
Multiply each reference probability by e to the power of its reward and normalize, and you get the optimal policy. B, with the highest reward, gets lifted up, but it doesn't take all of the probability.
Remember the rubber band from last episode? β sets how tight it is. Turn β up, say to 5, and the optimal policy hugs the reference model. Turn it down, say to 0.25, and almost all the probability rushes to B.
Watch out: Z adds up the weights of every possible answer so the probabilities sum to 1. With three answers that's easy, but real answers come in astronomical numbers, so there's no way to compute Z. Keep this problem in mind.
Why the optimum looks like this
The derivation takes only a few lines. To keep things short, drop the x:
J(π)=Ey∼π[R(y)]−βDKL(π∥πref)=y∑π(y)[R(y)−βlogπref(y)π(y)]=−βy∑π(y)logπref(y)eR(y)/βπ(y)=−βy∑π(y)logZπ∗(y)π(y)=−βDKL(π∥π∗)+βlogZ
Line by line:
- Write both the expectation and the KL as sums over all answers y; the KL is just πlogπrefπ added up.
- Pull out −β. R/β can be written as logeR/β, so the two terms merge into one log, and the e term ends up in the denominator.
- That denominator, πrefeR/β, is exactly Zπ∗ by the definition of the optimal solution.
- Split logZ off. Since π's probabilities add up to 1, it's just a constant, and what's left is exactly the KL divergence between π and π∗.
As we saw in episode 5, a KL divergence is always greater than or equal to zero, and zero only when the two distributions are identical; meanwhile logZ doesn't depend on π. So to maximize the objective, the KL must be zero, which means π=π∗. That's the optimum.
Step 2: write the reward the other way round
Now turn the relationship around so the reward is written in terms of the policy. Take the log of both sides: multiplication becomes addition, and the exponent comes down.
logπ∗(y∣x)=logπref(y∣x)+βR(x,y)−logZ(x)
Rearrange and multiply by β:
R(x,y)=βlogπref(y∣x)π∗(y∣x)+βlogZ(x)
This says something really interesting: an answer's reward is hidden in how much more the optimal model likes it than the reference model does. The DPO paper puts exactly this in its subtitle: Your Language Model is Secretly a Reward Model.
The one obstacle left is still logZ, a sum over every possible answer that we can't compute. Don't worry: in the next step it disappears by itself.
Step 3: Z disappears
Go back to the Bradley–Terry model we used to train the reward model: the probability that yw beats yl is the sigmoid of the difference between their rewards. The sigmoid squashes any real number into the range between 0 and 1; the bigger the reward gap, the closer the winning probability gets to 1.
P(yw≻yl∣x)=σ(R(x,yw)−R(x,yl))
Plug in the reward from step 2:
P=σ(βlogπref(yw∣x)π∗(yw∣x)+βlogZ(x)−βlogπref(yl∣x)π∗(yl∣x)−βlogZ(x))
Both answers respond to the same prompt x, so their βlogZ(x) terms are identical. One is added and one subtracted, so they cancel exactly. Just like that, the Z we couldn't compute is gone!
Now the preference probability depends only on two probability ratios. Replace the ideal π∗ with the model we're actually training, πθ. Then, just as with the reward model, make the observed preferences as probable as possible: minimize the negative log of that probability, averaged over all the preference data. That's the DPO loss, the negative log-likelihood of the observed preferences:
LDPO(θ)=−E(x,yw,yl)∼D[logσ(βlogπref(yw∣x)πθ(yw∣x)−βlogπref(yl∣x)πθ(yl∣x))]
Look at what we used: no reward model, no sampling, no reinforcement learning. Only log probabilities from two models, the one we're training and the frozen reference model.
What the DPO loss is doing
Call β times the log probability ratio the implicit reward, written r^. The DPO loss is the negative log-sigmoid of the difference between the two implicit rewards:
L=−logσ(r^w−r^l),r^=βlogπref(y∣x)πθ(y∣x)
Write the gap as m=r^w−r^l:
- At the start of training, the model is a copy of the reference model, so both ratios are 1 and their logs are 0. The gap m is zero and the loss is log2≈0.69.
- To make the loss smaller, widen the gap: push the good answer's probability up relative to the reference model, and push the bad answer's down.
- The slope of the curve is how hard the loss pushes. If the model ranks the bad answer above the good one, the gap is negative (say m=−1.5), the curve is steep, and it pushes hard. Once the gap is big, the curve flattens and it barely pushes at all. The more wrong the model is, the harder it gets corrected.
β is usually around 0.1, and it's still RLHF's rubber band: the smaller β is, the further the model may drift from the reference model.
Watch out: DPO only cares about the gap. Sometimes the good and the bad answer's probabilities both go down, the bad one just drops further, and the loss still gets smaller.
PPO, GRPO and DPO compared
|
PPO |
GRPO |
DPO |
| Models |
policy, reference, reward, value |
policy, reference, reward (often a program) |
policy, reference |
| Data |
sampled online during training |
G answers sampled online per question |
preference pairs collected in advance |
| Signal |
advantage from a value network |
comparison within the group |
gap in implicit rewards |
- PPO: the most general, and it can explore online; but four models make it heavy and hard to tune.
- GRPO: no value network, so it saves memory, and it shines on tasks with a known correct answer (verifiable rewards); but generating G answers per question is expensive.
- DPO: the simplest and most stable, and training it feels just like supervised learning; but it can only learn from a fixed dataset, can't explore online, and depends heavily on the quality of the preference data.
In practice, real post-training pipelines often combine these methods, using each where it's strongest.
Recap
GRPO replaces guessing with measuring: sample a group of answers per question, use Ai=std(r)ri−mean(r) as the advantage, plug it into PPO's clipped objective, and write the KL straight into the objective. The value network is gone. It fits rewards you can check directly, like math and code, and Dr. GRPO removes the two biases from dividing by the standard deviation (overweighting too-easy and too-hard questions) and by length (writing long when it can't get the answer). DPO goes further: using the closed-form optimum of the KL-regularized objective, π∗∝πrefeR/β, it writes the reward as βlogπrefπ∗+βlogZ; substituted into Bradley–Terry, Z cancels, leaving one classification loss computed directly on the policy.
That completes post-training's chain of patches: REINFORCE (high variance) → subtract a baseline (the advantage) → off-policy (reuse old samples) → PPO (clipping for stable multi-step updates) → RLHF (reward model plus KL penalty) → GRPO (a group-based empirical baseline replaces the critic) → DPO (skip RL and optimize preferences directly).