feat: clear unsloth_compiled_cache on startup, shutdown, and between model loads
This commit is contained in:
parent
2a117f57a9
commit
dbbcdb4f09
5 changed files with 48 additions and 3 deletions
5
setup.sh
5
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=false
|
||||
keynames=$'\n'$(printenv | cut -d= -f1)
|
||||
|
|
|
|||
|
|
@ -184,6 +184,10 @@ class InferenceBackend:
|
|||
# Clear GPU memory cache
|
||||
clear_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 True
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -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 FastLanguageModel
|
||||
self.is_vlm = is_vision_model(model_name) and is_dataset_multimodal
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ Main FastAPI application for Unsloth UI Backend
|
|||
"""
|
||||
import os
|
||||
import secrets
|
||||
import shutil
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
|
@ -19,12 +18,15 @@ from auth import storage
|
|||
from utils.hardware import detect_hardware, get_device, DeviceType
|
||||
import utils.hardware.hardware as _hw_module
|
||||
|
||||
UNSLOTH_CACHE_DIR = Path(__file__).parent / "unsloth_compiled_cache"
|
||||
from utils.cache_cleanup import clear_unsloth_compiled_cache
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async 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 everywhere
|
||||
detect_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 app
|
||||
|
|
|
|||
30
studio/backend/utils/cache_cleanup.py
Normal file
30
studio/backend/utils/cache_cleanup.py
Normal file
|
|
@ -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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue