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 / evidence / MAX-INTEROP / G2 / tools / max_cubin_probe.mojo
| 1 | # Gate-2 probe: load a cubin through MAX's own DeviceContext (max/v26.7.0 |
| 2 | # max.gpu.host.DeviceExternalFunction) and run it with MAX-owned context, |
| 3 | # buffers and stream. Verification tool only; refuses without SPARK_WINDOW. |
| 4 | # |
| 5 | # max_cubin_probe CUBIN FN MODE |
| 6 | # MODE run : load, launch omega_matmul_bf16 ABI (A bf16 16x16, B bf16 16x8, |
| 7 | # C f32 16x8), compare to the host fp32 oracle (same loop order as |
| 8 | # omega_matmul_cpu_oracle_bf16), exit 0 PASS / 5 numeric |
| 9 | # MODE loadonly : load + resolve only, exit 0 if accepted |
| 10 | # MODE reject : expect load to raise; exit 0 if it raised, 6 if accepted |
| 11 | # Exit 2 usage, 3 no window, 4 load/launch raised (run/loadonly). |
| 12 | from max.gpu.host import DeviceContext |
| 13 | from max.gpu.host.device_context import DeviceExternalFunction |
| 14 | from std.memory import bitcast |
| 15 | from std.os import getenv |
| 16 | from std.sys import argv, exit |
| 17 | |
| 18 | |
| 19 | def bf16_bits(x: Float32) -> UInt16: |
| 20 | # x is exactly representable in bf16 for every fixture value (k*0.25, k*0.5) |
| 21 | return UInt16(bitcast[DType.uint32](x) >> 16) |
| 22 | |
| 23 | |
| 24 | def bf16_val(h: UInt16) -> Float32: |
| 25 | return bitcast[DType.float32](UInt32(h) << 16) |
| 26 | |
| 27 | |
| 28 | def main() raises: |
| 29 | var args = argv() |
| 30 | if len(args) != 4: |
| 31 | print("usage: max_cubin_probe CUBIN FN run|loadonly|reject") |
| 32 | exit(2) |
| 33 | var window = getenv("SPARK_WINDOW") |
| 34 | if window == "": |
| 35 | print("refused: SPARK_WINDOW is not set; no authorized GPU window") |
| 36 | exit(3) |
| 37 | var path = String(args[1]) |
| 38 | var fn_name = String(args[2]) |
| 39 | var mode = String(args[3]) |
| 40 | var cubin: List[UInt8] |
| 41 | with open(path, "r") as f: |
| 42 | cubin = f.read_bytes() |
| 43 | |
| 44 | with DeviceContext() as ctx: |
| 45 | var loaded = True |
| 46 | var err = String("") |
| 47 | try: |
| 48 | var probe = DeviceExternalFunction( |
| 49 | ctx, |
| 50 | function_name=fn_name, |
| 51 | asm=String(StringSlice(unsafe_from_utf8=cubin)), |
| 52 | ) |
| 53 | _ = probe^ |
| 54 | except e: |
| 55 | loaded = False |
| 56 | err = String(e) |
| 57 | if mode == "reject": |
| 58 | print("mode=reject file=", path, " fn=", fn_name, " raised=", not loaded, " err=", err, " window=", window, sep="") |
| 59 | exit(0 if not loaded else 6) |
| 60 | if not loaded: |
| 61 | print("FAIL load file=", path, " fn=", fn_name, " err=", err, " window=", window, sep="") |
| 62 | exit(4) |
| 63 | if mode == "loadonly": |
| 64 | print("mode=loadonly file=", path, " fn=", fn_name, " accepted=True window=", window, sep="") |
| 65 | exit(0) |
| 66 | |
| 67 | comptime M = 16 |
| 68 | comptime K = 16 |
| 69 | comptime N = 8 |
| 70 | var func = DeviceExternalFunction( |
| 71 | ctx, |
| 72 | function_name=fn_name, |
| 73 | asm=String(StringSlice(unsafe_from_utf8=cubin)), |
| 74 | ) |
| 75 | var a = ctx.enqueue_create_buffer[DType.uint16](M * K) |
| 76 | var b = ctx.enqueue_create_buffer[DType.uint16](K * N) |
| 77 | var c = ctx.enqueue_create_buffer[DType.float32](M * N) |
| 78 | var ha = List[UInt16](capacity=M * K) |
| 79 | var hb = List[UInt16](capacity=K * N) |
| 80 | for i in range(M * K): |
| 81 | ha.append(bf16_bits(Float32((i % 7) - 3) * 0.25)) |
| 82 | for i in range(K * N): |
| 83 | hb.append(bf16_bits(Float32((i % 5) - 2) * 0.5)) |
| 84 | with a.map_to_host() as ah, b.map_to_host() as bh, c.map_to_host() as ch: |
| 85 | for i in range(M * K): |
| 86 | ah[i] = ha[i] |
| 87 | for i in range(K * N): |
| 88 | bh[i] = hb[i] |
| 89 | for i in range(M * N): |
| 90 | ch[i] = Float32(-12345.0) # sentinel: an unwritten C fails the check |
| 91 | ctx.enqueue_function(func, a, b, c, grid_dim=(1,), block_dim=(32,)) |
| 92 | ctx.synchronize() |
| 93 | var bad = 0 |
| 94 | var worst = Float64(0) |
| 95 | with c.map_to_host() as ch: |
| 96 | for i in range(M): |
| 97 | for j in range(N): |
| 98 | var s = Float32(0) |
| 99 | for p in range(K): |
| 100 | s += bf16_val(ha[i * K + p]) * bf16_val(hb[p * N + j]) |
| 101 | var d = abs(Float64(ch[i * N + j]) - Float64(s)) |
| 102 | if d > worst: |
| 103 | worst = d |
| 104 | if d > 1e-4: |
| 105 | bad += 1 |
| 106 | print("mode=run file=", path, " fn=", fn_name, " M=16 K=16 N=8 worst_abs=", worst, " bad=", bad, " window=", window, sep="") |
| 107 | exit(0 if bad == 0 else 5) |