Skip to content

[spmd] normalize partial to replicate on size-1 mesh axes in resolve_placements - #3762

Merged
shuhuayu merged 1 commit into
pytorch:mainfrom
shuhuayu:moe
Jun 24, 2026
Merged

shuhuayu merged 1 commit into
pytorch:mainfrom
shuhuayu:moe

Conversation

@shuhuayu

Copy link
Copy Markdown
Contributor

resolve_placements already collapses Shard(d) -> Replicate() on a size-1 mesh axis. Partial has the exact same property but wasn't covered, which makes full-DTensor MoE crash on any config with a size-1 data-parallel axis. To reproduce,

NGPU=2 MODULE=deepseek_v3 CONFIG=deepseek_v3_debugmodel ./run_train.sh \
      --parallelism.tensor_parallel_degree 2 \
      --parallelism.spmd_backend full_dtensor \
      --training.steps 2

Got

ValueError: GroupedExperts.num_local_tokens_per_expert_E: input DTensor has
  placements (Replicate(), Replicate()), but in_src_shardings expects
  (Partial(sum), Replicate())

root cause

Under full_dtensor, the batch is sharded across the data-parallel axis. With dp_shard == 1 that axis has a single rank, so there's nothing to split and the whole batch lives on that one rank, i.e. Replicate on the dp axis. In torchtitan/models/common/moe.py,

  • dp_shard=1: scores_BLE placements = (Replicate, Replicate)
  • dp_shard=2: scores_BLE placements = (Shard(0), Replicate) ← batch genuinely split and works well.

When dp_shard=1, routing_map_BLE = zeros_like(scores_BLE).scatter_(...) inherits that placement, so it's Replicate on the dp axis too. The MoE per-expert token counts (num_local_tokens_per_expert_E = routing_map.sum(dim=(0,1))) are declared Partial(sum) on the data-parallel axis via _tokens_per_expert_placement, correct, because each dp rank processes different tokens and contributes a partial count. With dp_shard == 1 the DP axis has a single rank, so the batch is not split and the runtime value comes out Replicate on that axis as shown above. resolve_placements only normalizes Shard -> Replicate on size-1 axes, not Partial, so the contract's expected keeps Partial(sum) while the actual value is Replicate. _redistribute_inputs then rejects the (numerically identical) mismatch.

Fix

Extend the existing size-1 normalization in resolve_placements to cover Partial as well as Shard.

Numerical test confirmed loss/gd_norm parity on dp_shard=2 after the fix.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 24, 2026
@shuhuayu
shuhuayu requested a review from pianpwk June 24, 2026 05:47

@tianyu-l tianyu-l 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.

sounds reasonable, thanks!!

@pianpwk pianpwk 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.

thanks!

@shuhuayu
shuhuayu merged commit 7083a80 into pytorch:main Jun 24, 2026
27 of 29 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jun 25, 2026
Clean merge, no conflicts, no code replays. Fused-QKV-by-default (pytorch#3714)
flows via common/ but ezpz/agpt keeps fuse_qkv=False (zero checkpoint
risk to 2B/80B chains); LoRA/protocol convert() change (pytorch#3456) benign for
ezpz (moe quant builds OK); varlen+PP assert (pytorch#3777) and SPMD Partial
size-1 fix (pytorch#3762) are no-op/free-win. Config-build smoke passed for
agpt_2b/2b_real/80b + moe_10b_2b/sdpa.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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