Skip to content

[FSDP2] Fix pre-existing all-gather copy-out bugs - #199430

Draft
weifengpy wants to merge 10 commits into
gh/weifengpy/209/basefrom
gh/weifengpy/209/head
Draft

weifengpy wants to merge 10 commits into
gh/weifengpy/209/basefrom
gh/weifengpy/209/head

Conversation

@weifengpy

@weifengpy weifengpy commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.

With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct copy_ failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.

On CUDA, split_with_sizes_copy crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.

Test Plan:

python test/test_torch.py -k test_split_with_sizes_copy_out
python test/distributed/_composable/fsdp/test_fully_shard_extensions.py

Without the fixes, test_all_gather_extension_single_rank_payloads fails with The size of tensor a (128) must match the size of tensor b (512), and the empty split dumps core. Both pass with them.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ You can merge normally! (2 Unrelated Failures), 1 Unclassified Failure

As of commit 7319b43 with merge base ff18741 (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

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.

[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204, so their fixes and regression tests land first.
>
> Resharding Shard(i>0) parameters after forward (`reshard_after_forward=int`) raised `IndexError` in the pre-backward all-gather. A post-forward shard is a flat chunk of the unsharded data, but `_default_all_gather_output_fn` still sent it through the Shard(i>0) reassembly, which indexed the 1-D post-forward size with the shard dim. The reassembly now runs only in the `SHARDED` state, since the post-forward all-gather output is already in the final layout. Separately, `_init_sharded_post_forward_param_metadata` sizes the post-forward shard by chunking dim 0, which matches the flat chunk only when the post-forward world size divides dim 0. Otherwise `to_sharded_post_forward` failed in `as_strided` with an out-of-bounds storage error. Supporting that case would need new post-forward shard metadata, and the configuration never worked, so `to_sharded_post_forward` now raises `NotImplementedError` for it, like uneven Shard(i>0) sharding. The check runs there rather than at construction because `reshard_after_forward=True` also computes post-forward metadata without using it.
>
> With a size-1 shard mesh, the direct copy in `FSDPParamGroup.wait_for_unshard` initialized and copied only the first all-gather input, so an extension returning several payloads received one output. It now copies every payload, and copies byte payloads bytewise into typed cached outputs as the world size > 1 path does.
>
> For an extension payload with a zero-size trailing dim, `_unflatten_all_gather_outputs` called `view(-1, 0)`, which cannot infer -1. It now computes the leading dim from the shard world size in that case.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements, which happens for an FSDP group whose all-gather output is empty. The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. #197204 avoided the crash with an early return in FSDP; fixing the kernel instead covers every caller.
>
> Test Plan:
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k test_all_gather_post_forward_shard_dim1
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, the new FSDP tests fail with `IndexError`, `ValueError: not enough values to unpack`, and `cannot reshape tensor of 0 elements into shape [-1, 0]`; the extension suite passes with them. A standalone all-empty `torch.split_with_sizes_copy` on CUDA dumps core before the ATen change. Still to run on a build with the ATen change: `python test/test_torch.py -k test_split_with_sizes_copy_out`, `python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k test_all_gather_empty_params`, and the full comm suite.

ghstack-source-id: b36ca37
Pull-Request: #199430
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204, so their fixes and regression tests land first.
>
> Resharding Shard(i>0) parameters after forward (`reshard_after_forward=int`) raised `IndexError` in the pre-backward all-gather. A post-forward shard is a flat chunk of the unsharded data, but `_default_all_gather_output_fn` still sent it through the Shard(i>0) reassembly, which indexed the 1-D post-forward size with the shard dim. The reassembly now runs only in the `SHARDED` state, since the post-forward all-gather output is already in the final layout. Separately, `_init_sharded_post_forward_param_metadata` sizes the post-forward shard by chunking dim 0, which matches the flat chunk only when the post-forward world size divides dim 0. Otherwise `to_sharded_post_forward` failed in `as_strided` with an out-of-bounds storage error. Supporting that case would need new post-forward shard metadata, and the configuration never worked, so `to_sharded_post_forward` now raises `NotImplementedError` for it, like uneven Shard(i>0) sharding. The check runs there rather than at construction because `reshard_after_forward=True` also computes post-forward metadata without using it.
>
> With a size-1 shard mesh, the direct copy in `FSDPParamGroup.wait_for_unshard` initialized and copied only the first all-gather input, so an extension returning several payloads received one output. It now copies every payload, and copies byte payloads bytewise into typed cached outputs as the world size > 1 path does.
>
> For an extension payload with a zero-size trailing dim, `_unflatten_all_gather_outputs` called `view(-1, 0)`, which cannot infer -1. It now computes the leading dim from the shard world size in that case.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements, which happens for an FSDP group whose all-gather output is empty. The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. #197204 avoided the crash with an early return in FSDP; fixing the kernel instead covers every caller.
>
> Test Plan:
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k test_all_gather_post_forward_shard_dim1
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, the new FSDP tests fail with `IndexError`, `ValueError: not enough values to unpack`, and `cannot reshape tensor of 0 elements into shape [-1, 0]`; the extension suite passes with them. A standalone all-empty `torch.split_with_sizes_copy` on CUDA dumps core before the ATen change. Still to run on a build with the ATen change: `python test/test_torch.py -k test_split_with_sizes_copy_out`, `python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k test_all_gather_empty_params`, and the full comm suite.

ghstack-source-id: b36ca37
Pull-Request: #199430
[ghstack-poisoned]
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 1f54e5d
Pull-Request: #199430
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, the size-1 shard mesh subtests of `test_legacy_all_gather_shard_dim` fail with `The size of tensor a (48) must match the size of tensor b (192)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 2cf5b91
Pull-Request: #199430
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, the size-1 shard mesh subtests of `test_legacy_all_gather_shard_dim` fail with `The size of tensor a (48) must match the size of tensor b (192)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 32d80fa
Pull-Request: #199430
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 8f09bcd
Pull-Request: #199430
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 2, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 73ea3c2
Pull-Request: #199430
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 4, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: af43e89
Pull-Request: #199430
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 4, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 3f30e66
Pull-Request: #199430
weifengpy added a commit that referenced this pull request Oct 5, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 3f30e66
Pull-Request: #199430
weifengpy added a commit that referenced this pull request Oct 5, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 3f30e66
Pull-Request: #199430
weifengpy added a commit that referenced this pull request Oct 5, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 3f30e66
Pull-Request: #199430
weifengpy added a commit that referenced this pull request Oct 5, 2026
> AI-assisted change prepared with Claude Code. It was split out of the all-gather contract draft #199726 (option 3), which inherited it from #188276, and is based on main so it can land on its own.
>
> With `reshard_after_forward` set to an int, a group that forward used but backward does not use is still prefetched by the next group's pre-backward. The group is in its post-forward state, so the prefetch all-gathers its post-forward shards over the smaller post-forward mesh, and since the group's own pre-backward never runs, nothing waits for that result. The last backward discards it in `finalize_backward`, but a backward that is not the last one, as in gradient accumulation with `set_is_last_backward(False)`, only reshards the group to the full mesh in `post_backward` and keeps the pending result. The next forward's `unshard` then returns early because a result is pending, and `wait_for_unshard` copies out a buffer gathered over the post-forward mesh as if the full mesh had gathered it, which fails in the copy-out (`start (0) + length (512) exceeds dimension size (256)` in the regression test). `_to_sharded` now waits for a pending all-gather result and discards it before the group returns to the sharded state, as `finalize_backward` and `_reset_iter_state` already do for mistargeted unshards. The three share the wait in `_wait_all_gather`, which waits through the FSDP device handle as #199677 made `finalize_backward` do. Only that path changes: `unshard` issues nothing while the group is unsharded, so a result is pending when `_to_sharded` runs only if an all-gather issued in the post-forward state was never waited on, such as this backward prefetch, and copying such a result out over the full mesh always failed.
>
> Test Plan: the local build predates main, so the tests ran with the same change on top of #199430, whose FSDP2 code paths for this bug match main's; the change differs only in the `finalize_backward` lines #199677 rewrote. `test_unused_backward_prefetch_with_post_forward_mesh` fails without the `_to_sharded` change, with the stale result still pending after the non-final backward, and passes with it.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardPrefetch
> ```
> All 8 tests pass on 4 B200 GPUs.

ghstack-source-id: 9a55a31
Pull-Request: #199729
weifengpy added a commit that referenced this pull request Oct 5, 2026
> AI-assisted change prepared with Claude Code. It was split out of the all-gather contract draft #199726 (option 3), which inherited it from #188276, and is based on main so it can land on its own.
>
> With `reshard_after_forward` set to an int, a group that forward used but backward does not use is still prefetched by the next group's pre-backward. The group is in its post-forward state, so the prefetch all-gathers its post-forward shards over the smaller post-forward mesh, and since the group's own pre-backward never runs, nothing waits for that result. The last backward discards it in `finalize_backward`, but a backward that is not the last one, as in gradient accumulation with `set_is_last_backward(False)`, only reshards the group to the full mesh in `post_backward` and keeps the pending result. The next forward's `unshard` then returns early because a result is pending, and `wait_for_unshard` copies out a buffer gathered over the post-forward mesh as if the full mesh had gathered it, which fails in the copy-out (`start (0) + length (512) exceeds dimension size (256)` in the regression test). `_to_sharded` now waits for a pending all-gather result and discards it before the group returns to the sharded state, as `finalize_backward` and `_reset_iter_state` already do for mistargeted unshards. The three share the wait in `_wait_all_gather`, which waits through the FSDP device handle as #199677 made `finalize_backward` do. Only that path changes: `unshard` issues nothing while the group is unsharded, so a result is pending when `_to_sharded` runs only if an all-gather issued in the post-forward state was never waited on, such as this backward prefetch, and copying such a result out over the full mesh always failed.
>
> Test Plan: the local build predates main, so the tests ran with the same change on top of #199430, whose FSDP2 code paths for this bug match main's; the change differs only in the `finalize_backward` lines #199677 rewrote. `test_unused_backward_prefetch_with_post_forward_mesh` fails without the `_to_sharded` change, with the stale result still pending after the non-final backward, and passes with it.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardPrefetch
> ```
> All 8 tests pass on 4 B200 GPUs.

ghstack-source-id: 9a55a31
Pull-Request: #199729
weifengpy added a commit that referenced this pull request Oct 5, 2026
> AI-assisted implementation and summary prepared with Claude Code. These bugs predate the collective copy refactor in #197204 and remain after it.
>
> With a size-1 shard mesh, FSDP copies the all-gather inputs directly into the all-gather outputs (since #160135). When an extension returned a byte view as a payload while its cached output kept another dtype, the direct `copy_` failed instead of copying the bytes as the world size > 1 path does. The direct copy now views such outputs as bytes first.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The launch size math computed zero chunks per block and then divided by it. The CUDA fast path now returns early for empty inputs. FSDP already skips the copy-out for empty all-gather buffers, so this covers other callers.
>
> Test Plan:
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py
> ```
> Without the fixes, `test_all_gather_extension_single_rank_payloads` fails with `The size of tensor a (128) must match the size of tensor b (512)`, and the empty split dumps core. Both pass with them.

ghstack-source-id: 3f30e66
Pull-Request: #199430
@weifengpy

Copy link
Copy Markdown
Contributor Author

This PR's two fixes are now standalone PRs on main, so they can land without waiting for this stack:

What remains here is the same one-line byte view inside the every-payload loop that #197204
introduces, plus test_all_gather_extension_single_rank_payloads. Both move into #197204 when the
stack is rebased onto main after #199732 and #199733 land, so this PR would end up empty. The drafts
stacked on it (#199720, #199721, #199726) will be rebased onto the new top of the stack.

pytorchmergebot pushed a commit that referenced this pull request Oct 5, 2026
#199733)

> AI-assisted change prepared with Claude Code. It was split out of #199430 and rewritten for main so it can land independently.
>
> With a size-1 shard mesh, FSDP skips the all-gather and copies the all-gather input directly into the cached all-gather output (since #160135). When an extension's `fsdp_pre_all_gather` returns a byte view (`uint8`) of its payload while the cached output keeps the payload's original dtype, `copy_` converts values instead of copying bytes. For wider dtypes that fails on the size mismatch (`The size of tensor a (128) must match the size of tensor b (512)` in the new test), and for one-byte dtypes such as float8 it converts the byte values silently. The world size > 1 path already views the outputs as bytes when the buffer is bytes, and the direct copy now views the cached output as bytes for byte payloads too. Main's single-rank path copies only the first payload; copying every payload is part of #197204.
>
> Test Plan: `test_all_gather_extension_single_rank_byte_payload` gathers a typed payload and then a byte view of it over a size-1 shard mesh; it fails without the fix with the size mismatch above and passes with it. The local build predates main, so the test ran on an older main (ff18741) whose single-rank path and test file match main's.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py -k test_all_gather_extension_single_rank_byte_payload
> ```
Pull Request resolved: #199733
Approved by: https://github.com/pytorchgreenlight
pytorchmergebot pushed a commit that referenced this pull request Oct 5, 2026
> AI-assisted change prepared with Claude Code. It was split out of #199430 so it can land on main independently.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since #117203). The fast path's launch size math computed zero chunks per block and then divided by it, so any call with an empty input that took the fast path crashed. The CUDA fast path now returns early for empty inputs, since there is nothing to copy.
>
> Test Plan: the new empty cases in `test_split_with_sizes_copy_out`, including all-empty splits, crash the process without the fix (found when the fix was written for #199430) and pass with it. The test runs on CUDA when a GPU is available. The local build predates main, so the test ran on an older main (ff18741) whose build includes this kernel change; `TensorShape.cu` is unchanged on main since then.
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> ```
Pull Request resolved: #199732
Approved by: https://github.com/pytorchgreenlight, https://github.com/Skylion007
weifengpy added a commit that referenced this pull request Oct 6, 2026
> AI-assisted change prepared with Claude Code. It was split out of the all-gather contract draft #199726 (option 3), which inherited it from #188276, and is based on main so it can land on its own.
>
> With `reshard_after_forward` set to an int, a group that forward used but backward does not use is still prefetched by the next group's pre-backward. The group is in its post-forward state, so the prefetch all-gathers its post-forward shards over the smaller post-forward mesh, and since the group's own pre-backward never runs, nothing waits for that result. The last backward discards it in `finalize_backward`, but a backward that is not the last one, as in gradient accumulation with `set_is_last_backward(False)`, only reshards the group to the full mesh in `post_backward` and keeps the pending result. The next forward's `unshard` then returns early because a result is pending, and `wait_for_unshard` copies out a buffer gathered over the post-forward mesh as if the full mesh had gathered it, which fails in the copy-out (`start (0) + length (512) exceeds dimension size (256)` in the regression test). `_to_sharded` now waits for a pending all-gather result and discards it before the group returns to the sharded state, as `finalize_backward` and `_reset_iter_state` already do for mistargeted unshards. The three share the wait in `_wait_all_gather`, which waits through the FSDP device handle as #199677 made `finalize_backward` do. Only that path changes: `unshard` issues nothing while the group is unsharded, so a result is pending when `_to_sharded` runs only if an all-gather issued in the post-forward state was never waited on, such as this backward prefetch, and copying such a result out over the full mesh always failed.
>
> Test Plan: the local build predates main, so the tests ran with the same change on top of #199430, whose FSDP2 code paths for this bug match main's; the change differs only in the `finalize_backward` lines #199677 rewrote. `test_unused_backward_prefetch_with_post_forward_mesh` fails without the `_to_sharded` change, with the stale result still pending after the non-final backward, and passes with it.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardPrefetch
> ```
> All 8 tests pass on 4 B200 GPUs.

Co-authored-by: wuyl1 <[email protected]>
ghstack-source-id: 9a55a31
Pull-Request: #199729
weifengpy added a commit that referenced this pull request Oct 6, 2026
> AI-assisted change prepared with Claude Code. It was split out of the all-gather contract draft #199726 (option 3), which inherited it from #188276, and is based on main so it can land on its own.
>
> With `reshard_after_forward` set to an int, a group that forward used but backward does not use is still prefetched by the next group's pre-backward. The group is in its post-forward state, so the prefetch all-gathers its post-forward shards over the smaller post-forward mesh, and since the group's own pre-backward never runs, nothing waits for that result. The last backward discards it in `finalize_backward`, but a backward that is not the last one, as in gradient accumulation with `set_is_last_backward(False)`, only reshards the group to the full mesh in `post_backward` and keeps the pending result. The next forward's `unshard` then returns early because a result is pending, and `wait_for_unshard` copies out a buffer gathered over the post-forward mesh as if the full mesh had gathered it, which fails in the copy-out (`start (0) + length (512) exceeds dimension size (256)` in the regression test). `_to_sharded` now waits for a pending all-gather result and discards it before the group returns to the sharded state, as `finalize_backward` and `_reset_iter_state` already do for mistargeted unshards. The three share the wait in `_wait_all_gather`, which waits through the FSDP device handle as #199677 made `finalize_backward` do. Only that path changes: `unshard` issues nothing while the group is unsharded, so a result is pending when `_to_sharded` runs only if an all-gather issued in the post-forward state was never waited on, such as this backward prefetch, and copying such a result out over the full mesh always failed.
>
> Test Plan: the local build predates main, so the tests ran with the same change on top of #199430, whose FSDP2 code paths for this bug match main's; the change differs only in the `finalize_backward` lines #199677 rewrote. `test_unused_backward_prefetch_with_post_forward_mesh` fails without the `_to_sharded` change, with the stale result still pending after the non-final backward, and passes with it.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardPrefetch
> ```
> All 8 tests pass on 4 B200 GPUs.

Co-authored-by: wuyl1 <[email protected]>
ghstack-source-id: 1209001
Pull-Request: #199729
pytorchmergebot pushed a commit that referenced this pull request Oct 7, 2026
…99729)

> AI-assisted change prepared with Claude Code. It was split out of the all-gather contract draft #199726 (option 3), which inherited it from #188276, and is based on main so it can land on its own.
>
> With `reshard_after_forward` set to an int, a group that forward used but backward does not use is still prefetched by the next group's pre-backward. The group is in its post-forward state, so the prefetch all-gathers its post-forward shards over the smaller post-forward mesh, and since the group's own pre-backward never runs, nothing waits for that result. The last backward discards it in `finalize_backward`, but a backward that is not the last one, as in gradient accumulation with `set_is_last_backward(False)`, only reshards the group to the full mesh in `post_backward` and keeps the pending result. The next forward's `unshard` then returns early because a result is pending, and `wait_for_unshard` copies out a buffer gathered over the post-forward mesh as if the full mesh had gathered it, which fails in the copy-out (`start (0) + length (512) exceeds dimension size (256)` in the regression test). `_to_sharded` now waits for a pending all-gather result and discards it before the group returns to the sharded state, as `finalize_backward` and `_reset_iter_state` already do for mistargeted unshards. The three share the wait in `_wait_all_gather`, which waits through the FSDP device handle as #199677 made `finalize_backward` do. Only that path changes: `unshard` issues nothing while the group is unsharded, so a result is pending when `_to_sharded` runs only if an all-gather issued in the post-forward state was never waited on, such as this backward prefetch, and copying such a result out over the full mesh always failed.
>
> Test Plan: the local build predates main, so the tests ran with the same change on top of #199430, whose FSDP2 code paths for this bug match main's; the change differs only in the `finalize_backward` lines #199677 rewrote. `test_unused_backward_prefetch_with_post_forward_mesh` fails without the `_to_sharded` change, with the stale result still pending after the non-final backward, and passes with it.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardPrefetch
> ```
> All 8 tests pass on 4 B200 GPUs.

Co-authored-by: wuyl1 <[email protected]>

Pull Request resolved: #199729
Approved by: https://github.com/anshul-si
ghstack dependencies: #197204
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
pytorch#199733)

> AI-assisted change prepared with Claude Code. It was split out of pytorch#199430 and rewritten for main so it can land independently.
>
> With a size-1 shard mesh, FSDP skips the all-gather and copies the all-gather input directly into the cached all-gather output (since pytorch#160135). When an extension's `fsdp_pre_all_gather` returns a byte view (`uint8`) of its payload while the cached output keeps the payload's original dtype, `copy_` converts values instead of copying bytes. For wider dtypes that fails on the size mismatch (`The size of tensor a (128) must match the size of tensor b (512)` in the new test), and for one-byte dtypes such as float8 it converts the byte values silently. The world size > 1 path already views the outputs as bytes when the buffer is bytes, and the direct copy now views the cached output as bytes for byte payloads too. Main's single-rank path copies only the first payload; copying every payload is part of pytorch#197204.
>
> Test Plan: `test_all_gather_extension_single_rank_byte_payload` gathers a typed payload and then a byte view of it over a size-1 shard mesh; it fails without the fix with the size mismatch above and passes with it. The local build predates main, so the test ran on an older main (ff18741) whose single-rank path and test file match main's.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_extensions.py -k test_all_gather_extension_single_rank_byte_payload
> ```
Pull Request resolved: pytorch#199733
Approved by: https://github.com/pytorchgreenlight
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
> AI-assisted change prepared with Claude Code. It was split out of pytorch#199430 so it can land on main independently.
>
> On CUDA, `split_with_sizes_copy` crashed the process when its input had no elements (since pytorch#117203). The fast path's launch size math computed zero chunks per block and then divided by it, so any call with an empty input that took the fast path crashed. The CUDA fast path now returns early for empty inputs, since there is nothing to copy.
>
> Test Plan: the new empty cases in `test_split_with_sizes_copy_out`, including all-empty splits, crash the process without the fix (found when the fix was written for pytorch#199430) and pass with it. The test runs on CUDA when a GPU is available. The local build predates main, so the test ran on an older main (ff18741) whose build includes this kernel change; `TensorShape.cu` is unchanged on main since then.
> ```bash
> python test/test_torch.py -k test_split_with_sizes_copy_out
> ```
Pull Request resolved: pytorch#199732
Approved by: https://github.com/pytorchgreenlight, https://github.com/Skylion007
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
…torch#199729)

> AI-assisted change prepared with Claude Code. It was split out of the all-gather contract draft pytorch#199726 (option 3), which inherited it from pytorch#188276, and is based on main so it can land on its own.
>
> With `reshard_after_forward` set to an int, a group that forward used but backward does not use is still prefetched by the next group's pre-backward. The group is in its post-forward state, so the prefetch all-gathers its post-forward shards over the smaller post-forward mesh, and since the group's own pre-backward never runs, nothing waits for that result. The last backward discards it in `finalize_backward`, but a backward that is not the last one, as in gradient accumulation with `set_is_last_backward(False)`, only reshards the group to the full mesh in `post_backward` and keeps the pending result. The next forward's `unshard` then returns early because a result is pending, and `wait_for_unshard` copies out a buffer gathered over the post-forward mesh as if the full mesh had gathered it, which fails in the copy-out (`start (0) + length (512) exceeds dimension size (256)` in the regression test). `_to_sharded` now waits for a pending all-gather result and discards it before the group returns to the sharded state, as `finalize_backward` and `_reset_iter_state` already do for mistargeted unshards. The three share the wait in `_wait_all_gather`, which waits through the FSDP device handle as pytorch#199677 made `finalize_backward` do. Only that path changes: `unshard` issues nothing while the group is unsharded, so a result is pending when `_to_sharded` runs only if an all-gather issued in the post-forward state was never waited on, such as this backward prefetch, and copying such a result out over the full mesh always failed.
>
> Test Plan: the local build predates main, so the tests ran with the same change on top of pytorch#199430, whose FSDP2 code paths for this bug match main's; the change differs only in the `finalize_backward` lines pytorch#199677 rewrote. `test_unused_backward_prefetch_with_post_forward_mesh` fails without the `_to_sharded` change, with the stale result still pending after the non-final backward, and passes with it.
> ```bash
> python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardPrefetch
> ```
> All 8 tests pass on 4 B200 GPUs.

Co-authored-by: wuyl1 <[email protected]>

Pull Request resolved: pytorch#199729
Approved by: https://github.com/anshul-si
ghstack dependencies: pytorch#197204

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant