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_bf16_gemv_test.c

422 lines · 30707 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * gpu_bf16_gemv_test: the bf16 streaming GEMV (omega_gpu_bf16_gemv_api.h), OWN-30B lm_head.
4 * --host-only emulation vs double oracle, codegen budgets and refusals (no device)
5 * --sim warp-simulator parity at 8 warps (both fragment orders, rows 1/2/4, depths, k 512/2048, multi-CTA
6 * with the row-group loop): bit-identical to the host f32 emulation; mutants 1-3 caught
7 * (default) the chip: all 151936 logits bit-identical to the host emulation and within tolerance of the double
8 * oracle, second call bit-identical, bf16-x entry, mutants 1-3 caught on a small tensor, timing
9 * (--reps N, --n N, --rows R, --warps W, --depth D, --ctas C, --no-evict, --spin-us, --out receipt.json)
10 * Verdict line last: "gpu_bf16_gemv_test: N checks, F failed: PASS|FAIL".
11 */
12#include "omega_gpu_bf16_gemv_api.h"
13#include "omega_gpu_session.h"
14#include "bw_warp_sim_k.h"
15#include "out_sha.h"
16#include <inttypes.h>
17#include <math.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22#include "test_out_file.h"
23
24static int g_checks, g_failed;
25#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
26static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
27static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
28static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
29static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; }
30static uint16_t bf16_rne(float f) { uint32_t x; memcpy(&x, &f, 4); uint32_t r = x + 0x7fffu + ((x >> 16) & 1u); return (uint16_t)(r >> 16); }
31static float bf16_f32(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; memcpy(&f, &x, 4); return f; }
32
33/* The kernel's exact f32 result for one row: per lane one FFMA chain (step, word, low half, high half), then the
34 * SHFL.DOWN tree 16, 8, 4, 2, 1 (lane L adds lane L + o's value; lane 0 is the result). */
35static float emu_row(const uint16_t *w, const float *x, uint32_t k) {
36 float a[32];
37 for (unsigned lane = 0; lane < 32; lane++) {
38 float acc = 0.0f;
39 for (uint32_t s = 0; s < k / 256u; s++) {
40 const uint16_t *wl = w + s * 256u + lane * 8u; const float *xl = x + s * 256u + lane * 8u;
41 for (unsigned i = 0; i < 8; i++) acc = fmaf(bf16_f32(wl[i]), xl[i], acc);
42 }
43 a[lane] = acc;
44 }
45 for (unsigned o = 16; o >= 1; o /= 2) { float b[32]; for (unsigned l = 0; l < 32; l++) b[l] = a[l + o < 32 ? l + o : l]; for (unsigned l = 0; l < 32; l++) a[l] += b[l]; }
46 return a[0];
47}
48static void oracle_row(const uint16_t *w, const float *x, uint32_t k, double *y, double *ys) {
49 double s = 0, m = 0;
50 for (uint32_t c = 0; c < k; c++) { double t = (double)bf16_f32(w[c]) * x[c]; s += t; m += fabs(t); }
51 *y = s; *ys = m;
52}
53static uint16_t *make_w(size_t count, uint32_t seed) {
54 uint16_t *w = malloc(count * 2); uint32_t s = seed;
55 for (size_t i = 0; i < count; i++) w[i] = bf16_rne(frand(&s, -0.06f, 0.06f));
56 return w;
57}
58static float *make_x(uint32_t k, uint32_t seed) {
59 float *x = malloc((size_t)k * 4); uint32_t s = seed;
60 for (uint32_t i = 0; i < k; i++) x[i] = frand(&s, -2.0f, 2.0f);
61 return x;
62}
63/* bit-identical compare of n outputs against the emulation; returns the number of differing rows */
64static size_t compare_emu(const uint16_t *w, const float *x, uint32_t n, uint32_t k, const float *y, size_t *first) {
65 size_t bad = 0; *first = (size_t)-1;
66 for (uint32_t r = 0; r < n; r++) {
67 float e = emu_row(w + (size_t)r * k, x, k);
68 if (memcmp(&e, &y[r], 4) != 0) { if (!bad) *first = r; bad++; }
69 }
70 return bad;
71}
72
73static void host_only(void) {
74 printf("host-only: emulation, oracle, budgets\n");
75 { uint16_t *w = make_w(2048, 5); float *x = make_x(2048, 6);
76 double yo, ys; oracle_row(w, x, 2048, &yo, &ys);
77 float e = emu_row(w, x, 2048);
78 CHECK(fabs((double)e - yo) <= 1e-6 * ys, "emulation within 1e-6 of the double oracle (%.9g vs %.9g, scale %.3g)", (double)e, yo, ys);
79 printf("emulation vs oracle: abs %.3e, scale %.3f\n", fabs((double)e - yo), ys);
80 /* a known hand case: w = 1 on the first 256 columns, x = c: sum 0..255 = 32640 */
81 uint16_t hw[256]; float hx[256]; for (unsigned i = 0; i < 256; i++) { hw[i] = 0x3f80; hx[i] = (float)i; }
82 CHECK(emu_row(hw, hx, 256) == 32640.0f, "hand case sum 0..255 = 32640");
83 free(w); free(x); }
84 uint32_t ws[] = { 4, 8, 16, 32 }, rs[] = { 1, 2, 4 };
85 for (unsigned i = 0; i < 4; i++) for (unsigned j = 0; j < 3; j++) for (uint32_t d = 1; d <= 8; d *= 2) for (int m = 0; m <= 3; m++) {
86 OmegaBf16GemvSpec s = { 2048, rs[j], ws[i], d, m, 0, 0, 0 };
87 if (rs[j] * d > 16u) continue;
88 uint32_t n = 0, g = 0; int rc = omega_bf16_gemv_prog_stats(&s, &n, &g);
89 uint32_t decl = ((g + 1 + 7) / 8) * 8;
90 CHECK(rc == 0 && n <= 1024, "codegen k2048 rows %u warps %u depth %u mutant %d: rc %d insns %u gprs %u", rs[j], ws[i], d, m, rc, n, g);
91 if (m == 0 && rc == 0 && rs[j] == 1 && d == 8) printf("codegen rows %u warps %2u depth %u: %u instructions, %u registers (declared %u, register file %u of 65536)%s\n", rs[j], ws[i], d, n, g, decl, 32u * ws[i] * decl, 32u * ws[i] * decl <= 65536u ? "" : " DOES NOT FIT");
92 }
93 { OmegaBf16GemvSpec s = { 2048, 3, 8, 8, 0, 0, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "rows 3 refused"); }
94 { OmegaBf16GemvSpec s = { 2000, 1, 8, 8, 0, 0, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "k 2000 refused"); }
95 { OmegaBf16GemvSpec s = { 2048, 1, 8, 3, 0, 0, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "depth 3 refused"); }
96 { OmegaBf16GemvSpec s = { 2048, 1, 8, 8, 4, 0, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "mutant 4 refused"); }
97 { /* the XBATCH form (L312 lever 1): the 35B lm_head shape, 4 x rows per CTA, 32 KiB of shared memory */
98 OmegaBf16GemvSpec s = { 2048, 1, 16, 8, 0, 249856, 4, 0 }; uint32_t ni = 0, gg = 0; int rc = omega_bf16_gemv_prog_stats(&s, &ni, &gg);
99 uint32_t decl = ((gg + 1 + 7) / 8) * 8;
100 CHECK(rc == 0 && omega_bf16_gemv_shared_bytes(&s) == 32768u && 32u * 16u * decl <= 65536u, "codegen XBATCH 4 k 2048 warps 16 depth 8: rc %d insns %u gprs %u shared %u", rc, ni, gg, omega_bf16_gemv_shared_bytes(&s));
101 printf("codegen XBATCH 4 k 2048 warps 16 depth 8: %u instructions, %u registers (declared %u), shared %u bytes\n", ni, gg, decl, omega_bf16_gemv_shared_bytes(&s)); }
102 { OmegaBf16GemvSpec s = { 2048, 1, 16, 8, 0, 249856, 5, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "xbatch 5 refused"); }
103 { OmegaBf16GemvSpec s = { 2048, 1, 16, 8, 0, 0, 2, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "xbatch without ystride refused"); }
104 { OmegaBf16GemvSpec s = { 4096, 1, 16, 16, 0, 249856, 4, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "xbatch 4 x k 4096 (64 KiB shared) refused"); }
105 { OmegaBf16GemvSpec s = { 2048, 1, 8, 8, 0, 249856, 4, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "xbatch 4 x k 2048 at 8 warps (more than 16 staging rounds) refused"); }
106 { OmegaBf16GemvSpec s = { 2048, 1, 16, 8, 4, 249856, 1, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "mutant 4 at xbatch 1 refused"); }
107 CHECK(OMEGA_BF16_ARGS_WORDS <= OMEGA_GPU_SESSION_MAX_ARGS, "argument words fit the constant bank window");
108 uint16_t bad[256]; memset(bad, 0, sizeof bad); bad[7] = 0x7f80;
109 OmegaGpuBf16Tensor *t = NULL;
110 CHECK(omega_gpu_bf16_tensor_upload(1, 256, bad, &t) == OMEGA_GPU_BF16_NAN_WEIGHT && !t, "an infinite bf16 weight is refused before any device use");
111 CHECK(omega_gpu_bf16_tensor_upload(1, 200, bad, &t) == OMEGA_GPU_BF16_BAD_ARGS, "k 200 refused at upload");
112}
113
114/* ------------------------------------------------------------- simulator */
115static uint64_t g_sim_issues;
116static int sim_point(uint32_t k, uint32_t n, uint32_t rows, uint32_t warps, uint32_t depth, uint32_t grid_x, int mutant, uint32_t seed, char *msg, size_t msgn, size_t *bad) {
117 static BlackwellIRProgram prog;
118 OmegaBf16GemvSpec spec = { k, rows, warps, depth, mutant, 0, 0, 0 };
119 if (omega_blackwell_codegen_bf16_gemv_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
120 uint16_t *w = aligned_alloc(16, (size_t)n * k * 2); { uint16_t *t = make_w((size_t)n * k, seed); memcpy(w, t, (size_t)n * k * 2); free(t); }
121 float *x = aligned_alloc(16, (size_t)k * 4); { float *t = make_x(k, seed + 1); memcpy(x, t, (size_t)k * 4); free(t); }
122 float *y = aligned_alloc(16, ((size_t)n * 4 + 15) & ~15ull);
123 uint8_t *smem = aligned_alloc(16, omega_bf16_gemv_shared_bytes(&spec));
124 float first[1] = { 0 }; float *ycopy = NULL; int err = 0;
125 for (int order = 0; order < 2 && !err; order++) {
126 WsSim s; memset(&s, 0, sizeof s);
127 s.p = &prog; s.threads = 32u * warps; s.order = order; s.smem = smem; s.smem_bytes = omega_bf16_gemv_shared_bytes(&spec);
128 for (uint32_t i = 0; i < n; i++) y[i] = -999.0f;
129 uint64_t A[3] = { (uintptr_t)w, (uintptr_t)x, (uintptr_t)y };
130 for (unsigned i = 0; i < 3; i++) { s.cbank[(OMEGA_BF16_ARG_W + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(OMEGA_BF16_ARG_W + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); }
131 s.cbank[OMEGA_BF16_ARG_RG / 4] = n / rows; s.cbank[OMEGA_BF16_ARG_STRIDE / 4] = grid_x * warps;
132 s.nranges = 3;
133 s.ranges[0].lo = (uintptr_t)w; s.ranges[0].hi = (uintptr_t)w + (size_t)n * k * 2;
134 s.ranges[1].lo = (uintptr_t)x; s.ranges[1].hi = (uintptr_t)x + (size_t)k * 4;
135 s.ranges[2].lo = (uintptr_t)y; s.ranges[2].hi = (uintptr_t)y + (size_t)n * 4;
136 err = ws_run(&s, grid_x, 1);
137 if (err) { snprintf(msg, msgn, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; }
138 g_sim_issues += s.warp_issues;
139 size_t unwritten = 0; for (uint32_t i = 0; i < n; i++) if (y[i] == -999.0f) unwritten++;
140 if (unwritten) { err = -1; snprintf(msg, msgn, "%zu output words never written", unwritten); break; }
141 if (order == 0) { ycopy = malloc((size_t)n * 4); memcpy(ycopy, y, (size_t)n * 4); }
142 else if (memcmp(ycopy, y, (size_t)n * 4) != 0) { err = -1; snprintf(msg, msgn, "the fragment order changed the result"); }
143 }
144 (void)first;
145 if (!err) { size_t f; *bad = compare_emu(w, x, n, k, y, &f); snprintf(msg, msgn, "%zu of %u rows differ from the emulation%s", *bad, n, *bad ? "" : " (bit-identical)"); }
146 free(ycopy); free(w); free(x); free(y); free(smem);
147 return err;
148}
149/* ROWS form (spec.ystride, sc #642 prefill; xb 0): grid_y = T token rows; row r reads x + r * k and writes y + r * ystride.
150 * XBATCH form (spec.xbatch = xb = T, L312 lever 1): one CTA row stages all T x rows and every weight read feeds them all.
151 * Each row must equal the one-row emulation of its own x, and the ystride - n gap after every row stays untouched. */
152static int sim_multi_point(uint32_t k, uint32_t n, uint32_t T, uint32_t xb, uint32_t ystride, uint32_t grid_x, int mutant, uint32_t seed, char *msg, size_t msgn, size_t *bad) {
153 static BlackwellIRProgram prog;
154 const uint32_t rows = 1, warps = 8, depth = 4; /* the host simulator runs up to 8 warps, so k (xb * k for XBATCH) <= 4096 here */
155 OmegaBf16GemvSpec spec = { k, rows, warps, depth, mutant, ystride, xb, 0 };
156 if (omega_blackwell_codegen_bf16_gemv_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
157 uint16_t *w = aligned_alloc(16, (size_t)n * k * 2); { uint16_t *t = make_w((size_t)n * k, seed); memcpy(w, t, (size_t)n * k * 2); free(t); }
158 float *x = aligned_alloc(16, (size_t)T * k * 4);
159 for (uint32_t r = 0; r < T; r++) { float *t = make_x(k, seed + 1 + r); memcpy(x + (size_t)r * k, t, (size_t)k * 4); free(t); }
160 const size_t ywords = (size_t)T * ystride;
161 float *y = aligned_alloc(16, (ywords * 4 + 15) & ~15ull);
162 uint8_t *smem = aligned_alloc(16, omega_bf16_gemv_shared_bytes(&spec));
163 for (size_t i = 0; i < ywords; i++) y[i] = -999.0f;
164 WsSim s; memset(&s, 0, sizeof s);
165 s.p = &prog; s.threads = 32u * warps; s.smem = smem; s.smem_bytes = omega_bf16_gemv_shared_bytes(&spec);
166 uint64_t A[3] = { (uintptr_t)w, (uintptr_t)x, (uintptr_t)y };
167 for (unsigned i = 0; i < 3; i++) { s.cbank[(OMEGA_BF16_ARG_W + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(OMEGA_BF16_ARG_W + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); }
168 s.cbank[OMEGA_BF16_ARG_RG / 4] = n / rows; s.cbank[OMEGA_BF16_ARG_STRIDE / 4] = grid_x * warps;
169 s.nranges = 3;
170 s.ranges[0].lo = (uintptr_t)w; s.ranges[0].hi = (uintptr_t)w + (size_t)n * k * 2;
171 s.ranges[1].lo = (uintptr_t)x; s.ranges[1].hi = (uintptr_t)x + (size_t)T * k * 4;
172 s.ranges[2].lo = (uintptr_t)y; s.ranges[2].hi = (uintptr_t)y + ywords * 4;
173 int err = ws_run(&s, grid_x, xb > 1u ? 1u : T);
174 if (err) snprintf(msg, msgn, "%s (pc %d)", s.msg, s.err_pc);
175 *bad = 0;
176 size_t gap_written = 0;
177 for (uint32_t r = 0; !err && r < T; r++) {
178 size_t f; *bad += compare_emu(w, x + (size_t)r * k, n, k, y + (size_t)r * ystride, &f);
179 for (uint32_t i = n; i < ystride; i++) if (y[(size_t)r * ystride + i] != -999.0f) gap_written++;
180 }
181 if (!err && gap_written) { err = -1; snprintf(msg, msgn, "%zu words written inside the ystride gap", gap_written); }
182 else if (!err) snprintf(msg, msgn, "%zu of %u rows x %u tokens differ from the emulation%s", *bad, n, T, *bad ? "" : " (bit-identical)");
183 free(w); free(x); free(y); free(smem);
184 return err;
185}
186/* MASK form (HUM L314 exact head): rows with hi >= tau must equal the plain kernel's row bit for bit (the emulation),
187 * every other row must hold -inf. hi covers below / equal / above tau and -0 against tau = +0 (a candidate). */
188static int sim_mask_point(uint32_t k, uint32_t n, uint32_t warps, uint32_t depth, uint32_t grid_x, int mutant, uint32_t seed,
189 char *msg, size_t msgn, size_t *bad, size_t *cands) {
190 static BlackwellIRProgram prog;
191 OmegaBf16GemvSpec spec = { k, 1, warps, depth, mutant, 0, 0, 1 };
192 if (omega_blackwell_codegen_bf16_gemv_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
193 uint16_t *w = aligned_alloc(16, (size_t)n * k * 2); { uint16_t *t = make_w((size_t)n * k, seed); memcpy(w, t, (size_t)n * k * 2); free(t); }
194 float *x = aligned_alloc(16, (size_t)k * 4); { float *t = make_x(k, seed + 1); memcpy(x, t, (size_t)k * 4); free(t); }
195 float *y = aligned_alloc(16, ((size_t)n * 4 + 15) & ~15ull);
196 float *hi = aligned_alloc(16, ((size_t)n * 4 + 15) & ~15ull);
197 float *tau = aligned_alloc(16, 16);
198 uint8_t *smem = aligned_alloc(16, omega_bf16_gemv_shared_bytes(&spec));
199 const float T = seed & 1 ? 0.0f : 0.25f;
200 tau[0] = T;
201 for (uint32_t i = 0; i < n; i++) {
202 uint32_t h = (i * 2654435761u) ^ seed;
203 hi[i] = (h % 3 == 0) ? T + 0.5f : (h % 3 == 1) ? T - 0.5f : (h % 5 == 0) ? T : T - 1e-6f;
204 }
205 hi[1] = T; hi[2] = nextafterf(T, -1.0f);
206 if (T == 0.0f) hi[3] = -0.0f; /* -0 >= +0 numerically: a candidate */
207 *cands = 0;
208 for (uint32_t i = 0; i < n; i++) *cands += hi[i] >= T;
209 int err = 0;
210 for (int order = 0; order < 2 && !err; order++) {
211 WsSim s; memset(&s, 0, sizeof s);
212 s.p = &prog; s.threads = 32u * warps; s.order = order; s.smem = smem; s.smem_bytes = omega_bf16_gemv_shared_bytes(&spec);
213 for (uint32_t i = 0; i < n; i++) y[i] = -999.0f;
214 uint64_t A[5] = { (uintptr_t)w, (uintptr_t)x, (uintptr_t)y, 0, (uintptr_t)hi };
215 for (unsigned i = 0; i < 3; i++) { s.cbank[(OMEGA_BF16_ARG_W + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(OMEGA_BF16_ARG_W + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); }
216 s.cbank[OMEGA_BF16_ARG_HI / 4] = (uint32_t)A[4]; s.cbank[OMEGA_BF16_ARG_HI / 4 + 1] = (uint32_t)(A[4] >> 32);
217 s.cbank[OMEGA_BF16_ARG_TAU / 4] = (uint32_t)(uintptr_t)tau; s.cbank[OMEGA_BF16_ARG_TAU / 4 + 1] = (uint32_t)((uint64_t)(uintptr_t)tau >> 32);
218 s.cbank[OMEGA_BF16_ARG_RG / 4] = n; s.cbank[OMEGA_BF16_ARG_STRIDE / 4] = grid_x * warps;
219 s.nranges = 5;
220 s.ranges[0].lo = (uintptr_t)w; s.ranges[0].hi = (uintptr_t)w + (size_t)n * k * 2;
221 s.ranges[1].lo = (uintptr_t)x; s.ranges[1].hi = (uintptr_t)x + (size_t)k * 4;
222 s.ranges[2].lo = (uintptr_t)y; s.ranges[2].hi = (uintptr_t)y + (size_t)n * 4;
223 s.ranges[3].lo = (uintptr_t)hi; s.ranges[3].hi = (uintptr_t)hi + (size_t)n * 4;
224 s.ranges[4].lo = (uintptr_t)tau; s.ranges[4].hi = (uintptr_t)tau + 4;
225 err = ws_run(&s, grid_x, 1);
226 if (err) { snprintf(msg, msgn, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; }
227 g_sim_issues += s.warp_issues;
228 size_t b = 0;
229 for (uint32_t i = 0; i < n; i++) {
230 float want = hi[i] >= T ? emu_row(w + (size_t)i * k, x, k) : -INFINITY;
231 b += memcmp(&want, &y[i], 4) != 0;
232 }
233 *bad = b;
234 snprintf(msg, msgn, "%zu candidates of %u rows, %zu rows wrong%s", *cands, n, b, b ? "" : " (candidates bit-identical, the rest -inf)");
235 }
236 free(w); free(x); free(y); free(hi); free(tau); free(smem);
237 return err;
238}
239static void sim_battery(void) {
240 { /* MASK form (L314): two thresholds (0 with a -0 row, 0.25), two grids, and mutant 4 must be caught */
241 static const struct { uint32_t k, n, warps, depth, grid, seed; } mp[] = { { 2048, 100, 8, 8, 2, 7 }, { 2048, 96, 8, 4, 3, 10 }, { 512, 70, 4, 2, 2, 13 } };
242 for (size_t i = 0; i < sizeof mp / sizeof mp[0]; i++) {
243 char msg[300]; size_t bad = 1, c = 0;
244 int err = sim_mask_point(mp[i].k, mp[i].n, mp[i].warps, mp[i].depth, mp[i].grid, 0, mp[i].seed, msg, sizeof msg, &bad, &c);
245 CHECK(!err && bad == 0 && c > 0 && c < mp[i].n, "sim MASK k %u n %u: %s", mp[i].k, mp[i].n, msg);
246 printf("sim MASK k %u n %u warps %u depth %u grid %u: %s %s\n", mp[i].k, mp[i].n, mp[i].warps, mp[i].depth, mp[i].grid, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
247 }
248 char msg[300]; size_t bad = 0, c = 0;
249 int err = sim_mask_point(2048, 64, 8, 4, 2, 4, 7, msg, sizeof msg, &bad, &c);
250 CHECK(!err && bad != 0, "sim MASK mutant 4 must be caught: %s", msg);
251 printf("sim MASK mutant 4: %s %s\n", err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", msg);
252 OmegaBf16GemvSpec r2 = { 2048, 2, 8, 4, 0, 0, 0, 1 }, ys = { 2048, 1, 8, 4, 0, 64, 0, 1 }, m4 = { 2048, 1, 8, 4, 4, 0, 0, 0 };
253 CHECK(omega_bf16_gemv_prog_stats(&r2, NULL, NULL) != 0 && omega_bf16_gemv_prog_stats(&ys, NULL, NULL) != 0 &&
254 omega_bf16_gemv_prog_stats(&m4, NULL, NULL) != 0, "MASK refuses rows 2, ystride, and mutant 4 without MASK");
255 }
256 { /* ROWS form: 3 token rows, ystride n + 4 (the gap must stay untouched); and ystride == n as the graph uses it */
257 static const struct { uint32_t k, n, T, ystride, grid; } rp[] = { { 2048, 96, 3, 100, 2 }, { 4096, 96, 4, 96, 3 } };
258 for (size_t i = 0; i < sizeof rp / sizeof rp[0]; i++) {
259 char msg[300]; size_t bad = 1;
260 int err = sim_multi_point(rp[i].k, rp[i].n, rp[i].T, 0, rp[i].ystride, rp[i].grid, 0, 91 + (uint32_t)i, msg, sizeof msg, &bad);
261 CHECK(!err && bad == 0, "sim ROWS k %u n %u T %u ystride %u: %s", rp[i].k, rp[i].n, rp[i].T, rp[i].ystride, msg);
262 printf("sim ROWS k %u n %u T %u ystride %u: %s %s\n", rp[i].k, rp[i].n, rp[i].T, rp[i].ystride, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
263 }
264 }
265 { /* XBATCH form: every weight read feeds xb x rows; mutant 4 (every row reads x row 0) must be caught */
266 struct { uint32_t k, n, xb, ystride, grid; } xp[] = { { 1024, 96, 2, 100, 2 }, { 1024, 96, 4, 96, 3 }, { 2048, 64, 2, 70, 2 }, { 1024, 40, 3, 50, 2 } };
267 for (size_t i = 0; i < sizeof xp / sizeof xp[0]; i++) {
268 char msg[300]; size_t bad = 1;
269 int err = sim_multi_point(xp[i].k, xp[i].n, xp[i].xb, xp[i].xb, xp[i].ystride, xp[i].grid, 0, 61 + (uint32_t)i, msg, sizeof msg, &bad);
270 CHECK(!err && bad == 0, "sim XBATCH k %u n %u xb %u ystride %u: %s", xp[i].k, xp[i].n, xp[i].xb, xp[i].ystride, msg);
271 printf("sim XBATCH k %u n %u xb %u ystride %u: %s %s\n", xp[i].k, xp[i].n, xp[i].xb, xp[i].ystride, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
272 }
273 char msg[300]; size_t bad = 0;
274 int err = sim_multi_point(1024, 64, 2, 2, 64, 2, 4, 79, msg, sizeof msg, &bad);
275 CHECK(!err && bad != 0, "sim XBATCH mutant 4 must be caught: %s", msg);
276 printf("sim XBATCH mutant 4: %s %s\n", err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", msg);
277 }
278 struct { uint32_t k, n, rows, warps, depth, grid; } pts[] = {
279 { 2048, 100, 1, 8, 8, 2 }, { 2048, 100, 1, 8, 4, 3 }, { 2048, 64, 1, 8, 1, 2 }, { 2048, 96, 2, 8, 4, 2 }, { 2048, 96, 4, 8, 4, 1 },
280 { 512, 70, 1, 8, 2, 2 }, { 512, 70, 2, 4, 1, 3 }, { 4096, 40, 1, 8, 16, 2 },
281 };
282 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
283 char msg[300]; size_t bad = 1;
284 int err = sim_point(pts[i].k, pts[i].n, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, 0, 31 + (uint32_t)i, msg, sizeof msg, &bad);
285 CHECK(!err && bad == 0, "sim k %u n %u rows %u warps %u depth %u grid %u: %s", pts[i].k, pts[i].n, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, msg);
286 printf("sim k %u n %u rows %u warps %u depth %u grid %u: %s %s\n", pts[i].k, pts[i].n, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
287 }
288 for (int m = 1; m <= 3; m++) {
289 char msg[300]; size_t bad = 0;
290 int err = sim_point(2048, 64, 1, 8, 4, 2, m, 77, msg, sizeof msg, &bad);
291 CHECK(!err && bad != 0, "sim mutant %d must be caught: %s", m, msg);
292 printf("sim mutant %d: %s %s\n", m, err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", msg);
293 }
294 printf("sim: %" PRIu64 " warp issues in total\n", g_sim_issues);
295}
296
297/* ------------------------------------------------------------- chip */
298static int g_evict = 1;
299static void evict_host_caches(void) {
300 static uint8_t *buf; const size_t sz = 512u << 20; static uint8_t pass;
301 if (!buf) buf = malloc(sz);
302 if (!buf) return;
303 memset(buf, ++pass, sz);
304 volatile uint64_t sum = 0; for (size_t i = 0; i < sz; i += 64) sum += buf[i];
305}
306static double med(uint64_t *v, uint32_t n) { qsort(v, n, 8, cmp_u64); return (double)v[n / 2]; }
307
308static int chip_run(const char *out_path, uint32_t reps, uint32_t N, uint32_t K) {
309 FILE *out = out_path ? test_out_open(out_path) : NULL;
310 if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; }
311 if (out) fprintf(out, "{\"receipt\":\"OMEGA_GPU_BF16_GEMV_V1\",\"n\":%u,\"k\":%u,\"reps\":%u,\"evict\":%d,\"spin_us\":%u,\"points\":[", N, K, reps, g_evict, omega_gpu_session_spin_us());
312 uint16_t *w = make_w((size_t)N * K, 90001); float *x = make_x(K, 90002);
313 OmegaGpuBf16Tensor *t = NULL;
314 double t0 = now_ms();
315 int rc = omega_gpu_bf16_tensor_upload(N, K, w, &t);
316 printf("upload %u x %u bf16 (%.0f MB): rc=%s %.0f ms (%s)\n", N, K, (double)N * K * 2 / 1e6, omega_gpu_bf16_rc_name(rc), now_ms() - t0, rc ? omega_gpu_bf16_last_error() : "");
317 CHECK(rc == 0, "upload");
318 if (rc) { if (out) { fprintf(out, "],\"verdict\":\"FAIL\"}\n"); fclose(out); } return 1; }
319 float *y = malloc((size_t)N * 4), *y2 = malloc((size_t)N * 4); OmegaGpuBf16Info info;
320 rc = omega_gpu_bf16_gemv_f32(t, x, y, &info);
321 CHECK(rc == 0, "gemv: %s %s", omega_gpu_bf16_rc_name(rc), rc ? omega_gpu_bf16_last_error() : "");
322 if (rc == 0) {
323 char lab[64]; snprintf(lab, sizeof lab, "gemv_f32_%ux%u", N, K);
324 out_sha_buf(lab, y, (size_t)N * 4);
325 size_t first; size_t bad = compare_emu(w, x, N, K, y, &first);
326 CHECK(bad == 0, "chip vs host emulation: %zu of %u rows differ (first %zd)", bad, N, (ssize_t)first);
327 double worst = 0, wy = 0; size_t arg_o = 0, arg_c = 0; double bo = -1e300; float bc = -1e30f;
328 for (uint32_t r = 0; r < N; r++) {
329 double yo, ys; oracle_row(w + (size_t)r * K, x, K, &yo, &ys);
330 double d = fabs((double)y[r] - yo) / (ys + 1e-30); if (d > worst) { worst = d; wy = yo; }
331 if (yo > bo) { bo = yo; arg_o = r; }
332 if (y[r] > bc) { bc = y[r]; arg_c = r; }
333 }
334 CHECK(worst < 1e-5, "chip vs double oracle: worst rel-to-scale %.2e", worst);
335 printf("parity: %zu of %u rows differ from the f32 emulation (bit compare); worst error vs double oracle %.2e of the term scale (y %.4g); argmax chip %zu oracle %zu\n", bad, N, worst, wy, arg_c, arg_o);
336 if (out) fprintf(out, "{\"point\":\"parity\",\"rows_bad\":%zu,\"worst_rel\":%.3e,\"argmax_chip\":%zu,\"argmax_oracle\":%zu}", bad, worst, arg_c, arg_o);
337 rc = omega_gpu_bf16_gemv_f32(t, x, y2, NULL);
338 CHECK(rc == 0 && memcmp(y, y2, (size_t)N * 4) == 0, "second call bit-identical");
339 printf("second call: %s\n", rc == 0 && memcmp(y, y2, (size_t)N * 4) == 0 ? "bit-identical" : "DIFFERS");
340 uint16_t *xb = malloc(K * 2); float *xf = malloc(K * 4);
341 for (uint32_t i = 0; i < K; i++) { xb[i] = bf16_rne(x[i]); xf[i] = bf16_f32(xb[i]); }
342 rc = omega_gpu_bf16_gemv_bf16(t, xb, y2, NULL);
343 size_t b2 = 0, f2; if (rc == 0) b2 = compare_emu(w, xf, N, K, y2, &f2);
344 CHECK(rc == 0 && b2 == 0, "bf16-x entry equals the emulation on the rounded x (%zu rows differ)", b2);
345 printf("bf16-x entry: %s\n", rc == 0 && b2 == 0 ? "bit-identical to the emulation" : "MISMATCH");
346 free(xb); free(xf);
347 }
348 /* timing */
349 {
350 uint64_t *ce = malloc(reps * 8), *cc = malloc(reps * 8); uint32_t done = 0; int ok = 1;
351 for (uint32_t i = 0; i < reps && ok; i++) {
352 if (g_evict) evict_host_caches();
353 if (omega_gpu_bf16_gemv_f32(t, x, y2, &info) != 0) { ok = 0; break; }
354 ce[done] = info.elapsed_ns; cc[done] = info.call_ns; done++;
355 }
356 CHECK(done == reps, "timing loop completed %u of %u", done, reps);
357 if (done) {
358 double mc = med(ce, done), ma = med(cc, done);
359 printf("timing (medians of %u, evict %d): rows %u warps %u depth %u ctas %u: chip %.1f us = %.1f GB/s on %.0f MB; call %.1f us = %.1f GB/s\n",
360 done, g_evict, info.rows_per_warp, info.warps_per_cta, info.depth, info.grid_x, mc / 1e3, (double)info.weight_bytes / mc, (double)info.weight_bytes / 1e6, ma / 1e3, (double)info.weight_bytes / ma);
361 if (out) fprintf(out, ",{\"point\":\"timing\",\"rows\":%u,\"warps\":%u,\"depth\":%u,\"ctas\":%u,\"reps\":%u,\"chip_median_ns\":%.0f,\"call_median_ns\":%.0f,\"weight_bytes\":%" PRIu64 ",\"chip_gbps\":%.2f,\"call_gbps\":%.2f,\"marker\":%u}",
362 info.rows_per_warp, info.warps_per_cta, info.depth, info.grid_x, done, mc, ma, info.weight_bytes, (double)info.weight_bytes / mc, (double)info.weight_bytes / ma, info.completion_marker);
363 }
364 free(ce); free(cc);
365 }
366 omega_gpu_bf16_tensor_free(t);
367 /* mutants on a small tensor */
368 {
369 const uint32_t MN = 4096; OmegaGpuBf16Tensor *ts = NULL;
370 rc = omega_gpu_bf16_tensor_upload(MN, K, w, &ts);
371 CHECK(rc == 0, "small tensor upload");
372 if (rc == 0) {
373 float *ys = malloc(MN * 4);
374 rc = omega_gpu_bf16_gemv_f32(ts, x, ys, NULL); size_t f; size_t b0 = rc == 0 ? compare_emu(w, x, MN, K, ys, &f) : (size_t)-1;
375 CHECK(b0 == 0, "small tensor unmutated parity (%zu rows differ)", b0);
376 for (int m = 1; m <= 3; m++) {
377 omega_gpu_bf16_test_set_mutant(m);
378 rc = omega_gpu_bf16_gemv_f32(ts, x, ys, NULL);
379 size_t b = rc == 0 ? compare_emu(w, x, MN, K, ys, &f) : (size_t)-1;
380 CHECK(b != 0, "chip mutant %d must be caught (rc %s)", m, omega_gpu_bf16_rc_name(rc));
381 printf("chip mutant %d: %s (%zu of %u rows differ)\n", m, b ? "caught" : "NOT CAUGHT", b == (size_t)-1 ? (size_t)MN : b, MN);
382 if (out) fprintf(out, ",{\"point\":\"mutant %d\",\"caught\":%d}", m, b != 0);
383 }
384 omega_gpu_bf16_test_set_mutant(0);
385 free(ys);
386 }
387 omega_gpu_bf16_tensor_free(ts);
388 }
389 free(w); free(x); free(y); free(y2);
390 if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); }
391 return g_failed ? 1 : 0;
392}
393
394int main(int argc, char **argv) {
395 int host = 0, sim = 0; const char *out_path = NULL; uint32_t reps = 0, rows = 0, warps = 0, depth = 0, ctas = 0, spin = 2000, N = 151936, K = 2048;
396 for (int i = 1; i < argc; i++) {
397 if (strcmp(argv[i], "--host-only") == 0) host = 1;
398 else if (strcmp(argv[i], "--sim") == 0) sim = 1;
399 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
400 else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]);
401 else if (strcmp(argv[i], "--rows") == 0 && i + 1 < argc) rows = (uint32_t)atoi(argv[++i]);
402 else if (strcmp(argv[i], "--warps") == 0 && i + 1 < argc) warps = (uint32_t)atoi(argv[++i]);
403 else if (strcmp(argv[i], "--depth") == 0 && i + 1 < argc) depth = (uint32_t)atoi(argv[++i]);
404 else if (strcmp(argv[i], "--ctas") == 0 && i + 1 < argc) ctas = (uint32_t)atoi(argv[++i]);
405 else if (strcmp(argv[i], "--n") == 0 && i + 1 < argc) N = (uint32_t)atoi(argv[++i]);
406 else if (strcmp(argv[i], "--k") == 0 && i + 1 < argc) K = (uint32_t)atoi(argv[++i]);
407 else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) spin = (uint32_t)atoi(argv[++i]);
408 else if (strcmp(argv[i], "--no-evict") == 0) g_evict = 0;
409 else { printf("unknown argument %s\n", argv[i]); return 2; }
410 }
411 if (host) host_only();
412 if (sim) sim_battery();
413 if (!host && !sim) {
414 if (rows || warps || depth) omega_gpu_bf16_set_shape(rows, warps, depth);
415 if (ctas) omega_gpu_bf16_set_cta_budget(ctas);
416 (void)omega_gpu_session_set_spin_us(spin);
417 chip_run(out_path, reps ? reps : 1, N, K);
418 omega_gpu_session_close();
419 }
420 printf("gpu_bf16_gemv_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
421 return g_failed ? 1 : 0;
422}