verl icon indicating copy to clipboard operation
verl copied to clipboard

[FR] Better metric reduction utility

Open tongyx361 opened this issue 1 year ago • 5 comments

Motivation

reduce_metrics seems to be used for reducing metrics across DP ranks and batches, which only simply means the values.

However, reducing metrics across DP ranks is non-trivial since some metrics like "mean" are not linear.

tongyx361 avatar May 13 '25 18:05 tongyx361

I've submitted PR #2990 to address this issue.

Implementation Summary

The solution adds proper weighted averaging support to handle non-linear metrics across distributed ranks:

Key Changes:

  1. New function: Properly calculates weighted averages when ranks have different sample counts
  2. Enhanced function with:
    • Optional parameter for weighted averaging
    • Optional parameter for custom reduction methods
    • Maintains full backward compatibility

Example Usage:

# Scenario: 2 DP ranks with different batch sizes
metrics = {
    "mean_accuracy": [0.8, 0.6],  # Rank 1: 80%, Rank 2: 60%
}
weights = {
    "mean_accuracy": [100, 200],  # Rank 1: 100 samples, Rank 2: 200 samples  
}

# Correct weighted average
result = reduce_metrics(metrics, weights=weights)
# Result: 0.6667 (correct) instead of 0.7 (incorrect simple average)

Design Decisions:

  • Preserved existing string pattern matching behavior for backward compatibility
  • Users can override auto-detection using the parameter when needed
  • All existing code continues to work without modifications

The implementation includes comprehensive tests and maintains full backward compatibility.

liqiongyu avatar Aug 09 '25 06:08 liqiongyu

Just sharing my take after looking at the code.

I don’t think the module itself necessarily needs a big change. Metric(MEAN) is not inherently DP-unsafe — it’s fine as long as the values appended are sample-level.

The real issue seems to be ambiguous usage patterns, especially:

appending batch- or rank-level means into Metric(MEAN), and

calling np.mean on raw lists inside reduce_metrics, which silently assumes equal weights.

Since reduce_metrics has no context about batch size or sample counts, it can’t really know whether a mean reduction is valid in DP settings. That’s why doing an automatic mean there feels risky.

From a code-review perspective, I think a reasonable improvement would be to add asserts or warnings in reduce_metrics to discourage mean-on-list, and make it clearer that mean aggregation should be handled explicitly via Metric, where the caller has the necessary context.

This keeps the existing structure largely intact while reducing the chance of silent incorrect aggregation.

If you think it's reasonable, I will add code as I say before.

GGMin avatar Jan 28 '26 11:01 GGMin

Hi @tongyx361, I audited the current state on main and would like to pick this up if still wanted.

Since this issue was filed, the Metric class + AggregationType + Metric.aggregate_dp (verl/utils/metric/utils.py) have landed and solve the mean-of-means concern for metrics that migrated to Metric (e.g. pg_loss/entropy_loss/kl_loss). PR #2990 also attempted weighted averaging, but it predates the Metric class and would now conflict with it (its rewritten reduce_metrics has no Metric branch).

Remaining gaps I found:

  1. No weighted aggregation: per-microbatch means with unequal token/sample counts still reduce as unweighted mean-of-means. Proposal: add AggregationType.WEIGHTED_MEAN — Metric stores (value, weight) pairs, aggregate() and aggregate_dp() compute Σ(w·v)/Σw, with weight defaulting to 1.0 so existing behavior is unchanged.

  2. The substring heuristic in reduce_metrics ("min" in key) silently mis-reduces keys like ".../minibatch...". Could tighten to a suffix convention or rely on explicit Metric aggregation.

  3. Legacy call sites (ray_trainer.py, main_ppo_sync.py) still pass raw lists through the heuristic path.

I'd like to start with (1) as a small PR with unit tests (no GPU needed), building on the Metric API rather than #2990's parameter-dict approach. Does that direction sound right?

chengcuiping avatar Jun 12 '26 02:06 chengcuiping

中文:我不建议现在关闭这个 issue。当前 main 已经引入 Metric / AggregationType / Metric.aggregate_dp(见 PR #4778 和 verl/utils/metric/utils.py),这部分缓解了“不同指标需要不同聚合策略”的问题;但 reduce_metrics 的 raw-list fallback 仍然按 key substring 选择 max/min,否则使用 np.mean,并且 AggregationType 目前只有 MEAN/SUM/MIN/MAX,没有权重或 sample-count 携带机制。因此,issue 中“跨 DP rank 的 mean 可能不是线性/等权可约”的核心诉求仍未完全解决。#2990 提供过 weighted averaging 方向,但它仍是 open,且当前 Metric API 落地后需要基于现有设计重新对齐。保守建议:保持 open,并把后续 PR 聚焦在显式 weighted mean/权重携带机制,或至少文档化 raw-list mean fallback 的等权假设。

English: I would not close this issue yet. Current main has introduced Metric / AggregationType / Metric.aggregate_dp (see PR #4778 and verl/utils/metric/utils.py), which partially addresses per-metric aggregation strategy. However, the raw-list fallback in reduce_metrics still chooses max/min by substring and otherwise uses np.mean, and AggregationType currently only has MEAN/SUM/MIN/MAX without a weight/sample-count carrying mechanism. So the original concern that mean-like metrics are not safely reducible across DP ranks by an unweighted mean is still not fully resolved. PR #2990 explored weighted averaging, but it remains open and would need to be reconciled with the Metric API now on main. Conservative recommendation: keep this open and make any follow-up PR explicit about weighted mean or documented equal-weight assumptions for the raw-list fallback. This response was generated by a scheduled Codex GPT5.5 issue processing task.

Kirrito-k423 avatar Jul 06 '26 01:07 Kirrito-k423

Hi — I audited the current state on main and would like to take this on, if it's still wanted.

Since this issue was filed, Metric / AggregationType / Metric.aggregate_dp landed (#4778), addressing the mean-of-means concern for metrics migrated to Metric. Two gaps remain, consistent with @Kirrito-k423's note above:

  1. No weight-carrying mechanism: per-microbatch means with unequal sample/token counts are still averaged with equal weights in both Metric.aggregate() and aggregate_dp() — DP ranks with unequal microbatches skew MEAN/SUM.
  2. The raw-list fallback in reduce_metrics silently assumes equal weights.

Proposed plan (aligned with the current Metric API, unlike the stale #2990): optional per-value weights on Metric (weighted MEAN via np.average), per-rank sample-count weighting in aggregate_dp, and documenting the equal-weight fallback assumption. Backward compatible; CPU-testable.

Happy to proceed if maintainers agree on the direction.

LCode121 avatar Jul 24 '26 19:07 LCode121