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 / gpu_gemm_any_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Reuse the independent tensor arithmetic oracle and simulator, not the kernel builder. */ |
| 3 | #define main gemm_existing_test_main |
| 4 | #include "gpu_gemm_test.c" |
| 5 | #undef main |
| 6 | |
| 7 | typedef struct { uint32_t m, n, k; } AnyShape; |
| 8 | static AnyShape shapes[300]; |
| 9 | static unsigned shape_count; |
| 10 | static void add_shape(uint32_t m, uint32_t n, uint32_t k) { |
| 11 | for (unsigned i = 0; i < shape_count; i++) |
| 12 | if (shapes[i].m == m && shapes[i].n == n && shapes[i].k == k) return; |
| 13 | if (shape_count >= sizeof shapes / sizeof shapes[0]) abort(); |
| 14 | shapes[shape_count++] = (AnyShape){m,n,k}; |
| 15 | } |
| 16 | static void add_axis(uint32_t d) { add_shape(d,3,5); add_shape(3,d,5); add_shape(3,5,d); } |
| 17 | static void init_shapes(void) { |
| 18 | static const uint32_t edges[] = {127,128,129,255,256,257,511,512,513,1023,1024,1025,2047,2048,2049,4095,4096,4097,8191,8192}; |
| 19 | for (uint32_t d = 1; d <= 64; d++) add_axis(d); |
| 20 | for (unsigned i = 0; i < sizeof edges / sizeof edges[0]; i++) add_axis(edges[i]); |
| 21 | for (uint32_t i = 1; i <= 30; i++) add_shape(i,2*i+1,3*i+7); |
| 22 | static const AnyShape coupled[] = {{17,19,33},{31,33,65},{63,65,17},{65,67,33},{127,129,3},{129,127,5}}; |
| 23 | for (unsigned i = 0; i < sizeof coupled / sizeof coupled[0]; i++) |
| 24 | add_shape(coupled[i].m,coupled[i].n,coupled[i].k); |
| 25 | check(shape_count == 286, "Wolfram-verified distinct shape count"); |
| 26 | } |
| 27 | /* The mathematical check supplements the stricter bitwise tensor-model comparison. |
| 28 | * WolframLanguageEvaluator: (33*4/2^26+1/2^23)<1/2^18 -> True; |
| 29 | * n=Ceiling[K/16]; gamma=n/(2^18-n). See om/gemm-any/README.md for assumptions. */ |
| 30 | static uint64_t mathematical_check(OmegaGpuGemmType t, unsigned lay, AnyShape sh, |
| 31 | const void *a, const void *b, const float *c) { |
| 32 | double steps = (sh.k + 15u) / 16u, gamma = steps / (262144.0 - steps); |
| 33 | uint64_t bad = 0; |
| 34 | for (uint32_t m = 0; m < sh.m; m++) for (uint32_t n = 0; n < sh.n; n++) { |
| 35 | double sum = 0, magnitude = 0; |
| 36 | for (uint32_t k = 0; k < sh.k; k++) { |
| 37 | double av = val(t,a,lay & OMEGA_GPU_GEMM_A_COL ? (uint64_t)k*sh.m+m : (uint64_t)m*sh.k+k); |
| 38 | double bv = val(t,b,lay & OMEGA_GPU_GEMM_B_COL ? (uint64_t)n*sh.k+k : (uint64_t)k*sh.n+n); |
| 39 | double p = av*bv; sum += p; magnitude += fabs(p); |
| 40 | } |
| 41 | if (!isfinite(c[(uint64_t)m*sh.n+n]) || fabs((double)c[(uint64_t)m*sh.n+n]-sum) > gamma*magnitude) bad++; |
| 42 | } |
| 43 | return bad; |
| 44 | } |
| 45 | static uint64_t digest_words(uint64_t h, const float *p, uint64_t n) { |
| 46 | for (uint64_t i = 0; i < n; i++) { h ^= f2u(p[i]); h *= UINT64_C(1099511628211); } |
| 47 | return h; |
| 48 | } |
| 49 | static int sweep(int chip, int digest) { |
| 50 | const size_t cap = 8192u*5u; |
| 51 | void *a = malloc(cap*2+2), *b = malloc(cap*2+2); float *c = malloc(cap*4); |
| 52 | if (!a || !b || !c) { free(a); free(b); free(c); return 2; } |
| 53 | unsigned runs = 0; uint64_t checked_total = 0; |
| 54 | for (int ti = 0; ti < 2; ti++) { |
| 55 | OmegaGpuGemmType t = (OmegaGpuGemmType)ti; |
| 56 | OmegaGemmCfg cfg = { t,1,1,1,1,4,0,0 }; |
| 57 | SimCtx sim; memset(&sim,0,sizeof sim); |
| 58 | if (!chip && sim_build(&sim,&cfg)) { fprintf(stderr,"kernel build failed type %d\n",ti); sim_free(&sim); return 1; } |
| 59 | for (unsigned s = 0; s < shape_count; s++) for (unsigned lay = 0; lay < 4; lay++) { |
| 60 | AnyShape sh = shapes[s]; uint64_t checked = 0; char first[200] = {0}; |
| 61 | fill(t,a,(uint64_t)sh.m*sh.k,s*4+lay); fill(t,b,(uint64_t)sh.k*sh.n,s*4+lay+1000); |
| 62 | sim.order = s & 1u; |
| 63 | int rc = chip ? omega_gpu_gemm(t,lay,sh.m,sh.n,sh.k,a,b,c,NULL) : sim_gemm(&sim,&cfg,lay,sh.m,sh.n,sh.k,a,b,c); |
| 64 | uint64_t bad = rc ? 1 : compare(t,lay,sh.m,sh.n,sh.k,a,b,c,&checked,first,sizeof first); |
| 65 | uint64_t math_bad = rc ? 1 : mathematical_check(t,lay,sh,a,b,c); |
| 66 | if (rc || bad || math_bad) { |
| 67 | fprintf(stderr,"FAIL any %s %ux%ux%u layout %u rc %d wrong %"PRIu64" math %"PRIu64" %s %s\n",tname(t),sh.m,sh.n,sh.k,lay,rc,bad,math_bad,first,sim.err); |
| 68 | sim_free(&sim); free(a); free(b); free(c); return 1; |
| 69 | } |
| 70 | checked_total += checked; runs++; |
| 71 | if (digest) printf("digest %d %u %u %016"PRIx64"\n",ti,s,lay,digest_words(UINT64_C(14695981039346656037),c,(uint64_t)sh.m*sh.n)); |
| 72 | } |
| 73 | sim_free(&sim); |
| 74 | } |
| 75 | printf("GEMM ANY %s SWEEP PASS (%u distinct shapes, %u multiplies, %"PRIu64" output words, bitwise model + per-K bound)\n",chip ? "CHIP" : "SIM",shape_count,runs,checked_total); |
| 76 | free(a); free(b); free(c); return 0; |
| 77 | } |
| 78 | static int mutants(int chip) { |
| 79 | AnyShape sh = {17,9,33}; uint16_t a[17*33+1], b[33*9+1]; float c[17*9]; |
| 80 | for (int t = 0; t < 2; t++) for (int mut = 1; mut <= 2; mut++) { |
| 81 | OmegaGemmCfg cfg = {(OmegaGpuGemmType)t,1,1,1,1,4,mut,0}; |
| 82 | SimCtx sim; memset(&sim,0,sizeof sim); |
| 83 | if (!chip && sim_build(&sim,&cfg)) { sim_free(&sim); return 1; } |
| 84 | fill(cfg.type,a,17*33,77); fill(cfg.type,b,33*9,78); |
| 85 | if (chip) omega_gpu_gemm_test_set_cfg(cfg.type,&cfg); |
| 86 | int rc = chip ? omega_gpu_gemm(cfg.type,0,sh.m,sh.n,sh.k,a,b,c,NULL) : |
| 87 | sim_gemm(&sim,&cfg,0,sh.m,sh.n,sh.k,a,b,c); |
| 88 | if (chip) omega_gpu_gemm_test_set_cfg(cfg.type,NULL); |
| 89 | uint64_t checked = 0; char first[200]; |
| 90 | uint64_t bad = rc ? 0 : compare(cfg.type,0,sh.m,sh.n,sh.k,a,b,c,&checked,first,sizeof first); |
| 91 | sim_free(&sim); |
| 92 | if (rc || !bad) { fprintf(stderr,"FAIL mutant %d type %d rc %d wrong %"PRIu64"\n",mut,t,rc,bad); return 1; } |
| 93 | printf("GEMM ANY MUTANT CAUGHT %s %s (%"PRIu64" differing output words)\n",tname(cfg.type),mut == 1 ? "dropped K tail" : "off-by-one edge predicate",bad); |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | int main(int argc, char **argv) { |
| 98 | int sim = 0, chip = 0, digest = 0, list = 0; double bf = 0, fp = 0; |
| 99 | for (int i = 1; i < argc; i++) { |
| 100 | if (!strcmp(argv[i],"--sim")) sim = 1; |
| 101 | else if (!strcmp(argv[i],"--digest")) digest = 1; |
| 102 | else if (!strcmp(argv[i],"--list")) list = 1; |
| 103 | else if (!strcmp(argv[i],"--chip")) chip = 1; |
| 104 | else if (!strcmp(argv[i],"--ceiling") && i+1 < argc && sscanf(argv[++i],"%lf,%lf",&bf,&fp) == 2 && isfinite(bf) && isfinite(fp) && bf > 0 && fp > 0) { CEIL_TFLOPS[0] = bf; CEIL_TFLOPS[1] = fp; } |
| 105 | else return 2; |
| 106 | } |
| 107 | if ((chip && (sim || list || !bf || !fp)) || (!sim && !chip && !list)) { |
| 108 | fprintf(stderr,"usage: %s --sim [--digest] | --list | --chip --ceiling MEASURED_BF16_TFLOPS,MEASURED_E4M3_TFLOPS\n",argv[0]); return 2; |
| 109 | } |
| 110 | init_shapes(); if (g_failed) return 1; |
| 111 | if (list) { for (unsigned s = 0; s < shape_count; s++) printf("%u %u %u\n",shapes[s].m,shapes[s].n,shapes[s].k); return 0; } |
| 112 | if (sweep(chip,digest)) return 1; |
| 113 | if (!digest && mutants(chip)) return 1; |
| 114 | if (chip) { |
| 115 | /* Dense speed probe is separate from the exhaustive skinny/edge sweep. */ |
| 116 | size_t n = (size_t)8192*8192; void *a = malloc(n*2), *b = malloc(n*2); float *c = malloc(n*4); |
| 117 | if (!a || !b || !c) { free(a); free(b); free(c); return 2; } |
| 118 | for (int t = 0; t < 2; t++) chip_one((OmegaGpuGemmType)t,0,8192,8192,8192,a,b,c); |
| 119 | free(a); free(b); free(c); omega_gpu_session_close(); |
| 120 | } |
| 121 | printf("GEMM ANY %s %s\n",chip ? "CHIP" : digest ? "SIM/DIGEST" : "HOST/SIM/MUTANTS",g_failed ? "FAIL" : "PASS"); |
| 122 | return g_failed ? 1 : 0; |
| 123 | } |