Skip to content

[mxfp8] Handle unevenly sharded expert weights in FSDP compute weights - #4371

Merged
anijain2305 merged 1 commit into
mainfrom
mxfp8-uneven-shard-padding
Sep 4, 2026
Merged

anijain2305 merged 1 commit into
mainfrom
mxfp8-uneven-shard-padding

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Core FSDP automatically pads for uneven sharding.

However, mxfp8 linear uses pre/post all gather hooks where the responsibility now lies on the the tensor subclass to perform the padding during pre-gather and then unpad (narrow) during post-gather. This PR does that.

For testing, we forcefully create a mxfp8 linear with rows not directly divisible by the number of shards (e.g. 96/5). This is more common in efsdp where number of experts might not be divisible by fsdp degree. We will see this more frequently come up when we release faster MoE implementations.


Stack created with GitHub Stacks CLI • Give Feedback 💬

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 28, 2026
@anijain2305
anijain2305 marked this pull request as draft August 28, 2026 22:44
@anijain2305 anijain2305 changed the title Handle unevenly sharded expert weights in FSDP compute weights [DO NOT REVIEW] Handle unevenly sharded expert weights in FSDP compute weights Aug 28, 2026
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 2211ec9 to 7a5bc02 Compare August 31, 2026 06:00
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 7a5bc02 to 606e17d Compare August 31, 2026 06:08
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 606e17d to 18a26d2 Compare August 31, 2026 18:23
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 18a26d2 to 7781e15 Compare August 31, 2026 18:38
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 7781e15 to 6bad387 Compare August 31, 2026 21:17
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 6bad387 to 1206932 Compare August 31, 2026 21:44
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 1206932 to 5995c6e Compare August 31, 2026 22:15
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 5995c6e to b5ac523 Compare September 1, 2026 05:43
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from b5ac523 to b3fd02e Compare September 1, 2026 05:53
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from b3fd02e to 844ac8c Compare September 1, 2026 06:13
@anijain2305
anijain2305 changed the base branch from mxfp8-grouped-experts to mxfp8-training September 1, 2026 18:17
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 844ac8c to fa75637 Compare September 1, 2026 18:19
Comment on lines +366 to +378
source = self._tensor.to(dtype)
# Pad to what FSDP calls ``padded_sharded_param_size``. The default
# path pre-pads to ``chunks[0].size()``, and torch.chunk puts the
# remainder in the earlier chunks, so that equals ceil(dim0 / world):
# https://github.com/pytorch/pytorch/blob/c7da99c173f2b67905ee798576a644b6b32cbfee/torch/distributed/fsdp/_fully_shard/_fsdp_param.py#L332-L345
# An extension must return exactly that size; only the short ranks
# would trip the check, so the rest hang in the all-gather instead:
# https://github.com/pytorch/pytorch/blob/c7da99c173f2b67905ee798576a644b6b32cbfee/torch/distributed/fsdp/_fully_shard/_fsdp_param.py#L1143-L1158
padded_rows = math.ceil(outer_size[0] / mesh.size())
if source.size(0) != padded_rows:
padded = source.new_zeros((padded_rows, *source.shape[1:]))
padded[: source.size(0)] = source
source = padded

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.

claude suggested saving 1 alloc and 1 copy with this

if self._tensor.size(0) != padded_rows:                                                                             
    source = self._tensor.new_zeros((padded_rows, *self._tensor.shape[1:]), dtype=dtype)                            
    source.narrow(0, 0, self._tensor.size(0)).copy_(self._tensor)                                                   
else:                                                                                                               
    source = self._tensor.to(dtype)" better than original approach

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.

add code comement that fsdp-pre-all-gather do dynamic padding at each unshard, while regular fsdp2 padding for plain bf16/fp32 params do static padding at init time once

the code comment matters because this might be room for further perf improvement

@weifengpy weifengpy left a comment

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.

overall looks good. just left 2 minor comments above. it's to up @anijain2305 if we just stop at dynamic padding, and leave pre-pad as a future work

@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from e07d5b7 to 9fe62e3 Compare September 3, 2026 00:40
@tianyu-l
tianyu-l requested a review from weifengpy September 3, 2026 00:54
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 9fe62e3 to 4dcd767 Compare September 3, 2026 05:17
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 4dcd767 to 0c59042 Compare September 3, 2026 05:27
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 0c59042 to 6e31194 Compare September 3, 2026 05:28
@weifengpy

Copy link
Copy Markdown
Contributor

when do we hit the issue? say dsv3 on DP X and EP Y ?

@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 6e31194 to f6f4d6b Compare September 3, 2026 18:35
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from f6f4d6b to 98e2213 Compare September 3, 2026 23:29
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 98e2213 to 404dbe2 Compare September 4, 2026 05:41
Base automatically changed from mxfp8-training to main September 4, 2026 14:10
FSDP pads for uneven sharding on its own: the default path pre-pads every
shard to ceil(dim0 / world) so all ranks contribute the same number of
elements to the all-gather. Defining the pre/post-all-gather extension hooks
opts a parameter out of that. FSDP hands the subclass its *unpadded* shard
and asserts that the padded size comes back, so padding on the way in and
narrowing on the way out become the subclass's job. Ours did neither: it
rejected the layout outright.

Pad the communication tensor to ceil(outer_size[0] / mesh.size()), pass
outer_size through as metadata, and narrow back to it in post-all-gather
before quantizing. Without the narrow, padding rows occupy real 32x32 scale
tiles and, for a grouped weight, look to the grouped GEMM like extra experts.
Getting this wrong is quiet rather than loud: only the short ranks trip
FSDP's assertion, so the others hang in the all-gather.

Reject a non-zero shard dimension explicitly while here. TorchTitan picks
Shard(1) for grouped experts once the FSDP degree exceeds the expert count --
gpt_oss has 8 -- and the all-gather then concatenates dim-1 shards along
dim 0, so the hook would have to rebuild the logical weight before
quantizing. That is left for later behind an error that names the condition.

Tested by driving the hooks directly with a stub mesh: a (96, 128) weight
over five ranks pads to 20 rows each, leaving the last rank holding 16. The
uneven case has to be forced this way because a dense MXFP8 weight cannot be
unevenly sharded across two ranks -- the kernels require out_features
divisible by 32, which always divides 2. It is not a contrived shape, though:
eFSDP shards on the expert count, where indivisible sizes are ordinary, and
that path will be exercised far harder once the faster MoE implementation
lands.
@anijain2305
anijain2305 force-pushed the mxfp8-uneven-shard-padding branch from 404dbe2 to fe94f86 Compare September 4, 2026 14:11
@anijain2305
anijain2305 merged commit 2af775e into main Sep 4, 2026
16 of 17 checks passed
sanketpurandare added a commit that referenced this pull request Sep 12, 2026
…ter"

Summary:
Add a thin TorchTitan adapter for the standalone Dist-MoE package. TorchTitan
owns backend selection, W13/W2 model parameters, FSDP/optimizer integration,
checkpoint key conversion, process groups, and context lifetime. The package
continues to own dispatch, expert compute, combine, memory planning, activation
storage, VMM, and kernels.

MXFP8 expert weights specialize TorchTitan's existing `_ShardedFSDPTensor`
contract. They share BF16 all-gather, operand preparation/refill/release,
SimpleFSDP gradient edges, RAF semantics, and checkpoint handling with
`MXFP8Linear`; only grouped Dist-MoE operand construction differs.

The package selects one of two entries to the same autograd implementation:
ordinary eager calls it directly, while Dynamo, FakeTensor, make_fx, and
activation-checkpoint tracing use the registered ordered-effect operation. The
adapter adds no third execution path or mode-specific kernel behavior.

Non-goals:
- Do not copy or modify kernels, routing policy, the memory planner, pipeline
  schedules, or the generic prepared-weight lifecycle.
- Do not silently support FSDP sharding outside logical dimension 0.

Dependencies and risks:
- Requires TorchTitan #4203/#4371, #4539, the PyTorch #194114/#196460 FSDP
  lifecycle, and a released exact `dist-moe` version. Pipeline use additionally
  requires #4540 and PyTorch #196462-#196465. Effectful AC requires #4559.
- Tests cover W13 reshape, W2 identity, shared qdata, stable four-buffer refill,
  logical gradient shape, RAF true/false, no-FSDP preparation, uneven shards,
  checkpoint round trip, and explicit cleanup.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py`
- Two-rank BF16 and MXFP8 FSDP+EP full-CUDA-graph training on Blackwell.
- Final-source MXFP8 device, ordinary/forced-spill VMM, and GraphTrainer rows
  completed with exact numerics, full graph replay, checked VMM teardown where
  applicable, and zero restarts.

[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 12, 2026
Summary:
Add a thin TorchTitan adapter for the standalone Dist-MoE package. TorchTitan
owns backend selection, W13/W2 model parameters, FSDP/optimizer integration,
checkpoint key conversion, process groups, and context lifetime. The package
continues to own dispatch, expert compute, combine, memory planning, activation
storage, VMM, and kernels.

MXFP8 expert weights specialize TorchTitan's existing `_ShardedFSDPTensor`
contract. They share BF16 all-gather, operand preparation/refill/release,
SimpleFSDP gradient edges, RAF semantics, and checkpoint handling with
`MXFP8Linear`; only grouped Dist-MoE operand construction differs.

The package selects one of two entries to the same autograd implementation:
ordinary eager calls it directly, while Dynamo, FakeTensor, make_fx, and
activation-checkpoint tracing use the registered ordered-effect operation. The
adapter adds no third execution path or mode-specific kernel behavior.

Non-goals:
- Do not copy or modify kernels, routing policy, the memory planner, pipeline
  schedules, or the generic prepared-weight lifecycle.
- Do not silently support FSDP sharding outside logical dimension 0.

Dependencies and risks:
- Requires TorchTitan #4203/#4371, #4539, the PyTorch #194114/#196460 FSDP
  lifecycle, and a released exact `dist-moe` version. Pipeline use additionally
  requires #4540 and PyTorch #196462-#196465. Effectful AC requires #4559.
- Tests cover W13 reshape, W2 identity, shared qdata, stable four-buffer refill,
  logical gradient shape, RAF true/false, no-FSDP preparation, uneven shards,
  checkpoint round trip, and explicit cleanup.

Test Plan:
- `python -m pytest -q tests/unit_tests/cpu/test_dist_moe.py`
- Two-rank BF16 and MXFP8 FSDP+EP full-CUDA-graph training on Blackwell.
- Final-source MXFP8 device, ordinary/forced-spill VMM, and GraphTrainer rows
  completed with exact numerics, full graph replay, checked VMM teardown where
  applicable, and zero restarts.

[ghstack-poisoned]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants