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 / tools / interop / cubin_probe.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* cubin_probe: load an exported sm_121 cubin through the CUDA driver and run it. |
| 3 | * |
| 4 | * This is an interop qualification tool, not part of libomega_gpu: it links |
| 5 | * libcuda only to validate that the artifact produced by Omega's own encoder is |
| 6 | * accepted by the vendor loader and computes the contract result. It refuses to |
| 7 | * run without SPARK_WINDOW. It never resets the device or changes driver state. |
| 8 | * |
| 9 | * cubin_probe [--load-only | --expect-reject] CUBIN MATMUL_FN M K N |
| 10 | * |
| 11 | * The image is loaded with cuModuleLoadDataEx and the driver's error and info |
| 12 | * logs are printed on every load, so a rejection carries whatever reason the |
| 13 | * loader gives. --load-only stops after cuModuleGetFunction. --expect-reject |
| 14 | * is a negative control: it passes only when load or symbol lookup fails. |
| 15 | * The output buffer is filled with a sentinel before launch, so a kernel that |
| 16 | * writes nothing cannot pass against a zero reference. |
| 17 | * |
| 18 | * Exit: 0 PASS (or the expected rejection), 2 usage, 3 no window, |
| 19 | * 4 CUDA/load failure, 5 numeric failure, 6 rejection expected but the |
| 20 | * image was accepted. */ |
| 21 | #include <cuda.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "omega_blackwell_matmul.h" |
| 28 | #include "../../om/standalone-oracles/oracle.h" |
| 29 | |
| 30 | #define CK(x) do { CUresult r_ = (x); if (r_) { const char *s_ = "?"; cuGetErrorString(r_, &s_); \ |
| 31 | fprintf(stderr, "FAIL %s -> %d %s\n", #x, (int)r_, s_); return 4; } } while (0) |
| 32 | |
| 33 | static void reference(const uint16_t *a,const uint16_t *b,float *out, |
| 34 | uint32_t m,uint32_t k,uint32_t n) { |
| 35 | #if CUDAPP_MIGRATION_PENDING_C_STANDALONE_ORACLES |
| 36 | omega_matmul_cpu_oracle_bf16(a,b,out,m,k,n); |
| 37 | #else |
| 38 | /* The loops only marshal indexed scalar cells; Omega owns multiply/add. */ |
| 39 | for(uint32_t i=0;i<m;i++) for(uint32_t j=0;j<n;j++) { |
| 40 | uint64_t sum=so_bits(0.0f); |
| 41 | for(uint32_t p=0;p<k;p++)sum=so_call(43,sum,a[(size_t)i*k+p],b[(size_t)p*n+j],0); |
| 42 | out[(size_t)i*n+j]=so_float(sum); |
| 43 | } |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | #define LOG_BYTES 8192 |
| 48 | #define SENTINEL (-12345.0f) |
| 49 | |
| 50 | static uint8_t *slurp(const char *p, size_t *n) { |
| 51 | FILE *f = fopen(p, "rb"); |
| 52 | if (!f) return NULL; |
| 53 | fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); |
| 54 | if (sz <= 0) { fclose(f); return NULL; } |
| 55 | uint8_t *b = malloc((size_t)sz); |
| 56 | if (!b) { fclose(f); return NULL; } |
| 57 | if (fread(b, 1, (size_t)sz, f) != (size_t)sz) { free(b); fclose(f); return NULL; } |
| 58 | fclose(f); *n = (size_t)sz; return b; |
| 59 | } |
| 60 | |
| 61 | /* Load with the driver's error/info logs attached; prints one LOAD line. */ |
| 62 | static CUresult load_logged(CUmodule *mod, const uint8_t *img) { |
| 63 | static char err[LOG_BYTES], info[LOG_BYTES]; |
| 64 | err[0] = info[0] = 0; |
| 65 | CUjit_option opt[] = { CU_JIT_ERROR_LOG_BUFFER, CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES, |
| 66 | CU_JIT_INFO_LOG_BUFFER, CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES, |
| 67 | CU_JIT_LOG_VERBOSE }; |
| 68 | void *val[] = { err, (void *)(uintptr_t)LOG_BYTES, info, (void *)(uintptr_t)LOG_BYTES, |
| 69 | (void *)(uintptr_t)1 }; |
| 70 | CUresult r = cuModuleLoadDataEx(mod, img, (unsigned)(sizeof opt / sizeof opt[0]), opt, val); |
| 71 | const char *s = "?"; |
| 72 | cuGetErrorString(r, &s); |
| 73 | printf("LOAD cuModuleLoadDataEx -> %d %s\n", (int)r, s); |
| 74 | printf("LOAD error_log=\"%s\"\nLOAD info_log=\"%s\"\n", err, info); |
| 75 | return r; |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char **argv) { |
| 79 | int load_only = 0, expect_reject = 0, a = 1; |
| 80 | if (argc > 1 && strcmp(argv[1], "--load-only") == 0) { load_only = 1; a++; } |
| 81 | else if (argc > 1 && strcmp(argv[1], "--expect-reject") == 0) { expect_reject = 1; a++; } |
| 82 | if (argc - a != 5) { |
| 83 | fprintf(stderr, "usage: cubin_probe [--load-only | --expect-reject] CUBIN MATMUL_FN M K N\n"); |
| 84 | return 2; |
| 85 | } |
| 86 | const char *path = argv[a], *fname = argv[a + 1]; |
| 87 | if (!getenv("SPARK_WINDOW")) { |
| 88 | fprintf(stderr, "refused: SPARK_WINDOW is not set; no authorized GPU window\n"); |
| 89 | return 3; |
| 90 | } |
| 91 | const uint32_t M = (uint32_t)strtoul(argv[a + 2], NULL, 0); |
| 92 | const uint32_t K = (uint32_t)strtoul(argv[a + 3], NULL, 0); |
| 93 | const uint32_t N = (uint32_t)strtoul(argv[a + 4], NULL, 0); |
| 94 | if (!so_all(M,K,N)) return 2; |
| 95 | size_t cub_n = 0; |
| 96 | uint8_t *cub = slurp(path, &cub_n); |
| 97 | if (!cub) { fprintf(stderr, "cannot read %s\n", path); return 2; } |
| 98 | |
| 99 | CK(cuInit(0)); |
| 100 | CUdevice dev; |
| 101 | CK(cuDeviceGet(&dev, 0)); |
| 102 | CUcontext ctx; |
| 103 | CUctxCreateParams cparams; |
| 104 | memset(&cparams, 0, sizeof cparams); |
| 105 | CK(cuCtxCreate(&ctx, &cparams, 0, dev)); |
| 106 | CUmodule mod; |
| 107 | CUresult lr = load_logged(&mod, cub); |
| 108 | CUfunction fn; |
| 109 | CUresult fr = CUDA_ERROR_NOT_FOUND; |
| 110 | if (lr == CUDA_SUCCESS) { |
| 111 | fr = cuModuleGetFunction(&fn, mod, fname); |
| 112 | const char *s = "?"; |
| 113 | cuGetErrorString(fr, &s); |
| 114 | printf("LOAD cuModuleGetFunction(%s) -> %d %s\n", fname, (int)fr, s); |
| 115 | } |
| 116 | if (expect_reject) { |
| 117 | if (lr == CUDA_SUCCESS) cuModuleUnload(mod); |
| 118 | cuCtxDestroy(ctx); |
| 119 | free(cub); |
| 120 | if (!so_all(so_eq(lr,CUDA_SUCCESS),so_eq(fr,CUDA_SUCCESS),1)) { |
| 121 | printf("REJECT-PASS cubin=%s fn=%s window=%s\n", path, fname, getenv("SPARK_WINDOW")); |
| 122 | return 0; |
| 123 | } |
| 124 | printf("REJECT-FAIL accepted cubin=%s fn=%s window=%s\n", path, fname, getenv("SPARK_WINDOW")); |
| 125 | return 6; |
| 126 | } |
| 127 | if (!so_all(so_eq(lr,CUDA_SUCCESS),so_eq(fr,CUDA_SUCCESS),1)) return 4; |
| 128 | if (load_only) { |
| 129 | printf("LOAD-PASS cubin=%s fn=%s window=%s\n", path, fname, getenv("SPARK_WINDOW")); |
| 130 | cuModuleUnload(mod); cuCtxDestroy(ctx); free(cub); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | const size_t asz = so_bytes(M,so_bytes(K,2,0),0), bsz = so_bytes(K,so_bytes(N,2,0),0), csz = so_bytes(M,so_bytes(N,4,0),0); |
| 135 | uint16_t *A = malloc(asz), *B = malloc(bsz); |
| 136 | float *C = malloc(csz), *ref = malloc(csz); |
| 137 | if (!A || !B || !C || !ref) return 4; |
| 138 | /* These fixture values are exactly BF16-representable; op42 is a bit narrowing, not a general quantizer. */ |
| 139 | for (uint32_t i = 0; i < (uint32_t)so_bytes(M,K,0); i++) A[i] = (uint16_t)so_call(42,so_call(18,i,0,0,0),0,0,0); |
| 140 | for (uint32_t i = 0; i < (uint32_t)so_bytes(K,N,0); i++) B[i] = (uint16_t)so_call(42,so_call(19,i,0,0,0),0,0,0); |
| 141 | reference(A, B, ref, M, K, N); |
| 142 | |
| 143 | CUdeviceptr dA, dB, dC; |
| 144 | CK(cuMemAlloc(&dA, asz)); CK(cuMemAlloc(&dB, bsz)); CK(cuMemAlloc(&dC, csz)); |
| 145 | CK(cuMemcpyHtoD(dA, A, asz)); CK(cuMemcpyHtoD(dB, B, bsz)); |
| 146 | float sentinel = SENTINEL; |
| 147 | uint32_t sbits; |
| 148 | memcpy(&sbits, &sentinel, sizeof sbits); |
| 149 | CK(cuMemsetD32(dC, sbits, so_bytes(M,N,0))); |
| 150 | void *args[] = { &dA, &dB, &dC }; |
| 151 | CK(cuLaunchKernel(fn, 1, 1, 1, 32, 1, 1, 0, NULL, args, NULL)); |
| 152 | CK(cuCtxSynchronize()); |
| 153 | CK(cuMemcpyDtoH(C, dC, csz)); |
| 154 | int bad = 0, untouched = 0; |
| 155 | double worst = 0; |
| 156 | for (uint32_t i = 0; i < (uint32_t)so_bytes(M,N,0); i++) { |
| 157 | untouched=(int)so_call(5,untouched,so_eq(so_bits(C[i]),so_bits(sentinel)),0,0); |
| 158 | uint64_t error=so_call(20,so_bits(C[i]),so_bits(ref[i]),0,0), old; |
| 159 | memcpy(&old,&worst,sizeof old); |
| 160 | worst=so_double(so_call(38,error,old,0,0)); |
| 161 | bad=(int)so_call(5,bad,so_call(21,error,0,0,0),0,0); |
| 162 | } |
| 163 | printf("cubin=%s fn=%s M=%u K=%u N=%u worst_abs=%.9g bad=%d untouched=%d window=%s\n", |
| 164 | path, fname, M, K, N, worst, bad, untouched, getenv("SPARK_WINDOW")); |
| 165 | cuMemFree(dA); cuMemFree(dB); cuMemFree(dC); |
| 166 | cuModuleUnload(mod); cuCtxDestroy(ctx); free(A); free(B); free(C); free(ref); free(cub); |
| 167 | return bad ? 5 : 0; |
| 168 | } |