21 / 27 · Training Agents
Reinforcement learning for language models
How policy gradients train a language model: the KL-regularized objective and its closed-form optimum, PPO with a value head and per-token penalties, GRPO's group-relative advantages without a critic, rewards checked by programs, and why optimizing a learned reward too hard backfires.
Policy gradients and actor-critic · The language model as a policy · Instruction tuning and alignment · KL divergence
On this page 8
The agents of the previous section, up to the teams of Multi-agent systems, were built around a model whose weights never changed: everything they learned lived in prompts, tools, memory and structure. This section changes the weights. The tool is the policy gradient of the second section, and the policy is the language model of The language model as a policy, read at the token level: the state is the prompt and the tokens written so far, the action is the next token, and a reward arrives when the response is complete. This note derives the objective that RLHF optimizes and its exact optimum, shows how PPO is adapted to it with a value head and a penalty at every token, replaces the critic by a group of samples as GRPO does, swaps the learned reward for a program that checks the answer, and measures what happens when a learned reward is optimized too hard. The NLP course’s instruction-tuning note introduced reward models, RLHF and DPO; this note does not repeat them and works out what that note took as given.
Notation. A prompt is drawn from a dataset ; a response is a sequence of tokens, and is the state before token . The policy is the language model, ; is a frozen copy of the model training starts from, usually the SFT model. A reward scores a whole response: a learned reward model or a verifier . is the KL coefficient and . As in Policy gradients and actor-critic, is the policy that sampled the data and an advantage estimate; the probability ratio, written there, is here, because is the reward. is a value head, the number of responses sampled per prompt.
Baseline: maximize the reward alone#
The obvious objective is the expected reward, . It is linear in , so its maximum puts all probability on the best-scored response. Worked example. A prompt has four candidate answers. The reference model gives them probabilities , and a reward model scores them . Answer 4 is the kind of response reward models are known to overrate: confident, long, agreeable, and wrong in a way the reward model did not learn to see. The expected reward of the reference is . The optimum of the plain objective is answer 4 with probability 1.
Three things go wrong at once. The reward model is accurate only near the responses it was trained on (instruction-tuning note), and the optimum lands on the response it knows least about. The policy loses all diversity, which the methods of this section need, because their signal comes from differences between samples. And nothing in the objective asks the model to keep what pretraining and instruction tuning gave it: fluency, knowledge, the format of an answer. The policy gradient of this objective, followed long enough, finds the same corner.
The KL-regularized objective and its optimum#
The objective of RLHF (Ziegler et al., 2019; Ouyang et al., 2022) and of most later methods is
The prompts are independent, so it is enough to maximize the bracket for one ; we drop from the notation. Step 1: collect both terms under one sum. Write the expected reward and the KL divergence as sums over responses, and take out :
Step 2: normalize the denominator. is positive but does not sum to 1. Divide it by and call the result ; then , and
Step 3: use that a KL divergence is non-negative. does not depend on , and with equality only at (Kullback–Leibler divergence). The objective is therefore largest at , where it equals :
The derivation needs and a bounded reward, so that is finite; responses with keep probability 0, since any mass on them would make the KL divergence infinite. The same result is old in reinforcement learning: relative entropy policy search derives this exponential reweighting of a reference policy (Peters, Mülling and Altun, 2010), and Korbak, Perez and Buckley (2022) read it as Bayesian inference, with as the prior and as the likelihood of “being a good response”. In those terms the bracket divided by is the evidence lower bound of variational inference, with as the approximate posterior and as the log evidence, and Step 2 is the decomposition of the log evidence into the bound plus the reverse KL divergence to the posterior .
Worked example. The four answers of the baseline, for three values of :
| 2.0 | (0.409, 0.315, 0.202, 0.074) | 0.449 | 0.023 | 0.4036 |
| 0.5 | (0.168, 0.274, 0.373, 0.185) | 0.732 | 0.374 | 0.5451 |
| 0.1 | (0.000, 0.004, 0.288, 0.708) | 1.140 | 2.048 | 0.9349 |
Large keeps the reference almost unchanged; small approaches the plain maximization of the baseline. The last column equals , as the boxed formula says, and no other distribution over the four answers scores higher (the Code section checks 10,000 random ones). At answer 3 is the most likely, although answer 4 scores higher: its reference probability costs more KL than its extra reward is worth.
Where DPO comes from. Solve the boxed formula for the reward: . In the Bradley–Terry likelihood of a preference pair, only the difference appears, and cancels because both responses answer the same prompt. What remains is a loss on the policy alone, the DPO loss of the instruction-tuning note (Rafailov et al., 2023). DPO and the methods below aim at the same ; DPO fits it offline on a fixed set of pairs, while PPO and GRPO approach it online with samples from the current policy.
Why not compute directly. sums over every response, of them for responses of tokens. What can be done is to sample responses, score them and move towards , which is reinforcement learning. One identity makes the KL term easy to handle during sampling. The log-ratio of a response is the sum of the log-ratios of its tokens,
because both probabilities are products over the same tokens. Its expectation under is the KL divergence, so the KL penalty can be charged token by token.
PPO for language models: rewards at every token#
PPO needs rewards at steps and a critic. For a language model both are built from the pieces above (Ziegler et al., 2019; Stiennon et al., 2020; Ouyang et al., 2022). The episode is one response; the action at step is token ; the transitions are deterministic. The reward of token is the KL penalty of that token, plus the reward model’s score at the last token:
This is equation (2) of DeepSeekMath (Shao et al., 2024), which calls it the standard approach. Summed over the response, with the discount that is usual here, , and its expectation under is exactly the bracket of . PPO on these rewards therefore maximizes the KL-regularized objective.
The critic is a value head : a linear layer on top of a transformer, often initialized from the reward model, that reads the prefix and predicts the return still to come. Its TD errors feed generalized advantage estimation, and the advantages feed the clipped objective, summed over tokens.
Worked example. A response of four tokens has log-ratios against the reference; ; the reward model scores it ; the value head predicts before each token and 0 after the end; , .
| token | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| reward | ||||
| TD error | ||||
| advantage |
The reward model said one number about the whole response. The four different advantages come from the value head, which decides how much of that number each token earned, and from the small per-token penalties. This is the step that is hard to get right for language models: only the last token carries reward, and the value head has to learn the worth of every prefix from that signal alone (Shao et al., 2024, section 4.1.1). It is also expensive. The value head is usually a model of the size of the policy, so PPO for RLHF keeps four large models in memory: policy, reference, reward model and critic.
GRPO: the group as the baseline#
Group relative policy optimization (Shao et al., 2024) removes the critic. For each prompt it samples a group of responses from , scores them, and uses the group itself as the baseline. With outcome rewards , every token of response receives the same advantage, its reward standardized within the group:
The KL penalty moves from the reward into the loss, which keeps the advantages free of it. The objective, maximized by gradient ascent, is
with the token ratio and the KL estimate
an estimator of at that token that is unbiased and never negative (Schulman, 2020): writing , its mean under is , and for every . For process rewards, one score per reasoning step, DeepSeekMath standardizes the step scores the same way and gives each token the sum of the standardized scores of the steps that end at or after it.
Worked example. Six responses to a maths question, checked against the reference answer: . The mean is and the standard deviation , so the two correct responses get at every token and the four wrong ones . The advantages sum to zero: the group pushes up what beat its own average and pushes down the rest. A group in which all six responses are right, or all six wrong, has standard deviation 0; its advantages are set to 0, and that prompt contributes nothing to the step except through the KL term.
PPO and GRPO for a language model
- Sample. The policy writes responses to the prompt: one for PPO, a group of for GRPO.
- Score. PPO’s reward model scores the response once, and the reference model gives the log-ratio of every token; GRPO scores each response of the group, with a reward model or a verifier.
- Per-token rewards. PPO turns these into rewards at every token: times the log-ratio, plus the score at the end.
- Value head. PPO’s critic predicts the value of every prefix.
- Advantages. PPO computes GAE along the response. GRPO standardizes the rewards within the group and gives every token of a response the same advantage.
- Update. Both take clipped policy-gradient steps; GRPO adds the KL estimate to the loss instead of the reward.
DeepSeekMath trained DeepSeekMath-Instruct 7B with GRPO on about 144,000 chain-of-thought questions from GSM8K and MATH, with rewards from a learned reward model, a learning rate of , , responses per question, responses of at most 1,024 tokens, batches of 1,024, and one policy update per batch of samples, so that and the clipping never acted (Shao et al., 2024, section 4.2). With chain-of-thought prompting and one answer per question, accuracy rose from 82.9 % to 88.2 % on GSM8K and from 46.8 % to 51.7 % on MATH (table 5). The authors also measured Maj@K, the accuracy of a majority vote over samples, and Pass@K, the share of questions solved by at least one of samples, at temperature 0.7: RL raised Maj@K but not Pass@K (figure 7). The training made correct answers more likely among those the model could already produce; it did not make new questions solvable. The same paper writes SFT, rejection-sampling fine-tuning, DPO, PPO and GRPO in one form, a sum of over tokens weighted by a gradient coefficient: the methods differ in where the data comes from, what scores it and how the score becomes that weight.
What the group baseline does in expectation#
Optional depth: an exact formula for GRPO’s expected update with right-or-wrong rewards, and the two biases that follow from it. The rest of the note does not depend on it.
Take one prompt, a reward , and let be the probability that a sampled response is right; is the gradient with respect to . Consider one on-policy step, so , and leave out the KL term and the factor. The step is . Let be the number of correct responses in the group and .
- Condition on . Given , the correct responses are independent draws from restricted to the correct set , and the wrong ones from its complement.
- The mean score of a correct response. , and in the same way for a wrong one.
- The advantages. The group’s mean is and its standard deviation , so a correct response gets and a wrong one .
- Add up. With correct and wrong responses,
- Average over . Groups with or give 0, as the worked example said.
In expectation, GRPO climbs the gradient of the success probability, scaled by a factor that depends on how hard the prompt is. Without the division by the standard deviation, the factor is , since for a binomial share: the group mean is an ordinary baseline, slightly biased because each response is part of its own baseline. The leave-one-out mean of RLOO removes even that (Ahmadian et al., 2024). With the division, for large groups and : prompts that the model nearly always solves, or nearly never, get the largest weight. With , at (the limit is 2), at (limit 3.333) and at (limit 10.05): small groups rarely contain a success on a hard prompt, so the extra weight seldom arrives. Liu et al. (2025) call this a difficulty bias of the per-question standard deviation, and point to a second one in the factor : a wrong response spreads its negative advantage over more tokens when it is longer, so each of its tokens is pushed down less, and long wrong answers are penalized least. Their Dr. GRPO drops both normalizations.
The KL estimate has a subtlety of its own. Its value is unbiased, but GRPO differentiates it as a loss on sampled tokens. Holding the prefix fixed, the gradient of with is , and its mean under is
using . The term pulls the policy towards the reference, but along the gradient of , the divergence with its arguments swapped, and it ignores how the change of earlier tokens moves the distribution of later prefixes (Tang and Munos, 2025). The Code section checks against a simulation and uses this expected KL gradient for the curves below.
Rewards that a program checks#
A learned reward model is the weakest part of RLHF: it can be wrong, and optimization finds where. For tasks with a checkable answer, the reward can instead be computed by a program. Tülu 3 named this reinforcement learning with verifiable rewards (RLVR) and trained with it after DPO, using PPO with the KL-regularized objective and a reward of 10 for a verified answer (Lambert et al., 2024, section 6). DeepSeek-R1-Zero used rule-based rewards alone, an accuracy reward and a format reward, because its authors found that neural reward models were hacked in large-scale RL (DeepSeek-AI, 2025, section 2.2.2). The protocols are simple:
- Maths. The prompt asks for the final answer in a fixed place, for example
\boxed{...}. The verifier takes the last boxed expression of the response, normalizes it (spaces,\frac{a}{b}againsta/b, a leadingx =), and compares it with the reference answer, numerically within a tolerance or symbolically. The reward is 1 if they match and 0 otherwise. - Code. The program runs against unit tests in a sandbox with limits on time and memory (Coding agents). The reward is 1 if every test passes, sometimes the fraction of tests passed, and 0 if the program does not compile or times out.
- Format and constraints. A response must put its reasoning between tags, or follow an instruction that a rule can check, such as “answer in exactly three bullet points”.
Worked example. The question is the positive root of . The response ”… so the answer is ” gets 1. The response ”… ” gets 1 if the normalizer strips x = and 0 otherwise, a false negative that adds noise to the reward. The response ”… ” gets 0. A false negative only weakens the signal. A false positive is worse, because it is optimized: if the normalizer accepted any boxed expression containing a 3, the policy would learn to write expressions full of threes.
The Code section shows this on a toy. A verifier that checks only the last token of a three-token answer lets 25 of the 125 possible responses pass. After the same training as with the exact verifier, the lenient verifier reports a pass rate of 0.959, while the exact accuracy of the same policy is 0.606, against 0.839 for the policy trained with the exact verifier. The agentic version of this failure, a coding agent that edits the tests instead of the code, is the subject of Training agents with reinforcement learning.
GRPO on a toy, with its KL#
The Code section trains a tiny policy over token sequences. There are 8 questions; each is answered by 3 tokens from a vocabulary of 5, and a verifier accepts only the right answer. The policy is a table of logits for every prefix, the tabular version of a language model, and it starts at a reference policy that finds some answers much likelier than others: the reference solves the 8 questions with probabilities from 0.536 down to 0.0019, 0.191 on average. GRPO samples responses per question, takes one step per batch with a learning rate of 0.5, and runs for 300 iterations at three values of .
GRPO on a toy: accuracy and KL
The curves are the expected trajectories, computed with the formula of the previous section instead of sampling; the sampled runs of the Code section follow them closely (0.839, 0.795 and 0.414 at the end, against 0.842, 0.795 and 0.417). Three things are visible:
- Reward is bought with KL. Without a penalty, mean accuracy rises from 0.191 to 0.842 while the policy moves 1.81 nats from the reference. With it reaches 0.795 at 1.55 nats; with it stops at 0.417 and 0.22 nats. The right panel is the trade-off itself, accuracy against KL, the quantity that the KL-regularized objective prices.
- Easy questions are learned first. Without a penalty, the six easiest questions end between 0.968 and 0.980. The seventh, which the reference solves with probability 0.005, climbs only in the last third of the run, to 0.874, and the mean accuracy steps up when it does.
- No success, no signal. The hardest question starts at . A group of 8 contains at least one success, and so a non-zero advantage, with probability . After 300 iterations its accuracy is 0.007 without a penalty. Reinforcement learning with outcome rewards can only reinforce what the policy already does sometimes, which is why it is run after pretraining and SFT, and why the choice of training questions, hard but not hopeless, matters as much as the algorithm.
Overoptimization: when the proxy keeps rising#
A learned reward model is a proxy for what people want. Optimizing a proxy too hard makes the true objective worse, an instance of Goodhart’s law. Gao, Schulman and Hilton (2023) measured this without human labellers: a 6 B-parameter reward model from InstructGPT played the role of the “gold” reward, labelled comparisons, and proxy reward models from 3 M to 3 B parameters were trained on those labels. A policy, initialized from an SFT model, was optimized against a proxy by best-of- sampling or by PPO, and its gold score was tracked against . The gold score first rose, then fell, while the proxy kept rising, and the curves fitted two forms:
with coefficients that changed smoothly with the size of the proxy model: larger proxies peak later and higher. Measured by KL, RL optimized and overoptimized more slowly than best-of-. In their PPO runs, a KL penalty raised the proxy score reached at a given KL but did not improve the gold score at a given KL; the authors note that this may depend on hyperparameters.
The KL of best-of-. Best-of- draws responses and keeps the one with the highest proxy score. If the scores have a continuous distribution with CDF under , the kept response has density times that of : the chosen one has score and the other score less. Its KL divergence from is the mean log of that ratio, , where is the kept score. is distributed as the largest of uniform variables, with density on , so , and
the formula Gao et al. use. Best-of-16 is nats from the policy it samples from; best-of-64, .
On a toy, exactly. Let 98 % of responses have a gold quality and a proxy score , a good but noisy reward model. The other 2 % are exploits: their gold quality is , and their proxy scores are spread as , because the proxy cannot tell them apart from good responses. With a mean proxy score of 0 they rarely win among a few samples; among many, the widest-spread scores win the top place. The Code section computes the gold and proxy scores of best-of- exactly, by integrating over the kept score:
Overoptimization: best-of-n against a proxy reward
The gold score rises from to a peak of near , where the KL is 3.57 nats, and falls to at , while the proxy rises the whole time, to . Nothing is wrong with the reward model on typical responses. Optimization reaches the rare responses on which it is wrong, and the harder it optimizes, the more of what it selects comes from there.
The practical defences follow: limit the optimization by or by early stopping, chosen on an evaluation that does not use the proxy; retrain the reward model on samples of the current policy, as iterative GRPO does, with 10 % of old data replayed (Shao et al., 2024, section 4.1.4); and replace the learned reward by a verifier wherever an answer can be checked.
What changed#
| SFT | RLHF with PPO | GRPO with verifiable rewards | |
|---|---|---|---|
| signal | demonstrations | a learned reward model | a program that checks the answer |
| data | fixed | sampled from the current policy | sampled, responses per prompt |
| baseline | none | a value head, as large as the policy | the group’s mean reward |
| credit per token | every target token alike | GAE along the response | the same advantage for every token |
| KL to the reference | implicit, by starting there | a penalty in every token’s reward | an estimate in the loss |
| models in memory | 1 | 4 | 2 (or 3 with a reward model) |
| what limits it | the demonstrations | reward hacking of | tasks must be checkable |
Limitations#
- The reward is the objective. Whatever the reward model or the verifier gets wrong, the policy learns. A learned reward is overoptimized; a verifier is exploited through its false positives.
- Outcome rewards are sparse. One number per response, however long, and none at all from a group that is all right or all wrong; hard prompts teach nothing until the policy succeeds on them by chance.
- RL sharpens more than it expands. In DeepSeekMath, RL improved majority voting but not the share of questions solved by some sample; later studies make the same point for RLVR at large numbers of samples, discussed in the next note.
- Many choices are heuristics. The per-question standard deviation, the factor and the KL estimator each bias the update in measurable ways.
- The KL coefficient is a guess. Neither the theory nor the measurements give a ; it is set by watching an evaluation that the optimization does not see.
- Not every task has a checker. Writing, advice and most conversations still need learned rewards or preferences, with their failure modes.
What you should remember#
- RLHF maximizes . Its optimum is , worth : a tilt of the reference, derived by rewriting the objective as . Solving it for and substituting into Bradley–Terry gives DPO.
- The sequence log-ratio is a sum of token log-ratios, so PPO charges at every token and adds the reward model’s score at the last; a value head turns this into per-token advantages with GAE.
- GRPO samples responses per prompt, standardizes their rewards within the group and gives every token that advantage; the KL estimate goes into the loss. No critic is needed.
- In expectation, GRPO with right-or-wrong rewards follows ; the standard deviation reweights prompts by difficulty, and groups with no success give no signal.
- Verifiable rewards replace a learned reward by a check of the answer: an extracted maths answer, unit tests, a format rule. Their false positives are optimized like any other reward.
- Optimizing a learned proxy raises the true score and then lowers it; best-of- moves nats from the policy. Limit the optimization with an evaluation the proxy does not see.
Code#
Seven parts in NumPy: the KL-regularized optimum checked against random policies; PPO’s per-token rewards and GAE on the worked example; GRPO’s advantages and the factor checked by simulation; GRPO on a tabular policy over token sequences with a verifier and the KL estimate as a loss, at three values of ; the same training in expectation, which gives the curves of the figure; a lenient verifier; and the exact overoptimization curve of best-of-. With a real model, the table of logits becomes a transformer, the verifier parses generated text, and every sum over the 125 responses becomes an average over samples; the advantages, the loss and the KL estimate are computed as written. Seeds are fixed; exact numbers can differ slightly across platforms and library versions. The script runs in about twelve seconds.
import numpy as np
from itertools import product
from math import comb, erf, log, sqrt, pi as PI
# 1. the KL-regularized optimum: pi* = pi_ref exp(r / beta) / Z, worth beta log Z
pi_ref = np.array([0.50, 0.30, 0.15, 0.05])
r = np.array([0.0, 0.5, 1.0, 1.2]) # answer 4 is the one the reward model overrates
def J(pi, beta): return pi @ r - beta * (pi @ np.log(pi / pi_ref))
others = np.random.default_rng(0).dirichlet(np.ones(4), 10_000)
for beta in (2.0, 0.5, 0.1):
w = pi_ref * np.exp(r / beta); star = w / w.sum()
print(beta, star.round(3), round(J(star, beta), 4), round(beta * log(w.sum()), 4),
all(J(q, beta) < J(star, beta) for q in others))
# 2.0 [0.409 0.315 0.202 0.074] 0.4036 0.4036 True
# 0.5 [0.168 0.274 0.373 0.185] 0.5451 0.5451 True
# 0.1 [0. 0.004 0.288 0.708] 0.9349 0.9349 True
# 2. PPO's token rewards: -beta * log-ratio at every token, plus the reward model's score at the last one; then GAE
log_ratio = np.array([0.2, -0.1, 0.5, 0.0]); beta, score = 0.1, 1.0
rewards = -beta * log_ratio; rewards[-1] += score
V = np.array([0.6, 0.5, 0.7, 0.9, 0.0]) # the value head at each prefix; 0 after the end
delta = rewards + V[1:] - V[:-1] # gamma = 1
adv, g = np.zeros(4), 0.0
for t in (3, 2, 1, 0): g = delta[t] + 0.95 * g; adv[t] = g # lambda = 0.95
print(rewards.round(3), delta.round(3), adv.round(4))
# [-0.02 0.01 -0.05 1. ] [-0.12 0.21 0.15 0.1 ] [0.3006 0.4428 0.245 0.1 ]
# 3. GRPO's group-relative advantages, and what they do in expectation for 0/1 rewards
def group_adv(rw):
s = rw.std() # the population standard deviation of the group
return np.zeros_like(rw) if s == 0 else (rw - rw.mean()) / s
print(group_adv(np.array([1., 0, 0, 1, 0, 0])).round(3), group_adv(np.zeros(6)))
# [ 1.414 -0.707 -0.707 1.414 -0.707 -0.707] [0. 0. 0. 0. 0. 0.]
def c_G(p, G): # E[(1/G) sum_i A_i grad log pi(y_i)] = c_G(p) grad p
return sum(comb(G, k) * p**k * (1 - p)**(G - k) * sqrt(k / G * (1 - k / G)) for k in range(1, G)) / (p * (1 - p))
rng = np.random.default_rng(1)
for p in (0.5, 0.1, 0.01):
# a two-outcome softmax: the score of a correct sample is (1 - p), of a wrong one -p, and dp/dlogit = p (1 - p)
est = np.mean([group_adv(y) @ (y - p) / 8 for y in (1.0 * (rng.random((200_000, 8)) < p))])
print(p, round(c_G(p, 8), 3), round(est / (p * (1 - p)), 3), round(1 / sqrt(p * (1 - p)), 3))
# 0.5 1.856 1.856 2.0
# 0.1 2.328 2.327 3.333
# 0.01 2.609 2.616 10.05
# 4. a tiny policy over token sequences: 8 questions, each answered by 3 tokens from a vocabulary of 5;
# the verifier accepts exactly the right answer; the reference policy finds some answers far likelier than others
VOC, L, NQ, G = 5, 3, 8, 8
NODES = [h for l in range(L) for h in product(range(VOC), repeat=l)] # every prefix is a state of the token MDP
NI = {h: i for i, h in enumerate(NODES)}
SEQS = list(product(range(VOC), repeat=L))
ANS = [((3 * q + 1) % VOC, (q + 2) % VOC, (2 * q + 3) % VOC) for q in range(NQ)]
BONUS = [3.0, 2.5, 2.0, 1.6, 1.2, 0.8, 0.4, 0.0] # how strongly the reference prefers the answer
def softmax(z):
e = np.exp(z - z.max(-1, keepdims=True)); return e / e.sum(-1, keepdims=True)
def reference(q):
z = np.array([[0.8 * np.sin(1.3 * i + 2.1 * v + 0.7 * q) for v in range(VOC)] for i in range(len(NODES))])
for t in range(L): z[NI[ANS[q][:t]], ANS[q][t]] += BONUS[q]
return z
REF = [reference(q) for q in range(NQ)]
def seq_prob(pi, y): return np.prod([pi[NI[y[:t]], y[t]] for t in range(L)])
def stats(z, q): # exact accuracy and exact KL(pi || pi_ref) over all 125 responses
pi, pr = softmax(z), softmax(REF[q])
P = np.array([seq_prob(pi, y) for y in SEQS]); R = np.array([seq_prob(pr, y) for y in SEQS])
return float(P[SEQS.index(ANS[q])]), float(P @ np.log(P / R))
print([round(stats(REF[q], q)[0], 4) for q in range(NQ)])
# [0.5355, 0.5112, 0.221, 0.1531, 0.081, 0.0215, 0.0051, 0.0019]
def sample(pi, rng):
y = ()
for t in range(L): y += (int(rng.choice(VOC, p=pi[NI[y]])),)
return y
def grpo(beta, verifier, iters=300, lr=0.5, seed=0):
rng, Z, log = np.random.default_rng(seed), [r.copy() for r in REF], []
for it in range(iters + 1):
if it % 50 == 0:
s = [stats(Z[q], q) for q in range(NQ)]
log.append((it, round(float(np.mean([a for a, _ in s])), 3), round(float(np.mean([k for _, k in s])), 2)))
for q in range(NQ):
pi, pr = softmax(Z[q]), softmax(REF[q])
ys = [sample(pi, rng) for _ in range(G)]
A = group_adv(np.array([1.0 * verifier(y, q) for y in ys])) # the verifier, then the group baseline
grad = np.zeros_like(Z[q])
for y, a in zip(ys, A):
for t in range(L):
i = NI[y[:t]]
score = -pi[i].copy(); score[y[t]] += 1 # d log pi(y_t | prefix) / d logits
rho = pr[i, y[t]] / pi[i, y[t]] # k3 = rho - log rho - 1, used as a loss
grad[i] += (a - beta * (1 - rho)) * score / (G * L) # 1/G over the group, 1/|o| over tokens
Z[q] += lr * grad # one step per batch, so the ratio is 1
return log, Z
exact = lambda y, q: y == ANS[q]
for beta in (0.0, 0.1, 1.0):
print(beta, grpo(beta, exact)[0])
# 0.0 [(0, 0.191, 0.0), (50, 0.471, 0.35), (100, 0.602, 0.73), (150, 0.692, 1.07), (200, 0.728, 1.24), (250, 0.797, 1.56), (300, 0.839, 1.79)]
# 0.1 [(0, 0.191, 0.0), (50, 0.456, 0.32), (100, 0.577, 0.64), (150, 0.661, 0.94), (200, 0.696, 1.08), (250, 0.741, 1.28), (300, 0.795, 1.55)]
# 1.0 [(0, 0.191, 0.0), (50, 0.348, 0.11), (100, 0.388, 0.17), (150, 0.396, 0.18), (200, 0.41, 0.21), (250, 0.413, 0.22), (300, 0.414, 0.22)]
# (iteration, mean accuracy over the 8 questions, mean KL to the reference in nats)
# 5. the same training in expectation, without sampling: the curves of the figure
def visits(pi): # probability that a response passes through each prefix
return np.array([np.prod([pi[NI[h[:t]], h[t]] for t in range(len(h))]) for h in NODES])
def grpo_expected(beta, iters=300, lr=0.5):
Z, log = [r.copy() for r in REF], []
for it in range(iters + 1):
s = [stats(Z[q], q) for q in range(NQ)]
log.append((float(np.mean([a for a, _ in s])), float(np.mean([k for _, k in s])), [a for a, _ in s]))
for q in range(NQ):
pi, pr, p = softmax(Z[q]), softmax(REF[q]), s[q][0]
grad = -beta / L * visits(pi)[:, None] * (pi - pr) # the expected gradient of the k3 term
for t in range(L): # c_G(p) grad p / L, on the answer's path
i = NI[ANS[q][:t]]; e = np.zeros(VOC); e[ANS[q][t]] = 1
grad[i] += c_G(p, G) * p * (e - pi[i]) / L
Z[q] += lr * grad
return log
for beta in (0.0, 0.1, 1.0):
lg = grpo_expected(beta)
print(beta, [(it, round(lg[it][0], 3), round(lg[it][1], 2)) for it in (0, 100, 200, 300)], np.round(lg[-1][2], 3))
# 0.0 [(0, 0.191, 0.0), (100, 0.641, 0.87), (200, 0.731, 1.25), (300, 0.842, 1.81)] [0.98 0.979 0.978 0.977 0.975 0.968 0.874 0.007]
# 0.1 [(0, 0.191, 0.0), (100, 0.61, 0.75), (200, 0.699, 1.1), (300, 0.795, 1.55)] [0.967 0.965 0.946 0.937 0.925 0.9 0.716 0.005]
# 1.0 [(0, 0.191, 0.0), (100, 0.392, 0.17), (200, 0.413, 0.21), (300, 0.417, 0.22)] [0.82 0.802 0.575 0.493 0.386 0.231 0.027 0.003]
# 6. a lenient verifier that checks only the last token: 25 of the 125 responses pass
lenient = lambda y, q: y[-1] == ANS[q][-1]
_, Z = grpo(0.0, lenient)
pis = [softmax(z) for z in Z]
print(round(np.mean([sum(seq_prob(pis[q], y) for y in SEQS if lenient(y, q)) for q in range(NQ)]), 3),
round(np.mean([seq_prob(pis[q], ANS[q]) for q in range(NQ)]), 3))
# 0.959 0.606 (what the lenient verifier reports; the accuracy an exact check would report)
# 7. overoptimization under best-of-n, computed exactly: 98% of responses have gold quality g ~ N(0, 1) and a proxy
# score g + N(0, 0.5^2); 2% are exploits with gold -1 and a proxy score ~ N(0, 2^2) that the proxy cannot tell apart
Phi = np.vectorize(lambda x: 0.5 * (1 + erf(x / sqrt(2))))
def normal(x, m, s): return np.exp(-0.5 * ((x - m) / s) ** 2) / (s * sqrt(2 * PI))
s = np.linspace(-10, 14, 24001); ds = s[1] - s[0]
h, sn = 0.02, sqrt(1 + 0.5 ** 2)
fN, fE = normal(s, 0, sn), normal(s, 0, 2.0)
F = (1 - h) * Phi(s / sn) + h * Phi(s / 2.0) # the CDF of the proxy score of one response
def best_of(n): # the selected score has density n f(s) F(s)^(n-1)
w = n * F ** (n - 1) * ds
gold = ((1 - h) * fN * s / sn**2 - h * fE) @ w # E[g | proxy s] = s / 1.25 for an honest response
proxy = ((1 - h) * fN + h * fE) * s @ w
return log(n) - (n - 1) / n, gold, proxy # KL(best-of-n || pi) = log n - (n - 1)/n
for n in (1, 4, 16, 64, 96, 256, 1024, 4096, 16384):
kl, gold, proxy = best_of(n)
print(f'n {n:5d} KL {kl:4.2f} gold {gold:6.3f} proxy {proxy:5.3f}')
# n 1 KL 0.00 gold -0.020 proxy 0.000
# n 4 KL 0.64 gold 0.869 proxy 1.172
# n 16 KL 1.84 gold 1.441 proxy 2.027
# n 64 KL 3.17 gold 1.730 proxy 2.736
# n 96 KL 3.57 gold 1.747 proxy 2.932
# n 256 KL 4.55 gold 1.619 proxy 3.406
# n 1024 KL 5.93 gold 0.930 proxy 4.128
# n 4096 KL 7.32 gold -0.149 proxy 4.951
# n 16384 KL 8.70 gold -0.835 proxy 5.820
Next#
Everything here trains a model to produce better single responses: a maths answer, a program, a reply. The most visible result of this machinery came when the response was allowed to grow. Trained with verifiable rewards on hard problems, models learned to write chains of thought thousands of tokens long, to check and revise themselves inside them, and to get better the longer they think. The next note follows that line, from OpenAI’s o1 and DeepSeek-R1 to the ways of spending computation at inference time: best-of-, voting, sequential revision, process reward models and their compute-optimal mix.
References#
- D. M. Ziegler, N. Stiennon, J. Wu, T. B. Brown, A. Radford, D. Amodei, P. Christiano, G. Irving. Fine-tuning language models from human preferences. arXiv, 2019. The KL penalty in the reward.
- N. Stiennon et al. Learning to summarize from human feedback. NeurIPS, 2020.
- L. Ouyang et al. Training language models to follow instructions with human feedback. NeurIPS, 2022.
- J. Schulman, F. Wolski, P. Dhariwal, A. Radford, O. Klimov. Proximal policy optimization algorithms. arXiv, 2017.
- J. Schulman, P. Moritz, S. Levine, M. Jordan, P. Abbeel. High-dimensional continuous control using generalized advantage estimation. ICLR, 2016.
- J. Peters, K. Mülling, Y. Altun. Relative entropy policy search. AAAI, 2010.
- T. Korbak, E. Perez, C. L. Buckley. RL with KL penalties is better viewed as Bayesian inference. Findings of EMNLP, 2022.
- R. Rafailov, A. Sharma, E. Mitchell, S. Ermon, C. D. Manning, C. Finn. Direct preference optimization: your language model is secretly a reward model. NeurIPS, 2023.
- Z. Shao et al. DeepSeekMath: pushing the limits of mathematical reasoning in open language models. arXiv, February 2024. Section 4.1 (PPO’s per-token KL, equation 2; GRPO, equations 3 and 4; outcome and process supervision; iterative RL), section 4.2 and table 5 (setup and results), section 5.2 (the unified paradigm; Maj@K and Pass@K, figure 7).
- J. Schulman. Approximating KL divergence. Blog post, 2020. The estimator .
- A. Ahmadian et al. Back to basics: revisiting REINFORCE style optimization for learning from human feedback in LLMs. ACL, 2024. RLOO, the leave-one-out baseline.
- Z. Liu, C. Chen, W. Li, P. Qi, T. Pang, C. Du, W. S. Lee, M. Lin. Understanding R1-Zero-like training: a critical perspective. COLM, 2025. The length and difficulty biases of GRPO; Dr. GRPO.
- Y. Tang, R. Munos. On a few pitfalls in KL divergence gradient estimation for RL. arXiv, June 2025.
- N. Lambert et al. Tülu 3: pushing frontiers in open language model post-training. arXiv, November 2024. Section 6, reinforcement learning with verifiable rewards.
- DeepSeek-AI. DeepSeek-R1: incentivizing reasoning capability in LLMs via reinforcement learning. arXiv, January 2025. Section 2.2.2, rule-based rewards.
- L. Gao, J. Schulman, J. Hilton. Scaling laws for reward model overoptimization. ICML, 2023. Section 2 (the synthetic setup; the KL of best-of-), the functional forms and their coefficients.
- R. S. Sutton, A. G. Barto. Reinforcement Learning: An Introduction, 2nd ed. MIT Press, 2018. Chapter 13.
- S. Levine. CS285: Deep Reinforcement Learning. University of California, Berkeley. Lectures on policy gradients and on RL for language models.