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 Qwen-Image-2.1: fused q/k RMSNorm + RoPE and broadcast modulation for eager renders on ROCm (#16891)

Daniel Hana6cd920e7dc8
diff --git a/studio/backend/core/inference/diffusion_qwenimage21_eager.py b/studio/backend/core/inference/diffusion_qwenimage21_eager.py
index d8a2b6e50f6..846be712212 100644
--- a/studio/backend/core/inference/diffusion_qwenimage21_eager.py
+++ b/studio/backend/core/inference/diffusion_qwenimage21_eager.py
@@ -73,6 +73,11 @@ def set_state(transformer: Any, state: Optional[str]) -> None:
module.__dict__[_ATTR] = state
+def _rocm() -> bool:
+ import torch
+ return is_rocm_torch(torch)
+
+
@functools.lru_cache(maxsize = 1)
def _kernel() -> Any:
import triton
@@ -84,8 +89,21 @@ def _kernel() -> Any:
from triton.language.extra.cuda import libdevice
# Module globals, which the JIT resolves. *_rn: Triton would otherwise FMA-contract the products eager rounds.
+ # The HIP libdevice has no *_rn: plain ops there, launched with enable_fp_fusion=False (``_launch_options``).
global _rsqrt, _mul, _add
- _rsqrt, _mul, _add = libdevice.rsqrt, libdevice.mul_rn, libdevice.add_rn
+ if _rocm():
+
+ @triton.jit
+ def _mul_plain(a, b):
+ return a * b
+
+ @triton.jit
+ def _add_plain(a, b):
+ return a + b
+
+ _rsqrt, _mul, _add = libdevice.rsqrt, _mul_plain, _add_plain
+ else:
+ _rsqrt, _mul, _add = libdevice.rsqrt, libdevice.mul_rn, libdevice.add_rn
# n_rows keeps its % 16 hint: runtime it was 2.5% slower at even token counts on A100.
@triton.jit(do_not_specialize = ["n_seq"])
@@ -163,6 +181,10 @@ def _fusion(device: Any) -> Optional[tuple]:
return _FUSION[index]
+def _launch_options() -> dict:
+ return {"enable_fp_fusion": False} if _rocm() else {}
+
+
def _eligible(x: Any, norm: Any, freqs: Any) -> bool:
import torch
@@ -246,6 +268,7 @@ def _norm_rope(x: Any, norm: Any, freqs: Any) -> Optional[Any]:
REAL_FMA_X = fusion[0] == "x",
IMAG_FMA_X = fusion[1] == "x",
num_warps = _WARPS,
+ **_launch_options(),
)
except Exception: # noqa: BLE001 - a JIT / driver failure: the stock math for the rest of the process
_FAILED[0] = True
@@ -310,7 +333,7 @@ def why_unsupported(mod: Any) -> Optional[str]:
def install(logger: Any = None) -> bool:
try:
import torch
- if is_rocm_torch(torch) or not torch.cuda.is_available():
+ if not torch.cuda.is_available():
return False
from diffusers.models.transformers import transformer_qwenimage21 as mod
except Exception: # noqa: BLE001 - no CUDA, or diffusers without Qwen-Image 2.1
diff --git a/studio/backend/tests/test_diffusion_qwenimage21_eager.py b/studio/backend/tests/test_diffusion_qwenimage21_eager.py
index 3678bf5f32c..74610408bd8 100644
--- a/studio/backend/tests/test_diffusion_qwenimage21_eager.py
+++ b/studio/backend/tests/test_diffusion_qwenimage21_eager.py
@@ -23,7 +23,7 @@ from test_diffusion_qwenimage21 import _inputs, _model, _render # noqa: E402
def _clean(monkeypatch):
monkeypatch.delenv(e.EAGER_FAST_ENV, raising = False)
monkeypatch.delenv(q.FAST_STEP_ENV, raising = False)
- # Wiring tests on CPU tensors posing as an NVIDIA host: ROCm torch keeps the stock step (test below).
+ # Wiring tests on CPU tensors posing as an NVIDIA host; the ROCm host has its own tests below.
monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
monkeypatch.setattr(e, "is_rocm_torch", lambda _torch: False)
q.uninstall()
@@ -131,14 +131,26 @@ def test_install_is_idempotent_and_uninstall_restores():
assert qmod._qwenimage21_prepare_qkv is stock
-def test_rocm_torch_keeps_the_stock_step(monkeypatch):
+def test_rocm_torch_takes_the_eager_step(monkeypatch):
monkeypatch.setattr(e, "is_rocm_torch", lambda _torch: True)
- stock = qmod._qwenimage21_prepare_qkv
m = _model()
- assert not e.install()
- assert not e.install_for_pipe(types.SimpleNamespace(transformer = m))
- assert qmod._qwenimage21_prepare_qkv is stock
- assert e._ATTR not in m.__dict__
+ assert e.install()
+ assert e.install_for_pipe(types.SimpleNamespace(transformer = m))
+ assert getattr(qmod._qwenimage21_prepare_qkv, "__unsloth_q21_eager__", False)
+ assert m.__dict__[e._ATTR] == e.EAGER
+ inp = _valid(_inputs())
+ e.uninstall()
+ ref = _render(_model(), inp)
+ got = _render(_armed(_model()), inp)
+ for a, b in zip(ref, got):
+ assert torch.equal(a, b)
+
+
+def test_rocm_launches_without_fp_contraction(monkeypatch):
+ monkeypatch.setattr(e, "is_rocm_torch", lambda _torch: True)
+ assert e._launch_options() == {"enable_fp_fusion": False}
+ monkeypatch.setattr(e, "is_rocm_torch", lambda _torch: False)
+ assert e._launch_options() == {}
def test_installed_diffusers_matches_the_fingerprints():