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: fix Qwen-Image-2.1 recompiles and Ideogram 4 attention and device errors on torch 2.11 to 2.14 (#12350)
Daniel Hana01982669964
diff --git a/studio/backend/core/inference/diffusion_attention.py b/studio/backend/core/inference/diffusion_attention.pyindex f9dfed8237f..28e02891924 100644--- a/studio/backend/core/inference/diffusion_attention.py+++ b/studio/backend/core/inference/diffusion_attention.py@@ -29,6 +29,7 @@ Best-effort: an unavailable backend falls back to the diffusers default. torch/dfrom __future__ import annotations+import osimport reimport threadingfrom typing import Any, Optional@@ -639,6 +640,112 @@ def _attention_dits(pipe: Any) -> list:return dits+# head_dim 256 (Ideogram 4) has no cuDNN kernel on torch 2.11-2.13 (cuDNN 9.19/9.20); a pinned cuDNN then has no fallback.+_CUDNN_HEAD_DIM_CACHE: dict[tuple[str, str, int], bool] = {}+++def _dit_head_dims(pipe: Any) -> set[int]:+ """The attention head dims of every denoiser DiT: ``config.attention_head_dim``, else any module's ``head_dim``."""+ dims: set[int] = set()+ for dit in _attention_dits(pipe):+ config = getattr(dit, "config", None)+ value = None+ try:+ value = config.get("attention_head_dim") if hasattr(config, "get") else None+ except Exception: # noqa: BLE001+ value = None+ if isinstance(value, int) and not isinstance(value, bool):+ dims.add(value)+ continue+ if isinstance(value, (list, tuple)) and value and all(isinstance(v, int) for v in value):+ dims.update(value)+ continue+ modules = getattr(dit, "modules", None)+ if not callable(modules):+ continue+ try:+ for module in modules():+ hd = getattr(module, "head_dim", None)+ if isinstance(hd, int) and not isinstance(hd, bool) and hd > 0:+ dims.add(hd)+ except Exception: # noqa: BLE001+ continue+ return dims+++CUDNN_HEAD_DIM_PROBE_ENV = "UNSLOTH_DIFFUSION_CUDNN_HEAD_DIM_PROBE"+++def _run_cudnn_head_dim_probe(device: str, dtype: Any, head_dim: int) -> bool:+ """True when cuDNN attention serves ``head_dim`` per SDPA's own gate (launches no kernel); raises when unaskable."""+ import torch++ if dtype not in (torch.float16, torch.bfloat16):+ dtype = torch.bfloat16+ q = torch.empty((1, 2, 8, int(head_dim)), device = device, dtype = dtype)+ try:+ from torch.backends.cuda import SDPAParams, can_use_cudnn_attention+ except ImportError:+ SDPAParams = can_use_cudnn_attention = None+ if SDPAParams is not None and can_use_cudnn_attention is not None:+ try:+ params = SDPAParams(q, q, q, None, 0.0, False, False)+ except TypeError: # torch < 2.5 has no enable_gqa argument+ params = SDPAParams(q, q, q, None, 0.0, False)+ return bool(can_use_cudnn_attention(params, False))+ from torch.nn.attention import SDPBackend, sdpa_kernel++ try:+ with sdpa_kernel([SDPBackend.CUDNN_ATTENTION]):+ torch.nn.functional.scaled_dot_product_attention(q, q, q)+ except torch.cuda.OutOfMemoryError:+ raise+ except Exception: # noqa: BLE001 - "No available kernel" is the answer+ return False+ return True+++def _cudnn_runs_head_dim(target: Any, head_dim: int) -> Optional[bool]:+ """Whether cuDNN attention runs at ``head_dim`` on ``target``; None when unaskable (not cached)."""+ device = str(getattr(target, "torch_device", None) or getattr(target, "device", None) or "")+ if not device.startswith("cuda"):+ return None+ device = _indexed_cuda_device(device)+ dtype = getattr(target, "dtype", None)+ key = (device, str(dtype), int(head_dim))+ cached = _CUDNN_HEAD_DIM_CACHE.get(key)+ if cached is not None:+ return cached+ try:+ ran = _run_cudnn_head_dim_probe(device, dtype, head_dim)+ except Exception: # noqa: BLE001 - no torch, no device, allocator trouble: no answer about the kernel+ return None+ return _CUDNN_HEAD_DIM_CACHE.setdefault(key, bool(ran))+++def _cudnn_serves_pipe(+ pipe: Any,+ target: Any,+ logger: Any = None,+) -> bool:+ """False only when cuDNN demonstrably lacks a DiT head_dim. ``UNSLOTH_DIFFUSION_CUDNN_HEAD_DIM_PROBE=0`` skips it."""+ if (os.environ.get(CUDNN_HEAD_DIM_PROBE_ENV) or "").strip().lower() in (+ "0",+ "off",+ "false",+ "no",+ ):+ return True+ missing = sorted(d for d in _dit_head_dims(pipe) if _cudnn_runs_head_dim(target, d) is False)+ if missing and logger is not None:+ logger.warning(+ "diffusion.attention: cuDNN attention has no kernel for head_dim %s with this torch/cuDNN build; "+ "keeping the default SDPA dispatch",+ ",".join(str(d) for d in missing),+ )+ return not missing++def apply_attention_backend(pipe: Any,backend: Optional[str],@@ -675,6 +782,12 @@ def apply_attention_backend(_ensure_attention_backend_installed(backend, logger)if backend == "sage" and target is not None and _sage_kernel_runs(target, logger) is False:backend = None+ if (+ backend == "_native_cudnn"+ and target is not None+ and not _cudnn_serves_pipe(pipe, target, logger)+ ):+ backend = Noneif backend is not None:engaged = Falsefor fn in setters:diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.pyindex cffb13c63dd..dbeddf02d7e 100644--- a/studio/backend/core/inference/diffusion_dynamic_text.py+++ b/studio/backend/core/inference/diffusion_dynamic_text.py@@ -2,12 +2,21 @@# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0"""Mark a DiT's prompt-length inputs dynamic (``dynamic_sources``, scoped to its forward) so a new prompt length-never recompiles; blanket ``dynamic=True`` hits torchao CantSplit."""+never recompiles; blanket ``dynamic=True`` hits torchao CantSplit.++torch 2.13+ guards symbolic slice bounds on ``s is None`` (Qwen-Image-2.1 ``cache_write_slice``), recompiling per+prompt length; ``install`` restores 2.12's answer. Probe-gated; kill switch ``UNSLOTH_DIFFUSION_SLICE_IDENTITY_FIX=0``."""from __future__ import annotations+import os+import threadingfrom typing import Any, Optional+SLICE_IDENTITY_ENV = "UNSLOTH_DIFFUSION_SLICE_IDENTITY_FIX"+_SLICE_LOCK = threading.Lock()+_SLICE_STATE: dict[str, Any] = {}+# segments[0][0] (always 0) stays static: a symbol there trips torchao CantSplit (rows become ``s0 - s_start``)._QWEN_IMAGE_21_SOURCES: tuple[str, ...] = ("L['hidden_states']",@@ -114,6 +123,70 @@ def fingerprint(transformer: Any, dynamic: Any) -> Optional[str]:return ",".join(sources) + (";unbacked:" + ",".join(unbacked) if unbacked else "")+def _slice_identity_specialises() -> bool:+ """Whether this torch reads (and guards) a traced slice's bounds to answer ``is``: 2.13+ yes, 2.11/2.12 no."""+ from torch._dynamo.variables import ConstantVariable, SliceVariable # noqa: PLC0415+ from torch._dynamo.variables.base import NO_SUCH_SUBOBJ # noqa: PLC0415++ if "get_real_python_backed_value" in vars(SliceVariable):+ return False # torch gives slices their own answer; trust it+ if not callable(getattr(SliceVariable, "get_real_python_backed_value", None)):+ return False+ probe = SliceVariable([ConstantVariable.create(0), ConstantVariable.create(1)])+ return probe.get_real_python_backed_value() is not NO_SUCH_SUBOBJ+++def install_slice_identity_fix(logger: Any = None) -> bool:+ """Idempotently keep symbolic slice bounds symbolic under ``is`` / ``is not``. True when the patch is active."""+ if (os.environ.get(SLICE_IDENTITY_ENV) or "").strip().lower() in ("0", "off", "false", "no"):+ return False+ with _SLICE_LOCK:+ if "original" in _SLICE_STATE:+ return True+ if _SLICE_STATE.get("not_needed"):+ return False+ try:+ if not _slice_identity_specialises():+ _SLICE_STATE["not_needed"] = True+ return False+ from torch._dynamo.variables import SliceVariable, SymNodeVariable # noqa: PLC0415+ from torch._dynamo.variables.base import NO_SUCH_SUBOBJ # noqa: PLC0415+ except Exception: # noqa: BLE001 - internals moved: leave torch alone+ return False+ original = SliceVariable.get_real_python_backed_value++ def get_real_python_backed_value(self: Any) -> object:+ # Stock builds a fresh slice (identical to nothing): "no backing object" keeps every answer, minus the guard.+ if any(isinstance(item, SymNodeVariable) for item in getattr(self, "items", ())):+ return NO_SUCH_SUBOBJ+ return original(self)++ get_real_python_backed_value._unsloth_slice_identity = True # type: ignore[attr-defined]+ SliceVariable.get_real_python_backed_value = get_real_python_backed_value+ _SLICE_STATE["original"] = original+ _SLICE_STATE["cls"] = SliceVariable+ if logger is not None:+ logger.info(+ "diffusion.dynamic_text: symbolic slice bounds stay symbolic under `is None` (torch 2.13+ recompile fix)"+ )+ return True+++def uninstall_slice_identity_fix() -> None:+ with _SLICE_LOCK:+ _SLICE_STATE.pop("not_needed", None)+ cls = _SLICE_STATE.pop("cls", None)+ if _SLICE_STATE.pop("original", None) is not None and cls is not None:+ try:+ del cls.get_real_python_backed_value+ except AttributeError:+ pass+++def _arms_slice_bound(sources: tuple[str, ...]) -> bool:+ return any(s.endswith((".start", ".stop", ".step")) for s in sources)++def _merge(current: str, extra: tuple[str, ...]) -> str:parts = [p for p in (current or "").split(",") if p.strip()]for s in extra:@@ -133,6 +206,13 @@ def install(return Falseif getattr(transformer, "_unsloth_dynamic_text", None) is not None:return True+ # dynamic=True makes the slice bound symbolic too, so the fix keys on the family, not on what gets armed.+ if _arms_slice_bound(sources_for(transformer)):+ try:+ install_slice_identity_fix(logger)+ except Exception as exc: # noqa: BLE001 - optimisation only+ if logger is not None:+ logger.warning("diffusion.dynamic_text: slice identity fix failed: %s", exc)unbacked = unbacked_sources_for(transformer)sources = sources_for(transformer) if dynamic is None else ()cfg = _compiler_config()diff --git a/studio/backend/core/inference/diffusion_ideogram4.py b/studio/backend/core/inference/diffusion_ideogram4.pyindex 64e90102958..42bb907dd33 100644--- a/studio/backend/core/inference/diffusion_ideogram4.py+++ b/studio/backend/core/inference/diffusion_ideogram4.py@@ -424,6 +424,35 @@ def load_ideogram4_transformer(return mark_source_precision(model, "fp8")+def _hidden_states_on_mask_device(original: Callable[..., Any]) -> Callable[..., Any]:+ """Return the tapped states on ``attention_mask``'s device: under group offload ``text_encoder.device`` reads CPU+ (so the mask lands there) while the states come back on the GPU, and ``encode_prompt``'s multiply raised."""++ def get_text_encoder_hidden_states(text_encoder, token_ids, attention_mask, pos_2d):+ states = original(text_encoder, token_ids, attention_mask, pos_2d)+ device = getattr(attention_mask, "device", None)+ if device is None:+ return states+ return [s.to(device) if getattr(s, "device", device) != device else s for s in states]++ get_text_encoder_hidden_states._unsloth_mask_device = True # type: ignore[attr-defined]+ return get_text_encoder_hidden_states+++def install_text_encoder_device_guard(pipe: Any) -> bool:+ """Shadow the pipeline's static ``_get_text_encoder_hidden_states`` on this instance. Idempotent."""+ original = getattr(pipe, "_get_text_encoder_hidden_states", None)+ if not callable(original):+ return False+ if getattr(original, "_unsloth_mask_device", False):+ return True+ try:+ pipe._get_text_encoder_hidden_states = _hidden_states_on_mask_device(original)+ except Exception: # noqa: BLE001 - a frozen / slotted pipeline keeps the stock path+ return False+ return True++def load_ideogram4_pipeline(repo_id: str,dtype,@@ -467,7 +496,7 @@ def load_ideogram4_pipeline()check_cancelled()logger.info("diffusion.ideogram4: assembled pipeline from %s per-component", repo_id)- return diffusers.Ideogram4Pipeline(+ pipe = diffusers.Ideogram4Pipeline(scheduler = scheduler,vae = vae,text_encoder = text_encoder,@@ -475,3 +504,5 @@ def load_ideogram4_pipeline(transformer = transformer,unconditional_transformer = unconditional_transformer,)+ install_text_encoder_device_guard(pipe)+ return pipediff --git a/studio/backend/tests/test_diffusion_attention_cudnn_head_dim.py b/studio/backend/tests/test_diffusion_attention_cudnn_head_dim.pynew file mode 100644index 00000000000..9dbf1d0246d--- /dev/null+++ b/studio/backend/tests/test_diffusion_attention_cudnn_head_dim.py@@ -0,0 +1,180 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""cuDNN attention is only pinned when the torch/cuDNN build has a kernel for every DiT head_dim.++Ideogram 4 (head_dim 256) under any speed profile raised "No available kernel" on torch 2.11-2.13, whose cuDNN+serves head_dim <= 128, while torch 2.14 runs it. Probe stubbed except the last test."""++from __future__ import annotations++import types++import pytest++import core.inference.diffusion_attention as att+from core.inference.diffusion_attention import apply_attention_backend+++class _Transformer:+ def __init__(+ self,+ config = None,+ modules = (),+ ):+ self.calls: list = []+ self.config = config+ self._modules_list = list(modules)++ def set_attention_backend(self, name):+ self.calls.append(name)++ def modules(self):+ return iter(self._modules_list)+++class _Logger:+ def __init__(self):+ self.warnings: list = []++ def warning(self, msg, *args):+ self.warnings.append(msg % args if args else msg)++ def info(self, *_a, **_k):+ pass+++def _target(+ device = "cuda",+ dtype = "bf16",+ torch_device = None,+):+ return types.SimpleNamespace(device = device, dtype = dtype, torch_device = torch_device or device)++[email protected](autouse = True)+def _isolated(monkeypatch):+ monkeypatch.setattr(att, "_CUDNN_HEAD_DIM_CACHE", {}, raising = False)+ monkeypatch.setattr(att, "_ensure_attention_backend_installed", lambda *a, **k: None)+ monkeypatch.setattr(att, "_active_attention_backend", lambda: "native")+ monkeypatch.setattr(att, "warn_if_sdpa_math_only", lambda *a, **k: False)+ monkeypatch.setattr(att, "_indexed_cuda_device", lambda device: device)+++def _stub_probe(+ monkeypatch,+ served = (64, 128),+ seen = None,+ raises = None,+):+ def _probe(device, dtype, head_dim):+ if seen is not None:+ seen.append((device, dtype, head_dim))+ if raises is not None:+ raise raises+ return head_dim in served++ monkeypatch.setattr(att, "_run_cudnn_head_dim_probe", _probe, raising = False)+++def test_ideogram_head_dim_256_keeps_native_where_cudnn_has_no_kernel(monkeypatch):+ _stub_probe(monkeypatch, served = (64, 128))+ cond = _Transformer(config = {"attention_head_dim": 256})+ uncond = _Transformer(config = {"attention_head_dim": 256})+ pipe = types.SimpleNamespace(transformer = cond, unconditional_transformer = uncond)+ log = _Logger()+ assert apply_attention_backend(pipe, "_native_cudnn", logger = log, target = _target()) is None+ assert "_native_cudnn" not in cond.calls and "_native_cudnn" not in uncond.calls+ assert any("head_dim 256" in w for w in log.warnings)+++def test_cudnn_stays_pinned_where_it_serves_the_head_dim(monkeypatch):+ _stub_probe(monkeypatch, served = (128, 256))+ t = _Transformer(config = {"attention_head_dim": 256})+ assert apply_attention_backend(+ types.SimpleNamespace(transformer = t), "_native_cudnn", target = _target()+ ) == ("_native_cudnn")+ assert t.calls == ["_native_cudnn"]+++def test_head_dim_read_from_modules_when_config_lacks_it(monkeypatch):+ _stub_probe(monkeypatch, served = (64, 128))+ t = _Transformer(config = {"dim": 3840}, modules = [types.SimpleNamespace(head_dim = 256)])+ assert (+ apply_attention_backend(+ types.SimpleNamespace(transformer = t), "_native_cudnn", target = _target()+ )+ is None+ )++[email protected]("exc", [ImportError("no torch"), RuntimeError("CUDA out of memory")])+def test_an_unanswerable_probe_keeps_cudnn(monkeypatch, exc):+ seen: list = []+ _stub_probe(monkeypatch, seen = seen, raises = exc)+ t = _Transformer(config = {"attention_head_dim": 256})+ assert apply_attention_backend(+ types.SimpleNamespace(transformer = t), "_native_cudnn", target = _target()+ ) == ("_native_cudnn")+ assert att._cudnn_runs_head_dim(_target(), 256) is None and len(seen) == 2+++def test_probe_is_memoised_per_device_dtype_and_head_dim(monkeypatch):+ seen: list = []+ _stub_probe(monkeypatch, seen = seen)+ assert att._cudnn_runs_head_dim(_target(), 256) is False+ assert att._cudnn_runs_head_dim(_target(), 256) is False+ assert att._cudnn_runs_head_dim(_target(), 128) is True+ att._cudnn_runs_head_dim(_target(torch_device = "cuda:1"), 256)+ assert seen == [("cuda", "bf16", 256), ("cuda", "bf16", 128), ("cuda:1", "bf16", 256)]+++def test_other_backends_unknown_head_dims_and_cpu_targets_never_probe(monkeypatch):+ seen: list = []+ _stub_probe(monkeypatch, seen = seen)+ t = _Transformer(config = {"attention_head_dim": 256})+ for backend in ("flash", "xformers", "aiter"):+ assert (+ apply_attention_backend(types.SimpleNamespace(transformer = t), backend, target = _target())+ == backend+ )+ bare = _Transformer()+ assert apply_attention_backend(+ types.SimpleNamespace(transformer = bare), "_native_cudnn", target = _target()+ ) == ("_native_cudnn")+ assert (+ apply_attention_backend(types.SimpleNamespace(transformer = t), "_native_cudnn")+ == "_native_cudnn"+ )+ assert att._cudnn_runs_head_dim(_target(device = "cpu"), 256) is None+ assert seen == []+++def test_real_probe_agrees_with_a_real_pinned_attention():+ torch = pytest.importorskip("torch")+ if not torch.cuda.is_available():+ pytest.skip(+ reason = "the probe asks a real CUDA device; the stubbed tests above cover CPU-only CI"+ )+ from torch.nn.attention import SDPBackend, sdpa_kernel++ for head_dim in (128, 256, 512):+ q = torch.zeros((1, 4, 64, head_dim), device = "cuda", dtype = torch.bfloat16)+ try:+ with sdpa_kernel([SDPBackend.CUDNN_ATTENTION]):+ torch.nn.functional.scaled_dot_product_attention(q, q, q)+ runs = True+ except RuntimeError:+ runs = False+ assert att._run_cudnn_head_dim_probe("cuda", torch.bfloat16, head_dim) is runs+++def test_kill_switch_pins_cudnn_without_asking(monkeypatch):+ seen: list = []+ _stub_probe(monkeypatch, served = (), seen = seen)+ monkeypatch.setenv("UNSLOTH_DIFFUSION_CUDNN_HEAD_DIM_PROBE", "0")+ t = _Transformer(config = {"attention_head_dim": 256})+ assert apply_attention_backend(+ types.SimpleNamespace(transformer = t), "_native_cudnn", target = _target()+ ) == ("_native_cudnn")+ assert seen == []diff --git a/studio/backend/tests/test_diffusion_dynamic_text.py b/studio/backend/tests/test_diffusion_dynamic_text.pyindex 79e6bbd94a8..46353320deb 100644--- a/studio/backend/tests/test_diffusion_dynamic_text.py+++ b/studio/backend/tests/test_diffusion_dynamic_text.py@@ -419,3 +419,90 @@ def test_regional_compile_of_a_dense_h3_arms_the_unbacked_temb(monkeypatch):assert (getattr(m, "_unsloth_dynamic_text", None) is not None) is dt.unbacked_supported()finally:dt.uninstall(m)+++class _Q21Block(torch.nn.Module):+ """The shape of diffusers' QwenImage21 prefill: ``cache_write_slice is not None`` guards the prefix KV write."""++ def forward(+ self,+ hidden_states,+ kv_cache_mode = None,+ cache_write_slice = None,+ ):+ if kv_cache_mode == "extract" and cache_write_slice is not None:+ return hidden_states[:, cache_write_slice].clone() * 2+ return hidden_states * 3+++class _Q21Transformer(torch.nn.Module):+ def __init__(self, dynamic = None):+ super().__init__()+ self.block = torch.compile(_Q21Block(), dynamic = dynamic)++ def forward(self, prefix_len, total):+ x = torch.randn(1, total, 8)+ out = self.block(x, kv_cache_mode = "extract", cache_write_slice = slice(0, prefix_len))+ torch.testing.assert_close(out, x[:, :prefix_len] * 2)+ return out++[email protected]("dynamic", [None, True])+def test_new_prompt_length_does_not_recompile_the_prefix_kv_write(dynamic):+ """torch 2.13+ guarded ``cache_write_slice.stop`` via ``is not None``: each new prompt length recompiled the block.+ dynamic=True is an unquantized DiT's default tier once the divisibility backport is in."""+ from torch._dynamo.utils import counters++ Transformer = type("QwenImage21Transformer2DModel", (_Q21Transformer,), {})+ torch._dynamo.reset()+ counters.clear()+ m = Transformer(dynamic)+ dt.install(m, dynamic = dynamic)+ try:+ for prefix_len, total in ((26, 90), (31, 95), (40, 104)):+ m(prefix_len, total)+ assert counters["stats"]["unique_graphs"] == 1+ finally:+ dt.uninstall(m)+ dt.uninstall_slice_identity_fix()+ torch._dynamo.reset()+++def test_slice_identity_fix_keeps_identity_answers():+ """Patched or not, a slice is never None and constant slices still fold."""+ dt.install_slice_identity_fix()+ try:++ def f(x, s):+ a = 1 if s is None else 2+ b = 1 if s is not None else 2+ return x[:, s] * a + b++ c = torch.compile(f, fullgraph = True)+ x = torch.randn(2, 16)+ for s in (slice(0, 4), slice(2, None), slice(None, 8, 2)):+ torch.testing.assert_close(c(x, s), f(x, s))+ finally:+ dt.uninstall_slice_identity_fix()+ torch._dynamo.reset()+++def test_slice_identity_fix_is_probe_gated_and_reversible(monkeypatch):+ from torch._dynamo.variables import SliceVariable++ stock = SliceVariable.__dict__.get("get_real_python_backed_value")+ monkeypatch.setenv(dt.SLICE_IDENTITY_ENV, "0")+ assert dt.install_slice_identity_fix() is False+ assert SliceVariable.__dict__.get("get_real_python_backed_value") is stock+ monkeypatch.delenv(dt.SLICE_IDENTITY_ENV)+ active = dt.install_slice_identity_fix()+ try:+ # Only a torch whose stock identity test reads the slice's bounds (2.13+) is patched.+ assert active == (+ stock is None+ and callable(getattr(SliceVariable, "get_real_python_backed_value", None))+ and dt._SLICE_STATE.get("original") is not None+ )+ finally:+ dt.uninstall_slice_identity_fix()+ assert SliceVariable.__dict__.get("get_real_python_backed_value") is stockdiff --git a/studio/backend/tests/test_diffusion_ideogram4_te_device.py b/studio/backend/tests/test_diffusion_ideogram4_te_device.pynew file mode 100644index 00000000000..dd2c12f6c24--- /dev/null+++ b/studio/backend/tests/test_diffusion_ideogram4_te_device.py@@ -0,0 +1,75 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Ideogram 4 under group offload with streamed text encoders: ``text_encoder.device`` reads CPU while the hooks+return every tapped state on the GPU, so ``encode_prompt``'s mask multiply raised a device mismatch. CPU only."""++from __future__ import annotations++import types++import pytest++torch = pytest.importorskip("torch")++from core.inference import diffusion_ideogram4 as ide # noqa: E402+++class _State:+ """A tensor stand-in on a named device (CPU CI has no second real device)."""++ def __init__(self, device):+ self.device = torch.device(device) if isinstance(device, str) else device+ self.moves: list = []++ def to(self, device):+ self.moves.append(device)+ return _State(device)+++def _streamed_encoder_states(text_encoder, token_ids, attention_mask, pos_2d):+ # What leaf-level group offload does: every layer, and so every returned state, runs on the onload device.+ return [_State("cuda:0") for _ in range(3)]+++class _Pipe:+ _get_text_encoder_hidden_states = staticmethod(_streamed_encoder_states)++ def __init__(self, **components):+ self.components = components+++def _load_with_stubs(monkeypatch):+ monkeypatch.setattr(ide, "_patch_create_causal_mask", lambda: None)+ monkeypatch.setattr(ide, "load_ideogram4_text_encoder", lambda *a, **k: "te")+ monkeypatch.setattr(ide, "load_krea2_tokenizer", lambda *a, **k: "tok")+ monkeypatch.setattr(ide, "load_ideogram4_transformer", lambda *a, **k: "dit")+ fake_diffusers = types.SimpleNamespace(+ Ideogram4Pipeline = _Pipe,+ AutoencoderKLFlux2 = types.SimpleNamespace(from_pretrained = lambda *a, **k: "vae"),+ FlowMatchEulerDiscreteScheduler = types.SimpleNamespace(+ from_pretrained = lambda *a, **k: "sched"+ ),+ )+ monkeypatch.setitem(__import__("sys").modules, "diffusers", fake_diffusers)+ return ide.load_ideogram4_pipeline("unsloth/ideogram-4-fp8", torch.bfloat16)+++def test_streamed_text_encoder_states_land_on_the_mask_device(monkeypatch):+ pipe = _load_with_stubs(monkeypatch)+ mask = torch.ones(1, 4, dtype = torch.long) # on the CPU, where text_encoder.device pointed it+ states = pipe._get_text_encoder_hidden_states("te", mask, mask, mask)+ assert [s.device for s in states] == [mask.device] * 3+++def test_matching_devices_are_left_alone():+ same = _State("cpu")+ guarded = ide._hidden_states_on_mask_device(lambda *a: [same])+ assert guarded("te", None, torch.ones(1), None)[0] is same and same.moves == []+++def test_guard_is_idempotent(monkeypatch):+ pipe = _load_with_stubs(monkeypatch)+ first = pipe._get_text_encoder_hidden_states+ assert ide.install_text_encoder_device_guard(pipe) is True+ assert pipe._get_text_encoder_hidden_states is firstdiff --git a/studio/backend/tests/test_diffusion_inductor_backports.py b/studio/backend/tests/test_diffusion_inductor_backports.pyindex 555a76dd06e..a94d7059426 100644--- a/studio/backend/tests/test_diffusion_inductor_backports.py+++ b/studio/backend/tests/test_diffusion_inductor_backports.py@@ -47,9 +47,10 @@ def test_never_proves_a_non_multiple():assert not _proves(4096 * a - 4095 * b, a - b)assert not _proves(4096 * a, a + b)assert not _proves(a * b + 1, a)- # sympy.gcd is taken over the rationals: gcd((a + b) / 2, a + b) == a + b, yet (a + b) / 2 is no multiple.- assert not _proves((a + b) / 2, a + b)- assert not _proves(sympy.Rational(8, 3) * a * b, a * b)+ # Asked of the backport, not torch 2.14 (whose own check says True for rational forms inductor never emits).+ allocator = sizevars.SizeVarAllocator()+ assert not bp._gcd_proves_multiple(allocator, (a + b) / 2, a + b)+ assert not bp._gcd_proves_multiple(allocator, sympy.Rational(8, 3) * a * b, a * b)def test_integer_denominators_keep_the_stock_answer():