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.

Fix rowwise FP8 scale axes in fused LoRA backward (#11799)

cherry77-cloud7581d5077263
diff --git a/tests/test_fp8_transposed_weight.py b/tests/test_fp8_transposed_weight.py
new file mode 100644
index 00000000000..54efd358738
--- /dev/null
+++ b/tests/test_fp8_transposed_weight.py
@@ -0,0 +1,68 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team.
+# LoRA backward passes W.t(); for a square W only the strides tell the scales' axis apart.
+import os
+
+import pytest
+
+torch = pytest.importorskip("torch")
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason = "needs CUDA")
+
+
[email protected](scope = "module")
+def F():
+ os.environ.setdefault("UNSLOTH_IS_PRESENT", "1")
+ from unsloth.kernels import fp8
+ return fp8
+
+
+def _rowwise(n):
+ w = torch.randn(n, n, device = "cuda") * torch.linspace(0.01, 1.0, n, device = "cuda")[:, None]
+ s = (w.abs().amax(1, keepdim = True) / 448).float()
+ q = (w / s).to(torch.float8_e4m3fn)
+ return q, s, q.float() * s
+
+
+def _block(n, bs):
+ torch.manual_seed(0)
+ p = -(-n // bs)
+ s = torch.rand(p, p, device = "cuda") * 4 + 0.1
+ full = s.repeat_interleave(bs, 0)[:n].repeat_interleave(bs, 1)[:, :n]
+ q = (torch.randn(n, n, device = "cuda") * 0.02 / full).to(torch.float8_e4m3fn)
+ s.block_size = [bs, bs]
+ return q, s, q.float() * full
+
+
+def _block_rect(n, k, bs):
+ torch.manual_seed(0)
+ s = torch.rand(-(-n // bs), -(-k // bs), device = "cuda") * 4 + 0.1
+ full = s.repeat_interleave(bs, 0)[:n].repeat_interleave(bs, 1)[:, :k]
+ q = (torch.randn(n, k, device = "cuda") * 0.02 / full).to(torch.float8_e4m3fn)
+ s.block_size = [bs, bs]
+ return q, s, q.float() * full
+
+
[email protected]("kind", ["rowwise", "block128", "block64"])
+def test_transposed_view_dequantizes_stored_layout(F, kind):
+ q, s, ref = (
+ _rowwise(256) if kind == "rowwise" else _block(256, 128 if kind == "block128" else 64)
+ )
+ torch.testing.assert_close(F.weight_dequant(q, s, torch.float32), ref, rtol = 1e-2, atol = 1e-4)
+ torch.testing.assert_close(
+ F.weight_dequant(q.t(), s, torch.float32), ref.t(), rtol = 1e-2, atol = 1e-4
+ )
+
+
[email protected]("kind", ["rowwise", "block128", "block64", "block128_rect"])
+def test_fp8_linear_on_transposed_view(F, kind):
+ if kind == "rowwise":
+ q, s, ref = _rowwise(256)
+ elif kind == "block128_rect":
+ q, s, ref = _block_rect(256, 384, 128)
+ else:
+ q, s, ref = _block(256, 128 if kind == "block128" else 64)
+ X = torch.randn(64, q.shape[0], device = "cuda", dtype = torch.bfloat16)
+ y = F.fp8_linear(X, q.t(), s)
+ expect = X.float() @ ref
+ assert ((y.float() - expect).norm() / expect.norm()) < 0.05
diff --git a/unsloth/kernels/fp8.py b/unsloth/kernels/fp8.py
index 7805ee58d0e..75de151a339 100644
--- a/unsloth/kernels/fp8.py
+++ b/unsloth/kernels/fp8.py
@@ -985,6 +985,19 @@ def fp8_linear(
weight_scale,
bias = None,
):
+ # Transposed block view (LoRA_MLP backward's downW.t()): the block kernel re-orients rectangular scale grids
+ # from the shapes, but a square grid (or non-square blocks) is ambiguous, so dequantize the stored layout.
+ if (
+ _is_transposed_view(weight)
+ and weight_scale.ndim == 2
+ and weight_scale.shape[1] > 1
+ and (
+ weight_scale.shape[0] == weight_scale.shape[1]
+ or len(set(getattr(weight_scale, "block_size", None) or [128, 128])) > 1
+ )
+ ):
+ out = torch.matmul(X, weight_dequant(weight, weight_scale, X.dtype).t())
+ return out if bias is None else out + bias
# Per-tensor (scalar scale) or block FP8 (2D scale, multiple columns).
if weight_scale.numel() == 1 or (weight_scale.ndim == 2 and weight_scale.shape[1] > 1):
out = fp8_block_quant_linear(X, weight, weight_scale)