← 返回 openai 的题目列表Debug and Improve a GRPO RL Training Loop for Language Models (PyTorch)
类型:online_judge
Problem: Debug and Improve a GRPO RL Training Loop for Language Models (PyTorch)
You are given a piece of PyTorch code that implements a reinforcement-learning fine-tuning loop for a language model (RLHF/RLAIF style). The code claims to use Group Relative Policy Optimization (GRPO). It runs, but the training behavior is incorrect/unstable/metrics look wrong. Your task during the interview is to fix and improve it.
Tasks
Identify and fix bugs so the RL training loop is logically and numerically sound.
Fill in missing key steps to make it closer to a correct GRPO implementation.
Explain your changes: what issue each change addresses (e.g., shapes/masks, gradient flow, probability computation, advantage/baseline, normalization).
Context (LM RL loop)
A typical loop includes:
Sampling/generating multiple candidate responses from policy for the same prompt (forming a group)
Scoring each response with a reward_model (or a reward function)
Constructing a relative advantage within each group (group baseline / relative comparison)
Computing a policy-gradient-style objective (logprobs, ratio, clipping, KL penalty, masking, length normalization)
Backpropagating and updating policy
Common issues to check/improve (the provided code will exhibit some of these)
Correct log-prob computation (token-level vs sequence-level, shift alignment, gather, padding/masking)
Reward/advantage construction (group baseline, standardization, detach, not treating reward as differentiable)
Correct ratio/clip implementation (cached old-policy logprobs, computing old logprobs under no_grad)
KL penalty (KL to a reference model, correct sign, token-mask averaging)
Numerical stability (logsumexp, avoiding exp overflow, grad clipping, NaN handling)
Consistency between sampling and training (sampling params and the tokens whose logprobs are trained)
Batch/group dimension handling (reshape/aggregate over group samples per prompt)
Expected output
Provide the fixed key code snippets (or describe precisely how you would change them)
Explain what you fixed/added and why
Constraints
Use PyTorch
You may consult PyTorch documentation
Assume input scale: batch size B, group size G samples per prompt, max generation length T, vocab size V
Note: The original interview post did not include the concrete code/I/O, so this is a cleaned-up, answerable version of the prompt.