Repository navigation
Conversation
Signed-off-by: Sylvan Liu <Sylvan.Liu@amd.com>
|
Thanks @sayakpaul. CUDA: Other RMS norms: there are 8 model-specific RMSNorm classes under |
Which snippet are you referring to? |
|
Sorry, that was wrong: #15457 has no snippet. Here is a standalone one that compares the import torch
import torch.nn.functional as F
def old_no_affine(x, eps=1e-6):
# RMSNorm.forward on main when weight is None
variance = x.to(torch.float32).pow(2).mean(-1, keepdim=True)
return (x * torch.rsqrt(variance + eps)).to(x.dtype)
def new_no_affine(x, eps=1e-6):
# this PR
return F.rms_norm(x, x.shape[-1:], eps=eps)
def bench(fn, x, iters=50):
for _ in range(5):
fn(x)
torch.cuda.synchronize()
start, end = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
start.record()
for _ in range(iters):
fn(x)
end.record()
torch.cuda.synchronize()
return start.elapsed_time(end) / iters
print(torch.__version__, torch.cuda.get_device_name(0))
for dtype in (torch.bfloat16, torch.float16, torch.float32):
x = torch.randn(1, 8160, 4096, device="cuda", dtype=dtype) # LTX-2 960x544x121 video tokens
old, new = old_no_affine(x), new_no_affine(x)
print(
f"{dtype}: old {bench(old_no_affine, x):.3f} ms, new {bench(new_no_affine, x):.3f} ms, "
f"max |new - old| {(new.float() - old.float()).abs().max().item():.3e}"
)Output on a Radeon AI PRO R9700, torch 2.14.0+rocm10.1.0: The bf16/fp16 differences are one step of the output dtype at the largest values, as in the description. |
|
Do you have a suggestion on which NVIDIA card I should try it out on? |
|
Any CUDA card you have handy works; the input is 1 x 8160 x 4096, so under 1 GB of memory. The op is memory-bound, so one datacenter card (A100 or H100) and, if you have one, a consumer card (for example an RTX 4090) would cover the range. Please include the torch version in the output: I have not checked which release first dispatches |
|
Thanks for running it, @sayakpaul. A100 and L40S show the same picture as the R9700: about 8x for bf16 and fp16, about 2x for fp32, and the same max differences. |

What does this PR do?
Fixes #15457.
RMSNorm(elementwise_affine=False)ranpow,mean,rsqrt, a multiply and a cast as separate kernels. When there is no weight,forwardnow callsF.rms_norm, which does the same computation in one kernel. The affine path and the NPU path are unchanged.On a
[1, 8160, 4096]input (960x544x121 LTX-2 video tokens), per call, torch 2.14.0+rocm10.1.0:The output is not bitwise identical: for bf16, 165 of 33,423,360 elements differ, by one bf16 step at most (fp16: 1,207 elements, one fp16 step; fp32: 24.7% of elements, at most 9.5e-7). The error against an fp32 reference on the largest values is unchanged in bf16 and fp16. The output dtype is unchanged.
Tests:
RMSNormTestsintests/models/test_layers_utils.py: the no-affine output matches the explicit fp32 formula in fp32, fp16 and bf16 and keeps the input dtype. It fails ifepsis changed in the new call.tests/models/transformers/test_models_transformer_ltx2.py(includes thetorch.compiletests) andtests/models/test_layers_utils.py, torch 2.14.0+rocm10.1.0: onmain(facc812ea, Radeon AI PRO R9600D) no failure or error; with this change (Radeon AI PRO R9700) the same results plus the new test, also no failure or error.tests/pipelines/ltx2/test_ltx2.pywas not run to completion. Onmainand on this branch the first 71 results are identical, including two errors at the same position, and the file takes more than an hour on this setup, so I stopped both runs.Self-review notes (diffusers
self-reviewskill).ai/references/review-rules.md, read against the whole diff (two source lines and one test class).utils/check_copies.pypasses (no# Copied fromcopy ofRMSNormexists); ruff 0.17.5 reports no issue on the changed files (the repo pins 0.9.10, which I did not install).F.rms_normis used only where the previous code already took the pure-PyTorch route.Before submitting
self-reviewskill on the diff?Who can review?
@yiyixuxu @sayakpaul