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
add intel gpu with vllm support (#2903)
Lei Zhenyuan74b9feb674c4
diff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.pyindex 5c955a3c8dd..1c65246b312 100644--- a/unsloth/kernels/utils.py+++ b/unsloth/kernels/utils.py@@ -78,7 +78,7 @@ HAS_CUDA_STREAM = False# INTEL GPU specific logicif DEVICE_TYPE == "xpu":# TODO: Changed here after adding XPU BNB support- HAS_XPU_STREAM = False+ HAS_XPU_STREAM = Truedef get_ptr(x: Optional[torch.Tensor]):raise RuntimeError("XPU BNB support is not implemented yet. This function should not be called.")else:diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.pyindex 76eefc3c3cd..f576d17dc13 100644--- a/unsloth/models/_utils.py+++ b/unsloth/models/_utils.py@@ -142,6 +142,12 @@ warnings.filterwarnings(action = "ignore", category = RuntimeWarning, module = "import logginglogging.getLogger("transformers.tokenization_utils_base").setLevel(logging.CRITICAL+1)+def get_device_num():+ if DEVICE_TYPE == "xpu":+ return torch.xpu.device_count()+ else:+ return torch.cuda.device_count()+# Ignore logging messagesclass HideLoggingMessage(logging.Filter):__slots__ = "text",@@ -740,7 +746,7 @@ def get_statistics():passpasstry:- devices = torch.cuda.device_count()+ devices = get_device_num()_get_statistics(f"{devices if devices <= 8 else 9}")except:pass@@ -767,7 +773,7 @@ BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.replace()exec(BitsAndBytesConfig__init__, globals())-if torch.cuda.device_count() == 1:+if get_device_num() == 1:from accelerate.utils.dataclasses import DistributedTypedef _prepare_backend(self, *args, **kwargs): return None, DistributedType.NOimport accelerate.statediff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex 7d56dac2ecd..f08b4762eb8 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -85,6 +85,11 @@ from triton import __version__ as triton_versionHAS_XFORMERS = xformers is not NoneBlockDiagonalCausalMask = xformers.attn_bias.BlockDiagonalCausalMask if HAS_XFORMERS else None+def clean_gpu_cache():+ if DEVICE_TYPE == "xpu":+ torch.xpu.empty_cache()+ else:+ torch.cuda.empty_cache()def original_apply_qkv(self, X):Q = self.q_proj(X)@@ -1752,10 +1757,11 @@ class FastLlamaModel:if not is_vLLM_available():print("Unsloth: vLLM is not installed! Will use Unsloth inference!")fast_inference = False- major_version, minor_version = torch.cuda.get_device_capability()- if major_version < 7:- print("Unsloth: vLLM does not work on older GPUs - will switch to Unsloth inference!")- fast_inference = False+ if DEVICE_TYPE == "cuda":+ major_version, minor_version = torch.cuda.get_device_capability()+ if major_version < 7:+ print("Unsloth: vLLM does not work on older GPUs - will switch to Unsloth inference!")+ fast_inference = Falseif unsloth_vllm_standby and os.environ.get("UNSLOTH_VLLM_STANDBY", "0") == "0":raise RuntimeError("Unsloth: `unsloth_vllm_standby` is True, but environment variable `UNSLOTH_VLLM_STANDBY` is not set to 1!")pass@@ -1779,8 +1785,8 @@ class FastLlamaModel:num_gpus = torch.xpu.device_count()gpu_stats_snippet = f"Intel Toolkit: {gpu_version}."- # TODO: After adding vLLM support for XPU, changed this- vllm_version = ""+ try: vllm_version = f" vLLM: {importlib_version('vllm')}."+ except: vllm_version = ""else:raise ValueError(f"Unsloth: Unsupported device type: {DEVICE_TYPE}")@@ -2020,7 +2026,10 @@ class FastLlamaModel:import gcfor _ in range(3):gc.collect()- torch.cuda.empty_cache()"""+ if DEVICE_TYPE == "xpu":+ torch.xpu.empty_cache()+ else:+ torch.cuda.empty_cache()"""debug_info = debug_info.split('\n')debug_info = "\n".join([debug_info[0]] + [spaces + x[8:] for x in debug_info[1:]])@@ -2508,7 +2517,7 @@ class FastLlamaModel:# Remove old items to save VRAMfor _ in range(3):gc.collect()- torch.cuda.empty_cache()+ clean_gpu_cache()passif train_lm_head:@@ -2519,7 +2528,7 @@ class FastLlamaModel:# Remove old items to save VRAMfor _ in range(3):gc.collect()- torch.cuda.empty_cache()+ clean_gpu_cache()passpass@@ -2580,7 +2589,7 @@ class FastLlamaModel:# Clear deleted GPU itemsfor _ in range(3):gc.collect()- torch.cuda.empty_cache()+ clean_gpu_cache()pass# Patch for fast inference@@ -2796,7 +2805,7 @@ class FastLlamaModel:# Clear deleted GPU itemsfor _ in range(3):gc.collect()- torch.cuda.empty_cache()+ clean_gpu_cache()pass# Patch for fast inferencediff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.pyindex 4cf9174f261..002e4d18630 100644--- a/unsloth/models/rl_replacements.py+++ b/unsloth/models/rl_replacements.py@@ -26,6 +26,8 @@ import torchimport inspectfrom collections import defaultdictfrom unsloth_zoo.rl_replacements import RL_REPLACEMENTS+from unsloth import DEVICE_TYPE+RL_EXTRA_ARGS = defaultdict(list)RL_FUNCTIONS = defaultdict(list)RL_PRE_ITEMS = defaultdict(list)@@ -258,7 +260,7 @@ def grpo_trainer__get_per_token_logps(function_name, function):if os.environ.get('UNSLOTH_FORCE_FLOAT32', '0') == '1': self._autocast_dtype = torch.float16os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "1"- with torch.amp.autocast(device_type = 'cuda', dtype = self._autocast_dtype):+ with torch.amp.autocast(device_type = DEVICE_TYPE, dtype = self._autocast_dtype):# We add 1 to `logits_to_keep` because the last logits of the sequence is later excludedlogits = model(input_ids = input_ids,