Skip to content

Keep Muon's update at full size under fp16 loss scaling - #8655

Merged
delock merged 3 commits into
deepspeedai:masterfrom
alanhuangyoo:fix/muon-fp16-loss-scale
Sep 30, 2026
Merged

delock merged 3 commits into
deepspeedai:masterfrom
alanhuangyoo:fix/muon-fp16-loss-scale

Conversation

@alanhuangyoo

Copy link
Copy Markdown
Contributor

Under fp16, Muon parameters barely train with ZeRO-1/2/3 unless the optimizer is offloaded. Newton-Schulz returns the same update at any loss scale, but the step then treats it like a scaled gradient: unscale_and_clip_grads divides it by the loss scale, and the global norm takes it as scaled too. So a Muon parameter moves by 1/loss_scale of its update, 1/65536 with the default dynamic loss scaler.

On 2×H20, the norm of one Muon step on a small model, static loss scale 1 vs 1024, no clipping:

scale 1 scale 1024
ZeRO-1/2/3, master 4.47e-2 4.37e-5
ZeRO-1/2/3, this PR 4.47e-2 4.47e-2
ZeRO-1/2/3 with CPU offload, master 4.47e-2 4.47e-2

And 60 steps of a small regression with the default fp16 config (dynamic loss scale, clipping 1.0), ZeRO-1 and ZeRO-3 alike:

loss at step 0 → 59
bf16 0.403 → 0.295
fp16, master 0.403 → 0.385
fp16, this PR 0.403 → 0.295

It's the same with gradient accumulation (gas=2: bf16 and this PR 0.362 → 0.320, master 0.362 → 0.418), and with an initial scale of 2^30 that overflows for the first steps this PR trains normally afterwards with no NaN.

The CPU-offload path from #8464 already handles this: it unscales before Newton-Schulz and scales the update back before the norm. Without offload the update sits in the fp16 gradient buffers, where scaling it back up could overflow, so this does it after the cast to fp32, for the norm and before unscale_and_clip_grads. In the 2^30 run the dynamic scaler settles at 2^22, so scaling the update up in fp16 would overflow it. The momentum keeps the scaled gradient as before, which in fp16 keeps small gradients out of the subnormal range. An invalid group norm stays at its -1 sentinel. bf16 and fp32 have a loss scale of 1 and don't reach the new code.

TestMuonLossScaling, next to the offload test, checks ZeRO-1/2/3 with clipping on: on master the update under scale 1024 is 3e-3 of the one under 1, here they match. The Muon tests, the fp16 dynamic loss scale tests and the ZeRO cases of test_fp16.py pass on 2×H20 (294 passed, 8 skipped).

Not covered: ZenFlow and SuperOffload, which have their own step. Also, with fp16 the momentum is in scaled units here but unscaled under offload, so a universal checkpoint moved between the two would carry it off by the loss scale; that was already the case before this.

@ghost ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Independent two-GPU validation on RTX 5090 (Python 3.12.13, PyTorch 2.13.0+cu130):

Revision TestMuonLossScaling Observed update at loss scale 1024 vs 1
Base 3571027338c85c3f2d74c896886bebf868680bad ZeRO-1 and ZeRO-3 failed linears.0.weight update ratio was 3.135e-03 in both stages.
Head ca279b08a8bc102353aa4ad0c7bc0a8723ba5179 ZeRO-1, ZeRO-2, and ZeRO-3 passed The regression's scale-invariance assertions passed.

This reproduces the original under-scaled Muon update on the base and validates the targeted fix on the current head. The run used the PR's exact changed ZeRO files and test over installed DeepSpeed 0.19.7. For ZeRO-3, the isolated test tree omitted an obsolete zeropp_loco_param argument in the installed engine's constructor call; the same compatibility adjustment was used for the base and head. Dynamic scaling, overflow recovery, and checkpoint behavior were outside this run.

@delock
delock self-requested a review September 28, 2026 14:39
Comment thread deepspeed/runtime/zero/stage3.py Outdated
carry the scale that the norm and unscale_and_clip_grads assume. Optimizer offload scales
it back itself; otherwise step() does.
"""
return (self.use_muon and not self.offload_optimizer and self.loss_scale != 1.0

@delock delock Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This condition is tightly coupled to implementation, which makes it fragile. Especially the offload_optimizer part. Do you think we can make offload optimizer tag the subgroup with (has_loss_scale) then check for this flag instead of offload_optimizer? Alternative could be let the path where loss scale will be lost to tag the subgroup with lack_loss_scale, you can evaluate these two options and pick a better one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Went with the second option. The path that writes the Muon update without loss scale now tags its sub-group, and step only scales the tagged ones back, so the offload_optimizer check is gone. Stage 1/2 do the same per group.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Dynamic loss-scale increases can mix pre-update and post-update scales during norm calculation and clipping.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Ensures non-offloaded Muon updates remain invariant under fp16 loss scaling across ZeRO stages.

Changes:

  • Rescales Muon norms and fp32 updates before standard unscaling.
  • Adds loss-scale invariance tests for ZeRO-1/2/3.
  • Preserves invalid norm sentinels.
File Description
deepspeed/​runtime/​zero/​stage_1_and_2.py Rescales Muon updates and norms.
deepspeed/​runtime/​zero/​stage3.py Applies equivalent ZeRO-3 handling.
tests/​unit/​v1/​ops/​muon/​test_muon.py Tests static loss-scale invariance.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +2482 to +2484
if self._muon_update_lacks_loss_scale(i):
# Leave the -1 an invalid norm is masked to as it is.
norm = torch.where(norm >= 0, norm * self.loss_scale, norm)
Comment on lines +2618 to +2620
if self._muon_update_lacks_loss_scale(i):
# Leave the -1 an invalid norm is masked to as it is.
norm = torch.where(norm >= 0, norm * self.loss_scale, norm)
Newton-Schulz returns the same update at any loss scale, but without
optimizer offload ZeRO-1/2/3 then treated it like a gradient: the global
norm took it as scaled and unscale_and_clip_grads divided it by the loss
scale. Under fp16 a Muon parameter moved by 1/loss_scale of its update
(1/65536 by default) and effectively stopped training. Scale it back in
fp32 for both, as the CPU-offload path already does.

Signed-off-by: alanhuangyoo <[email protected]>
@alanhuangyoo
alanhuangyoo force-pushed the fix/muon-fp16-loss-scale branch from ca279b0 to 6c0de31 Compare September 29, 2026 08:38
Comment thread deepspeed/runtime/zero/stage3.py Outdated
"""
if not self.use_muon:
return
self.sub_groups_lacking_loss_scale.clear()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

there is a clear in stage 3, why in stage 1 and 2 there is no clear? What is the reason we need a clear here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No reason, it was left over. The set has the same members every step so it doesn't need clearing, and stage 1/2 don't have one either. Removed it.

@delock
delock enabled auto-merge September 30, 2026 01:18
@delock
delock added this pull request to the merge queue Sep 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 30, 2026
@delock
delock added this pull request to the merge queue Sep 30, 2026
Merged via the queue into deepspeedai:master with commit 3509874 Sep 30, 2026
13 checks passed
@tohtana tohtana added needs-gpu-test GPU testing is recommended to validate the changes in this PR. and removed needs-gpu-test GPU testing is recommended to validate the changes in this PR. labels Nov 10, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
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. will-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants