Repository navigation
[Module] Modularize MoE components - #2615
Merged
Merged
Conversation
acisseJZhong
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
March 17, 2026 17:32
tianyu-l
approved these changes
Mar 17, 2026
weifengpy
pushed a commit
to weifengpy/torchtitan-flexshard
that referenced
this pull request
Mar 27, 2026
# Motivation Continues the modularization effort from pytorch#2428 (Embedding), pytorch#2434 (RMSNorm), and pytorch#2527 (Linear). This PR makes MoE components (GroupedExperts, TokenChoiceTopKRouter, TokenReorderer) inherit from Module and use the Config/build() pattern, enabling per-component configurability and consistent weight initialization. # Summary - GroupedExperts inherits from Module with a nested Config dataclass. dim, hidden_dim, and num_experts use field(init=False) so they are set at build() time by the parent MoE. - TokenChoiceTopKRouter inherits from Module with a nested Config dataclass. Replaces the flat gate_bias: bool field with gate: Linear.Config, enabling full gate configuration (e.g., Linear.Config(bias=True)). dim and num_experts use field(init=False). - TokenReorderer inherits from Module with a no-op init_weights (stateless module). num_experts and top_k use field(init=False). - MoE.Config replaces flat router/expert fields (top_k, score_func, route_norm, route_scale, gate_bias, use_grouped_mm, num_expert_groups, num_limited_groups, _debug_force_load_balance) with nested experts: GroupedExperts.Config and router: TokenChoiceTopKRouter.Config. - Updates all model configs (DeepSeek V3, Qwen3, GPT-OSS, Llama4) to use nested router/expert configs. - Adds 34 unit tests covering Config/build(), init_weights, forward passes, and nested config propagation for all modularized components. cc @fegin pytorch#2540 may needs to rebase on this.
TXacs
pushed a commit
to McmillanTAC/torchtitan
that referenced
this pull request
Apr 13, 2026
# Motivation Continues the modularization effort from pytorch#2428 (Embedding), pytorch#2434 (RMSNorm), and pytorch#2527 (Linear). This PR makes MoE components (GroupedExperts, TokenChoiceTopKRouter, TokenReorderer) inherit from Module and use the Config/build() pattern, enabling per-component configurability and consistent weight initialization. # Summary - GroupedExperts inherits from Module with a nested Config dataclass. dim, hidden_dim, and num_experts use field(init=False) so they are set at build() time by the parent MoE. - TokenChoiceTopKRouter inherits from Module with a nested Config dataclass. Replaces the flat gate_bias: bool field with gate: Linear.Config, enabling full gate configuration (e.g., Linear.Config(bias=True)). dim and num_experts use field(init=False). - TokenReorderer inherits from Module with a no-op init_weights (stateless module). num_experts and top_k use field(init=False). - MoE.Config replaces flat router/expert fields (top_k, score_func, route_norm, route_scale, gate_bias, use_grouped_mm, num_expert_groups, num_limited_groups, _debug_force_load_balance) with nested experts: GroupedExperts.Config and router: TokenChoiceTopKRouter.Config. - Updates all model configs (DeepSeek V3, Qwen3, GPT-OSS, Llama4) to use nested router/expert configs. - Adds 34 unit tests covering Config/build(), init_weights, forward passes, and nested config propagation for all modularized components. cc @fegin pytorch#2540 may needs to rebase on this.
ACharacterInASimulation
pushed a commit
to ACharacterInASimulation/torchtitan
that referenced
this pull request
Apr 21, 2026
# Motivation Continues the modularization effort from pytorch#2428 (Embedding), pytorch#2434 (RMSNorm), and pytorch#2527 (Linear). This PR makes MoE components (GroupedExperts, TokenChoiceTopKRouter, TokenReorderer) inherit from Module and use the Config/build() pattern, enabling per-component configurability and consistent weight initialization. # Summary - GroupedExperts inherits from Module with a nested Config dataclass. dim, hidden_dim, and num_experts use field(init=False) so they are set at build() time by the parent MoE. - TokenChoiceTopKRouter inherits from Module with a nested Config dataclass. Replaces the flat gate_bias: bool field with gate: Linear.Config, enabling full gate configuration (e.g., Linear.Config(bias=True)). dim and num_experts use field(init=False). - TokenReorderer inherits from Module with a no-op init_weights (stateless module). num_experts and top_k use field(init=False). - MoE.Config replaces flat router/expert fields (top_k, score_func, route_norm, route_scale, gate_bias, use_grouped_mm, num_expert_groups, num_limited_groups, _debug_force_load_balance) with nested experts: GroupedExperts.Config and router: TokenChoiceTopKRouter.Config. - Updates all model configs (DeepSeek V3, Qwen3, GPT-OSS, Llama4) to use nested router/expert configs. - Adds 34 unit tests covering Config/build(), init_weights, forward passes, and nested config propagation for all modularized components. cc @fegin pytorch#2540 may needs to rebase on this.
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.
Motivation
Continues the modularization effort from #2428 (Embedding), #2434 (RMSNorm), and
#2527 (Linear). This PR makes MoE components (GroupedExperts,
TokenChoiceTopKRouter, TokenReorderer) inherit from Module and use the
Config/build() pattern, enabling per-component configurability and consistent
weight initialization.
Summary
hidden_dim, and num_experts use field(init=False) so they are set at build() time
by the parent MoE.
Replaces the flat gate_bias: bool field with gate: Linear.Config, enabling full
gate configuration (e.g., Linear.Config(bias=True)). dim and num_experts use
field(init=False).
module). num_experts and top_k use field(init=False).
route_scale, gate_bias, use_grouped_mm, num_expert_groups, num_limited_groups,
_debug_force_load_balance) with nested experts: GroupedExperts.Config and router:
TokenChoiceTopKRouter.Config.
router/expert configs.
nested config propagation for all modularized components.
cc @fegin #2540 may needs to rebase on this.