Repository navigation
[Module] Modularize MoE components - #2571
Merged
Merged
Conversation
[ghstack-poisoned]
acisseJZhong
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
March 13, 2026 22:04
acisseJZhong
marked this pull request as draft
March 13, 2026 22:30
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
acisseJZhong
marked this pull request as ready for review
March 14, 2026 19:38
tianyu-l
reviewed
Mar 15, 2026
| score_func: Literal["softmax", "sigmoid"] = "sigmoid" | ||
| route_norm: bool = False | ||
| route_scale: float = 1.0 | ||
| gate: Linear.Config = field(default_factory=Linear.Config) |
Contributor
There was a problem hiding this comment.
Maybe better if it doesn't come with a default, so that model config constructor have to specify.
| _debug_force_load_balance: bool = False, | ||
| ): | ||
| @dataclass(kw_only=True, slots=True) | ||
| class Config(Module.Config): |
Contributor
There was a problem hiding this comment.
makes sense. I wonder if we should put the following to router as well
tokens_per_expertandexpert_biasbuffer to router moduleload_balance_coeffto router config
# 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 - 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 #2540 may needs to rebase on this. [ghstack-poisoned]
# 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 - 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 #2540 may needs to rebase on this. [ghstack-poisoned]
# 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 - 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 #2540 may needs to rebase on this. [ghstack-poisoned]
# 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 - 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 #2540 may needs to rebase on this. [ghstack-poisoned]
# 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 - 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 #2540 may needs to rebase on this. [ghstack-poisoned]
acisseJZhong
added a commit
that referenced
this pull request
Mar 17, 2026
This reverts commit 1aa38d4.
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.
Stack from ghstack (oldest at bottom):
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.