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
omega-mv / evidence / FB1-CUT5-cd80bf4 / gpu_attention_test.c
| 1 | /* gpu_attention_test: FB-1 cut 5 gate for omega_gpu_attention_api. |
| 2 | * |
| 3 | * ./gpu_attention_test --host-only refusals, codegen (both kernel families and every |
| 4 | * mutant), register budget, nvdisasm listing check |
| 5 | * ./gpu_attention_test --timing [--out r.json] cut 4b gate: 1 query x 32 heads x 2048 ctx, 100 calls |
| 6 | * through the persistent session, median under 3 ms |
| 7 | * ./gpu_attention_test --sweep [--out s.json] head-scaling sweep: paged bf16 at head_dim 64 over q heads |
| 8 | * 1..64 x every kv-head divisor x ctx 256/2048, parity + timings |
| 9 | * (receipt OMEGA_GPU_ATTENTION_SWEEP_V1; --sim marks host timings) |
| 10 | * ./gpu_attention_test --out receipt.json chip gate: every shape against the host oracle, the |
| 11 | * mutants (deliberately wrong kernels, all of |
| 12 | * 1 .. OMEGA_GPU_ATTN_MUTANT_COUNT-1) which the same |
| 13 | * check must catch, a negative control, timings |
| 14 | * |
| 15 | * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/backend.rs (main 8b5caed) in C: |
| 16 | * ReferenceCpuBackend::gqa_attention (f64 scores, max, exp, sum, 1/max(sum,1e-12), f64 weighted sum), |
| 17 | * TensorBackend::paged_attention (f64 online softmax token by token over the paged bf16 pool, tokens |
| 18 | * past the block table dropped, zeros for empty context) and paged_attention_batch (ctx <= 0 -> 0, |
| 19 | * table rows past the array -> empty, negative entries removed). Compiled with -ffp-contract=off. |
| 20 | * |
| 21 | * Tolerance (why): the chip accumulates the 64-term dot products, the per-chunk sums and the |
| 22 | * rescaled running sums in f32 (worst case about n * 2^-24 relative over n = 2048 terms, 1.2e-4), |
| 23 | * uses MUFU.EX2 (PTX ex2.approx: 2^-22.5 relative) for every weight and for the 32 chunk rescale |
| 24 | * factors, and MUFU.RCP for the final normalisation. Every output is a convex combination of V |
| 25 | * values of unit scale, so |error| <= 2e-4 * |want| + 2e-5 covers these with margin; the mutants |
| 26 | * move results by 1e-2 or more (wrong head, wrong token) or produce NaN (overflow without the max). |
| 27 | * The large-score case (scores up to ~300) carries the f32 rounding of the score itself into the |
| 28 | * exponent (3e-5 absolute per score), so it uses 1e-3 relative + 1e-4 absolute. |
| 29 | */ |
| 30 | #include "omega_gpu_attention_api.h" |
| 31 | #include "omega_gpu_session.h" |
| 32 | #include "omega_blackwell_codegen.h" |
| 33 | #include <inttypes.h> |
| 34 | #include <math.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <time.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | static int g_checks, g_failed; |
| 42 | static int g_sim_mode; /* --sim: the host simulator never opens the device */ |
| 43 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 44 | |
| 45 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 46 | static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } |
| 47 | static uint16_t f2bf(float f) { uint32_t u; memcpy(&u, &f, 4); uint32_t r = ((u >> 16) & 1u) + 0x7fffu; return (uint16_t)((u + r) >> 16); } |
| 48 | static float bf2f(uint16_t b) { uint32_t u = (uint32_t)b << 16; float f; memcpy(&f, &u, 4); return f; } |
| 49 | |
| 50 | /* ----------------------------------------------------------------- oracles */ |
| 51 | static void oracle_gqa(const float *q, const float *kc, const float *vc, size_t seq_len, size_t nqh, size_t nkv, size_t hd, float *out) { |
| 52 | if (seq_len == 0) { memset(out, 0, nqh * hd * 4); return; } |
| 53 | size_t ratio = nqh / nkv; double inv_sqrt_d = 1.0 / sqrt((double)hd); |
| 54 | double *scores = malloc(seq_len * sizeof *scores); |
| 55 | for (size_t h = 0; h < nqh; h++) { |
| 56 | size_t kvh = h / ratio; const float *qh = q + h * hd; |
| 57 | for (size_t t = 0; t < seq_len; t++) { |
| 58 | const float *kt = kc + (t * nkv + kvh) * hd; double dot = 0.0; |
| 59 | for (size_t d = 0; d < hd; d++) dot += (double)qh[d] * (double)kt[d]; |
| 60 | scores[t] = dot * inv_sqrt_d; |
| 61 | } |
| 62 | double mx = -INFINITY; for (size_t t = 0; t < seq_len; t++) if (scores[t] > mx) mx = scores[t]; |
| 63 | double sum = 0.0; for (size_t t = 0; t < seq_len; t++) { scores[t] = exp(scores[t] - mx); sum += scores[t]; } |
| 64 | double inv = 1.0 / (sum > 1e-12 ? sum : 1e-12); |
| 65 | for (size_t t = 0; t < seq_len; t++) scores[t] *= inv; |
| 66 | for (size_t d = 0; d < hd; d++) { |
| 67 | double s = 0.0; |
| 68 | for (size_t t = 0; t < seq_len; t++) s += scores[t] * (double)vc[(t * nkv + kvh) * hd + d]; |
| 69 | out[h * hd + d] = (float)s; |
| 70 | } |
| 71 | } |
| 72 | free(scores); |
| 73 | } |
| 74 | /* bf16 pool in the canonical layout; the oracle's read_kv_head: element_offset(block, layer, is_value, token) + kv_head*head_dim*2 */ |
| 75 | static const uint16_t *kv_head(const uint8_t *pool, const OmegaGpuKvLayout *ly, size_t blk, size_t layer, int is_v, size_t tok, size_t kvh, size_t hd) { |
| 76 | size_t off = blk * ly->block_stride_bytes + layer * ly->layer_stride_bytes + (is_v ? ly->kv_plane_stride_bytes : 0) + tok * ly->token_stride_bytes + kvh * hd * 2; |
| 77 | return (const uint16_t *)(pool + off); |
| 78 | } |
| 79 | static void oracle_paged(const float *q, const uint8_t *pool, const OmegaGpuKvLayout *ly, const uint32_t *ids, size_t nids, size_t ctx, size_t layer, |
| 80 | size_t nqh, size_t nkv, size_t hd, float *out) { |
| 81 | if (ctx == 0 || nids == 0) { memset(out, 0, nqh * hd * 4); return; } |
| 82 | size_t ratio = nqh / nkv; double inv_sqrt_d = 1.0 / sqrt((double)hd); size_t bs = ly->block_size; |
| 83 | double *acc = malloc(hd * sizeof *acc); |
| 84 | for (size_t h = 0; h < nqh; h++) { |
| 85 | size_t kvh = h / ratio; const float *qh = q + h * hd; |
| 86 | double m = -INFINITY, l = 0.0; for (size_t d = 0; d < hd; d++) acc[d] = 0.0; |
| 87 | for (size_t t = 0; t < ctx; t++) { |
| 88 | size_t bi = t / bs; if (bi >= nids) break; |
| 89 | const uint16_t *kh = kv_head(pool, ly, ids[bi], layer, 0, t % bs, kvh, hd); |
| 90 | const uint16_t *vh = kv_head(pool, ly, ids[bi], layer, 1, t % bs, kvh, hd); |
| 91 | double dot = 0.0; for (size_t d = 0; d < hd; d++) dot += (double)qh[d] * (double)bf2f(kh[d]); |
| 92 | double score = dot * inv_sqrt_d, mn = m > score ? m : score, alpha = exp(m - mn), beta = exp(score - mn); |
| 93 | l = l * alpha + beta; |
| 94 | for (size_t d = 0; d < hd; d++) acc[d] = acc[d] * alpha + beta * (double)bf2f(vh[d]); |
| 95 | m = mn; |
| 96 | } |
| 97 | double inv = l > 0.0 ? 1.0 / l : 0.0; |
| 98 | for (size_t d = 0; d < hd; d++) out[h * hd + d] = (float)(acc[d] * inv); |
| 99 | } |
| 100 | free(acc); |
| 101 | } |
| 102 | static void oracle_batch(const float *q, const uint8_t *pool, const OmegaGpuKvLayout *ly, const int32_t *tables, const int32_t *ctxs, size_t maxb, size_t nseq, |
| 103 | size_t layer, size_t nqh, size_t nkv, size_t hd, float *out) { |
| 104 | size_t stride = nqh * hd; |
| 105 | uint32_t *ids = malloc((maxb ? maxb : 1) * sizeof *ids); |
| 106 | for (size_t s = 0; s < nseq; s++) { |
| 107 | size_t ctx = ctxs[s] > 0 ? (size_t)ctxs[s] : 0, n = 0; |
| 108 | size_t total = nseq * maxb; /* the table array length as the trait sees it */ |
| 109 | if ((s + 1) * maxb <= total) for (size_t b = 0; b < maxb; b++) if (tables[s * maxb + b] >= 0) ids[n++] = (uint32_t)tables[s * maxb + b]; |
| 110 | oracle_paged(q + s * stride, pool, ly, ids, n, ctx, layer, nqh, nkv, hd, out + s * stride); |
| 111 | } |
| 112 | free(ids); |
| 113 | } |
| 114 | |
| 115 | static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { |
| 116 | size_t bad = 0; *worst = 0.0; |
| 117 | for (size_t i = 0; i < n; i++) { |
| 118 | double d = fabs((double)got[i] - (double)want[i]); |
| 119 | double lim = rel * fabs((double)want[i]) + abs_tol; |
| 120 | double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); |
| 121 | if (scaled > *worst) *worst = scaled; |
| 122 | if (!(d <= lim)) bad++; /* NaN counts as bad */ |
| 123 | } |
| 124 | return bad; |
| 125 | } |
| 126 | |
| 127 | /* ------------------------------------------------------------- host only */ |
| 128 | static int nvdisasm_check(bool f32, const char *name, const char *const *must, int n_must) { |
| 129 | const char *nv = "/usr/local/cuda/bin/nvdisasm"; |
| 130 | if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } |
| 131 | OmegaBlackwellKernel k; memset(&k, 0, sizeof k); |
| 132 | if (omega_gpu_attention_codegen(f32, f32 ? 12 : 4, 3, &k) != OMEGA_GPU_ATTN_OK) return -1; |
| 133 | char bin[64], lst[64], cmd[256]; |
| 134 | snprintf(bin, sizeof bin, "/tmp/omega_attn_%s_%d.bin", name, (int)getpid()); |
| 135 | snprintf(lst, sizeof lst, "/tmp/omega_attn_%s_%d.sass", name, (int)getpid()); |
| 136 | FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; } |
| 137 | fwrite(k.code, 1, k.code_size, f); fclose(f); |
| 138 | snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); |
| 139 | int rc = system(cmd); |
| 140 | free(k.code); |
| 141 | FILE *l = fopen(lst, "r"); if (!l) return -1; |
| 142 | char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; |
| 143 | while (fgets(line, sizeof line, l)) { |
| 144 | if (!strstr(line, "/*0")) continue; |
| 145 | insns++; |
| 146 | if (strstr(line, "?") || strstr(line, "error")) bad++; |
| 147 | for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; |
| 148 | } |
| 149 | fclose(l); |
| 150 | int miss = 0; |
| 151 | for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } |
| 152 | printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); |
| 153 | unlink(bin); |
| 154 | return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; |
| 155 | } |
| 156 | |
| 157 | static OmegaGpuKvLayout layout_for(uint32_t num_blocks, uint32_t bs, uint32_t layers, uint32_t nkv, uint32_t hd) { |
| 158 | OmegaGpuKvLayout ly; |
| 159 | ly.head_stride_bytes = (uint64_t)hd * 2; ly.token_stride_bytes = ly.head_stride_bytes * nkv; |
| 160 | ly.kv_plane_stride_bytes = ly.token_stride_bytes * bs; ly.layer_stride_bytes = 2 * ly.kv_plane_stride_bytes; |
| 161 | ly.block_stride_bytes = ly.layer_stride_bytes * layers; ly.pool_bytes = ly.block_stride_bytes * num_blocks; |
| 162 | ly.num_blocks = num_blocks; ly.num_layers = layers; ly.block_size = bs; |
| 163 | return ly; |
| 164 | } |
| 165 | |
| 166 | static void host_only(void) { |
| 167 | float q[64 * 64], kv[64 * 64], out[64 * 64]; memset(q, 0, sizeof q); memset(kv, 0, sizeof kv); |
| 168 | CHECK(omega_gpu_gqa_attention_f32(NULL, kv, kv, 1, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "NULL q refused"); |
| 169 | CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 32, 4, 128, out, NULL) == OMEGA_GPU_ATTN_TOO_LARGE, "head_dim 128 refused (TOO_LARGE)"); |
| 170 | CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 6, 2, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "gqa ratio 3 refused"); |
| 171 | CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 32, 5, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "32 q heads / 5 kv heads refused"); |
| 172 | CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, OMEGA_GPU_ATTN_MAX_GQA_CTX + 1, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_TOO_LARGE, "context past the envelope refused"); |
| 173 | for (int i = 0; i < 64; i++) out[i] = 1.0f; |
| 174 | OmegaGpuAttnInfo info; |
| 175 | CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 0, 1, 1, 64, out, &info) == OMEGA_GPU_ATTN_OK && out[0] == 0.0f && out[63] == 0.0f && info.chip_calls == 0, "seq_len 0 writes zeros without a launch"); |
| 176 | OmegaGpuKvLayout ly = layout_for(8, 16, 2, 4, 64); |
| 177 | uint8_t *pool = calloc(ly.pool_bytes, 1); |
| 178 | uint32_t ids[2] = { 1, 9 }; |
| 179 | CHECK(omega_gpu_paged_attention_bf16(q, pool, &ly, ids, 2, 20, 0, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "block id past the pool refused"); |
| 180 | CHECK(omega_gpu_paged_attention_bf16(q, pool, &ly, ids, 1, 20, 2, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "layer past the pool refused"); |
| 181 | OmegaGpuKvLayout bad = ly; bad.block_size = 12; |
| 182 | CHECK(omega_gpu_paged_attention_bf16(q, pool, &bad, ids, 1, 10, 0, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "block size 12 (not a power of two) refused"); |
| 183 | bad = ly; bad.head_stride_bytes = 256; |
| 184 | CHECK(omega_gpu_paged_attention_bf16(q, pool, &bad, ids, 1, 10, 0, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "layout whose head stride is not head_dim*2 refused"); |
| 185 | free(pool); |
| 186 | CHECK(strcmp(omega_gpu_attention_rc_name(OMEGA_GPU_ATTN_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); |
| 187 | |
| 188 | static const char *const must_bf16[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E.U16", "LDG.E ", "LDS R", "STS [R", "STG.E", "LOP3.LUT", "SHF.R.U32.HI" }; |
| 189 | static const char *const must_f32[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E ", "LDS R", "STS [R", "STG.E" }; |
| 190 | for (int f32 = 0; f32 < 2; f32++) { |
| 191 | const char *name = f32 ? "gqa_f32" : "paged_bf16"; |
| 192 | OmegaBlackwellKernel k; memset(&k, 0, sizeof k); |
| 193 | int rc = omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k); |
| 194 | CHECK(rc == OMEGA_GPU_ATTN_OK && k.code_size > 0 && k.gpr_count <= 64, "%s kernel generated (rc %s, %zu insns, %u gprs)", name, omega_gpu_attention_rc_name(rc), k.insn_count, k.gpr_count); |
| 195 | printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", name, k.insn_count, k.gpr_count, k.uniform_gpr_count); |
| 196 | /* every mutant (1 .. COUNT-1) is a distinct kernel: different from the baseline and from |
| 197 | * every other mutant, named, and inside the register budget */ |
| 198 | uint8_t digests[OMEGA_GPU_ATTN_MUTANT_COUNT][32]; memcpy(digests[0], k.code_digest, 32); |
| 199 | free(k.code); |
| 200 | for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) { |
| 201 | omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)m); |
| 202 | memset(&k, 0, sizeof k); |
| 203 | int mrc = omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k); |
| 204 | CHECK(mrc == OMEGA_GPU_ATTN_OK && k.gpr_count <= 64, "%s mutant %s generated (rc %s, %u gprs)", name, omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m), omega_gpu_attention_rc_name(mrc), k.gpr_count); |
| 205 | CHECK(strcmp(omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m), "?") != 0, "mutant %d has a name", m); |
| 206 | memcpy(digests[m], k.code_digest, 32); |
| 207 | free(k.code); |
| 208 | omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); |
| 209 | for (int o = 0; o < m; o++) |
| 210 | CHECK(memcmp(digests[o], digests[m], 32) != 0, "%s mutant %s kernel differs from %s", name, omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m), omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)o)); |
| 211 | } |
| 212 | CHECK(strcmp(omega_gpu_attention_mutant_name(OMEGA_GPU_ATTN_MUTANT_COUNT), "?") == 0, "COUNT has no name"); |
| 213 | /* a different shape key is a different kernel */ |
| 214 | memset(&k, 0, sizeof k); |
| 215 | CHECK(omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 2, &k) == OMEGA_GPU_ATTN_OK && memcmp(digests[0], k.code_digest, 32) != 0, "%s log2gqa 2 kernel differs from log2gqa 3", name); |
| 216 | free(k.code); |
| 217 | CHECK(nvdisasm_check(f32 != 0, name, f32 ? must_f32 : must_bf16, f32 ? 11 : 14) == 0, "%s nvdisasm listing decodes and shows the expected instructions", name); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* ------------------------------------------------------------- chip gate */ |
| 222 | typedef struct { const char *name; size_t n; int rc; size_t bad; double worst; int pass; uint64_t chip_ns, call_ns; uint32_t calls, unwritten; |
| 223 | int mutants_run, mutants_caught; char mutant_note[400]; |
| 224 | uint64_t kv_staged, kv_naive; uint32_t blk_logical, blk_unique; } Case; |
| 225 | static void case_info(Case *c, const OmegaGpuAttnInfo *i) { |
| 226 | c->chip_ns = i->elapsed_ns; c->call_ns = i->call_ns; c->calls = i->chip_calls; c->unwritten = i->unwritten_words; |
| 227 | c->kv_staged = i->kv_bytes_staged; c->kv_naive = i->kv_bytes_naive; c->blk_logical = i->kv_blocks_logical; c->blk_unique = i->kv_blocks_unique; |
| 228 | } |
| 229 | static Case g_cases[32]; static int g_ncases; |
| 230 | static int g_mutant_caught[OMEGA_GPU_ATTN_MUTANT_COUNT], g_mutant_run[OMEGA_GPU_ATTN_MUTANT_COUNT]; |
| 231 | |
| 232 | static void record(Case c, FILE *out) { |
| 233 | g_cases[g_ncases++] = c; |
| 234 | CHECK(c.pass, "%s rc=%s violations=%zu worst=%g", c.name, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst); |
| 235 | CHECK(c.mutants_caught == c.mutants_run, "%s mutants caught %d of %d (%s)", c.name, c.mutants_caught, c.mutants_run, c.mutant_note); |
| 236 | printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " call_ns=%" PRIu64 " calls=%u unwritten=%u kv_staged=%" PRIu64 "/%" PRIu64 " blocks=%u/%u | mutants %d/%d %s\n", |
| 237 | c.pass && c.mutants_caught == c.mutants_run ? "PASS" : "FAIL", c.name, c.n, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst, c.chip_ns, c.call_ns, c.calls, c.unwritten, |
| 238 | c.kv_staged, c.kv_naive, c.blk_unique, c.blk_logical, c.mutants_caught, c.mutants_run, c.mutant_note); |
| 239 | if (out) fprintf(out, "%s{\"case\":\"%s\",\"n\":%zu,\"rc\":\"%s\",\"violations\":%zu,\"worst_scaled_err\":%g,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"call_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u," |
| 240 | "\"kv_bytes_staged\":%" PRIu64 ",\"kv_bytes_naive\":%" PRIu64 ",\"kv_blocks_unique\":%u,\"kv_blocks_logical\":%u,\"mutants_run\":%d,\"mutants_caught\":%d,\"mutant_note\":\"%s\"}", |
| 241 | g_ncases > 1 ? "," : "", c.name, c.n, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst, c.pass ? "true" : "false", c.chip_ns, c.call_ns, c.calls, c.unwritten, |
| 242 | c.kv_staged, c.kv_naive, c.blk_unique, c.blk_logical, c.mutants_run, c.mutants_caught, c.mutant_note); |
| 243 | } |
| 244 | |
| 245 | static const char *mutant_name(int m) { return omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m); } |
| 246 | |
| 247 | /* Runs the mutants listed in `ms` (terminated by 0) through `run` and counts the catches. */ |
| 248 | typedef int (*RunFn)(void *ctx, float *got, OmegaGpuAttnInfo *info); |
| 249 | static void run_mutants(Case *c, const int *ms, RunFn run, void *ctx, float *got, const float *want, size_t n, double rel, double abs_tol) { |
| 250 | size_t pos = strlen(c->mutant_note); /* appends: a case may call this more than once */ |
| 251 | for (int i = 0; ms[i]; i++) { |
| 252 | OmegaGpuAttnInfo info; double w; |
| 253 | omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)ms[i]); |
| 254 | int rc = run(ctx, got, &info); |
| 255 | size_t bad = compare(got, want, n, rel, abs_tol, &w); |
| 256 | int caught = rc != OMEGA_GPU_ATTN_OK || bad > 0; |
| 257 | omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); |
| 258 | c->mutants_run++; c->mutants_caught += caught; g_mutant_run[ms[i]]++; g_mutant_caught[ms[i]] += caught; |
| 259 | pos += (size_t)snprintf(c->mutant_note + pos, sizeof c->mutant_note - pos, "%s%s:%s(rc=%s,bad=%zu)", pos ? " " : "", mutant_name(ms[i]), caught ? "CAUGHT" : "MISSED", omega_gpu_attention_rc_name(rc), bad); |
| 260 | if (pos >= sizeof c->mutant_note) break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | typedef struct { const float *q, *kc, *vc; uint32_t seq, nqh, nkv; } GqaCtx; |
| 265 | static int run_gqa(void *p, float *got, OmegaGpuAttnInfo *info) { GqaCtx *c = p; return omega_gpu_gqa_attention_f32(c->q, c->kc, c->vc, c->seq, c->nqh, c->nkv, 64, got, info); } |
| 266 | typedef struct { const float *q; const uint8_t *pool; const OmegaGpuKvLayout *ly; const uint32_t *ids; uint32_t nids, ctx, layer, nqh, nkv; } PagedCtx; |
| 267 | static int run_paged(void *p, float *got, OmegaGpuAttnInfo *info) { PagedCtx *c = p; return omega_gpu_paged_attention_bf16(c->q, c->pool, c->ly, c->ids, c->nids, c->ctx, c->layer, c->nqh, c->nkv, 64, got, info); } |
| 268 | typedef struct { const float *q; const uint8_t *pool; const OmegaGpuKvLayout *ly; const int32_t *tables, *ctxs; uint32_t maxb, nseq, layer, nqh, nkv; } BatchCtx; |
| 269 | static int run_batch(void *p, float *got, OmegaGpuAttnInfo *info) { BatchCtx *c = p; return omega_gpu_paged_attention_batch_bf16(c->q, c->pool, c->ly, c->tables, c->ctxs, c->maxb, c->nseq, c->layer, c->nqh, c->nkv, 64, got, info); } |
| 270 | |
| 271 | static int chip(const char *out_path) { |
| 272 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 273 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 274 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_V1\",\"tolerances\":{\"rel\":2e-4,\"abs\":2e-5,\"large_scores_rel\":1e-3,\"large_scores_abs\":1e-4},\"cases\":["); |
| 275 | uint32_t seed = 0xfb1c0005u; |
| 276 | const uint32_t hd = 64; |
| 277 | const struct { uint32_t nqh, nkv; const char *model; } shapes[2] = { { 32, 4, "tinyllama" }, { 32, 8, "llama32_1b" } }; |
| 278 | /* 65, 129, 300 and 1000 end in a partial 64-token chunk after whole ones: the case that |
| 279 | * caught the diverged-warp shuffle on the chip (2026-10-04); 1, 17, 128, 256, 2048 did not. */ |
| 280 | const uint32_t ctxs[9] = { 1, 17, 256, 2048, 65, 128, 129, 300, 1000 }; |
| 281 | const int no_mutants[1] = { 0 }; |
| 282 | |
| 283 | /* 1. contiguous f32 KV (gqa_attention) */ |
| 284 | for (int sh = 0; sh < 2; sh++) for (int ci = 0; ci < 9; ci++) { |
| 285 | uint32_t nqh = shapes[sh].nqh, nkv = shapes[sh].nkv, seq = ctxs[ci]; |
| 286 | size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 287 | float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4), *again = malloc(n * 4); |
| 288 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 289 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 290 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 291 | char name[96]; snprintf(name, sizeof name, "gqa_f32_%s_%uq%ukv_ctx%u", shapes[sh].model, nqh, nkv, seq); |
| 292 | Case c = { .name = strdup(name), .n = n }; |
| 293 | OmegaGpuAttnInfo info; |
| 294 | GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; |
| 295 | c.rc = run_gqa(&gc, got, &info); |
| 296 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 297 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); |
| 298 | if (ci == 3) { |
| 299 | OmegaGpuAttnInfo i2; int rc2 = run_gqa(&gc, again, &i2); |
| 300 | CHECK(rc2 == OMEGA_GPU_ATTN_OK && i2.kernel_cache_hit && memcmp(got, again, n * 4) == 0, "%s repeat run (cache hit) is bit-identical", name); |
| 301 | /* negative control: the comparison must reject a wrong expectation (kv heads swapped) */ |
| 302 | float *wrong = malloc(n * 4); double w2; |
| 303 | for (uint32_t h = 0; h < nqh; h++) memcpy(wrong + (size_t)h * hd, want + (size_t)((h + nqh / nkv) % nqh) * hd, hd * 4); |
| 304 | size_t neg = compare(got, wrong, n, 2e-4, 2e-5, &w2); |
| 305 | CHECK(neg > n / 2, "%s negative control: wrong expectation flagged (%zu of %zu)", name, neg, n); |
| 306 | free(wrong); |
| 307 | static const int ms[5] = { OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; |
| 308 | run_mutants(&c, ms, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 309 | } else if (ci == 1 && sh == 1) { |
| 310 | static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; |
| 311 | run_mutants(&c, ms, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 312 | } else run_mutants(&c, no_mutants, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 313 | record(c, out); free(q); free(kc); free(vc); free(got); free(want); free(again); |
| 314 | } |
| 315 | /* 2. large scores (q scaled by 200): the softmax without the running max overflows */ |
| 316 | { |
| 317 | uint32_t nqh = 32, nkv = 4, seq = 256; size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 318 | float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 319 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -200.0f, 200.0f); /* scores ~ +-200 natural: EX2 without the max subtraction overflows f32 (2^128) */ |
| 320 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 321 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 322 | Case c = { .name = "gqa_f32_large_scores_32q4kv_ctx256", .n = n }; |
| 323 | OmegaGpuAttnInfo info; GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; |
| 324 | c.rc = run_gqa(&gc, got, &info); |
| 325 | c.bad = compare(got, want, n, 1e-3, 1e-4, &c.worst); |
| 326 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); |
| 327 | static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_NO_MAX, 0 }; |
| 328 | run_mutants(&c, ms, run_gqa, &gc, got, want, n, 1e-3, 1e-4); |
| 329 | record(c, out); free(q); free(kc); free(vc); free(got); free(want); |
| 330 | } |
| 331 | /* 2b. MHA (16q/16kv, ratio 1, ctx 300 = 4 full chunks + a partial one): the head-wiring |
| 332 | * mutants Q_ROW and OUT_ROW are observably different here (Q_ROW keeps kv head h, OUT_ROW |
| 333 | * lands on kv head h^1), unlike GQA where both swap the same pair of outputs. */ |
| 334 | { |
| 335 | uint32_t nqh = 16, nkv = 16, seq = 300; size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 336 | float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4), *got_q = malloc(n * 4); |
| 337 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 338 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 339 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 340 | Case c = { .name = "gqa_f32_mha_16q16kv_ctx300", .n = n }; |
| 341 | OmegaGpuAttnInfo info; GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; |
| 342 | c.rc = run_gqa(&gc, got, &info); |
| 343 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 344 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); |
| 345 | static const int ms_q[2] = { OMEGA_GPU_ATTN_MUTANT_Q_ROW, 0 }; |
| 346 | static const int ms_o[3] = { OMEGA_GPU_ATTN_MUTANT_OUT_ROW, OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; |
| 347 | run_mutants(&c, ms_q, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 348 | memcpy(got_q, got, n * 4); /* Q_ROW output */ |
| 349 | run_mutants(&c, ms_o, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 350 | /* `got` now holds the KV_HEAD output; rerun OUT_ROW alone to compare it with Q_ROW */ |
| 351 | omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_OUT_ROW); |
| 352 | OmegaGpuAttnInfo i3; int rc3 = run_gqa(&gc, got, &i3); |
| 353 | omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); |
| 354 | double wq; size_t differ = compare(got, got_q, n, 2e-4, 2e-5, &wq); |
| 355 | CHECK(rc3 == OMEGA_GPU_ATTN_OK && differ > n / 2, "mha: Q_ROW and OUT_ROW outputs are distinct wrong answers (%zu of %zu differ)", differ, n); |
| 356 | record(c, out); free(q); free(kc); free(vc); free(got); free(want); free(got_q); |
| 357 | } |
| 358 | /* 3. paged bf16 KV, block size 16, 2 layers (layer 1 under test), scattered block ids */ |
| 359 | for (int sh = 0; sh < 2; sh++) for (int ci = 0; ci < 9; ci++) { |
| 360 | uint32_t nqh = shapes[sh].nqh, nkv = shapes[sh].nkv, ctx = ctxs[ci], bs = 16, layers = 2, layer = 1; |
| 361 | uint32_t nblk = (ctx + bs - 1) / bs, pool_blocks = nblk + 7; |
| 362 | OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); |
| 363 | uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; |
| 364 | for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); |
| 365 | uint32_t *ids = malloc(nblk * 4); /* a scattered permutation of the pool blocks */ |
| 366 | for (uint32_t b = 0; b < nblk; b++) ids[b] = (b * 7 + 3) % pool_blocks; |
| 367 | size_t n = (size_t)nqh * hd; |
| 368 | float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 369 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 370 | oracle_paged(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, want); |
| 371 | char name[96]; snprintf(name, sizeof name, "paged_bf16_%s_%uq%ukv_bs%u_ctx%u", shapes[sh].model, nqh, nkv, bs, ctx); |
| 372 | Case c = { .name = strdup(name), .n = n }; |
| 373 | OmegaGpuAttnInfo info; PagedCtx pc = { q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv }; |
| 374 | c.rc = run_paged(&pc, got, &info); |
| 375 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 376 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); |
| 377 | if (ci == 3) { |
| 378 | /* NO_MAX cannot overflow on unit-scale scores (softmax is shift invariant); it is caught by the large-score gqa case */ |
| 379 | static const int ms3[6] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; |
| 380 | run_mutants(&c, ms3, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 381 | } else if (ci == 1) { |
| 382 | static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; |
| 383 | run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 384 | } else if (ci == 0) { |
| 385 | /* one token: its softmax weight is 1 whatever K says, so a K-only slot shift is invisible; KV_HEAD still shows */ |
| 386 | static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; |
| 387 | run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 388 | } else run_mutants(&c, no_mutants, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 389 | record(c, out); free(pool); free(ids); free(q); free(got); free(want); |
| 390 | } |
| 391 | /* 4. batch: three sequences (short, empty, truncated by its table), a negative table entry, table rows of 16 */ |
| 392 | { |
| 393 | uint32_t nqh = 32, nkv = 8, bs = 16, layers = 3, layer = 2, maxb = 16, nseq = 3, pool_blocks = 40; |
| 394 | OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); |
| 395 | uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; |
| 396 | for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); |
| 397 | int32_t *tables = malloc((size_t)nseq * maxb * 4); |
| 398 | for (size_t i = 0; i < (size_t)nseq * maxb; i++) tables[i] = -1; |
| 399 | /* seq 0: ctx 40 over blocks 5, 17 (-1 in the middle closes up), 29 */ |
| 400 | tables[0] = 5; tables[1] = -1; tables[2] = 17; tables[3] = 29; |
| 401 | /* seq 1: ctx 0 with a table */ |
| 402 | tables[maxb + 0] = 1; |
| 403 | /* seq 2: ctx 100 but only 4 blocks (64 tokens used) */ |
| 404 | tables[2 * maxb + 0] = 30; tables[2 * maxb + 1] = 2; tables[2 * maxb + 2] = 33; tables[2 * maxb + 3] = 11; |
| 405 | int32_t ctxs2[3] = { 40, 0, 100 }; |
| 406 | size_t n = (size_t)nseq * nqh * hd; |
| 407 | float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 408 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 409 | oracle_batch(q, pool, &ly, tables, ctxs2, maxb, nseq, layer, nqh, nkv, hd, want); |
| 410 | Case c = { .name = "paged_bf16_batch_3seq_32q8kv_ctx40_0_100", .n = n }; |
| 411 | OmegaGpuAttnInfo info; BatchCtx bc = { q, pool, &ly, tables, ctxs2, maxb, nseq, layer, nqh, nkv }; |
| 412 | c.rc = run_batch(&bc, got, &info); |
| 413 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 414 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); |
| 415 | int zeros = 1; for (size_t i = 0; i < (size_t)nqh * hd; i++) if (got[(size_t)nqh * hd + i] != 0.0f) zeros = 0; |
| 416 | CHECK(zeros, "batch: the empty sequence is all zeros"); |
| 417 | static const int ms[5] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; |
| 418 | run_mutants(&c, ms, run_batch, &bc, got, want, n, 2e-4, 2e-5); |
| 419 | /* all 7 referenced blocks are distinct here, so dedupe stages exactly the naive figure */ |
| 420 | CHECK(c.blk_logical == 7 && c.blk_unique == 7 && c.kv_staged == c.kv_naive && c.kv_staged == 7ull * ly.layer_stride_bytes, "batch: 7 distinct blocks staged once each (%u/%u, %" PRIu64 " bytes)", c.blk_unique, c.blk_logical, c.kv_staged); |
| 421 | record(c, out); free(pool); free(tables); free(q); free(got); free(want); |
| 422 | } |
| 423 | /* 5. branch-heavy batch (attention hardening cut): four sequences share the same 10 physical |
| 424 | * prefix blocks (160 tokens, scattered ids) and own private tails of 1..3 blocks ending in a |
| 425 | * partial 64-token chunk. The shared prefix must be staged once per launch, keyed by the |
| 426 | * physical block id. Two head shapes: 8q/2kv puts all four sequences in one launch |
| 427 | * (unique = 10 + 9 tails), 32q/8kv splits them two per launch (unique = 2*10 + 9). |
| 428 | * Negative control: the legacy per-sequence staging (test hook) copies the prefix four |
| 429 | * times and the byte-reduction check must flag it. */ |
| 430 | for (int sh = 0; sh < 2; sh++) { |
| 431 | uint32_t nqh = sh ? 32 : 8, nkv = sh ? 8 : 2, bs = 16, layers = 2, layer = 1, nseq = 4, prefix_blocks = 10, pool_blocks = 64; |
| 432 | uint32_t expect_unique = sh ? 2 * prefix_blocks + 9 : prefix_blocks + 9; |
| 433 | uint32_t maxb = prefix_blocks + 3; |
| 434 | OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); |
| 435 | uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; |
| 436 | for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); |
| 437 | int32_t *tables = malloc((size_t)nseq * maxb * 4); |
| 438 | for (size_t i = 0; i < (size_t)nseq * maxb; i++) tables[i] = -1; |
| 439 | const uint32_t tails[4] = { 1, 2, 3, 3 }; /* private blocks per sequence: 9 in all */ |
| 440 | const int32_t ctxs5[4] = { 165, 180, 193, 207 }; /* 160 + 5, 20, 33, 47: every last chunk partial */ |
| 441 | for (uint32_t s = 0; s < nseq; s++) { |
| 442 | for (uint32_t b = 0; b < prefix_blocks; b++) tables[s * maxb + b] = (int32_t)((b * 5 + 7) % pool_blocks); /* same scattered physical prefix */ |
| 443 | for (uint32_t b = 0; b < tails[s]; b++) tables[s * maxb + prefix_blocks + b] = (int32_t)(50 + s * 3 + b); /* private, distinct */ |
| 444 | } |
| 445 | size_t n = (size_t)nseq * nqh * hd; |
| 446 | float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *legacy = malloc(n * 4); |
| 447 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 448 | oracle_batch(q, pool, &ly, tables, ctxs5, maxb, nseq, layer, nqh, nkv, hd, want); |
| 449 | char name[96]; snprintf(name, sizeof name, "paged_bf16_shared_prefix_4seq_%uq%ukv_10pre", nqh, nkv); |
| 450 | Case c = { .name = strdup(name), .n = n }; |
| 451 | OmegaGpuAttnInfo info; BatchCtx bc = { q, pool, &ly, tables, ctxs5, maxb, nseq, layer, nqh, nkv }; |
| 452 | c.rc = run_batch(&bc, got, &info); |
| 453 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 454 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); |
| 455 | uint32_t logical = 4 * prefix_blocks + 9; |
| 456 | CHECK(c.blk_logical == logical && c.blk_unique == expect_unique, "%s: %u logical references, %u unique staged (expected %u/%u)", name, c.blk_logical, c.blk_unique, logical, expect_unique); |
| 457 | CHECK(c.kv_naive == (uint64_t)logical * ly.layer_stride_bytes && c.kv_staged == (uint64_t)expect_unique * ly.layer_stride_bytes && c.kv_staged < c.kv_naive, |
| 458 | "%s: staged %" PRIu64 " < naive %" PRIu64 " bytes (saved %" PRIu64 ")", name, c.kv_staged, c.kv_naive, c.kv_naive - c.kv_staged); |
| 459 | CHECK(info.kv_source == OMEGA_GPU_ATTN_KV_STAGED && info.q_bytes == n * 4 && info.out_bytes == n * 4 && info.tab_bytes > 0, "%s: q/out/table traffic accounted (q=%" PRIu64 " out=%" PRIu64 " tab=%" PRIu64 ")", name, info.q_bytes, info.out_bytes, info.tab_bytes); |
| 460 | /* negative control: legacy per-sequence staging duplicates the shared prefix; same answer, more bytes */ |
| 461 | omega_gpu_attention_test_set_dedupe(false); |
| 462 | OmegaGpuAttnInfo li; int lrc = run_batch(&bc, legacy, &li); |
| 463 | omega_gpu_attention_test_set_dedupe(true); |
| 464 | double lw; size_t lbad = compare(legacy, want, n, 2e-4, 2e-5, &lw); |
| 465 | CHECK(lrc == OMEGA_GPU_ATTN_OK && lbad == 0, "%s legacy staging: parity (rc=%s bad=%zu)", name, omega_gpu_attention_rc_name(lrc), lbad); |
| 466 | CHECK(li.kv_blocks_unique == logical && li.kv_bytes_staged == li.kv_bytes_naive && li.kv_bytes_staged > c.kv_staged, |
| 467 | "%s negative control: duplicated prefix staging is flagged (legacy staged %" PRIu64 " == naive %" PRIu64 ", dedupe %" PRIu64 ")", name, li.kv_bytes_staged, li.kv_bytes_naive, c.kv_staged); |
| 468 | CHECK(memcmp(got, legacy, n * 4) == 0, "%s: dedupe and legacy staging are bit-identical", name); |
| 469 | static const int ms5[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; |
| 470 | run_mutants(&c, ms5, run_batch, &bc, got, want, n, 2e-4, 2e-5); |
| 471 | record(c, out); free(pool); free(tables); free(q); free(got); free(want); free(legacy); |
| 472 | } |
| 473 | /* every mutant the API knows (1 .. COUNT-1) ran at least once and was caught every time; |
| 474 | * a mutant added to the enum without a battery entry fails here */ |
| 475 | for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) { |
| 476 | CHECK(g_mutant_run[m] > 0 && g_mutant_caught[m] == g_mutant_run[m], "mutant %s caught %d of %d runs", mutant_name(m), g_mutant_caught[m], g_mutant_run[m]); |
| 477 | printf("mutant %s: caught %d of %d\n", mutant_name(m), g_mutant_caught[m], g_mutant_run[m]); |
| 478 | } |
| 479 | CHECK(omega_gpu_session_open_count() == (g_sim_mode ? 0u : 1u), "device opened %s for the battery (opens=%u)", g_sim_mode ? "never (simulator)" : "once", omega_gpu_session_open_count()); |
| 480 | printf("device_opens=%u\n", omega_gpu_session_open_count()); |
| 481 | if (out) { |
| 482 | fprintf(out, "],\"mutant_count\":%d,\"mutants\":{", OMEGA_GPU_ATTN_MUTANT_COUNT - 1); |
| 483 | for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) fprintf(out, "%s\"%s\":{\"run\":%d,\"caught\":%d}", m > 1 ? "," : "", mutant_name(m), g_mutant_run[m], g_mutant_caught[m]); |
| 484 | fprintf(out, "},\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_session_open_count(), g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ATTENTION_PASS" : "OMEGA_GPU_ATTENTION_FAIL"); |
| 485 | fclose(out); |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | /* ------------------------------------------------------------ host simulator |
| 491 | * Executes the allocated IR program (physical registers, so the register allocator's |
| 492 | * loop handling is under test too) for every CTA of a launch: 64 threads, 1 KB shared, |
| 493 | * host pointers as addresses, SHFL and BAR as lockstep points (all threads of the CTA |
| 494 | * must sit at the same instruction). EX2 and RCP are exact here; the chip's are approximate. */ |
| 495 | /* path: hash of every branch decision so far. The IR has no reconvergence instruction |
| 496 | * (no BSSY/BSYNC/WARPSYNC), so once a warp splits it may stay split on the chip, and a |
| 497 | * SHFL reading an inactive lane is undefined (CUDA guide, warp shuffle functions). Every |
| 498 | * SHFL therefore requires one path across the warp, or the simulator stops with error 7. */ |
| 499 | typedef struct { uint32_t r[256]; int pc; int p0; int done; uint64_t path; } SimThr; |
| 500 | static uint32_t sim_cbank[256]; |
| 501 | static uint8_t sim_shared[1024]; |
| 502 | #define SIM_T 64 |
| 503 | static int g_sim_err; |
| 504 | static int g_trace, g_trace_launch; static uint32_t g_trace_cx; |
| 505 | static uint32_t rd(const SimThr *t, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : t->r[ph & 0xff]; } |
| 506 | static uint64_t rd64(const SimThr *t, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)t->r[ph] | ((uint64_t)t->r[ph + 1] << 32); } |
| 507 | static void wr(SimThr *t, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) t->r[ph & 0xff] = x; } |
| 508 | static void wr64(SimThr *t, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; t->r[ph] = (uint32_t)x; t->r[ph + 1] = (uint32_t)(x >> 32); } |
| 509 | static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 510 | static uint32_t f2u_(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 511 | /* run one thread until it reaches a SHFL or BAR (not executed) or EXIT; returns 1 if parked at a sync op */ |
| 512 | static int sim_step(SimThr *t, const BlackwellIRProgram *p, uint32_t tid, uint32_t cx, uint32_t cy) { |
| 513 | for (int guard = 0; guard < 50000000; guard++) { |
| 514 | if (t->done) return 0; |
| 515 | if (t->pc < 0 || (size_t)t->pc >= p->count) { g_sim_err = 1; t->done = 1; return 0; } |
| 516 | const BlackwellIRInsn *in = &p->insns[t->pc]; |
| 517 | uint32_t a = rd(t, in->src1_vreg, p), b = rd(t, in->src2_vreg, p), c = rd(t, in->src3_vreg, p); |
| 518 | switch (in->op) { |
| 519 | case BW_IR_SHFL_DOWN: case BW_IR_BAR_SYNC: return 1; |
| 520 | case BW_IR_EXIT: if (!in->predicate_p0 || t->p0) { t->done = 1; return 0; } break; |
| 521 | case BW_IR_BRA: { |
| 522 | int take = !in->predicate_p0 || (in->predicate_not ? !t->p0 : t->p0); |
| 523 | t->path = t->path * 1000003u + (((uint64_t)t->pc << 1) | (uint64_t)take); |
| 524 | if (take) { if ((int32_t)in->imm == 0) { g_sim_err = 2; t->done = 1; return 0; } t->pc += (int32_t)in->imm; continue; } |
| 525 | break; } |
| 526 | case BW_IR_MOV_IMM: wr(t, in->dst_vreg, in->imm, p); break; |
| 527 | case BW_IR_MOV_RZ: wr(t, in->dst_vreg, 0, p); break; |
| 528 | case BW_IR_S2R: wr(t, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; |
| 529 | case BW_IR_LDC: wr(t, in->dst_vreg, sim_cbank[in->imm / 4], p); break; |
| 530 | case BW_IR_LDC64: wr64(t, in->dst_vreg, (uint64_t)sim_cbank[in->imm / 4] | ((uint64_t)sim_cbank[in->imm / 4 + 1] << 32), p); break; |
| 531 | case BW_IR_LDCU64: break; /* memory descriptor: not modelled */ |
| 532 | case BW_IR_IMAD: wr(t, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; |
| 533 | case BW_IR_IMAD_WIDE: wr64(t, in->dst_vreg, (uint64_t)a * in->imm + rd64(t, in->src3_vreg, p), p); break; |
| 534 | case BW_IR_IADD3: wr(t, in->dst_vreg, a + b, p); break; |
| 535 | case BW_IR_SHF_R: wr(t, in->dst_vreg, a >> (in->imm & 31), p); break; |
| 536 | case BW_IR_LOP3_AND: wr(t, in->dst_vreg, a & in->imm, p); break; |
| 537 | case BW_IR_LOP3_XOR: wr(t, in->dst_vreg, a ^ b, p); break; |
| 538 | case BW_IR_ISETP_GE_U32: t->p0 = a >= b; break; |
| 539 | case BW_IR_LDG_E: { uint32_t x; memcpy(&x, (const void *)(uintptr_t)rd64(t, in->src1_vreg, p), 4); wr(t, in->dst_vreg, x, p); break; } |
| 540 | case BW_IR_LDG_E_U16: { uint16_t x; memcpy(&x, (const void *)(uintptr_t)rd64(t, in->src1_vreg, p), 2); wr(t, in->dst_vreg, x, p); break; } |
| 541 | case BW_IR_STG_E: memcpy((void *)(uintptr_t)rd64(t, in->src1_vreg, p), &b, 4); break; |
| 542 | case BW_IR_LDS32: { uint32_t x; if (a + 4 > sizeof sim_shared) { g_sim_err = 3; t->done = 1; return 0; } memcpy(&x, sim_shared + a, 4); wr(t, in->dst_vreg, x, p); break; } |
| 543 | case BW_IR_STS32: if (a + 4 > sizeof sim_shared) { g_sim_err = 3; t->done = 1; return 0; } memcpy(sim_shared + a, &b, 4); break; |
| 544 | case BW_IR_FADD: wr(t, in->dst_vreg, f2u_(u2f(a) + u2f(b)), p); break; |
| 545 | case BW_IR_FSUB: wr(t, in->dst_vreg, f2u_(u2f(a) - u2f(b)), p); break; |
| 546 | case BW_IR_FMUL: wr(t, in->dst_vreg, f2u_(u2f(a) * u2f(b)), p); break; |
| 547 | case BW_IR_FFMA: wr(t, in->dst_vreg, f2u_(fmaf(u2f(a), u2f(b), u2f(c))), p); break; |
| 548 | case BW_IR_FMNMX_MAX: wr(t, in->dst_vreg, f2u_(fmaxf(u2f(a), u2f(b))), p); break; |
| 549 | case BW_IR_MUFU_EX2: wr(t, in->dst_vreg, f2u_(exp2f(u2f(a))), p); break; |
| 550 | case BW_IR_MUFU_RCP: wr(t, in->dst_vreg, f2u_(1.0f / u2f(a)), p); break; |
| 551 | default: g_sim_err = 100 + (int)in->op; t->done = 1; return 0; |
| 552 | } |
| 553 | if (g_trace && tid == 0 && cx == g_trace_cx && cy == 0 && g_trace_launch == 1) { uint32_t dv = rd(t, in->dst_vreg, p); printf("T pc=%d op=%d dst=v%d(r%d)=%08x f=%g p0=%d a=%08x b=%08x c=%08x\n", t->pc, (int)in->op, in->dst_vreg, in->dst_vreg >= 0 ? p->regalloc.vreg_to_phys[in->dst_vreg] : -1, dv, u2f(dv), t->p0, a, b, c); } |
| 554 | t->pc++; |
| 555 | } |
| 556 | g_sim_err = 4; t->done = 1; return 0; |
| 557 | } |
| 558 | static int sim_launch(const void *progv, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info) { |
| 559 | const BlackwellIRProgram *p = progv; |
| 560 | memset(sim_cbank, 0, sizeof sim_cbank); |
| 561 | uint64_t qa = (uintptr_t)L->q, pa = (uintptr_t)L->pool, oa = (uintptr_t)L->out, ta = (uintptr_t)L->tab; |
| 562 | sim_cbank[0x380 / 4] = (uint32_t)qa; sim_cbank[0x380 / 4 + 1] = (uint32_t)(qa >> 32); |
| 563 | sim_cbank[0x388 / 4] = (uint32_t)pa; sim_cbank[0x388 / 4 + 1] = (uint32_t)(pa >> 32); |
| 564 | sim_cbank[0x390 / 4] = (uint32_t)oa; sim_cbank[0x390 / 4 + 1] = (uint32_t)(oa >> 32); |
| 565 | sim_cbank[0x398 / 4] = (uint32_t)ta; sim_cbank[0x398 / 4 + 1] = (uint32_t)(ta >> 32); |
| 566 | memcpy(&sim_cbank[0x3a0 / 4], L->params, sizeof L->params); |
| 567 | uint32_t *outw = L->out; for (size_t i = 0; i < L->out_bytes / 4; i++) outw[i] = 0xffbadbadu; |
| 568 | g_sim_err = 0; |
| 569 | static SimThr th[SIM_T]; |
| 570 | g_trace_launch++; if (getenv("ATTN_SIM_TRACE")) { g_trace = 1; g_trace_cx = (uint32_t)atoi(getenv("ATTN_SIM_TRACE")); } |
| 571 | for (uint32_t cy = 0; cy < L->grid_y && !g_sim_err; cy++) for (uint32_t cx = 0; cx < L->grid_x && !g_sim_err; cx++) { |
| 572 | memset(th, 0, sizeof th); memset(sim_shared, 0, sizeof sim_shared); |
| 573 | for (;;) { |
| 574 | int parked = 0, alive = 0; |
| 575 | for (int i = 0; i < SIM_T; i++) { if (th[i].done) continue; alive++; parked += sim_step(&th[i], p, (uint32_t)i, cx, cy); } |
| 576 | if (g_sim_err) break; |
| 577 | if (alive == 0) break; |
| 578 | if (parked == 0) continue; /* every live thread exited this round */ |
| 579 | if (parked != alive) { g_sim_err = 5; break; } /* a thread exited while others wait at a barrier */ |
| 580 | int pc = -1; |
| 581 | for (int i = 0; i < SIM_T; i++) if (!th[i].done) { if (pc < 0) pc = th[i].pc; else if (th[i].pc != pc) g_sim_err = 6; } |
| 582 | if (g_sim_err) break; |
| 583 | const BlackwellIRInsn *in = &p->insns[pc]; |
| 584 | if (in->op == BW_IR_SHFL_DOWN) { |
| 585 | for (int i = 0; i < SIM_T; i++) if (!th[i].done && !th[i & ~31].done && th[i].path != th[i & ~31].path) g_sim_err = 7; |
| 586 | if (g_sim_err) break; |
| 587 | uint32_t src[SIM_T]; |
| 588 | for (int i = 0; i < SIM_T; i++) src[i] = rd(&th[i], in->src1_vreg, p); |
| 589 | for (int i = 0; i < SIM_T; i++) { int lane = i & 31, from = lane + (int)(in->imm & 31); wr(&th[i], in->dst_vreg, from < 32 ? src[(i & ~31) + from] : src[i], p); } |
| 590 | } |
| 591 | for (int i = 0; i < SIM_T; i++) th[i].pc++; |
| 592 | } |
| 593 | } |
| 594 | uint32_t unwritten = 0; for (size_t i = 0; i < L->out_bytes / 4; i++) if (outw[i] == 0xffbadbadu) unwritten++; |
| 595 | if (info) { info->chip_calls++; info->unwritten_words += unwritten; info->ctas_last_launch = L->grid_x * L->grid_y; info->threads_per_cta = SIM_T; } |
| 596 | if (g_sim_err) { printf("simulator error %d\n", g_sim_err); return OMEGA_GPU_ATTN_CHIP_FAIL; } |
| 597 | return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /* ---------------------------------------------------- timing (cut 4b) */ |
| 602 | /* One query step of TinyLlama attention at context 2048 (32 q heads, 4 kv heads, |
| 603 | * f32 KV) 100 times through the persistent session, every call checked against the |
| 604 | * oracle; gate: median wall time per call under 3 ms, one device open per process. */ |
| 605 | static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } |
| 606 | static int cmp_d(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } |
| 607 | static int timing(const char *out_path) { |
| 608 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 609 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 610 | enum { N = 100 }; |
| 611 | const uint32_t hd = 64, nqh = 32, nkv = 4, seq = 2048; |
| 612 | const char *name = "gqa_f32_tinyllama_32q4kv_ctx2048"; |
| 613 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_TIMING_V1\",\"gate_median_ms\":3.0,\"calls\":%d,\"cases\":[", N); |
| 614 | uint32_t seed = 0xfb1c004bu; |
| 615 | size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 616 | float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 617 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 618 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 619 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 620 | double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; |
| 621 | OmegaGpuAttnInfo info; |
| 622 | /* warm-up: the first call opens the device and generates the kernel (not timed) */ |
| 623 | int rc0 = omega_gpu_gqa_attention_f32(q, kc, vc, seq, nqh, nkv, hd, got, &info); |
| 624 | double first_ms = info.call_ns / 1e6; |
| 625 | CHECK(rc0 == OMEGA_GPU_ATTN_OK, "%s first call rc=%s err=\"%s\"", name, omega_gpu_attention_rc_name(rc0), omega_gpu_attention_last_error()); |
| 626 | for (int i = 0; i < N; i++) { |
| 627 | double t0 = now_ms(); |
| 628 | int rc = omega_gpu_gqa_attention_f32(q, kc, vc, seq, nqh, nkv, hd, got, &info); |
| 629 | samples[i] = now_ms() - t0; |
| 630 | double w; size_t bad = compare(got, want, n, 2e-4, 2e-5, &w); |
| 631 | if (w > worst) worst = w; |
| 632 | bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; |
| 633 | if (rc == OMEGA_GPU_ATTN_OK && bad == 0) ok_calls++; |
| 634 | } |
| 635 | qsort(samples, N, sizeof samples[0], cmp_d); |
| 636 | double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; |
| 637 | uint32_t opens = omega_gpu_session_open_count(); |
| 638 | int pass = ok_calls == N && bad_total == 0 && median < 3.0 && opens == 1; |
| 639 | CHECK(pass, "timing %s ok_calls=%d/%d violations=%zu median_ms=%.3f opens=%u last_rc=%s err=\"%s\"", name, ok_calls, N, bad_total, median, opens, omega_gpu_attention_rc_name(rc_last), omega_gpu_attention_last_error()); |
| 640 | printf("%s timing %s first_call_ms=%.3f ok_calls=%d/%d violations=%zu worst_scaled_err=%g median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_us_mean=%.1f device_opens=%u\n", |
| 641 | pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); |
| 642 | if (out) fprintf(out, "{\"case\":\"%s\",\"first_call_ms\":%.3f,\"ok_calls\":%d,\"violations\":%zu,\"worst_scaled_err\":%g,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f,\"chip_us_mean\":%.1f,\"device_opens\":%u,\"pass\":%s}", |
| 643 | name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); |
| 644 | free(q); free(kc); free(vc); free(got); free(want); |
| 645 | if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ATTENTION_TIMING_PASS" : "OMEGA_GPU_ATTENTION_TIMING_FAIL"); fclose(out); } |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* ------------------------------------------------ head-scaling sweep (attention hardening cut) |
| 650 | * `--sweep [--out sweep.json]`: the production paged bf16 path at head_dim 64 over query-head |
| 651 | * counts 1, 2, 4, 8, 16, 24, 32, 64 and every divisor as the kv-head count, at context 256 |
| 652 | * (latency) and 2048 (bandwidth), block size 16, scattered block ids. Points whose GQA ratio |
| 653 | * is not a power of two are mathematically legal (the generic contract allows them) but |
| 654 | * outside the current kernel envelope (codegen selects the kv head with a shift): they are |
| 655 | * called anyway, must be refused with BAD_ARGS, and are recorded as |
| 656 | * "unsupported_by_kernel_envelope" (no chip time). Every supported point is checked against |
| 657 | * the host oracle on its first call, then warmed and timed over N calls (median / p90 of the |
| 658 | * whole call and of the chip kernel time). Gates: parity, no unwritten word, one device open, |
| 659 | * every call OK; the timings are evidence, not a threshold. Only quantities the stack measures |
| 660 | * are recorded; SM occupancy, bandwidth utilisation and cache hit rates are not observable |
| 661 | * through this path and are reported as unavailable. Under --sim the timings are host-simulator |
| 662 | * timings and the receipt says so. */ |
| 663 | static int sweep(const char *out_path) { |
| 664 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 665 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 666 | const int N = g_sim_mode ? 1 : 20; |
| 667 | const uint32_t hd = 64, bs = 16, layers = 1, layer = 0; |
| 668 | static const uint32_t qheads[8] = { 1, 2, 4, 8, 16, 24, 32, 64 }; |
| 669 | static const uint32_t ctxs[2] = { 256, 2048 }; |
| 670 | uint32_t seed = 0xfb1c05eeu; |
| 671 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_SWEEP_V1\",\"path\":\"paged_bf16\",\"head_dim\":%u,\"block_size\":%u,\"timed_calls\":%d,\"host_simulator\":%s," |
| 672 | "\"tolerances\":{\"rel\":2e-4,\"abs\":2e-5},\"hardware_counters\":\"unavailable (SM occupancy, bandwidth utilisation, cache hit rates are not observable through this path)\",\"points\":[", |
| 673 | hd, bs, N, g_sim_mode ? "true" : "false"); |
| 674 | int npoints = 0, supported = 0, unsupported = 0; |
| 675 | double *samples = malloc((size_t)N * sizeof *samples), *ksamples = malloc((size_t)N * sizeof *ksamples); |
| 676 | for (int qi = 0; qi < 8; qi++) for (uint32_t nkv = 1; nkv <= qheads[qi]; nkv++) { |
| 677 | uint32_t nqh = qheads[qi]; |
| 678 | if (nqh % nkv) continue; |
| 679 | uint32_t ratio = nqh / nkv; int pow2 = (ratio & (ratio - 1)) == 0; |
| 680 | for (int ci = 0; ci < 2; ci++) { |
| 681 | uint32_t ctx = ctxs[ci], nblk = (ctx + bs - 1) / bs, pool_blocks = nblk + 5; |
| 682 | OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); |
| 683 | uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; |
| 684 | for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); |
| 685 | uint32_t *ids = malloc(nblk * 4); |
| 686 | for (uint32_t b = 0; b < nblk; b++) ids[b] = (b * 7 + 3) % pool_blocks; |
| 687 | size_t n = (size_t)nqh * hd; |
| 688 | float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 689 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 690 | OmegaGpuAttnInfo info; memset(&info, 0, sizeof info); |
| 691 | int rc = omega_gpu_paged_attention_bf16(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, got, &info); |
| 692 | const char *klass; int pass; size_t bad = 0; double worst = 0; int ok_calls = 0; uint32_t unwritten = 0, cache_hits = 0; double med = 0, p90 = 0, kmed = 0, kp90 = 0; |
| 693 | if (!pow2) { |
| 694 | klass = "unsupported_by_kernel_envelope"; |
| 695 | pass = rc == OMEGA_GPU_ATTN_BAD_ARGS; unsupported++; |
| 696 | CHECK(pass, "sweep %uq/%ukv ctx %u: ratio %u (not a power of two) refused with BAD_ARGS (got %s)", nqh, nkv, ctx, ratio, omega_gpu_attention_rc_name(rc)); |
| 697 | } else { |
| 698 | klass = "supported"; supported++; |
| 699 | oracle_paged(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, want); |
| 700 | bad = compare(got, want, n, 2e-4, 2e-5, &worst); |
| 701 | unwritten = info.unwritten_words; |
| 702 | uint64_t kv_staged = info.kv_bytes_staged, kv_naive = info.kv_bytes_naive, qb = info.q_bytes, tb = info.tab_bytes, ob = info.out_bytes; |
| 703 | uint32_t launches = info.chip_calls, ctas = info.ctas_last_launch, blk_l = info.kv_blocks_logical, blk_u = info.kv_blocks_unique, kv_src = info.kv_source; |
| 704 | for (int i = 0; i < N; i++) { |
| 705 | OmegaGpuAttnInfo ti; |
| 706 | int trc = omega_gpu_paged_attention_bf16(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, got, &ti); |
| 707 | samples[i] = ti.call_ns / 1e6; ksamples[i] = ti.elapsed_ns / 1e6; |
| 708 | double w; size_t b2 = compare(got, want, n, 2e-4, 2e-5, &w); |
| 709 | if (w > worst) worst = w; |
| 710 | bad += b2; unwritten += ti.unwritten_words; cache_hits += ti.kernel_cache_hit; |
| 711 | if (trc == OMEGA_GPU_ATTN_OK && b2 == 0) ok_calls++; |
| 712 | } |
| 713 | qsort(samples, (size_t)N, sizeof samples[0], cmp_d); qsort(ksamples, (size_t)N, sizeof ksamples[0], cmp_d); |
| 714 | med = samples[N / 2]; p90 = samples[N * 9 / 10]; kmed = ksamples[N / 2]; kp90 = ksamples[N * 9 / 10]; |
| 715 | pass = rc == OMEGA_GPU_ATTN_OK && bad == 0 && ok_calls == N && unwritten == 0 && cache_hits == (uint32_t)N; |
| 716 | CHECK(pass, "sweep %uq/%ukv ctx %u: rc=%s violations=%zu ok_calls=%d/%d unwritten=%u cache_hits=%u", nqh, nkv, ctx, omega_gpu_attention_rc_name(rc), bad, ok_calls, N, unwritten, cache_hits); |
| 717 | printf("%s sweep %2uq/%2ukv ratio %2u ctx %4u: launches=%u ctas=%u call_ms med=%.3f p90=%.3f kernel_ms med=%.3f p90=%.3f kv_staged=%" PRIu64 " (naive %" PRIu64 ") blocks=%u/%u q=%" PRIu64 " tab=%" PRIu64 " out=%" PRIu64 " worst=%g\n", |
| 718 | pass ? "PASS" : "FAIL", nqh, nkv, ratio, ctx, launches, ctas, med, p90, kmed, kp90, kv_staged, kv_naive, blk_u, blk_l, qb, tb, ob, worst); |
| 719 | if (out) fprintf(out, "%s{\"q_heads\":%u,\"kv_heads\":%u,\"gqa_ratio\":%u,\"head_dim\":%u,\"context\":%u,\"q_width\":%zu,\"out_width\":%zu,\"class\":\"%s\",\"rc\":\"%s\",\"pass\":%s," |
| 720 | "\"violations\":%zu,\"worst_scaled_err\":%g,\"unwritten_words\":%u,\"launches_per_call\":%u,\"ctas_last_launch\":%u,\"threads_per_cta\":%u," |
| 721 | "\"call_ms_median\":%.4f,\"call_ms_p90\":%.4f,\"kernel_ms_median\":%.4f,\"kernel_ms_p90\":%.4f,\"kernel_cache_hits\":%u,\"first_call_cache_hit\":%s," |
| 722 | "\"kv_bytes_staged\":%" PRIu64 ",\"kv_bytes_naive\":%" PRIu64 ",\"kv_blocks_logical\":%u,\"kv_blocks_unique\":%u,\"q_bytes\":%" PRIu64 ",\"tab_bytes\":%" PRIu64 ",\"out_bytes\":%" PRIu64 ",\"kv_source\":\"%s\"}", |
| 723 | npoints ? "," : "", nqh, nkv, ratio, hd, ctx, n, n, klass, omega_gpu_attention_rc_name(rc), pass ? "true" : "false", bad, worst, unwritten, launches, ctas, info.threads_per_cta, |
| 724 | med, p90, kmed, kp90, cache_hits, info.kernel_cache_hit ? "true" : "false", kv_staged, kv_naive, blk_l, blk_u, qb, tb, ob, kv_src == OMEGA_GPU_ATTN_KV_STAGED ? "staged" : "resident"); |
| 725 | npoints++; |
| 726 | } |
| 727 | if (!pow2) { |
| 728 | printf("%s sweep %2uq/%2ukv ratio %2u ctx %4u: %s (rc=%s)\n", pass ? "PASS" : "FAIL", nqh, nkv, ratio, ctx, klass, omega_gpu_attention_rc_name(rc)); |
| 729 | if (out) fprintf(out, "%s{\"q_heads\":%u,\"kv_heads\":%u,\"gqa_ratio\":%u,\"head_dim\":%u,\"context\":%u,\"q_width\":%zu,\"out_width\":%zu,\"class\":\"%s\",\"rc\":\"%s\",\"pass\":%s}", |
| 730 | npoints ? "," : "", nqh, nkv, ratio, hd, ctx, n, n, klass, omega_gpu_attention_rc_name(rc), pass ? "true" : "false"); |
| 731 | npoints++; |
| 732 | } |
| 733 | free(pool); free(ids); free(q); free(got); free(want); |
| 734 | } |
| 735 | } |
| 736 | free(samples); free(ksamples); |
| 737 | uint32_t opens = omega_gpu_session_open_count(); |
| 738 | CHECK(opens == (g_sim_mode ? 0u : 1u), "sweep: device opened %s (opens=%u)", g_sim_mode ? "never (simulator)" : "once", opens); |
| 739 | printf("sweep: %d points (%d supported, %d unsupported by the kernel envelope), device_opens=%u\n", npoints, supported, unsupported, opens); |
| 740 | if (out) { fprintf(out, "],\"points_total\":%d,\"points_supported\":%d,\"points_unsupported\":%d,\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", npoints, supported, unsupported, opens, g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ATTENTION_SWEEP_PASS" : "OMEGA_GPU_ATTENTION_SWEEP_FAIL"); fclose(out); } |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | int main(int argc, char **argv) { |
| 745 | const char *out_path = NULL; int host = 0, sim = 0, timing_mode = 0, sweep_mode = 0; |
| 746 | for (int i = 1; i < argc; i++) { |
| 747 | if (strcmp(argv[i], "--host-only") == 0) host = 1; |
| 748 | else if (strcmp(argv[i], "--sim") == 0) sim = 1; |
| 749 | else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; |
| 750 | else if (strcmp(argv[i], "--sweep") == 0) sweep_mode = 1; |
| 751 | else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; |
| 752 | } |
| 753 | if (sim) { g_sim_mode = 1; omega_gpu_attention_test_set_simulator(sim_launch); } |
| 754 | if (host) host_only(); |
| 755 | else if (timing_mode) { if (timing(out_path) != 0) return 2; } |
| 756 | else if (sweep_mode) { if (sweep(out_path) != 0) return 2; } |
| 757 | else if (chip(out_path) != 0) return 2; |
| 758 | printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); |
| 759 | return g_failed ? 1 : 0; |
| 760 | } |