Repository navigation
compute: lower binary16 VOP1 rcp, sqrt, exp and rsq - #3220
Conversation
969df10 to
d5c3738
Compare
There was a problem hiding this comment.
Rebasing exact head d5c37380 over main c1de64a5 conflicts in tests/native_f16_alu_check.py. Keep both interpreter arms in _exec: main's unaryBits.i32(i32 33, ...) for the merged scalar FMA tests (#3187) and this PR's unary.f32 arm for exp/log/sqrt/rsq.
The test helper also needs to survive the rebase. After resolving only the text conflict, native_fma_f16_ir fails with AttributeError: module 'native_f16_alu_check' has no attribute 'same_binary16' because native_fma_f16_check.py still calls it. Please preserve the existing exact same_binary16 comparator 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.
|
I tried to review git fetch origin main "pull/3220/head:review/3220"
git merge-tree --write-tree --name-only origin/main review/3220Could you merge or rebase onto current This reply was AI-assisted. |
KelboLive
left a comment
There was a problem hiding this comment.
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:
- The special-case handling for rsq/log (enforcing RDNA2's exact special results) correctly addresses that D3D Rsqrt doesn't guarantee -infinity for -0, so explicit selection based on source bits is used.
- SDWA support properly handles the unary instruction encoding difference (VOP1 vs VOP2).
- Test coverage includes all five new operations with various input patterns including edge cases (zeros, infinities, NaNs).
Verdict: Approve
d5c3738 to
ece04c8
Compare
|
Rebased on
On this head (RTX 5080, Release, Windows SDK 10.0.26100 DXC):
AI-assisted. |
|
For the rebased |
|
Rebased on @akimfomin, I checked that midpoint lane with a one-off readback through this branch's direct path on the RTX 5080 (Release):
So 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 AI-assisted. |
ece04c8 to
8027ad6
Compare
|
Thanks @Leyukaka. I independently recomputed that midpoint in a 60-digit CPU reference: rounding once gives |
8027ad6 to
eceb81a
Compare
|
Your |
|
Thanks for checking it on both heads. I would rather not special-case I counted over every finite binary16 input, against a 70-digit
Two options I can implement here:
I'll wait for the maintainer's choice. Either way it stays in this PR. AI-assisted. |
|
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 eceb81a on main fdb6faa, 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. |
pigalot
left a comment
There was a problem hiding this comment.
I reviewed eceb81af merged over main fdb6faaa. The merge is clean (tree 855194f4) and git diff --cached --check is clean. It also merges cleanly over 4d7265c0, which only touches startup tests. I first ran everything below on 8027ad6e over 50e4f763. The rebase to eceb81af changes only _exec and refusal_programs in tests/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).- The formal sign-off is still R0Y00's.
What I traced. binary16_unary joins half_alu in decode and the shared binary16 lowering in DirectCompute, so the MODE contract, clamp/omod and op_sel refusals carry over. The source half is widened with legacyF16ToF32, then:
- rcp is
fdiv 1.0, x; - sqrt/rsq/log/exp are dx.op 24/25/23/21;
- the result is rounded with
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;- log(1) →
+0.
-0 is 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_check in dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 4 -R 'native_(f16|sdwa_f16|fma_f16|cvt_f16|packed|pk|frexp|dpp|dot2)|native_direct_container' -E '_hardware'
python .agentic\scripts\with_gpu_lock.py ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '(native_(f16|sdwa_f16|fma_f16|cvt_f16|packed|pk|frexp|dpp|dot2)|native_direct_container).*_hardware'- Results: 30/30 non-hardware and 26/26 hardware tests pass in both Debug and Release
/O2, with no skips. - Negative control: on
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". - Interaction with #3240: #3240 lets SGPR and inline-constant sources reach these SDWA unary forms, and no test here covers them. I ran a one-off probe of all five ops with SGPR sources (WORD_0
-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 in dev and ran native_(f16_alu|sdwa_f16)_(ir|hardware):
| Mutant | Result |
|---|---|
rsq(±0) always +inf |
killed (IR and hardware) |
| sqrt and exp opcodes swapped | killed |
| log(±0) gives NaN | killed |
| rcp numerator 2.0 | killed |
log(+inf) gives +0 |
killed |
| VOP1 DPP16 accepted | killed (unsupported v_rcp_f16 DPP form expected, got "unsupported DPP arithmetic") |
VOP1 DPP8 accepted (dpp8 limited to VOP2) |
survives in native_(f16_alu|sdwa_f16|sdwa_f16_scalar|*dpp*)_(ir|controls) |
Restored and rebuilt afterwards; git diff --quiet is clean.
Non-blocking.
- The VOP1 DPP8 refusal has no test. The description says DPP8 stays refused on these forms, and the code refuses it. But a
vop1(op, 3, 233)entry next to the DPP16 refusals would pin it. - A stale comment in #3240's test.
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. - Tolerance. The one-ULP allowance for exp, rsq and log is documented, and so is the
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 Windows passes on eceb81af.
This reply was AI-assisted.
The direct compute path lowered the binary16 VOP2 ALU ops only. The three VOP1 ops now take the same route, plain and SDWA: the selected half is widened with legacyF16ToF32, the f32 op is applied (fdiv 1.0, dx.op Sqrt, dx.op Exp), and the result is rounded to binary16 with the nearest-even conversion v_cvt_f16_f32 uses, keeping D[31:16] or placing it per SDWA dst_sel. The same MODE contract applies. v_rsq_f16 and v_log_f16 stay refused. native_f16_alu and native_sdwa_f16 check the three ops against f16_packed_model: rcp and sqrt bit-exact, exp within one binary16 ULP, NaN by class. AI-assisted.
Extends the binary16 VOP1 lowering to v_rsq_f16 (dx.op Rsqrt) and v_log_f16 (dx.op Log, base 2), plain and SDWA, with the same widen, f32 op and nearest-even rounding. The special results are selected from the f16 input bits, because the D3D contract does not fix them: a negative nonzero input (including -inf) or NaN gives a quiet NaN, rsq(+-0) is +-inf, rsq(+inf) is +0, log(+-0) is -inf, log(+inf) is +inf and log(1) is +0. The f16 checks compare rsq and log within one binary16 ULP, the special results exactly and NaN by class. AI-assisted.
exp: the binary32 exp2 result is rounded to binary16 as before, then the 70 inputs whose exact result lies closer than 8 binary32 ULP to a binary16 rounding midpoint take their correctly rounded value from a table. 8 ULP covers the documented D3D exp relative error of 2^-21. tests/f16_exp_midpoints.py regenerates the table from a 90-digit reference and checks the C++ array. The exact tie exp2(-25) rounds to +0. exp results are now compared bit for bit. rsq: the documented 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. The test requires the correctly rounded value, and accepts the other neighbouring encoding only within 8 binary32 ULP of a midpoint. log: v_log_f16 is refused again, as on main. Its documented absolute error of 2^-21 on [0.5, 2] can exceed 0.51 binary16 ULP near 1. AI-assisted.
eceb81a to
41d7970
Compare
|
Done as you asked: bounded exp, rsq kept, log moved out. Pushed
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. |
|
I compared the exact 70 C++ table entries at 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. |
R0Y00
left a comment
There was a problem hiding this comment.
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 41d79706 plus main a166a753, Debug and Release /O2 each gave 12/12, including scalar/SDWA binary16, packed FMA and DPP8 regression checks. After #3270, Release /O2 on main fdba6e15 (tree f1c049e3) passed all five focused cases:
ctest --test-dir <build> --no-tests=error --output-on-failure -j 1 -R '^(native_f16_alu_(ir|hardware)|native_sdwa_f16_(ir|hardware)|native_direct_container)$'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.
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.