[Bug] Advantage whitening fails with a single valid response token
Overview
Advantage computation can fail when response filtering leaves exactly one valid response token and either reward or advantage whitening is enabled.
Root cause
Both compute_advantage and agentic_compute_advantage disable whitening only when response_mask.sum() == 0. However, masked_whiten uses masked_var(..., unbiased=True), which requires at least two valid elements and raises ValueError when the mask sum is one.
The unbiased variance estimator applies Bessel's correction with a denominator of n - 1. For n = 1, that denominator is zero. The failure occurs while computing var, before masked_whiten evaluates torch.rsqrt(var + 1e-8), so the epsilon cannot prevent it.
Affected paths
- RLVR advantage computation
- Agentic advantage computation
- Reward whitening and advantage whitening
Expected behavior
When fewer than two valid response tokens remain, whitening should be skipped because an unbiased variance cannot be computed. Normal whitening behavior for two or more valid tokens should remain unchanged.
Acceptance criteria
- Skip reward and advantage whitening for zero or one valid response token.
- Apply the behavior consistently to RLVR and Agentic pipelines.
- Preserve the existing unbiased variance behavior for normal batches.
- Add regression coverage for zero, one, and multiple valid tokens in both pipelines.