Skip to content

[Module] Modularize MoE components - #2615

Merged
acisseJZhong merged 3 commits into
mainfrom
modularize_moe_experts
Mar 17, 2026
Merged

acisseJZhong merged 3 commits into
mainfrom
modularize_moe_experts

Conversation

@acisseJZhong

Copy link
Copy Markdown
Contributor

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-source-id: 21a20a2
Pull Request resolved: #2614
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 17, 2026
@acisseJZhong
acisseJZhong merged commit 3787817 into main Mar 17, 2026
23 of 30 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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