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 export: skip the GPU cache clear when nothing was loaded since the last one (#15737)
Daniel Hana121a0701815
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.pyindex 4d37ee119ae..60894b33c10 100644--- a/studio/backend/core/export/export.py+++ b/studio/backend/core/export/export.py@@ -746,6 +746,8 @@ class ExportBackend:self._audio_type = Noneself.decision = Noneself.diffusion_lm_profile = None+ # True from a load's start until cleanup_memory clears the GPU cache.+ self._gpu_cache_dirty = Falsedef _diffusion_refusal(self, method: str) -> Optional[str]:profile = getattr(self, "diffusion_lm_profile", None)@@ -761,6 +763,13 @@ class ExportBackend:logger.info("Starting memory cleanup...")model_names = list(self.inference_backend.models.keys())+ # Before the resets below; no flag (built without __init__) always clears.+ needs_clear = (+ bool(model_names)+ or self.current_model is not None+ or self.current_tokenizer is not None+ or getattr(self, "_gpu_cache_dirty", True)+ )for model_name in model_names:self.inference_backend.unload_model(model_name)@@ -772,7 +781,9 @@ class ExportBackend:self._audio_type = Noneself.decision = None- clear_gpu_cache()+ if needs_clear:+ clear_gpu_cache()+ self._gpu_cache_dirty = Falselogger.info("Memory cleanup completed successfully")return True@@ -833,6 +844,7 @@ class ExportBackend:logger.info(f"Loading checkpoint: {checkpoint_path}")self.cleanup_memory()+ self._gpu_cache_dirty = Trueself.diffusion_lm_profile = None# Before the base / audio / vision probes: a decision run never reaches the chat loaders.diff --git a/studio/backend/tests/test_export_cleanup_skip.py b/studio/backend/tests/test_export_cleanup_skip.pynew file mode 100644index 00000000000..2bf7335bdc3--- /dev/null+++ b/studio/backend/tests/test_export_cleanup_skip.py@@ -0,0 +1,119 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""cleanup_memory clears the GPU cache only when something was loaded since the last clear."""++from __future__ import annotations++import sys+from pathlib import Path++import pytest++_BACKEND_DIR = Path(__file__).resolve().parent.parent+_TESTS_DIR = Path(__file__).resolve().parent+for _path in (_BACKEND_DIR, _TESTS_DIR):+ if str(_path) not in sys.path:+ sys.path.insert(0, str(_path))++from test_export_absolute_paths import ( # noqa: E402+ _install_export_backend_stubs,+ _load_module,+)+++class _Inference:+ def __init__(self):+ self.models = {}+ self.unloaded = []++ def unload_model(self, name):+ self.unloaded.append(name)+ self.models.pop(name, None)+++def backend(monkeypatch):+ _install_export_backend_stubs(monkeypatch)+ mod = _load_module(+ "test_core_export_backend_cleanup_skip", "core/export/export.py", monkeypatch+ )+ clears = []+ monkeypatch.setattr(mod, "clear_gpu_cache", lambda: clears.append(1))+ inference = _Inference()+ monkeypatch.setattr(mod, "get_inference_backend", lambda: inference)++ def failing_decision_kind(path):+ raise RuntimeError("stop after the cleanup")++ import core.export.decision as decision++ monkeypatch.setattr(decision, "decision_kind", failing_decision_kind)+ return mod, mod.ExportBackend(), clears, inference+++def test_fresh_backend_skips_the_clear_and_a_started_load_restores_it(backend, tmp_path):+ mod, b, clears, _ = backend+ assert b.cleanup_memory() is True+ assert clears == []+ ok, _ = b.load_checkpoint(str(tmp_path / "missing"))+ assert ok is False+ assert clears == [] # load_checkpoint's own cleanup on a fresh backend+ assert b.cleanup_memory() is True+ assert clears == [1] # a load started (and failed) since the last clear+ assert b.cleanup_memory() is True+ assert clears == [1]+++def test_reload_after_the_worker_cleanup_clears_once(backend, tmp_path):+ mod, b, clears, inference = backend+ b._gpu_cache_dirty = True+ b.current_model, b.current_tokenizer = object(), object()+ inference.models["m"] = object()+ b.cleanup_memory() # the worker's own cleanup before a new load+ assert clears == [1] and inference.unloaded == ["m"]+ assert b.current_model is None and b.current_tokenizer is None+ b.load_checkpoint(str(tmp_path / "missing"))+ assert clears == [1]++[email protected]("held", ["model", "tokenizer", "inference"])+def test_anything_still_held_is_cleared(backend, held):+ mod, b, clears, inference = backend+ if held == "model":+ b.current_model = object()+ elif held == "tokenizer":+ b.current_tokenizer = object()+ else:+ inference.models["m"] = object()+ b.cleanup_memory()+ assert clears == [1]+ assert b.current_model is None and b.current_tokenizer is None and inference.models == {}+++def test_a_backend_built_without_init_clears_on_its_first_cleanup(backend):+ mod, _, clears, inference = backend+ b = object.__new__(mod.ExportBackend)+ b.inference_backend = inference+ b.current_model = b.current_tokenizer = None+ b.cleanup_memory()+ assert clears == [1]+ b.current_model = object()+ b.cleanup_memory()+ assert clears == [1, 1]+++def test_a_failed_unload_keeps_the_next_clear(backend, monkeypatch):+ mod, b, clears, inference = backend+ b._gpu_cache_dirty = True++ def boom(name):+ raise RuntimeError("unload failed")++ inference.models["m"] = object()+ monkeypatch.setattr(inference, "unload_model", boom)+ assert b.cleanup_memory() is False+ assert clears == []+ monkeypatch.setattr(inference, "unload_model", _Inference.unload_model.__get__(inference))+ assert b.cleanup_memory() is True+ assert clears == [1]