omega-gpu-bench
First-party Omega GPU kernel benchmark harness and receipts
Brought by drakestapleton
omega-gpu-bench / tests / fakecuda.py
| 1 | # tests/fakecuda.py -- TEST FIXTURE, not a GPU. A pure-Python stand-in for the dozen libcuda entry points |
| 2 | # kit/ck.py calls, so CI (which has no GPU) can drive ./run end to end: probe, load, launch, copy back, |
| 3 | # event timing, receipt writing. ck.py loads it only when OGB_FAKE_DRIVER points at this file and then |
| 4 | # stamps "driver": "fake" into every result; tools/validate.py rejects such receipts unless it is given |
| 5 | # --allow-fake (used only by tests/run.sh). The kernel is emulated on the CPU, so nothing here says |
| 6 | # anything about a real GPU. |
| 7 | # |
| 8 | # OGB_FAKE_CC compute capability to report (default 8.9) |
| 9 | # OGB_FAKE_MODE "" (vecadd correct), "wrong" (vecadd corrupts three words), "nogpu" (cuInit fails 100) |
| 10 | import ctypes, os |
| 11 | from array import array |
| 12 | |
| 13 | ELF = b"\x7fELF" |
| 14 | |
| 15 | |
| 16 | class Driver: |
| 17 | def __init__(self): |
| 18 | self.mem, self.next, self.img, self.kernel = {}, 0x10000, None, None |
| 19 | self.mode = os.environ.get("OGB_FAKE_MODE", "") |
| 20 | self.cc = [int(x) for x in os.environ.get("OGB_FAKE_CC", "8.9").split(".")] |
| 21 | |
| 22 | @staticmethod |
| 23 | def _set(ref, value): |
| 24 | ref._obj.value = value |
| 25 | return 0 |
| 26 | |
| 27 | def cuInit(self, flags): |
| 28 | return 100 if self.mode == "nogpu" else 0 # CUDA_ERROR_NO_DEVICE |
| 29 | |
| 30 | def cuDeviceGet(self, ref, ordinal): |
| 31 | return self._set(ref, 0) |
| 32 | |
| 33 | def cuDeviceGetAttribute(self, ref, attr, dev): |
| 34 | return self._set(ref, self.cc[0] if attr == 75 else self.cc[1]) |
| 35 | |
| 36 | def cuDriverGetVersion(self, ref): |
| 37 | return self._set(ref, 13000) |
| 38 | |
| 39 | def cuDeviceGetName(self, buf, size, dev): |
| 40 | buf.value = b"FAKE DRIVER (test fixture, not a GPU)" |
| 41 | return 0 |
| 42 | |
| 43 | def cuCtxCreate_v2(self, ref, flags, dev): |
| 44 | return self._set(ref, 1) |
| 45 | |
| 46 | def cuModuleLoadData(self, ref, img): |
| 47 | if not img.startswith(ELF): |
| 48 | return 200 # CUDA_ERROR_INVALID_IMAGE |
| 49 | self.img = img |
| 50 | return self._set(ref, 1) |
| 51 | |
| 52 | def cuModuleGetFunction(self, ref, mod, name): |
| 53 | if b"\0" + name + b"\0" not in self.img: |
| 54 | return 500 # CUDA_ERROR_NOT_FOUND |
| 55 | self.kernel = name.decode() |
| 56 | return self._set(ref, 1) |
| 57 | |
| 58 | def cuMemAlloc_v2(self, ref, size): |
| 59 | handle, self.next = self.next, self.next + ((size + 0xFFFF) & ~0xFFFF) |
| 60 | self.mem[handle] = bytearray(size) if size <= (16 << 20) else None # timing buffers stay virtual |
| 61 | return self._set(ref, handle) |
| 62 | |
| 63 | def cuMemcpyHtoD_v2(self, dptr, host, size): |
| 64 | self.mem[dptr][:size] = ctypes.string_at(host.value, size) |
| 65 | return 0 |
| 66 | |
| 67 | def cuMemcpyDtoH_v2(self, host, dptr, size): |
| 68 | ctypes.memmove(host.value, bytes(self.mem[dptr][:size]), size) |
| 69 | return 0 |
| 70 | |
| 71 | def cuLaunchKernel(self, fn, gx, gy, gz, bx, by, bz, shared, stream, args, extra): |
| 72 | pa, p1, p2 = (ctypes.c_uint64.from_address(args[k]).value for k in range(3)) |
| 73 | n = ctypes.c_uint32.from_address(args[3]).value |
| 74 | if self.mem[p2] is None: |
| 75 | return 0 |
| 76 | a, b = array("I", bytes(self.mem[pa][:4 * n])), array("I", bytes(self.mem[p1][:4 * n])) |
| 77 | if self.kernel == "neg": |
| 78 | c = array("I", ((x + 1) & 0xFFFFFFFF for x in a)) |
| 79 | else: |
| 80 | c = array("I", ((x + y) & 0xFFFFFFFF for x, y in zip(a, b))) |
| 81 | if self.mode == "wrong": |
| 82 | for i in (5, 77, n - 1): |
| 83 | c[i] ^= 1 |
| 84 | self.mem[p2][:4 * n] = c.tobytes() |
| 85 | return 0 |
| 86 | |
| 87 | def cuCtxSynchronize(self): |
| 88 | return 0 |
| 89 | |
| 90 | def cuEventCreate(self, ref, flags): |
| 91 | return self._set(ref, 1) |
| 92 | |
| 93 | def cuEventRecord(self, ev, stream): |
| 94 | return 0 |
| 95 | |
| 96 | def cuEventSynchronize(self, ev): |
| 97 | return 0 |
| 98 | |
| 99 | def cuEventElapsedTime(self, ref, e0, e1): |
| 100 | return self._set(ref, 20.0) |
| 101 | |
| 102 | def cuGetErrorName(self, r, ref): |
| 103 | return self._set(ref, b"FAKE_ERROR_%d" % r) |