Repository navigation
Fix Muon momentum across ZeRO loss-scale changes - #9137
taking-lying-flat wants to merge 3 commits into
Conversation
Signed-off-by: taking-lying-flat <1615405@qq.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7d5c3651d7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| scale = self.loss_scale | ||
| previous = getattr(self.loss_scaler, "muon_momentum_scale", scale) | ||
| if previous != scale: | ||
| momentum.mul_(scale / previous) |
There was a problem hiding this comment.
Prevent rebasing overflow from poisoning ZeRO-3 momentum
For non-offloaded FP16 ZeRO-3, the gathered momentum is FP16, so doubling the loss scale can overflow this multiplication even when the raw gradient and correctly blended momentum are finite. For example, momentum 34560 becomes inf, although blending its rebased value with gradient -10000 at the default beta yields 65164, below FP16's limit. muon_update guards only the gradient's finiteness, and _muon_update_sub_group writes the resulting non-finite momentum into optimizer state before the norm check rejects the step. Subsequent steps therefore keep overflowing despite loss-scale backoff, eventually terminating the run. Perform the rebase and blend in a wider/unscaled representation, or preserve the prior committed momentum when this overflow rejects the step.
Useful? React with 👍 / 👎.
Signed-off-by: taking-lying-flat <1615405@qq.com>
Signed-off-by: taking-lying-flat <1615405@qq.com>
Summary
Muon with ZeRO-1/2/3 and FP16 loss scaling can move a weight in the wrong direction after the scale grows. The gradient partition contains the current scaled gradient, while the momentum buffer still uses the previous backward's scale. Mixing them changes the direction before Newton–Schulz orthogonalization.
When the scale changes, multiply the previous momentum by
current_backward_scale / saved_momentum_scaleand blend it with the current gradient in at least FP32. Cast the blend back to the buffer dtype before Nesterov uses it. ZeRO-1/2/3 commit the new momentum and its backward scale only after the overflow checks pass. This lets a skipped step retry with its original history.The scale tag travels with the optimizer checkpoint. For an older native checkpoint saved immediately after scale growth, the scaler's counters identify the scale used by the last successful backward. Earlier contributions already mixed at different scales cannot be recovered. Optimizer offload already unscales gradients before the Muon update.
Reproduction
Use a 2×2 weight, default Muon momentum and Nesterov, learning rate 1/64, and zero clipping and weight decay. Supply gradients
I, I, diag(-0.75, 1). With an initial loss scale of 16 and a growth window of one, the three backwards use16, 16, 32without overflow.Related work: #8655 compensates the final orthogonalized update, #8662 addresses the scale used to unscale the current step, and #8435 handles discarded steps. This change addresses the units of momentum carried between successful steps.
I used AI assistance and personally reviewed the code changes and test results throughout.