Repository navigation
Shaders: narrow f32 to f16 to nearest even, as the console does - #422
LouisRiverstone wants to merge 1 commit into
Conversation
towiice
left a comment
There was a problem hiding this comment.
Tested PR head 2661581 on Windows 11 x64 build 10.0.26300 (26H2), NVIDIA GeForce GTX 1650 SUPER (driver 610.88), Rust 1.100.0 (stable-x86_64-pc-windows-gnullvm):
cargo test -p victory-shader -- --test-threads=1: 70 passed, 0 failed (53.59 s).
All victory-shader tests (including f32-to-f16 round-to-nearest-even) pass cleanly.
…sole does Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
RDNA2 hardware measurement of the conversion: Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs
One mode note, as for the other MODE-dependent lowerings: with FP16 denormals flushed, every subnormal result in the set (5 distinct inputs) becomes AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run. |
|
End-to-end evidence for this PR from a differential check of the translator against RDNA2 hardware: every f16-narrowing difference I found goes away with it. How. 121 of
Packed and mixed ops too (second run, VOP3P words, 240 inputs, same hosts):
In the first run, 9 opcodes go from differing to matching the hardware on every row, and none gets worse: fully consistent opcodes go from 97 to 106 of 121. The rest of the remaining differences belong to other PRs: #605 ( AI use: Claude Code (Claude Opus 5.5) built the check, ran it on the GPUs above and drafted this comment; the numbers come from those runs. |
Typed-format stores whose format is in the instruction (MTBUF) were refused by name. They now lower as raw stores of the element's dwords when no conversion is involved: formats of 32-bit channels (uint, sint, float; 32 to 32_32_32_32) store the data's bits, and the _d16_ forms of the 16-bit float formats (16_16, 16_16_16_16) store their halves as they are. The instruction must supply every channel of the format, as the typed loads' layout gives it. Everything else stays refused with a reason: norm, scaled and narrow integer formats (conversions), elements under a dword (a read-modify-write lanes would race on), and f32 data for half formats, because DXIL's f32-to-f16 op rounds toward zero (measured below) where the console rounds to nearest even; Marvisak#422 addresses that. GPU tests store each lowered layout on the host GPU and WARP and read the dwords back; another test checks the refusals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Typed-format stores whose format is in the instruction (MTBUF) were refused by name. They now lower as raw stores of the element's dwords when no conversion is involved: formats of 32-bit channels (uint, sint, float; 32 to 32_32_32_32) store the data's bits, and the _d16_ forms of the 16-bit float formats (16_16, 16_16_16_16) store their halves as they are. The instruction must supply every channel of the format, as the typed loads' layout gives it. Everything else stays refused with a reason: norm, scaled and narrow integer formats (conversions), elements under a dword (a read-modify-write lanes would race on), and f32 data for half formats, because DXIL's f32-to-f16 op rounds toward zero (measured below) where the console rounds to nearest even; Marvisak#422 addresses that. GPU tests store each lowered layout on the host GPU and WARP and read the dwords back; another test checks the refusals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of
|
The host's f32-to-f16 conversion (DXIL op 130) rounds toward zero, on NVIDIA and WARP alike: a tie went down, a finite value past 65504 became 65504 instead of infinity, and subnormal results were truncated. Every f16 result went through it (f16 ALU ops, v_cvt_f16_f32, v_fma_mix, packed f16 ops, f16 interpolants, d16 loads), all of which the console rounds to nearest even. f16_nearest does it exactly on the bits; f16_toward_zero (v_cvt_pkrtz_f16_f32) now steps back from it, so it no longer depends on how the host rounds.
2661581 to
a746d53
Compare
|
Independent recheck of the latest head
No game data or title execution used. |
Typed-format stores whose format is in the instruction (MTBUF) were refused by name. They now lower as raw stores of the element's dwords when no conversion is involved: formats of 32-bit channels (uint, sint, float; 32 to 32_32_32_32) store the data's bits, and the _d16_ forms of the 16-bit float formats (16_16, 16_16_16_16) store their halves as they are. The instruction must supply every channel of the format, as the typed loads' layout gives it. Everything else stays refused with a reason: norm, scaled and narrow integer formats (conversions), elements under a dword (a read-modify-write lanes would race on), and f32 data for half formats, because DXIL's f32-to-f16 op rounds toward zero (measured below) where the console rounds to nearest even; #422 addresses that. GPU tests store each lowered layout on the host GPU and WARP and read the dwords back; another test checks the refusals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mercuryVM
left a comment
There was a problem hiding this comment.
Tested a746d53 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 121 passed, including the new nearest-even f16 VALU tests on the GPU backend. cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 programs.
I checked f16_nearest against the IEEE rules:
- Normal path: adding
0xFFF + oddwith the exponent rebiased by 112 << 23, then>> 13, is the standard round-half-even. A carry out of the mantissa correctly bumps the exponent. - Subnormal path:
126 - eis at least 14 for every input below 2^-14, and clamping at 31 sends f32 zero and denormals to 0. The remainder versus half comparison is tie-to-even onq. - Edges: from
0x477FF000(65520) and up, including f32 inf, gives0x7C00. NaN keeps its sign and the top payload bits, quietened with0x7E00. - Small thing: the doc mentions MODE's default rounding. If a later PR models
s_round_mode, this helper is the single place to switch.
AI-assisted (Claude Code).
catminou4
left a comment
There was a problem hiding this comment.
This is the important kind of hardware-fidelity fix: the DXIL legacyF32ToF16 op rounds toward zero on NVIDIA and WARP (the comment documents the measured behavior: 1 + 3/2048 → 0x3C01, 1e6 → 0x7BFF), while the console's MODE default is round-to-nearest-even. Replacing all seven call sites with a software f16_nearest — pure bit manipulation, no DXIL op — makes f16 narrowing match the console.
The implementation handles the full range: normal rebias (127−15), subnormals with the 24-bit significand shift and clamp at 31, ties-to-even via the odd term, overflow to infinity at 65520, NaN quieting with payload preservation, and sign restoration. f16_toward_zero correctly keeps its semantics on top of nearest (one step back toward zero).
Test quality is the strongest I've seen in this queue: f16_nearest_reference is an independent search-based implementation that does NOT share the lowering's bit arithmetic, the test sweeps midpoint ties (both signs, ±1 ulp around each), a dense exponent spread, infinities, NaNs, −0.0, subnormals, and separately pins v_cvt_pkrtz_f16_f32 to toward-zero and v_add_f16 tie behavior.
Verified locally: test-merge with current main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) green — the new valu_tests execution cases are #[cfg(windows)]-gated like the rest of that suite, so they will run on the Windows CI lane, not here.: 96 passed / 0 failed / 1 ignored (execution tests Windows-gated; the bit-arithmetic itself I reviewed statically).
Typed-format stores whose format is in the instruction (MTBUF) were refused by name. They now lower as raw stores of the element's dwords when no conversion is involved: formats of 32-bit channels (uint, sint, float; 32 to 32_32_32_32) store the data's bits, and the _d16_ forms of the 16-bit float formats (16_16, 16_16_16_16) store their halves as they are. The instruction must supply every channel of the format, as the typed loads' layout gives it. Everything else stays refused with a reason: norm, scaled and narrow integer formats (conversions), elements under a dword (a read-modify-write lanes would race on), and f32 data for half formats, because DXIL's f32-to-f16 op rounds toward zero (measured below) where the console rounds to nearest even; JMAN730#422 addresses that. GPU tests store each lowered layout on the host GPU and WARP and read the dwords back; another test checks the refusals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 837bb40) Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
Two things about the s -> f16 step had no test: the VOP1 forms that convert an integer (Marvisak#866), and the step itself (`legacyF32ToF16`), which every f16 result in the translator goes through. `v_cvt_f16_u16` (0x050) and `v_cvt_f16_i16` (0x051) read the low 16 bits of the source; the i16 form shifts left then arithmetically right by 16 first, a path shared with no other tested instruction. The table holds 27 sources: everywhere below 0x8000, where the two forms must agree, and 0x8000, 0x8001, 0xC000, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFFE and 0xFFFF, where they part ways. Each conversion runs twice, once into a destination whose high half is zero and once into one seeded with 0xDEAD0000, so the half a 16-bit write leaves alone is checked as well. The expected bits are the truncating ones, not the nearest-even ones Marvisak#866 lists. The step rounds toward zero: 2051 gives 2050, 32767 gives 32752, and 65520, 65536 and 1e30 all give 65504, so no conversion produces an f16 infinity. Measured on an AMD RX 7600M XT (RDNA3, driver 32.0.31007.5012) and on WARP, which agree on every row, and consistent with the NVIDIA figures quoted in Marvisak#866 and with the "0x6801 on main" in Marvisak#422's note. Marvisak#422 moves the ties to nearest even, so its merge has to update these rows. Test-only: the lowering is unchanged. Flipping the i16 extension from an arithmetic to a logical shift fails the test at 0x8000, and so does adding an extension to the u16 form. 122 victory-shader tests pass, the one benchmark ignored; clippy with warnings denied, rustfmt and `cargo xtask policy` (154 files) are clean. AI-assisted; synthetic tests only, no game data.
… even, as the console does Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Independent verification on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head
|
The tester tree (upstream main ffe310e plus its PRs, update 8; see its INTEGRATION.md) ran 11-25 minutes in the world on an RTX 5070 without a device removal. Where both sides carry the same PR (the wave64 stack #223/#233/#320 with #439 and #180, #454, #336, #259, #331 and others), the tester tree's newer head is taken; our own work and the PRs only we merged are kept. Conflicts, file by file: - descriptor/host.rs: the tester layout (`spare`, the guard slot after the dynamic slots, `free_table`, the capped sampler heap). A capped view heap keeps `capped_reserve()` dynamic slots, now 2^18 by default (the tester measurement, and our 91fed158), still `VICTORY_DYNAMIC_SLOTS` in diagnostics builds; uncapped hosts keep our 2^19. Our slot statistics (TAKEN/PEAK/SHORT), the bindless plan cache (`CommittedRegions`, `timed_valid_title_address`, `timed_find`, `title_bounds`) and the revision triple are kept beside the tester's `guard_slot`. - Buffer states: the tester tree's buffer-state tracker (#334: every list moves host copies of title memory between GENERIC_READ and UNORDERED_ACCESS and back to common before it closes) and depth twins (#469) are taken. #40's enhanced/legacy indirect barriers (the indirect prelude, `use_indirect_buffers`, the title buffer handoff mirrored in `host_barrier`) cannot share buffers with that tracker and are dropped; #40's argument checks (`byte_len`, exact spans, overflow-safe clamp shader, aligned counts) stay. `indirect::bound` and #282's `rectangles` go through `buffer_state`, and `indirect::execute` (#334) replaces `done`. #178's DXC fallback compiles both of our passes. Our segments (#387: `close` ends the segment, `close_native` closes a native list) carry the tracker: each native list returns its buffers to common and its twins to their depth targets, and each prelude also holds our texture uploads. - shadow.rs: our write flow (`queue_write`, one `snapshot` after the batch's refreshes, `finish_writes`, the unchanged-page cache) with #261's copy-back that also gives the snapshot the title's bytes; its tests run through our flow. - pipeline/make.rs: #275's per-frame budget and per-pipeline wake pair, with our compute waits (a dispatch waits for its pipeline, up to 20 s). - pipeline/library.rs: the tester version (#347, #275's saver thread); our concurrent-write test kept. shader_cache.rs: our byte-returning cache API with #358's "translated, only not kept". - draw.rs: #493's root SRV tables; where no table can stand in, our drop of a draw whose root view is unset or outside title buffers. Our refusal of a table offset past the capped heap stays. - resource.rs: `HostTexture` has both our `staged` generation and the depth twin. - Shaders: the tester's lower.rs for the wave64 stack, #160's image sampling (it covers our #73 and #136 sample forms) and #453/#587; kept from our side: #136's preference for an image or buffer use of a descriptor read as data and its `s_buffer_` arm, #422's nearest-even narrowing (also for #169's d16 loads and #453's interpolation), #452's spin-wait limit, #391's LDS limit, #302's spilled root arguments (now in `NativeProgram::translation_ir`), #136's PSV0 interpolation beside the tester's row harmonisation. split_tests.rs, lane_read_loop_tests.rs and docs/wave64-on-wave32.md are the tester versions (the same PRs at newer heads). - Tests: #483's heap guard dwords filled in our GPU tests of #200 (v_div_scale) and #437 (scalar loads from a raw view), which read through the guard slot otherwise; #334's debug-layer test passes an offset to our `indirect::bound`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
AMD check at
Synthetic tests only, no game files. Tested with AI assistance (Claude Code). |
|
Tested on AMD: Radeon 860M (RDNA 3.5, driver 32.0.32029.2010) and WARP, at Heads-up: this conflicts with #1387 in |
|
Hardware evidence that this fix matters on AMD. On
A cross-device fuzzer ran the same 400 inputs per opcode (edge and random f16/f32 values) on all three devices:
With this PR, the RX 9070, the iGPU and WARP agree bit for bit on all of them. |
|
A heads-up on overlap: #169 adds one more |
The fuzzer also runs every VOP3P opcode the translator lowers
(v_pk_*, v_fma_mix*), comparing each f16 half on its own and the high
half of v_fma_mixhi_f16, with any NaN matching any NaN.
On main, v_pk_add_f16, v_pk_fma_f16 and v_fma_mix{lo,hi}_f16 differ on
AMD (f16 rounded toward zero by legacyF32ToF16); with Marvisak#422 the RX 9070,
a gfx1036 iGPU and WARP agree on all of them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Follow-up: the packed and mixed-precision f16 ops have the same AMD rounding issue, and this PR fixes them too. Same three devices, 300 inputs per opcode, each f16 half compared on its own:
|
|
Your round-to-nearest-even change for FP16 narrowing covered the mismatches we saw on both the RTX 5080 and Microsoft WARP. I ran the half-precision sweep from #1575 ( One example was |
|
Correction to my explanation above; the data and the conclusion stand. Measurements in fxceless/victory-fork#136 (RTX 3070 Ti, Intel Iris Xe, WARP) show that |
Problem. Every f16 result the translator produces goes through DXIL's f32-to-f16 conversion (
legacyF32ToF16, op 130). I measured it on an RTX 4080 and on WARP, and it rounds toward zero:0x3C010x3C020x03EE0x03EF0x00000x00010x7BFF0x7C00(inf)The code assumes round to nearest even ("Rounded to nearest even (the MODE default)" on
v_cvt_f16_f32, "round once to f16" invalu16), so every f16 op could be one ulp off. The affected paths are the f16 ALU ops,v_cvt_f16_f32,v_fma_mix*, the packed f16 ops, f16 interpolants and d16 loads. A value past 65504 also stayed finite instead of becoming infinity.Fix. A new
f16_nearestdoes the narrowing exactly with integer ops on the bits:All eight uses of op 130 now call it.
f16_toward_zero(v_cvt_pkrtz_f16_f32) already stepped back from a nearest-even conversion. It now steps back fromf16_nearest, so it gives the same results without depending on how the host rounds.Test.
f32_to_f16_rounds_to_nearest_eveninvalu_tests.rsruns on the host GPU and WARP:v_cvt_f16_f32on about 250 inputs: the midpoints between neighbouring halves, positive and negative, plus the f32 on either side of each, a spread from 2^-26 to past 65504, and ±inf, NaN, -0, 2^-25 and the f32 minimum normal. Each result is checked against a reference that finds the nearest half by search over the f16 values, so it shares no arithmetic with the lowering.v_add_f16: a tie, 1 + 2^-10 plus 2^-11 gives0x3C02; and 65504 + 32 gives inf.v_cvt_pkrtz_f16_f32still rounds toward zero.Results:
dxil.dll10.0.26100 passes.lower.rsit fails:v_cvt_f16_f32(2.9802326e-8 = 0x33000001) = 0x0000, want 0x0001.cargo test --workspaceandcargo xtask policypass. Clippy shows only the two warnings main already has.Rounding follows the ISA and MODE's default (round to nearest even). I haven't measured on console hardware whether the title changes the f16 rounding mode.
No game files.
AI-assisted (Claude Code): I reviewed the change and ran the tests above.