Skip to content

rdna2spirv: round f16 typed-buffer stores toward zero and saturate at 65504 (SHADER-395) - #2262

Closed
Youssefrah16 wants to merge 1 commit into
ItsNotRexed:mainfrom
Youssefrah16:fix-shader395-f16-store-rtz
Closed

Youssefrah16 wants to merge 1 commit into
ItsNotRexed:mainfrom
Youssefrah16:fix-shader395-f16-store-rtz

Conversation

@Youssefrah16

Copy link
Copy Markdown

Fix SHADER-395 ("f16 typed-buffer stores round toward zero on RDNA2 and gfx1150, and saturate at 65504, where the translator rounds to nearest even and overflows to infinity: switch the f16 store narrowing to the measured rule, keeping d16 loads' rule separate"), claimed in #2078 (#issuecomment-7082017955).

Summary

  • tools/rdna2spirv.py (Gen.encode_format, line 1304): Switch kind == "f16" typed-buffer store format narrowing (16_FLOAT, 16_16_FLOAT, 16_16_16_16_FLOAT used by buffer_store_format_* and tbuffer_store_format_*) from self.f16_bits(self.tof(fcomps[c])) (round-to-nearest-even, |x| >= 65520 -> ±inf) to self.f16_bits(self.tof(fcomps[c]), rtz=True) (round toward zero, finite overflow clamped to ±65504 (0x7bff / 0xfbff), ±inf -> ±0x7c00, quiet NaNs and f16 subnormals preserved).
  • d16 loads kept separate: buffer_load_format_d16_*, tbuffer_load_format_d16_*, and image_* ... d16 continue using Gen.f16_bits(..., rtz=False) / Gen.d16_norm_f16.
  • tests/test_translate.py: Update test_encode_format_f16_uses_f16_bits_rtz to cover odd ties (0x3f803000 -> 0x3c01, not RTE 0x3c02), finite overflow saturation (+65520.0, -65536.0, ±FLT_MAX -> 0x7bff / 0xfbff), +inf -> 0x7c00, and subnormal underflow (0x337fffff -> 0x0000).
  • docs/design.md: Document the hardware-measured 16_FLOAT typed-buffer store RTZ + ±65504 saturation rule in the d16 and f16 format narrowing contract row.

Native RDNA2 Hardware Measurement (gfx1035)

In addition to the gfx1036 (@CLHelper) and gfx1150 (@AlxcBTW) measurements reported in #1902, we ran 19 f32 inputs across both MUBUF buffer_store_format_x and MTBUF tbuffer_store_format_x (format:[BUF_FMT_16_FLOAT], format 13) in both wave32 and wave64 (76 native RDNA2 kernels) on an AMD Radeon 680M (gfx1035) via tools/hw_oracle with the destination dword pre-filled to 0xdead0000:

Input f32 Description gfx1035 MUBUF & MTBUF (wave32 & wave64) Gen.f16_bits(..., rtz=True) Old rtz=False
0x3f801000 1 + 2^-11 (even tie) 0xdead3c00 0x3c00 0x3c00
0x3f803000 1 + 3·2^-11 (odd tie) 0xdead3c01 0x3c01 0x3c02 (diverged)
0x3f803fff Just below 0x3c02 0xdead3c01 0x3c01 0x3c02 (diverged)
0xbf803000 -(1 + 3·2^-11) (neg odd tie) 0xdeadbc01 0xbc01 0xbc02 (diverged)
0x477fe000 65504.0 (max f16) 0xdead7bff 0x7bff 0x7bff
0x477ff000 65520.0 (tie above 65504) 0xdead7bff 0x7bff 0x7c00 (diverged)
0x47800000 65536.0 (finite overflow) 0xdead7bff 0x7bff 0x7c00 (diverged)
0xc7800000 -65536.0 (neg finite overflow) 0xdeadfbff 0xfbff 0xfc00 (diverged)
0x7f7fffff +FLT_MAX 0xdead7bff 0x7bff 0x7c00 (diverged)
0xff7fffff -FLT_MAX 0xdeadfbff 0xfbff 0xfc00 (diverged)
0x7f800000 +inf 0xdead7c00 0x7c00 0x7c00
0xff800000 -inf 0xdeadfc00 0xfc00 0xfc00
0x7fc00000 +qNaN 0xdead7e00 0x7e00 0x7e00
0xffc00000 -qNaN 0xdeadfe00 0xfe00 0xfe00
0x7f800001 +sNaN 0xdead7c01 0x7e00 (qNaN) 0x7e00 (qNaN)
0x38000000 2^-15 (f16 subnormal) 0xdead0200 0x0200 0x0200
0x33800000 2^-24 (min f16 subnormal) 0xdead0001 0x0001 0x0001
0x337fffff Just below 2^-24 0xdead0000 0x0000 0x0001 (diverged)
0x80000000 -0.0 0xdead8000 0x8000 0x8000

Testing (AI-assisted)

  • python -m pytest tests/test_translate.py -k "encode_format_f16 or buffer_format_f16" -v (7/7 passed)
  • python -I tests/test_d16_pack.py (3/3 passed)
  • python -I tests/semantic/test_tbuffer_store.py (4/4 passed)
  • python -I tests/semantic/test_buffer_load_format.py (22/22 passed)
  • Dual-GPU Vulkan verification via scripts/run_gpu_tests.py --filter store on Windows 11:
    • NVIDIA GeForce RTX 4060 Laptop GPU (driver 595.71.0)
    • AMD Radeon 680M (gfx1035, RDNA2) (driver 2.0.317 / HIP 60443483)

@Youssefrah16

Copy link
Copy Markdown
Author

Closing in favor of @Vonsek's #2216 (SHADER-358, which SHADER-395 duplicated and which also covers 32_FLOAT d16 buffer loads and the sNaN payload rule). Our 76-kernel gfx1035 (wave32 & wave64, MUBUF & MTBUF) measurement independently confirms #2216's findings. AI-assisted.

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.

2 participants