Skip to content

fix(translator): round v_fma_mixlo/hi_f16 once to f16 (finding 7079901079) - #1774

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shadshuvo:fix/fma-mix-single-rounding
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shadshuvo:fix/fma-mix-single-rounding

Conversation

@shadshuvo

Copy link
Copy Markdown

Overview

Hardware measurement on AMD RDNA2 (gfx1036, reported by @Sanonymous01 in comment 7079901079) confirms that �_fma_mixlo_f16 and �_fma_mixhi_f16 round the exact x * y + z once, straight to f16, in nearest-even mode as well as toward zero (all 7 discriminating tie inputs in ools/fma_mix_probe.py follow the single-rounding rule).

Previously, in nearest-even mode the translator computed f32 Fma and then narrowed ( 16_res), which produced a double rounding error (1 f16 ulp off on ties).

Changes

  1. Translator ( ools/rdna2spirv.py):
    • �_fma_mixlo_f16 and �_fma_mixhi_f16: use Gen.fma_mix_odd across all rounding modes. Round-to-odd at f32 computes intermediate round-to-odd arithmetic, which narrows directly and exactly to f16 in both nearest-even and toward-zero modes without double rounding.
  2. Probe Test & Validation ( ests/test_fma_mix_probe.py, docs/shader_validation.md):
    • Updated est_translator_rounds_twice -> est_translator_rounds_once asserting probe.SINGLE and the single-rounded words for all 7 cases (A through G).
    • Documented the RDNA2 measurement in docs/shader_validation.md and updated docs/design.md.
  3. Semantic Tests ( ests/semantic/test_valu_fma_mix.py):
    • Added est_single_rounding_discriminating_cases covering discriminating tie cases A, B, C, and G (which would fail under double rounding).

Testing

  • python -m compileall -q make_game.py tools tests: OK
  • python -I make_game.py --help: OK
  • python -I tools/fma_mix_probe.py: OK (all 7 discriminating cases report -> single, exit code 0)
  • python -I tests/test_fma_mix_probe.py: 18/18 tests passed
  • python -I tests/semantic/test_valu_fma_mix.py: 9/9 tests passed
  • python -m pytest tests/test_mode_round.py: 53/53 tests passed
  • python -m unittest tests/semantic/test_f16_fma_rounding.py tests/semantic/test_f16_int_convert.py: 42/42 tests passed

Tested on Windows 11 with AMD Radeon RX 7800 XT, driver 24.20.19.05. Zero game content, executables, or shaders involved.
AI-assisted.

@esteban-dcp

esteban-dcp commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested 383c68bb (Linux, rebuilt runtime/build/linux). No game files.

  • CPU: tests/semantic/test_valu_fma_mix.py 9 OK; tests/test_fma_mix_probe.py OK.
  • tools/fma_mix_probe.py --oracle vulkan: RTX 4050 Laptop (Ada, driver 620.73.04): translator and Vulkan follow the single rule, 8 of 8 words. Radeon 740M (RADV): translator follows the single rule, but the Vulkan side gives 7 of 8 words and reports "follows neither rule": F, (1 + 2^-12)^2 + 0, gives 0x3c00 instead of 0x3c01. Before this change RADV got 1 of 8 (it followed double rounding), so this is an improvement, not a regression.
  • scripts/run_gpu_tests.py, 0 failures: RTX 4050 native 127/166 (39 skipped), RADV 158/166 (8 skipped).

About F on RADV: my guess, not confirmed by dumping the driver's output, is that fma_mix_odd's product error Fma(x, y, -p) is only guaranteed exact with OpFmaKHR. With --fma khr I got all 8 words on RADV. It may deserve a note in docs/shader_validation.md next to the probe results; I had written one in a branch I won't open.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Hardware measurement on RDNA2 (gfx1036, finding 7079901079) confirms that
v_fma_mixlo_f16 / v_fma_mixhi_f16 round the exact fma once, straight to
f16, across all rounding modes (7 of 7 discriminating cases in
tools/fma_mix_probe.py match single rounding). The translator previously
double-rounded in nearest-even mode (f32 Fma then f16 narrow).

Change v_fma_mixlo_f16 / v_fma_mixhi_f16 to use Gen.fma_mix_odd across all
modes, which rounds to odd at f32 so that narrowing to f16 gives the
exact single-rounded result in both nearest-even and toward-zero modes.

Update tests/test_fma_mix_probe.py and docs/shader_validation.md to reflect
the RDNA2 hardware measurement, and add discriminating semantic tests in
tests/semantic/test_valu_fma_mix.py.

AI-assisted.
@Pierre-Kookis

Copy link
Copy Markdown

Three-way before/after with tools/fma_mix_probe.py --oracle hw,vulkan at 40e17da6. This PR makes the translator, both Vulkan drivers and the RDNA2 silicon agree.

Setup: Windows 11. AMD Radeon Ryzen 7 7800X3D iGPU (gfx1036, RDNA2), driver 32.0.21043.5001 (Vulkan driverInfo 26.3.1), used for both --oracle hw (tools/hw_oracle, amdhip64_6.dll, AMD clang 23.1.6) and Vulkan. RTX 4070 Ti SUPER, driver 32.0.16.1088, for Vulkan. vk_compute_test from main 89b047c.

side main 89b047cd this PR 40e17da6
translator double rounding single
Vulkan, AMD Windows driver (gfx1036) double single, 8/8
Vulkan, RTX 4070 Ti SUPER double single, 8/8
hardware, gfx1036 native single single
probe verdict sides disagree all sides follow single rounding

On main the discriminating cases F and G come out 0x3c00 and 0x3c02, the double-rounded words; with this PR they are 0x3c01, as on the silicon. The H control is 0x4100 everywhere.

Note on @esteban-dcp's Radeon 740M result (RADV, 7 of 8 on the Vulkan side): the AMD Windows driver gives 8 of 8 here on the same translation, so that word looks specific to RADV or that GPU, not to the translation.

GPU suite at 383c68bb: AMD 152/166 passed (14 skipped), NV 127/166 (39 skipped), NV emulated 158/166 (8 skipped), 0 FAIL. tests/semantic/test_valu_fma_mix.py: 9 passed.

(AI-assisted runs; synthetic probes, no game files.)

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.md
#	docs/shader_validation.md
@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 40e17da on AMD Radeon(TM) Graphics (Ryzen 9 9950X3D iGPU, RDNA2, subgroup 32..64), driver 26.10.1, and NVIDIA GeForce RTX 5080 (subgroup 32..32), driver 616.92, Windows 11 (26200): pass.

Detached worktree at the PR head (base 003e781); main for the red check is a second worktree at 003e781. Runtime built from the PR head (-j 4).

  • python -I tools/fma_mix_probe.py --oracle vulkan --build <build>: the translated compute shader on each GPU, LEONIDA_VK_DEVICE=radeon and =5080, one at a time. Only the translator and vulkan sides ran (no HIP/clang here, so no hw side).
    • PR head: both devices, "all sides follow single rounding"; cases A to G give the single-rounded words (A 0x3c01, B 0x3c01, C 0xbc01, D 0x0001, E 0x7bff, F 0x3c01, G 0x3c01), H the control 0x4100.
    • Red on main (003e781): both devices, "all sides follow double rounding"; A 0x3c00, B 0x3c02, C 0xbc00, D 0x0000, E 0x7c00, F 0x3c00, G 0x3c02, H 0x4100. So the Vulkan GPUs reproduce the rounding change on both vendors. This only shows that the driver runs the translator's sequence as written, not what RDNA2 does.
  • python -I scripts/run_gpu_tests.py --build <build> --work <dir>: Radeon 161/170 passed, 9 skipped, 0 failed; RTX 5080 130/170 passed, 40 skipped, 0 failed.
  • python -I -m unittest discover -s tests/semantic: 1568 tests OK. python tests/semantic/regen_fixtures.py --check: clean. pytest tests/test_fma_mix_probe.py: 18 passed, 10 subtests.
  • Diff skimmed: v_fma_mixlo_f16 / v_fma_mixhi_f16 now always use Gen.fma_mix_odd and drop the mode_r16 select. That is consistent with the probe and the docs rows.

Not run: the negative control and an RDNA2 hw side.

AI-assisted.

@Spaghetih

Copy link
Copy Markdown
Contributor

Tested 40e17da6 on NVIDIA GeForce RTX 4070 Ti, driver 621.24, Windows 11, MSVC (VS 2022), Vulkan SDK 1.4.363, llvm-mc 21.1.8 in WSL: pass.

No regression against main on the same machine (ctest 97/98 with the same d3d12_draw failure; GPU suite 127/166 passed, 39 skipped, 0 failed).

AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 773807f into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
… one still off

PROC-96: the Known approximations row still listed mix ops with the packs that
round to nearest because the mode says so. SHADER-135 (#1722) measured
v_fma_mixlo/mixhi_f16 on gfx1036: they round the exact x*y+z once in every mode,
7 of 7 discriminating tie inputs. The toward-zero path already does that; the
nearest-even path narrows an f32 Fma, which is 1 f16 ulp off on ties until
#1774 (SHADER-296) lands, so the row now says which of the two is the
approximation instead of implying both are correct by construction.

The f16 inline constants row needed no change: #1722 already replaced its
"Not measured" note with the measured per-operand rule.
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.

7 participants