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
feat: clear unsloth_compiled_cache on startup, shutdown, and between model loads
Roland Tannousdbbcdb4f09f6
diff --git a/setup.sh b/setup.shindex b54f782ded8..1f55e141fd8 100755--- a/setup.sh+++ b/setup.sh@@ -24,6 +24,11 @@ echo "╔═══════════════════════echo "║ Unsloth Studio Setup Script ║"echo "╚══════════════════════════════════════╝"+# ── Clean up stale Unsloth compiled caches ──+rm -rf "$SCRIPT_DIR/unsloth_compiled_cache"+rm -rf "$SCRIPT_DIR/studio/backend/unsloth_compiled_cache"+rm -rf "$SCRIPT_DIR/studio/tmp/unsloth_compiled_cache"+# ── Detect Colab (like unsloth does) ──IS_COLAB=falsekeynames=$'\n'$(printenv | cut -d= -f1)diff --git a/studio/backend/core/inference/inference.py b/studio/backend/core/inference/inference.pyindex 6423e7a1285..e90e6c0c2ae 100644--- a/studio/backend/core/inference/inference.py+++ b/studio/backend/core/inference/inference.py@@ -184,6 +184,10 @@ class InferenceBackend:# Clear GPU memory cacheclear_gpu_cache()+ # Remove stale compiled cache so the next model gets a fresh one+ from utils.cache_cleanup import clear_unsloth_compiled_cache+ clear_unsloth_compiled_cache()+logger.info(f"Model '{model_name}' successfully unloaded.")return Trueexcept Exception as e:diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.pyindex d78c429e5ae..e996603ae6b 100644--- a/studio/backend/core/training/trainer.py+++ b/studio/backend/core/training/trainer.py@@ -126,6 +126,10 @@ class UnslothTrainer:print("\nClearing GPU memory before training...")clear_gpu_cache()+ # Remove stale compiled cache so the new model gets a fresh one+ from utils.cache_cleanup import clear_unsloth_compiled_cache+ clear_unsloth_compiled_cache()+# Detect if this is a vision model AND dataset is multimodal# A vision-capable model with a text-only dataset should use FastLanguageModelself.is_vlm = is_vision_model(model_name) and is_dataset_multimodaldiff --git a/studio/backend/main.py b/studio/backend/main.pyindex e7a33750ff3..5ccd6cec123 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -3,7 +3,6 @@ Main FastAPI application for Unsloth UI Backend"""import osimport secrets-import shutilfrom contextlib import asynccontextmanagerfrom fastapi import FastAPI@@ -19,12 +18,15 @@ from auth import storagefrom utils.hardware import detect_hardware, get_device, DeviceTypeimport utils.hardware.hardware as _hw_module-UNSLOTH_CACHE_DIR = Path(__file__).parent / "unsloth_compiled_cache"+from utils.cache_cleanup import clear_unsloth_compiled_cache@asynccontextmanagerasync def lifespan(app: FastAPI):"""Startup: detect hardware, print setup token if needed. Shutdown: clean up compiled cache."""+ # Clean up any stale compiled cache from previous runs+ clear_unsloth_compiled_cache()+# Detect hardware first — sets DEVICE global used everywheredetect_hardware()@@ -52,7 +54,7 @@ async def lifespan(app: FastAPI):yield# Cleanup_hw_module.DEVICE = None- shutil.rmtree(UNSLOTH_CACHE_DIR, ignore_errors=True)+ clear_unsloth_compiled_cache()# Create FastAPI appdiff --git a/studio/backend/utils/cache_cleanup.py b/studio/backend/utils/cache_cleanup.pynew file mode 100644index 00000000000..4673cf995f4--- /dev/null+++ b/studio/backend/utils/cache_cleanup.py@@ -0,0 +1,30 @@+"""+Utility for cleaning up the Unsloth compiled cache directory.++The unsloth_compiled_cache is created by unsloth_zoo/compiler.py during+FastModel.from_pretrained() and contains model-type-specific compiled Python+files. It should be cleared between model loads to avoid stale artefacts.+"""+import shutil+import logging+from pathlib import Path++logger = logging.getLogger(__name__)++# Possible locations where unsloth_compiled_cache may appear+_BACKEND_DIR = Path(__file__).resolve().parent.parent # studio/backend+_PROJECT_ROOT = _BACKEND_DIR.parent.parent # repo root++_CACHE_DIRS = [+ _BACKEND_DIR / "unsloth_compiled_cache",+ _PROJECT_ROOT / "unsloth_compiled_cache",+ _PROJECT_ROOT / "studio" / "tmp" / "unsloth_compiled_cache",+]+++def clear_unsloth_compiled_cache() -> None:+ """Remove every known unsloth_compiled_cache directory (idempotent)."""+ for cache_dir in _CACHE_DIRS:+ if cache_dir.exists():+ logger.info(f"Removing unsloth compiled cache: {cache_dir}")+ shutil.rmtree(cache_dir, ignore_errors=True)