Repository navigation
Fix: [Bug] AutoEP FP32 gradient clipping averages norms across ranks with d - #8691
Open
goransh-walia wants to merge 1 commit into
Open
goransh-walia wants to merge 1 commit into
goransh-walia wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The reduction overcounts replicated dense gradients, mishandles infinity norms, and lacks the targeted AutoEP regression test.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Attempts to correct FP32 gradient clipping for uneven AutoEP expert ownership.
Changes:
- Accumulates per-parameter p-norm powers.
- Replaces DP norm averaging with summed contributions.
- Takes the p-root after DP reduction.
| File | Description |
|---|---|
deepspeed/runtime/utils.py |
Revises distributed gradient-norm calculation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+403
to
+405
| total_norm = total_norm.pow(norm_type) | ||
| dist.all_reduce(total_norm, op=dist.ReduceOp.SUM, group=pg) | ||
| total_norm = total_norm.pow(1. / norm_type) |
| dist.all_reduce(scaled_norm_tensor, group=pg) | ||
| total_norm = scaled_norm_tensor | ||
| total_norm = total_norm.pow(norm_type) | ||
| dist.all_reduce(total_norm, op=dist.ReduceOp.SUM, group=pg) |
banxingmjj
pushed a commit
to openanolis/DeepSpeed
that referenced
this pull request
Nov 7, 2026
…speedai#8823) AutoEP + AutoTP folding gives TP peers different routed experts. ZeRO-1/2 currently filters non-TP expert parameters to TP rank zero when computing the clipping norm, then applies the ordinary expert-norm averaging path. This can omit expert owners and produce an incorrect clipping coefficient even when reconstructed logical gradients are correct. Compute folded expert norms over the actual expert-DP partition group and EP owner group, without another TP sum or expert-norm average. Only groups tagged with the AutoEP expert placement family use this path. Dense groups and native MoE groups retain their existing reductions and native expert-norm averaging, including when native ETP and AutoEP groups coexist. ZeRO-3 is unchanged and is checked as a sibling reference. The existing folding contract rejects optimizer/parameter offload. Refs deepspeedai#8672 and deepspeedai#8670. The live duplicate check found no open PR fixing this ZeRO-1/2 folding path. deepspeedai#8476, deepspeedai#8478 and deepspeedai#8691 address FP32/ZeRO-0 clipping in `runtime/utils.py`; they do not change the partitioned optimizer reductions fixed here. deepspeedai#8739 implements DeepEP weight-gradient overlap and does not change these norm reductions. ### Final change This PR contains three files: - `deepspeed/runtime/zero/stage_1_and_2.py`: correct the folded expert clipping reductions. - `tests/unit/v1/moe/test_autoep_autotp_sharded_attention.py`: reconstruct full logical gradients and FP32 master parameters, then compare an actual SGD update against an unsharded ZeRO-0 reference across all 45 parameter families. Thresholds of 0.5 and 2.0 times the reference norm cover active clipping and an inactive control. Snapshots are cloned before in-place updates; distributed diagnostics finish before assertions. - `docs/code-docs/source/autoep.rst`: document expert ownership and the reduction contract. The sharded-attention regression supports 4- and 8-rank TP2/EP4 configurations, ZeRO-1/2/3 and ZeRO-3 `zero.Init`. An additional two-rank analytic SGD regression covers native ETP alone and mixed native ETP/folded AutoEP groups at ZeRO-1/2. ### Incremental review [Incremental review report and validation](deepspeedai#8823 (comment)) · [diff from the reviewed head](deepspeedai/DeepSpeed@352ee55...fb5d0e5). The correction preserves native ETP reductions alongside folded AutoEP. **On the correction commit, 8 positive cases passed on 2 H100 GPUs, and 2 published-head negative controls failed as expected.** ZeRO-1/2 optimizer cases validate native norm 5, mixed norm 13 and the actual clipped-SGD updates; TP2/EP2 tiny GLM4-MoE active/inactive clipping cases validate all 45 parameter families against an unsharded reference. The old mixed cases produce partial norms 12.369317 / 12.649111 instead of 13. These runs used FP32, TF32 disabled, Torch 2.12.1+cu130 / CUDA 13.0, Transformers 5.15.1 and driver 595.91.07. Changed-file pre-commit checks and independent archive/report readback passed; owned workers and locks closed. The report records the exact execution method and CPU diagnostic separately. The 8/16-GPU results below belong to the original submitted head. ### Original submitted-head validation The original submitted three-file diff at `352ee55cd14f77f30e5170939c7e60c4d4bd7754` was tested on source based on `1c862f7df93f0345b977c67f5408d967b94bf4ae`. The extended finite workload used **16 NVIDIA H100 GPUs across four nodes**, random tiny GLM4-MoE, FP32 with TF32 disabled, Torch 2.12.1+cu130, CUDA 13.0 and Transformers 5.15.1. The original receipt did not capture the NVML driver package version. | Coverage | Final result | |---|---| | Clipping and initialization | TP2/EP4 and TP2/EP8, ZeRO-1/2/3, active/inactive clipping and ZeRO-3 `zero.Init` passed. An unfixed ZeRO-2 control failed the norm/update oracle. | | Routing and topology | Rotating hotspots, idle expert owners, GAS4, and TP4/EP16 with 16 experts and ZeRO-3 `zero.Init` passed. | | Communication | Overlap off/on and small-bucket configurations with actual repeated/oversize flushes passed. | | Save and resume | Continuous four-update training matched save after two updates, process exit and new-process resume through four updates, including parameters, momentum 0.9, losses and WarmupLR state. | | Sustained training | 200 actual updates across four 50-update routing stages passed the reference loss, gradient and update checks. Late live allocated-memory spread stayed below 64 MiB for this tiny model. | | Independent readback | All 28 cases, 448 rank packets and 247,635 raw arrays from the 16-GPU workloads passed separate NumPy arithmetic and complete archive checksum verification. | | Source checks | All applicable changed-files pre-commit hooks passed, including yapf, flake8, codespell, license and accelerator/distributed import checks. `git diff --check` and a clean three-way merge check against current upstream master passed. | The extended distributed drivers used the same contributed reference helpers. An additional 8-H100 clipping matrix passed 16 cases and 28,800 raw arrays. All owned GPU workers and contexts closed, the original GPU locks were released, and complete outputs were archived. Idle-owner checks exclude alignment padding using the frozen permutation formula `ceil((n_tokens + E_local*8)/8)*8`; equality to `E_local*8` proves zero actual received rows. The original finalizer's incorrect padding assertion and failed scheduler status are preserved in the private evidence. The separate corrected reader passed against the same completed workload and unchanged numerical tolerances. These results establish tiny-model correctness, optimizer-state continuity and bounded-run stability. They do not establish pretrained-model convergence, a speedup, BF16/FP16 or Muon behavior, offload, PP/SP, DeepEP overlap, or completion of every Q4 roadmap item. Reproduce the contributed regression and source checks: ```sh pytest --forked -q tests/unit/v1/moe/test_autoep_autotp_sharded_attention.py \ -k TestShardedAttentionClippedUpdate pre-commit run --files deepspeed/runtime/zero/stage_1_and_2.py \ tests/unit/v1/moe/test_autoep_autotp_sharded_attention.py \ docs/code-docs/source/autoep.rst ``` AI assistance was used for implementation, test preparation and incremental review. The human submitter reviewed the original submission, confirmed its test results, and authorized this follow-up correction and DCO sign-off. --------- Signed-off-by: 0z5a <dezhen.lu@student.uni-tuebingen.de> Co-authored-by: pengdurice <pengduhit@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


This PR addresses #8475.
[Bug] AutoEP FP32 gradient clipping averages norms across ranks with different expert ownership
Generated with AI assistance and validated against the original
file before submission (syntax check + change-scope check).
Please review carefully — happy to adjust based on feedback.