Skip to content

[FSDP2] Add native collective copy ops - #197204

Closed
weifengpy wants to merge 61 commits into
gh/weifengpy/181/basefrom
gh/weifengpy/181/head
Closed

weifengpy wants to merge 61 commits into
gh/weifengpy/181/basefrom
gh/weifengpy/181/head

Conversation

@weifengpy

@weifengpy weifengpy commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

FSDP2 copies nonzero-dimension shards through intermediate buffers on both
sides of its collectives: the all-gather copy-out splits the rank-major
output by rank and concatenates the pieces along the shard dim, and the
reduce-scatter copy-in chunks each gradient and concatenates the chunks by
destination rank. This adds two private ops that copy directly between a
collective buffer and the parameter or gradient layout,
fsdp::_all_gather_copy_out_(out, input, split_sizes, outer_sizes, num_chunks)
and fsdp::_reduce_scatter_copy_in_(out, tensors, num_leading_dims, num_chunks).
Nothing in FSDP2 calls them yet; #197701 adds the opt-in that does.

The CUDA kernels live in ChunkCat.cu and reuse its chunk-cat kernel, which
chunk_cat_mixed_dtype now shares: the reduce-scatter copy-in treats each
index into a gradient's leading dims as one chunk-cat input and casts bf16
or fp16 into an fp32 buffer, and the all-gather copy-out copies each outer
slice of each output directly into place. Conjugate, negative,
non-contiguous, cross-device, and overlapping tensors take the composite
instead. The composites issue one strided copy per output or gradient, so a
functionalized trace does not grow with the outer sizes, and the SymInt
schemas support dynamic shapes. Both ops require exact output sizes.

Test Plan

python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardNativeCollectiveCopy -k TestFullyShardChunkCatMixedDtype

opcheck covers both ops on CPU and CUDA, a CUDA graph checks that a
copy-in and copy-out round trip replays on new data, and the
chunk_cat_mixed_dtype tests cover the shared kernel.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 2 Pending

As of commit 4e4d4e2 with merge base 381cc3e (image):
💚 Looks good so far! There are no failures yet. 💚

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 16, 2026
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 16, 2026
> AI-assisted draft prepared with Codex.
>
> FSDP2 currently uses temporary buffers and separate `cat` reorders for nonzero-dimension shards. For a contiguous `[N, F, D]` parameter with `Shard(1)`, this change passes N contiguous `[F, D]` views to the existing native copy kernels, folding the reorder into all-gather copy-out and reduce-scatter packing without a new device kernel.
>
> The same decomposition supports `Shard(k)` by flattening dimensions before k. It preserves the original parameter and gradient offsets, mixed-dtype all-gather byte offsets, and version counters. Custom all-gather layouts, post-forward shards, and noncontiguous gradients retain their existing copy paths.
>
> Test Plan:
> ```bash
> spin quicklint -a
> git diff --check
> ```
> Both checks passed.
>
> - Added two-rank training tests for N=1,2,3,5 with Shard(1), Shard(2), padded Shard(0), and FP32/BF16 inputs with FP32 reduction, plus mixed-dtype inference coverage.
> - Build and runtime tests are pending. Large-prefix performance remains to be measured because copy descriptor overhead grows with N.

ghstack-source-id: 167a089
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 16, 2026
> AI-assisted draft prepared with Codex.
>
> FSDP2 currently uses temporary buffers and separate `cat` reorders for nonzero-dimension shards. For a contiguous `[N, F, D]` parameter with `Shard(1)`, this change passes N contiguous `[F, D]` views to the existing native copy kernels, folding the reorder into all-gather copy-out and reduce-scatter packing without a new device kernel.
>
> The same decomposition supports `Shard(k)` by flattening dimensions before k. It preserves the original parameter and gradient offsets, mixed-dtype all-gather byte offsets, and version counters. Custom all-gather layouts, post-forward shards, and noncontiguous gradients retain their existing copy paths.
>
> Test Plan:
> ```bash
> spin quicklint -a
> git diff --check
> ```
> Both checks passed.
>
> - Added two-rank training tests for N=1,2,3,5 with Shard(1), Shard(2), padded Shard(0), and FP32/BF16 inputs with FP32 reduction, plus mixed-dtype inference coverage.
> - Build and runtime tests are pending. Large-prefix performance remains to be measured because copy descriptor overhead grows with N.

ghstack-source-id: 74f4015
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> FSDP2 currently uses temporary buffers and separate `cat` reorders for nonzero-dimension shards. For a contiguous `[N, F, D]` parameter with `Shard(1)`, this change passes N contiguous `[F, D]` views to the existing native copy kernels, folding the reorder into all-gather copy-out and reduce-scatter packing without a new device kernel.
>
> The same decomposition supports `Shard(k)` by flattening dimensions before k. It preserves the original parameter and gradient offsets, mixed-dtype all-gather byte offsets, and version counters. Custom all-gather layouts, post-forward shards, and noncontiguous gradients retain their existing copy paths.
>
> Test Plan:
> ```bash
> spin quicklint -a
> git diff --check
> ```
> Both checks passed.
>
> - Added two-rank training tests for N=1,2,3,5 with Shard(1), Shard(2), padded Shard(0), and FP32/BF16 inputs with FP32 reduction, plus mixed-dtype inference coverage.
> - Build and runtime tests are pending. Large-prefix performance remains to be measured because copy descriptor overhead grows with N.

ghstack-source-id: 6a04985
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> FSDP2 currently uses temporary buffers and separate `cat` reorders for nonzero-dimension shards. For a contiguous `[N, F, D]` parameter with `Shard(1)`, this change passes N contiguous `[F, D]` views to the existing native copy kernels, folding the reorder into all-gather copy-out and reduce-scatter packing without a new device kernel.
>
> The same decomposition supports `Shard(k)` by flattening dimensions before k. It preserves the original parameter and gradient offsets, mixed-dtype all-gather byte offsets, and version counters. Custom all-gather layouts, post-forward shards, and noncontiguous gradients retain their existing copy paths.
>
> Test Plan:
> ```bash
> spin quicklint -a
> git diff --check
> ```
> Both checks passed.
>
> - Added two-rank training tests for N=1,2,3,5 with Shard(1), Shard(2), padded Shard(0), and FP32/BF16 inputs with FP32 reduction, plus mixed-dtype inference coverage.
> - Build and runtime tests are pending. Large-prefix performance remains to be measured because copy descriptor overhead grows with N.

ghstack-source-id: ed5fdbe
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add optional implementations of the copy-preparation hooks from the parent PR. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> The default remains reorder preparation. Users opt in by registering the views hooks independently for all-gather copy-out and reduce-scatter copy-in. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardNonzeroDimCopy -v
> spin quicklint -a
> git diff --check
> ```
> The two test methods cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference, with default and explicitly registered views callbacks.
> Both test methods and lint passed.

ghstack-source-id: e4bad30
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add optional implementations of the copy-preparation hooks from the parent PR. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> The default remains reorder preparation. Users opt in by registering the views hooks independently for all-gather copy-out and reduce-scatter copy-in. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardNonzeroDimCopy -v
> spin quicklint -a
> git diff --check
> ```
> The two test methods cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference, with default and explicitly registered views callbacks.
> Both test methods and lint passed.

ghstack-source-id: 5c74742
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add independent setters for all-gather output preparation and reduce-scatter input preparation, with recursive registration by default. Store the selected callbacks on each FSDP parameter group and pass them to the collective implementations. The default callbacks use the reorder helpers extracted by the parent PR.
>
> Add optional contiguous-view callbacks. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> Users opt in by registering the views callbacks independently. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> The registration test covers recursive and local selection, independent callbacks, execution, and restoring defaults. Numerical tests cover training and inference with default and views callbacks. Update the existing shared-communication-context test wrapper to forward the new callback keyword argument.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardInputOutputFns TestFullyShardNonzeroDimCopy -v
> CUDA_VISIBLE_DEVICES=3,4 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShardShareCommContext.test_share_comm_context -v
> spin quicklint -a
> git diff --check
> ```
> All four targeted test methods and lint passed. Numerical cases cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference.

ghstack-source-id: 5c74742
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add independent setters for all-gather output preparation and reduce-scatter input preparation, with recursive registration by default. Store the selected callbacks on each FSDP parameter group and pass them to the collective implementations. The default callbacks use the reorder helpers extracted by the parent PR.
>
> Add optional contiguous-view callbacks. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> Users opt in by registering the views callbacks independently. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> The registration test covers recursive and local selection, independent callbacks, execution, and restoring defaults. Numerical tests cover training and inference with default and views callbacks. Update the existing shared-communication-context test wrapper to forward the new callback keyword argument.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardInputOutputFns TestFullyShardNonzeroDimCopy -v
> CUDA_VISIBLE_DEVICES=3,4 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShardShareCommContext.test_share_comm_context -v
> spin quicklint -a
> git diff --check
> ```
> All four targeted test methods and lint passed. Numerical cases cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference.

ghstack-source-id: c0c5eb7
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add independent setters for all-gather output preparation and reduce-scatter input preparation, with recursive registration by default. Store the selected callbacks on each FSDP parameter group and pass them to the collective implementations. The default callbacks use the reorder helpers extracted by the parent PR.
>
> Add optional contiguous-view callbacks. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> Users opt in by registering the views callbacks independently. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> The registration test covers recursive and local selection, independent callbacks, execution, and restoring defaults. Numerical tests cover training and inference with default and views callbacks. Update the existing shared-communication-context test wrapper to forward the new callback keyword argument.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardInputOutputFns TestFullyShardNonzeroDimCopy -v
> CUDA_VISIBLE_DEVICES=3,4 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShardShareCommContext.test_share_comm_context -v
> spin quicklint -a
> git diff --check
> ```
> All four targeted test methods and lint passed. Numerical cases cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference.

ghstack-source-id: d5af010
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add independent setters for all-gather output preparation and reduce-scatter input preparation, with recursive registration by default. Store the selected callbacks on each FSDP parameter group and pass them to the collective implementations. The default callbacks use the reorder helpers extracted by the parent PR.
>
> Add optional contiguous-view callbacks. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> Users opt in by registering the views callbacks independently. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> The registration test covers recursive and local selection, independent callbacks, execution, and restoring defaults. Numerical tests cover training and inference with default and views callbacks. Update the existing shared-communication-context test wrapper to forward the new callback keyword argument.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardInputOutputFns TestFullyShardNonzeroDimCopy -v
> CUDA_VISIBLE_DEVICES=3,4 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShardShareCommContext.test_share_comm_context -v
> spin quicklint -a
> git diff --check
> ```
> All four targeted test methods and lint passed. Numerical cases cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference.

ghstack-source-id: a880502
Pull-Request: #197204
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Sep 17, 2026
> AI-assisted draft prepared with Codex.
>
> Add independent setters for all-gather output preparation and reduce-scatter input preparation, with recursive registration by default. Store the selected callbacks on each FSDP parameter group and pass them to the collective implementations. The default callbacks use the reorder helpers extracted by the parent PR.
>
> Add optional contiguous-view callbacks. For a contiguous `[N, F, D]` parameter with `Shard(1)`, they pass N contiguous views to the existing native copy kernels, avoiding separate all-gather and reduce-scatter reorder copies. Flattening the prefix generalizes this to `Shard(k)`.
>
> Users opt in by registering the views callbacks independently. Unsupported layouts retain their existing paths. All-gather adjusts split sizes for expanded destinations, including mixed-dtype byte offsets, and skips inference tensors when preserving fallback reorder version counters.
>
> The registration test covers recursive and local selection, independent callbacks, execution, and restoring defaults. Numerical tests cover training and inference with default and views callbacks. Update the existing shared-communication-context test wrapper to forward the new callback keyword argument.
>
> Test Plan:
> ```bash
> CUDA_VISIBLE_DEVICES=1,2 python test/distributed/_composable/fsdp/test_fully_shard_comm.py TestFullyShardInputOutputFns TestFullyShardNonzeroDimCopy -v
> CUDA_VISIBLE_DEVICES=3,4 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShardShareCommContext.test_share_comm_context -v
> spin quicklint -a
> git diff --check
> ```
> All four targeted test methods and lint passed. Numerical cases cover N=1,2,3,5, Shard(1), Shard(2), padded Shard(0), FP32/BF16 training, and mixed-dtype inference.

ghstack-source-id: a880502
Pull-Request: #197204
[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x 7aa17e49b00f4af72e5fd536a1a22a4e965fb493 returned non-zero exit code 1

Auto-merging test/distributed/_composable/fsdp/test_fully_shard_training.py
Auto-merging torch/distributed/fsdp/_fully_shard/_fsdp_api.py
CONFLICT (content): Merge conflict in torch/distributed/fsdp/_fully_shard/_fsdp_api.py
Auto-merging torch/distributed/fsdp/_fully_shard/_fsdp_param.py
Auto-merging torch/distributed/fsdp/_fully_shard/_fsdp_param_group.py
error: could not apply 7aa17e49b00... [FSDP2] Separate collective copy preparation from execution
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Details for Dev Infra team Raised by workflow job

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@weifengpy

Copy link
Copy Markdown
Contributor Author

@pytorchmergebot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: New commits were pushed while merging. Please rerun the merge command.

Details for Dev Infra team Raised by workflow job

@weifengpy

Copy link
Copy Markdown
Contributor Author

@pytorchmergebot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

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

Labels

ciflow/h100-symm-mem ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (fsdp2) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants