Repository navigation
AutoEP: keep overlap_weight_grad's async combine tensors in DeepEP's event - #9105
Draft
pengdurice wants to merge 1 commit into
Draft
pengdurice wants to merge 1 commit into
pengdurice wants to merge 1 commit into
Conversation
…event Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
This branch has not been deployed
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.
Summary
expert_parallel.overlap_weight_grad(#8739) starts DeepEP's backward combine asynchronously(
async_with_compute_stream=True), computes the experts' weight gradients while it is in flight, then waits on thereturned event. With non-reentrant activation checkpointing, the default in Transformers 5, this makes training
slower, not faster: on Qwen3-30B-A3B the step takes 61.9 s instead of 32.6 s when memory is tight.
The cause is how DeepEP keeps the call's tensors alive. For an asynchronous V2 call it calls
record_streamon everytensor of the call, for the compute stream and for its own stream (
stream_control_epiloguein DeepEP'scsrc/elastic/buffer.hpp). PyTorch's caching allocator then cannot reuse such a block until DeepEP's stream has passedthat point; it does not wait for it. In a backward the host queues work far ahead of the GPU, so freed but unusable
blocks accumulate. When an allocation can no longer be served, the allocator synchronizes the device, returns its
whole cache to CUDA and retries. That "allocator flush" (counted in
num_alloc_retries) happened about 30 times per GPUper optimizer step in our runs, each costing a device synchronization and a round of
cudaFree/cudaMalloc.DeepEP has an option for exactly this: with
EP_AVOID_RECORD_STREAM=1, an asynchronous V2 call keeps its tensors inthe returned event instead (
EventHandle.tensors_to_record). They are released when the caller drops the event afterwaiting on it, and their blocks are reusable at once. DeepEP reads the variable with
std::getenvon every call.This PR sets
EP_AVOID_RECORD_STREAM=1only around AutoEP's one asynchronous DeepEP call (the overlapped combine) andrestores the previous value right after it. The backward already waits on the event before it reads the combine's
outputs and drops the event when it returns, which is the lifetime the option needs.
Results
Qwen3-30B-A3B (48 layers, 128 experts, top 8), the released checkpoint with its trained router, PG-19 text,
1 node of 8 x H100 80GB, expert parallel 8, ZeRO-2, micro-batch 4 x 4,096 tokens, 16 accumulation steps, full
activation recompute with Hugging Face non-reentrant checkpointing, DeepEP v2 on 64 SMs,
row_weighting_impl: "fused". 14 steps, step time = mean of steps 5-14; each table is one job on one node.When memory is tight (fp32 master weights with bf16 Adam states, using the fp32-master/bf16-moment option of a
separate PR in this series, because master cannot fit fp32 master weights with fp32 states here):
overlap_weight_gradWith more free memory (bf16 master weights and bf16 Adam states, which master supports):
overlap_weight_gradEP_AVOID_RECORD_STREAM=1set by handWith free memory, the overlap holds 8 GiB more in reserve than it allocates: blocks waiting for DeepEP's stream to
catch up. When that reserve is not available, the allocator runs out of free blocks and flushes, and the step takes
1.9x as long. With this PR the reserve returns to the level without the overlap, and the flushes are gone. The step
time is the same as without the overlap at this SM count; the overlap's own gain is small with 64 DeepEP SMs (#8739
measured 0.25 to 0.47 s at 20 and 12 SMs). Losses agree within the run-to-run spread of DeepEP's arrival order.
Design notes
in the process, including calls made by other code. Scoping it to AutoEP's call changes nothing else.
from the training thread; nothing else reads the variable meanwhile.
Tests
test_async_combine_keeps_its_tensors_in_the_event. The existing DeepEP stand-in records the variableduring the asynchronous combine. It must be
"1"during the call and the user's own value afterwards, both when theuser left it unset and when they set it to
"0". On master the call seesNoneand"0", so the test fails there.test_overlapped_weight_gradients_match_the_serial_backward(4 GPUs, real DeepEP) now also runs with non-reentrantcheckpointing: 16 cases (reentrant and non-reentrant x ZeRO 1 and 2 x even and skewed routing x separate and fused
gate+up). Before, this path had no GPU test with non-reentrant checkpointing.
Hardware: 1 node, 8 x NVIDIA H100 80GB HBM3, driver 595.112.07; NVIDIA PyTorch container, torch 2.14.0a0 (nv26.08),
CUDA 13.4, transformers 5.18.0, pytest 9.0.3; DeepEP v2 (
01dc3aa).pre-commit run --filespasses on the three changed files.Compatibility
overlap_weight_grad, DeepEP v2, ZeRO 1 and 2, reentrant and non-reentrant checkpointing, 4 GPUsoverlap_weight_grad, 8 GPUs, ZeRO-2, Qwen3-30B-A3B, non-reentrant checkpointing, bf16 master and fp32 master + bf16 momentsoverlap_weight_gradoverlap_weight_gradalready requires ZeRO 1/2 and DeepEPDuplicate check
No open PR or issue mentions
EP_AVOID_RECORD_STREAMor a slowdown ofoverlap_weight_gradwith non-reentrantcheckpointing (searched GitHub for both).
AI assistance
This change, its tests and this description were prepared with AI assistance (Claude).
🤖 Generated with Claude Code