Repository navigation
Recompiler: round f16 add / sub / fma / ldexp results to nearest even - #1841
vladbondarenko97 wants to merge 1 commit into
Conversation
The HLSL path converted the f32 result of v_add / sub / subrev / fma / fmac / fmamk / fmaak / ldexp_f16 and v_pk_add / pk_fma / pk_fmac_f16 with f32tof16, which rounds toward zero on NVIDIA (RDNA2's default is nearest even). Only v_mul_f16 / v_pk_mul_f16 used f32_to_f16_rm. Measured on an RTX 3080 Ti: v_add_f16(0x3C01, 2^-11) gave 0x3C01 (want 0x3C02), v_pk_add_f16 hi lane 0xbfff (want 0xc000), v_ldexp_f16 into the denormals 0x207 (want 0x208). add / sub / ldexp are exact enough in binary32 (2p+2 bits), so they go through the same rounded conversion as v_mul_f16. fma needs one rounding: fma_rto computes the exact product, the TwoSum error of the sum, and rounds the f32 result to odd, so the later binary16 rounding sees the exact value's side of every tie (fma(0x3C01, 0x0FFE, 0x3C01) is 0x3C01; an RNE f32 sum gives 0x3C02). v_cvt_f16_u16 / i16 (xcl-dev#515), the directed modes (xcl-dev#128) and v_fma_mix*_f16 are not touched. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hri58aiQKmMwqS6K8CHq2s
|
Checked on Ampere: RTX 3070 8 GB, driver 616.56, Windows 11, debug, The direct DXIL backend still has the same bug. The test compiles the HLSL itself, so #1742 doesn't cover these. It moves only AI-assisted (Claude Code). Synthetic tests only. |
|
Independent HLSL check of 89724e5 on an RTX 3080 Ti (621.35), Windows 11 build 26300, Rust 1.99.1 MSVC debug, DXC 1.8.2502.11. I extended the existing exact-integer reference test to cover
AI-assisted. Codex (GPT-6 family, exact variant unavailable) extended and ran the test and drafted this report. |
|
Full-suite run on NVIDIA and the AMD iGPU, to go with the targeted checks above. Tested exact head Tested
Not tested
AI use
|
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
|
Both AMD and NVIDIA GPU checks and CI are green on Remaining before merge: please address @upipibi's review suggestion to route the new test through |
In the HLSL path the f32 result of
v_add/sub/subrev/fma/fmac/fmamk/fmaak/ldexp_f16andv_pk_add/pk_fma/pk_fmac_f16was converted withf32tof16, which rounds toward zero on NVIDIA; RDNA2's default is round to nearest even. Onlyv_mul_f16/v_pk_mul_f16usedf32_to_f16_rm(#166). Found on an RTX 3080 Ti (reported on #1269 and #128):v_add_f16(0x3C01, 2^-11)gave0x3C01(want0x3C02),v_pk_add_f16high half0xbfff(want0xc000),v_ldexp_f16into the denormals0x207(want0x208).What is in it
crates/xcl/src/gpu/recomp/emit.rs:v_pk_add_f16use the same rounded conversion asv_mul_f16(the f32 result is exact or correctly rounded with 2p+2 bits, so one rounding to f16 is right).v_pk_fma_f16/v_pk_fmac_f16use a new prelude helperfma_rto: the exact product plus the TwoSum error, rounded to odd in f32, so the following binary16 rounding is a single correct rounding (fma(0x3C01, 0x0FFE, 0x3C01)=0x3C01; an RNE f32 sum gives0x3C02).fma_rtois inPRELUDE, so every program's DXIL cache entry is rebuilt once.crates/xcl/src/gpu/recomp/tests.rs:gpu_f16_arithmetic_rounds_to_nearest_even: 8 results × 4096 lanes against an exact integer reference, with ties, subnormals and the double-rounding case.v_cvt_f16_u16/i16(Recompiler: v_cvt_f16_u16 / v_cvt_f16_i16 round to nearest even #515), directed rounding modes (Recompiler: f16 add / sub / fma rounded once under a directed mode (#… #128),v_fma_mix*_f16,v_mad_f16, and the transcendental f16 ops (stillf32tof16).Tested
cargo test -p xcl --lib gpu_f16_arithmetic_rounds -- --test-threads=1: without theemit.rschange FAILED, 13780 wrong (e.g.v_add_f16 a=3c01 b=1000: got 3c01, want 3c02;v_fma_f16 a=9008 b=9130 c=823a: got 8234, want 8235); with it 1 passed (on this head,89724e5, and on the same change before the rebase,4c4b190).cargo build --workspace, thencargo test --workspace --no-fail-fast -- --test-threads=4on this head89724e5: 960 passed, 0 failed, 43 ignored.gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware,the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu,cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu,coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu,a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu,a_per_lane_2d_or_3d_group_runs_on_the_gpuand the wave64 halves off16/f32/f64/integer_programs_on_the_gpu; their per-lane halves that the per-lane form refuses;pso_measurement_compiles_an_exact_synthetic_record(cannot disable the driver cache);heap_window_out_of_memory_falls_back_to_the_switch_form(no heap windows).Regression run
main:
this PR:
Not tested
f32tof16may already round to nearest); the f16 ops listed above as not in it; a game that depends on these results (the regression run is a 120 s boot/menu run).AI use
Related: #128, #515, #1269
🤖 Generated with Claude Code
https://claude.ai/code/session_01Hri58aiQKmMwqS6K8CHq2s