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 compile cache: keep every graph across re-saves, and save under torch's compile lock (#14341)
Daniel Han1a6397cce3ca
diff --git a/studio/backend/core/inference/diffusion_compile_cache.py b/studio/backend/core/inference/diffusion_compile_cache.pyindex 22f06a3258d..81db420ed1e 100644--- a/studio/backend/core/inference/diffusion_compile_cache.py+++ b/studio/backend/core/inference/diffusion_compile_cache.py@@ -56,6 +56,7 @@ version is deliberately NOT bumped, since bumping it is what would invalidate thfrom __future__ import annotations+import contextlibimport dataclassesimport hashlibimport json@@ -924,6 +925,58 @@ def _collect_superseded(cdir: Path, logger: Any) -> list[str]:return removed+def _compile_lock() -> Any:+ try:+ from torch._dynamo.convert_frame import compile_lock # noqa: PLC0415+ return compile_lock+ except Exception: # noqa: BLE001 - no dynamo lock on this build: save unlocked, as before+ return contextlib.nullcontext()+++def _one_group_per_type(data: bytes, logger: Any) -> bytes:+ """One ``(type, artifacts)`` group per type: torch appends a group per save and loads them with ``dict(...)``,+ so a re-saved bundle kept only the newest group of each type."""+ try:+ from torch.compiler._cache import ( # noqa: PLC0415+ CacheArtifactManager,+ _deserialize_single_cache,+ _serialize_single_cache,+ )+ from torch.utils._appending_byte_serializer import AppendingByteSerializer # noqa: PLC0415+ except ImportError:+ # torch 2.7 writes one flat artifact list and loads it without dict(), so there is nothing to merge+ return data+ try:+ CacheArtifactManager._ensure_cache_artifacts_registered()+ groups = AppendingByteSerializer.to_list(data, deserialize_fn = _deserialize_single_cache)+ # Dedupe on (key, content) only: Inductor / AOT entries sharing a key are distinct guarded candidates+ # (stored under a content hash), and a repeat moves to its latest position so a single-slot type+ # (autotune) still applies its newest bytes last, as torch's in-order load would.+ merged: dict[str, dict] = {}+ for kind, artifacts in groups:+ bucket = merged.setdefault(kind, {})+ for artifact in artifacts:+ content = artifact.content+ if not isinstance(content, (bytes, bytearray, memoryview)):+ content = json.dumps(+ content, sort_keys = True, default = repr+ ).encode() # autotune: JSON+ ident = (artifact.key, hashlib.sha256(content).digest())+ bucket.pop(ident, None)+ bucket[ident] = artifact+ if len(merged) == len(groups):+ return data+ out = AppendingByteSerializer(serialize_fn = _serialize_single_cache)+ out.extend((kind, list(bucket.values())) for kind, bucket in merged.items())+ return out.to_bytes()+ except Exception as exc: # noqa: BLE001 - keep torch's own bytes+ _warn(+ logger,+ f"compile-cache: could not merge artifact groups ({exc}); saving torch's bundle as is",+ )+ return data++def _write_bundle(ctx: CacheContext, logger: Any) -> bool:"""The save itself. Runs on the caller's thread (``save``) or the worker (``save_async``)."""if not _save_enabled(ctx.mode) or ctx.saved:@@ -935,7 +988,10 @@ def _write_bundle(ctx: CacheContext, logger: Any) -> bool:shapes = sorted(list(s) for s in ctx.shapes)try:import torch # noqa: PLC0415- result = torch.compiler.save_cache_artifacts()++ # an artifact recorded mid-serialise is dropped from pending but stays "seen", so it would never be saved+ with _compile_lock():+ result = torch.compiler.save_cache_artifacts()except Exception as exc: # noqa: BLE001_warn(logger, f"compile-cache: save_cache_artifacts failed: {exc}")return False@@ -943,7 +999,7 @@ def _write_bundle(ctx: CacheContext, logger: Any) -> bool:_warn(logger, "compile-cache: nothing to save (empty artifacts)")return False- data = result[0]+ data = _one_group_per_type(result[0], logger)try:ctx.dir.mkdir(parents = True, exist_ok = True)digest = hashlib.sha256(data).hexdigest()diff --git a/studio/backend/tests/test_diffusion_compile_cache.py b/studio/backend/tests/test_diffusion_compile_cache.pyindex ae4a121ce1e..d6c2be3f505 100644--- a/studio/backend/tests/test_diffusion_compile_cache.py+++ b/studio/backend/tests/test_diffusion_compile_cache.py@@ -1462,3 +1462,131 @@ def test_evict_counts_a_key_another_evictor_took(monkeypatch, tmp_path):monkeypatch.setattr(os, "rename", another_process_got_there_first)assert cc.evict(root = tmp_path, max_bytes = 1500) == []assert not taken.exists() and nxt.exists()+++def _bundle_ctx(tmp_path):+ return cc.CacheContext(+ key = "abc",+ dir = tmp_path / "abc",+ bundle = tmp_path / "abc" / "cache.bin",+ manifest_path = tmp_path / "abc" / "manifest.json",+ env_fp = "e",+ model_fp = "m",+ mode = "auto",+ )+++def _grouped_mega_cache():+ # The per-type group format these tests exercise is torch 2.8+; torch 2.7 wrote one flat artifact list.+ cache_mod = pytest.importorskip("torch.compiler._cache")+ if not hasattr(cache_mod, "_deserialize_single_cache"):+ pytest.skip("torch build without per-type artifact groups")+ return cache_mod+++def test_a_resave_after_a_recompile_restores_every_graph(tmp_path, monkeypatch):+ # Before the merge, a re-saved bundle restored only the newest per-type group (torch loads them with dict()).+ _grouped_mega_cache()+ from torch.compiler._cache import CacheArtifactManager as M++ monkeypatch.setenv("UNSLOTH_DIFFUSION_COMPILE_CACHE_DIR", str(tmp_path))+ M._ensure_cache_artifacts_registered()+ M.clear()+ try:+ M.record_artifact("inductor", "first-resolution", b"one")+ assert cc._write_bundle(_bundle_ctx(tmp_path), None)+ M.record_artifact("inductor", "second-resolution", b"two")+ ctx = _bundle_ctx(tmp_path)+ assert cc._write_bundle(ctx, None)+ manifest = json.loads(ctx.manifest_path.read_text())+ data = (ctx.dir / manifest["bundle"]).read_bytes()+ restored = M.deserialize(data)+ assert sorted(a.key for a in restored["inductor"]) == [+ "first-resolution",+ "second-resolution",+ ]+ finally:+ M.clear()+++def test_a_resave_keeps_both_contents_of_a_repeated_inductor_key(tmp_path, monkeypatch):+ _grouped_mega_cache()+ from torch.compiler._cache import CacheArtifactManager as M++ monkeypatch.setenv("UNSLOTH_DIFFUSION_COMPILE_CACHE_DIR", str(tmp_path))+ M._ensure_cache_artifacts_registered()+ M.clear()+ try:+ M.record_artifact("inductor", "kept", b"one")+ M.record_artifact("inductor", "tuned", b"old")+ assert cc._write_bundle(_bundle_ctx(tmp_path), None)+ M.record_artifact("inductor", "tuned", b"new")+ ctx = _bundle_ctx(tmp_path)+ assert cc._write_bundle(ctx, None)+ manifest = json.loads(ctx.manifest_path.read_text())+ restored = M.deserialize((ctx.dir / manifest["bundle"]).read_bytes())+ assert [(a.key, bytes(a.content)) for a in restored["inductor"]] == [+ ("kept", b"one"),+ ("tuned", b"old"),+ ("tuned", b"new"),+ ]+ finally:+ M.clear()+++def test_a_resave_keeps_every_guarded_candidate_and_applies_a_repeated_autotune_key_last(+ tmp_path, monkeypatch+):+ # Inductor candidates sharing a key are separate guarded entries; an autotune key is one slot, written in order.+ _grouped_mega_cache()+ from torch.compiler._cache import CacheArtifactManager as M++ monkeypatch.setenv("UNSLOTH_DIFFUSION_COMPILE_CACHE_DIR", str(tmp_path))+ M._ensure_cache_artifacts_registered()+ M.clear()+ try:+ M.record_artifact("inductor", "graph", b"shape-a")+ M.record_artifact("autotune", "kernel", {"best": "old"})+ assert cc._write_bundle(_bundle_ctx(tmp_path), None)+ M.record_artifact("inductor", "graph", b"shape-b")+ M.record_artifact("autotune", "kernel", {"best": "new"})+ ctx = _bundle_ctx(tmp_path)+ assert cc._write_bundle(ctx, None)+ manifest = json.loads(ctx.manifest_path.read_text())+ restored = M.deserialize((ctx.dir / manifest["bundle"]).read_bytes())+ assert sorted(bytes(a.content) for a in restored["inductor"]) == [b"shape-a", b"shape-b"]+ assert json.loads(restored["autotune"][-1].content) == {"best": "new"}+ finally:+ M.clear()+++def test_the_bundle_is_serialised_under_torch_compile_lock(tmp_path, monkeypatch):+ # An artifact recorded while torch serialises is cleared from the pending list but stays "seen": lost for good.+ # A compile on another thread (a render, a background compile) records under compile_lock, so the save takes it.+ import threading++ import torch+ from torch._dynamo import convert_frame++ lock = threading.RLock()+ held = []++ def save():+ held.append(lock._is_owned())+ return None++ monkeypatch.setattr(convert_frame, "compile_lock", lock)+ # raising=False: torch 2.6 has no Mega-cache API, the lock is what is under test+ monkeypatch.setattr(torch.compiler, "save_cache_artifacts", save, raising = False)+ cc._write_bundle(_bundle_ctx(tmp_path), None)+ assert held == [True]+++def test_a_flat_artifact_list_is_saved_as_is_without_a_warning(monkeypatch):+ # torch 2.7 (flat artifact list, no _deserialize_single_cache): saves must not log a merge failure.+ cache_mod = pytest.importorskip("torch.compiler._cache")+ monkeypatch.delattr(cache_mod, "_deserialize_single_cache", raising = False)+ warnings = []+ logger = types.SimpleNamespace(warning = lambda *a: warnings.append(a))+ assert cc._one_group_per_type(b"flat-list-bundle", logger) == b"flat-list-bundle"+ assert warnings == []