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: Qwen-Image-2.1 RoPE in real arithmetic inside the compiled blocks (#11888)
Daniel Hane628a57256ef
diff --git a/studio/backend/core/inference/diffusion_qwenimage21_rope.py b/studio/backend/core/inference/diffusion_qwenimage21_rope.pynew file mode 100644index 00000000000..004cd6d228d--- /dev/null+++ b/studio/backend/core/inference/diffusion_qwenimage21_rope.py@@ -0,0 +1,268 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Qwen-Image-2.1 RoPE in real arithmetic under ``torch.compile``, bit-identical to the complex form.++Inductor cannot fuse complex multiply; ATen's fma placement differs per card (B200 vs A100), so+``install`` probes the card and mirrors it via ``addcmul`` (one ``fma``). Kill switch:+``UNSLOTH_DIFFUSION_Q21_REAL_ROPE=0``.+"""++from __future__ import annotations++import ast+import functools+import hashlib+import inspect+import io+import os+import textwrap+import threading+import tokenize+from typing import Any, Optional++REAL_ROPE_ENV = "UNSLOTH_DIFFUSION_Q21_REAL_ROPE"++_MODULE = "diffusers.models.transformers.transformer_qwenimage21"++_FINGERPRINTS: dict[str, frozenset] = {+ "apply_rotary_emb_qwen": frozenset({"ba81dd3fc907c23a"}),+ "_qwenimage21_prepare_qkv": frozenset({"574bacc8f355456d"}),+}++_LOCK = threading.Lock()+_INSTALLED: dict = {}+# Reuse one wrapper per stock function: dynamo guards on the global's identity.+_WRAPPERS: dict = {}+++def real_rope_disabled() -> bool:+ return (os.environ.get(REAL_ROPE_ENV) or "").strip().lower() in ("0", "off", "false", "no")+++def _digest(fn: Any) -> Optional[str]:+ """Source text, not ``ast.dump`` (varies across Python versions), minus docstrings/comments."""+ try:+ src = textwrap.dedent(inspect.getsource(inspect.unwrap(fn)))+ tree = ast.parse(src)+ comments = [+ tok.start+ for tok in tokenize.generate_tokens(io.StringIO(src).readline)+ if tok.type == tokenize.COMMENT+ ]+ except (OSError, TypeError, SyntaxError, ValueError, tokenize.TokenError):+ return None+ lines = src.splitlines()+ for row, col in comments:+ lines[row - 1] = lines[row - 1][:col]+ for node in ast.walk(tree):+ body = getattr(node, "body", None)+ if (+ isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef))+ and body+ and isinstance(body[0], ast.Expr)+ and isinstance(getattr(body[0], "value", None), ast.Constant)+ and isinstance(body[0].value.value, str)+ ):+ for row in range(body[0].lineno, body[0].end_lineno + 1):+ lines[row - 1] = ""+ text = "\n".join(line.rstrip() for line in lines if line.strip())+ return hashlib.sha256(text.encode()).hexdigest()[:16]+++def why_unsupported(module: Any) -> Optional[str]:+ for name, want in _FINGERPRINTS.items():+ got = _digest(getattr(module, name, None))+ if got not in want:+ return f"{name} differs from the version this was written against ({got})"+ return None++[email protected]_cache(maxsize = 1)+def _addcmul_lowering() -> tuple:+ """(addcmul -> fma, only under emulate_precision_casts): torch 2.11 gates it, 2.12+ does not."""+ try:+ import torch+ from torch._inductor import lowering++ if getattr(torch.version, "hip", None):+ return False, False+ src = inspect.getsource(lowering.addcmul)+ return (+ "ops.fma(t1_val, t2_val, self_val)" in src,+ "not config.emulate_precision_casts" in src,+ )+ except Exception: # noqa: BLE001 - no inductor, or its source is unreadable+ return False, False+++def inductor_addcmul_is_fma() -> bool:+ return _addcmul_lowering()[0]+++# Set by ``install``: read inside the traced wrapper, where the lru_cache above would break the graph.+_NEEDS_EMULATE = [True]+++# device index -> (even lanes, odd lanes), each "x" (fma on x * cos) or "s" (fma on swapped * sin).+_FUSION: dict = {}+++def classify_fusion(a: Any, b: Any, c: Any, d: Any, real: Any, imag: Any) -> Optional[tuple]:+ """Inputs in [1, 2) keep candidates exact in float64; "x" = real fma(a, c, -bd), imag fma(b, c, ad)."""+ import numpy as np++ f32, f64 = np.float32, np.float64+ a, b, c, d = (np.asarray(t, dtype = f32).astype(f64) for t in (a, b, c, d))+ bd, ac = (b * d).astype(f32).astype(f64), (a * c).astype(f32).astype(f64)+ ad, bc = (a * d).astype(f32).astype(f64), (b * c).astype(f32).astype(f64)+ real, imag = np.asarray(real, dtype = f32), np.asarray(imag, dtype = f32)+ real_x, real_s = (a * c - bd).astype(f32), (ac - b * d).astype(f32)+ imag_x, imag_s = (b * c + ad).astype(f32), (a * d + bc).astype(f32)+ even = "x" if np.array_equal(real, real_x) else "s" if np.array_equal(real, real_s) else None+ odd = "x" if np.array_equal(imag, imag_x) else "s" if np.array_equal(imag, imag_s) else None+ if even is None or odd is None:+ return None+ return even, odd+++def probe_fusion(device: Any) -> Optional[tuple]:+ import torch++ g = torch.Generator(device = "cpu").manual_seed(0)+ sign = lambda *shape: torch.randint(0, 2, shape, generator = g).float() * 2 - 1 # noqa: E731+ x = (torch.rand(1, 64, 4, 16, 2, generator = g) + 1) * sign(1, 64, 4, 16, 2)+ f = (torch.rand(64, 16, 2, generator = g) + 1) * sign(64, 16, 2)+ out = torch.view_as_real(+ torch.view_as_complex(x.to(device)) * torch.view_as_complex(f.to(device)).unsqueeze(1)+ ).cpu()+ fb = f.unsqueeze(1).expand(64, 4, 16, 2).unsqueeze(0)+ return classify_fusion(+ x[..., 0].numpy(),+ x[..., 1].numpy(),+ fb[..., 0].numpy(),+ fb[..., 1].numpy(),+ out[..., 0].numpy(),+ out[..., 1].numpy(),+ )+++def _real_rope(x: Any, freqs_cis: Any, fusion: tuple) -> Any:+ import torch++ xf = x.float()+ f = torch.view_as_real(freqs_cis).unsqueeze(1)+ cos = f[..., :1].expand(*f.shape[:-1], 2).flatten(-2)+ sin = torch.stack([-f[..., 1], f[..., 1]], dim = -1).flatten(-2)+ swapped = xf.unflatten(-1, (-1, 2)).flip(-1).flatten(-2)+ on_x = torch.addcmul(swapped * sin, xf, cos) if "x" in fusion else None+ on_s = torch.addcmul(xf * cos, swapped, sin) if "s" in fusion else None+ if on_s is None:+ out = on_x+ elif on_x is None:+ out = on_s+ else:+ even = torch.arange(x.shape[-1], device = x.device) % 2 == 0+ out = torch.where(even, on_x, on_s) if fusion[0] == "x" else torch.where(even, on_s, on_x)+ return out.type_as(x)+++def _inductor_emulates() -> bool:+ # Read at trace time (dynamo guards on it): without the flag torch 2.11 splits ``addcmul``.+ from torch._inductor import config+ return bool(getattr(config, "emulate_precision_casts", False))+++def _make_rope(stock: Any) -> Any:+ import torch++ def apply_rotary_emb_qwen(+ x,+ freqs_cis,+ use_real = True,+ use_real_unbind_dim = -1,+ ):+ if (+ not use_real+ and torch.compiler.is_compiling()+ and x.device.type == "cuda"+ and getattr(freqs_cis, "dtype", None) == torch.complex64+ and x.shape[-1] % 2 == 0+ ):+ fusion = _FUSION.get(x.device.index)+ if fusion is not None and (not _NEEDS_EMULATE[0] or _inductor_emulates()):+ return _real_rope(x, freqs_cis, fusion)+ return stock(x, freqs_cis, use_real, use_real_unbind_dim)++ functools.update_wrapper(+ apply_rotary_emb_qwen, stock, assigned = ("__name__", "__doc__"), updated = ()+ )+ apply_rotary_emb_qwen.__unsloth_real_rope__ = True+ return apply_rotary_emb_qwen+++def install(logger: Any = None, device: Any = None) -> bool:+ """Idempotent; False keeps the stock RoPE. Run before the first compiled forward."""+ if real_rope_disabled() or not inductor_addcmul_is_fma():+ uninstall()+ return False+ _NEEDS_EMULATE[0] = _addcmul_lowering()[1]+ try:+ import torch++ dev = (+ torch.device(device)+ if device is not None+ else torch.device("cuda", torch.cuda.current_device())+ )+ if dev.type != "cuda":+ return False+ index = dev.index if dev.index is not None else torch.cuda.current_device()+ if index not in _FUSION:+ _FUSION[index] = probe_fusion(torch.device("cuda", index))+ if _FUSION[index] is None:+ if logger is not None:+ logger.info(+ "diffusion.qwenimage21: complex RoPE kept: this card's complex multiply is not a known fma form"+ )+ return False+ except Exception as exc: # noqa: BLE001 - no CUDA, or the probe failed: keep the complex form+ if logger is not None:+ logger.info("diffusion.qwenimage21: complex RoPE kept: probe failed: %s", exc)+ return False+ try:+ import importlib+ mod = importlib.import_module(_MODULE)+ except Exception: # noqa: BLE001 - diffusers without Qwen-Image 2.1+ return False+ with _LOCK:+ current = getattr(mod, "apply_rotary_emb_qwen", None)+ if getattr(current, "__unsloth_real_rope__", False):+ return True+ why = why_unsupported(mod)+ if why is not None:+ if logger is not None:+ logger.info("diffusion.qwenimage21: complex RoPE kept: %s", why)+ return False+ wrapper = _WRAPPERS.get(current)+ if wrapper is None:+ wrapper = _WRAPPERS[current] = _make_rope(current)+ _INSTALLED[mod] = current+ mod.apply_rotary_emb_qwen = wrapper+ if logger is not None:+ logger.info(+ "diffusion.qwenimage21: RoPE runs in real arithmetic inside the compiled blocks"+ )+ return True+++def uninstall() -> None:+ with _LOCK:+ for mod, stock in list(_INSTALLED.items()):+ if getattr(getattr(mod, "apply_rotary_emb_qwen", None), "__unsloth_real_rope__", False):+ mod.apply_rotary_emb_qwen = stock+ _INSTALLED.pop(mod, None)+++def is_installed() -> bool:+ return bool(_INSTALLED)diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.pyindex bb64b3fea3c..4a6fde6d338 100644--- a/studio/backend/core/inference/diffusion_speed.py+++ b/studio/backend/core/inference/diffusion_speed.py@@ -611,6 +611,12 @@ def _compile_repeated_blocks(dit_kwargs["dynamic"] = compile_dynamic(transformer, kwargs["dynamic"])# Read by auto_dynamic_active: the generalising recompile on a new text length must reach the bundle.transformer._unsloth_auto_dynamic = dit_kwargs["dynamic"] is None+ if type(transformer).__name__ == "QwenImage21Transformer2DModel":+ try:+ from .diffusion_qwenimage21_rope import install as install_real_rope+ install_real_rope(logger)+ except Exception as exc: # noqa: BLE001 - optimisation only+ _warn(logger, "qwen-image-2.1 real rope", exc)try:transformer.compile_repeated_blocks(**dit_kwargs)engaged = Truediff --git a/studio/backend/tests/test_diffusion_qwenimage21_rope.py b/studio/backend/tests/test_diffusion_qwenimage21_rope.pynew file mode 100644index 00000000000..c8816b2cf14--- /dev/null+++ b/studio/backend/tests/test_diffusion_qwenimage21_rope.py@@ -0,0 +1,307 @@+# 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_qwenimage21_rope.py``; bit-identity tests need CUDA + inductor fma lowering."""++from __future__ import annotations++import importlib+import types++import pytest++from core.inference import diffusion_qwenimage21_rope as rope++torch = pytest.importorskip("torch")+qmod = pytest.importorskip("diffusers.models.transformers.transformer_qwenimage21")+++def _cuda_dtype():+ return torch.bfloat16 if torch.cuda.get_device_capability() >= (8, 0) else torch.float16+++_needs_exact = pytest.mark.skipif(+ not (torch.cuda.is_available() and rope.inductor_addcmul_is_fma()),+ reason = "needs CUDA and inductor's fma addcmul lowering (torch 2.11+)",+)+++def emulating():+ import torch._inductor.config as icfg++ prev = icfg.emulate_precision_casts+ icfg.emulate_precision_casts = True+ yield+ icfg.emulate_precision_casts = prev++[email protected](autouse = True)+def _stock_after(monkeypatch):+ monkeypatch.delenv(rope.REAL_ROPE_ENV, raising = False)+ rope.uninstall()+ yield+ rope.uninstall()+ torch._dynamo.reset()+++def _attention(+ device,+ heads = 4,+ dim_head = 32,+):+ torch.manual_seed(0)+ attn = qmod.QwenImage21Attention(dim = heads * dim_head, heads = heads, dim_head = dim_head)+ attn = attn.to(device, _cuda_dtype() if device == "cuda" else torch.bfloat16).eval()+ with torch.no_grad():+ for p in attn.parameters():+ p.normal_(0, 0.05)+ attn.norm_q.weight.normal_(1.0, 0.1)+ attn.norm_k.weight.normal_(1.0, 0.1)+ return attn+++def _qk(attn, seq, device):+ g = torch.Generator(device = "cpu").manual_seed(3)+ x = torch.randn(2, seq, attn.inner_dim, generator = g).to(device, attn.to_q.weight.dtype)+ ang = torch.randn(seq, attn.inner_dim // attn.heads // 2, generator = g).to(device) * 40+ freqs = torch.polar(torch.ones_like(ang), ang)++ def prep(hidden_states, rotary_emb):+ q, k, _, _ = qmod._qwenimage21_prepare_qkv(+ attn, hidden_states, rotary_emb, None, None, None+ )+ return q, k++ return prep, x, freqs+++def _compiled(prep, x, freqs, dynamic):+ torch._dynamo.reset()+ with torch.inference_mode():+ return torch.compile(prep, dynamic = dynamic)(x, freqs)+++@_needs_exact[email protected]("dynamic", [False, True])[email protected]("seq", [77, 1024])+def test_compiled_real_rope_matches_the_complex_one(dynamic, seq, emulating):+ """Fused QK-norm may move a few elements one ulp off B200; read off each pair's length."""+ # Inductor's norm schedule depends on this head layout.+ attn = _attention("cuda", heads = 32, dim_head = 128)+ prep, x, freqs = _qk(attn, seq, "cuda")+ with torch.inference_mode():+ eager = prep(x, freqs)+ stock = _compiled(prep, x, freqs, dynamic)+ assert rope.install()+ real = _compiled(prep, x, freqs, dynamic)+ for r, s, e in zip(real, stock, eager):+ moved = (r != s).sum().item()+ assert moved <= 2 * (s != e).sum().item() + 16+ if moved:+ pair = (+ s.float()+ .unflatten(-1, (-1, 2))+ .norm(dim = -1, keepdim = True)+ .expand(*s.shape[:-1], -1, 2)+ )+ bound = 3 * torch.finfo(s.dtype).eps * pair.flatten(-2)+ assert bool(((r.float() - s.float()).abs() <= bound).all())+++@_needs_exact+def test_the_real_form_runs_inside_the_compiled_block(monkeypatch, emulating):+ calls = []+ real = rope._real_rope+ monkeypatch.setattr(+ rope, "_real_rope", lambda x, f, fusion: calls.append(x.shape) or real(x, f, fusion)+ )+ assert rope.install()+ attn = _attention("cuda")+ prep, x, freqs = _qk(attn, 64, "cuda")+ _compiled(prep, x, freqs, False)+ assert len(calls) == 2 # traced once for q, once for k+++def _fake_fusion(monkeypatch):+ monkeypatch.setattr(rope, "inductor_addcmul_is_fma", lambda: True)+ monkeypatch.setattr(rope, "probe_fusion", lambda device: ("x", "x"))+ monkeypatch.setattr(rope, "_FUSION", {})+ monkeypatch.setattr(torch.cuda, "current_device", lambda: 0)+++def test_eager_calls_keep_the_complex_form(monkeypatch):+ _fake_fusion(monkeypatch)+ assert rope.install()+ monkeypatch.setattr(+ rope, "_real_rope", lambda *a: pytest.fail("real form used outside a compile")+ )+ attn = _attention("cpu")+ prep, x, freqs = _qk(attn, 16, "cpu")+ with torch.inference_mode():+ got = prep(x, freqs)+ rope.uninstall()+ with torch.inference_mode():+ want = prep(x, freqs)+ assert torch.equal(got[0], want[0]) and torch.equal(got[1], want[1])+++def test_install_is_idempotent_reuses_one_wrapper_and_uninstall_restores(monkeypatch):+ _fake_fusion(monkeypatch)+ stock = qmod.apply_rotary_emb_qwen+ assert rope.install() and rope.install()+ wrapper = qmod.apply_rotary_emb_qwen+ assert wrapper is not stock and wrapper.__wrapped__ is stock+ rope.uninstall()+ assert qmod.apply_rotary_emb_qwen is stock+ assert rope.install()+ assert qmod.apply_rotary_emb_qwen is wrapper+++def test_installed_diffusers_matches_the_fingerprints():+ """On failure re-check the real form against the new stock one, then add the digest."""+ assert rope.why_unsupported(qmod) is None+++def test_a_drifted_rope_is_left_alone(monkeypatch):+ _fake_fusion(monkeypatch)+ fake = types.SimpleNamespace(**vars(qmod))++ def apply_rotary_emb_qwen(+ x,+ freqs_cis,+ use_real = True,+ use_real_unbind_dim = -1,+ ):+ return x++ fake.apply_rotary_emb_qwen = apply_rotary_emb_qwen+ assert "apply_rotary_emb_qwen differs" in rope.why_unsupported(fake)+ real_import = importlib.import_module+ monkeypatch.setattr(+ importlib,+ "import_module",+ lambda name, *a: fake if name == rope._MODULE else real_import(name, *a),+ )+ assert rope.install() is False+ assert fake.apply_rotary_emb_qwen is apply_rotary_emb_qwen+++def test_kill_switch_and_non_fma_inductor_keep_the_complex_form(monkeypatch):+ stock = qmod.apply_rotary_emb_qwen+ monkeypatch.setattr(rope, "inductor_addcmul_is_fma", lambda: False)+ assert rope.install() is False+ assert qmod.apply_rotary_emb_qwen is stock+ _fake_fusion(monkeypatch)+ monkeypatch.setenv(rope.REAL_ROPE_ENV, "0")+ assert rope.install() is False+ assert qmod.apply_rotary_emb_qwen is stock+ monkeypatch.delenv(rope.REAL_ROPE_ENV)+ monkeypatch.setattr(rope, "probe_fusion", lambda device: None)+ assert rope.install() is False+ assert qmod.apply_rotary_emb_qwen is stock+++def test_kill_switch_on_a_later_load_restores_the_stock_function(monkeypatch):+ _fake_fusion(monkeypatch)+ stock = qmod.apply_rotary_emb_qwen+ assert rope.install()+ assert qmod.apply_rotary_emb_qwen is not stock+ monkeypatch.setenv(rope.REAL_ROPE_ENV, "0")+ assert rope.install() is False+ assert qmod.apply_rotary_emb_qwen is stock and not rope.is_installed()+++def test_addcmul_probe_reads_the_lowering():+ rope._addcmul_lowering.cache_clear()+ try:+ from torch._inductor import lowering++ import inspect++ src = inspect.getsource(lowering.addcmul)+ want = "ops.fma(t1_val, t2_val, self_val)" in src and not torch.version.hip+ except Exception: # noqa: BLE001 - torch without that lowering+ want = False+ assert rope.inductor_addcmul_is_fma() is want+ if want:+ assert rope._addcmul_lowering()[1] is ("emulate_precision_casts" in src)++[email protected](not torch.cuda.is_available(), reason = "needs CUDA")+def test_a_torch_that_needs_the_emulate_flag_keeps_the_complex_form_without_it(monkeypatch):+ import torch._inductor.config as icfg++ _fake_fusion(monkeypatch)+ monkeypatch.setattr(torch.cuda, "current_device", lambda: torch.device("cuda").index or 0)+ assert rope.install()+ monkeypatch.setattr(rope, "_NEEDS_EMULATE", [True])+ monkeypatch.setattr(icfg, "emulate_precision_casts", False)+ calls = []+ real = rope._real_rope+ monkeypatch.setattr(+ rope, "_real_rope", lambda x, f, fusion: calls.append(1) or real(x, f, fusion)+ )+ attn = _attention("cuda")+ prep, x, freqs = _qk(attn, 16, "cuda")+ torch._dynamo.reset()+ with torch.inference_mode():+ torch.compile(prep, backend = "eager")(x, freqs)+ assert calls == []+ monkeypatch.setattr(icfg, "emulate_precision_casts", True)+ torch._dynamo.reset()+ with torch.inference_mode():+ torch.compile(prep, backend = "eager")(x, freqs)+ assert len(calls) == 2+++def _fused(a, c, bd):+ import numpy as np+ return (a.astype(np.float64) * c.astype(np.float64) + bd.astype(np.float64)).astype(np.float32)++[email protected]("even", ["x", "s"])[email protected]("odd", ["x", "s"])+def test_classify_fusion_names_each_half(even, odd):+ import numpy as np++ rng = np.random.default_rng(0)+ a, b, c, d = (+ ((rng.random(4096) + 1) * rng.choice([-1, 1], 4096)).astype(np.float32) for _ in range(4)+ )+ real = _fused(a, c, -(b * d)) if even == "x" else _fused(-b, d, a * c)+ imag = _fused(b, c, a * d) if odd == "x" else _fused(a, d, b * c)+ assert rope.classify_fusion(a, b, c, d, real, imag) == (even, odd)+ assert rope.classify_fusion(a, b, c, d, a * c - b * d, imag) is None++[email protected](not torch.cuda.is_available(), reason = "needs CUDA")+def test_this_card_multiplies_complex_numbers_in_a_known_fused_form():+ assert rope.probe_fusion(torch.device("cuda")) in {+ ("x", "x"),+ ("x", "s"),+ ("s", "x"),+ ("s", "s"),+ }+++@_needs_exact[email protected]("fusion", [("x", "x"), ("x", "s"), ("s", "x"), ("s", "s")])+def test_each_fusion_form_compiles_to_that_fma(fusion, emulating):+ """Inputs where the forms disagree: catches inductor re-fusing the products."""+ import numpy as np++ g = torch.Generator(device = "cpu").manual_seed(1)+ x = (torch.rand(1, 64, 4, 32, generator = g) + 1) * (+ torch.randint(0, 2, (1, 64, 4, 32), generator = g) * 2 - 1+ )+ f = (torch.rand(64, 16, 2, generator = g) + 1) * (+ torch.randint(0, 2, (64, 16, 2), generator = g) * 2 - 1+ )+ freqs = torch.view_as_complex(f.contiguous())+ fn = torch.compile(lambda x, fr: rope._real_rope(x, fr, fusion))+ out = fn(x.cuda(), freqs.cuda()).cpu().numpy()+ a, b = x[..., 0::2].numpy(), x[..., 1::2].numpy()+ c = f[..., 0].unsqueeze(1).expand(64, 4, 16).unsqueeze(0).numpy()+ d = f[..., 1].unsqueeze(1).expand(64, 4, 16).unsqueeze(0).numpy()+ assert rope.classify_fusion(a, b, c, d, out[..., 0::2], out[..., 1::2]) == fusiondiff --git a/studio/backend/tests/test_diffusion_speed.py b/studio/backend/tests/test_diffusion_speed.pyindex 6fa0f9e5571..e19f4ed17ef 100644--- a/studio/backend/tests/test_diffusion_speed.py+++ b/studio/backend/tests/test_diffusion_speed.py@@ -1586,6 +1586,40 @@ def test_auto_dynamic_active_follows_the_torchao_marker():assert isinstance(ds_mod.dynamo_graph_count(), int)+def test_real_rope_installed_before_the_qwen_image_21_block_compile_only(monkeypatch):+ from core.inference import diffusion_qwenimage21_rope as rope++ _stub_torch(monkeypatch)+ order = []+ monkeypatch.setattr(rope, "install", lambda logger = None: order.append("rope") or True)+ pipe = _Pipe(with_compile = True)+ real_compile = pipe._compile+ pipe.transformer.compile_repeated_blocks = lambda **kw: order.append("compile") or real_compile(+ **kw+ )+ apply_speed_optims(pipe, _target(), is_gguf = False, family = _family(), speed_mode = SPEED_DEFAULT)+ assert order == ["compile"] # any other DiT keeps the stock RoPE++ order.clear()+ QwenImage21Transformer2DModel = type("QwenImage21Transformer2DModel", (), {})+ pipe = _Pipe(with_compile = True)+ pipe.transformer = QwenImage21Transformer2DModel()+ pipe.transformer.compile_repeated_blocks = lambda **kw: order.append("compile")+ applied = apply_speed_optims(+ pipe, _target(), is_gguf = False, family = _family(), speed_mode = SPEED_DEFAULT+ )+ assert order == ["rope", "compile"] and applied["compiled"] is True++ order.clear()+ monkeypatch.setattr(+ rope, "install", lambda logger = None: (_ for _ in ()).throw(RuntimeError("probe"))+ )+ applied = apply_speed_optims(+ pipe, _target(), is_gguf = False, family = _family(), speed_mode = SPEED_DEFAULT+ )+ assert order == ["compile"] and applied["compiled"] is True++def test_family_compiles_regionally_reads_the_repeated_blocks_declaration(monkeypatch):from core.inference.diffusion_speed import family_compiles_regionally