Skip to content

Fix Muon momentum across ZeRO loss-scale changes - #9137

Open
taking-lying-flat wants to merge 3 commits into
deepspeedai:masterfrom
taking-lying-flat:fix/muon-zero-momentum-loss-scale
Open

taking-lying-flat wants to merge 3 commits into
deepspeedai:masterfrom
taking-lying-flat:fix/muon-zero-momentum-loss-scale

Conversation

@taking-lying-flat

@taking-lying-flat taking-lying-flat commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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_scale and 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 use 16, 16, 32 without overflow.

Third-step change in FP32 weight [0,0] ZeRO-1 ZeRO-2 ZeRO-3
Fixed scale 1 −0.014892578125 −0.014892578125 −0.014892578125
Dynamic scale, before +0.01129150390625 +0.01129150390625 +0.01129150390625
Dynamic scale, after −0.014892578125 −0.014892578125 −0.014892578125

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.

Signed-off-by: taking-lying-flat <1615405@qq.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T12:45:27.563247Z 7d5c365 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 20, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread deepspeed/runtime/base_optimizer.py Outdated
scale = self.loss_scale
previous = getattr(self.loss_scaler, "muon_momentum_scale", scale)
if previous != scale:
momentum.mul_(scale / previous)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 20, 2026
Signed-off-by: taking-lying-flat <1615405@qq.com>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 20, 2026

This branch has not been deployed

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

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant