Skip to content

Translator: opt-in native f16 arithmetic, about 20x faster f16 ops (--native-f16, SHADER-673) - #4012

Open
tibzejoker wants to merge 1 commit into
ItsNotRexed:mainfrom
tibzejoker:translator/native-f16-r3
Open

tibzejoker wants to merge 1 commit into
ItsNotRexed:mainfrom
tibzejoker:translator/native-f16-r3

Conversation

@tibzejoker

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-673 (claim, with the correction on why OpFConvert narrowing doesn't work)

What and why

--native-f16 (off by default) translates f16 arithmetic to 16-bit float ops instead of f32 math plus integer narrowing:

  • v_add_f16, v_sub_f16, v_subrev_f16, v_mul_f16, v_pk_add_f16, v_pk_mul_f16 become one %half / %v2half op on operands bitcast from the packed word.
  • With --fma khr, v_fma_f16, v_fmac_f16 and v_pk_fma_f16 become OpFmaKHR on %half (correctly rounded, so one rounding as on RDNA2).
  • Source selection (op_sel, SDWA), neg and abs are applied to the bits.
  • Clamp, omod, the other f16 ops and every shader that writes MODE keep the integer narrowing.
  • The module declares RoundingModeRTE 16, DenormPreserve 16 and SignedZeroInfNanPreserve 16. The index lists shaderFloat16 (plus shaderFmaFloat16 for the fma) and the three 16-bit float-controls properties. vk_pipeline_check checks them, the runtime enables shaderFloat16 where the GPU has it, and make_game.py forwards the flag.

Why not narrow the f32 result with OpFConvert: NVIDIA folds the f32 -> f16 -> f32 round trip between two ops (as in #513). On an RTX 4090 single ops matched, but two-op chains were wrong in 8 to 23% of cases, and NoContraction, OpQuantizeToF16 and a volatile variable don't stop it. The design.md row says so.

Tested

Windows 11, RTX 4090 (621.01), on aa2988cc.

  • Exhaustive GPU comparison with the integer path: each native form and the integer form run in the same shader, through this translator, over 2^32 inputs each: add, sub, subrev, mul (all operand pairs), fma, fmac, pk_add, pk_mul, pk_fma, neg, abs, op_sel on sources and destination, packed op_sel/op_sel_hi/neg_lo/neg_hi, a literal, an inline constant, SDWA, and a seven-op chain. No difference, NaN payloads included. A clamp form stays on the integer path (also no difference), and a negative control (another op on one side) gives 4,026,592,227 differences.
  • Speed: a hand-written compute shader, 4.19M threads x 800 dependent f16 add/mul, ending in v_cvt_f32_f16: 4.15 ms with the integer path, 0.21 ms with --native-f16 (0.13 ms for the same chain in f32, 0.07 ms without the ops).
  • vk_pipeline_check creates the pipeline of a --native-f16 --fma khr module, and the runtime object with the shaderFloat16 change builds.
  • The default translation is byte-identical to aa2988cc on all 81 semantic fixtures.
  • python -m unittest discover -s tests/semantic: same result as aa2988cc (its 9 failures in test_loop_ballot_barrier, test_cmp_class_f64 and test_mutations fail the same way there), plus 6 new tests. pytest on the translate, f16, MODE, d16, FMA, make_game and docs test files: 772 passed, 598 skipped, 0 failed.
  • spirv-val: all modules, native and default.

Not run: AMD and Intel GPUs (an AMD run of the same comparison would decide whether it can become a default there), the GPU test suite, and the game's shader corpus.

AI-assisted (Claude Code).

Checklist

  • 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.

f16 add/sub/mul, scalar and packed, become 16-bit float ops (fma too with
--fma khr). Same results as the integer narrowing, NaN payloads aside, on
every operand pair on an RTX 4090; about 20x faster on an f16 add/mul chain.
Off by default; the runtime enables shaderFloat16 and vk_pipeline_check skips
a shader the device can't run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CaravelaLabs

CaravelaLabs commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Automated test: 392c83b1 merged onto main aa2988cc on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: GPU matched main; Python tests this PR touches had a failure.

  • scripts/run_gpu_tests.py: 192/209 passed, 15 skipped (plain main: 192/209 passed, 15 skipped).
  • Python tests this PR touches: 1 failed, 5 passed.
    • test_spirv_tools_accept_it: SPIRV-Tools v2025.1 spirv-val rejects OpCapability FMAKHR. The same check fails on main with this spirv-val. Not a GPU-suite change.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 392c83b on Windows, CPython 3.14.7, CPU only (no GPU, no game files). This computer has no SPIRV-Tools.

python -m pytest -q -rs tests/semantic/test_native_f16.py tests/semantic/test_alu_cvt_f16.py gives 29 tests passed, 102 subtests passed and 1 skipped. I used a virtual environment with tests/requirements.txt.

I found one defect in the feature list. The list requires FMA support for two float widths when the shader uses FMA at only one width.
The pipeline checker tests each feature in the list. Thus it can skip a shader on a device that supports the width that the shader uses.

Run this script from the repository root:

import sys, re
sys.path.insert(0, "tests/semantic")
import test_native_f16 as t
for ops in (["v_fma_f16 v1, v2, v3, v4"], ["v_add_f16_e32 v1, v2, v3", "v_fma_f32 v5, v2, v3, v4"]):
    asm, meta = t.translate(ops, fma_khr=True)
    print(re.findall(r"= OpFmaKHR (%\w+)", asm), [x for x in meta["features"] if "Fma" in x])

Actual result:

['%half'] ['shaderFmaFloat16', 'shaderFmaFloat32']
['%float'] ['shaderFmaFloat16', 'shaderFmaFloat32']

Expected result: the first shader requires shaderFmaFloat16 only. The second shader requires shaderFmaFloat32 only. A 16-bit add does not require the 16-bit FMA feature.

Possible correction: record the width of each FMA instruction during translation, and put only those widths in the list.

I did not run a GPU. AI-assisted.

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Radeon iGPU check at 392c83b1: this driver reports shaderFloat16 and all three required f16 float controls. Native and integer translations both matched 3/3 expected packed/scalar words with zero validation warnings or errors, including two rounding ties, a preserved subnormal and negative zero. Full GPU report and scope.

Two harness details affect repeatability on other devices:

  • vk_pipeline_check creates the module before the new float16 feature/control checks. For a device without shaderFloat16, that is already too late for a module declaring Float16. #3045 has a similar pre-module gate.
  • vk_compute_test currently creates its device without enabling shaderFloat16. The scratch runner checked the required properties, enabled the feature for the native variant, and then dispatched both modules. This keeps the ordinary harness from being mistaken for a native-f16 validation path. Vulkan float-control properties

AI-assisted (Codex).

@ofc0urse

Copy link
Copy Markdown

Tested 392c83b1 on NVIDIA GeForce RTX 4060 Ti (Ada), driver 621.24, Windows 11 Pro build 26300: pass, no change against main aa2988cc

Runtime and test programs built from each commit with scripts\build_runtime.ps1 (MSVC from VS Build Tools 2026, Vulkan SDK 1.4.363, llvm-mc 21.1.8).

GPU suite (scripts\run_gpu_tests.py), same results on both commits, test by test:

Run | main | this PR -- | -- | -- default | 164/210, 0 failed, 46 skipped | 164/210, 0 failed, 46 skipped LEONIDA_W64_EMULATE=1 | 195/210, 0 failed, 15 skipped | 195/210, 0 failed, 15 skipped

Python tests (CPython 3.13.17, tests/requirements.txt): pytest -q -rs tests/semantic/test_native_f16.py tests/semantic/test_alu_cvt_f16.py gives 30 passed, 102 subtests passed, 0 skipped. test_spirv_tools_accept_it passes here: the spirv-val from Vulkan SDK 1.4.363 accepts OpCapability FMAKHR, which SPIRV-Tools v2025.1 rejected in the RADV report above. So that failure looks like an old SPIRV-Tools, not this PR.

vk_pipeline_check on --native-f16 modules: I built four synthetic compute modules with test_native_f16.translate() (hand-written listings, no game data), assembled them with spirv-as, wrote their metadata to an index.json and ran this PR's vk_pipeline_check:

  • f16_default (integer path)
  • f16_native (add, sub, mul with neg/abs, packed add with op_sel/neg_hi, packed mul)
  • f16_native_fma (--fma khr: v_fma_f16, v_fmac_f16, v_pk_fma_f16)
  • f16_native_mixed (v_add_f16 plus v_fma_f32, --fma khr)

Result: 4 modules ok, 4 compute pipelines ok, 0 skipped. The driver reports shaderFloat16, shaderFmaFloat16 and the 16-bit RTE, denorm-preserve and NaN-preserve float controls, so nothing was skipped.

This only shows that the driver accepts the modules. I did not repeat your 2^32-input comparison, so the results themselves are checked only on your RTX 4090 (same Ada architecture).

f16_native_mixed also reproduces @shdann's finding: its features list shaderFmaFloat16 and shaderFmaFloat32, but the shader only has an f32 FMA.

AI-assisted (Claude Code).


@requiemsnkrp

Copy link
Copy Markdown

Tested 392c83b1 (merged onto main aa2988cc) on NVIDIA GeForce RTX 3060, driver 32.0.16.1714, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0: pass (CPU, translation and regression checks; I did not reproduce the GPU comparison over all 2^32 inputs).

  • tests/semantic/test_native_f16.py (llvm-mc 21, SPIR-V tools from the SDK): Ran 6 tests ... OK, including test_spirv_tools_accept_it, test_off_by_default_and_unchanged and test_integer_path_where_native_is_not_exact.
  • Full ctest (-E "present|swapchain|frame_pacer", LEONIDA_VK_VALIDATION=1, llvm-mc build, includes the rebuilt vk_pipeline_check and the runtime with shaderFloat16 enabled): 100% tests passed, 0 tests failed out of 148 (9 skipped/disabled, same set as main: shader_library_corpus, bink_decode, bink_play_headless, fsr_dispatch, bvh_builder, bink_audio, gameinput, d3d12_rgb9e5_rtv, d3d12_first_light). With the flag off by default, nothing regressed.

AI-assisted (Claude). No game files.

@gabsilvs

Copy link
Copy Markdown

Tested 392c83b (base main aa2988c) on AMD Radeon RX 6750 XT (RDNA2, AMD proprietary driver 26.11.2, Windows driver 32.0.21045.29003, Vulkan 1.4.315, shaderFloat16 and RTE/denorm-preserve float controls for 16-bit all true), Windows 11 Pro 10.0.26300, Ryzen 5 5600X, MSVC 14.44.35207, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 built with scripts/build_llvm_mc.ps1: pass for --native-f16; --fma khr does not work on this driver (see below, not caused by this PR).

  • Runtime rebuilt from the branch (scripts/build_runtime.ps1). python -m unittest discover -s tests/semantic -p test_native_f16.py: 6 tests OK.
  • python -I scripts/run_gpu_tests.py --build runtime/build, default translation: 195/210 passed, 15 skipped, 0 failed.
  • Same run with every fixture translated with --native-f16 (default --fma): 195/210 passed, 15 skipped, 0 failed. I confirmed with spirv-dis that 8 fixtures really contain OpFAdd/OpFSub/OpFMul %half in that mode (cs_signed_zero_nan, ps_f16_const, ps_f16_const2, ps_f16_vop2, ps_sdwa_dword_hi, ps_sdwa_f16_const, ps_valu16_subrev, ps_vop3_const_mods), and all 8 pass (ps_* 4096/4096 pixels exact).
  • To pass the flag I used a local, uncommitted 2-line edit of runtime/test/shaders/build.py (LEONIDA_NATIVE_F16=1 appends --native-f16); it is not part of this PR and was reverted. Maybe worth adding as a real env switch so testers can repeat this.
  • LEONIDA_FMA=khr (the --fma khr the PR needs for the f16 fma forms) on this driver: 166/210 passed, 17 skipped, 28 failed, mostly vkCreateGraphicsPipelines ... -> -13 (e.g. ps_cube_sample, ps_flat, ps_imgatomic, cs_w32_new_ops). The same 28 fail with --fma khr and NO --native-f16, so this is independent of the PR (I did not run it on main; vulkaninfo here shows no FMA extension). Consequence: v_fma_f16/v_fmac_f16/v_pk_fma_f16 on OpFmaKHR cannot be tested on RDNA2 + this Windows driver; only the add/sub/mul/pk forms were.
  • Not run: the PR's exhaustive 2^32 comparison (not in the PR), vk_pipeline_check on a --native-f16 --fma khr module, the full semantic suite.

No game files used. AI-assisted (Claude Code).

@bielfreedom

Copy link
Copy Markdown

Tested 392c83b1 (on its base aa2988cc) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, MSVC build of runtime/, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass, including with --native-f16 on. This adds an AMD run to the RTX 4090 numbers above.

run_gpu_tests.py has no switch for the flag, so I added the extra translator arguments to its command locally (not committed) and ran the suite three ways:

translator flags translated / spirv-val GPU tests modules with OpCapability Float16
default 214 / 214 196/210 passed, 14 skipped, 0 failures 0
--native-f16 214 / 214 196/210 passed, 14 skipped, 0 failures 8
--native-f16 + LEONIDA_FMA=khr 214 / 214 196/210 passed, 14 skipped, 0 failures 12 (85 OpFmaKHR)

So every fixture that takes the native half path gives the same words and pixels as the integer-narrowing path on this GPU, and validation is clean.

I didn't measure the speed-up; these are correctness runs.

AI-assisted (Claude Code). No game files.

@vibryxcx

Copy link
Copy Markdown

Tested 392c83b on AMD Radeon RX 6800 (RDNA2), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200): pass for the default translation (flag off).

  • Full run_gpu_tests.py: 195/210 passed, 15 skipped, 0 failed; 214 shaders translated, all pass spirv-val. The base aa2988c, built and run the same way, gives the identical list.
  • Full ctest -C RelWithDebInfo: 0 failed out of 153 on both the PR and its base, the same tests skipped (the LEONIDA_GAME_DIR ones, d3d12_first_light, vk_sync_validation with validation off, gameinput without SDL3).
  • Not covered: --native-f16 itself. run_gpu_tests.py has no switch for it, so this run only shows the default path is unchanged on AMD; the exhaustive native-vs-integer comparison you asked for on AMD still needs your comparison harness.

Built with scripts\build_runtime.ps1 (MSVC 14.44, VS 2022 17.14), Vulkan SDK 1.4.363, llvm-mc 23.1.6 (conda-forge), Python 3.12.10. Wave64 ran natively. No game files.

AI-assisted (Claude Code): it built both commits and ran the tests.

@Darkz0rz

Copy link
Copy Markdown

Tested 392c83b1 (base aa2988cc) with the exhaustive and fuzz comparisons the GPU suite doesn't cover, on an AMD Radeon iGPU (Ryzen 7 9800X3D, RDNA2 gfx1036), AMD 25.10.36 (32.0.21036.18), and the RTX 4090, 617.42, Windows 11 (build 26300). The 4090 is bit-exact everywhere, as you found. On this AMD driver --native-f16 isn't exact in three ways: a wave64 miscompile, unquieted signalling NaNs, and NaN choice when both operands are NaN. The RDNA2 and RDNA4 suite runs above pass because no fixture hits these cases.

1. Wrong packed result in a wave64 shader (AMD only)

tools/valu_fuzz.py --family f16 program 1:404:64 mismatches only with --native-f16, and deterministically. It minimizes to six instructions:

v_mov_b32_e32 v20, 0xc5e24200
v_mov_b32_e32 v22, 0x872e6479
v_mov_b32_e32 v24, 0x3c00d774
v_mov_b32_e32 v28, 0x3f32cba8
v_mov_b32_e32 v29, 0xabed671b
v_mov_b32_e32 v34, 0x4109b6ae
v_and_b32_e32 v7, 7, v0
v_or_b32_e32 v20, 0x3c00, v7
v_lshlrev_b16_e64 v25, v29, v20 op_sel:[0,1,0]
v_sub_f16_e32 v25, v22, v24
v_pack_b32_f16 v33, v28, v25 op_sel:[0,1]
v_pk_mul_f16 v26, v34, v33 op_sel:[0,1] op_sel_hi:[0,0]

v33 is 0x0000cba8, the same in every build. The right v26 is 0x46658000 (low half: 0xb6ae * 0x0000 = -0; high half: 0xb6ae * 0xcba8).

module AMD iGPU RTX 4090
integer path, wave32 / wave64 0x46658000 / 0x46658000 0x46658000 / (no wave64)
--native-f16, wave32 0x46658000 0x46658000
--native-f16, wave64 module, run at wave64 0x46654665 (no wave64)
--native-f16, the same wave64 module, run at subgroup 32 0x46654665 0x46658000
--native-f16 --wave64-emulate n/a 0x46658000

The low half takes v33's low half instead of its high half. Both native modules pass spirv-val, and their f16 instructions are identical once ids are normalised (OpFSub %half, OpCompositeConstruct, OpFMul %v2half). They differ only in the exec-mask setup. The same wave64 module is right on the 4090 and wrong on AMD even at subgroup 32. So this looks like an AMD driver compiler bug rather than the translator, but a game shader in wave64 would get the wrong value either way. The instruction alone, without the preceding ops, is correct in all four builds.

2. Signalling NaN inputs come back unquieted (AMD only)

This is an exhaustive comparison (my own harness, not in the PR). One wave32 kernel runs 65,536 threads with a = thread id and loops b over all 65,536 values. Each op's 32-bit result is folded into two per-thread accumulators (xor, and rotl1 + add). The same kernel is translated with and without --native-f16 and run through vk_compute_test. That's 2^32 pairs for each of 15 forms:

  • v_add/sub/subrev/mul_f16_e64 with an SGPR b
  • -a + |b| and |a| * -b
  • v_add_f16_e32, a literal multiply, an inline constant
  • v_pk_add/mul_f16, v_pk_add_f16 op_sel:[1,0] op_sel_hi:[0,1] neg_lo:[1,0] neg_hi:[0,1], v_pk_mul_f16 op_sel:[1,1] op_sel_hi:[0,0]
  • v_add_f16_sdwa into WORD_1 with UNUSED_PRESERVE
  • a five-op add/mul/sub chain

There's no fma, because this iGPU has no VK_KHR_shader_fma.

  • RTX 4090: all 15 forms match over 2^32 pairs.
  • AMD: with b = 1.0 and a over all 65,536 values, exactly the 1,022 sNaN values of a differ, in all 15 forms. Native returns the input bits, for example v_add_f16 0x7c01, 1.0 gives 0x7c01 and v_pk_add_f16 gives 0x7c017c01. The integer path, and the 4090 natively, give 0x7e01 / 0x7e017e01.
  • A negative control (the native side computes sub where the other has add) is caught on both GPUs.

3. Two NaN operands (AMD only)

With every NaN input quieted first (the same integer instructions in both modules), 13 of 15 forms match over 2^32 pairs on AMD. The two that differ are -a + |b| and the neg_lo/neg_hi packed add, and only where both operands are NaN. Packed high half a + (-a) with a = 0x7e01: the integer path gives 0xfe01, native gives 0x7e01. Low half b + (-b) with b = 0x7e05: the integer path gives 0x7e05, native gives 0xfe05. So which operand's NaN comes out isn't stable on AMD. Every non-NaN input matched, rounding and signed zeros included.

Other results

  • Fuzz (2,000 programs, seed 1, wave32 + wave64, with the fuzzer's ISA model as the oracle; the flag passed through a one-line local hook): RTX 4090: wave32 2,000/2,000 match; wave64 with --wave64-emulate 2,000/2,000 match. AMD iGPU: 3,993/3,999 match. One program didn't run because the device was briefly lost while my exhaustive run shared the iGPU. Five of the six mismatches (1:378:64, 1:534:32, 1:1146:32, 1:1540:32, 1:1556:32, all on v_pk_ashrrev_i16 / v_mul_lo_u16 with op_sel) fail the same way without --native-f16, so they're on main and not from this PR. The sixth is 1:404:64 above.
  • vk_pipeline_check (this PR's), on 20 fuzz modules. AMD: 20/20 pipelines with --native-f16; with --fma khr added, 7 ok and 13 skipped (no VK_KHR_shader_fma (shaderFmaFloat16)). 4090: 20/20 both ways. The AMD iGPU has shaderFloat16 and the 16-bit RTE / denorm-preserve / NaN-preserve controls, and both independence properties are 32_BIT_ONLY (the 4090 has ALL).

So, to the description's question of whether it could become a default on AMD: not on this driver as it stands. (1) is a wrong value, which design.md's contract table should record if the flag stays. (2) and (3) only matter if NaN bits matter, which depends on the console's IEEE mode (SHADER-190).

Local test changes, not in the PR:

  • vk_compute_test doesn't enable shaderFloat16, so a --native-f16 module runs there without the feature. I enabled it when the module declares Float16, as the runtime does with this PR. It might be worth doing in the PR, since the fuzzers and the GPU suite run through vk_compute_test.
  • valu_fuzz.py has no way to pass translator flags.

Tools: Vulkan SDK 1.4.363.0, llvm-mc 21.1.8, MSVC from VS 2026 (18.11), Ninja RelWithDebInfo, VK_LOADER_LAYERS_DISABLE=~implicit~. No game files.

AI-assisted: the harness, setup and runs were done with Claude Code.

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.

10 participants