Repository navigation
AutoEP: support combine_impl="fused_weighted_sum" with AutoEP+AutoTP folding - #8847
pengdurice wants to merge 4 commits into
Conversation
…folding Signed-off-by: pengdurice <pengduhit@gmail.com>
0z5a
left a comment
There was a problem hiding this comment.
Read at 2f187305.
The folded path looks right to me. all_flat_indices carries token * top_k + slot for each gathered row, which is what the unfolded call passes as token_indices_sorted, so the kernel sees the same row-to-assignment map. The rows and the routing weights still go through the differentiable gathers, so the folding gradient factor is unchanged.
The kernel needs CUDA and Triton, so on CPU I swapped fused_weighted_restore for an FP32 reference that weights and sums the top-k slots in slot order. I then ran the shapes of your TestRestoreCombinedFusedTensorParallel, with fp32 rows, on 2 gloo ranks: 48 tokens, top-k 4, 8 experts, hidden 130, uneven per-peer row counts (98 and 94), a different upstream gradient per peer.
- On both ranks,
restore_combined_fusedmatches the unpartitioned reference bitwise. - The summed row gradients differ by at most 1.2e-7, and the score gradients by at most 4.8e-6.
- In fp32 it is also bitwise equal to the default
restore_combined: output, row gradients and score gradients.
So the new indexing, gather and score scatter check out. The kernel itself is unchanged and wasn't exercised here. The CPU-runnable tests in test_autoep_autotp_dispatch.py, test_autoep_unit.py and test_autoep_fused_parity.py pass (202); the rest need CUDA, most also Triton.
Merge note: this branch merges cleanly with master. It conflicts with #8745 only in tests/unit/v1/moe/test_autoep_unit.py.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
…s in the config docs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
Resolve the conflict with deepspeedai#8745 in tests/unit/v1/moe/test_autoep_unit.py: keep its two skip_recompute_combine tests and this PR's test_fused_combine_accepts_folded_tensor_parallelism. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
Codex Review Summary: ✅ CompletedThis 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. |
AutoEP: support
combine_impl="fused_weighted_sum"with AutoEP+AutoTP foldingPart of: #8672 (Q4 2026 roadmap), "Mixture-of-Expert (MoE) Training Scaling and Kernel Development": "AutoEP Performance Optimization & Parallel Folding (AutoEP + AutoTP)" (PoC @tohtana @pengdurice). Follows #8670.
Problem
combine_impl="fused_weighted_sum"was refused whenevertensor_parallel.autotp_size > 1:deepspeed/module_inject/auto_ep_config.py(validate_autoep_config) raisedValueError.deepspeed/module_inject/auto_ep_layer.py(set_deepspeed_parallelism) raised the same error.The reason was that the folded path restores tokens with its own function,
restore_combinedindeepspeed/moe/ep_tp_dispatch.py, and the fused kernel was only wired into the unfolded path.restore_combinedworks like this:partition_assignments).TP group.
index_add_pertop-k slot, all in the activation dtype.
So under folding, users could not get the memory saving and the FP32 accumulation of the fused kernel, and the
folded restore did its arithmetic in bf16.
Change
restore_combined_fusedindeepspeed/moe/ep_tp_dispatch.py:restore_combinedfor the rows and the routing weights, so thetp_sizegradient factor that the folded AVERAGE and EXPERT_TP_CANCEL strategies cancel is unchanged.token * top_k + slot) instead of the token index and the slot separately.[tokens, top_k], and calls the existingfused_weighted_restore. The kernel is unchanged.RuntimeErrorif the gathered rows do not cover every (token, slot). The folded payload neverdrops or pads assignments today, so this cannot trigger now; it protects the kernel from reading an unset source
row if capacity dropping is added later.
validate_coverage=Trueruns the same coverage check asrestore_combined.AutoEPMoELayer._forwardcallsrestore_combined_fusedin the folded branch whencombine_impl == "fused_weighted_sum". The default ("auto"→"weighted_sum") still callsrestore_combined.autotp_size > 1refusals are removed. The other fused refusals stay:expert_tensor_parallel_size > 1,DeepEP,
score_applyother than"post", and unsupported device or dtype.docs/code-docs/source/autoep.rstdescribes the folded fused restore and drops the refusal from the list;docs/_pages/config-json.mddrops theautotp_size=1requirement and lists float32 among the accepted activationdtypes, as the kernel's check does.
Tests
On master e35282b with this change, pytest 8.3.5:
test_autoep_unit.py(includesTestAutoEPConfig::test_fused_combine_accepts_folded_tensor_parallelism, was..._rejects_...)tp_size=2; no other config changetest_autoep_autotp_dispatch.py(includestest_restore_combined_fused_rejects_a_missing_assignmentandTestRestoreCombinedFusedTensorParallel, fp32 and bf16 rows)fused_weighted_restore, and the row and score gradients, summed over the peers, match the unpartitioned gradientstest_autoep_fused_parity.py::TestAutoEPFusedParityFoldedTensorParallel(ZeRO 0, 2 and 3, with and without activation checkpointing)test_autoep_fused_parity.py(master's cases)test_autoep_fused_token_ops.pytest_autoep_autotp_grad_parity.pywithDEEPSPEED_RUN_H100_TESTS=1test_auto_ep_comm.py,test_autoep_autotp_folding_config.pytest_autoep_expert_activation.pycombine_implHardware: 1 node of 8 NVIDIA H100 80GB (driver 595.112.07); torch 2.8.0+cu126 with Triton 3.4.0, and torch
2.9.1+cu128. JIT-built ops used the CUDA 12.8 toolkit (
CUDA_HOME).Every failure and stall above also happens on master without this change, and passes or does not occur on torch 2.9.1:
test_auto_ep_comm.py::TestSkipRecomputeCombine(6 cases, added by AutoEP: opt-in skip of the recomputed DeepEP combine #8745) runstorch._grouped_mmon the CPU,which torch 2.8 does not implement (
Could not run 'aten::_grouped_mm' with arguments from the 'CPU' backend); thesame 6 fail on master e35282b.
test_autoep_fused_parity.py(TestAutoEPFusedParityExpertParallelx 16,TestAutoEPFusedParityLocalExperts) fail on torch 2.8 withRuntimeError: Expected mat_a to be BFloat16 matrix got Half, also on master 3fe2d95 without this change: torch 2.8'storch._grouped_mmaccepts only bf16, and thesecases run in fp16 with grouped GEMM.
torch._grouped_mmdoes not return for some shapes when one expert receives no rows:test_autoep_expert_activation.py::TestFusedGateUp::test_matches_separate_with_skewed_and_empty_expertsstallsthere the same way on master 3fe2d95 without this change.
The folded test reads each parameter before and after the step. Under ZeRO-3 a parameter holds only its partition, so
the test's
_parametershelper now reads the full fp32 value of ZeRO-3 parameters (safe_get_full_fp32_param); inthese fp32 runs that is the parameter itself. Master's cases have no ZeRO-3 parameters and are unaffected.
The end-to-end folded test runs in FP32, on
MockMoEOnlyTransformer, without grouped GEMM:two restores differ by 1 to 1.5 units in the last place of the MoE output (about 1,600 in this model), which is more
than
PARITY_TOLERANCEallows after the LM head. In FP32 the two forward passes are bitwise equal. Since both pathsrun in FP32, the folded test compares them directly; master's fp16 cases are judged against an FP32 reference
instead (Fix torch-latest workflow unit failures #8950), which here would equal the default path.
nn.MultiheadAttentionhas a true gradient of zero, so its computed gradient isrounding noise, and Adam's first step turns that noise into ±lr. The parameter-update check then fails by 2 × lr no
matter which restore is used.
torch._grouped_mmaccepts only bf16, so the fp32 run would fail before reaching therestore.
pre-commit run --filespasses on the changed files.Benchmark
One TP group of 2 H200 GPUs on one node. bf16 rows, FP32 routing weights, random routing with distinct experts per
token. The hidden size, top-k and expert count are the MiniMax-M3 and GLM-5.2 values. Memory is the peak above the
inputs. Time is one forward and backward, averaged over 10 runs after 3 warm-up runs, and includes the identical
partition step. Errors are against an FP64 reference. (Measured on master 5f266e2; neither
ep_tp_dispatch.pynor the kernel'sfused_weighted_restorehas changed on master since.)The fused output error of 1.66e-3 is the rounding of the bf16 output itself. The score gradient is the router
gradient; the default folded path computes it through bf16 weights, the fused path in FP32.
Script:
bench_folded_restore.py(run withtorchrun --nproc_per_node=2).Notes
multiplies and adds in the activation dtype.
end-to-end test covers ZeRO 0, 2 and 3.
Duplicate check
Searched open pull requests on 2026-11-13 and again on 2026-11-17 ("fused_weighted_sum", "AutoEP AutoTP folding", "folding universal"): none
enables the fused combine under folding. #8829 (optimizer offload under folding) and #8823 (clipping under folding)
are merged and change other parts of folding; #8745 and #8739 change the DeepEP combine.
AI assistance
This change, its tests and this description were prepared with AI assistance (Claude).