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(device_type): flush and fence through the shared device helpers (#12284)

li-lizheed7a07863a39
diff --git a/tests/test_device_helpers.py b/tests/test_device_helpers.py
index c57d9030d09..d3c55390388 100644
--- a/tests/test_device_helpers.py
+++ b/tests/test_device_helpers.py
@@ -187,6 +187,12 @@ def test_model_call_sites_use_shared_cache_dispatch():
gemma_source = (REPO_ROOT / "unsloth" / "models" / "gemma.py").read_text(encoding = "utf-8")
gemma2_source = (REPO_ROOT / "unsloth" / "models" / "gemma2.py").read_text(encoding = "utf-8")
granite_source = (REPO_ROOT / "unsloth" / "models" / "granite.py").read_text(encoding = "utf-8")
+ loader_utils_source = (REPO_ROOT / "unsloth" / "models" / "loader_utils.py").read_text(
+ encoding = "utf-8"
+ )
+ q_galore_source = (REPO_ROOT / "unsloth" / "optimizers" / "q_galore_adamw.py").read_text(
+ encoding = "utf-8"
+ )
assert "torch.xpu.empty_cache()" not in llama_source
assert "torch.xpu.empty_cache()" not in vision_source
@@ -202,6 +208,12 @@ def test_model_call_sites_use_shared_cache_dispatch():
assert "clean_gpu_cache()" in gemma2_source
assert "torch.cuda.empty_cache()" not in granite_source
assert "clean_gpu_cache()" in granite_source
+ # Keyed off cuda alone, the load-retry flush and the optimizer fence were no-ops on NPU / XPU.
+ assert "torch.cuda.empty_cache()" not in loader_utils_source
+ assert "torch.xpu.empty_cache()" not in loader_utils_source
+ assert "clean_gpu_cache()" in loader_utils_source
+ assert "torch.cuda.synchronize()" not in q_galore_source
+ assert "device_synchronize()" in q_galore_source
NPU_PROPERTIES = types.SimpleNamespace(
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index 47c4d191aaf..296920ea38f 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -9,7 +9,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from ..device_type import DEVICE_TYPE_TORCH
+from ..device_type import DEVICE_TYPE_TORCH, clean_gpu_cache
import hashlib
import importlib
import os
@@ -1428,7 +1428,7 @@ def _offline_quantize_to_fp8(
model.save_pretrained(new_model_name, safe_serialization = False)
del model
for _ in range(2):
- torch.cuda.empty_cache()
+ clean_gpu_cache()
gc.collect()
tokenizer.save_pretrained(new_model_name)
return new_model_name
@@ -3551,10 +3551,7 @@ def _offline_aware_load(fn):
# Retry OUTSIDE the except so the failed attempt's traceback (a partial model) is freed before reallocating, else a large VLM can OOM on the second load.
try:
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()
except Exception:
pass
# A failed attempt may have left HF progress bars disabled; restore before retry.
diff --git a/unsloth/optimizers/q_galore_adamw.py b/unsloth/optimizers/q_galore_adamw.py
index 1aa4df7a50e..d8a900d99d2 100644
--- a/unsloth/optimizers/q_galore_adamw.py
+++ b/unsloth/optimizers/q_galore_adamw.py
@@ -16,6 +16,7 @@ import inspect
import torch
from typing import Optional, List
+from unsloth_zoo.device_type import device_synchronize
from .q_galore_projector import (
GaLoreProjector,
_quantize,
@@ -213,8 +214,7 @@ class QGaLoreAdamW8bit(Optimizer2State):
# dequantizes before the next forward pass.
p.data = torch.empty(1, dtype = p.data.dtype, device = p.data.device)
- if torch.cuda.is_available():
- torch.cuda.synchronize()
+ device_synchronize()
return loss