interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / gpu_tilelab_test.c

270 lines · 15509 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * gpu_tilelab_test: TileLab chip driver (aien-architecture-mv#347 T2/T3). A/B of GEMM tile schedules on the GB10.
4 * The schedules (rt ct wm wn band) come from the command line; the native .om planner (omega-mv seed/tilelab/tilelab.om)
5 * chose them. This file contains no planner: it only runs the cfgs it is given through omega_gpu_gemm_test_set_cfg.
6 * --host-only refusals and oracle self-tests, no device
7 * (default) chip (caller holds flock /tmp/aien-gb10.lock): per shape and cfg, one verified run against an f64 oracle,
8 * then interleaved timed rounds; red controls; JSON receipt with every raw sample.
9 * --shapes "M,N,K;..." --cfg "name:rt,ct,wm,wn,band" (repeatable; "default" is always arm 0) --rounds N --out FILE
10 * Oracle: f64 sum of bf16 products (inputs decoded arithmetically). Bound: |c - ref| <= (K/16 + 2) 2^-22 sum|a b| per element
11 * (one tensor-core step per 16 values of k, each step rounded at most 2^-23 relative to the running partial sum, doubled for slack).
12 * Large shapes check the first 32 rows and 32 spread rows (all columns), small shapes every element.
13 */
14#include "omega_gpu_gemm.h"
15#include "omega_gpu_session.h"
16#include <inttypes.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <math.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#define MAXCFG 16
25#define MAXSH 16
26#define MAXR 64
27typedef struct { char name[40]; OmegaGemmCfg c; } Arm;
28static Arm arms[MAXCFG]; static int narms;
29static uint64_t rng = 0x243f6a8885a308d3ull;
30static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); }
31static uint16_t rbf(void) { float f = ((float)(rnd() & 0xffff) / 32768.0f) - 1.0f; uint32_t u; memcpy(&u, &f, 4); return (uint16_t)(u >> 16); }
32static double bf(uint16_t h) { uint32_t u = (uint32_t)h << 16; float f; memcpy(&f, &u, 4); return f; }
33static int cmpd(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; }
34static double pct(double *v, int n, double p) { qsort(v, n, sizeof *v, cmpd); int i = (int)ceil(p * n) - 1; return v[i < 0 ? 0 : i]; }
35
36/* returns number of wrong elements; *worst = max (|c-ref| / bound) */
37static uint64_t oracle(uint32_t M, uint32_t N, uint32_t K, const uint16_t *A, const uint16_t *B, const float *C, double *worst, uint64_t *checked) {
38 uint64_t bad = 0, ck = 0; *worst = 0;
39 int full = (double)M * N * K <= 2.2e9;
40 for (uint32_t r = 0; r < M; r++) {
41 if (!full && !(r < 32 || r % (M / 32 + 1) == 0)) continue;
42 for (uint32_t c = 0; c < N; c++) {
43 double s = 0, a = 0;
44 for (uint32_t k = 0; k < K; k++) { double p = bf(A[(size_t)r * K + k]) * bf(B[(size_t)k * N + c]); s += p; a += fabs(p); }
45 double bound = ((double)(K / 16 + 2)) * ldexp(1.0, -22) * a + 1e-30, e = fabs((double)C[(size_t)r * N + c] - s);
46 if (!(e <= bound)) bad++; /* NaN counts as wrong */
47 if (e / bound > *worst || e != e) *worst = e != e ? INFINITY : e / bound;
48 ck++;
49 }
50 }
51 *checked = ck; return bad;
52}
53
54/* Consume the entire list, including separators and capacity. Never test a prefix. */
55static int shape_uint(const char **sp, uint32_t *v) {
56 const unsigned char *p = (const unsigned char *)*sp; uint32_t n = 0;
57 if (*p < '0' || *p > '9') return -1;
58 do {
59 unsigned d = *p++ - '0';
60 if (n > UINT32_MAX / 10 || (n == UINT32_MAX / 10 && d > UINT32_MAX % 10)) return -1;
61 n = n * 10 + d;
62 } while (*p >= '0' && *p <= '9');
63 if (!n) return -1;
64 *v = n; *sp = (const char *)p; return 0;
65}
66static int parse_shape(const char *s, uint32_t sh[][3], int *n) {
67 *n = 0;
68 for (;;) {
69 if (*n == MAXSH) return -1;
70 for (int i = 0; i < 3; i++) {
71 if (shape_uint(&s, &sh[*n][i])) return -1;
72 if (i < 2 && *s++ != ',') return -1;
73 }
74 (*n)++;
75 if (!*s) return 0;
76 if (*s++ != ';' || !*s) return -1;
77 }
78}
79
80/* RFC 3629: reject overlong forms, surrogates, truncated sequences and >U+10FFFF. */
81static int valid_utf8(const char *s) {
82 const unsigned char *p = (const unsigned char *)s;
83 while (*p) {
84 unsigned c = *p++, count, lo = 0x80, hi = 0xbf;
85 if (c < 0x80) continue;
86 if (c >= 0xc2 && c <= 0xdf) count = 1;
87 else if (c >= 0xe0 && c <= 0xef) {
88 count = 2;
89 if (c == 0xe0) lo = 0xa0;
90 if (c == 0xed) hi = 0x9f;
91 } else if (c >= 0xf0 && c <= 0xf4) {
92 count = 3;
93 if (c == 0xf0) lo = 0x90;
94 if (c == 0xf4) hi = 0x8f;
95 } else return 0;
96 for (unsigned i = 0; i < count; i++) {
97 if (*p < lo || *p > hi) return 0;
98 p++; lo = 0x80; hi = 0xbf;
99 }
100 }
101 return 1;
102}
103
104static void json_string(FILE *f, const char *s) {
105 fputc('"', f);
106 for (const unsigned char *p = (const unsigned char *)s; *p; p++) {
107 if (*p == '"' || *p == '\\') { fputc('\\', f); fputc(*p, f); }
108 else if (*p < 32) fprintf(f, "\\u%04x", *p);
109 else fputc(*p, f);
110 }
111 fputc('"', f);
112}
113
114#ifdef TILELAB_HOST_FAULT
115/* Host regression build only. No delegation to any device or session entry point. */
116static OmegaGemmCfg fault_cfg;
117static const char *fault_mode = "";
118static unsigned fault_calls;
119static int fault_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg) {
120 if (cfg) fault_cfg = *cfg; else omega_gpu_gemm_default_cfg(type, &fault_cfg);
121 return 0;
122}
123static int fault_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K,
124 const void *va, const void *vb, float *C, OmegaGpuGemmInfo *info) {
125 (void)type; (void)layout; fault_calls++;
126 if ((!strcmp(fault_mode, "warm_error") && fault_calls == 2) ||
127 (!strcmp(fault_mode, "timed_error") && fault_calls == 3) ||
128 (!strcmp(fault_mode, "mutant_error") && fault_cfg.mutant))
129 return OMEGA_GPU_GEMM_CHIP_FAIL;
130 const uint16_t *A = va, *B = vb;
131 for (uint32_t r = 0; r < M; r++) for (uint32_t c = 0; c < N; c++) {
132 double s = 0;
133 for (uint32_t k = 0; k < K; k++) s += bf(A[(size_t)r * K + k]) * bf(B[(size_t)k * N + c]);
134 C[(size_t)r * N + c] = (float)s;
135 if (fault_cfg.mutant && strcmp(fault_mode, "mutant_good")) C[(size_t)r * N + c] += 10.0f;
136 }
137 if (info) { memset(info, 0, sizeof *info); info->main_ns = 1000; info->pack_ns = 1000; }
138 return 0;
139}
140#define omega_gpu_gemm_test_set_cfg fault_set_cfg
141#define omega_gpu_gemm fault_gemm
142#define omega_gpu_session_last_error() "synthetic host fault"
143#endif
144
145int main(int argc, char **argv) {
146#ifdef TILELAB_HOST_FAULT
147 puts("TILELAB: synthetic CPU fault build; GPU NOT_RUN");
148#endif
149 const char *out = NULL, *shapes = "1024,1024,1024"; int host = 0, rounds = 7;
150 uint32_t sh[MAXSH][3]; int nsh;
151 omega_gpu_gemm_default_cfg(OMEGA_GPU_GEMM_BF16, &arms[0].c); strcpy(arms[0].name, "default"); narms = 1;
152 for (int i = 1; i < argc; i++) {
153 if (!strcmp(argv[i], "--host-only")) host = 1;
154#ifdef TILELAB_HOST_FAULT
155 else if (!strcmp(argv[i], "--fault") && i + 1 < argc) fault_mode = argv[++i];
156#endif
157 else if (!strcmp(argv[i], "--out") && i + 1 < argc) out = argv[++i];
158 else if (!strcmp(argv[i], "--shapes") && i + 1 < argc) shapes = argv[++i];
159 else if (!strcmp(argv[i], "--rounds") && i + 1 < argc) { rounds = atoi(argv[++i]); if (rounds < 1 || rounds > MAXR) { fprintf(stderr, "rounds 1..%d\n", MAXR); return 2; } }
160 else if (!strcmp(argv[i], "--cfg") && i + 1 < argc && narms < MAXCFG) {
161 Arm *a = &arms[narms]; unsigned rt, ct, wm, wn, band; char nm[40];
162 if (sscanf(argv[++i], "%39[^:]:%u,%u,%u,%u,%u", nm, &rt, &ct, &wm, &wn, &band) != 6) { fprintf(stderr, "bad --cfg\n"); return 2; }
163 if (!valid_utf8(nm)) { fprintf(stderr, "bad --cfg: arm name must be UTF-8\n"); return 2; }
164 omega_gpu_gemm_default_cfg(OMEGA_GPU_GEMM_BF16, &a->c); a->c.rt = rt; a->c.ct = ct; a->c.wm = wm; a->c.wn = wn; a->c.band = band;
165 strcpy(a->name, nm); narms++;
166 } else { fprintf(stderr, "usage: %s [--host-only] [--shapes S] [--cfg name:rt,ct,wm,wn,band]... [--rounds N] [--out FILE]\n", argv[0]); return 2; }
167 }
168 if (parse_shape(shapes, sh, &nsh)) { fprintf(stderr, "bad --shapes\n"); return 2; }
169 int fails = 0;
170 /* refusals: an arm the API refuses is reported, never run */
171 int ok[MAXCFG];
172 for (int a = 0; a < narms; a++) { ok[a] = omega_gpu_gemm_cfg_ok(&arms[a].c); printf("arm %s rt=%u ct=%u wm=%u wn=%u band=%u: %s\n", arms[a].name, arms[a].c.rt, arms[a].c.ct, arms[a].c.wm, arms[a].c.wn, arms[a].c.band, ok[a] ? "accepted" : "REFUSED"); }
173 { OmegaGemmCfg bad = arms[0].c; bad.rt = 3; int r1 = omega_gpu_gemm_cfg_ok(&bad); bad = arms[0].c; bad.wm = 3; int r2 = omega_gpu_gemm_cfg_ok(&bad);
174 printf("red: rt=3 refused=%s, wm=3 refused=%s\n", !r1 ? "yes" : "NO", !r2 ? "yes" : "NO"); if (r1 || r2) fails++; }
175 { /* oracle self-test: one element moved by 1e-3 relative must be flagged */
176 uint16_t A[16 * 32], B[32 * 8]; float C[16 * 8]; double w; uint64_t ck;
177 for (int i = 0; i < 16 * 32; i++) A[i] = rbf();
178 for (int i = 0; i < 32 * 8; i++) B[i] = rbf();
179 for (int r = 0; r < 16; r++) for (int c = 0; c < 8; c++) { double s = 0; for (int k = 0; k < 32; k++) s += bf(A[r * 32 + k]) * bf(B[k * 8 + c]); C[r * 8 + c] = (float)s; }
180 uint64_t good = oracle(16, 8, 32, A, B, C, &w, &ck); C[37] *= 1.001f; C[37] += 0.01f; uint64_t bad = oracle(16, 8, 32, A, B, C, &w, &ck);
181 printf("oracle self-test: exact result %" PRIu64 " wrong, planted error %" PRIu64 " wrong\n", good, bad); if (good || !bad) fails++; }
182 if (host) { printf("TILELAB_CHIP host: %s\n", fails ? "FAIL" : "PASS"); return fails ? 1 : 0; }
183
184 int jfd = out ? open(out, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644) : -1;
185 FILE *js = jfd >= 0 ? fdopen(jfd, "w") : NULL; if (out && !js) { perror(out); if (jfd >= 0) close(jfd); return 2; }
186 if (js) {
187#ifdef TILELAB_HOST_FAULT
188 fprintf(js, "{\"execution\":\"synthetic-host\",");
189#else
190 fprintf(js, "{\"execution\":\"device\",");
191#endif
192 fprintf(js, "\"tool\":\"gpu_tilelab_test\",\"rounds\":%d,\"cells\":[", rounds);
193 }
194 int firstcell = 1;
195 for (int s = 0; s < nsh; s++) {
196 uint32_t M = sh[s][0], N = sh[s][1], K = sh[s][2];
197 uint16_t *A = malloc((size_t)M * K * 2), *B = malloc((size_t)K * N * 2); float *C = malloc((size_t)M * N * 4);
198 if (!A || !B || !C) { printf("NOT_RUN: host allocation\n"); return 2; }
199 for (size_t i = 0; i < (size_t)M * K; i++) A[i] = rbf();
200 for (size_t i = 0; i < (size_t)K * N; i++) B[i] = rbf();
201 double samp[MAXCFG][MAXR]; int ns[MAXCFG] = {0}; double worst[MAXCFG]; uint64_t wrong[MAXCFG], chk[MAXCFG]; uint32_t gpr[MAXCFG]; double packms[MAXCFG];
202 memset(wrong, 0, sizeof wrong);
203 for (int a = 0; a < narms; a++) {
204 if (!ok[a]) continue;
205 omega_gpu_gemm_test_set_cfg(OMEGA_GPU_GEMM_BF16, &arms[a].c);
206 OmegaGpuGemmInfo info; memset(C, 0xff, (size_t)M * N * 4);
207 int rc = omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, M, N, K, A, B, C, &info);
208 if (rc) { printf("%ux%ux%u %s: chip rc %s: %s\n", M, N, K, arms[a].name, omega_gpu_gemm_rc_name(rc), omega_gpu_session_last_error()); ok[a] = 0; fails++; continue; }
209 wrong[a] = oracle(M, N, K, A, B, C, &worst[a], &chk[a]); gpr[a] = info.gpr[OMEGA_GEMM_K_MAIN]; packms[a] = info.pack_ns / 1e6;
210 if (wrong[a]) fails++;
211 }
212 for (int r = 0; r < rounds; r++)
213 for (int j = 0; j < narms; j++) {
214 int a = (j + r) % narms; if (!ok[a] || wrong[a]) continue;
215 omega_gpu_gemm_test_set_cfg(OMEGA_GPU_GEMM_BF16, &arms[a].c);
216 OmegaGpuGemmInfo info;
217 int rc = omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, M, N, K, A, B, C, &info); /* warm: builds kernels */
218 if (rc) {
219 printf("%ux%ux%u %s: warmup rc %s: %s\n", M, N, K, arms[a].name, omega_gpu_gemm_rc_name(rc), omega_gpu_session_last_error());
220 ok[a] = 0; fails++; continue;
221 }
222 rc = omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, M, N, K, A, B, C, &info);
223 if (rc) {
224 printf("%ux%ux%u %s: timed rc %s: %s\n", M, N, K, arms[a].name, omega_gpu_gemm_rc_name(rc), omega_gpu_session_last_error());
225 ok[a] = 0; fails++; continue;
226 }
227 samp[a][ns[a]++] = info.main_ns / 1e6;
228 }
229 for (int a = 0; a < narms; a++) {
230 const Arm *m = &arms[a];
231 if (!ok[a] || wrong[a] || !ns[a]) { printf("%ux%ux%u %-12s %s\n", M, N, K, m->name, ok[a] ? "WRONG RESULT" : "NOT_RUN/REFUSED"); continue; }
232 double sc[MAXR]; memcpy(sc, samp[a], ns[a] * sizeof(double));
233 double p50 = pct(sc, ns[a], 0.5), p95 = pct(sc, ns[a], 0.95), tf = 2.0 * M * N * K / (p50 * 1e-3) / 1e12;
234 printf("%ux%ux%u %-12s main p50 %.4f ms p95 %.4f ms %.2f TFLOPS gpr %u worst err/bound %.3f (%" PRIu64 " checked)\n", M, N, K, m->name, p50, p95, tf, gpr[a], worst[a], chk[a]);
235 if (js) {
236 fprintf(js, "%s{\"m\":%u,\"n\":%u,\"k\":%u,\"arm\":", firstcell ? "" : ",", M, N, K);
237 json_string(js, m->name);
238 fprintf(js, ",\"rt\":%u,\"ct\":%u,\"wm\":%u,\"wn\":%u,\"band\":%u,\"gpr_main\":%u,\"p50_ms\":%.6f,\"p95_ms\":%.6f,\"tflops_p50\":%.4f,\"pack_ms_first\":%.6f,\"worst_err_over_bound\":%.6f,\"checked\":%" PRIu64 ",\"wrong\":%" PRIu64 ",\"samples_ms\":[", m->c.rt, m->c.ct, m->c.wm, m->c.wn, m->c.band, gpr[a], p50, p95, tf, packms[a], worst[a], chk[a], wrong[a]);
239 for (int i = 0; i < ns[a]; i++) fprintf(js, "%s%.6f", i ? "," : "", samp[a][i]);
240 fprintf(js, "]}"); firstcell = 0;
241 }
242 }
243 free(A); free(B); free(C);
244 }
245 /* red controls on the chip: the three API mutants must produce wrong results under the oracle */
246 int caught = 0;
247 for (int mut = 1; mut <= 3; mut++) {
248 OmegaGemmCfg c = arms[0].c; c.mutant = mut; omega_gpu_gemm_test_set_cfg(OMEGA_GPU_GEMM_BF16, &c);
249 uint16_t A[65 * 100], B[100 * 63]; float C[65 * 63]; double w; uint64_t ck;
250 for (int i = 0; i < 65 * 100; i++) A[i] = rbf();
251 for (int i = 0; i < 100 * 63; i++) B[i] = rbf();
252 int rc = omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, 65, 63, 100, A, B, C, NULL);
253 if (rc) {
254 printf("red: mutant %d RUNTIME ERROR rc %s: %s (oracle NOT_RUN)\n", mut, omega_gpu_gemm_rc_name(rc), omega_gpu_session_last_error());
255 fails++; continue;
256 }
257 uint64_t bad = oracle(65, 63, 100, A, B, C, &w, &ck);
258 printf("red: mutant %d %s (%" PRIu64 " wrong)\n", mut, bad ? "CAUGHT" : "NOT CAUGHT", bad); caught += bad > 0;
259 }
260 if (caught != 3) fails++;
261 omega_gpu_gemm_test_set_cfg(OMEGA_GPU_GEMM_BF16, NULL);
262 if (js) {
263 fprintf(js, "],\"red_mutants_caught\":%d,\"fails\":%d}\n", caught, fails);
264 int output_error = ferror(js);
265 if (fclose(js)) output_error = 1;
266 if (output_error) { fprintf(stderr, "receipt output failed: %s\n", out); return 2; }
267 }
268 printf("TILELAB_CHIP: %s\n", fails ? "FAIL" : "PASS");
269 return fails ? 1 : 0;
270}