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.

Name the Ascend NPU as its own tests expect, and close the spoofable skip-guard hole (#10968)

Daniel Han3fe93e093bb7
diff --git a/tests/_shared/real_accelerator.py b/tests/_shared/real_accelerator.py
index 3a055575155..f73eaa7ff8e 100644
--- a/tests/_shared/real_accelerator.py
+++ b/tests/_shared/real_accelerator.py
@@ -38,26 +38,38 @@ which is ahead of every test module in the session.
from __future__ import annotations
_REAL_ACCELERATOR: bool | None = None
+_REAL_CUDA: bool | None = None
-def _probe() -> bool:
+def _ask(probe) -> bool:
try:
- import torch
+ return bool(probe())
except Exception:
+ # A probe that raises is not an accelerator. torch.xpu on a build without
+ # XPU support, and torch.accelerator on torch < 2.6, both do this.
return False
- for probe in (
- lambda: hasattr(torch, "cuda") and torch.cuda.is_available(),
- lambda: hasattr(torch, "xpu") and torch.xpu.is_available(),
- lambda: hasattr(torch, "accelerator") and torch.accelerator.is_available(),
- ):
- try:
- if probe():
- return True
- except Exception:
- # A probe that raises is not an accelerator. torch.xpu on a build without
- # XPU support, and torch.accelerator on torch < 2.6, both do this.
- pass
- return False
+
+
+def _record() -> None:
+ """Fill both caches from one pass, so a single pre-spoof call primes them together.
+
+ ``tests/conftest.py`` calls ``has_real_accelerator()`` once, early. If the CUDA answer
+ were recorded lazily on its own first call, that call could land after a test module had
+ imported the spoof, and the CUDA cache would then hold the spoof's answer while the
+ accelerator cache held the machine's.
+ """
+ global _REAL_ACCELERATOR, _REAL_CUDA
+ try:
+ import torch
+ except Exception:
+ _REAL_ACCELERATOR, _REAL_CUDA = False, False
+ return
+ _REAL_CUDA = _ask(lambda: hasattr(torch, "cuda") and torch.cuda.is_available())
+ _REAL_ACCELERATOR = (
+ _REAL_CUDA
+ or _ask(lambda: hasattr(torch, "xpu") and torch.xpu.is_available())
+ or _ask(lambda: hasattr(torch, "accelerator") and torch.accelerator.is_available())
+ )
def has_real_accelerator() -> bool:
@@ -67,7 +79,23 @@ def has_real_accelerator() -> bool:
any spoof runs, so every later caller gets the pre-spoof answer no matter what
has patched ``torch.cuda`` since.
"""
- global _REAL_ACCELERATOR
if _REAL_ACCELERATOR is None:
- _REAL_ACCELERATOR = _probe()
+ _record()
return _REAL_ACCELERATOR
+
+
+def has_real_cuda() -> bool:
+ """True if this machine really has a CUDA device specifically.
+
+ ``has_real_accelerator()`` is the right gate for a test that only needs somewhere to put a
+ tensor. It is the wrong one for a test that names ``cuda``: it is true on an XPU-only or
+ Ascend NPU-only host too, so a CUDA-only test gated on it un-skips there and either dies
+ allocating on a device that is not present, or raises out of ``torch.cuda`` at import while
+ the decorator is still being evaluated.
+
+ Spoof-resistant on the same terms as its sibling, which a bare ``torch.cuda.is_available()``
+ is not.
+ """
+ if _REAL_CUDA is None:
+ _record()
+ return _REAL_CUDA
diff --git a/tests/python/test_accelerator_skip_guards.py b/tests/python/test_accelerator_skip_guards.py
index e57951ca1f3..8c6d2ed40b0 100644
--- a/tests/python/test_accelerator_skip_guards.py
+++ b/tests/python/test_accelerator_skip_guards.py
@@ -40,16 +40,57 @@ _TESTS_ROOT = Path(__file__).resolve().parents[1]
# The spoof itself, the recorded-answer helper and conftest are what implement the
# real probe. They are the only places allowed to call the raw torch API for it.
+_SPOOF = _TESTS_ROOT / "_zoo_aggressive_cuda_spoof.py"
+
_ALLOWED = {
- _TESTS_ROOT / "_zoo_aggressive_cuda_spoof.py",
+ _SPOOF,
_TESTS_ROOT / "_shared" / "real_accelerator.py",
_TESTS_ROOT / "conftest.py",
}
+# Every probe the spoof answers that a skip guard could plausibly ask. Listing only the
+# is_available three left `skipif(torch.cuda.device_count() == 0)` reading as safe and
+# evading this test; test_the_spoofed_probe_list_keeps_up_with_the_spoof keeps it in step.
_SPOOFED_CALLS = {
("torch", "cuda", "is_available"),
("torch", "xpu", "is_available"),
("torch", "accelerator", "is_available"),
+ ("torch", "cuda", "device_count"),
+ ("torch", "cuda", "is_initialized"),
+ ("torch", "cuda", "is_bf16_supported"),
+ ("torch", "cuda", "get_device_capability"),
+ ("torch", "cuda", "get_device_name"),
+ ("torch", "cuda", "get_device_properties"),
+}
+
+# The rest of what the spoof patches: things a test DOES, not things it asks. Listed so the
+# meta-test below can insist every newly spoofed name lands in one bucket or the other.
+_SPOOFED_NON_PREDICATES = {
+ ("torch", "Tensor", "is_pinned"),
+ ("torch", "Tensor", "pin_memory"),
+ ("torch", "cuda", "Event"),
+ ("torch", "cuda", "Stream"),
+ ("torch", "cuda", "_is_in_bad_fork"),
+ ("torch", "cuda", "_unsloth_consolidated_spoof"),
+ ("torch", "cuda", "amp"),
+ ("torch", "cuda", "cudart"),
+ ("torch", "cuda", "current_device"),
+ ("torch", "cuda", "current_stream"),
+ ("torch", "cuda", "default_stream"),
+ ("torch", "cuda", "empty_cache"),
+ ("torch", "cuda", "get_rng_state"),
+ ("torch", "cuda", "get_rng_state_all"),
+ ("torch", "cuda", "initial_seed"),
+ ("torch", "cuda", "manual_seed"),
+ ("torch", "cuda", "manual_seed_all"),
+ ("torch", "cuda", "nvtx"),
+ ("torch", "cuda", "seed"),
+ ("torch", "cuda", "seed_all"),
+ ("torch", "cuda", "set_device"),
+ ("torch", "cuda", "set_rng_state"),
+ ("torch", "cuda", "set_rng_state_all"),
+ ("torch", "cuda", "stream"),
+ ("torch", "cuda", "synchronize"),
}
# The other half of the hazard above: a skip guard decides whether a test RUNS, these
@@ -85,6 +126,79 @@ def _skipif_calls(tree: ast.AST):
yield decorator
+# has_real_accelerator() for a test that just needs somewhere to put a tensor, has_real_cuda()
+# for one that names cuda. Which of the two suffices is per-namespace; see _satisfying_gates.
+_REAL_PROBES = ("has_real_accelerator", "has_real_cuda")
+
+
+def _is_real_accelerator_call(node: ast.AST) -> bool:
+ return isinstance(node, ast.Call) and _dotted(node.func)[-1:] in {(p,) for p in _REAL_PROBES}
+
+
+def _is_negated_real_accelerator(node: ast.AST) -> bool:
+ return (
+ isinstance(node, ast.UnaryOp)
+ and isinstance(node.op, ast.Not)
+ and _is_real_accelerator_call(node.operand)
+ )
+
+
+def _gate_named(node: ast.AST, negated: bool) -> str:
+ """The recorded-answer probe this conjunct gates on, or "" if it is not one."""
+ if negated:
+ if not (isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not)):
+ return ""
+ node = node.operand
+ if not isinstance(node, ast.Call):
+ return ""
+ name = _dotted(node.func)[-1:]
+ return name[0] if name and name[0] in _REAL_PROBES else ""
+
+
+def _satisfying_gates(call: ast.Call) -> frozenset[str]:
+ """Which recorded-answer probes are strong enough to dominate this probe.
+
+ A torch.cuda.* call needs has_real_cuda(). has_real_accelerator() is true on an XPU-only
+ or Ascend NPU-only host, so it lets a CUDA-only API through to be evaluated on a machine
+ with no CUDA at all, where it raises or hands back the spoof's answer. That is the same
+ defect as gating a CUDA-only test on the broad probe, one level up in the decorator.
+ """
+ if _dotted(call.func)[:2] == ("torch", "cuda"):
+ return frozenset({"has_real_cuda"})
+ return frozenset(_REAL_PROBES)
+
+
+def _unguarded_spoofed_calls(node: ast.AST, gates: frozenset[str] = frozenset()):
+ """Spoofable probes in `node` whose value the spoof is still free to decide.
+
+ Python's `and` / `or` short-circuit, so a probe is safe once an earlier conjunct in the
+ same chain has settled the case it would otherwise be asked about:
+
+ skipif(not has_real_cuda() or torch.cuda.device_count() < 2)
+ skipif(has_real_cuda() and torch.cuda.get_device_capability()[0] >= 12)
+
+ `gates` carries the probes already established at this point in the chain. Which of them
+ suffices depends on the namespace being called into; see `_satisfying_gates`.
+ """
+ if isinstance(node, ast.BoolOp):
+ negated = not isinstance(node.op, ast.And)
+ seen = gates
+ for value in node.values:
+ yield from _unguarded_spoofed_calls(value, seen)
+ named = _gate_named(value, negated)
+ if named:
+ seen = seen | {named}
+ return
+ if (
+ isinstance(node, ast.Call)
+ and _dotted(node.func) in _SPOOFED_CALLS
+ and not (gates & _satisfying_gates(node))
+ ):
+ yield node
+ for child in ast.iter_child_nodes(node):
+ yield from _unguarded_spoofed_calls(child, gates)
+
+
def _python_test_files():
for path in sorted(_TESTS_ROOT.rglob("*.py")):
if path in _ALLOWED:
@@ -104,17 +218,51 @@ def test_no_skip_guard_reads_a_spoofable_accelerator_probe():
# interpreter (the 3.9 floor checks). Not our business.
continue
for decorator in _skipif_calls(tree):
- for call in ast.walk(decorator):
- if isinstance(call, ast.Call) and _dotted(call.func) in _SPOOFED_CALLS:
- offenders.append(
- f"{path.relative_to(_TESTS_ROOT)}:{call.lineno}: "
- f"{'.'.join(_dotted(call.func))}()"
- )
+ for call in _unguarded_spoofed_calls(decorator):
+ offenders.append(
+ f"{path.relative_to(_TESTS_ROOT)}:{call.lineno}: "
+ f"{'.'.join(_dotted(call.func))}()"
+ )
assert not offenders, (
"these skip guards read a probe tests/_zoo_aggressive_cuda_spoof.py patches to "
"True process-wide, so they un-skip on a CPU-only box whenever they share a "
- "pytest session with tests/version_compat or tests/vllm_compat. Use "
- "`from real_accelerator import has_real_accelerator` instead:\n " + "\n ".join(offenders)
+ "pytest session with tests/version_compat or tests/vllm_compat. Gate on "
+ "`from real_accelerator import has_real_accelerator` first, either alone or as the "
+ "conjunct that short-circuits ahead of the torch call:\n " + "\n ".join(offenders)
+ )
+
+
+def test_the_spoofed_probe_list_keeps_up_with_the_spoof():
+ """Every name the spoof patches has to be classified, or this guard rots quietly.
+
+ _SPOOFED_CALLS started as the three is_available probes while the spoof was already
+ answering device_count, is_initialized, is_bf16_supported and get_device_capability. A
+ skip guard reading any of those was un-skipped by the spoof and invisible to the test
+ above. Adding a patch to the spoof now fails here until it is called a predicate a skip
+ guard could read, or plumbing no guard would."""
+ spoof = ast.parse(_SPOOF.read_text(encoding = "utf-8"))
+ patched = set()
+ for node in ast.walk(spoof):
+ if not isinstance(node, ast.Assign):
+ continue
+ for target in node.targets:
+ name = _dotted(target)
+ if name[:1] == ("torch",) and len(name) >= 3:
+ patched.add(name)
+
+ classified = _SPOOFED_CALLS | _SPOOFED_NON_PREDICATES
+ unclassified = sorted(".".join(n) for n in patched - classified)
+ assert not unclassified, (
+ "the spoof patches these and this file does not say what they are. Put each in "
+ "_SPOOFED_CALLS if a skip guard could read it, or _SPOOFED_NON_PREDICATES if no "
+ "guard would:\n " + "\n ".join(unclassified)
+ )
+
+ # Nothing listed may have quietly stopped being spoofed, or the guard above now rejects
+ # an idiom that has become safe.
+ stale = sorted(".".join(n) for n in (classified - patched) if n[1] == "cuda")
+ assert not stale, (
+ "these are listed as spoofed but the spoof no longer patches them:\n " + "\n ".join(stale)
)
@@ -174,9 +322,10 @@ _SURVIVES_THE_SPOOF_PROBE = textwrap.dedent(
sys.path.insert(0, {tests_root!r})
sys.path.insert(0, {shared_dir!r})
- from real_accelerator import has_real_accelerator
+ from real_accelerator import has_real_accelerator, has_real_cuda
before = has_real_accelerator()
+ before_cuda = has_real_cuda()
import _zoo_aggressive_cuda_spoof as spoof
@@ -187,6 +336,8 @@ _SURVIVES_THE_SPOOF_PROBE = textwrap.dedent(
print("SPOOF_PROBE " + json.dumps({{
"before": before,
"after": has_real_accelerator(),
+ "before_cuda": before_cuda,
+ "after_cuda": has_real_cuda(),
"spoof_patched_is_available": bool(torch.cuda.is_available()),
}}))
"""
@@ -246,6 +397,14 @@ def test_the_recorded_answer_survives_the_spoof():
"has_real_accelerator() moved after the spoof was applied, which is the whole "
f"thing it is supposed to be immune to: {verdict}"
)
+ assert verdict["after_cuda"] == verdict["before_cuda"], (
+ "has_real_cuda() moved after the spoof was applied. It is the gate for the tests that "
+ f"name cuda, so it is the one the spoof most directly targets: {verdict}"
+ )
+ assert not (verdict["before_cuda"] and not verdict["before"]), (
+ "has_real_cuda() is true where has_real_accelerator() is false, so the narrow probe is "
+ f"no longer a subset of the broad one: {verdict}"
+ )
def test_this_file_never_applies_the_spoof_in_process():
@@ -282,9 +441,170 @@ def test_the_scanner_would_catch_a_regression(probe, tmp_path):
)
tree = ast.parse(offending.read_text(encoding = "utf-8"))
found = [
- call
- for decorator in _skipif_calls(tree)
- for call in ast.walk(decorator)
- if isinstance(call, ast.Call) and _dotted(call.func) in _SPOOFED_CALLS
+ call for decorator in _skipif_calls(tree) for call in _unguarded_spoofed_calls(decorator)
]
assert len(found) == 1
+
+
+def _offenders_in(condition: str) -> list[str]:
+ tree = ast.parse(
+ "import pytest\nimport torch\n"
+ "from real_accelerator import has_real_accelerator, has_real_cuda\n\n\n"
+ f"@pytest.mark.skipif({condition}, reason = 'x')\n"
+ "def test_x():\n pass\n"
+ )
+ return [
+ ".".join(_dotted(call.func))
+ for decorator in _skipif_calls(tree)
+ for call in _unguarded_spoofed_calls(decorator)
+ ]
+
+
+ "condition",
+ [
+ # Both polarities, and nested one level down.
+ "not has_real_cuda() or torch.cuda.device_count() < 2",
+ "has_real_cuda() and torch.cuda.get_device_capability()[0] >= 12",
+ "not has_real_cuda() or (torch.cuda.device_count() < 2 or x)",
+ "has_real_cuda() and not torch.cuda.is_bf16_supported()",
+ # Outside the torch.cuda namespace the broad probe is the matching one.
+ "not has_real_accelerator() or not torch.xpu.is_available()",
+ "has_real_accelerator() and torch.accelerator.is_available()",
+ ],
+)
+def test_a_guard_short_circuited_on_the_real_probe_is_accepted(condition):
+ assert _offenders_in(condition) == []
+
+
+ "condition",
+ [
+ # The gate is present but does not dominate the call, so the spoof still decides.
+ ("torch.cuda.device_count() < 2 or not has_real_accelerator()", "gate comes after"),
+ ("has_real_accelerator() or torch.cuda.device_count() < 2", "and-gate under or"),
+ ("not has_real_accelerator() and torch.cuda.device_count() < 2", "or-gate under and"),
+ ("torch.cuda.device_count() < 2", "no gate at all"),
+ ("x if has_real_cuda() else torch.cuda.device_count() < 2", "not a bool chain"),
+ # The broad probe is true on an XPU-only host, so it does not settle whether
+ # torch.cuda is there to answer.
+ ("not has_real_accelerator() or torch.cuda.device_count() < 2", "broad gate on cuda"),
+ (
+ "has_real_accelerator() and torch.cuda.get_device_capability()[0] >= 12",
+ "broad gate on cuda",
+ ),
+ ],
+)
+def test_a_gate_that_does_not_short_circuit_is_still_an_offender(condition):
+ expression, why = condition
+ assert _offenders_in(expression), f"missed: {why}"
+
+
+_OTHER_ACCELERATOR_DEVICES = ("xpu", "npu", "mps", "hpu")
+
+
+def _names_a_cuda_device(node: ast.AST) -> list[int]:
+ """Lines where the body asks for a cuda device by name.
+
+ Only the device string itself, so a test that merely mentions cuda in a message is not
+ swept up: `device = "cuda"`, `.to("cuda:1")`, `torch.device("cuda")`.
+
+ A body that also names xpu or npu as a device picks its device at run time and is not
+ CUDA-only, whatever it calls the CUDA branch. tests/utils/test_packing.py is the case:
+ it falls through to torch.device("xpu"), and _build_packed_training_setup has an xpu
+ dtype arm, so narrowing its gate would have dropped real XPU coverage.
+ """
+ strings = [
+ child.value
+ for child in ast.walk(node)
+ if isinstance(child, ast.Constant) and isinstance(child.value, str)
+ ]
+ if any(
+ s == other or s.startswith(other + ":")
+ for s in strings
+ for other in _OTHER_ACCELERATOR_DEVICES
+ ):
+ return []
+ return [
+ child.lineno
+ for child in ast.walk(node)
+ if isinstance(child, ast.Constant)
+ and isinstance(child.value, str)
+ and (child.value == "cuda" or child.value.startswith("cuda:"))
+ ]
+
+
+def _gated_only_on_the_broad_probe(func: ast.AST) -> bool:
+ probes = {
+ _dotted(call.func)[-1]
+ for decorator in func.decorator_list
+ if isinstance(decorator, ast.Call) and _dotted(decorator.func)[-1:] == ("skipif",)
+ for call in ast.walk(decorator)
+ if isinstance(call, ast.Call) and _dotted(call.func)[-1:] in {(p,) for p in _REAL_PROBES}
+ }
+ return probes == {"has_real_accelerator"}
+
+
+def test_no_cuda_only_test_is_gated_on_the_broad_accelerator_probe():
+ """has_real_accelerator() is true on an XPU-only or Ascend NPU-only host.
+
+ A test that allocates on "cuda" and gates on it therefore un-skips on those machines and
+ dies in torch, or, when the decorator itself calls into torch.cuda, raises during
+ collection. Unsloth supports both backends, so this is a host somebody runs. Caught on
+ tests/test_stopping_criteria_device.py, which named cuda three times behind the broad gate.
+ """
+ offenders = []
+ for path in _python_test_files():
+ try:
+ tree = ast.parse(path.read_text(encoding = "utf-8"))
+ except SyntaxError:
+ continue
+ for node in ast.walk(tree):
+ if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
+ continue
+ if not _gated_only_on_the_broad_probe(node):
+ continue
+ for lineno in _names_a_cuda_device(node):
+ offenders.append(f"{path.relative_to(_TESTS_ROOT)}:{lineno}: in {node.name}")
+ assert not offenders, (
+ "these tests ask for a cuda device but gate on has_real_accelerator(), which is also "
+ "true on an XPU-only or Ascend NPU-only host. Gate them on has_real_cuda() instead:"
+ "\n " + "\n ".join(offenders)
+ )
+
+
+def test_the_cuda_gate_scanner_would_catch_a_regression():
+ """Not vacuous, and not over-broad: the narrow gate and a non-device string both pass."""
+
+ def offenders(source):
+ tree = ast.parse(source)
+ return [
+ lineno
+ for node in ast.walk(tree)
+ if isinstance(node, ast.FunctionDef) and _gated_only_on_the_broad_probe(node)
+ for lineno in _names_a_cuda_device(node)
+ ]
+
+ head = (
+ "import pytest\n"
+ "import torch\n"
+ "from real_accelerator import has_real_accelerator, has_real_cuda\n\n\n"
+ )
+ bad = "@pytest.mark.skipif(not has_real_accelerator(), reason = 'x')\ndef test_x():\n torch.tensor([1], device = 'cuda')\n"
+ assert len(offenders(head + bad)) == 1
+
+ fixed = bad.replace("has_real_accelerator", "has_real_cuda")
+ assert offenders(head + fixed) == []
+
+ # A cuda mention that is not a device string must not be swept up.
+ prose = "@pytest.mark.skipif(not has_real_accelerator(), reason = 'x')\ndef test_x():\n assert True, 'cuda is not required here'\n"
+ assert offenders(head + prose) == []
+
+ # Nor may a test that picks its own device: naming cuda in one branch and xpu in another
+ # makes it device-generic, and narrowing its gate would drop the XPU coverage it has.
+ generic = (
+ "@pytest.mark.skipif(not has_real_accelerator(), reason = 'x')\n"
+ "def test_x():\n"
+ " device = torch.device('cuda') if torch.cuda.is_available() else torch.device('xpu')\n"
+ )
+ assert offenders(head + generic) == []
diff --git a/tests/studio/test_gpu_inference_smoke.py b/tests/studio/test_gpu_inference_smoke.py
index 92c540ba468..f63d26917e2 100644
--- a/tests/studio/test_gpu_inference_smoke.py
+++ b/tests/studio/test_gpu_inference_smoke.py
@@ -16,7 +16,7 @@ from __future__ import annotations
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
torch = pytest.importorskip("torch")
@@ -30,7 +30,7 @@ MIN_NEW_TOKENS = 4
MAX_NEW_TOKENS = 16
[email protected](not has_real_accelerator(), reason = "requires a CUDA GPU")
[email protected](not has_real_cuda(), reason = "requires a CUDA GPU")
def test_gpu_generation_smoke():
try:
from transformers import AutoModelForCausalLM, AutoTokenizer
diff --git a/tests/test_cross_entropy_softcap_padding.py b/tests/test_cross_entropy_softcap_padding.py
index fc670e5fa03..899a4b21076 100644
--- a/tests/test_cross_entropy_softcap_padding.py
+++ b/tests/test_cross_entropy_softcap_padding.py
@@ -3,7 +3,7 @@
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
import torch
import torch.nn.functional as F
@@ -11,7 +11,7 @@ import torch.nn.functional as F
pytestmark = pytest.mark.gpu
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
@pytest.mark.parametrize(
"vocab_size,softcap,offset",
@@ -46,7 +46,7 @@ def test_cross_entropy_softcap_padding(dtype, vocab_size, softcap, offset):
torch.testing.assert_close(logits.grad, reference_logits.grad, rtol = 1e-2, atol = 1e-7)
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("vocab_size", [32000, 65537, 256000])
@pytest.mark.parametrize("logit_scaling", [0.0625, 0.5, 2.0])
def test_cross_entropy_softcap_padding_with_logit_scaling(vocab_size, logit_scaling):
@@ -76,7 +76,7 @@ def test_cross_entropy_softcap_padding_with_logit_scaling(vocab_size, logit_scal
torch.testing.assert_close(logits.grad, reference_logits.grad, rtol = 1e-2, atol = 1e-7)
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("vocab_size", [32000, 65537])
@pytest.mark.parametrize("softcap", [0.0, 30.0])
def test_negative_logit_scaling_does_not_nan(vocab_size, softcap):
@@ -104,7 +104,7 @@ def test_negative_logit_scaling_does_not_nan(vocab_size, softcap):
torch.testing.assert_close(actual, expected, rtol = 1e-5, atol = 1e-5)
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("vocab_size", [32000, 65537, 256000, 262208])
def test_softcapped_probabilities_sum_to_one(vocab_size):
"""Guards the denominator without leaning on a loss tolerance."""
diff --git a/tests/test_float32_generate_autocast.py b/tests/test_float32_generate_autocast.py
index b9f6c84d007..b57949c63ac 100644
--- a/tests/test_float32_generate_autocast.py
+++ b/tests/test_float32_generate_autocast.py
@@ -42,7 +42,7 @@ from pathlib import Path
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
import torch
@@ -103,7 +103,7 @@ def test_the_gate_by_execution(dtype, expected):
assert (dtype in (torch.float16, torch.bfloat16)) is expected
[email protected](not has_real_accelerator(), reason = "needs a CUDA device")
[email protected](not has_real_cuda(), reason = "needs a CUDA device")
def test_cuda_really_does_accept_float32_as_an_autocast_dtype():
"""The premise. If torch ever starts rejecting or ignoring this, the fix
above is no longer load-bearing and this test says so rather than letting
@@ -113,7 +113,7 @@ def test_cuda_really_does_accept_float32_as_an_autocast_dtype():
assert torch.get_autocast_dtype("cuda") == torch.float32
[email protected](not has_real_accelerator(), reason = "needs a CUDA device")
[email protected](not has_real_cuda(), reason = "needs a CUDA device")
def test_the_gate_turns_that_into_a_no_op():
dtype = torch.float32
with torch.autocast(
diff --git a/tests/test_offload_frozen_module_device.py b/tests/test_offload_frozen_module_device.py
index 0a78cc32a6d..10be35f1f1a 100644
--- a/tests/test_offload_frozen_module_device.py
+++ b/tests/test_offload_frozen_module_device.py
@@ -29,7 +29,7 @@ card is testable on any box, including CI runners with no GPU.
import types
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
import torch
@@ -183,7 +183,7 @@ def test_a_module_without_modules_to_save_is_untouched():
assert plain.to_calls == []
[email protected](not has_real_accelerator(), reason = "needs a CUDA device")
[email protected](not has_real_cuda(), reason = "needs a CUDA device")
def test_on_real_hardware_a_module_on_the_current_card_is_unchanged():
class _Real:
def __init__(self):
diff --git a/tests/test_rmsnorm_gradient_layout.py b/tests/test_rmsnorm_gradient_layout.py
index 1cc74dd5323..25da172adf2 100644
--- a/tests/test_rmsnorm_gradient_layout.py
+++ b/tests/test_rmsnorm_gradient_layout.py
@@ -3,14 +3,14 @@
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
import torch
pytestmark = pytest.mark.gpu
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("gemma", [False, True])
@pytest.mark.parametrize("layout", ["contiguous", "columns", "rows", "expanded"])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
@@ -47,7 +47,7 @@ def test_rmsnorm_backward_gradient_layout(gemma, layout, dtype):
torch.testing.assert_close(actual_inputs.grad, reference_inputs.grad, rtol = 2e-2, atol = 1e-2)
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("gemma", [False, True])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
def test_rmsnorm_forward_column_strided_input(gemma, dtype):
@@ -77,7 +77,7 @@ def test_rmsnorm_forward_column_strided_input(gemma, dtype):
torch.testing.assert_close(source.grad, reference_source.grad, rtol = 2e-2, atol = 1e-2)
[email protected](not has_real_accelerator(), reason = "CUDA Triton kernels required")
[email protected](not has_real_cuda(), reason = "CUDA Triton kernels required")
@pytest.mark.parametrize("gemma", [False, True])
@pytest.mark.parametrize("layout", ["contiguous", "columns", "expanded"])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
diff --git a/tests/test_stopping_criteria_device.py b/tests/test_stopping_criteria_device.py
index 26ce603af7f..ee388948386 100644
--- a/tests/test_stopping_criteria_device.py
+++ b/tests/test_stopping_criteria_device.py
@@ -9,6 +9,9 @@ non-CUDA build.
import types
import pytest
+from real_accelerator import (
+ has_real_cuda,
+) # tests/_shared, on sys.path via tests/conftest.py
import torch
from unsloth.chat_templates import create_stopping_criteria
@@ -48,7 +51,9 @@ def test_multi_token_criteria_builds_and_runs_on_cpu():
assert criteria[0](torch.tensor([100, 101, 7]), None) is False
[email protected](not torch.cuda.is_available(), reason = "needs an accelerator")
+# Neither torch.cuda.is_available(), which the spoof patches True process-wide, nor
+# has_real_accelerator(), which is true on an XPU-only host: the body names cuda three times.
[email protected](not has_real_cuda(), reason = "needs a real CUDA device")
def test_stop_token_follows_the_input_device():
criteria = create_stopping_criteria(_FakeTokenizer(eos_token_id = 2))
diff --git a/tests/test_uma_safetensors_load.py b/tests/test_uma_safetensors_load.py
index 0d72647345d..3497bd92ed7 100644
--- a/tests/test_uma_safetensors_load.py
+++ b/tests/test_uma_safetensors_load.py
@@ -18,7 +18,7 @@ from pathlib import Path
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
torch = pytest.importorskip("torch")
@@ -173,7 +173,7 @@ def test_cpu_target_is_passthrough(uma, force_uma, monkeypatch, tiny_safetensors
@pytest.mark.skipif(
- not has_real_accelerator(),
+ not has_real_cuda(),
reason = "needs a GPU for the host->device clone-and-move path",
)
def test_cuda_target_clones_and_moves(uma, force_uma, monkeypatch, tiny_safetensors):
@@ -193,7 +193,7 @@ def test_cuda_target_clones_and_moves(uma, force_uma, monkeypatch, tiny_safetens
@pytest.mark.skipif(
- not has_real_accelerator(),
+ not has_real_cuda(),
reason = "needs a GPU for the low-memory fallback path",
)
def test_low_memory_falls_back_to_direct_move(uma, force_uma, monkeypatch, tiny_safetensors):
diff --git a/tests/utils/test_attention_masks.py b/tests/utils/test_attention_masks.py
index dfb533cb066..0392f78218d 100644
--- a/tests/utils/test_attention_masks.py
+++ b/tests/utils/test_attention_masks.py
@@ -20,6 +20,9 @@ import weakref
import pytest
import torch
+from real_accelerator import (
+ has_real_cuda,
+) # tests/_shared, on sys.path via tests/conftest.py
from unsloth.utils import attention_dispatch
from unsloth.utils import packing as packing_utils
@@ -236,7 +239,11 @@ def test_xformers_bias_move_skips_matching_metadata_device():
@pytest.mark.skipif(
- torch.cuda.device_count() < 2 or packing_utils._XFormersBlockMask is None,
+ # The spoof answers device_count() with 1, so this skips today only because 1 < 2.
+ # Raise that stub to exercise a multi-GPU path and it un-skips on a box with no card.
+ not has_real_cuda()
+ or torch.cuda.device_count() < 2
+ or packing_utils._XFormersBlockMask is None,
reason = "needs xFormers and two CUDA devices",
)
def test_real_xformers_packed_mask_validates_on_each_device():
diff --git a/tests/utils/test_attn_mask_compat.py b/tests/utils/test_attn_mask_compat.py
index d2b799feb9c..1ad3dd1f696 100644
--- a/tests/utils/test_attn_mask_compat.py
+++ b/tests/utils/test_attn_mask_compat.py
@@ -13,7 +13,7 @@ from unittest import mock
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
import torch
@@ -134,7 +134,7 @@ def test_prepare_4d_causal_attention_mask_for_sdpa_matches_transformers(
assert torch.equal(actual, expected)
[email protected](not has_real_accelerator(), reason = "needs CUDA")
[email protected](not has_real_cuda(), reason = "needs CUDA")
@pytest.mark.parametrize(
"attention_mask,past_length",
[
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index 7065e0fafd8..fb650fa4a37 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -1053,6 +1053,8 @@ def test_enable_sample_packing_only_requires_torch_call():
assert torch.equal(batch["packed_seq_lengths"], torch.tensor([2, 1, 3], dtype = torch.int32))
+# has_real_accelerator(), not has_real_cuda(): the body below picks xpu when cuda is absent
+# and _build_packed_training_setup has an xpu dtype arm, so this is real XPU coverage.
@pytest.mark.skipif(not has_real_accelerator(), reason = "builds a real 4bit model on an accelerator")
def test_enable_sample_packing_trl_collator(tmp_path):
if torch.cuda.is_available():
@@ -1115,6 +1117,8 @@ def test_enable_padding_free_metadata():
assert trainer.args.remove_unused_columns is False
+# has_real_accelerator(), not has_real_cuda(): the body below picks xpu when cuda is absent
+# and _build_packed_training_setup has an xpu dtype arm, so this is real XPU coverage.
@pytest.mark.skipif(not has_real_accelerator(), reason = "builds a real 4bit model on an accelerator")
def test_packing_sdpa(tmp_path):
if torch.cuda.is_available():
diff --git a/tests/utils/test_xformers_capability_gate.py b/tests/utils/test_xformers_capability_gate.py
index 18a82ef7504..77ecb328edf 100644
--- a/tests/utils/test_xformers_capability_gate.py
+++ b/tests/utils/test_xformers_capability_gate.py
@@ -5,7 +5,7 @@ compute-capability major version."""
import pytest
from real_accelerator import (
- has_real_accelerator,
+ has_real_cuda,
) # tests/_shared, on sys.path via tests/conftest.py
import torch
import unsloth # noqa: F401
@@ -36,11 +36,11 @@ def test_capability_gate(capability, probe_result, expect_disabled):
@pytest.mark.skipif(
- not (has_real_accelerator() and ad.HAS_XFORMERS),
+ not (has_real_cuda() and ad.HAS_XFORMERS),
reason = "needs a CUDA GPU with a working xformers build",
)
@pytest.mark.skipif(
- has_real_accelerator() and torch.cuda.get_device_capability()[0] >= 12,
+ has_real_cuda() and torch.cuda.get_device_capability()[0] >= 12,
reason = "on real sm_120+ the probe legitimately returns False when the build ships no "
"sm_120 kernel, so asserting True there would be a false failure",
)
diff --git a/unsloth/device_type.py b/unsloth/device_type.py
index cbf4cf8265c..86dd0f39078 100644
--- a/unsloth/device_type.py
+++ b/unsloth/device_type.py
@@ -285,13 +285,16 @@ def get_device_stats() -> tuple[str, str, float]:
name = gpu_stats.name + ". " if gpu_stats.name else "Intel XPU Device. "
snippet = f"Intel Toolkit: {torch.version.xpu}."
elif DEVICE_TYPE == "npu":
- name = gpu_stats.name + ". " if gpu_stats.name else "NPU Device. "
+ # Named for the vendor, like the arms either side of it: torch.npu and torch_npu are
+ # Ascend's, so an unnamed one is an Ascend NPU the driver declined to name, not some
+ # generic NPU. #10686 added the tests that say so and the code that did not.
+ name = gpu_stats.name + ". " if gpu_stats.name else "Ascend NPU Device. "
# Report the toolkit like the cuda/xpu arms, not the name already in `name`.
try:
import torch_npu
- snippet = f"NPU. torch_npu: {torch_npu.__version__}."
+ snippet = f"Ascend NPU. torch_npu: {torch_npu.__version__}."
except Exception:
- snippet = "NPU."
+ snippet = "Ascend NPU."
else:
name = gpu_stats.name + ". " if gpu_stats.name else "NVIDIA GPU Device. "
snippet = f"CUDA: {gpu_stats.major}.{gpu_stats.minor}. CUDA Toolkit: {torch.version.cuda}."