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.

Keep the plain capability cache on single-GPU hosts (#13962)

Daniel Han6baec3b2ab0a
diff --git a/tests/test_mixed_nvidia_capabilities.py b/tests/test_mixed_nvidia_capabilities.py
index f63ab7afb52..491aff1f978 100644
--- a/tests/test_mixed_nvidia_capabilities.py
+++ b/tests/test_mixed_nvidia_capabilities.py
@@ -184,6 +184,23 @@ def test_capability_cache_resolves_device_spellings(monkeypatch):
assert inspect.signature(cached) == inspect.signature(probe)
+def test_capability_cache_single_device_keeps_the_plain_cache(monkeypatch):
+ import unsloth.models._utils as model_utils
+
+ calls = []
+
+ def probe(device = None):
+ calls.append(device)
+ return SM86
+
+ monkeypatch.setattr(model_utils, "torch", _fake_torch([SM86]))
+ cached = model_utils._cache_device_capability(probe)
+ assert hasattr(cached, "cache_info") # functools.cache: no per-call Python work
+ assert cached() == cached() == cached(0) == SM86
+ assert calls == [None, 0]
+ assert model_utils._cache_device_capability(cached) is cached
+
+
def _run_pre_volta(fake, monkeypatch, capsys):
applied = []
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 6c9e9f3040d..4d7a486cf00 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -3853,6 +3853,16 @@ def _cache_device_capability(get_device_capability):
if getattr(get_device_capability, "_unsloth_per_device_cache", False):
return get_device_capability
+ # One visible device: every spelling names it, so the plain C-level cache is exact and fastest.
+ try:
+ single_device = torch.cuda.device_count() <= 1
+ except Exception:
+ single_device = False
+ if single_device:
+ cached = functools.cache(get_device_capability)
+ cached._unsloth_per_device_cache = True
+ return cached
+
cache = {}
@functools.wraps(get_device_capability)