← Back to Pull requests

Overlap AutoEP's expert weight gradients with the DeepEP backward combine #8739

Open
yh0903 opened this pull request with 869 lines diff across 13 files
View on GitHub
yh0903 yh0903 commented on Oct 04, 2026 04:23 UTC
Author

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.forward takes an optional ExpertWeightGradSlot.

  • - With a slot, _DeferredWeightGradExperts runs the same forward as the torch._grouped_mm path, 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).
  • The DeepEP dispatch takes the expert weights as extra inputs. Its 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.
  • SM carveout:

  • - torch._grouped_mm launches persistent kernels sized to every SM, which leaves the combine nowhere to run.
    - The dW GEMMs are therefore launched with comm_num_sm SMs withheld through torch._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 finally block as well, so a dW error does not leave the queued communication unaccounted for.
  • Activation backward:

  • - The activation registry gains optional standalone backward functions for the fused kernels. swiglu provides them, via new swiglu_backward and swiglu_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.
  • The default, off, keeps the existing path.

  • Master's per-layer checkpoint-dispatch replay and exact-size buffer-slice optimization are retained. Overlap slots are separate per invocation and do not replace replay handles.

  • The deferred custom backward is first-order only: create_graph=True raises rather than returning an incomplete derivative.

  • overlap_weight_grad is rejected, rather than ignored, unless all of these hold:

  • - comm_backend="deepep" with autoep_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.
  • Docs: autoep.rst and the config reference.
  • Tests

  • CPU, TestDeferredExpertWeightGrad:

  • - Output, input gradient and w1/w2/w3 gradients 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_mm requirement, and config validation for every rejected setting.
  • CPU, 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_sm withheld, 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.
  • GPU, H100, 402 passed:

  • - 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_backward and swiglu_packed_backward bitwise 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.
  • End to end, Qwen3-30B-A3B on 8 H100s:

  • - 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 to comm_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

  • The ZeRO-0 feedback is addressed by failing closed: zero_stage not in (1, 2) is rejected while the disabled default still permits the ordinary stage-0 path.

  • The engine now passes its actual ZeRO stage into that validator. Previously it omitted the keyword, so tightening the validator alone would incorrectly treat every optimizer as stage 0; both direct-config and engine-wiring cases cover stages 0/1/2/3.

  • Master synchronization preserves the already merged numerical-reference policy, packed-kernel input-device/stream guard and higher-order rejection. Extracted standalone backward helpers use the input device rather than whichever CUDA device happens to be current.

  • The real-DeepEP comparison now reads trainable accumulated gradients through safe_get_full_grad before engine.step(), including stage-2 cases where param.grad is released. It does not accept an empty expert-gradient dictionary as proof of parity.

  • New targeted H100 acceptance: 40 passed, 0 skipped, on an 8x NVIDIA H100 80GB HBM3 host (distributed cases use four ranks), driver 580.159.04, torch 2.10.0.2/CUDA 13.0, Triton 3.6.0 and transformers 5.12.0. It covers both supported ZeRO stages, uniform/skewed routing, separate/fused gate+up, replay composition, standalone helper device selection and every trainable gradient before clipping. The 1e-2 relative-L2 criterion is unchanged.

  • On Apple M4 Pro CPU with torch 2.14.1, the comm suite passes 66/66 in forked mode; targeted config/folding/activation checks pass. Removing only the three guards reproduces all four expected failures (ZeRO 0 accepted, missing event wait, and separate/fused incomplete higher-order derivatives). CPU mocks are not presented as physical DeepEP validation.

  • Existing 402-passing-case H100 and E2E results above are reused, not described as fresh broad-hardware or full-training validation. The new follow-up retains the old numerical thresholds and performs no throughput sweep.

  • The activation-weighting child, deepspeedai/DeepSpeed#8748, is reviewed after this parent and remains a separate opt-in feature; its shifted-rounding semantics are not part of this PR.
  • 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.

    Diff: 869 lines (+818 / -51) across 13 files Reviewer: 0z5a Review duration: 21049m
    All Vouch review confidence checks passed
    Residual risk 0.13 = Change Risk (0.61) × (1 − Review Confidence (0.79)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.61 · 869 lines diff across 13 files
    0.61
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.68 · 2 substantive comment(s)
    0.68
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    78%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Real PR Conversation Timeline
    6 items
    0z5a 0z5a commented on Oct 06, 2026 08:59 UTC
    Reviewer

    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.

    chatgpt-codex-connector[bot] chatgpt-codex-connector[bot] commented on Oct 13, 2026 03:10 UTC
    Bot

    <!-- 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

  • Open a pull request for review

  • Mark a draft as ready

  • Comment "@codex review" or "@codex security review".
  • Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

    </details>

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Oct 13, 2026 03:16 UTC
    Reviewer

    <!-- 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:

  • Adds deferred expert weight-gradient computation with async DeepEP combine and SM carveout.

  • Validates supported DeepEP, grouped-GEMM, ZeRO, and PyTorch configurations.

  • Adds activation backward helpers, documentation, and CPU/H100 coverage.
  • | 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>

    yh0903 yh0903 commented on Oct 13, 2026 03:29 UTC
    Author

    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!

    tohtana tohtana requested changes on Oct 14, 2026 05:57 UTC
    Changes Requested Reviewer

    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.

    Reviewers
    Assignees
    yh0903 yh0903
    Labels
    risk: low confidence: 78.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 60.6% (MEDIUM) · Higher = larger blast radius
    60.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 78.5% · Lower = rushed or shallow review
    78.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 13% · Safe to merge
    13%