Skip to content

[Module] Add configurable RMSNorm that inherits from Module - #2434

Merged
fegin merged 11 commits into
mainfrom
gh/fegin/82/head
Mar 7, 2026
Merged

fegin merged 11 commits into
mainfrom
gh/fegin/82/head

Conversation

@fegin

@fegin fegin commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 24, 2026
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
fegin marked this pull request as draft February 24, 2026 21:28
Comment thread torchtitan/models/qwen3/__init__.py Resolved
Comment thread torchtitan/models/common/decoder.py Resolved
[ghstack-poisoned]
[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]
[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
fegin requested a review from tianyu-l February 26, 2026 22:52
@fegin
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: curious why we use norm.eps for model_args, and hf_config.rms_norm_eps below, can we align them?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw I think this file it to be deprecated.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw I think this file it to be deprecated.

Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/flux/model/layers.py Resolved
Comment thread torchtitan/models/qwen3/__init__.py Resolved
Comment thread torchtitan/models/common/rmsnorm.py Resolved
Comment thread torchtitan/models/common/rmsnorm.py Resolved
@fegin

fegin commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

This PR is not up-to-date. I realized that it is painful to do two PRs at the same time while #2428 keeps changing. So I didn't update this PR even though #2428 is updated. I'll update this PR after #2428 is landed.

fegin added 2 commits March 5, 2026 11:48
[ghstack-poisoned]
[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
[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: 4758ca2
Pull-Request: #2434
@fegin
fegin requested review from acisseJZhong and tianyu-l March 5, 2026 20:41
@fegin
fegin changed the base branch from gh/fegin/82/base to main March 6, 2026 23:46
@fegin
fegin merged commit 319e350 into main Mar 7, 2026
18 of 27 checks passed
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.
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.
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.

4 participants