Unit 4 of 11 · about 5 hours · derive, implement

Group-normalized advantages, zero-variance groups and sequence versus token normalization

Two runs called GRPO can optimize different weighted objectives. Fixed-rollout comparisons separate this mechanism from changes in sampled answers or verifier quality.

You'll be able to

  • Implement group advantages with a declared standard-deviation convention and finite all-equal-group behavior.
  • Derive how sequence, token and constant denominators weight unequal-length responses.
  • Specify the exact GRPO variant used in the study and isolate one normalization ablation.

Covers

Group-normalized advantages and zero-variance groupsSequence versus token loss normalization and length pressure
1

Read

In this order, only the parts listed

0 of 3 done

  1. CS336 Spring 2026 Assignment 5: normalization choicesCS336 Assignment 5 - Alignment and Reasoning RL

    Sections 4.1.6 and 4.1.7; Section 4.2.3 group normalization and aggregation problems; Section 5.1, Dr. GRPO

  2. DeepSeekMath: original GRPO objectiveDeepSeekMath, Section 4 (Group Relative Policy Optimization) (Shao et al., 2024)

    arXiv:2402.03300v3, Sections 4.1.1 and 4.1.2, Equations 3 and 4 and outcome advantages

    Read the objective, not the large-model training recipe. Its KL estimator is nonnegative, not strictly positive: it is zero when the sampled probability ratio is 1.

  3. TRL 1.15.0: explicit objective configurationTRL GRPO Trainer

    GRPOConfig: scale_rewards, loss_type, beta and use_bias_correction_kl field documentation

2

Focus on

  • The assignment uses sample standard deviation via torch.std; a population-standard-deviation implementation has different finite-group weights. Specify the stabilizer and reject singleton sample-standard-deviation groups.
  • All-equal rewards yield zero outcome advantages with a finite denominator. They do not imply identical texts or zero KL gradient. Log all-correct and all-wrong groups separately.
  • Original GRPO averages token losses within each response, then responses. Dr. GRPO removes response-length division and group-standard-deviation division. DAPO keeps group scaling and uses an active-token denominator; these choices are not interchangeable.
  • TRL 1.15.0 defaults to loss_type="dapo", scale_rewards="group" and beta=0.0. Set every relevant choice explicitly. Choosing loss_type="grpo" alone does not reproduce every paper detail or KL gradient.
Common mistakes · 3
  • Calling group scaling an unbiased raw-reward gradient. Its random denominator changes prompt weighting.
  • Averaging microbatch token means equally when active token counts differ. Accumulate sums against one shared denominator to match the full-batch token mean.
  • Filtering equal-reward groups without counting generation attempts. Filtering changes prompt exposure and still consumes the rollout budget.
3

Try it yourself

Closed-book, before checking anything

On CPU with PyTorch 2.14.1 in torch.float64, normalize rewards [2., 2., 0., 0.] by centered reward divided by standard deviation plus 1e-6. Sample-standard-deviation magnitudes are approximately 0.86602465; population magnitudes are approximately 0.99999900. Next reduce two response loss lists [2.] and [0., 0., 0.]: the sequence mean is 1, the active-token mean is 0.5, and total sum divided by a fixed constant 8 is 0.25. Verify gradients and padding invariance as well as values.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Does zero scalar loss prove there is no policy gradient?Answer

    No. Centered contributions can cancel in value but have different derivatives. Test gradients explicitly.

  2. 2.Why can padding change a token-mean implementation accidentally?Answer

    Including padded positions in the denominator dilutes active gradients. Both numerator and denominator must use the action mask.

  3. 3.Is changing only the loss denominator a complete Dr. GRPO ablation?Answer

    No. Dr. GRPO also removes group-standard-deviation scaling. Isolate each change before combining them.

5

Practice

  • CS336 Assignment 5: GRPO and Dr. GRPO normalization components

    test_compute_group_normalized_rewards_grpo, test_compute_group_normalized_rewards_drgrpo, test_aggregate_loss_across_microbatch_sequence and test_aggregate_loss_across_microbatch_constant

    These compare outputs against snapshots. Add equal groups, singleton rejection, unequal lengths and gradient tests yourself. The mini-project reuses the components in one live normalization ablation. Do not publish solutions.

You're done when

All four normalization snapshots and your edge-case and gradient tests pass, and a written objective states the mean, scale, mask, denominator and KL choices without relying on the name GRPO.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 17 more in the section
  • CS336 Assignment 5 - Alignment and Reasoning RLStanford University · primary

    Do Sections 3 to 5: prompting and grading baselines, baseline_calcs, then the Section 4.2 functions (tokenization, response log probabilities, rollout rewards, group-normalized rewards, loss aggregation, the on-policy GRPO step) until the GRPO and metrics tests pass on CPU. The handout runs OLMo-2 1B with a two-GPU vLLM loop; run a small GRPO on a model and task that fit your GPU instead, with one normalization ablation and one preserved failure.

  • DeepSeekMath, Section 4 (Group Relative Policy Optimization) (Shao et al., 2024)DeepSeek-AI · required

    Read Section 4: GRPO drops the critic and uses the group mean and standard deviation as the baseline, averages within each sequence, then over the group, and adds a KL term. Section 5 relates RFT, DPO, PPO and GRPO. TRL 1.15.0 defaults differ (loss_type="dapo", beta=0.0).

  • TRL GRPO TrainerHugging Face · reference

    Read after your scratch loss passes. In TRL 1.15.0 the defaults are loss_type="dapo" (token mean), beta=0.0 (no reference model loaded) and scale_rewards="group"; set loss_type="grpo" and beta above zero to approach the DeepSeekMath objective, and state the choice beside any comparison.

Also in this section