Skip to content

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

Merged
danielhanchen merged 7 commits into
mainfrom
fix-ascend-npu-name-and-spoofable-skip-guard
Sep 15, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
fix-ascend-npu-name-and-spoofable-skip-guard

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Repo tests (CPU, python) and Repo tests (CPU, rest) have been failing on every open PR since yesterday evening. Three tests, none of them belonging to the PRs they fail on:

tests/test_device_helpers.py::test_npu_detected_with_count_and_stats
tests/test_device_helpers.py::test_npu_blank_name_falls_back
tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe

Where they came from

#10686 added Ascend NPU detection and merged at 22:38 with Core zoo (HF=4.57.6 + TRL<1), Repo tests (CPU, python) and Repo tests (CPU, rest) already red on its own head.

Its source and its tests disagree about one string. get_device_stats() returns "NPU Device. " and "NPU."; the tests it added in the same commit assert "Ascend NPU Device. " and "Ascend NPU.".

I took the tests as correct here, for a reason beyond their being newer. The arms either side of this one are vendor-qualified ("Intel XPU Device. ", "NVIDIA GPU Device. "), and the detection this PR added keys on torch.npu / torch_npu, which are Ascend's. A device found through those is an Ascend NPU whose driver declined to give a name, not a generic NPU.

#10684 changed create_stopping_criteria to stop hardcoding CUDA and added a test gated on torch.cuda.is_available(). That is exactly the shape tests/python/test_accelerator_skip_guards.py exists to reject, because tests/_zoo_aggressive_cuda_spoof.py patches torch.cuda.is_available to return True and never restores it, so on a CPU-only runner the test un-skips and dies inside torch.

The guard did not catch it before the merge because no Actions workflow ran on that PR at all. Its entire status rollup is one pre-commit.ci context; the rest sat in action_required and was never approved.

Two probes, not one

The obvious fix for #10684's test is has_real_accelerator(), the pre-spoof recorded answer. That fixes the spoof half and breaks the other half: has_real_accelerator() accepts torch.xpu and torch.accelerator too, so on an XPU-only or Ascend NPU-only host it is true, and a test that names cuda un-skips there and dies allocating on a device that is not present. Where the decorator itself calls into torch.cuda, it raises during collection and takes the whole module with it. Unsloth supports both backends, so this is a machine somebody runs.

So there are now two probes. has_real_accelerator() for a test that just needs somewhere to put a tensor, has_real_cuda() for one that names cuda. Both are recorded in a single pre-spoof pass, so the one priming call tests/conftest.py already makes fills both; a lazily recorded second cache could be filled after a test module had imported the spoof, leaving the CUDA answer spoofed while the accelerator answer stayed real.

Verified against a simulated XPU-only host: the broad probe runs the test, the narrow one skips it.

Widening the guard

The guard that caught #10684 knew about the three is_available probes. The spoof was already answering six more: device_count returns 1, is_initialized True, is_bf16_supported True, get_device_capability (8, 0), plus get_device_name and get_device_properties. A guard written skipif(torch.cuda.device_count() == 0) read as safe and walked straight past the scan.

All nine are covered now. Every other name the spoof patches is listed as plumbing, and a meta-test fails if a future patch lands in neither bucket, or if something listed as spoofed quietly stops being spoofed and the guard starts rejecting an idiom that has become safe.

Widening it turned up two existing guards:

  • tests/utils/test_attention_masks.py gates a two-GPU test on torch.cuda.device_count() < 2. Real, and it skips today only by luck: the spoof answers 1, and 1 is less than 2.
  • tests/utils/test_xformers_capability_gate.py reads get_device_capability() behind a real-probe conjunct, so the call is only reached on a machine that really has a card. That one was already correct.

The second is the more useful result, because a naive scan would have forced it to be rewritten into something worse. The scan models short-circuiting: a spoofable probe is accepted once a recorded-answer conjunct earlier in the same chain has settled the case it would otherwise be asked about. Which conjunct suffices is per-namespace, since has_real_accelerator() does not settle whether torch.cuda is there to answer. Near-misses where the gate does not dominate are pinned as still failing, so the exemption cannot widen into a hole.

A second scan covers the other direction: a test that asks for a cuda device by name while gating only on the broad probe. It matches the device string itself, so a test that merely mentions cuda in a message is not swept up, and a body that names xpu, npu, mps or hpu as a device is picking its device at run time and is left alone. That exemption is not theoretical: two tests in tests/utils/test_packing.py fall through to torch.device("xpu") against an xpu dtype arm in _build_packed_training_setup, and narrowing them would have silently dropped real XPU coverage.

Eight files that predate this PR were mis-gated the same way, each already saying so in its own reason string ("requires a CUDA GPU", "CUDA Triton kernels required", "needs CUDA"). Narrowing them changes nothing on a CUDA host and stops the crash elsewhere.

Verification

tests/python/test_accelerator_skip_guards.py and tests/test_device_helpers.py: 39 passed, from 3 failed, 20 passed before.

The 140 local failures in tests/python/ on this branch reproduce identically on unmodified main (139 over the same file set, the difference being one file not in the control run). They are a local vLLM 0.24 against transformers 4.x mismatch, not this branch.

tests/test_stopping_criteria_device.py, tests/utils/test_attention_masks.py and tests/utils/test_xformers_capability_gate.py do not import in that environment for the same reason; CI pins a matching pair. The guard reads all three with ast, so their skip conditions are checked without importing them.

The device_type.py change is source, not test, so it wants a look from someone other than me.

…skip-guard hole

get_device_stats()'s npu arm returned "NPU Device. " and "NPU." while the tests
added alongside it assert "Ascend NPU Device. " and "Ascend NPU.". torch.npu and
torch_npu are Ascend's, so the vendor-qualified string is the one that matches the
xpu and cuda arms either side of it; the source is what was out of step.

tests/test_stopping_criteria_device.py gated on torch.cuda.is_available(), which
tests/_zoo_aggressive_cuda_spoof.py patches to True for the rest of the process.
It is now on has_real_accelerator(), the recorded pre-spoof answer.

tests/python/test_accelerator_skip_guards.py only knew about the three is_available
probes while the spoof was already answering device_count, is_initialized,
is_bf16_supported, get_device_capability, get_device_name and get_device_properties.
A guard reading any of those evaded it. The scan now covers all nine, every other
name the spoof patches is classified as plumbing, and a meta-test fails if a new
patch lands in neither bucket or if a listed one stops being spoofed.

Widening it turned up two guards. One was real: test_attention_masks.py gated a
two-GPU test on device_count() < 2, which the spoof answers with 1. It skips today
only because 1 is less than 2. The other, test_xformers_capability_gate.py, was
already correct, because has_real_accelerator() short-circuits ahead of its
get_device_capability() call. The scan now understands that: a probe reached only
after the real gate has settled the CPU-only case is accepted, and five near-misses
where the gate does not dominate are pinned as still failing.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-15T05:09:48.888143Z 4fed755 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

chatgpt-codex-connector[bot]

This comment was marked as resolved.

pre-commit-ci Bot and others added 2 commits September 15, 2026 04:42
has_real_accelerator() accepts torch.xpu and torch.accelerator as well as
torch.cuda, so it is the wrong gate for a test that names cuda. On an XPU-only or
Ascend NPU-only host such a test un-skips and dies allocating on a device that is
not there; where the decorator itself calls into torch.cuda it raises during
collection instead, taking the whole module with it. Unsloth supports both
backends, so this is a machine somebody runs.

Adds has_real_cuda() beside it, recorded in the same pre-spoof pass so both
answers come from one priming call in conftest. A lazily recorded second cache
could be filled after a test module had imported the spoof, leaving the CUDA
answer spoofed while the accelerator answer was real.

tests/test_stopping_criteria_device.py is the one this PR introduced: it names
cuda three times. Reproduced on a simulated XPU-only host, where the old gate
runs the test and the new one skips it.

The scan then found the same mis-gating in eight files that predate this PR, each
already saying so in its own reason string ("requires a CUDA GPU", "CUDA Triton
kernels required", "needs CUDA"). Narrowing them changes nothing on a CUDA host
and stops the crash elsewhere.

A new guard keeps it from coming back: a test that asks for a cuda device by name
and gates only on the broad probe now fails the scan. It matches the device string
itself, so a test that merely mentions cuda in a message is not swept up, and both
directions are pinned.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 3 commits September 15, 2026 05:30
… alone

Two defects in the previous commit's scanner, both found in review.

The short-circuit exemption accepted has_real_accelerator() as dominating any
spoofable probe, including one in the torch.cuda namespace. That is the same
mistake one level up from the one the commit set out to fix: the broad probe is
true on an XPU-only or Ascend NPU-only host, so a decorator gated on it still
evaluates a CUDA-only API during collection on a machine with no CUDA. The rule
is now per-namespace, so only has_real_cuda() dominates a torch.cuda call, and
the four fixtures that asserted the old behaviour are now on the reject side.

The CUDA-only scan also matched any body containing the string "cuda", which
caught two tests in tests/utils/test_packing.py that fall through to
torch.device("xpu") when CUDA is absent, against an xpu dtype arm in
_build_packed_training_setup. Narrowing those dropped real XPU coverage, so they
go back to has_real_accelerator(). A body that names xpu, npu, mps or hpu as a
device picks its device at run time and is no longer treated as CUDA-only.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@danielhanchen
danielhanchen merged commit 3fe93e0 into main Sep 15, 2026
32 checks passed
@danielhanchen
danielhanchen deleted the fix-ascend-npu-name-and-spoofable-skip-guard branch September 15, 2026 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant