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

607 lines · 42279 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * gpu_prefill_attn256_test: the head_dim 256 causal prefill attention for Qwen3.8-27B (omega-mv#545,
4 * omega_gpu_prefill_attn256_api.h), on the decode attention's cache (omega_gpu_decode_attn256_api.h).
5 * --host-only codegen budgets of both kernels (instructions <= 1024, registers within the opt-in limit and the
6 * 256-thread register file, attend SHFL.BFLY / F2FP.BF16 emitted natively), refusals, layout constants. No device.
7 * --sim both launches through tests/bw_warp_sim_pa.h (no chip): several (pos0, m) points incl. odd starts,
8 * history, a 256 boundary and full rope; both fragment orders on the small points. Gates: appended K/V
9 * rows bit-equal to m runs of the DECODE program on a twin cache (the same simulator), outputs within
10 * the tolerance of a double reference, prep mutants 1 and 3 and attend mutants 1 and 2 caught. The q'
11 * scratch starts as NaN so a read of a slot the prep launch never wrote shows up.
12 * (default) the chip: parity against the double reference (every token up to 512 tokens, 8 sampled tokens
13 * above), rows bit-identical to m decode calls on a twin cache and the next decode step on both caches
14 * bit-identical, second call identical, out_scale 2^20 exact, device entry == host entry, mutants caught,
15 * timing medians of 2048-token chunks at contexts 2k, 8k, 32k (and 128k with --max-pos 131072); JSON
16 * receipt with --out.
17 * The reference works in the model's dim order (crates/aien-inference-abi/src/qwen38.rs `attention`): zero-centred
18 * RMSNorm x * rsqrt(mean + eps) * (1 + w), RoPE on the first rotary_dim dims with pairs (j, j + rotary_dim/2), scores
19 * q.k / sqrt(256), causal softmax. The kernel sees q, k and the norm weights permuted (omega_dattn256_perm_vec) and K rows
20 * permuted; V and the output are in model order.
21 * Tolerance: |out - ref| <= 5e-3 * max|v| * out_scale per element. The reference rounds q' * (log2(e) / 16) to bf16 as
22 * the prep kernel does (from double, the kernel from f32: rare one-ulp differences) and uses its own bf16 K/V rows; what
23 * remains is P rounded to bf16 for the tensor cores (relative 2^-9 per weight, l summed from the unrounded p:
24 * <= 2^-9 * max|v| = 0.0020) and f32 / EX2 error (~2^-22). The measured worst ratio is printed; the mutants miss by far
25 * more. Host and simulator passes are never chip results.
26 */
27#include "omega_gpu_prefill_attn256_api.h"
28#include "omega_gpu_session.h"
29#include "omega_blackwell_codegen_tc.h"
30#include "bw_warp_sim_pa.h"
31#include <inttypes.h>
32#include <math.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <time.h>
37#include "test_out_file.h"
38
39static int g_checks, g_failed;
40#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
41static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
42static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
43static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
44static 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; }
45static const double THETA = 10000000.0; /* Qwen3.8-27B rope_theta */
46static const float EPS = 1e-6f;
47#define HD 256u
48/* Model shape under test: the 27B (24 q / 4 KV heads, 6 q per decode CTA row); -DPATTN_TEST_Q36 builds
49 * Qwen3.6-35B-A3B (16 / 2 / 4: 8 q per KV head, two decode CTA rows per KV head; sc L306). */
50#ifdef PATTN_TEST_Q36
51#define QH 16u
52#define KVH 2u
53#define QPC 4u
54#else
55#define QH 24u
56#define KVH 4u
57#define QPC 6u
58#endif
59#define VY (QH / QPC) /* decode / prep CTA rows */
60#define SPEC_SHAPE , .q_heads = QH, .kv_heads = KVH, .q_per_cta = QPC
61#define ATT_SHAPE , .q_heads = QH, .kv_heads = KVH
62#define QW ((QH + 2u * KVH) * HD) /* 8192 */
63#define OW (QH * HD) /* 6144 */
64#define QFP (QH * 1024u) /* q' bytes per token pair */
65#define QEND (QH * HD)
66#define KEND (QEND + KVH * HD)
67#define RB OMEGA_DATTN256_ROW_BYTES
68static const float QKS = (float)((1.0 / 16.0) * 1.4426950408889634);
69#define TOL_REL 5e-3
70
71/* one layer's random state in MODEL order: m qkv rows, norm weights w (zero-centred), history rows 0..pos0-1 */
72typedef struct {
73 uint32_t max_pos, pos0, m, rotary;
74 float *qkv; /* [m][8192] */
75 float qn[HD], kn[HD];
76 uint16_t *kc, *vc; /* [4][max_pos][256] model order */
77} Case;
78static void case_make(Case *c, uint32_t max_pos, uint32_t pos0, uint32_t m, uint32_t rotary, uint32_t seed, float qscale) {
79 uint32_t s = seed;
80 c->max_pos = max_pos; c->pos0 = pos0; c->m = m; c->rotary = rotary;
81 c->qkv = malloc((size_t)m * QW * 4);
82 for (size_t t = 0; t < m; t++) for (size_t i = 0; i < QW; i++) c->qkv[t * QW + i] = frand(&s, -1.0f, 1.0f) * (i < QEND ? qscale : 1.0f);
83 for (unsigned i = 0; i < HD; i++) { c->qn[i] = frand(&s, -0.2f, 0.2f); c->kn[i] = frand(&s, -0.2f, 0.2f); }
84 size_t words = (size_t)KVH * max_pos * HD;
85 c->kc = calloc(words, 2); c->vc = calloc(words, 2);
86 for (unsigned g = 0; g < KVH; g++) for (uint32_t t = 0; t < pos0; t++) for (unsigned i = 0; i < HD; i++) {
87 size_t o = ((size_t)g * max_pos + t) * HD + i;
88 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));
89 }
90}
91static void case_free(Case *c) { free(c->qkv); free(c->kc); free(c->vc); }
92/* the kernel's inputs: q and k heads permuted, v as is; norms 1 + w permuted */
93static void case_kernel_qkv(const Case *c, float *qkv_k) {
94 for (size_t t = 0; t < c->m; t++) {
95 const float *src = c->qkv + t * QW; float *dst = qkv_k + t * QW;
96 for (unsigned h = 0; h < QH + KVH; h++) omega_dattn256_perm_vec(src + h * HD, dst + h * HD, c->rotary);
97 memcpy(dst + KEND, src + KEND, (size_t)KVH * HD * 4);
98 }
99}
100static void case_kernel_norms(const Case *c, float *qn_k, float *kn_k) {
101 float t[HD];
102 for (unsigned i = 0; i < HD; i++) t[i] = 1.0f + c->qn[i];
103 omega_dattn256_perm_vec(t, qn_k, c->rotary);
104 for (unsigned i = 0; i < HD; i++) t[i] = 1.0f + c->kn[i];
105 omega_dattn256_perm_vec(t, kn_k, c->rotary);
106}
107/* K history rows permuted, V as is, into a [K plane | V plane] buffer laid out like the cache (plane bytes apart) */
108static void case_history_into(const Case *c, uint8_t *kv, size_t plane) {
109 for (unsigned g = 0; g < KVH; g++) for (uint32_t t = 0; t < c->pos0; t++) {
110 size_t o = ((size_t)g * c->max_pos + t);
111 omega_dattn256_perm_vec_bf16(c->kc + o * HD, (uint16_t *)(kv + o * RB), c->rotary);
112 memcpy(kv + plane + o * RB, c->vc + o * HD, RB);
113 }
114}
115
116/* ---- double-precision oracle (test-owned: libm) in MODEL order ---- */
117static void norm_rope(double *x, const float *w, float eps, uint32_t rotary, uint32_t pos) {
118 double ss = 0; for (unsigned i = 0; i < HD; i++) ss += x[i] * x[i];
119 double r = 1.0 / sqrt(ss / (double)HD + (double)eps);
120 for (unsigned i = 0; i < HD; i++) x[i] = x[i] * r * (1.0 + (double)w[i]);
121 const unsigned half = rotary / 2;
122 for (unsigned j = 0; j < half; j++) {
123 double inv = 1.0 / pow(THETA, (double)(2 * j) / (double)rotary), ang = (double)pos * inv, cs = cos(ang), sn = sin(ang);
124 double x1 = x[j], x2 = x[j + half];
125 x[j] = x1 * cs - x2 * sn; x[j + half] = x2 * cs + x1 * sn;
126 }
127}
128/* the reference K/V rows (model order, bf16) of every chunk token: kn/vn [m][4][256] */
129static void ref_rows(const Case *c, uint16_t *kn, uint16_t *vn) {
130 for (size_t t = 0; t < c->m; t++) for (unsigned g = 0; g < KVH; g++) {
131 const float *row = c->qkv + t * QW; double k[HD];
132 for (unsigned i = 0; i < HD; i++) k[i] = row[QEND + g * HD + i];
133 norm_rope(k, c->kn, EPS, c->rotary, c->pos0 + (uint32_t)t);
134 for (unsigned i = 0; i < HD; i++) { kn[(t * KVH + g) * HD + i] = omega_dattn_bf16_rne((float)k[i]); vn[(t * KVH + g) * HD + i] = omega_dattn_bf16_rne(row[KEND + g * HD + i]); }
135 }
136}
137/* reference output of chunk token t: out[6144] (* out_scale) */
138static void ref_token(const Case *c, const uint16_t *kn, const uint16_t *vn, size_t t, float out_scale, double *out) {
139 const uint32_t pos = c->pos0 + (uint32_t)t;
140 double *p = malloc(((size_t)pos + 1) * sizeof(double));
141 for (unsigned h = 0; h < QH; h++) {
142 const unsigned g = h / (QH / KVH);
143 double q[HD]; float qb[HD];
144 for (unsigned i = 0; i < HD; i++) q[i] = c->qkv[t * QW + h * HD + i];
145 norm_rope(q, c->qn, EPS, c->rotary, pos);
146 for (unsigned i = 0; i < HD; i++) qb[i] = omega_dattn_bf16_f32(omega_dattn_bf16_rne((float)(q[i] * (double)QKS)));
147 double mx = -1e300;
148 for (uint32_t j = 0; j <= pos; j++) {
149 const uint16_t *kr = j < c->pos0 ? c->kc + ((size_t)g * c->max_pos + j) * HD : kn + ((size_t)(j - c->pos0) * KVH + g) * HD;
150 double s = 0; for (unsigned i = 0; i < HD; i++) s += (double)qb[i] * (double)omega_dattn_bf16_f32(kr[i]);
151 p[j] = s; if (s > mx) mx = s;
152 }
153 double l = 0, o[HD]; memset(o, 0, sizeof o);
154 for (uint32_t j = 0; j <= pos; j++) {
155 const double w = exp2(p[j] - mx); l += w;
156 const uint16_t *vr = j < c->pos0 ? c->vc + ((size_t)g * c->max_pos + j) * HD : vn + ((size_t)(j - c->pos0) * KVH + g) * HD;
157 for (unsigned i = 0; i < HD; i++) o[i] += w * (double)omega_dattn_bf16_f32(vr[i]);
158 }
159 for (unsigned i = 0; i < HD; i++) out[h * HD + i] = o[i] / l * (double)out_scale;
160 }
161 free(p);
162}
163/* the tokens parity checks: all up to 512, else first 2, last 2 and 4 spread */
164static size_t parity_tokens(uint32_t m, uint32_t *tok) {
165 if (m <= 512) { for (uint32_t t = 0; t < m; t++) tok[t] = t; return m; }
166 const uint32_t pick[8] = { 0, 1, m / 5, (2 * m) / 5, (3 * m) / 5, (4 * m) / 5, m - 2, m - 1 };
167 memcpy(tok, pick, sizeof pick);
168 return 8;
169}
170static size_t parity(const Case *c, const uint16_t *kn, const uint16_t *vn, const float *out, float out_scale, double *worst) {
171 uint32_t *tok = malloc((size_t)(c->m > 8 ? c->m : 8) * 4); size_t nt = parity_tokens(c->m, tok);
172 double *ref = malloc(OW * sizeof(double)), tol = TOL_REL * out_scale + 1e-6 * out_scale; size_t bad = 0; *worst = 0;
173 for (size_t k = 0; k < nt; k++) {
174 ref_token(c, kn, vn, tok[k], out_scale, ref);
175 for (size_t i = 0; i < OW; i++) { double d = fabs((double)out[(size_t)tok[k] * OW + i] - ref[i]); if (!(d <= tol)) bad++; if (d / tol > *worst || d != d) *worst = d != d ? INFINITY : d / tol; }
176 }
177 free(ref); free(tok);
178 return bad;
179}
180/* the rows the kernel appended (K permuted, V model order; [4][256] per token at kchip/vchip + t * 1024 words) against
181 * the reference rows: v exact, k as below */
182static float bf16f(uint16_t h) { uint32_t u = (uint32_t)h << 16; float f; memcpy(&f, &u, 4); return f; }
183/* K rows: chip == ref, or one bf16 ulp apart (at most 32 per token), or, for a value left small by the rotation's
184 * cancellation (x1 cos - x2 sin), within 8 f32 roundings of the head row's largest |k| (8 * 2^-24 * rowmax): the
185 * chip rotates in f32, the oracle in double, and a 3-ulp gap at 7e-6 in a row of O(1) values is f32 rounding, not a
186 * kernel error (seen at 30720+2048 and 129024+2048 on 2026-10-18; the rows were bit-equal to the decode program's). */
187static int rows_ok(const Case *c, const uint16_t *kchip, const uint16_t *vchip, const uint16_t *kn, const uint16_t *vn, char *why, size_t n) {
188 unsigned flips = 0, worst_tok = 0, small = 0;
189 for (size_t t = 0; t < c->m; t++) {
190 unsigned tf = 0;
191 for (unsigned g = 0; g < KVH; g++) {
192 float rowmax = 0;
193 for (unsigned i = 0; i < HD; i++) { float a = fabsf(bf16f(kn[(t * KVH + g) * HD + i])); if (a > rowmax) rowmax = a; }
194 for (unsigned i = 0; i < HD; i++) {
195 size_t ci = (t * KVH + g) * HD + i, ri = (t * KVH + g) * HD + omega_dattn256_perm_slot(i, c->rotary);
196 if (vchip[ci] != vn[ci]) { snprintf(why, n, "token %zu v[%u][%u] chip %04x ref %04x", t, g, i, vchip[ci], vn[ci]); return 0; }
197 if (kchip[ci] == kn[ri]) continue;
198 int d = (int)kchip[ci] - (int)kn[ri];
199 if (d >= -1 && d <= 1) { tf++; continue; }
200 if (fabsf(bf16f(kchip[ci]) - bf16f(kn[ri])) <= 8.0f * 0x1p-24f * rowmax) { small++; continue; }
201 snprintf(why, n, "token %zu k[%u] slot %u chip %04x ref %04x (row max %.3g)", t, g, i, kchip[ci], kn[ri], (double)rowmax);
202 return 0;
203 }
204 }
205 if (tf > 32) { snprintf(why, n, "token %zu: %u one-ulp k flips", t, tf); return 0; }
206 if (tf > worst_tok) worst_tok = tf;
207 flips += tf;
208 }
209 snprintf(why, n, "ok (%u one-ulp k flips, worst token %u; %u small-value k gaps within 8 f32 roundings of the row max)", flips, worst_tok, small);
210 return 1;
211}
212/* ------------------------------------------------------------- host only */
213static void host_only(void) {
214 for (int m = 0; m <= 2; m++) {
215 OmegaPrefillAttn256Spec s = { .mutant = m ATT_SHAPE }; uint32_t n = 0, gp = 0;
216 int rc = omega_prefill_attn256_prog_stats(&s, &n, &gp);
217 const uint32_t dec = omega_bw_gpr_declared(gp);
218 CHECK(rc == 0 && n <= BW_MAX_IR_INSNS && gp <= BW_PHYS_GPR_HW_LIMIT && 256u * dec <= 65536u,
219 "attend codegen mutant %d: rc %d insns %u gprs %u (declared %u)", m, rc, n, gp, dec);
220 if (rc == 0 && m == 0) printf("attend: %u instructions, %u registers (declared %u, 256 threads -> %u of 65536)\n", n, gp, dec, 256 * dec);
221 }
222 { OmegaPrefillAttn256Spec s = { .mutant = 3 ATT_SHAPE }; CHECK(omega_prefill_attn256_prog_stats(&s, NULL, NULL) != 0, "attend mutant 3 refused");
223 s.mutant = -1; CHECK(omega_prefill_attn256_prog_stats(&s, NULL, NULL) != 0, "attend mutant -1 refused"); }
224 static const int pm[3] = { 0, 1, 3 };
225 for (unsigned k = 0; k < 3; k++) {
226 OmegaDecodeAttn256Spec s = { .depth = OMEGA_GPU_DATTN256_DEPTH, .mutant = pm[k], .prep = 1 SPEC_SHAPE }; uint32_t n = 0, gp = 0;
227 int rc = omega_decode_attn256_prog_stats(&s, &n, &gp);
228 CHECK(rc == 0 && n <= BW_MAX_IR_INSNS && gp <= BW_PHYS_GPR_MAX - 2, "prep codegen mutant %d: rc %d insns %u gprs %u", pm[k], rc, n, gp);
229 if (rc == 0 && k == 0) printf("prep: %u instructions, %u registers\n", n, gp);
230 }
231 { OmegaDecodeAttn256Spec s = { .depth = 4, .prep = 2 SPEC_SHAPE }; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "prep 2 refused"); }
232 static uint8_t code[1 << 17];
233 for (unsigned k = 0; k < 2; k++) {
234 uint32_t gp = 0; size_t n = omega_gpu_prefill_attn256_encode(k, code, sizeof code, &gp);
235 CHECK(n > 0 && gp > 0, "encode %s: %zu bytes, gprs %u", k ? "attend" : "prep", n, gp);
236 }
237 CHECK(omega_gpu_prefill_attn256_encode(2, code, sizeof code, NULL) == 0, "encode kind 2 refused");
238 CHECK(omega_gpu_prefill_attn256_encode(1, code, 64, NULL) == 0, "encode into a too-small buffer refused");
239 CHECK(OMEGA_PATTN256_SHARED_BYTES <= 16384u, "attend shared memory within the QMD cap");
240 CHECK(OMEGA_PATTN256_QF_OFF_SHAPE(KVH, QH / KVH, 1, 0, 0, 0, 0) == QFP && OMEGA_PATTN256_QF_OFF_SHAPE(KVH, QH / KVH, 0, KVH - 1, QH / KVH - 1, 15, 3) + 16u == QFP, "QF layout: one token pair is %u bytes, densely packed", QFP);
241 CHECK(OMEGA_PATTN256_QF_OFF(1, 0, 0, 0, 0) == OMEGA_PATTN256_QF_PAIR_BYTES, "QF layout: the 27B macro is the 4 x 6 case");
242 CHECK(omega_gpu_prefill_attn256(NULL, 0, 0, 1, (const float *)code, EPS, 1.0f, (float *)code, NULL) == OMEGA_GPU_DATTN_BAD_ARGS, "NULL cache refused");
243 CHECK(omega_gpu_prefill_attn256_reserve(0) == OMEGA_GPU_DATTN_BAD_ARGS && omega_gpu_prefill_attn256_reserve(OMEGA_GPU_PATTN256_MAX_M + 1) == OMEGA_GPU_DATTN_BAD_ARGS, "reserve 0 and above MAX_M refused before any device work");
244 /* reference sanity: one token at position 0 returns its own rounded v */
245 { Case c; case_make(&c, 256, 0, 1, 64, 11, 1.0f); uint16_t kn[KVH * HD], vn[KVH * HD]; double ref[OW];
246 ref_rows(&c, kn, vn); ref_token(&c, kn, vn, 0, 1.0f, ref);
247 int good = 1; for (unsigned h = 0; h < QH; h++) for (unsigned i = 0; i < HD; i++) good &= fabs(ref[h * HD + i] - (double)omega_dattn_bf16_f32(vn[(h / (QH / KVH)) * HD + i])) < 1e-12;
248 CHECK(good, "reference: context 1 returns the rounded v of the head's KV group");
249 case_free(&c); }
250}
251
252/* ------------------------------------------------------------- simulator */
253static uint64_t g_sim_issues;
254static uint32_t g_poison; /* --poison SEED: registers start as junk in every simulated CTA (tests/bw_warp_sim_pa.h reg_poison) */
255typedef struct {
256 const Case *c;
257 uint8_t *kv; size_t plane;
258 float *rope, *norms, *qkv_k;
259} SimBufs;
260static void cb64(WsSim *s, unsigned off, const void *p) { uint64_t a = (uintptr_t)p; s->cbank[off / 4] = (uint32_t)a; s->cbank[off / 4 + 1] = (uint32_t)(a >> 32); }
261static void cbf(WsSim *s, unsigned off, float f) { memcpy(&s->cbank[off / 4], &f, 4); }
262/* the decode program (prep 0 or 1) at position pos (prep: tokens pos .. pos+grid_x-1, qkv rows from qkv_k) */
263static int sim_decode_launch(const BlackwellIRProgram *prog, const SimBufs *b, uint32_t pos, uint32_t grid_x, const float *qkv_k, size_t qkv_bytes,
264 void *out, size_t out_bytes, float *part, size_t part_bytes, uint32_t *cnt, uint32_t splits, int order, char *msg, size_t n) {
265 static uint8_t smem[OMEGA_DATTN256_SHARED_BYTES];
266 WsSim s; memset(&s, 0, sizeof s);
267 s.p = prog; s.threads = 256; s.order = order; s.reg_poison = g_poison; s.smem = smem; s.smem_bytes = sizeof smem;
268 cb64(&s, OMEGA_DATTN256_ARG_QKV, qkv_k); cb64(&s, OMEGA_DATTN256_ARG_KC, b->kv); cb64(&s, OMEGA_DATTN256_ARG_VC, b->kv + b->plane);
269 cb64(&s, OMEGA_DATTN256_ARG_ROPE, b->rope); cb64(&s, OMEGA_DATTN256_ARG_QN, b->norms); cb64(&s, OMEGA_DATTN256_ARG_KN, b->norms + HD);
270 cb64(&s, OMEGA_DATTN256_ARG_OUT, out); cb64(&s, OMEGA_DATTN256_ARG_PART, part); cb64(&s, OMEGA_DATTN256_ARG_CNT, cnt);
271 s.cbank[OMEGA_DATTN256_ARG_POS / 4] = pos; s.cbank[OMEGA_DATTN256_ARG_PER_SPLIT / 4] = 256; s.cbank[OMEGA_DATTN256_ARG_SPLITS / 4] = splits;
272 s.cbank[OMEGA_DATTN256_ARG_KV_STRIDE / 4] = b->c->max_pos * RB;
273 cbf(&s, OMEGA_DATTN256_ARG_EPS, EPS); cbf(&s, OMEGA_DATTN256_ARG_OUT_SCALE, 1.0f); cbf(&s, OMEGA_DATTN256_ARG_QK_SCALE, QKS);
274 s.nranges = 5;
275 s.ranges[0].lo = (uintptr_t)qkv_k; s.ranges[0].hi = (uintptr_t)qkv_k + qkv_bytes;
276 s.ranges[1].lo = (uintptr_t)b->kv; s.ranges[1].hi = (uintptr_t)b->kv + 2 * b->plane;
277 s.ranges[2].lo = (uintptr_t)b->rope; s.ranges[2].hi = (uintptr_t)b->rope + (size_t)b->c->max_pos * OMEGA_DATTN256_ROPE_ROW_BYTES;
278 s.ranges[3].lo = (uintptr_t)b->norms; s.ranges[3].hi = (uintptr_t)b->norms + 2048;
279 s.ranges[4].lo = (uintptr_t)out; s.ranges[4].hi = (uintptr_t)out + out_bytes;
280 if (part) { /* prep never touches the partials or counters: no range, so a stray access faults */
281 s.ranges[5].lo = (uintptr_t)part; s.ranges[5].hi = (uintptr_t)part + part_bytes;
282 s.ranges[6].lo = (uintptr_t)cnt; s.ranges[6].hi = (uintptr_t)cnt + 16; s.nranges = 7;
283 }
284 int err = ws_run(&s, grid_x, VY);
285 if (err) { snprintf(msg, n, "%s launch at %u: %s (pc %d)", part ? "decode" : "prep", pos, s.msg, s.err_pc); return -1; }
286 g_sim_issues += s.warp_issues;
287 return 0;
288}
289static int sim_attend_launch(const BlackwellIRProgram *prog, const SimBufs *b, const uint8_t *qf, size_t qf_bytes, float *out, int order, char *msg, size_t n) {
290 static uint8_t smem[OMEGA_PATTN256_SHARED_BYTES];
291 const Case *c = b->c;
292 WsSim s; memset(&s, 0, sizeof s);
293 s.p = prog; s.threads = OMEGA_PATTN256_THREADS; s.order = order; s.reg_poison = g_poison; s.smem = smem; s.smem_bytes = sizeof smem;
294 cb64(&s, OMEGA_PATTN256_ARG_QF, qf); cb64(&s, OMEGA_PATTN256_ARG_KC, b->kv); cb64(&s, OMEGA_PATTN256_ARG_VC, b->kv + b->plane); cb64(&s, OMEGA_PATTN256_ARG_OUT, out);
295 s.cbank[OMEGA_PATTN256_ARG_POS0 / 4] = c->pos0; s.cbank[OMEGA_PATTN256_ARG_MC / 4] = c->m; s.cbank[OMEGA_PATTN256_ARG_KV_STRIDE / 4] = c->max_pos * RB;
296 cbf(&s, OMEGA_PATTN256_ARG_OUT_SCALE, 1.0f);
297 s.nranges = 3;
298 s.ranges[0].lo = (uintptr_t)qf; s.ranges[0].hi = (uintptr_t)qf + qf_bytes;
299 s.ranges[1].lo = (uintptr_t)b->kv; s.ranges[1].hi = (uintptr_t)b->kv + 2 * b->plane;
300 s.ranges[2].lo = (uintptr_t)out; s.ranges[2].hi = (uintptr_t)out + (size_t)c->m * OW * 4;
301 int err = ws_run(&s, (c->m + OMEGA_PATTN256_TOKENS - 1) / OMEGA_PATTN256_TOKENS, KVH);
302 if (err) { snprintf(msg, n, "attend: %s (pc %d)", s.msg, s.err_pc); return -1; }
303 g_sim_issues += s.warp_issues;
304 return 0;
305}
306static int sim_point(uint32_t max_pos, uint32_t pos0, uint32_t m, uint32_t rotary, float qscale, uint32_t seed, int orders, int prep_mut, int att_mut,
307 char *msg, size_t n, double *worst, size_t *bad) {
308 static BlackwellIRProgram pprog, aprog, dprog;
309 OmegaDecodeAttn256Spec ps = { .depth = OMEGA_GPU_DATTN256_DEPTH, .mutant = prep_mut, .prep = 1 SPEC_SHAPE }, ds = { .depth = OMEGA_GPU_DATTN256_DEPTH SPEC_SHAPE };
310 OmegaPrefillAttn256Spec as = { .mutant = att_mut ATT_SHAPE };
311 if (omega_blackwell_codegen_decode_attn256_prog(&ps, &pprog) != 0 || omega_blackwell_codegen_prefill_attn256_prog(&as, &aprog) != 0 ||
312 omega_blackwell_codegen_decode_attn256_prog(&ds, &dprog) != 0) { snprintf(msg, n, "codegen refused"); return -1; }
313 Case c; case_make(&c, max_pos, pos0, m, rotary, seed, qscale);
314 SimBufs b = { .c = &c };
315 b.plane = (size_t)KVH * max_pos * RB + OMEGA_DATTN256_PLANE_SLACK;
316 b.kv = aligned_alloc(16, 2 * b.plane);
317 uint8_t *twin = aligned_alloc(16, 2 * b.plane);
318 b.rope = aligned_alloc(16, (size_t)max_pos * OMEGA_DATTN256_ROPE_ROW_BYTES);
319 for (uint32_t p = 0; p < max_pos; p++) omega_dattn256_rope_row(p, THETA, rotary, b.rope + (size_t)p * 256);
320 b.norms = aligned_alloc(16, 2048); case_kernel_norms(&c, b.norms, b.norms + HD);
321 b.qkv_k = aligned_alloc(16, (size_t)m * QW * 4); case_kernel_qkv(&c, b.qkv_k);
322 const size_t qf_bytes = (size_t)((m + 1) / 2) * QFP;
323 uint8_t *qf = aligned_alloc(16, qf_bytes);
324 float *out = aligned_alloc(16, (size_t)m * OW * 4), *copy = malloc((size_t)m * OW * 4);
325 int err = 0;
326 for (int order = 0; order < orders && !err; order++) {
327 memset(b.kv, 0, 2 * b.plane); case_history_into(&c, b.kv, b.plane);
328 memset(qf, 0xff, qf_bytes); /* NaN bf16 pairs: a slot the prep never wrote poisons its row */
329 for (size_t i = 0; i < (size_t)m * OW; i++) out[i] = -999.0f;
330 err = sim_decode_launch(&pprog, &b, pos0, m, b.qkv_k, (size_t)m * QW * 4, qf, qf_bytes, NULL, 0, NULL, 0, order, msg, n);
331 if (!err) err = sim_attend_launch(&aprog, &b, qf, qf_bytes, out, order, msg, n);
332 if (err) break;
333 size_t unwritten = 0; for (size_t i = 0; i < (size_t)m * OW; i++) if (out[i] == -999.0f) unwritten++;
334 if (unwritten) { err = -1; snprintf(msg, n, "%zu output words never written", unwritten); break; }
335 if (order == 0) memcpy(copy, out, (size_t)m * OW * 4);
336 else if (memcmp(copy, out, (size_t)m * OW * 4) != 0) { err = -1; snprintf(msg, n, "the fragment order changed the result"); }
337 }
338 if (!err) {
339 /* twin: m decode steps of the decode program from the same history */
340 memset(twin, 0, 2 * b.plane); case_history_into(&c, twin, b.plane);
341 SimBufs tb = b; tb.kv = twin;
342 float *dout = aligned_alloc(16, OW * 4);
343 const uint32_t smax = (pos0 + m + 255) / 256;
344 float *part = aligned_alloc(16, (size_t)VY * smax * OMEGA_DATTN256_PARTIAL_WORDS * 4);
345 uint32_t *cnt = aligned_alloc(16, 16);
346 for (uint32_t t = 0; t < m && !err; t++) {
347 const uint32_t pos = pos0 + t, splits = (pos + 256) / 256;
348 memset(cnt, 0, 16);
349 err = sim_decode_launch(&dprog, &tb, pos, splits, b.qkv_k + (size_t)t * QW, QW * 4, dout, OW * 4, part, (size_t)VY * splits * OMEGA_DATTN256_PARTIAL_WORDS * 4, cnt, splits, 0, msg, n);
350 }
351 free(dout); free(part); free(cnt);
352 size_t rowdiff = 0;
353 for (unsigned h = 0; h < 2 * KVH && !err; h++) {
354 const size_t o = (h >= KVH ? b.plane : 0) + (size_t)(h % KVH) * max_pos * RB;
355 for (uint32_t t = 0; t < pos0 + m; t++) rowdiff += memcmp(b.kv + o + (size_t)t * RB, twin + o + (size_t)t * RB, RB) != 0;
356 }
357 uint16_t *kn = malloc((size_t)m * KVH * HD * 2), *vn = malloc((size_t)m * KVH * HD * 2), *kch = malloc((size_t)m * KVH * HD * 2), *vch = malloc((size_t)m * KVH * HD * 2);
358 ref_rows(&c, kn, vn);
359 for (uint32_t t = 0; t < m; t++) for (unsigned g = 0; g < KVH; g++) {
360 const size_t o = ((size_t)g * max_pos + pos0 + t) * RB;
361 memcpy(kch + ((size_t)t * KVH + g) * HD, b.kv + o, RB); memcpy(vch + ((size_t)t * KVH + g) * HD, b.kv + b.plane + o, RB);
362 }
363 char why[160];
364 if (!err) {
365 int rok = rows_ok(&c, kch, vch, kn, vn, why, sizeof why);
366 *bad = parity(&c, kn, vn, out, 1.0f, worst);
367 if (rowdiff) *bad += 1000000;
368 if (!rok) *bad += 2000000;
369 snprintf(msg, n, "bad=%zu worst=%.3f rows vs decode twin: %zu differ; vs reference: %s", *bad, *worst, rowdiff, why);
370 }
371 free(kn); free(vn); free(kch); free(vch);
372 }
373 free(b.kv); free(twin); free(b.rope); free(b.norms); free(b.qkv_k); free(qf); free(out); free(copy); case_free(&c);
374 return err;
375}
376static void sim_battery(void) {
377 char msg[400]; double worst; size_t bad;
378 struct { uint32_t max_pos, pos0, m, rot; float qs; int orders; const char *name; } pts[] = {
379 { 256, 0, 1, 64, 1.0f, 2, "one token at position 0" },
380 { 256, 0, 8, 64, 1.0f, 2, "8 tokens from 0 (one CTA)" },
381 { 256, 0, 37, 64, 1.0f, 2, "37 tokens from 0 (odd m, partial CTA)" },
382 { 256, 37, 19, 64, 1.0f, 2, "19 tokens from 37 (odd start)" },
383 { 256, 100, 19, 64, 4.0f, 1, "19 tokens from 100, sharp q (x4)" },
384 { 512, 250, 40, 64, 1.0f, 1, "40 tokens from 250 (crosses 256)" },
385 { 256, 0, 24, 256, 1.0f, 1, "24 tokens from 0, full rope (rotary 256)" },
386 };
387 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
388 double t0 = now_ms();
389 int err = sim_point(pts[i].max_pos, pts[i].pos0, pts[i].m, pts[i].rot, pts[i].qs, 100 + (uint32_t)i, pts[i].orders, 0, 0, msg, sizeof msg, &worst, &bad);
390 CHECK(err == 0 && bad == 0, "sim %s: %s", pts[i].name, msg);
391 printf("sim %-42s %s %s (%.1f s)\n", pts[i].name, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg, (now_ms() - t0) / 1e3);
392 fflush(stdout);
393 }
394 static const struct { int pm, am; } mut[] = { { 1, 0 }, { 3, 0 }, { 0, 1 }, { 0, 2 } };
395 for (size_t i = 0; i < sizeof mut / sizeof mut[0]; i++) {
396 int err = sim_point(256, 40, 24, 64, 1.0f, 200, 1, mut[i].pm, mut[i].am, msg, sizeof msg, &worst, &bad);
397 int caught = err != 0 || bad > 0;
398 CHECK(caught, "sim %s mutant %d NOT caught (%s)", mut[i].pm ? "prep" : "attend", mut[i].pm ? mut[i].pm : mut[i].am, msg);
399 printf("sim %s mutant %d at 24 tokens from 40: %s (%s)\n", mut[i].pm ? "prep" : "attend", mut[i].pm ? mut[i].pm : mut[i].am, caught ? "caught" : "MISSED", msg);
400 fflush(stdout);
401 }
402 printf("sim: %" PRIu64 " warp issues in total\n", g_sim_issues);
403}
404
405/* ------------------------------------------------------------- chip */
406static FILE *g_json;
407static int g_json_first = 1;
408static int cache_with_history(const Case *c, OmegaGpuDecodeAttn256Cache **out) {
409 int rc = omega_gpu_decode_attn256_cache_alloc_shape(1, c->max_pos, THETA, c->rotary, QH, KVH, QPC, out);
410 if (rc != OMEGA_GPU_DATTN_OK) return rc;
411 float qn_k[HD], kn_k[HD]; case_kernel_norms(c, qn_k, kn_k);
412 rc = omega_gpu_decode_attn256_set_norms(*out, 0, qn_k, kn_k);
413 if (rc != OMEGA_GPU_DATTN_OK || c->pos0 == 0) return rc;
414 uint16_t *kp = malloc((size_t)c->pos0 * RB);
415 for (unsigned g = 0; g < KVH && rc == OMEGA_GPU_DATTN_OK; g++) {
416 for (uint32_t t = 0; t < c->pos0; t++) omega_dattn256_perm_vec_bf16(c->kc + ((size_t)g * c->max_pos + t) * HD, kp + (size_t)t * HD, c->rotary);
417 rc = omega_gpu_decode_attn256_cache_write(*out, 0, g, 0, c->pos0, kp, c->vc + (size_t)g * c->max_pos * HD);
418 }
419 free(kp);
420 return rc;
421}
422/* the chunk's appended rows, [m][4][256] each (K permuted as stored) */
423static int read_rows(OmegaGpuDecodeAttn256Cache *cc, const Case *c, uint16_t *k, uint16_t *v) {
424 for (uint32_t t = 0; t < c->m; t++) for (unsigned g = 0; g < KVH; g++) {
425 int rc = omega_gpu_decode_attn256_cache_read(cc, 0, g, c->pos0 + t, k + ((size_t)t * KVH + g) * HD, v + ((size_t)t * KVH + g) * HD);
426 if (rc != OMEGA_GPU_DATTN_OK) return rc;
427 }
428 return OMEGA_GPU_DATTN_OK;
429}
430static uint64_t median_u64(uint64_t *v, int n) { qsort(v, (size_t)n, sizeof *v, cmp_u64); return v[n / 2]; }
431static int chip_point(uint32_t max_pos, uint32_t pos0, uint32_t m, float qscale, uint32_t seed, int twin, int reps) {
432 printf("-- chip %u tokens from %u (max_pos %u)\n", m, pos0, max_pos); fflush(stdout);
433 Case c; case_make(&c, max_pos, pos0, m, 64, seed, qscale);
434 float *qkv_k = malloc((size_t)m * QW * 4); case_kernel_qkv(&c, qkv_k);
435 float *out = malloc((size_t)m * OW * 4), *out2 = malloc((size_t)m * OW * 4);
436 const size_t rw = (size_t)m * KVH * HD;
437 uint16_t *kn = malloc(rw * 2), *vn = malloc(rw * 2), *kch = malloc(rw * 2), *vch = malloc(rw * 2), *ktw = malloc(rw * 2), *vtw = malloc(rw * 2);
438 OmegaGpuDecodeAttn256Cache *cc = NULL, *tc = NULL;
439 OmegaGpuPrefillAttn256Info info;
440 int ok = 1; char why[200]; double worst = 0; size_t bad = 0;
441 int rc = cache_with_history(&c, &cc);
442 CHECK(rc == OMEGA_GPU_DATTN_OK, "cache with history: %s %s", omega_gpu_decode_attn_rc_name(rc), rc ? omega_gpu_decode_attn_last_error() : "");
443 if (rc != OMEGA_GPU_DATTN_OK) { ok = 0; goto done; }
444 rc = omega_gpu_prefill_attn256(cc, 0, pos0, m, qkv_k, EPS, 1.0f, out, &info);
445 CHECK(rc == OMEGA_GPU_DATTN_OK, "prefill %u+%u: %s %s", pos0, m, omega_gpu_decode_attn_rc_name(rc), rc ? omega_gpu_decode_attn_last_error() : "");
446 if (rc != OMEGA_GPU_DATTN_OK) { ok = 0; goto done; }
447 printf(" prep %u insns %u regs, attend %u insns %u regs; first call %.3f ms (prep %.3f, attend %.3f)\n", info.insns[0], info.gprs[0], info.insns[1], info.gprs[1],
448 info.call_ns / 1e6, info.elapsed_ns[0] / 1e6, info.elapsed_ns[1] / 1e6);
449 ref_rows(&c, kn, vn);
450 rc = read_rows(cc, &c, kch, vch);
451 int rok = rc == OMEGA_GPU_DATTN_OK && rows_ok(&c, kch, vch, kn, vn, why, sizeof why);
452 CHECK(rok, "%u+%u: appended rows vs reference: %s", pos0, m, rc ? "read failed" : why);
453 double t0 = now_ms();
454 bad = parity(&c, kn, vn, out, 1.0f, &worst);
455 CHECK(bad == 0, "%u+%u: parity: %zu elements outside the tolerance (worst ratio %.3f)", pos0, m, bad, worst);
456 printf(" parity: %zu bad, worst ratio %.3f of the tolerance (%.1f s reference); rows: %s\n", bad, worst, (now_ms() - t0) / 1e3, rok ? why : "FAIL");
457 ok &= rok && bad == 0;
458 /* second call over the same positions: the same rows are rewritten, the same output */
459 rc = omega_gpu_prefill_attn256(cc, 0, pos0, m, qkv_k, EPS, 1.0f, out2, &info);
460 CHECK(rc == OMEGA_GPU_DATTN_OK && memcmp(out, out2, (size_t)m * OW * 4) == 0 && info.kernel_cache_hits == 2, "%u+%u: second call bit-identical with both kernels cached (rc %d hits %u)", pos0, m, rc, info.kernel_cache_hits);
461 /* out_scale 2^20: exact */
462 rc = omega_gpu_prefill_attn256(cc, 0, pos0, m, qkv_k, EPS, 1048576.0f, out2, NULL);
463 { size_t diff = 0; for (size_t i = 0; i < (size_t)m * OW; i++) diff += out2[i] != out[i] * 1048576.0f;
464 CHECK(rc == OMEGA_GPU_DATTN_OK && diff == 0, "%u+%u: out_scale 2^20 is exact (%zu words differ)", pos0, m, diff); }
465 /* device entry == host entry */
466 {
467 OmegaGpuMem q, o; memset(&q, 0, sizeof q); memset(&o, 0, sizeof o);
468 omega_gpu_session_lock();
469 int arc = omega_gpu_session_open() && omega_gpu_session_alloc((size_t)m * QW * 4, &q) == 0 && omega_gpu_session_alloc((size_t)m * OW * 4, &o) == 0 ? 0 : -1;
470 if (arc == 0) { memcpy(q.cpu, qkv_k, (size_t)m * QW * 4); float *od = o.cpu; for (size_t i = 0; i < (size_t)m * OW; i++) od[i] = -999.0f;
471 arc = omega_gpu_session_sync_to_gpu(&q) | omega_gpu_session_sync_to_gpu(&o); }
472 omega_gpu_session_unlock();
473 rc = arc == 0 ? omega_gpu_prefill_attn256_dev(cc, 0, pos0, m, q.va, EPS, 1.0f, o.va, NULL) : OMEGA_GPU_DATTN_CHIP_FAIL;
474 omega_gpu_session_lock();
475 int same = rc == OMEGA_GPU_DATTN_OK && omega_gpu_session_sync_to_cpu(&o) == 0 && memcmp(o.cpu, out, (size_t)m * OW * 4) == 0;
476 if (q.cpu) omega_gpu_session_free(&q);
477 if (o.cpu) omega_gpu_session_free(&o);
478 omega_gpu_session_unlock();
479 CHECK(same, "%u+%u: device entry bit-identical to the host entry (rc %s)", pos0, m, omega_gpu_decode_attn_rc_name(rc));
480 }
481 /* twin: m decode calls from the same history; rows bit-identical, then the next decode step on both caches */
482 if (twin) {
483 rc = cache_with_history(&c, &tc);
484 float *dout = malloc(OW * 4);
485 t0 = now_ms();
486 for (uint32_t t = 0; t < m && rc == OMEGA_GPU_DATTN_OK; t++) rc = omega_gpu_decode_attn256(tc, 0, pos0 + t, qkv_k + (size_t)t * QW, EPS, 1.0f, dout, NULL);
487 const double dec_ms = now_ms() - t0;
488 CHECK(rc == OMEGA_GPU_DATTN_OK, "%u+%u: twin decode loop: %s %s", pos0, m, omega_gpu_decode_attn_rc_name(rc), rc ? omega_gpu_decode_attn_last_error() : "");
489 if (rc == OMEGA_GPU_DATTN_OK) rc = read_rows(tc, &c, ktw, vtw);
490 size_t kd = 0, vd = 0;
491 for (size_t i = 0; i < rw && rc == OMEGA_GPU_DATTN_OK; i++) { kd += kch[i] != ktw[i]; vd += vch[i] != vtw[i]; }
492 CHECK(rc == OMEGA_GPU_DATTN_OK && kd == 0 && vd == 0, "%u+%u: rows bit-identical to %u decode calls (k %zu, v %zu words differ)", pos0, m, m, kd, vd);
493 printf(" twin: %u decode calls %.1f ms; rows k %zu v %zu words differ\n", m, dec_ms, kd, vd);
494 if (pos0 + m < max_pos && rc == OMEGA_GPU_DATTN_OK) {
495 float *nq = malloc(QW * 4), *o1 = malloc(OW * 4), *o2 = malloc(OW * 4); uint32_t s = seed ^ 0x5a5a5a5au;
496 for (unsigned h = 0; h < QH + KVH; h++) { float raw[HD]; for (unsigned i = 0; i < HD; i++) raw[i] = frand(&s, -1.0f, 1.0f); omega_dattn256_perm_vec(raw, nq + h * HD, 64); }
497 for (unsigned i = KEND; i < QW; i++) nq[i] = frand(&s, -1.0f, 1.0f);
498 int r1 = omega_gpu_decode_attn256(cc, 0, pos0 + m, nq, EPS, 1.0f, o1, NULL), r2 = omega_gpu_decode_attn256(tc, 0, pos0 + m, nq, EPS, 1.0f, o2, NULL);
499 CHECK(r1 == OMEGA_GPU_DATTN_OK && r2 == OMEGA_GPU_DATTN_OK && memcmp(o1, o2, OW * 4) == 0, "%u+%u: next decode step bit-identical after prefill and after the decode loop", pos0, m);
500 free(nq); free(o1); free(o2);
501 }
502 free(dout);
503 }
504 /* timing: reps calls on the same positions (rows rewritten with the same bits) */
505 if (reps > 0) {
506 uint64_t *pp = malloc((size_t)reps * 8), *aa = malloc((size_t)reps * 8), *cl = malloc((size_t)reps * 8);
507 for (int r = 0; r < reps && rc == OMEGA_GPU_DATTN_OK; r++) {
508 rc = omega_gpu_prefill_attn256(cc, 0, pos0, m, qkv_k, EPS, 1.0f, out2, &info);
509 pp[r] = info.elapsed_ns[0]; aa[r] = info.elapsed_ns[1]; cl[r] = info.call_ns;
510 }
511 CHECK(rc == OMEGA_GPU_DATTN_OK, "%u+%u: timing calls", pos0, m);
512 if (rc == OMEGA_GPU_DATTN_OK) {
513 const uint64_t mp = median_u64(pp, reps), ma = median_u64(aa, reps), mc = median_u64(cl, reps);
514 /* useful work: QK^T and PV over the causal context, 2 flops per multiply-add, 24 heads x 256 dims */
515 double flops = 0, kvb = 0;
516 for (uint32_t t = 0; t < m; t++) flops += 4.0 * (double)(pos0 + t + 1) * QH * HD;
517 for (uint32_t cx = 0; cx * OMEGA_PATTN256_TOKENS < m; cx++) {
518 uint32_t nk = (cx + 1) * OMEGA_PATTN256_TOKENS; if (nk > m) nk = m;
519 kvb += (double)KVH * (((pos0 + nk - 1) >> 4) + 1) * 16.0 * 2 * RB;
520 }
521 printf(" timing (median of %d): prep %.3f ms, attend %.3f ms, call %.3f ms -> %.0f tokens/s; attend %.2f TFLOPS useful, %.1f GB/s K/V streamed\n",
522 reps, mp / 1e6, ma / 1e6, mc / 1e6, m / ((mp + ma) / 1e9), flops / (double)ma / 1e3, kvb / (double)ma);
523 if (g_json) {
524 fprintf(g_json, "%s {\"pos0\": %u, \"m\": %u, \"max_pos\": %u, \"reps\": %d, \"prep_ns\": %" PRIu64 ", \"attend_ns\": %" PRIu64 ", \"call_ns\": %" PRIu64 ", \"attend_useful_flops\": %.0f, \"attend_kv_bytes\": %.0f, \"parity_bad\": %zu, \"parity_worst_ratio\": %.4f}",
525 g_json_first ? "" : ",\n", pos0, m, max_pos, reps, mp, ma, mc, flops, kvb, bad, worst);
526 g_json_first = 0;
527 }
528 }
529 free(pp); free(aa); free(cl);
530 }
531done:
532 if (tc) omega_gpu_decode_attn256_cache_free(tc);
533 if (cc) omega_gpu_decode_attn256_cache_free(cc);
534 free(qkv_k); free(out); free(out2); free(kn); free(vn); free(kch); free(vch); free(ktw); free(vtw); case_free(&c);
535 return ok;
536}
537static void chip_mutants(void) {
538 static const struct { int pm, am; } mut[] = { { 1, 0 }, { 3, 0 }, { 0, 1 }, { 0, 2 } };
539 for (size_t i = 0; i < sizeof mut / sizeof mut[0]; i++) {
540 omega_gpu_prefill_attn256_test_set_mutant(mut[i].pm, mut[i].am);
541 Case c; case_make(&c, 256, 40, 24, 64, 300, 1.0f);
542 float *qkv_k = malloc((size_t)c.m * QW * 4); case_kernel_qkv(&c, qkv_k);
543 float *out = malloc((size_t)c.m * OW * 4);
544 uint16_t *kn = malloc((size_t)c.m * KVH * HD * 2), *vn = malloc((size_t)c.m * KVH * HD * 2), *kch = malloc((size_t)c.m * KVH * HD * 2), *vch = malloc((size_t)c.m * KVH * HD * 2);
545 OmegaGpuDecodeAttn256Cache *cc = NULL;
546 int rc = cache_with_history(&c, &cc);
547 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_prefill_attn256(cc, 0, c.pos0, c.m, qkv_k, EPS, 1.0f, out, NULL);
548 double worst = 0; size_t bad = 0; char why[200] = "";
549 int rok = 1;
550 if (rc == OMEGA_GPU_DATTN_OK) {
551 ref_rows(&c, kn, vn);
552 rok = read_rows(cc, &c, kch, vch) == OMEGA_GPU_DATTN_OK && rows_ok(&c, kch, vch, kn, vn, why, sizeof why);
553 bad = parity(&c, kn, vn, out, 1.0f, &worst);
554 }
555 const int caught = rc != OMEGA_GPU_DATTN_OK || bad > 0 || !rok;
556 CHECK(caught, "chip %s mutant %d NOT caught", mut[i].pm ? "prep" : "attend", mut[i].pm ? mut[i].pm : mut[i].am);
557 printf("chip %s mutant %d: %s (rc %s, %zu bad, worst %.1f, rows %s)\n", mut[i].pm ? "prep" : "attend", mut[i].pm ? mut[i].pm : mut[i].am, caught ? "caught" : "MISSED",
558 omega_gpu_decode_attn_rc_name(rc), bad, worst, rok ? "ok" : "differ");
559 if (cc) omega_gpu_decode_attn256_cache_free(cc);
560 free(qkv_k); free(out); free(kn); free(vn); free(kch); free(vch); case_free(&c);
561 }
562 omega_gpu_prefill_attn256_test_set_mutant(0, 0);
563}
564
565int main(int argc, char **argv) {
566 int host = 0, sim = 0, reps = 9, quick = 0, timing_only = 0; const char *json = NULL; uint32_t maxp = 32768;
567 for (int i = 1; i < argc; i++) {
568 if (!strcmp(argv[i], "--host-only")) host = 1;
569 else if (!strcmp(argv[i], "--sim")) sim = 1;
570 else if (!strcmp(argv[i], "--poison") && i + 1 < argc) g_poison = (uint32_t)strtoul(argv[++i], NULL, 0);
571 else if (!strcmp(argv[i], "--sim-point") && i + 3 < argc) { /* one simulator point: MAX_POS POS0 M */
572 uint32_t a = (uint32_t)atoi(argv[i + 1]), b = (uint32_t)atoi(argv[i + 2]), m = (uint32_t)atoi(argv[i + 3]); i += 3;
573 char msg[400]; double worst = 0; size_t bad = 0; double t0 = now_ms();
574 int err = sim_point(a, b, m, 64, 1.0f, 77, 1, 0, 0, msg, sizeof msg, &worst, &bad);
575 CHECK(err == 0 && bad == 0, "sim point %u+%u (max_pos %u): %s", b, m, a, msg);
576 printf("sim point %u tokens from %u (max_pos %u): %s %s (%.1f s)\n", m, b, a, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg, (now_ms() - t0) / 1e3);
577 host = 1; }
578 else if (!strcmp(argv[i], "--quick")) quick = 1;
579 else if (!strcmp(argv[i], "--timing-only")) timing_only = 1;
580 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]);
581 else if (!strcmp(argv[i], "--max-pos") && i + 1 < argc) maxp = (uint32_t)atoi(argv[++i]);
582 else if (!strcmp(argv[i], "--out") && i + 1 < argc) json = argv[++i];
583 else { fprintf(stderr, "usage: %s [--host-only | --sim | [--quick] [--timing-only] [--reps N] [--max-pos 32768|131072] [--out receipt.json]]\n", argv[0]); return 2; }
584 }
585 if (reps < 1) reps = 1;
586 host_only();
587 if (sim) sim_battery();
588 if (!host && !sim) {
589 if (json) { g_json = test_out_open(json); if (g_json) fprintf(g_json, "{\"test\": \"gpu_prefill_attn256\", \"rotary_dim\": 64, \"points\": [\n"); }
590 if (!timing_only) {
591 chip_point(256, 0, 1, 1.0f, 1, 1, 0);
592 chip_point(256, 0, 37, 1.0f, 2, 1, 0);
593 chip_point(512, 100, 19, 1.0f, 3, 1, 0);
594 chip_point(512, 37, 141, 4.0f, 4, 1, 0);
595 chip_mutants();
596 }
597 if (!quick) {
598 chip_point(4096, 0, 2048, 1.0f, 8, !timing_only, reps);
599 chip_point(8192, 6144, 2048, 1.0f, 9, !timing_only, reps);
600 chip_point(32768, 30720, 2048, 1.0f, 10, !timing_only, reps);
601 if (maxp >= 131072) chip_point(131072, 129024, 2048, 1.0f, 11, !timing_only, reps);
602 }
603 if (g_json) { fprintf(g_json, "\n], \"checks\": %d, \"failed\": %d}\n", g_checks, g_failed); fclose(g_json); }
604 }
605 printf("gpu_prefill_attn256_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
606 return g_failed ? 1 : 0;
607}