d16_format_probe: compute the rule models once per input (TEST-D16-MODELS-CACHE) #2376
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!2376
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2376"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@enlight3d on GitHub · 2026-11-26 16:29 UTC · original
enlight3d:test-d16-models-cache→main· +35 −10 · 2 files · 2 commits · merged 2026-11-27 09:55 UTCClaimed in the coordination issue as: TEST-D16-MODELS-CACHE (#1608), picking up the item @amine23012004 claimed and then released as free for anyone (#1608).
What and why
models16()andmodels32()intools/d16_format_probe.pyare pure functions of (kind, bits, value), but a sweep,classify()and the tests call them about 955,000 times for about 131,000 distinct inputs, each time redoing the exactFractionarithmetic. The rule bits are now computed once per input (functools.lru_cacheon a tuple), and each call still returns a fresh dict, so a caller that edits it cannot change what the next call gets. Every caller looks the rules up by name (classify()iterates its ownrulestuple), so the dict's key order does not reach any output. No result changes.The commit is amine23012004's, kept as written and with them as its author (their branch
test-d16-models-cache, 9a7128947), rebased on currentmain. It adds a test that the cache hands each call its own dict.tests/semantic/test_buffer_load_format.pyandtools/rdna2spirv.pyonly name the probe in comments; nothing else imports it excepttools/d16_image_probe.pyand the two d16 test files.This PR touches the same two files as #1664; the two merge without conflict (
git merge-treeof both heads).Tested
On
main2e2c51c09+ this commit, Windows 11 (26300), Python 3.12.10, i9-13900K, timed while a fullmake_game.pybuild was using the CPU, so the absolute times are noisy:python -I tests/test_d16_format_probe.py: 26.6 s before, 12.0 s after (12.4 s after the rebase); 24 tests OK (2 skipped, GPU).python -I tests/test_d16_image_probe.py: 22.6 s before, 17.0 s after; OK (2 skipped).python -I tools/d16_format_probe.py --self-test: self-test ok.python -I tests/test_image_d16_probe.py: 22 tests OK (2 skipped).python -m unittest discover -s tests/semantic -p test_buffer_load_format.py: 34 tests OK.python -m compileall -q make_game.py tools tests,python -I make_game.py --help,python -I tests/test_text_write_line_endings.py(3 OK),python -I scripts/check_no_game_data.pyon both changed files,git diff --check: clean.amine23012004 measured 26.7 s -> 7.7 s and 21.8 s -> 13.1 s on an idle machine.
GPUs tested: n/a (CPU-only change; the probe's GPU kernels are untouched)
AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (No translator rule here.)🤖 Generated with Claude Code
@ecox982745 on GitHub · 2026-11-26 17:48 UTC · original
I compared the cached model at
e6b97d1e43with its parent2e2c51c092on every encoding of UNORM8, SNORM8, UNORM10, UNORM16 and SNORM16. For each input I compared the dictionaries by rule name: all ten half-precision results (including denormal-flush variants) and all three single-precision results.All 1,723,904 named outputs matched across the 132,608 inputs. Clearing a returned dictionary from each model also left the next lookup intact for the SNORM16 minimum and UNORM16 maximum. This was a CPU comparison on Windows 11, Python 3.13.18. The float16 passthrough control does not use these rounding models.
AI-assisted verification with OpenAI Codex (GPT-6).
@lixonik on GitHub · 2026-11-26 18:02 UTC · original · reviewed
Windows 11: 22/24 D16 model tests passed with two fixture-dependent checks skipped. The cache's fresh-dict regression passed. No findings from the checks that ran.
AI-assisted.
@wavehs on GitHub · 2026-11-26 18:32 UTC · original · reviewed
macOS arm64 check of #2376 (TEST-D16-MODEL-CACHE). A pure Python tool change; no GPU or build involved.
Tested head
e6b97d1e4with its diff applied to a copy ofmainbdbc7e923(git applyclean; branch 12 commits behind).Environment: Apple M3 Pro, arm64, macOS 27.2.1, CPython 3.15.2.
The change itself is correct
RULES16is("f32_rte", "f32_rtz", "rte", "rtz", "mul_rte")and the cached tuple is built in exactly that order (tools/d16_format_probe.py:66and thebits = (...)line), so thedict(zip(KEYS16, ...))mapping is right;RULES32likewise. A wrong order would fail the existing value assertions, and they pass.KEYS16isRULES16 + tuple(r + "_flush" ...), so the dict always has all ten keys.python -I tests/test_d16_format_probe.py: 24 tests, OK (skipped=2). The full file takes 3.3 s against 10.9 s onmain- the optimization does what it says.python -I scripts/run_semantic_tests.py tests/semantic: 500 files / 6,367 tests / 0 files failed, same asmain.Finding: the new test does not test the cache
test_models_are_cached_but_each_call_gets_its_own_dictonly checks that a caller's edit to the returned dict does not reach the next caller. I verified the two cases separately:@functools.lru_cache(maxsize=None)decorators (keepingKEYS16and the dict copy) leaves the new test green - so a future edit that drops the cache is not caught, and the 10.9 s regression would come back silently;Since the point of the PR is the caching, assert it:
functools.lru_cacheexposes_models16.cache_info(), sobefore = _models16.cache_info().hits; models16(kind, n, v); models16(kind, n, v); self.assertEqual(_models16.cache_info().hits, before + 1)pins a hit without timing anything. That plus the existing fresh-dict assertion would cover both promises in the docstring.I found no functional bug in the change; the unbounded cache is bounded in practice by the probe's 16-bit
vdomain.python -I scripts/check_no_game_data.pyon the two files: clean.No game files. AI-assisted.
@enlight3d on GitHub · 2026-11-26 21:13 UTC · original
@wavehs Fixed in
afee1e34b:test_models_are_cached_but_each_call_gets_its_own_dictnow also asserts that the secondmodels16/models32call adds one hit to_models16.cache_info()/_models32.cache_info(), next to the fresh-dict check.Checked on Windows 11, CPython 3.12.10:
python -I tests/test_d16_format_probe.py: 24 tests OK (skipped=2).@functools.lru_cachedecorators removed, the test now fails ('function' object has no attribute 'cache_info'); before this commit it stayed green.python -I scripts/check_no_game_data.pyon both files andgit diff --check: clean.AI-assisted (Claude Code).