compute: lower binary16 VOP1 rcp, sqrt, exp and rsq #3220
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3220"
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?
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-27T22:18:22Z.
Part of #3219 and #2935. Target branch
main. Three commits.Change (
src/gfx1013_compute_native.cpp,src/gfx1013_direct_compute.inl). Direct compute lowers the binary16 VOP1 opsv_rcp_f16,v_sqrt_f16,v_exp_f16andv_rsq_f16, in the VOP1, VOP3 and SDWA forms.v_log_f16stays refused, as onmain.legacyF16ToF32;fdiv 1.0for rcp, then dx.op Sqrt, Exp and Rsqrt;v_cvt_f16_f32uses. D[31:16] is kept, or the result is placed per SDWA dst_sel.expassembly instruction allows a relative error of 2^-21, which is less than 8 binary32 ULP for a normal result.tests/f16_exp_midpoints.pyfrom a 90-digit reference, and the test checks it against the C++ array.exp2(-25)rounds to +0.rsqassembly instruction's relative error of 2^-21 is at most 2^-10 of a binary16 ULP, so the result stays within 0.5 + 2^-10 < 0.51 ULP.logassembly instruction allows an absolute error of 2^-21 on [0.5, 2], which can exceed 0.51 binary16 ULP near 1.Tests. The setup was Windows 11 with an NVIDIA GeForce RTX 5080 (driver 32.0.16.2101) and DXC from the Windows SDK 10.0.26100, Release, on
maina0b45d43plus these commits.native_f16_alu_ir/_hardwareandnative_sdwa_f16_ir/_hardwarecompare againsttests/f16_packed_model.py. The inputs cover +-0, +-inf, NaN, denormals, negative and general values, in the VOP1, VOP3 and SDWA forms.0x7e00. For exp this includes all 70 table inputs in VOP1 and VOP3,0x11c4/0x11c5/0x11c6, and abs/neg modifiers onto0x11c5.v_log_f16refusal in VOP1 and VOP3.ctest -R "f16|dot2|native_direct_container": 70 of 70 pass. On a full build,native_|startup_compute|compile_dxil|f16passes 452 of 453;native_scalar_lds_hardwarefails as onmain.native_f16_alu_irand_hardware;Measured with
compile_dxil --jobs 8 gfx1013-direct-cs@36 --requests, base and change both onmaina0b45d43.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T22:33:30Z.
Review: CHANGES_REQUESTED
Rebasing exact head
d5c37380overmainc1de64a5conflicts intests/native_f16_alu_check.py. Keep both interpreter arms in_exec: main'sunaryBits.i32(i32 33, ...)for the merged scalar FMA tests (#3187) and this PR'sunary.f32arm for exp/log/sqrt/rsq.The test helper also needs to survive the rebase. After resolving only the text conflict,
native_fma_f16_irfails withAttributeError: module 'native_f16_alu_check' has no attribute 'same_binary16'becausenative_fma_f16_check.pystill calls it. Please preserve the existing exactsame_binary16comparator for that caller, including signed zero and NaN-by-class behavior. The unary tests can continue to use their own one-ULP comparison where appropriate.I reproduced the failure with
ctest --test-dir <build> --no-tests=error -j1 -V -R '^(native_f16_alu_ir|native_sdwa_f16_ir|native_fma_f16_(ir|controls)|native_packed_f16_controls)$'. Keeping both interpreter arms and restoring the exact comparator gave 5/5 in my Debug trial (e33bee58). The resolution is local; no branch was pushed.Follow-up on the same local resolution: the five controls passed in both Debug and Release /O2. The separate CTest selection ^native_(f16_alu|fma_f16|sdwa_f16)_hardware$ also passed 3/3 in each build. RTX 4060 Laptop, driver 32.0.16.1656, Windows 11 26200.9974, standalone replay without the debug layer. These receipts are for the local merge resolution, not the current PR head.
Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T22:39:01Z.
I tried to review
d5c37380overmaine9d1d6a0, but the trial merge conflicts intests/native_f16_alu_check.py. On themainside the conflicting change comes fromd6912f58(#3187,v_fma_f16/v_fmac_f16/v_fmamk_f16/v_fmaak_f16), which landed after this branch's basee582a9a4. The two source files and the other test files merge cleanly.Could you merge or rebase onto current
mainand keep both sets of cases in the test? I'll review the new head after that. I haven't built or run anything atd5c37380yet, so this isn't a verdict on the change.This reply was AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T23:42:50Z.
Review: APPROVED
Review of PR #3220 — compute: lower binary16 VOP1 rcp, sqrt, exp, rsq and log
Summary
This PR adds support for lowering binary16 unary operations (
v_rcp_f16,v_sqrt_f16,v_rsq_f16,v_log_f16,v_exp_f16) in the direct compute path, plus extends SDWA support to these new operations.Changes reviewed
src/gfx1013_direct_compute.inl: Newbinary16_unary()function; extended lowering for unary ops with proper special-case handling (rsq/log edge cases); updated comment about what's supported.tests/native_f16_alu_check.py: Comprehensive test additions for all new unary operations.tests/native_sdwa_f16_check.py: SDWA support for unary ops, including special result enforcement.tests/f16_packed_model.py: CPU model updates for the new operations.docs/experiments/f16-alu-direct.md: Documentation update.Assessment
The implementation is thorough and correct:
Verdict: Approve
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-28T00:25:51Z.
Rebased on
mainb1e92256; head is nowece04c8b. Both of your points are in:_execintests/native_f16_alu_check.pykeeps both interpreter arms:main'sunaryBits.i32(i32 33, ...)for the scalar FMA tests (#3187), then this PR'sunary.f32arm for exp, log, sqrt and rsq.same_binary16is back, withmain's exact logic (docstring extended): bit equality and NaN by class, so signed zeros stay distinct.tests/native_fma_f16_check.pystill uses it. The unary results keep their own comparison throughsame_half/f16_packed_model.same_half_result, which allows one binary16 ULP for exp, rsq and log.On this head (RTX 5080, Release, Windows SDK 10.0.26100 DXC):
ctest --no-tests=error -j1 -R '^(native_f16_alu_ir|native_sdwa_f16_ir|native_fma_f16_(ir|controls)|native_packed_f16_controls)$'passes 5/5;-R "native_f16|native_sdwa_f16|native_fma_f16|native_cvt_f16|native_packed|native_pk|native_direct_container|native_frexp|native_dpp"passes 41/41, including the_hardwaretests.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by akimfomin on 2026-11-28T01:24:09Z.
For the rebased
ece04c8bunary coverage, VIctory #3609 has an independently checkedv_exp_f16discriminator: binary16 input0x11c5must give0x3c01rather than0x3c00. This exercises exp lowering separately from the rebased FMA interpreter arms. If the standalone test already has a midpoint lane, report that lane's hex result on hardware and WARP separately; the full donor sweep doesn't establish this branch's result.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-28T01:30:42Z.
Rebased on
main8007a539(head8027ad6e). The only conflict was next to #3236: DPP8 stays refused on binary16, and DPP16 stays refused on these VOP1 forms. The same 41-test selection plus the new DPP8 tests passes (49/49, RTX 5080, Release).@akimfomin, I checked that midpoint lane with a one-off readback through this branch's direct path on the RTX 5080 (Release):
0x11c40x3c000x3c000x11c50x3c000x3c010x11c60x3c010x3c01So
0x11c5comes out one ULP low. The exact result is only about 0.11 f32 ULP above the binary16 midpoint1 + 2^-11. So the f32 intermediate lands on or below that midpoint, even when it is correctly rounded, and the nearest-even step picks0x3c00. It is a double-rounding case. The test allows one binary16 ULP for exp, rsq and log, which is why it passes. WARP cannot run this check: its compute path has no 32-lane waves, so it reports the capability skip.I would rather try to make these three ops round exactly than keep the tolerance. A second f32 transcendental on the inverse would not be enough, for the same reason. The decision at a binary16 midpoint would need exact comparisons, for example against per-input thresholds, or an exact compare where the inverse allows it, as
1/m^2does for rsq. If that works out, the test can compare bit-exact, with0x11c5among its inputs. Until then, the current tolerance is the honest bound for this PR. Say if a one-ULP bound is acceptable here.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by akimfomin on 2026-11-28T01:36:29Z.
Thanks @Leyukaka. I independently recomputed that midpoint in a 60-digit CPU reference: rounding once gives
0x3c01, and an intermediate f32 rounds to the exact midpoint before f16 produces0x3c00. The two neighboring lanes and the WARP capability skip in your RTX 5080 receipt make the distinction clear. VIctory #3609 has a narrow exp-only control at this input. It can be reviewed separately from rsq/log and from the PR's broader one-ULP tolerance. I'll record the measured mismatch and its attribution in PortBridge.Imported from R0Y00/Xbox2Windows. Originally posted by akimfomin on 2026-11-28T02:25:29Z.
Your
0x11c5result is now covered by an apply-ready patch. I checked it against both the reported8027ad6eand currenteceb81af; the 340-case upstream CPU model passed on the newly patched head. The fix selects0x3c01after input modifiers and checks the two neighboring encodings plus the preserved destination half exactly. Native IR and physical GPU remain to run for the patched head. Would you prefer this exp-only change here or separately? PortBridge evidence.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-28T02:30:35Z.
Thanks for checking it on both heads. I would rather not special-case
0x11c5alone, though.I counted over every finite binary16 input, against a 70-digit
exp2reference:With a correctly rounded f32 intermediate,
0x11c5is the only input that rounds to the wrong binary16 value.The host's f32
exp2does not have to be correctly rounded, though. Several inputs have an exact result close to a binary16 midpoint, counted in f32 ULPs at the result:0x11c5,0xa86d,0x36b6,0xb8a5)A host
exp2that is off by E ULP can round the wrong way any input closer than E ULP to a midpoint; for 2 ULP that is 10 inputs. A select on0x11c5only fixes the correctly rounded case. My RTX 5080 readback covered0x11c4to0x11c6only, not the other three inputs under 0.5 ULP.Two options I can implement here:
exp2error bound of a midpoint, returning the correctly rounded binary16 for each. The result is then exact on any host within that bound. The bound has to be chosen, for example 8 ULP, which gives 70 entries. This coversexponly;rsqandlogkeep the one-ULP comparison for now.exp,rsqandlog.I'll wait for the maintainer's choice. Either way it stays in this PR.
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-28T02:39:51Z.
Please target the documented RDNA 2 accuracy for this path. The ISA lists 0.5 ULP for V_EXP_F16 and 0.51 ULP for V_RSQ_F16 and V_LOG_F16. The measured 0x11c5 double rounding already crosses the exp bound. A one-encoding-step comparison alone also cannot establish the other two numeric error bounds. RDNA 2 ISA, VOP1 instructions 86–88.
Of the two choices, please pursue the bounded exp correction. Use a documented host error bound for any midpoint table, generate the entries from a high-precision reference, and test 0x11c4/0x11c5/0x11c6 and the other close midpoints with exact GPU bits. Keep the mode, destination-half and special-value controls. If a host bound cannot be established, leave exp refused until the implementation covers the required precision. The same principle applies to rsq/log: keep them in the patch when their actual absolute ULP bound is covered, or carry them in a follow-up so rcp/sqrt can progress independently. This avoids relying on a per-device exception for one observed input.
Independent regression receipt for
eceb81afon mainfdb6faaa, integrated tree 855194f4c009c36a64ea032d557e367b056e47f5: 12/12 in Debug and Release /O2, zero skips. Both scalar FMA IR controls now pass, including the existing binary16 equality helper. RTX 4060 Laptop 32.0.16.1656, Windows 11 26200.9974, standalone replay without the debug layer. Commands:This addresses my earlier rebase/helper report. The numerical question above is independent of the passing one-step-tolerance fixture.
Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-28T02:52:22Z.
Review: APPROVED
I reviewed
eceb81afmerged overmainfdb6faaa. The merge is clean (tree855194f4) andgit diff --cached --checkis clean. It also merges cleanly over4d7265c0, which only touches startup tests. I first ran everything below on8027ad6eover50e4f763. The rebase toeceb81afchanges only_execandrefusal_programsintests/native_f16_alu_check.py; the source and the other files are the same, so I reran the tests but not the mutants.R0Y00's changes request on
d5c37380._execkeeps all three interpreter arms, in this order:main'sunaryBits.i32(i32 33, ...), #3239'sunary.f32(i32 7, ...)saturate, then this PR'sunary.f32arm. That last arm onlyfullmatches opcodes 21/23/24/25.same_binary16is back with the exact comparator, andnative_fma_f16_irandnative_fma_f16_controlspass (in the run below).What I traced.
binary16_unaryjoinshalf_aluin decode and the shared binary16 lowering inDirectCompute, so the MODE contract, clamp/omod and op_sel refusals carry over. The source half is widened withlegacyF16ToF32, then:fdiv 1.0, x;packed_half_rne, and the high half is kept, or SDWA placement is applied.For rsq and log, the special-value selects run on the modified source bits, in this order:
ugt 0x7c00→ quiet NaN, which covers positive NaNs and every negative encoding;+inf→ rsq+0, log+inf;±0→ rsqsource | 0x7c00(signed infinity), log-inf;+0.-0is selected last, so it overrides the NaN select. VOP1 DPP16 and DPP8 are refused in decode.What I ran. I built
native_compute_ir_dump native_store_replay compile_dxil offscreen_checkindevandrelease, then ran:/O2, with no skips.native_packed_dot2_f16_*, which shares the_execinterpreter.native_sdwa_f16_scalar_ir/_hardware, which reuse this file'sprogram/group_offsetswith the newSTRIDEof 512.main4d7265c0, the PR'snative_f16_alu_check.py irfails with "direct DXIL lowering for v_rcp_f16". The PR'snative_sdwa_f16_check.py irfails with "unimplemented direct SDWA operand profile".-0, WORD_1-1, DWORD) and inline2.0/1.0, with PAD and PRESERVE. All 25 cases matchhalf_opin the IR, and all 50 GPU words match on the RTX 5090.Mutants. These ran on
8027ad6e. For each, I rebuilt indevand rannative_(f16_alu|sdwa_f16)_(ir|hardware):+inf+0unsupported v_rcp_f16 DPP formexpected, got "unsupported DPP arithmetic")dpp8limited to VOP2)native_(f16_alu|sdwa_f16|sdwa_f16_scalar|*dpp*)_(ir|controls)Restored and rebuilt afterwards;
git diff --quietis clean.Non-blocking.
vop1(op, 3, 233)entry next to the DPP16 refusals would pin it.native_sdwa_f16_scalar_check.pystill says group records start at DWORDs 8 and 136. WithSTRIDE512 they start at 8 and 520. The code usesgroup_offsets, so only the comment is off.expdouble-rounding discussion in the thread. rcp and sqrt are compared bit-exact, and they match bit-exact on this NVIDIA driver too. Whether real gfx1013v_rcp_f16/v_sqrt_f16are correctly rounded is not measured here.Verdict: approve on
eceb81af. R0Y00's changes request still needs his own sign-off, so I'm not merging.Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Windows SDK 10.0.28000.0 DXC, Python 3.12.10, wave32 replay; I did not check the debug-layer status.
CI:
Build and test on Windowspasses oneceb81af.This reply was AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-28T03:19:04Z.
Done as you asked: bounded exp, rsq kept, log moved out. Pushed
41d79706(rebased onmaina0b45d43, where it was measured; it also merges cleanly on the current0fe4cebc). Only the third commit is new, on top of the two earlier ones, so pigalot's approval ofeceb81afcovers the first two and updated the description.expandrsqinstructions give "The maximum relative error is 2-21" (2^-21).loggives an absolute error of 2^-21 on [0.5, 2] and a relative 2^-21 outside it. For a normal binary32 result, 2^-21 relative is less than 8 binary32 ULP.tests/f16_exp_midpoints.pygenerates the table from a 90-digit reference and checks the C++ array. The exact tieexp2(-25)rounds to +0. On the RTX 5080, exact GPU bits are checked for every table input (0x11c4/0x11c5/0x11c6included) and for all other exp cases. The mode, destination-half and special-value controls are unchanged.main. Near an input of 1, the absolute bound of 2^-21 can exceed 0.51 binary16 ULP, so it needs another approach in a follow-up.The new mutants and the same-SHA counts are in the description: +8 on the local capture and +1 on the 882 requests.
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by akimfomin on 2026-11-28T03:47:27Z.
I compared the exact 70 C++ table entries at
41d79706with an independent 96-digit CPU reference. All 70 input/output pairs match. With the2^-21relative band, 62 of those finite interior inputs need coverage; the conservative table covers them. The exact underflow tie and the runtime results are separate. Reproduction.I updated PortBridge #155 so an AMD or Intel volunteer can run the existing exact-bit hardware target on this head. A second adapter receipt would complement your RTX result.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-28T03:51:31Z.
Review: APPROVED
The precision change addresses my earlier review. The generated table covers the finite exp2 inputs near a half rounding midpoint, including
0x11c5. The revised fixture checks the neighboring inputs and exact bits, the rsqrt comparison uses the numeric bound, and the logarithm stays refused. I corrected my earlier wording about the exp limit: the current RDNA 2 guide lists 0.51 ULP. The correctly rounded exp target is within that limit. AMD RDNA 2 ISA, Microsoft Exp, Microsoft Rsq.At
41d79706plus maina166a753, Debug and Release /O2 each gave 12/12, including scalar/SDWA binary16, packed FMA and DPP8 regression checks. After #3270, Release /O2 on mainfdba6e15(treef1c049e3) passed all five focused cases:No skips. Windows 11 26200.9974, RTX 4060 Laptop 32.0.16.1656, SDK/DXC 10.0.26100.0, standalone replay without the debug layer.