Skip to content

Overlap experimental FSDP communication - #5416

Draft
wujingyue wants to merge 5 commits into
NVIDIA:mainfrom
wujingyue:single-comm-stream
Draft

Overlap experimental FSDP communication#5416
wujingyue wants to merge 5 commits into
NVIDIA:mainfrom
wujingyue:single-comm-stream

Conversation

@wujingyue

@wujingyue wujingyue commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a lazy FsdpContext shared by nested experimental FSDP units.
  • Delay full-parameter storage release until stream-ordered consumers are done.
  • Launch packed reduce-scatters asynchronously from backward and finalize their temporary buffers after communication completes.
  • Use one CUDA communication stream for both all-gather and reduce-scatter work to keep AG/RS temporary buffers on one allocator stream.
  • Add unconditional FSDP NVTX compute ranges such as FSDP:layers.0:forward_compute and FSDP:layers.0:backward_compute for nsys attribution.
  • Expand experimental FSDP tests for context sharing, delayed release, AG/compute overlap, RS/compute overlap, shared AG/RS stream ownership, and NVTX range naming.

Why

This brings the doublebuffer communication-overlap changes onto the experimental FSDP path now merged in main. The single communication stream intentionally serializes AG and RS relative to each other, but keeps temporary communication buffers owned by one PyTorch caching-allocator stream. The NVTX ranges make the resulting compute/communication overlap easier to inspect in nsys.

Validation

  • BASE_REF=main CHECK_ONLY=true SKIP_DOCS=false bash tools/autoformat.sh
  • python -m py_compile megatron/core/distributed/fsdp/src/megatron_fsdp/experimental/module.py megatron/core/distributed/fsdp/src/megatron_fsdp/experimental/parameter_group.py tests/unit_tests/distributed/megatron_fsdp/test_experimental_fully_shard.py
  • CUDA_VISIBLE_DEVICES=0,1 MASTER_PORT=29569 uv run --no-sync python -m torch.distributed.run --nproc-per-node 2 -m pytest -q --experimental tests/unit_tests/distributed/megatron_fsdp/test_experimental_fully_shard.py::test_fsdp_nvtx_ranges_use_named_module_paths tests/unit_tests/distributed/megatron_fsdp/test_experimental_fully_shard.py::test_fully_sharded_root_with_child_units_overlaps_all_gather_and_compute
  • CUDA_VISIBLE_DEVICES=0,1 MASTER_PORT=29570 uv run --no-sync python -m torch.distributed.run --nproc-per-node 2 -m pytest -q --experimental tests/unit_tests/distributed/megatron_fsdp/test_experimental_fully_shard.py

@copy-pr-bot

copy-pr-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@wujingyue
wujingyue force-pushed the single-comm-stream branch from 84cab1b to b7328b5 Compare June 22, 2026 04:30
@wujingyue wujingyue changed the title Use one stream for experimental FSDP communication Overlap experimental FSDP communication Jun 22, 2026
@wujingyue
wujingyue force-pushed the single-comm-stream branch from b7328b5 to c8706f9 Compare June 22, 2026 04:58
@wujingyue
wujingyue changed the base branch from pull-request/5387 to main June 23, 2026 23:17
@wujingyue
wujingyue force-pushed the single-comm-stream branch from c8706f9 to 0d42783 Compare June 24, 2026 21:33
@wujingyue wujingyue removed the MFSDPv2 label Jun 29, 2026
@wujingyue wujingyue self-assigned this Jun 29, 2026
@wujingyue wujingyue linked an issue Jul 4, 2026 that may be closed by this pull request
wujingyue and others added 2 commits July 9, 2026 18:44
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
wujingyue and others added 3 commits July 13, 2026 01:36
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Mirror the delayed-release path for gradient reductions: replace
PreparedReduction / launch_prepared_reductions with a DelayedReduction
dataclass, a delayed_reductions queue, enqueue_reduction(), and
drain_delayed_reductions(target_length) structured like drain_delayed_releases.

enqueue_reduction records a consumer event on the compute stream where the
gradients are packed, so each reduce-scatter waits only for its own packing
instead of a blanket reduce_scatter_stream.wait_stream(current_stream), removing
a synchronization bubble.

Drain at target_length=0 in pre_backward (right after the all-gather unshard) and
in the end-of-backward callback, so each reduction launches as soon as its
module's backward finishes and overlaps the next module's compute.

Drop the always-true consumer_event "is not None" guards (record_event never
returns None) and tighten DelayedRelease/DelayedReduction consumer_event to
torch.cuda.Event.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
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.

MFSDP v2 overlap, prefetching, and double-buffering schedule support

1 participant