Repository navigation
[mxfp8] Handle unevenly sharded expert weights in FSDP compute weights - #4371
Merged
Merged
Conversation
anijain2305
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
August 28, 2026 22:44
anijain2305
marked this pull request as draft
August 28, 2026 22:44
Merged
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 06:00
2211ec9 to
7a5bc02
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 06:08
7a5bc02 to
606e17d
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 18:23
606e17d to
18a26d2
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 18:38
18a26d2 to
7781e15
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 21:17
7781e15 to
6bad387
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 21:44
6bad387 to
1206932
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
August 31, 2026 22:15
1206932 to
5995c6e
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 1, 2026 05:43
5995c6e to
b5ac523
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 1, 2026 05:53
b5ac523 to
b3fd02e
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 1, 2026 06:13
b3fd02e to
844ac8c
Compare
anijain2305
changed the base branch from
mxfp8-grouped-experts
to
mxfp8-training
September 1, 2026 18:17
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 1, 2026 18:19
844ac8c to
fa75637
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 2, 2026 17:49
4cfefbe to
e07d5b7
Compare
weifengpy
reviewed
Sep 2, 2026
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 |
Contributor
There was a problem hiding this comment.
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
Contributor
There was a problem hiding this comment.
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
reviewed
Sep 2, 2026
weifengpy
left a comment
Contributor
There was a problem hiding this comment.
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
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 3, 2026 00:40
e07d5b7 to
9fe62e3
Compare
tianyu-l
approved these changes
Sep 3, 2026
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 3, 2026 05:17
9fe62e3 to
4dcd767
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 3, 2026 05:27
4dcd767 to
0c59042
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 3, 2026 05:28
0c59042 to
6e31194
Compare
Contributor
|
when do we hit the issue? say dsv3 on DP X and EP Y ? |
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 3, 2026 18:35
6e31194 to
f6f4d6b
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 3, 2026 23:29
f6f4d6b to
98e2213
Compare
anijain2305
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 4, 2026 05:41
98e2213 to
404dbe2
Compare
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
force-pushed
the
mxfp8-uneven-shard-padding
branch
from
September 4, 2026 14:11
404dbe2 to
fe94f86
Compare
This was referenced Sep 10, 2026
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Core FSDP automatically pads for uneven sharding.
However,
mxfp8linear 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
efsdpwhere 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 💬