Repository navigation
Shaders: f16 arithmetic in native half on hosts with 16-bit operations (37x on an RTX 5070 Ti; for #2384) - #2825
0xkn0wh3r3 wants to merge 5 commits into
Conversation
|
Verified and benchmarked
|
|
Results on AMD hardware, the adapter the PR lists as unverified. Setup: AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32015.7029, Intel Xeon E5-2670 v3 (times are GPU timestamps, so the CPU is not in them), Windows 11 IoT Enterprise LTSC 26100, Tests: all pass on the hardware adapter and on WARP.
Benchmark (
The native path is faster in every run, but far less so than the 39×, 18× and 21× on the RTX 5070 Ti, and it varies from run to run. On this same card, #2806's hand-written
AI-assisted (Claude Code). The tests and benchmark ran on the machine above. |
kotekompotek
left a comment
There was a problem hiding this comment.
Scoped review — the part that had to be right is right: the shader-cache build key folds options().native16 in, so an f16-native translation can never be served to a non-native host (and the un-initialized-host -> native16=false direction fails safe). options() centralizing view_marks+native16 prevents drift between the cached and uncached translate paths. The rest — bitcode f16 type, host probe, the +200 lines of f16 lowering — is a big surface; the DXIL-level tests plus the claimed 39x on the 5070 Ti suggest it's exercised, but a second pair of eyes on the rounding/saturation semantics of the packed-f16 ops would be worthwhile.
…rvisak#2821 together Native f16 and view-heap paging (Marvisak#2121) are both host translation options: both kept in emit::Options, both in the shader cache key, and the test harness's dispatch takes both its PC-view and timing arguments. Marvisak#2821's lock-free check in HostHeap::mirror passes by a slot made from the same record; Marvisak#2121 takes pages back and places them again keeping that record, so a placed-again page was never remade. Taking a page back and noting it for a remake now clear the check (view_heap_pages_taken_back_are_placed_again_when_read failed without).
The translator did every console f16 operation in f32 and rounded each result to f16 in integer code (`narrow16`, about 40 operations), which costs about 34x the host's own half math (Marvisak#2806). With the new `emit::Options::native16`, `v_add/sub/subrev/mul_f16`, `v_fma/fmac_f16`, `v_min/max/min3/max3/med3_f16` and `v_pk_add/mul/fma/fmac/min/max_f16` become DXIL `half` arithmetic: 16-bit types, the native low precision shader flags, the 16-bit data layout and SFI0's native 16-bit bit, set only when a live instruction uses them. The option is for hosts that report `Native16BitShaderOpsSupported` (D3D12 OPTIONS4; SM 6.6 is required anyway) and round a 16-bit FMad once, as the console's v_fma_f16 does (`Host::rounds_f16_fmad_once`, a dispatch of Marvisak#2384's case, run once as `fuses_fmad` is). The RTX 5070 Ti does. WARP rounds in f32 first and keeps the f32 path, which since Marvisak#1387 rounds v_fma_f16 once itself. Results: sums, differences and products are the same as the f32 path's (rounding in f32 first never changes the f16 result), as are minima, maxima and medians (exact; the -0/+0 order and the clamp, which gives +0 for NaN and -0, are done on the bits). NaN payloads are the host's where the f32 path quiets them. Fused multiply-adds round once; the f32 path still rounds the packed ones twice. Programs that round toward zero (the title's around its 16-bit interpolation) or toward an infinity keep the f32 path, as do conversions, interpolation, v_mad/mac_f16, v_fma_mix* and the transcendental and rounding operations. victory-gpu asks the host once (`host::native16`, off with `VICTORY_F16_IN_F32=1`) and puts the answer in `shader_cache::options()` and the cache key, so one path's entries never serve the other. Tests (RTX 5070 Ti, driver 621.24; WARP is refused and skips them): the 16-bit probes natively; the fused inputs the f32 path rounds twice, Marvisak#2384's three cases and 8,192 random finite triples (denormals included) rounded once natively; every 16-bit probe and the random triples lowered both ways, native never further from the model (they differ on 1 input, a packed one); the toward-zero mode natively; the host check (WARP refused); flags and validation of the lowered modules (dxil.dll 1.6). A translated benchmark (ignored; 262,144 threads, dependent chains of 512 instructions, best of five, GPU timestamps): v_pk_fma_f16 f32 path 0.221 ms native 0.006 ms 37x v_fma_f16 f32 path 0.129 ms native 0.006 ms 23x v_mul_f16 + v_add_f16 f32 path 0.221 ms native 0.011 ms 21x
bdc08a9 to
d1c39bc
Compare
|
Follow-up to my AMD results above, with the ISA. The two possible reasons I gave there were both wrong. Same RX 9070 XT ( 1. The native path picks the same instructions as hand-written HLSL. Each native
2. The chains are half as long as the test assumes, which made the AMD timings mostly fixed cost.
The same benchmark with
Results were alike in all 262,144 threads in every run. Counting instructions rather than words in 3. A possible follow-up, not for this PR: the merge after scalar f16 results. Each native scalar result is merged into its 32-bit register to keep the other half, as the console does:
The packed chains need no merge. Where the other half of the destination is never read before it is overwritten, skipping the merge would take these chains from ~3 instructions per op to 1, which probably explains the lower scalar speed-ups. AI-assisted (Claude Code). The benchmark runs and ISA dumps were made on the machine above, with a local, unpushed hook that wrote the modules and root signatures to disk. |
AI-assisted testing (Gemini in OpenCode); measured locally on my own machine. First real-game RTX 4080 verification (ported onto
|
Metric (framelog usual frame / cardmeter) |
fxceless f32 (legacyF32ToF16) |
Native DXIL half (#2825 port, warm) |
|---|---|---|
framelog usual frame |
113 ms (41 frames) |
107 ms (36 frames, -6 ms) |
| Peak world FPS | 9.30 fps |
9.70 fps |
framelog all other lists (GPU) |
38.8 ms (201 lists) |
34.1 ms (176 lists) |
Total GPU time (send-and-hash + compare + lists) |
47.9 ms |
41.9 ms (23.9 fps GPU ceiling; card 43% busy) |
cardmeter 256-frame mean |
40.8 ms (210 lists) |
40.9 ms (211 lists) |
Native half f16 works cleanly end-to-end on real game shaders on Ada Lovelace / Windows 10 + Agility 1.620.2, with zero pipeline refusals and lower settled frame time even against a legacyF32ToF16 baseline.
lower.rs went past the repository's 512 KiB limit for a source file with the native f16 lowering (CI's policy check failed). Its helpers move, unchanged, into lower/half.rs, next to lower/bvh.rs.
`chain` repeated a step CHAIN / its word count times, and VOP3 and VOP3P instructions take two words: the v_pk_fma_f16 and v_fma_f16 chains held 256 instructions where 512 were counted, so their f16 operations per second read twice too high. Steps now give their instruction count. The chains are 4,096 instructions long: at 512 a dispatch's fixed cost hid the arithmetic on an RX 9070 XT. On an RTX 5070 Ti the native path is 53.5x (v_pk_fma_f16), 30.9x (v_fma_f16) and 25.9x (v_mul_f16 + v_add_f16) faster, results alike in all 262,144 threads.
|
@xndbogdan thank you, both findings were right. aef37bb makes Interesting that both the native path and dxc's CI was also failing on the policy check: |
|
@0xkn0wh3r3 thanks for the quick turnaround, and for the credit. 53.5× on the corrected chains is a great result. Good luck getting this merged. |
|
AI-assisted (Claude Opus 5.5 in OpenCode), measured on my own machine. This replaces two overlapping AI-written reports I posted here earlier; on re-auditing the logs, some of their numbers were single samples or misattributed, and one said there were no visual artifacts, which was wrong. Setup: RTX 4080 16 GB, Windows 10 22H2, Series X build, diagnostics release, prison-cell scene (~4,800 draws/frame). Three single 360 s runs, so this is a bundle comparison, not an isolated A/B:
For this PR: the earlier report here (22:42) gave 38.8 → 34.1 ms GPU lists and 113 → 107 ms per frame. Those were the last single sample of each run. The medians are 40.0 → 38.1 ms GPU lists, |
|
Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head Tested
Not tested
AI use
|
With Marvisak#2121: both options (view_pages, native16) in emit::Options and in the cache key. lower.rs's tests move to lower/tests/mod.rs (unchanged), which keeps lower.rs under the asset policy's 512 KiB.
|
Integration finding for #2915: with its production I tried a local change allowing an instruction with
The other cases covered ADD ×2, ×4, ×0.5, ×4 with clamp, and a single shader containing ordinary MUL followed by modified ADD. That mixed shader returned Windows 26300.10075, driver 621.40, synthetic RDNA2 → DXIL → D3D12 readbacks. Tested with Codex assistance. |
|
Automated triage for the maintainers. Re-reviewed at
The earlier assessment stands. The native Verdict: merge candidate (a recommendation to look, not an approval). CI: passing. Generated by Claude Code |
…ively `valu16_native` claimed v_add/v_mul_f16 and the rest whatever their output modifier and refused one, so with native 16-bit operations a program the f32 path translates (once it handles output modifiers on f16 results, Marvisak#2915) would be refused. An instruction with `omod` now falls through to the f32 path, per instruction; a test checks both lowerings translate or refuse v_add/v_mul_f16 with mul:2, mul:4 and div:2 alike, and that a translated one declares no native 16-bit operations. Found by steven-seagull27 combining Marvisak#2825 with Marvisak#2915.
|
@steven-seagull27 thanks, that's exactly the integration case. Fixed in dbc004b: The new test |
…ively `valu16_native` claimed v_add/v_mul_f16 and the rest whatever their output modifier and refused one, so with native 16-bit operations a program the f32 path translates (once it handles output modifiers on f16 results, Marvisak#2915) would be refused. An instruction with `omod` now falls through to the f32 path, per instruction; a test checks both lowerings translate or refuse v_add/v_mul_f16 with mul:2, mul:4 and div:2 alike, and that a translated one declares no native 16-bit operations. Found by steven-seagull27 combining Marvisak#2825 with Marvisak#2915.
dbc004b to
47078d7
Compare
|
Confirmed on current head |
With Marvisak#2948's root UAVs the cache entry is VSH3, and the new test passes native16 (Marvisak#2825).
Dukestar69
left a comment
There was a problem hiding this comment.
Verified on RTX 5090 against origin/main (63bcca4):
cargo test -p victory-shader --lib(all 8 new native-f16 tests): passed on RTX 5090 (sixteen_bit_floats_match_the_hardware_natively,fused_f16_results_round_once_natively,f16_results_round_as_the_mode_says_natively,native_f16_is_never_further_from_the_console,output_modifiers_on_f16_results_take_the_f32_path_natively,native_16bit_operations_are_declared_where_used,hosts_that_round_f16_fmad_twice_keep_the_f32_path,the_f16_fma_cases_round_once_in_the_model).cargo test -p victory-gpu --lib shader_cache:: -- --test-threads=1: 4/4 passed.cargo clippy -p victory-shader -p victory-gpu -p victory --all-targets -- -D warnings: clean.cargo xtask policy: 192 files ok.
Cleanly enables native 16-bit floating-point arithmetic on hosts that support it and pass the single-rounding FMA probe, with proper shader cache keying.
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal
left a comment
There was a problem hiding this comment.
Audited the DXIL encoding, native half lowering (lower/half.rs and packed16), rounding-mode / omod gating, rounds_f16_fmad_once probe, and shader_cache key isolation, and verified on Windows 11 with an RTX 5090, WARP, and the Windows 11 SDK validator (10.0.26100.0\x64\dxil.dll).
Verification (origin/main 63bcca43 merge-tree clean)
cargo clippy --workspace --all-targets --locked -- -D warnings(default &--all-features): clean;cargo xtask policy: 191 files ok.victory-shaderwith SDKdxil.dllvalidation: all 9 f16 / native16 tests passed (hosts_that_round_f16_fmad_twice_keep_the_f32_path,sixteen_bit_floats_match_the_hardware,sixteen_bit_floats_match_the_hardware_natively,the_f16_fma_cases_round_once_in_the_model,fused_f16_results_round_once_natively,f16_results_round_as_the_mode_says_natively,output_modifiers_on_f16_results_take_the_f32_path_natively,native_16bit_operations_are_declared_where_used,native_f16_is_never_further_from_the_console):- RTX 5090:
rounds_f16_fmad_once() == Ok(true),native16() == true; 0 model mismatches across all 23 probes, the#2384cases, and 8,192 random finite/denormal FMA triples;native_f16_is_never_further_from_the_consolediffered on 1 packed FMA input (toward the model). - WARP:
rounds_f16_fmad_once() == Ok(false),native16() == false(keeps f32 path). emit::bitcode::+emit::lower::tests::: 45/45 passed.
- RTX 5090:
- RTX 5090 release benchmark (
translated_f16_math_costs, 4,096-inst chains, 262,144 threads, best of 5 × 10 dispatches):v_pk_fma_f16:2.284 ms(940 Gf16 ops/s) →0.036 ms(60,404 Gf16 ops/s) — 64.3x (262,144/262,144 identical)v_fma_f16:1.297 ms(828 G) →0.035 ms(30,402 G) — 36.7x (262,144/262,144 identical)v_mul_f16 + v_add_f16:1.017 ms(1,056 G) →0.035 ms(30,632 G) — 29.0x (262,144/262,144 identical)
victory-gpu:shader_cache::tests::4/4 passed on RTX 5090 default, RTX 5090VICTORY_F16_IN_F32=1(short-circuits the probe), RTX 5090VICTORY_D3D12_DEBUG=1, and WARPVICTORY_D3D12_DEBUG=1;draw::tests::20/20 passed on RTX 5090.
Minor non-blocking note
Inspecting ID3D12InfoQueue under the D3D12 Debug Layer shows Host::fmad_probe (crates/victory-shader/src/emit/host.rs:694, shared with the pre-existing fuses_fmad check) binds only the CBV/SRV/UAV descriptor heap before SetComputeRootSignature(&root), while root_signature_for sets D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED, logging D3D12_MESSAGE_ID(1419) (ERROR) once per probe call on both RTX 5090 and WARP (2 messages total now: once from fuses_fmad, once from rounds_f16_fmad_once). Pre-existing on main and harmless for the probe dispatch, though binding a 1-slot shader-visible sampler heap alongside views in fmad_probe would keep the debug queue completely clean (fine as a follow-up).
|
First Ampere (RTX 30) result, since the native path is gated on the card rounding a 16-bit Setup: NVIDIA GeForce RTX 3060 12 GB (Ampere, GA106), driver 610.88, Ryzen 7 5700G, Windows 11 Pro 26200, Rust 1.98.1, Capability and correctness (the
Results alike in 262,144 of 262,144 threads in every run. The first run's native Full Not tested: a title (only synthetic chains and the PR's tests), other Ampere cards. AI use: Claude Code ran the builds, tests and benchmark on this machine and drafted this comment. |
For #2384, following the measurement in #2806.
The translator does every console f16 operation in f32 and rounds each result to f16 in integer code (
Lower::narrow16, about 40 operations). #2806 measured that at 34x the host's ownhalf2math on an RTX 5070 Ti. This PR adds a native path behind a host capability check.What changes
emit::Options::native16(new). When it is set and the program rounds 16-bit results to nearest even (mode16isSome(0), orNone, whichnarrow16also rounds to nearest even), these become DXILhalfarithmetic:v_add/sub/subrev/mul_f16,v_fma/fmac_f16,v_min/max/min3/max3/med3_f16andv_pk_add/mul/fma/fmac/min/max_f16. Sources are read as bits: the abs/neg modifiers flip the sign bit, thentruncandbitcastgive ahalf. Results are bitcast back and written into the destination half as before. The -0/+0 order of min/max and the clamp (+0 for NaN and for -0, assemantics_testsmodels the console) are decided on the bits, not left to the host.mode16 == Some(3), which the title sets around its 16-bit interpolation) or toward an infinity; conversions (v_cvt_*, where the host's own conversion rounds as it likes); interpolation;v_mad/mac_f16(whether the console rounds the product is not settled; Shaders: lower v_mad_f16 and v_mac_f16 with unfused binary16 product rounding #1598);v_fma_mix*; and the transcendental and rounding operations.halftype in the bitcode writer, andUses::native16for any livehalfori16value. When it is set, the module gets the 16-bit data layout, the shader flagsLowPrecisionPresent | UseNativeLowPrecision, and SFI0's native 16-bit ops bit. A module without live 16-bit values is unchanged.host::native16()asks once. It requires three things: the device reportsNative16BitShaderOpsSupported(D3D12 OPTIONS4; SM 6.6 is required anyway); the device rounds a 16-bitFMadonce (Host::rounds_f16_fmad_oncedispatches Performance ideas from two other Rust game ports: native 16-bit shader math, and an LTO build profile #2384's case3c01·3e00+8001and expects3e01, run once at startup likefuses_fmad); andVICTORY_F16_IN_F32=1is not set. WARP has the ops but rounds theFMadtwice, so it keeps the f32 path. Since Round v_fma_f16 once in DXIL lowering #1387, that path roundsv_fma_f16once itself. The answer goes into a newshader_cache::options()and into the cache key, so entries from one path are never served to the other (keytest). The check isvictory_shader::emit::host::Host::native16, shared with the tests, the same wayfuses_fmadis shared.victory translate's tally keeps the f32 path.--cachegoes throughhost_shaderand translates as the host does.emit/mod.rs.How the native results differ from the f32 path's
halfFMad, thennarrow16: rounded twice, 1 f16 ULP off where the f32 value lands on an f16 tie that the exact value is not onv_pk_fma_f16does0x7c01through, WARP gives0x7fff)So where the option is on, the native path is never further from the console than the f32 path, and closer for packed FMAs. Hosts whose 16-bit
FMadrounds twice (WARP) don't get the option.Tests (RTX 5070 Ti, driver 621.24, Windows 11)
All in
emit/semantics_tests.rs. The native tests run only on adapters that passHost::native16(): the hardware adapter here. WARP is refused and skips them.hosts_that_round_f16_fmad_twice_keep_the_f32_path: WARP fails the 16-bitFMadcheck and is refused. The RTX 5070 Ti passes.sixteen_bit_floats_match_the_hardware_natively: every 16-bit probe lowered natively, against the model. The existing f32-path tests still run unchanged on both adapters.fused_f16_results_round_once_natively(hardware adapter only): the probe inputs the f32 path rounds twice (F16_DOUBLE), Performance ideas from two other Rust game ports: native 16-bit shader math, and an LTO build profile #2384's three cases (3c01·3e00+8001 → 3e01,3c03·3e00+0001 → 3e05,3c01·3e00+be00 → 1600) throughv_fma_f16,v_fmac_f16andv_pk_fma_f16, and 8,192 random finite triples (denormals included). All match single rounding.native_f16_is_never_further_from_the_console: every 16-bit probe plus the 8,192 random FMAs, lowered both ways. Wherever the two results differ, the native one must be the model's. They differ on 1 input (a packed FMA), toward the model.f16_results_round_as_the_mode_says_natively: under the toward-zero mode the sum stays on the f32 path, and after going back to nearest even it is native.native_16bit_operations_are_declared_where_used: the SFI0 bit is set exactly for native arithmetic (not for conversions or toward-zero programs), and the module passes the validator.the_f16_fma_cases_round_once_in_the_model: CPU check of Performance ideas from two other Rust game ports: native 16-bit shader math, and an LTO build profile #2384's cases.The validator was dxil.dll 1.6 via
VICTORY_DXIL, because this machine's SDK has no x64 dxil.dll. As a control, the validator rejects the native modules if the 16-bit flags are dropped ("Bitcast on minprecison types is not allowed").Benchmark
translated_f16_math_costs(ignored;cargo test -p victory-shader --release --lib translated_f16_math -- --ignored --nocapture). It times translated console programs: dependent chains of 4,096 instructions, 262,144 threads, GPU timestamps around 10 back-to-back dispatches, best of five. Before is the f32 path, after is native:v_pk_fma_f16v_fma_f16(f32 path with #1387)v_mul_f16+v_add_f16With operands near 1, the two paths' final results were the same in every thread. Earlier figures here (37x, 23x and 21x, with 512-word chains) understated the work:
chaincounted encoding words, so the two VOP3/VOP3P chains held 256 instructions, and at that length a dispatch's fixed cost hid the arithmetic. Thanks to xndbogdan's RX 9070 XT ISA check for finding it.Other checks
cargo test -p victory-shader(rebased on f3578e3): 305 passed, 3 failed. The 3 failures and 1 crash (skipped) also happen on origin/main 13f91ca on this machine:heap_direct_texture_reads_mark_their_heap_indexcrashes with 0xc0000005, anda_heap_buffer_read_where_no_buffer_view_is_reads_zeros,a_heap_buffer_read_by_element_takes_the_views_strideandgathers_at_a_level_match_the_hardwarefail.cargo test -p victory-gpu: 226 passed, 2 failed. These are the two that already fail on main on this NVIDIA card:dynamic_slots_are_held_until_releasedandlists_share_a_table_and_copies_keep_their_views.cargo clippy --workspace --all-targets --locked -- -D warnings, with and without--all-features: clean.Not verified
halfFMA is expected to becomev_fma_f16and pass the check, but that is not measured. The startup check tests one case only. A host that rounds that case once but other cases differently would get the option wrongly.v_add_f16etc. are not known. The native path takes the host's payloads.VICTORY_F16_IN_F32=1turns it off.AI-assisted.
Since review
lower.rswent past the repository's 512 KiB source-file limit, which CI's policy check refused. The native-halfhelpers moved, unchanged, intolower/half.rs(905732a).