verl
verl copied to clipboard
[Bug][P0] Cross-sequence `torch.roll` corrupts log_prob at batch boundaries in `log_probs_from_logits_response_rmpad`
System Info
Discovered via static code analysis. Reproducible with default config (use_remove_padding=True, batch_size > 1). No special hardware required to reproduce.
Information
- [x] The official example scripts
Tasks
- [x] An officially supported task in the
examplesfolder (such as GLUE/SQuAD, ...)
Reproduction
import torch
# Two packed sequences: [A0, A1, A2, B0, B1] (cu_seqlens = [0, 3, 5])
input_ids_rmpad = torch.tensor([10, 11, 12, 20, 21])
# Current code — torch_functional.py line 632
rolled = torch.roll(input_ids_rmpad, shifts=-1, dims=0)
# → [11, 12, 20, 21, 10]
# BUG: index 2 (last token of seq A) → label 20 (first token of seq B) ✗
# BUG: index 4 (last token of seq B) → label 10 (first token of seq A) ✗
Same pattern in log_probs_from_logits_all_rmpad (line 663).
Expected behavior
The shift used to construct next-token labels must respect sequence boundaries. After the roll, the last token of sequence i should not receive the label of the first token of sequence i+1.
Correct approach — shift within each sequence using cu_seqlens:
# Option A: unpad → shift along seq dim → re-gather
padded = unpad_to_padded(input_ids_rmpad, cu_seqlens, max_seqlen) # [B, L]
labels = padded[:, 1:] # shift within each row, no leakage across rows
# Option B: per-sequence slice
labels_list = [input_ids_rmpad[start+1:end] for start, end in zip(cu_seqlens[:-1], cu_seqlens[1:])]
labels = torch.cat(labels_list)
Impact
- Silent correctness bug — no error or NaN; training appears to run normally.
- Every log-prob at a sequence boundary position is computed against the wrong target token.
- Directly biases PPO/GRPO policy gradients and importance-sampling ratios.
- Affects every run with
use_remove_padding=True(default) and batch size > 1.