Skip to content

Log GRPO loss-path metrics once per optimizer window - #7053

Closed
behroozazarkhalili wants to merge 3 commits into
mainfrom
fix/7047-grpo-window-aggregation
Closed

behroozazarkhalili wants to merge 3 commits into
mainfrom
fix/7047-grpo-window-aggregation

Conversation

@behroozazarkhalili

@behroozazarkhalili behroozazarkhalili commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

GRPO gave longer gradient accumulation windows more weight when averaging loss metrics. I changed GRPO and its GMPO and GSPO-token copies to record one value per optimizer window, and tests for equal and uneven windows pass. This overlaps #7042 on the same lines and will need a small conflict resolution.
Fixes #7047.


Note

Low Risk
Changes only how training metrics are aggregated for logging; optimization math is explicitly unchanged. Clip-ratio and other per-micro-batch diagnostics are untouched.

Overview
Fixes incorrect logged policy_loss, entropy, kl, and aux_loss when gradient accumulation uses uneven windows (e.g. a shortened last window), while leaving the actual training loss and gradients unchanged.

In train mode, micro-batch metric values are now scaled by 1 / current_gradient_accumulation_steps, summed in _loss_window_metrics, and appended to _metrics only when accelerator.sync_gradients is true—so log()’s mean is one value per optimizer step with equal weight per window. Eval still logs per batch. train() clears the accumulator at the start to avoid stale partial sums after an interrupted run. The same logging block is applied in the GMPO and GSPO-token GRPO trainer copies.

Adds test_policy_loss_is_averaged_over_accumulation_windows to assert window-weighted averaging vs the old flat micro-batch mean.

Reviewed by Cursor Bugbot for commit 0c3d4dc. Bugbot is set up for automated code reviews on this repo. Configure here.

_compute_loss appended entropy, kl, policy_loss and aux_loss to
self._metrics once per micro-batch and log() took a flat mean over the
entries of the logging interval. transformers shortens the last
accumulation window of an epoch to the remainder, so an interval that
mixes window sizes weighted each window by its micro-batch count: with
gradient_accumulation_steps=3 over four micro-batches policy_loss
logged -0.000557 where the mean over the two optimizer windows is
-0.001114.

In train mode each metric's micro-batch contribution is now divided by
the current window size and accumulated, and the window value is
appended once when the accelerator syncs gradients, so the entry count
equals the optimizer-step count and log()'s mean is exact. Equal
windows log the same numbers as before. Evaluation has no accumulation
and keeps appending per batch. train() clears the window accumulator
on entry so a run interrupted inside a window cannot leak into the
next call. The training loss and gradients are unchanged.

The GMPO and GSPO-token copies of the block are updated identically;
MiniLLM and the replay-buffer trainer inherit the method.

The regression test drives two windows of sizes 3 and 1 through
_compute_loss and asserts one entry per window with the per-window
mean; the equal-window case asserts the old and new values coincide.
On the previous code the uneven case logs -1.75 for an expected -2.5.

Fixes #7047.
@bot-ci-comment

bot-ci-comment Bot commented Sep 4, 2026

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@behroozazarkhalili

Copy link
Copy Markdown
Collaborator Author

Closing this one myself. It came from reading the code, not from a failure in a real training run, and per the discussion on #6862 that isn't the bar for this repo. If someone hits this in practice, the branch is still here and I'm happy to reopen it with a reproduction.

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.

GRPO loss-path metrics average micro-batch entries, so uneven accumulation windows are weighted by their size

1 participant