Repository navigation
[deepep] Fix wrong gradients under FullAC: save dispatch and combine instead of replaying them - #5123
Merged
tianyu-l merged 2 commits intoOct 7, 2026
Conversation
added 2 commits
October 7, 2026 07:49
…instead of replaying them FullAC, and RegionAC without ep_communication saved, replayed DeepEP's dispatch in backward. DeepEP assigns receive slots with atomics, so the replay can receive rows in another order, while the backward of dispatch and combine routes gradients with the forward's handle. Each token's gradient met another token's activations: at DSv3 671B shapes (EP=8, 4k tokens per rank) the routed-expert weight grads were 1.4 relative L2 off. The deepep ops are now registered as ordered effects, whose outputs PyTorch's selective checkpointing (FullAC's included) saves instead of replaying, and dispatch_tokens / combine_tokens always retain their remat regions (RegionAC). Grads match the no-AC run to run-to-run noise. FullAC gets 4.8% faster at 16k tokens per rank (no replayed dispatch or combine in backward) and saves both ops' outputs (+281 MiB per MoE layer at 4k).
…epEP CI install The new 2-GPU test applies FullAC, SelectiveAC and RegionAC (no saved regions) to a DeepEP MoE block, counts ElasticBuffer.dispatch/combine calls, and checks every gradient against the same block without AC. On main, FullAC and RegionAC replay both ops (3 calls instead of 2) and the gradients are about 100% relative L2 off; with the fix the call counts match and the gradients are bitwise equal at these sizes. It skips when deep_ep is not installed, so the H100 workflow runs it right after installing DeepEP. The DeepEP install cloned DeepEP's default branch with --recursive. Since DeepEP's 2026-09-29 release that branch has an SSH-only submodule, so the clone failed in CI and no DeepEP test ran. The pinned commit's submodules are still fetched after checkout.
anijain2305
approved these changes
Oct 7, 2026
felipemello1
marked this pull request as ready for review
October 7, 2026 18:13
felipemello1
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
October 7, 2026 18:13
wwwjn
approved these changes
Oct 7, 2026
tianyu-l
approved these changes
Oct 7, 2026
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.
TLDR: Under FullAC, and under RegionAC without
ep_communicationsaved, DeepEP computes wrong gradients for the routed experts, the router and the MoE input: at DeepSeek-V3 671B shapes the routed-expert weight grads are 1.41 relative L2 off. This PR saves DeepEP's dispatch and combine instead of replaying them in backward, as SelectiveAC already does. Gradients now match a run without AC, and FullAC gets 4.8% faster at 16k tokens per rank, for +1.1 GiB of saved activations per MoE layer at 16k. A new 2-GPU test fails on main and passes here. It runs in the H100 workflow, whose DeepEP install this PR also fixes.Problem
ep_communicationis not insave_regions.deep_ep/include/deep_ep/impls/dispatch.cuh:345), so the second dispatch can receive the rows in another order.Solution
deepep::dispatchanddeepep::combineare registered as ordered effects. PyTorch's selective checkpointing saves an ordered-effect op unless the policy explicitly asksMUST_RECOMPUTE(torch/utils/checkpoint.py:1618-1626). That covers FullAC, SelectiveAC and its subclasses; FullAC's docstring already relies on it.dispatch_tokensandcombine_tokensalways retain their remat regions, which covers RegionAC (torch_remat). Theirrecomputeargument is gone, andep_communicationinsave_regionsno longer affects DeepEP._handle_cache.Test
tests/unit_tests/gpu/test_deepep_activation_checkpoint.py(2 GPUs) applies FullAC, SelectiveAC and RegionAC with no saved regions to a DeepEP MoE block.ElasticBuffer.dispatchandcombinecalls, and compares every gradient with the same block without AC.deep_ep, so the unit-test workflows skip it. The H100 workflow runs it right after installing DeepEP, as the B200 workflow does for Dist-MoE.--recursive, and DeepEP's 2026-09-29 release added an SSH-only submodule there. This PR clones without--recursive; the pinned commit's submodules are still fetched after checkout.Results
The noise comes from DeepEP's receive order (expert weight grads) and the bf16 atomic adds in torchtitan's eager gather/scatter around the grouped GEMM (input grads).
Training, DSv3 debugmodel, FSDP8+EP8, seed 0, step-2 loss / grad norm:
NOTE: no recipe pairs DeepEP with FullAC or RegionAC today: the DeepEP recipes and the CI test use SelectiveAC. Nothing rejects the combination, and on main it trains with these wrong gradients without an error.
NOTE: in DeepEP MoE layers, FullAC now saves almost as much as SelectiveAC (677 vs 744 MiB for 2 layers at 4k), at the same speed at 4k (74.3 vs 74.1 ms) and 1.7% slower at 16k (261.4 vs 257.0 ms). Attention and dense layers are unchanged: FullAC still recomputes them.
NOTE: RegionAC configs without
ep_communicationnow also keep DeepEP's comm outputs (+281 MiB per MoE layer at 4k).NOTE: needs torch nightly 20260926 or later: older torch registers the effect, but its checkpointing still replays the ops. torchtitan's FullAC already relies on the same mechanism.
NOTE: not run on GPU: pipeline parallelism. On a CPU probe with a fake DeepEP buffer, FullAC + DeepEP under split input/weight backward (ZBV, DualPipeV, InterleavedZeroBubble) now raises "Trying to backward an extra time" instead of silently giving wrong gradients. SelectiveAC already fails there, so DeepEP with these schedules has no working activation checkpointing until the split backward supports saved effectful ops.
You may ask:
_lib._register_effectful_op, a private method?custom_op(...).register_effectis the public form, but these ops are defined withtorch.library.Library("deepep", "DEF"), andregister_effectonly calls_lib._register_effectful_opitself (torch/_library/custom_ops.py:605-606).deepepcheck to_full_ac_policy? It puts a backend name in a generic policy, and a SelectiveAC subclass that overridesget_save_opswithout the deepep ops would replay them again (1.2 relative L2 on a CPU probe). The effect covers every policy.deep_ep, so the test would always skip there.Test plan
pytest tests/unit_tests/gpu/test_deepep_activation_checkpoint.pyon 2x H100: passed all 8 runs on this PR; fails on main with the replay counts above. Skips withoutdeep_ep.FullAC,SelectiveAC,RegionACwith and withoutep_communication); all gradients compared with a run without AC. Timing: the same harness with 2 MoE layers, 3 fresh processes per row, no other process on the GPUs.test_moe.py,test_expert_parallel.py,test_inference_moe.py,test_config_validation.py,test_transforms.py, the DSv3, Qwen3.5 and GPT-OSS MoE tests) andtests/unit_tests/gpu/test_activation_checkpoint.py: 98 passed. Pyrefly: same 41 errors as main.--recursive, checking out the pin and updating submodules fetches onlythird-party/fmt(run locally; the full CI job needsciflow/h100.8).Hardware: 8x H100 SXM, no RDMA NIC (
EP_DISABLE_GIN=1), DeepEP v2.1.0 (01dc3aa). Torch 2.15.0.dev20260926 for the layer tests, 2.15.0.dev20261004 for training and the new test.