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/omega-gpu-bench/blob/main/kit/ck.py # omega-gpu-bench First-party Omega GPU kernel benchmark harness and receipts 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 `omega-gpu-bench / kit / ck.py` 192 lines ยท 7647 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # kit/ck.py -- run one Omega-encoded cubin on this machine's NVIDIA GPU and check every output word. 3 # 4 # ck.py probe print the GPU name, compute capability and driver API version 5 # ck.py CUBIN KIND KIND is vecadd or neg; prints one JSON line 6 # 7 # The cubin is loaded with NVIDIA's driver library (libcuda, cuModuleLoadData) and launched with 8 # cuLaunchKernel. This is an interop oracle for Omega's machine-code encoder (asm2 + sm80.tbl) and 9 # cubin writer (withdrawn pending a clean-room rewrite, README.md). It is NOT CUDA++'s own user-space 10 # driver path. 11 # 12 # Inputs are public and deterministic: a[i], b[i] = Python random.Random(`20261026`).getrandbits(32), 13 # n = 2^20 + 7, 128 threads per block. vecadd must give c[i] = (a[i] + b[i]) mod 2^32 for every i. 14 # neg is the same kernel with one register operand changed (R4 for R5), so a working check must 15 # report mismatches for it (NEG_DETECTED). A neg that fails to load or run is LOAD_FAIL/RUN_FAIL, 16 # never NEG_DETECTED. A correct vecadd is then timed on its own 2^26-element buffers (768 MiB moved per 17 # launch, so the GPU and not Python's launch overhead sets the pace) with CUDA events: 3 warm-up launches, 18 # then 5 rounds of 20 back-to-back launches; kernel_ms is the median round / 20, and 19 # gbps = 12 * 2^26 bytes / kernel time (two 4-byte loads and one 4-byte store per element). 20 # 21 # Every driver entry point has explicit argtypes and every return code is checked. Each call is 22 # announced on stderr ("stage=...") first and faulthandler is on, so a crash names the faulting call. 23 # 24 # INTERIM host glue (Python + ctypes), derived from the CloudKernels lane's ck.py (2026-10-26) with 25 # three changes: neg counts only when it ran, the output buffer hash is recorded, and vecadd is timed. 26 # Omega successor: an omc-built loader that maps the cubin through CUDA++'s user-space driver path, 27 # with the CPU reference in .om (see DEPENDENCIES.md). 28 import ctypes, faulthandler, hashlib, json, os, random, statistics, sys 29 from array import array 30 31 faulthandler.enable() 32 N, B, SEED, M = (1 << 20) + 7, 128, `20261026`, 0xFFFFFFFF 33 C, P, U32, U64, SZ = ctypes.c_int, ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint64, ctypes.c_size_t 34 PI, PP = ctypes.POINTER(C), ctypes.POINTER(P) 35 SIGS = { 36 "cuInit": [U32], "cuDeviceGet": [PI, C], "cuDeviceGetAttribute": [PI, C, C], 37 "cuDriverGetVersion": [PI], "cuDeviceGetName": [ctypes.c_char_p, C, C], 38 "cuCtxCreate_v2": [PP, U32, C], "cuModuleLoadData": [PP, ctypes.c_char_p], 39 "cuModuleGetFunction": [PP, P, ctypes.c_char_p], "cuMemAlloc_v2": [ctypes.POINTER(U64), SZ], 40 "cuMemcpyHtoD_v2": [U64, P, SZ], "cuMemcpyDtoH_v2": [P, U64, SZ], "cuCtxSynchronize": [], 41 "cuLaunchKernel": [P, U32, U32, U32, U32, U32, U32, U32, P, PP, PP], 42 "cuEventCreate": [PP, U32], "cuEventRecord": [P, P], "cuEventSynchronize": [P], 43 "cuEventElapsedTime": [ctypes.POINTER(ctypes.c_float), P, P], 44 "cuGetErrorName": [C, ctypes.POINTER(ctypes.c_char_p)], 45 } 46 out = {} 47 48 49 def emit(code): 50 print(json.dumps(out, sort_keys=True)) 51 sys.stdout.flush() 52 sys.exit(code) 53 54 55 class Fail(Exception): 56 pass 57 58 59 def load_driver(): 60 fake = os.environ.get("OGB_FAKE_DRIVER") 61 if fake: # test fixture only (tests/fakecuda.py); the validator rejects receipts made with it 62 sys.path.insert(0, os.path.dirname(os.path.abspath(fake))) 63 out["driver"] = "fake" 64 return __import__(os.path.splitext(os.path.basename(fake))[0]).Driver() 65 out["driver"] = "libcuda" 66 lib = ctypes.CDLL("libcuda.so.1") 67 for fname, at in SIGS.items(): 68 f = getattr(lib, fname) 69 f.argtypes, f.restype = at, C 70 return lib 71 72 73 def call(what, *a): 74 sys.stderr.write("stage=%s\n" % what) 75 sys.stderr.flush() 76 r = getattr(cu, what)(*a) 77 if r != 0: 78 s = ctypes.c_char_p() 79 cu.cuGetErrorName(r, ctypes.byref(s)) 80 out["error"] = "%s: %d %s" % (what, r, s.value.decode() if s.value else "") 81 raise Fail(what) 82 83 84 try: 85 cu = load_driver() 86 except OSError as e: 87 out.update(verdict="NO_DRIVER", error=str(e)) 88 emit(2) 89 90 try: 91 call("cuInit", 0) 92 dev = C() 93 call("cuDeviceGet", ctypes.byref(dev), 0) 94 mj, mn, dv = C(), C(), C() 95 call("cuDeviceGetAttribute", ctypes.byref(mj), 75, dev.value) # COMPUTE_CAPABILITY_MAJOR 96 call("cuDeviceGetAttribute", ctypes.byref(mn), 76, dev.value) # COMPUTE_CAPABILITY_MINOR 97 call("cuDriverGetVersion", ctypes.byref(dv)) 98 nm = ctypes.create_string_buffer(256) 99 call("cuDeviceGetName", nm, 256, dev.value) 100 out.update(device=nm.value.decode(errors="replace"), cc="%d.%d" % (mj.value, mn.value), 101 cuda_driver_api=dv.value) 102 except Fail: 103 out["verdict"] = "INIT_FAIL" 104 emit(2) 105 if sys.argv[1] == "probe": 106 out["verdict"] = "PROBED" 107 emit(0) 108 109 path, kind = sys.argv[1], sys.argv[2] 110 img = open(path, "rb").read() 111 out.update(cubin=os.path.basename(path), kind=kind, cubin_sha256=hashlib.sha256(img).hexdigest()) 112 try: 113 ctx, mod, fn = P(), P(), P() 114 call("cuCtxCreate_v2", ctypes.byref(ctx), 0, dev.value) 115 call("cuModuleLoadData", ctypes.byref(mod), img) 116 call("cuModuleGetFunction", ctypes.byref(fn), mod, kind.encode()) 117 except Fail: 118 out["verdict"] = "LOAD_FAIL" 119 emit(1) 120 121 rng = random.Random(SEED) 122 a = array("I", (rng.getrandbits(32) for _ in range(N))) 123 b = array("I", (rng.getrandbits(32) for _ in range(N))) 124 ref = array("I", ((x + y) & M for x, y in zip(a, b))) 125 U = ctypes.c_uint32 * N 126 h = U() 127 128 129 def buffers(count, sources): 130 bufs = [] 131 for src in sources: 132 p = U64() 133 call("cuMemAlloc_v2", ctypes.byref(p), 4 * count) 134 if src is not None: 135 host = U.from_buffer_copy(src.tobytes()) 136 call("cuMemcpyHtoD_v2", p.value, ctypes.cast(host, P), 4 * count) 137 bufs.append(p) 138 n = U32(count) 139 keep = (bufs[0], bufs[1], bufs[2], n) 140 return keep, (P * 4)(*[ctypes.cast(ctypes.byref(x), P) for x in keep]) 141 142 143 def launch(count, args): 144 call("cuLaunchKernel", fn, (count + B - 1) // B, 1, 1, B, 1, 1, 0, None, ctypes.cast(args, PP), None) 145 146 147 try: 148 keep, args = buffers(N, (a, b, array("I", bytes(4 * N)))) 149 launch(N, args) 150 call("cuCtxSynchronize") 151 call("cuMemcpyDtoH_v2", ctypes.cast(h, P), keep[2].value, 4 * N) 152 except Fail: 153 out["verdict"] = "RUN_FAIL" 154 emit(1) 155 156 g = array("I", bytes(h)) 157 bad = [i for i in range(N) if g[i] != ref[i]] 158 le = g if sys.byteorder == "little" else array("I", g) 159 if sys.byteorder != "little": 160 le.byteswap() 161 out.update(output_sha256=hashlib.sha256(le.tobytes()).hexdigest(), checked=N, mismatches=len(bad), 162 first_bad=[[i, g[i], ref[i]] for i in bad[:3]]) 163 if kind == "neg": 164 out["verdict"] = "NEG_DETECTED" if bad else "NEG_MISSED" 165 emit(0 if bad else 1) 166 out["verdict"] = "WRONG" if bad else "PASS" 167 if bad: 168 emit(1) 169 170 NT = 1 << 26 171 try: 172 tkeep, targs = buffers(NT, (None, None, None)) # contents do not matter for timing 173 e0, e1, ms = P(), P(), ctypes.c_float() 174 call("cuEventCreate", ctypes.byref(e0), 0) 175 call("cuEventCreate", ctypes.byref(e1), 0) 176 for _ in range(3): 177 launch(NT, targs) 178 call("cuCtxSynchronize") 179 rounds = [] 180 for _ in range(5): 181 call("cuEventRecord", e0, None) 182 for _ in range(20): 183 launch(NT, targs) 184 call("cuEventRecord", e1, None) 185 call("cuEventSynchronize", e1) 186 call("cuEventElapsedTime", ctypes.byref(ms), e0, e1) 187 rounds.append(ms.value / 20) 188 kms = statistics.median(rounds) 189 out.update(timing_n=NT, kernel_ms=round(kms, 6), gbps=round(12 * NT / (kms * 1e6), 3)) 190 except Fail: 191 out["timing_error"] = out.pop("error") 192 emit(0)