Repository navigation
[Module] Add configurable RMSNorm that inherits from Module - #2434
Merged
Merged
Conversation
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Feb 24, 2026
** BC Breaking ** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. ** Why the Change ** Same as #2428. This PR changes RMSNorm to inherit from Module. ** Summary ** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config. ghstack-source-id: bf4d8ec Pull-Request: #2434
fegin
marked this pull request as draft
February 24, 2026 21:28
tianyu-l
reviewed
Feb 24, 2026
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Feb 24, 2026
** BC Breaking ** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. ** Why the Change ** Same as #2428. This PR changes RMSNorm to inherit from Module. ** Summary ** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config. ghstack-source-id: 207cf6a Pull-Request: #2434
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Feb 26, 2026
** BC Breaking ** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. ** Why the Change ** Same as #2428. This PR changes RMSNorm to inherit from Module. ** Summary ** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config. ghstack-source-id: 0888ebc Pull-Request: #2434
fegin
marked this pull request as ready for review
February 26, 2026 22:53
Comment on lines
+161
to
+306
| @@ -248,8 +248,8 @@ def __init__(self, layer_id: int, model_args: Qwen3Model.Config): | |||
| dim=model_args.dim, hidden_dim=model_args.hidden_dim | |||
| ) | |||
|
|
|||
| self.attention_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm_eps) | |||
| self.ffn_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm_eps) | |||
| self.attention_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm.eps) | |||
| self.ffn_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm.eps) | |||
|
|
|||
| if model_args.depth_init: | |||
| self.weight_init_std = 0.02 / (2 * (layer_id + 1)) ** 0.5 | |||
| @@ -303,7 +303,7 @@ def __init__(self, model_args: Qwen3Model.Config): | |||
| for layer_id in range(model_args.n_layers): | |||
| self.layers[str(layer_id)] = TransformerBlock(layer_id, model_args) | |||
|
|
|||
| self.norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm_eps) | |||
| self.norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm.eps) | |||
Contributor
There was a problem hiding this comment.
nit: curious why we use norm.eps for model_args, and hf_config.rms_norm_eps below, can we align them?
Contributor
There was a problem hiding this comment.
btw I think this file it to be deprecated.
tianyu-l
reviewed
Feb 27, 2026
Comment on lines
+161
to
+306
| @@ -248,8 +248,8 @@ def __init__(self, layer_id: int, model_args: Qwen3Model.Config): | |||
| dim=model_args.dim, hidden_dim=model_args.hidden_dim | |||
| ) | |||
|
|
|||
| self.attention_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm_eps) | |||
| self.ffn_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm_eps) | |||
| self.attention_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm.eps) | |||
| self.ffn_norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm.eps) | |||
|
|
|||
| if model_args.depth_init: | |||
| self.weight_init_std = 0.02 / (2 * (layer_id + 1)) ** 0.5 | |||
| @@ -303,7 +303,7 @@ def __init__(self, model_args: Qwen3Model.Config): | |||
| for layer_id in range(model_args.n_layers): | |||
| self.layers[str(layer_id)] = TransformerBlock(layer_id, model_args) | |||
|
|
|||
| self.norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm_eps) | |||
| self.norm = VLLMRMSNorm(model_args.dim, eps=model_args.norm.eps) | |||
Contributor
There was a problem hiding this comment.
btw I think this file it to be deprecated.
wwwjn
reviewed
Mar 2, 2026
Contributor
Author
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Mar 5, 2026
** BC Breaking ** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. ** Why the Change ** Same as #2428. This PR changes RMSNorm to inherit from Module. ** Summary ** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config. ghstack-source-id: bb81105 Pull-Request: #2434
fegin
added a commit
that referenced
this pull request
Mar 5, 2026
** BC Breaking ** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. ** Why the Change ** Same as #2428. This PR changes RMSNorm to inherit from Module. ** Summary ** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config. ghstack-source-id: 4758ca2 Pull-Request: #2434
fegin
added a commit
that referenced
this pull request
Mar 9, 2026
**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
fegin
added a commit
that referenced
this pull request
Mar 9, 2026
**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
fegin
added a commit
that referenced
this pull request
Mar 9, 2026
**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
fegin
added a commit
that referenced
this pull request
Mar 11, 2026
**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
acisseJZhong
pushed a commit
that referenced
this pull request
Mar 11, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ #2434 **BC Breaking** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. **Why the Change** Same as #2428. This PR changes RMSNorm to inherit from Module. **Summary** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config.
fegin
added a commit
that referenced
this pull request
Mar 12, 2026
**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
fegin
added a commit
that referenced
this pull request
Mar 12, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ #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.
mori360
pushed a commit
to mori360/torchtitan
that referenced
this pull request
Mar 12, 2026
) 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.
acisseJZhong
added a commit
that referenced
this pull request
Mar 17, 2026
# 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
# 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
# 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
# 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
# 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
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.
This was referenced Mar 17, 2026
acisseJZhong
added a commit
that referenced
this pull request
Mar 17, 2026
# 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.
weifengpy
pushed a commit
to weifengpy/torchtitan-flexshard
that referenced
this pull request
Mar 27, 2026
…2434) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2434 **BC Breaking** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. **Why the Change** Same as pytorch#2428. This PR changes RMSNorm to inherit from Module. **Summary** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config.
weifengpy
pushed a commit
to weifengpy/torchtitan-flexshard
that referenced
this pull request
Mar 27, 2026
) 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.
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
…2434) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2434 **BC Breaking** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. **Why the Change** Same as pytorch#2428. This PR changes RMSNorm to inherit from Module. **Summary** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config.
TXacs
pushed a commit
to McmillanTAC/torchtitan
that referenced
this pull request
Apr 13, 2026
) 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.
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
…2434) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2434 **BC Breaking** This is a breaking change for any downstream code that passes `norm_eps` directly to model configs. **Why the Change** Same as pytorch#2428. This PR changes RMSNorm to inherit from Module. **Summary** Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method. All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to: - Use RMSNorm.Config fields in their dataclass configs instead of bare norm_eps: float - Build norms via config.build(normalized_shape=dim) instead of nn.RMSNorm(dim, eps=...) - Call norm.init_weights() instead of norm.reset_parameters() The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config.
ACharacterInASimulation
pushed a commit
to ACharacterInASimulation/torchtitan
that referenced
this pull request
Apr 21, 2026
) 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.
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.
Stack from ghstack (oldest at bottom):
BC Breaking
This is a breaking change for any downstream code that passes
norm_epsdirectly to model configs.Why the Change
Same as #2428. This PR changes RMSNorm to inherit from Module.
Summary
Introduces RMSNorm, a configurable wrapper around nn.RMSNorm that inherits from the Module. This enables RMSNorm to participate in the Config.build() pattern and provides a standardized init_weights() method.
All model families (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss, Flux) are updated to:
The norm_eps field is removed from TransformerBlock.Config and Decoder.Config since the eps value is now encapsulated in RMSNorm.Config.