Repository navigation
AutoEP: do not slice DeepEP buffers that already hold exactly the arrived rows - #8747
Conversation
…ived rows A fresh DeepEP dispatch synchronizes for exact counts, so the received rows and weights usually hold exactly handle.num_expanded_tokens rows. The DeepEP route sliced them to that length anyway. The slice changes nothing forward, but under activation checkpointing the recompute records a SliceBackward, which zero-fills a gradient the size of the whole buffer and copies the real one into it: on Qwen3-30B-A3B (8 H100s, 16384 tokens per rank) a 512 MB fill and copy per layer and micro-batch. Slice only when the buffer holds more rows than arrived. Values and gradients are unchanged. Signed-off-by: yh0903 <helloyu0903@gmail.com>
…ents Extend the route regression to independent row/weight capacities, absent weights, zero arrivals and pre/post weighting. Compare outputs and full buffer gradients against an independently constructed always-sliced eager route with nonlinear experts; padding must retain zero gradients. Check the actual SliceBackward profiler event to pin the redundant buffer-sized gradient allocation, rather than using tensor identity as a proxy. Runtime code and accepted GPU/performance behavior are unchanged. Signed-off-by: yh0903 <helloyu0903@gmail.com>
06386d2 to
417f5fa
Compare
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 417f5faf54
ℹ️ About Codex in GitHub
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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
🟢 Approval recommended
The optimization preserves existing semantics and is covered by focused output, gradient, padding, and profiler assertions.
0 open findings
What changed in this PR
Optimizes AutoEP’s DeepEP route by avoiding redundant slices when received buffers already match the arrived-row count.
Changes:
- Independently slices only overallocated row and routing-weight buffers.
- Adds comprehensive forward, gradient, and profiler regression coverage.
- Documents the optimization and unchanged communication semantics.
| File | Description |
|---|---|
deepspeed/module_inject/auto_ep_layer.py |
Skips unnecessary exact-size buffer slices. |
tests/unit/module_inject/test_auto_ep_comm.py |
Tests exact, padded, empty, weighted, and unweighted cases. |
docs/code-docs/source/autoep.rst |
Documents buffer trimming behavior and performance rationale. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
tohtana
left a comment
There was a problem hiding this comment.
Looks good to me, thank you!
Signed-off-by: Masahiro Tanaka <tanaka.masahiro@gmail.com>
## Summary Add opt-in `expert_parallel.skip_recompute_combine` for AutoEP's DeepEP route. In supported Hugging Face per-decoder-layer **reentrant** checkpointing, the recomputed MoE output is used only by the residual add and is discarded by the checkpoint. The route records post-expert row weighting and combine backward without executing those forward operations; dispatch, expert compute and all required backward transfers still run. Default behavior is unchanged. This is the existing skip-combine PR, not a duplicate implementation. It remains distinct from the non-reentrant layout replay merged in deepspeedai#8731. The branch preserves that replay, the fused gate+up defaults from deepspeedai#8733, and the independent exact-size buffer checks from deepspeedai#8747. AI assistance was used for finalization, with author review before submission. ## Supported contract - `comm_backend="deepep"`, `autoep_size > 1`, BF16 dispatch; eager and fused row weighting, at their unchanged `score_apply` boundary. - Supported decoder presets: `deepseek_v2`, `deepseek_v3`, `minimax_m3`, `mixtral`, `qwen3_5_moe`, `qwen3_moe`, checked against Transformers 5.12.0 modeling code. - The recompute guard checks the attached decoder layer's current state and checkpoint configuration: `training=True`, `gradient_checkpointing=True` and the standard Torch checkpoint partial with `use_reentrant=True`. An eval-mode decoder does not run its own Hugging Face checkpoint, so an outer reentrant recompute is rejected rather than recording a NaN output. Ordinary eval forwards are unchanged. Transformers 5 defaults to non-reentrant, so explicitly use `gradient_checkpointing_kwargs={"use_reentrant": True}`. - Any non-null `moe_layer_pattern` is rejected during configuration validation when the skip is enabled, even with an explicit or automatically selected supported preset, a pattern matching the preset, or an empty string. Leave the pattern unset or disable the skip; custom-pattern support is unchanged with the option off. - Other checkpoint APIs, non-reentrant checkpointing with this flag and unsupported presets are outside the supported contract. Without recomputation, the ordinary path runs. - The skip requires that the MoE output has no value-dependent consumer in the recompute. The combined sentinel is NaN; an invalid consumer can produce non-finite values or gradients, but the sentinel is **not** a general runtime proof that arbitrary wrappers, hooks or patched decoder graphs are safe. ## Finalization Resolved the upstream conflicts without dropping replay or buffer behavior. Both normal and recorded combines now share their backward-context setup, including transferring the pending dispatch entry from the exchange to the consuming autograd node. Otherwise the recorded path could retain a stale entry and attempt to replay the previous step's routing in a later reentrant backward. The CPU reproducer fails before the context fix with `activation checkpointing recomputed a different routing`. The new regression performs three successive steps with changing routes and compares outputs, input/router/expert gradients and required transfers for both pre/post score application. The single CUDA weighting test now runs in a fresh `DistributedTest` worker, retaining its actual fused/recorded-backward comparison rather than allocating CUDA from an ordinary forked unittest. The existing real-DeepEP skip parity cases run two steps per model. The one-step test helper's defaults and original tolerances remain unchanged. ### Review follow-up Following the review, reject pattern overrides before model conversion. Applying an override preserves the supported preset name but can select a different decoder parent, so a preset-name whitelist cannot establish the residual-only output-consumer contract. The fix is a four-line configuration guard, not a decoder-graph validator or a runtime fallback. Six parametrized cases cover automatic and explicit preset selection, a custom parent, the preset's own pattern and an empty pattern. Each also checks that disabling the skip or leaving the pattern unset remains accepted. Both configuration references document the restriction. The kernels, recorded backward, supported execution paths and numerical thresholds are unchanged. ### Training-state guard follow-up The decoder must actually be checkpointing, not merely configured to checkpoint. Hugging Face enables the per-layer checkpoint only when `gradient_checkpointing` and `training` are both true. Without the training-state check, an eval decoder under an outer reentrant checkpoint can feed the recorded NaN into a value-dependent consumer such as `y * y`. The original guard was reproduced on CPU with finite forward output but non-finite input, router and expert gradients, for both pre and post score application. The guard now fails closed in that invalid recompute. A faithful decoder harness uses actual Torch nested reentrant checkpoints: training-mode output and every input/router/expert gradient match the unskipped CPU reference, eval-mode outer recomputes raise, and ordinary train/eval forwards retain the full route unchanged. The kernel, backward and numerical thresholds are unchanged. The latest master merge preserves the maintainers' existing merge commits and resolves only the import conflict by retaining `math`, `weakref` and `partial`. The lightweight `__new__` test layer explicitly initializes the optional overlap flag to its disabled default for eventual merge compatibility; the independent overlap feature from deepspeedai#8739 is not imported into this PR. ## Testing Latest training-state guard validation: **145 passed, 1 CUDA test skipped, 58 subtests passed**, on **1x Apple M4 Pro CPU**, macOS 26.7, Python 3.11.15, Torch **2.14.1**, pytest 9.1.1; CUDA and accelerator driver are not applicable. This covers the complete comm/replay suite, configuration tests and supported decoder attachment. The same selector previously reported **139 passed, 1 skipped, 52 subtests passed** for the earlier pattern-guard follow-up: ```bash DS_ACCELERATOR=cpu PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONDONTWRITEBYTECODE=1 \ python -m pytest -q -p no:cacheprovider \ tests/unit/module_inject/test_auto_ep_comm.py \ tests/unit/v1/moe/test_autoep_unit.py::TestAutoEPConfig \ tests/unit/v1/moe/test_autoep_unit.py::TestModelDetectionAndReplacement::test_skip_recompute_combine_attaches_each_layer_to_its_decoder_layer ``` The skipped case requires CUDA and Triton for recorded fused weighting; its existing device evidence is listed below. The newly affected nested-checkpoint and ordinary-forward train/eval tests also pass under isolated forked workers (**2 passed, 0 skipped**): ```bash DS_ACCELERATOR=cpu PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONDONTWRITEBYTECODE=1 \ python -m pytest -q -p no:cacheprovider -p pytest_forked --forked \ tests/unit/module_inject/test_auto_ep_comm.py::TestSkipRecomputeCombine::test_outer_recompute_respects_the_decoder_training_state \ tests/unit/module_inject/test_auto_ep_comm.py::TestSkipRecomputeCombine::test_nothing_is_skipped_without_a_recompute ``` The new regression produces exactly two missing-error failures with the original guard (eval mode, pre/post), while its two training controls pass. The earlier six pattern regressions passed under isolated forked workers (**6 passed, 0 skipped**) on the same CPU setup: ```bash DS_ACCELERATOR=cpu PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONDONTWRITEBYTECODE=1 \ python -m pytest -q -p no:cacheprovider -p pytest_forked --forked \ tests/unit/v1/moe/test_autoep_unit.py::TestAutoEPConfig::test_skip_recompute_combine_rejects_pattern_overrides_even_with_supported_presets ``` Existing product validation is bound to the original PR commit `29303c378`: **275 passed, 1 skipped** on four H100s. All four eager/fused and uniform/skewed skip-parity cases ran, as did the recorded fused-weighting backward test. The skipped case is a MiniMax-M3 router-logit test requiring Transformers >=5.15.0; the validation environment has 5.12.0. This is reused evidence for the original implementation, not a claim that the entire suite was rerun after rebase. Pre-review focused CPU validation: **75 passed, 1 CUDA test skipped**, under both ordinary pytest and `pytest --forked`, on macOS with Torch 2.10.0 and Transformers 5.12.0. This covers the comm/replay suite, three skip config/conversion cases and the one-step helper defaults. Ordinary pytest also reports **52 subtests passed**. Earlier focused device validation, before the pattern and training-state guard follow-ups: **9 passed, 0 skipped** with `pytest --forked`, on **4x NVIDIA H100 80GB HBM3**, driver **580.159.04**, Torch **2.10.0.2+cu130 / CUDA 13.0**, Triton **3.6.0**, Transformers **5.12.0**, using the previously validated DeepEP runtime: - All four eager/fused and uniform/skewed skip-parity cases run two training steps on each model. They retain the original numerical bounds, check last-step output/input/router/parameter gradients and accumulated parameter deltas, and verify one forward combine per layer per step rather than two. - The four upstream non-reentrant/nested-reentrant replay cases still pass, confirming that the shared context preserves normal replay behavior. - The recorded fused-weighting backward comparison runs in a fresh worker. The original ordinary unittest reproduces `Cannot re-initialize CUDA in forked subprocess` on the same setup; the new worker-based case passes. GPU reproduction selector, from `tests/`: ```bash PYTHONPATH=.. DEEPSPEED_RUN_H100_TESTS=1 pytest --forked \ unit/module_inject/test_auto_ep_comm.py unit/v1/moe/test_autoep_deepep_parity.py \ -k 'skipping_the_recomputed_combine or recorded_fused_row_weighting or non_reentrant_checkpointing_replays' ``` Changed-files pre-commit passed for all 11 files in the final PR delta. Existing device evidence is reused for unchanged kernels/backward and supported training paths; GPU tests, full E2E and the existing performance matrix were not restarted for these guard follow-ups. The new state predicate is exercised through actual Torch checkpoint/autograd on CPU, not presented as a fresh H100 execution or a real Transformers-model run. The focused tests do not establish every ZeRO/offload/TP/PP combination, other hardware, arbitrary checkpoint composition or long-run convergence. The static CI selector includes the v1 AutoEP unit and parity files. Real DeepEP cases remain gated on the opt-in H100 test flag, four GPUs and a GIN-capable DeepEP build, so test collection alone does not prove they ran. The standalone recorded-weighting CUDA comparison is outside Modal's v1 test scope and was explicitly included in the focused hardware command above; its recorded path is also exercised by the v1 model-parity cases. Full GPU CI follows the repository's merge-queue workflow. ## Existing performance evidence Qwen3-30B-A3B, **8 H100s**, EP8, micro-batch **4**, gradient accumulation **16**, sequence **4096**, DeepEP **28 SMs**, reentrant activation checkpointing; **30 steps with 10 warmup**. The equivalent benchmark prototype records the same weighting/combine backward but returns uninitialized output where the product fills NaN. Both arms inherit concatenated gate+up and backward weight-gradient overlap; this is the **incremental skip effect**, not their combined benefit. One allocation, order normal / skip / skip / normal: | Recompute | Run medians | Mean | |---|---|---:| | Normal | 20,575.1 / 20,574.2 ms | 20,574.7 ms | | Combine + post-weighting forward skipped | 19,945.7 / 19,928.2 ms | **19,936.9 ms (-3.10%)** | Peak allocated memory stayed at **58.69 GB**; peak reserved memory was **0.25 GB lower**. Loss differences over these 30 steps were within same-arm repeat noise (maximum **9.4e-4**). The 8-GPU residual-block gate had numerical equality under `torch.equal` for output/input/router gradients and expert-gradient differences around **5e-5 relative L2**, within two normal recomputes' variation. `torch.equal` is not a claim about signed-zero bit patterns. The measurements are historical prototype evidence, **not** a retiming of the rebased product or a universal benefit across models/hardware. Skipping the forward combine removes that recompute transfer; it does not add overlap or remove the cached dispatch and weight-gradient communication required by backward. --------- Signed-off-by: yh0903 <helloyu0903@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: pengdurice <pengduhit@gmail.com>
Summary
Avoid redundant prefix slices of DeepEP's received-row and routing-weight buffers when their lengths already equal
handle.num_expanded_tokens.A fresh dispatch synchronizes for exact counts and normally returns exact-size buffers.
received[:arrived]then changes no values, but records aSliceBackwardin checkpoint recomputation. Its backward zero-fills a buffer-sized gradient and copies the real gradient into it.With 16,384 tokens per rank, top-8 routing and hidden size 2,048, a BF16 buffer holding 131,072 expanded rows is 512 MiB. A redundant slice of that buffer adds a 512 MiB fill and copy per layer/micro-batch. A motivating profile measured about 128 ms of BF16 fills and 304 ms of device-to-device copies per step; not every copy is attributed to this slice.
Change and scope
handle.num_expanded_tokensas the host-side row count; add no device-to-host synchronization.score_apply="pre"/"post", routing-weight gradients, and the rows passed to experts and combine.Correctness and tests
The CPU regression is consolidated in
tests/unit/module_inject/test_auto_ep_comm.py:torch.equal; all padding row and weight gradients remain zero.aten::slice_backwardwork: zero events for exact-size buffers, and one event for each buffer that still requires trimming. This pins the allocation/copy regression directly rather than using tensor identity as a proxy.Results:
pre-commitchecks passed.The H100 run used the actual original PR runtime. The initial review follow-up changed only tests and documentation. The later master synchronization below preserves the same slice-optimization delta while inheriting upstream replay support; it is not presented as a fresh GPU qualification of the merged tree. No GPU suite or full benchmark was restarted.
End-to-end evidence
Qwen3-30B-A3B training on 8 H100s:
The saving is approximately 336 ms / 1.69%. Losses agree within same-arm repeat noise; peak allocated memory is unchanged. A real-DeepEP 8-GPU check before timing matched the existing route within DeepEP's arrival-order noise.
The benchmark prototype skipped both slices when both buffers were exact-size, as they were in this workload. The product implementation checks them independently; mixed capacities are covered by the new CPU regression, not by additional performance measurements.
These measurements cover one model, hardware type and training configuration. No universal speedup or long-run convergence result is claimed. Routing weights in SwiGLU are a separate optimization and are not included in the reported 1.69% saving.
Master synchronization
The maintainer synchronized this existing approved PR with master in
7a1e82639; no additional optimization or duplicate PR was introduced. Both the exact-size buffer-trimming paragraph and upstream checkpoint-replay documentation are retained. The route keeps upstream'sreplay=wiring, and the existing fake-dispatch test accepts that keyword. The runtime additions/deletions relative to master are identical to the approved slice change.The published tree was verified to exactly match the human-reviewed conflict resolution used for these checks:
Commands:
Compatibility scope: CPU checks exercise BF16 received rows, FP32 routing weights, pre/post weighting, absent weights, empty/padded buffers and checkpoint replay contracts using a fake DeepEP transport; they do not qualify physical DeepEP communication. The accepted GPU/E2E evidence above covers H100, EP8 and reentrant checkpointing. No fresh ZeRO-stage/offload, expert-TP, multi-node or non-CUDA-device matrix sweep was performed for this documentation/mock-signature resolution. The retained GPU summary does not record an exact driver version, so none is inferred here.
AI assistance was used for conflict inspection and validation. The human author reviewed the resolution before signoff; the maintainer's identical merge was retained rather than overwritten.