Skip to content

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.

fix: enable XPU support for tests with agnostic test selection (#8319)

JoshuaL300067e67adb56b2
diff --git a/tests/fast_inference/test_fast_inference.py b/tests/fast_inference/test_fast_inference.py
index d482dbd6c25..dcdc43ee860 100644
--- a/tests/fast_inference/test_fast_inference.py
+++ b/tests/fast_inference/test_fast_inference.py
@@ -15,7 +15,7 @@ Kept deliberately tiny so it finishes in well under a minute: a 0.6B model,
prompts/completions. Seeded, so the asserted metrics are reproducible.
Run directly (`python tests/fast_inference/test_fast_inference.py`) or via
-pytest; it skips automatically when no CUDA device is present.
+pytest; it skips automatically when no GPU device is present.
"""
import math
@@ -29,7 +29,6 @@ import pytest
from real_accelerator import (
has_real_accelerator,
) # tests/_shared, on sys.path via tests/conftest.py
-import torch
from tests.utils import header_footer_context
@@ -86,7 +85,7 @@ def _metric(metrics, *names):
return None
[email protected](not has_real_accelerator(), reason = "fast_inference needs a CUDA GPU + vLLM")
[email protected](not has_real_accelerator(), reason = "fast_inference needs an accelerator + vLLM")
def test_fast_inference():
# Import here, not at module load: importing unsloth probes for an accelerator and errors on CPU-only machines, so
# deferring keeps pytest collection and the skip path import-free. Unsloth must precede TRL.
@@ -193,7 +192,7 @@ def test_fast_inference():
if __name__ == "__main__":
- if torch.cuda.is_available():
+ if has_real_accelerator():
test_fast_inference()
else:
- print("Skipping fast_inference test: needs a CUDA GPU + vLLM")
+ print("Skipping fast_inference test: needs an accelerator + vLLM")
diff --git a/tests/saving/test_gguf_export_and_inference.py b/tests/saving/test_gguf_export_and_inference.py
index 3d46a3b7c8c..5075cc8079c 100644
--- a/tests/saving/test_gguf_export_and_inference.py
+++ b/tests/saving/test_gguf_export_and_inference.py
@@ -7,7 +7,7 @@ Trains a tiny LoRA to imprint a distinctive phrase, exports a full-model q8_0 GG
* if a `llama-cli` binary is available: runs one bounded generation and asserts the trained
phrase round-trips through HF -> GGUF -> quantize -> inference.
-Skipped without CUDA (the export needs a real train + merge). The llama-cli step is skipped
+Skipped without an accelerator (the export needs a real train + merge). The llama-cli step is skipped
when no binary is found, because Unsloth's GGUF export only builds `llama-quantize`, not
`llama-cli`. The generation is hard-bounded (byte cap + watchdog kill) because recent
`llama-cli` builds are conversation-first and otherwise spin on empty stdin.
@@ -22,7 +22,9 @@ import subprocess
import threading
import pytest
-import torch
+from real_accelerator import (
+ has_real_accelerator,
+) # tests/_shared, on sys.path via tests/conftest.py
from unsloth import FastLanguageModel
@@ -32,8 +34,8 @@ from unsloth import FastLanguageModel
pytestmark = [
pytest.mark.gpu,
pytest.mark.skipif(
- not torch.cuda.is_available(),
- reason = "GGUF export smoke test needs a GPU to train + merge",
+ not has_real_accelerator(),
+ reason = "GGUF export smoke test needs an accelerator to train + merge",
),
]
diff --git a/tests/saving/test_save_method_lora_routing.py b/tests/saving/test_save_method_lora_routing.py
index e8879cc4e3f..2b1c75aa352 100644
--- a/tests/saving/test_save_method_lora_routing.py
+++ b/tests/saving/test_save_method_lora_routing.py
@@ -423,6 +423,7 @@ def _adapter_save_environment(monkeypatch):
cuda = types.SimpleNamespace(empty_cache = lambda: None),
),
fast_save_pickle = lambda *args, **kwargs: None,
+ clean_gpu_cache = lambda: None,
), uploads
diff --git a/unsloth/save.py b/unsloth/save.py
index 4d7e119f16c..97367224498 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -71,6 +71,7 @@ from .models.loader_utils import (
from .models._utils import _convert_torchao_model
from .models.mistral_format import raise_if_merging_mistral_format_view
from .ollama_template_mappers import OLLAMA_TEMPLATES, MODEL_TO_OLLAMA_TEMPLATE_MAPPER
+from .device_type import clean_gpu_cache
from transformers import ProcessorMixin, PreTrainedTokenizerBase
from huggingface_hub import HfApi
@@ -994,7 +995,7 @@ def unsloth_save_model(
assert maximum_memory_usage > 0 and maximum_memory_usage <= 0.95
for _ in range(3):
- torch.cuda.empty_cache()
+ clean_gpu_cache()
gc.collect()
save_method = save_method.lower().replace(" ", "_")
@@ -1479,17 +1480,17 @@ def unsloth_save_model(
for j, (key, value) in enumerate(state_dict.items()):
state_dict[key] = None
if j % 10 == 0:
- torch.cuda.empty_cache()
+ clean_gpu_cache()
gc.collect()
state_dict = None
del state_dict
- torch.cuda.empty_cache()
+ clean_gpu_cache()
gc.collect()
shutil.rmtree(temporary_location, ignore_errors = True)
for _ in range(3):
- torch.cuda.empty_cache()
+ clean_gpu_cache()
gc.collect()
return save_directory, username
@@ -4128,8 +4129,7 @@ def unsloth_save_pretrained_gguf(
for _ in range(3):
import gc
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
+ clean_gpu_cache()
try:
model_dtype = dtype_from_config(self.config)
@@ -6104,10 +6104,7 @@ def _unsloth_save_torchao_with_given_config(
model_restore = _offload_model_for_quantize_subprocess(model)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
- if hasattr(torch, "xpu") and torch.xpu.is_available():
- torch.xpu.empty_cache()
+ clean_gpu_cache()
# The original stays offloaded until the quantized copy is saved AND released, else both are resident at once and the restore OOMs.
try:
@@ -6145,10 +6142,7 @@ def _unsloth_save_torchao_with_given_config(
traceback.clear_frames(_exc.__traceback__)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
- if hasattr(torch, "xpu") and torch.xpu.is_available():
- torch.xpu.empty_cache()
+ clean_gpu_cache()
_restore_model_after_quantize_subprocess(model, model_restore)
if os.path.exists(save_directory):
@@ -6640,8 +6634,7 @@ def _unsloth_save_compressed_tensors(
model_restore = _offload_model_for_quantize_subprocess(model)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
+ clean_gpu_cache()
# Same boundary as the LoRA GGUF converter: False scrubs, it does not merely withhold.
env = os.environ.copy()
@@ -6707,8 +6700,7 @@ def _unsloth_save_compressed_tensors(
shutil.rmtree(work_tmp, ignore_errors = True)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
+ clean_gpu_cache()
def _unsloth_save_torchao(
@@ -6827,14 +6819,10 @@ def _unsloth_save_torchao(
auto_processor = AutoProcessor if is_vlm else AutoTokenizer
# Free the in-memory model's accelerator memory before reloading from disk, else it sits beside the copy and OOMs a device that fit the model once. Covers CUDA, XPU and multi-GPU dispatched shards, which a plain .to("cpu") cannot move.
- _has_xpu = hasattr(torch, "xpu") and torch.xpu.is_available()
model_restore = _offload_model_for_quantize_subprocess(model)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
- if _has_xpu:
- torch.xpu.empty_cache()
+ clean_gpu_cache()
# Reload the staged 16bit checkpoint with torchao applied: bfloat16 is required, and device_map="auto" falls back to CPU, so this works on any hardware.
print(f"Unsloth: Quantizing the merged model to torchao {kind}...")
@@ -6853,8 +6841,7 @@ def _unsloth_save_torchao(
del quantized_model
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
+ clean_gpu_cache()
cfg_path = os.path.join(out_dir, "config.json")
cfg = {}
@@ -6904,17 +6891,13 @@ def _unsloth_save_torchao(
traceback.clear_frames(_exc.__traceback__)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
- if hasattr(torch, "xpu") and torch.xpu.is_available():
- torch.xpu.empty_cache()
+ clean_gpu_cache()
_restore_model_after_quantize_subprocess(model, model_restore)
if work_tmp is not None:
shutil.rmtree(work_tmp, ignore_errors = True)
for _ in range(3):
gc.collect()
- if torch.cuda.is_available():
- torch.cuda.empty_cache()
+ clean_gpu_cache()
def unsloth_save_pretrained_torchao(