Repository navigation
Translator: f16 rounding — integer f16 narrowing and single-rounded f16 FMAs (SHADER-55, SHADER-56) - #575
Conversation
…ADER-56, ItsNotRexed#513) Drivers fold PackHalf2x16 -> UnpackHalf2x16 to a no-op, so a following f16 op saw the unrounded f32 (ItsNotRexed#513: v_pk_add_f16 -> v_pk_mul_f16 1 ulp off under --wave64-emulate on NVIDIA; v_cvt_pkrtz_f16_f32 rounding to nearest on llvmpipe). Gen.f16_bits converts with integer ops (RTE, or RTZ with +-65504 for pkrtz) and is used by f16_out, v_cvt_f16_f32, v_pk_*_f16, f16 sin/cos, v_interp_p2_f16, v_fma_mix* and f16_rtz. tests/spirv_interp.py gains OpU{Less,Greater}ThanEqual and OpFUnordNotEqual.
v_fma_f16, v_fmac_f16 and v_pk_fma_f16 used GLSL Fma (rounded to f32) and then narrowed, a double rounding; an RX 6700 matches a correctly rounded f16 FMA in 12,612/12,612 cases (ItsNotRexed#374). fma_f16_odd: exact f32 product, TwoSum, round-to-odd at f32, then f16_bits. Design row for f16 rounding.
be5bd60 to
5c8cc4f
Compare
|
TEST-82 on an RTX 5090 (Blackwell), Windows 11 Pro 10.0.26300.9974, NVIDIA 621.24 / Vulkan 1.4.357, MSVC 14.44,
No game files were used. |
|
TEST-82: tested 5c8cc4f against the f16 fuzz mismatches, on two GPUs, Linux 7.3.5 (CachyOS): pass.
0 harness errors, 0 not translated. On For comparison, #551 (8ad2ed5) on the same runs: RADV 396/400 native and 388/400 emulated, NVIDIA 200/200 and 400/400. Its remaining RADV mismatches are mostly |
|
Tested AMD Radeon RX 9070 XT (RDNA4), driver 26.11.2 (Vulkan 2.0.416), Windows 11 Pro 10.0.26300, runtime built from
On |
|
nice, thx for running it on the windows driver :) |
|
Replying to @zneel. One detail for the requested rebase: the new main at AI-assisted. |
|
Tested
So on AMD this PR fixes the round-toward-zero
Both go away when the module declares
My reading, a hypothesis: without the execution mode the driver's compiler may assume there are no NaNs or −0. A NaN result is then undefined, and through the integer narrowing it reaches the store, which gets dropped. #494 mentions this execution mode but leaves it out; I've claimed adding it in #374. With it, this PR is clean on AMD. No game files. AI-assisted (Claude Code). |
# Conflicts: # docs/design.md # tests/spirv_interp.py # tools/rdna2spirv.py
|
Measured the two tips asked about above, same GPU and settings as my earlier comment (RX 9070 XT, AMD 26.11.2, Windows 11;
So the merged |
|
Tested
The two base mismatches are seed-1 programs 34 and 49: |
|
nice, thx for the ampere run :) |
|
TEST-82 supplemental check on the rebased The sole mismatch was This leaves one reproducible signed-zero difference on this backend and shows no newly mismatching programs from the PR. The requested RDNA2 and NVIDIA replays are separate. AI-assisted, synthetic inputs. |
|
TEST-82, RDNA2 part: tested 5c8cc4f on an AMD Radeon RX 6700 (gfx1031, RDNA2): pass. The f16 fuzz mismatches on main are gone, native and emulated.
No programs skipped, untranslated or with harness errors in any of the four runs. Setup: AMD Windows driver 26.11.2 (Vulkan 1.4.315), Windows 11 Pro 26300, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (scripts/build_llvm_mc.ps1), runtime built with scripts/build_runtime.ps1 from main b8f6c74, Python 3.15.2. Subgroup 32..64. Synthetic fuzz programs only, no game files. AI-assisted (Claude); the runs were on my GPU. |
|
Tested
The two mismatches before #551 are exactly No game files. AI-assisted (Claude Code): it ran the fuzzer and wrote this comment. |
|
Heads-up from the RTX 3070 runs: on current
The FMA-specific value tests here probably still cover behavior the fuzzer does not, so I am not saying the PR is redundant. Just flagging that this fuzzer seed no longer reproduces on main, in case another merge covered part of the same rounding path. No game files. |
|
Tested 6f4bd2a on RTX 5070 (Blackwell), NVIDIA 621.01, Windows 11 Pro (10.0.26200): pass. 0 mismatched, 0 untranslated, 0 harness errors in all 12 runs; native wave64 skipped (no 64-wide subgroups). Same as the Ada and Ampere runs above, now on Blackwell. No game files. AI-assisted (Claude): it wrote the test script and this entry; I ran it on this machine. |
|
Tested
Counts only. AI-assisted (Claude Code in OpenCode): it ran these on this machine. |
|
thx for the 4080 run :) |
|
f16 fuzz before and after this PR on an NVIDIA RTX 3060, Linux: no regression.
Native wave64 is skipped because NVIDIA has no 64-wide subgroup. There were no harness errors, and nothing failed to translate. Driver comparison: seed-1 programs 34 and 49 mismatch on the Windows NVIDIA drivers in #166, 610.88 and 621.24. They match on
AI-assisted (Claude Code). Only synthetic fuzzer programs were used; no game files. |
|
ty for the 3060 run ^^ |
|
TEST-82 on an NVIDIA RTX 4080 (Ada, AD103), driver 621.24, Windows 11 Pro 10.0.26200, Vulkan 1.4.357, subgroup 32..32. Each tree was built and run on its own (
The two base mismatches are Rebase note: the conflict against main is gone; AI-assisted (OpenCode); results checked by hand. No game files. |
|
Tested
Mismatch Details & ResolutionOn
Native wave64 tests skipped as expected (Turing compute subgroups are 32-wide). Zero untranslated shaders or harness errors across all runs. Additional Checks
Synthetic fuzz programs only, no game files. AI-assisted. |
|
Tested
Before #551 the two mismatches are |
|
AMD data point for the record (tested the merged head, 6f4bd2a). On an AMD Windows driver, main c432cdc did fail the f16 fuzzer, and #575 fixes it. Setup: Windows 11. AMD Radeon Graphics (Ryzen 7 7800X3D iGPU, RDNA2), driver 32.0.21043.5001, subgroup 32..64. RTX 4070 Ti SUPER, driver 610.88, subgroup 32. Test programs from main c432cdc (llvm-mingw clang), llvm-mc 23.1.6, SPIRV-Tools 2026.4.
On main, every AMD mismatch is the low half written by
(AI-assisted run, counts only.) |
… and -0 RDNA2 float ops return NaN, +-inf and -0 as computed. Without float controls, Vulkan lets the driver assume none of them occur, and AMD's compiler does. On an RX 9070 XT (26.11.2), x != x NaN checks fold to false and -0 products come back +0. With integer f16 narrowing (ItsNotRexed#575), NaN results are never stored. - rdna2spirv.py: every module declares the SignedZeroInfNanPreserve capability and OpExecutionMode SignedZeroInfNanPreserve 32. The index entry lists the property it needs (float_controls). --no-float-controls leaves both out. - vk_pipeline_check: reads float_controls and skips a shader on a device without shaderSignedZeroInfNanPreserveFloat32. - GPU test cs_signed_zero_nan (check.py, run_gpu_tests.py): without the mode, out[4..7] are 0 on AMD; with it, all eight words match. - tests/test_translate.py: covers the mode, the index entry and the opt-out. - docs/design.md: a "Float controls" contract row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and -0 RDNA2 float ops return NaN, +-inf and -0 as computed. Without float controls, Vulkan lets the driver assume none of them occur, and AMD's compiler does. On an RX 9070 XT (26.11.2), x != x NaN checks fold to false and -0 products come back +0. With integer f16 narrowing (ItsNotRexed#575), NaN results are never stored. - rdna2spirv.py: every module declares the SignedZeroInfNanPreserve capability and OpExecutionMode SignedZeroInfNanPreserve 32. The index entry lists the property it needs (float_controls). --no-float-controls leaves both out. - vk_pipeline_check: reads float_controls and skips a shader on a device without shaderSignedZeroInfNanPreserveFloat32. - GPU test cs_signed_zero_nan (check.py, run_gpu_tests.py): without the mode, out[4..7] are 0 on AMD; with it, all eight words match. - tests/test_translate.py: covers the mode, the index entry and the opt-out. - docs/design.md: a "Float controls" contract row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and -0 RDNA2 float ops return NaN, +-inf and -0 as computed. Without float controls, Vulkan lets the driver assume none of them occur, and AMD's compiler does. On an RX 9070 XT (26.11.2), x != x NaN checks fold to false and -0 products come back +0. With integer f16 narrowing (ItsNotRexed#575), NaN results are never stored. - rdna2spirv.py: every module declares the SignedZeroInfNanPreserve capability and OpExecutionMode SignedZeroInfNanPreserve 32. The index entry lists the property it needs (float_controls). --no-float-controls leaves both out. - vk_pipeline_check: reads float_controls and skips a shader on a device without shaderSignedZeroInfNanPreserveFloat32. - GPU test cs_signed_zero_nan (check.py, run_gpu_tests.py): without the mode, out[4..7] are 0 on AMD; with it, all eight words match. - tests/test_translate.py: covers the mode, the index entry and the opt-out. - docs/design.md: a "Float controls" contract row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and -0 RDNA2 float ops return NaN, +-inf and -0 as computed. Without float controls, Vulkan lets the driver assume none of them occur, and AMD's compiler does. On an RX 9070 XT (26.11.2), x != x NaN checks fold to false and -0 products come back +0. With integer f16 narrowing (ItsNotRexed#575), NaN results are never stored. - rdna2spirv.py: every module declares the SignedZeroInfNanPreserve capability and OpExecutionMode SignedZeroInfNanPreserve 32. The index entry lists the property it needs (float_controls). --no-float-controls leaves both out. - vk_pipeline_check: reads float_controls and skips a shader on a device without shaderSignedZeroInfNanPreserveFloat32. - GPU test cs_signed_zero_nan (check.py, run_gpu_tests.py): without the mode, out[4..7] are 0 on AMD; with it, all eight words match. - tests/test_translate.py: covers the mode, the index entry and the opt-out. - docs/design.md: a "Float controls" contract row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two parts, one per commit. Items: SHADER-55 and SHADER-56 (both claimed in #374). Closes #513.
(Part 1 was the mini-interpreter NaN fix for #509. I dropped it because #533 / TEST-67 already does that. The f16 tests here export zero-high-half words, so they don't depend on it.)
Part 1: f16 narrowing with integer ops (SHADER-56, #513)
Drivers fold
PackHalf2x16→UnpackHalf2x16to a no-op, so the next f16 op saw the unrounded f32 value:--wave64-emulate,v_pk_add_f16→v_pk_mul_f16came out 1 ulp low on every lane.v_cvt_pkrtz_f16_f32came out round-to-nearest on llvmpipe. Its RTZ correction compared the round trip with the input, and the round trip had been folded away.Gen.f16_bitsconverts using only integer ops. It rounds to nearest even, or toward zero with ±65504 for pkrtz. It keeps f16 denormals, gives ±0 for f32 denormal inputs, and turns NaN into a quiet NaN. It's used byf16_out(every f16 VOP1/2/3 result),v_pk_*_f16,v_cvt_f16_f32, f16 sin/cos,v_interp_p2_f16,v_fma_mix*andf16_rtz.Not touched: image/buffer d16 packing (#489's area), and
f16_roundtripfor 16-bit interpolants (#512'sOpQuantizeToF16).OpQuantizeToF16flushes f16 denormals, so I didn't use it for arithmetic.Test:
tests/semantic/test_f16_int_convert.pychecks RTE and RTZ against Python's binary16 on ties, the subnormal and overflow boundaries, ±0, ±inf and NaN, plus a seeded sweep over every f32 exponent from 2⁻²⁷ to 2¹⁸. It also asserts that the f16 ALU sites emit noPackHalf2x16.tests/spirv_interp.pygainsOpU{Less,Greater}ThanEqualandOpFUnordNotEqual.Part 2: f16 FMAs round once (SHADER-55)
v_fma_f16,v_fmac_f16andv_pk_fma_f16wereFma(rounded to f32) followed by narrowing, which is a double rounding. @BetterJdue's RX 6700 measurement in #374 shows the hardware is a correctly rounded f16 FMA (0 mismatches in 12,612 cases).fma_f16_odddoes it in four steps:x*yis exact in f32: 22 significant bits, and the result stays inside the f32 normal range.NoContraction.f16_bitsthen rounds that correctly to f16, because round-to-odd at 24 bits is innocuous for ≤ 22-bit targets.omod and clamp keep this valid, since they're exact power-of-two scaling and clamping to representable bounds.
v_fma_mix*with f32 sources still usesFma, and the design row says so.Test:
tests/semantic/test_f16_fma_rounding.pycompares against an exactFractionreference. It uses random triples plus triples built so thata*blands exactly on an f16 halfway point andcis below half an f32 ulp. f32-first rounding gets those wrong about half the time. Negative control: withmain'stools/, 107 subtests fail. All pass here.Tested
python -I -m unittest discover -s tests/semantic: all OK apart fromtest_alu_shift_b64's pre-existing sNaN case (spirv_mini_interp: OpBitcast and float outputs quiet signalling-NaN bit patterns (bit 22 flips) #509, fixed by tests/semantic: OpBitcast keeps signalling-NaN bits in the mini interpreter (TEST-67) #533), includingtest_mutationswith itsv_pk_fma_f16anchor updated.pytest tests --ignore=tests/semantic: 1760 passed. 3 failures (test_make_game_dry_runskip-assets, twotest_swizzle_searchpreview tests) also fail onmainon the same machine.run_gpu_tests.py: 44/71 passed with 27 skipped (wave64), and 70/71 withLEONIDA_W64_EMULATE=1. Identical tomain, no failures.0x3f40…, previously0x3f3f…).tools/valu_fuzz.py --family f16from Tools: f16 and buffer memory differential fuzzers on the GPU (FUZZ-2, FUZZ-5) #476 (seeds 11–13, 60 programs each, wave32 native + wave64 emulated) with no--skip-op, so the FMAs are included: 360/360 match. Onmain, the FMA ops had to be skipped and one emulated program still mismatched (--wave64-emulate: packed f16 result reaches the next op unrounded (1 ULP off) on NVIDIA #513).mainit had 8 mismatches, allv_cvt_pkrtz_f16_f32.Overlap: #494 adds a
fractparameter tof16_outand will conflict on a few lines. Whichever lands second rebases, and I'll do it if it's this one. After this merges, #476's README note about skipping the f16 FMAs can go.AI-assisted (Claude Code): it wrote the changes and tests, and ran everything above.