Skip to content

compute: lower binary16 VOP1 v_log_f16 in the direct compute path - #3425

Open
Ammmae0x wants to merge 1 commit into
R0Y00:mainfrom
Ammmae0x:compute/direct-v-log-f16
Open

Ammmae0x wants to merge 1 commit into
R0Y00:mainfrom
Ammmae0x:compute/direct-v-log-f16

Conversation

@Ammmae0x

@Ammmae0x Ammmae0x commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3219
Refs #2935

Direct compute lowers the remaining binary16 VOP1 op from #3219, v_log_f16 (VOP1 0x57 / VOP3 0x1d7, plus SDWA), in the direct C++ compute path. PR #3220 lowered v_rcp_f16, v_sqrt_f16, v_exp_f16 and v_rsq_f16; this completes the issue.

Accuracy

The host Log (DXIL dx.op.unary.f32, opcode 23) follows the SM4 log contract: absolute error at most 2^-21 on [0.5, 2], relative error at most 2^-21 elsewhere. That error can cross a binary16 rounding midpoint only for inputs whose exact log2 lies within that window of a midpoint, so the emitter selects the correctly rounded half for exactly those inputs. The new tests/f16_log_midpoints.py generator screens all 65,536 binary16 inputs with float64 and verifies the near-midpoint candidates against a 90-digit Decimal reference; it finds 73 such inputs and no exact midpoint ties. Every other positive finite input keeps the host Log result rounded by the existing integer nearest-even conversion. The model (tests/f16_packed_model.py) rounds log2 with the same 90-digit reference, and an exhaustive cross-check confirmed the Decimal model agrees with exact-Log-then-round for every positive finite input except the 73 table entries, where the table value is the correctly rounded one.

Special cases are selected explicitly from the modified source bits: log2(1) is +0, log2(+-0) is -infinity, log2(+inf) is +infinity, and negative nonzero inputs (including -infinity) and NaNs give the canonical quiet NaN. VOP3 abs/neg, SDWA src0_sel/abs/neg/dst_sel WORD_0/WORD_1 and dst_unused UNUSED_PAD/UNUSED_PRESERVE are covered; the existing binary16 MODE contract (clamp/omod/DPP/high-half op_sel stay refused) is unchanged, and non-SDWA writes still preserve D[31:16].

Tests

Linux (Python 3.12.3; compile_dxil is Windows-only, so the IR dumper was built from src/gfx1013_compute_native.cpp, src/gfx1013_isa.cpp, src/guest_shader_container.cpp, src/gfx1013_compute_ir.cpp and tests/native_compute_ir_dump.cpp with g++ 13.3.0 and a local stub of d3d12.h and wrl/client.h, not in the tree):

python3 -B tests/native_f16_alu_check.py model
python3 -B tests/native_f16_alu_check.py ir <native_compute_ir_dump>
python3 -B tests/native_sdwa_f16_check.py ir <native_compute_ir_dump>
python3 -B tests/f16_log_midpoints.py
python3 -B tests/f16_exp_midpoints.py
python3 -B tests/form_coverage_check.py
python3 -B tests/docs_links_check.py
python3 -B tests/bidi_controls_check.py
  • native_f16_alu model: 676 results match f32-then-round; a sub/subrev swap and a zeroed upper half are rejected
  • native_f16_alu IR: 676 results match the model; 43 refusals carry the named diagnostic
  • native_sdwa_f16 IR: 46 results and 15 refusals
  • f16_log_midpoints: 73 entries match the C++ table
  • form coverage checks passed, docs_links_check passed (687 links), bidi_controls_check passed
  • 5/5 spot-mutations on /tmp copies failed as required (corrupted table entry, corrupted log2(1) model, corrupted -0 special case, corrupted table entry via IR, removed v_log_f16 admission guard) and were restored; /tmp cleaned
  • native_fma_f16, native_minmax_f16, native_dot2_f32_f16 and native_fract_f16 IR checks still pass against the same dumper (STRIDE raised 512 to 1024 for the larger case table)

native_f16_alu_hardware and native_sdwa_f16_hardware were not run on Linux. Windows verification (compile_dxil, native CTest) still pending.

Windows Verification (325219d)

Commit: 325219d284122a71429bbeae3ff3b14a076e8226 (Windows 11 build 26100.26100)
Adapter: NVIDIA GeForce RTX 4050 Laptop GPU (10de:28e1), driver 32.0.15.9186 (D3D12 debug layer not enabled by native_store_replay)
dev:
  cmake --build --preset dev --target compile_dxil native_compute_ir_dump native_store_replay
  $env:XBOX2WINDOWS_ADAPTER='NVIDIA'; ctest --preset dev --no-tests=error -R '^(native_f16_alu|native_sdwa_f16)_(ir|hardware)$' --output-on-failure
  -> 4/4 passed, 0 failed, 0 skipped (33.75s); extended f16 suite 15/15 passed (22.83s)
release:
  cmake --build --preset release --target compile_dxil native_compute_ir_dump native_store_replay
  $env:XBOX2WINDOWS_ADAPTER='NVIDIA'; ctest --preset release --no-tests=error -R '^(native_f16_alu|native_sdwa_f16)_(ir|hardware)$' -E "cpu_native_archive_" --output-on-failure
  -> 4/4 passed, 0 failed, 0 skipped (14.47s)
Python/Repo checks: native_f16_alu_check model, f16_log_midpoints (73), f16_exp_midpoints, form_coverage_check, docs_links_check, bidi_controls_check -> 6/6 passed
AI-assisted.

@Ammmae0x
Ammmae0x marked this pull request as ready for review November 29, 2026 01:14

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 325219d2 by diff only (not built or run). The rounding approach is correct; two test gaps.

Accuracy. I wrote a separate exhaustive check over every positive finite binary16 input (70-digit Decimal log2, RNE of both ends of the SM4 window: absolute 2^-21 on [0.5, 2], relative 2^-21 elsewhere):

  • 73 inputs can round differently somewhere inside the window. They are exactly the keys of log_f16_midpoints, and all 73 results equal the correctly rounded half.
  • With a correctly rounded f32 log2, no input double-rounds to the wrong half (unlike exp 0x11c5 in #3220).
  • The special-value selects give NaN, +inf, negative values including -inf and ±0 the expected results from the modified bits; 1 gives +0 through the table.

1. The IR check does not exercise the table. The interpreter models Log as math.log2 rounded to f32 (tests/native_f16_alu_check.py:643). By reading, not run: my check shows a correctly rounded f32 log2 already rounds all 73 table inputs correctly, so if the for (const auto &entry : log_f16_midpoints) select loop is deleted from gfx1013_direct_compute.inl, model still passes (check_log_cpp_table only reads the array text) and ir should too. Only a GPU whose Log errs near a midpoint would catch it. One fix: for table inputs, move the interpreter's Log result across the midpoint, still inside the window. That is what the Rsqrt -0 model does for rsq.

2. The SDWA refusal control was deleted instead of moved. sdwa(0x57, WORD_0, PAD, WORD_0, unary=True) was the only unary op checked for unimplemented direct SDWA operand profile. Nothing else at this head checks that diagnostic for a unary op. Pointing it at an op that is still refused, such as v_sin_f16 (0x60), would keep it covered. The plain-form fixture already does this at :422.

Minor: the magnitude ugt 31744 select is redundant with the source ugt 31744 select. The source == 31744 select is redundant on a conforming host. By reading, deleting either one leaves every check passing.

CI. On PRs the Linux job runs native_f16_alu_check.py model. The Windows job runs ctest --preset pr, whose filter includes no native_f16_alu_* or native_sdwa_f16_* tests. The ir and _hardware results come only from the Windows receipt in the PR body.

Approving 325219d2; 1 and 2 can be follow-ups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

direct compute: lower binary16 VOP1 rcp, sqrt, exp, rsq and log

2 participants