Skip to content

[FSDP2] Extend ATen copy operators for nonzero-dimension sharding - #197784

Closed
weifengpy wants to merge 2 commits into
gh/weifengpy/193/basefrom
gh/weifengpy/193/head
Closed

weifengpy wants to merge 2 commits into
gh/weifengpy/193/basefrom
gh/weifengpy/193/head

Conversation

@weifengpy

@weifengpy weifengpy commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

draf the change as a peer PR to HEAD, so i can compare alternatives side by side

AI-assisted implementation and description prepared with Codex. This is an alternative to #197701 on the same parent, #197204.

The opt-in Shard(1+) copy callbacks can spend more CPU time creating Tensor views than submitting the copy. Extend the existing ATen split_with_sizes_copy and _chunk_cat families with .prefix_out overloads accepting the original tensors and concrete layout metadata. Native CUDA implementations prepare raw descriptors and launch the existing kernels.

split_with_sizes_copy.prefix_out treats its input as num_chunks flat rows. split_sizes partitions each row, and num_prefixes[i] divides an output into contiguous regions collecting the corresponding pieces from all rows. With uint8 input, split sizes count bytes and output dtypes may differ. _chunk_cat.prefix_out uses each input's num_leading_dims as its shard dimension and packs its prefixes into output rows. Nonzero shard dimensions require even division; dimension zero retains the existing padding behavior.

Descriptor preparation, validation, and fallback implementations now live in ATen. The separate FSDP prefix-copy operators and C++ files are removed, with no build-list changes relative to the parent. ReduceScatterInput and the opt-in callbacks remain; the default FSDP copy operator and existing ATen overloads retain their behavior. Review the native schemas and common validation first, the CUDA descriptor path next, then the FSDP call sites and tests.

CompositeImplicitAutograd provides the common decomposition for other backends and FakeTensor/compiler tracing. Two explicit Functionalize registrations use that same decomposition. Two exact Python-binding exclusions leave these caller-allocated operations accessible through torch.ops.aten without generating top-level Python bindings that require matching allocating variants. The new overloads also enforce out-operation autograd and version/inference rules.

On B200, using 224 MiB BF16 [N, F, D] inputs, Shard(1), eight shards, and FP32 reduce-scatter output, graph GPU medians differed by at most 0.31% from the previous #197784 implementation (d307e02db7b2) across two candidate runs. Each phase used one CUDA kernel and one metadata H2D copy; peak and retained CUDA allocations and view counts matched exactly. Eager times in microseconds were:

N AG: previous #197784 AG: native runs RS: previous #197784 RS: native runs
2 102.12 101.99 / 102.34 349.77 348.46 / 348.57
128 103.65 102.46 / 101.76 353.69 355.58 / 360.09
8192 264.17 281.62 / 265.32 625.00 619.67 / 616.97

N=2 and N=128 eager timings stayed within 2%; host submission was lower in both candidate runs. N=8192 all-gather eager time increased 6.61% in the first run and 0.44% on repeat, with host time 196.82 / 176.10 us versus 182.90 us. Exact CPU parity is not established. These separate-build measurements include submission/backpressure but exclude NCCL and model work. The baseline above is the prior #197784 implementation. Earlier #197701 measurements remain historical context: N=8192 AG/RS eager times were 244.04/594.08 us, so these results do not establish equal CPU performance with #197701 either.

Test Plan:

The CUDA 13 / SM100 editable build, lintrunner -a, and git diff --check passed. The regression suites ran 272 cases: 265 passed and seven expected skips. Coverage includes CPU/CUDA copies, mixed dtypes, noncontiguous fallback, device/stream handling, CUDA graphs, autograd and inference semantics, Functionalize, FakeTensor, fullgraph aot_eager compilation, and FSDP training parity. Both benchmark runs passed all three exact correctness cases.

bash agent_space/fsdp_aten_overloads/build.sh
CUDA_VISIBLE_DEVICES=4,6 python test/test_shape_ops.py TestPrefixCopyCPU TestPrefixCopyCUDA -v
CUDA_VISIBLE_DEVICES=7 python agent_space/fsdp_aten_overloads/dispatch_smoke.py -v
CUDA_VISIBLE_DEVICES=5 python test/distributed/_composable/fsdp/test_fully_shard_prefix_copy.py -v
CUDA_VISIBLE_DEVICES=0,2 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShard1DTrainingCore.test_train_parity_single_group_shard_dim0 TestFullyShard1DTrainingCore.test_train_parity_single_group_shard_largest_dim TestFullyShardShardPlacementFnMultiProcess.test_train_parity_shard_placement_fn_shard_largest_dim TestFullyShardShardPlacementFnMultiThread.test_shard_placement_fn_contiguous_params_grads -v
CUDA_VISIBLE_DEVICES=3 python test/test_ops.py TestCommonCUDA -k _chunk_cat -v
CUDA_VISIBLE_DEVICES=1 python test/test_torch.py TestTorch.test_split_with_sizes_copy_out -v
CUDA_VISIBLE_DEVICES=5 FSDP_BENCH_LABEL=candidate python agent_space/fsdp_aten_overloads/benchmark.py TestATenOverloadsCUDA -v
CUDA_VISIBLE_DEVICES=5 FSDP_BENCH_LABEL=candidate_repeat python agent_space/fsdp_aten_overloads/benchmark.py TestATenOverloadsCUDA -v
lintrunner -a
git diff --check

The build script invokes pip install -e . -v --no-build-isolation with the saved local CUDA configuration. Scripts under agent_space/ are local validation artifacts and are not committed. ROCm and Windows builds were not run locally.

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests labels Sep 20, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 36 Pending

As of commit 58311ee with merge base 3fda599 (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 the release notes: distributed (fsdp) release notes category label Sep 20, 2026
[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 20, 2026

Copy link
Copy Markdown

Attention! native_functions.yaml was changed

If you are adding a new function or defaulted argument to native_functions.yaml, you cannot use it from pre-existing Python frontend code until our FC window passes (two weeks). Split your PR into two PRs, one which adds the new C++ functionality, and one that makes use of it from Python, and land them two weeks apart. See https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#forwards-compatibility-fc for more info.

@weifengpy weifengpy changed the title [FSDP2] Share ATen copy kernels for nonzero-dimension sharding [FSDP2] Extend ATen copy operators for nonzero-dimension sharding Sep 20, 2026
weifengpy added a commit that referenced this pull request Sep 20, 2026
draf the change as a peer PR to HEAD, so i can compare alternatives side by side

> AI-assisted implementation and description prepared with Codex. This is an alternative to #197701 on the same parent, #197204.
>
> The opt-in Shard(1+) copy callbacks can spend more CPU time creating Tensor views than submitting the copy. Extend the existing ATen `split_with_sizes_copy` and `_chunk_cat` families with `.prefix_out` overloads accepting the original tensors and concrete layout metadata. Native CUDA implementations prepare raw descriptors and launch the existing kernels.
>
> `split_with_sizes_copy.prefix_out` treats its input as `num_chunks` flat rows. `split_sizes` partitions each row, and `num_prefixes[i]` divides an output into contiguous regions collecting the corresponding pieces from all rows. With uint8 input, split sizes count bytes and output dtypes may differ. `_chunk_cat.prefix_out` uses each input's `num_leading_dims` as its shard dimension and packs its prefixes into output rows. Nonzero shard dimensions require even division; dimension zero retains the existing padding behavior.
>
> Descriptor preparation, validation, and fallback implementations now live in ATen. The separate FSDP prefix-copy operators and C++ files are removed, with no build-list changes relative to the parent. `ReduceScatterInput` and the opt-in callbacks remain; the default FSDP copy operator and existing ATen overloads retain their behavior. Review the native schemas and common validation first, the CUDA descriptor path next, then the FSDP call sites and tests.
>
> `CompositeImplicitAutograd` provides the common decomposition for other backends and FakeTensor/compiler tracing. Two explicit Functionalize registrations use that same decomposition. Two exact Python-binding exclusions leave these caller-allocated operations accessible through `torch.ops.aten` without generating top-level Python bindings that require matching allocating variants. The new overloads also enforce out-operation autograd and version/inference rules.
>
> On B200, using 224 MiB BF16 [N, F, D] inputs, Shard(1), eight shards, and FP32 reduce-scatter output, graph GPU medians differed by at most 0.31% from the previous #197784 implementation (`d307e02db7b2`) across two candidate runs. Each phase used one CUDA kernel and one metadata H2D copy; peak and retained CUDA allocations and view counts matched exactly. Eager times in microseconds were:
>
> | N | AG: previous #197784 | AG: native runs | RS: previous #197784 | RS: native runs |
> | ---: | ---: | ---: | ---: | ---: |
> | 2 | 102.12 | 101.99 / 102.34 | 349.77 | 348.46 / 348.57 |
> | 128 | 103.65 | 102.46 / 101.76 | 353.69 | 355.58 / 360.09 |
> | 8192 | 264.17 | 281.62 / 265.32 | 625.00 | 619.67 / 616.97 |
>
> N=2 and N=128 eager timings stayed within 2%; host submission was lower in both candidate runs. N=8192 all-gather eager time increased 6.61% in the first run and 0.44% on repeat, with host time 196.82 / 176.10 us versus 182.90 us. Exact CPU parity is not established. These separate-build measurements include submission/backpressure but exclude NCCL and model work. The baseline above is the prior #197784 implementation. Earlier #197701 measurements remain historical context: N=8192 AG/RS eager times were 244.04/594.08 us, so these results do not establish equal CPU performance with #197701 either.
>
> Test Plan:
>
> The CUDA 13 / SM100 editable build, `lintrunner -a`, and `git diff --check` passed. The regression suites ran 272 cases: 265 passed and seven expected skips. Coverage includes CPU/CUDA copies, mixed dtypes, noncontiguous fallback, device/stream handling, CUDA graphs, autograd and inference semantics, Functionalize, FakeTensor, fullgraph aot_eager compilation, and FSDP training parity. Both benchmark runs passed all three exact correctness cases.
>
> ```bash
> bash agent_space/fsdp_aten_overloads/build.sh
> CUDA_VISIBLE_DEVICES=4,6 python test/test_shape_ops.py TestPrefixCopyCPU TestPrefixCopyCUDA -v
> CUDA_VISIBLE_DEVICES=7 python agent_space/fsdp_aten_overloads/dispatch_smoke.py -v
> CUDA_VISIBLE_DEVICES=5 python test/distributed/_composable/fsdp/test_fully_shard_prefix_copy.py -v
> CUDA_VISIBLE_DEVICES=0,2 python test/distributed/_composable/fsdp/test_fully_shard_training.py TestFullyShard1DTrainingCore.test_train_parity_single_group_shard_dim0 TestFullyShard1DTrainingCore.test_train_parity_single_group_shard_largest_dim TestFullyShardShardPlacementFnMultiProcess.test_train_parity_shard_placement_fn_shard_largest_dim TestFullyShardShardPlacementFnMultiThread.test_shard_placement_fn_contiguous_params_grads -v
> CUDA_VISIBLE_DEVICES=3 python test/test_ops.py TestCommonCUDA -k _chunk_cat -v
> CUDA_VISIBLE_DEVICES=1 python test/test_torch.py TestTorch.test_split_with_sizes_copy_out -v
> CUDA_VISIBLE_DEVICES=5 FSDP_BENCH_LABEL=candidate python agent_space/fsdp_aten_overloads/benchmark.py TestATenOverloadsCUDA -v
> CUDA_VISIBLE_DEVICES=5 FSDP_BENCH_LABEL=candidate_repeat python agent_space/fsdp_aten_overloads/benchmark.py TestATenOverloadsCUDA -v
> lintrunner -a
> git diff --check
> ```
>
> The build script invokes `pip install -e . -v --no-build-isolation` with the saved local CUDA configuration. Scripts under `agent_space/` are local validation artifacts and are not committed. ROCm and Windows builds were not run locally.

ghstack-source-id: 0ae74e7
Pull-Request: #197784
@weifengpy weifengpy closed this Sep 20, 2026
@github-actions
github-actions Bot deleted the gh/weifengpy/193/head branch November 24, 2026 01:48
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant