upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/mem_diff.py
diff options
context:
space:
mode:
authorYaBulochka <157796093+YaBulochka@users.noreply.github.com>2026-11-23 06:19:54 +0300
committerYaBulochka <157796093+YaBulochka@users.noreply.github.com>2026-11-23 06:19:54 +0300
commit79dfd24a30f78e725427cc57962a873d2904919b (patch)
treedf9b2e2ae776d0eb5a0c239982221843faf64453 /tools/mem_diff.py
parent03b09a086592a197db64d91655c6e65c30764d96 (diff)
Typed buffer loads keep a half NaN's payload, measured on gfx1036
mem_diff found one class of mismatch: a 16_FLOAT/16_16_FLOAT/16_16_16_16_FLOAT typed buffer load of a half NaN. Measured natively on RDNA2 (gfx1036) with the new tools/f16_widen_probe.py, wave32 and wave64, MUBUF and MTBUF, formats 13, 29 and 71: the hardware widens a half NaN to f32 as sign | 0x7F800000 | (payload << 13), keeping the sign and the quiet bit exactly as the half has them (0x7ddb -> 0x7fbb6000, 0x7fe6 -> 0x7ffcc000, 0xffb2 -> 0xfff64000). Gen.decode_format decoded every half with UnpackHalf2x16, whose NaN result is undefined and which the interpreter canonicalises to 0x7fc00000, so the payload was lost. It now uses UnpackHalf2x16 for the non-NaN halves (an exact widening) and builds the NaN word from the bits with integer ops. v_cvt_f32_f16 was measured too: it quiets an sNaN with the payload kept (0x7ffb6000), which the typed load does not. mem_diff's reference used struct's '<e', which keeps the payload only from Python 3.14; f16_to_f32 pins the measured rule so the fuzzer is version independent. tests/semantic/test_buffer_f16_widen_hw.py pins the ten measured words for MUBUF and MTBUF and formats 13/29/71; tests/test_f16_widen_probe.py covers the probe (models, fake oracle, CLI); mem_diff --self-test now pins the rule too.
Diffstat (limited to 'tools/mem_diff.py')
-rw-r--r--tools/mem_diff.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/tools/mem_diff.py b/tools/mem_diff.py
index c2282e7ef..762e101c2 100644
--- a/tools/mem_diff.py
+++ b/tools/mem_diff.py
@@ -125,6 +125,25 @@ def f32_round(q):
125 return min(cands, key=lambda b: (err(b), b & 1)) 125 return min(cands, key=lambda b: (err(b), b & 1))
126 126
127 127
128def f16_to_f32(h):
129 """16_FLOAT -> f32 bits. Exact for finite and infinite halves; a NaN shifts its 10-bit payload into the f32
130 mantissa and keeps the sign and the quiet bit exactly as the half has them (measured on gfx1036 with
131 tools/f16_widen_probe.py: 0x7ddb -> 0x7fbb6000, 0x7fe6 -> 0x7ffcc000). struct's '<e' is not used: before
132 Python 3.14 it drops the payload and returns a canonical NaN, from 3.14 it keeps it, and the hardware keeps it."""
133 h &= 0xFFFF
134 sign = (h >> 15) << 31
135 exp = (h >> 10) & 0x1F
136 frac = h & 0x03FF
137 if exp == 0x1F:
138 return sign | 0x7F800000 | (frac << 13)
139 if exp == 0 and frac:
140 b = frac.bit_length()
141 return sign | ((b + 102) << 23) | ((frac << (24 - b)) & 0x7FFFFF)
142 if exp == 0:
143 return sign
144 return sign | ((exp + 112) << 23) | (frac << 13)
145
146
128def decode_element(raw, widths, kind): 147def decode_element(raw, widths, kind):
129 """Decode one element (an int of sum(widths) bits) -> 4 result words; missing channels repeat (*c* mod *n*).""" 148 """Decode one element (an int of sum(widths) bits) -> 4 result words; missing channels repeat (*c* mod *n*)."""
130 from fractions import Fraction 149 from fractions import Fraction
@@ -145,8 +164,8 @@ def decode_element(raw, widths, kind):
145 out.append((v - (1 << w) if v >> (w - 1) else v) & MASK32) 164 out.append((v - (1 << w) if v >> (w - 1) else v) & MASK32)
146 elif w == 32: 165 elif w == 32:
147 out.append(v) 166 out.append(v)
148 else: # FLOAT 16 -> f32 bits (exact) 167 else: # FLOAT 16 -> f32 bits (exact, NaN payload kept)
149 out.append(struct.unpack("<I", struct.pack("<f", struct.unpack("<e", struct.pack("<H", v))[0]))[0]) 168 out.append(f16_to_f32(v))
150 n = len(out) 169 n = len(out)
151 while len(out) < 4: 170 while len(out) < 4:
152 out.append(out[len(out) % n]) 171 out.append(out[len(out) % n])
@@ -725,6 +744,10 @@ def self_test():
725 if status != "mismatch": 744 if status != "mismatch":
726 print("canary FAILED: a corrupted reference was not caught") 745 print("canary FAILED: a corrupted reference was not caught")
727 return False 746 return False
747 # The f16 reference keeps a NaN payload and its quiet bit, as gfx1036 does (tools/f16_widen_probe.py).
748 if decode_element(0x7DDB, [16], "FLOAT")[0] != 0x7FBB6000:
749 print("canary FAILED: the f16 NaN payload was not kept")
750 return False
728 print("canaries: match ok, mismatch detected") 751 print("canaries: match ok, mismatch detected")
729 return True 752 return True
730 753