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
Debugging (#739)
Daniel Han75df21a314db
diff --git a/unsloth/__init__.py b/unsloth/__init__.pyindex 298ed13399f..feb550be5f2 100644--- a/unsloth/__init__.py+++ b/unsloth/__init__.py@@ -39,10 +39,7 @@ if "CUDA_VISIBLE_DEVICES" in os.environ:first_id = devices.split(",")[0]warnings.warn(f"Unsloth: 'CUDA_VISIBLE_DEVICES' is currently {devices} \n"\- "Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so "\- "enabling it will require much more work, so we have to prioritize. Please understand!"\- "We do have a beta version, which you can contact us about!\n"\- "Thank you for your understanding and we appreciate it immensely!\n\n"\+ "Unsloth currently does not support multi GPU setups - but we are working on it!\n"\"Multiple CUDA devices detected but we require a single device.\n"\f"We will override CUDA_VISIBLE_DEVICES to first device: {first_id}.")diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.pyindex fd1b87c5325..e0df218a930 100644--- a/unsloth/models/_utils.py+++ b/unsloth/models/_utils.py@@ -21,6 +21,7 @@ __all__ = ["xformers_version","__version__","HAS_FLASH_ATTENTION",+ "PRE_CHECK","platform_system","patch_tokenizer","get_statistics",@@ -32,30 +33,27 @@ __all__ = ["unsloth_offloaded_gradient_checkpoint","torch_compile_options","patch_linear_scaling",+ "check_nvidia","create_boolean_mask",]import torchfrom typing import Union, Optional, List, Any, Callable, Tuple-import warningsfrom platform import system as platform_systemplatform_system = platform_system()-import mathimport numpy as np-import os-import psutil-import inspect-import re+import warnings, subprocess, re, inspect, psutil, os, math# =============================================# Disable some warnings which can get annoyingwarnings.filterwarnings(action = "ignore", category = UserWarning, module = "torch")warnings.filterwarnings(action = "ignore", category = UserWarning, module = "huggingface_hub")+warnings.filterwarnings(action = "ignore", category = FutureWarning, module = "huggingface_hub")warnings.filterwarnings(action = "ignore", category = RuntimeWarning, module = "subprocess")warnings.filterwarnings(action = "ignore", category = UserWarning, module = "transformers")warnings.filterwarnings(action = "ignore", category = FutureWarning, module = "accelerate")-warnings.filterwarnings(action = "ignore", category = FutureWarning, module = "huggingface_hub")warnings.filterwarnings(action = "ignore", category = RuntimeWarning, module = "multiprocessing")+warnings.filterwarnings(action = "ignore", category = RuntimeWarning, module = "multiprocess")# Stop "Special tokens have been added in the vocabulary, ..."import logging@@ -74,7 +72,10 @@ for model_name in model_architectures:config_filename = f"{model_name.title()}Config"exec(f"from {config_filepath} import {config_filename}", globals())- config = inspect.getsource(eval(config_filename))+ try:+ config = inspect.getsource(eval(config_filename))+ except:+ continueif "rope_scaling" in config: continueconfig = re.sub(r"(\*\*kwargs)[\s]{0,}\,[\s]{0,}\)[\s]{0,}\:",@@ -345,7 +346,6 @@ def get_statistics():# We simply download a README.md file from HF - all data is made public.# This is simply so we can check if some envs are broken or not.try:- from huggingface_hub import hf_hub_downloadfrom huggingface_hub.utils import disable_progress_bars, enable_progress_bars, are_progress_bars_disabledimport psutiln_cpus = psutil.cpu_count(logical = False)@@ -367,7 +367,13 @@ def get_statistics():disable_progress_bars()disabled = Truepass- hf_hub_download(f"unslothai/statistics-{statistics}", "README.md", force_download = True)++ from transformers import AutoModelForCausalLM+ stats_model = AutoModelForCausalLM.from_pretrained(+ f"unslothai/statistics-{statistics}",+ force_download = True,+ )+ del stats_modelif disabled:enable_progress_bars()pass@@ -659,6 +665,19 @@ def patch_linear_scaling(pass+def check_nvidia():+ # Unsloth doesn't work yet on AMD devices - we're working on it!+ try:+ output = subprocess.check_output("nvidia-smi --query-gpu=memory.used --format=csv", shell = True)+ except:+ raise RuntimeError("Unsloth: We do not support AMD / Intel machines yet - it is a work in progress!")+ output = re.findall(rb'([\d]{1,})[\s]{1,}M', output)+ output = np.array([int(x.decode('utf-8'))/1024 for x in output])+ return output+pass+PRE_CHECK = check_nvidia()++def create_boolean_mask(n = 4096, sliding_window = 2048):# Creates a boolean mask for attentionmask = torch.ones(n, n, dtype = torch.bool)diff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex c7ae67e4282..32f2c6b71c1 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -83,7 +83,8 @@ def _fast_prepare_inputs_for_generation(self, input_ids, **kwargs,):if "past_key_values" in kwargs:input_ids = input_ids[:,[-1]]kwargs["attention_mask"] = kwargs["attention_mask"][:,[-1]]- kwargs["position_ids"] = kwargs["cache_position"]+ if "cache_position" in kwargs:+ kwargs["position_ids"] = kwargs["cache_position"]return { "input_ids" : input_ids, **kwargs, }pass@@ -1128,7 +1129,7 @@ class FastLlamaModel:f' "-____-" Free Apache license: http://github.com/unslothai/unsloth'print(statistics)model_patcher.pre_patch()- # get_statistics()+ get_statistics() # For debugging - we use a download counter to see if environments are not breakingif dtype is None:dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16@@ -1180,6 +1181,8 @@ class FastLlamaModel:# Add to kwargskwargs["rope_scaling"] = rope_scalingpass+ # We currently only support NVIDIA GPUs - AMD / Intel is a work in progress!+ pre_check = check_nvidia()bnb_config = Noneif load_in_4bit:@@ -1206,6 +1209,8 @@ class FastLlamaModel:attn_implementation = "eager",**kwargs,)+ # We currently only support NVIDIA GPUs - AMD / Intel is a work in progress!+ post_check = check_nvidia()# Counteract saved tokenizerstokenizer_name = model_name if tokenizer_name is None else tokenizer_name@@ -1235,14 +1240,12 @@ class FastLlamaModel:else:inner_training_loop = Trainer._original_training_loopexcept:- raise RuntimeError(- 'Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so '\- 'enabling it will require much more work, so we have to prioritize. Please understand!\n'\- 'We do have a separate beta version, which you can contact us about!\n'\- 'Thank you for your understanding and we appreciate it immensely!'- )+ raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')pass+ if ((post_check - post_check) >= 1).sum() > 1:+ raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')+import transformers.traineritems_in_trainer = dir(transformers.trainer)good_items = []@@ -1266,16 +1269,15 @@ class FastLlamaModel:f"\\ / Total batch size = {total_train_batch_size:,} | Total steps = {max_steps:,}\\n"\\f' "-____-" Number of trainable parameters = {get_model_param_count(model, trainable_only=True):,}'logger.warning(debug_info)- import subprocess, re, gc- output = subprocess.check_output(- 'nvidia-smi --query-gpu=memory.used --format=csv', shell = True)- output = re.findall(rb'([\\d]{1,})[\\s]{1,}M', output)- output = sum(int(x.decode('utf-8'))/1024 > 4 for x in output)- if output > 1: print(- '********************\\nUnsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so '\\- 'enabling it will require much more work, so we have to prioritize. Please understand!\\n'\\- '********************\\nWe do have a separate beta version, which you can contact us about!\\n'\\- '********************\\nThank you for your understanding and we appreciate it immensely!')+ import subprocess, re, gc, numpy as np+ try:+ a = subprocess.check_output('nvidia-smi --query-gpu=memory.used --format=csv', shell = True)+ except:+ raise RuntimeError('Unsloth: We do not support AMD / Intel machines yet - it is a work in progress!')+ a = re.findall(rb'([\\d]{1,})[\\s]{1,}M', a)+ a = np.array([int(x.decode('utf-8'))/1024 for x in a])+ if ((a - PRE_CHECK) >= 1).sum() > 1:+ raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')for _ in range(3):gc.collect()torch.cuda.empty_cache()"""@@ -1287,12 +1289,7 @@ class FastLlamaModel:debug_info = """n_total_devices = total_train_batch_size // \\args.gradient_accumulation_steps // self._train_batch_sizeif n_total_devices > 1:- logger.warning_once(- '* Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so ' \\- '* enabling it will require much more work, so we have to prioritize. Please understand!\\n' \\- '* We do have a separate beta version, which you can contact us about!\\n'\\- '* Thank you for your understanding and we appreciate it immensely!'- )+ logger.warning_once('Unsloth currently does not support multi GPU setups - but we are working on it!')debug_info ="""debug_info = debug_info.split('\n')debug_info = "\n".join([debug_info[0]] + [spaces + x[8:] for x in debug_info[1:]])@@ -1317,12 +1314,7 @@ class FastLlamaModel:total_batches = bsz * ga * args.world_sizen_total_devices = total_batches // ga // bszif n_total_devices > 1:- logger.warning_once(- '* Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so ' \\- '* enabling it will require much more work, so we have to prioritize. Please understand!\\n' \\- '* We do have a separate beta version, which you can contact us about!\\n'\\- '* Thank you for your understanding and we appreciate it immensely!'- )+ logger.warning_once('Unsloth currently does not support multi GPU setups - but we are working on it!')divisor = n_total_devices / 1bsz = self._train_batch_size = max(int(bsz / divisor), 1)if total_batches // ga // bsz > 1:@@ -1346,12 +1338,7 @@ class FastLlamaModel:"False",)if "n_total_devices >" not in inner_training_loop:- raise RuntimeError(- 'Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so '\- 'enabling it will require much more work, so we have to prioritize. Please understand!\n'\- 'We do have a separate beta version, which you can contact us about!\n'\- 'Thank you for your understanding and we appreciate it immensely!'- )+ raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')passinner_training_loop = inner_training_loop.replace("is_sagemaker_mp_enabled()",diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.pyindex b0bc514f67b..440a53c1f05 100644--- a/unsloth/tokenizer_utils.py+++ b/unsloth/tokenizer_utils.py@@ -24,6 +24,7 @@ import itertoolsimport collectionsimport numpy as npimport gc+import subprocess__all__ = ["load_correct_tokenizer",@@ -907,6 +908,19 @@ def add_new_tokens(pass+def check_nvidia():+ # Unsloth doesn't work yet on AMD devices - we're working on it!+ try:+ output = subprocess.check_output("nvidia-smi --query-gpu=memory.used --format=csv", shell = True)+ except:+ raise RuntimeError("Unsloth: We do not support AMD / Intel machines yet - it is a work in progress!")+ output = re.findall(rb'([\d]{1,})[\s]{1,}M', output)+ output = np.array([int(x.decode('utf-8'))/1024 for x in output])+ return output+pass+PRE_CHECK = check_nvidia()++from inspect import getsourceimport trl.trainer.sft_trainerfrom trl.trainer.sft_trainer import *@@ -957,17 +971,15 @@ def patch_sft_trainer_tokenizer():" 'Please do not edit specific areas of the Unsloth codebase or you will get CUDA segfaults.'\n"\" )\n"\"pass\n"\- "n_devices = torch.cuda.device_count()\n"\- "import subprocess, re\n"\- "output = subprocess.check_output(\n"\- " 'nvidia-smi --query-gpu=memory.used --format=csv', shell = True)\n"\- "output = re.findall(rb'([\\d]{1,})[\\s]{1,}M', output)\n"\- "output = sum(int(x.decode('utf-8'))/1024 > 4 for x in output)\n"\- "if output > 1: print(\n"\- " '********************\\nUnsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so '\\\n"\- " 'enabling it will require much more work, so we have to prioritize. Please understand!\\n'\\\n"\- " '********************\\nWe do have a separate beta version, which you can contact us about!\\n'\\\n"\- " '********************\\nThank you for your understanding and we appreciate it immensely!')\n"\+ "import subprocess, re, gc, numpy as np\n"\+ "try:\n"\+ " a = subprocess.check_output('nvidia-smi --query-gpu=memory.used --format=csv', shell = True)\n"\+ "except:\n"\+ " raise RuntimeError('Unsloth: We do not support AMD / Intel machines yet - it is a work in progress!')\n"\+ "a = re.findall(rb'([\\d]{1,})[\\s]{1,}M', a)\n"\+ "a = np.array([int(x.decode('utf-8'))/1024 for x in a])\n"\+ "if ((a - PRE_CHECK) >= 1).sum() > 1:\n"\+ " raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')\n"\"for _ in range(3):\n"\" gc.collect()\n"\" torch.cuda.empty_cache()\n"\