Skip to content

amd_swizzle: validate arguments before needing numpy, and gate the tests the way the tool runs - #2671

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
fiestorra:fix/amd-swizzle-numpy-late
Dec 1, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
fiestorra:fix/amd-swizzle-numpy-late

Conversation

@fiestorra

@fiestorra fiestorra commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Two independent reasons tests/test_amd_swizzle.py is red on a machine where numpy came from pip install --user, and both are the test asking the wrong question.

1. The tool imported numpy before it validated anything

tools/amd_swizzle.py had import numpy as np at module level, so with numpy invisible to the process every run died before argument checking:

python3 -I tools/amd_swizzle.py blob.bin --width 0 --height 4
  before:  ERROR: amd_swizzle.py needs numpy (python -m pip install numpy)
  after:   ERROR: --width and --height must be at least 1 (got 0x4)

numpy is now imported late and _require_numpy() is called after the argument, file and header checks - the checks that never needed it. The computation still demands it and still says so, with the same message.

2. The skip gate asked the wrong process

try:
    import amd_swizzle as A      # in THIS process
    HAVE_NUMPY = True
except ModuleNotFoundError:
    HAVE_NUMPY = False

but the tests it gates run the tool as sys.executable -I …, and -I implies -s, which hides the user site. So the gate opens and the child dies. It now asks the tool's question:

def _numpy_visible_to_the_tool() -> bool:
    probe = subprocess.run([sys.executable, "-I", "-c", "import numpy"], capture_output=True)
    return probe.returncode == 0

and the five cases that need a real run (the detile, the no-pattern report, the successful run and the two previews) are gated on it. The in-process needs_numpy gate stays for the module-level tests - two questions, two probes.

Measured

main b8bd37111, Windows 11, Python 3.13.14, where numpy is in the user site:

tests/test_amd_swizzle.py before:  9 failed, 8 passed, 3 skipped
                       after:     14 passed, 6 skipped   (0 failed)

and on a machine where -I can see numpy - CI - _numpy_visible_to_the_tool() returns true, so the gated cases run as before.

A correction, because an earlier version of this description overstated it and the thread caught it: making the import late also made import amd_swizzle succeed with A.np is None, so the test file's try: import amd_swizzle; HAVE_NUMPY = True no longer established that numpy was present - two module-level cases that compute with numpy then ran and failed. merzleckeier found it (READ IN CODE) and sent a test-only follow-up, applied here as its own commit (aec457e1d, theirs) on top of mine (34046550a) so the history says whose fix it is. The gated CLI cases were unaffected; the ungated ones were the regression.

What this does not do

It does not make the tool work without numpy, and it should not: the computation is numpy. It makes the errors reachable and the skip honest. tests/test_tile_probe.py has the same symptom (5 failures, "needs numpy" from the child) and no gate at all; it needs the same treatment and I did not touch it here.

The extra commit at the tip (no longer carried)

The branch used to carry a3b5ebea1 (test(semantic): align computed-I/J f16 interp oracle…), added only because semantic-tests was red on main for that oracle and every branch cut from main inherited it. main now has the same edit as c717967d0, so a rebase drops the commit and this pull request no longer touches tests/semantic/test_interp_computed_ij_fallback.py - the diff is only what this change actually needs.

AI-assisted: yes - DeepSeek v4.1 (flash) as the coding agent, with a separate reviewer agent reading the change before submission. Where and on what: the measurements above were taken on Windows 11 with MSVC 19.44.35226 on an RTX 4090 (NVIDIA driver 621.24) where a C++ or GPU path is involved; Python and docs cases need none of it.

Written with an AI assistant.

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, Python 3.12.16, main 40e7fe6, PR head b713c15. MEASURED, both with numpy (venv, pytest) and without it (Homebrew python3.12 -I, where import numpy raises ModuleNotFoundError):

main's test + main's tool (red) this PR (green)
with numpy (pytest) 20/20, 8 subtests 20/20, 8 subtests
without numpy (python3.12 -I tests/test_amd_swizzle.py) Ran 4, OK Ran 8, FAILED (errors=4)

So there is no red with numpy, and without numpy this PR turns an OK run into errors. The errors are AmdSwizzleCliGuardTests.test_write_oserror_returns_2_without_traceback and test_write_value_error_returns_2_without_traceback:

File "tools/amd_swizzle.py", line 257, in main
    _require_numpy()  # every check above must report its own error without numpy
SystemExit: ERROR: amd_swizzle.py needs numpy (python -m pip install numpy)

Those two cases get past every argument/file check and reach the computation, so with numpy absent they hit _require_numpy() (the fake numpy module in setUp does not help: np is already None in the module they call). They need the same numpy gate as the computation tests, or a mock of the computation.

Merges cleanly with main and with #2566.

AI-assisted: Claude Code

@merzleckeier

Copy link
Copy Markdown
Contributor

I confirmed the stdlib failure from OreoFR’s macOS run on CachyOS with Python 3.14.10. I also tested with NumPy and Pillow installed in the user site. The test process can import both, while its python -I children see neither.

  • MEASURED With neither dependency available, pytest also activates test_64k_block_extent_comes_from_the_element_count and test_uncompressed_feature_uses_raw_rgb_bytes, which fail because NumPy is absent. With both dependencies in the user site, the R8, RG8 and partial-BC preview cases still fail in the isolated child at the NumPy check.
  • READ IN CODE Importing amd_swizzle now succeeds with A.np is None, so a successful module import no longer establishes NumPy availability. The two mocked output-write checks can patch the new _require_numpy guard in their existing fake-computation fixture and still exercise the real output error handling.
  • MEASURED With the test-only follow-up, all four direct stdlib guard checks pass. Together with the test adjustment for tests: four numpy gates decided in the wrong process, so their cases judged the exit code instead #2672, the three affected files pass with both dependencies visible, both user-site-only, Pillow user-site-only, and both absent. With both visible, the combined run still has 65 passes and one expected skip.

Base 40e7fe69, PR b713c15f. The follow-up modifies only this PR’s test file. AI-assisted with OpenAI Codex.

@fiestorra

Copy link
Copy Markdown
Contributor Author

Rebased onto current main with the one commit main is missing, like my other pull requests:

a3b5ebea1  (TEST-INTERP-COMPUTED-IJ-FALLBACK-RTZ, the commit #2633 carries)

semantic-tests was the only failing check here and it fails on main, for the interp oracle - so a branch cut from main inherits it. Nothing here touches the translator.

Verified after the rebase, on this branch:

python3 -m pytest tests/test_amd_swizzle.py tests/semantic/test_interp_computed_ij_fallback.py -q
  -> 23 passed, 6 skipped, 21 subtests

The six skips are the numpy-dependent cases this machine cannot run (-I hides the user site), which is what the change is about in the first place. If #2633 merges first this second commit becomes a duplicate and can be dropped.

Written with an AI assistant.

@fiestorra

Copy link
Copy Markdown
Contributor Author

You are right, and it is a regression I introduced: making the import late also made "the import succeeded" stop meaning "numpy is here". Applied as c7133a80d.

For the record, the mechanism in my own words so it stays visible: tests/test_amd_swizzle.py used to derive HAVE_NUMPY from

try:
    import amd_swizzle as A
    HAVE_NUMPY = True
except ModuleNotFoundError as e:
    ...

and after the lazy import that try always succeeds, with A.np is None. So the gate stays open and the two module-level cases that compute with numpy run and fail - exactly what you measured with test_64k_block_extent_comes_from_the_element_count and test_uncompressed_feature_uses_raw_rgb_bytes.

Your follow-up (36654350a2) is applied on top, unchanged, as a separate commit so the history says whose fix it is. Verified here after it:

python3 -m pytest tests/test_amd_swizzle.py -q   ->  14 passed, 6 skipped, 8 subtests

That is this machine's user-site configuration, which is the case the PR is about; your matrix (both visible, both user-site-only, Pillow user-site-only, both absent) is the evidence I do not have and could not produce here, and the four direct guard checks passing with the follow-up is the part that matters most.

Two things I will fix in the body rather than pretend were covered:

  • the PR claimed "on a machine where -I sees numpy the probe returns true and the three gated cases run exactly as before" - the module-level cases were not in that sentence and are exactly the ones the lazy import broke;
  • I did not test the "both absent" combination at all, which is where your test_64k_block_extent… finding lives.

Thank you for reproducing OreoFR's macOS report instead of taking my word for it - a reviewer who runs the matrix is worth more than one who reads the diff.

Written with an AI assistant.

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Re-check by OreoFR on the new head b0013cd, same Mac (macOS 27.2.1, Apple M5, Python 3.12.16), main 1d22590. MEASURED:

AI-assisted: Claude Code

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Re-check by OreoFR on head c7133a8 ("exercise amd_swizzle guards with isolated dependencies"), same Mac, main 1d22590. MEASURED:

AI-assisted: Claude Code

@fiestorra

Copy link
Copy Markdown
Contributor Author

Thank you - and the conflict you found is not what I would have guessed. I diffed the two sides: #2566 carries a different implementation of the same interp fix, not the same edit.

#2633 / a3b5ebea1 (what this branch carries) #2566
helper f16_rtz_bits(x) f16_rtz_bits(val)
docstring "Narrow x to f16 bits toward zero…" "Narrow to f16 bits toward zero…"
size 30,936 B 30,870 B
lines differing 504 of 566

So "resolve by taking either side" is wrong here: it is a choice between two oracles, and only one of them can be the one that ends up describing SHADER-468 in main. I have written that up in the coordination thread rather than pretending the conflict is trivial - it is the useful thing your check produced.

What the commit in this branch is, precisely: a3b5ebea1, the commit #2633 carries, added because semantic-tests is red on main and every branch cut from it inherits the failure. It is disposable - when #2633 or #2566 lands, a rebase drops it and the conflict goes with it. If the maintainers would rather I remove it now and let this pull request show the inherited red until then, say so and I will.

On the rest of your re-check: thank you for the no-numpy case. Ran 4, OK with python3.12 -I is exactly the configuration I cannot produce here, and it is the one that disproved my first version of this change. merzleckeier's follow-up is in, and both of you have now run the case I could not.

Written with an AI assistant.

@fiestorra

Copy link
Copy Markdown
Contributor Author

Correction to what I wrote above: I diffed the wrong thing. It is one change, not two.

I compared the two files at their own branch heads and read "504 of 566 lines differ" as two implementations. Those files come from different bases, so most of that was drift. Compared properly - the added lines of each patch, and the file a3b5ebea1 produces when applied to current main - the only differences are x vs val as the parameter name and two comment phrasings. Same helper, same algorithm, same constants.

So your report was right and my escalation of it was wrong: the conflict is the same edit on both sides, and taking either resolves it. The duplicate is still real - whichever of #2633 / #2566 lands first, the other's copy is dropped - and the copy in this branch is exactly a3b5ebea1.

Thank you for finding the conflict; the part I got wrong after that was mine, not yours.

Written with an AI assistant.

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Update by OreoFR: the conflict with #2566 I reported here is gone. main (1b0f657) now carries the SHADER-468 change to tests/semantic/test_interp_computed_ij_fallback.py (71db4c405), and this PR merges cleanly both with main and on top of main + #2566 (git merge-tree, checked 2026-11-30 11:10 local). Note also that my earlier check merged this PR with main first and then compared against #2566's head, which can report a conflict through an older merge base; I now check main + #2566 first, then the PR.

AI-assisted: Claude Code

fiestorra and others added 2 commits November 30, 2026 14:55
…tion

Two things kept this test file red on a machine where numpy came from
pip install --user:

- the tool imported numpy at module level, so a bad --width reported
  "needs numpy" instead of "must be at least 1". numpy is imported late now
  and only the computation requires it;
- the skip gate asked whether numpy imports *here*, while the tests run the
  tool with -I, which hides the user site. It now probes the way the tool runs.

Measured on main b8bd371, Windows, Python 3.13: 9 failed, 8 passed, 3 skipped
before; 14 passed, 6 skipped after. On a machine where -I sees numpy, the probe
returns true and the three gated cases run exactly as before.

Written with an AI assistant.
@fiestorra
fiestorra force-pushed the fix/amd-swizzle-numpy-late branch from c7133a8 to aec457e Compare November 30, 2026 13:55
@fiestorra

Copy link
Copy Markdown
Contributor Author

Re-verified after the rebase onto main 20fe0c754, on this machine (Windows 11, MSVC 19.44.35226): the diff no longer carries the interp-oracle commit (main has it as c717967d0), and what is left still passes.

python3 -m pytest tests/test_amd_swizzle.py -q
14 passed, 6 skipped, 8 subtests passed in 1.51s

What I did not test: the rest of the change, and anything the rebase did not touch. This confirms the rebase kept the change working, not that the change is correct - that is what the review and the CI are for.

Written with an AI assistant.

@aegish-zagi

Copy link
Copy Markdown
Contributor

VERIFY-2671 — PASS

  • PR: amd_swizzle: validate arguments before needing numpy, and gate the tests the way the tool runs #2671 | head aec457e | merge state vs main b17b22d: CLEAN (scratch merge, no conflicts)
  • Tests run (PR head merged with current main, venv with the pinned tests/requirements.txt: pytest 9.2.0, numpy 2.5.5, Pillow 13.0.0): python -m pytest tests/test_amd_swizzle.py — 20 passed, 8 subtests passed; python -I tests/test_amd_swizzle.py (script mode) — 8 tests OK
  • Red-on-main vs green-on-PR: the PR's test file run against plain main (b17b22d) gives 2 failed, 18 passed — AmdSwizzleCliGuardTests::test_write_oserror_returns_2_without_traceback and test_write_value_error_returns_2_without_traceback fail on main because main's tools/amd_swizzle.py has no _require_numpy guard to exercise (top-level numpy import, validation after import). On the PR both pass.
  • Diff audit: 2 files. tools/amd_swizzle.py: numpy import becomes optional (np = None when missing), _require_numpy() raises SystemExit with the pip hint, and main() calls it only after argument/header validation, so validation errors report themselves without numpy. tests/test_amd_swizzle.py: HAVE_NUMPY now reads A.np, CLI tests that launch the tool get subprocess gates probing what python -I can actually see (numpy / Pillow), and the guard tests patch _require_numpy. No game data.
  • Platform: Linux, Python 3.12, Profile Z (no GPU)
  • AI-assisted: yes (Muse Wonder, Muse by Meta)

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at aec457e1d.

  • python -I tests/test_amd_swizzle.py: 8 tests OK, including the isolated-dependency guard cases (test_symbols_and_xor, test_rejects_code).

Environment: Windows 11 Pro 10.0.26200.9711, Python 3.12.10.

AI-assisted: yes (opencode).

@aslan2142 aslan2142 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head diff against main: tools/amd_swizzle.py cleanly defers the missing-numpy exit via _require_numpy() until after CLI argument and header validation, and tests/test_amd_swizzle.py properly gates subprocess tests on python -I visibility of numpy/Pillow. Clean-room verified and all CI checks green.

(AI-assisted review.)

@aslan2142
aslan2142 merged commit 21e6bbf into ProjectLeonida:main Dec 1, 2026
5 checks passed
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.

6 participants