Skip to content

tools: fma_mix_probe tells single from double f16 rounding of v_fma_mix (nearest mode) - #1487

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Zmirzz:test/fma-mix-nearest-probe
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Zmirzz:test/fma-mix-nearest-probe

Conversation

@Zmirzz

@Zmirzz Zmirzz commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Item: new SHADER item, claimed in #1380 (ID to be assigned by the coordinator).

What and why

docs/design.md (Known approximations) leaves one f16 question open: "Nearest-mode v_fma_mix* still rounds twice (f32 Fma, then narrow): main's behaviour, unmeasured." Does RDNA2 round the exact x · y + z once, straight to f16? Or does it round to f32 first and then narrow (double), as the translator does?

The RX 6800 word behind the toward-zero rule can't settle it. For 1 · 2 - 2^-30 both readings give 0x4000 in nearest mode, and toward zero two truncations equal one. No fuzzer generates v_fma_mix* today.

tools/fma_mix_probe.py (standard library only) has seven inputs where the two rules differ by one f16 ulp, plus a control:

  • ties just above and just below, for both signs;
  • the subnormal tie at 2^-25;
  • the overflow tie at 65520 (inf against 65504);
  • a tie made by the product rather than by z;
  • the same with f16 operands (op_sel_hi:[1,1,1]).

It reports which rule each side follows:

Side What it runs
translator (always) rdna2spirv.py → tests/semantic/spirv_mini_interp.py, on the CPU
--oracle hw the native instruction on an AMD GPU through tools/hw_oracle, one wave32 lane, under the game's inferred MODE (IEEE 0, DX10_CLAMP 1, f32 denormals flushed, f16 kept, nearest). This is the measurement.
--oracle vulkan --build DIR the translated compute shader through vk_compute_test. Operands come in user SGPRs (VKC_SGPRS), so the driver can't fold them.

Exit codes:

  • 0: every side follows the same rule;
  • 1: the sides differ, or a side follows neither rule;
  • 2: harness error;
  • 77: a requested side is unavailable.

The translator is unchanged. If AMD hardware rounds once, the fix is to use Gen.fma_mix_odd in nearest mode too: round-to-odd at f32 narrows exactly, and it is already there for toward zero. That fix was tried on a scratch copy (results below). It is not part of this PR, because it waits for the measurement.

Tested

  • python -I tests/test_fma_mix_probe.py: 18 tests pass. They check:
    • the expected words against a derivation written out by hand in the module docstring, not the tool's own model;
    • that the translator follows the double rule today;
    • that the Vulkan side's compute listing gives the pixel shader's words on spirv_mini_interp.run_workgroup;
    • the verdict logic (a mix of rules, a third value, a wrong control, a clobbered other half, a missing result);
    • the GFX10 kernel text and its MODE directives;
    • the CLI exit codes.
  • Negative control: a copy of tools/ with fma_mix_odd in nearest mode makes the probe report "single". The two tests that pin the rule fail, and the rest still pass.
  • GPU: NVIDIA GeForce RTX 5060 (Blackwell, desktop), driver 621.24, Windows 11 (26300), vk_compute_test built with MSVC (VS Build Tools 2026) at this commit, SPIRV-Tools vulkan-sdk-1.4.363.0:
    • --oracle vulkan: 8 of 8 words follow the translator's (double) rule;
    • the fma_mix_odd copy gives the single-rounded word on all seven inputs on the GPU too;
    • the compute module passes spirv-val --target-env vulkan1.2.
  • --oracle hw exits 77 on this machine, which has no AMD GPU. The AMD run is what's still missing. It needs an RDNA GPU with the hardware-oracle setup in docs/shader_validation.md.
  • python -m compileall -q make_game.py tools tests, python -m unittest discover -s tests/semantic (OK, 1 skip as on main), tests/test_docs_links.py.

Docs:

  • docs/shader_validation.md: a new section with a results table, one RTX 5060 row and the hw column still empty.
  • docs/design.md: one sentence pointing to the probe.

Other open PRs touching docs/shader_validation.md: #1345, #1365, #1373 (different sections). #1364 changes the translator's v_fma_mix* Fma emission under --fma khr. That is orthogonal: it makes the f32 Fma correctly rounded, while this PR is about the f32 → f16 step. None of the probe's inputs depend on whether the f32 Fma is fused.

No game data, keys or decryption. AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • No new translator rule (the probe only measures; the contract table is unchanged).

🤖 Generated with Claude Code

docs/design.md leaves one f16 question open: in round-to-nearest mode, does
v_fma_mixlo/hi_f16 round the exact x * y + z once to f16, or to f32 first
and then narrow? The translator does the second (f32 Fma, then f16_res).
The RX 6800 word behind the toward-zero rule can't settle it, because
1 * 2 - 2^-30 gives 0x4000 under both readings in nearest mode.

tools/fma_mix_probe.py has seven inputs where the two rules differ by one
f16 ulp, plus a control:
- ties just above and below, for both signs;
- the subnormal tie at 2^-25;
- the overflow tie at 65520;
- a tie made by the product;
- the same with f16 operands.
It reports which rule each side follows:
- the translator, on the CPU interpreter;
- --oracle hw: the native instruction on an AMD GPU, through
  tools/hw_oracle, under the game's inferred MODE;
- --oracle vulkan: the translated compute shader through vk_compute_test,
  with operands in user SGPRs so the driver can't fold them.

tests/test_fma_mix_probe.py checks:
- the expected words against a derivation written out by hand;
- that the translator follows the double rule today;
- that the compute listing gives the pixel shader's words;
- the verdict and exit codes.

On an RTX 5060 (621.24) the translated shader follows the translator's rule
on all eight inputs. A translator copy that uses fma_mix_odd in nearest mode
gives the single-rounded words on the CPU and on the GPU. That is the fix
if AMD hardware turns out to round once. The translator itself is unchanged.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ItsNotRexed
ItsNotRexed merged commit 3aeeedb into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
@Zmirzz
Zmirzz deleted the test/fma-mix-nearest-probe branch November 22, 2026 12:43
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.

2 participants