DeepSpeed
d099bc61 - Fix Muon optimizer under ZeRO CPU offload and bound gather buffers (#8464)

Commit
61 days ago
Fix Muon optimizer under ZeRO CPU offload and bound gather buffers (#8464) ### Motivation In PR #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 <youzhi.jin@intel.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Author
Parents
Loading