Potential GRPO NaN from overflowed log-ratios at masked completion tokens
Potential GRPO numerical edge case (needs native-runtime confirmation)
At swift/rlhf_trainers/grpo_trainer.py in _compute_loss_and_metrics, torch.exp(per_token_logps - old_per_token_logps) is computed before multiplying the per-token PPO loss by completion_mask. An extreme log-ratio at an excluded token can become infinity; the later inf * 0 operation yields NaN.
Minimal CPU reproduction
The example below reproduces the upstream expressions with PyTorch 2.10 CPU; it is not an import of the installed ms-swift trainer.
import torch
new = torch.tensor([[-1., -1.]], requires_grad=True)
old = torch.tensor([[-101., -1.]])
advantages = torch.tensor([[-1., 1.]])
mask = torch.tensor([[0., 1.]])
ratio = (new - old).exp()
loss1 = ratio * advantages
loss2 = ratio.clamp(0.8, 1.2) * advantages
loss = (-torch.min(loss1, loss2) * mask).sum() / mask.sum()
loss.backward()
print(loss, new.grad) # loss NaN; excluded-token gradient NaN
Selecting zero for excluded log-ratios with torch.where(mask.bool(), new - old, 0) before exp gives finite loss -1.0, zero gradient for the excluded token, and the original valid-token PPO result.
Scope and next step
This is a potential numerical issue, not a confirmed production training failure. I have not established whether upstream padding or rollout preprocessing already rules out this input state. Could maintainers confirm? If the state is possible, I can submit a focused patch and regression test following the first-time-contributor issue-before-PR workflow.
QuotientGuard provenance: I used QuotientGuard's explicit synthetic coefficient controls during a 10-repository numerical audit (https://github.com/ot4562-glitch/QuotientGuard). This is not native ms-swift runtime capture or automated certification (reconstruction_validation=false). The independent PyTorch CPU reproduction is the evidence.