Repository navigation
[Module] Add configurable Linear that inherits from Module - #2527
Conversation
[ghstack-poisoned]
**Why the Change** Same as #2428 and #2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip. ghstack-source-id: 12305fc Pull-Request: #2527
**Why the Change** Same as #2428 and #2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip. ghstack-source-id: d957648 Pull-Request: #2527
**Why the Change** Same as #2428 and #2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip. ghstack-source-id: b2da087 Pull-Request: #2527
[ghstack-poisoned]
| @@ -546,12 +558,8 @@ def forward( | |||
|
|
|||
| def init_weights(self, init_std: float = 0.02, **kwargs) -> None: | |||
| for linear in (self.wq, self.wk, self.wv): | |||
There was a problem hiding this comment.
So after this pr, we will call init_weights to all weights in linear init_weights
| self.w1 = nn.Linear(dim, config.hidden_dim, bias=config.bias) | ||
| self.w2 = nn.Linear(config.hidden_dim, dim, bias=config.bias) | ||
| self.w3 = nn.Linear(dim, config.hidden_dim, bias=config.bias) | ||
| self.w1 = config.w1.build( |
There was a problem hiding this comment.
same: pass in config.bias?
There was a problem hiding this comment.
Same reply. config.bias has been set when config is created. Users should only pass arguments that need to be computed during runtime or shared across modules to build().
|
@claude review this PR |
|
Claude finished @tianyu-l's task in 3m 10s —— View job PR Review: [Module] Add configurable Linear that inherits from Module
Overall AssessmentThis PR introduces a well-structured Below are the issues and suggestions I identified, roughly ordered by severity. Issues1.
|
| w1: Linear.Config | ||
| w2: Linear.Config | ||
| w3: Linear.Config |
There was a problem hiding this comment.
I would consider these as "SwiGLUFeedForward" implementation details that doesn't need to be swapped / configurable, until we receive requests to configure them. It's kind verbose to have them in every model config registry without specifying any real arg.
You may argue that people still want to experiment with different init -- that's the reason why we have next PR.
Same for wq/wk/wv/wo.
There was a problem hiding this comment.
Chatted offline
- we think it's not necessary to use build style at all for these internal details of a module.
- the init method can be owned by root module, as a dict from fqn to init function mapping, which can be changed by users who modify model code
- the sharding may still need to be defined per each module (unclear right now), but probably should still be owned by parent module
| # We need to first verify if all nn.Linear have been converted to Linear. | ||
| verify_module_protocol(model, nn.Linear, Linear) | ||
| saved_attrs = capture_module_attrs( | ||
| model, ["_init_mean", "_init_std"], nn_module_cls=nn.Linear |
There was a problem hiding this comment.
Are these the only thing we need to save? I thought
- these fields could be moved to config, not in the Linear class itself.
- but then we still need to keep the inheritance from Float8Linear to torchtitan.Linear.
After all, if we have to keep such code, they should exist in torchao, not torchtitan.
@danielvegamyhre what's your latest thought on torchao APIs? In this PR, we are having torchtitan-specialized nn.Linear and I think torchao should still work without we do anything special here.
There was a problem hiding this comment.
these fields could be moved to config, not in the Linear class itself.
These fields are moved to StateInitializer in the next PR, #2540. The only thing we need to save is_state_initializer.
After all, if we have to keep such code, they should exist in torchao, not torchtitan.
I'm happy to remove this logic if torchao handle this. But I don't want to be blocked by another refactor from another repo. I would consider to move forward with this logic living inside this repo and refactor until both repo are happy.
There was a problem hiding this comment.
That's fine. If so, let's
- move these utils inside quantization, not in general
protocolsfolder. - mark them as temporary and leave TODOs
**Why the Change** Same as #2428 and #2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip. ghstack-source-id: 85093ae Pull-Request: #2527
tianyu-l
left a comment
There was a problem hiding this comment.
sg, please address #2527 (comment) before merge
[ghstack-poisoned]
**Why the Change** Same as #2428 and #2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip. ghstack-source-id: f205521 Pull-Request: #2527
) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2527 **Why the Change** Same as pytorch#2428 and pytorch#2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip.
# 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]
Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at bottom): * __->__ #2571 # 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.
# 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.
) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2527 **Why the Change** Same as pytorch#2428 and pytorch#2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip.
# 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.
) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2527 **Why the Change** Same as pytorch#2428 and pytorch#2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip.
# 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.
) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2527 **Why the Change** Same as pytorch#2428 and pytorch#2434. This is the third PR that changes Linear to inherit from Module. But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules. **Summary** - Introduces `torchtitan.models.common.linear.Linear`, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding. - Updates all shared components (GQAttention, FeedForward, Decoder) and all model configs (Llama3, Llama4, Qwen3, DeepSeek V3, GPT-OSS, Flux) to use Linear.Config instead of raw nn.Linear. - Adds capture_module_configs() and inject_module_protocol() utilities in protocols/module.py to preserve the Module protocol through Float8/MX quantization conversions. Float8 creates new instances (losing attributes), so configs are captured beforehand and re-attached afterwards. MX uses class swaps (attributes survive), so re-attachment acts as a safety net. - Replaces the bias: bool field on GQAttention.Config with per-projection Linear.Config fields (wq, wk, wv, wo), enabling per-linear configuration of bias, init_std, and init_mean. - Adds comprehensive unit tests for Linear, config injection, and the capture/re-attach round-trip.
# 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.
Stack from ghstack (oldest at bottom):
Why the Change
Same as #2428 and #2434. This is the third PR that changes Linear to inherit from Module.
But unlike previous two PRs, we also need to consider converter cases that swap the linear modules with fp8/mx linear modules.
Summary
torchtitan.models.common.linear.Linear, a configurable nn.Linear subclass that also inherits from Module, following the same Config/build() pattern as RMSNorm and Embedding.