Skip to content

Use BlockShard instead of Shard(1) for routed experts - #4989

Draft
weifengpy wants to merge 9 commits into
gh/weifengpy/78/basefrom
gh/weifengpy/78/head
Draft

weifengpy wants to merge 9 commits into
gh/weifengpy/78/basefrom
gh/weifengpy/78/head

Conversation

@weifengpy

@weifengpy weifengpy commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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

When the expert FSDP degree is larger than the number of local experts,
routed-expert weights [num_experts, rows, cols] used Shard(1) to avoid
padding. That needs rows % fsdp_degree == 0 and costs a chunk-cat copy on
every all-gather and reduce-scatter. They now use
BlockShard.split_leading(shape, 2), which shards the merged
num_local_experts * rows dim like Shard(0) of the flattened weight: no
padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
params become (R, BlockShard, Shard(0)) on the sparse mesh. All other
cases keep Shard(0) as before.

Known limitations in the cases that used Shard(1):

  • float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
    FSDP2 raises NotImplementedError for BlockShard with them;
  • DistMuon: it raises NotImplementedError for BlockShard storage when the
    optimizer is built.
    Supporting BlockShard in FSDP2 extensions (torchao) and in DistMuon's
    storage handling are follow-ups.

BlockShard is imported unconditionally, following torchtitan's
latest-nightly requirement, so this needs a nightly that includes the PyTorch
stack (pytorch/pytorch#199232 -> #199235 -> #199234).

Test Plan

Not yet validated end to end: the deepseek_v3 debug model run needs the
grain data loader, and importing the trainer fails in the local env
(spmd_types version mismatch). torchtitan.distributed.fsdp imports.

[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 1, 2026
[ghstack-poisoned]
@weifengpy weifengpy changed the title Use BlockShard for routed-expert FSDP when FSDP degree > local experts Use BlockShard for routed experts that don't divide evenly over expert FSDP ranks Oct 1, 2026
weifengpy added a commit that referenced this pull request Oct 1, 2026
…t FSDP ranks

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

> Routed-expert weights `[num_experts, rows, cols]` used `Shard(0)` when the
> expert FSDP degree is at most the number of local experts, and `Shard(1)`
> otherwise. `Shard(0)` pads whenever the local experts don't divide evenly
> (e.g. 12 local experts over 8 ranks hold 2, 2, 2, 2, 2, 2, 0, 0 experts), and
> `Shard(1)` needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on every
> all-gather and reduce-scatter.
>
> `_expert_shard_placement` now picks:
> - `Shard(0)` when the local experts divide evenly over the expert FSDP
>   ranks: each rank holds whole experts, with no padding;
> - otherwise `BlockShard.split_leading(shape, 2)`, which shards the merged
>   `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
>   padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
>   params become `(R, BlockShard, Shard(0))` on the sparse mesh.
>
> Two cases keep the old `Shard(1)` / padded `Shard(0)` rule until they support
> `BlockShard` storage:
> - DistMuon: its storage-placement handling only understands `Replicate`,
>   `Shard`, and `_StridedShard`. `TrainingEngine.Config` turns off the new
>   `parallelism.expert_fsdp_block_shard` flag when an optimizer param group uses
>   DistMuon.
> - Tensor subclasses with FSDP extensions (float8/MXFP8 grouped experts):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> `_expert_shard_placement` returns `Shard(0)` for evenly divisible local
> experts, `BlockShard` otherwise, and the old rule for subclasses and with
> `use_block_shard=False`. Not yet validated end to end: the deepseek_v3 debug
> model run needs the `grain` data loader, and importing `TrainingEngine` fails
> in the local env (`spmd_types` version mismatch), so the DistMuon flag flip
> is untested here.

ghstack-source-id: fcdb8a2
Pull-Request: #4989
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Oct 1, 2026
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 1, 2026
…t FSDP ranks

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

> Routed-expert weights `[num_experts, rows, cols]` used `Shard(0)` when the
> expert FSDP degree is at most the number of local experts, and `Shard(1)`
> otherwise. `Shard(0)` pads whenever the local experts don't divide evenly
> (e.g. 12 local experts over 8 ranks hold 2, 2, 2, 2, 2, 2, 0, 0 experts), and
> `Shard(1)` needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on every
> all-gather and reduce-scatter.
>
> Expert placements are now:
> - `Shard(0)` when the local experts divide evenly over the expert FSDP
>   ranks: each rank holds whole experts, with no padding;
> - otherwise `BlockShard.split_leading(shape, 2)`, which shards the merged
>   `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
>   padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
>   params become `(R, BlockShard, Shard(0))` on the sparse mesh.
>
> Two cases keep the old `Shard(1)` / padded `Shard(0)` rule until they support
> `BlockShard` storage:
> - DistMuon: its storage-placement handling only understands `Replicate`,
>   `Shard`, and `_StridedShard`. `TrainingEngine.Config` turns off the new
>   `parallelism.expert_fsdp_block_shard` flag when an optimizer param group uses
>   DistMuon.
> - Tensor subclasses with FSDP extensions (float8/MXFP8 grouped experts):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> The placement rule picks `Shard(0)` for evenly divisible local
> experts, `BlockShard` otherwise, and the old rule for subclasses and with
> `expert_fsdp_block_shard=False`. Not yet validated end to end: the deepseek_v3 debug
> model run needs the `grain` data loader, and importing `TrainingEngine` fails
> in the local env (`spmd_types` version mismatch), so the DistMuon flag flip
> is untested here.

ghstack-source-id: a71e080
Pull-Request: #4989
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 1, 2026
…t FSDP ranks

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

> Routed-expert weights `[num_experts, rows, cols]` used `Shard(0)` when the
> expert FSDP degree is at most the number of local experts, and `Shard(1)`
> otherwise. `Shard(0)` pads whenever the local experts don't divide evenly
> (e.g. 12 local experts over 8 ranks hold 2, 2, 2, 2, 2, 2, 0, 0 experts), and
> `Shard(1)` needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on every
> all-gather and reduce-scatter.
>
> Expert placements are now `Shard(0)` when the local experts divide evenly
> over the expert FSDP ranks (each rank holds whole experts, with no padding),
> and otherwise `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh.
>
> Known limitations when the local experts don't divide evenly (evenly
> divisible runs are unchanged):
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Both used `Shard(1)` or padded `Shard(0)` before. Supporting `BlockShard` in
> FSDP2 extensions (torchao) and in DistMuon's storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: d7a9b6b
Pull-Request: #4989
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 1, 2026
…t FSDP ranks

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

> Routed-expert weights `[num_experts, rows, cols]` used `Shard(0)` when the
> expert FSDP degree is at most the number of local experts, and `Shard(1)`
> otherwise. `Shard(0)` pads whenever the local experts don't divide evenly
> (e.g. 12 local experts over 8 ranks hold 2, 2, 2, 2, 2, 2, 0, 0 experts), and
> `Shard(1)` needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on every
> all-gather and reduce-scatter.
>
> Expert placements are now `Shard(0)` when the local experts divide evenly
> over the expert FSDP ranks (each rank holds whole experts, with no padding),
> and otherwise `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh.
>
> Known limitations when the local experts don't divide evenly (evenly
> divisible runs are unchanged):
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Both used `Shard(1)` or padded `Shard(0)` before. Supporting `BlockShard` in
> FSDP2 extensions (torchao) and in DistMuon's storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: 07efce1
Pull-Request: #4989
[ghstack-poisoned]
@weifengpy weifengpy changed the title Use BlockShard for routed experts that don't divide evenly over expert FSDP ranks Use BlockShard instead of Shard(1) for routed experts Oct 1, 2026
weifengpy added a commit that referenced this pull request Oct 1, 2026
This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

> When the expert FSDP degree is larger than the number of local experts,
> routed-expert weights `[num_experts, rows, cols]` used `Shard(1)` to avoid
> padding. That needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on
> every all-gather and reduce-scatter. They now use
> `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh. All other
> cases keep `Shard(0)` as before.
>
> Known limitations in the cases that used `Shard(1)`:
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Supporting `BlockShard` in FSDP2 extensions (torchao) and in DistMuon's
> storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: a09fd85
Pull-Request: #4989
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 1, 2026
This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

> When the expert FSDP degree is larger than the number of local experts,
> routed-expert weights `[num_experts, rows, cols]` used `Shard(1)` to avoid
> padding. That needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on
> every all-gather and reduce-scatter. They now use
> `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh. All other
> cases keep `Shard(0)` as before.
>
> Known limitations in the cases that used `Shard(1)`:
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Supporting `BlockShard` in FSDP2 extensions (torchao) and in DistMuon's
> storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: 4380d7b
Pull-Request: #4989
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 1, 2026
This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

> When the expert FSDP degree is larger than the number of local experts,
> routed-expert weights `[num_experts, rows, cols]` used `Shard(1)` to avoid
> padding. That needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on
> every all-gather and reduce-scatter. They now use
> `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh. All other
> cases keep `Shard(0)` as before.
>
> Known limitations in the cases that used `Shard(1)`:
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Supporting `BlockShard` in FSDP2 extensions (torchao) and in DistMuon's
> storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: 4349a67
Pull-Request: #4989
weifengpy added a commit that referenced this pull request Oct 1, 2026
This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

> When the expert FSDP degree is larger than the number of local experts,
> routed-expert weights `[num_experts, rows, cols]` used `Shard(1)` to avoid
> padding. That needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on
> every all-gather and reduce-scatter. They now use
> `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh. All other
> cases keep `Shard(0)` as before.
>
> Known limitations in the cases that used `Shard(1)`:
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Supporting `BlockShard` in FSDP2 extensions (torchao) and in DistMuon's
> storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: 4349a67
Pull-Request: #4989
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Oct 1, 2026
This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

> When the expert FSDP degree is larger than the number of local experts,
> routed-expert weights `[num_experts, rows, cols]` used `Shard(1)` to avoid
> padding. That needs `rows % fsdp_degree == 0` and costs a chunk-cat copy on
> every all-gather and reduce-scatter. They now use
> `BlockShard.split_leading(shape, 2)`, which shards the merged
> `num_local_experts * rows` dim like `Shard(0)` of the flattened weight: no
> padding and no reorder copies, for any FSDP degree. With EP > 1 the expert
> params become `(R, BlockShard, Shard(0))` on the sparse mesh. All other
> cases keep `Shard(0)` as before.
>
> Known limitations in the cases that used `Shard(1)`:
> - float8/MXFP8 grouped experts (tensor subclasses with FSDP extensions):
>   FSDP2 raises `NotImplementedError` for `BlockShard` with them;
> - DistMuon: it raises `NotImplementedError` for `BlockShard` storage when the
>   optimizer is built.
> Supporting `BlockShard` in FSDP2 extensions (torchao) and in DistMuon's
> storage handling are follow-ups.
>
> `BlockShard` is imported unconditionally, following torchtitan's
> latest-nightly requirement, so this needs a nightly that includes the PyTorch
> stack (pytorch/pytorch#199232 -> #199235 -> #199234).
>
> Test Plan
>
> Not yet validated end to end: the deepseek_v3 debug model run needs the
> `grain` data loader, and importing the trainer fails in the local env
> (`spmd_types` version mismatch). `torchtitan.distributed.fsdp` imports.

ghstack-source-id: f432bbf
Pull-Request: #4989
expert_placements: dict[nn.Parameter, Shard | BlockShard] = {
# Merge the [num_experts, rows] dims; each block is the rest.
param: (
BlockShard(block_numels=(math.prod(param.shape[2:]),))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed offline:

  • the benefit is that this allows flattened dim-0 and dim-1 to be used together for FSDP sharding
  • question is when it'd be useful? It is useful when non of num_experts // EP degree or feature dim is larger than EDP degree -- the former is the reason we shard on dim-1, but the latter sounds ... surprising

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/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants