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 / tests / models / mxfp4-check.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Verification-only oracle; never part of the runtime or installed engine.""" |
| 4 | import argparse |
| 5 | import hashlib |
| 6 | import math |
| 7 | from pathlib import Path |
| 8 | import struct |
| 9 | import subprocess |
| 10 | import tempfile |
| 11 | |
| 12 | PIN = "189ada82e00bb440cb41351bd252cc68c54a1b1050051cc42a1df700081c3b54" |
| 13 | # OpenAI gpt_oss/torch/weights.py FP4_VALUES. ldexp models its torch.ldexp; |
| 14 | # struct packs IEEE binary32 once, independently of the candidate bit operations. |
| 15 | VALUES = (0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, |
| 16 | -0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0) |
| 17 | ROOT = Path(__file__).resolve().parents[2] |
| 18 | |
| 19 | |
| 20 | def build(compiler, source, target): |
| 21 | result = subprocess.run([compiler], input=source, capture_output=True, timeout=60) |
| 22 | if result.returncode: |
| 23 | raise RuntimeError(result.stderr.decode()) |
| 24 | target.write_bytes(result.stdout) |
| 25 | target.chmod(0o700) |
| 26 | return str(target) |
| 27 | |
| 28 | |
| 29 | def oracle(packed, lane, scale): |
| 30 | value = math.ldexp(VALUES[(packed >> (lane * 4)) & 15], scale - 127) |
| 31 | try: |
| 32 | return struct.pack('<f', value) |
| 33 | except OverflowError: |
| 34 | return struct.pack('<f', math.copysign(math.inf, value)) |
| 35 | |
| 36 | |
| 37 | def run(): |
| 38 | parser = argparse.ArgumentParser() |
| 39 | parser.add_argument('compiler', type=Path) |
| 40 | args = parser.parse_args() |
| 41 | compiler = str(args.compiler.resolve()) |
| 42 | digest = hashlib.sha256(Path(compiler).read_bytes()).hexdigest() |
| 43 | if digest != PIN: |
| 44 | raise RuntimeError(f'compiler pin mismatch: {digest}') |
| 45 | unit = (ROOT / 'native/models/mxfp4.om').read_bytes() |
| 46 | driver = (ROOT / 'tests/models/mxfp4-driver.om').read_bytes() |
| 47 | records = [(packed, lane, scale) for scale in range(255) |
| 48 | for packed in range(256) for lane in range(2)] |
| 49 | data = bytes(value for record in records for value in record) |
| 50 | expected = b''.join(oracle(*record) for record in records) |
| 51 | with tempfile.TemporaryDirectory(prefix='mxfp4-check-') as tmp: |
| 52 | exe = build(compiler, unit + driver, Path(tmp) / 'candidate') |
| 53 | result = subprocess.run([exe], input=data, capture_output=True, timeout=90) |
| 54 | if result.returncode or result.stdout != expected: |
| 55 | raise RuntimeError(f'candidate mismatch: rc={result.returncode}, bytes={len(result.stdout)}') |
| 56 | for bad, status in [(b'\x00\x02\x7f', 105), (b'\x00\x00\xff', 105), (b'\x00', 3)]: |
| 57 | r = subprocess.run([exe], input=bad, capture_output=True, timeout=10) |
| 58 | if r.returncode != status or r.stdout: |
| 59 | raise RuntimeError(f'refusal mismatch: {r.returncode}, {r.stdout!r}') |
| 60 | for call in ['mxfp4bits 16 127', 'mxfp4bits 0 256', 'mxfp4byte 256 0 127']: |
| 61 | main = ('fn main to int\nx = ' + call + '\nret int x\nend\n').encode() |
| 62 | negative = build(compiler, unit + main, Path(tmp) / 'negative') |
| 63 | r = subprocess.run([negative], capture_output=True, timeout=10) |
| 64 | if r.returncode != 105: |
| 65 | raise RuntimeError(f'out-of-range accepted: {call}') |
| 66 | changed = unit.replace(b'sign = shl and nib 8 28', b'sign = uint 0') |
| 67 | if changed == unit: |
| 68 | raise RuntimeError('mutant was not applied') |
| 69 | mutant = build(compiler, changed + driver, Path(tmp) / 'mutant') |
| 70 | r = subprocess.run([mutant], input=data, capture_output=True, timeout=90) |
| 71 | if r.returncode or len(r.stdout) != len(expected) or r.stdout == expected: |
| 72 | raise RuntimeError('sign mutant did not run to a full mismatching result') |
| 73 | print(f'compiler_sha256={digest}') |
| 74 | print(f'source_sha256={hashlib.sha256(unit).hexdigest()}') |
| 75 | print(f'candidate_sha256={hashlib.sha256(Path(exe).read_bytes()).hexdigest()}') |
| 76 | print(f'oracle_sha256={hashlib.sha256(expected).hexdigest()}') |
| 77 | print(f'MXFP4 native CPU exact matches={len(records)}; refusals=6; sign mutant rejected') |
| 78 | print('NVIDIA/Intel/AMD GPU: NOT_RUN; model logits: NOT_RUN') |
| 79 | |
| 80 | |
| 81 | if __name__ == '__main__': |
| 82 | run() |