The loss in Module 6 is a single on-policy step. Longer runs need a limit on how far one update may move, a way to notice that the run has left the rails, and sometimes a second reward so the model cannot collect the correctness bonus with an unreadable string. This module adds those pieces one at a time. Each one changes the optimization problem. None of them is free.
What to plot
Log at least these series against step.
Series
Healthy shape
A bad shape
Mean training reward
rises, then flattens below 1
jumps to 1 while eval accuracy does not, or falls to 0
MATH-500 accuracy
rises and stays up
a late collapse to 0
Mean response length
stable or slowly changing
runs into the token cap
Advantage mean
about 0
anything else is a bug
Advantage std
away from 0 on mixed groups
stuck at 0 because every group is tied
Entropy of
moderate
near 0 (the model repeats one string) or near (the model is uniform)
Mean policy ratio
near 1
far from 1 for many steps
The advantage mean is a unit test, not a result. Entropy is the early-warning channel. A correctness reward can stay high for a while after the model has started to emit a single memorized template. Entropy drops first.
Token entropy at a position is , computed from the same logits as the log probabilities. Average it over generated positions. Very low entropy, under about nats, means one token dominates. Values around to are a comfortable regime for a peaked but not frozen distribution.
Clipped policy ratios
Once the weights move, the log probability in the Module 6 loss is no longer the log probability of the policy that sampled the rollout. Let be the snapshot that sampled , and the weights being updated.
Definition 7.1 (Clipped ratio).
The policy ratio and its clip at are
The clipped loss is
Definition 7.1 is the PPO surrogate, applied to a whole rollout because is a single number. If , then and \eqnref{equ:clip-loss} agrees with the on-policy loss of Module 6 at that point. The gradient does not: the ratio form differentiates through only, with held fixed, which is the same credit assignment as the on-policy loss at the first step and a controlled one afterward.
The is the pessimistic choice. When , a ratio above is not given extra credit. When , a ratio below is not given extra credit for pushing a bad rollout even lower. In both cases the update stops helping you once you have left the trust region.
A worked clip, with and four ratios :
Only the first and last move. The inner two were already inside .
DeepSeek-R1 used a very wide clip, , which barely constrains a ratio. Classic PPO uses or . A wide clip is closer to the unclipped objective. A narrow clip is more stable and slower. Record the mean of . A mean of means the new policy has put several times more probability on the sampled tokens than the sampler did, and the clip is the only thing keeping the step finite.
clip windowratioclippedFigure 7.1. Ratios outside the window from 1 minus epsilon to 1 plus epsilon are pulled back before they scale the advantage.
Figure 7.1 is per rollout. The window is around , the point where the new policy and the sampling policy agree.
A practical loop that fits on one GPU: sample a small group, take one clipped update, throw the group away, sample again. That is a short-horizon approximation of “collect a big batch under , then optimize.” Taking many steps on one tiny group drives out of the window immediately, and the clip then zeros the gradient of those rollouts.
A KL term, and what it actually is
Clipping limits one step. A KL penalty limits drift away from a reference policy , usually the model at the start of training.
Remark 7.1 (Sampled log-ratio penalty).
The penalty used in the chapter-7 implementation is
added to the policy loss. is a small coefficient, often between and .
Remark 7.1 is not . The divergence would weight the log-ratio by and sum over the vocabulary. \eqnref{equ:kl-surrogate} evaluates the log-ratio only on tokens sampled from , and it sums them over the completion, so the magnitude grows with length. Because is a constant with respect to , descending on this term pushes probability down on the sampled tokens. That is a reasonable regularizer when the policy-gradient term is alive. When every advantage is zero, it is the only gradient left, and it can drive the policy toward a high-entropy distribution that no longer answers the question.
A run with can climb from the mid-teens to around 40 percent MATH-500 accuracy and then collapse: the loss spikes, the reward falls to zero, and the samples become non-words. Length grows into the cap before the crash. That is the failure mode to recognize. Plot entropy next to length. If you keep a KL term, the more stable variants are a smaller , a length-normalized log-ratio, or an importance weight in front of the log-ratio so the penalty refers to rather than to the old samples. Several later systems, including Dr. GRPO and the GRPO variant in DeepSeek-V3.2, drop the KL term on math and rely on the clip. Dropping it also frees the memory of the frozen reference copy.
referencecurrent policyFigure 7.2. The reference stays at the initial weights. The sampling policy is a recent snapshot and is not this reference.
Figure 7.2 is the distinction that makes the logs readable. The old policy in the ratio is updated every time you resample. The reference in \eqnref{equ:kl-surrogate} is not, except on a slow schedule if you choose to refresh it.
A format reward
Correctness does not constrain the shape of the trace. A format reward does.
Definition 7.2 (Think-span reward).
Let the generated ids be , and let and be the ids of <think> and </think>. The format reward is when both ids occur and the first comes before the first . Otherwise it is .
Definition 7.2 is if either tag is missing, if they are swapped, or if a typo means the tokenizer never emitted those ids. On the Qwen3 reasoning tokenizer the ids are and . On the base tokenizer the string <think> is several ordinary subwords, so the test never fires. Either add those special tokens before training, or load the reasoning tokenizer. The embedding table already has rows up to index , so the new ids do not require a larger matrix. They do require the optimizer to learn embeddings that the base model never used.
with unless you have a reason to weight them differently. A correct, well-formed answer scores . A correct answer with no tags scores . An incorrect answer with tags scores . The group can now prefer a formatted wrong answer over an unformatted wrong answer, which is what you asked for, and it can also prefer a formatted wrong answer over an unformatted right one if you are not careful with . Look at a few transcripts when you turn this on.
The prompt has to mention the tags. A reward for a string the prompt never requested is a sparse signal at the start of training, when almost every sample scores zero on format and the advantages collapse.
Reward hacking, briefly
Any reward that is not the thing you care about will be gamed once the model is strong enough to find the seam. With a pure correctness reward the seam is short: box the right number and write whatever else you like. With a format reward the seam is a pair of tags around an empty span. With a length bonus the seam is padding. The defense that fits this course is to keep the reward equal to the evaluation rule, to read samples every time a curve looks too good, and to trust MATH-500 accuracy over the training reward when they disagree.