Skip to content

Add experimental Megatron-FSDP fully_shard implementation#4976

Closed
wujingyue wants to merge 13 commits into
NVIDIA:pull-request/4835from
wujingyue:fsdp/minimal
Closed

Add experimental Megatron-FSDP fully_shard implementation#4976
wujingyue wants to merge 13 commits into
NVIDIA:pull-request/4835from
wujingyue:fsdp/minimal

Conversation

@wujingyue

@wujingyue wujingyue commented May 26, 2026

Copy link
Copy Markdown
Contributor

Stacked on top of #4835.

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, ParameterGroup, 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
  • 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

  • BASE_REF=pull-request/4835 CHECK_ONLY=true bash tools/autoformat.sh
  • git diff --check origin/pull-request/4835..HEAD

@copy-pr-bot

copy-pr-bot Bot commented May 26, 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.

Comment thread megatron/core/distributed/fsdp/src/megatron_fsdp/experimental/fully_shard.py Outdated
Comment thread megatron/core/distributed/fsdp/src/megatron_fsdp/experimental/fully_shard.py Outdated
Comment thread megatron/core/distributed/fsdp/src/megatron_fsdp/experimental/fully_shard.py Outdated
@wujingyue wujingyue changed the base branch from main to pull-request/4835 June 10, 2026 22:58
@wujingyue wujingyue changed the title Add minimal experimental FSDP path Add experimental Megatron-FSDP fully_shard path Jun 11, 2026
@wujingyue wujingyue changed the title Add experimental Megatron-FSDP fully_shard path Add experimental Megatron-FSDP fully_shard implementation Jun 11, 2026
@wujingyue wujingyue marked this pull request as ready for review June 11, 2026 04:26
@wujingyue wujingyue requested review from a team as code owners June 11, 2026 04:26
wujingyue added 13 commits June 16, 2026 22:22
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>
else:
self.model_weight = DBuffer(
mesh=self.mesh,
placements=model_weight_placements,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From @Autumn1998: seems similar to how we create unsharded_model_weight. Consider dedup.


return grad_hook

def pre_forward(self) -> None:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From @Autumn1998 : pre_forward/forward/post_forward may be called multiple times when activation recomputation is on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Autumn1998 to provide the code pointer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should probably call module.forward instead of module(...) when the intention is to avoid triggering hooks.

@copy-pr-bot copy-pr-bot Bot deleted the branch NVIDIA:pull-request/4835 June 17, 2026 04:18
@copy-pr-bot copy-pr-bot Bot closed this Jun 17, 2026
@wujingyue

Copy link
Copy Markdown
Contributor Author

Recovered as replacement draft PR #5387 after this PR was closed due to the deleted base ref. The branch has been rebased directly on main: #5387

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants