Skip to content

[no-ci] [FSDP] Move the FlexShard engine into core; add experimental flex_shard API (AI-assisted) - #203763

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

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

Conversation

@weifengpy

@weifengpy weifengpy commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):


  • Passes lint (spin fixlint)
  • Added/updated tests
  • Updated documentation where applicable
  • Included benchmark results

Linked issue or supporting maintainer

No linked issue; the author has write access. Stacked on #204282, a copy of #199726, on #203961 (the FSDPModule APIs torchtitan needs, independent of this PR); the PR above runs fully_shard on this engine.

BC-breaking?

No. This adds a private package and an experimental module; nothing existing changes.

Summary (human written only)

Placeholder for the author.

Agent details (AI-generated)

Drafted with an AI assistant at the author's request. This is a draft for CI and discussion, not ready for review or merge. CI is deferred with [no-ci] because the PR above runs CI on the whole stack.

What. Moves the FlexShard engine from meta-pytorch/flex_shard (main d6bb546) into torch.distributed.fsdp._flex_shard and exposes its frontend as the experimental public module torch.distributed.fsdp.experimental.flex_shard. flex_shard shards parameters as plain torch.Tensor local shards with sharding metadata, and each bucket of parameters names its own placement (Shard, BlockShard, BucketedBlockShard, BucketedOwned, MixedBucketPlacement, ...).

How. The move is mechanical. Imports are rewritten to the core package. The FP8/MXFP8 blockwise placements stay in the flex_shard repo, and MixedBucketPlacement gets a registry so that the repo can register its quantized member types. The repo's tests move to test/distributed/_composable/flex_shard, with their helpers in torch/testing/_internal/distributed. The experimental module reports itself as the __module__ of the names it re-exports, as torch.distributed.fsdp does for FSDP2's, and test_testing.py's import check skips the two modules that need the optional spmd_types and triton packages. At about 21.7k added lines this is far over the 1,500-line PR limit; landing it would mean splitting it.

Review order. placement_contract.py (the prepare/run/finish contract every placement implements), bucket_storage.py (one byte buffer per bucket, laid out by its placement), placements/shard.py (FSDP-style Shard(dim) with FSDP's native copy ops), bucket_comm.py and bucket_runtime.py (flex_shard's hook-driven scheduler), then flex_shard.py and the experimental re-export module.

Known gaps. Lint passes for the files only this PR touches; the engine files the PR above also changes (bucket_runtime.py, bucket_comm.py, placement_contract.py, placements/shard.py, placements/utils.py) get their lint fixes there. Most pyrefly errors in the moved code are silenced with # pyrefly: ignore rather than fixed.

Test Plan

python -m pytest test/distributed/_composable/flex_shard/<file>.py   # each file on its own, 4x GB300
python -m pytest test/test_public_bindings.py -k test_correct_module_names
spin lint -- --take PYREFLY,RUFF,PYFMT,FLAKE8,MYPY,TEST_LINTER,TESTOWNERS,TEST_HAS_MAIN,CODESPELL <changed files>

On main bba8f39 (4x GB300), every file passes except test_flex_shard_fsdp2_parity.py::test_matches_fully_shard_with_head_called_twice (140 passed, 1 failed). It fails the same way in the flex_shard repo against this torch build: it expects FSDP2's older post-backward behavior for a module called twice in one forward. With the PR above applied, all 144 pass. test_correct_module_names passes. test_testing.py's import check, run over torch/distributed/fsdp with spmd_types hidden, passes; the full check needs packages this host lacks (e.g. tabulate).

#204282 copies #199726's latest revision (d9d8b3c), in which a layout carries its own comm; this PR needed no change for it.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 8 New Failures

As of commit 057d568 with merge base bba8f39 (image):

NEW FAILURES - The following jobs have failed:

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

@pytorch-bot pytorch-bot Bot added the release notes: distributed (fsdp) release notes category label Nov 6, 2026
@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests labels Nov 6, 2026
weifengpy added a commit that referenced this pull request Nov 6, 2026
…flex_shard API (AI-assisted)

- [ ] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation where applicable
- [ ] Included benchmark results

## Linked issue or supporting maintainer

> No linked issue; the author has write access. Bottom of a two-PR stack on #199726; the PR above runs `fully_shard` on this engine.

## BC-breaking?

> No. This adds a private package and an experimental module; nothing existing changes.

## Summary (human written only)

> _Placeholder for the author._

## Agent details (AI-generated)

Drafted with an AI assistant at the author's request. This is a draft for CI and discussion, not ready for review or merge. CI is deferred with `[no-ci]` because the PR above runs CI on the whole stack.

**What.** Moves the FlexShard engine from meta-pytorch/flex_shard (main d6bb546) into `torch.distributed.fsdp._flex_shard` and exposes its frontend as the experimental public module `torch.distributed.fsdp.experimental.flex_shard`. `flex_shard` shards parameters as plain `torch.Tensor` local shards with sharding metadata, and each bucket of parameters names its own placement (`Shard`, `BlockShard`, `BucketedBlockShard`, `BucketedOwned`, `MixedBucketPlacement`, ...).

**How.** The move is mechanical. Imports are rewritten to the core package. The FP8/MXFP8 blockwise placements stay in the flex_shard repo, and `MixedBucketPlacement` gets a registry so that the repo can register its quantized member types. The repo's tests move to `test/distributed/_composable/flex_shard`, with their helpers in `torch/testing/_internal/distributed`. At about 21.7k added lines this is far over the 1,500-line PR limit; landing it would mean splitting it.

**Review order.** `placement_contract.py` (the prepare/run/finish contract every placement implements), `bucket_storage.py` (one byte buffer per bucket, laid out by its placement), `placements/shard.py` (FSDP-style `Shard(dim)` with FSDP's native copy ops), `bucket_comm.py` and `bucket_runtime.py` (flex_shard's hook-driven scheduler), then `flex_shard.py` and the experimental re-export module.

**Known gaps.** Ruff still warns in the moved files (TC001/TC003 type-checking imports, S101 asserts), and the full `spin fixlint`, including pyrefly, has not been run.

### Test Plan

```
python -m pytest test/distributed/_composable/flex_shard/<file>.py   # each file on its own, 4x B200
```

Every file passes except `test_flex_shard_fsdp2_parity.py::test_matches_fully_shard_with_head_called_twice`, which fails the same way in the flex_shard repo against this torch build: it expects FSDP2's older post-backward behavior for a module called twice in one forward. With the PR above applied, the suite reports 140 passed and that failure.

These runs used the previous revision of #199726 (aad1e3c). The stack was then cherry-picked onto its current revision without conflicts; that revision changes only `_fully_shard.py` and `test_fully_shard_comm.py`.


ghstack-source-id: 46bf155
Pull-Request: #203763
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Nov 8, 2026
…flex_shard API (AI-assisted)

- [ ] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation where applicable
- [ ] Included benchmark results

## Linked issue or supporting maintainer

> No linked issue; the author has write access. Bottom of a two-PR stack on #199726; the PR above runs `fully_shard` on this engine.

## BC-breaking?

> No. This adds a private package and an experimental module; nothing existing changes.

## Summary (human written only)

> _Placeholder for the author._

## Agent details (AI-generated)

Drafted with an AI assistant at the author's request. This is a draft for CI and discussion, not ready for review or merge. CI is deferred with `[no-ci]` because the PR above runs CI on the whole stack.

**What.** Moves the FlexShard engine from meta-pytorch/flex_shard (main d6bb546) into `torch.distributed.fsdp._flex_shard` and exposes its frontend as the experimental public module `torch.distributed.fsdp.experimental.flex_shard`. `flex_shard` shards parameters as plain `torch.Tensor` local shards with sharding metadata, and each bucket of parameters names its own placement (`Shard`, `BlockShard`, `BucketedBlockShard`, `BucketedOwned`, `MixedBucketPlacement`, ...).

**How.** The move is mechanical. Imports are rewritten to the core package. The FP8/MXFP8 blockwise placements stay in the flex_shard repo, and `MixedBucketPlacement` gets a registry so that the repo can register its quantized member types. The repo's tests move to `test/distributed/_composable/flex_shard`, with their helpers in `torch/testing/_internal/distributed`. The experimental module reports itself as the `__module__` of the names it re-exports, as `torch.distributed.fsdp` does for FSDP2's, and `test_testing.py`'s import check skips the two modules that need the optional `spmd_types` and `triton` packages. At about 21.7k added lines this is far over the 1,500-line PR limit; landing it would mean splitting it.

**Review order.** `placement_contract.py` (the prepare/run/finish contract every placement implements), `bucket_storage.py` (one byte buffer per bucket, laid out by its placement), `placements/shard.py` (FSDP-style `Shard(dim)` with FSDP's native copy ops), `bucket_comm.py` and `bucket_runtime.py` (flex_shard's hook-driven scheduler), then `flex_shard.py` and the experimental re-export module.

**Known gaps.** Lint passes for the files only this PR touches; the engine files the PR above also changes (`bucket_runtime.py`, `bucket_comm.py`, `placement_contract.py`, `placements/shard.py`, `placements/utils.py`) get their lint fixes there. Most pyrefly errors in the moved code are silenced with `# pyrefly: ignore` rather than fixed.

### Test Plan

```
python -m pytest test/distributed/_composable/flex_shard/<file>.py   # each file on its own, 4x B200
python -m pytest test/test_public_bindings.py -k test_correct_module_names
spin lint -- --take PYREFLY,RUFF,PYFMT,FLAKE8,MYPY,TEST_LINTER,TESTOWNERS,TEST_HAS_MAIN,CODESPELL <changed files>
```

Every file passes except `test_flex_shard_fsdp2_parity.py::test_matches_fully_shard_with_head_called_twice`, which fails the same way in the flex_shard repo against this torch build: it expects FSDP2's older post-backward behavior for a module called twice in one forward. With the PR above applied, the suite reports 140 passed and that failure. `test_correct_module_names` passes. `test_testing.py`'s import check, run over `torch/distributed/fsdp` with `spmd_types` hidden, passes; the full check needs packages this host lacks (e.g. `tabulate`).

These runs used the previous revision of #199726 (aad1e3c). The stack was then cherry-picked onto its current revision without conflicts; that revision changes only `_fully_shard.py` and `test_fully_shard_comm.py`.

ghstack-source-id: 5bc7e97
Pull-Request: #203763
weifengpy added a commit that referenced this pull request Nov 8, 2026
…flex_shard API (AI-assisted)

- [ ] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation where applicable
- [ ] Included benchmark results

## Linked issue or supporting maintainer

> No linked issue; the author has write access. Bottom of a two-PR stack on #199726; the PR above runs `fully_shard` on this engine.

## BC-breaking?

> No. This adds a private package and an experimental module; nothing existing changes.

## Summary (human written only)

> _Placeholder for the author._

## Agent details (AI-generated)

Drafted with an AI assistant at the author's request. This is a draft for CI and discussion, not ready for review or merge. CI is deferred with `[no-ci]` because the PR above runs CI on the whole stack.

**What.** Moves the FlexShard engine from meta-pytorch/flex_shard (main d6bb546) into `torch.distributed.fsdp._flex_shard` and exposes its frontend as the experimental public module `torch.distributed.fsdp.experimental.flex_shard`. `flex_shard` shards parameters as plain `torch.Tensor` local shards with sharding metadata, and each bucket of parameters names its own placement (`Shard`, `BlockShard`, `BucketedBlockShard`, `BucketedOwned`, `MixedBucketPlacement`, ...).

**How.** The move is mechanical. Imports are rewritten to the core package. The FP8/MXFP8 blockwise placements stay in the flex_shard repo, and `MixedBucketPlacement` gets a registry so that the repo can register its quantized member types. The repo's tests move to `test/distributed/_composable/flex_shard`, with their helpers in `torch/testing/_internal/distributed`. The experimental module reports itself as the `__module__` of the names it re-exports, as `torch.distributed.fsdp` does for FSDP2's, and `test_testing.py`'s import check skips the two modules that need the optional `spmd_types` and `triton` packages. At about 21.7k added lines this is far over the 1,500-line PR limit; landing it would mean splitting it.

**Review order.** `placement_contract.py` (the prepare/run/finish contract every placement implements), `bucket_storage.py` (one byte buffer per bucket, laid out by its placement), `placements/shard.py` (FSDP-style `Shard(dim)` with FSDP's native copy ops), `bucket_comm.py` and `bucket_runtime.py` (flex_shard's hook-driven scheduler), then `flex_shard.py` and the experimental re-export module.

**Known gaps.** Lint passes for the files only this PR touches; the engine files the PR above also changes (`bucket_runtime.py`, `bucket_comm.py`, `placement_contract.py`, `placements/shard.py`, `placements/utils.py`) get their lint fixes there. Most pyrefly errors in the moved code are silenced with `# pyrefly: ignore` rather than fixed.

### Test Plan

```
python -m pytest test/distributed/_composable/flex_shard/<file>.py   # each file on its own, 4x B200
python -m pytest test/test_public_bindings.py -k test_correct_module_names
spin lint -- --take PYREFLY,RUFF,PYFMT,FLAKE8,MYPY,TEST_LINTER,TESTOWNERS,TEST_HAS_MAIN,CODESPELL <changed files>
```

Every file passes except `test_flex_shard_fsdp2_parity.py::test_matches_fully_shard_with_head_called_twice`, which fails the same way in the flex_shard repo against this torch build: it expects FSDP2's older post-backward behavior for a module called twice in one forward. With the PR above applied, the suite reports 140 passed and that failure. `test_correct_module_names` passes. `test_testing.py`'s import check, run over `torch/distributed/fsdp` with `spmd_types` hidden, passes; the full check needs packages this host lacks (e.g. `tabulate`).

These runs used the previous revision of #199726 (aad1e3c). The stack was then cherry-picked onto its current revision without conflicts; that revision changes only `_fully_shard.py` and `test_fully_shard_comm.py`.

ghstack-source-id: 5bc7e97
Pull-Request: #203763
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Nov 11, 2026
…flex_shard API (AI-assisted)

- [ ] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation where applicable
- [ ] Included benchmark results

## Linked issue or supporting maintainer

> No linked issue; the author has write access. Stacked on #204282, a copy of #199726, and #203961 (the `FSDPModule` APIs torchtitan needs, independent of this PR); the PR above runs `fully_shard` on this engine.

## BC-breaking?

> No. This adds a private package and an experimental module; nothing existing changes.

## Summary (human written only)

> _Placeholder for the author._

## Agent details (AI-generated)

Drafted with an AI assistant at the author's request. This is a draft for CI and discussion, not ready for review or merge. CI is deferred with `[no-ci]` because the PR above runs CI on the whole stack.

**What.** Moves the FlexShard engine from meta-pytorch/flex_shard (main d6bb546) into `torch.distributed.fsdp._flex_shard` and exposes its frontend as the experimental public module `torch.distributed.fsdp.experimental.flex_shard`. `flex_shard` shards parameters as plain `torch.Tensor` local shards with sharding metadata, and each bucket of parameters names its own placement (`Shard`, `BlockShard`, `BucketedBlockShard`, `BucketedOwned`, `MixedBucketPlacement`, ...).

**How.** The move is mechanical. Imports are rewritten to the core package. The FP8/MXFP8 blockwise placements stay in the flex_shard repo, and `MixedBucketPlacement` gets a registry so that the repo can register its quantized member types. The repo's tests move to `test/distributed/_composable/flex_shard`, with their helpers in `torch/testing/_internal/distributed`. The experimental module reports itself as the `__module__` of the names it re-exports, as `torch.distributed.fsdp` does for FSDP2's, and `test_testing.py`'s import check skips the two modules that need the optional `spmd_types` and `triton` packages. At about 21.7k added lines this is far over the 1,500-line PR limit; landing it would mean splitting it.

**Review order.** `placement_contract.py` (the prepare/run/finish contract every placement implements), `bucket_storage.py` (one byte buffer per bucket, laid out by its placement), `placements/shard.py` (FSDP-style `Shard(dim)` with FSDP's native copy ops), `bucket_comm.py` and `bucket_runtime.py` (flex_shard's hook-driven scheduler), then `flex_shard.py` and the experimental re-export module.

**Known gaps.** Lint passes for the files only this PR touches; the engine files the PR above also changes (`bucket_runtime.py`, `bucket_comm.py`, `placement_contract.py`, `placements/shard.py`, `placements/utils.py`) get their lint fixes there. Most pyrefly errors in the moved code are silenced with `# pyrefly: ignore` rather than fixed.

### Test Plan

```
python -m pytest test/distributed/_composable/flex_shard/<file>.py   # each file on its own, 4x B200
python -m pytest test/test_public_bindings.py -k test_correct_module_names
spin lint -- --take PYREFLY,RUFF,PYFMT,FLAKE8,MYPY,TEST_LINTER,TESTOWNERS,TEST_HAS_MAIN,CODESPELL <changed files>
```

Every file passes except `test_flex_shard_fsdp2_parity.py::test_matches_fully_shard_with_head_called_twice`, which fails the same way in the flex_shard repo against this torch build: it expects FSDP2's older post-backward behavior for a module called twice in one forward. With the PR above applied, the suite reports 140 passed and that failure. `test_correct_module_names` passes. `test_testing.py`'s import check, run over `torch/distributed/fsdp` with `spmd_types` hidden, passes; the full check needs packages this host lacks (e.g. `tabulate`).

These runs used an earlier revision of #199726 (aad1e3c). The stack was then cherry-picked onto the revision that #204282 copies, without conflicts; that revision changes only `_fully_shard.py` and `test_fully_shard_comm.py`.

ghstack-source-id: e15e938
Pull-Request: #203763
[ghstack-poisoned]
[ghstack-poisoned]

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