Skip to content

gpu tests: f64 reciprocal, inverse square root and square root on the GPU - #5268

Open
gertify wants to merge 1 commit into
ItsNotRexed:mainfrom
gertify:fix-contract-rcpf64
Open

gertify wants to merge 1 commit into
ItsNotRexed:mainfrom
gertify:fix-contract-rcpf64

Conversation

@gertify

@gertify gertify commented Nov 23, 2026

Copy link
Copy Markdown

Adds runtime/test/shaders/cs_f64_trans.asm, a hand-written compute fixture for the docs/design.md row "v_rcp_f64, v_rsq_f64, v_sqrt_f64", run and judged by scripts/run_gpu_tests.py.

The fixture computes rcp(4.0) = 0.25, rsq(4.0) = 0.5, sqrt(2^52 + 2^27 + 1) = 67108865 and -sqrt(-4.0) div:2 = 1.0, storing each double low word first. The operand 2^52 + 2^27 + 1 = (2^26 + 1)^2 needs 53 significand bits, so no binary32 value can hold it and its square root is exact only in binary64; the last case carries the VOP3 neg input modifier, applied to the double operand, and the div:2 output modifier.

Tested with scripts/run_gpu_tests.py --filter cs_f64_trans on the AMD RADV RAPHAEL_MENDOCINO (Ryzen 9 9950X3D iGPU) and the NVIDIA GeForce RTX 5080 (driver 620.70.06): PASS cs_f64_trans 0x0 0x3fd00000 0x0 0x3fe00000 0x4000000 0x41900000 0x0 0x3ff00000, 1/1 passed, exit 0. The branch changes no translator file, so the tested bytes are main's translator. Negative control, with tools/rdna2spirv.py patched so v_rsq_f64 emits Sqrt and v_sqrt_f64 emits InverseSqrt, fails on both devices: rsq(4.0) comes back as 2.0 and sqrt(2^52 + 2^27 + 1) as about 1.49e-8.

The row now records this GPU run. tests/test_f64_transcendentals.py stays the CPU check of the same lowering.

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted. Tested 010363c on Windows 11 with the NVIDIA GeForce RTX 4090 and AMD Radeon(TM) Graphics. The new GPU test passes on both adapters.

I set LEONIDA_VK_DEVICE to 4090 and then to Radeon. I ran this command for each adapter.

py -3.14 -I scripts/run_gpu_tests.py --build E:/Leonida-review-work-build --work E:/Leonida-review-work-build/gpu --filter cs_f64_trans --require-pass

The shader passes SPIR-V validation. Both devices return the same eight words: 0 1070596096 0 1071644672 67108864 1099956224 0 1072693248. They encode 0.25, 0.5, 67108865 and 1.0. The harness reports subgroup 32..32 on NVIDIA and 32..64 on AMD.

With the new fixture and the runner from parent 3faac55, the same command translates the shader and exits 2 because the parent has no test case for it. The two tests in tests/test_f64_transcendentals.py also pass on the head.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 010363c947536f8a2331cb2cad553e68b4f50b3d on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

At 010363c9, cs_f64_trans passed on a Linux GeForce RTX 5090 (driver 595.99.02), returning 0.25, 0.5, 67,108,865 and 1.0 exactly.

I checked the square-root fixture's sensitivity to lost input precision. Rounding its integer operand (2^26 + 1)^2 to binary32 gives exactly 2^52. In a temporary shader copy I substituted that pre-rounded value as a binary64 literal, leaving the square-root instruction and expected output unchanged. The third result dropped to 67,108,864: its low word changed from 0x04000000 to zero, and all seven other output words matched. Both modules passed SPIR-V validation and the runner rejected the modified readback.

AI-assisted verification.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Head 010363c94. git diff --check clean. New GPU fixture plus one dispatch branch; no translator change.

  • Build: vcvars64 + Ninja, -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF -DLEONIDA_REQUIRE_VULKAN=ON -DVulkan_INCLUDE_DIR=.../1.4.363.0/Include -DVulkan_LIBRARY=.../vulkan-1.lib -DVulkan_GLSLANG_VALIDATOR_EXECUTABLE=.../glslangValidator.exe; cmake --build <build> --target vk_compute_test -> exit 0.
  • python -I scripts/run_gpu_tests.py --build <build> --filter cs_f64_trans (LEONIDA_VK_DEVICE='RTX 5070 Ti', Vulkan 1.4.363, LLVM_MC=wsl llvm-mc-21):
    PASS cs_f64_trans 0x0 0x3fd00000 0x0 0x3fe00000 0x4000000 0x41900000 0x0 0x3ff00000, 1/1 passed, exit 0. That reproduces the PR's two-device result on a third device (RTX 5070 Ti).
  • spirv-dis of the produced cs_f64_trans.spv: OpFDiv %double, InverseSqrt %double, Sqrt %double, and OpFNegate %double -> Sqrt -> OpFMul (div:2). All four intended forms are exercised, not just transcribed. The 0x4330000008000001 operand (2^52 + 2^27 + 1) and the exact 67108865 result are discriminating: no f32 round-trip can produce them.
  • Expected words match the .asm header (0 1070596096 0 1071644672 67108864 1099956224 0 1072693248) and the judge_cs_dump(..., hexwords=True) list.

Verdict: merge. Note runtime/test/shaders/check.py has no cs_f64_trans entry, so only run_gpu_tests.py judges it; that matches most cs_* fixtures and the PR's stated scope.

@sovljanskii

Copy link
Copy Markdown
Contributor

AI-assisted. Independent run on an Apple M4 (macOS, MoltenVK 1.4.2) at 010363c9, which already contains main 3faac557. The runtime was built from main. This device has no shaderFloat64, so the run covers the skip path and the translation only, not the double arithmetic.

Skip path.

  • --filter cs_f64_trans gives SKIP (no shaderFloat64 on this device), 0/1 passed and 1 skipped.
  • vk_compute_test cs_f64_trans.spv 1 dump called directly prints unsupported: ... uses Float64 and the device has no shaderFloat64. That is the exit-77 path the new branch keys on.

Translation. The translated module passes spirv-val. It declares Float64 and computes its results with:

  • one OpFDiv %double;
  • one InverseSqrt on a double;
  • two Sqrt on doubles.

Full suite. 152/218 passed and 59 skipped, against main's 152/217 passed and 58 skipped. Compared row by row, the only difference is the new cs_f64_trans SKIP row.

Unit tests. tests/test_run_gpu_tests.py (41) and tests/test_f64_transcendentals.py (2) are OK on Python 3.12, 3.14 and 3.15.

@x2Tweek x2Tweek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI-assisted.

Verified cs_f64_trans on RTX 5090: v_rcp_f64/v_rsq_f64/v_sqrt_f64 on binary64 operands produce correct results, including the 53-bit significand operand (2^52 + 2^27 + 1) that no binary32 value can hold. 1/1 passed, exit 0.

Approved.

🤖 Generated with Claude Code

@milas6669

Copy link
Copy Markdown

Tested 010363c9 on Windows 11 with an NVIDIA GeForce RTX 3070 (driver 32.0.16.2124), MSVC 14.44, llvm-mc 21:

python -I scripts/run_gpu_tests.py --build runtime/build --filter f64 --require-pass
=== rdna2spirv.py ===
4 shaders: 4 translated, 4 pass spirv-val

=== GPU TEST RESULTS ===
  PASS  cs_f64_add            0x0 0x40040000
  PASS  cs_f64_trans          0x0 0x3fd00000 0x0 0x3fe00000 0x4000000 0x41900000 0x0 0x3ff00000
  PASS  ps_f64_mods           4096/4096 pixels exact
  PASS  ps_f64_round          4096/4096 pixels exact

4/4 passed

Exit 0, 3 s. The words are the fixture's stated expectations, low word first: 0x3FD0000000000000 = rcp(4.0) = 0.25, 0x3FE0000000000000 = rsq(4.0) = 0.5, 0x4190000000400000 = sqrt(2^52 + 2^27 + 1) = 67108865, 0x3FF0000000000000 = -sqrt(-4.0) div:2 = 1.0. So the operand that needs 53 significand bits survives the round trip on this GPU.

The two neighbouring tests in the same filter are unchanged and still exact on the RTX 3070, and tests/test_run_gpu_tests.py and tests/test_shader_checks.py pass under python -I.

This is the f64 row on Ampere; the PR's hardware note asked for a GPU with working binary64, and this one agrees with the fixture.

AI-assisted. No game data.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 010363c9 on RTX 5080, NVIDIA 615.78.08, Linux: pass. This is a second driver on the same GPU model; the PR body has 620.70.06.

python -I scripts/run_gpu_tests.py --build <linux build> --filter f64 --require-pass, with the programs built from this tree (-DLEONIDA_VULKAN_HEADERS_FETCH=ON) and llvm-mc 23.1.3:

5 shaders: 5 translated, 5 pass spirv-val
PASS  cs_f64_add            0x0 0x40040000
PASS  cs_f64_trans          0x0 0x3fd00000 0x0 0x3fe00000 0x4000000 0x41900000 0x0 0x3ff00000
PASS  ps_f64_mods           4096/4096 pixels exact
PASS  ps_f64_round          4096/4096 pixels exact

I got the same 4/4 with the SHADER-865 literal fixes merged on top: #2863, #3642, #3791, and #2863 + #3791 together. I also got it with this PR merged onto the new main 561b65a8, which contains #2863. That merge conflicts only in the docs/design.md row. Note that the fixture feeds every operand through register pairs, so it is not a check of the SHADER-865 literal path. I ran that path separately with a synthetic literal fixture on the PRs that change it.

Merge-order note. I merged #4371 (SHADER-721, open) on top. It drops the f64 omod after gfx1036 measurements, and with it cs_f64_trans fails: word 7 becomes 0x40000000 (2.0) instead of 0x3ff00000. The cause is the div:2 on the last case. #4371 and #4293 also record that v_sqrt_f64 on gfx1036 is not correctly rounded, for example sqrt(4.0) = 0x4000000004000000. So words 4..7 pin the translator's IEEE result, not an RDNA2 hardware word. Whichever of the two lands second needs to update that case. Dropping div:2 from it would leave the test independent of #4371.

AI-assisted (Claude Code). Synthetic programs only, no game files.

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.

8 participants