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: fused int8 GEMM with the dequant epilogue, bf16 out (Qwen-Image-2.1 14-17% faster per step) (#12448)

Daniel Han66453bb8a531
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index a51c19b2d16..73976163611 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -163,6 +163,7 @@ from .diffusion_speed import (
fp16_compile_explicit_only,
fp16_unet_offloaded,
fresh_compile_count,
+ int8_gemm_live,
normalize_speed_mode,
resolve_speed_mode,
restore_backend_flags,
@@ -9512,6 +9513,7 @@ class DiffusionBackend:
resolved, speed_optims = cuda_graph.live_status(
state.resolved, state.speed_optims, getattr(state, "cuda_graphs", ())
)
+ speed_optims = int8_gemm_live(state.pipe, speed_optims)
return {
"loaded": True,
"repo_id": state.repo_id,
diff --git a/studio/backend/core/inference/diffusion_int8_fused.py b/studio/backend/core/inference/diffusion_int8_fused.py
index faeb8d67151..9d1fa301edf 100644
--- a/studio/backend/core/inference/diffusion_int8_fused.py
+++ b/studio/backend/core/inference/diffusion_int8_fused.py
@@ -553,6 +553,11 @@ def _linear_from_q(q: Any, xs: Any, weight: Any, bias: Any, out_dtype: Any) -> A
"""torchao Int8Tensor linear on an already-quantized activation: same ops, same order, as the stock epilogue."""
import torch
+ if out_dtype == torch.bfloat16:
+ from .diffusion_int8_gemm import linear_from_q
+ fused = linear_from_q(q, xs.reshape(-1).to(torch.float32), weight, bias)
+ if fused is not None:
+ return fused
y = (_int_mm(q, weight) * xs.reshape(-1, 1).to(torch.float32)).to(out_dtype)
y = y * weight.scale.flatten()
if bias is not None:
diff --git a/studio/backend/core/inference/diffusion_int8_gemm.py b/studio/backend/core/inference/diffusion_int8_gemm.py
new file mode 100644
index 00000000000..4cb6b1bbb1a
--- /dev/null
+++ b/studio/backend/core/inference/diffusion_int8_gemm.py
@@ -0,0 +1,636 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""int8 x int8 GEMM with torchao's dequant epilogue in the kernel: bf16 out instead of an int32 ``_int_mm`` output.
+
+The stock torchao W8A8 linear writes the int32 accumulator to HBM (4 bytes per output element) and the next compiled
+kernel reads it back to apply ``(c * x_scale) * w_scale``. Here one Triton kernel does the GEMM and the epilogue and
+writes bf16, so every int8 Linear writes half the bytes and its consumer (norm, RoPE, SwiGLU, act quant) reads half.
+
+NUMERICS: the Linear output is bit-identical to torchao's epilogue, eager and compiled under Studio's
+``emulate_precision_casts``: ``bf16(bf16(r(c) * xs) * ws)`` (+ bias as a bf16 add), where ``r`` rounds the int32
+accumulator to bf16 when the activation scale is bf16 (int32 * bf16 promotes to bf16) and is exact when it is fp32.
+The activation quant is torchao's own math, so Inductor still fuses it into the producer. A compiled block is NOT
+bit-identical end to end: the consumer reductions (LayerNorm / RMSNorm Welford) that used to fuse the epilogue now
+read bf16 and Inductor re-tiles them, a rounding-order change smaller than compiled-vs-eager (LPIPS-gated).
+
+Per-arch gate: sm80 / sm89 / sm120 on (measured), everything else stock. sm75: Triton cannot lower the int8 dot;
+sm100: Triton int8 is slower than cuBLAS. ROCm (weight-only int8 there) and CPU never reach it.
+
+Kill switch: ``UNSLOTH_DIFFUSION_INT8_GEMM=0``; ``=1`` also enables it on an unmeasured arch (still probe-gated).
+"""
+
+from __future__ import annotations
+
+import os
+import threading
+import types
+from functools import lru_cache
+from typing import Any, Optional
+
+INT8_GEMM_ENV = "UNSLOTH_DIFFUSION_INT8_GEMM"
+_MIN_TRITON = (3, 2)
+# torch._int_mm needs M > 16; below that the stock path (safe_int_mm padding) stays in charge.
+_MIN_ROWS = 17
+_K_ALIGN = 64
+_N_ALIGN = 16
+_OP_NAMESPACE = "unsloth_studio"
+_OP_NAME = "int8_mm_dequant"
+_REC = "_unsloth_i8_gemm"
+_MARK = "_unsloth_i8_gemm_prev"
+_NO_PREV = object()
+_LOCK = threading.Lock()
+_OP_HANDLE: Any = None
+# Engagement census; never read inside a traced region.
+_CALLS = [0]
+
+# (BLOCK_M, BLOCK_N, BLOCK_K, GROUP_M, num_warps, num_stages) per (major, minor), tuned on DiT shapes. Absent = off.
+_ARCH_CONFIG = {
+ (8, 0): (
+ 128,
+ 128,
+ 128,
+ 8,
+ 4,
+ 3,
+ ), # A100
+ (8, 9): (256, 128, 128, 8, 8, 3), # L4
+ (12, 0): (128, 128, 64, 8, 4, 4), # RTX PRO 6000
+}
+# When the arch tile does not fit this part's shared memory.
+_FALLBACK_CONFIG = (128, 128, 64, 8, 4, 4)
+
+
+def int8_gemm_mode() -> str:
+ """'off', 'force' or 'auto' from the environment."""
+ raw = (os.environ.get(INT8_GEMM_ENV) or "").strip().lower()
+ if raw in ("0", "off", "false", "no"):
+ return "off"
+ if raw in ("1", "on", "true", "yes", "force"):
+ return "force"
+ return "auto"
+
+
+def _triton_version_ok(version: Optional[str] = None) -> bool:
+ if version is None:
+ try:
+ import triton
+ version = str(triton.__version__)
+ except Exception: # noqa: BLE001
+ return False
+ import re
+
+ match = re.match(r"(\d+)\.(\d+)", version)
+ return bool(match) and (int(match.group(1)), int(match.group(2))) >= _MIN_TRITON
+
+
+def arch_config(capability: Optional[tuple], mode: Optional[str] = None) -> Optional[tuple]:
+ """The tile for this compute capability, or None where the lever stays off."""
+ mode = int8_gemm_mode() if mode is None else mode
+ if mode == "off" or capability is None:
+ return None
+ cap = (int(capability[0]), int(capability[1]))
+ cfg = _ARCH_CONFIG.get(cap)
+ if cfg is None and mode == "force" and cap >= (8, 0):
+ return _FALLBACK_CONFIG
+ return cfg
+
+
+@lru_cache(maxsize = 1)
+def _kernels() -> Optional[types.SimpleNamespace]:
+ try:
+ import triton
+ import triton.language as tl
+ except Exception: # noqa: BLE001 - no Triton means the stock path
+ return None
+ globals().update(triton = triton, tl = tl)
+
+ @triton.jit
+ def _rbf16(x):
+ return x.to(tl.bfloat16).to(tl.float32)
+
+ @triton.jit
+ def i8mm_dq(
+ a_ptr,
+ w_ptr,
+ xs_ptr,
+ ws_ptr,
+ b_ptr,
+ c_ptr,
+ M,
+ N,
+ K,
+ stride_am,
+ stride_wn,
+ stride_cm,
+ HAS_BIAS: tl.constexpr,
+ XS_FP32: tl.constexpr,
+ WS_FP32: tl.constexpr,
+ EVEN_K: tl.constexpr,
+ BLOCK_M: tl.constexpr,
+ BLOCK_N: tl.constexpr,
+ BLOCK_K: tl.constexpr,
+ GROUP_M: tl.constexpr,
+ ):
+ pid = tl.program_id(0)
+ num_pid_m = tl.cdiv(M, BLOCK_M)
+ num_pid_n = tl.cdiv(N, BLOCK_N)
+ num_pid_in_group = GROUP_M * num_pid_n
+ group_id = pid // num_pid_in_group
+ first_pid_m = group_id * GROUP_M
+ group_size_m = min(num_pid_m - first_pid_m, GROUP_M)
+ pid_m = first_pid_m + ((pid % num_pid_in_group) % group_size_m)
+ pid_n = (pid % num_pid_in_group) // group_size_m
+
+ # int64 row / column offsets: M * K overflows int32 on large video token grids.
+ offs_m = pid_m.to(tl.int64) * BLOCK_M + tl.arange(0, BLOCK_M)
+ offs_n = pid_n.to(tl.int64) * BLOCK_N + tl.arange(0, BLOCK_N)
+ rm = tl.max_contiguous(tl.multiple_of(offs_m % M, BLOCK_M), BLOCK_M)
+ rn = tl.max_contiguous(tl.multiple_of(offs_n % N, BLOCK_N), BLOCK_N)
+ offs_k = tl.arange(0, BLOCK_K)
+ a_ptrs = a_ptr + rm[:, None] * stride_am + offs_k[None, :]
+ w_ptrs = w_ptr + rn[None, :] * stride_wn + offs_k[:, None]
+ acc = tl.zeros((BLOCK_M, BLOCK_N), dtype = tl.int32)
+ for k in range(0, tl.cdiv(K, BLOCK_K)):
+ if EVEN_K:
+ a = tl.load(a_ptrs)
+ b = tl.load(w_ptrs)
+ else:
+ kmask = offs_k < K - k * BLOCK_K
+ a = tl.load(a_ptrs, mask = kmask[None, :], other = 0)
+ b = tl.load(w_ptrs, mask = kmask[:, None], other = 0)
+ acc = tl.dot(a, b, acc, out_dtype = tl.int32)
+ a_ptrs += BLOCK_K
+ w_ptrs += BLOCK_K
+
+ # torchao epilogue, every rounding kept: (int32 * xs) -> xs dtype, * ws -> ws dtype, -> bf16, + bias.
+ xs = tl.load(xs_ptr + rm).to(tl.float32)
+ ws = tl.load(ws_ptr + rn).to(tl.float32)
+ y = acc.to(tl.float32)
+ if not XS_FP32:
+ y = _rbf16(y)
+ y = _rbf16(y * xs[:, None])
+ y = y * ws[None, :]
+ if not WS_FP32:
+ y = _rbf16(y)
+ if HAS_BIAS:
+ y = y + tl.load(b_ptr + rn).to(tl.float32)[None, :]
+ c_ptrs = c_ptr + offs_m[:, None] * stride_cm + offs_n[None, :]
+ mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
+ tl.store(c_ptrs, y.to(tl.bfloat16), mask = mask)
+
+ return types.SimpleNamespace(i8mm_dq = i8mm_dq)
+
+
+# device index -> probed tile (None = stock).
+_DEVICE_CFG: dict = {}
+
+
+def reference(a: Any, w: Any, xs: Any, ws: Any, bias: Any) -> Any:
+ """Eager torchao epilogue, same ops and order as the stock linear."""
+ import torch
+
+ m = a.shape[0]
+ if m < _MIN_ROWS: # _int_mm's row floor: pad, then drop the pad rows (exact)
+ a = torch.cat([a, a.new_zeros((_MIN_ROWS - m, a.shape[1]))])
+ c = torch._int_mm(a, w.t())[:m]
+ y = (c * xs.reshape(-1, 1)).to(torch.bfloat16)
+ y = y * ws.reshape(
+ -1
+ ) # bf16 scales: a bf16 product; fp32 (prequant) scales: fp32 until after the bias (v2)
+ if bias is not None:
+ y = y + bias
+ return y.to(torch.bfloat16)
+
+
+def _launch(a: Any, w: Any, xs: Any, ws: Any, bias: Any, cfg: tuple) -> Any:
+ import torch
+
+ k = _kernels()
+ m, kk = a.shape
+ n = w.shape[0]
+ bm, bn, bk, gm, warps, stages = cfg
+ out = torch.empty((m, n), device = a.device, dtype = torch.bfloat16)
+ grid = (triton.cdiv(m, bm) * triton.cdiv(n, bn),)
+ with torch.cuda.device(a.device):
+ k.i8mm_dq[grid](
+ a,
+ w,
+ xs,
+ ws,
+ bias if bias is not None else ws,
+ out,
+ m,
+ n,
+ kk,
+ a.stride(0),
+ w.stride(0),
+ out.stride(0),
+ HAS_BIAS = bias is not None,
+ XS_FP32 = xs.dtype == torch.float32,
+ WS_FP32 = ws.dtype == torch.float32,
+ EVEN_K = kk % bk == 0,
+ BLOCK_M = bm,
+ BLOCK_N = bn,
+ BLOCK_K = bk,
+ GROUP_M = gm,
+ num_warps = warps,
+ num_stages = stages,
+ enable_fp_fusion = False,
+ )
+ return out
+
+
+def _run(a: Any, w: Any, xs: Any, ws: Any, bias: Any) -> Any:
+ """Op body: the probed tile for this device, the stock epilogue if the launch fails."""
+ _CALLS[0] += 1
+ a = a if a.stride(-1) == 1 else a.contiguous()
+ cfg = _DEVICE_CFG.get(a.device.index)
+ if cfg is not None:
+ try:
+ return _launch(a, w, xs, ws, bias, cfg)
+ except Exception: # noqa: BLE001 - a failed launch keeps the stock math
+ _DEVICE_CFG[a.device.index] = None
+ return reference(a, w, xs, ws, bias)
+
+
+@lru_cache(maxsize = 1)
+def _op() -> Any:
+ """``unsloth_studio::int8_mm_dequant`` (opaque to Inductor; no host sync, allocation only), or None."""
+ try:
+ import torch
+ except Exception: # noqa: BLE001
+ return None
+ ns = getattr(torch.ops, _OP_NAMESPACE, None)
+ if ns is not None and hasattr(ns, _OP_NAME):
+ return getattr(ns, _OP_NAME)
+ custom_op = getattr(getattr(torch, "library", None), "custom_op", None)
+ if custom_op is None: # torch < 2.4
+ return None
+ try:
+
+ @custom_op(
+ f"{_OP_NAMESPACE}::{_OP_NAME}",
+ mutates_args = (),
+ schema = "(Tensor a, Tensor w, Tensor xs, Tensor ws, Tensor? bias) -> Tensor",
+ )
+ def _int8_mm_dequant(a, w, xs, ws, bias):
+ return _run(a, w, xs, ws, bias)
+
+ @_int8_mm_dequant.register_fake
+ def _(a, w, xs, ws, bias):
+ return a.new_empty((a.shape[0], w.shape[0]), dtype = torch.bfloat16)
+ except Exception: # noqa: BLE001 - a registration failure keeps the stock path
+ return None
+ return getattr(torch.ops, _OP_NAMESPACE).int8_mm_dequant
+
+
+def device_config(index: int) -> Optional[tuple]:
+ """Probe once per device: arch gate, Triton, op registration, then a launch that must match the eager torchao
+ epilogue bit for bit (bf16 and fp32 activation scales, bias, ragged M / N / K). None keeps the stock path."""
+ if index in _DEVICE_CFG:
+ return _DEVICE_CFG[index]
+ cfg = None
+ try:
+ import torch
+ if (
+ torch.cuda.is_available()
+ and not getattr(torch.version, "hip", None)
+ and _triton_version_ok()
+ and _kernels() is not None
+ and _op() is not None
+ ):
+ want = arch_config(torch.cuda.get_device_capability(index))
+ for cand in (want, _FALLBACK_CONFIG) if want is not None else ():
+ if cand is not None and _probe(index, cand):
+ cfg = cand
+ break
+ except Exception: # noqa: BLE001
+ cfg = None
+ _DEVICE_CFG[index] = cfg
+ return cfg
+
+
+def _probe(index: int, cfg: tuple) -> bool:
+ import torch
+
+ dev = torch.device("cuda", index)
+ g = torch.Generator(device = "cpu").manual_seed(0)
+ try:
+ for m, n, k, bias, xs32 in (
+ (257, 384, 512, False, False),
+ (33, 200, 136, True, True),
+ (300, 520, 1000, True, False),
+ ):
+ a = torch.randint(-127, 128, (m, k), generator = g, dtype = torch.int8).to(dev)
+ w = torch.randint(-127, 128, (n, k), generator = g, dtype = torch.int8).to(dev)
+ xs = (torch.rand(m, generator = g) * 0.02 + 1e-4).to(torch.bfloat16)
+ xs = (xs.float() if xs32 else xs).to(dev)
+ ws = (torch.rand(n, generator = g) * 0.002 + 1e-5).to(torch.bfloat16).to(dev)
+ ws = (
+ ws.float() if xs32 else ws
+ ) # fp32 weight scales + bias: the prequant (v2) rounding order
+ b = (torch.randn(n, generator = g) * 0.1).to(torch.bfloat16).to(dev) if bias else None
+ if not torch.equal(_launch(a, w, xs, ws, b, cfg), reference(a, w, xs, ws, b)):
+ return False
+ torch.cuda.synchronize(dev)
+ return True
+ except Exception: # noqa: BLE001 - out of shared memory, compile failure, ...
+ return False
+
+
+def _v1_parts(w: Any) -> Optional[tuple]:
+ """(int8 [N, K], scale [N]) of a torchao v1 ``LinearActivationQuantizedTensor`` over a plain-layout symmetric
+ per-channel ``AffineQuantizedTensor`` with the per-token reduced-range activation quant, else None."""
+ try:
+ if type(w).__name__ != "LinearActivationQuantizedTensor":
+ return None
+ fn = getattr(w, "input_quant_func", None)
+ if getattr(fn, "__name__", "") != "_int8_symm_per_token_reduced_range_quant":
+ return None
+ if getattr(w, "quant_kwargs", None):
+ return None
+ aqt = w.original_weight_tensor
+ if type(aqt).__name__ != "AffineQuantizedTensor":
+ return None
+ impl = aqt.tensor_impl
+ if (
+ type(impl).__name__ != "PlainAQTTensorImpl"
+ or type(getattr(impl, "_layout", None)).__name__ != "PlainLayout"
+ ):
+ return None
+ data, scale, zp = impl.int_data, impl.scale, getattr(impl, "zero_point", None)
+ import torch
+
+ if data.dtype != torch.int8 or data.dim() != 2 or scale.numel() != data.shape[0]:
+ return None
+ if zp is not None and bool((zp != 0).any()):
+ return None
+ if tuple(getattr(aqt, "block_size", ())) != (1, data.shape[1]):
+ return None
+ return data, scale.reshape(-1)
+ except Exception: # noqa: BLE001
+ return None
+
+
+def _v2_parts(w: Any, check_zero_point: bool = True) -> Optional[tuple]:
+ """(int8 [N, K], scale [N]) of a plain dynamic symmetric per-row torchao ``Int8Tensor``, else None.
+ ``check_zero_point=False`` inside a traced forward: the check is a host sync and a graph break."""
+ try:
+ from .diffusion_int8_fused import _plain_int8_weight
+
+ if not _plain_int8_weight(w):
+ return None
+ if (
+ check_zero_point
+ and getattr(w, "zero_point", None) is not None
+ and bool((w.zero_point != 0).any())
+ ):
+ return None
+ return w.qdata, w.scale.reshape(-1)
+ except Exception: # noqa: BLE001
+ return None
+
+
+def _act_quant_v1(x2d: Any) -> tuple:
+ """torchao 0.17's ``_int8_symm_per_token_reduced_range_quant`` minus the tensor-subclass wrapper."""
+ import torch
+ from torchao.quantization.quant_primitives import (
+ MappingType,
+ choose_qparams_affine,
+ quantize_affine,
+ )
+
+ block = (1, x2d.shape[-1])
+ scale, zero_point = choose_qparams_affine(
+ x2d,
+ MappingType.SYMMETRIC,
+ block,
+ torch.int8,
+ -127,
+ 127,
+ 1e-5,
+ torch.float32 if x2d.dtype == torch.float16 else None,
+ None,
+ )
+ q = quantize_affine(x2d, block, scale, zero_point, torch.int8, -127, 127)
+ return q, scale
+
+
+def _act_quant_v2(x2d: Any, weight: Any) -> tuple:
+ from .diffusion_int8_fused import _act_quant
+ return _act_quant(x2d, weight)
+
+
+@lru_cache(maxsize = 4)
+def _v1_act_quant_matches(index: int) -> bool:
+ """The subclass-free v1 activation quant must equal torchao's own, bit for bit, on this torchao."""
+ try:
+ import torch
+ from torchao.quantization.quant_api import _int8_symm_per_token_reduced_range_quant
+
+ g = torch.Generator(device = "cpu").manual_seed(1)
+ x = (torch.randn(67, 256, generator = g) * 3).to(torch.bfloat16)
+ x[5] = 0
+ x[9, :7] *= 300
+ x = x.to(torch.device("cuda", index))
+ ref = _int8_symm_per_token_reduced_range_quant(x)
+ q, s = _act_quant_v1(x)
+ impl = ref.tensor_impl
+ return bool(
+ torch.equal(q, impl.int_data) and torch.equal(s.reshape(-1), impl.scale.reshape(-1))
+ )
+ except Exception: # noqa: BLE001
+ return False
+
+
+def _linear_forward(self: Any, x: Any) -> Any:
+ """``nn.Linear.forward`` for a torchao int8 dynamic weight: torchao's activation quant, then the fused GEMM."""
+ import torch
+
+ rec = self.__dict__.get(_REC)
+ if rec is None or x.dtype != torch.bfloat16 or not x.is_cuda:
+ return type(self).forward(self, x)
+ kind, _wq, _ws, weight = rec
+ if self.weight is not weight: # weight replaced since install (reload / LoRA bake): stock
+ return type(self).forward(self, x)
+ # Payload off the live parameter, never a cached alias: a moved weight must not leave a stale device or pin a copy.
+ if kind == "v1":
+ impl = weight.original_weight_tensor.tensor_impl
+ wq, ws = impl.int_data, impl.scale.reshape(-1)
+ else:
+ wq, ws = weight.qdata, weight.scale.reshape(-1)
+ if wq.device != x.device:
+ return type(self).forward(self, x)
+ lead = x.shape[:-1]
+ x2d = x.reshape(-1, x.shape[-1])
+ if x2d.shape[0] < _MIN_ROWS:
+ return type(self).forward(self, x)
+ if kind == "v1":
+ xq, xs = _act_quant_v1(x2d)
+ else:
+ xq, xs = _act_quant_v2(x2d, weight)
+ y = _OP_HANDLE(xq, wq, xs.reshape(-1), ws, self.bias)
+ return y.reshape(*lead, y.shape[-1])
+
+
+def linear_from_q(q: Any, xs: Any, weight: Any, bias: Any) -> Optional[Any]:
+ """For ``diffusion_int8_fused``: the down projection on an already-quantized activation, or None (stock)."""
+ import torch
+
+ if _OP_HANDLE is None or int8_gemm_mode() == "off" or q.device.index is None:
+ return None
+ if _DEVICE_CFG.get(q.device.index) is None:
+ return None
+ # The fused MLP already admitted this weight (symmetric, and its stock epilogue ignores the zero point too).
+ parts = _v2_parts(weight, check_zero_point = False)
+ if (
+ parts is None
+ or q.shape[0] < _MIN_ROWS
+ or parts[1].dtype not in (torch.bfloat16, torch.float32)
+ ):
+ return None
+ if parts[0].shape[1] % _K_ALIGN or parts[0].shape[0] % _N_ALIGN:
+ return None
+ if bias is not None and bias.dtype != torch.bfloat16:
+ return None
+ return _OP_HANDLE(q, parts[0], xs.reshape(-1), parts[1], bias)
+
+
+def _eligible(module: Any) -> Optional[tuple]:
+ from torch import nn
+
+ if type(module) is not nn.Linear: # a subclass (ConvRotLinear) transforms the input first
+ return None
+ w = module.weight
+ bias = module.bias
+ import torch
+
+ if bias is not None and bias.dtype != torch.bfloat16:
+ return None
+ # v1: bf16 scales only; v2 also fp32 (prequant checkpoints). Off-grid K runs masked loads, far slower than cuBLAS.
+ out_f, in_f = getattr(module, "out_features", 0), getattr(module, "in_features", 0)
+ if in_f % _K_ALIGN or out_f % _N_ALIGN:
+ return None
+ parts = _v1_parts(w)
+ if parts is not None and parts[1].dtype == torch.bfloat16:
+ return ("v1",) + parts + (w,)
+ parts = _v2_parts(w)
+ if parts is not None and parts[1].dtype in (torch.bfloat16, torch.float32):
+ return ("v2",) + parts + (w,)
+ return None
+
+
+def candidates(transformer: Any) -> int:
+ try:
+ return sum(1 for m in transformer.modules() if _eligible(m) is not None)
+ except Exception: # noqa: BLE001
+ return 0
+
+
+def install(
+ transformer: Any,
+ logger: Any = None,
+ offload_active: bool = False,
+) -> int:
+ """Idempotent; returns the (candidate) count. Must run before the first compiled forward."""
+ if int8_gemm_mode() == "off" or transformer is None or offload_active:
+ return 0
+ from .diffusion_int8_fused import resident_cuda_device, run_on_first_call
+
+ if resident_cuda_device(transformer) is not None:
+ return _finalize(transformer, logger)
+ # Weights not on the GPU yet: arch gate now (status never claims a stock arch), probe + swap at the first forward.
+ try:
+ import torch
+
+ if not torch.cuda.is_available() or getattr(torch.version, "hip", None):
+ return 0
+ if not _triton_version_ok() or _kernels() is None:
+ return 0
+ if arch_config(torch.cuda.get_device_capability(torch.cuda.current_device())) is None:
+ return 0
+ except Exception: # noqa: BLE001
+ return 0
+ n = candidates(transformer)
+ if n:
+ run_on_first_call(transformer, "int8_gemm", lambda t: _finalize(t, logger))
+ return n
+
+
+def _finalize(transformer: Any, logger: Any = None) -> int:
+ count = _swap(transformer, logger)
+ try:
+ transformer._unsloth_int8_gemm = count # the deferred install recorded the candidate count
+ except Exception: # noqa: BLE001
+ pass
+ return count
+
+
+def _swap(transformer: Any, logger: Any = None) -> int:
+ global _OP_HANDLE
+ from .diffusion_int8_fused import resident_cuda_device
+
+ dev = resident_cuda_device(transformer)
+ if dev is None:
+ return 0
+ import torch
+
+ index = dev.index if dev.index is not None else torch.cuda.current_device()
+ recs = [(m, _eligible(m)) for m in transformer.modules()]
+ recs = [(m, r) for m, r in recs if r is not None]
+ if not recs or device_config(index) is None:
+ return 0
+ if any(r[0] == "v1" for _, r in recs) and not _v1_act_quant_matches(index):
+ recs = [(m, r) for m, r in recs if r[0] != "v1"]
+ count = 0
+ with _LOCK:
+ _OP_HANDLE = _op()
+ if _OP_HANDLE is None:
+ return 0
+ for module, rec in recs:
+ if _MARK in module.__dict__:
+ count += 1
+ continue
+ module.__dict__[_REC] = (
+ rec[0],
+ None,
+ None,
+ rec[3],
+ ) # kind + the Parameter, no payload alias
+ module.__dict__[_MARK] = module.__dict__.get("forward", _NO_PREV)
+ module.forward = types.MethodType(_linear_forward, module)
+ count += 1
+ if logger is not None and count:
+ logger.info(
+ "diffusion.int8_gemm: %d int8 Linear(s) run the fused-dequant GEMM (bf16 out) on sm_%d%d",
+ count,
+ *torch.cuda.get_device_capability(index),
+ )
+ return count
+
+
+def uninstall(transformer: Any = None) -> None:
+ if transformer is None:
+ return
+ from .diffusion_int8_fused import cancel_first_call
+
+ cancel_first_call(transformer, "int8_gemm")
+ with _LOCK:
+ for module in transformer.modules():
+ prev = module.__dict__.pop(_MARK, None)
+ if prev is None:
+ continue
+ module.__dict__.pop(_REC, None)
+ if prev is _NO_PREV:
+ module.__dict__.pop("forward", None)
+ else:
+ module.forward = prev
+
+
+def is_installed(module: Any) -> bool:
+ return _MARK in getattr(module, "__dict__", {})
+
+
+def call_count() -> int:
+ return _CALLS[0]
diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.py
index 196b3a21395..fdd4faec045 100644
--- a/studio/backend/core/inference/diffusion_speed.py
+++ b/studio/backend/core/inference/diffusion_speed.py
@@ -450,6 +450,7 @@ def apply_speed_optims(
"rocm_query_chunks": False,
"compiled_vae_decode": False,
"cuda_graph": False,
+ "int8_gemm": False,
}
mode = normalize_speed_mode(speed_mode)
# TF32 (max) and cudnn.benchmark (any non-off CUDA load) are process-global; the caller restores them so a later
@@ -505,6 +506,7 @@ def apply_speed_optims(
max_autotune = False,
cache_active = cache_active,
offload_active = offload_active,
+ denoiser_offloaded = denoiser_offloaded,
)
elif (
mode == SPEED_MAX
@@ -517,6 +519,12 @@ def apply_speed_optims(
max_autotune = True,
cache_active = cache_active,
offload_active = offload_active,
+ denoiser_offloaded = denoiser_offloaded,
+ )
+
+ if applied["compiled"]:
+ applied["int8_gemm"] = any(
+ bool(getattr(t, "_unsloth_int8_gemm", 0)) for t in _denoiser_dits(pipe)
)
if applied["compiled"] and _vae_decode_compile_allowed(pipe, mode):
@@ -745,6 +753,16 @@ def _denoiser_dits(pipe: Any) -> list:
return dits
+def int8_gemm_live(pipe: Any, optims: Any) -> list:
+ """``speed_optims`` with ``int8_gemm`` dropped once a deferred install's first-forward probe swapped nothing."""
+ optims = list(optims or ())
+ if "int8_gemm" in optims and not any(
+ bool(getattr(t, "_unsloth_int8_gemm", 0)) for t in _denoiser_dits(pipe)
+ ):
+ optims.remove("int8_gemm")
+ return optims
+
+
# Blocks MEASURED to raise inductor CantSplit under dynamic = True; measure before adding.
_STREAM_MERGING_BLOCKS: frozenset[str] = frozenset({"FluxSingleTransformerBlock"})
@@ -810,6 +828,7 @@ def _compile_repeated_blocks(
max_autotune: bool = False,
cache_active: bool = False,
offload_active: bool = False,
+ denoiser_offloaded: Optional[bool] = None,
) -> bool:
dits = [
t for t in _denoiser_dits(pipe) if callable(getattr(t, "compile_repeated_blocks", None))
@@ -906,6 +925,20 @@ def _compile_repeated_blocks(
install_int8_fused(transformer, logger, offload_active = offload_active)
except Exception as exc: # noqa: BLE001 - optimisation only
_warn(logger, "int8 fused mlp", exc)
+ # After the fused MLP (its down projection calls the same GEMM), before the compile traces the Linears.
+ try:
+ from .diffusion_int8_gemm import install as install_int8_gemm
+
+ # Keyed on the DENOISER's placement: a group plan that streams only the encoders keeps it resident.
+ transformer._unsloth_int8_gemm = install_int8_gemm(
+ transformer,
+ logger,
+ offload_active = offload_active
+ if denoiser_offloaded is None
+ else bool(denoiser_offloaded),
+ )
+ except Exception as exc: # noqa: BLE001 - optimisation only
+ _warn(logger, "int8 fused-dequant gemm", exc)
if type(transformer).__name__ == "QwenImageTransformer2DModel":
try:
from .diffusion_qwenimage_rope import install as install_qwen_real_rope
diff --git a/studio/backend/tests/test_diffusion_int8_gemm.py b/studio/backend/tests/test_diffusion_int8_gemm.py
new file mode 100644
index 00000000000..afb19cae355
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_int8_gemm.py
@@ -0,0 +1,265 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Tests for ``diffusion_int8_gemm.py``: the per-arch gate and kill switch run anywhere; the kernel / Linear tests
+need NVIDIA CUDA, Triton and torchao (they force the lever on, so an arch outside the shipped table still runs)."""
+
+from __future__ import annotations
+
+import pytest
+
+from core.inference import diffusion_int8_gemm as g8
+
+torch = pytest.importorskip("torch")
+
+
[email protected](autouse = True)
+def _clean(monkeypatch):
+ monkeypatch.delenv(g8.INT8_GEMM_ENV, raising = False)
+ g8._DEVICE_CFG.clear()
+ yield
+ g8._DEVICE_CFG.clear()
+
+
+ "cap, on",
+ [
+ ((7, 5), False), # T4: Triton cannot lower the int8 dot
+ ((8, 0), True), # A100
+ ((8, 6), False), # unmeasured
+ ((8, 9), True), # L4
+ ((9, 0), False), # H100: unmeasured
+ ((10, 0), False), # B200: Triton int8 ~2.5x slower than cuBLAS
+ ((12, 0), True), # RTX PRO 6000
+ ],
+)
+def test_arch_gate_auto(cap, on):
+ assert (g8.arch_config(cap, "auto") is not None) is on
+
+
+def test_kill_switch_turns_every_arch_off(monkeypatch):
+ monkeypatch.setenv(g8.INT8_GEMM_ENV, "0")
+ assert g8.int8_gemm_mode() == "off"
+ for cap in ((8, 0), (8, 9), (12, 0)):
+ assert g8.arch_config(cap) is None
+
+
+def test_force_enables_unmeasured_sm80_plus_only(monkeypatch):
+ monkeypatch.setenv(g8.INT8_GEMM_ENV, "1")
+ assert g8.arch_config((9, 0)) == g8._FALLBACK_CONFIG
+ assert g8.arch_config((10, 0)) == g8._FALLBACK_CONFIG
+ assert g8.arch_config((7, 5)) is None
+
+
+def test_install_is_a_noop_when_killed(monkeypatch):
+ monkeypatch.setenv(g8.INT8_GEMM_ENV, "0")
+ lin = torch.nn.Linear(64, 64)
+ assert g8.install(lin) == 0 and not g8.is_installed(lin)
+
+
+def test_offload_keeps_stock():
+ assert g8.install(torch.nn.Linear(64, 64), offload_active = True) == 0
+
+
+def test_rocm_never_probes(monkeypatch):
+ monkeypatch.setattr(torch.version, "hip", "6.4", raising = False)
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
+ assert g8.device_config(0) is None
+
+
+def test_deferred_finalize_records_the_final_count():
+ """A deferred install records the candidate count; a first forward that swaps nothing must clear it (status)."""
+ holder = torch.nn.Sequential(torch.nn.Linear(64, 64))
+ holder._unsloth_int8_gemm = 3
+ assert g8._finalize(holder) == 0
+ assert holder._unsloth_int8_gemm == 0
+
+
+def test_dense_linear_is_not_eligible():
+ assert g8._eligible(torch.nn.Linear(128, 128)) is None
+
+
+def _cuda_ready() -> bool:
+ if not torch.cuda.is_available() or getattr(torch.version, "hip", None):
+ return False
+ if torch.cuda.get_device_capability() < (8, 0):
+ return False
+ try:
+ import torchao # noqa: F401
+ import triton # noqa: F401
+ except Exception: # noqa: BLE001
+ return False
+ return True
+
+
+needs_cuda = pytest.mark.skipif(
+ not _cuda_ready(), reason = "needs NVIDIA sm80+ CUDA, Triton and torchao"
+)
+
+
+def forced(monkeypatch):
+ monkeypatch.setenv(g8.INT8_GEMM_ENV, "1")
+ cfg = g8.device_config(torch.cuda.current_device())
+ if cfg is None:
+ pytest.skip("int8 GEMM probe refused this device")
+ return cfg
+
+
+def _int8_linear(k, n, bias, version):
+ from torchao.quantization import Int8DynamicActivationInt8WeightConfig, quantize_
+
+ torch.manual_seed(0)
+ lin = torch.nn.Linear(k, n, bias = bias).cuda().to(torch.bfloat16)
+ cfg = Int8DynamicActivationInt8WeightConfig(set_inductor_config = False)
+ if version is not None:
+ if not hasattr(cfg, "version"):
+ pytest.skip("torchao without config versions")
+ cfg.version = version
+ quantize_(lin, cfg)
+ return lin
+
+
+@needs_cuda
+ "m, k, n, bias, xs32",
+ [
+ (4096, 4096, 4096, False, False),
+ (1037, 520, 1400, True, False),
+ (17, 256, 1024, True, True),
+ (300, 12288, 256, False, False),
+ ],
+)
[email protected]("ws32", [False, True])
+def test_op_is_bit_exact_vs_torchao_epilogue(forced, m, k, n, bias, xs32, ws32):
+ g = torch.Generator().manual_seed(m + k + n)
+ a = torch.randint(-127, 128, (m, k), generator = g, dtype = torch.int8).cuda()
+ w = torch.randint(-127, 128, (n, k), generator = g, dtype = torch.int8).cuda()
+ xs = (torch.rand(m, generator = g) * 0.02 + 1e-4).to(torch.bfloat16)
+ xs = (xs.float() if xs32 else xs).cuda()
+ ws = (torch.rand(n, generator = g) * 0.002 + 1e-5).to(torch.bfloat16).cuda()
+ ws = ws.float() if ws32 else ws
+ b = (torch.randn(n, generator = g) * 0.1).to(torch.bfloat16).cuda() if bias else None
+ out = g8._op()(a, w, xs, ws, b)
+ assert out.dtype == torch.bfloat16
+ assert torch.equal(out, g8.reference(a, w, xs, ws, b))
+
+
+@needs_cuda
[email protected]("version", [None, 2])
[email protected]("bias", [False, True])
+def test_linear_swap_is_bit_identical_eager_and_compiled(forced, version, bias):
+ from torch._dynamo.utils import counters
+
+ stock = _int8_linear(1024, 768, bias, version)
+ fused = _int8_linear(1024, 768, bias, version)
+ holder = torch.nn.Sequential(fused)
+ assert g8.install(holder) == 1 and g8.is_installed(fused)
+ x = torch.randn(2, 300, 1024, device = "cuda", dtype = torch.bfloat16)
+ with torch.inference_mode():
+ before = g8.call_count()
+ assert torch.equal(fused(x), stock(x))
+ assert g8.call_count() == before + 1
+ counters.clear()
+ torch._dynamo.reset()
+ # Studio compiles with emulate_precision_casts (diffusion_speed); without it Inductor's stock epilogue keeps an
+ # fp32 chain eager never had, and the fused kernel matches eager instead.
+ with torch._inductor.config.patch(emulate_precision_casts = True):
+ out = torch.compile(fused, fullgraph = True)(x)
+ assert not counters["graph_break"]
+ assert torch.equal(out, torch.compile(stock, fullgraph = True)(x))
+ g8.uninstall(holder)
+ assert not g8.is_installed(fused) and "forward" not in fused.__dict__
+
+
+@needs_cuda
[email protected]("bias", [False, True])
+def test_prequant_style_fp32_weight_scale(forced, bias):
+ """Studio's int8 prequant checkpoints rebuild an Int8Tensor with fp32 weight scales (bias added before rounding)."""
+ from torch._dynamo.utils import counters
+
+ stock = _int8_linear(1024, 768, bias, 2)
+ fused = _int8_linear(1024, 768, bias, 2)
+ for lin in (stock, fused):
+ lin.weight.scale = lin.weight.scale.float()
+ holder = torch.nn.Sequential(fused)
+ assert g8.install(holder) == 1
+ x = torch.randn(300, 1024, device = "cuda", dtype = torch.bfloat16) * 3
+ with torch.inference_mode():
+ assert torch.equal(fused(x), stock(x))
+ counters.clear()
+ torch._dynamo.reset()
+ with torch._inductor.config.patch(emulate_precision_casts = True):
+ out = torch.compile(fused, fullgraph = True)(x)
+ assert not counters["graph_break"]
+ assert torch.equal(out, torch.compile(stock, fullgraph = True)(x))
+
+
+@needs_cuda
+def test_small_m_and_misaligned_keep_stock(forced):
+ lin = _int8_linear(1000, 768, False, None) # K off the 64 grid
+ assert g8._eligible(lin) is None
+ ok = _int8_linear(1024, 768, False, None)
+ holder = torch.nn.Sequential(ok)
+ assert g8.install(holder) == 1
+ before = g8.call_count()
+ with torch.inference_mode():
+ ok(
+ torch.randn(16, 1024, device = "cuda", dtype = torch.bfloat16)
+ ) # M = 16 < _int_mm's floor: stock
+ assert g8.call_count() == before
+
+
+@needs_cuda
+def test_fused_mlp_down_projection_traces_without_breaks(forced, monkeypatch):
+ """The fused MLP's down projection reaches the GEMM through ``linear_from_q`` inside the compiled block: no host
+ sync, no graph break, and the same bits as the fused MLP's own stock epilogue (kill switch), eager and compiled."""
+ from torch._dynamo.utils import counters
+
+ from core.inference import diffusion_int8_fused as f8
+
+ attention = pytest.importorskip("diffusers.models.attention")
+ from torchao.quantization import Int8DynamicActivationInt8WeightConfig, quantize_
+
+ torch.manual_seed(0)
+ ff = attention.FeedForward(1024, dim_out = 1024, mult = 4, activation_fn = "gelu-approximate")
+ ff = ff.cuda().to(torch.bfloat16)
+ quantize_(ff, Int8DynamicActivationInt8WeightConfig(version = 2, set_inductor_config = False))
+ fused = torch.nn.Sequential(ff)
+ if not f8.install(fused):
+ pytest.skip("int8 fused MLP probe refused this device")
+ g8.install(fused) # as diffusion_speed does: sets the op handle linear_from_q calls
+ x = torch.randn(2, 300, 1024, device = "cuda", dtype = torch.bfloat16)
+
+ def run(mode):
+ monkeypatch.setenv(g8.INT8_GEMM_ENV, mode)
+ before = g8.call_count()
+ eager = fused(x)
+ counters.clear()
+ torch._dynamo.reset()
+ with torch._inductor.config.patch(emulate_precision_casts = True):
+ compiled = torch.compile(fused, fullgraph = True)(x)
+ assert not counters["graph_break"]
+ return eager, compiled, g8.call_count() - before
+
+ with torch.inference_mode():
+ eager, compiled, calls = run("1")
+ ref_eager, ref_compiled, ref_calls = run("0")
+ assert calls == 2 and ref_calls == 0
+ assert torch.equal(eager, ref_eager)
+ assert torch.equal(compiled, ref_compiled)
+ g8.uninstall(fused)
+ f8.uninstall(fused)
+
+
+def test_status_drops_int8_gemm_once_the_deferred_probe_swapped_nothing():
+ from types import SimpleNamespace
+
+ from core.inference.diffusion_speed import int8_gemm_live
+
+ dit = SimpleNamespace(_unsloth_int8_gemm = 224)
+ pipe = SimpleNamespace(transformer = dit)
+ assert int8_gemm_live(pipe, ("compiled", "int8_gemm")) == ["compiled", "int8_gemm"]
+ dit._unsloth_int8_gemm = 0
+ assert int8_gemm_live(pipe, ("compiled", "int8_gemm")) == ["compiled"]
+ assert int8_gemm_live(pipe, ("compiled",)) == ["compiled"]
diff --git a/studio/backend/tests/test_diffusion_speed.py b/studio/backend/tests/test_diffusion_speed.py
index 99346c5e941..6043df89238 100644
--- a/studio/backend/tests/test_diffusion_speed.py
+++ b/studio/backend/tests/test_diffusion_speed.py
@@ -438,6 +438,7 @@ def test_speed_off_applies_nothing(monkeypatch):
"compiled_vae_decode": False,
"fp16_accum": False,
"cuda_graph": False,
+ "int8_gemm": False,
}
assert pipe.vae.mem_format is None and pipe.compiled is False
# off must not touch any process-wide flag (the bit-identical reference path).