d16_format_probe: compute the rule models once per input (TEST-D16-MODELS-CACHE) #2376

Merged
leonida/mandy
enlight3d merged 2 commits from test-d16-models-cache into main 2026-11-27 09:55:42 +00:00

Pull request successfully merged and closed

enlight3d commented 2026-11-26 16:29:04 +00:00

Claimed in the coordination issue as: TEST-D16-MODELS-CACHE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7197567323), picking up the item @amine23012004 claimed and then released as free for anyone (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191191563).

What and why

models16() and models32() in tools/d16_format_probe.py are 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 exact Fraction arithmetic. The rule bits are now computed once per input (functools.lru_cache on 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 own rules tuple), 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 current main. It adds a test that the cache hands each call its own dict.

tests/semantic/test_buffer_load_format.py and tools/rdna2spirv.py only name the probe in comments; nothing else imports it except tools/d16_image_probe.py and the two d16 test files.

This PR touches the same two files as #1664; the two merge without conflict (git merge-tree of both heads).

Tested

On main 2e2c51c09 + this commit, Windows 11 (26300), Python 3.12.10, i9-13900K, timed while a full make_game.py build 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.py on 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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule here.)

🤖 Generated with Claude Code

Claimed in the coordination issue as: TEST-D16-MODELS-CACHE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7197567323), picking up the item @amine23012004 claimed and then released as free for anyone (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191191563). ### What and why `models16()` and `models32()` in `tools/d16_format_probe.py` are 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 exact `Fraction` arithmetic. The rule bits are now computed once per input (`functools.lru_cache` on 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 own `rules` tuple), 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 current `main`. It adds a test that the cache hands each call its own dict. `tests/semantic/test_buffer_load_format.py` and `tools/rdna2spirv.py` only name the probe in comments; nothing else imports it except `tools/d16_image_probe.py` and the two d16 test files. This PR touches the same two files as #1664; the two merge without conflict (`git merge-tree` of both heads). ### Tested On `main` 2e2c51c09 + this commit, Windows 11 (26300), Python 3.12.10, i9-13900K, timed while a full `make_game.py` build 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.py` on 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator rule here.) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ecox982745 commented 2026-11-26 17:48:28 +00:00

I compared the cached model at e6b97d1e43 with its parent 2e2c51c092 on 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).

I compared the cached model at `e6b97d1e43` with its parent `2e2c51c092` on 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 reviewed 2026-11-26 18:02:02 +00:00
lixonik left a comment
No description provided.
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 reviewed 2026-11-26 18:32:56 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2376** (TEST-D16-MODEL-CACHE). A pure Python tool change; no GPU or build involved. Tested head `e6b97d1e4` with its diff applied to a copy of `main` `bdbc7e923` (`git apply` clean; branch 12 commits behind). Environment: Apple M3 Pro, arm64, macOS 27.2.1, CPython 3.15.2. **The change itself is correct** - `RULES16` is `("f32_rte", "f32_rtz", "rte", "rtz", "mul_rte")` and the cached tuple is built in exactly that order (`tools/d16_format_probe.py:66` and the `bits = (...)` line), so the `dict(zip(KEYS16, ...))` mapping is right; `RULES32` likewise. A wrong order would fail the existing value assertions, and they pass. - The flush rule is applied to all five cached words, as before; `KEYS16` is `RULES16 + 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 on `main` - the optimization does what it says. - `python -I scripts/run_semantic_tests.py tests/semantic`: 500 files / 6,367 tests / 0 files failed, same as `main`. **Finding: the new test does not test the cache** `test_models_are_cached_but_each_call_gets_its_own_dict` only checks that a caller's edit to the returned dict does not reach the next caller. I verified the two cases separately: - removing both `@functools.lru_cache(maxsize=None)` decorators (keeping `KEYS16` and 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; - returning the cached value directly (so the dict is shared) fails, which is the half the test does pin. Since the point of the PR is the caching, assert it: `functools.lru_cache` exposes `_models16.cache_info()`, so `before = _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 `v` domain. `python -I scripts/check_no_game_data.py` on the two files: clean. No game files. AI-assisted.
enlight3d commented 2026-11-26 21:13:13 +00:00

@wavehs Fixed in afee1e34b: test_models_are_cached_but_each_call_gets_its_own_dict now also asserts that the second models16/models32 call 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).
  • With both @functools.lru_cache decorators 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.py on both files and git diff --check: clean.

AI-assisted (Claude Code).

@wavehs Fixed in afee1e34b: `test_models_are_cached_but_each_call_gets_its_own_dict` now also asserts that the second `models16`/`models32` call 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). - With both `@functools.lru_cache` decorators 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.py` on both files and `git diff --check`: clean. AI-assisted (Claude Code).
Sign in to join this conversation.
No description provided.