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/CUDApp/blob/main/tests/gpu_decode_attn256_test.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / gpu_decode_attn256_test.c` 482 lines · 36575 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * gpu_decode_attn256_test: the one-launch decode attention for Qwen3.8-27B (omega-mv#544, omega_gpu_decode_attn256_api.h). 4 * --host-only host pieces: RoPE rows (partial: pairs >= rotary_dim/2 are identity), the permutation (bijection, and 5 * "full rotate-half on the permuted head with the partial table == the model's rotate-half within the 6 * first rotary_dim dims", double), reference sanity (context 1 returns v_new), codegen budget per depth 7 * and mutant, refusals. No device. 8 * --sim the kernel through tests/bw_warp_sim_k.h (no chip): contexts 1, 7, 256, 257, 301, 701 (1..3 splits, 9 * the cross-split combine, 2 chunks per CTA), rotary_dim 64 and 256, both fragment orders, every mutant 10 * caught at context 301. 11 * (default) the chip: parity against the double reference at contexts 1, 7, 256, 301 (sharp q), 1024, 4096, 8192, 12 * 32768 (and 131072 with --max-pos 131072), the appended K/V row read back and compared, second call 13 * identical, out_scale 2^64 path, timing medians with the host caches evicted, every mutant caught at 14 * context 301, JSON receipt with --out. 15 * The reference is the model's maths in the model's dim order (crates/aien-inference-abi/src/qwen38.rs `attention`: 16 * zero-centred RMSNorm x * rsqrt(mean + eps) * (1 + w), RoPE on the first rotary_dim dims with pairs (j, j + rotary_dim/2), 17 * scale 1/sqrt(256), causal softmax over the context). The test permutes q, k and the norm weights with 18 * omega_dattn256_perm_vec before handing them to the kernel, stores history K rows permuted, and un-permutes nothing on 19 * the way back (V and the output are in model order). The gate (sigmoid) is outside the kernel and outside this test. 20 * Tolerance: |out - ref| <= 3e-3 * max|v| + 1e-6 per element. Host and simulator passes are never chip results. 21 */ 22 #include "omega_gpu_decode_attn256_api.h" 23 #include "omega_gpu_session.h" 24 #include "omega_gpu_hostmath.h" 25 #include "bw_warp_sim_k.h" 26 #include 27 #include 28 #include 29 #include 30 #include 31 #include 32 #include "test_out_file.h" 33 34 static int g_checks, g_failed; 35 #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 36 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 37 static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / `16777216`.0f); } 38 static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } 39 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; } 40 static const double THETA = `10000000`.0; /* Qwen3.8-27B rope_theta */ 41 static const float EPS = 1e-6f; 42 #define HD 256u 43 /* Model shape under test: 0 = the 27B (24 q / 4 KV heads, 6 q per CTA row); -DDATTN_TEST_Q36 builds Qwen3.6-35B-A3B (16 / 2 / 4, 44 * two CTA rows per KV head; sc L306). */ 45 #ifdef DATTN_TEST_Q36 46 #define QH 16u 47 #define KVH 2u 48 #define QPC 4u 49 #else 50 #define QH 24u 51 #define KVH 4u 52 #define QPC 6u 53 #endif 54 #define SPEC_SHAPE , .q_heads = QH, .kv_heads = KVH, .q_per_cta = QPC 55 #define VY (QH / QPC) /* CTA rows */ 56 #define QW ((QH + 2u * KVH) * HD) /* 8192 */ 57 #define OW (QH * HD) /* 6144 */ 58 #define QEND (QH * HD) /* 6144: k block starts here */ 59 #define KEND (QEND + KVH * HD) /* 7168: v block starts here */ 60 61 /* one layer's random state in MODEL order: qkv, history rows (bf16) for 4 KV heads, zero-centred norm weights w */ 62 typedef struct { 63 uint32_t max_pos, pos, rotary; 64 float qkv[QW], qn[HD], kn[HD]; 65 uint16_t *kc, *vc; /* [4][max_pos][256] model order (history 0..pos-1 filled; the rest zero) */ 66 float vmax; 67 } Case; 68 static void case_make(Case *c, uint32_t max_pos, uint32_t pos, uint32_t rotary, uint32_t seed, float qscale) { 69 uint32_t s = seed; 70 c->max_pos = max_pos; c->pos = pos; c->rotary = rotary; 71 for (size_t i = 0; i < QW; i++) c->qkv[i] = frand(&s, -1.0f, 1.0f) * (i < QEND ? qscale : 1.0f); 72 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); } 73 size_t words = (size_t)KVH * max_pos * HD; 74 c->kc = calloc(words, 2); c->vc = calloc(words, 2); 75 c->vmax = 1.0f; 76 for (unsigned g = 0; g < KVH; g++) for (uint32_t t = 0; t < pos; t++) for (unsigned i = 0; i < HD; i++) { 77 size_t o = ((size_t)g * max_pos + t) * HD + i; 78 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)); 79 } 80 } 81 static void case_free(Case *c) { free(c->kc); free(c->vc); } 82 /* what the kernel sees: q and k heads permuted, v as is; norms 1 + w permuted; K history rows permuted */ 83 static void case_kernel_inputs(const Case *c, float *qkv_k, float *qn_k, float *kn_k) { 84 for (unsigned h = 0; h < QH + KVH; h++) omega_dattn256_perm_vec(c->qkv + h * HD, qkv_k + h * HD, c->rotary); 85 memcpy(qkv_k + KEND, c->qkv + KEND, (size_t)KVH * HD * 4); 86 float t[HD]; 87 for (unsigned i = 0; i < HD; i++) t[i] = 1.0f + c->qn[i]; 88 omega_dattn256_perm_vec(t, qn_k, c->rotary); 89 for (unsigned i = 0; i < HD; i++) t[i] = 1.0f + c->kn[i]; 90 omega_dattn256_perm_vec(t, kn_k, c->rotary); 91 } 92 static uint16_t *case_kc_perm(const Case *c) { 93 size_t rows = (size_t)KVH * c->max_pos; 94 uint16_t *p = calloc(rows * HD, 2); 95 for (size_t r = 0; r < rows; r++) omega_dattn256_perm_vec_bf16(c->kc + r * HD, p + r * HD, c->rotary); 96 return p; 97 } 98 99 /* ---- double-precision oracle (test-owned: libm) in MODEL order ---- */ 100 static void norm_rope(double *x, const float *w, float eps, uint32_t rotary, uint32_t pos, double theta, int rope) { 101 double ss = 0; for (unsigned i = 0; i < HD; i++) ss += x[i] * x[i]; 102 double r = 1.0 / sqrt(ss / (double)HD + (double)eps); 103 for (unsigned i = 0; i < HD; i++) x[i] = x[i] * r * (1.0 + (double)w[i]); 104 if (!rope) return; 105 const unsigned half = rotary / 2; 106 for (unsigned j = 0; j < half; j++) { 107 double inv = 1.0 / pow(theta, (double)(2 * j) / (double)rotary), ang = (double)pos * inv, cs = cos(ang), sn = sin(ang); 108 double x1 = x[j], x2 = x[j + half]; 109 x[j] = x1 * cs - x2 * sn; x[j + half] = x2 * cs + x1 * sn; 110 } 111 } 112 /* Reference of the whole layer step. kc/vc: this layer's bf16 planes [4][max_pos][256] (model order) holding positions 113 * 0..pos-1; out: [24*256] doubles (already * out_scale); k_new/v_new: the bf16 rows (model order) the kernel must have 114 * appended at pos ([4][256] each). The reference rounds its k_new/v_new to bf16 and attends over the rounded values. */ 115 static void reference(const float *qkv, const uint16_t *kc, const uint16_t *vc, uint32_t max_pos, uint32_t pos, uint32_t rotary, 116 const float *q_w, const float *k_w, float eps, double theta, float out_scale, double *out, uint16_t *k_new, uint16_t *v_new) { 117 const double qscale = 1.0 / sqrt((double)HD); 118 for (unsigned gk = 0; gk < KVH; gk++) { 119 double k[HD]; 120 for (unsigned i = 0; i < HD; i++) k[i] = qkv[QEND + gk * HD + i]; 121 norm_rope(k, k_w, eps, rotary, pos, theta, 1); 122 for (unsigned i = 0; i < HD; i++) { k_new[gk * HD + i] = omega_dattn_bf16_rne((float)k[i]); v_new[gk * HD + i] = omega_dattn_bf16_rne(qkv[KEND + gk * HD + i]); } 123 } 124 double *p = calloc((size_t)pos + 1, sizeof(double)); 125 for (unsigned h = 0; h < QH; h++) { 126 unsigned gk = h / (QH / KVH); 127 double q[HD]; 128 for (unsigned i = 0; i < HD; i++) q[i] = qkv[h * HD + i]; 129 norm_rope(q, q_w, eps, rotary, pos, theta, 1); 130 double mx = -1e300; 131 for (uint32_t t = 0; t <= pos; t++) { 132 const uint16_t *kr = t == pos ? k_new + gk * HD : kc + ((size_t)gk * max_pos + t) * HD; 133 double s = 0; for (unsigned i = 0; i < HD; i++) s += q[i] * (double)omega_dattn_bf16_f32(kr[i]); 134 s *= qscale; p[t] = s; if (s > mx) mx = s; 135 } 136 double l = 0; for (uint32_t t = 0; t <= pos; t++) { p[t] = exp(p[t] - mx); l += p[t]; } 137 double o[HD]; memset(o, 0, sizeof o); 138 for (uint32_t t = 0; t <= pos; t++) { 139 const uint16_t *vr = t == pos ? v_new + gk * HD : vc + ((size_t)gk * max_pos + t) * HD; 140 for (unsigned i = 0; i < HD; i++) o[i] += p[t] * (double)omega_dattn_bf16_f32(vr[i]); 141 } 142 for (unsigned i = 0; i < HD; i++) out[h * HD + i] = o[i] / l * (double)out_scale; 143 } 144 free(p); 145 } 146 147 /* compares out (f32 [6144]) against the reference; returns the number of elements outside the tolerance and the worst ratio */ 148 static size_t parity(const Case *c, const float *out, float out_scale, double *worst, uint16_t *k_new, uint16_t *v_new) { 149 double *ref = malloc(OW * sizeof(double)); 150 reference(c->qkv, c->kc, c->vc, c->max_pos, c->pos, c->rotary, c->qn, c->kn, EPS, THETA, out_scale, ref, k_new, v_new); 151 double tol = 3e-3 * c->vmax * out_scale + 1e-6 * out_scale; size_t bad = 0; *worst = 0; 152 for (size_t i = 0; i < OW; i++) { double d = fabs((double)out[i] - ref[i]); if (d / tol > *worst) *worst = d / tol; if (d > tol) bad++; } 153 free(ref); 154 return bad; 155 } 156 /* appended rows (chip rows: K permuted, V model order) against the reference rows (model order): v exact, k within one 157 * bf16 ulp on at most 32 elements over the 4 heads */ 158 static int rows_ok(const uint16_t *k_chip, const uint16_t *v_chip, const uint16_t *k_ref, const uint16_t *v_ref, uint32_t rotary, char *why, size_t n) { 159 unsigned kflips = 0; 160 for (unsigned g = 0; g < KVH; g++) for (unsigned i = 0; i < HD; i++) { 161 unsigned ci = g * HD + i, ri = g * HD + omega_dattn256_perm_slot(i, rotary); 162 if (v_chip[ci] != v_ref[ci]) { snprintf(why, n, "v[%u] chip %04x ref %04x", ci, v_chip[ci], v_ref[ci]); return 0; } 163 if (k_chip[ci] != k_ref[ri]) { int d = (int)k_chip[ci] - (int)k_ref[ri]; if (d < -1 || d > 1) { snprintf(why, n, "k slot %u (model dim %u) chip %04x ref %04x", ci, ri, k_chip[ci], k_ref[ri]); return 0; } kflips++; } 164 } 165 if (kflips > 32) { snprintf(why, n, "%u one-ulp k flips", kflips); return 0; } 166 snprintf(why, n, "ok (%u one-ulp k flips)", kflips); 167 return 1; 168 } 169 170 /* ------------------------------------------------------------- host only */ 171 static void perm_checks(void) { 172 static const uint32_t rots[] = { 64, 256, 2, 128, 32 }; 173 for (unsigned ri = 0; ri < sizeof rots / sizeof rots[0]; ri++) { 174 uint32_t R = rots[ri], P = R / 2; uint8_t seen[HD]; memset(seen, 0, sizeof seen); int bij = 1; 175 for (uint32_t i = 0; i < HD; i++) { uint32_t m = omega_dattn256_perm_slot(i, R); if (m >= HD || seen[m]) bij = 0; else seen[m] = 1; } 176 CHECK(bij, "perm rotary %u is a bijection", R); 177 int pairs = 1; for (uint32_t i = 0; i < P; i++) pairs &= omega_dattn256_perm_slot(i, R) == i && omega_dattn256_perm_slot(i + 128, R) == i + P; 178 CHECK(pairs, "perm rotary %u: slots (i, i+128), i < %u hold the model pairs (i, i+%u)", R, P, P); 179 if (R == 256) { int id = 1; for (uint32_t i = 0; i < HD; i++) id &= omega_dattn256_perm_slot(i, R) == i; CHECK(id, "perm rotary 256 is the identity"); } 180 /* the kernel's rotate-half on the permuted head with the partial table == the model's rope, in double */ 181 uint32_t s = 99 + R; double maxd = 0; 182 for (unsigned trial = 0; trial < 50; trial++) { 183 uint32_t pos = trial < 3 ? trial : lcg(&s) % 131072u; 184 double x[HD], y_model[HD], xp[HD], yp[HD], y_back[HD]; float row[HD]; 185 for (unsigned i = 0; i < HD; i++) x[i] = frand(&s, -2.0f, 2.0f); 186 memcpy(y_model, x, sizeof x); 187 { float w0[HD]; for (unsigned i = 0; i < HD; i++) w0[i] = 0.0f; /* rope only: norm with w = 0 then undo the scale */ 188 double ss = 0; for (unsigned i = 0; i < HD; i++) ss += x[i] * x[i]; double r = 1.0 / sqrt(ss / HD + (double)EPS); 189 norm_rope(y_model, w0, EPS, R, pos, THETA, 1); for (unsigned i = 0; i < HD; i++) y_model[i] /= r; } 190 omega_dattn256_rope_row(pos, THETA, R, row); 191 for (unsigned i = 0; i < HD; i++) xp[i] = x[omega_dattn256_perm_slot(i, R)]; 192 for (unsigned i = 0; i < 128; i++) { double cs = row[i], sn = row[128 + i]; yp[i] = xp[i] * cs - xp[i + 128] * sn; yp[i + 128] = xp[i + 128] * cs + xp[i] * sn; } 193 for (unsigned i = 0; i < HD; i++) y_back[omega_dattn256_perm_slot(i, R)] = yp[i]; 194 for (unsigned i = 0; i < HD; i++) { double d = fabs(y_back[i] - y_model[i]); if (d > maxd) maxd = d; } 195 } 196 CHECK(maxd < 1e-5, "perm rotary %u: rotate-half on the permuted head == model rope (max abs diff %.3g; f32 table)", R, maxd); 197 printf("perm rotary %3u: bijection, pairs, rope-by-permutation max abs diff %.3g over 50 positions\n", R, maxd); 198 } 199 } 200 static void host_only(void) { 201 float row[HD]; omega_dattn256_rope_row(0, THETA, 64, row); 202 int ok = 1; for (unsigned i = 0; i < 128; i++) ok &= row[i] == 1.0f && row[128 + i] == 0.0f; 203 CHECK(ok, "rope row 0 is cos 1 sin 0"); 204 omega_dattn256_rope_row(100, THETA, 64, row); 205 CHECK(fabs((double)row[0] - cos(100.0)) < 1e-6 && fabs((double)row[128] - sin(100.0)) < 1e-6, "rope row 100 pair 0 is cos/sin(100)"); 206 CHECK(fabs((double)row[31] - cos(100.0 * pow(THETA, -62.0 / 64.0))) < 1e-6, "rope row 100 pair 31 uses theta^(-62/64)"); 207 ok = 1; for (unsigned i = 32; i < 128; i++) ok &= row[i] == 1.0f && row[128 + i] == 0.0f; 208 CHECK(ok, "rope row 100: pairs 32..127 are identity for rotary_dim 64"); 209 omega_dattn256_rope_row(100, THETA, 256, row); 210 CHECK(fabs((double)row[127] - cos(100.0 * pow(THETA, -254.0 / 256.0))) < 1e-6, "rope row 100 rotary 256 pair 127 uses theta^(-254/256)"); 211 perm_checks(); 212 /* reference sanity: context 1 returns v_new (rounded), whatever q and k */ 213 { Case c; case_make(&c, 256, 0, 64, 11, 1.0f); double *ref = malloc(OW * sizeof(double)); uint16_t kn[KVH * HD], vn[KVH * HD]; 214 reference(c.qkv, c.kc, c.vc, 256, 0, 64, c.qn, c.kn, EPS, THETA, 1.0f, ref, kn, vn); 215 int good = 1; for (unsigned h = 0; h < QH && good; h++) for (unsigned i = 0; i < HD; i++) if (ref[h * HD + i] != (double)omega_dattn_bf16_f32(vn[(h / (QH / KVH)) * HD + i])) { good = 0; break; } 216 CHECK(good, "reference: context 1 returns the rounded v_new of the head's KV group"); 217 free(ref); case_free(&c); } 218 { Case c; case_make(&c, 256, 8, 64, 12, 1.0f); 219 double *ref = malloc(OW * sizeof(double)); uint16_t kn[KVH * HD], vn[KVH * HD]; 220 reference(c.qkv, c.kc, c.vc, 256, 8, 64, c.qn, c.kn, EPS, THETA, 1.0f, ref, kn, vn); 221 double ss = 0; for (unsigned i = 0; i < HD; i++) { double v = omega_dattn_bf16_f32(kn[i]); ss += v * v; } 222 CHECK(ss / HD > 0.6 && ss / HD < 1.5, "reference: k_new has rms about 1 after RMSNorm (%.3f)", ss / HD); 223 double os = 0; for (unsigned i = 0; i < OW; i++) os += fabs(ref[i]); 224 CHECK(os / OW < 0.6, "reference: a 9-token softmax average of U(-1,1) rows is small (%.3f)", os / OW); 225 free(ref); case_free(&c); } 226 /* codegen budget */ 227 for (uint32_t d = 1; d <= 4; d++) for (int m = 0; m <= 5; m++) { 228 OmegaDecodeAttn256Spec s = { .depth = d, .mutant = m SPEC_SHAPE }; uint32_t n = 0, gp = 0; 229 int rc = omega_decode_attn256_prog_stats(&s, &n, &gp); 230 CHECK(rc == 0 && n <= 1024 && gp <= 126, "codegen depth %u mutant %d: rc %d insns %u gprs %u", d, m, rc, n, gp); 231 if (rc == 0 && m == 0) printf("codegen depth %u: %u instructions, %u registers (declared %u, 256 threads -> %u of 65536)\n", d, n, gp, omega_bw_gpr_declared(gp), 256 * omega_bw_gpr_declared(gp)); 232 } 233 { OmegaDecodeAttn256Spec s = { .depth = 0 SPEC_SHAPE }; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "depth 0 refused"); 234 s.depth = 5; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "depth 5 refused"); 235 s.depth = 1; s.mutant = 6; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "mutant 6 refused"); 236 s.mutant = 0; s.q_heads = 24; s.kv_heads = 4; s.q_per_cta = 5; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "q_per_cta not dividing q heads refused"); 237 s.q_heads = 24; s.kv_heads = 2; s.q_per_cta = 4; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "three CTA rows per KV head refused"); 238 s.q_heads = 16; s.kv_heads = 2; s.q_per_cta = 8; CHECK(omega_decode_attn256_prog_stats(&s, NULL, NULL) != 0, "more than 6 q heads per CTA row refused"); } 239 CHECK(omega_gpu_decode_attn256_cache_alloc_shape(0, 256, THETA, 64, QH, KVH, QPC, NULL) == OMEGA_GPU_DATTN_BAD_ARGS, "NULL out refused"); 240 { OmegaGpuDecodeAttn256Cache *c = (OmegaGpuDecodeAttn256Cache *)1; 241 CHECK(omega_gpu_decode_attn256_cache_alloc_shape(1, 100, THETA, 64, QH, KVH, QPC, &c) == OMEGA_GPU_DATTN_BAD_ARGS && c == NULL, "max_pos not a multiple of 256 refused before any device work"); 242 c = (OmegaGpuDecodeAttn256Cache *)1; 243 CHECK(omega_gpu_decode_attn256_cache_alloc_shape(1, 256, THETA, 63, QH, KVH, QPC, &c) == OMEGA_GPU_DATTN_BAD_ARGS && c == NULL, "odd rotary_dim refused"); 244 c = (OmegaGpuDecodeAttn256Cache *)1; 245 CHECK(omega_gpu_decode_attn256_cache_alloc_shape(1, 256, THETA, 258, QH, KVH, QPC, &c) == OMEGA_GPU_DATTN_BAD_ARGS && c == NULL, "rotary_dim > 256 refused"); 246 c = (OmegaGpuDecodeAttn256Cache *)1; 247 CHECK(omega_gpu_decode_attn256_cache_alloc_shape(1, 131072 + 256, THETA, 64, QH, KVH, QPC, &c) == OMEGA_GPU_DATTN_BAD_ARGS && c == NULL, "max_pos > 131072 refused"); } 248 CHECK(OMEGA_DATTN256_SHARED_BYTES <= 16384u, "shared memory within the QMD cap (%u)", OMEGA_DATTN256_SHARED_BYTES); 249 CHECK(OMEGA_DATTN256_SM_FLAG + 16u == OMEGA_DATTN256_SHARED_BYTES && OMEGA_DATTN256_SM_S + 8192u <= OMEGA_DATTN256_SM_M && OMEGA_DATTN256_SM_M + 288u <= OMEGA_DATTN256_SM_L && OMEGA_DATTN256_SM_L + 256u <= OMEGA_DATTN256_SM_FLAG, "shared map does not overlap"); 250 } 251 252 /* ------------------------------------------------------------- simulator */ 253 static uint64_t g_sim_issues; 254 static int sim_point(uint32_t max_pos, uint32_t pos, uint32_t rotary, uint32_t depth, int mutant, uint32_t per_split, float qscale, uint32_t seed, char *msg, size_t n, double *worst, size_t *bad, char *rows, size_t rn) { 255 static BlackwellIRProgram prog; 256 OmegaDecodeAttn256Spec spec = { .depth = depth, .mutant = mutant SPEC_SHAPE }; 257 if (omega_blackwell_codegen_decode_attn256_prog(&spec, &prog) != 0) { snprintf(msg, n, "codegen refused"); return -1; } 258 Case c; case_make(&c, max_pos, pos, rotary, seed, qscale); 259 uint16_t *kcp = case_kc_perm(&c); 260 const uint32_t splits = (pos + per_split) / per_split; 261 size_t plane = (size_t)KVH * max_pos * OMEGA_DATTN256_ROW_BYTES + OMEGA_DATTN256_PLANE_SLACK, hist = (size_t)KVH * max_pos * OMEGA_DATTN256_ROW_BYTES; 262 uint8_t *kv = aligned_alloc(16, 2 * plane); memset(kv, 0, 2 * plane); 263 float *rope = aligned_alloc(16, (size_t)max_pos * OMEGA_DATTN256_ROPE_ROW_BYTES); for (uint32_t p = 0; p < max_pos; p++) omega_dattn256_rope_row(p, THETA, rotary, rope + (size_t)p * 256); 264 float *norms = aligned_alloc(16, 2048); 265 float *qkv = aligned_alloc(16, sizeof c.qkv); 266 case_kernel_inputs(&c, qkv, norms, norms + HD); 267 float *out = aligned_alloc(16, OW * 4), *copy = malloc(OW * 4); 268 float *part = aligned_alloc(16, (size_t)VY * splits * OMEGA_DATTN256_PARTIAL_WORDS * 4); 269 uint32_t *cnt = aligned_alloc(16, 16); 270 int err = 0; 271 for (int order = 0; order < 2 && !err; order++) { 272 WsSim s; memset(&s, 0, sizeof s); 273 s.p = &prog; s.threads = 256; s.order = order; 274 static uint8_t smem[OMEGA_DATTN256_SHARED_BYTES]; s.smem = smem; s.smem_bytes = OMEGA_DATTN256_SHARED_BYTES; 275 memset(cnt, 0, 16); for (unsigned i = 0; i < OW; i++) out[i] = -999.0f; 276 memcpy(kv, kcp, hist); memcpy(kv + plane, c.vc, hist); /* fresh history (the kernel appends) */ 277 uint64_t A[9] = { (uintptr_t)qkv, (uintptr_t)kv, (uintptr_t)(kv + plane), (uintptr_t)rope, (uintptr_t)norms, (uintptr_t)(norms + HD), (uintptr_t)out, (uintptr_t)part, (uintptr_t)cnt }; 278 for (unsigned i = 0; i < 9; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); } 279 float fe = EPS, fo = 1.0f, fq = (float)((1.0 / 16.0) * 1.`4426950408889634`); 280 memcpy(&s.cbank[OMEGA_DATTN256_ARG_EPS / 4], &fe, 4); memcpy(&s.cbank[OMEGA_DATTN256_ARG_OUT_SCALE / 4], &fo, 4); memcpy(&s.cbank[OMEGA_DATTN256_ARG_QK_SCALE / 4], &fq, 4); 281 s.cbank[OMEGA_DATTN256_ARG_POS / 4] = pos; s.cbank[OMEGA_DATTN256_ARG_PER_SPLIT / 4] = per_split; s.cbank[OMEGA_DATTN256_ARG_SPLITS / 4] = splits; s.cbank[OMEGA_DATTN256_ARG_KV_STRIDE / 4] = max_pos * OMEGA_DATTN256_ROW_BYTES; 282 s.nranges = 7; 283 s.ranges[0].lo = (uintptr_t)qkv; s.ranges[0].hi = (uintptr_t)qkv + sizeof c.qkv; 284 s.ranges[1].lo = (uintptr_t)kv; s.ranges[1].hi = (uintptr_t)kv + 2 * plane; 285 s.ranges[2].lo = (uintptr_t)rope; s.ranges[2].hi = (uintptr_t)rope + (size_t)max_pos * OMEGA_DATTN256_ROPE_ROW_BYTES; 286 s.ranges[3].lo = (uintptr_t)norms; s.ranges[3].hi = (uintptr_t)norms + 2048; 287 s.ranges[4].lo = (uintptr_t)out; s.ranges[4].hi = (uintptr_t)out + OW * 4; 288 s.ranges[5].lo = (uintptr_t)part; s.ranges[5].hi = (uintptr_t)part + (size_t)VY * splits * OMEGA_DATTN256_PARTIAL_WORDS * 4; 289 s.ranges[6].lo = (uintptr_t)cnt; s.ranges[6].hi = (uintptr_t)cnt + 16; 290 err = ws_run(&s, splits, VY); 291 if (err) { snprintf(msg, n, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; } 292 g_sim_issues += s.warp_issues; 293 size_t unwritten = 0; for (unsigned i = 0; i < OW; i++) if (out[i] == -999.0f) unwritten++; 294 if (unwritten) { err = -1; snprintf(msg, n, "%zu output words never written", unwritten); break; } 295 if (cnt[0] | cnt[1] | cnt[2] | cnt[3]) { err = -1; snprintf(msg, n, "counters not reset: %u %u %u %u", cnt[0], cnt[1], cnt[2], cnt[3]); break; } 296 if (order == 0) memcpy(copy, out, OW * 4); 297 else if (memcmp(copy, out, OW * 4) != 0) { err = -1; snprintf(msg, n, "the fragment order changed the result"); } 298 } 299 if (!err) { 300 uint16_t kn[KVH * HD], vn[KVH * HD]; 301 *bad = parity(&c, out, 1.0f, worst, kn, vn); 302 uint16_t kchip[KVH * HD], vchip[KVH * HD]; 303 for (unsigned g = 0; g < KVH; g++) { memcpy(kchip + g * HD, kv + ((size_t)g * max_pos + pos) * OMEGA_DATTN256_ROW_BYTES, OMEGA_DATTN256_ROW_BYTES); memcpy(vchip + g * HD, kv + plane + ((size_t)g * max_pos + pos) * OMEGA_DATTN256_ROW_BYTES, OMEGA_DATTN256_ROW_BYTES); } 304 if (!rows_ok(kchip, vchip, kn, vn, rotary, rows, rn)) *bad += 1000; 305 snprintf(msg, n, "bad=%zu worst=%.3f rows: %s", *bad, *worst, rows); 306 } 307 free(kv); free(rope); free(norms); free(qkv); free(out); free(copy); free(part); free(cnt); free(kcp); case_free(&c); 308 return err; 309 } 310 static void sim_battery(void) { 311 char msg[300], rows[160]; double worst; size_t bad; 312 struct { uint32_t max_pos, pos, rotary, depth, per; float qscale; const char *name; } pts[] = { 313 { 256, 0, 64, 1, 256, 1.0f, "context 1" }, 314 { 256, 6, 64, 2, 256, 1.0f, "context 7" }, 315 { 256, 6, 64, 4, 256, 4.0f, "context 7, sharp q (x4)" }, 316 { 256, 6, 256, 4, 256, 1.0f, "context 7, full rope (rotary 256)" }, 317 { 256, 255, 64, 4, 256, 1.0f, "context 256 (full chunk, 1 split)" }, 318 { 512, 256, 64, 4, 256, 1.0f, "context 257 (2 splits, last holds one position)" }, 319 { 512, 300, 64, 3, 256, 1.0f, "context 301 (2 splits)" }, 320 { 1024, 700, 64, 4, 256, 1.0f, "context 701 (3 splits)" }, 321 { 1024, 700, 64, 2, 512, 1.0f, "context 701 (per_split 512: 2 chunks per CTA)" }, 322 }; 323 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { 324 int err = sim_point(pts[i].max_pos, pts[i].pos, pts[i].rotary, pts[i].depth, 0, pts[i].per, pts[i].qscale, 100 + (uint32_t)i, msg, sizeof msg, &worst, &bad, rows, sizeof rows); 325 CHECK(err == 0 && bad == 0, "sim %s depth %u: %s", pts[i].name, pts[i].depth, msg); 326 printf("sim %-50s depth %u: %s %s\n", pts[i].name, pts[i].depth, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); 327 } 328 for (int m = 1; m <= 5; m++) { 329 int err = sim_point(512, 300, 64, 2, m, 256, 1.0f, 200, msg, sizeof msg, &worst, &bad, rows, sizeof rows); 330 int caught = err != 0 || bad > 0; 331 CHECK(caught, "sim mutant %d NOT caught (%s)", m, msg); 332 printf("sim mutant %d at context 301: %s (%s)\n", m, caught ? "caught" : "MISSED", msg); 333 } 334 printf("sim: %" PRIu64 " warp issues in total\n", g_sim_issues); 335 } 336 337 /* ------------------------------------------------------------- chip */ 338 static int g_evict = 1; static const char *g_only; static uint32_t g_maxp = 32768; 339 static void evict_host_caches(void) { 340 static uint8_t *buf; const size_t sz = 512u << 20; static uint8_t pass; 341 if (!buf) buf = malloc(sz); 342 if (!buf) return; 343 memset(buf, ++pass, sz); 344 volatile uint64_t sum = 0; for (size_t i = 0; i < sz; i += 64) sum += buf[i]; 345 } 346 static int fill_history(OmegaGpuDecodeAttn256Cache *cache, const Case *c, const uint16_t *kcp) { 347 if (c->pos == 0) return OMEGA_GPU_DATTN_OK; 348 for (unsigned g = 0; g < KVH; g++) { 349 int rc = omega_gpu_decode_attn256_cache_write(cache, 0, g, 0, c->pos, kcp + (size_t)g * c->max_pos * HD, c->vc + (size_t)g * c->max_pos * HD); 350 if (rc != OMEGA_GPU_DATTN_OK) return rc; 351 } 352 return OMEGA_GPU_DATTN_OK; 353 } 354 static int chip_run(const char *out_path, uint32_t reps) { 355 FILE *out = out_path ? test_out_open(out_path) : NULL; 356 if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; } 357 if (out) fprintf(out, "{\"receipt\":\"OMEGA_GPU_DECODE_ATTN256_V1\",\"reps\":%u,\"evict\":%d,\"max_pos\":%u,\"rotary_dim\":64,\"points\":[", reps, g_evict, g_maxp); 358 const uint32_t MAXP = g_maxp; 359 OmegaGpuDecodeAttn256Cache *cache = NULL; 360 double t0 = now_ms(); 361 int rc = omega_gpu_decode_attn256_cache_alloc_shape(1, MAXP, THETA, 64, QH, KVH, QPC, &cache); 362 printf("cache alloc (1 layer, %u positions, %.0f MiB): rc=%s %.1f ms (%s)\n", MAXP, (double)MAXP * 2 * KVH * 512 / `1048576`.0, omega_gpu_decode_attn_rc_name(rc), now_ms() - t0, rc ? omega_gpu_decode_attn_last_error() : ""); 363 CHECK(rc == OMEGA_GPU_DATTN_OK, "cache alloc"); 364 if (rc != OMEGA_GPU_DATTN_OK) { if (out) fprintf(out, "],\"verdict\":\"OMEGA_GPU_DECODE_ATTN256_FAIL\"}\n"); return 1; } 365 struct { uint32_t pos; float qscale; const char *name; } pts[] = { 366 { 0, 1.0f, "context 1" }, { 6, 1.0f, "context 7" }, { 255, 1.0f, "context 256" }, { 300, 4.0f, "context 301 sharp q" }, 367 { 1023, 1.0f, "context 1024" }, { 4095, 1.0f, "context 4096" }, { 8191, 1.0f, "context 8192" }, { 32767, 1.0f, "context 32768" }, 368 { 131071, 1.0f, "context 131072" }, 369 }; 370 int first = 1; 371 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { 372 if (pts[i].pos >= MAXP) continue; 373 if (g_only && !strstr(pts[i].name, g_only)) continue; 374 Case c; case_make(&c, MAXP, pts[i].pos, 64, 500 + (uint32_t)i, pts[i].qscale); 375 uint16_t *kcp = case_kc_perm(&c); 376 float *qkv_k = malloc(QW * 4), qn_k[HD], kn_k[HD]; 377 case_kernel_inputs(&c, qkv_k, qn_k, kn_k); 378 CHECK(omega_gpu_decode_attn256_set_norms(cache, 0, qn_k, kn_k) == OMEGA_GPU_DATTN_OK, "set norms"); 379 t0 = now_ms(); 380 rc = fill_history(cache, &c, kcp); 381 double fill_ms = now_ms() - t0; 382 float *y = malloc(OW * 4), *y2 = malloc(OW * 4); 383 OmegaGpuDecodeAttn256Info info; memset(&info, 0, sizeof info); 384 if (rc == OMEGA_GPU_DATTN_OK) rc = omega_gpu_decode_attn256(cache, 0, c.pos, qkv_k, EPS, 1.0f, y, &info); 385 size_t bad = 0; double worst = 1e9; uint16_t kn[KVH * HD], vn[KVH * HD], kchip[KVH * HD], vchip[KVH * HD]; char rows[160] = "n/a"; 386 if (rc == OMEGA_GPU_DATTN_OK) { 387 bad = parity(&c, y, 1.0f, &worst, kn, vn); 388 for (unsigned g = 0; g < KVH; g++) if (omega_gpu_decode_attn256_cache_read(cache, 0, g, c.pos, kchip + g * HD, vchip + g * HD) != OMEGA_GPU_DATTN_OK) bad += 1000; 389 if (!rows_ok(kchip, vchip, kn, vn, 64, rows, sizeof rows)) bad += 1000; 390 } 391 int ok = rc == OMEGA_GPU_DATTN_OK && bad == 0; 392 int same = 0; OmegaGpuDecodeAttn256Info i2; memset(&i2, 0, sizeof i2); 393 if (ok) { int r2 = omega_gpu_decode_attn256(cache, 0, c.pos, qkv_k, EPS, 1.0f, y2, &i2); same = r2 == OMEGA_GPU_DATTN_OK && memcmp(y, y2, OW * 4) == 0 && i2.kernel_cache_hit; } 394 CHECK(ok && same, "chip %s: rc=%s bad=%zu worst=%.3f rows=%s second-call-identical=%d (%s)", pts[i].name, omega_gpu_decode_attn_rc_name(rc), bad, worst, rows, same, omega_gpu_decode_attn_last_error()); 395 if (ok) { 396 float *ys = malloc(OW * 4); int r3 = omega_gpu_decode_attn256(cache, 0, c.pos, qkv_k, EPS, `18446744073709551616`.0f, ys, NULL); 397 int good = r3 == OMEGA_GPU_DATTN_OK; double wr = 0; 398 for (unsigned k = 0; k < OW && good; k++) { double e = fabs((double)ys[k] / `18446744073709551616`.0 - (double)y[k]); double r = e / (fabs((double)y[k]) + 1e-6); if (r > wr) wr = r; } 399 CHECK(good && wr < 1e-5, "chip %s: out_scale 2^64 matches the unscaled output (worst rel %.2e)", pts[i].name, wr); 400 free(ys); 401 } 402 uint64_t *el = calloc(reps, sizeof *el), *cl = calloc(reps, sizeof *cl); uint32_t done = 0; 403 if (g_evict) evict_host_caches(); 404 for (uint32_t r = 0; r < reps && ok; r++) { OmegaGpuDecodeAttn256Info ti; if (omega_gpu_decode_attn256(cache, 0, c.pos, qkv_k, EPS, 1.0f, y2, &ti) != OMEGA_GPU_DATTN_OK) break; el[r] = ti.elapsed_ns; cl[r] = ti.call_ns; done++; } 405 uint64_t med_el = 0, med_cl = 0; double gbs = 0; 406 if (done) { qsort(el, done, sizeof *el, cmp_u64); qsort(cl, done, sizeof *cl, cmp_u64); med_el = el[done / 2]; med_cl = cl[done / 2]; gbs = med_el ? (double)info.kv_bytes / (double)med_el : 0; } 407 printf("chip %-24s rc=%-9s bad=%zu worst=%.3f rows=%s grid=%ux%u per_split=%u depth=%u kv_bytes=%" PRIu64 " fill=%.1fms chip_med=%.1fus (%.1f GB/s) call_med=%.1fus reps=%u\n", 408 pts[i].name, omega_gpu_decode_attn_rc_name(rc), bad, worst, rows, info.grid_x, info.grid_y, info.per_split, info.depth, info.kv_bytes, fill_ms, med_el / 1e3, gbs, med_cl / 1e3, done); 409 if (out) fprintf(out, "%s{\"name\":\"%s\",\"pos\":%u,\"rc\":\"%s\",\"bad\":%zu,\"worst_of_tol\":%.3f,\"rows\":\"%s\",\"second_call_identical\":%d,\"grid_x\":%u,\"grid_y\":%u,\"per_split\":%u,\"depth\":%u," 410 "\"kv_bytes\":%" PRIu64 ",\"fill_ms\":%.1f,\"chip_median_ns\":%" PRIu64 ",\"gb_per_s\":%.1f,\"call_median_ns\":%" PRIu64 ",\"reps\":%u}", 411 first ? "" : ",", pts[i].name, c.pos, omega_gpu_decode_attn_rc_name(rc), bad, worst, rows, same, info.grid_x, info.grid_y, info.per_split, info.depth, info.kv_bytes, fill_ms, med_el, gbs, med_cl, done); 412 first = 0; 413 free(el); free(cl); 414 if (ok && c.pos == 300) { 415 for (int m = 1; m <= 5; m++) { 416 omega_gpu_decode_attn256_test_set_mutant(m); 417 (void)fill_history(cache, &c, kcp); /* mutant 3 must not inherit the row the honest kernel wrote */ 418 uint16_t zk[HD] = { 0 }, zv[HD] = { 0 }; 419 for (unsigned g = 0; g < KVH; g++) (void)omega_gpu_decode_attn256_cache_write(cache, 0, g, c.pos, 1, zk, zv); 420 int r = omega_gpu_decode_attn256(cache, 0, c.pos, qkv_k, EPS, 1.0f, y2, NULL); 421 size_t mb = 0; double mw = 0; uint16_t k2[KVH * HD], v2[KVH * HD]; 422 if (r == OMEGA_GPU_DATTN_OK) { mb = parity(&c, y2, 1.0f, &mw, k2, v2); } 423 int caught = r != OMEGA_GPU_DATTN_OK || mb > 0; 424 if (m == 4 && info.grid_x == 1) { printf("chip %-24s mutant 4: inert (1 split, the combine is not exercised)\n", pts[i].name); omega_gpu_decode_attn256_test_set_mutant(0); continue; } 425 CHECK(caught, "chip mutant %d NOT caught", m); 426 printf("chip %-24s mutant %d: %s (rc=%s bad=%zu worst=%.3f)\n", pts[i].name, m, caught ? "caught" : "MISSED", omega_gpu_decode_attn_rc_name(r), mb, mw); 427 if (out) fprintf(out, ",{\"name\":\"%s mutant %d\",\"caught\":%d,\"rc\":\"%s\",\"bad\":%zu,\"worst_of_tol\":%.3f}", pts[i].name, m, caught, omega_gpu_decode_attn_rc_name(r), mb, mw); 428 omega_gpu_decode_attn256_test_set_mutant(0); 429 } 430 } 431 free(y); free(y2); free(qkv_k); free(kcp); case_free(&c); 432 if (omega_gpu_session_is_blocked()) { printf("session latched: stopping\n"); break; } 433 } 434 omega_gpu_decode_attn256_cache_free(cache); 435 if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"device_opens\":%u,\"verdict\":\"%s\"}\n", g_checks, g_failed, omega_gpu_session_open_count(), g_failed == 0 ? "OMEGA_GPU_DECODE_ATTN256_PASS" : "OMEGA_GPU_DECODE_ATTN256_FAIL"); fclose(out); } 436 return 0; 437 } 438 439 /* --q-heads Q --kv-heads K --head-dim D: any geometry other than 24 / 4 / 256 is REFUSED (receipt verdict "REFUSED", exit 3). */ 440 static int geometry_refusal(uint32_t qh, uint32_t kvh, uint32_t hd, const char *out_path) { 441 if (qh == QH && kvh == KVH && hd == HD) return 0; 442 printf("REFUSED: decode attention 256 at %u query / %u KV heads, head dim %u (built for %u / %u / %u)\n", qh, kvh, hd, QH, KVH, HD); 443 if (out_path) { FILE *f = test_out_open(out_path); if (f) { fprintf(f, "{\"test\":\"gpu_decode_attn256_test\",\"q_heads\":%u,\"kv_heads\":%u,\"head_dim\":%u,\"verdict\":\"REFUSED\"}\n", qh, kvh, hd); fclose(f); } } 444 return 3; 445 } 446 447 int main(int argc, char **argv) { 448 int host = 0, sim = 0; const char *out_path = NULL; uint32_t reps = 20, depth = 0, per = 0, spin = 2000; int sim_pos = -1; uint32_t sim_max = 256; 449 uint32_t q_heads = QH, kv_heads = KVH, head_dim = HD; 450 for (int i = 1; i < argc; i++) { 451 if (strcmp(argv[i], "--host-only") == 0) host = 1; 452 else if (strcmp(argv[i], "--sim") == 0) sim = 1; 453 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; 454 else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); 455 else if (strcmp(argv[i], "--depth") == 0 && i + 1 < argc) depth = (uint32_t)atoi(argv[++i]); 456 else if (strcmp(argv[i], "--per-split") == 0 && i + 1 < argc) per = (uint32_t)atoi(argv[++i]); 457 else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) spin = (uint32_t)atoi(argv[++i]); 458 else if (strcmp(argv[i], "--no-evict") == 0) g_evict = 0; 459 else if (strcmp(argv[i], "--sim-pos") == 0 && i + 1 < argc) sim_pos = atoi(argv[++i]); 460 else if (strcmp(argv[i], "--sim-max") == 0 && i + 1 < argc) sim_max = (uint32_t)atoi(argv[++i]); 461 else if (strcmp(argv[i], "--only") == 0 && i + 1 < argc) g_only = argv[++i]; 462 else if (strcmp(argv[i], "--max-pos") == 0 && i + 1 < argc) g_maxp = (uint32_t)atoi(argv[++i]); 463 else if (strcmp(argv[i], "--q-heads") == 0 && i + 1 < argc) q_heads = (uint32_t)atoi(argv[++i]); 464 else if (strcmp(argv[i], "--kv-heads") == 0 && i + 1 < argc) kv_heads = (uint32_t)atoi(argv[++i]); 465 else if (strcmp(argv[i], "--head-dim") == 0 && i + 1 < argc) head_dim = (uint32_t)atoi(argv[++i]); 466 else { printf("unknown argument %s\n", argv[i]); return 2; } 467 } 468 { int g = geometry_refusal(q_heads, kv_heads, head_dim, out_path); if (g) return g; } 469 if (host) host_only(); 470 if (sim && sim_pos >= 0 && sim_max <= (uint32_t)sim_pos) sim_max = (uint32_t)sim_pos + 1u; 471 if (sim && sim_pos >= 0) { char msg[300], rows[160]; double worst; size_t bad; int err = sim_point(sim_max, (uint32_t)sim_pos, 64, depth ? depth : 2, 0, per ? per : 256, 1.0f, 777, msg, sizeof msg, &worst, &bad, rows, sizeof rows); printf("sim pos %d: %s %s\n", sim_pos, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); return err || bad; } 472 if (sim) sim_battery(); 473 if (!host && !sim) { 474 if (depth) omega_gpu_decode_attn256_set_depth(depth); 475 if (per) omega_gpu_decode_attn256_set_per_split(per); 476 (void)omega_gpu_session_set_spin_us(spin); 477 chip_run(out_path, reps ? reps : 1); 478 omega_gpu_session_close(); 479 } 480 printf("gpu_decode_attn256_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); 481 return g_failed ? 1 : 0; 482 }