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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio diffusion: compiled int8 W8A8 linear on RDNA3 / RDNA4 ROCm (Qwen-Image-2.1 int8 21% faster per step on gfx1151, now ahead of bf16) (#16825)
Daniel Han22233cd114ed
diff --git a/studio/backend/core/inference/diffusion_native_quant.py b/studio/backend/core/inference/diffusion_native_quant.pyindex 9d7104dac6b..e5c792d6f07 100644--- a/studio/backend/core/inference/diffusion_native_quant.py+++ b/studio/backend/core/inference/diffusion_native_quant.py@@ -9,6 +9,7 @@ from __future__ import annotationsimport contextlibimport contextvarsimport os+import reimport timefrom functools import lru_cachefrom typing import Any, Callable, Optional@@ -304,8 +305,24 @@ def _w8a8_rows_into(_FUSED_STATE: dict = {"fn": None, "failed": None}+# ROCm archs where the compiled W8A8 linear was measured: RDNA3 / RDNA3.5 / RDNA4 (gfx11xx / gfx12xx). On gfx1151 it+# matches eager exactly and beats bf16 (1.03-1.36x per linear at 4096 rows), where the eager int8 path is 0.93x bf16.+_ROCM_FUSED_W8A8_ARCH = re.compile(r"^gfx1[12][0-9a-f]{2}$")+++def _rocm_fused_w8a8_refusal(torch: Any) -> Optional[str]:+ """None when the compiled linear may run on this ROCm device, else why not (other ROCm archs: unmeasured)."""+ from .rocm_bf16 import _device_gfx_arch++ arch = _device_gfx_arch(torch, None)+ if _ROCM_FUSED_W8A8_ARCH.match(arch):+ return None+ return f"ROCm {arch or 'unknown arch'}"++def _fused_w8a8() -> Optional[Callable[..., Any]]:- """Compiled ``_w8a8_rows``, or None if disabled, no inductor runtime, ROCm, or already failed."""+ """Compiled ``_w8a8_rows``, or None if disabled, no inductor runtime, a ROCm arch other than RDNA3 / RDNA4, or+ already failed."""if _FUSED_STATE["failed"] is not None or int8_fused_disabled():return Nonefn = _FUSED_STATE["fn"]@@ -320,9 +337,22 @@ def _fused_w8a8() -> Optional[Callable[..., Any]]:runtime = torch_compile_runtime_available()except Exception: # noqa: BLE001 - unknown runtime: stay eagerruntime = False- if not runtime or is_rocm_torch(torch):- _FUSED_STATE["failed"] = "no inductor runtime" if not runtime else "ROCm"+ try: # Studio's structured logger (the line lands in the server log), plain logging outside Studio+ from loggers import get_logger+ log = get_logger(__name__)+ except Exception: # noqa: BLE001+ import logging+ log = logging.getLogger(__name__)+ if not runtime:+ _FUSED_STATE["failed"] = "no inductor runtime"+ log.info("diffusion.native_quant: int8 W8A8 linear stays eager (no inductor runtime)")return None+ if is_rocm_torch(torch):+ refusal = _rocm_fused_w8a8_refusal(torch)+ if refusal is not None:+ _FUSED_STATE["failed"] = refusal+ log.info("diffusion.native_quant: int8 W8A8 linear stays eager (%s)", refusal)+ return None# Registered in eager, before any trace reads the handle._FUSED_STATE["row_major"] = _row_major_op()# Every key exists before the first trace: older dynamo guards on len(_FUSED_STATE), so adding@@ -332,6 +362,7 @@ def _fused_w8a8() -> Optional[Callable[..., Any]]:# One graph for every slice offset and chunk height (dynamic storage offset), written in place by inductor._FUSED_STATE["into"] = torch.compile(_w8a8_rows_into, dynamic = True, fullgraph = True)_FUSED_STATE["fn"] = fn+ log.info("diffusion.native_quant: int8 W8A8 linear runs as one compiled function")return fndiff --git a/studio/backend/tests/test_diffusion_native_quant_fused.py b/studio/backend/tests/test_diffusion_native_quant_fused.pyindex c2644e176eb..aef8a081ee5 100644--- a/studio/backend/tests/test_diffusion_native_quant_fused.py+++ b/studio/backend/tests/test_diffusion_native_quant_fused.py@@ -53,21 +53,66 @@ def test_kill_switch_disables_the_compiled_linear(monkeypatch, value):def test_no_inductor_runtime_or_rocm_keeps_eager(monkeypatch):import core.inference.diffusion_speed as sp+ import core.inference.rocm_bf16 as rb+ # An unreadable gfx arch, whatever card this host has (a real RDNA3 / RDNA4 host would compile).+ monkeypatch.setattr(rb, "_device_gfx_arch", lambda _torch, _ordinal: "")monkeypatch.setattr(sp, "torch_compile_runtime_available", lambda: False)assert nq._fused_w8a8() is Noneassert nq.fused_w8a8_state()["failed"] == "no inductor runtime"monkeypatch.setattr(nq, "_FUSED_STATE", {"fn": None, "failed": None})monkeypatch.setattr(sp, "torch_compile_runtime_available", lambda: True)monkeypatch.setattr(torch.version, "hip", "6.4", raising = False)+ # No readable gfx arch: unmeasured, so eager.assert nq._fused_w8a8() is None- assert nq.fused_w8a8_state()["failed"] == "ROCm"+ assert nq.fused_w8a8_state()["failed"] == "ROCm unknown arch"# AMD SDK / Radeon wheels leave torch.version.hip unset and tag __version__ instead.monkeypatch.setattr(nq, "_FUSED_STATE", {"fn": None, "failed": None})monkeypatch.setattr(torch.version, "hip", None, raising = False)monkeypatch.setattr(torch, "__version__", "2.9.0+rocm7.0")assert nq._fused_w8a8() is None- assert nq.fused_w8a8_state()["failed"] == "ROCm"+ assert nq.fused_w8a8_state()["failed"].startswith("ROCm")++[email protected]("arch", ["gfx1151", "gfx1100", "gfx1103", "gfx1201"])+def test_rdna3_and_rdna4_rocm_get_the_compiled_linear(monkeypatch, arch):+ # Measured on gfx1151 (Strix Halo): compiled == eager bit for bit, 1.03-1.36x bf16 per linear at 4096 rows, where+ # the eager int8 path is 0.93x bf16.+ import core.inference.diffusion_speed as sp+ import core.inference.rocm_bf16 as rb++ monkeypatch.setattr(nq, "_FUSED_STATE", {"fn": None, "failed": None})+ monkeypatch.setattr(sp, "torch_compile_runtime_available", lambda: True)+ monkeypatch.setattr(torch.version, "hip", "7.13", raising = False)+ monkeypatch.setattr(rb, "_device_gfx_arch", lambda _torch, _ordinal: arch)+ assert nq._fused_w8a8() is not None+ assert nq.fused_w8a8_state()["failed"] is None+ assert nq._FUSED_STATE["into"] is not None++[email protected]("arch", ["gfx942", "gfx90a", "gfx1030", "gfx906", ""])+def test_other_rocm_archs_stay_eager(monkeypatch, arch):+ import core.inference.diffusion_speed as sp+ import core.inference.rocm_bf16 as rb++ monkeypatch.setattr(nq, "_FUSED_STATE", {"fn": None, "failed": None})+ monkeypatch.setattr(sp, "torch_compile_runtime_available", lambda: True)+ monkeypatch.setattr(torch.version, "hip", "7.0", raising = False)+ monkeypatch.setattr(rb, "_device_gfx_arch", lambda _torch, _ordinal: arch)+ assert nq._fused_w8a8() is None+ assert nq.fused_w8a8_state()["failed"] == f"ROCm {arch or 'unknown arch'}"+++def test_kill_switch_wins_on_rdna(monkeypatch):+ import core.inference.diffusion_speed as sp+ import core.inference.rocm_bf16 as rb++ monkeypatch.setattr(nq, "_FUSED_STATE", {"fn": None, "failed": None})+ monkeypatch.setenv(nq.NATIVE_INT8_FUSED_ENV, "0")+ monkeypatch.setattr(sp, "torch_compile_runtime_available", lambda: True)+ monkeypatch.setattr(torch.version, "hip", "7.13", raising = False)+ monkeypatch.setattr(rb, "_device_gfx_arch", lambda _torch, _ordinal: "gfx1151")+ assert nq._fused_w8a8() is Nonedef test_cpu_input_never_takes_the_compiled_linear(monkeypatch):