Skip to content

Stage Muon's momentum once per partition in ZeRO-1/2 - #8633

Merged
pengdurice merged 2 commits into
deepspeedai:masterfrom
alanhuangyoo:fix/muon-stage-momentum-once
Sep 24, 2026
Merged

pengdurice merged 2 commits into
deepspeedai:masterfrom
alanhuangyoo:fix/muon-stage-momentum-once

Conversation

@alanhuangyoo

Copy link
Copy Markdown
Contributor

#8435 (mine) broke ZeRO-1/2 Muon: every matrix except the last one in each partition loses its momentum.

_muon_staging_momentum copies the committed momentum into the staging buffer, and it was called inside the per-parameter loop of get_flat_partition / _get_flat_partition_unpadded. So each parameter wiped what the ones before it had just written, and only the last one's momentum got committed. Nothing errors and the loss still goes down. This stages once per partition, before the loop.

Four matrices on 2×H20, fp32, the same batch on every rank, clipping off, 3 steps. ZeRO-0 applies Muon per parameter inside the optimizer, so it is the reference. Relative weight difference to ZeRO-0, per matrix:

master this PR
ZeRO-1 2.0e-3 to 3.4e-2 0
ZeRO-2 2.0e-3 to 3.4e-2 0
ZeRO-3 0 0

The #8435 invariant still holds: staging still starts from the committed momentum, so a step the loss scaler throws away leaves every momentum where it was.

Tests:

  • New TestMuonMatchesZeroStage0 compares ZeRO-1/2 with ZeRO-0 after three steps. Fails on master (3.5e-2), passes here.
  • The step-0 check in test_a_discarded_step_leaves_every_momentum_where_it_was was vacuous. The buffer does not exist before the first step, so it compared None with the new norms. It now requires both momenta to be non-zero, and fails on master, where the first matrix stays at exactly 0.
  • tests/unit/v1/ops/muon and tests/unit/runtime/zero/test_per_head_muon.py: 292 passed.

Clipping is off in the comparison because the default clipping scales the Muon update by its own norm (#8439), and that norm is taken over different tensors at each stage.

_muon_staging_momentum copies the committed momentum into the staging
buffer, and it was called inside the per-parameter loop of
get_flat_partition and _get_flat_partition_unpadded. Each call wiped what
the parameters before it had just written, so only the last Muon matrix of
each partition kept its momentum. Stage once, before the loop.

The step-0 check in test_a_discarded_step_leaves_every_momentum_where_it_was
compared against a momentum buffer that does not exist before the first
step, so it held whatever happened. It now requires both momenta to be
non-zero. TestMuonMatchesZeroStage0 compares ZeRO-1/2 against ZeRO-0, which
applies Muon per parameter in the optimizer.

Signed-off-by: alanhuangyoo <[email protected]>
@alanhuangyoo

Copy link
Copy Markdown
Contributor Author

@pengdurice Thanks for setting auto-merge! It's still waiting on an approving review, in case that got missed.

@pengdurice
pengdurice added this pull request to the merge queue Sep 24, 2026
Merged via the queue into deepspeedai:master with commit 1355e9d Sep 24, 2026
13 checks passed
@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
meefs pushed a commit to meefs/DeepSpeed that referenced this pull request Nov 15, 2026
On runners whose CPU supports oneDNN fp16 (Sapphire Rapids or newer),
fp16 tests that normally skip start running, and Muon's Newton-Schulz
switches to fp16. `cpu-torch-latest-v1-multirank` then fails 10 tests,
e.g. runs 43556673085 and 43396382279 (identical values). All fixes are
test-only:

- **`tests/unit/v1/zero/test_zero_autocast.py`** (8 tests): the baseline
`torch.amp.GradScaler()` defaults to CUDA and disables itself on CPU, so
the baseline trained fp16 without loss scaling. It is now created on
`get_accelerator().device_name()`; CUDA is unchanged.
- **`tests/unit/v1/ops/muon/test_muon_overflow.py`** (2 tests): ZeRO-0
and ZeRO-1/2 run fp16 Newton-Schulz through different compiled graphs,
which land ~1e-3 apart. Since that comes from the code under test, the
bound is now derived from `ns_compute_dtype("gram")`: 1e-5 for fp32 as
before, 7.8e-3 for fp16 (also on CUDA, where it was bit-exact). The test
still catches the deepspeedai#8633 bug, which moves matrices 2e-2 to 3.5e-2 away
from ZeRO-0.
- **`tests/unit/v1/zero/test_zero.py`**
(`TestGradientAllreduceOpTraining`, 2 Muon cases, not hit by CI today):
SUM halves an unscaled fp16 loss, which rounds subnormal gradients, and
Newton-Schulz grows that past the tolerance. It fails on torch 2.10 and
in 6 of 32 seeded runs on 2.14. A static `loss_scale` of 1024 makes MEAN
and SUM bit-identical in all 32; the tolerance is unchanged.

Signed-off-by: Hongwei Chen <[email protected]>
Co-authored-by: Olatunji Ruwase <[email protected]>
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.

2 participants