Policy Gradients and GRPO
🏆 What is GRPO? Policy-gradient methods train a model from rewards instead of labels: sample outputs, score them, and push up the probability of the better ones. GRPO (group relative policy optimization) samples a group of completions for each prompt and scores each one against its own group's average, so it needs no learned value model, and it limits each update with PPO-style ratio clipping and a KL penalty to a reference model.
Anthropic has run this as a research engineer RL fundamentals round in its debugging form. One report describes
GRPO code producing NaNs, traced to sampling with multinomial from logits without a softmax and to a missing
epsilon in the advantage's standard deviation, followed by questions on the ratio, clipping and why the printed
ratio is not 1. Another describes a simple GRPO training script with three bugs, basic RL follow-ups, and the
question of why training is not strictly on-policy, with the poster noting the cause was simpler than
asynchronous rollouts (the question).
The Prompt
Whether you are asked to write the trainer or to fix one, the interviewer grades the same things: correct log-probabilities and advantages, a loss with the right sign and shape, numerical safety, and an explanation of every term.
"Implement a GRPO trainer for a toy sequence task: for each prompt, sample a group of completions, score them, compute group-relative advantages, and update the policy with a clipped ratio and a KL penalty to a frozen reference."
Clarifying Questions
You: What is the policy and the task? I'll use a tiny autoregressive model over 8 tokens that writes 4 tokens after a prompt token, rewarded by the fraction of positions that count up from the prompt.
Interviewer: Fine. Reward is a black box returning one number per completion.
You: Advantages are (r - mean) / (std + eps) within each prompt's group, and the same advantage is used for
every token of a completion?
Interviewer: Yes. Group size G = 8.
You: How many gradient steps per batch of samples? With one step the ratio is always 1, and clipping does nothing.
Interviewer: Several, say 4, against the log-probabilities from sampling time.
You: Clip range and KL coefficient? I'll use 0.2 and a small beta with the k3 estimator.
Interviewer: Those are fine.
What You'll Build
sample(policy, prompts, G, gen) -> (p, tokens):Gcompletions per prompt,(P * G, L)tokens.token_logp(policy, p, tokens) -> (N, L): log-probability of each sampled token.group_advantages(r, G, eps) -> (N,): rewards normalized within each group.grpo_loss(logp, old_logp, ref_logp, adv, clip, beta) -> (loss, ratio)and atrainloop.