From 6427d91d1e0e85420146d4da6fc744542dd366e5 Mon Sep 17 00:00:00 2001 From: mstojkovicTT Date: Thu, 27 Nov 2025 15:33:09 +0000 Subject: [PATCH 1/4] RMSNorm module implementation change -> torch.nn.functional.rms_norm --- src/transformers/models/aimv2/modeling_aimv2.py | 6 +----- .../models/apertus/modeling_apertus.py | 6 +----- src/transformers/models/arcee/modeling_arcee.py | 6 +----- src/transformers/models/aria/modeling_aria.py | 6 +----- src/transformers/models/bamba/modeling_bamba.py | 6 +----- src/transformers/models/bitnet/modeling_bitnet.py | 6 +----- src/transformers/models/blt/modeling_blt.py | 6 +----- .../models/chameleon/modeling_chameleon.py | 6 +----- src/transformers/models/clvp/modeling_clvp.py | 6 +----- src/transformers/models/csm/modeling_csm.py | 6 +----- src/transformers/models/cwm/modeling_cwm.py | 6 +----- .../models/deepseek_v2/modeling_deepseek_v2.py | 6 +----- .../models/deepseek_v3/modeling_deepseek_v3.py | 6 +----- src/transformers/models/dia/modeling_dia.py | 6 +----- .../models/diffllama/modeling_diffllama.py | 6 +----- src/transformers/models/doge/modeling_doge.py | 6 +----- src/transformers/models/emu3/modeling_emu3.py | 6 +----- .../models/ernie4_5/modeling_ernie4_5.py | 6 +----- .../models/ernie4_5_moe/modeling_ernie4_5_moe.py | 6 +----- src/transformers/models/evolla/modeling_evolla.py | 6 +----- .../models/exaone4/modeling_exaone4.py | 6 +----- .../models/falcon_h1/modeling_falcon_h1.py | 6 +----- src/transformers/models/glm/modeling_glm.py | 6 +----- src/transformers/models/glm4/modeling_glm4.py | 6 +----- .../models/glm4_moe/modeling_glm4_moe.py | 6 +----- src/transformers/models/glm4v/modeling_glm4v.py | 8 +++----- .../models/glm4v_moe/modeling_glm4v_moe.py | 14 ++++---------- .../models/gpt_neox/modeling_gpt_neox.py | 6 +----- .../models/granite/modeling_granite.py | 6 +----- .../models/granitemoe/modeling_granitemoe.py | 6 +----- .../granitemoehybrid/modeling_granitemoehybrid.py | 6 +----- .../granitemoeshared/modeling_granitemoeshared.py | 6 +----- .../hunyuan_v1_dense/modeling_hunyuan_v1_dense.py | 6 +----- .../hunyuan_v1_moe/modeling_hunyuan_v1_moe.py | 6 +----- .../models/idefics2/modeling_idefics2.py | 6 +----- .../models/idefics3/modeling_idefics3.py | 6 +----- .../models/internvl/modeling_internvl.py | 6 +----- src/transformers/models/jamba/modeling_jamba.py | 6 +----- src/transformers/models/jetmoe/modeling_jetmoe.py | 6 +----- src/transformers/models/lfm2/modeling_lfm2.py | 6 +----- .../models/lfm2_moe/modeling_lfm2_moe.py | 6 +----- src/transformers/models/llama/modeling_llama.py | 6 +----- .../models/longcat_flash/modeling_longcat_flash.py | 6 +----- .../models/minimax/modeling_minimax.py | 6 +----- .../models/mistral/modeling_mistral.py | 6 +----- .../models/mistral3/modeling_mistral3.py | 6 +----- .../models/mixtral/modeling_mixtral.py | 6 +----- src/transformers/models/mllama/modeling_mllama.py | 6 +----- src/transformers/models/olmoe/modeling_olmoe.py | 6 +----- src/transformers/models/ovis2/modeling_ovis2.py | 6 +----- src/transformers/models/phi3/modeling_phi3.py | 6 +----- .../phi4_multimodal/modeling_phi4_multimodal.py | 6 +----- src/transformers/models/phimoe/modeling_phimoe.py | 6 +----- .../models/pixtral/modeling_pixtral.py | 6 +----- .../models/qwen2_moe/modeling_qwen2_moe.py | 6 +----- .../models/qwen3_moe/modeling_qwen3_moe.py | 6 +----- .../qwen3_omni_moe/modeling_qwen3_omni_moe.py | 12 ++---------- .../models/qwen3_vl_moe/modeling_qwen3_vl_moe.py | 6 +----- .../models/seed_oss/modeling_seed_oss.py | 6 +----- .../models/smollm3/modeling_smollm3.py | 6 +----- .../models/timesfm/modeling_timesfm.py | 6 +----- src/transformers/models/zamba/modeling_zamba.py | 6 +----- src/transformers/models/zamba2/modeling_zamba2.py | 6 +----- 63 files changed, 69 insertions(+), 325 deletions(-) diff --git a/src/transformers/models/aimv2/modeling_aimv2.py b/src/transformers/models/aimv2/modeling_aimv2.py index 2ce372c1040a..fc29ae7a90c5 100644 --- a/src/transformers/models/aimv2/modeling_aimv2.py +++ b/src/transformers/models/aimv2/modeling_aimv2.py @@ -90,11 +90,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/apertus/modeling_apertus.py b/src/transformers/models/apertus/modeling_apertus.py index 4f0407862c25..572ef762d03a 100644 --- a/src/transformers/models/apertus/modeling_apertus.py +++ b/src/transformers/models/apertus/modeling_apertus.py @@ -67,11 +67,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/arcee/modeling_arcee.py b/src/transformers/models/arcee/modeling_arcee.py index 900a530a34b7..bfc295226f4b 100644 --- a/src/transformers/models/arcee/modeling_arcee.py +++ b/src/transformers/models/arcee/modeling_arcee.py @@ -72,11 +72,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/aria/modeling_aria.py b/src/transformers/models/aria/modeling_aria.py index a9ef9ea7cb28..50821769e22a 100644 --- a/src/transformers/models/aria/modeling_aria.py +++ b/src/transformers/models/aria/modeling_aria.py @@ -54,11 +54,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/bamba/modeling_bamba.py b/src/transformers/models/bamba/modeling_bamba.py index afcce31c7c62..4e24d658d8f6 100644 --- a/src/transformers/models/bamba/modeling_bamba.py +++ b/src/transformers/models/bamba/modeling_bamba.py @@ -1010,11 +1010,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/bitnet/modeling_bitnet.py b/src/transformers/models/bitnet/modeling_bitnet.py index 76532cd10982..93bc9a0d892b 100644 --- a/src/transformers/models/bitnet/modeling_bitnet.py +++ b/src/transformers/models/bitnet/modeling_bitnet.py @@ -51,11 +51,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/blt/modeling_blt.py b/src/transformers/models/blt/modeling_blt.py index d0cf94dc14d4..a7b9171822ad 100644 --- a/src/transformers/models/blt/modeling_blt.py +++ b/src/transformers/models/blt/modeling_blt.py @@ -75,11 +75,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/chameleon/modeling_chameleon.py b/src/transformers/models/chameleon/modeling_chameleon.py index 25307fc90fc6..8b3c4ff3f8c6 100644 --- a/src/transformers/models/chameleon/modeling_chameleon.py +++ b/src/transformers/models/chameleon/modeling_chameleon.py @@ -56,11 +56,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/clvp/modeling_clvp.py b/src/transformers/models/clvp/modeling_clvp.py index 6c4259404697..191713e94aed 100644 --- a/src/transformers/models/clvp/modeling_clvp.py +++ b/src/transformers/models/clvp/modeling_clvp.py @@ -225,11 +225,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/csm/modeling_csm.py b/src/transformers/models/csm/modeling_csm.py index d27a1cd19671..ee3a503bee0b 100644 --- a/src/transformers/models/csm/modeling_csm.py +++ b/src/transformers/models/csm/modeling_csm.py @@ -109,11 +109,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/cwm/modeling_cwm.py b/src/transformers/models/cwm/modeling_cwm.py index 4bfa7842294e..f5ccc73e0ac2 100644 --- a/src/transformers/models/cwm/modeling_cwm.py +++ b/src/transformers/models/cwm/modeling_cwm.py @@ -255,11 +255,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py b/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py index 1132cc5a6cd3..edc6ab6e1960 100644 --- a/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py +++ b/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py @@ -157,11 +157,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py index b878938f4331..fb36a8e5e50f 100644 --- a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py +++ b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py @@ -44,11 +44,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/dia/modeling_dia.py b/src/transformers/models/dia/modeling_dia.py index 2ce385242c74..59cd8b0f49e2 100644 --- a/src/transformers/models/dia/modeling_dia.py +++ b/src/transformers/models/dia/modeling_dia.py @@ -119,11 +119,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/diffllama/modeling_diffllama.py b/src/transformers/models/diffllama/modeling_diffllama.py index 673925f7d819..2e13f63ed75f 100644 --- a/src/transformers/models/diffllama/modeling_diffllama.py +++ b/src/transformers/models/diffllama/modeling_diffllama.py @@ -519,11 +519,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/doge/modeling_doge.py b/src/transformers/models/doge/modeling_doge.py index 74aaf8fc1276..3aa251055079 100644 --- a/src/transformers/models/doge/modeling_doge.py +++ b/src/transformers/models/doge/modeling_doge.py @@ -61,11 +61,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/emu3/modeling_emu3.py b/src/transformers/models/emu3/modeling_emu3.py index 3b3d7513e7ee..959ad4b070ef 100644 --- a/src/transformers/models/emu3/modeling_emu3.py +++ b/src/transformers/models/emu3/modeling_emu3.py @@ -200,11 +200,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ernie4_5/modeling_ernie4_5.py b/src/transformers/models/ernie4_5/modeling_ernie4_5.py index 2a04ca804c49..eb4e0cb831b9 100644 --- a/src/transformers/models/ernie4_5/modeling_ernie4_5.py +++ b/src/transformers/models/ernie4_5/modeling_ernie4_5.py @@ -278,11 +278,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py b/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py index 0134bbe61a5c..659993198fa6 100644 --- a/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py +++ b/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py @@ -52,11 +52,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/evolla/modeling_evolla.py b/src/transformers/models/evolla/modeling_evolla.py index cb06976c5145..84c90be5cc30 100644 --- a/src/transformers/models/evolla/modeling_evolla.py +++ b/src/transformers/models/evolla/modeling_evolla.py @@ -954,11 +954,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/exaone4/modeling_exaone4.py b/src/transformers/models/exaone4/modeling_exaone4.py index 3cc8c7301e7d..95904a5aacd6 100644 --- a/src/transformers/models/exaone4/modeling_exaone4.py +++ b/src/transformers/models/exaone4/modeling_exaone4.py @@ -59,11 +59,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/falcon_h1/modeling_falcon_h1.py b/src/transformers/models/falcon_h1/modeling_falcon_h1.py index a52daaab1352..386270b3717d 100644 --- a/src/transformers/models/falcon_h1/modeling_falcon_h1.py +++ b/src/transformers/models/falcon_h1/modeling_falcon_h1.py @@ -1080,11 +1080,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm/modeling_glm.py b/src/transformers/models/glm/modeling_glm.py index 51072f7a67c6..8a8df636a860 100644 --- a/src/transformers/models/glm/modeling_glm.py +++ b/src/transformers/models/glm/modeling_glm.py @@ -296,11 +296,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4/modeling_glm4.py b/src/transformers/models/glm4/modeling_glm4.py index 3b1261a1ef27..f1831ea0c815 100644 --- a/src/transformers/models/glm4/modeling_glm4.py +++ b/src/transformers/models/glm4/modeling_glm4.py @@ -345,11 +345,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4_moe/modeling_glm4_moe.py b/src/transformers/models/glm4_moe/modeling_glm4_moe.py index d56d08622064..6495fca10bbc 100644 --- a/src/transformers/models/glm4_moe/modeling_glm4_moe.py +++ b/src/transformers/models/glm4_moe/modeling_glm4_moe.py @@ -322,11 +322,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4v/modeling_glm4v.py b/src/transformers/models/glm4v/modeling_glm4v.py index 78c4c9581994..19e34f88e500 100644 --- a/src/transformers/models/glm4v/modeling_glm4v.py +++ b/src/transformers/models/glm4v/modeling_glm4v.py @@ -55,11 +55,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -1402,6 +1398,8 @@ def forward( **kwargs: Unpack[TransformersKwargs], ) -> Union[tuple, Glm4vCausalLMOutputWithPast]: r""" + rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*): + The rope index difference between sequence length and multimodal rope. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored diff --git a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py index 40d80b4e35e3..bc6a39628f54 100644 --- a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py +++ b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py @@ -56,11 +56,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -517,11 +513,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -1665,6 +1657,8 @@ def forward( **kwargs: Unpack[TransformersKwargs], ) -> Union[tuple, Glm4vMoeCausalLMOutputWithPast]: r""" + rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*): + The rope index difference between sequence length and multimodal rope. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index ad4805cb5633..d7b0d0e6e354 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -322,11 +322,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granite/modeling_granite.py b/src/transformers/models/granite/modeling_granite.py index 66f9c38e7832..fddc2cb233e3 100644 --- a/src/transformers/models/granite/modeling_granite.py +++ b/src/transformers/models/granite/modeling_granite.py @@ -198,11 +198,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoe/modeling_granitemoe.py b/src/transformers/models/granitemoe/modeling_granitemoe.py index 7a8b29446115..628fefce65cc 100644 --- a/src/transformers/models/granitemoe/modeling_granitemoe.py +++ b/src/transformers/models/granitemoe/modeling_granitemoe.py @@ -53,11 +53,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py index 0af5a547a2c4..62231f6946ea 100644 --- a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py +++ b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py @@ -1144,11 +1144,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py index b62a7ca99a39..8defb62d1e06 100644 --- a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py +++ b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py @@ -104,11 +104,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py b/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py index 6079a5aeedc3..e414531ab2ed 100644 --- a/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py +++ b/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py @@ -53,11 +53,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py b/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py index 458bd0f5723d..2fd9475a7b53 100644 --- a/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py +++ b/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py @@ -53,11 +53,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/idefics2/modeling_idefics2.py b/src/transformers/models/idefics2/modeling_idefics2.py index 349324ff6743..1975e556d69a 100644 --- a/src/transformers/models/idefics2/modeling_idefics2.py +++ b/src/transformers/models/idefics2/modeling_idefics2.py @@ -531,11 +531,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/idefics3/modeling_idefics3.py b/src/transformers/models/idefics3/modeling_idefics3.py index 2a394b4e6fd7..0e5d728b00dd 100644 --- a/src/transformers/models/idefics3/modeling_idefics3.py +++ b/src/transformers/models/idefics3/modeling_idefics3.py @@ -386,11 +386,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/internvl/modeling_internvl.py b/src/transformers/models/internvl/modeling_internvl.py index e09c8aaf495a..e4026f909f2f 100644 --- a/src/transformers/models/internvl/modeling_internvl.py +++ b/src/transformers/models/internvl/modeling_internvl.py @@ -54,11 +54,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/jamba/modeling_jamba.py b/src/transformers/models/jamba/modeling_jamba.py index 0797dbb31dd0..3d265af7552c 100755 --- a/src/transformers/models/jamba/modeling_jamba.py +++ b/src/transformers/models/jamba/modeling_jamba.py @@ -58,11 +58,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/jetmoe/modeling_jetmoe.py b/src/transformers/models/jetmoe/modeling_jetmoe.py index 2a33f09bc6bd..a9064eccaa79 100644 --- a/src/transformers/models/jetmoe/modeling_jetmoe.py +++ b/src/transformers/models/jetmoe/modeling_jetmoe.py @@ -56,11 +56,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lfm2/modeling_lfm2.py b/src/transformers/models/lfm2/modeling_lfm2.py index 3b9fc6a115c4..4df0fb097bf0 100644 --- a/src/transformers/models/lfm2/modeling_lfm2.py +++ b/src/transformers/models/lfm2/modeling_lfm2.py @@ -56,11 +56,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py b/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py index 539de3c2cd24..2d7fa3af9150 100644 --- a/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py +++ b/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py @@ -57,11 +57,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/llama/modeling_llama.py b/src/transformers/models/llama/modeling_llama.py index 54ea8e0da271..f853336c6a23 100644 --- a/src/transformers/models/llama/modeling_llama.py +++ b/src/transformers/models/llama/modeling_llama.py @@ -60,11 +60,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/longcat_flash/modeling_longcat_flash.py b/src/transformers/models/longcat_flash/modeling_longcat_flash.py index 2246fbc71756..d8850ef795fd 100644 --- a/src/transformers/models/longcat_flash/modeling_longcat_flash.py +++ b/src/transformers/models/longcat_flash/modeling_longcat_flash.py @@ -55,11 +55,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/minimax/modeling_minimax.py b/src/transformers/models/minimax/modeling_minimax.py index a7c535888007..7cee620f282b 100644 --- a/src/transformers/models/minimax/modeling_minimax.py +++ b/src/transformers/models/minimax/modeling_minimax.py @@ -60,11 +60,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mistral/modeling_mistral.py b/src/transformers/models/mistral/modeling_mistral.py index bcb8b007400d..9c6a9705f809 100644 --- a/src/transformers/models/mistral/modeling_mistral.py +++ b/src/transformers/models/mistral/modeling_mistral.py @@ -196,11 +196,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mistral3/modeling_mistral3.py b/src/transformers/models/mistral3/modeling_mistral3.py index 2bfc0e18a2c9..1aa6e4e20068 100644 --- a/src/transformers/models/mistral3/modeling_mistral3.py +++ b/src/transformers/models/mistral3/modeling_mistral3.py @@ -48,11 +48,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mixtral/modeling_mixtral.py b/src/transformers/models/mixtral/modeling_mixtral.py index e41870213a4a..9c0751dbee4a 100644 --- a/src/transformers/models/mixtral/modeling_mixtral.py +++ b/src/transformers/models/mixtral/modeling_mixtral.py @@ -142,11 +142,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mllama/modeling_mllama.py b/src/transformers/models/mllama/modeling_mllama.py index d84e22c4d2be..66d8ad67a6d6 100644 --- a/src/transformers/models/mllama/modeling_mllama.py +++ b/src/transformers/models/mllama/modeling_mllama.py @@ -376,11 +376,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/olmoe/modeling_olmoe.py b/src/transformers/models/olmoe/modeling_olmoe.py index 4e41a75fea5f..ed5f87ab1998 100644 --- a/src/transformers/models/olmoe/modeling_olmoe.py +++ b/src/transformers/models/olmoe/modeling_olmoe.py @@ -50,11 +50,7 @@ def __init__(self, hidden_size, eps=1e-5): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ovis2/modeling_ovis2.py b/src/transformers/models/ovis2/modeling_ovis2.py index 8e77e36f8633..9348bb6c865b 100644 --- a/src/transformers/models/ovis2/modeling_ovis2.py +++ b/src/transformers/models/ovis2/modeling_ovis2.py @@ -102,11 +102,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/phi3/modeling_phi3.py b/src/transformers/models/phi3/modeling_phi3.py index aef959f42720..7388b70f8ee4 100644 --- a/src/transformers/models/phi3/modeling_phi3.py +++ b/src/transformers/models/phi3/modeling_phi3.py @@ -290,11 +290,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py index b37888669ee9..fadfa58fd802 100644 --- a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py @@ -1137,11 +1137,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/phimoe/modeling_phimoe.py b/src/transformers/models/phimoe/modeling_phimoe.py index 60b0ba979eeb..b98904e02878 100644 --- a/src/transformers/models/phimoe/modeling_phimoe.py +++ b/src/transformers/models/phimoe/modeling_phimoe.py @@ -557,11 +557,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/pixtral/modeling_pixtral.py b/src/transformers/models/pixtral/modeling_pixtral.py index 64cf923b11cd..00fba8e5a27c 100644 --- a/src/transformers/models/pixtral/modeling_pixtral.py +++ b/src/transformers/models/pixtral/modeling_pixtral.py @@ -294,11 +294,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py index cc67efff6bd9..4069f1c28355 100644 --- a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py +++ b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py @@ -63,11 +63,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py index 5700813e5dac..b340bf91b8f8 100644 --- a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py +++ b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py @@ -297,11 +297,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py b/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py index 11cd873592b8..73e1ba8a8cd7 100644 --- a/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py +++ b/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py @@ -1407,11 +1407,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -1629,11 +1625,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py b/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py index cd59bd25eabc..42a9bb92e00f 100644 --- a/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py +++ b/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py @@ -55,11 +55,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/seed_oss/modeling_seed_oss.py b/src/transformers/models/seed_oss/modeling_seed_oss.py index c3dd76357e2e..84c42f36f635 100644 --- a/src/transformers/models/seed_oss/modeling_seed_oss.py +++ b/src/transformers/models/seed_oss/modeling_seed_oss.py @@ -55,11 +55,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/smollm3/modeling_smollm3.py b/src/transformers/models/smollm3/modeling_smollm3.py index 2a9107bc67e9..89a5a3910ab8 100644 --- a/src/transformers/models/smollm3/modeling_smollm3.py +++ b/src/transformers/models/smollm3/modeling_smollm3.py @@ -274,11 +274,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/timesfm/modeling_timesfm.py b/src/transformers/models/timesfm/modeling_timesfm.py index 8010de984313..f005f7b8c772 100644 --- a/src/transformers/models/timesfm/modeling_timesfm.py +++ b/src/transformers/models/timesfm/modeling_timesfm.py @@ -127,11 +127,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/zamba/modeling_zamba.py b/src/transformers/models/zamba/modeling_zamba.py index fac9f62ee652..d8d5c16929e1 100644 --- a/src/transformers/models/zamba/modeling_zamba.py +++ b/src/transformers/models/zamba/modeling_zamba.py @@ -72,11 +72,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/zamba2/modeling_zamba2.py b/src/transformers/models/zamba2/modeling_zamba2.py index 5a40187596c6..72765ab349c6 100644 --- a/src/transformers/models/zamba2/modeling_zamba2.py +++ b/src/transformers/models/zamba2/modeling_zamba2.py @@ -91,11 +91,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" From 995f7d8a1f61875d1ed52d08cbf9ac1c28bbebe5 Mon Sep 17 00:00:00 2001 From: mstojkovicTT Date: Wed, 17 Dec 2025 10:40:09 +0000 Subject: [PATCH 2/4] Apply style and consistency fixes --- src/transformers/models/glm4v/modeling_glm4v.py | 2 -- src/transformers/models/glm4v_moe/modeling_glm4v_moe.py | 2 -- src/transformers/models/ministral3/modeling_ministral3.py | 6 +----- .../models/paddleocr_vl/modeling_paddleocr_vl.py | 6 +----- 4 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/transformers/models/glm4v/modeling_glm4v.py b/src/transformers/models/glm4v/modeling_glm4v.py index 19e34f88e500..d8345238b3fa 100644 --- a/src/transformers/models/glm4v/modeling_glm4v.py +++ b/src/transformers/models/glm4v/modeling_glm4v.py @@ -1398,8 +1398,6 @@ def forward( **kwargs: Unpack[TransformersKwargs], ) -> Union[tuple, Glm4vCausalLMOutputWithPast]: r""" - rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*): - The rope index difference between sequence length and multimodal rope. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored diff --git a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py index bc6a39628f54..7c718101f1e6 100644 --- a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py +++ b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py @@ -1657,8 +1657,6 @@ def forward( **kwargs: Unpack[TransformersKwargs], ) -> Union[tuple, Glm4vMoeCausalLMOutputWithPast]: r""" - rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*): - The rope index difference between sequence length and multimodal rope. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored diff --git a/src/transformers/models/ministral3/modeling_ministral3.py b/src/transformers/models/ministral3/modeling_ministral3.py index 871d333f3430..ae062e928906 100644 --- a/src/transformers/models/ministral3/modeling_ministral3.py +++ b/src/transformers/models/ministral3/modeling_ministral3.py @@ -206,11 +206,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py index dbc729375ce2..feee656c68ea 100644 --- a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py @@ -370,11 +370,7 @@ def __init__(self, hidden_size, eps=1e-6): self.variance_epsilon = eps def forward(self, hidden_states): - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + return nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], self.weight, self.variance_epsilon) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" From 72536fb906397fdf779cb1bc27adf305505191f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Gallou=C3=A9dec?= Date: Wed, 25 Nov 2026 14:42:25 +0000 Subject: [PATCH 3/4] Use F.rms_norm in LlamaRMSNorm and regenerate the norms derived from it rms_norm normalizes in one fused kernel; the weight is applied after the cast back to the input dtype, so outputs and dtypes match the previous implementation (autocast runs rms_norm in fp32). Only LlamaRMSNorm is edited, the other 140 norms come from `make fix-repo`. --- .../models/aimv2/modeling_aimv2.py | 8 ++--- .../models/apertus/modeling_apertus.py | 8 ++--- .../models/apertus1p5/modeling_apertus1p5.py | 8 ++--- .../models/arcee/modeling_arcee.py | 8 ++--- src/transformers/models/aria/modeling_aria.py | 8 ++--- src/transformers/models/axk1/modeling_axk1.py | 8 ++--- src/transformers/models/axk2/modeling_axk2.py | 8 ++--- .../bailing_hybrid/modeling_bailing_hybrid.py | 8 ++--- .../models/bamba/modeling_bamba.py | 8 ++--- .../models/bitnet/modeling_bitnet.py | 8 ++--- src/transformers/models/blt/modeling_blt.py | 8 ++--- .../models/chameleon/modeling_chameleon.py | 8 ++--- src/transformers/models/clvp/modeling_clvp.py | 8 ++--- .../cohere2_moe/modeling_cohere2_moe.py | 8 ++--- .../cosmos3_edge/modeling_cosmos3_edge.py | 8 ++--- src/transformers/models/csm/modeling_csm.py | 8 ++--- src/transformers/models/cwm/modeling_cwm.py | 8 ++--- .../deepseek_ocr2/modeling_deepseek_ocr2.py | 16 ++++------ .../deepseek_v2/modeling_deepseek_v2.py | 8 ++--- .../deepseek_v3/modeling_deepseek_v3.py | 8 ++--- .../deepseek_v32/modeling_deepseek_v32.py | 8 ++--- .../models/deimv2/modeling_deimv2.py | 8 ++--- src/transformers/models/dia/modeling_dia.py | 8 ++--- .../models/diffllama/modeling_diffllama.py | 8 ++--- src/transformers/models/doge/modeling_doge.py | 8 ++--- .../models/dots1/modeling_dots1.py | 8 ++--- .../models/dots3_note/modeling_dots3_note.py | 8 ++--- src/transformers/models/emu3/modeling_emu3.py | 8 ++--- .../models/ernie4_5/modeling_ernie4_5.py | 8 ++--- .../ernie4_5_moe/modeling_ernie4_5_moe.py | 8 ++--- .../modeling_ernie4_5_vl_moe.py | 8 ++--- .../models/eurobert/modeling_eurobert.py | 8 ++--- .../models/evolla/modeling_evolla.py | 8 ++--- .../models/exaone4/modeling_exaone4.py | 8 ++--- .../models/exaone4_5/modeling_exaone4_5.py | 8 ++--- .../models/exaone_moe/modeling_exaone_moe.py | 8 ++--- .../models/falcon_h1/modeling_falcon_h1.py | 8 ++--- .../falcon_mamba/modeling_falcon_mamba.py | 8 ++--- src/transformers/models/glm/modeling_glm.py | 8 ++--- src/transformers/models/glm4/modeling_glm4.py | 8 ++--- .../models/glm4_moe/modeling_glm4_moe.py | 8 ++--- .../glm4_moe_lite/modeling_glm4_moe_lite.py | 8 ++--- .../models/glm4v/modeling_glm4v.py | 8 ++--- .../models/glm4v_moe/modeling_glm4v_moe.py | 16 ++++------ .../models/glm5_next/modeling_glm5_next.py | 16 ++++------ .../models/glm_image/modeling_glm_image.py | 8 ++--- .../glm_moe_dsa/modeling_glm_moe_dsa.py | 8 ++--- .../models/glm_ocr/modeling_glm_ocr.py | 8 ++--- .../models/granite/modeling_granite.py | 8 ++--- .../modeling_granite4_vision.py | 8 ++--- .../modeling_granite_docling.py | 8 ++--- .../granite_swa/modeling_granite_swa.py | 8 ++--- .../models/granitemoe/modeling_granitemoe.py | 8 ++--- .../granitemoe_swa/modeling_granitemoe_swa.py | 8 ++--- .../modeling_granitemoehybrid.py | 8 ++--- .../modeling_granitemoeshared.py | 8 ++--- .../higgs_audio_v2/modeling_higgs_audio_v2.py | 8 ++--- .../modeling_hunyuan_v1_dense.py | 8 ++--- .../hunyuan_v1_moe/modeling_hunyuan_v1_moe.py | 8 ++--- .../models/hunyuan_vl/modeling_hunyuan_vl.py | 8 ++--- .../models/hy_v3/modeling_hy_v3.py | 8 ++--- .../models/hy_v4/modeling_hy_v4.py | 8 ++--- .../models/hy_v4_vl/modeling_hy_v4_vl.py | 8 ++--- .../hyperclovax/modeling_hyperclovax.py | 8 ++--- .../models/idefics2/modeling_idefics2.py | 8 ++--- .../models/idefics3/modeling_idefics3.py | 8 ++--- .../models/inkling/modeling_inkling.py | 8 ++--- .../models/internvl/modeling_internvl.py | 8 ++--- .../models/jamba/modeling_jamba.py | 8 ++--- .../models/jetmoe/modeling_jetmoe.py | 8 ++--- .../kimi_linear/modeling_kimi_linear.py | 8 ++--- .../models/kolibri1/modeling_kolibri1.py | 8 ++--- .../models/kolibri1_1/modeling_kolibri1_1.py | 8 ++--- .../models/laguna/modeling_laguna.py | 8 ++--- src/transformers/models/lfm2/modeling_lfm2.py | 8 ++--- .../models/lfm2_moe/modeling_lfm2_moe.py | 8 ++--- src/transformers/models/lfm3/modeling_lfm3.py | 8 ++--- .../models/lfm3_vl/modeling_lfm3_vl.py | 8 ++--- .../lighton_ocr/modeling_lighton_ocr.py | 8 ++--- .../models/llama/modeling_llama.py | 8 ++--- .../longcat_flash/modeling_longcat_flash.py | 8 ++--- .../models/mellum/modeling_mellum.py | 8 ++--- .../mimo_v2_flash/modeling_mimo_v2_flash.py | 8 ++--- .../models/minicpm3/modeling_minicpm3.py | 8 ++--- .../models/minimax/modeling_minimax.py | 8 ++--- .../models/minimax_m2/modeling_minimax_m2.py | 8 ++--- .../models/ministral/modeling_ministral.py | 8 ++--- .../models/ministral3/modeling_ministral3.py | 8 ++--- .../models/mistral/modeling_mistral.py | 8 ++--- .../models/mistral3/modeling_mistral3.py | 8 ++--- .../models/mistral4/modeling_mistral4.py | 8 ++--- .../mistral_large4/modeling_mistral_large4.py | 8 ++--- .../models/mixtral/modeling_mixtral.py | 8 ++--- .../models/mllama/modeling_mllama.py | 8 ++--- .../models/molmo2/modeling_molmo2.py | 8 ++--- .../modeling_muse_glimmer_assistant.py | 8 ++--- .../models/neucodec/modeling_neucodec.py | 8 ++--- .../models/olmoe/modeling_olmoe.py | 8 ++--- .../models/ovis2/modeling_ovis2.py | 8 ++--- .../paddleocr_vl/modeling_paddleocr_vl.py | 8 ++--- .../models/pe_audio/modeling_pe_audio.py | 8 ++--- .../pe_audio_video/modeling_pe_audio_video.py | 8 ++--- .../models/pe_video/modeling_pe_video.py | 8 ++--- src/transformers/models/phi3/modeling_phi3.py | 8 ++--- .../modeling_phi4_multimodal.py | 8 ++--- .../models/pixtral/modeling_pixtral.py | 8 ++--- .../qianfan_ocr/modeling_qianfan_ocr.py | 8 ++--- .../models/qwen2/modeling_qwen2.py | 8 ++--- .../qwen2_5_omni/modeling_qwen2_5_omni.py | 8 ++--- .../models/qwen2_5_vl/modeling_qwen2_5_vl.py | 8 ++--- .../models/qwen2_moe/modeling_qwen2_moe.py | 8 ++--- .../models/qwen2_vl/modeling_qwen2_vl.py | 8 ++--- .../models/qwen3/modeling_qwen3.py | 8 ++--- .../models/qwen3_moe/modeling_qwen3_moe.py | 8 ++--- .../qwen3_omni_moe/modeling_qwen3_omni_moe.py | 32 +++++++------------ .../models/qwen3_vl/modeling_qwen3_vl.py | 8 ++--- .../qwen3_vl_moe/modeling_qwen3_vl_moe.py | 8 ++--- .../models/sapiens2/modeling_sapiens2.py | 8 ++--- .../models/seed_oss/modeling_seed_oss.py | 8 ++--- .../models/smollm3/modeling_smollm3.py | 8 ++--- .../models/solar_open/modeling_solar_open.py | 8 ++--- .../models/timesfm/modeling_timesfm.py | 8 ++--- .../models/timesfm2_5/modeling_timesfm2_5.py | 8 ++--- .../models/ultrabert/modeling_ultrabert.py | 8 ++--- .../models/ultravbert/modeling_ultravbert.py | 8 ++--- .../unlimited_ocr/modeling_unlimited_ocr.py | 8 ++--- .../models/vibevoice/modeling_vibevoice.py | 8 ++--- .../modeling_vibevoice_acoustic_tokenizer.py | 8 ++--- .../vibevoice_asr/modeling_vibevoice_asr.py | 8 ++--- .../modeling_voxtral_realtime.py | 8 ++--- .../models/xcodec2/modeling_xcodec2.py | 8 ++--- .../models/youtu/modeling_youtu.py | 8 ++--- .../models/zamba/modeling_zamba.py | 8 ++--- .../models/zamba2/modeling_zamba2.py | 8 ++--- src/transformers/models/zaya/modeling_zaya.py | 8 ++--- 135 files changed, 423 insertions(+), 705 deletions(-) diff --git a/src/transformers/models/aimv2/modeling_aimv2.py b/src/transformers/models/aimv2/modeling_aimv2.py index 7a7463152f41..7b9a3746deaa 100644 --- a/src/transformers/models/aimv2/modeling_aimv2.py +++ b/src/transformers/models/aimv2/modeling_aimv2.py @@ -87,11 +87,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/apertus/modeling_apertus.py b/src/transformers/models/apertus/modeling_apertus.py index 7a312e665188..7335f17b3869 100644 --- a/src/transformers/models/apertus/modeling_apertus.py +++ b/src/transformers/models/apertus/modeling_apertus.py @@ -67,11 +67,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/apertus1p5/modeling_apertus1p5.py b/src/transformers/models/apertus1p5/modeling_apertus1p5.py index 3fb5c2cb9c36..391c0beae8c4 100644 --- a/src/transformers/models/apertus1p5/modeling_apertus1p5.py +++ b/src/transformers/models/apertus1p5/modeling_apertus1p5.py @@ -385,11 +385,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/arcee/modeling_arcee.py b/src/transformers/models/arcee/modeling_arcee.py index 68ed2a42e45e..c637b7d33498 100644 --- a/src/transformers/models/arcee/modeling_arcee.py +++ b/src/transformers/models/arcee/modeling_arcee.py @@ -72,11 +72,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/aria/modeling_aria.py b/src/transformers/models/aria/modeling_aria.py index dcbd93466cc0..9c631560114f 100644 --- a/src/transformers/models/aria/modeling_aria.py +++ b/src/transformers/models/aria/modeling_aria.py @@ -63,11 +63,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/axk1/modeling_axk1.py b/src/transformers/models/axk1/modeling_axk1.py index c63160b4d716..e16ef971d180 100644 --- a/src/transformers/models/axk1/modeling_axk1.py +++ b/src/transformers/models/axk1/modeling_axk1.py @@ -58,11 +58,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/axk2/modeling_axk2.py b/src/transformers/models/axk2/modeling_axk2.py index ef50eb7dd597..66db8f6a2b21 100644 --- a/src/transformers/models/axk2/modeling_axk2.py +++ b/src/transformers/models/axk2/modeling_axk2.py @@ -58,11 +58,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py b/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py index fc82a368f813..fa9ec108085f 100644 --- a/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py +++ b/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py @@ -62,11 +62,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/bamba/modeling_bamba.py b/src/transformers/models/bamba/modeling_bamba.py index b6fc892a9d12..8ca724810c35 100644 --- a/src/transformers/models/bamba/modeling_bamba.py +++ b/src/transformers/models/bamba/modeling_bamba.py @@ -829,11 +829,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/bitnet/modeling_bitnet.py b/src/transformers/models/bitnet/modeling_bitnet.py index 673c448e5f47..9d1478eb5eff 100644 --- a/src/transformers/models/bitnet/modeling_bitnet.py +++ b/src/transformers/models/bitnet/modeling_bitnet.py @@ -51,11 +51,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/blt/modeling_blt.py b/src/transformers/models/blt/modeling_blt.py index 5b1af09be5ce..b7d72696b62a 100644 --- a/src/transformers/models/blt/modeling_blt.py +++ b/src/transformers/models/blt/modeling_blt.py @@ -76,11 +76,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/chameleon/modeling_chameleon.py b/src/transformers/models/chameleon/modeling_chameleon.py index 1da03863a09d..de3ed082c826 100644 --- a/src/transformers/models/chameleon/modeling_chameleon.py +++ b/src/transformers/models/chameleon/modeling_chameleon.py @@ -75,11 +75,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/clvp/modeling_clvp.py b/src/transformers/models/clvp/modeling_clvp.py index 350bab6de278..8bd5e1994626 100644 --- a/src/transformers/models/clvp/modeling_clvp.py +++ b/src/transformers/models/clvp/modeling_clvp.py @@ -229,11 +229,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py b/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py index 44dae2690458..2f2cec3a75d0 100644 --- a/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py +++ b/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py @@ -55,11 +55,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py b/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py index aec88deb809a..ba5c02bfef5d 100644 --- a/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py +++ b/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py @@ -285,11 +285,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/csm/modeling_csm.py b/src/transformers/models/csm/modeling_csm.py index 45c9dca06d21..ada81e07c849 100644 --- a/src/transformers/models/csm/modeling_csm.py +++ b/src/transformers/models/csm/modeling_csm.py @@ -107,11 +107,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/cwm/modeling_cwm.py b/src/transformers/models/cwm/modeling_cwm.py index f90c7242434b..e6eb4222221d 100644 --- a/src/transformers/models/cwm/modeling_cwm.py +++ b/src/transformers/models/cwm/modeling_cwm.py @@ -232,11 +232,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py b/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py index c69d0752ae95..d69d9d5fef14 100644 --- a/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py +++ b/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py @@ -624,11 +624,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -1201,11 +1199,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py b/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py index f1c865a54ad3..e63b79980b35 100644 --- a/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py +++ b/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py @@ -163,11 +163,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py index 5bce14fc9cbf..87d6f5ce4f9a 100644 --- a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py +++ b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py @@ -44,11 +44,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py b/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py index 991f322670a0..e63092b9f7de 100644 --- a/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py +++ b/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/deimv2/modeling_deimv2.py b/src/transformers/models/deimv2/modeling_deimv2.py index 17db7066aa1a..b0289409eece 100644 --- a/src/transformers/models/deimv2/modeling_deimv2.py +++ b/src/transformers/models/deimv2/modeling_deimv2.py @@ -168,11 +168,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/dia/modeling_dia.py b/src/transformers/models/dia/modeling_dia.py index 948b568ff9ed..58f0be9ba4a4 100644 --- a/src/transformers/models/dia/modeling_dia.py +++ b/src/transformers/models/dia/modeling_dia.py @@ -128,11 +128,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/diffllama/modeling_diffllama.py b/src/transformers/models/diffllama/modeling_diffllama.py index e9433d46976f..af7097e0009f 100644 --- a/src/transformers/models/diffllama/modeling_diffllama.py +++ b/src/transformers/models/diffllama/modeling_diffllama.py @@ -323,11 +323,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/doge/modeling_doge.py b/src/transformers/models/doge/modeling_doge.py index 743defee89d4..cc9027d54090 100644 --- a/src/transformers/models/doge/modeling_doge.py +++ b/src/transformers/models/doge/modeling_doge.py @@ -62,11 +62,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/dots1/modeling_dots1.py b/src/transformers/models/dots1/modeling_dots1.py index b57c1b74e712..401f08cb186d 100644 --- a/src/transformers/models/dots1/modeling_dots1.py +++ b/src/transformers/models/dots1/modeling_dots1.py @@ -53,11 +53,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/dots3_note/modeling_dots3_note.py b/src/transformers/models/dots3_note/modeling_dots3_note.py index b28d78054b26..f44d08e5be3d 100644 --- a/src/transformers/models/dots3_note/modeling_dots3_note.py +++ b/src/transformers/models/dots3_note/modeling_dots3_note.py @@ -67,11 +67,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/emu3/modeling_emu3.py b/src/transformers/models/emu3/modeling_emu3.py index 16daac5a3881..96dabc804b78 100644 --- a/src/transformers/models/emu3/modeling_emu3.py +++ b/src/transformers/models/emu3/modeling_emu3.py @@ -207,11 +207,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ernie4_5/modeling_ernie4_5.py b/src/transformers/models/ernie4_5/modeling_ernie4_5.py index c6029f4b4cc3..baf240126a90 100644 --- a/src/transformers/models/ernie4_5/modeling_ernie4_5.py +++ b/src/transformers/models/ernie4_5/modeling_ernie4_5.py @@ -254,11 +254,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py b/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py index 93ef417904de..68662655dd21 100644 --- a/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py +++ b/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py @@ -53,11 +53,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py index 7953a9cd24fc..fab3d379cfea 100644 --- a/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py @@ -276,11 +276,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/eurobert/modeling_eurobert.py b/src/transformers/models/eurobert/modeling_eurobert.py index 86812b602e7c..8f25cb65c78d 100644 --- a/src/transformers/models/eurobert/modeling_eurobert.py +++ b/src/transformers/models/eurobert/modeling_eurobert.py @@ -52,11 +52,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/evolla/modeling_evolla.py b/src/transformers/models/evolla/modeling_evolla.py index 4bfc2d775c66..19d393ed8eb7 100644 --- a/src/transformers/models/evolla/modeling_evolla.py +++ b/src/transformers/models/evolla/modeling_evolla.py @@ -1008,11 +1008,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/exaone4/modeling_exaone4.py b/src/transformers/models/exaone4/modeling_exaone4.py index 7067a8f878ae..f34011fac339 100644 --- a/src/transformers/models/exaone4/modeling_exaone4.py +++ b/src/transformers/models/exaone4/modeling_exaone4.py @@ -57,11 +57,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/exaone4_5/modeling_exaone4_5.py b/src/transformers/models/exaone4_5/modeling_exaone4_5.py index 264cce8fbeb6..892396851e03 100644 --- a/src/transformers/models/exaone4_5/modeling_exaone4_5.py +++ b/src/transformers/models/exaone4_5/modeling_exaone4_5.py @@ -147,11 +147,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/exaone_moe/modeling_exaone_moe.py b/src/transformers/models/exaone_moe/modeling_exaone_moe.py index 3e4ef65572d6..e64df52d8b29 100644 --- a/src/transformers/models/exaone_moe/modeling_exaone_moe.py +++ b/src/transformers/models/exaone_moe/modeling_exaone_moe.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/falcon_h1/modeling_falcon_h1.py b/src/transformers/models/falcon_h1/modeling_falcon_h1.py index a7247d025bf0..d6470eafcdba 100644 --- a/src/transformers/models/falcon_h1/modeling_falcon_h1.py +++ b/src/transformers/models/falcon_h1/modeling_falcon_h1.py @@ -854,11 +854,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py b/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py index 764d69bc7758..f840a3dd77f4 100644 --- a/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py +++ b/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py @@ -484,11 +484,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm/modeling_glm.py b/src/transformers/models/glm/modeling_glm.py index 6a8dfc3c3f74..0902a0ded4f9 100644 --- a/src/transformers/models/glm/modeling_glm.py +++ b/src/transformers/models/glm/modeling_glm.py @@ -272,11 +272,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4/modeling_glm4.py b/src/transformers/models/glm4/modeling_glm4.py index de7d3cc0226e..6b68a5da250c 100644 --- a/src/transformers/models/glm4/modeling_glm4.py +++ b/src/transformers/models/glm4/modeling_glm4.py @@ -320,11 +320,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4_moe/modeling_glm4_moe.py b/src/transformers/models/glm4_moe/modeling_glm4_moe.py index 5b7a310b786d..eae6402fece5 100644 --- a/src/transformers/models/glm4_moe/modeling_glm4_moe.py +++ b/src/transformers/models/glm4_moe/modeling_glm4_moe.py @@ -318,11 +318,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py b/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py index 445f1f4f1985..6c28fcb0dbf6 100644 --- a/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py +++ b/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py @@ -408,11 +408,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4v/modeling_glm4v.py b/src/transformers/models/glm4v/modeling_glm4v.py index 4f7fd5e6b49a..8f04ff8cba0f 100644 --- a/src/transformers/models/glm4v/modeling_glm4v.py +++ b/src/transformers/models/glm4v/modeling_glm4v.py @@ -66,11 +66,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py index 6275d46ea6b4..db632a8ed1aa 100644 --- a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py +++ b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py @@ -400,11 +400,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -556,11 +554,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm5_next/modeling_glm5_next.py b/src/transformers/models/glm5_next/modeling_glm5_next.py index 2923bb7fd609..87645eb3879a 100644 --- a/src/transformers/models/glm5_next/modeling_glm5_next.py +++ b/src/transformers/models/glm5_next/modeling_glm5_next.py @@ -74,11 +74,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -1576,11 +1574,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm_image/modeling_glm_image.py b/src/transformers/models/glm_image/modeling_glm_image.py index b71b13de3276..bdbce429400a 100644 --- a/src/transformers/models/glm_image/modeling_glm_image.py +++ b/src/transformers/models/glm_image/modeling_glm_image.py @@ -681,11 +681,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py b/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py index 4b546f04afa1..81a26f0a1b9d 100644 --- a/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py +++ b/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/glm_ocr/modeling_glm_ocr.py b/src/transformers/models/glm_ocr/modeling_glm_ocr.py index 1b32a0edb4d2..2f9ecad4aed0 100644 --- a/src/transformers/models/glm_ocr/modeling_glm_ocr.py +++ b/src/transformers/models/glm_ocr/modeling_glm_ocr.py @@ -66,11 +66,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granite/modeling_granite.py b/src/transformers/models/granite/modeling_granite.py index 0e5a6401bc05..9846328c7786 100644 --- a/src/transformers/models/granite/modeling_granite.py +++ b/src/transformers/models/granite/modeling_granite.py @@ -191,11 +191,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granite4_vision/modeling_granite4_vision.py b/src/transformers/models/granite4_vision/modeling_granite4_vision.py index cb083c230462..42888c2e669a 100644 --- a/src/transformers/models/granite4_vision/modeling_granite4_vision.py +++ b/src/transformers/models/granite4_vision/modeling_granite4_vision.py @@ -414,11 +414,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granite_docling/modeling_granite_docling.py b/src/transformers/models/granite_docling/modeling_granite_docling.py index 6036032b8498..c6e54e2c6286 100644 --- a/src/transformers/models/granite_docling/modeling_granite_docling.py +++ b/src/transformers/models/granite_docling/modeling_granite_docling.py @@ -249,11 +249,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granite_swa/modeling_granite_swa.py b/src/transformers/models/granite_swa/modeling_granite_swa.py index 34361d630356..51a82b536dc4 100644 --- a/src/transformers/models/granite_swa/modeling_granite_swa.py +++ b/src/transformers/models/granite_swa/modeling_granite_swa.py @@ -208,11 +208,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoe/modeling_granitemoe.py b/src/transformers/models/granitemoe/modeling_granitemoe.py index 73db2e1bfba2..9780196f7977 100644 --- a/src/transformers/models/granitemoe/modeling_granitemoe.py +++ b/src/transformers/models/granitemoe/modeling_granitemoe.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py b/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py index 9e62b4cc2bbf..70841ddbf451 100644 --- a/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py +++ b/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py @@ -338,11 +338,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py index ec97a978b444..d236fad2cb4b 100644 --- a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py +++ b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py @@ -930,11 +930,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py index 44310c7d507c..7a8ca8e6f90e 100644 --- a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py +++ b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py @@ -104,11 +104,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py b/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py index 886d214cc5df..e34353bf1af3 100644 --- a/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py +++ b/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py @@ -71,11 +71,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py b/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py index d40a3203f373..31de4454efa4 100644 --- a/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py +++ b/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py @@ -53,11 +53,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py b/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py index f00bb81965c2..27be7472ffaa 100644 --- a/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py +++ b/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py @@ -53,11 +53,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py b/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py index e18b96457198..9721b265b02f 100644 --- a/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py +++ b/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py @@ -92,11 +92,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hy_v3/modeling_hy_v3.py b/src/transformers/models/hy_v3/modeling_hy_v3.py index 45db000ed022..b268096340c1 100644 --- a/src/transformers/models/hy_v3/modeling_hy_v3.py +++ b/src/transformers/models/hy_v3/modeling_hy_v3.py @@ -53,11 +53,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hy_v4/modeling_hy_v4.py b/src/transformers/models/hy_v4/modeling_hy_v4.py index 9ab4ca154e3a..2f7d9146b61c 100644 --- a/src/transformers/models/hy_v4/modeling_hy_v4.py +++ b/src/transformers/models/hy_v4/modeling_hy_v4.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py b/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py index 23e131bd5dcd..fc33a6c1bc36 100644 --- a/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py +++ b/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py @@ -742,11 +742,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/hyperclovax/modeling_hyperclovax.py b/src/transformers/models/hyperclovax/modeling_hyperclovax.py index ecdad829c700..c8b297b1cb61 100644 --- a/src/transformers/models/hyperclovax/modeling_hyperclovax.py +++ b/src/transformers/models/hyperclovax/modeling_hyperclovax.py @@ -51,11 +51,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/idefics2/modeling_idefics2.py b/src/transformers/models/idefics2/modeling_idefics2.py index 995d4b698c1b..08d3807155fb 100644 --- a/src/transformers/models/idefics2/modeling_idefics2.py +++ b/src/transformers/models/idefics2/modeling_idefics2.py @@ -487,11 +487,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/idefics3/modeling_idefics3.py b/src/transformers/models/idefics3/modeling_idefics3.py index 9a19547c88e9..7617dbddf804 100644 --- a/src/transformers/models/idefics3/modeling_idefics3.py +++ b/src/transformers/models/idefics3/modeling_idefics3.py @@ -349,11 +349,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/inkling/modeling_inkling.py b/src/transformers/models/inkling/modeling_inkling.py index cb6e98ea3f6d..1c535701cc28 100644 --- a/src/transformers/models/inkling/modeling_inkling.py +++ b/src/transformers/models/inkling/modeling_inkling.py @@ -97,11 +97,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/internvl/modeling_internvl.py b/src/transformers/models/internvl/modeling_internvl.py index 19c555e154d8..4bcd9f2b2575 100644 --- a/src/transformers/models/internvl/modeling_internvl.py +++ b/src/transformers/models/internvl/modeling_internvl.py @@ -62,11 +62,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/jamba/modeling_jamba.py b/src/transformers/models/jamba/modeling_jamba.py index 702dd23c870f..561bc552b80c 100755 --- a/src/transformers/models/jamba/modeling_jamba.py +++ b/src/transformers/models/jamba/modeling_jamba.py @@ -71,11 +71,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/jetmoe/modeling_jetmoe.py b/src/transformers/models/jetmoe/modeling_jetmoe.py index 5cc9689b56e7..18e8ee3833f4 100644 --- a/src/transformers/models/jetmoe/modeling_jetmoe.py +++ b/src/transformers/models/jetmoe/modeling_jetmoe.py @@ -56,11 +56,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/kimi_linear/modeling_kimi_linear.py b/src/transformers/models/kimi_linear/modeling_kimi_linear.py index e8ca8f25cddd..435ee53ed956 100644 --- a/src/transformers/models/kimi_linear/modeling_kimi_linear.py +++ b/src/transformers/models/kimi_linear/modeling_kimi_linear.py @@ -61,11 +61,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/kolibri1/modeling_kolibri1.py b/src/transformers/models/kolibri1/modeling_kolibri1.py index f5a2d2678e18..09f370d0379b 100644 --- a/src/transformers/models/kolibri1/modeling_kolibri1.py +++ b/src/transformers/models/kolibri1/modeling_kolibri1.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py b/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py index c4e18c2e9260..45609025cbbf 100644 --- a/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py +++ b/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py @@ -380,11 +380,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/laguna/modeling_laguna.py b/src/transformers/models/laguna/modeling_laguna.py index 3b8c8f8844ad..6f56d4605278 100644 --- a/src/transformers/models/laguna/modeling_laguna.py +++ b/src/transformers/models/laguna/modeling_laguna.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lfm2/modeling_lfm2.py b/src/transformers/models/lfm2/modeling_lfm2.py index d8764c2d5e07..c90c7a028504 100644 --- a/src/transformers/models/lfm2/modeling_lfm2.py +++ b/src/transformers/models/lfm2/modeling_lfm2.py @@ -52,11 +52,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py b/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py index de5341b4a432..052d555240ce 100644 --- a/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py +++ b/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py @@ -59,11 +59,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lfm3/modeling_lfm3.py b/src/transformers/models/lfm3/modeling_lfm3.py index 43b5a52db4a3..e51b32e8fa27 100644 --- a/src/transformers/models/lfm3/modeling_lfm3.py +++ b/src/transformers/models/lfm3/modeling_lfm3.py @@ -53,11 +53,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py b/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py index e7a9575d1b9a..29b37778ba4f 100644 --- a/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py +++ b/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py @@ -59,11 +59,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py b/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py index 3fbb5431de08..b4d01f808486 100644 --- a/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py +++ b/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/llama/modeling_llama.py b/src/transformers/models/llama/modeling_llama.py index 6f57f4fda612..cba98a6cd0c0 100644 --- a/src/transformers/models/llama/modeling_llama.py +++ b/src/transformers/models/llama/modeling_llama.py @@ -60,11 +60,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/longcat_flash/modeling_longcat_flash.py b/src/transformers/models/longcat_flash/modeling_longcat_flash.py index 60a191181d22..46c7942d8c4d 100644 --- a/src/transformers/models/longcat_flash/modeling_longcat_flash.py +++ b/src/transformers/models/longcat_flash/modeling_longcat_flash.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mellum/modeling_mellum.py b/src/transformers/models/mellum/modeling_mellum.py index d5a3795ea962..c2709e0e4cca 100644 --- a/src/transformers/models/mellum/modeling_mellum.py +++ b/src/transformers/models/mellum/modeling_mellum.py @@ -118,11 +118,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py b/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py index d35360572d78..bfaa5acae979 100644 --- a/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py +++ b/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py @@ -54,11 +54,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/minicpm3/modeling_minicpm3.py b/src/transformers/models/minicpm3/modeling_minicpm3.py index 3a369d923401..0c4edd90599b 100644 --- a/src/transformers/models/minicpm3/modeling_minicpm3.py +++ b/src/transformers/models/minicpm3/modeling_minicpm3.py @@ -67,11 +67,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/minimax/modeling_minimax.py b/src/transformers/models/minimax/modeling_minimax.py index 07e45800885d..8fbff38ff3fb 100644 --- a/src/transformers/models/minimax/modeling_minimax.py +++ b/src/transformers/models/minimax/modeling_minimax.py @@ -60,11 +60,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/minimax_m2/modeling_minimax_m2.py b/src/transformers/models/minimax_m2/modeling_minimax_m2.py index af8138aac224..696ed8c1987d 100644 --- a/src/transformers/models/minimax_m2/modeling_minimax_m2.py +++ b/src/transformers/models/minimax_m2/modeling_minimax_m2.py @@ -135,11 +135,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ministral/modeling_ministral.py b/src/transformers/models/ministral/modeling_ministral.py index 69dfdd72b116..2dd92c5fc312 100644 --- a/src/transformers/models/ministral/modeling_ministral.py +++ b/src/transformers/models/ministral/modeling_ministral.py @@ -208,11 +208,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ministral3/modeling_ministral3.py b/src/transformers/models/ministral3/modeling_ministral3.py index 36dbf7076302..8280d1434dee 100644 --- a/src/transformers/models/ministral3/modeling_ministral3.py +++ b/src/transformers/models/ministral3/modeling_ministral3.py @@ -202,11 +202,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mistral/modeling_mistral.py b/src/transformers/models/mistral/modeling_mistral.py index e519b1d72d3f..bbc2807c2448 100644 --- a/src/transformers/models/mistral/modeling_mistral.py +++ b/src/transformers/models/mistral/modeling_mistral.py @@ -190,11 +190,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mistral3/modeling_mistral3.py b/src/transformers/models/mistral3/modeling_mistral3.py index df8d6baf9f31..8111bccceea3 100644 --- a/src/transformers/models/mistral3/modeling_mistral3.py +++ b/src/transformers/models/mistral3/modeling_mistral3.py @@ -57,11 +57,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mistral4/modeling_mistral4.py b/src/transformers/models/mistral4/modeling_mistral4.py index e94bb470c8fd..8e31325eb207 100644 --- a/src/transformers/models/mistral4/modeling_mistral4.py +++ b/src/transformers/models/mistral4/modeling_mistral4.py @@ -57,11 +57,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mistral_large4/modeling_mistral_large4.py b/src/transformers/models/mistral_large4/modeling_mistral_large4.py index 96340aad3936..d98c15067836 100644 --- a/src/transformers/models/mistral_large4/modeling_mistral_large4.py +++ b/src/transformers/models/mistral_large4/modeling_mistral_large4.py @@ -64,11 +64,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mixtral/modeling_mixtral.py b/src/transformers/models/mixtral/modeling_mixtral.py index cacadae03c3e..7c8412ef10a0 100644 --- a/src/transformers/models/mixtral/modeling_mixtral.py +++ b/src/transformers/models/mixtral/modeling_mixtral.py @@ -141,11 +141,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/mllama/modeling_mllama.py b/src/transformers/models/mllama/modeling_mllama.py index 23b3d2f6b5f6..c8692ef7337a 100644 --- a/src/transformers/models/mllama/modeling_mllama.py +++ b/src/transformers/models/mllama/modeling_mllama.py @@ -387,11 +387,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/molmo2/modeling_molmo2.py b/src/transformers/models/molmo2/modeling_molmo2.py index 34f75e00c3ab..90ab3e8c06a2 100644 --- a/src/transformers/models/molmo2/modeling_molmo2.py +++ b/src/transformers/models/molmo2/modeling_molmo2.py @@ -141,11 +141,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py b/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py index c08c66ee8607..2dc089fa8c85 100644 --- a/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py +++ b/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py @@ -49,11 +49,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/neucodec/modeling_neucodec.py b/src/transformers/models/neucodec/modeling_neucodec.py index 06517ae59417..f395b24a1f4f 100644 --- a/src/transformers/models/neucodec/modeling_neucodec.py +++ b/src/transformers/models/neucodec/modeling_neucodec.py @@ -269,11 +269,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/olmoe/modeling_olmoe.py b/src/transformers/models/olmoe/modeling_olmoe.py index eca7e4610943..79fb04c79d82 100644 --- a/src/transformers/models/olmoe/modeling_olmoe.py +++ b/src/transformers/models/olmoe/modeling_olmoe.py @@ -51,11 +51,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ovis2/modeling_ovis2.py b/src/transformers/models/ovis2/modeling_ovis2.py index 79ce48aeff1e..709de85bdc93 100644 --- a/src/transformers/models/ovis2/modeling_ovis2.py +++ b/src/transformers/models/ovis2/modeling_ovis2.py @@ -96,11 +96,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py index 1311f26c0283..10d117558096 100644 --- a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py @@ -402,11 +402,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/pe_audio/modeling_pe_audio.py b/src/transformers/models/pe_audio/modeling_pe_audio.py index 2d1dc17aec20..420483d3b042 100644 --- a/src/transformers/models/pe_audio/modeling_pe_audio.py +++ b/src/transformers/models/pe_audio/modeling_pe_audio.py @@ -351,11 +351,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py b/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py index cf85f1a2812b..a9bfb7689504 100644 --- a/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py +++ b/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py @@ -440,11 +440,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/pe_video/modeling_pe_video.py b/src/transformers/models/pe_video/modeling_pe_video.py index fd1c7622e84d..43cc810186bd 100644 --- a/src/transformers/models/pe_video/modeling_pe_video.py +++ b/src/transformers/models/pe_video/modeling_pe_video.py @@ -275,11 +275,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/phi3/modeling_phi3.py b/src/transformers/models/phi3/modeling_phi3.py index 1165fc7027f0..0ad71be3074f 100644 --- a/src/transformers/models/phi3/modeling_phi3.py +++ b/src/transformers/models/phi3/modeling_phi3.py @@ -264,11 +264,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py index ef79c38c38dc..1d44201e8bfe 100644 --- a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py @@ -1112,11 +1112,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/pixtral/modeling_pixtral.py b/src/transformers/models/pixtral/modeling_pixtral.py index c7148f5cca9f..ba73782722ed 100644 --- a/src/transformers/models/pixtral/modeling_pixtral.py +++ b/src/transformers/models/pixtral/modeling_pixtral.py @@ -248,11 +248,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py b/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py index 17c84560939d..7aa5f56bc7e4 100644 --- a/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py +++ b/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py @@ -84,11 +84,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen2/modeling_qwen2.py b/src/transformers/models/qwen2/modeling_qwen2.py index aa7b5f32808a..78d0ba6d4248 100644 --- a/src/transformers/models/qwen2/modeling_qwen2.py +++ b/src/transformers/models/qwen2/modeling_qwen2.py @@ -238,11 +238,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py b/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py index e3a164fc8a1d..8e17e091ef06 100644 --- a/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py +++ b/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py @@ -1093,11 +1093,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py b/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py index c16e7b77722a..b61e1e29d9a4 100644 --- a/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +++ b/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py @@ -72,11 +72,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py index 8c68e0047065..0563bb7ba686 100644 --- a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py +++ b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py @@ -63,11 +63,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py b/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py index c2a200d58501..d8a7ca9c6c75 100644 --- a/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py @@ -75,11 +75,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3/modeling_qwen3.py b/src/transformers/models/qwen3/modeling_qwen3.py index a567dfb9ec21..e282051847ce 100644 --- a/src/transformers/models/qwen3/modeling_qwen3.py +++ b/src/transformers/models/qwen3/modeling_qwen3.py @@ -57,11 +57,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py index bb2e000a835a..9dc90651c73a 100644 --- a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py +++ b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py @@ -293,11 +293,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py b/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py index 58f87dac2e8b..00457a65a9bc 100644 --- a/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py +++ b/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py @@ -1388,11 +1388,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -1603,11 +1601,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -2418,11 +2414,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" @@ -3617,11 +3611,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py b/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py index 05eac64d8c8c..aaf46d1018ff 100644 --- a/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py +++ b/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py @@ -430,11 +430,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py b/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py index cd48863500a1..013618084f5e 100644 --- a/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py +++ b/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py @@ -100,11 +100,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/sapiens2/modeling_sapiens2.py b/src/transformers/models/sapiens2/modeling_sapiens2.py index 42fd1451be37..58471639a58d 100644 --- a/src/transformers/models/sapiens2/modeling_sapiens2.py +++ b/src/transformers/models/sapiens2/modeling_sapiens2.py @@ -338,11 +338,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/seed_oss/modeling_seed_oss.py b/src/transformers/models/seed_oss/modeling_seed_oss.py index c0a48754ed9f..acf62efa68a8 100644 --- a/src/transformers/models/seed_oss/modeling_seed_oss.py +++ b/src/transformers/models/seed_oss/modeling_seed_oss.py @@ -56,11 +56,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/smollm3/modeling_smollm3.py b/src/transformers/models/smollm3/modeling_smollm3.py index 242fbd05828d..c0943e7a5daf 100644 --- a/src/transformers/models/smollm3/modeling_smollm3.py +++ b/src/transformers/models/smollm3/modeling_smollm3.py @@ -251,11 +251,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/solar_open/modeling_solar_open.py b/src/transformers/models/solar_open/modeling_solar_open.py index 89c731d5a8e0..f3187df3718d 100644 --- a/src/transformers/models/solar_open/modeling_solar_open.py +++ b/src/transformers/models/solar_open/modeling_solar_open.py @@ -353,11 +353,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/timesfm/modeling_timesfm.py b/src/transformers/models/timesfm/modeling_timesfm.py index 0d40f8d63d45..dadb1215a3ad 100644 --- a/src/transformers/models/timesfm/modeling_timesfm.py +++ b/src/transformers/models/timesfm/modeling_timesfm.py @@ -127,11 +127,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py b/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py index 8b3eeab4d49b..32181cc25ff5 100644 --- a/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py +++ b/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py @@ -127,11 +127,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ultrabert/modeling_ultrabert.py b/src/transformers/models/ultrabert/modeling_ultrabert.py index 8bccca0989be..ff88efd35906 100644 --- a/src/transformers/models/ultrabert/modeling_ultrabert.py +++ b/src/transformers/models/ultrabert/modeling_ultrabert.py @@ -52,11 +52,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/ultravbert/modeling_ultravbert.py b/src/transformers/models/ultravbert/modeling_ultravbert.py index 31065ef0bcc1..c73f2d7e3042 100644 --- a/src/transformers/models/ultravbert/modeling_ultravbert.py +++ b/src/transformers/models/ultravbert/modeling_ultravbert.py @@ -83,11 +83,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py b/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py index 70505dd36639..daa3d08a8522 100644 --- a/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py +++ b/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py @@ -1184,11 +1184,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/vibevoice/modeling_vibevoice.py b/src/transformers/models/vibevoice/modeling_vibevoice.py index 1017c4b4285c..cf68bfaf03e1 100644 --- a/src/transformers/models/vibevoice/modeling_vibevoice.py +++ b/src/transformers/models/vibevoice/modeling_vibevoice.py @@ -77,11 +77,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py b/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py index 574f797e7f06..eaa3bf77e4ab 100644 --- a/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py +++ b/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py @@ -96,11 +96,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py b/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py index 62f280b7b660..597dc146f323 100644 --- a/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py +++ b/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py @@ -47,11 +47,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py b/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py index 0ac6f9fe72f3..4c2478d243e6 100644 --- a/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py +++ b/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py @@ -242,11 +242,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/xcodec2/modeling_xcodec2.py b/src/transformers/models/xcodec2/modeling_xcodec2.py index faafe6611c13..291ef3a8609f 100644 --- a/src/transformers/models/xcodec2/modeling_xcodec2.py +++ b/src/transformers/models/xcodec2/modeling_xcodec2.py @@ -310,11 +310,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/youtu/modeling_youtu.py b/src/transformers/models/youtu/modeling_youtu.py index 2c7e5a00760e..78b15e2b4f7b 100644 --- a/src/transformers/models/youtu/modeling_youtu.py +++ b/src/transformers/models/youtu/modeling_youtu.py @@ -59,11 +59,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/zamba/modeling_zamba.py b/src/transformers/models/zamba/modeling_zamba.py index cad18c8ae713..f4593a3c6065 100644 --- a/src/transformers/models/zamba/modeling_zamba.py +++ b/src/transformers/models/zamba/modeling_zamba.py @@ -64,11 +64,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/zamba2/modeling_zamba2.py b/src/transformers/models/zamba2/modeling_zamba2.py index a8d3b1dae88f..3441c9f08ed1 100644 --- a/src/transformers/models/zamba2/modeling_zamba2.py +++ b/src/transformers/models/zamba2/modeling_zamba2.py @@ -79,11 +79,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" diff --git a/src/transformers/models/zaya/modeling_zaya.py b/src/transformers/models/zaya/modeling_zaya.py index 027c5eb3ad1e..b2213d9a2117 100755 --- a/src/transformers/models/zaya/modeling_zaya.py +++ b/src/transformers/models/zaya/modeling_zaya.py @@ -118,11 +118,9 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - input_dtype = hidden_states.dtype - hidden_states = hidden_states.to(torch.float32) - variance = hidden_states.pow(2).mean(-1, keepdim=True) - hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) - return self.weight * hidden_states.to(input_dtype) + # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) + return self.weight * normalized.to(hidden_states.dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" From 78ea2974784dee60594df74302bf2937698ed719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Gallou=C3=A9dec?= Date: Wed, 25 Nov 2026 15:53:59 +0000 Subject: [PATCH 4/4] Say why the weight stays outside rms_norm --- src/transformers/models/aimv2/modeling_aimv2.py | 3 ++- src/transformers/models/apertus/modeling_apertus.py | 3 ++- .../models/apertus1p5/modeling_apertus1p5.py | 3 ++- src/transformers/models/arcee/modeling_arcee.py | 3 ++- src/transformers/models/aria/modeling_aria.py | 3 ++- src/transformers/models/axk1/modeling_axk1.py | 3 ++- src/transformers/models/axk2/modeling_axk2.py | 3 ++- .../models/bailing_hybrid/modeling_bailing_hybrid.py | 3 ++- src/transformers/models/bamba/modeling_bamba.py | 3 ++- src/transformers/models/bitnet/modeling_bitnet.py | 3 ++- src/transformers/models/blt/modeling_blt.py | 3 ++- .../models/chameleon/modeling_chameleon.py | 3 ++- src/transformers/models/clvp/modeling_clvp.py | 3 ++- .../models/cohere2_moe/modeling_cohere2_moe.py | 3 ++- .../models/cosmos3_edge/modeling_cosmos3_edge.py | 3 ++- src/transformers/models/csm/modeling_csm.py | 3 ++- src/transformers/models/cwm/modeling_cwm.py | 3 ++- .../models/deepseek_ocr2/modeling_deepseek_ocr2.py | 6 ++++-- .../models/deepseek_v2/modeling_deepseek_v2.py | 3 ++- .../models/deepseek_v3/modeling_deepseek_v3.py | 3 ++- .../models/deepseek_v32/modeling_deepseek_v32.py | 3 ++- src/transformers/models/deimv2/modeling_deimv2.py | 3 ++- src/transformers/models/dia/modeling_dia.py | 3 ++- .../models/diffllama/modeling_diffllama.py | 3 ++- src/transformers/models/doge/modeling_doge.py | 3 ++- src/transformers/models/dots1/modeling_dots1.py | 3 ++- .../models/dots3_note/modeling_dots3_note.py | 3 ++- src/transformers/models/emu3/modeling_emu3.py | 3 ++- .../models/ernie4_5/modeling_ernie4_5.py | 3 ++- .../models/ernie4_5_moe/modeling_ernie4_5_moe.py | 3 ++- .../ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py | 3 ++- .../models/eurobert/modeling_eurobert.py | 3 ++- src/transformers/models/evolla/modeling_evolla.py | 3 ++- src/transformers/models/exaone4/modeling_exaone4.py | 3 ++- .../models/exaone4_5/modeling_exaone4_5.py | 3 ++- .../models/exaone_moe/modeling_exaone_moe.py | 3 ++- .../models/falcon_h1/modeling_falcon_h1.py | 3 ++- .../models/falcon_mamba/modeling_falcon_mamba.py | 3 ++- src/transformers/models/glm/modeling_glm.py | 3 ++- src/transformers/models/glm4/modeling_glm4.py | 3 ++- .../models/glm4_moe/modeling_glm4_moe.py | 3 ++- .../models/glm4_moe_lite/modeling_glm4_moe_lite.py | 3 ++- src/transformers/models/glm4v/modeling_glm4v.py | 3 ++- .../models/glm4v_moe/modeling_glm4v_moe.py | 6 ++++-- .../models/glm5_next/modeling_glm5_next.py | 6 ++++-- .../models/glm_image/modeling_glm_image.py | 3 ++- .../models/glm_moe_dsa/modeling_glm_moe_dsa.py | 3 ++- src/transformers/models/glm_ocr/modeling_glm_ocr.py | 3 ++- src/transformers/models/granite/modeling_granite.py | 3 ++- .../granite4_vision/modeling_granite4_vision.py | 3 ++- .../granite_docling/modeling_granite_docling.py | 3 ++- .../models/granite_swa/modeling_granite_swa.py | 3 ++- .../models/granitemoe/modeling_granitemoe.py | 3 ++- .../models/granitemoe_swa/modeling_granitemoe_swa.py | 3 ++- .../granitemoehybrid/modeling_granitemoehybrid.py | 3 ++- .../granitemoeshared/modeling_granitemoeshared.py | 3 ++- .../models/higgs_audio_v2/modeling_higgs_audio_v2.py | 3 ++- .../hunyuan_v1_dense/modeling_hunyuan_v1_dense.py | 3 ++- .../models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py | 3 ++- .../models/hunyuan_vl/modeling_hunyuan_vl.py | 3 ++- src/transformers/models/hy_v3/modeling_hy_v3.py | 3 ++- src/transformers/models/hy_v4/modeling_hy_v4.py | 3 ++- .../models/hy_v4_vl/modeling_hy_v4_vl.py | 3 ++- .../models/hyperclovax/modeling_hyperclovax.py | 3 ++- .../models/idefics2/modeling_idefics2.py | 3 ++- .../models/idefics3/modeling_idefics3.py | 3 ++- src/transformers/models/inkling/modeling_inkling.py | 3 ++- .../models/internvl/modeling_internvl.py | 3 ++- src/transformers/models/jamba/modeling_jamba.py | 3 ++- src/transformers/models/jetmoe/modeling_jetmoe.py | 3 ++- .../models/kimi_linear/modeling_kimi_linear.py | 3 ++- .../models/kolibri1/modeling_kolibri1.py | 3 ++- .../models/kolibri1_1/modeling_kolibri1_1.py | 3 ++- src/transformers/models/laguna/modeling_laguna.py | 3 ++- src/transformers/models/lfm2/modeling_lfm2.py | 3 ++- .../models/lfm2_moe/modeling_lfm2_moe.py | 3 ++- src/transformers/models/lfm3/modeling_lfm3.py | 3 ++- src/transformers/models/lfm3_vl/modeling_lfm3_vl.py | 3 ++- .../models/lighton_ocr/modeling_lighton_ocr.py | 3 ++- src/transformers/models/llama/modeling_llama.py | 3 ++- .../models/longcat_flash/modeling_longcat_flash.py | 3 ++- src/transformers/models/mellum/modeling_mellum.py | 3 ++- .../models/mimo_v2_flash/modeling_mimo_v2_flash.py | 3 ++- .../models/minicpm3/modeling_minicpm3.py | 3 ++- src/transformers/models/minimax/modeling_minimax.py | 3 ++- .../models/minimax_m2/modeling_minimax_m2.py | 3 ++- .../models/ministral/modeling_ministral.py | 3 ++- .../models/ministral3/modeling_ministral3.py | 3 ++- src/transformers/models/mistral/modeling_mistral.py | 3 ++- .../models/mistral3/modeling_mistral3.py | 3 ++- .../models/mistral4/modeling_mistral4.py | 3 ++- .../models/mistral_large4/modeling_mistral_large4.py | 3 ++- src/transformers/models/mixtral/modeling_mixtral.py | 3 ++- src/transformers/models/mllama/modeling_mllama.py | 3 ++- src/transformers/models/molmo2/modeling_molmo2.py | 3 ++- .../modeling_muse_glimmer_assistant.py | 3 ++- .../models/neucodec/modeling_neucodec.py | 3 ++- src/transformers/models/olmoe/modeling_olmoe.py | 3 ++- src/transformers/models/ovis2/modeling_ovis2.py | 3 ++- .../models/paddleocr_vl/modeling_paddleocr_vl.py | 3 ++- .../models/pe_audio/modeling_pe_audio.py | 3 ++- .../models/pe_audio_video/modeling_pe_audio_video.py | 3 ++- .../models/pe_video/modeling_pe_video.py | 3 ++- src/transformers/models/phi3/modeling_phi3.py | 3 ++- .../phi4_multimodal/modeling_phi4_multimodal.py | 3 ++- src/transformers/models/pixtral/modeling_pixtral.py | 3 ++- .../models/qianfan_ocr/modeling_qianfan_ocr.py | 3 ++- src/transformers/models/qwen2/modeling_qwen2.py | 3 ++- .../models/qwen2_5_omni/modeling_qwen2_5_omni.py | 3 ++- .../models/qwen2_5_vl/modeling_qwen2_5_vl.py | 3 ++- .../models/qwen2_moe/modeling_qwen2_moe.py | 3 ++- .../models/qwen2_vl/modeling_qwen2_vl.py | 3 ++- src/transformers/models/qwen3/modeling_qwen3.py | 3 ++- .../models/qwen3_moe/modeling_qwen3_moe.py | 3 ++- .../models/qwen3_omni_moe/modeling_qwen3_omni_moe.py | 12 ++++++++---- .../models/qwen3_vl/modeling_qwen3_vl.py | 3 ++- .../models/qwen3_vl_moe/modeling_qwen3_vl_moe.py | 3 ++- .../models/sapiens2/modeling_sapiens2.py | 3 ++- .../models/seed_oss/modeling_seed_oss.py | 3 ++- src/transformers/models/smollm3/modeling_smollm3.py | 3 ++- .../models/solar_open/modeling_solar_open.py | 3 ++- src/transformers/models/timesfm/modeling_timesfm.py | 3 ++- .../models/timesfm2_5/modeling_timesfm2_5.py | 3 ++- .../models/ultrabert/modeling_ultrabert.py | 3 ++- .../models/ultravbert/modeling_ultravbert.py | 3 ++- .../models/unlimited_ocr/modeling_unlimited_ocr.py | 3 ++- .../models/vibevoice/modeling_vibevoice.py | 3 ++- .../modeling_vibevoice_acoustic_tokenizer.py | 3 ++- .../models/vibevoice_asr/modeling_vibevoice_asr.py | 3 ++- .../voxtral_realtime/modeling_voxtral_realtime.py | 3 ++- src/transformers/models/xcodec2/modeling_xcodec2.py | 3 ++- src/transformers/models/youtu/modeling_youtu.py | 3 ++- src/transformers/models/zamba/modeling_zamba.py | 3 ++- src/transformers/models/zamba2/modeling_zamba2.py | 3 ++- src/transformers/models/zaya/modeling_zaya.py | 3 ++- 135 files changed, 282 insertions(+), 141 deletions(-) diff --git a/src/transformers/models/aimv2/modeling_aimv2.py b/src/transformers/models/aimv2/modeling_aimv2.py index 7b9a3746deaa..7e861e23d446 100644 --- a/src/transformers/models/aimv2/modeling_aimv2.py +++ b/src/transformers/models/aimv2/modeling_aimv2.py @@ -87,7 +87,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/apertus/modeling_apertus.py b/src/transformers/models/apertus/modeling_apertus.py index 7335f17b3869..e941f6d00367 100644 --- a/src/transformers/models/apertus/modeling_apertus.py +++ b/src/transformers/models/apertus/modeling_apertus.py @@ -67,7 +67,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/apertus1p5/modeling_apertus1p5.py b/src/transformers/models/apertus1p5/modeling_apertus1p5.py index 391c0beae8c4..8400a45bde9d 100644 --- a/src/transformers/models/apertus1p5/modeling_apertus1p5.py +++ b/src/transformers/models/apertus1p5/modeling_apertus1p5.py @@ -385,7 +385,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/arcee/modeling_arcee.py b/src/transformers/models/arcee/modeling_arcee.py index c637b7d33498..1341086e48a4 100644 --- a/src/transformers/models/arcee/modeling_arcee.py +++ b/src/transformers/models/arcee/modeling_arcee.py @@ -72,7 +72,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/aria/modeling_aria.py b/src/transformers/models/aria/modeling_aria.py index 9c631560114f..b87fe16eab39 100644 --- a/src/transformers/models/aria/modeling_aria.py +++ b/src/transformers/models/aria/modeling_aria.py @@ -63,7 +63,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/axk1/modeling_axk1.py b/src/transformers/models/axk1/modeling_axk1.py index e16ef971d180..943f23ebff53 100644 --- a/src/transformers/models/axk1/modeling_axk1.py +++ b/src/transformers/models/axk1/modeling_axk1.py @@ -58,7 +58,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/axk2/modeling_axk2.py b/src/transformers/models/axk2/modeling_axk2.py index 66db8f6a2b21..47dbdddc4c45 100644 --- a/src/transformers/models/axk2/modeling_axk2.py +++ b/src/transformers/models/axk2/modeling_axk2.py @@ -58,7 +58,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py b/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py index fa9ec108085f..fb9f3904517d 100644 --- a/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py +++ b/src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py @@ -62,7 +62,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/bamba/modeling_bamba.py b/src/transformers/models/bamba/modeling_bamba.py index 8ca724810c35..54cb3e8948d5 100644 --- a/src/transformers/models/bamba/modeling_bamba.py +++ b/src/transformers/models/bamba/modeling_bamba.py @@ -829,7 +829,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/bitnet/modeling_bitnet.py b/src/transformers/models/bitnet/modeling_bitnet.py index 9d1478eb5eff..921bad505b3d 100644 --- a/src/transformers/models/bitnet/modeling_bitnet.py +++ b/src/transformers/models/bitnet/modeling_bitnet.py @@ -51,7 +51,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/blt/modeling_blt.py b/src/transformers/models/blt/modeling_blt.py index b7d72696b62a..e61ad20536e1 100644 --- a/src/transformers/models/blt/modeling_blt.py +++ b/src/transformers/models/blt/modeling_blt.py @@ -76,7 +76,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/chameleon/modeling_chameleon.py b/src/transformers/models/chameleon/modeling_chameleon.py index de3ed082c826..9233b21e8459 100644 --- a/src/transformers/models/chameleon/modeling_chameleon.py +++ b/src/transformers/models/chameleon/modeling_chameleon.py @@ -75,7 +75,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/clvp/modeling_clvp.py b/src/transformers/models/clvp/modeling_clvp.py index 8bd5e1994626..8392bb0bc43e 100644 --- a/src/transformers/models/clvp/modeling_clvp.py +++ b/src/transformers/models/clvp/modeling_clvp.py @@ -229,7 +229,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py b/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py index 2f2cec3a75d0..f46d8439029c 100644 --- a/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py +++ b/src/transformers/models/cohere2_moe/modeling_cohere2_moe.py @@ -55,7 +55,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py b/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py index ba5c02bfef5d..ecf972c403c9 100644 --- a/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py +++ b/src/transformers/models/cosmos3_edge/modeling_cosmos3_edge.py @@ -285,7 +285,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/csm/modeling_csm.py b/src/transformers/models/csm/modeling_csm.py index ada81e07c849..94f37e40167e 100644 --- a/src/transformers/models/csm/modeling_csm.py +++ b/src/transformers/models/csm/modeling_csm.py @@ -107,7 +107,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/cwm/modeling_cwm.py b/src/transformers/models/cwm/modeling_cwm.py index e6eb4222221d..6378275707d2 100644 --- a/src/transformers/models/cwm/modeling_cwm.py +++ b/src/transformers/models/cwm/modeling_cwm.py @@ -232,7 +232,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py b/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py index d69d9d5fef14..83c351ca0c0a 100644 --- a/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py +++ b/src/transformers/models/deepseek_ocr2/modeling_deepseek_ocr2.py @@ -624,7 +624,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) @@ -1199,7 +1200,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py b/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py index e63b79980b35..1321191d729d 100644 --- a/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py +++ b/src/transformers/models/deepseek_v2/modeling_deepseek_v2.py @@ -163,7 +163,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py index 87d6f5ce4f9a..38e8b7a0d469 100644 --- a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py +++ b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py @@ -44,7 +44,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py b/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py index e63092b9f7de..5781c240f49c 100644 --- a/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py +++ b/src/transformers/models/deepseek_v32/modeling_deepseek_v32.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/deimv2/modeling_deimv2.py b/src/transformers/models/deimv2/modeling_deimv2.py index b0289409eece..762c3cd5b36e 100644 --- a/src/transformers/models/deimv2/modeling_deimv2.py +++ b/src/transformers/models/deimv2/modeling_deimv2.py @@ -168,7 +168,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/dia/modeling_dia.py b/src/transformers/models/dia/modeling_dia.py index 58f0be9ba4a4..f1d1f09d020f 100644 --- a/src/transformers/models/dia/modeling_dia.py +++ b/src/transformers/models/dia/modeling_dia.py @@ -128,7 +128,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/diffllama/modeling_diffllama.py b/src/transformers/models/diffllama/modeling_diffllama.py index af7097e0009f..1c3d85aeb7b5 100644 --- a/src/transformers/models/diffllama/modeling_diffllama.py +++ b/src/transformers/models/diffllama/modeling_diffllama.py @@ -323,7 +323,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/doge/modeling_doge.py b/src/transformers/models/doge/modeling_doge.py index cc9027d54090..7ed288de9608 100644 --- a/src/transformers/models/doge/modeling_doge.py +++ b/src/transformers/models/doge/modeling_doge.py @@ -62,7 +62,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/dots1/modeling_dots1.py b/src/transformers/models/dots1/modeling_dots1.py index 401f08cb186d..c518db5c0928 100644 --- a/src/transformers/models/dots1/modeling_dots1.py +++ b/src/transformers/models/dots1/modeling_dots1.py @@ -53,7 +53,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/dots3_note/modeling_dots3_note.py b/src/transformers/models/dots3_note/modeling_dots3_note.py index f44d08e5be3d..79ee0d5013d5 100644 --- a/src/transformers/models/dots3_note/modeling_dots3_note.py +++ b/src/transformers/models/dots3_note/modeling_dots3_note.py @@ -67,7 +67,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/emu3/modeling_emu3.py b/src/transformers/models/emu3/modeling_emu3.py index 96dabc804b78..23c6541b04ed 100644 --- a/src/transformers/models/emu3/modeling_emu3.py +++ b/src/transformers/models/emu3/modeling_emu3.py @@ -207,7 +207,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ernie4_5/modeling_ernie4_5.py b/src/transformers/models/ernie4_5/modeling_ernie4_5.py index baf240126a90..db93b2ded8b3 100644 --- a/src/transformers/models/ernie4_5/modeling_ernie4_5.py +++ b/src/transformers/models/ernie4_5/modeling_ernie4_5.py @@ -254,7 +254,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py b/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py index 68662655dd21..d9d0a127a3b4 100644 --- a/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py +++ b/src/transformers/models/ernie4_5_moe/modeling_ernie4_5_moe.py @@ -53,7 +53,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py index fab3d379cfea..16507afb5110 100644 --- a/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py @@ -276,7 +276,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/eurobert/modeling_eurobert.py b/src/transformers/models/eurobert/modeling_eurobert.py index 8f25cb65c78d..eb672b601f85 100644 --- a/src/transformers/models/eurobert/modeling_eurobert.py +++ b/src/transformers/models/eurobert/modeling_eurobert.py @@ -52,7 +52,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/evolla/modeling_evolla.py b/src/transformers/models/evolla/modeling_evolla.py index 19d393ed8eb7..b94b5c43d04e 100644 --- a/src/transformers/models/evolla/modeling_evolla.py +++ b/src/transformers/models/evolla/modeling_evolla.py @@ -1008,7 +1008,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/exaone4/modeling_exaone4.py b/src/transformers/models/exaone4/modeling_exaone4.py index f34011fac339..58db19f6ab6e 100644 --- a/src/transformers/models/exaone4/modeling_exaone4.py +++ b/src/transformers/models/exaone4/modeling_exaone4.py @@ -57,7 +57,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/exaone4_5/modeling_exaone4_5.py b/src/transformers/models/exaone4_5/modeling_exaone4_5.py index 892396851e03..d6fd7d65ad15 100644 --- a/src/transformers/models/exaone4_5/modeling_exaone4_5.py +++ b/src/transformers/models/exaone4_5/modeling_exaone4_5.py @@ -147,7 +147,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/exaone_moe/modeling_exaone_moe.py b/src/transformers/models/exaone_moe/modeling_exaone_moe.py index e64df52d8b29..2d7fb76105dc 100644 --- a/src/transformers/models/exaone_moe/modeling_exaone_moe.py +++ b/src/transformers/models/exaone_moe/modeling_exaone_moe.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/falcon_h1/modeling_falcon_h1.py b/src/transformers/models/falcon_h1/modeling_falcon_h1.py index d6470eafcdba..45d224361c07 100644 --- a/src/transformers/models/falcon_h1/modeling_falcon_h1.py +++ b/src/transformers/models/falcon_h1/modeling_falcon_h1.py @@ -854,7 +854,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py b/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py index f840a3dd77f4..c8623049b65d 100644 --- a/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py +++ b/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py @@ -484,7 +484,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm/modeling_glm.py b/src/transformers/models/glm/modeling_glm.py index 0902a0ded4f9..6f7b5f555226 100644 --- a/src/transformers/models/glm/modeling_glm.py +++ b/src/transformers/models/glm/modeling_glm.py @@ -272,7 +272,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm4/modeling_glm4.py b/src/transformers/models/glm4/modeling_glm4.py index 6b68a5da250c..28f279c3c126 100644 --- a/src/transformers/models/glm4/modeling_glm4.py +++ b/src/transformers/models/glm4/modeling_glm4.py @@ -320,7 +320,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm4_moe/modeling_glm4_moe.py b/src/transformers/models/glm4_moe/modeling_glm4_moe.py index eae6402fece5..54dc73ffc131 100644 --- a/src/transformers/models/glm4_moe/modeling_glm4_moe.py +++ b/src/transformers/models/glm4_moe/modeling_glm4_moe.py @@ -318,7 +318,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py b/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py index 6c28fcb0dbf6..5a012d9b7a39 100644 --- a/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py +++ b/src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py @@ -408,7 +408,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm4v/modeling_glm4v.py b/src/transformers/models/glm4v/modeling_glm4v.py index 8f04ff8cba0f..b9cc2ed7a677 100644 --- a/src/transformers/models/glm4v/modeling_glm4v.py +++ b/src/transformers/models/glm4v/modeling_glm4v.py @@ -66,7 +66,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py index db632a8ed1aa..6620607ebe2e 100644 --- a/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py +++ b/src/transformers/models/glm4v_moe/modeling_glm4v_moe.py @@ -400,7 +400,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) @@ -554,7 +555,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm5_next/modeling_glm5_next.py b/src/transformers/models/glm5_next/modeling_glm5_next.py index 87645eb3879a..178c961ee244 100644 --- a/src/transformers/models/glm5_next/modeling_glm5_next.py +++ b/src/transformers/models/glm5_next/modeling_glm5_next.py @@ -74,7 +74,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) @@ -1574,7 +1575,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm_image/modeling_glm_image.py b/src/transformers/models/glm_image/modeling_glm_image.py index bdbce429400a..bf671b63b7cb 100644 --- a/src/transformers/models/glm_image/modeling_glm_image.py +++ b/src/transformers/models/glm_image/modeling_glm_image.py @@ -681,7 +681,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py b/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py index 81a26f0a1b9d..011c4f4e734a 100644 --- a/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py +++ b/src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/glm_ocr/modeling_glm_ocr.py b/src/transformers/models/glm_ocr/modeling_glm_ocr.py index 2f9ecad4aed0..dda98974f7d0 100644 --- a/src/transformers/models/glm_ocr/modeling_glm_ocr.py +++ b/src/transformers/models/glm_ocr/modeling_glm_ocr.py @@ -66,7 +66,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granite/modeling_granite.py b/src/transformers/models/granite/modeling_granite.py index 9846328c7786..53355d5f2a0e 100644 --- a/src/transformers/models/granite/modeling_granite.py +++ b/src/transformers/models/granite/modeling_granite.py @@ -191,7 +191,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granite4_vision/modeling_granite4_vision.py b/src/transformers/models/granite4_vision/modeling_granite4_vision.py index 42888c2e669a..51798458881b 100644 --- a/src/transformers/models/granite4_vision/modeling_granite4_vision.py +++ b/src/transformers/models/granite4_vision/modeling_granite4_vision.py @@ -414,7 +414,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granite_docling/modeling_granite_docling.py b/src/transformers/models/granite_docling/modeling_granite_docling.py index c6e54e2c6286..311d043d0ce4 100644 --- a/src/transformers/models/granite_docling/modeling_granite_docling.py +++ b/src/transformers/models/granite_docling/modeling_granite_docling.py @@ -249,7 +249,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granite_swa/modeling_granite_swa.py b/src/transformers/models/granite_swa/modeling_granite_swa.py index 51a82b536dc4..77cc53db9bcd 100644 --- a/src/transformers/models/granite_swa/modeling_granite_swa.py +++ b/src/transformers/models/granite_swa/modeling_granite_swa.py @@ -208,7 +208,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granitemoe/modeling_granitemoe.py b/src/transformers/models/granitemoe/modeling_granitemoe.py index 9780196f7977..c78b811f2c19 100644 --- a/src/transformers/models/granitemoe/modeling_granitemoe.py +++ b/src/transformers/models/granitemoe/modeling_granitemoe.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py b/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py index 70841ddbf451..5489197150e8 100644 --- a/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py +++ b/src/transformers/models/granitemoe_swa/modeling_granitemoe_swa.py @@ -338,7 +338,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py index d236fad2cb4b..55bde007fb2f 100644 --- a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py +++ b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py @@ -930,7 +930,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py index 7a8ca8e6f90e..cd29fad41db1 100644 --- a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py +++ b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py @@ -104,7 +104,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py b/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py index e34353bf1af3..8b9621117fd4 100644 --- a/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py +++ b/src/transformers/models/higgs_audio_v2/modeling_higgs_audio_v2.py @@ -71,7 +71,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py b/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py index 31de4454efa4..8e00ec711040 100644 --- a/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py +++ b/src/transformers/models/hunyuan_v1_dense/modeling_hunyuan_v1_dense.py @@ -53,7 +53,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py b/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py index 27be7472ffaa..c11b0d2044b6 100644 --- a/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py +++ b/src/transformers/models/hunyuan_v1_moe/modeling_hunyuan_v1_moe.py @@ -53,7 +53,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py b/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py index 9721b265b02f..158a20daecf0 100644 --- a/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py +++ b/src/transformers/models/hunyuan_vl/modeling_hunyuan_vl.py @@ -92,7 +92,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hy_v3/modeling_hy_v3.py b/src/transformers/models/hy_v3/modeling_hy_v3.py index b268096340c1..6bbc352e42b8 100644 --- a/src/transformers/models/hy_v3/modeling_hy_v3.py +++ b/src/transformers/models/hy_v3/modeling_hy_v3.py @@ -53,7 +53,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hy_v4/modeling_hy_v4.py b/src/transformers/models/hy_v4/modeling_hy_v4.py index 2f7d9146b61c..57c738f1ab8b 100644 --- a/src/transformers/models/hy_v4/modeling_hy_v4.py +++ b/src/transformers/models/hy_v4/modeling_hy_v4.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py b/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py index fc33a6c1bc36..6debb0ce3c7a 100644 --- a/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py +++ b/src/transformers/models/hy_v4_vl/modeling_hy_v4_vl.py @@ -742,7 +742,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/hyperclovax/modeling_hyperclovax.py b/src/transformers/models/hyperclovax/modeling_hyperclovax.py index c8b297b1cb61..42ab2f106222 100644 --- a/src/transformers/models/hyperclovax/modeling_hyperclovax.py +++ b/src/transformers/models/hyperclovax/modeling_hyperclovax.py @@ -51,7 +51,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/idefics2/modeling_idefics2.py b/src/transformers/models/idefics2/modeling_idefics2.py index 08d3807155fb..f57f473e8fe0 100644 --- a/src/transformers/models/idefics2/modeling_idefics2.py +++ b/src/transformers/models/idefics2/modeling_idefics2.py @@ -487,7 +487,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/idefics3/modeling_idefics3.py b/src/transformers/models/idefics3/modeling_idefics3.py index 7617dbddf804..94af4cd173d9 100644 --- a/src/transformers/models/idefics3/modeling_idefics3.py +++ b/src/transformers/models/idefics3/modeling_idefics3.py @@ -349,7 +349,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/inkling/modeling_inkling.py b/src/transformers/models/inkling/modeling_inkling.py index 1c535701cc28..756181c63d2e 100644 --- a/src/transformers/models/inkling/modeling_inkling.py +++ b/src/transformers/models/inkling/modeling_inkling.py @@ -97,7 +97,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/internvl/modeling_internvl.py b/src/transformers/models/internvl/modeling_internvl.py index 4bcd9f2b2575..a59a7ee077aa 100644 --- a/src/transformers/models/internvl/modeling_internvl.py +++ b/src/transformers/models/internvl/modeling_internvl.py @@ -62,7 +62,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/jamba/modeling_jamba.py b/src/transformers/models/jamba/modeling_jamba.py index 561bc552b80c..0c0bfb5e450e 100755 --- a/src/transformers/models/jamba/modeling_jamba.py +++ b/src/transformers/models/jamba/modeling_jamba.py @@ -71,7 +71,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/jetmoe/modeling_jetmoe.py b/src/transformers/models/jetmoe/modeling_jetmoe.py index 18e8ee3833f4..a63a20526742 100644 --- a/src/transformers/models/jetmoe/modeling_jetmoe.py +++ b/src/transformers/models/jetmoe/modeling_jetmoe.py @@ -56,7 +56,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/kimi_linear/modeling_kimi_linear.py b/src/transformers/models/kimi_linear/modeling_kimi_linear.py index 435ee53ed956..54bf2943ef4b 100644 --- a/src/transformers/models/kimi_linear/modeling_kimi_linear.py +++ b/src/transformers/models/kimi_linear/modeling_kimi_linear.py @@ -61,7 +61,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/kolibri1/modeling_kolibri1.py b/src/transformers/models/kolibri1/modeling_kolibri1.py index 09f370d0379b..1f3725cf958d 100644 --- a/src/transformers/models/kolibri1/modeling_kolibri1.py +++ b/src/transformers/models/kolibri1/modeling_kolibri1.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py b/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py index 45609025cbbf..6bed90524a5e 100644 --- a/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py +++ b/src/transformers/models/kolibri1_1/modeling_kolibri1_1.py @@ -380,7 +380,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/laguna/modeling_laguna.py b/src/transformers/models/laguna/modeling_laguna.py index 6f56d4605278..71eb59d566a0 100644 --- a/src/transformers/models/laguna/modeling_laguna.py +++ b/src/transformers/models/laguna/modeling_laguna.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/lfm2/modeling_lfm2.py b/src/transformers/models/lfm2/modeling_lfm2.py index c90c7a028504..194aa5510456 100644 --- a/src/transformers/models/lfm2/modeling_lfm2.py +++ b/src/transformers/models/lfm2/modeling_lfm2.py @@ -52,7 +52,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py b/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py index 052d555240ce..ecd05a5e874f 100644 --- a/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py +++ b/src/transformers/models/lfm2_moe/modeling_lfm2_moe.py @@ -59,7 +59,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/lfm3/modeling_lfm3.py b/src/transformers/models/lfm3/modeling_lfm3.py index e51b32e8fa27..f0c15f19fbdc 100644 --- a/src/transformers/models/lfm3/modeling_lfm3.py +++ b/src/transformers/models/lfm3/modeling_lfm3.py @@ -53,7 +53,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py b/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py index 29b37778ba4f..2391d79d6b2c 100644 --- a/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py +++ b/src/transformers/models/lfm3_vl/modeling_lfm3_vl.py @@ -59,7 +59,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py b/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py index b4d01f808486..d1997c4e6631 100644 --- a/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py +++ b/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/llama/modeling_llama.py b/src/transformers/models/llama/modeling_llama.py index cba98a6cd0c0..74c27ba5d80f 100644 --- a/src/transformers/models/llama/modeling_llama.py +++ b/src/transformers/models/llama/modeling_llama.py @@ -60,7 +60,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/longcat_flash/modeling_longcat_flash.py b/src/transformers/models/longcat_flash/modeling_longcat_flash.py index 46c7942d8c4d..d90de26ead42 100644 --- a/src/transformers/models/longcat_flash/modeling_longcat_flash.py +++ b/src/transformers/models/longcat_flash/modeling_longcat_flash.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mellum/modeling_mellum.py b/src/transformers/models/mellum/modeling_mellum.py index c2709e0e4cca..998ab2ba0a26 100644 --- a/src/transformers/models/mellum/modeling_mellum.py +++ b/src/transformers/models/mellum/modeling_mellum.py @@ -118,7 +118,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py b/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py index bfaa5acae979..0033817c676c 100644 --- a/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py +++ b/src/transformers/models/mimo_v2_flash/modeling_mimo_v2_flash.py @@ -54,7 +54,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/minicpm3/modeling_minicpm3.py b/src/transformers/models/minicpm3/modeling_minicpm3.py index 0c4edd90599b..29ec57fadeb6 100644 --- a/src/transformers/models/minicpm3/modeling_minicpm3.py +++ b/src/transformers/models/minicpm3/modeling_minicpm3.py @@ -67,7 +67,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/minimax/modeling_minimax.py b/src/transformers/models/minimax/modeling_minimax.py index 8fbff38ff3fb..6d38677247fb 100644 --- a/src/transformers/models/minimax/modeling_minimax.py +++ b/src/transformers/models/minimax/modeling_minimax.py @@ -60,7 +60,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/minimax_m2/modeling_minimax_m2.py b/src/transformers/models/minimax_m2/modeling_minimax_m2.py index 696ed8c1987d..05a4255983e6 100644 --- a/src/transformers/models/minimax_m2/modeling_minimax_m2.py +++ b/src/transformers/models/minimax_m2/modeling_minimax_m2.py @@ -135,7 +135,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ministral/modeling_ministral.py b/src/transformers/models/ministral/modeling_ministral.py index 2dd92c5fc312..9b95c4707080 100644 --- a/src/transformers/models/ministral/modeling_ministral.py +++ b/src/transformers/models/ministral/modeling_ministral.py @@ -208,7 +208,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ministral3/modeling_ministral3.py b/src/transformers/models/ministral3/modeling_ministral3.py index 8280d1434dee..1463c43f8e0f 100644 --- a/src/transformers/models/ministral3/modeling_ministral3.py +++ b/src/transformers/models/ministral3/modeling_ministral3.py @@ -202,7 +202,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mistral/modeling_mistral.py b/src/transformers/models/mistral/modeling_mistral.py index bbc2807c2448..a70e6c950cae 100644 --- a/src/transformers/models/mistral/modeling_mistral.py +++ b/src/transformers/models/mistral/modeling_mistral.py @@ -190,7 +190,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mistral3/modeling_mistral3.py b/src/transformers/models/mistral3/modeling_mistral3.py index 8111bccceea3..0f9b4f73a589 100644 --- a/src/transformers/models/mistral3/modeling_mistral3.py +++ b/src/transformers/models/mistral3/modeling_mistral3.py @@ -57,7 +57,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mistral4/modeling_mistral4.py b/src/transformers/models/mistral4/modeling_mistral4.py index 8e31325eb207..19daf9f02967 100644 --- a/src/transformers/models/mistral4/modeling_mistral4.py +++ b/src/transformers/models/mistral4/modeling_mistral4.py @@ -57,7 +57,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mistral_large4/modeling_mistral_large4.py b/src/transformers/models/mistral_large4/modeling_mistral_large4.py index d98c15067836..5eb714df6fd0 100644 --- a/src/transformers/models/mistral_large4/modeling_mistral_large4.py +++ b/src/transformers/models/mistral_large4/modeling_mistral_large4.py @@ -64,7 +64,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mixtral/modeling_mixtral.py b/src/transformers/models/mixtral/modeling_mixtral.py index 7c8412ef10a0..d927810d0554 100644 --- a/src/transformers/models/mixtral/modeling_mixtral.py +++ b/src/transformers/models/mixtral/modeling_mixtral.py @@ -141,7 +141,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/mllama/modeling_mllama.py b/src/transformers/models/mllama/modeling_mllama.py index c8692ef7337a..9a6b2451ca88 100644 --- a/src/transformers/models/mllama/modeling_mllama.py +++ b/src/transformers/models/mllama/modeling_mllama.py @@ -387,7 +387,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/molmo2/modeling_molmo2.py b/src/transformers/models/molmo2/modeling_molmo2.py index 90ab3e8c06a2..e0347edd887b 100644 --- a/src/transformers/models/molmo2/modeling_molmo2.py +++ b/src/transformers/models/molmo2/modeling_molmo2.py @@ -141,7 +141,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py b/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py index 2dc089fa8c85..ba5c4aab2f08 100644 --- a/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py +++ b/src/transformers/models/muse_glimmer_assistant/modeling_muse_glimmer_assistant.py @@ -49,7 +49,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/neucodec/modeling_neucodec.py b/src/transformers/models/neucodec/modeling_neucodec.py index f395b24a1f4f..a97386ec981c 100644 --- a/src/transformers/models/neucodec/modeling_neucodec.py +++ b/src/transformers/models/neucodec/modeling_neucodec.py @@ -269,7 +269,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/olmoe/modeling_olmoe.py b/src/transformers/models/olmoe/modeling_olmoe.py index 79fb04c79d82..41d5383fb5cc 100644 --- a/src/transformers/models/olmoe/modeling_olmoe.py +++ b/src/transformers/models/olmoe/modeling_olmoe.py @@ -51,7 +51,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ovis2/modeling_ovis2.py b/src/transformers/models/ovis2/modeling_ovis2.py index 709de85bdc93..0bbe2deab0e0 100644 --- a/src/transformers/models/ovis2/modeling_ovis2.py +++ b/src/transformers/models/ovis2/modeling_ovis2.py @@ -96,7 +96,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py index 10d117558096..1133b00a561a 100644 --- a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py @@ -402,7 +402,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/pe_audio/modeling_pe_audio.py b/src/transformers/models/pe_audio/modeling_pe_audio.py index 420483d3b042..11ae34b696f2 100644 --- a/src/transformers/models/pe_audio/modeling_pe_audio.py +++ b/src/transformers/models/pe_audio/modeling_pe_audio.py @@ -351,7 +351,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py b/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py index a9bfb7689504..41a743d661d4 100644 --- a/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py +++ b/src/transformers/models/pe_audio_video/modeling_pe_audio_video.py @@ -440,7 +440,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/pe_video/modeling_pe_video.py b/src/transformers/models/pe_video/modeling_pe_video.py index 43cc810186bd..6d6291079264 100644 --- a/src/transformers/models/pe_video/modeling_pe_video.py +++ b/src/transformers/models/pe_video/modeling_pe_video.py @@ -275,7 +275,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/phi3/modeling_phi3.py b/src/transformers/models/phi3/modeling_phi3.py index 0ad71be3074f..81af53b6b607 100644 --- a/src/transformers/models/phi3/modeling_phi3.py +++ b/src/transformers/models/phi3/modeling_phi3.py @@ -264,7 +264,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py index 1d44201e8bfe..10e13626fcb9 100644 --- a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py @@ -1112,7 +1112,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/pixtral/modeling_pixtral.py b/src/transformers/models/pixtral/modeling_pixtral.py index ba73782722ed..333c7ca91ddf 100644 --- a/src/transformers/models/pixtral/modeling_pixtral.py +++ b/src/transformers/models/pixtral/modeling_pixtral.py @@ -248,7 +248,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py b/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py index 7aa5f56bc7e4..a68cc2e85101 100644 --- a/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py +++ b/src/transformers/models/qianfan_ocr/modeling_qianfan_ocr.py @@ -84,7 +84,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen2/modeling_qwen2.py b/src/transformers/models/qwen2/modeling_qwen2.py index 78d0ba6d4248..4ea1d5adaf59 100644 --- a/src/transformers/models/qwen2/modeling_qwen2.py +++ b/src/transformers/models/qwen2/modeling_qwen2.py @@ -238,7 +238,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py b/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py index 8e17e091ef06..5f9123fa208e 100644 --- a/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py +++ b/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py @@ -1093,7 +1093,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py b/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py index b61e1e29d9a4..1ef692ec5bdf 100644 --- a/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +++ b/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py @@ -72,7 +72,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py index 0563bb7ba686..dc81161aee21 100644 --- a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py +++ b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py @@ -63,7 +63,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py b/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py index d8a7ca9c6c75..15b6896e3880 100644 --- a/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py @@ -75,7 +75,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen3/modeling_qwen3.py b/src/transformers/models/qwen3/modeling_qwen3.py index e282051847ce..b39bcca39df6 100644 --- a/src/transformers/models/qwen3/modeling_qwen3.py +++ b/src/transformers/models/qwen3/modeling_qwen3.py @@ -57,7 +57,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py index 9dc90651c73a..1ad29f622771 100644 --- a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py +++ b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py @@ -293,7 +293,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py b/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py index 00457a65a9bc..fbecc764c710 100644 --- a/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py +++ b/src/transformers/models/qwen3_omni_moe/modeling_qwen3_omni_moe.py @@ -1388,7 +1388,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) @@ -1601,7 +1602,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) @@ -2414,7 +2416,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) @@ -3611,7 +3614,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py b/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py index aaf46d1018ff..51d7f18970ed 100644 --- a/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py +++ b/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py @@ -430,7 +430,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py b/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py index 013618084f5e..94dcd2004cd2 100644 --- a/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py +++ b/src/transformers/models/qwen3_vl_moe/modeling_qwen3_vl_moe.py @@ -100,7 +100,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/sapiens2/modeling_sapiens2.py b/src/transformers/models/sapiens2/modeling_sapiens2.py index 58471639a58d..7ce0a71be1c7 100644 --- a/src/transformers/models/sapiens2/modeling_sapiens2.py +++ b/src/transformers/models/sapiens2/modeling_sapiens2.py @@ -338,7 +338,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/seed_oss/modeling_seed_oss.py b/src/transformers/models/seed_oss/modeling_seed_oss.py index acf62efa68a8..0a7c12c3a500 100644 --- a/src/transformers/models/seed_oss/modeling_seed_oss.py +++ b/src/transformers/models/seed_oss/modeling_seed_oss.py @@ -56,7 +56,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/smollm3/modeling_smollm3.py b/src/transformers/models/smollm3/modeling_smollm3.py index c0943e7a5daf..1abd58e90c6a 100644 --- a/src/transformers/models/smollm3/modeling_smollm3.py +++ b/src/transformers/models/smollm3/modeling_smollm3.py @@ -251,7 +251,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/solar_open/modeling_solar_open.py b/src/transformers/models/solar_open/modeling_solar_open.py index f3187df3718d..97eba5007a9a 100644 --- a/src/transformers/models/solar_open/modeling_solar_open.py +++ b/src/transformers/models/solar_open/modeling_solar_open.py @@ -353,7 +353,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/timesfm/modeling_timesfm.py b/src/transformers/models/timesfm/modeling_timesfm.py index dadb1215a3ad..c4da1386df62 100644 --- a/src/transformers/models/timesfm/modeling_timesfm.py +++ b/src/transformers/models/timesfm/modeling_timesfm.py @@ -127,7 +127,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py b/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py index 32181cc25ff5..1f68a9993015 100644 --- a/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py +++ b/src/transformers/models/timesfm2_5/modeling_timesfm2_5.py @@ -127,7 +127,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ultrabert/modeling_ultrabert.py b/src/transformers/models/ultrabert/modeling_ultrabert.py index ff88efd35906..269abfd59a6b 100644 --- a/src/transformers/models/ultrabert/modeling_ultrabert.py +++ b/src/transformers/models/ultrabert/modeling_ultrabert.py @@ -52,7 +52,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/ultravbert/modeling_ultravbert.py b/src/transformers/models/ultravbert/modeling_ultravbert.py index c73f2d7e3042..3e0c3d66bd08 100644 --- a/src/transformers/models/ultravbert/modeling_ultravbert.py +++ b/src/transformers/models/ultravbert/modeling_ultravbert.py @@ -83,7 +83,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py b/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py index daa3d08a8522..4b7b606ab5a5 100644 --- a/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py +++ b/src/transformers/models/unlimited_ocr/modeling_unlimited_ocr.py @@ -1184,7 +1184,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/vibevoice/modeling_vibevoice.py b/src/transformers/models/vibevoice/modeling_vibevoice.py index cf68bfaf03e1..cde438cd29ef 100644 --- a/src/transformers/models/vibevoice/modeling_vibevoice.py +++ b/src/transformers/models/vibevoice/modeling_vibevoice.py @@ -77,7 +77,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py b/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py index eaa3bf77e4ab..319c3fff7ed6 100644 --- a/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py +++ b/src/transformers/models/vibevoice_acoustic_tokenizer/modeling_vibevoice_acoustic_tokenizer.py @@ -96,7 +96,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py b/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py index 597dc146f323..f815a14a638d 100644 --- a/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py +++ b/src/transformers/models/vibevoice_asr/modeling_vibevoice_asr.py @@ -47,7 +47,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py b/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py index 4c2478d243e6..c1563484dabb 100644 --- a/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py +++ b/src/transformers/models/voxtral_realtime/modeling_voxtral_realtime.py @@ -242,7 +242,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/xcodec2/modeling_xcodec2.py b/src/transformers/models/xcodec2/modeling_xcodec2.py index 291ef3a8609f..cce7f9279cde 100644 --- a/src/transformers/models/xcodec2/modeling_xcodec2.py +++ b/src/transformers/models/xcodec2/modeling_xcodec2.py @@ -310,7 +310,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/youtu/modeling_youtu.py b/src/transformers/models/youtu/modeling_youtu.py index 78b15e2b4f7b..418b06553f56 100644 --- a/src/transformers/models/youtu/modeling_youtu.py +++ b/src/transformers/models/youtu/modeling_youtu.py @@ -59,7 +59,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/zamba/modeling_zamba.py b/src/transformers/models/zamba/modeling_zamba.py index f4593a3c6065..2c7df81b3bea 100644 --- a/src/transformers/models/zamba/modeling_zamba.py +++ b/src/transformers/models/zamba/modeling_zamba.py @@ -64,7 +64,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/zamba2/modeling_zamba2.py b/src/transformers/models/zamba2/modeling_zamba2.py index 3441c9f08ed1..3f6ab8b61cda 100644 --- a/src/transformers/models/zamba2/modeling_zamba2.py +++ b/src/transformers/models/zamba2/modeling_zamba2.py @@ -79,7 +79,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype) diff --git a/src/transformers/models/zaya/modeling_zaya.py b/src/transformers/models/zaya/modeling_zaya.py index b2213d9a2117..a293a7b57734 100755 --- a/src/transformers/models/zaya/modeling_zaya.py +++ b/src/transformers/models/zaya/modeling_zaya.py @@ -118,7 +118,8 @@ def __init__(self, hidden_size, eps: float = 1e-6) -> None: self.variance_epsilon = eps def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: - # Scale after casting back to the input dtype, as the original implementations do (autocast runs it in fp32) + # Don't pass the weight to rms_norm: scaling after the cast to the input dtype (autocast runs rms_norm in + # fp32) keeps the rounding and output dtype of the original implementations, fusing it shifts the logits normalized = nn.functional.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.variance_epsilon) return self.weight * normalized.to(hidden_states.dtype)