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.

Studio: give each rotated native int8 Linear its own ConvRot buffer (FLUX.3 INT8 on T4 failed its first render) (#17217)

Daniel Han6511d950e9da
diff --git a/studio/backend/core/inference/diffusion_native_quant.py b/studio/backend/core/inference/diffusion_native_quant.py
index d9712b86568..89e07d41ae3 100644
--- a/studio/backend/core/inference/diffusion_native_quant.py
+++ b/studio/backend/core/inference/diffusion_native_quant.py
@@ -270,6 +270,11 @@ def int8_fused_disabled() -> bool:
return os.environ.get(NATIVE_INT8_FUSED_ENV, "").strip().lower() in ("0", "false", "no", "off")
+def _own_rotation(h: Any, dtype: Any) -> Any:
+ """Per-layer copy: at float32 ``h.to(dtype)`` aliases the cached tensor, so group offload moved one shared buffer."""
+ return h.to(dtype, copy = True)
+
+
def _w8a8_rows(x2: Any, rot_h: Any, weight_q: Any, weight_scale: Any, bias: Any) -> Any:
"""Eager ``_rotate`` + ``_int_mm_rows`` maths on tensors only, so offload hooks never invalidate the graph."""
import torch
@@ -529,7 +534,9 @@ def native_linear_class():
h = build_convrot_hadamard(
self.rot_group, device = codes.device, dtype = torch.float32
)
- self.register_buffer("rot_h", h.to(self.compute_dtype), persistent = False)
+ self.register_buffer(
+ "rot_h", _own_rotation(h, self.compute_dtype), persistent = False
+ )
else:
weight = linear.weight.detach()
with torch.no_grad():
@@ -544,7 +551,9 @@ def native_linear_class():
w = (w.reshape(self.out_features, -1, g) @ h.T).reshape(
self.out_features, -1
)
- self.register_buffer("rot_h", h.to(self.compute_dtype), persistent = False)
+ self.register_buffer(
+ "rot_h", _own_rotation(h, self.compute_dtype), persistent = False
+ )
scale = w.abs().amax(dim = 1, keepdim = True).clamp(min = 1e-12) / _QMAX[scheme]
if scheme == NATIVE_INT8 and scale_dtype is not None:
scale = snap_scale_up(scale, scale_dtype)
diff --git a/studio/backend/tests/test_diffusion_native_int8_rot_per_layer.py b/studio/backend/tests/test_diffusion_native_int8_rot_per_layer.py
new file mode 100644
index 00000000000..dfc6458fca3
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_native_int8_rot_per_layer.py
@@ -0,0 +1,57 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Each rotated int8 Linear owns its ``rot_h``; a shared float32 alias broke group offload ("mat2 is on cpu")."""
+
+from __future__ import annotations
+
+import pytest
+
+torch = pytest.importorskip("torch")
+
+import core.inference.diffusion_native_quant as nq # noqa: E402
+from core.inference.diffusion_convrot import build_convrot_hadamard # noqa: E402
+
+
[email protected]("dtype", [torch.float32, torch.float16, torch.bfloat16])
[email protected]("from_codes", [False, True])
+def test_each_layer_owns_its_rotation(dtype, from_codes):
+ cls = nq.native_linear_class()
+ torch.manual_seed(0)
+ layers = []
+ for _ in range(3):
+ lin = torch.nn.Linear(512, 64).to(dtype)
+ if from_codes:
+ codes = torch.randint(-127, 128, (64, 512), dtype = torch.int8)
+ scale = torch.rand(64) + 0.01
+ layers.append(cls(lin, "int8", act_int8 = True, rot_group = 256, codes = codes, scale = scale))
+ else:
+ layers.append(cls(lin, "int8", act_int8 = True, rot_group = 256))
+ cached = build_convrot_hadamard(256, device = "cpu", dtype = torch.float32)
+ ptrs = {layer.rot_h.data_ptr() for layer in layers}
+ assert len(ptrs) == len(layers) and cached.data_ptr() not in ptrs
+ for layer in layers:
+ assert layer.rot_h.dtype == layer.compute_dtype
+ assert torch.equal(layer.rot_h.float(), cached.to(layer.compute_dtype).float())
+ assert "rot_h" not in layer.state_dict()
+
+
+def test_repointing_one_layers_buffer_leaves_the_others_alone():
+ cls = nq.native_linear_class()
+ a = cls(torch.nn.Linear(512, 64), "int8", act_int8 = True, rot_group = 256)
+ b = cls(torch.nn.Linear(512, 64), "int8", act_int8 = True, rot_group = 256)
+ before = b.rot_h.data_ptr()
+ a.rot_h.data = torch.zeros_like(a.rot_h)
+ assert b.rot_h.data_ptr() == before and b.rot_h.abs().sum() > 0
+ x = torch.randn(4, 512)
+ with torch.no_grad():
+ ref = torch.nn.functional.linear(x, b.dequantized_weight(torch.float32), b.bias)
+ got = b(x)
+ assert ((got - ref).norm() / ref.norm()).item() < 0.05
+
+
+def test_the_cached_hadamard_is_never_handed_out_at_float32():
+ cached = build_convrot_hadamard(256, device = "cpu", dtype = torch.float32)
+ assert cached.to(torch.float32) is cached
+ own = nq._own_rotation(cached, torch.float32)
+ assert own is not cached and own.data_ptr() != cached.data_ptr() and torch.equal(own, cached)