Overlap AutoEP's expert weight gradients with the DeepEP backward combine #8739
The documented contract here says overlap_weight_grad is supported with ZeRO stage 1 or 2, but the validation only rejects stage 3, so ZeRO-0 currently passes through.
Could we either explicitly reject zero_stage not in (1, 2), or add ZeRO-0 correctness coverage and document it as supported? Since this path reroutes expert dW through the dispatch autograd node, I'd prefer failing closed for an unvalidated optimizer path.
<!-- codex-pull-request-review-summary -->
Codex Review Summary
This comment shows the latest Codex review activity on this pull request.
| Review | Status | Commit | Review trigger |
| --- | --- | --- | --- |
| 📝 Code Review | ✅ Completed <relative-time datetime="2026-10-13T03:19:57.663917Z">2026-10-13T03:19:57.663917Z</relative-time> | b016772 | Draft marked ready |
<details> <summary>ℹ️ About Codex in GitHub</summary>
<br/>
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
</details>
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
The distributed asynchronous autograd scheduling and private PyTorch SM-carveout integration warrant final human review despite extensive targeted testing.
0 open findings
<details>
<summary><strong>What changed in this PR</strong></summary>
Adds opt-in overlap of AutoEP expert weight-gradient GEMMs with DeepEP’s backward combine, building on the merged fused gate/up work while keeping activation weighting separate.
Changes:
| File | Description |
| ---- | ----------- |
| deepspeed/moe/ep_experts.py | Implements deferred expert weight gradients. |
| deepspeed/module_inject/auto_ep_comm.py | Overlaps combine with gradient GEMMs. |
| deepspeed/module_inject/auto_ep_config.py | Validates overlap requirements. |
| deepspeed/module_inject/auto_ep_layer.py | Connects overlap to AutoEP routing. |
| deepspeed/module_inject/auto_ep_presets/base.py | Adds the configuration option. |
| deepspeed/ops/triton_ops/swiglu_triton.py | Exposes standalone SwiGLU backward helpers. |
| deepspeed/runtime/engine.py | Passes the actual ZeRO stage. |
| docs/code-docs/source/autoep.rst | Documents behavior and performance. |
| docs/_pages/config-json.md | Documents the configuration field. |
| tests/unit/module_inject/test_auto_ep_comm.py | Tests scheduling, recovery, and checkpointing. |
| tests/unit/v1/moe/test_autoep_deepep_parity.py | Tests real DeepEP gradient parity. |
| tests/unit/v1/moe/test_autoep_expert_activation.py | Tests deferral and validation. |
| tests/unit/v1/ops/triton_ops/test_swiglu_triton.py | Tests backward helper parity and device selection. |
</details>
🧠 Review effort: Balanced
---
💡 <a href="/deepspeedai/DeepSpeed/new/master?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
The documented contract here says overlap_weight_grad is supported with ZeRO stage 1 or 2, but the validation only rejects stage 3, so ZeRO-0 currently passes through.
> Could we either explicitly reject zero_stage not in (1, 2), or add ZeRO-0 correctness coverage and document it as supported? Since this path reroutes expert dW through the dispatch autograd node, I'd prefer failing closed for an unvalidated optimizer path.
Thanks @0z5a. Overlap now explicitly rejects ZeRO 0/3 and accepts only stages 1/2, with the engine forwarding its actual stage to validation. Error paths restore the carveout and wait for the pending async combine!
Hi @yh0903,
Thank you for submitting this PR!
I wasn't able to run this with real DeepEP setting, but the code change overall looks good.
One small change I want to request: the engine now passes zero_stage to the AutoEP config validator, but an existing validator test func rejects that keyword. A minimal signed test-only repair would be like commit e452d350.
0z5a
yh0903
The fused gate+up prerequisite, deepspeedai/DeepSpeed#8733, has merged. This branch is synchronized with current master and retains only the backward-overlap feature delta; no force-rewrite of the earlier submitted commits is required.
In AutoEP's DeepEP backward, the experts compute their input gradient (dX) and their weight gradients (dW). DeepEP's combine then sends dX back to the token ranks. dW and the combine are independent, yet they run serially: a grouped GEMM's backward computes dX and dW together, and only then does the dispatch backward start the combine. In a kernel profile of Qwen3-30B-A3B on 8 H100s, DeepEP ran concurrently with no compute kernel at all.
This adds an opt-in
expert_parallel.overlap_weight_grad, which runs the combine and the dW GEMMs side by side.Change
GroupedExperts.forwardtakes an optionalExpertWeightGradSlot.- With a slot,
_DeferredWeightGradExpertsruns the same forward as thetorch._grouped_mmpath, for separate or fused gate+up and every registered activation.- Its backward returns dX at once and leaves the dW GEMMs in the slot.
- The GEMMs are the products autograd forms for
torch._grouped_mm(_grouped_mm_mat1_backward,_grouped_mm_mat2_backward).1. launches the dX combine with
async_with_compute_stream=True;2. runs the slot's dW GEMMs on the compute stream;
3. waits on the combine's event;
4. returns dW through autograd, so ZeRO's gradient handling is unchanged.
-
torch._grouped_mmlaunches persistent kernels sized to every SM, which leaves the combine nowhere to run.- The dW GEMMs are therefore launched with
comm_num_smSMs withheld throughtorch._C._set_sm_carveout_experimental.- The previous carveout is restored even if the GEMMs raise.
- The pending combine's event is waited on in a
finallyblock as well, so a dW error does not leave the queued communication unaccounted for.- The activation registry gains optional standalone backward functions for the fused kernels.
swigluprovides them, via newswiglu_backwardandswiglu_packed_backward, which the existing autograd functions now also call.- Forms without them are recomputed and differentiated, so the deferred path always matches the forward that ran.
create_graph=Trueraises rather than returning an incomplete derivative.overlap_weight_gradis rejected, rather than ignored, unless all of these hold:-
comm_backend="deepep"withautoep_size > 1;-
use_grouped_mm=true, and the experts do not select the Triton grouped GEMM;- ZeRO stage 1 or 2, explicitly rejecting both stage 0 and stage 3;
- PyTorch provides both the SM carveout getter and setter APIs.
autoep.rstand the config reference.Tests
TestDeferredExpertWeightGrad:- Output, input gradient and
w1/w2/w3gradients match the plain path's values exactly in the tested CPU cases, for every activation, separate and fused gate+up, FP32 and BF16, with weight gradients immediate or deferred, including an empty expert. This does not assert universal bit-pattern equivalence.- Also: slot lifecycle errors, the
torch._grouped_mmrequirement, and config validation for every rejected setting.TestWeightGradOverlap, the route against a DeepEP contract buffer:- Exact numerical agreement with the serial backward in the tested CPU cases, separate and fused gate+up, with no checkpointing, reentrant and non-reentrant checkpointing, now composed with upstream's replay coordinator.
- The order is async combine, then dW with
comm_num_smwithheld, then wait.- The carveout is restored, including on error.
- A dW exception still waits for the async combine before propagating; it does not widen optimizer/backend support.
-
test_overlapped_weight_gradients_match_the_serial_backward, on 4 GPUs with DeepEP, uniform and skewed routing, separate and fused gate+up: input and every parameter gradient within 1e-2 relative L2, the shared parity checks, and no leaked carveout.- The CUDA bitwise check with the Triton kernels.
-
swiglu_backwardandswiglu_packed_backwardbitwise against autograd.- The rest of the DeepEP parity, AutoEP unit and activation suites.
- Not part of this change: the 2 failures are
TestMiniMaxM3Block::test_replaced_block_matches_hf_block. That preset needs Transformers >= 5.15.0, and the image has 5.12.0.- Setup: expert parallel size 8, micro-batch 4, 16 accumulation steps, sequence length 4096, DeepEP, reentrant checkpointing, fused gate+up, 30 steps with 10 warmup.
- Measured with an equivalent benchmark patch on our benchmark base: the same deferral, async combine and carveout.
- Two pairs in one job:
| Arm | Mean step |
|---|---:|
| Serial backward | 21,569.5 ms |
| Overlap without the carveout | 21,418.5 ms (-0.70%) |
| Overlap with the carveout | 21,097.3 ms (-2.19%) |
- A kernel profile showed DeepEP running alongside grouped GEMMs for 1,325 ms of the step, against none before.
- Step-1 loss identical. Later losses agree within same-arm repeat noise.
- Peak allocated memory unchanged; reserved memory about 0.7 GB higher.
- A follow-up sweep in one job: withholding more SMs than DeepEP uses (24 with
comm_num_sm=12) was 0.9% slower. That supports tying the carveout tocomm_num_sm.- The overlap's gain depends on how much backward communication is left to hide. A second job separated the SM budget from the overlap:
| Arm | Mean step |
|---|---:|
| Serial backward,
comm_num_sm=12| 21,569.9 ms || Serial backward,
comm_num_sm=20| 20,942.6 ms || Overlap,
comm_num_sm=20| 20,696.9 ms (-1.17% vs serial at 20) || Overlap,
comm_num_sm=28| 20,638.8 ms |On this single node, more DeepEP SMs speed up every DeepEP call regardless of this option. The overlap saves 472 ms at 12 SMs and 246 ms at 20.
Review follow-up
zero_stage not in (1, 2)is rejected while the disabled default still permits the ordinary stage-0 path.safe_get_full_gradbeforeengine.step(), including stage-2 cases whereparam.gradis released. It does not accept an empty expert-gradient dictionary as proof of parity.AI assistance was used for review and test preparation. The human author reviewed the exact task-specific diff before signoff/publication. This updates an existing PR rather than opening a duplicate feature implementation.
DCO, test collection and changed-files pre-commit passed on the published head. Full GPU CI and infrastructure smoke jobs were skipped; they are not described as a full GPU CI run. On-device evidence for this review follow-up is the targeted H100 execution above.