Repository navigation
Translator: hardware NaN rules for float min/max/med3, and v_fract below 1.0 (SHADER-45) - #494
Conversation
|
Tested 3de414d on two GPUs, Linux 7.3.5 (CachyOS): pass.
Ran |
IAmNotSiob
left a comment
There was a problem hiding this comment.
Ran the two new GPU tests on another vendor, since you only had AMD. RTX 6000 Ada, NVIDIA 615.71.09, Linux, PR head:
PASS ps_fnan_med3 4096/4096 pixels exact
PASS ps_fnan_fract 4096/4096 pixels exact
Negative control, with tools/rdna2spirv.py from main. On NVIDIA this matters more than on AMD, because fract is wrong there too, not just med3:
FAIL ps_fnan_med3 got 0x40e00000 0x40600000 0xc0200000 0x40600000, want 0x40a00000 0xc0200000 0xc0200000 0x40600000
FAIL ps_fnan_fract got 0x3f800000 0x3c00 0xc0200000 0xc0200000, want 0x3f7fffff 0x3bff 0xc0200000 0xc0200000
So NVIDIA returns a correctly rounded Fract (1.0 for a tiny negative input) and the plain-median med3 with NaN. Both differ from RDNA2, and this PR fixes both. That answers your "elsewhere it turns implementation-dependent results into specified ones" with a concrete case.
tests/semantic/test_float_nan_fract.py: 19/19 OK locally. One inline edge case below, not blocking. LGTM. AI-assisted review (Claude Code).
| self.f16_out(a[0], r, dhi) # GFX10: f16 ops keep the other half | ||
| # 0x3c00 -> 0x3bff only when 0x3c00 is Fract's own rounding: after mul:2 fract(0.5) is a true 1.0. | ||
| omod = any(m in ("mul:2", "mul:4", "div:2") for m in mods) | ||
| self.f16_out(a[0], r, dhi, fract=fn == "Fract" and not omod) # GFX10: f16 ops keep the other half |
There was a problem hiding this comment.
Edge case, not blocking: with div:2 the order differs from hardware. On RDNA2 the f16 fract result is rounded to f16 first (so 0x3c00 → 0x3bff) and omod is applied after. Here omod runs on the f32 Fract and only then narrows. For x = -1e-4, hardware gives 0x3bff × 0.5 = 0x37ff, but this path gives f16(0.9999 × 0.5) = 0x3800. mul:2/mul:4 are fine, because the not omod guard covers the true-1.0 case you mention. Probably vanishingly rare in real shaders (v_fract_f16 … div:2). If you want it exact, narrow and fix up before clamp_mods, then apply omod to the f16 value. Otherwise a one-line note in the design row is enough.
|
Tested
|
|
Tested 3de414d on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200: pass.
Tools: llvm-mc 21.1.8 (WSL), SPIRV-Tools v2026.4. |
|
Rebased onto current main (91b36cf, after this round's merges); the branch is now 7c59de8. Thanks @esteban-dcp and @dcastell1 for the NVIDIA and RADV runs on 3de414d. One thing changed besides conflict resolution.
So this branch now sorts that reference with -0 below +0 and makes Re-run on AMD Radeon RX 6800, Windows 11, driver 26.11.2: AI-assisted (Claude Code). |
3de414d to
7c59de8
Compare
|
Follow-up to my comment above, to back one sentence in it with a run. I said the AMD Vulkan driver already returned the hardware words for the signed-zero
All 4096 pixels agree in each run. So the translator's med3 shape gives the hardware result for signed zeros on this driver before and after this PR; the test-reference change only brings the Python model in line with that. AI-assisted (Claude Code). |
|
Re-tested the rebased head 7c59de8 on the same two GPUs, Linux 7.3.5 (CachyOS): pass.
|
|
Tested
No game files involved. AI-assisted (Claude Code). |
|
FYI for the rebase: #627 (TEST-84, merged) added AI-assisted (Claude Code). |
7c59de8 to
acabed9
Compare
Measured on an AMD Radeon RX 6800 (gfx1030) with the hw-oracle tool from the AnyPS5 project, all four IEEE/DX10_CLAMP combinations, wave32 and wave64: - v_min/v_max f32/f16, each half of v_pk_min/max_f16, and v_min3/v_max3 now emit NMin/NMax (one NaN operand gives the other) instead of FMin/FMax. - v_med3_f32/_f16: FClamp(z, NMin(x,y), NMax(x,y)), and with OpIsNan of each operand that can be a NaN, select NMin(NMin(x,y), z) (hardware: min3 when any operand is NaN). Non-NaN constants get no check; no select when none. - v_fract_f32: Fract, then 1.0 -> 0x3f7fffff by compare and select (the NaN of fract(+-inf) is kept). v_fract_f16: the same, then a packed 0x3c00 -> 0x3bff (not after an output multiplier). Other FMin/FMax/FClamp uses (clamp bit, LOD and conversion clamps) are unchanged. GPU fixtures ps_fnan_med3 and ps_fnan_fract read their inputs from memory; on the RX 6800 the two med3 words differed before this change. tests/semantic/test_float_nan_fract.py runs the measured rows; both interpreters gain NMin/NMax, and spirv_mini_interp OpIsNan and exact Fract. tests/semantic/test_alu_med3.py (merged after this branch was cut): its float reference used Python min/max, which return the first argument for equal zeros. Hardware orders -0 below +0 (RX 6800: med3(-0, +0, 1.0) = +0, med3(+0, -0, -1.0) = -0 in every operand order), so the reference now sorts with that order, and spirv_mini_interp's FMin/FMax/FClamp order zeros the same way as its NMin/NMax. Rebased onto 8df277f. Two upstream tests were written against the old behaviour and are updated: tests/semantic/test_valu_f32_min3_max3_mad.py (its min3/max3 reference used first-operand order for mixed zeros, and its negative control replaced FMin/FMax; now -0 < +0, as measured on the RX 6800, and the control swaps NMin/NMax) and tests/semantic/test_valu_f16_round.py (it asserted that the interpreter rejects Fract; it now asserts the value). Upstream had added an OpIsNan/OpIsInf branch to spirv_mini_interp, so this commit's own OpIsNan branch is dropped (it shadowed upstream's). docs/design.md notes that an output modifier on v_fract_f16 is applied before narrowing to f16, an order not measured on hardware. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Rebased onto current main (067d3e8); the branch is now 1293cc2. Thanks @IAmNotSiob for the review and the NVIDIA negative control, and @kotorvr for the pointer to #627's tests. What changed in the rebase
Tested on AMD Radeon RX 6800, Windows 11, driver 26.11.2:
Corpus (my own copy of the 1.0.2.1 package, counts only):
One thing I checked and ruled out: AI-assisted (Claude Code). |
acabed9 to
1293cc2
Compare
|
Tested 1293cc2 on two GPUs, Linux 7.3.5 (CachyOS): pass. This is the head that merged; posting for the record.
|
What and why
SHADER-45 from #374 (claimed in #156): float
v_min/v_max/v_min3/v_max3/v_med3with NaN operands, andv_fractjust below zero, measured on RDNA2 hardware and compared with what the translator emits.Hardware measurement. AMD Radeon RX 6800 (gfx1030, not the console's gfx1013), the instructions run as raw compute kernels (the hw-oracle tool from the AnyPS5 project): 537 input rows, 7 kernels, IEEE mode on and off × DX10_CLAMP on and off, wave32 and wave64 (56 runs). wave32 and wave64 agree in every word, and for quiet NaNs the four modes agree too.
v_min_f32/v_max_f32,_f16, each half ofv_pk_min/max_f16v_min3/v_max3v_med3_f32/_f16v_fract_f320x3f7fffff; f16:0x3bff. fract(-0) = +0, fract(±inf) =0xffc00000, NaN passes throughRows that decide it (hex bits): min/max(
7fc10001,c0200000) =c0200000; min/max(c0200000,ffc20002) =c0200000; med3(7fc10001, 7.0, 5.0) = 5.0; med3(7fc10001, 3.5, -2.5) = -2.5; med3(-0, +0, -0) = -0; fract(-1e-10) =3f7fffff, where the hardware's ownv_sub(x, v_floor(x))gives3f800000.What the translator emitted.
FMin/FMax(GLSL.std.450 leaves the result for a NaN operand to the implementation),FClamp(z, FMin(x, y), FMax(x, y))for med3, and a bareFract, which is specified as correctly rounded, so 1.0 for a tiny negative input.What it emits now
NMin/NMax, whose result for one NaN operand is specified as the other operand.NMin/NMax, plusOpIsNanof each operand that can be a NaN (none for an inline constant that isn't one), selectingNMin(NMin(x, y), z). Switching to the N ops alone does not give the hardware result here: med3(NaN, 7, 5) would be 7.v_fract_f32:Fract, then a compare with 1.0 and a select of0x3f7fffff(notFMin, so fract(±inf) stays a NaN).v_fract_f16: the same, then a packed0x3c00becomes0x3bff.How much of this is observable. I ran the same rows through the unmodified translator on the same GPU under Vulkan (inputs read from memory at run time, so nothing is folded). On the AMD driver only the two med3 rows differed from hardware;
FMin/FMaxwith a NaN,Fractbelow zero and the f16 fract already returned the hardware words, presumably because the driver lowers them to the same instructions. So on AMD this PR changes two results; elsewhere it turns implementation-dependent results into specified ones. I could not test another vendor's driver, and I have no game files, so I could not count how many shaders feed a NaN into these instructions.Not changed, and why
clampbit. Hardware gives +0 with DX10_CLAMP=1 and the NaN with DX10_CLAMP=0 (v_add_f32 … clampof a NaN:00000000/7fc10001).NClampwould match DX10_CLAMP=1 only, and the translator does not read the MODE register, so I leftFClampinclamp_modsalone. Question for the maintainers: is DX10_CLAMP=1 a safe assumption for every shader in the corpus? If so,NClampthere is a one-word follow-up.SignedZeroInfNanPreserve. Without that execution mode Vulkan does not strictly guarantee NaN or signed-zero behaviour, so these rules hold where the driver keeps them. Declaring it needs a device feature and anindex.jsonentry; I have not done that here.v_med3_f32 … clamp: hardware does clamp (med3(2.5, 3.5, 1.5) clamp = 1.0). That is Apply output scaling and clamping to v_med3_f32 #350's fix, not repeated here. This PR and Apply output scaling and clamping to v_med3_f32 #350 touch neighbouring lines of the f32 med3 path; whichever lands second needs a small rebase, which I will do if it is this one.One more observation from the Vulkan run, not acted on here: on the AMD driver
PackHalf2x16rounds toward zero (3f7ff800packs to3bff, nearest is3c00), which bears on the f16 rounding item already claimed in #374.Tests added
runtime/test/shaders/ps_fnan_med3.asmandps_fnan_fract.asm, incheck.pyandscripts/run_gpu_tests.py, with their inputs seeded in the heap. Expected words are the hardware words.tests/semantic/test_float_nan_fract.py: the measured rows through the translator and the interpreter.spirv_mini_interp.pygainsNMin/NMax,OpIsNanand an exactFract;tests/spirv_interp.pygainsNMin/NMax.Tested
AMD Radeon RX 6800, Windows 11, driver 26.11.2, branch at 3de414d on main 4abd545:
scripts/run_gpu_tests.pyruntime/test/shaders/check.pypython -I -m unittest discover -s tests/semanticpython -m pytest testsps_fnan_med3on the GPU with main's translator40e00000 40600000 c0200000 40600000, want40a00000 c0200000 c0200000 40600000ps_fnan_fracton the GPU with main's translatorNone of the existing test shaders' SPIR-V changes. Not tested: any GPU other than the RX 6800. A run on an NVIDIA or Intel driver, with main's translator and with this branch, would show whether the
NMin/NMaxand fract changes are observable there.AI-assisted (Claude Code): measured and specified by one AI agent, implemented by a second, then reviewed and re-run on the hardware above.
Checklist
docs/design.md, with how it was confirmed. (New row "Float min/max/med3 with NaN,v_fract".)🤖 Generated with Claude Code