Skip to content

fix moe-expert-shard-degree-hsdp - #4767

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
rakkit:fix-moe-expert-shard-degree-hsdp
Sep 18, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
rakkit:fix-moe-expert-shard-degree-hsdp

Conversation

@rakkit

@rakkit rakkit commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

apply_fsdp_to_decoder picks Shard(0)/Shard(1) for routed experts by comparing an FSDP
shard degree against num_experts, but the two branches compute different things:

if ep_degree > 1:
    efsdp_ep_size = edp_mesh["efsdp"].size() * ep_degree   # dp_shard * cp * tp
else:
    efsdp_ep_size = fsdp_config["mesh"].size()             # dp_replicate * dp_shard * cp * tp

by definition, efsdp_ep_size should be "FSDP" size, but since lots of refactor and at some point fsdp_config["mesh"] included the DP dimension so now efsdp_ep_size include DP.

a broken example is
dp_replicate=2, dp_shard=4, num_experts=4: current gives 8 > 4 → Shard(1). which should be Shard(0) as its HSDP..

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 17, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 17, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@rakkit

rakkit commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

another related question, i think pytorch at some point does not really care about it anymore? (number of experts can be smaller than shard world size, i.e. some rank can have 0 experts) so do we really have to switch to shard(1)?

Comment thread torchtitan/distributed/fsdp.py Outdated
Comment on lines +300 to +303
dp_storage_mesh = fsdp_config["mesh"]
efsdp_ep_size = dp_storage_mesh.size()
if "dp_replicate" in (dp_storage_mesh.mesh_dim_names or ()):
efsdp_ep_size //= dp_storage_mesh["dp_replicate"].size()

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes you are right.

i switched to efsdp_ep_size = dp_storage_mesh["dp_shard"].size()
but i think we need to multiply CP when it exists?

@rakkit
rakkit force-pushed the fix-moe-expert-shard-degree-hsdp branch from 10783b6 to d61fe19 Compare September 18, 2026 09:08
@tianyu-l
tianyu-l merged commit 4b5023b into pytorch:main Sep 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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