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 diffusion: keep Qwen-Image-2.1 CFG pairing through its first-render compile (#17177)

Daniel Han10f638a301b7
diff --git a/studio/backend/core/inference/diffusion_cfg_batch.py b/studio/backend/core/inference/diffusion_cfg_batch.py
index dc743d428ca..e6c60c9e098 100644
--- a/studio/backend/core/inference/diffusion_cfg_batch.py
+++ b/studio/backend/core/inference/diffusion_cfg_batch.py
@@ -16,6 +16,8 @@ from typing import Any, Optional
CFG_BATCH_ENV = "UNSLOTH_DIFFUSION_CFG_BATCH"
COMPILED_RESIDENT_ENV = "UNSLOTH_DIFFUSION_CFG_BATCH_COMPILED"
+# 0: any graph a paired call compiles turns pairing off for the load, the first render included
+CFG_PAIR_COMPILE_WARMUP_ENV = "UNSLOTH_DIFFUSION_CFG_PAIR_COMPILE_WARMUP"
# The CFG policy of every denoiser class Studio loads (test_diffusion_cfg_batch_policy pins it to diffusion_families
# and video_families): exactly one of the three tables below.
@@ -88,6 +90,12 @@ _MISS_LIMIT = 3
_OFF_TRANSIENT = "transient"
+def cfg_pair_compile_warmup() -> bool:
+ """A paired call may compile lazily during the first render of its request shape and cache mode."""
+ raw = (os.environ.get(CFG_PAIR_COMPILE_WARMUP_ENV) or "").strip().lower()
+ return raw not in ("0", "off", "false", "no", "disable", "disabled")
+
+
def cfg_batch_mode() -> str:
"""``off`` / ``auto`` (default: a listed class; offloaded, batched whenever the doubled call fits; resident, after the
per-shape timing trial) / ``timed`` (the trial for offloaded loads too) / ``force`` (no trial, any class).
@@ -571,6 +579,8 @@ class CfgBatchRouter:
self.render_checked = False
self.offloaded = False
self.pending_disable: Optional[str] = None
+ # _pair_key (request shape, cache mode, compiled) -> the render its paired calls may compile in
+ self.pair_warm: dict = {}
self.pair_t0: Optional[float] = None
self.renders = 0 # completed renders: a doubled-batch compile is warm-up only in a shape's trial render
self.render_compiled: Optional[str] = None
@@ -1166,11 +1176,32 @@ class CfgBatchRouter:
self.disable(f"the paired call failed ({type(exc).__name__}: {exc})")
return None
if _graph_count() != graphs:
- self.pending_disable = "the paired call compiled a new graph"
+ if not cfg_pair_compile_warmup():
+ self.pending_disable = "the paired call compiled a new graph"
+ elif (
+ self.pair_warm.setdefault(self._pair_key(shape, second), self.renders)
+ != self.renders
+ ):
+ # lazy compilation is the warm-up of a shape's first paired render; a graph after it is churn
+ self.pending_disable = "the paired call compiled a graph after its warm-up render"
+ else:
+ self.pair_warm.setdefault(self._pair_key(shape, second), self.renders)
self.kept = {"out": other, "snapshots": snapshots, "latent": latent}
self.stats["paired_steps"] += 1
return mine
+ def _pair_key(self, shape: _Shape, second: tuple) -> tuple:
+ """Request shape, second-branch shapes, cache mode and compiled flag: each combination compiles its own graphs."""
+ mg = _mg()
+ branch_cache = getattr(self.paired, "branch_cache", None)
+ mode = None if branch_cache is None else second[1].get(branch_cache[2])
+ return (
+ shape.key,
+ tuple(tuple(t.shape) for t in mg._iter_tensors(second)),
+ mode if isinstance(mode, (str, type(None))) else repr(mode),
+ self.render_compiled,
+ )
+
def _take(self, args: tuple, kwargs: dict, shape: _Shape) -> Any:
kept, self.kept = self.kept, None
if not _matches(kept["snapshots"], (args, kwargs)):
diff --git a/studio/backend/tests/test_diffusion_cfg_pair.py b/studio/backend/tests/test_diffusion_cfg_pair.py
index 019d851ab44..d38abb6ffc5 100644
--- a/studio/backend/tests/test_diffusion_cfg_pair.py
+++ b/studio/backend/tests/test_diffusion_cfg_pair.py
@@ -314,3 +314,110 @@ def test_q21_kill_switch(monkeypatch):
"QwenImage21Transformer2DModel has no paired block loop"
)
assert cb.cfg_pair_decline(_Pipe(_offload(_tiny())), offload_policy = "group") is None
+
+
+_WARMUP_ENV = "UNSLOTH_DIFFUSION_CFG_PAIR_COMPILE_WARMUP"
+
+
+def test_compile_warmup_env_name():
+ assert cb.CFG_PAIR_COMPILE_WARMUP_ENV == _WARMUP_ENV
+
+
+def _q21_compiled_below_hooks(layers = 1):
+ """Every block's forward compiled below its offload hook (diffusion_block_graph), counting Dynamo frames."""
+ from torch._dynamo.testing import CompileCounter
+
+ torch._dynamo.reset()
+ model = _offload(_q21(layers = layers))
+ counter = CompileCounter()
+ for block in model.transformer_blocks:
+ ref = cp.below_hook_ref(block)
+ ref.forward = torch.compile(ref.forward, backend = counter)
+ block._unsloth_below_hook_ref = ref
+ return model, counter
+
+
+def _q21_renders(
+ model,
+ renders = 3,
+ steps = 4,
+):
+ pipe = _Pipe(model)
+ assert cb.install_cfg_batch(pipe, offload_policy = "group") is None
+ router = cb.router_for(pipe)
+ counts = _block_calls(model)
+ per_render, outs = [], []
+ for _ in range(renders):
+ before = list(counts)
+ got, _ = _q21_loop(model, steps = steps)
+ outs.append(got)
+ per_render.append([a - b for a, b in zip(counts, before)])
+ cb.end_cfg_batch_render(pipe)
+ return router, per_render, outs
+
+
+def test_q21_lazy_compile_in_the_first_render_keeps_pairing(monkeypatch):
+ """The prefill and the first decode step compile their graphs during render 1: that is warm-up, not churn."""
+ monkeypatch.delenv(_WARMUP_ENV, raising = False)
+ ref, _ = _q21_loop(_offload(_q21(layers = 1)), steps = 4)
+ model, counter = _q21_compiled_below_hooks()
+ router, per_render, outs = _q21_renders(model)
+ assert router.disabled is None and router.pending_disable is None
+ # step 1 prefills plain (2 calls), steps 2-4 pair (1 call each), every render
+ assert per_render == [[5], [5], [5]]
+ assert router.stats["paired_steps"] == 9
+ assert all(torch.equal(o, ref) for o in outs)
+ graphs = counter.frame_count
+ assert graphs > 0
+ _q21_loop(model, steps = 4)
+ assert counter.frame_count == graphs # no graph after warm-up
+ assert not torch._dynamo.utils.counters["graph_break"]
+
+
+def test_q21_compile_warmup_kill_switch(monkeypatch):
+ monkeypatch.setenv(_WARMUP_ENV, "0")
+ model, _ = _q21_compiled_below_hooks()
+ router, per_render, _ = _q21_renders(model)
+ assert router.disabled == "the paired call compiled a new graph"
+ assert per_render == [[5], [8], [8]]
+
+
+def test_q21_a_graph_after_the_warm_up_render_still_turns_pairing_off(monkeypatch):
+ monkeypatch.delenv(_WARMUP_ENV, raising = False)
+ graphs = {"n": 0, "churn": False}
+ monkeypatch.setattr(cb, "_graph_count", lambda: graphs["n"])
+ model = _offload(_q21(layers = 1))
+ model.transformer_blocks[0].register_forward_pre_hook(
+ lambda *_a: graphs.__setitem__("n", graphs["n"] + (graphs["churn"] or graphs["n"] == 0))
+ )
+ pipe = _Pipe(model)
+ cb.install_cfg_batch(pipe, offload_policy = "group")
+ router = cb.router_for(pipe)
+ _q21_loop(model, steps = 4)
+ cb.end_cfg_batch_render(pipe)
+ assert router.disabled is None
+ graphs["churn"] = True # render 2: every block call compiles again
+ _q21_loop(model, steps = 4)
+ cb.end_cfg_batch_render(pipe)
+ assert router.disabled == "the paired call compiled a graph after its warm-up render"
+ counts = _block_calls(model)
+ _q21_loop(model, steps = 4)
+ assert counts == [8]
+
+
+def test_q21_pair_warm_up_key_separates_cache_mode_and_negative_length():
+ router = cb.CfgBatchRouter.__new__(cb.CfgBatchRouter)
+ router.paired = type(
+ "P", (), {"branch_cache": cp.BRANCH_CACHES["QwenImage21Transformer2DModel"]}
+ )()
+ router.render_compiled = None
+ shape = cb._Shape(((1, 4),))
+
+ def key(n, mode):
+ return router._pair_key(shape, ((), {"x": torch.zeros(1, n), "kv_cache_mode": mode}))
+
+ assert key(5, "cached") == key(5, "cached")
+ eager = key(5, "cached")
+ assert len({eager, key(7, "cached"), key(5, "extract")}) == 3
+ router.render_compiled = "the denoiser runs a compiled forward below its hooks"
+ assert key(5, "cached") != eager