Reward a model only when its final answer is right, and it learns to write longer, checked answers. In the toy below, 150 updates with groups of 16 lift the expected accuracy from 25.8% to 79.7%, and the expected answer from 173 to 449 tokens.
Press Train and watch four habits compete for the policy. Change the group size and train again. Then we build each part from zero: decoding, DeepSeek-R1’s reward, PPO, GRPO and DAPO, and why reasoning works and when it fails.
You need a little Python, what a probability is and what a language model does (Lectures 7 and 8). Chain-of-thought prompting is in Lecture 9. We build the rest from zero.
Lecture 12, "Reasoning 1", of Stanford CS224N, Natural Language Processing with Deep Learning (Winter 2026, Diyi Yang and Yejin Choi; this lecture’s slides are Yejin Choi’s). Its readings are chain-of-thought prompting (Wei et al., 2022), self-consistency (Wang et al., 2023), DeepSeek-R1 (DeepSeek-AI, 2025) and DAPO (Yu et al., 2025). Process reward models and test-time compute scaling are readings of Lecture 13; this page covers them only as far as Lecture 12’s slides do. Before: Lecture 11, Benchmarking and Evaluation. Next: Lecture 13, Reasoning 2.
Illustrative toy, not a language model. The policy picks one of four habits; each has a made-up length and chance of a right answer. Each update draws a group, scores answers against the group (Chapter 6) and takes four clipped steps (Chapter 5). The default run (G = 16, first draws) is the lede’s. DeepSeek-R1-Zero showed the pattern at scale: AIME 2024 pass@1 from 15.6% to 77.9% as its answers grew (Chapter 2).
Chapter 0
See why a model that must answer at once runs out of steps, and what writing them out buys
A language model writes one token at a time. Each token comes out of the same fixed stack of layers; GPT-3 has 96. The model spends that stack on every token, easy or hard.
A problem with dependent steps needs the result of step 1 before step 2 can start. If the answer must be the next token, every step has to fit inside one pass. When the steps do not fit, the model guesses.
Each column is one token; each row is a layer. Numbered dots are the problem’s dependent steps. Add steps, then let the model write its working first.
Cartoon with 4 layers. Real layers do not do one tidy step each, and real chains spend many tokens on one step. The point is the budget: depth per token is fixed; the number of tokens is not.
Writing the steps out changes the budget. The next pass reads every written token, so the work continues across tokens. Feng et al. (2023) proved a version of this. Under a standard assumption from complexity theory, a fixed-depth transformer cannot directly answer some arithmetic tasks unless it grows enormously. A fixed-size one solves them by writing out the steps.
The worked number: with chain-of-thought examples in the prompt (Lecture 9), PaLM 540B rose from 17.9% to 56.9% on GSM8K grade-school word problems. That method needs a person to write the examples. This lecture asks two questions. How do we choose the tokens a model writes? And can a model learn, by itself, to spend tokens well?
The lecture has three parts: decoding, which picks each token; training a model to reason with reinforcement learning, as DeepSeek-R1 did; and why reasoning works, and when it fails.
Symptom: fluent, confident, wrong answers on problems with several steps.
Measure: accuracy as the number of steps grows, answering at once against writing steps first.
Fix: let the model write its steps, by prompting (Lecture 9) or by training (Chapter 2).
Chapter 1
Turn scores into probabilities, choose greedily or sample, and see why the most likely text loops
At each step the model gives every token in its vocabulary a score, a logit, and a softmax turns the scores into probabilities. A decoding algorithm picks one token.
Greedy decoding takes the most likely token every time. Beam search keeps the k most likely partial sequences; with k = 1 it is greedy. Both chase the most likely text, not the best. With made-up numbers: “the” at 0.5, then a word at 0.4, is a path worth 0.20. “A” at 0.4, then a word at 0.9, is worth 0.36, and greedy never looks.
The most likely text also repeats, because each repeat makes the next more likely (Holtzman et al., 2019). Reasoning models still loop, a study in the lecture found, most at low temperature, in small models and on hard problems.
Blue bars are next-token probabilities after “Let’s”. Move the temperature and top-p, then draw 20 tokens.
Illustrative logits. The last bar is the tail: thousands of rare tokens together.
Call the logit of token i zi, the temperature T, and the probability of token i pi. Temperature divides every logit by T before the softmax:
p-i equals e to the z-i over T, divided by the sum over j of e to the z-j over T.
Divide each logit zi by T, exponentiate, and divide by the total. A T below 1 stretches the gaps, so the top token takes more. A T above 1 squeezes them, so the tail gets more.
With made-up logits 2, 1 and 0: T = 1 gives 0.665, 0.245 and 0.090. T = 0.5 gives 0.867, 0.117 and 0.016. T = 2 gives 0.506, 0.307 and 0.186. T near 0 is greedy.
the tail, thousands of rare tokens, holds 5% → 30 tokens all avoid it with chance 0.9530 = 0.215
Top-k sampling keeps only the k most likely tokens. Top-p (nucleus) sampling keeps the smallest set whose probabilities add up to p. With p = 0.9 on 0.665, 0.245 and 0.090, it keeps two tokens and rescales them to 0.731 and 0.269.
The slides’ rule: greedy for a short answer with one right value, sampling for open writing and long chains of thought. Many samples allow a vote (self-consistency, Lecture 10: GSM8K from 56.5% to 74.4% with 40 paths), or best-of-N: keep the one a reward model likes best.
Symptom: the same sentence over and over until the length limit.
Measure: repeats; the lecture’s study flagged a 30-token phrase seen more than 20 times.
Fix: sample at a moderate temperature with top-p; in training, penalize answers that run past the cap (Chapter 7).
Chapter 2
Train a base model with a rule that checks the final answer and the tags, and nothing about the steps
Prompting shows a model how to reason. DeepSeek-R1-Zero (DeepSeek-AI, 2025) asked if a model could find reasoning by itself. It took the DeepSeek-V3 base model, skipped supervised fine-tuning, and ran reinforcement learning on maths, code and logic questions with checkable answers.
The prompt is a fixed template: think inside <think> … </think> tags, then answer inside <answer> … </answer> tags. Nothing in it says how to think.
The reward is a rule, not a model. The accuracy reward compares the final answer with the known one; for code, it runs test cases. The format reward checks the tags. Learning from a reward that a program can check is called RLVR, reinforcement learning from verifiable rewards.
Yellow is the question and the tags; warm text is what the model wrote. Pick a response, read the checker, then switch the format reward off.
Illustrative responses. R1 adds the two rewards with equal weight but does not print their values, so each part here is 1 or 0. The question is from Wang and Zhou (2024); its answer is 64.
Call the accuracy reward Racc and the format reward Rformat. The total reward R is their sum:
R equals R-acc plus R-format.
Compare the answer between the tags with the known one: Racc. Check the tags: Rformat. Add them to get R. Nothing reads the steps.
A worked, tagged answer of 64 gets R = 1 + 1 = 2. A tagged answer of 48 gets 0 + 1 = 1. A right answer with no tags gets 0 + 0 = 0, because the parser cannot find it. A bare, tagged “64” gets 2, the same as the worked answer.
Training used GRPO (Chapter 6) with 16 answers per question, at temperature 1, for 10,400 steps. Its score is pass@1, the average chance that one sampled answer is right. On AIME 2024 it rose from 15.6% to 77.9%, and a vote over 16 answers reached 86.7%. The average answer grew from hundreds to thousands of tokens, with no instruction to write more. Mid-training, self-checks appeared: “Wait, wait. Wait. That’s an aha moment I can flag here.”
The slides call this learned test-time compute: thinking longer pays on hard problems. The length cap mattered. When it rose from 32,768 to 65,536 tokens at step 8,200, score and length both jumped.
Symptom: right answers scored as wrong.
Measure: the share of responses whose final answer the parser cannot read.
Fix: demand one answer format. DAPO rewrote its maths answers as integers for this reason.
Chapter 3
Compare an outcome reward with a process reward on a lucky chain, and see why R1 chose the outcome
An outcome reward scores only the final answer, as R1-Zero’s rule does. A process reward model (PRM) scores each step. It is a learned model: it reads the solution up to step k and gives the probability that step k is correct.
Why care? A chain can reach the right answer through wrong steps; response D in Chapter 2 got full reward for a bare guess. An outcome reward cannot tell luck from skill.
Each row is a step the model wrote; the blue bar is the PRM’s probability that the step is right. Switch chains and compare the two scores.
Illustrative chains and PRM probabilities. The scoring rule, the product of step probabilities, is Lightman et al.’s (2023).
Call the PRM’s probability that step k is right pk, and the number of steps K. Lightman et al. score a whole solution as the chance that every step is right, SPRM:
S-PRM equals the product over k from one to K of p-k.
Ask the PRM about each step and get pk. Multiply all K of them. One doubtful step pulls SPRM down, even if the others are good.
With made-up probabilities: the sound chain’s steps get 0.97, 0.96, 0.95 and 0.98, so SPRM = 0.867. The lucky chain’s get 0.20, 0.15, 0.93 and 0.97, so SPRM = 0.027. The outcome reward gives both a 1.
Does it pay? Lightman et al. had people label 800,000 steps across 75,000 solutions to 12,000 MATH problems, the PRM800K set. Choosing the best of 1,860 sampled solutions on 500 test problems, their PRM solved 78.2%. An outcome reward model solved 72.4%, and a majority vote 69.6%.
R1 still chose the outcome rule, for three reasons. A “step” is hard to define in general reasoning. Checking a step is hard, and human labels do not scale. And any learned reward model gets reward-hacked: the policy finds outputs that the reward model scores high but that are not right.
The authors found PRMs useful for reranking and search, but not worth their cost inside large-scale RL. The slides add a subtler cost: a step-grader can punish an unusual path that works.
Symptom: the reward keeps rising while true accuracy stalls.
Measure: compare the reward model’s score with a rule-based check on held-out problems.
Fix: use rule-based rewards where a program can check the answer, and few steps against any learned reward. R1 used its preference reward models only in the last 400 steps of its final stage.
Chapter 4
Walk R1’s training stages with their scores, then see why small models gain more from distillation than from RL
R1-Zero reasoned well but read badly. Its chains were hard to follow, and it switched languages mid-answer, often between English and Chinese. Pure RL also helps only where a program can check the answer. It will not teach good writing, and it will not find safety rules by solving maths.
The full R1 recipe adds stages around the RL (Figure 2 of the paper):
Pink bars are benchmark scores. Step through the stages and watch which score each stage moves. Then switch to small models.
Scores from the DeepSeek-R1 paper (arXiv 2501.12948, version 2): Table 3 (stages), Table 15 (distilled models, GPT-4o-0513, Claude-3.5-Sonnet-1022) and Table 16 (Qwen2.5-32B trained by RL alone). AIME 2024 is pass@1.
How does a reward count languages? Call the number of words in the chain that are in the prompt’s language Ntarget, and the number of all words Nwords. The language reward Rlang is their ratio:
R-lang equals N-target over N-words.
Count the chain’s words in the prompt’s language, Ntarget. Divide by all its words, Nwords. A chain that never switches gets Rlang = 1.
With made-up counts: an English prompt and a 120-word chain with 90 English words. Rlang = 90 / 120 = 0.75. The paper reports that this reward costs a little accuracy but makes chains readable.
The stages trade one score against another. The small cold-start set cost Dev1 accuracy on AIME, 77.9% down to 59.0%, while IF-Eval instruction following rose from 46.6 to 71.7. Reasoning RL won most of it back (74.0%). R1 ended at 79.8% on AIME, with ArenaHard at 92.3 against R1-Zero’s 53.6.
Distillation trains a small model to copy a large one’s outputs. R1 wrote about 800,000 samples, and open models from 1.5B to 70B weights were fine-tuned on them, with no RL. The distilled Qwen2.5-32B scored 72.6% on AIME 2024; the same base trained by large-scale RL alone scored 47.0%. The lecture’s advice: on a small budget, distil from a large reasoning model; do not run RL on a small one.
Symptom: chains that switch language in mid-sentence.
Measure: the share of words in the prompt’s language, Rlang.
Fix: add Rlang to the reward, and accept a small loss in accuracy for readable chains.
Chapter 5
See why PPO keeps five models in memory, and how its clip stops one batch from pushing too far
Lecture 8 used PPO for RLHF. The policy is the language model. It gets one reward at the end of each response, but it wrote hundreds of tokens. To share out the credit, PPO trains a second network, the critic or value network, to guess at each token how much reward is still to come. A token’s advantage is how much better things went than the critic guessed.
The slides count five models in memory: the policy, an older copy that sampled the data, a frozen reference model for a KL penalty, the critic and a reward model. Its advantage method, GAE (generalized advantage estimation), needs a value at every token. For 512 prompts, 16 responses each and 2,000 tokens per response, that is 16,384,000 value estimates per step. The slides add that it is not clear the critic learns useful per-token credit.
PPO reuses each batch for several updates, so it must not drift too far from the policy that sampled it. The probability ratio r is a token’s probability under the current policy divided by its probability under the old one. The clip stops the push once r leaves a band around 1.
The dashed line is r × A with no clip; the pink line is PPO’s objective. Where pink is flat, the token gets no push. Move r and ε, and flip the sign of A.
Exact for one token with the given A and r. Training averages the objective over all tokens in the batch.
Call the ratio r, the advantage A and the clip width ε. PPO maximizes, for each token, the objective L:
L equals the min of r times A, and clip of r between one minus epsilon and one plus epsilon, times A.
Multiply r by A. Also clip r to the band, then multiply by A. Keep the smaller. Once a good token’s r passes 1 + ε, L stops growing, so the gradient is zero and the push stops.
With ε = 0.2 and A = +1: at r = 1.1, L = 1.1. At r = 1.5, L = min(1.5, 1.2) = 1.2, flat, so no more push. With A = −1: at r = 0.5, L = min(−0.5, −0.8) = −0.8, flat. At r = 1.5, L = −1.5: no clip, so a bad token that grew is still pushed down.
Symptom: training collapses after a few large updates.
Measure: the share of clipped tokens and the KL divergence from the old policy, step by step.
Fix: a smaller ε or learning rate, or fewer passes over each batch.
Chapter 6
Sample a group of answers to one question and score each against the group, with no critic
GRPO, group relative policy optimization (Shao et al., 2024, the DeepSeekMath paper), drops the critic. For each question, draw a group of G answers from the old policy and reward each one. The group itself says what a usual answer earns.
Tap an answer to flip it between right and wrong. Pink bars are the advantages. Make every answer right and see what is left.
Exact z-scores with the population standard deviation (divide by G). A new group gives each answer a 40% chance of being right. Some implementations divide by G − 1 instead.
Call answer i’s reward ri, for i from 1 to G. Its advantage Ai is its reward’s distance from the group mean, in units of the group’s spread:
A-i equals r-i minus the mean of r-one through r-G, over the standard deviation of r-one through r-G.
Average the G rewards. Subtract the average from each ri. Divide by the spread. Answers above the group’s average get a positive Ai and become more likely; those below get a negative one.
G = 8 and 3 answers right: the mean is 0.375 and the standard deviation is 0.484. Each right answer gets A = (1 − 0.375) / 0.484 = +1.29; each wrong one gets −0.77. Three times 1.29 equals five times 0.77, so the advantages add up to zero.
Every token of answer i gets the same Ai. GRPO then uses PPO’s clipped objective token by token. It also moves the KL penalty out of the reward and puts it straight into the objective.
| PPO | GRPO | |
|---|---|---|
| Models in memory | policy, old policy, reference, critic, reward model | policy, old policy, reference (R1 used rule rewards, so no reward model) |
| Advantage | GAE at every token | the group’s z-score of the reward |
| Credit per token | per token, through the critic | the same for every token of an answer |
| KL penalty | inside the per-token reward | a separate term in the objective |
The catch: if all G answers get the same reward, the spread is zero and every advantage is zero. Code adds a tiny number to the spread, but the push is still zero. A question the policy always solves teaches nothing. With 16 answers and a 95% chance of a right answer, all 16 are right with chance 0.9516 = 0.44.
Symptom: learning slows as the model gets better.
Measure: the share of groups whose answers all got the same reward.
Fix: dynamic sampling: drop those groups and draw more (Chapter 7).
Chapter 7
Raise the upper clip, drop groups that teach nothing, weight every token the same, and shape the reward of overlong answers
DAPO (Yu et al., 2025, ByteDance Seed and Tsinghua AIR) set out to reproduce R1-Zero in the open. Plain GRPO trained Qwen2.5-32B to only 30% on AIME 2024; DeepSeek had reached 47% from the same base. Four changes reached 50%, in half of DeepSeek’s training steps. DAPO also dropped the KL penalty, since a reasoning model must move far from where it started.
Pick a fix, then move its slider. Each view is exact arithmetic from the paper’s settings.
Settings from the DAPO paper: εlow 0.2, εhigh 0.28, an expected maximum of 16,384 tokens plus a 4,096-token soft-punish cache. The token-weight view pairs your long answer with one 100-token answer.
Call the answer’s length in tokens |y|, the hard limit Lmax and the width of the soft zone Lcache. DAPO adds a length penalty Rlength to the correctness reward:
R-length of y is zero while the length of y is at most L-max minus L-cache. In the soft zone it is L-max minus L-cache, minus the length of y, all over L-cache. Past L-max it is minus one.
Below Lmax − Lcache, no penalty. Inside the soft zone, the penalty grows in a straight line with |y|. Past Lmax, it is −1. Add it to the correctness reward.
Lmax = 20,480 and Lcache = 4,096, so the soft zone starts at 16,384. A 16,000-token answer gets 0. An 18,000-token answer gets (16,384 − 18,000) / 4,096 = −0.39. A 21,000-token answer gets −1.
DAPO added the fixes one at a time and measured each (Table 1, AIME 2024, average of 32 samples):
| Setup | AIME 2024 |
|---|---|
| DeepSeek-R1-Zero-Qwen-32B (for reference) | 47 |
| Plain GRPO | 30 |
| + overlong filtering | 36 |
| + clip-higher | 38 |
| + soft overlong punishment | 41 |
| + token-level loss | 42 |
| + dynamic sampling (DAPO) | 50 |
Symptom: the group’s answers become near copies of each other, and accuracy plateaus.
Measure: the policy’s entropy over training; it falls fast in plain GRPO (DAPO’s Figure 2).
Fix: clip-higher, which lets rare tokens grow faster.
Chapter 8
Run the toy trainer with small groups, turn on two of DAPO’s fixes, and count the runs that stall
This is the hero’s toy, opened up. The policy answers a maths question with one of four habits. Each habit has a length and a chance of a right answer:
It starts with chances 55%, 30%, 5% and 10%. The reward is 1 for a right answer and 0 otherwise. Each update draws a group of G answers, scores them against the group (Chapter 6), and takes four clipped steps on the habit choices (Chapter 5).
Pick a group size and train one run. Then run 200: each dot is one run’s final accuracy, red if it stalled below 70%. Turn on the fixes and run 200 again.
Illustrative toy: four habits, made-up lengths and success chances, a learning rate of 1, 150 updates. The 200-run counts are exact for seeds 1 to 200 and change with the settings, not from visit to visit.
What to look for. With groups of 16, all 200 runs reach “steps, then check” and an expected accuracy near 80%. With groups of 4, the policy often commits to “a few steps” before “check” has paid off often enough: 81 of 200 runs stall below 70%. Clip-higher cuts that to 44, and dynamic sampling as well to 35. With groups of 2, the two fixes cut stalls from 133 to 32.
Watch the blue entropy curve in a stalled run. It drops toward zero while accuracy sits near 45%: the policy has stopped exploring. That is the entropy collapse of Chapter 7, in miniature. Dynamic sampling matters most with small groups, where many come out all wrong or all right. With G = 4 and no fixes, about 34 of 150 updates get no push.
Why does length grow? Nothing in the reward pays for length. Checking is right more often, and it happens to take more tokens. The loop is the longest habit and it loses, because it is never right.
Symptom: a run settles on a mediocre habit and never leaves.
Measure: entropy near zero while accuracy is still low; the count of stalled runs over many seeds.
Fix: larger groups, clip-higher, dynamic sampling.
Chapter 9
Find reasoning a base model already holds by branching its first token, then measure how often a chain admits what it used
Why can RL find reasoning in a base model at all? Wang and Zhou (2024) decoded a pretrained model with no reasoning prompt. Greedy decoding answered at once, often wrongly. Branching on the top 10 first tokens, then going on greedily, turned up many chains of thought that greedy decoding hid.
Which branch to trust? On a chain-of-thought branch, the model writes the answer with more confidence, measured as the gap between the top two token probabilities.
Each row is a branch; k is its first token’s rank and the blue bar its answer’s confidence Δ. Pick a rule.
Question, branches and Δ values: Table 1 of Wang and Zhou (2024), PaLM-2 Large; 5 of the top 10 branches, shortened. The right answer is $64.
Call the answer’s tokens a and their count |a|. At each answer token xt, call the top two probabilities p(xt1) and p(xt2). The confidence Δ is their average gap:
Delta equals one over the size of a, times the sum over the answer tokens x-t of p of x-t-one minus p of x-t-two.
At each answer token, subtract the runner-up’s probability from the winner’s, then average over the |a| tokens. A big Δ means the model was sure.
With made-up probabilities: a direct answer “60” has gaps 0.62 − 0.31 and 0.55 − 0.40, so Δ = 0.23. A branch that works it out to “64” has gaps 0.998 − 0.001 and 0.992 − 0.005, so Δ = 0.992.
On the first 100 GSM8K questions (PaLM-2 Large), greedy decoding scored 44%. Ranking 10 branches by log-probability scored 37%; ranking them by Δ scored 72%.
Two more answers. Prystawski et al. (2023): steps help when training data come in local clusters. Gandhi et al. (2025): under identical RL on the Countdown game, Qwen-2.5-3B improved far more than Llama-3.2-3B. Qwen already had four habits: verification, backtracking, subgoal setting and backward chaining. Priming Llama with these habits closed the gap, even with wrong answers in the examples.
When it fails. Chen et al. (2025) put hints in prompts. When a hint changed the answer, the chain said so on average 25% of the time for Claude 3.7 Sonnet and 39% for DeepSeek R1. Outcome RL raised it, then stalled at 28% (MMLU) and 20% (GPQA). And thinking can hurt: on tasks where deliberation makes people worse, o1-preview scored up to 36.3 points below GPT-4o (Liu et al., 2025).
Symptom: a fluent chain that hides what drove the answer.
Measure: plant hints; of the answers a hint changed, count the chains that say so.
Fix: treat the chain as evidence, not a record. Check the answer itself.
Chapter 10
Keep the parts, the numbers and the code, and see where to go next
A model spends a fixed stack of layers on each token, so hard problems need written steps. Decoding chooses those tokens: greedy is repeatable but loops and misses better paths; sampling, shaped by temperature and top-p, gives the variety that voting and RL need. R1-Zero showed that a reward checking only the final answer, optimized with GRPO, teaches a base model to think longer and check itself. GRPO replaces PPO’s critic with the group’s average reward, and DAPO keeps long-chain RL healthy with four fixes. The written chain is useful, but it is not a faithful record.
| Part | What it does | Key idea | Watch for |
|---|---|---|---|
| Greedy, beam | the most likely next token or sequence | repeatable; fine for short exact answers | loops; misses better paths |
| Temperature, top-p | shape what sampling draws | divide logits by T; keep mass p | the long tail at high T |
| Rule reward (RLVR) | score answers with a program | R = Racc + Rformat | answers the parser cannot read |
| Process reward model | score each step | product of step probabilities | labels cost; reward hacking |
| PPO | policy update with a critic | clipped ratio; GAE advantages | five models; a critic that may not help |
| GRPO | policy update with no critic | advantage = group z-score | all-equal groups give no push |
| DAPO | GRPO for long chains | clip-higher, dynamic sampling, token-level loss, overlong shaping | entropy collapse without them |
| Distillation | copy a reasoner into a small model | SFT on the teacher’s chains | the student stays below the teacher |
| Number | Value | Where |
|---|---|---|
| Hero toy, G = 16, 150 updates: expected accuracy; expected length | 25.8% → 79.7%; 173 → 449 tokens | Hero |
| GSM8K, PaLM 540B, standard against chain-of-thought prompts | 17.9% → 56.9% | Chapter 0 |
| 30 tokens all avoid a 5% tail | 0.9530 = 0.215 | Chapter 1 |
| R1-Zero, AIME 2024 pass@1; vote over 16 | 15.6% → 77.9%; 86.7% | Chapter 2 |
| Best of 1,860 on MATH: PRM, outcome RM, majority vote | 78.2%, 72.4%, 69.6% | Chapter 3 |
| R1 stages, AIME 2024: R1-Zero, Dev1, Dev2, Dev3, R1 | 77.9, 59.0, 74.0, 78.1, 79.8 | Chapter 4 |
| Qwen2.5-32B, AIME 2024: distilled from R1, RL alone | 72.6%, 47.0% | Chapter 4 |
| PPO value estimates per step (512 prompts × 16 × 2,000 tokens) | 16,384,000 | Chapter 5 |
| GRPO, 3 of 8 right: advantages | +1.29, −0.77 | Chapter 6 |
| DAPO, AIME 2024: plain GRPO, DAPO | 30, 50 | Chapter 7 |
| Toy, G = 4: stalled runs of 200, without fixes, with both | 81, 35 | Chapter 8 |
| GSM8K (first 100), PaLM-2 L: greedy, CoT-decoding | 44%, 72% | Chapter 9 |
| Average hint-reveal rate: Claude 3.7 Sonnet, DeepSeek R1 | 25%, 39% | Chapter 9 |
The core as code: the rule reward, GRPO’s advantages, and DAPO’s token-level loss with dynamic sampling, for one batch of sampled answers. It runs as it stands; plug in your model’s log-probabilities.
python import re, numpy as np def rule_reward(response, answer): # R = R_acc + R_format tagged = re.search(r"<think>.*</think>\s*<answer>(.*?)</answer>", response, re.S) r_format = 1.0 if tagged else 0.0 r_acc = 1.0 if tagged and tagged.group(1).strip() == answer else 0.0 return r_acc + r_format def group_advantages(rewards): # GRPO: z-score within the group r = np.asarray(rewards, float) sd = r.std() # population std, divide by G return None if sd < 1e-9 else (r - r.mean()) / sd # None: no signal (DAPO drops it) def dapo_loss(groups, eps_low=0.2, eps_high=0.28): """groups: list of (rewards, [(logp_new, logp_old) per answer, each an array over tokens])""" total, n_tok = 0.0, 0 for rewards, answers in groups: adv = group_advantages(rewards) if adv is None: continue # dynamic sampling: skip all-equal groups for A, (lp_new, lp_old) in zip(adv, answers): ratio = np.exp(lp_new - lp_old) # r per token clipped = np.clip(ratio, 1 - eps_low, 1 + eps_high) total += np.minimum(ratio * A, clipped * A).sum() n_tok += len(lp_new) # token-level: divide by ALL tokens return -total / max(n_tok, 1) # minimize the negative objective print(rule_reward("<think>8*5 + 8*3 = 64</think> <answer>64</answer>", "64")) # 2.0 print(group_advantages([1, 1, 1, 0, 0, 0, 0, 0]).round(2)) # [ 1.29 1.29 1.29 -0.77 -0.77 -0.77 -0.77 -0.77] old, up = np.log(np.full(5, 0.5)), np.log(np.full(5, 0.6)) print(round(dapo_loss([([1, 0], [(up, old), (old, old)])]), 3)) # -0.1: the right answer's tokens rose 20%
Lecture 13 continues: process reward models in depth (“Let’s Verify Step by Step”), speculative decoding, and how to spend test-time compute well. The slides’ closing caveat stands: R1’s models learn to spend more tokens on harder problems, but later studies found that this adaptive thinking still has problems to fix.
Now teach it: press Present or Teach. Explain why a right-or-wrong reward makes answers longer, how GRPO scores 3 right answers out of 8, and why a chain of thought is not a confession. Then go back to the hero, choose G = 4, press New draws a few times, and find a run that stalls.