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
Fixing stuck training processes
Manan17ace86ed0b8f6
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.pyindex 013c7817b27..5a3d8683e23 100644--- a/studio/backend/core/training/trainer.py+++ b/studio/backend/core/training/trainer.py@@ -2,13 +2,16 @@Unsloth Training BackendIntegrates Unsloth training capabilities with the FastAPI backend"""+import os+# Prevent tokenizer parallelism deadlocks when datasets uses multiprocessing fork+os.environ["TOKENIZERS_PARALLELISM"] = "false"+import torchfrom 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-import osimport jsonimport threadingimport mathdiff --git a/studio/backend/core/training/training.py b/studio/backend/core/training/training.pyindex 62febadc136..62aa021136f 100644--- a/studio/backend/core/training/training.py+++ b/studio/backend/core/training/training.py@@ -6,6 +6,7 @@ from typing import Any, Generator, Tupleimport loggingfrom .trainer import get_trainer, TrainingProgress+from utils.hardware import clear_gpu_cachelogger = logging.getLogger(__name__)@@ -101,6 +102,31 @@ class TrainingBackend:True if training started successfully, False otherwise."""try:+ # Wait for any previous training thread to finish+ old_thread = getattr(self.trainer, "training_thread", None)+ if old_thread and old_thread.is_alive():+ logger.info("Waiting for previous training thread to finish...")+ old_thread.join(timeout=30)++ # Explicitly free old SFTTrainer and CUDA resources before loading new model.+ # Without this, forked multiprocessing workers (num_proc tokenization) inherit+ # stale CUDA state from the previous run, causing extreme slowdowns or crashes.+ if self.trainer.trainer is not None:+ logger.info("Cleaning up previous SFTTrainer...")+ self.trainer.trainer = None+ if self.trainer.model is not None:+ self.trainer.model = None+ if self.trainer.tokenizer is not None:+ self.trainer.tokenizer = None+ # Flush all pending async CUDA ops so forked tokenization processes+ # don't inherit stale async state that causes pool join to hang.+ import torch as _torch+ if _torch.cuda.is_available():+ _torch.cuda.synchronize()+ import gc+ gc.collect()+ clear_gpu_cache()+# Reset stop flag and clear historyself.trainer.should_stop = Falseself.trainer.save_on_stop = True