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
use get_device() for device selection and clear_gpu_cache() for GPU memory cleanup in inference, trainer, and export
Roland Tannousda1cde971cdb
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.pyindex 11662c9e5f7..8977586ac3f 100644--- a/studio/backend/core/export/export.py+++ b/studio/backend/core/export/export.py@@ -11,6 +11,7 @@ from unsloth import FastLanguageModel, FastVisionModelfrom huggingface_hub import HfApi, ModelCardfrom transformers.modeling_utils import PushToHubMixinimport torch+from utils.hardware import clear_gpu_cachefrom utils.models import is_vision_model, get_base_model_from_lorafrom core.inference import get_inference_backend@@ -69,14 +70,8 @@ class ExportBackend:self.current_tokenizer = Noneself.current_checkpoint = None- # Force garbage collection- import gc- gc.collect()-- # Clear CUDA cache- if torch.cuda.is_available():- torch.cuda.empty_cache()- torch.cuda.ipc_collect()+ # Clear GPU memory cache (handles gc + backend-specific cleanup)+ clear_gpu_cache()logger.info("Memory cleanup completed successfully")return Truediff --git a/studio/backend/core/inference/inference.py b/studio/backend/core/inference/inference.pyindex 70b6f49f321..fe75d6a976f 100644--- a/studio/backend/core/inference/inference.py+++ b/studio/backend/core/inference/inference.py@@ -11,7 +11,8 @@ import torchfrom typing import Optional, Generator, Tuplefrom utils.models import ModelConfig, get_base_model_from_lorafrom utils.paths import is_model_cached-from utils.utils import format_error_message, log_gpu_memory+from utils.utils import format_error_message+from utils.hardware import get_device, clear_gpu_cache, log_gpu_memoryfrom io import StringIOimport logging@@ -35,7 +36,7 @@ class InferenceBackend:"unsloth/Gemma-3-4B-it","unsloth/Qwen2-VL-2B-Instruct-bnb-4bit",]- self.device = "cuda" if torch.cuda.is_available() else "cpu"+ self.device = get_device().value# Thread safetyimport threading@@ -154,12 +155,8 @@ class InferenceBackend:if self.active_model_name == model_name:self.active_model_name = None- # Use garbage collection and clear CUDA cache to release memory- import gc- import torch- gc.collect()- if torch.cuda.is_available():- torch.cuda.empty_cache()+ # Clear GPU memory cache+ clear_gpu_cache()logger.info(f"Model '{model_name}' successfully unloaded.")return True@@ -562,11 +559,11 @@ class InferenceBackend:input_text,add_special_tokens=False,return_tensors="pt",- ).to("cuda")+ ).to(self.device)else:# Text-only for vision modelformatted_prompt = self.format_chat_prompt(messages, system_prompt)- inputs = processor.tokenizer(formatted_prompt, return_tensors="pt").to("cuda")+ inputs = processor.tokenizer(formatted_prompt, return_tensors="pt").to(self.device)# Generate with streamingcaptured_output = StringIO()@@ -888,11 +885,8 @@ class InferenceBackend:for model_name in self.models.keys():self._reset_model_generation_state(model_name)- import torch- if torch.cuda.is_available():- torch.cuda.empty_cache()- torch.cuda.ipc_collect()- logger.debug("Cleared CUDA cache and IPC resources")+ clear_gpu_cache()+ logger.debug("Cleared GPU cache")import gcgc.collect()diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.pyindex 5fc48aa18a9..af5c3ebe01a 100644--- a/studio/backend/core/training/trainer.py+++ b/studio/backend/core/training/trainer.py@@ -3,6 +3,7 @@ Unsloth Training BackendIntegrates Unsloth training capabilities with the Gradio UI"""import torch+from utils.hardware import clear_gpu_cachetorch._dynamo.config.recompile_limit = 64from unsloth import FastLanguageModel, FastVisionModel, is_bfloat16_supportedfrom unsloth.chat_templates import get_chat_template@@ -98,9 +99,7 @@ class UnslothTrainer:"""Load model for training (supports both text and vision models)"""try:print("\nClearing GPU memory before training...")- torch.cuda.empty_cache()- import gc- gc.collect()+ clear_gpu_cache()# Detect if this is a vision model firstself.is_vlm = is_vision_model(model_name)@@ -804,8 +803,7 @@ class UnslothTrainer:self.tokenizer = None# Clear GPU memory- if torch.cuda.is_available():- torch.cuda.empty_cache()+ clear_gpu_cache()def _ensure_deepseek_ocr_installed():