Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Concretize RMSNorm width before the narrow-row self-check (#16228)

Daniel Han7cc7f98525d6
diff --git a/tests/test_rms_layernorm_multirow.py b/tests/test_rms_layernorm_multirow.py
index 3ea786ddb7d..c0955b77db8 100644
--- a/tests/test_rms_layernorm_multirow.py
+++ b/tests/test_rms_layernorm_multirow.py
@@ -152,29 +152,37 @@ class _Norm(torch.nn.Module):
self.variance_epsilon = 1e-6
-def _qk_norm_case(fn, dtype, gemma):
+def _qk_norm_case(
+ fn,
+ dtype,
+ gemma,
+ dim = 128,
+):
g = torch.Generator(device = "cuda").manual_seed(0)
- norm = _Norm(128, dtype)
- X = torch.randn(2, 37, 16, 128, device = "cuda", generator = g).to(dtype).requires_grad_(True)
- dY = torch.randn(2, 37, 16, 128, device = "cuda", generator = g).to(dtype)
+ norm = _Norm(dim, dtype)
+ X = torch.randn(2, 37, 16, dim, device = "cuda", generator = g).to(dtype).requires_grad_(True)
+ dY = torch.randn(2, 37, 16, dim, device = "cuda", generator = g).to(dtype)
Y = fn(norm, X, gemma)
Y.backward(dY)
return Y.detach(), X.grad
@pytest.mark.parametrize("gemma", [False, True], ids = ["llama", "gemma"])
-def test_multirow_compiled_matches_one_row_eager(multirow, gemma):
- """Under torch.compile (fullgraph, no graph break) the bytes match the one-row eager kernel."""
[email protected]("dynamic", [False, True], ids = ["static", "dynamic"])
[email protected]("dim", [64, 128])
+def test_multirow_compiled_matches_one_row_eager(multirow, gemma, dynamic, dim):
if not getattr(torch.library, "triton_op", None):
pytest.skip("needs torch.library.triton_op")
dtype = torch.bfloat16 if rms_layernorm._BF16_TRACEABLE else torch.float16
multirow.setattr(rms_layernorm, "_MULTIROW", False)
- ref = _qk_norm_case(fast_rms_layernorm, dtype, gemma)
+ ref = _qk_norm_case(fast_rms_layernorm, dtype, gemma, dim)
multirow.setattr(rms_layernorm, "_MULTIROW", True)
torch._dynamo.reset()
dynamo_utils.counters.clear()
- compiled = torch.compile(lambda n, x, gm: fast_rms_layernorm(n, x, gm), fullgraph = True)
- got = _qk_norm_case(compiled, dtype, gemma)
+ compiled = torch.compile(
+ lambda n, x, gm: fast_rms_layernorm(n, x, gm), fullgraph = True, dynamic = dynamic
+ )
+ got = _qk_norm_case(compiled, dtype, gemma, dim)
torch._dynamo.reset()
assert sum(dynamo_utils.counters["graph_break"].values()) == 0
assert list(rms_layernorm._MULTIROW_CHECKED.values()) == [True]
diff --git a/tests/test_rms_layernorm_multirow_settings.py b/tests/test_rms_layernorm_multirow_settings.py
index 49d8e386fbb..b3ee104775e 100644
--- a/tests/test_rms_layernorm_multirow_settings.py
+++ b/tests/test_rms_layernorm_multirow_settings.py
@@ -9,6 +9,7 @@ CUDA keeps the 32-lane replay and settings it shipped with; ROCm divides by the
import ast
import functools
import types
+import warnings
from pathlib import Path
import pytest
@@ -134,3 +135,43 @@ def test_multirow_is_no_longer_gated_off_on_hip():
)
assert "version.hip" not in ast.unparse(gate.value)
assert "_triton_at_least(3, 6)" in ast.unparse(gate.value)
+
+
[email protected]("n", [64, 128])
+def test_multirow_self_check_gets_concrete_arguments(n):
+ import torch
+ from torch._dynamo.source import ConstantSource
+ from torch.fx.experimental.symbolic_shapes import ShapeEnv
+
+ env = ShapeEnv()
+ width = env.create_symintnode(env.create_symbol(n, ConstantSource("n_cols")), hint = n)
+ with pytest.raises(TypeError):
+ hash(width)
+ settings, _ = _load(hip = None, lanes = 32)
+ ns = settings.__globals__
+ seen = []
+
+ def self_check(device, dtype, W_dtype, n_cols, eps, gemma, multirow):
+ seen.append((n_cols, multirow))
+ hash((n_cols, multirow))
+ return True
+
+ ns.update(
+ torch = torch,
+ warnings = warnings,
+ _settings = lambda n: (width, 4),
+ _MULTIROW_CHECKED = {},
+ _multirow_self_check = self_check,
+ )
+ names = ("_multirow_disable", "_multirow_checked")
+ exec(compile(ast.Module([FUNCS[name] for name in names], []), str(SOURCE), "exec"), ns)
+ X = types.SimpleNamespace(shape = (8, width), device = torch.device("cpu"), dtype = torch.float32)
+ W = types.SimpleNamespace(dtype = torch.float32)
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ multirow = ns["_multirow_checked"](X, W, 1e-6, False)
+ assert ns["_MULTIROW"]
+ assert len(seen) == 1
+ assert type(seen[0][0]) is int
+ assert all(type(v) is int for v in seen[0][1])
+ assert multirow == seen[0][1] == (n, 4096 // n, n // 32, 4)
diff --git a/unsloth/kernels/rms_layernorm.py b/unsloth/kernels/rms_layernorm.py
index 1d564b3d07f..6e77a3d6112 100644
--- a/unsloth/kernels/rms_layernorm.py
+++ b/unsloth/kernels/rms_layernorm.py
@@ -677,6 +677,8 @@ def _multirow_checked(X, W, eps, gemma):
if verdict is None:
if X.device.type == "cuda" and torch.cuda.is_current_stream_capturing():
return None # check on a later uncaptured call
+ # Real self-check launches need concrete dimensions, even during dynamic tracing.
+ n_cols, multirow = int(n_cols), tuple(int(v) for v in multirow)
try:
verdict = _multirow_self_check(X.device, X.dtype, W.dtype, n_cols, eps, gemma, multirow)
reason = f"self-check mismatch for {key}"