GRPO in NumPy: group-relative advantages, process and shaped rewards
I was asked this
No reports yet. Sign in to say where you were asked it.
An AI/ML design interview question asked in Amazon's system design round, last asked Jan 2026. It tests reinforcement learning, rlhf and numpy.
In a notebook, implement the core math of Group Relative Policy Optimization with NumPy. For each prompt the policy samples a group of completions, each completion gets a score, and every completion is judged against the others in its group.
- Compute the group-relative advantages from the scores.
- Extend it to process rewards, where intermediate steps of a completion are scored, and turn those into per-token advantages.
- Add shaped rewards and show how they change the advantages.
Explain why GRPO needs no learned value function and what can go wrong when a group's scores are all the same.
Requirements
- Functional: a function from a
(groups, group_size)array of scores to per-completion advantages; a version that takes per-step scores and returns per-token advantages; a shaped-reward term added on top, with a worked example of how it moves the advantages. - Non-functional: vectorized NumPy (no Python loops over completions), numerically safe when a group's spread is zero, and small enough to check by hand.
Clarifying questions
- Normalize by the group's standard deviation, or only subtract the group mean? Population or sample std?
- For process rewards, which steps does a token belong to: the step it falls in, or every step at or after it?
- Are completions of different lengths padded, and does a mask come with them?
- Is the shaped reward a per-completion bonus (format, length) or a per-step one, and is it added before or after normalization?
What a strong answer covers
- Advantage as
(r - mean_group) / (std_group + eps), computed along the group axis with broadcasting, and why the group mean acts as the baseline a value network would otherwise learn. - The degenerate group: when every score is equal the advantages are all zero (or blow up without
eps), so the prompt contributes no gradient; naming the fixes (skip or resample such groups, mix in a small absolute term). - Process rewards: normalizing step scores across the whole group, then giving each token the sum of normalized scores of its step and the later steps (a return-to-go), with padding masked out.
- Shaped rewards: showing on a small group that a bonus shared by every completion cancels after normalization, while one that varies within the group reorders the advantages, and how a shaped term can be gamed (length bonuses that reward rambling).
- Tests on tiny hand-computed groups: zero-mean advantages per group, unit spread, an all-equal group, a single-step process reward matching the outcome version.
Follow-ups
- How the advantages enter the clipped policy loss, and where the KL penalty to the reference model goes.
- What group size you would pick, and how variance of the advantage changes with it.
- Dividing by std biases updates toward easy and hard prompts; what happens if you drop it?
- How you would detect reward hacking of the process reward model during training.
Where it's asked
- Amazon · System design · Last asked Jan 202655 min · Design discussion
What it tests
reinforcement learning · rlhf · numpy
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
