[Bug] Reward manager writes reward to index -1 when `valid_response_length == 0`
System Info
Discovered via static code analysis. Affects prime.py, naive.py, and dapo.py reward managers.
Information
- [x] The official example scripts
Tasks
- [x] An officially supported task in the
examplesfolder (such as GLUE/SQuAD, ...)
Reproduction
In verl/workers/reward_manager/prime.py line 177 (identical pattern in naive.py:100 and dapo.py:132):
reward_tensor[i, valid_response_length[i].item() - 1] = scores[i]
When valid_response_length[i] == 0 (empty generation, aborted rollout, or edge-case input), this becomes:
reward_tensor[i, -1] = scores[i] # writes to the LAST element of the row
Python/PyTorch silently accepts negative indices, so the reward is written to the wrong position with no error raised.
Expected behavior
When valid_response_length[i] == 0 there is no valid response token to attach a reward to. The code should either skip the assignment or raise an explicit error:
if valid_response_length[i].item() > 0:
reward_tensor[i, valid_response_length[i].item() - 1] = scores[i]
Impact
- Silent data corruption — reward placed at the wrong position, corrupting training signal.
- Affects all three reward managers:
prime.py,naive.py,dapo.py. - Triggered by empty/aborted generations which can occur during early training or with constrained sampling.
Related
Also in dapo.py line 121: self.overlong_buffer_cfg.enable crashes with AttributeError: 'NoneType' object has no attribute 'enable' when overlong_buffer_cfg=None (its default value). The fix is:
if self.overlong_buffer_cfg is not None and self.overlong_buffer_cfg.enable:
When valid_response_length == 0, it's ok to assign reward to last token.