Skip to content

Add experimental Megatron-FSDP fully_shard implementation - #5387

Merged
wujingyue merged 24 commits into
NVIDIA:mainfrom
wujingyue:fsdp/minimal
Jun 24, 2026
Merged

Add experimental Megatron-FSDP fully_shard implementation#5387
wujingyue merged 24 commits into
NVIDIA:mainfrom
wujingyue:fsdp/minimal

Conversation

@wujingyue

@wujingyue wujingyue commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Recovered replacement for #4976, which GitHub closed after its base ref pull-request/4835 was deleted. This branch is now rebased directly on main.

What does this PR do ?

Adds an experimental per-module Megatron-FSDP fully_shard path that uses DBuffer primitives to shard parameters, materialize full weights for compute, and reduce gradients back into sharded optimizer state.

Meta-parameter materialization is intentionally split out to the follow-up draft PR at #5369.

Issue tracking

Linked issue: N/A

Summary

  • add the experimental fully_shard(...) entry point plus FsdpModule, FsdpParameterGroup, and Placements runtime state
  • group parameters by dtype and requires_grad, manage sharded main/model weight buffers, and sync optimizer-updated weights into compute weights
  • allocate model/gradient buffers from DBuffer.device instead of reaching through local_buffer.device
  • install forward/backward hooks for unshard, reshard, and parameter-completion-based gradient reduction, including accumulation across backward calls
  • document storage-lifetime choices for persistent main gradients, autograd-saved unsharded parameter views, and the shared reshard storage-release path
  • extend distributed coverage for DBuffer layout/redistribution and the experimental FSDP path, including nested ownership, frozen parameters, optimizer-step visibility, CPU-initialized parameter sharding, autograd storage reuse, loss parity, and peak-memory reduction

Testing

  • git diff --check origin/main..HEAD
  • git diff --check for the latest comment-only update
  • uv run --no-sync python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/megatron_fsdp/test_dbuffer.py tests/unit_tests/distributed/megatron_fsdp/test_experimental_fully_shard.py (27 passed, 6 skipped)

wujingyue added 13 commits June 17, 2026 04:25
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Summarize today's FSDP work:

- Add DBuffer storage release/reallocate support and an in-place fully_allgather_into path for materializing replicated buffers.

- Simplify ParameterGroup and FsdpModule around sharded DTensor parameters, reused unsharded Parameters, meta materialization, and default-stream unshard/reshard/reduce behavior.

- Remove unused optimizer/offload/state/helper surface area from the minimal path and keep version-counter preservation scoped to unsharded model-weight materialization.

- Expand DBuffer and experimental FSDP tests for layouts, storage lifecycle, DTensor contracts, meta reset, nested ownership, train-step parity, and peak-memory reduction.

Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Add out= support to DBuffer redistribution and primitive communication ops, keeping axis inference in redistribute only.

Use preallocated model and gradient buffers in the minimal FSDP path where possible, including direct first-gradient reduce-scatter into main_grad.

Update DBuffer and experimental FSDP tests for AVG reductions, explicit primitive axes, storage reuse, and gradient accumulation behavior.

Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Use ordered parameter tuples for FSDP parameter swapping and keep the sharded data path aligned with main_weight storage.

Set grad_dtype for FSDP-managed parameters so BF16 main gradients can be reduced without pre-reduce casts, and update tests to verify sharded parameter data and grad backing buffers.

Clean up hook naming, local gradient accumulation handling, and memory/test assertions for the minimal experimental path.

Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Add DBuffer casting for dtype conversion before model-weight sync, refresh model weights from main weights before unshard, and cover the next-forward optimizer update path with FP32 main weights and default BF16 main grads on SGD's non-foreach path.

Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
@wujingyue

Copy link
Copy Markdown
Contributor Author

/ok to test

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28079891589

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

Labels

Approved All necessary approvals have been made complexity: medium Run tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bare functional FSDP with no performance optimizations

5 participants