Skip to content

Shaders: round v_exp_f16 once at its double-rounding midpoint - #3609

Open
Moon0755 wants to merge 1 commit into
Marvisak:mainfrom
Moon0755:fix/exp-f16-midpoint
Open

Moon0755 wants to merge 1 commit into
Marvisak:mainfrom
Moon0755:fix/exp-f16-midpoint

Conversation

@Moon0755

Copy link
Copy Markdown

On RDNA2 (full 65,536-input sweep on RX 6700 XT / gfx1031 and matching counts on gfx1036 in Leonida docs/design.md), v_exp_f16 is correctly rounded on every non-NaN input. Lowering through f32 Exp and narrowing back to f16 double-rounds on one finite input: 0x11c5 (0.0007042884826660156), where exact $2^x \approx 1.0004882947531497$ is strictly above the 0x3c00/0x3c01 midpoint (1.00048828125), while the nearest f32 (0x3f801000) lands on that exact midpoint and ties-to-even down to 0x3c00 instead of 0x3c01.

Changes

  • crates/victory-shader/src/emit/lower.rs (Lower::valu v_exp_f16, 521,301 bytes / +738 vs main, 2,987 bytes below the 524,288 limit):
    • After reading a = self.half_fsrc(inst, 0)? (post-abs/neg) and emitting unary(21, a), select widened 0x3f802000 (f16 0x3c01) when a == 0x3a38a000 (f16 0x11c5), before output modifiers and f16 narrowing.
  • crates/victory-shader/src/emit/valu_tests.rs:
    • Added v_exp_f16_rounds_the_exact_result_once testing 0x0000 -> 0x3c00, 0xbc00 -> 0x3800, and 0x11c5 -> 0x3c01.

Verification

  • Fork CI control (test only on main): https://github.com/Moon0755/VIctory/actions/runs/46063683100 (failure — 302 passed, 1 failed solely on v_exp_f16_rounds_the_exact_result_once: left=15360 (0x3c00), right=15361 (0x3c01))
  • Fork CI fix (devin/ci-exp-f16-rounding): https://github.com/Moon0755/VIctory/actions/runs/46063917357 (success — 303 passed / 0 failed, Clippy + diagnostics Clippy clean, asset policy: 191 files ok)
  • Local verification (3e6a6f5): v_exp_f16_rounds_the_exact_result_once (1/1, hardware + WARP), both Clippy configs, and cargo xtask policy (191 files ok) clean.
  • Not tested: gfx1013 native hardware measurement, non-midpoint host Exp ULP accuracy, NaN/denormal/MODE interactions (#3365), dxil.dll 1.7, game files. Synthetic tests only (AI-assisted).

— Devin & Neon

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I found a round-to-zero regression at the same midpoint on 3e6a6f5. With FP16 MODE set to toward zero, the translated shader returns 0x3c01 for v_exp_f16(0x11c5). Main returns 0x3c00 in that mode. In nearest-even mode, the PR correctly gives 0x3c01 where main gives 0x3c00.

The follow-up selects the f32 immediately above the midpoint (0x3f801001), still below the upper half. The existing narrow16 can then return 0x3c01 in nearest-even mode and 0x3c00 in round-to-zero mode. Follow-up 25ce885

All 32 readbacks passed across the two harness paths, both forced to app-local WARP 1.0.22. The fixture checks plain, abs, neg and clamp forms, with a zero-input control. A separate lowering check confirmed that the three output scales remain unsupported. All 37 VALU tests, strict shader Clippy and the asset policy passed.

Could you confirm the midpoint with explicit FP16 MODE 0 and 3 on the RX 6700 XT used for the native sweep? This report covers VIctory's translated DXIL on WARP. Please retain the Git author when taking the follow-up, or use Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into the PR commit. AI-assisted with Codex.

@oni-swr

oni-swr commented Nov 27, 2026

Copy link
Copy Markdown

Verification of #3609: PASS, with a file-size caveat.

Environment: native Windows 11, WARP only (no hardware GPU; VICTORY_WARP=1, VICTORY_D3D12_WARP=1, dxil.dll). AI-assisted (Claude).

  • Head 3e6a6f5 is unchanged. It merges onto main 63bcca4 with no conflicts.
  • cargo test -p victory-shader --lib:
    • base: 298 passed, 4 failed, 3 ignored.
    • head: 299 passed, 4 failed, 3 ignored.
    • The same 4 emit::synthetic_tests fail on both, so they are not caused by this PR. Three say "no pipeline for a wave of 32", which WARP cannot run. gathers_at_a_level_match_the_hardware fails on a lane mismatch.
    • The one extra pass is the new test.
  • Control: I applied only the new test to base. It fails there with v_exp_f16(0x11c5) = 0x3c00, want 0x3c01. It passes on head.
  • cargo clippy -p victory-shader --all-targets -- -D warnings on head: clean.
  • Exhaustive sweep: I ran all 65,536 f16 inputs through the shader on WARP, on base and on head. A temporary test did this, and I did not commit it.
    • Base and head differ at exactly one input, 0x11c5 (0x3c00 -> 0x3c01).
    • I compared head against a correctly rounded 2^x table computed with mpmath at 300-bit precision, for every non-NaN input. There are 0 mismatches.
    • The same mpmath analysis finds 0x11c5 is the only input where rounding the exact result via f32 differs from rounding it once.
    • So the fix is a hard-coded special case that covers the whole defect and leaves every other input unchanged.
    • It costs one compare and one select. The comment in the diff reads slightly oddly ("round the exact result up, not the f32 midpoint tie down"), but the code is correct.
  • Size caveat: lower.rs is 521,301 bytes alone, which is fine. It adds 738 bytes.

@Moon0755

Copy link
Copy Markdown
Author

@andrewpozdnakov7-jpg Thank you for catching the round-to-zero regression and sharing 25ce885!

I pushed a follow-up commit (c444dff, with Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>) that switches the 0x11c5 nudge to 0x3F80_1001 and adds v_exp_f16_midpoint_preserves_round_to_zero. Verified locally across all three D3D12 hosts (AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 2060 with Max-Q Design, and WARP: 32 readbacks, wrong=[0, 0]), cargo xtask policy (191 files ok), both Clippy targets, and PR CI (run 46070263747 — SUCCESS).

On your RX 6700 XT question: we do not have an RX 6700 XT locally (the 0x11c5 hardware sweep comes from the Leonida project's published RX 6700 XT data, which did not publish separate explicit MODE = 3 sweeps). Mathematically, 2^(0x11c5) lies strictly between 1 + 2^-11 (0x3f801000) and 1 + 2^-10 (0x3f802000), so round-to-zero of the exact result is strictly 0x3c00 and 0x3F80_1001 preserves both modes.

@oni-swr Thanks for the exhaustive 65,536-input mpmath verification! We also tightened the comment in lower.rs, bringing its size down from 521,301 B to 521,110 B (-191 B).

— Neon

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

@Moon0755 I checked the native sweep again and need to correct my earlier round-to-zero expectation. The published RX 6700 XT run includes keep16_rtz with the FP16 rounding field set to 3, and its harness reads back MODE before the sweep. It reports identical results under the two rounding settings for all seven FP16 transcendental instructions, including v_exp_f16. Native measurements · MODE check

#3617 adjusts the translator's rounding for those instructions. 4ccc63e combines it with your exact c444dff and keeps the midpoint correction. On app-local WARP, all 16 midpoint and control readbacks pass. The plain, abs and neg variants give 0x3c01 at the midpoint under both settings. The clamped variant and zero input give 0x3c00.

If you have access to a native gfx103x test, a direct run of the midpoint with the rounding field changed inside the kernel would complement the published sweep, which sets it in the kernel descriptor. The combined branch is available for review either way.

Please keep the Git author on a cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if the work is folded into another commit.

AI-assisted with OpenAI Codex.

@Moon0755

Copy link
Copy Markdown
Author

@andrewpozdnakov7-jpg Thank you for re-checking Leonida's keep16_rtz sweep on the RX 6700 XT!

Since RDNA2 returns 0x3c01 at 0x11c5 in both MODE = 0 and MODE = 3, I reverted c444dff (the 0x3F80_1001 nudge and the RTZ-expects-0x3c00 test) back to our original single-commit fix (d545701 using 0x3F80_2000), keeping only the tightened comment in lower.rs (521,168 B). This keeps #3609 consistent with the RX 6700 XT measurements under both modes without colliding with #3617.

Verified locally (cargo xtask policy — 191 files ok, both cargo clippy targets clean, v_exp_f16_rounds_the_exact_result_once ok).

— Neon

@Moon0755
Moon0755 force-pushed the fix/exp-f16-midpoint branch from c444dff to d545701 Compare November 27, 2026 22:34
@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I retested the revised d545701 on app-local WARP 1.0.22 with the DXIL validator. The original midpoint regression passes, and a new synthetic kernel confirms that 0x11c5 gives 0x3c01 on both sides of an in-shader s_round_mode change, in both directions.

The same kernel uses 0x2e00 as an ordinary value that rounds up. On d545701 the packed result is 0x3c453c44 for 0 → 3 and 0x3c443c45 for 3 → 0. Combined cef2f77 preserves your exact head and includes #3617 and #3620. It gives 0x3c453c45 in both directions and passes all eight packed readbacks. The original midpoint test, the 28 seven-op readbacks and the 62-input FP32-to-FP16 conversion control also passed. Strict shader Clippy passed in both configurations, as did the asset policy.

Please keep the Git author on a cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if this work is folded into another commit.

AI-assisted with OpenAI Codex.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I retested d545701 on app-local WARP 1.0.22, then merged it cleanly with #3617 at 8bfad4c on 63bcca4. Both revisions passed the 16 midpoint and control combinations. With VICTORY_WARP=1, the harness ran each combination twice on WARP, for 32 matching readbacks per revision.

The plain, abs and neg variants return 0x3c01 at the midpoint in both MODE 0 and MODE 3. Clamp and zero-input controls return 0x3c00. The VALU suite, including the temporary probe, passed 37 tests on d545701 and 38 on the merge. The additional #3617 regression ran in the latter suite.

The merge preserves both heads. Its lower.rs is 521,703 bytes, leaving 2,585 bytes under the limit. Verified integration branch 8f20866

AI-assisted with Codex.

@ColorMeBoom

Copy link
Copy Markdown

Hardware run on AMD, since the reports so far are WARP: RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003, Windows 11 26200), head d545701 on 63bcca4. The test fixture checks the hardware adapter and WARP in turn, and neither was skipped.

  • v_exp_f16_rounds_the_exact_result_once passes on both.
  • Full victory-shader --lib --features host: 303 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

@akimfomin

Copy link
Copy Markdown

PortBridge independently checked the binary16 midpoint. The CPU reference found exactly one nearest-f32 → nearest-f16 mismatch, 0x11c5 → 0x3c01. A separate hypothetical ±1 binary32 ULP test, adapted from Cai-piroska’s AnyXboxX #566 method, produced four other mismatches in each direction even with the midpoint patch. At 0x8dc6, for example, the injected +1 ULP gives 0x3c00 where the high-precision reference gives 0x3bff. Reproducible output.

That is neither a driver result nor a claim that the scoped fix is wrong. If there is already a hardware run planned on the latest head, could it add 0x8dc6 and 0x36b6 as diagnostic inputs? It would tell us whether the remaining sensitivity matters on that adapter.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I added the two requested inputs to the existing generated compute test directly on d545701. With required DXIL validation and app-local WARP 1.0.22 / Agility 1.620.2, 0x8dc6 returned 0x3bff and 0x36b6 returned 0x3d5a. Both match the correctly rounded FP16 reference. The existing 0x11c5 midpoint and two controls also passed. Strict all-features shader Clippy passed.

Test-only commit 742119f adds the inputs to v_exp_f16_rounds_the_exact_result_once. If the RX 5700 XT test is rerun, the same test can check both diagnostic outputs on the hardware adapter.

If you reuse the test, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com if folding it into another commit.

@ColorMeBoom

Copy link
Copy Markdown

@andrewpozdnakov7-jpg @akimfomin Reran v_exp_f16_rounds_the_exact_result_once with the two inputs (742119f, on d545701) on the RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003). It passes on the hardware adapter and on WARP, and neither was skipped:

  • 0x8dc6 gives 0x3bff and 0x36b6 gives 0x3d5a. Both are the correctly rounded FP16 results.
  • The 0x11c5 → 0x3c01 midpoint and the controls pass too.

So on this card the ±1 ULP sensitivity from the CPU model doesn't show up at those two inputs.

AI use: Claude Code (Claude Opus 5.5).

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

Thanks for rerunning 742119f on the RX 5700 XT. The hardware results for 0x8dc6 and 0x36b6 match the expected 0x3bff and 0x3d5a. The original midpoint 0x11c5 and the controls also passed on the hardware adapter and WARP. Hardware report

Moon0755, the two cases are in test-only commit 742119f on the current d545701 head. It is ready to cherry-pick if you want the extra coverage.

Please preserve the Git author if cherry-picking or rebasing, or keep this trailer if folding the test into another commit.

Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>

@akimfomin

Copy link
Copy Markdown

Cross-project hardware follow-through: PortBridge passed your 0x11c5 v_exp_f16 vector to Xbox2Windows. Leyukaka ran it on RTX 5080 at 8027ad6e: neighboring inputs matched, while 0x11c5 returned 0x3c00 instead of 0x3c01. WARP explicitly skipped on the receiving runtime. A separate 60-digit CPU check agreed with your rounding explanation. This confirms the same numerical boundary matters in the independent direct frontend. This result is for Xbox2Windows only.

@upipibi

upipibi commented Nov 28, 2026

Copy link
Copy Markdown

Hardware run, since this was WARP-only: RTX 3070 8 GB, driver 616.56, Windows 11, head d545701. v_exp_f16_rounds_the_exact_result_once passed 3 of 3 runs. run_cases runs it on the hardware adapter and on WARP in the same process, so the NVIDIA host agrees with the expected single rounding.

AI-assisted (Claude Code, Claude Opus 5.5 built this head and ran the test on this card).

@kilolimaVI

Copy link
Copy Markdown

AMD Radeon 780M (RDNA3) + WARP, Windows 11, head d545701.

  • Before (main 63bcca4 + this PR's test): fails on the hardware adapter. v_exp_f16(0x11c5) gives 0x3c00; want 0x3c01.
  • After: passes, 5 of 5 runs.
  • Full suite (victory-shader + victory-gpu --features diagnostics): 552 passed.

AI-assisted (Claude).

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.

7 participants