[Bug] RuntimeError when computing metrics on empty tensors in compute_data_metrics
System Info
- verl: 0.7.0
- python: 3.11
- pytorch: 2.9.0
Information
- [ ] The official example scripts
- [x] My own modified scripts
Tasks
- [ ] An officially supported task in the
examplesfolder (such as GLUE/SQuAD, ...) - [x] My own task or dataset (give details below)
Reproduction
When all samples are aborted or response_mask is all False, compute_data_metrics crashes with:
Error Message:
RuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the 'dim' argument.
Root Cause:
verl/trainer/ppo/metric_utils.py performs torch.max() / torch.min() operations without checking if tensors are empty:
# Line 121-130
non_aborted_sequence_score = sequence_score[non_aborted_mask]
non_aborted_sequence_reward = sequence_reward[non_aborted_mask]
score_mean = torch.mean(non_aborted_sequence_score).detach().item()
score_max = torch.max(non_aborted_sequence_score).detach().item() # RuntimeError when empty!
score_min = torch.min(non_aborted_sequence_score).detach().item() # RuntimeError when empty!
reward_mean = torch.mean(non_aborted_sequence_reward).detach().item()
reward_max = torch.max(non_aborted_sequence_reward).detach().item() # RuntimeError when empty!
reward_min = torch.min(non_aborted_sequence_reward).detach().item() # RuntimeError when empty!
# Line 132-133 and later in metrics dict
valid_adv = torch.masked_select(advantages, response_mask)
valid_returns = torch.masked_select(returns, response_mask)
# Later used in metrics dict:
"critic/advantages/mean": torch.mean(valid_adv).detach().item(),
"critic/advantages/max": torch.max(valid_adv).detach().item(), # RuntimeError when empty!
"critic/advantages/min": torch.min(valid_adv).detach().item(), # RuntimeError when empty!
"critic/returns/mean": torch.mean(valid_returns).detach().item(),
"critic/returns/max": torch.max(valid_returns).detach().item(), # RuntimeError when empty!
"critic/returns/min": torch.min(valid_returns).detach().item(), # RuntimeError when empty!
non_aborted_sequence_score/non_aborted_sequence_reward- empty when all samples are abortedvalid_adv/valid_returns- empty whenresponse_maskis allFalse
Expected behavior
Goal: Replace crash with warning + NaN, so that training can continue gracefully in edge cases.
This is important for:
- System debugging: Early training runs may produce invalid responses; crashing blocks iteration
- Unique sampling strategies: Some strategies may occasionally produce batches where all responses are invalid
Instead of crashing the entire training job, log a warning and return NaN metrics:
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:
logger.warning("All samples are aborted, returning default score metrics")
score_mean = score_max = score_min = float("nan")
Same pattern for reward, advantage, and return metrics.
Impact on training accuracy: This fix does not affect training accuracy for two reasons:
compute_data_metricsis purely an observation/logging function — its return values do not feed into loss computation, so NaN metrics have no effect on training dynamics- When all responses are invalid, the loss computation ensures no parameter updates occur:
agg_lossusesmasked_sum(loss, mask) / (num_tokens + 1e-8), which produces loss = 0 and gradient = 0
Related
- Issue #5859 / PR #5860 addressed a similar issue in
verl/utils/debug/metrics.py
Thank you for the detailed bug report and analysis.
This is a well-documented issue. The proposed fix (returning NaN instead of crashing) makes sense for the following reasons:
-
No impact on training accuracy: As you correctly noted,
compute_data_metricsis purely for logging/observation and does not feed into loss computation. -
Common edge cases: Early training iterations or certain sampling strategies can produce batches where all responses are invalid, which shouldn't crash the entire training job.
Implementation notes:
The suggested pattern is clean and maintainable:
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:
score_mean = score_max = score_min = float("nan")
This should be applied to all four affected metric groups: score, reward, advantages, and returns.
Would you be interested in submitting a PR for this fix?
Given your clear understanding of the issue and proposed solution, a PR would be welcome. The fix should:
- Add the
numel()check before max/min operations - Include appropriate warning logs
- Add tests for edge cases (empty tensors)
Related PR #5899 appears to have addressed a similar issue in compute_data_metrics for empty tensors. You may want to check if that PR covers this case or if additional handling is needed.
Thanks for the review and suggestions! I'm updating the code and running tests in PR #5899. Will push for merge once everything is verified.