DeepSpeed
b5e000c4 - [muon] Keep the momentum out of steps the loss scaler discards (#8435)

Commit
72 days ago
[muon] Keep the momentum out of steps the loss scaler discards (#8435) Fixes #8432. ## Problem Muon under ZeRO 1/2 with fp16 does not train. Running the configuration `tests/unit/ops/muon/test_muon.py` itself uses, for longer than its five steps: ``` Exception: Current loss scale already at minimum - cannot decrease scale anymore. Exiting run. ``` No parameter is ever updated. The first loss-scale overflow is folded into Muon's momentum before the overflow check decides to discard the step, and the buffer stays non-finite for the rest of the run. The failure sustains itself because both halves of `muon_update` touch the gradient: ```python momentum.lerp_(grad, 1 - beta) # inf/nan enters the momentum update = grad.lerp_(momentum, beta) if nesterov # ...and is written back into grad ``` so the next step's gradient is already non-finite whatever the loss scale has been reduced to. Backing off cannot help. ## Fix The momentum stays out of a step whose gradient is not finite, and the non-finite gradient is still returned so the overflow is seen and the step skipped. **Both halves are needed.** Protecting the momentum alone is not enough: on the non-nesterov path `update` *is* the momentum, so a protected momentum would hand an overflowed step a finite update and the step would be applied rather than skipped. The non-finiteness has to keep propagating. Evaluated on device, so this costs no synchronization. ## Verification 30 steps, 2 × H20, ZeRO 1/2, `SimpleModel(hidden_dim=128, nlayers=5)`, `lr=0.05`: | stage | initial scale | master | this branch | | ---: | ---: | --- | --- | | 1 | 65536 (default) | 0/10 moved, momentum non-finite, **dies** | **10/10 moved, momentum finite** | | 2 | 65536 (default) | 0/10 moved, momentum non-finite, **dies** | **10/10 moved, momentum finite** | | 1 | 1 (no overflow) | 10/10 moved, finite | 10/10 moved, finite | | 2 | 1 (no overflow) | 10/10 moved, finite | 10/10 moved, finite | The scale-1 rows are the control: with no overflow there was never a problem, so the failure is entirely the overflow interaction rather than anything about Muon. ## Tests `tests/unit/ops/muon/test_muon_overflow.py`. Three of the four fail on the parent commit: ``` master 3 failed, 1 passed FAILED test_an_overflowed_gradient_does_not_enter_the_momentum - AssertionError: an overflowed step must not move the momentum FAILED test_training_recovers_from_the_initial_overflow[1] - Exception: Current loss scale already at minimum FAILED test_training_recovers_from_the_initial_overflow[2] - Exception: Current loss scale already at minimum this PR 4 passed ``` The unit-level case pins both halves directly: the momentum must not move, *and* the returned update must stay non-finite. `test_a_finite_gradient_still_moves_the_momentum` is the guard against the guard — it would catch a fix that simply disabled the optimizer. Existing suite on this branch, non-offload configurations: ``` 2 failed, 80 passed ``` Both failures are `op_builder.builder.CUDAMismatchException` in `test_muon_reduce_scatter_with_optimizer_offload_raises`, from this box's system CUDA not matching the one torch was built against, so CPUAdam will not build. They are unrelated to this change and reproduce on master. ## Note on the suite Worth recording, since it is why this survived: `TestMuonConfigs` captures `initial_params` **before** `deepspeed.initialize`, which casts the model to fp16. The assertion is therefore fp32 against fp16 and `torch.equal` is `False` whatever happened in between: ``` initial dtype (pre-init) torch.float32 after-training dtype torch.float16 repo assertion (pre-init vs after) 10/10 "changed" same-dtype comparison 0/10 actually changed cast alone, no training at all 10/10 "changed" by the same assertion ``` Casting a fresh model to fp16 and training it zero steps satisfies it. That is out of scope here — the new file captures after `initialize` and says why in a comment — but the assertion is worth tightening separately. --------- Signed-off-by: alanhuangyoo <alanhuangyoo@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Author
Parents
Loading