Skip to content

perf(grpo): batch rollout diagnostic metric gathering - #10082

Open
taking-lying-flat wants to merge 1 commit into
modelscope:mainfrom
taking-lying-flat:perf/grpo-rollout-metrics
Open

perf(grpo): batch rollout diagnostic metric gathering#10082
taking-lying-flat wants to merge 1 commit into
modelscope:mainfrom
taking-lying-flat:perf/grpo-rollout-metrics

Conversation

@taking-lying-flat

Copy link
Copy Markdown
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

When rollout log probabilities and IS correction are enabled, HF GRPO performs 13 individual gather_for_metrics() calls for off-policy diagnostics and three for IS diagnostics in each micro-batch. Each call is followed by a scalar read. These operations execute in the loss path rather than only when logging.

Keep the local diagnostic values as tensors and gather the combined metrics once per micro-batch. Preserve each metric's existing cross-rank nanmean, max, or min reduction. Packing one row per rank also preserves Accelerate's dataloader remainder slicing.

Retain the reduced tensors until log(), then copy them together and compute the logging-period means in CPU double precision, matching the range of the previous Python-float aggregation. Importance-sampling weights and their update frequency are unchanged.

This PR changes only swift/rlhf_trainers/grpo_trainer.py: 34 insertions and 23 deletions. Megatron and other diagnostic metrics are outside this change.

Experiment results

Per-micro-batch diagnostic operations Before After
gather_for_metrics() with IS correction 16 1
Scalar .item() reads 16 0

For 16 micro-batches, these diagnostic gathers decrease from 256 to 16. This is an operation-count reduction, not an end-to-end throughput measurement.

39 local two-rank Gloo comparisons of the production metric, loss, and logging methods passed. Coverage includes all four IS modes, diagnostics without correction, uneven local batch sizes, empty masks, NaNs, extrema, dataloader remainder trimming, train/eval logging, and low-precision/large-value logging means. Loss and gradients matched exactly; metric and logging values matched within floating-point tolerance. CPU profiling confirmed no aten::item operations in the updated diagnostic path.

All applicable pre-commit checks passed. Validation scripts remain outside the repository; this PR contains production code only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant