Repository navigation
Enable optimized Adam backend for MuonWithAuxAdam optimizer - #8278
Conversation
Signed-off-by: iLeGend <[email protected]>
Signed-off-by: iLeGend <[email protected]>
Signed-off-by: iLeGend <[email protected]>
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
| ], | ||
| ) | ||
| def test_adam_backend_selection(engine, parameters, adam_w_mode, expected_class, expected_kwargs): | ||
| optimizer_class, optimizer_kwargs = DeepSpeedEngine._select_adam_optimizer(engine, parameters, adam_w_mode) |
There was a problem hiding this comment.
Why not this?
| optimizer_class, optimizer_kwargs = DeepSpeedEngine._select_adam_optimizer(engine, parameters, adam_w_mode) | |
| optimizer_class, optimizer_kwargs = engine._select_adam_optimizer(parameters, adam_w_mode) |
There was a problem hiding this comment.
Yes, it's strange. Thank you for your suggestions.
There was a problem hiding this comment.
@sfc-gh-truwase Sorry, I need to revert this modification. engine here is an instance of the local _AdamSelectionEngine test double, not a fully constructed DeepSpeedEngine. Because of that, we need to call DeepSpeedEngine's method directly (DeepSpeedEngine.get_optimizer_configuration(engine, ...)) rather than as a bound method on engine.
Fully instantiating a real DeepSpeedEngine (via deepspeed.initialize()) would require a model, config, and distributed backend — overhead that's disproportionate to what this test is actually checking, and it would distract from the test's real purpose (verifying Adam-backend selection logic).
…n method in test Signed-off-by: iLeGend <[email protected]>
|
@PKUWZP FYI |
Signed-off-by: iLeGend <[email protected]>
Signed-off-by: iLeGend <[email protected]>
Signed-off-by: iLeGend <[email protected]>
pengdurice
left a comment
There was a problem hiding this comment.
Thank you for the great PR. I think this changes warrants an end to end test with loss value and curves reported on a realistic training (e.g. a 128m model for 100 steps) between baseline and this change. LMK what you think.
Thank you for your suggestion. I agree that an end-to-end training loss comparison would be necessary. I’ll set up a training-loss curve check focusing on the auxiliary Adam backend with Muon. Do you have a particular model or configuration you would recommend? |
Thank you! DeepSpeed/deepspeed/__init__.py Line 84 in aa3914d test baseline and the possible arms after the PR (fused Adam, cpu Adam etc) Also, make sure the lr are set appropriately (not sure if the grad clipping issue is solved or not), the seed is set so for each arm same data example is used by different arms at the same step. LMK what you think, thank you! |
|
Hi @jinyouzhi, Before this PR, non-Muon groups under the Muon optimizer use the inline Adam path. After this PR is merged, the same Muon configuration automatically selects FusedAdam. A pre-PR checkpoint therefore loads under the same user-facing optimizer and configuration, but its first resumed step fails because the new backend expects fields absent from the saved state (e.g. |
Signed-off-by: iLeGend <[email protected]>
Nice catch, @tohtana! Thanks, I missed that. I’ve pushed a fix in commit cf0f900. |
I am still working on it. Quick update, the losses of FusedAdam and PyTorch impl is close to base's, there's a gap about CPUAdam, still investigate into it. Thank you for your advice. |
CPUAdam itself is supported. However, Muon CPU offload is not currently correctness-safe because ZeRO’s flat CPU partitioning path bypasses Muon’s full-matrix update semantics. I plan to address the Muon CPU offload issues in a separate PR. |
thank you for the great work! Look forward to the PR fixing Muon CPU offload issue. |
d47e4f4
…eepspeedai#8464) ### Motivation In PR deepspeedai#8278, we extended the auxiliary optimizer in `MuonWithAuxAdam` to support optimized backends such as `CPUAdam`. However, Muon with CPU offload under ZeRO stages had critical correctness issues and performance/memory pitfalls: 1. **Geometric structure loss under ZeRO CPU offload**: Parameters and gradients flattened into 1D partitions lost matrix geometric properties, bypassing Newton-Schulz polar decomposition and first-order momentum tracking. 2. **Device mismatch & duplicate update under ZeRO-3 offload**: When offload is enabled, the momentum buffer resides in CPU memory while `param.grad` is on accelerator devices. Calling `_apply_distributed_muon_update` during backward caused cross-device runtime errors and double-updating. 3. **Multi-GPU ZeRO-1/2 single-buffer gather nesting**: Reconstructing single-buffer partitions returned nested lists, causing `TypeError: zeros_like()` crashes on the first step. 4. **Buffer allocation spikes & memory fragmentation**: Lacking bounded cache management for all-gather scratch buffers led to unbounded VRAM allocations. ### Changes - **Shape reconstruction & metadata propagation**: - Unflatten 2D/3D logical parameters before Newton-Schulz polar decomposition, slice back to rank partitions, and preserve 1D auxiliary Adam parameters (`CPUAdam`) intact. - **ZeRO-3 optimizer offload support**: - Schedule Muon updates during `_get_norm_groups()` and guard `_apply_distributed_muon_update` during backward when `offload_optimizer` is enabled. - **Bounded scratch buffers with LRU eviction**: - Introduce `_muon_allgather_buffers` LRU cache (capped at 256MB) using `OrderedDict` to prevent VRAM spikes and memory fragmentation. - Properly unwrap single-buffer gathered partitions and clear buffers in `destroy()`. ### Tests - All 23 Muon unit tests pass: ```bash pytest tests/unit/ops/muon/test_muon.py -k 'not TestMuonConfigs' ``` Including: - `TestMuonOptimizerOffload` (ZeRO stages 1, 2, and 3 with CPU offload) - `TestMuonAllGatherBufferLifecycle` - `TestMuonZero12NumericalCorrectness` - Verified pre-commit checks pass (`yapf`, `flake8`, `codespell`, `check-torchdist`, `check-license`). --------- Signed-off-by: Jin, Youzhi <[email protected]> Co-authored-by: Copilot <[email protected]>
Motivation
This pull request aim to extend the Muon optimizer auxiliary Adam(w) optimizer to support more built-in optimized implementation such as
FusedAdam.Changes
_select_adam_optimizermethod, improving modularity and allowing consistent backend selection for both Adam and Muon optimizers.CPUAdamandZenflowTests
tested on RTX 5090: 128 Tensors 16,384 FP32 elements, warmup 30, steps 200.