KatInterview

Debug a GRPO Training Loop and Explain Ratio Drift

Machine LearningModel answer
MediumModel answerMachine Learningreinforcement-learningRLHFpolicy-gradient

Last updated

Walk through a GRPO policy-gradient loop, identify three subtle implementation bugs, and explain why the importance-sampling ratio drifts from 1 even in an on-policy setup.

Setting the Scene

You are working on a reinforcement-learning-from-human-feedback (RLHF) pipeline that fine-tunes a large language model. The optimization algorithm is GRPO (Group Relative Policy Optimization), a critic-free variant of PPO in which the advantage baseline is derived from a group of completions sampled for the same prompt, rather than from a learned value network.

The loop is intended to be strictly on-policy: the same policy that generates the rollouts is immediately updated on them. Despite that intention, training is unstable and the importance-sampling ratios are behaving unexpectedly. Your job is to reason through the loop, surface the bugs, and explain the anomaly.

This is a discussion question. There is no code to run. A strong answer reasons precisely about the GRPO objective, autoregressive log-probability mechanics, and the difference between behavior that is expected by design versus behavior caused by an actual implementation mistake.


Useful clarifying questions to consider

Before diving in, think about which of these you would want to know:

  • Is the reward per-completion (one scalar at the end) or per-step (a reward at every token)? The answer affects how advantages are broadcast across tokens.
  • How many optimizer steps are taken per rollout batch — one, or multiple minibatch epochs PPO-style? This fundamentally changes whether a ratio of exactly 1 is achievable.
  • Is generation done through the same forward pass as the log-prob scoring, or through a separate fast-inference engine that recomputes log-probs in the trainer?
  • Are the sampling hyperparameters (temperature, top-p, repetition penalty) identical between rollout generation and log-prob recomputation?
  • Is there a frozen reference model for KL regularization, and is the KL penalty folded into the reward signal or kept as a standalone loss term?
  • What numerical precision and attention backend is used on each path (bf16 vs. fp32, fused kernels vs. eager)?

Part 1 — The end-to-end GRPO training step

Describe one complete training iteration, touching on:

  1. Sampling prompts from the dataset.
  2. Generating a group of completions per prompt.
  3. Computing group-relative advantages (no value network involved).
  4. Forming the policy-gradient loss.
  5. Updating the model weights.

Part 2 — Three common, easy-to-miss bugs

Assume training is failing for implementation reasons. Name three bugs that are easy to overlook in a GRPO/PPO-style loop — drawn from areas such as log-prob computation, token masking, advantage normalization, or policy mixing.

For each bug, explain:

  • how you would detect it in practice, and
  • how you would fix it.

Part 3 — Why does the ratio drift from 1?

During debugging you observe that the importance-sampling ratio

$$ r_t = \exp!\bigl(\log \pi_\theta(a_t \mid s_t) - \log \pi_{\text{old}}(a_t \mid s_t)\bigr) $$

is not always 1, even though the setup is described as strictly on-policy.

List the most likely causes of this drift and, for each, describe what you would check in the pipeline to confirm it.

Hints

Hint 1

For Part 1, be precise about what 'group-relative' means: the advantage of a completion is its reward minus the mean reward of all completions for that same prompt, optionally normalized by their standard deviation. That normalization step is load-bearing — getting it wrong at the wrong granularity is one of the most common bugs.

Hint 2

For Part 2, think about the token-level mechanics of an autoregressive model. Log-probabilities are produced by the model over the full sequence, but only the *completion* tokens should contribute to the loss and to the advantage weighting. Prompt tokens leaking into the loss, or a shift-by-one indexing error when aligning logits to target token IDs, are both silent and damaging.

Hint 3

For Part 3, separate causes into two buckets: (a) things that make the ratio deviate 'by design' even in a correct implementation (e.g., multi-epoch updates), and (b) things that indicate a genuine mismatch bug (e.g., different sampling configs, precision differences, or accidentally using the updated weights to rescore old rollouts).