Skip to content

[MoE] Deprecate pure TP on routed experts - #4794

Merged
acisseJZhong merged 7 commits into
mainfrom
deprecate_etp
Sep 20, 2026
Merged

acisseJZhong merged 7 commits into
mainfrom
deprecate_etp

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Remove pure tensor parallelism for routed experts and require MoE expert parallelism to be at least the dense tensor-parallel degree.
  • Keep routed-expert token activations sequence-sharded under EP, avoiding duplicated routed-expert computation.
  • Keep shared experts and other dense layers tensor-parallel; the shared-expert path gathers its input and reduce-scatters its output to the routed-expert layout.
  • Update token-count reductions, affected model layouts, and documentation, and remove GPT-OSS bias scaling that was specific to expert TP.

Numerics

Compared main (ab670811b) against this PR (b83c9b925) with the DeepSeek-V3 debug model for 10 steps. Each configuration used a newly generated shared seed checkpoint, --debug.seed=42, deterministic mode, and CUDA graphs disabled on both sides.

Configuration Average loss (main -> PR) Final loss (main -> PR) Max abs loss diff Max abs grad-norm diff
EP=2, dense TP=2 4.868575 -> 4.868575 3.807293 -> 3.807293 0 0
EP=2, dense TP=1 4.868060 -> 4.868060 3.881874 -> 3.881874 0 0

Loss and grad norm are bitwise identical across all 10 steps for both supported configurations. EP=1, dense TP=2 is no longer tested because MoE configurations now require EP >= TP.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Sep 19, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 19, 2026
@acisseJZhong
acisseJZhong marked this pull request as draft September 19, 2026 00:17
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 19, 2026
@acisseJZhong
acisseJZhong force-pushed the deprecate_etp branch 3 times, most recently from 31efd58 to 93759a6 Compare September 19, 2026 00:57
@acisseJZhong
acisseJZhong marked this pull request as ready for review September 19, 2026 01:02
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 19, 2026
@acisseJZhong acisseJZhong changed the title [MoE] Remove pure tensor parallel expert sharding [MoE] Remove pure TP on routed experts Sep 19, 2026
"padding_mask_T": padding_mask_src_layout,
},
in_dst_shardings={
"x_TD": desired_input_layout,

@anijain2305 anijain2305 Sep 19, 2026 •

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.

For EP=False, SP=True, this is dense_activation_placement(tp=spmd.R, cp=spmd.S(0))

Essentially, the routed-experts weights are already replicated with this PR, and this logic replicates the input x_TD as well. It works but I don't think we have to replicate the x_TD, it can be kept sequence sharded.. But there is a tradeoff here

  • Today - everything replicated, each rank doing full computation, just an extra all gather at the beginning.
  • Keep seq-sharded - each rank does local compute (faster compute), and then do a reduce-scatter for the grads (slower comms).

Should we consider this? I am asking for 2 reasons

  • Just genuine curiosity of which one is better
  • Having all-gather just for the shared expert (which will be TP sharded and therefore actually needs all_gather) will make my TP refactor PR somewhat cleaner.

@acisseJZhong acisseJZhong Sep 19, 2026 •

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.

For EP=False, dense SP = True case if input remains sharded for routed experts:

  1. there will be an extra reduce scatter at the end(vs if inputs replicated, we do replicated computation and no extra comm needed)
  2. there will still be an all gather. The all gather just moved from the shared+routed path to only the shared expert path. But this all gather for shared expert could be overlapped by asyncTP or fused with distGEMM? (vs if inputs replicated, the all gather in the beginning for shared+routed will be fully exposed)

is my understanding correct? If so I think keeping inputs sharded might be better since we just trade one exposed all gather for one exposed reduce scatter, while each rank doing separate comp rather than duplicating comp.

@anijain2305 anijain2305 Sep 19, 2026 •

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.

Yes, precisely, thats my understanding as well.

Caveat: The overlapping by asyncTP is true and can give you some benefit, but in my mind, thats not the main optimization. I think the main perf win should be from routed-experts working on sharded inputs and therefore computing faster.

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.

in titan shared expert and routed experts are computed sequentially. If we assume no asyncTP/distGEMM, then the all gather is still exposed, we need to benchmark if the reduce computation can outweigh the extra reduce scatter.

But if in most case the all gather will be almost fully overlapped/hidden with TP comp(also not every model has shared expert), then I think we just trade all gather for reduce scatter(they should have similar latency?), while enjoying the reduced comp.

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.

changed the routed experts input from replicate to sharded.

I benchmarked on my local H100 devgpu with no asyncTP/distGEMM.

  • TP=2: sharded routed input improved forward/backward time
    by 1.15% (138.75 -> 137.16 ms).

  • TP=4: it improved forward/backward time by 3.83% (143.42 ->
    137.93 ms).


The diagram below shows the DTensor placement flow through the MoE layer
for all four parallelism configurations (EP on/off × SP on/off).
The diagram below shows the DTensor placement flow through the MoE layer for

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.

DTensor for fwd-bwd is gone, should we migrate to spmd_types layouts

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.

updated the placements in the graph.

Comment thread torchtitan/models/common/moe.py Outdated
local token shard. Keep logical capital suffixes here to avoid encoding
a specific parallel layout in these local tensor names.
sharding. Under EP, E may be a local shard of experts; without EP,
expert weights are replicated across TP. Under SP, R may be a local

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.

very confusing -- when dense TP is enabled, and we don't support sparse TP, we HAVE TO enable sparse EP, i.e. TP to EP.

o/w what's the point of replicating across TP?

@acisseJZhong acisseJZhong Sep 20, 2026 •

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.

got it, I will enforce TP2EP then. Previously I thought we should support the case where user specific EP=1, and dense TP on, despite it is less ideal.

Comment thread torchtitan/models/common/moe.py Outdated
TP and their load-balancing statistics therefore require TP reduction.
"""

tp_shards_tokens: bool = False

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.

confusing: we are deleting things, why there's need to add new field. Is it addressing previously existing bugs?

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.

this is to support the case EP off, dense TP on, and we still want to shard the inputs on replicate routed experts.

Should be gone if we enforce TP2EP.

Comment on lines +80 to +83
EP off, SP off: input Replicate, gate computes on all tokens, output stays
Replicate.
EP off, SP on: input Shard(0) on tokens, gate computes on the local shard,
and the output remains Shard(0).

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.

EP is always on, when TP / SP is on

@acisseJZhong
acisseJZhong force-pushed the deprecate_etp branch 4 times, most recently from 9fda4b1 to e6c99b8 Compare September 20, 2026 02:43
_hf_sequence_parallel_placement()
if enable_sp
else _hf_activation_placement(tp=spmd.P)
else _hf_activation_placement(tp=spmd.P if enable_ep else spmd.R)

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.

dense TP can't be used with sparse EP

Comment thread torchtitan/models/common/decoder.py Outdated
# Routed experts do not use dense TP. Reuse the dense TP ranks as
# EP when an MoE model has not explicitly configured EP.
if moe_configs and tp > 1 and parallelism.expert_parallel_degree == 1:
parallelism.expert_parallel_degree = tp

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.

prefer to not introduce implicit casting -- it won't help anyone

just error out if ep < tp or ep > dp_shard * cp * tp

Comment thread torchtitan/config/configs.py Outdated
Comment on lines +304 to +305
For MoE models with tensor_parallel_degree > 1, a value of 1 automatically
selects tensor_parallel_degree.

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.

same

enable_ep=enable_ep,
enable_sp=enable_sp,
expert_param_layout=_GROUPED_EXPERTS_PARAM_LAYOUT,
expert_param_names=_GROUPED_EXPERTS_PARAM_NAMES,

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.

we might not need this arg anymore, do we?

"w2_EDF": spmd.S(2),
"w3_EFD": spmd.S(1),
}
_GROUPED_EXPERTS_PARAM_NAMES = ("w1_EFD", "w2_EDF", "w3_EFD")

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.

can we remove this from every model sharding.py?

name: expert_param_placement_sparse() for name in expert_param_names
}
experts_input_layout = dense_sequence_parallel_placement()
else:

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.

else means FSDP-only sharding on routed experts, we should reject TP by not allowing "tp" in the sharding config

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.

correct, now set inner_experts_sharding_config to None when enable_ep=False.

@acisseJZhong
acisseJZhong force-pushed the deprecate_etp branch 3 times, most recently from f80a43c to b83c9b9 Compare September 20, 2026 05:02
Comment thread torchtitan/config/configs.py Outdated

max_context_length: int = 2048
"""Maximum logical context length used for training."""
"""Maximum logical context length used for trnaining."""

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.

oops

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.

wow can't believe my agent would change this out of no where.

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.

i guess it's trying to save my job lol

Comment thread torchtitan/models/common/decoder.py Outdated
)
# Sequence parallelism is effective only when TP has multiple ranks.
if moe_configs and tp == 1:
parallelism.enable_sequence_parallel = False

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.

does this concretely matter for anything?

@acisseJZhong acisseJZhong Sep 20, 2026 •

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.

it matters because in moe_sharding.py we need to pass in enable_sp, it would be wrong if we pass in default True when tp=1.

it doesn't matter.

Comment on lines +331 to +332
if enable_sp and not enable_ep:
raise ValueError("MoE sequence parallelism requires expert parallelism")

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.

I guess this is related to the other comment -- enable_sp is for dense SP right? where does it concretely matter?

@acisseJZhong acisseJZhong Sep 20, 2026 •

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.

actually yeah I think you are right, passing enable_sp=True when TP=1 wouldn't matter for MoE, as we don't have tp mesh anyways. Concretely, setting TP: spmd.V would be equivalent to TP: spmd.R when tp=1.

@acisseJZhong acisseJZhong changed the title [MoE] Remove pure TP on routed experts [MoE] Deprecate pure TP on routed experts Sep 20, 2026
enable_sp: bool,
expert_param_layout: dict[str, spmd.PerMeshAxisSpmdType],
) -> tuple[ShardingConfig, ShardingConfig]:
expert_param_names: Collection[str],

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.

do we still need this? If it's for gpt-oss, we probably should have a special function for gpt-oss, rather than requiring everyone to send these fields, as most are reusing common/moe.py

Comment thread torchtitan/config/configs.py Outdated

max_context_length: int = 2048
"""Maximum logical context length used for training."""
"""Maximum logical context length used for trnaining."""

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.

i guess it's trying to save my job lol

@acisseJZhong
acisseJZhong merged commit 610bb6f into main Sep 20, 2026
24 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 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.

3 participants