Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/gpu_prefill_attn_test.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / gpu_prefill_attn_test.c` 669 lines ยท 47504 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * gpu_prefill_attn_test: the tensor-core causal prefill attention (omega_gpu_prefill_attn_api.h, OWN-30B L1 attention). 19 * --host-only codegen budgets (instructions <= 1024, declared registers <= 128, native SHFL.BFLY / F2FP.BF16 words 20 * against the oracle words), refusals, reference sanity. No device. 21 * --sim the kernel through tests/bw_warp_sim_pa.h (chip-exact HMMA model; EX2/RSQ/RCP exact): several 22 * (pos0, m) points incl. two chunks, warps 4 and 8, both fragment orders. Gates: appended K/V rows 23 * bit-equal to a host emulation of the decode kernel's operation order; outputs within the tolerance 24 * of a double reference over the cache contents; mutants 1-4 caught. 25 * (default) the chip: parity against the double reference; appended K/V rows bit-identical to m calls of the 26 * decode kernel on a twin cache (gate 2) and its outputs within the tolerance; second call identical; 27 * out_scale 2^64; mutants 1-4 caught; timing medians (prefill vs the per-token decode loop). 28 * Tolerance: |out - ref| <= 1e-2 * max|v| * out_scale per element. Derivation: q' and P are rounded to bf16 (relative 29 * 2^-9 each). P rounding moves each weight by <= 2^-9 relative while l sums the f32 p: <= 2^-9 * max|v| = 0.0020. 30 * q' rounding moves a score (log2 units) by <= 2^-9 * sum|q'_i k_i|; for the normed rows here (|q'|, |k| ~ sqrt(128) 31 * with random signs) that is ~2^-9 * sqrt(128) * log2(e)/sqrt(128) * ... ~ 3e-3 relative on the weights in the worst 32 * measured case; EX2/RSQ on the chip add ~2^-22. 1e-2 covers the sum with margin; the measured worst ratio is printed 33 * and the mutants miss by far more than the tolerance. 34 * Host and simulator passes are never chip results. 35 */ 36 #include "omega_gpu_prefill_attn_api.h" 37 #include "omega_gpu_session.h" 38 #include "omega_blackwell_codegen_tc.h" 39 #include "bw_warp_sim_pa.h" 40 #if defined(__has_include) 41 #if __has_include("omega_gpu_prefill.h") 42 #include "omega_gpu_prefill.h" /* W-L1G device-resident module (integration builds): GPU-cached buffers for the dev check */ 43 #define PATTN_HAVE_PF 1 44 #endif 45 #endif 46 #ifndef PATTN_HAVE_PF 47 #define PATTN_HAVE_PF 0 48 #endif 49 #include 50 #include 51 #include 52 #include 53 #include 54 #include 55 56 static int g_checks, g_failed; 57 #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 58 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 59 static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / `16777216`.0f); } 60 static 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; } 61 static const double THETA = `10000000`.0; 62 static const float EPS = 1e-6f; 63 #define QKVW OMEGA_PATTN_QKV_WORDS 64 #define OUTW OMEGA_PATTN_OUT_WORDS 65 66 /* one layer's random state */ 67 typedef struct { 68 uint32_t max_pos, pos0, m; 69 float *qkv; /* [m][5120] */ 70 float qn[128], kn[128]; 71 uint16_t *kc, *vc; /* [4][max_pos][128]: history 0..pos0-1 */ 72 } Case; 73 static void case_make(Case *c, uint32_t max_pos, uint32_t pos0, uint32_t m, uint32_t seed, float qscale) { 74 uint32_t s = seed; 75 c->max_pos = max_pos; c->pos0 = pos0; c->m = m; 76 c->qkv = malloc((size_t)m * QKVW * 4); 77 for (size_t t = 0; t < m; t++) for (size_t i = 0; i < QKVW; i++) c->qkv[t * QKVW + i] = frand(&s, -1.0f, 1.0f) * (i < 4096 ? qscale : 1.0f); 78 for (unsigned i = 0; i < 128; i++) { c->qn[i] = frand(&s, 0.8f, 1.2f); c->kn[i] = frand(&s, 0.8f, 1.2f); } 79 size_t words = (size_t)4 * max_pos * 128; 80 c->kc = calloc(words, 2); c->vc = calloc(words, 2); 81 for (unsigned g = 0; g < 4; g++) for (uint32_t t = 0; t < pos0; t++) for (unsigned i = 0; i < 128; i++) { 82 size_t o = ((size_t)g * max_pos + t) * 128 + i; 83 c->kc[o] = omega_dattn_bf16_rne(frand(&s, -1.0f, 1.0f)); c->vc[o] = omega_dattn_bf16_rne(frand(&s, -1.0f, 1.0f)); 84 } 85 } 86 static void case_free(Case *c) { free(c->qkv); free(c->kc); free(c->vc); } 87 88 /* ---- host emulation of the decode kernel's K/V row (float, the kernel's operation order; RSQ exact as in the simulator) */ 89 static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 90 static void emu_rows(const float *qkv_row, const float *kn, float eps, uint32_t pos, double theta, uint16_t *krow /*[4][128]*/, uint16_t *vrow) { 91 float rope[128]; omega_dattn_rope_row(pos, theta, rope); 92 for (unsigned gk = 0; gk < 4; gk++) { 93 float x[128], sq[32]; 94 for (unsigned i = 0; i < 128; i++) x[i] = qkv_row[4096 + gk * 128 + i]; 95 for (unsigned L = 0; L < 32; L++) { float a = x[4 * L] * x[4 * L]; for (unsigned i = 1; i < 4; i++) a = fmaf(x[4 * L + i], x[4 * L + i], a); sq[L] = a; } 96 for (unsigned o = 16; o >= 1; o /= 2) { float nx[32]; for (unsigned L = 0; L < 32; L++) nx[L] = sq[L] + (L + o < 32 ? sq[L + o] : sq[L]); memcpy(sq, nx, sizeof sq); } 97 float rs = ws_mufu_rsq(fmaf(sq[0], u2f(0x3c000000u), eps)); /* MUFU.RSQ, bw_mufu_ftz.h (#462); eps > 0 keeps it normal */ 98 for (unsigned i = 0; i < 128; i++) { x[i] = x[i] * rs; x[i] = x[i] * kn[i]; } 99 float y[128]; 100 for (unsigned i = 0; i < 128; i++) { 101 unsigned j = i % 64; float cs = rope[j], sn = rope[64 + j]; float part = x[i < 64 ? i + 64 : i - 64]; 102 float ts = x[i] * cs; y[i] = fmaf(part, i < 64 ? -sn : sn, ts); 103 } 104 for (unsigned i = 0; i < 128; i++) { krow[gk * 128 + i] = omega_dattn_bf16_rne(y[i]); vrow[gk * 128 + i] = omega_dattn_bf16_rne(qkv_row[4096 + 512 + gk * 128 + i]); } 105 } 106 } 107 108 /* ---- double reference over the cache contents (kc/vc hold positions 0 .. pos0+m-1 after the call) */ 109 static void norm_rope_d(double *x, const float *w, float eps, uint32_t pos) { 110 double ss = 0; for (unsigned i = 0; i < 128; i++) ss += x[i] * x[i]; 111 double r = 1.0 / sqrt(ss / 128.0 + (double)eps); 112 for (unsigned i = 0; i < 128; i++) x[i] = x[i] * r * (double)w[i]; 113 double y[128]; 114 for (unsigned i = 0; i < 64; i++) { 115 double ang = (double)pos * pow(THETA, -(double)(2 * i) / 128.0), cs = cos(ang), sn = sin(ang); 116 y[i] = x[i] * cs - x[i + 64] * sn; y[i + 64] = x[i + 64] * cs + x[i] * sn; 117 } 118 memcpy(x, y, sizeof y); 119 } 120 static void reference(const Case *c, const uint16_t *kc, const uint16_t *vc, float out_scale, double *out /*[m][4096]*/) { 121 double *p = malloc(((size_t)c->pos0 + c->m) * sizeof(double)); 122 for (uint32_t t = 0; t < c->m; t++) { 123 const uint32_t pos = c->pos0 + t; 124 for (unsigned h = 0; h < 32; h++) { 125 unsigned gk = h / 8; double q[128]; 126 for (unsigned i = 0; i < 128; i++) q[i] = c->qkv[(size_t)t * QKVW + h * 128 + i]; 127 norm_rope_d(q, c->qn, EPS, pos); 128 double mx = -1e300; 129 for (uint32_t k = 0; k <= pos; k++) { 130 const uint16_t *kr = kc + ((size_t)gk * c->max_pos + k) * 128; 131 double s = 0; for (unsigned i = 0; i < 128; i++) s += q[i] * (double)omega_dattn_bf16_f32(kr[i]); 132 s /= sqrt(128.0); p[k] = s; if (s > mx) mx = s; 133 } 134 double l = 0, o[128]; memset(o, 0, sizeof o); 135 for (uint32_t k = 0; k <= pos; k++) { p[k] = exp(p[k] - mx); l += p[k]; } 136 for (uint32_t k = 0; k <= pos; k++) { const uint16_t *vr = vc + ((size_t)gk * c->max_pos + k) * 128; for (unsigned i = 0; i < 128; i++) o[i] += p[k] * (double)omega_dattn_bf16_f32(vr[i]); } 137 for (unsigned i = 0; i < 128; i++) out[(size_t)t * OUTW + h * 128 + i] = o[i] / l * (double)out_scale; 138 } 139 } 140 free(p); 141 } 142 static size_t parity(const Case *c, const uint16_t *kc, const uint16_t *vc, const float *out, float out_scale, double *worst) { 143 double *ref = malloc((size_t)c->m * OUTW * sizeof(double)); 144 reference(c, kc, vc, out_scale, ref); 145 double tol = 1e-2 * out_scale; size_t bad = 0; *worst = 0; 146 for (size_t i = 0; i < (size_t)c->m * OUTW; i++) { double d = fabs((double)out[i] - ref[i]); if (!(d <= tol)) bad++; if (d / tol > *worst || d != d) *worst = d != d ? 1e9 : d / tol; } 147 free(ref); 148 return bad; 149 } 150 /* appended rows (cache contents at pos0 .. pos0+m-1) against the host emulation: count of differing bf16 words and the 151 * largest difference in ulps */ 152 static void rows_vs_emu(const Case *c, const uint16_t *kc, const uint16_t *vc, size_t *kdiff, size_t *vdiff, int *maxulp) { 153 *kdiff = *vdiff = 0; *maxulp = 0; 154 uint16_t kr[512], vr[512]; 155 for (uint32_t t = 0; t < c->m; t++) { 156 emu_rows(c->qkv + (size_t)t * QKVW, c->kn, EPS, c->pos0 + t, THETA, kr, vr); 157 for (unsigned gk = 0; gk < 4; gk++) for (unsigned i = 0; i < 128; i++) { 158 size_t o = ((size_t)gk * c->max_pos + c->pos0 + t) * 128 + i; 159 if (kc[o] != kr[gk * 128 + i]) { (*kdiff)++; int d = abs((int)kc[o] - (int)kr[gk * 128 + i]); if (d > *maxulp) *maxulp = d; } 160 if (vc[o] != vr[gk * 128 + i]) (*vdiff)++; 161 } 162 } 163 } 164 165 /* ------------------------------------------------------------- host only */ 166 static void host_only(void) { 167 CHECK(omega_blackwell_verify_codegen_fixtures_bfly_f2fp() == 0, "SHFL.BFLY / F2FP.BF16 golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_bfly_f2fp()); 168 for (uint32_t w = 4; w <= 8; w += 4) for (uint32_t md = 0; md <= 2; md++) for (int m = 0; m <= 4; m++) { 169 OmegaPrefillAttnSpec s = { w, m, md, 0 }; uint32_t n = 0, gp = 0; 170 int rc = omega_prefill_attn_prog_stats(&s, &n, &gp); 171 CHECK(rc == 0 && n <= 1024 && omega_bw_gpr_declared(gp) <= 128, "codegen warps %u mode %u mutant %d: rc %d insns %u gprs %u", w, md, m, rc, n, gp); 172 static uint8_t code[1024 * 16]; uint32_t kg = 0; 173 size_t sz = omega_gpu_prefill_attn_encode(&s, code, sizeof code, &kg); 174 CHECK(sz == 16u * ((n + 7u) & ~7u) && kg > 0, "encode warps %u mode %u mutant %d: size %zu for %u instructions", w, md, m, sz, n); 175 if (rc == 0 && m == 0) printf("codegen warps %u mode %u: %u instructions, %u registers (declared %u, %u threads -> %u of 65536)\n", 176 w, md, n, gp, omega_bw_gpr_declared(gp), 32 * w, 32 * w * omega_bw_gpr_declared(gp)); 177 } 178 { OmegaPrefillAttnSpec s = { 2, 0, 0, 0 }; CHECK(omega_prefill_attn_prog_stats(&s, NULL, NULL) != 0, "warps 2 refused"); 179 s.warps = 8; s.mutant = 5; CHECK(omega_prefill_attn_prog_stats(&s, NULL, NULL) != 0, "mutant 5 refused"); 180 s.mutant = 0; s.mode = 3; CHECK(omega_prefill_attn_prog_stats(&s, NULL, NULL) != 0, "mode 3 refused"); 181 s.mode = OMEGA_PATTN_MODE_ATTEND; s.mirror = 1; CHECK(omega_prefill_attn_prog_stats(&s, NULL, NULL) != 0, "mirror outside APPEND refused"); 182 s.mode = OMEGA_PATTN_MODE_APPEND; uint32_t n = 0, gp = 0; 183 CHECK(omega_prefill_attn_prog_stats(&s, &n, &gp) == 0 && n <= 1024 && omega_bw_gpr_declared(gp) <= 128, "APPEND + mirror in budget: %u insns %u gprs", n, gp); 184 printf("codegen warps 8 APPEND + mirror: %u instructions, %u registers\n", n, gp); } 185 CHECK(omega_gpu_prefill_attn(NULL, 0, 0, 1, NULL, EPS, 1.0f, NULL, NULL) == OMEGA_GPU_DATTN_BAD_ARGS, "NULL cache refused"); 186 CHECK(OMEGA_PATTN_SHARED_BYTES <= 16384u, "shared memory within the QMD cap"); 187 /* reference sanity: a single token at position 0 returns its own v row (rounded) */ 188 { Case c; case_make(&c, 256, 0, 1, 5, 1.0f); uint16_t kr[512], vr[512]; 189 emu_rows(c.qkv, c.kn, EPS, 0, THETA, kr, vr); 190 for (unsigned gk = 0; gk < 4; gk++) { memcpy(c.kc + (size_t)gk * 256 * 128, kr + gk * 128, 256); memcpy(c.vc + (size_t)gk * 256 * 128, vr + gk * 128, 256); } 191 double *ref = malloc(OUTW * sizeof(double)); reference(&c, c.kc, c.vc, 1.0f, ref); 192 int good = 1; for (unsigned h = 0; h < 32 && good; h++) for (unsigned i = 0; i < 128; i++) if (ref[h * 128 + i] != (double)omega_dattn_bf16_f32(vr[(h / 8) * 128 + i])) { good = 0; break; } 193 CHECK(good, "reference: one token at position 0 returns the rounded v row of its KV group"); 194 free(ref); case_free(&c); } 195 } 196 197 /* ------------------------------------------------------------- simulator */ 198 static uint64_t g_sim_issues; 199 /* mirror planes for the simulated APPEND (write) / ATTEND (read) launches */ 200 static struct { uint8_t *k, *v; uint32_t kvs; size_t plane; int write, read; } g_sm; 201 /* one simulated launch of prog over tokens p0 .. p0+mc-1 whose rows are qbuf / obuf (host memory) */ 202 static int sim_launch(const BlackwellIRProgram *prog, uint32_t W, const Case *c, uint8_t *kv, size_t plane, const float *rope, const float *norms, 203 float out_scale, float *qbuf, float *obuf, uint32_t p0, uint32_t mc, int order, char *msg, size_t n) { 204 static uint8_t smem[OMEGA_PATTN_SHARED_BYTES]; 205 WsSim s; memset(&s, 0, sizeof s); 206 s.p = prog; s.threads = 32 * W; s.order = order; s.smem = smem; s.smem_bytes = sizeof smem; 207 uint64_t A[7] = { (uintptr_t)qbuf, (uintptr_t)kv, (uintptr_t)(kv + plane), (uintptr_t)rope, (uintptr_t)norms, (uintptr_t)(norms + 128), (uintptr_t)obuf }; 208 for (unsigned i = 0; i < 7; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); } 209 float fe = EPS, fo = out_scale, fq = (float)((1.0 / sqrt(128.0)) * 1.`4426950408889634`); 210 s.cbank[OMEGA_PATTN_ARG_POS0 / 4] = p0; s.cbank[OMEGA_PATTN_ARG_MC / 4] = mc; s.cbank[OMEGA_PATTN_ARG_KV_STRIDE / 4] = c->max_pos * 256; 211 memcpy(&s.cbank[OMEGA_PATTN_ARG_EPS / 4], &fe, 4); memcpy(&s.cbank[OMEGA_PATTN_ARG_OUT_SCALE / 4], &fo, 4); memcpy(&s.cbank[OMEGA_PATTN_ARG_QK_SCALE / 4], &fq, 4); 212 s.nranges = 5; 213 s.ranges[0].lo = (uintptr_t)qbuf; s.ranges[0].hi = (uintptr_t)qbuf + (size_t)mc * QKVW * 4; 214 s.ranges[1].lo = (uintptr_t)kv; s.ranges[1].hi = (uintptr_t)kv + 2 * plane; 215 s.ranges[2].lo = (uintptr_t)rope; s.ranges[2].hi = (uintptr_t)rope + (size_t)c->max_pos * 512; 216 s.ranges[3].lo = (uintptr_t)norms; s.ranges[3].hi = (uintptr_t)norms + 1024; 217 s.ranges[4].lo = (uintptr_t)obuf; s.ranges[4].hi = (uintptr_t)obuf + (size_t)mc * OUTW * 4; 218 if (g_sm.write) { 219 uint64_t k2 = (uintptr_t)g_sm.k, v2 = (uintptr_t)g_sm.v; 220 s.cbank[OMEGA_PATTN_ARG_KC2 / 4] = (uint32_t)k2; s.cbank[OMEGA_PATTN_ARG_KC2 / 4 + 1] = (uint32_t)(k2 >> 32); 221 s.cbank[OMEGA_PATTN_ARG_VC2 / 4] = (uint32_t)v2; s.cbank[OMEGA_PATTN_ARG_VC2 / 4 + 1] = (uint32_t)(v2 >> 32); 222 s.cbank[OMEGA_PATTN_ARG_KV_STRIDE2 / 4] = g_sm.kvs; 223 s.ranges[5].lo = (uintptr_t)g_sm.k; s.ranges[5].hi = (uintptr_t)g_sm.k + 2 * g_sm.plane; s.nranges = 6; 224 } 225 if (g_sm.read) { /* ATTEND reads only the mirror: the cache range is replaced, so a stray cache read faults */ 226 uint64_t k2 = (uintptr_t)g_sm.k, v2 = (uintptr_t)g_sm.v; 227 s.cbank[OMEGA_PATTN_ARG_KC / 4] = (uint32_t)k2; s.cbank[OMEGA_PATTN_ARG_KC / 4 + 1] = (uint32_t)(k2 >> 32); 228 s.cbank[OMEGA_PATTN_ARG_VC / 4] = (uint32_t)v2; s.cbank[OMEGA_PATTN_ARG_VC / 4 + 1] = (uint32_t)(v2 >> 32); 229 s.cbank[OMEGA_PATTN_ARG_KV_STRIDE / 4] = g_sm.kvs; 230 s.ranges[1].lo = (uintptr_t)g_sm.k; s.ranges[1].hi = (uintptr_t)g_sm.k + 2 * g_sm.plane; 231 } 232 int err = ws_run(&s, (mc + W - 1) / W, 4); 233 if (err) { snprintf(msg, n, "launch at %u: %s (pc %d)", p0, s.msg, s.err_pc); return -1; } 234 g_sim_issues += s.warp_issues; 235 return 0; 236 } 237 /* runs the API's launch plan through the simulator on host buffers; kv: [K plane | V plane] holding the history. 238 * progs[mode]; split: one APPEND + one ATTEND launch over all m tokens, else fused 64-token chunks */ 239 static int sim_call(BlackwellIRProgram *const progs[4], int split, uint32_t W, const Case *c, uint8_t *kv, size_t plane, const float *rope, const float *norms, 240 float out_scale, float *out, int order, char *msg, size_t n) { 241 float *qbuf = aligned_alloc(16, (size_t)c->m * QKVW * 4), *obuf = aligned_alloc(16, (size_t)c->m * OUTW * 4); 242 memcpy(qbuf, c->qkv, (size_t)c->m * QKVW * 4); 243 for (size_t i = 0; i < (size_t)c->m * OUTW; i++) obuf[i] = -999.0f; 244 int err = 0; 245 if (split) { 246 uint8_t *mir = NULL; 247 if (split == 2) { /* the API's mirror plan: history rows copied in, APPEND writes both, ATTEND reads the mirror */ 248 const uint32_t P = c->pos0 + c->m, Pr = (P + 31) & ~31u; 249 g_sm.kvs = Pr * 256; g_sm.plane = (size_t)4 * g_sm.kvs; 250 mir = aligned_alloc(16, 2 * g_sm.plane); memset(mir, 0, 2 * g_sm.plane); 251 g_sm.k = mir; g_sm.v = mir + g_sm.plane; 252 for (unsigned h = 0; h < 4; h++) { 253 memcpy(g_sm.k + (size_t)h * g_sm.kvs, kv + (size_t)h * c->max_pos * 256, (size_t)c->pos0 * 256); 254 memcpy(g_sm.v + (size_t)h * g_sm.kvs, kv + plane + (size_t)h * c->max_pos * 256, (size_t)c->pos0 * 256); 255 } 256 g_sm.write = 1; 257 } 258 err = sim_launch(progs[split == 2 ? 3 : OMEGA_PATTN_MODE_APPEND], W, c, kv, plane, rope, norms, out_scale, qbuf, obuf, c->pos0, c->m, order, msg, n); 259 /* APPEND writes no output */ 260 for (size_t i = 0; i < (size_t)c->m * OUTW && !err; i++) if (obuf[i] != -999.0f) { err = -1; snprintf(msg, n, "APPEND launch wrote output word %zu", i); } 261 if (split == 2 && !err) for (unsigned h = 0; h < 4 && !err; h++) { /* the mirror holds exactly the cache rows 0 .. P-1 */ 262 const size_t P = (size_t)c->pos0 + c->m; 263 if (memcmp(g_sm.k + (size_t)h * g_sm.kvs, kv + (size_t)h * c->max_pos * 256, P * 256) || 264 memcmp(g_sm.v + (size_t)h * g_sm.kvs, kv + plane + (size_t)h * c->max_pos * 256, P * 256)) { err = -1; snprintf(msg, n, "mirror rows of head %u differ from the cache", h); } 265 } 266 g_sm.write = 0; g_sm.read = split == 2; 267 if (!err) err = sim_launch(progs[OMEGA_PATTN_MODE_ATTEND], W, c, kv, plane, rope, norms, out_scale, qbuf, obuf, c->pos0, c->m, order, msg, n); 268 g_sm.read = 0; free(mir); 269 } else { 270 for (uint32_t done = 0; done < c->m && !err; done += 64) { 271 const uint32_t mc = c->m - done < 64 ? c->m - done : 64; 272 err = sim_launch(progs[OMEGA_PATTN_MODE_FUSED], W, c, kv, plane, rope, norms, out_scale, qbuf + (size_t)done * QKVW, obuf + (size_t)done * OUTW, 273 c->pos0 + done, mc, order, msg, n); 274 } 275 } 276 if (!err) { 277 size_t unwritten = 0; for (size_t i = 0; i < (size_t)c->m * OUTW; i++) if (obuf[i] == -999.0f) unwritten++; 278 if (unwritten) { snprintf(msg, n, "%zu output words never written", unwritten); err = -1; } 279 else memcpy(out, obuf, (size_t)c->m * OUTW * 4); 280 } 281 free(qbuf); free(obuf); 282 return err; 283 } 284 static int sim_point(uint32_t W, int mutant, int split, uint32_t max_pos, uint32_t pos0, uint32_t m, float qscale, uint32_t seed, int orders, 285 char *msg, size_t n, double *worst, size_t *bad) { 286 static BlackwellIRProgram p0, p1, p2, p3; 287 BlackwellIRProgram *const progs[4] = { &p0, &p1, &p2, &p3 }; 288 for (uint32_t md = 0; md < 4; md++) { 289 OmegaPrefillAttnSpec spec = { W, mutant, md == 3 ? OMEGA_PATTN_MODE_APPEND : md, md == 3 }; 290 if (omega_blackwell_codegen_prefill_attn_prog(&spec, progs[md]) != 0) { snprintf(msg, n, "codegen refused (mode %u)", md); return -1; } 291 } 292 Case c; case_make(&c, max_pos, pos0, m, seed, qscale); 293 size_t plane = (size_t)4 * max_pos * 256 + OMEGA_DATTN_PLANE_SLACK; 294 uint8_t *kv = aligned_alloc(16, 2 * plane); 295 float *rope = aligned_alloc(16, (size_t)max_pos * 512); for (uint32_t p = 0; p < max_pos; p++) omega_dattn_rope_row(p, THETA, rope + (size_t)p * 128); 296 float *norms = aligned_alloc(16, 1024); memcpy(norms, c.qn, 512); memcpy(norms + 128, c.kn, 512); 297 float *out = malloc((size_t)m * OUTW * 4), *copy = malloc((size_t)m * OUTW * 4); 298 int err = 0; 299 for (int order = 0; order < orders && !err; order++) { 300 memset(kv, 0, 2 * plane); 301 memcpy(kv, c.kc, (size_t)4 * max_pos * 256); memcpy(kv + plane, c.vc, (size_t)4 * max_pos * 256); 302 err = sim_call(progs, split, W, &c, kv, plane, rope, norms, 1.0f, out, order, msg, n); 303 if (!err && order == 0) memcpy(copy, out, (size_t)m * OUTW * 4); 304 else if (!err && memcmp(copy, out, (size_t)m * OUTW * 4) != 0) { err = -1; snprintf(msg, n, "the fragment order changed the result"); } 305 } 306 if (!err) { 307 size_t kd, vd; int mu; 308 rows_vs_emu(&c, (const uint16_t *)kv, (const uint16_t *)(kv + plane), &kd, &vd, &mu); 309 *bad = parity(&c, (const uint16_t *)kv, (const uint16_t *)(kv + plane), out, 1.0f, worst); 310 if (kd || vd) *bad += `1000000`; 311 snprintf(msg, n, "bad=%zu worst=%.3f rows: k %zu v %zu words differ from the emulation (max %d ulp)", *bad, *worst, kd, vd, mu); 312 } 313 free(kv); free(rope); free(norms); free(out); free(copy); case_free(&c); 314 return err; 315 } 316 static void sim_battery(void) { 317 char msg[300]; double worst; size_t bad; 318 struct { uint32_t w, max_pos, pos0, m; float qs; int orders, split; const char *name; } pts[] = { 319 { 8, 256, 0, 1, 1.0f, 2, 0, "one token at position 0" }, 320 { 8, 256, 0, 5, 1.0f, 2, 0, "5 tokens from 0 (partial CTA)" }, 321 { 4, 256, 37, 19, 1.0f, 2, 0, "19 tokens from 37, warps 4" }, 322 { 8, 256, 37, 19, 4.0f, 1, 0, "19 tokens from 37, sharp q (x4)" }, 323 { 8, 256, 100, 64, 1.0f, 1, 0, "64 tokens from 100 (full chunk)" }, 324 { 8, 512, 0, 70, 1.0f, 1, 0, "70 tokens from 0 (two chunks)" }, 325 { 4, 512, 200, 33, 1.0f, 1, 0, "33 tokens from 200, warps 4" }, 326 { 8, 256, 0, 1, 1.0f, 2, 1, "split: one token at position 0" }, 327 { 8, 256, 0, 5, 1.0f, 2, 1, "split: 5 tokens from 0" }, 328 { 4, 256, 37, 19, 1.0f, 2, 1, "split: 19 tokens from 37, warps 4" }, 329 { 8, 512, 0, 70, 1.0f, 1, 1, "split: 70 tokens from 0" }, 330 { 8, 512, 37, 141, 4.0f, 1, 1, "split: 141 tokens from 37, sharp q" }, 331 { 8, 256, 0, 5, 1.0f, 2, 2, "mirror: 5 tokens from 0" }, 332 { 4, 256, 37, 19, 1.0f, 2, 2, "mirror: 19 tokens from 37, warps 4" }, 333 { 8, 512, 37, 141, 4.0f, 1, 2, "mirror: 141 tokens from 37, sharp q" }, 334 { 8, 512, 300, 45, 1.0f, 1, 2, "mirror: 45 tokens from 300" }, 335 }; 336 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { 337 int err = sim_point(pts[i].w, 0, pts[i].split, pts[i].max_pos, pts[i].pos0, pts[i].m, pts[i].qs, 100 + (uint32_t)i, pts[i].orders, msg, sizeof msg, &worst, &bad); 338 CHECK(err == 0 && bad == 0, "sim %s: %s", pts[i].name, msg); 339 printf("sim %-40s W=%u: %s %s\n", pts[i].name, pts[i].w, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); 340 fflush(stdout); 341 } 342 for (int split = 0; split <= 2; split++) for (int m = 1; m <= 4; m++) { 343 int err = sim_point(8, m, split, 256, 40, 24, 1.0f, 200, 1, msg, sizeof msg, &worst, &bad); 344 int caught = err != 0 || bad > 0; 345 CHECK(caught, "sim %s mutant %d NOT caught (%s)", split == 2 ? "mirror" : split ? "split" : "fused", m, msg); 346 printf("sim %s mutant %d at 24 tokens from 40: %s (%s)\n", split == 2 ? "mirror" : split ? "split" : "fused", m, caught ? "caught" : "MISSED", msg); 347 } 348 printf("sim: %" PRIu64 " warp issues in total\n", g_sim_issues); 349 } 350 351 /* ------------------------------------------------------------- chip */ 352 static FILE *g_json; 353 static int g_warm; /* untimed calls before each timing series (--warm N) */ 354 static int g_cached; /* --cached: device-entry checks use omega_gpu_pf_alloc_dev buffers (needs PATTN_HAVE_PF) */ 355 static int g_mirror_on = 1; 356 static int g_fused; /* --fused: the cut-1 fused path, which never builds a K/V mirror */ 357 /* the mirror contract: forget the cache's K/V mirrors before freeing it */ 358 static void cache_free(OmegaGpuDecodeAttnCache *c) { omega_gpu_prefill_attn_kv_mirror_forget(c); omega_gpu_decode_attn_cache_free(c); } 359 static int cache_with_history(const Case *c, OmegaGpuDecodeAttnCache **out) { 360 int rc = omega_gpu_decode_attn_cache_alloc(1, c->max_pos, THETA, out); 361 if (rc != OMEGA_GPU_DATTN_OK) return rc; 362 rc = omega_gpu_decode_attn_set_norms(*out, 0, c->qn, c->kn); 363 for (unsigned g = 0; g < 4 && rc == OMEGA_GPU_DATTN_OK && c->pos0; g++) 364 rc = omega_gpu_decode_attn_cache_write(*out, 0, g, 0, c->pos0, c->kc + (size_t)g * c->max_pos * 128, c->vc + (size_t)g * c->max_pos * 128); 365 return rc; 366 } 367 /* reads rows pos0 .. pos0+m-1 of every KV head into the case-shaped arrays kc/vc (history copied from the case) */ 368 static int read_rows(OmegaGpuDecodeAttnCache *cc, const Case *c, uint16_t *kc, uint16_t *vc) { 369 memcpy(kc, c->kc, (size_t)4 * c->max_pos * 256); memcpy(vc, c->vc, (size_t)4 * c->max_pos * 256); 370 for (unsigned g = 0; g < 4; g++) for (uint32_t t = 0; t < c->m; t++) { 371 size_t o = ((size_t)g * c->max_pos + c->pos0 + t) * 128; 372 if (omega_gpu_decode_attn_cache_read(cc, 0, g, c->pos0 + t, kc + o, vc + o) != OMEGA_GPU_DATTN_OK) return -1; 373 } 374 return 0; 375 } 376 static int chip_point(uint32_t max_pos, uint32_t pos0, uint32_t m, float qscale, uint32_t seed, int with_decode, float out_scale, int reps) { 377 Case c; case_make(&c, max_pos, pos0, m, seed, qscale); 378 OmegaGpuDecodeAttnCache *pc = NULL, *dc = NULL; 379 float *out = malloc((size_t)m * OUTW * 4), *out2 = malloc((size_t)m * OUTW * 4), *dout = malloc((size_t)m * OUTW * 4); 380 uint16_t *kc = malloc((size_t)4 * max_pos * 256), *vc = malloc((size_t)4 * max_pos * 256); 381 uint16_t *dk = malloc((size_t)4 * max_pos * 256), *dv = malloc((size_t)4 * max_pos * 256); 382 int ok = 1; 383 OmegaGpuPrefillAttnInfo info; 384 int rc = cache_with_history(&c, &pc); 385 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn(pc, 0, pos0, m, c.qkv, EPS, out_scale, out, &info); 386 CHECK(rc == OMEGA_GPU_DATTN_OK, "chip prefill pos0 %u m %u: rc %s (%s)", pos0, m, omega_gpu_decode_attn_rc_name(rc), omega_gpu_decode_attn_last_error()); 387 if (rc != OMEGA_GPU_DATTN_OK) { ok = 0; goto done; } 388 CHECK(read_rows(pc, &c, kc, vc) == 0, "read rows"); 389 double worst; size_t bad = parity(&c, kc, vc, out, out_scale, &worst); 390 size_t kd, vd; int mu; rows_vs_emu(&c, kc, vc, &kd, &vd, &mu); 391 CHECK(bad == 0, "chip pos0 %u m %u: %zu outputs outside the tolerance (worst %.3f)", pos0, m, bad, worst); 392 CHECK(vd == 0 && mu <= 1, "chip pos0 %u m %u: rows vs host emulation: k %zu differ (max %d ulp), v %zu differ", pos0, m, kd, mu, vd); 393 printf("chip pos0 %5u m %5u: parity bad=%zu worst=%.3f; rows vs emulation k %zu (max %d ulp) v %zu; chunks %u grid %ux%u x%u thr; chip %.3f ms call %.3f ms\n", 394 pos0, m, bad, worst, kd, mu, vd, info.chunks, info.grid_x, info.grid_y, info.threads, info.elapsed_ns / 1e6, info.call_ns / 1e6); 395 ok &= bad == 0 && vd == 0 && mu <= 1; 396 /* second call: same rows rewritten, output bit-identical */ 397 rc = omega_gpu_prefill_attn(pc, 0, pos0, m, c.qkv, EPS, out_scale, out2, NULL); 398 int same = rc == OMEGA_GPU_DATTN_OK && memcmp(out, out2, (size_t)m * OUTW * 4) == 0; 399 CHECK(same, "chip pos0 %u m %u: second call not bit-identical", pos0, m); 400 ok &= same; 401 double pre_med = 0, dec_ms = 0; 402 if (reps > 0) { 403 uint64_t *t = malloc(sizeof(uint64_t) * reps), *tc = malloc(sizeof(uint64_t) * reps); 404 static uint64_t lns[256], worst_l[256]; uint32_t worst_n = 0; uint64_t worst_t = 0; 405 for (int r = 0; r < g_warm; r++) omega_gpu_prefill_attn(pc, 0, pos0, m, c.qkv, EPS, out_scale, out2, NULL); 406 printf(" reps chip ms (in order, %d untimed warm calls first):", g_warm); 407 for (int r = 0; r < reps; r++) { 408 omega_gpu_prefill_attn(pc, 0, pos0, m, c.qkv, EPS, out_scale, out2, &info); t[r] = info.elapsed_ns; tc[r] = info.call_ns; 409 uint32_t nl = omega_gpu_prefill_attn_test_launch_ns(lns, 256); 410 printf(" %.3f", t[r] / 1e6); 411 if (t[r] > worst_t) { worst_t = t[r]; worst_n = nl < 256 ? nl : 256; memcpy(worst_l, lns, worst_n * sizeof *lns); } 412 } 413 printf("\n slowest rep, per launch ms (%u launches):", worst_n); 414 for (uint32_t i = 0; i < worst_n; i++) printf(" %.3f", worst_l[i] / 1e6); 415 printf("\n"); 416 qsort(t, reps, sizeof *t, cmp_u64); qsort(tc, reps, sizeof *tc, cmp_u64); 417 pre_med = t[reps / 2] / 1e6; 418 double flops = 0; for (uint32_t i = 0; i < m; i++) flops += 4.0 * 32 * 128 * (pos0 + i + 1.0); 419 printf(" timing prefill pos0 %u m %u: chip median %.3f ms (min %.3f) call median %.3f ms; %.2f useful TFLOPS on chip time\n", 420 pos0, m, pre_med, t[0] / 1e6, tc[reps / 2] / 1e6, flops / (pre_med * 1e-3) / 1e12); 421 if (g_json) fprintf(g_json, " {\"pos0\": %u, \"m\": %u, \"prefill_chip_ms_median\": %.4f, \"prefill_call_ms_median\": %.4f, \"useful_tflops\": %.3f},\n", pos0, m, pre_med, tc[reps / 2] / 1e6, flops / (pre_med * 1e-3) / 1e12); 422 free(t); free(tc); 423 } 424 if (with_decode) { 425 /* gate 2: m decode steps on a twin cache: rows bit-identical, outputs within the tolerance of each other */ 426 rc = cache_with_history(&c, &dc); 427 uint64_t chip = 0, call = 0; OmegaGpuDecodeAttnInfo di; 428 for (uint32_t i = 0; i < m && rc == OMEGA_GPU_DATTN_OK; i++) { rc = omega_gpu_decode_attn(dc, 0, pos0 + i, c.qkv + (size_t)i * QKVW, EPS, out_scale, dout + (size_t)i * OUTW, &di); chip += di.elapsed_ns; call += di.call_ns; } 429 CHECK(rc == OMEGA_GPU_DATTN_OK, "decode twin: rc %s (%s)", omega_gpu_decode_attn_rc_name(rc), omega_gpu_decode_attn_last_error()); 430 if (rc == OMEGA_GPU_DATTN_OK && read_rows(dc, &c, dk, dv) == 0) { 431 size_t rk = 0, rv = 0; 432 for (unsigned g = 0; g < 4; g++) for (uint32_t t2 = 0; t2 < m; t2++) for (unsigned i = 0; i < 128; i++) { 433 size_t o = ((size_t)g * max_pos + pos0 + t2) * 128 + i; rk += kc[o] != dk[o]; rv += vc[o] != dv[o]; } 434 double dw = 0; size_t dbad = 0, tol_n = 0; 435 for (size_t i = 0; i < (size_t)m * OUTW; i++) { double d = fabs((double)out[i] - (double)dout[i]) / (1e-2 * out_scale); if (d > dw) dw = d; if (d > 1.0) dbad++; tol_n++; } 436 CHECK(rk == 0 && rv == 0, "gate 2: prefill rows differ from the decode kernel's rows: k %zu v %zu", rk, rv); 437 CHECK(dbad == 0, "gate 2: prefill vs decode outputs: %zu outside the tolerance (worst %.3f)", dbad, dw); 438 dec_ms = chip / 1e6; 439 printf(" gate 2 vs %u decode steps: rows k %zu v %zu differ; outputs worst %.3f of tol; decode loop chip %.3f ms call %.3f ms%s\n", 440 m, rk, rv, dw, chip / 1e6, call / 1e6, pre_med > 0 ? "" : ""); 441 if (pre_med > 0) printf(" speed: prefill %.3f ms vs decode loop %.3f ms chip time: %.1fx\n", pre_med, dec_ms, dec_ms / pre_med); 442 if (g_json) fprintf(g_json, " {\"pos0\": %u, \"m\": %u, \"gate2_rows_k_diff\": %zu, \"gate2_rows_v_diff\": %zu, \"gate2_out_worst\": %.4f, \"decode_loop_chip_ms\": %.4f, \"decode_loop_call_ms\": %.4f},\n", pos0, m, rk, rv, dw, chip / 1e6, call / 1e6); 443 ok &= rk == 0 && rv == 0 && dbad == 0; 444 } else ok = 0; 445 } 446 done: 447 if (pc) cache_free(pc); 448 if (dc) cache_free(dc); 449 free(out); free(out2); free(dout); free(kc); free(vc); free(dk); free(dv); case_free(&c); 450 return ok; 451 } 452 /* device-resident entry: qkv and out in session buffers (GPU VAs); output and rows must equal the host entry's bit for bit */ 453 static int dev_buf(size_t bytes, OmegaGpuMem *m) { 454 omega_gpu_session_lock(); 455 int rc = omega_gpu_session_open() ? omega_gpu_session_alloc(bytes, m) : -1; 456 omega_gpu_session_unlock(); 457 return rc; 458 } 459 static int chip_dev(uint32_t max_pos, uint32_t pos0, uint32_t m, uint32_t seed, int reps) { 460 Case c; case_make(&c, max_pos, pos0, m, seed, 1.0f); 461 OmegaGpuDecodeAttnCache *hc = NULL, *dc = NULL; 462 float *hout = malloc((size_t)m * OUTW * 4); 463 uint16_t *hk = malloc((size_t)4 * max_pos * 256), *hv = malloc((size_t)4 * max_pos * 256); 464 uint16_t *dk = malloc((size_t)4 * max_pos * 256), *dv = malloc((size_t)4 * max_pos * 256); 465 OmegaGpuMem q, o; memset(&q, 0, sizeof q); memset(&o, 0, sizeof o); 466 float *odh = NULL; 467 int ok = 1; 468 OmegaGpuPrefillAttnInfo info, dinfo; 469 int rc = cache_with_history(&c, &hc); 470 if (rc == OMEGA_GPU_DATTN_OK) rc = cache_with_history(&c, &dc); 471 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn(hc, 0, pos0, m, c.qkv, EPS, 1.0f, hout, &info); 472 CHECK(rc == OMEGA_GPU_DATTN_OK, "dev point %u+%u: host entry rc %s", pos0, m, omega_gpu_decode_attn_rc_name(rc)); 473 if (rc != OMEGA_GPU_DATTN_OK) { ok = 0; goto done; } 474 #if PATTN_HAVE_PF 475 if (g_cached) { /* W-L1G's GPU-cached device-private buffers, filled and read only through GPU copies */ 476 CHECK(omega_gpu_pf_alloc_dev((size_t)m * QKVW * 4, &q) == OMEGA_GPU_PF_OK && omega_gpu_pf_alloc_dev((size_t)m * OUTW * 4, &o) == OMEGA_GPU_PF_OK, "dev point: cached device buffers"); 477 if (!q.va || !o.va) { ok = 0; goto done; } 478 odh = malloc((size_t)m * OUTW * 4); 479 for (size_t i = 0; i < (size_t)m * OUTW; i++) odh[i] = -999.0f; 480 CHECK(omega_gpu_pf_upload(&q, 0, c.qkv, (size_t)m * QKVW * 4) == OMEGA_GPU_PF_OK && omega_gpu_pf_upload(&o, 0, odh, (size_t)m * OUTW * 4) == OMEGA_GPU_PF_OK, "dev point: upload through the copy kernel"); 481 } else 482 #endif 483 { 484 CHECK(dev_buf((size_t)m * QKVW * 4, &q) == 0 && dev_buf((size_t)m * OUTW * 4, &o) == 0, "dev point: session buffers"); 485 if (!q.cpu || !o.cpu) { ok = 0; goto done; } 486 memcpy(q.cpu, c.qkv, (size_t)m * QKVW * 4); 487 float *op = o.cpu; for (size_t i = 0; i < (size_t)m * OUTW; i++) op[i] = -999.0f; 488 omega_gpu_session_lock(); omega_gpu_session_sync_to_gpu(&q); omega_gpu_session_sync_to_gpu(&o); omega_gpu_session_unlock(); 489 } 490 /* negative control: misaligned and zero VAs are refused before any launch */ 491 CHECK(omega_gpu_prefill_attn_dev(dc, 0, pos0, m, q.va + 4, EPS, 1.0f, o.va, NULL) == OMEGA_GPU_DATTN_BAD_ARGS, "dev: misaligned qkv VA refused"); 492 CHECK(omega_gpu_prefill_attn_dev(dc, 0, pos0, m, q.va, EPS, 1.0f, 0, NULL) == OMEGA_GPU_DATTN_BAD_ARGS, "dev: zero out VA refused"); 493 rc = omega_gpu_prefill_attn_dev(dc, 0, pos0, m, q.va, EPS, 1.0f, o.va, &dinfo); 494 CHECK(rc == OMEGA_GPU_DATTN_OK, "dev point %u+%u: rc %s (%s)", pos0, m, omega_gpu_decode_attn_rc_name(rc), omega_gpu_decode_attn_last_error()); 495 if (rc != OMEGA_GPU_DATTN_OK) { ok = 0; goto done; } 496 float *od; 497 #if PATTN_HAVE_PF 498 if (g_cached) { CHECK(omega_gpu_pf_download(&o, 0, odh, (size_t)m * OUTW * 4) == OMEGA_GPU_PF_OK, "dev point: download through the copy kernel"); od = odh; } 499 else 500 #endif 501 { omega_gpu_session_lock(); omega_gpu_session_sync_to_cpu(&o); omega_gpu_session_unlock(); od = o.cpu; } 502 size_t unw = 0, diff = 0; 503 for (size_t i = 0; i < (size_t)m * OUTW; i++) { if (od[i] == -999.0f) unw++; else if (memcmp(&od[i], &hout[i], 4)) diff++; } 504 CHECK(read_rows(hc, &c, hk, hv) == 0 && read_rows(dc, &c, dk, dv) == 0, "dev point: read rows"); 505 size_t rk = 0, rv = 0; 506 for (size_t i = 0; i < (size_t)4 * max_pos * 128; i++) { rk += hk[i] != dk[i]; rv += hv[i] != dv[i]; } 507 CHECK(unw == 0 && diff == 0, "dev point %u+%u: %zu outputs unwritten, %zu differ from the host entry", pos0, m, unw, diff); 508 CHECK(rk == 0 && rv == 0, "dev point %u+%u: cache rows differ from the host entry: k %zu v %zu", pos0, m, rk, rv); 509 ok &= unw == 0 && diff == 0 && rk == 0 && rv == 0; 510 printf("chip dev%s pos0 %5u m %5u: outputs vs host entry: %zu unwritten, %zu differ; rows k %zu v %zu differ; launches %u grid %ux%u; chip %.3f ms call %.3f ms\n", 511 g_cached ? " (cached buffers)" : "", pos0, m, unw, diff, rk, rv, dinfo.chunks, dinfo.grid_x, dinfo.grid_y, dinfo.elapsed_ns / 1e6, dinfo.call_ns / 1e6); 512 if (reps > 0) { 513 uint64_t *t = malloc(sizeof(uint64_t) * reps), *tc = malloc(sizeof(uint64_t) * reps); 514 for (int r = 0; r < g_warm; r++) omega_gpu_prefill_attn_dev(dc, 0, pos0, m, q.va, EPS, 1.0f, o.va, NULL); 515 printf(" dev reps chip ms:"); 516 for (int r = 0; r < reps; r++) { omega_gpu_prefill_attn_dev(dc, 0, pos0, m, q.va, EPS, 1.0f, o.va, &dinfo); t[r] = dinfo.elapsed_ns; tc[r] = dinfo.call_ns; printf(" %.3f", t[r] / 1e6); } 517 qsort(t, reps, sizeof *t, cmp_u64); qsort(tc, reps, sizeof *tc, cmp_u64); 518 double flops = 0; for (uint32_t i = 0; i < m; i++) flops += 4.0 * 32 * 128 * (pos0 + i + 1.0); 519 printf("\n timing dev pos0 %u m %u: chip median %.3f ms (min %.3f) call median %.3f ms; %.2f useful TFLOPS on chip time\n", 520 pos0, m, t[reps / 2] / 1e6, t[0] / 1e6, tc[reps / 2] / 1e6, flops / (t[reps / 2] * 1e-9) / 1e12); 521 if (g_json) fprintf(g_json, " {\"dev\": 1, \"pos0\": %u, \"m\": %u, \"chip_ms_median\": %.4f, \"chip_ms_min\": %.4f, \"call_ms_median\": %.4f, \"out_diff\": %zu, \"rows_diff\": %zu},\n", 522 pos0, m, t[reps / 2] / 1e6, t[0] / 1e6, tc[reps / 2] / 1e6, diff, rk + rv); 523 free(t); free(tc); 524 } 525 done: 526 #if PATTN_HAVE_PF 527 if (g_cached) { if (q.va) omega_gpu_pf_free(&q); if (o.va) omega_gpu_pf_free(&o); } else 528 #endif 529 { omega_gpu_session_lock(); if (q.cpu) omega_gpu_session_free(&q); if (o.cpu) omega_gpu_session_free(&o); omega_gpu_session_unlock(); } 530 free(odh); 531 if (hc) cache_free(hc); 532 if (dc) cache_free(dc); 533 free(hout); free(hk); free(hv); free(dk); free(dv); case_free(&c); 534 return ok; 535 } 536 /* persistent mirror (cut 5): prefill 300, decode 5, prefill 64, then a foreign rewrite of rows 0..99 and prefill 32. 537 * Cache X runs with the mirror, cache Y without (cut 3 path, the reference). Outputs must be bit-identical, except 538 * the negative control: the rewrite WITHOUT invalidate must make X differ (the stale mirror is visible), and after 539 * invalidate X must match again. */ 540 static int chip_continuation(void) { 541 if (!omega_gpu_prefill_attn_kv_mirror_available() || !g_mirror_on) { printf("chip continuation: skipped (no mirror in this build or --no-mirror)\n"); return 1; } 542 /* the mirror lives on the split path (APPEND + ATTEND): the API calls mirror_get only when not fused, so the fused 543 * path never builds one and the check has nothing to compare there. The default (split) run covers it. */ 544 if (g_fused) { printf("chip continuation: skipped (--fused: the fused path builds no K/V mirror; the split run covers it)\n"); return 1; } 545 const uint32_t MP = 1024, NA = 300, ND = 5, NB = 64, NC = 32, N = NA + ND + NB + NC; 546 Case c, junk; case_make(&c, MP, 0, N, 21, 1.0f); case_make(&junk, MP, 100, 1, 22, 1.0f); 547 OmegaGpuDecodeAttnCache *x = NULL, *y = NULL; 548 float *ox = malloc((size_t)N * OUTW * 4), *oy = malloc((size_t)N * OUTW * 4), *ox2 = malloc((size_t)NC * OUTW * 4); 549 int ok = 1, rc = cache_with_history(&c, &x); 550 if (rc == OMEGA_GPU_DATTN_OK) rc = cache_with_history(&c, &y); 551 OmegaGpuDecodeAttnInfo di; 552 OmegaGpuDecodeAttnCache *cs[2] = { x, y }; 553 float *os[2] = { ox, oy }; 554 for (int k = 0; k < 2 && rc == OMEGA_GPU_DATTN_OK; k++) { 555 omega_gpu_prefill_attn_set_kv_mirror(k == 0); 556 const float *q = c.qkv; float *o = os[k]; 557 rc = omega_gpu_prefill_attn(cs[k], 0, 0, NA, q, EPS, 1.0f, o, NULL); 558 for (uint32_t i = 0; i < ND && rc == OMEGA_GPU_DATTN_OK; i++) 559 rc = omega_gpu_decode_attn(cs[k], 0, NA + i, q + (size_t)(NA + i) * QKVW, EPS, 1.0f, o + (size_t)(NA + i) * OUTW, &di); 560 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn(cs[k], 0, NA + ND, NB, q + (size_t)(NA + ND) * QKVW, EPS, 1.0f, o + (size_t)(NA + ND) * OUTW, NULL); 561 } 562 CHECK(rc == OMEGA_GPU_DATTN_OK, "continuation: rc %s (%s)", omega_gpu_decode_attn_rc_name(rc), omega_gpu_decode_attn_last_error()); 563 if (rc != OMEGA_GPU_DATTN_OK) { ok = 0; goto done; } 564 OmegaGpuPrefillAttnMirrorView mv; 565 int vrc = omega_gpu_prefill_attn_kv_mirror_view(x, 0, &mv); 566 CHECK(vrc == OMEGA_GPU_DATTN_OK && mv.valid == NA + ND + NB && mv.kv_stride == MP * 256u, "continuation: mirror view valid %u (want %u) stride %u", mv.valid, NA + ND + NB, mv.kv_stride); 567 size_t d1 = 0; for (size_t i = 0; i < (size_t)(NA + ND + NB) * OUTW; i++) d1 += memcmp(&ox[i], &oy[i], 4) != 0; 568 CHECK(d1 == 0, "continuation: %zu outputs differ, mirror vs no mirror", d1); 569 /* foreign writer: rows 0..99 of head 0 rewritten in both caches */ 570 for (int k = 0; k < 2; k++) omega_gpu_decode_attn_cache_write(cs[k], 0, 0, 0, 100, junk.kc, junk.vc); 571 const size_t offC = (size_t)(NA + ND + NB); 572 omega_gpu_prefill_attn_set_kv_mirror(0); 573 rc = omega_gpu_prefill_attn(y, 0, offC, NC, c.qkv + offC * QKVW, EPS, 1.0f, oy + offC * OUTW, NULL); 574 omega_gpu_prefill_attn_set_kv_mirror(1); 575 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn(x, 0, offC, NC, c.qkv + offC * QKVW, EPS, 1.0f, ox2, NULL); /* no invalidate */ 576 size_t stale = 0; for (size_t i = 0; i < (size_t)NC * OUTW; i++) stale += memcmp(&ox2[i], &oy[offC * OUTW + i], 4) != 0; 577 omega_gpu_prefill_attn_kv_mirror_invalidate(x, UINT32_MAX, 0); 578 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn(x, 0, offC, NC, c.qkv + offC * QKVW, EPS, 1.0f, ox + offC * OUTW, NULL); 579 size_t d2 = 0; for (size_t i = 0; i < (size_t)NC * OUTW; i++) d2 += memcmp(&ox[offC * OUTW + i], &oy[offC * OUTW + i], 4) != 0; 580 CHECK(rc == OMEGA_GPU_DATTN_OK, "continuation C: rc %s", omega_gpu_decode_attn_rc_name(rc)); 581 CHECK(stale > 0, "negative control: a foreign rewrite without invalidate must show (stale mirror), %zu outputs differ", stale); 582 CHECK(d2 == 0, "after invalidate: %zu outputs differ, mirror vs no mirror", d2); 583 printf("chip continuation (prefill %u, decode %u, prefill %u, rewrite, prefill %u): mirror valid %u; outputs differ mirror vs none: %zu; " 584 "stale control %zu differ (must be > 0); after invalidate %zu differ\n", NA, ND, NB, NC, mv.valid, d1, stale, d2); 585 ok &= d1 == 0 && stale > 0 && d2 == 0 && vrc == OMEGA_GPU_DATTN_OK; 586 done: 587 omega_gpu_prefill_attn_set_kv_mirror(1); 588 if (x) cache_free(x); 589 if (y) cache_free(y); 590 free(ox); free(oy); free(ox2); case_free(&c); case_free(&junk); 591 return ok; 592 } 593 static void chip_mutants(void) { 594 for (int mu = 1; mu <= 4; mu++) { 595 omega_gpu_prefill_attn_test_set_mutant(mu); 596 Case c; case_make(&c, 256, 40, 24, 200, 1.0f); 597 OmegaGpuDecodeAttnCache *pc = NULL; 598 float *out = malloc((size_t)24 * OUTW * 4); 599 uint16_t *kc = malloc((size_t)4 * 256 * 256), *vc = malloc((size_t)4 * 256 * 256); 600 int rc = cache_with_history(&c, &pc); 601 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn(pc, 0, 40, 24, c.qkv, EPS, 1.0f, out, NULL); 602 int caught = rc != OMEGA_GPU_DATTN_OK; 603 double worst = 0; size_t bad = 0, kd = 0, vd = 0; int mx = 0; 604 if (!caught && read_rows(pc, &c, kc, vc) == 0) { 605 bad = parity(&c, kc, vc, out, 1.0f, &worst); 606 rows_vs_emu(&c, kc, vc, &kd, &vd, &mx); 607 caught = bad > 0 || vd > 0 || mx > 1; 608 } 609 CHECK(caught, "chip mutant %d NOT caught", mu); 610 printf("chip mutant %d: %s (rc %s, bad %zu, worst %.2f, rows k %zu v %zu)\n", mu, caught ? "caught" : "MISSED", omega_gpu_decode_attn_rc_name(rc), bad, worst, kd, vd); 611 if (pc) cache_free(pc); 612 free(out); free(kc); free(vc); case_free(&c); 613 } 614 omega_gpu_prefill_attn_test_set_mutant(0); 615 } 616 617 int main(int argc, char **argv) { 618 int host = 0, sim = 0, reps = 9, quick = 0, fused = 0, timing_only = 0; const char *json = NULL; uint32_t warps = 0; 619 for (int i = 1; i < argc; i++) { 620 if (!strcmp(argv[i], "--host-only")) host = 1; 621 else if (!strcmp(argv[i], "--sim")) sim = 1; 622 else if (!strcmp(argv[i], "--quick")) quick = 1; 623 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]); 624 else if (!strcmp(argv[i], "--warps") && i + 1 < argc) warps = (uint32_t)atoi(argv[++i]); 625 else if (!strcmp(argv[i], "--out") && i + 1 < argc) json = argv[++i]; 626 else if (!strcmp(argv[i], "--fused")) fused = g_fused = 1; 627 else if (!strcmp(argv[i], "--no-mirror")) { omega_gpu_prefill_attn_set_kv_mirror(0); g_mirror_on = 0; } 628 else if (!strcmp(argv[i], "--cached") && PATTN_HAVE_PF) g_cached = 1; 629 else if (!strcmp(argv[i], "--mirror-per-call")) omega_gpu_prefill_attn_test_set_mirror_per_call(1); 630 else if (!strcmp(argv[i], "--timing-only")) timing_only = 1; 631 else if (!strcmp(argv[i], "--warm") && i + 1 < argc) g_warm = atoi(argv[++i]); 632 else { fprintf(stderr, "usage: %s [--host-only | --sim | [--quick] [--reps N] [--warps 4|8] [--fused] [--no-mirror] [--mirror-per-call] [--cached] [--warm N] [--timing-only] [--out receipt.json]]\n", argv[0]); return 2; } 633 } 634 host_only(); 635 if (sim) sim_battery(); 636 if (!host && !sim) { 637 if (json) { g_json = fopen(json, "w"); if (g_json) fprintf(g_json, "{\"test\": \"gpu_prefill_attn\", \"points\": [\n"); } 638 if (warps) omega_gpu_prefill_attn_set_warps(warps); 639 int ok = 1; 640 if (fused) omega_gpu_prefill_attn_set_fused(1); 641 printf("path: %s, warps %u, warm %d, dev buffers %s\n", fused ? "fused 64-token chunks" : g_mirror_on && omega_gpu_prefill_attn_kv_mirror_available() ? "split APPEND + ATTEND, ATTEND reads the GPU-cached K/V mirror" : "split APPEND + ATTEND, ATTEND reads the uncached cache", warps ? warps : OMEGA_GPU_PATTN_WARPS, g_warm, g_cached ? "GPU-cached (pf_alloc_dev)" : "session (uncached)"); 642 if (timing_only) { 643 ok &= chip_point(4096, 0, 2048, 1.0f, 8, 0, 1.0f, reps); 644 ok &= chip_dev(4096, 0, 2048, 8, reps); 645 ok &= chip_dev(8192, 4096, 1500, 12, reps); 646 ok &= chip_point(4096, 4032, 64, 1.0f, 7, 0, 1.0f, reps); 647 ok &= chip_dev(8192, 4096, 64, 13, reps); 648 } else { 649 ok &= chip_point(256, 0, 1, 1.0f, 1, 1, 1.0f, 0); 650 ok &= chip_point(256, 37, 19, 1.0f, 2, 1, 1.0f, 0); 651 ok &= chip_point(256, 0, 64, 1.0f, 3, 1, 1.0f, reps); 652 ok &= chip_point(512, 100, 70, 4.0f, 4, 1, 1.0f, 0); 653 ok &= chip_point(512, 200, 64, 1.0f, 5, 1, `18446744073709551616`.0f, 0); 654 if (!quick) { 655 ok &= chip_point(1024, 0, 512, 1.0f, 6, 1, 1.0f, reps); 656 ok &= chip_point(4096, 4032, 64, 1.0f, 7, 1, 1.0f, reps); 657 ok &= chip_point(4096, 0, 2048, 1.0f, 8, 0, 1.0f, reps); 658 } 659 ok &= chip_dev(512, 100, 70, 11, 0); 660 if (!quick) { ok &= chip_dev(4096, 0, 2048, 8, reps); ok &= chip_dev(8192, 4096, 1500, 12, reps); } 661 ok &= chip_continuation(); 662 chip_mutants(); 663 } 664 (void)ok; 665 if (g_json) { fprintf(g_json, " {}\n], \"checks\": %d, \"failed\": %d}\n", g_checks, g_failed); fclose(g_json); } 666 } 667 printf("gpu_prefill_attn_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); 668 return g_failed ? 1 : 0; 669 }