verl icon indicating copy to clipboard operation
verl copied to clipboard

[Bug] RuntimeError when computing metrics on empty tensors in compute_data_metrics

Open Jackie2049 opened this issue 7 months ago • 1 comments

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:

  1. non_aborted_sequence_score / non_aborted_sequence_reward - empty when all samples are aborted
  2. valid_adv / valid_returns - empty when response_mask is all False

Steps to Reproduce

This can occur in edge cases where:

  • All sequences in a batch are aborted during generation
  • The response_mask contains only False values (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 metrics
  • valid_adv → advantage metrics
  • valid_returns → return metrics

Related

  • PR #5860 addressed a similar issue in verl/utils/debug/metrics.py for calculate_debug_metrics

Environment

  • verl version: latest main branch
  • PyTorch version: 2.x

Jackie2049 avatar Apr 07 '26 07:04 Jackie2049

Closing to re-create with proper format.

Jackie2049 avatar Apr 07 '26 07:04 Jackie2049