Repository navigation
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". 🟡 AUTOMATED REVIEW - OUTDATED (earlier commit) - Changes requested:
Findings (4): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199726
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (16 Unrelated Failures)As of commit 6aaaafb with merge base 4865066 ( 🟡 GREEN LIGHT - PR was reverted - re-landing requires human review:
Green Light will not review this PR again, on this or any later commit; re-landing it needs a human approval. NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following jobs failed, but the AI CI Advisor judged the failures not to be evidence against this change:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
…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
…ayout > 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 adopts #188276's `AllGatherLayout` (prepare, copy-in and finalize, plus `AllGather.release_output()` and backend-owned outputs) as the single all-gather contract, ported onto the stack. The stack's copy-out becomes the default layout's finalize step: `DefaultAllGatherLayout(output_fn=...)` runs #197204's copy-out with a configurable `AllGatherOutputFn`, and the native copy is `DefaultAllGatherLayout(all_gather_output_fn_with_native_copy)`. An experimental `FSDPModule.set_all_gather_layout` replaces #197701's `set_all_gather_output_fn`, and `set_custom_all_gather` installs a comm's own layout, so a MORI-style backend is still one call. #197204's per-payload `outer_sizes` replace #188276's per-parameter `shard_dim` and `padded_sharded_size` metadata, which also avoids #188276's post-forward Shard(i>0) `IndexError`. The default layout skips the eligibility checks and per-parameter metadata. > > Selecting both a custom comm and the native layout is order-dependent because `set_custom_all_gather` resets the layout, the native copy does not compose with a custom layout's rank-major fallback, and backend-owned storage keeps #188276's memory model. On the default path, `_to_sharded` now discards a pending all-gather. Review the contract and default finalize in `_all_gather_layout.py` first, then `_fsdp_collectives.py`, `_fsdp_param.py` and `_fsdp_param_group.py`, then `_fully_shard.py`. The tests port #188276's layout tests and switch #197701's native-copy tests to the layout API. > > 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 -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k NativeCollectiveCopy -k strided_allocation > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py -k test_reduce_dtype > python test/distributed/_composable/fsdp/test_fully_shard_training.py -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group > ``` > All pass. ghstack-source-id: e8840af Pull-Request: #199721
…with FSDP-owned buffers > 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 option 2's `AllGatherLayout` contract and changes who owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. By default FSDP owns it: on a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it likes and returns `AllGatherOutputs(tensors, buffers)` with outputs that view those buffers. The group then frees the buffers on reshard, re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refills the same outputs in place, so `reshard_after_forward` keeps saving parameter memory for custom layouts too. The default layout keeps today's per-output allocation and its copy-out. Backend-owned storage, #188276's model, becomes an explicit opt-in: only a `BackendOwnedAllGatherLayout`, whose `release_output()` is abstract, may return outputs that FSDP never frees, and FSDP calls `release_output()` after reshard, after a discarded prefetch, and after a failed setup. `release_output` moves from `AllGather` to the layout, and `AllGather.reuses_output_storage` is removed. > > The group frees shared buffers after all parameters transition, because freeing per parameter broke post-forward resharding, where a parameter still reads a buffer another parameter already left. A group's first finalize decides whether it owns buffers, and MORI's adapter would subclass the opt-in layout and move `release_output` to it. Review the ownership rules and `AllGatherOutputs` in `_all_gather_layout.py` first, then buffer re-allocation in `_fsdp_collectives.py` and the frees in `_fsdp_param_group.py`. The tests add an FSDP-owned parameter-contiguous layout that checks its buffer is freed after reshard and refilled with one copy, keep #188276's backend-owned tests on the opt-in, and cover the rejected ownership cases. > > 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 -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k NativeCollectiveCopy -k strided_allocation > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py -k test_reduce_dtype > python test/distributed/_composable/fsdp/test_fully_shard_training.py -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group > ``` > All pass. ghstack-source-id: 03a0194 Pull-Request: #199726
…with FSDP-owned buffers > 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 option 2's `AllGatherLayout` contract and changes who owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. By default FSDP owns it: on a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it likes and returns `AllGatherOutputs(tensors, buffers)` with outputs that view those buffers. The group then frees the buffers on reshard, re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refills the same outputs in place, so `reshard_after_forward` keeps saving parameter memory for custom layouts too. The default layout keeps today's per-output allocation and its copy-out. Backend-owned storage, #188276's model, becomes an explicit opt-in: only a `BackendOwnedAllGatherLayout`, whose `release_output()` is abstract, may return outputs that FSDP never frees, and FSDP calls `release_output()` after reshard, after a discarded prefetch, and after a failed setup. `release_output` moves from `AllGather` to the layout, and `AllGather.reuses_output_storage` is removed. > > `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. `set_all_gather_layout` is the only way to install a layout, and `AllGather` no longer carries one. A layout that needs its own collective sets `comm`, and installing the layout installs that comm in the same call. While such a layout is installed, `set_custom_all_gather` rejects any other comm, as do the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm`; installing another layout lifts the binding and keeps the comm. An installed `DefaultAllGatherLayout`, including the native copy's, takes a fast path that runs the previous default code: `foreach_all_gather` builds no `AllGatherInputMetadata`, calls no `prepare`, and enters no version-counter context, `foreach_all_gather_copy_out` calls the layout's `output_fn` without building a copy plan, and reshard frees layout storage only for custom layouts. Copy-out dispatches on the installed layout rather than the per-call one, because a custom layout's rank-major fallback still needs the ownership-aware path. The default-path changes this draft inherited from #188276 are in the previous PR in the stack: `_to_sharded` discards a pending post-forward all-gather, `_unsharded_param` starts as `None`, and `init_unsharded_param` honors the output's storage offset. > > The group frees shared buffers after all parameters transition, because freeing per parameter broke post-forward resharding, where a parameter still reads a buffer another parameter already left. A group's first finalize decides whether it owns buffers, and MORI's adapter would subclass the opt-in layout, set its `comm`, and move `release_output` to it. Review the ownership rules, `AllGatherOutputs` and `AllGatherFinalizeMetadata` in `_all_gather_layout.py` first, then the default fast path, buffer re-allocation in `_fsdp_collectives.py`, the frees in `_fsdp_param_group.py`, and the setters in `_fully_shard.py`. The tests add an FSDP-owned parameter-contiguous layout that checks its buffer is freed after reshard and refilled with one copy, keep #188276's backend-owned tests on the opt-in, cover the rejected ownership cases, and check the setter rules. `TestFullyShardLayoutParity` reuses its worker processes through `FSDPTestContinuous` instead of launching processes per test, which cuts its runtime from about 27 minutes to about 6 minutes (1652 s to 353 s for its 77 tests); the compiled case resets dynamo itself because reused workers skip `setUp` and `tearDown`. > > Test Plan (local build, 4 B200 GPUs). On the final revision: > ```bash > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardAllGatherLayoutSetters > ``` > The broader suites ran on the revision before the test conversion, whose library code differs from the final one only in comments, an error message and an always-true condition: > ```bash > python test/test_public_bindings.py -k test_correct_module_names > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k NativeCollectiveCopy -k strided_allocation > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardAllGatherLayoutSetters > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k test_unused_backward_prefetch_with_post_forward_mesh > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py -k test_reduce_dtype > ``` > All of these pass. The training subset (`test_fully_shard_training.py -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group`) passed on the previous PR alone and on this draft before the split, and was not rerun after it. ghstack-source-id: 6ad27fe Pull-Request: #199726
> 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
> 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
…with FSDP-owned buffers > 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 option 2's `AllGatherLayout` contract and changes who owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. By default FSDP owns it: on a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it likes and returns `AllGatherOutputs(tensors, buffers)` with outputs that view those buffers. The group then frees the buffers on reshard, re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refills the same outputs in place, so `reshard_after_forward` keeps saving parameter memory for custom layouts too. The default layout keeps today's per-output allocation and its copy-out. Backend-owned storage, #188276's model, becomes an explicit opt-in: only a `BackendOwnedAllGatherLayout`, whose `release_output()` is abstract, may return outputs that FSDP never frees, and FSDP calls `release_output()` after reshard, after a discarded prefetch, and after a failed setup. `release_output` moves from `AllGather` to the layout, and `AllGather.reuses_output_storage` is removed. > > `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. `set_all_gather_layout` is the only way to install a layout, and `AllGather` no longer carries one. A layout that needs its own collective sets `comm`, and installing the layout installs that comm in the same call. While such a layout is installed, `set_custom_all_gather` rejects any other comm, as do the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm`; installing another layout lifts the binding and keeps the comm. An installed `DefaultAllGatherLayout`, including the native copy's, takes a fast path that runs the previous default code: `foreach_all_gather` builds no `AllGatherInputMetadata`, calls no `prepare`, and enters no version-counter context, `foreach_all_gather_copy_out` calls the layout's `output_fn` without building a copy plan, and reshard frees layout storage only for custom layouts. Copy-out dispatches on the installed layout rather than the per-call one, because a custom layout's rank-major fallback still needs the ownership-aware path. #199729 lands the `_to_sharded` fix for a pending post-forward all-gather separately on main; this draft carries the same fix and its `_wait_all_gather` helper so that it stands alone on the stack, while the regression test stays in #199729. `FSDPParam._unsharded_param` stays lazily created, because out-of-tree patches of `post_backward` check it with `hasattr`, and `init_unsharded_param` honors the output's storage offset so that parameters can be views into a layout's shared buffer. > > The group frees shared buffers after all parameters transition, because freeing per parameter broke post-forward resharding, where a parameter still reads a buffer another parameter already left. A group's first finalize decides whether it owns buffers, and MORI's adapter would subclass the opt-in layout, set its `comm`, and move `release_output` to it. Review the ownership rules, `AllGatherOutputs` and `AllGatherFinalizeMetadata` in `_all_gather_layout.py` first, then the default fast path, buffer re-allocation in `_fsdp_collectives.py`, the frees in `_fsdp_param_group.py`, and the setters in `_fully_shard.py`. The tests add an FSDP-owned parameter-contiguous layout that checks its buffer is freed after reshard and refilled with one copy, keep #188276's backend-owned tests on the opt-in, cover the rejected ownership cases, and check the setter rules. `TestFullyShardLayoutParity` reuses its worker processes through `FSDPTestContinuous` instead of launching processes per test, which cuts its local runtime for its 77 tests from 1652 s to between 353 s and 447 s; the compiled case resets dynamo itself because reused workers skip `setUp` and `tearDown`. > > Test Plan (local build, 4 B200 GPUs), run on this revision: > ```bash > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardAllGatherLayoutSetters > python test/test_public_bindings.py -k test_correct_module_names > ``` > All pass. The extensions, mixed-precision and training suites ran on earlier revisions of this draft and were not rerun on this one. ghstack-source-id: 02f1151 Pull-Request: #199726
…fers > AI-assisted draft prepared with Claude Code for the all-gather contract discussion on #188276. Of the three alternatives, option 3 was chosen over #199720 (option 1) and #199721 (option 2). > > This adds the contract that a custom all-gather backend uses to pack its own input and produce its own output layout, e.g. parameter-contiguous output. The interface shape comes from #188276 by wuyl1 and inkcherry: `AllGatherLayout.prepare`, `prepare_output`, where returning `None` falls back to the default layout for that call, `copy_in` and `finalize_outputs`, with tensor-only metadata types. Per-payload `outer_sizes` from #197204 replace its `shard_dim` and `padded_sharded_size`. `init_unsharded_param` honoring the output's storage offset, so that parameters can view a layout's shared buffer, also comes from #188276. > > FSDP owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. On a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it chooses and returns `AllGatherOutputs(tensors, buffers)` with outputs that view them. The group frees the buffers on reshard unless its parameters keep their unsharded storage (#198417), re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refilled the same outputs in place, so `reshard_after_forward` keeps saving parameter memory with custom layouts. `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. > > #197701's native copy becomes `DefaultAllGatherLayout(all_gather_output_fn_with_native_copy)`, and `set_all_gather_layout` replaces `set_all_gather_output_fn` as the only way to install a layout. A layout that needs its own collective sets `comm`, and installing the layout installs that comm. While such a layout is installed, `set_custom_all_gather` and the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm` reject any other comm. An installed `DefaultAllGatherLayout`, including the native copy's, takes a fast path that runs the previous default code. > > This leaves out #188276's backend-owned outputs, its parameter-contiguous zero copy (views into the collective buffer and FSDP's eligibility check for them) and its per-group owner binding, along with their tests, so that they remain #188276's feature on top of this contract. #199872 and #199873 propose that split to MORI as a diff against a clone of #188276. Without owner binding, the setter documents that a layout that keeps per-call state must not be shared across parameter groups. #199729 lands the `_to_sharded` fix for a pending post-forward all-gather separately on main; this draft carries the same fix and its `_wait_all_gather` helper so that it stands alone on the stack. > > Review `_all_gather_layout.py` first, then the default fast path and buffer re-allocation in `_fsdp_collectives.py`, the frees in `_fsdp_param_group.py`, and the setters in `_fully_shard.py`. The tests add a parameter-contiguous layout whose comm reorders a rank-major all-gather. Its training parity cases check that one FSDP-owned buffer backs each group, that reshard frees it unless storage is kept, and that refills take one copy, also over the post-forward mesh and after a rank-major first unshard. Unit tests cover the rejected ownership cases, the default copy-out with and without delegation, and the setter rules. > > Test Plan (local build, 4 B200 GPUs): > ```bash > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k TestFullyShardAllGatherLayoutSetters > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_comm.py > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > python test/distributed/_composable/fsdp/test_fully_shard_training.py -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_memory.py > python test/test_public_bindings.py -k test_correct_module_names > ``` > The layout unit and setter tests (29) and the parity suite (38) pass on this revision. The other commands ran one revision earlier, which differed only in the test layout's views and a docstring. The full comm file had 125 passed and 2 skipped, and `test_fully_shard_alloc_from_pg` failed, as it also does on the parent commit on this machine, where NCCL registers a buffer before process-group allocation is enabled. The other suites passed: extensions 9, mixed precision 26, training 7, state dict 1, memory 7, public bindings 1. Co-authored-by: wuyl1 <[email protected]> Co-authored-by: inkcherry <[email protected]> ghstack-source-id: 75f08b1 Pull-Request: #199726
…fers > AI-assisted draft prepared with Claude Code for the all-gather contract discussion on #188276. Of the three alternatives, option 3 was chosen over #199720 (option 1) and #199721 (option 2). > > This adds the contract that a custom all-gather backend uses to pack its own input and produce its own output layout, e.g. parameter-contiguous output. The interface shape comes from #188276 by wuyl1 and inkcherry: `AllGatherLayout.prepare`, `prepare_output`, where returning `None` falls back to the default layout for that call, `copy_in` and `finalize_outputs`, with tensor-only metadata types. Per-payload `outer_sizes` from #197204 replace its `shard_dim` and `padded_sharded_size`. `init_unsharded_param` honoring the output's storage offset, so that parameters can view a layout's shared buffer, also comes from #188276. > > FSDP owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. On a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it chooses and returns `AllGatherOutputs(tensors, buffers)` with outputs that view them. The group frees the buffers on reshard unless its parameters keep their unsharded storage (#198417), re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refilled the same outputs in place, so `reshard_after_forward` keeps saving parameter memory with custom layouts. `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. > > #197701's native copy becomes `DefaultAllGatherLayout(all_gather_output_fn_with_native_copy)`, and `set_all_gather_layout` replaces `set_all_gather_output_fn` as the only way to install a layout. A layout that needs its own collective sets `comm`, and installing the layout installs that comm. While such a layout is installed, `set_custom_all_gather` and the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm` reject any other comm. FSDP runs every all-gather through the installed layout's `prepare`, `copy_in` and `finalize_outputs`, including the default layout's, whose packing and copy-out are the previous default code, so a subclass's overrides take effect. A fast path that inlined the default layout measured within noise of this on a CPU-bound unshard and reshard benchmark, so there is none. > > This leaves out #188276's backend-owned outputs, its parameter-contiguous zero copy (views into the collective buffer and FSDP's eligibility check for them) and its per-group owner binding, along with their tests, so that they remain #188276's feature on top of this contract. #199872 and #199873 propose that split to MORI as a diff against a clone of #188276. Without owner binding, the setter documents that a layout that keeps per-call state must not be shared across parameter groups. #199729 lands the `_to_sharded` fix for a pending post-forward all-gather separately on main; this draft carries the same fix and its `_wait_all_gather` helper so that it stands alone on the stack. > > Review `_all_gather_layout.py` first, then the all-gather and buffer re-allocation in `_fsdp_collectives.py`, the frees in `_fsdp_param_group.py`, and the setters in `_fully_shard.py`. The tests add a parameter-contiguous layout whose comm reorders a rank-major all-gather. Its training parity cases check that one FSDP-owned buffer backs each group, that reshard frees it unless storage is kept, and that refills take one copy, also over the post-forward mesh and after a rank-major first unshard. Unit tests cover the rejected ownership cases, the default copy-out with and without delegation, and the setter rules. > > Test Plan (local build, 4 B200 GPUs): > ```bash > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k TestFullyShardAllGatherLayoutSetters > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_comm.py > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_memory.py > python test/distributed/_composable/fsdp/test_fully_shard_compile.py > python test/test_public_bindings.py -k test_correct_module_names > ``` > All pass on this revision except `test_fully_shard_alloc_from_pg` in the full comm file, which also fails on the parent commit on this machine, where NCCL registers a buffer before process-group allocation is enabled. The comm file had 125 passed and 2 skipped. The other suites passed: compile 7, extensions 9, mixed precision 26, state dict 1, memory 7, public bindings 1. Co-authored-by: wuyl1 <[email protected]> Co-authored-by: inkcherry <[email protected]> ghstack-source-id: 5ceea13 Pull-Request: #199726
…fers > AI-assisted draft prepared with Claude Code for the all-gather contract discussion on #188276. Of the three alternatives, option 3 was chosen over #199720 (option 1) and #199721 (option 2). > > This adds the contract that a custom all-gather backend uses to pack its own input and produce its own output layout, e.g. parameter-contiguous output. The interface shape comes from #188276 by wuyl1 and inkcherry: `AllGatherLayout.prepare`, `prepare_output`, where returning `None` falls back to the default layout for that call, `copy_in` and `finalize_outputs`, with tensor-only metadata types. Per-payload `outer_sizes` from #197204 replace its `shard_dim` and `padded_sharded_size`. `init_unsharded_param` honoring the output's storage offset, so that parameters can view a layout's shared buffer, also comes from #188276. > > FSDP owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. On a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it chooses and returns `AllGatherOutputs(tensors, buffers)` with outputs that view them. The group frees the buffers on reshard unless its parameters keep their unsharded storage (#198417), re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refilled the same outputs in place, so `reshard_after_forward` keeps saving parameter memory with custom layouts. `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. > > `set_all_gather_layout` is the only way to install a layout, and it makes #197701's native all-gather copy usable as `DefaultAllGatherLayout(all_gather_output_fn_with_native_copy)`. A layout that needs its own collective sets `comm`, and installing the layout installs that comm. While such a layout is installed, `set_custom_all_gather` and the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm` reject any other comm. FSDP runs every all-gather through the installed layout's `prepare`, `copy_in` and `finalize_outputs`, including the default layout's, whose packing and copy-out are the previous default code, so a subclass's overrides take effect. A fast path that inlined the default layout measured within noise of this on a CPU-bound unshard and reshard benchmark, so there is none. > > This leaves out #188276's backend-owned outputs, its parameter-contiguous zero copy (views into the collective buffer and FSDP's eligibility check for them) and its per-group owner binding, along with their tests, so that they remain #188276's feature on top of this contract. #199872 and #199873 propose that split to MORI as a diff against a clone of #188276. Without owner binding, the setter documents that a layout that keeps per-call state must not be shared across parameter groups. #199729 lands the `_to_sharded` fix for a pending post-forward all-gather separately on main; this draft carries the same fix and its `_wait_all_gather` helper so that it stands alone on the stack. > > Review `_all_gather_layout.py` first, then the all-gather and buffer re-allocation in `_fsdp_collectives.py`, the frees in `_fsdp_param_group.py`, and the setters in `_fully_shard.py`. The tests add a parameter-contiguous layout whose comm reorders a rank-major all-gather. Its training parity cases check that one FSDP-owned buffer backs each group, that reshard frees it unless storage is kept, and that refills take one copy, also over the post-forward mesh and after a rank-major first unshard. Unit tests cover the rejected ownership cases, the default copy-out with and without delegation, and the setter rules. This PR also turns on the native all-gather copy in the strided-allocation, extension, mixed-precision, training-parity and inference tests, which #197701 runs with only the native reduce-scatter copy. > > Test Plan (local build, 4 B200 GPUs): > ```bash > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k TestFullyShardAllGatherLayoutSetters > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_comm.py > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_memory.py > python test/distributed/_composable/fsdp/test_fully_shard_compile.py > python test/test_public_bindings.py -k test_correct_module_names > ``` > All pass on this revision except `test_fully_shard_alloc_from_pg` in the full comm file, which also fails on the parent commit on this machine, where NCCL registers a buffer before process-group allocation is enabled. The comm file had 125 passed and 2 skipped. The other suites passed: compile 7, extensions 9, mixed precision 26, state dict 1, memory 7, public bindings 1. Co-authored-by: wuyl1 <[email protected]> Co-authored-by: inkcherry <[email protected]> ghstack-source-id: 5ceea13 Pull-Request: #199726
> AI-assisted draft prepared with Claude Code for discussion on #188276. > > On top of the previous PR's clone of #188276, this shows the changes we suggest. The result is the all-gather contract we'd like to land, option 3 of #199720, #199721 and #199726, with #188276's backend-owned zero copy as an explicit opt-in. > > - FSDP owns the storage behind the parameters' all-gather outputs by default, following FlexShard's placement contract. On a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it chooses and returns `AllGatherOutputs(tensors, buffers)` with outputs that view them. The group frees the buffers on reshard unless its parameters keep their unsharded storage (#198417), re-allocates them before every later finalize, and checks that the layout refilled the same outputs in place, so `reshard_after_forward` keeps saving memory with custom layouts. > - Backend-owned storage, #188276's model, becomes an explicit opt-in. Only a `BackendOwnedAllGatherLayout`, whose `release_output()` is abstract, may return outputs that FSDP never frees, and FSDP calls `release_output()` after reshard, after a discarded prefetch and after a failed setup. `release_output` moves from `AllGather` to the layout, and `AllGather.layout` and `AllGather.reuses_output_storage` are removed. > - `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. > - `set_all_gather_layout` is the only way to install a layout. A layout that needs its own collective sets `comm`, and installing the layout installs that comm. While it is installed, `set_custom_all_gather` and the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm` reject any other comm. > - An installed `DefaultAllGatherLayout`, including the native copy's, takes a fast path that runs the stack's default code, so the default path builds no per-call layout metadata. > - `FSDPParam._unsharded_param` stays lazily created, because out-of-tree patches of `post_backward` check it with `hasattr`. > - Separately from the contract, `test_compiled_backward_eligibility` sets multiprocessing's start method to spawn before compiling. Compiling otherwise fixes it to fork after CUDA is initialized, so every `FSDPTestContinuous` class later in the file fails to start its workers when the file runs in one process. > > In MORI's adapter at ROCm/mori@7f40b1bd, `_MoriSdmaAllGatherLayout` would subclass `BackendOwnedAllGatherLayout`, set `comm`, and implement `release_output()` with the logic now in `MoriSdmaAllGather.release_output`. Its `finalize_outputs` would read the metadata argument and return `AllGatherOutputs(views)`, and users would install it with `module.set_all_gather_layout(layout)` instead of `set_custom_all_gather(comm)`. > > Review `_all_gather_layout.py` first, then `_fsdp_collectives.py`, `_fsdp_param_group.py` and `_fully_shard.py`. The tests add an FSDP-owned parameter-contiguous layout that checks its buffer is freed after reshard unless storage is kept and refilled with one copy. They keep #188276's backend-owned tests on the opt-in, cover the rejected ownership cases and the setter rules, and run `TestFullyShardLayoutParity` on `FSDPTestContinuous`. > > 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 > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > python test/distributed/_composable/fsdp/test_fully_shard_training.py -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_memory.py > ``` > In the full comm file, 197 of 200 tests pass and 2 are skipped, as on #197701. `test_fully_shard_alloc_from_pg` fails, as it also does on #197701 on this machine, where NCCL registers a buffer before process-group allocation is enabled. The other suites pass: public bindings 1, extensions 9, mixed precision 26, training 7, state dict 1, memory 7. ghstack-source-id: 3704a5b Pull-Request: #199873
> 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
…fers > AI-assisted draft prepared with Claude Code for the all-gather contract discussion on #188276. Of the three alternatives, option 3 was chosen over #199720 (option 1) and #199721 (option 2). > > This adds the contract that a custom all-gather backend uses to pack its own input and produce its own output layout, e.g. parameter-contiguous output. The interface shape comes from #188276 by wuyl1 and inkcherry: `AllGatherLayout.prepare`, `prepare_output`, where returning `None` falls back to the default layout for that call, `copy_in` and `finalize_outputs`, with tensor-only metadata types. Per-payload `outer_sizes` from #197204 replace its `shard_dim` and `padded_sharded_size`. `init_unsharded_param` honoring the output's storage offset, so that parameters can view a layout's shared buffer, also comes from #188276. > > FSDP owns the storage behind the parameters' all-gather outputs, following FlexShard's placement contract. On a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it chooses and returns `AllGatherOutputs(tensors, buffers)` with outputs that view them. The group frees the buffers on reshard unless its parameters keep their unsharded storage (#198417), re-allocates them to their recorded storage sizes before every later finalize, and checks that the layout refilled the same outputs in place, so `reshard_after_forward` keeps saving parameter memory with custom layouts. `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. > > `set_all_gather_layout` is the only way to install a layout, and it makes #197701's native all-gather copy usable as `DefaultAllGatherLayout(all_gather_output_fn_with_native_copy)`. A layout that needs its own collective sets `comm`, and installing the layout installs that comm. While such a layout is installed, `set_custom_all_gather` and the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm` reject any other comm. FSDP runs every all-gather through the installed layout's `prepare`, `copy_in` and `finalize_outputs`, including the default layout's, whose packing and copy-out are the previous default code, so a subclass's overrides take effect. A fast path that inlined the default layout measured within noise of this on a CPU-bound unshard and reshard benchmark, so there is none. > > This leaves out #188276's backend-owned outputs, its parameter-contiguous zero copy (views into the collective buffer and FSDP's eligibility check for them) and its per-group owner binding, along with their tests, so that they remain #188276's feature on top of this contract. #199872 and #199873 propose that split to MORI as a diff against a clone of #188276. Without owner binding, the setter documents that a layout that keeps per-call state must not be shared across parameter groups. It builds on #199729, at the bottom of this stack, which discards a pending post-forward all-gather in `_to_sharded` and adds the `_wait_all_gather` helper. > > Review `_all_gather_layout.py` first, then the all-gather and buffer re-allocation in `_fsdp_collectives.py`, the frees in `_fsdp_param_group.py`, and the setters in `_fully_shard.py`. The tests add a parameter-contiguous layout whose comm reorders a rank-major all-gather. Its training parity cases check that one FSDP-owned buffer backs each group, that reshard frees it unless storage is kept, and that refills take one copy, also over the post-forward mesh and after a rank-major first unshard. Unit tests cover the rejected ownership cases, the default copy-out with and without delegation, and the setter rules. This PR also turns on the native all-gather copy in the strided-allocation, extension, mixed-precision, training-parity and inference tests, which #197701 runs with only the native reduce-scatter copy. > > Test Plan (local build, 4 B200 GPUs): > ```bash > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestAllGatherLayouts -k TestAllGatherLayoutCopyOut -k TestFullyShardAllGatherLayoutSetters > python test/distributed/_composable/fsdp/test_fully_shard_comm.py -k TestFullyShardLayoutParity > python test/distributed/_composable/fsdp/test_fully_shard_comm.py > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_memory.py > python test/distributed/_composable/fsdp/test_fully_shard_compile.py > python test/test_public_bindings.py -k test_correct_module_names > ``` > All pass on this revision except `test_fully_shard_alloc_from_pg` in the full comm file, which also fails on the parent commit on this machine, where NCCL registers a buffer before process-group allocation is enabled. The comm file had 125 passed and 2 skipped. The other suites passed: compile 7, extensions 9, mixed precision 26, state dict 1, memory 7, public bindings 1. Co-authored-by: wuyl1 <[email protected]> Co-authored-by: inkcherry <[email protected]> ghstack-source-id: 2216a7b Pull-Request: #199726
> AI-assisted draft prepared with Claude Code for discussion on #188276. > > On top of the previous PR's clone of #188276, this shows the changes we suggest. The result is the all-gather contract we'd like to land, option 3 of #199720, #199721 and #199726, with #188276's backend-owned zero copy as an explicit opt-in. > > - FSDP owns the storage behind the parameters' all-gather outputs by default, following FlexShard's placement contract. On a parameter group's first unshard, the layout's finalize allocates persistent buffers in any layout it chooses and returns `AllGatherOutputs(tensors, buffers)` with outputs that view them. The group frees the buffers on reshard unless its parameters keep their unsharded storage (#198417), re-allocates them before every later finalize, and checks that the layout refilled the same outputs in place, so `reshard_after_forward` keeps saving memory with custom layouts. > - Backend-owned storage, #188276's model, becomes an explicit opt-in. Only a `BackendOwnedAllGatherLayout`, whose `release_output()` is abstract, may return outputs that FSDP never frees, and FSDP calls `release_output()` after reshard, after a discarded prefetch and after a failed setup. `release_output` moves from `AllGather` to the layout, and `AllGather.layout` and `AllGather.reuses_output_storage` are removed. > - `finalize_outputs` takes a single `AllGatherFinalizeMetadata`, so later fields do not break out-of-tree layouts. > - `set_all_gather_layout` is the only way to install a layout. A layout that needs its own collective sets `comm`, and installing the layout installs that comm. While it is installed, `set_custom_all_gather` and the internal comm swaps of `set_symm_mem_for_comm` and `set_allocate_memory_from_process_group_for_comm` reject any other comm. > - An installed `DefaultAllGatherLayout`, including the native copy's, takes a fast path that runs the stack's default code, so the default path builds no per-call layout metadata. > - `FSDPParam._unsharded_param` stays lazily created, because out-of-tree patches of `post_backward` check it with `hasattr`. > - Separately from the contract, `test_compiled_backward_eligibility` sets multiprocessing's start method to spawn before compiling. Compiling otherwise fixes it to fork after CUDA is initialized, so every `FSDPTestContinuous` class later in the file fails to start its workers when the file runs in one process. > > In MORI's adapter at ROCm/mori@7f40b1bd, `_MoriSdmaAllGatherLayout` would subclass `BackendOwnedAllGatherLayout`, set `comm`, and implement `release_output()` with the logic now in `MoriSdmaAllGather.release_output`. Its `finalize_outputs` would read the metadata argument and return `AllGatherOutputs(views)`, and users would install it with `module.set_all_gather_layout(layout)` instead of `set_custom_all_gather(comm)`. > > Review `_all_gather_layout.py` first, then `_fsdp_collectives.py`, `_fsdp_param_group.py` and `_fully_shard.py`. The tests add an FSDP-owned parameter-contiguous layout that checks its buffer is freed after reshard unless storage is kept and refilled with one copy. They keep #188276's backend-owned tests on the opt-in, cover the rejected ownership cases and the setter rules, and run `TestFullyShardLayoutParity` on `FSDPTestContinuous`. > > 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 > python test/distributed/_composable/fsdp/test_fully_shard_extensions.py > python test/distributed/_composable/fsdp/test_fully_shard_mixed_precision.py > python test/distributed/_composable/fsdp/test_fully_shard_training.py -k test_train_parity_single_group -k test_inference -k test_train_parity_multi_group -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_state_dict.py -k keep_unsharded_storage > python test/distributed/_composable/fsdp/test_fully_shard_memory.py > ``` > In the full comm file, 197 of 200 tests pass and 2 are skipped, as on #197701. `test_fully_shard_alloc_from_pg` fails, as it also does on #197701 on this machine, where NCCL registers a buffer before process-group allocation is enabled. The other suites pass: public bindings 1, extensions 9, mixed precision 26, training 7, state dict 1, memory 7. ghstack-source-id: 7839cc6 Pull-Request: #199873
…fers [ghstack-poisoned]
5fd0f4b to
815c634
Compare
> 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
|
My agent came up with this analysis, but I've read and approved it.
|
|
@weifengpy CI is green at 6aaaafb and the ROCm results above still hold (the MoRI adapter is validated against this head). Anything left before landing? AMD's #188276 follow-up is waiting on this one. |
|
@weifengpy @anshul-si any objection to landing this as-is at 6aaaafb? It's approved, green and conflict-free, and the ROCm validation above covers the MI300X check from Nov 5. If neither of you objects, I'll merge it so the #188276 follow-up can move. |
|
@pytorchbot merge |
|
This PR has pending changes requested. Please address the comments and update the PR before merging. |
|
@pytorchbot merge |
Merge failedReason: 1 mandatory check(s) are pending/not yet run. The first few are:
Dig deeper by viewing the pending checks on hud |
|
/easycla |
|
@pytorchbot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 1, 3, macos-m1-stable) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
|
@pytorchbot merge -i |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
|
@jeffdaily @wuyl1 this break internal so i have to revert for now. totally understand the need for MORI to build on top of it. This contract is pretty fundamental in FSDP2 & FlexShard unification I will spend time this week to investigate internal breakage and unblock this |
|
✴️ iz2: Reverting this PR because it breaks Meta-internal tests that OSS CI does not run. The author will follow up with a fix and can reland it. |
|
@pytorchbot successfully started a revert job. Check the current status here. |
|
@weifengpy your PR has been successfully reverted. |
@weifengpy Thanks for looking into this. Understood on the revert. We’re happy to help validate the update on ROCm with the MoRI adapter. |
|
@weifengpy Could you share what broke internally, i.e. the failing test names and the error message, with internal names removed? The PR intentionally changes private FSDP2 signatures ( |
Stack from ghstack (oldest at bottom):
BC-breaking?
Summary (human written only)
Agent details (if applicable)
Co-authored-by: wuyl1 [email protected]
Co-authored-by: inkcherry [email protected]