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: restore ROCm TunableOp state when the last diffusion pipeline unloads (#17340)
Daniel Hane5aeca4c0232
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex bf9aa62d07e..2638ada17ed 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -319,6 +319,7 @@ from .diffusion_speed import (normalize_speed_mode,resolve_speed_mode,family_render_cudnn_scope,+ release_rocm_tunableop as _release_rocm_tunableop,restore_backend_flags,settle_compile_fallback,snapshot_backend_flags,@@ -11654,6 +11655,7 @@ class DiffusionBackend:load_bg_compile.close()load_bg_compile = Nonerestore_backend_flags(backend_flags_before)+ _release_rocm_tunableop(pipe)compile_cache.restore(compile_ctx, logger = logger)gguf_compile.uninstall_all() # idempotentcuda_graph.uninstall_all(getattr(pipe, "_unsloth_cuda_graphs", ()) or ())@@ -15985,6 +15987,7 @@ class DiffusionBackend:pass# Restore process-wide backend flags so the next `off` load is bit-identical; all calls idempotent.restore_backend_flags(state.backend_flags_before)+ _release_rocm_tunableop(state.pipe)prebuild = sys.modules.get(f"{__package__}.diffusion_vae_prebuild")if prebuild is not None:prebuild.cancel_all()diff --git a/studio/backend/core/inference/diffusion_rocm_tunableop.py b/studio/backend/core/inference/diffusion_rocm_tunableop.pyindex c454a8c26db..8b19dde6ee2 100644--- a/studio/backend/core/inference/diffusion_rocm_tunableop.py+++ b/studio/backend/core/inference/diffusion_rocm_tunableop.py@@ -10,6 +10,7 @@ from __future__ import annotationsimport osimport reimport threading+import weakreffrom pathlib import Pathfrom typing import Any, Optional@@ -21,8 +22,9 @@ TUNED_ARCHES = frozenset({"gfx1151"})MAX_TUNING_MS = 30MAX_TUNING_ITERS = 20_HOOK_ATTR = "_unsloth_tunableop_hooks"+_OWNER_ATTR = "_unsloth_tunableop_owner"-_LOCK = threading.Lock()+_LOCK = threading.RLock()_STATE: dict = {"active": False,"depth": 0,@@ -30,6 +32,9 @@ _STATE: dict = {"file": None,"last_key": None,"tune": False,+ # Pipe tags using the table; the last release() restores prev_enabled.+ "owners": set(),+ "prev_enabled": False,}@@ -208,6 +213,10 @@ def _activate(arch: str, logger: Any = None) -> bool:except OSError:if tune:return False+ try:+ prev_enabled = bool(tunable.is_enabled())+ except Exception: # noqa: BLE001+ prev_enabled = Falsetunable.tuning_enable(False)tunable.enable(True)_prime(torch)@@ -220,7 +229,7 @@ def _activate(arch: str, logger: Any = None) -> bool:str(k): str(v) for k, v in validators.items()}if not tune and not tables and not cached:- tunable.enable(False)+ tunable.enable(prev_enabled)return Falsetunable.set_filename(str(path), insert_device_ordinal = False)tunable.set_max_tuning_duration(MAX_TUNING_MS)@@ -234,7 +243,14 @@ def _activate(arch: str, logger: Any = None) -> bool:_STATE["written"] = len(tunable.get_results() or ())except Exception: # noqa: BLE001_STATE["written"] = 0- _STATE.update(active = True, depth = 0, file = str(path), last_key = None, tune = tune)+ _STATE.update(+ active = True,+ depth = 0,+ file = str(path),+ last_key = None,+ tune = tune,+ prev_enabled = prev_enabled,+ )if logger is not None:logger.info("diffusion.rocm_tunableop: %s GEMMs use the measured fastest solution for %d shapes (%s)%s",@@ -282,8 +298,14 @@ def install_for_pipe(uninstall(None)return Falsedits = _denoisers(pipe)- if not dits or not _activate(why, logger):+ if not dits:return False+ with (+ _LOCK+ ): # activation and the claim are one step, so a concurrent last release cannot undo them+ if not _activate(why, logger):+ return False+ _claim(pipe)if not _STATE.get("tune"):return Truefor dit in dits:@@ -303,14 +325,24 @@ def uninstall(pipe: Any = None) -> None:except Exception: # noqa: BLE001returnwith _LOCK:- if _STATE["active"]:- _write(tunable)- try:- tunable.tuning_enable(False)- tunable.enable(False)- except Exception: # noqa: BLE001- pass- _STATE.update(active = False, depth = 0)+ _restore(tunable)+ _remove_hooks(pipe)+++def _restore(tunable: Any) -> None:+ # Caller holds _LOCK.+ if _STATE["active"]:+ _write(tunable)+ try:+ tunable.tuning_enable(False)+ tunable.enable(bool(_STATE.get("prev_enabled")))+ except Exception: # noqa: BLE001+ pass+ _STATE["owners"].clear()+ _STATE.update(active = False, depth = 0)+++def _remove_hooks(pipe: Any) -> None:for dit in _denoisers(pipe) if pipe is not None else ():for h in dit.__dict__.pop(_HOOK_ATTR, None) or ():try:@@ -319,5 +351,65 @@ def uninstall(pipe: Any = None) -> None:pass+def _claim(pipe: Any) -> None:+ # Tag on the pipe so views over it (second expert, named DiT) share one owner.+ token = getattr(pipe, _OWNER_ATTR, None)+ if token is None:+ token = object()+ try:+ setattr(pipe, _OWNER_ATTR, token)+ except Exception: # noqa: BLE001 - untaggable: never released, stays on as before+ return+ root = pipe+ while "_pipe" in getattr(+ root, "__dict__", {}+ ): # a view: the claim lives on the pipe it wraps+ root = root.__dict__["_pipe"]+ try:+ # A load that fails or is cancelled drops the pipe without release(); collecting it ends the claim.+ weakref.finalize(root, _drop, token)+ except TypeError:+ pass+ with _LOCK:+ _STATE["owners"].add(token)+++def _drop(token: object) -> None:+ with _LOCK:+ if token not in _STATE["owners"]:+ return+ _STATE["owners"].discard(token)+ if _STATE["active"] and not _STATE["owners"]:+ try:+ import torch+ _restore(torch.cuda.tunable)+ except Exception: # noqa: BLE001+ pass+++def release(pipe: Any) -> None:+ """Pipeline teardown: drop its claim; the last owner out restores the pre-activation TunableOp state."""+ if pipe is None:+ return+ token = getattr(pipe, _OWNER_ATTR, None)+ if token is None:+ return+ try:+ delattr(pipe, _OWNER_ATTR)+ except Exception: # noqa: BLE001+ pass+ with (+ _LOCK+ ): # the last-owner check and the restore must not interleave with another load's claim+ _STATE["owners"].discard(token)+ if _STATE["active"] and not _STATE["owners"]:+ try:+ import torch+ _restore(torch.cuda.tunable)+ except Exception: # noqa: BLE001+ pass+ _remove_hooks(pipe)++def is_active() -> bool:return bool(_STATE["active"])diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.pyindex c21571f2118..2ee06104a8e 100644--- a/studio/backend/core/inference/diffusion_speed.py+++ b/studio/backend/core/inference/diffusion_speed.py@@ -192,6 +192,17 @@ def snapshot_backend_flags() -> Optional[dict]:return state+def release_rocm_tunableop(pipe: Any) -> None:+ """Unload: drop this pipe's TunableOp claim (process-global, so not in snapshot_backend_flags)."""+ mod = sys.modules.get(f"{__package__}.diffusion_rocm_tunableop")+ if mod is None or pipe is None:+ return+ try:+ mod.release(pipe)+ except Exception: # noqa: BLE001 - teardown is best effort+ pass++def restore_backend_flags(state: Optional[dict]) -> None:"""Restore the flags captured by ``snapshot_backend_flags``. No-op on None. Each flag isrestored independently so one failure can't leak the others."""diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.pyindex 790d9f19da6..6b0902848fa 100644--- a/studio/backend/core/inference/video.py+++ b/studio/backend/core/inference/video.py@@ -188,6 +188,7 @@ from .diffusion_speed import (arm_graphs_after_placement,engage_pinned_denoisers,rocm_probe_load_scoped,+ release_rocm_tunableop as _release_rocm_tunableop,resolve_speed_mode,restore_backend_flags,settle_compile_fallback,@@ -15159,6 +15160,7 @@ class VideoBackend:except Exception: # noqa: BLE001 -- teardown is best effortpassrestore_backend_flags(state.backend_flags)+ _release_rocm_tunableop(getattr(state, "pipe", None))# Restore stock GGUF kernels so a later speed=off load is bit-identical.from . import diffusion_gguf_compilefrom . import diffusion_cuda_graphdiff --git a/studio/backend/tests/test_diffusion_rocm_tunableop_teardown.py b/studio/backend/tests/test_diffusion_rocm_tunableop_teardown.pynew file mode 100644index 00000000000..bbab2f09284--- /dev/null+++ b/studio/backend/tests/test_diffusion_rocm_tunableop_teardown.py@@ -0,0 +1,205 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Unloading a diffusion pipeline restores the process-global TunableOp state (a fake torch.cuda.tunable; no GPU)."""++from __future__ import annotations++import types++import pytest++torch = pytest.importorskip("torch")+from torch import nn # noqa: E402++from core.inference import diffusion_rocm_tunableop as rt # noqa: E402+from core.inference import diffusion_speed # noqa: E402++_VALIDATORS = (("PT_VERSION", "2.11.0"), ("GCN_ARCH_NAME", "gfx1151"))+++class FakeTunable:+ def __init__(self, enabled = False):+ self.enabled = enabled+ self.tuning = False++ def enable(self, v = True):+ self.enabled = bool(v)++ def is_enabled(self):+ return self.enabled++ def tuning_enable(self, v = True):+ self.tuning = bool(v)++ def set_filename(+ self,+ name,+ insert_device_ordinal = False,+ ):+ pass++ def set_max_tuning_duration(self, ms):+ pass++ def set_max_tuning_iterations(self, n):+ pass++ def read_file(self, name = None):+ return True++ def get_results(self):+ return ()++ def get_validators(self):+ return _VALIDATORS+++def fake(monkeypatch, tmp_path):+ f = FakeTunable()+ for name in (+ "enable",+ "is_enabled",+ "tuning_enable",+ "set_filename",+ "set_max_tuning_duration",+ "set_max_tuning_iterations",+ "read_file",+ "get_results",+ "get_validators",+ ):+ monkeypatch.setattr(torch.cuda.tunable, name, getattr(f, name))+ monkeypatch.setattr(rt, "is_rocm_torch", lambda _t: True)+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)+ monkeypatch.setattr(rt, "_arch", lambda device = None: "gfx1151")+ monkeypatch.setattr(rt, "_prime", lambda _torch: None)+ monkeypatch.setenv("UNSLOTH_DIFFUSION_COMPILE_CACHE_DIR", str(tmp_path))+ monkeypatch.delenv(rt.TUNABLEOP_ENV, raising = False)+ for k in [k for k in __import__("os").environ if k.startswith("PYTORCH_TUNABLEOP_")]:+ monkeypatch.delenv(k)+ tables = tmp_path / "tables"+ tables.mkdir()+ lines = [f"Validator,{k},{v}" for k, v in _VALIDATORS]+ lines.append("GemmTunableOp_BFloat16_TN,tn_8_8_8_ld_8_8_8,Gemm_Hipblaslt_1,1.0")+ (tables / "gfx1151_test.csv").write_text("\n".join(lines) + "\n", encoding = "utf-8")+ monkeypatch.setattr(rt, "_TABLE_DIR", tables)+ rt._STATE.update(active = False, depth = 0, written = 0, file = None, tune = False)+ rt._STATE.get("owners", set()).clear()+ yield f+ rt._STATE.update(active = False, depth = 0, written = 0, file = None, tune = False)+ rt._STATE.get("owners", set()).clear()+++def _pipe():+ return types.SimpleNamespace(transformer = nn.Linear(4, 4), vae = nn.Linear(4, 4))+++def test_unload_restores_tunableop_only_after_the_last_pipeline(fake):+ image, video = _pipe(), _pipe()+ assert rt.install_for_pipe(image) and fake.enabled+ assert rt.install_for_pipe(video)+ diffusion_speed.release_rocm_tunableop(image)+ assert fake.enabled and rt.is_active() # the co-resident pipeline still uses the table+ diffusion_speed.release_rocm_tunableop(video)+ assert not fake.enabled and not rt.is_active()+++def test_unload_restores_a_previously_enabled_state(fake):+ fake.enabled = True+ pipe = _pipe()+ assert rt.install_for_pipe(pipe)+ diffusion_speed.release_rocm_tunableop(pipe)+ assert fake.enabled and not rt.is_active()+++def test_user_owned_tunableop_is_left_alone(fake, monkeypatch):+ monkeypatch.setenv("PYTORCH_TUNABLEOP_ENABLED", "1")+ fake.enabled = True+ pipe = _pipe()+ assert not rt.install_for_pipe(pipe)+ diffusion_speed.release_rocm_tunableop(pipe)+ assert fake.enabled+++def test_image_and_video_teardown_release_tunableop():+ import ast+ from pathlib import Path++ root = Path(rt.__file__).resolve().parent+ for fname, method in (+ ("diffusion.py", "_unload_locked"),+ ("video.py", "_teardown_state_locked"),+ ):+ tree = ast.parse((root / fname).read_text(encoding = "utf-8"))+ fn = next(n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == method)+ called = {+ c.func.id+ for c in ast.walk(fn)+ if isinstance(c, ast.Call) and isinstance(c.func, ast.Name)+ }+ assert "_release_rocm_tunableop" in called, fname+++def test_a_load_racing_the_last_release_keeps_its_claim(fake, monkeypatch):+ # A load that lands between the last-owner check and the restore must stay installed.+ image, video = _pipe(), _pipe()+ assert rt.install_for_pipe(image)+ raced = []+ real_uninstall = rt.uninstall++ def racing_uninstall(pipe = None):+ if not raced:+ raced.append(rt.install_for_pipe(video))+ return real_uninstall(pipe)++ monkeypatch.setattr(rt, "uninstall", racing_uninstall)+ diffusion_speed.release_rocm_tunableop(image)+ if not raced:+ raced.append(rt.install_for_pipe(video))+ assert raced == [True]+ assert rt.is_active() and fake.enabled and rt._STATE["owners"]+++def test_install_waits_for_a_release_in_progress(fake):+ import threading++ image, video = _pipe(), _pipe()+ assert rt.install_for_pipe(image)+ entered, go = threading.Event(), threading.Event()++ class SlowOwners(set):+ def discard(self, x):+ entered.set()+ go.wait(5)+ return super().discard(x)++ rt._STATE["owners"] = SlowOwners(rt._STATE["owners"])+ try:+ t = threading.Thread(target = diffusion_speed.release_rocm_tunableop, args = (image,))+ t.start()+ assert entered.wait(5)+ result = []+ u = threading.Thread(target = lambda: result.append(rt.install_for_pipe(video)))+ u.start()+ go.set()+ t.join(5)+ u.join(5)+ assert result == [True]+ assert rt.is_active() and fake.enabled and rt._STATE["owners"]+ finally:+ rt._STATE["owners"] = set(rt._STATE["owners"])+++def test_a_failed_load_that_drops_its_pipe_releases_the_claim(fake):+ import gc++ class Pipe: # weakref-able, like a diffusers pipeline+ def __init__(self):+ self.transformer = nn.Linear(4, 4)++ pipe = Pipe()+ assert rt.install_for_pipe(pipe) and fake.enabled+ del pipe # a cancelled video load drops the unpublished pipe without release()+ gc.collect()+ assert not rt.is_active() and not fake.enabled and not rt._STATE["owners"]