Practice in Lab: Own grouped advantages and the clipped policy surrogate →
Own grouped advantages and the clipped policy surrogate
Grouped policy optimization compares completions of the same prompt rather than fitting a value baseline. Reward normalization must use that prompt’s group, and old policy scores are immutable evidence from rollout collection. Clipping limits the surrogate incentive after a ratio crosses a boundary, with different signs for good and bad advantages. Per-response token reduction prevents long completions from acquiring accidental extra weight. The Lab deliberately separates this mechanism from an end-to-end RL training system.
Prepare offline
Choose Download entire course or Download entire category first. After completion, code and checks run locally on CPU. The independent example needs no account, remote service or API key.
import torch
rewards = torch.tensor([[1., 3., 2.]])
a = rewards - rewards.mean(-1, keepdim=True)
print(a / (rewards.std(-1, correction=0, keepdim=True)+1e-8))Review the evidence
Inspect constant-reward groups, stale rollout policies and response masks. Normalizing rewards is not a substitute for an informative reward function, a reference policy or an evaluation protocol.
Your task
Implement the GRPO-style clipped surrogate core for [prompts,completions,tokens] sampled log probabilities. Use detached within-prompt reward advantages normalized by population standard deviation plus 1e-8. Compute exp(logp-old_logp.detach()), take the minimum of unclipped and [1-clip,1+clip] clipped ratio times advantage, average over valid tokens within each response, then equally over prompts/completions. Detach rewards. Require each response to contain a token, at least two completions, 0<clip<1 and absolute log-ratio<=20. This exercise isolates the surrogate; rollout collection, a reference-KL penalty and reward design are separate components.
The explanation opens here once every check passes.