Skip to content

[FSDP2] Add an all-gather input preparation hook - #197500

Draft
weifengpy wants to merge 1 commit into
gh/weifengpy/186/basefrom
gh/weifengpy/186/head
Draft

weifengpy wants to merge 1 commit into
gh/weifengpy/186/basefrom
gh/weifengpy/186/head

Conversation

@weifengpy

Copy link
Copy Markdown
Contributor

"add the input hook" as only refactor of existing logic. then integration with #188276 can be a follow up later

AI-assisted draft prepared with Codex.

  • Extract existing input preparation behind set_all_gather_input_fn, with recursive registration.
  • Preserve default packing, allocation, stream ordering, and communication.
  • Tests: exact training and packing parity, existing all-gather paths, compiled forward/backward, and lint.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197500

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (2 Unrelated Failures)

As of commit f3d32c9 with merge base 3fda599 (image):

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (fsdp) release notes category labels Sep 18, 2026
wuyl1 pushed a commit to wuyl1/pytorch that referenced this pull request Sep 30, 2026
> AI-assisted implementation prepared with Codex.
>
> Extract the existing preparation, metadata collection, backend allocation,
> and rank-major copy-in into a default callback. Add recursive registration
> through FSDPModule.set_all_gather_input_fn. Stream dependencies, collective
> launch, output processing, and the world-size-one shortcut are unchanged.
>
> The existing input/output storage aliasing contract is retained. Backend
> layout integration and borrowed-output lifetime changes are separate work.
>
> Test Plan
>
> Exact training parity and recursive/local dispatch pass on two GPUs:
> ```bash
> CUDA_VISIBLE_DEVICES=0,1 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShard1DTrainingCore.test_all_gather_input_fn TestFullyShardShareCommContext.test_share_comm_context -v
> ```
>
> Existing all-gather, Inductor forward and compiled-autograd tests pass.
> All 64 original/extracted packing comparisons match bytes, metadata,
> allocator arguments, storage aliases and rank offsets. Lint passes.
> The threaded-PG all-gather fixture emits its existing
> pg_flight_recorder_hooks teardown error after passing its assertions.

ghstack-source-id: 79c72e8
Pull-Request: pytorch#197500
weifengpy added a commit that referenced this pull request Oct 5, 2026
…t hook and add an input hook

> AI-assisted draft prepared with Claude Code for the all-gather contract discussion on #188276. It is one of three alternatives, #199720 (option 1), #199721 (option 2) and #199726 (option 3), and is not ready for review.
>
> This keeps the stack's tensor-only hooks as the all-gather contract and extends them to cover what #188276's MORI SDMA backend needs. `AllGatherOutputFn` may now return one tensor per output instead of writing into `outputs`. When the comm sets `AllGather.reuses_output_storage` and the group is eligible (every payload has outer size 1 and the buffer dtype, and no parameter is post-forward, a DTensor, an extension, or under compiled autograd), FSDP adopts the returned views as the parameters' all-gather outputs on their first all-gather, so later gathers are zero-copy. Otherwise FSDP copies the returned tensors into its own outputs. A new tensor-only `AllGatherInputFn`, set with `FSDPModule.set_all_gather_input_fn`, packs the collective input; it reworks #197500 without its `AllGatherInput` name, which #197204 now uses for payload records. From #188276 this ports `AllGather.release_output()` and its call sites, the output ownership flag, honoring the outputs' storage offset in the unsharded parameters, the version-counter guards, and holding the collective input until the copy-out. It drops the `AllGatherLayout` object model.
>
> A zero-copy backend is three separately installed pieces (comm, input function, output function) that agree on the layout through the backend's own state, and adopted storage stays allocated across reshard, as in #188276. On the default path, `_to_sharded` now discards a pending all-gather and `_unsharded_param` is always an attribute. Review the contracts and adoption in `_fsdp_collectives.py` first, then ownership and release in `_fsdp_param.py` and `_fsdp_param_group.py`, then the setters in `_fully_shard.py`. The tests port #188276's pure-PyTorch parameter-contiguous backend and its end-to-end parity tests onto the hooks.
>
> Test Plan (local build, 4 B200 GPUs):
> ```bash
> python test/test_public_bindings.py -k test_correct_module_names
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -v
> python test/distributed/_composable/fsdp/test_fully_shard_training.py -v -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py -v
> python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py -v -k test_reduce_dtype
> ```
> All pass except `test_fully_shard_alloc_from_pg`, which fails the same way on the parent commit.

ghstack-source-id: 68dc861
Pull-Request: #199720
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Nov 17, 2026

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

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (fsdp) release notes category Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant