Repository navigation
Keep Muon's update at full size under fp16 loss scaling - #8655
Conversation
ghost
left a comment
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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.
| 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) |
| 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]>
…erring it Signed-off-by: alanhuangyoo <[email protected]>
ca279b0 to
6c0de31
Compare
| """ | ||
| if not self.use_muon: | ||
| return | ||
| self.sub_groups_lacking_loss_scale.clear() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Signed-off-by: alanhuangyoo <[email protected]>

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_gradsdivides 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:
And 60 steps of a small regression with the default fp16 config (dynamic loss scale, clipping 1.0), ZeRO-1 and ZeRO-3 alike:
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 oftest_fp16.pypass 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.