From 76cd1dc24c7007c90c7ac7a1f1400329144df3ae Mon Sep 17 00:00:00 2001 From: Manan17 Date: Wed, 18 Feb 2026 08:18:13 +0000 Subject: [PATCH] fixing the hangup of training after multiple back to back training processes --- studio/backend/core/training/trainer.py | 8 ++++++++ studio/backend/utils/hardware/hardware.py | 1 + 2 files changed, 9 insertions(+) diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py index df742f64f0..396c6c4956 100644 --- a/studio/backend/core/training/trainer.py +++ b/studio/backend/core/training/trainer.py @@ -115,6 +115,14 @@ class UnslothTrainer: is_dataset_multimodal: bool = False) -> bool: """Load model for training (supports both text and vision models)""" try: + if self.model is not None: + del self.model + if self.tokenizer is not None: + del self.tokenizer + + if self.trainer is not None: + del self.trainer + print("\nClearing GPU memory before training...") clear_gpu_cache() diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py index 754ef6fdae..0d7cc97cfb 100644 --- a/studio/backend/utils/hardware/hardware.py +++ b/studio/backend/utils/hardware/hardware.py @@ -120,6 +120,7 @@ def clear_gpu_cache(): if device == DeviceType.CUDA: import torch + torch.cuda.synchronize() torch.cuda.empty_cache() torch.cuda.ipc_collect() elif device == DeviceType.MLX: