Shaders: round half-precision fused multiply-add once #2623
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2623
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2623"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@andrewpozdnakov7-jpg on GitHub · 2026-11-25 17:02 UTC · original
andrewpozdnakov7-jpg:codex2/half-fma-rounding→main· +214 −16 · 2 files · 4 commits · openAfter #1387, the scalar nearest-even V_FMA_F16 path already rounds once. V_FMAC_F16 and the toward-zero fused forms still use an f32 intermediate that can land on the wrong side of a half boundary.
This PR uses the shared fma_f16 helper for both fused instructions. The product of two finite halves is exact in f32, and TwoSum recovers the addition tail. Nearest-even keeps the midpoint logic from #1387. For toward-zero conversion the helper steps inward one f32 value only when the ordered tail comparison shows that f32 rounding moved outward. Clamp follows that correction. NaN tails fail the ordered comparison, so a genuine infinity is preserved.
Validation
32.0.21045.29003), the earlierbcdaee6head failed one of five targeted tests in both hardware runs. Exact head17a4e0dpassed all five in both runs. The tester's RGA disassembly of the toward-zero compute fixture shows the former float saturate folded into the integer subtract's clamp modifier. The ordered comparisons on the current head avoid that fold. Failure report, passing hardware rerun, disassembly comparison.17a4e0dovermain63bcca4. App-local WARP 1.0.22 (SM 6.6), Agility SDK 1.620.2 and DXIL validation. All fivehalf_fcases passed in both targeted runs. All 40 VALU, 34 semantics (one ignored), and 33 lowerer tests passed. Strict shader Clippy in default and all-feature configurations and the asset policy passed. GitHub CI.Coordination claim.
If you cherry-pick or rebase the fix or tests, please retain the Git author. If folding them into another commit, please add the trailer
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.AI-assisted.
@oni-swr on GitHub · 2026-11-25 17:38 UTC · original
Independent verification (no physical GPU, no game files, no competing PR). AI-assisted: Claude Code (Claude Sonnet 5.5).
VICTORY_D3D12_WARP=1andVICTORY_WARP=1(software WARP only;VICTORY_WARP=1is what makes the shader test harness skip its "hardware" adapter pass).origin/main=13f91caee2c230c6bb35(based on the main tip; merges cleanly)Results
cargo test -p victory-shader --lib --locked valu_tests -- --test-threads=1: 33 passed, 0 failed, 0 ignored (includes the 3 newhalf_fma*/half_fmac*tests). No "device ... skipped" lines.lower.rsreset toorigin/mainand the PR's tests kept, all 3 new tests FAIL (case 0); with the PR'slower.rsthey pass.cargo test -p victory-shader --lib --locked -- --test-threads=1: 270 passed, 3 failed, 3 ignored. The same 3 fail onorigin/main(267 passed, 3 failed, 3 ignored); all are cross-lane tests (gathers_at_a_level_match_the_hardware,permutes_read_the_lane_named_and_zero_from_lanes_left_out,a_mask_joined_with_data_at_a_uniform_loop_is_its_number) that assume 32-lane waves, which WARP does not provide. So +3 passes, no new failures.cargo clippy -p victory-shader --all-targets --locked: no warnings.Review: Knuth's branch-free TwoSum on the f32 sum of an exact f16 product, then round-to-odd (move the low bit toward the residual's sign when the sum's low bit is even and the residual is nonzero) before the f16 conversion. 24 >= 2*11+2, so odd rounding followed by the conversion is a single correct rounding. The ordered
ONEcompare leaves NaN/inf on the old path.v_mad_f16/v_mac_f16correctly keep the unfused path. No issues found.Not tested: any physical GPU or driver. The TwoSum relies on the driver not reassociating or contracting the plain f32 add/sub chain (the tests show only that WARP's compiler does not), so one run on real hardware is worth it. No retail title.
@andrewpozdnakov7-jpg on GitHub · 2026-11-25 17:43 UTC · original
Thanks for the independent A/B run. I disassembled the nearest-even fixture from exact head
c230c6bwith DXC. The separate product and the full TwoSum residual use unflagged fmul, fsub and fadd, and the residual comparison is an unflagged fcmp. DXIL gives those instructions precise semantics when their fast-math flags are unset. DXIL precise qualifierThe original sum is an explicit FMad operation. For finite half operands the product is exact in f32, so the fused and separate forms have the same rounded f32 sum. A physical-driver run remains a separate runtime check.
@kotekompotek on GitHub · 2026-11-25 17:49 UTC · original
Reviewed: this is the Boldo–Melquiond round-to-odd construction — compute the exact f32 sum, reconstruct both residuals (
lost_product/lost_addend), and bump the mantissa to odd only when the sum is even AND inexact AND ordered (NaN residual keeps the old path). Splittingv_fma/v_fmac(fused, single rounding) fromv_mad/v_mac(two roundings, stays onfmad) matches GCN semantics. Tests cover the tie-to-even and midpoint-plus-tail cases. CI green.@Mericas77 on GitHub · 2026-11-25 18:27 UTC · original
Tested on NVIDIA: RTX 4070 (the only adapter), driver 621.01, Windows 11, rustc 1.98.1. Head
c230c6bagainstmain13f91ca.half_fma_rounds_once_to_nearest_even,half_fmac_rounds_once_to_nearest_even,half_fma_rounds_once_toward_zero: 3/3 pass, throughrun_cases, so on the NVIDIA hardware adapter as well as WARP (noskippedline).f16filter, 12 tests, includingf16_arithmetic_rounds_once): 12/12 on bothmainand this PR.cargo test -p victory-shader --lib --locked(with--skip heap_direct_texture_reads_mark_their_heap_index, which crashes the process onmainhere, #2571):main266 passed / 3 failed / 3 ignored, this PR 269 / 3 / 3. The 3 failures are the known NVIDIA ones onmain(a_heap_buffer_read_by_element_takes_the_views_stride,a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,gathers_at_a_level_match_the_hardware) and are the same.So the round-to-odd intermediate gives the expected bits on NVIDIA hardware too, no regression. (Why it matters here: on #2594 the 4070's hardware results differed from WARP's in the same kind of rounding edge case.) No game data; the title was not run.
AI-assisted.
@Marvisak on GitHub · 2026-11-25 22:19 UTC · original · edited 2026-11-26 03:19 UTC
Automated triage for the maintainers.
Verdict: merge candidate (kept). Re-reviewed at
bcdaee6, which merges cleanly intomain. Both review points are addressed. The separatefmad_halfhelper and itspred::ONEcompare are gone.v_fma_f16andv_fmac_f16now both go through the existingLower::fma_f16incrates/victory-shader/src/emit/lower.rs. In toward-zero mode (mode16 == Some(3)) it steps the f32 sum one ulp inward only when the sign-adjusted TwoSum tail is negative (pred::OLT). That compare is ordered, so a NaN tail from an infinite sum leaves the sum as it is. No f32 value lies strictly between the stepped sum and the exact result, so the later truncation to f16 is correct. Clamp is applied after the correction.v_mad_f16/v_mac_f16keep the unfusedfmadpath.An AMD hardware run of the new
valu_testscases would still be welcome.CI: passing. Recommending a look, not approving.
Generated by Claude Code
@andrewpozdnakov7-jpg on GitHub · 2026-11-25 22:37 UTC · original
I reran the three PR fixtures on current main
f3578e3and with this PR applied on top of it. Both runs used the same app-local WARP SM 6.6 setup with the DXIL validator active.V_FMA_F16nearest-even passed on both. #1387 now covers that case.V_FMA_F16toward zero failed on main (0x6800vs. expected0x67ff) and passed with this PR.V_FMAC_F16nearest-even failed on main (0x6400vs. expected0x6401) and passed with this PR.The integrated build at local
64bcbd0also passed all 39 VALU tests. The last two cases still exercise the behavior this PR adds.@fuzeofficlal on GitHub · 2026-11-26 00:42 UTC · original
Code review and RTX 5090 + WARP + DXIL verification at head
c230c6bb35b9merged cleanly ontomain(63bcca43c6a0;emit/lower.rs=522,237B,2,051B under the 512 KiB cap):crates/victory-shader/src/emit/lower.rs:6825):let inexact = self.s.m.cmp(c, pred::ONE, residual, zero_f);emitspred::ONE.crates/victory-shader/src/emit/bitcode.rs:106-109(// Not emitted: the AMD D3D12 driver treats it as UNE, so v_cmp_lg lowers as OLT | OGT instead.), AMD's D3D12 driver evaluatespred::ONEasUNE. Whena * b + cis±InforNaN,residualbecomesNaN, soUNE(NaN, 0.0)returnstrue. For+Inf(0x7F80_0000, which has an even mantissa LSB),even && inexactfires andodd = bits + deltacorrupts+InfintosNaN(0x7F80_0001) orFLT_MAX(0x7F7F_FFFF).pred::ONEwithpred::OLT | pred::OGT(matchingv_cmp_lgatlower.rs:7962-7963) avoids the AMD driver hazard.lower.rsbudget note:main(63bcca43) already hasfma_f16(lower.rs:2300-2374) for RNEv_fma_f16(and#2884extends that helper to packedv_pk_fma_f16/v_pk_fmac_f16). Merging#2623as-is leaves two separate single-rounded f16 FMA implementations inlower.rswith only2,051B left under the 512 KiB policy limit. Unifying on one helper (potentially in a submodule) would avoid the duplication and save space inlower.rs.dxil.dll10.0.26100.0,VICTORY_D3D12_DEBUG=1, Rust 1.98.1):cargo clippy(default +--features diagnostics): 0 warnings;cargo test -p xtask --locked: 6/6 passedvalu_tests(3 new half FMA/FMAC tests + existingv_fma_f16_rounds_once_and_preserves_other_half): passed on both RTX 5090 and WARP;emit::lower::tests33/33;emit::semantics_tests34/34 (1 ignored).@andrewpozdnakov7-jpg on GitHub · 2026-11-26 01:06 UTC · original
Addressed the residual predicate and duplication review in bcdaee6.
Both fused scalar forms now share the existing fma_f16 helper and its TwoSum residual. The toward-zero branch uses an ordered less-than comparison and clamps after correcting the sum. The separate fmad_half helper and its ONE predicate are gone.
On the old PR merged with current main, temporarily substituting UNE for ONE reproduced the reported failure on WARP: positive infinity narrowed to 0x7bff instead of 0x7c00 in the toward-zero fixture. The same test passes on the updated head for all 24 FMA/FMAC exceptional cases. All five clamp cases and 40 project VALU tests also pass with verified app-local WARP SM 6.6 and active DXIL validation. This control models the predicate behavior on software WARP. An AMD hardware rerun is still useful.
Checked clean merges with live main and with the current #2884, #2917, #2825 and #2938 heads individually. The integrated lower.rs remains below the 512 KiB cap in each case.
Please retain the Git author if cherry-picking the change or tests. If they are folded into another commit, please add the trailer
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.@Dukestar69 on GitHub · 2026-11-26 05:13 UTC · original · approved these changes
Tested merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP,dxil.dllvalidator active):cargo test -p victory-shader --lib emit::valu_tests::half_f: 5 passed, 0 failed (RTX 5090 hardware + WARP).cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@wwwwwwww3333333 on GitHub · 2026-11-26 08:36 UTC · original
AMD hardware run of
bcdaee6, as requested. RX 6750 XT (RDNA2), driver 32.0.21045.29003, Windows 11, Rust 1.98.1.cargo test -p victory-shader --lib -- half_f --test-threads=1: 4 passed, 1 failed, two runs out of two.half_fma_toward_zero_clamps_after_correcting_the_sumfails on the hardware adapter only, atclamped case 3(+inf: 0x7C00, want 0x3C00); WARP passes.It is wider than +inf. In toward-zero mode the clamp is not applied at all on this GPU: 2, 65504, -1, -inf and NaN come back as 0x4000, 0x7BFF, 0xBC00, 0xFC00 and 0x7E00. Nearest mode is right for all of them. Cases 0 to 2 of the current test pass without any clamp (they end at or below 1.0 anyway), so only the infinity cases catch it.
The DXIL is right: DXC's disassembly of the toward-zero shader has
Saturateonbitcast(sub i32 bits, zext(olt))and its result is used. On this driver the clamp is lost when its operand is that integer step. What I measured, all in toward-zero mode, hardware adapter:selectas the operand, and withFMax/FMinin place of Saturate.That fits a driver folding the float clamp into the instruction that made the value, but I have not seen the ISA, so please read it as a pattern and not a proven cause.
A change that avoids it, used at the two clamp sites in
fma_f16:NaN still gives 0, +inf gives 1. With it: all 13 inputs I tried are right on the hardware adapter in both modes, the whole
victory-shadersuite is 307 passed, 0 failed, 3 ignored (two runs),cargo clippy -p victory-shader --all-targets -- -D warningsis clean, and lower.rs grows by 497 bytes (521,831).Suggested test additions: 2.0 -> 0x3C00, 0x7BFF -> 0x3C00 and -1.0 -> 0 among the clamped cases. They fail here without the change and do not depend on infinity.
Feel free to take any of this. Unit tests only, no game files.
@andrewpozdnakov7-jpg on GitHub · 2026-11-26 09:16 UTC · original
@wwwwwwww3333333 thank you for the AMD run — that is exactly the case the WARP control could not model.
Implemented your clamp at both sites in
fma_f16, in 17a4e0d oncodex2/half-fma-rounding:Saturateno longer sits on the integer step in toward-zero mode, and neither mode clamps through it. NaN maps to zero and negative zero to positive zero, as before.Tests: the fixture now covers
2.0 -> 0x3C00,-1.0 -> 0,65504 -> 0x3C00andNaN -> 0in both rounding modes, which is what you suggested.Verification here (app-local WARP SM 6.6, DXIL validator active):
17a4e0d;lower.rsreverted to63bcca4fails, so the clamp is what changes the result;cargo clippy -p victory-shader --all-targets -- -D warningsis clean;Could you rerun
cargo test -p victory-shader --lib -- half_f --test-threads=1on the same RX 6750 XT? The interesting cases are the toward-zero clamps:2,65504,-1,-infandNaN. If the driver still folds the compares away, the next step is the float multiply by1+2^-23that you found works, but I would rather not add it without the ISA evidence.Git author and the
Co-authored-bytrailer are as stated above if this is folded into another commit.@wwwwwwww3333333 on GitHub · 2026-11-26 09:22 UTC · original
Reran
17a4e0don the same RX 6750 XT (RDNA2, driver 32.0.21045.29003, Windows 11, Rust 1.98.1):cargo test -p victory-shader --lib -- half_f --test-threads=1gives 5 passed, 0 failed on the hardware adapter, two runs out of two (it was 4 passed, 1 failed onbcdaee6).cargo clippy -p victory-shader --all-targets -- -D warningsis clean. So the ordered-compare clamp is enough here and the 1+2^-23 multiply is not needed. The cause on the driver side is still unproven: I have no ISA output, only that the same input clamps correctly through compares and selects and not through saturate on the integer step.@wwwwwwww3333333 on GitHub · 2026-11-26 09:30 UTC · original
I got the ISA for the toward-zero fma from the installed driver (32.0.21045.29003) with Radeon GPU Analyzer 2.15 (-s dx12, gfx1031, the DXIL dumped from
bcdaee6and from the ordered-compare version). Onbcdaee6the driver folds the float saturate into the integer subtract at the end of the sign step:v_sub_co_ci_u32_e64 v0, vcc_lo, v1, 0, vcc_lo clamp. On an integer VALU op the clamp bit clamps the integer result, not a float to [0, 1], so the float clamp is lost, which matches 2, 65504, -1, -inf and NaN coming back unclamped. With the ordered-compare clamp (17a4e0d) there is no clamp bit on that op; the clamp is v_cmp_lt_f32/v_cndmask_b32 and v_cmp_gt_f32/v_cndmask_b32. So the cause is a driver fold of a float saturate on a bitcast integer result into an integer clamp modifier, and the ordered compares avoid it. This is from a standalone compute pipeline of the test shader, toward-zero mode only; I did not disassemble nearest mode or the game's own pipelines.@fuzeofficlal on GitHub · 2026-11-26 09:33 UTC · original · reviewed
Verified
17a4e0d7locally on Windows 11 x64 (dxil.dll10.0.26100.0):cargo clippy -p victory-shader --all-targets --locked -- -D warnings(default and--all-features): clean (2.37s/2.21s).cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed, 0 failed (0.35s).cargo test -p victory-shader --lib --locked half_f -- --test-threads=1: 5 passed, 0 failed on both RTX 5090 + WARP (4.19s) andVICTORY_WARP=1(0.12s).emit/lower.rsis521,936bytes (2,352bytes under the 524,288-byte policy cap).Replacing both
Saturatecalls withclamp01_ordered(pred::OGT/pred::OLT+select) gives the expected endpoint and signed-zero/NaN behavior in both RNE and RTZ modes (neither clamped endpoint is a half midpoint in RNE, so the TwoSum residual cannot move it), and the expandedhalf_fma_clamps_in_both_rounding_modesfixture now covers finite out-of-range inputs (2.0,65504.0,-1.0) alongside±InfandNaN. Pending confirmation on the RX 6750 XT that reproduced thebcdaee6clamp drop, this looks ready.@andrewpozdnakov7-jpg on GitHub · 2026-11-26 09:57 UTC · original
@fuzeofficlal the AMD rerun requested in your review is in. Current head
17a4e0dpassed all fivehalf_ftests in both runs on the same RX 6750 XT with driver32.0.21045.29003.@zed6000 on GitHub · 2026-11-26 14:25 UTC · original
A hardware result from AMD, where this fails. Head
17a4e0dmerged ontomain63bcca4(a clean merge),cargo test -p victory-shader --lib --lockedon an RX 7900 XTX (RDNA3, Windows 11): 303 passed, 2 failed. Both failures areemit::semantics_tests::f16_arithmetic_rounds_onceandsixteen_bit_floats_match_the_hardware, on the same case:In the low half that's
0x0001(the smallest f16 subnormal) ×0xfc00(−inf) +0x5152. The expected result is −inf (fc00, which WARP returns). The AMD hardware returns a NaN (fe00), as if the subnormal were flushed to zero before the multiply (0 × inf = NaN). Every other case in both tests matches on hardware and WARP, includingv_fma_f16andv_pk_fmac_f16. Onmain63bcca4the same two tests pass on this card. So the newv_fmac_f16path through the sharedfma_f16helper seems to let the host flush an f16 subnormal operand on AMD, at least for this inf case. That's 6 of 143 inputs; I only printed the first.@andrewpozdnakov7-jpg on GitHub · 2026-11-26 14:56 UTC · original
Thanks, @zed6000. I added your exact
0001 × fc00 + 5152input to the non-finite VALU fixture, along with signed subnormals, true zero, conflicting infinities and signed-zero controls. The expanded fixture passes on WARP 1.0.22 on both17a4e0dand the candidate.The candidate uses the original FP32
FMadoperation for the sum and keeps the separate FP32 product for the TwoSum residual. Two finite FP16 operands have an exact FP32 product, so the rounding correction still sees the same mathematical sum. For the reported infinite product, the candidate tests whether routing the sum through the operation that passed on main is enough. Candidate on top of17a4e0dOn WARP, both builds pass all five
half_ftests and both semantics tests named in your report. The candidate also passes all 40 VALU tests, strict Clippy and policy. The RX 7900 XTX result is still open. Could you rerun the same two semantics tests and the expandedhalf_fused_nonfinite_results_survive_roundingtest on that card witha67da51? That will tell us whether to bring the candidate into this PR.AI-assisted with Codex.
@andrewpozdnakov7-jpg on GitHub · 2026-11-26 15:01 UTC · original
@zed6000 Thanks for checking the same fixtures on
mainand17a4e0d. I am taking the RX 7900 XTX regression. I will reduce the reported subnormal × infinity input to a directed test and check it alongside the finite rounding and clamp cases. If you still have the driver version or the other five failing rows, those would help pin down the hardware comparison. I'll post the exact follow-up SHA for a rerun.@andrewpozdnakov7-jpg on GitHub · 2026-11-26 15:10 UTC · original
@zed6000
a756f0fis now the PR head. Scalar FMA and FMAC convert signed half subnormals from their bits before the fused operation. The finite rounding and clamp paths are unchanged.The directed test covers the reported row, swapped operands, both infinity signs, a normal-input control, a finite subnormal product and addend, and a true zero-times-infinity control. It checks 28 results across FMA/FMAC and nearest-even/toward-zero on WARP with DXIL validation. Default WARP passed before and after. In a separate test-only run that forces signed half subnormals to zero at the legacy conversion,
17a4e0dreproduced yourfe00versusfc00result anda756f0fpassed all 28. The VALU, semantics and lowerer suites passed 41, 34 and 33 tests respectively, with one ignored semantics test. Strict shader Clippy and the asset-policy check passed.Could you rerun
fused_half_keeps_subnormal_operands,f16_arithmetic_rounds_onceandsixteen_bit_floats_match_the_hardwareon the RX 7900 XTX at exacta756f0f? That will check whether the fix covers the failure on the adapter itself.Please retain the Git author on cherry-pick or rebase. If folding the fix or tests into another commit, use
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.@zed6000 on GitHub · 2026-11-26 16:13 UTC · original
@andrewpozdnakov7-jpg Retested
a756f0fon the same RX 7900 XTX (RDNA3, Windows 11, Adrenalin driver 32.0.32029.2010): merged ontomain63bcca4(fast-forward),cargo test -p victory-shader --lib --lockedgives 308 passed, 0 failed (3 ignored). Bothf16_arithmetic_rounds_onceand the expanded non-finite fixture now pass on AMD hardware.@andrewpozdnakov7-jpg on GitHub · 2026-11-26 16:19 UTC · original
Thanks for rerunning the exact head, @zed6000. The same-card RX 7900 XTX retest resolves the six earlier
v_fmac_f16mismatches: bothf16_arithmetic_rounds_onceand the expanded non-finite fixture pass ona756f0f, and the shader suite reports 308 passed, 0 failed, 3 ignored. The PR check passed on the same head. This is ready for maintainer review.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.