Repository navigation
Name the Ascend NPU as its own tests expect, and close the spoofable skip-guard hole - #10968
Merged
danielhanchen merged 7 commits intoSep 15, 2026
Merged
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex security review |
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.
… 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.
danielhanchen
deleted the
fix-ascend-npu-name-and-spoofable-skip-guard
branch
September 15, 2026 06:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Repo tests (CPU, python)andRepo 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: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)andRepo 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 ontorch.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_criteriato stop hardcoding CUDA and added a test gated ontorch.cuda.is_available(). That is exactly the shapetests/python/test_accelerator_skip_guards.pyexists to reject, becausetests/_zoo_aggressive_cuda_spoof.pypatchestorch.cuda.is_availableto 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.cicontext; the rest sat inaction_requiredand 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()acceptstorch.xpuandtorch.acceleratortoo, so on an XPU-only or Ascend NPU-only host it is true, and a test that namescudaun-skips there and dies allocating on a device that is not present. Where the decorator itself calls intotorch.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 calltests/conftest.pyalready 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_availableprobes. The spoof was already answering six more:device_countreturns 1,is_initializedTrue,is_bf16_supportedTrue,get_device_capability(8, 0), plusget_device_nameandget_device_properties. A guard writtenskipif(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.pygates a two-GPU test ontorch.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.pyreadsget_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 whethertorch.cudais 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,mpsorhpuas a device is picking its device at run time and is left alone. That exemption is not theoretical: two tests intests/utils/test_packing.pyfall through totorch.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.pyandtests/test_device_helpers.py: 39 passed, from3 failed, 20 passedbefore.The 140 local failures in
tests/python/on this branch reproduce identically on unmodifiedmain(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.pyandtests/utils/test_xformers_capability_gate.pydo not import in that environment for the same reason; CI pins a matching pair. The guard reads all three withast, so their skip conditions are checked without importing them.The
device_type.pychange is source, not test, so it wants a look from someone other than me.