verl
verl copied to clipboard
[Bug] RuntimeError when computing metrics on empty tensors in compute_data_metrics
Description
When all samples are aborted or response_mask is all False, calling torch.max(), torch.min(), torch.mean() on empty tensors raises:
RuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the 'dim' argument.
Root Cause
In verl/trainer/ppo/metric_utils.py, the compute_data_metrics function performs tensor reduction operations without checking if the tensors are empty:
non_aborted_sequence_score/non_aborted_sequence_reward- empty when all samples are abortedvalid_adv/valid_returns- empty whenresponse_maskis allFalse
Steps to Reproduce
This can occur in edge cases where:
- All sequences in a batch are aborted during generation
- The
response_maskcontains onlyFalsevalues (no valid response tokens)
Proposed Solution
Add explicit numel() > 0 checks before performing reduction operations, and raise descriptive ValueError messages:
if non_aborted_sequence_score.numel() > 0:
score_mean = torch.mean(non_aborted_sequence_score).detach().item()
score_max = torch.max(non_aborted_sequence_score).detach().item()
score_min = torch.min(non_aborted_sequence_score).detach().item()
else:
raise ValueError("All samples are aborted, cannot compute score metrics.")
Similar checks should be added for:
non_aborted_sequence_reward→ reward metricsvalid_adv→ advantage metricsvalid_returns→ return metrics
Related
- PR #5860 addressed a similar issue in
verl/utils/debug/metrics.pyforcalculate_debug_metrics
Environment
- verl version: latest main branch
- PyTorch version: 2.x
Closing to re-create with proper format.