Skip to content

AutoEP: keep overlap_weight_grad's async combine tensors in DeepEP's event - #9105

Draft
pengdurice wants to merge 1 commit into
deepspeedai:masterfrom
pengdurice:peng-fix-autoep-overlap-weight-grad-non-reentrant-v1
Draft

pengdurice wants to merge 1 commit into
deepspeedai:masterfrom
pengdurice:peng-fix-autoep-overlap-weight-grad-non-reentrant-v1

Conversation

@pengdurice

Copy link
Copy Markdown
Contributor

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 the
returned 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_stream on every
tensor of the call, for the compute stream and for its own stream (stream_control_epilogue in DeepEP's
csrc/elastic/buffer.hpp). PyTorch's caching allocator then cannot reuse such a block until DeepEP's stream has passed
that 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 GPU
per 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 in
the returned event instead (EventHandle.tensors_to_record). They are released when the caller drops the event after
waiting on it, and their blocks are reusable at once. DeepEP reads the variable with std::getenv on every call.

This PR sets EP_AVOID_RECORD_STREAM=1 only around AutoEP's one asynchronous DeepEP call (the overlapped combine) and
restores 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):

Code overlap_weight_grad Step time Allocator flushes (14 steps, 8 GPUs) Peak allocated / reserved in step 14 (highest GPU) Global loss at step 14
master off 32.618 s 0 69.15 / 72.43 GiB 2.233572
master on 61.917 s 3,159 69.31 / 74.32 GiB 2.233508
master + this PR on 32.594 s 0 69.31 / 72.96 GiB 2.233546

With more free memory (bf16 master weights and bf16 Adam states, which master supports):

Code overlap_weight_grad Step time Allocator flushes Peak allocated / reserved in step 14 (highest GPU) Global loss at step 14
master off 32.566 s 0 62.04 / 65.34 GiB 2.263005
master on 32.471 s 0 62.21 / 73.82 GiB 2.263044
master, EP_AVOID_RECORD_STREAM=1 set by hand on 32.477 s 0 62.21 / 65.95 GiB 2.263084
master + this PR on 32.485 s 0 62.21 / 65.75 GiB 2.263108

With 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

  • Why scoped, not global: setting the variable once for the process would change every DeepEP V2 asynchronous call
    in the process, including calls made by other code. Scoping it to AutoEP's call changes nothing else.
  • A per-call DeepEP argument would be cleaner than an environment variable; DeepEP has none today.
  • The variable changes the process environment for the duration of one DeepEP call. AutoEP's backward issues that call
    from the training thread; nothing else reads the variable meanwhile.
  • No other asynchronous DeepEP call exists in DeepSpeed today, so no other path needs the change.

Tests

  • New (CPU): test_async_combine_keeps_its_tensors_in_the_event. The existing DeepEP stand-in records the variable
    during the asynchronous combine. It must be "1" during the call and the user's own value afterwards, both when the
    user left it unset and when they set it to "0". On master the call sees None and "0", so the test fails there.
  • test_overlapped_weight_gradients_match_the_serial_backward (4 GPUs, real DeepEP) now also runs with non-reentrant
    checkpointing: 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).

cd tests
pytest unit/module_inject/test_auto_ep_comm.py               # 75 passed (66 subtests)
pytest unit/v1/moe/test_autoep_deepep_parity.py -k 'overlapped_weight_gradients or non_reentrant_checkpointing_replays'
                                                             # 20 passed

pre-commit run --files passes on the three changed files.

Compatibility

Cell Validated
overlap_weight_grad, DeepEP v2, ZeRO 1 and 2, reentrant and non-reentrant checkpointing, 4 GPUs yes (unit tests)
overlap_weight_grad, 8 GPUs, ZeRO-2, Qwen3-30B-A3B, non-reentrant checkpointing, bf16 master and fp32 master + bf16 moments yes (the results above)
Without overlap_weight_grad unchanged: no asynchronous DeepEP call is made
ZeRO-3, offload, other accelerators unchanged: overlap_weight_grad already requires ZeRO 1/2 and DeepEP

Duplicate check

No open PR or issue mentions EP_AVOID_RECORD_STREAM or a slowdown of overlap_weight_grad with non-reentrant
checkpointing (searched GitHub for both).

AI assistance

This change, its tests and this description were prepared with AI assistance (Claude).

🤖 Generated with Claude Code

…event

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: pengdurice <pengduhit@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant