You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RMSNorm without affine weight runs as five or six kernels; F.rms_norm is 2.5-7.5x faster (not bitwise identical) #15457
diffusers.models.normalization.RMSNorm with elementwise_affine=False (the default of the LTX-2 transformer blocks, which use it before self-attention, cross-attention and the feed-forward) runs the pure-PyTorch path: pow, mean, rsqrt, a multiply and a cast, which are five or six separate GPU kernels, each reading or writing the whole tensor. torch.nn.functional.rms_norm does the same computation in one kernel.
Per call, RMSNorm(4096, eps=1e-6, elementwise_affine=False) on a [1, 8160, 4096] input (the token count of a 960x544x121 LTX-2 video), CUDA-event timing, torch 2.14.0+rocm10.1.0, median of 5 x 20 calls:
GPU
dtype
current
F.rms_norm
speed-up
Radeon AI PRO R9600D (gfx1201)
bf16
1.704 ms
0.259 ms
6.6x
Radeon AI PRO R9600D (gfx1201)
fp16
1.699 ms
0.258 ms
6.6x
Radeon AI PRO R9600D (gfx1201)
fp32
1.135 ms
0.452 ms
2.5x
Radeon AI PRO R9700 (gfx1201)
bf16
1.701 ms
0.226 ms
7.5x
Radeon AI PRO R9700 (gfx1201)
fp16
1.699 ms
0.228 ms
7.5x
With a batch of 2 (classifier-free guidance) the times double. In an earlier LTX-2.5 960x544x121 run on a Radeon GPU, replacing this one op took the DiT step from 6.81 s to 6.47 s; I have not re-measured that end-to-end number for this issue.
What changes numerically
The output is not bitwise identical to the current implementation. On the [1, 8160, 4096] input above:
dtype
elements that differ
largest difference
bf16
165 of 33,423,360 (0.0005%)
1.56e-2 (one bf16 step at that magnitude)
fp16
1,207 of 33,423,360 (0.0036%)
1.95e-3 (one fp16 step)
fp32
24.7%
9.5e-7
The error against an fp32 reference is the same as before in bf16/fp16 (1.56e-2 and 1.95e-3 on the largest values), and the output dtype is unchanged.
Proposed change
In RMSNorm.forward, call F.rms_norm(hidden_states, hidden_states.shape[-1:], eps=self.eps) when self.weight is None, and leave every other path (affine weight, NPU) as it is. It is a two-line change; I opened a PR with it (linked below).
Questions
Is the non-bitwise difference above acceptable for the elementwise_affine=False path, or do you want it kept bit-exact?
The affine path (weight present) is still the multi-kernel version. I left it alone because its dtype handling differs (the weight dtype decides the cast); should it get the same treatment in a separate PR?
On the no-affine path (self.weight is None), switching to F.rms_norm fits cleanly with the repo constraints: setup.py already pins torch>=2.6, and transformer_krea2.py calls F.rms_norm directly. Even transformer_anyflow.py, which keeps diffusers RMSNorm for legacy checkpoint numerics, leaves elementwise_affine=True. One small note on your PR #15458: RMSNorm.__init__ stores self.dim as a torch.Size, so hidden_states.shape[-1:] matches the existing mean(-1) behavior for 1D dim while self.dim would apply across multi-element dim tuples if any model ever passes one. It is also worth checking test_ltx2_connectors.py, since that test checks a non-affine RMSNorm output against an explicit rsqrt(expected.pow(2).mean(-1, keepdim=True) + 1e-6) formula.
Keeping the affine path separate makes sense once you look at the dtype logic in normalization.py. Right now RMSNorm.forward only casts hidden_states to self.weight.dtype when self.weight.dtype is float16 or bfloat16, and when self.weight is float32 (for example under _keep_in_fp32_modules) it never casts back to input_dtype, so the layer returns float32. Because F.rms_norm requires hidden_states and weight to share the same dtype, casting hidden_states up front changes either the precision of the variance reduction (if hidden_states was float32 and weight is half-precision) or the kernel dtype (if weight is float32), while casting weight down would change the returned dtype.
Thanks for checking. #15458 passes hidden_states.shape[-1:] to F.rms_norm, so it matches the existing mean(-1) for any dim. It leaves the affine path as it is, for the dtype reasons you list.
I had not run tests/pipelines/ltx2/test_ltx2_connectors.py. On the #15458 branch (ce9c5e2) it gives 4 passed; that was a CPU run, since the container had no GPU.
Activity
varien199x commentedon Dec 2, 2026
Hey Sylvan, hope you're well.
On the no-affine path (
self.weight is None), switching toF.rms_normfits cleanly with the repo constraints: setup.py already pinstorch>=2.6, and transformer_krea2.py callsF.rms_normdirectly. Even transformer_anyflow.py, which keeps diffusersRMSNormfor legacy checkpoint numerics, leaveselementwise_affine=True. One small note on your PR #15458:RMSNorm.__init__storesself.dimas atorch.Size, sohidden_states.shape[-1:]matches the existingmean(-1)behavior for 1Ddimwhileself.dimwould apply across multi-elementdimtuples if any model ever passes one. It is also worth checking test_ltx2_connectors.py, since that test checks a non-affineRMSNormoutput against an explicitrsqrt(expected.pow(2).mean(-1, keepdim=True) + 1e-6)formula.Keeping the affine path separate makes sense once you look at the dtype logic in normalization.py. Right now
RMSNorm.forwardonly castshidden_statestoself.weight.dtypewhenself.weight.dtypeisfloat16orbfloat16, and whenself.weightisfloat32(for example under_keep_in_fp32_modules) it never casts back toinput_dtype, so the layer returnsfloat32. BecauseF.rms_normrequireshidden_statesandweightto share the same dtype, castinghidden_statesup front changes either the precision of the variance reduction (ifhidden_stateswasfloat32andweightis half-precision) or the kernel dtype (ifweightisfloat32), while castingweightdown would change the returned dtype.Shan2L commentedon Dec 3, 2026
Thanks for checking. #15458 passes
hidden_states.shape[-1:]toF.rms_norm, so it matches the existingmean(-1)for anydim. It leaves the affine path as it is, for the dtype reasons you list.I had not run
tests/pipelines/ltx2/test_ltx2_connectors.py. On the #15458 branch (ce9c5e2) it gives 4 passed; that was a CPU run, since the container had no GPU.