Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/amd-exec/sgemm.py # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / amd-exec / sgemm.py` 211 lines · 10527 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 """Test oracle only: arbitrary rational arithmetic, one RN-even binary32 rounding. 4 No host float operation implements FMA. Independent of the executor's fixed limbs. 5 """ 6 from fractions import Fraction 7 import itertools 8 import pathlib 9 import random 10 import struct 11 import subprocess 12 import sys 13 14 SHAPES = ((1, 1, 1), (1, 3, 3), (3, 1, 65), (3, 65, 3), 15 (64, 3, 1), (65, 1, 64), (1, 64, 65), (3, 65, 64), 16 (65, 65, 3), (65, 65, 65), (1, 1, 0), (3, 65, 0), 17 (0, 65, 3), (3, 0, 65)) 18 VALUES = (0, 0x80000000, 1, 0x80000001, 0x007fffff, 0x00800000, 19 0x3f800000, 0xbf800000, 0x3f800001, 0x3f7ffffe, 0x7f7fffff, 20 0xff7fffff, 0x7f800000, 0xff800000, 0x7fc12345, 0x7f812345, 21 0x00800001, 0x33000000, 0x4c000000, 0xcc000000) 22 FINITE = (0x3f800001, 0xbf800001, 0x3f7ffffe, 0x3eaaaaab, 0xbe4ccccd, 23 0x33000000, 1, 0x80000001, 0x4c000000, 0xcc000000, 0x3f000000) 24 25 26 def finite(bits): 27 exponent = (bits >> 23) & 255 28 significand = (bits & 0x7fffff) | (0x800000 if exponent else 0) 29 power = max(1, exponent) - 150 30 value = Fraction(significand) * Fraction(2) ** power 31 return -value if bits >> 31 else value 32 33 34 def round32(value): 35 if not value: 36 return 0 37 sign = 0x80000000 if value < 0 else 0 38 value = abs(value) 39 e = value.numerator.bit_length() - value.denominator.bit_length() 40 if value < Fraction(2) ** e: 41 e -= 1 42 quantum = max(-149, e - 23) 43 scaled = value / (Fraction(2) ** quantum) 44 q, r = divmod(scaled.numerator, scaled.denominator) 45 if 2 * r > scaled.denominator or (2 * r == scaled.denominator and q & 1): 46 q += 1 47 if q == 1 << 24: 48 q >>= 1 49 quantum += 1 50 if quantum > 104: 51 return sign | 0x7f800000 52 exponent = quantum + 150 if q >= 1 << 23 else 0 53 return sign | exponent << 23 | (q & 0x7fffff) 54 55 56 def fma(a, b, c): 57 ax, bx, cx = (x & 0x7fffffff for x in (a, b, c)) 58 if max(ax, bx, cx) > 0x7f800000: 59 return 0x7fc00000 60 product_sign = (a ^ b) & 0x80000000 61 if ax == 0x7f800000 or bx == 0x7f800000: 62 if not ax or not bx or (cx == 0x7f800000 and product_sign != c & 0x80000000): 63 return 0x7fc00000 64 return product_sign | 0x7f800000 65 if cx == 0x7f800000: 66 return c 67 if (not ax or not bx) and not cx: 68 return product_sign & c & 0x80000000 69 return round32(finite(a) * finite(b) + finite(c)) 70 71 72 def probe(exe): 73 triples = list(itertools.product(VALUES, repeat=3)) 74 rng = random.Random(94232) 75 triples += [tuple(rng.getrandbits(32) for _ in range(3)) for _ in range(10000)] 76 # Deep product cancellation, overflow canceled before rounding, and subnormal halfway ties. 77 triples += [(0x3f800001, 0x3f7ffffe, 0xbf800000), 78 (0x7f7fffff, 0x40000000, 0xff7fffff), 79 (1, 0x3f000000, 0), (3, 0x3f000000, 0), 80 (0x80000001, 0x3f000000, 0x80000000)] 81 data = b''.join(struct.pack('