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: warm the compile workers and Triton / inductor keys during a load that compiles inline (#16918)

Daniel Han1624f01b2321
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 71ea713545b..24fc5f52abf 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -10773,6 +10773,9 @@ class DiffusionBackend:
logger = logger,
reduction_filter = family_filters_reductions(fam),
)
+ if compile_ctx is not None and getattr(target, "device", None) == "cuda":
+ # The first render compiles inline: warm the workers and keys while the load finishes.
+ bg_compile.start_deferred_prewarm(logger)
self._raise_if_load_cancelled(_load_token)
# A shipped grid was tuned on the shipped scheduler: no ComfyUI shift.
diff --git a/studio/backend/core/inference/diffusion_bg_compile.py b/studio/backend/core/inference/diffusion_bg_compile.py
index 6686181e48f..0e870c769a6 100644
--- a/studio/backend/core/inference/diffusion_bg_compile.py
+++ b/studio/backend/core/inference/diffusion_bg_compile.py
@@ -137,6 +137,21 @@ def warm_triton_key(logger: Any = None) -> bool:
return True
+def warm_inductor_key(logger: Any = None) -> bool:
+ """Fill inductor's per-process ``torch_key()`` (a hash of torch's sources), paid by the first compile otherwise."""
+ try:
+ from torch._inductor.codecache import torch_key
+ except Exception: # noqa: BLE001 - another torch layout: the first compile computes it as before
+ return False
+ try:
+ torch_key()
+ except Exception as exc: # noqa: BLE001
+ if logger is not None:
+ logger.debug("diffusion.bg_compile: inductor key warm-up skipped: %s", exc)
+ return False
+ return True
+
+
def warm_engage_imports(logger: Any = None) -> int:
"""Model-free first-use work of the engagement; returns how many steps ran."""
@@ -173,6 +188,7 @@ def start_deferred_prewarm(logger: Any = None) -> bool:
t0 = time.perf_counter()
warm_compile_pool(logger)
warm_triton_key(logger)
+ warm_inductor_key(logger)
n = warm_engage_imports(logger)
if logger is not None:
logger.info(
diff --git a/studio/backend/tests/test_diffusion_deferred_prewarm.py b/studio/backend/tests/test_diffusion_deferred_prewarm.py
index 3318b5b2775..f1e85f2b2c6 100644
--- a/studio/backend/tests/test_diffusion_deferred_prewarm.py
+++ b/studio/backend/tests/test_diffusion_deferred_prewarm.py
@@ -45,12 +45,17 @@ def recorder(monkeypatch, fresh):
calls.append(("key", threading.current_thread().name))
return True
+ def inductor_key(logger = None):
+ calls.append(("inductor_key", threading.current_thread().name))
+ return True
+
def imports(logger = None):
calls.append(("imports", threading.current_thread().name))
return 3
monkeypatch.setattr(bg, "warm_compile_pool", pool)
monkeypatch.setattr(bg, "warm_triton_key", key)
+ monkeypatch.setattr(bg, "warm_inductor_key", inductor_key)
monkeypatch.setattr(bg, "warm_engage_imports", imports)
return calls, release
@@ -62,7 +67,7 @@ def test_prewarm_runs_off_the_calling_thread_and_never_blocks_it(recorder):
assert ("key", "unsloth-deferred-prewarm") not in calls
release.set()
assert bg.wait_prewarm(10) is True
- assert [c[0] for c in calls] == ["pool", "key", "imports"]
+ assert [c[0] for c in calls] == ["pool", "key", "inductor_key", "imports"]
assert {c[1] for c in calls} == {"unsloth-deferred-prewarm"}
assert threading.current_thread().name not in {c[1] for c in calls}
@@ -73,7 +78,7 @@ def test_prewarm_starts_once(recorder):
assert bg.start_deferred_prewarm(LOG) is True
assert bg.start_deferred_prewarm(LOG) is False
assert bg.wait_prewarm(10) is True
- assert [c[0] for c in calls] == ["pool", "key", "imports"]
+ assert [c[0] for c in calls] == ["pool", "key", "inductor_key", "imports"]
@pytest.mark.parametrize("raw", ["0", "off", "false", "no"])
@@ -225,3 +230,44 @@ def test_engagement_joins_the_warm_up_before_patching():
and sub.func.attr == "install_patches"
]
assert waits and patches and min(waits) < min(patches)
+
+
+def test_warm_inductor_key_fills_the_key(monkeypatch):
+ codecache = pytest.importorskip("torch._inductor.codecache")
+ calls: list = []
+ monkeypatch.setattr(codecache, "torch_key", lambda: calls.append(1) or b"key")
+ assert bg.warm_inductor_key(LOG) is True
+ assert calls == [1]
+
+
+def test_warm_inductor_key_real_torch_is_cached():
+ codecache = pytest.importorskip("torch._inductor.codecache")
+ assert bg.warm_inductor_key(LOG) is True
+ assert codecache.torch_key() == codecache.torch_key()
+
+
+def test_warm_inductor_key_never_raises(monkeypatch):
+ codecache = pytest.importorskip("torch._inductor.codecache")
+
+ def boom():
+ raise RuntimeError("no sources")
+
+ monkeypatch.setattr(codecache, "torch_key", boom)
+ assert bg.warm_inductor_key(LOG) is False
+
+
+def test_a_load_that_compiles_inline_starts_the_side_warm_up_after_the_cache_begins():
+ """The first render compiles inline: workers and keys warm while the rest of the load runs."""
+ tree = ast.parse(DIFFUSION.read_text(encoding = "utf-8"))
+ load = _function(tree, "DiffusionBackend", "load_pipeline")
+ starts = [line for n, line in _bg_calls(load) if n == "start_deferred_prewarm"]
+ begins = [
+ sub.lineno
+ for sub in ast.walk(load)
+ if isinstance(sub, ast.Call)
+ and isinstance(sub.func, ast.Attribute)
+ and isinstance(sub.func.value, ast.Name)
+ and sub.func.value.id == "compile_cache"
+ and sub.func.attr == "begin"
+ ]
+ assert starts and begins and min(begins) < min(starts)