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
CUDApp / om / q38-head / envelope-check.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | # Independent external numerical verification; not a runtime dependency. |
| 3 | import math |
| 4 | import random |
| 5 | import struct |
| 6 | import subprocess |
| 7 | import sys |
| 8 | from fractions import Fraction |
| 9 | |
| 10 | |
| 11 | def fbits(f): |
| 12 | return struct.unpack('<I', struct.pack('<f', f))[0] |
| 13 | |
| 14 | |
| 15 | def value(b): |
| 16 | return struct.unpack('<f', struct.pack('<I', b))[0] |
| 17 | |
| 18 | |
| 19 | def run(bits): |
| 20 | assert len(bits) == 5120 |
| 21 | p = subprocess.run([sys.argv[1]], input=struct.pack('<5120I', *bits), capture_output=True) |
| 22 | assert p.returncode == 0, (p.returncode, p.stderr) |
| 23 | assert len(p.stdout) == 8 |
| 24 | return struct.unpack('<Q', p.stdout)[0] |
| 25 | |
| 26 | |
| 27 | rng = random.Random(710) |
| 28 | cases = [[0] * 5120, [fbits(1.0)] * 5120, [fbits(2.7109375)] * 5120] |
| 29 | cases += [[fbits(-1.0)] * 5120, [0] * 5119 + [fbits(-2.7109375)]] |
| 30 | for _ in range(25): |
| 31 | cases.append([rng.randrange(0, 0x7d000000) | (rng.randrange(2) << 31) for _ in range(5120)]) |
| 32 | for b in [1, 0x007fffff, 0x00800000, 0x7e000000, 0x7f7fffff]: |
| 33 | a = [0] * 5120 |
| 34 | a[-1] = b |
| 35 | cases.append(a) |
| 36 | for exponent in range(1, 255): |
| 37 | for mantissa in [0, 1, 0x7ffffe, 0x7fffff, rng.randrange(0x800000)]: |
| 38 | a = [0] * 5120 |
| 39 | a[rng.randrange(5120)] = (exponent << 23) | mantissa |
| 40 | cases.append(a) |
| 41 | checks = 0 |
| 42 | for a in cases: |
| 43 | w = max(abs(value(b)) for b in a) |
| 44 | r = run(a) |
| 45 | if w < value(0x00800000): |
| 46 | assert r == fbits(1.0), (w, r) |
| 47 | else: |
| 48 | exact = Fraction(w) * 72 * Fraction(1025, 1024) |
| 49 | if exact > Fraction(value(0x7f7fffff)): |
| 50 | assert r == 0 |
| 51 | else: |
| 52 | assert 0 < r < 0x7f800000 |
| 53 | assert Fraction(value(r)) >= exact |
| 54 | assert Fraction(value(r - 1)) < exact |
| 55 | assert value(r) >= math.sqrt(5120) * w |
| 56 | checks += 1 |
| 57 | for b in [0x7f800000, 0xff800000, 0x7fc00000, 0xffc00001]: |
| 58 | a = [fbits(1.0)] * 5120 |
| 59 | a[-1] = b |
| 60 | assert run(a) == 0 |
| 61 | checks += 1 |
| 62 | print(f'PASS: {checks} envelope cases, exact rational upward rounding, tail invalid-input refusal') |