Repository navigation
[MoE] sibling token_dispatcher + grouped_experts for composable override - #3859
Conversation
| @override( | ||
| "inference_moe", | ||
| target=GroupedExperts.Config, | ||
| description="Inference experts: fused SwiGLU + cudagraph-able DeepEP expand dispatch.", |
There was a problem hiding this comment.
I don't like this solution, because it couples different override together. E.g. the name of this override should be DeepEP_inference_and_fused_swiglu_moe.
A good design should compose them more or less independently.
Our current override design is module-oriented. The problem today is that
- DeepEP inference swaps the grouped_experts.dispatcher
- fused swiglu swaps the grouped_experts
- Despite they should compose, the override engine would error out to be conservative. Like, if you remove the erroring out, these two overrides should just work together out-of-box, regardless of application order, right?
I can think of two alternative solutions:
- make another parent module moe.X (I don't have a good name for now), so that we have X.grouped_experts (for swiglu override) and X.dispatcher (for deepep override). We still need moe.X to enter & exit local map region.
- lift the constraint of two overrides could not target the nested modules. This needs some soundness detection -- e.g. maybe we should first apply parent override (grouped_experts) then apply child override (grouped_experts.dispatcher) so that if the child is gone we could safely error out.
There was a problem hiding this comment.
it couples different override together. E.g. the name of this override should be DeepEP_inference_and_fused_swiglu_moe
Yes my previous design is when overriding the same module, all the override should go into a compound class(combining all the override targeting the same class) and make the name indicative. Someone could make it's override class "DeepEP_inference", another on could make
"HybridEP_inference_and_trick1_and_trick2". Each experiment run would only contain one override. It's impossible to be "composable" given we don't allow double/nested override and the override needs to align with torchtitan module boundary. I choose to put "deepEP_inference_and_fused_swiglu_moe" in torchtitan as an example is because this runs faster in inference testing.
the override engine would error out to be conservative.
This part I agree, we should not allow nested override to be conservative.
make another parent module moe.X (I don't have a good name for now), so that we have X.grouped_experts (for swiglu override) and X.dispatcher (for deepep override). We still need moe.X to enter & exit local map region.
It solved problem, but I'm hesitate about this solution if we only introduce the less meaningful wrapper X because of override. But if introducing X is because our MoE module structure is not clear enough, I think it's fine .We can have X=routed_experts, so MoE has moe.shared_experts and moe.routed_experts.dispatcher, moe.routed_experts.grouped_experts(just grouped gemm), and using routed_experts as the local map entrance and exit, instead of putting dispatcher within the grouped_experts
There was a problem hiding this comment.
class GroupedExperts
fqn: inner_experts
|
@claude review my review above |
|
Claude finished @tianyu-l's task in 3m 17s —— View job Review of @tianyu-l's reviewI traced the override engine ( 1. The coupling critique is valid (and it's a regression)Agreed that
So the pushback is well-founded. 2. "Regardless of application order, right?" — no, and here's whyThe claim that simply removing the error-out (
Now walk the two orders for B then A (child first): works.
A then B (parent first): silently drops the flip.
So it's order-dependent, and the failing order fails silently — exactly the "conservative error-out" the engine is protecting against. Claims iterate in 3. What this means for your two alternativesAlt (2) — lift the nesting constraint. This is viable but it's more than "remove the check plus detect soundness." To be correct it must:
The key correction: your phrasing "first apply parent then child" is the right order, but it only works if the child is resolved live after the parent runs. With today's collect-everything-up-front-then-mutate design, parent-then-child is precisely the broken order above. So alt (2) is a real change to Alt (1) — interpose a parent module with RecommendationYour review is correct to reject the coupling. I'd lean alt (1) if the goal is a principled, engine-unchanged composition (siblings are the design the current per-node engine actually wants), and treat alt (2) as the option only if you want to keep the module tree as-is — in which case the implementation must be staged (ancestor-first) with live child re-resolution, not merely a removed check. Whichever you pick, the standalone One more note for the author: if any interim version does allow both overrides on the same actor, the A-then-B silent-drop above means a missing-flip needs to fail loudly (assert the resulting dispatcher is |
| from . import model_registry | ||
|
|
||
|
|
||
| def enable_fused_swiglu(config: Trainer.Config) -> None: |
There was a problem hiding this comment.
need to update the name here
| @override( | ||
| "inference_moe", | ||
| target=GroupedExperts.Config, | ||
| description="Inference experts: fused SwiGLU + cudagraph-able DeepEP expand dispatch.", |
There was a problem hiding this comment.
it couples different override together. E.g. the name of this override should be DeepEP_inference_and_fused_swiglu_moe
Yes my previous design is when overriding the same module, all the override should go into a compound class(combining all the override targeting the same class) and make the name indicative. Someone could make it's override class "DeepEP_inference", another on could make
"HybridEP_inference_and_trick1_and_trick2". Each experiment run would only contain one override. It's impossible to be "composable" given we don't allow double/nested override and the override needs to align with torchtitan module boundary. I choose to put "deepEP_inference_and_fused_swiglu_moe" in torchtitan as an example is because this runs faster in inference testing.
the override engine would error out to be conservative.
This part I agree, we should not allow nested override to be conservative.
make another parent module moe.X (I don't have a good name for now), so that we have X.grouped_experts (for swiglu override) and X.dispatcher (for deepep override). We still need moe.X to enter & exit local map region.
It solved problem, but I'm hesitate about this solution if we only introduce the less meaningful wrapper X because of override. But if introducing X is because our MoE module structure is not clear enough, I think it's fine .We can have X=routed_experts, so MoE has moe.shared_experts and moe.routed_experts.dispatcher, moe.routed_experts.grouped_experts(just grouped gemm), and using routed_experts as the local map entrance and exit, instead of putting dispatcher within the grouped_experts
Address review (tianyu-l on #3859): the config field / module attribute (and thus the FQN moe.routed_experts.grouped_experts) is renamed to inner_experts, while the class name stays GroupedExperts and the fused_grouped_experts override (which targets GroupedExperts.Config, not the field) is unchanged. State-dict adapters, sharding, quantization, graph_trainer, RL configs, tests and docs updated to the new FQN. Pure rename (word-boundary; class/override names preserved).
Address review (tianyu-l on #3859): the config field / module attribute (and thus the FQN moe.routed_experts.grouped_experts) is renamed to inner_experts, while the class name stays GroupedExperts and the fused_grouped_experts override (which targets GroupedExperts.Config, not the field) is unchanged. State-dict adapters, sharding, quantization, graph_trainer, RL configs, tests and docs updated to the new FQN. Pure rename (word-boundary; class/override names preserved).
tianyu-l
left a comment
There was a problem hiding this comment.
I'm positive about this change, but given the scope I'd like to have @sanketpurandare give feedback as well.
| b2_RD = ScaleBiasForward.apply(b2_RD, tp_degree, h_RD.dtype) | ||
| return h_RD + b2_RD | ||
|
|
||
| def forward( |
There was a problem hiding this comment.
are these code gone because now we share the dispatcher logic in routed experts?
There was a problem hiding this comment.
Those functions go away because dispatch/combine now live once in the shared RoutedExperts.forward() and RoutedExperts.paralell()
There was a problem hiding this comment.
The file MOE_SHARDING.md is not present in this PR, it should also be updated since it still mentions GroupedExperts for the LocalMapConfig
There was a problem hiding this comment.
Updated MOE_SHARDING.md, thank you
|
The CI timeout (8 GPU Model tests) are not related to this change |
…_experts}
Separate the routed experts' communication (token dispatch/combine) from the
grouped-GEMM computation into sibling submodules under moe.routed_experts
(RoutedExperts, the local_map region):
routed_experts (RoutedExperts)
- token_dispatcher token dispatch/combine
- grouped_experts (GroupedExperts) grouped-GEMM expert weights
This makes the fused-experts override (fused_grouped_experts -> grouped_experts)
and the DeepEP inference override (deepep_inference -> token_dispatcher) claim
disjoint sibling nodes, so they compose without the ancestor/descendant conflict
that previously forced the combined inference_moe override. inference_moe is
removed; deepep_inference is a standalone dispatcher override; fused_swiglu
registers both the dense-FFN and routed-experts fusion.
Expert FQNs move moe.experts.* -> moe.routed_experts.grouped_experts.*; per-model
HF state-dict adapters updated. Pure structural refactor (bitwise-identical loss
on deepseek_v3 and gpt_oss).
| class Config(Module.Config): | ||
| num_experts: int | ||
| inner_experts: InnerExperts.Config | ||
| dispatcher: LocalTokenDispatcher.Config |
There was a problem hiding this comment.
Need to token_dispatcher
|
|
||
| @dataclass(kw_only=True, slots=True) | ||
| class Config(Module.Config): | ||
| num_experts: int |
| @override( | ||
| "inference_moe", | ||
| target=GroupedExperts.Config, | ||
| description="Inference experts: fused SwiGLU + cudagraph-able DeepEP expand dispatch.", |
There was a problem hiding this comment.
class GroupedExperts
fqn: inner_experts
| b2_RD = ScaleBiasForward.apply(b2_RD, tp_degree, h_RD.dtype) | ||
| return h_RD + b2_RD | ||
|
|
||
| def forward( |
There was a problem hiding this comment.
Those functions go away because dispatch/combine now live once in the shared RoutedExperts.forward() and RoutedExperts.paralell()
There was a problem hiding this comment.
Updated MOE_SHARDING.md, thank you
|
…3859) 70th sync: upstream pytorch#3859 split routed experts into sibling submodules -- MoE.routed_experts (RoutedExperts, the local_map region) now composes inner_experts (GroupedExperts weights) + token_dispatcher, instead of the old flat MoE.experts=GroupedExperts(owns dispatcher). GroupedExperts._experts_forward was renamed to forward and token_dispatcher removed from its Config. Our ezpz/moe mirrored the old pattern -> moe_debugmodel crashed (EzpzGroupedExperts.Config got unexpected kwarg token_dispatcher; caught by sync_smoke job 12470895, rc=143). Replay across the ezpz/moe tree: - experts.py: _experts_forward -> forward (override + super() call + comment) - __init__.py: make_ezpz_experts_config now returns RoutedExperts.Config (inner_experts + token_dispatcher siblings); make_ezpz_moe_config + caller use routed_experts=; EP=1 dispatcher-rebuild block walks routed_experts.{inner_experts,token_dispatcher} - model.py: config-mutation walks routed_experts.inner_experts.compute_backend + routed_experts.token_dispatcher - parallelize.py: runtime access moe.routed_experts.inner_experts.parameters() - state_dict_adapter.py: expert weight FQNs moe.experts.wN -> moe.routed_experts.inner_experts.wN (HF-side mlp.experts unchanged) Config-build verified: moe_debugmodel() constructs the RoutedExperts(inner + dispatcher) hierarchy with our compute_backend preserved. GPU smoke next.
…rts replay 3 commits (merge 412d93f): the MoE refactor pytorch#3859 (sibling RoutedExperts=inner_experts+token_dispatcher) required a 5-file replay in ezpz/moe; sync_smoke VERDICT ok (job 12470902, all 3 configs incl moe rc=0). Full detail in the 70th-sync entry.
Absorbs 3 upstream commits: RL CI fix (pytorch#3930), the FlexAttention spmd_types output typing change (pytorch#3924), and pytorch#3859 ("[MoE] sibling token_dispatcher + grouped_experts for composable override"), which refactored the core MoE. pytorch#3859 required adapting the HF backend (no git conflicts, but silent API/struct breakage): - moe_replacement.py: make_experts_config -> make_routed_experts_config + make_moe_config(routed_experts=...); dropped the token_dispatcher kwarg from the GroupedExperts.Config probe (token_dispatcher moved to RoutedExperts). - parallelize.py: FSDP expert sharding reads moe.routed_experts.inner_experts (was moe.experts). - state_dict_adapter.py: titan MoE expert FQNs moved experts.* -> routed_experts.inner_experts.*; the adapter still emitted the old prefix, so load_state_dict(strict=False) silently skipped the expert weights (random init -> HF-vs-titan forward cos ~= 0 while the round-trip still passed). Route the routed-expert keys through the new prefix; also tighten the gate_up/down special-cases to ".experts." so shared_experts can't be misrouted. Verified: MoE numerical equivalence restored (qwen3_moe/deepseek_v2/olmoe PASS cos ~= 1.0; mixtral WARN at cos 0.999996 -- the documented bf16-combine band, not structural); round-trips max_diff=0.00; MoE EP=2 trains 12.33 -> 6.72.
… RL cleanup) + fix dashboard SFT-full row - journal.md: add the 2026-07-18 session -- full-mix SFT eval complete on all 3 axes (ckpt-900 deliverable, 8672 overfit; LR-x-tokens root cause), GRPO 0.31->0.74 validation + the vLLM-path detour lesson, RL script cleanup, 70th sync (MoE pytorch#3859 replay), macOS import-ezpz gotcha - dashboard: fix self-contradictory SFT-full row (advancing+complete) -> clean complete with deliverable=ckpt-900 summary across all 3 eval axes
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #4235 * __->__ #4249 PR #3859 moved grouped expert weights from MoE.experts to MoE.routed_experts.inner_experts. Update the transformers modeling backend tests to follow the new module structure.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #4235 * __->__ #4249 PR #3859 moved grouped expert weights from MoE.experts to MoE.routed_experts.inner_experts. Update the transformers modeling backend tests to follow the new module structure. Original PR: #4249 (cherry picked from commit 1dfeff9)
Previously
fused_swiglu (grouped-experts variant) swapped the experts module and deepep_inference
swapped its token dispatcher. Since the dispatcher was a child of the experts module, the
two overrides were ancestor/descendant and the override engine rejected them (it's
order-dependent). The workaround was a combined inference_moe override.
Now
Split the routed experts so communication and computation are sibling submodules, with
the parent as the local_map region:
MoE
├── routed_experts # RoutedExperts — local_map region (dispatch -> GEMM ->
combine)
│ ├── token_dispatcher # <- deepep_inference
│ └── grouped_experts # grouped-GEMM weights <- fused_grouped_experts
└── shared_experts
Now the two overrides claim disjoint sibling nodes and compose out of the box,
order-independently — no engine change needed.
dispatcher override.
(fused_grouped_experts) fusion.
updated (no native-DCP back-compat by design).
Validation
tests).