Skip to content

Commit a1b6bc9

Browse files
committed
Drop redundant num_experts from RoutedExperts
num_experts is intrinsic to the grouped-GEMM (GroupedExperts, which sizes w1_EFD/w2_EDF/w3_EFD); RoutedExperts is a pure wrapper (token_dispatcher + inner_experts) and carried a duplicate. Read it from the source of truth (routed_experts.inner_experts.num_experts) in MoE buffer sizing and the HF export instead.
1 parent 00f5206 commit a1b6bc9

4 files changed

Lines changed: 3 additions & 7 deletions

File tree

torchtitan/experiments/rl/models/vllm_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def model_spec_to_hf_config_dict(spec: ModelSpec) -> dict[str, Any]:
167167

168168
if moe is not None:
169169
# Presence required: >0 toggles MoE/EP branches.
170-
hf["num_experts"] = moe.routed_experts.num_experts
170+
hf["num_experts"] = moe.routed_experts.inner_experts.num_experts
171171
# Unused: only per-model loaders (qwen3_moe, deepseek_v2, ...) and v1/metrics/perf.py (off) read these.
172172
hf[
173173
"num_experts_per_tok"

torchtitan/models/common/config_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ def make_routed_experts_config(
392392
inference override targets the latter.
393393
"""
394394
return RoutedExperts.Config(
395-
num_experts=num_experts,
396395
inner_experts=GroupedExperts.Config(
397396
dim=dim,
398397
hidden_dim=hidden_dim,

torchtitan/models/common/moe.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,11 @@ class RoutedExperts(Module):
141141

142142
@dataclass(kw_only=True, slots=True)
143143
class Config(Module.Config):
144-
num_experts: int
145144
inner_experts: GroupedExperts.Config
146145
token_dispatcher: LocalTokenDispatcher.Config
147146

148147
def __init__(self, config: Config):
149148
super().__init__()
150-
self.num_experts = config.num_experts
151149
self.inner_experts = config.inner_experts.build()
152150
self.token_dispatcher = config.token_dispatcher.build()
153151

@@ -559,9 +557,9 @@ def _init_self_buffers(self, *, buffer_device: torch.device | None = None) -> No
559557

560558
with torch.device(buffer_device):
561559
self.tokens_per_expert_E = torch.zeros(
562-
self.routed_experts.num_experts, dtype=torch.float32
560+
self.routed_experts.inner_experts.num_experts, dtype=torch.float32
563561
)
564562
if self.load_balance_coeff is not None:
565563
self.expert_bias_E = torch.zeros(
566-
self.routed_experts.num_experts, dtype=torch.float32
564+
self.routed_experts.inner_experts.num_experts, dtype=torch.float32
567565
)

torchtitan/models/gpt_oss/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ def _make_gptoss_experts_config(
174174
"mlp2_bias_ED": partial(nn.init.trunc_normal_, std=std),
175175
}
176176
return RoutedExperts.Config(
177-
num_experts=num_experts,
178177
inner_experts=GptOssGroupedExperts.Config(
179178
dim=dim,
180179
hidden_dim=hidden_dim,

0 commit comments

Comments
 (0)