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-CUT4B-0396844 / 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 --out receipt.json chip gate: every shape against the host oracle, the |
| 8 | * mutants (deliberately wrong kernels) which the same |
| 9 | * check must catch, a negative control, timings |
| 10 | * |
| 11 | * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/backend.rs (main 8b5caed) in C: |
| 12 | * ReferenceCpuBackend::gqa_attention (f64 scores, max, exp, sum, 1/max(sum,1e-12), f64 weighted sum), |
| 13 | * TensorBackend::paged_attention (f64 online softmax token by token over the paged bf16 pool, tokens |
| 14 | * past the block table dropped, zeros for empty context) and paged_attention_batch (ctx <= 0 -> 0, |
| 15 | * table rows past the array -> empty, negative entries removed). Compiled with -ffp-contract=off. |
| 16 | * |
| 17 | * Tolerance (why): the chip accumulates the 64-term dot products, the per-chunk sums and the |
| 18 | * rescaled running sums in f32 (worst case about n * 2^-24 relative over n = 2048 terms, 1.2e-4), |
| 19 | * uses MUFU.EX2 (PTX ex2.approx: 2^-22.5 relative) for every weight and for the 32 chunk rescale |
| 20 | * factors, and MUFU.RCP for the final normalisation. Every output is a convex combination of V |
| 21 | * values of unit scale, so |error| <= 2e-4 * |want| + 2e-5 covers these with margin; the mutants |
| 22 | * move results by 1e-2 or more (wrong head, wrong token) or produce NaN (overflow without the max). |
| 23 | * The large-score case (scores up to ~300) carries the f32 rounding of the score itself into the |
| 24 | * exponent (3e-5 absolute per score), so it uses 1e-3 relative + 1e-4 absolute. |
| 25 | */ |
| 26 | #include "omega_gpu_attention_api.h" |
| 27 | #include "omega_gpu_session.h" |
| 28 | #include "omega_blackwell_codegen.h" |
| 29 | #include <inttypes.h> |
| 30 | #include <math.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | static int g_checks, g_failed; |
| 38 | static int g_sim_mode; /* --sim: the host simulator never opens the device */ |
| 39 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 40 | |
| 41 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 42 | static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } |
| 43 | 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); } |
| 44 | static float bf2f(uint16_t b) { uint32_t u = (uint32_t)b << 16; float f; memcpy(&f, &u, 4); return f; } |
| 45 | |
| 46 | /* ----------------------------------------------------------------- oracles */ |
| 47 | 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) { |
| 48 | if (seq_len == 0) { memset(out, 0, nqh * hd * 4); return; } |
| 49 | size_t ratio = nqh / nkv; double inv_sqrt_d = 1.0 / sqrt((double)hd); |
| 50 | double *scores = malloc(seq_len * sizeof *scores); |
| 51 | for (size_t h = 0; h < nqh; h++) { |
| 52 | size_t kvh = h / ratio; const float *qh = q + h * hd; |
| 53 | for (size_t t = 0; t < seq_len; t++) { |
| 54 | const float *kt = kc + (t * nkv + kvh) * hd; double dot = 0.0; |
| 55 | for (size_t d = 0; d < hd; d++) dot += (double)qh[d] * (double)kt[d]; |
| 56 | scores[t] = dot * inv_sqrt_d; |
| 57 | } |
| 58 | double mx = -INFINITY; for (size_t t = 0; t < seq_len; t++) if (scores[t] > mx) mx = scores[t]; |
| 59 | double sum = 0.0; for (size_t t = 0; t < seq_len; t++) { scores[t] = exp(scores[t] - mx); sum += scores[t]; } |
| 60 | double inv = 1.0 / (sum > 1e-12 ? sum : 1e-12); |
| 61 | for (size_t t = 0; t < seq_len; t++) scores[t] *= inv; |
| 62 | for (size_t d = 0; d < hd; d++) { |
| 63 | double s = 0.0; |
| 64 | for (size_t t = 0; t < seq_len; t++) s += scores[t] * (double)vc[(t * nkv + kvh) * hd + d]; |
| 65 | out[h * hd + d] = (float)s; |
| 66 | } |
| 67 | } |
| 68 | free(scores); |
| 69 | } |
| 70 | /* bf16 pool in the canonical layout; the oracle's read_kv_head: element_offset(block, layer, is_value, token) + kv_head*head_dim*2 */ |
| 71 | 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) { |
| 72 | 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; |
| 73 | return (const uint16_t *)(pool + off); |
| 74 | } |
| 75 | 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, |
| 76 | size_t nqh, size_t nkv, size_t hd, float *out) { |
| 77 | if (ctx == 0 || nids == 0) { memset(out, 0, nqh * hd * 4); return; } |
| 78 | size_t ratio = nqh / nkv; double inv_sqrt_d = 1.0 / sqrt((double)hd); size_t bs = ly->block_size; |
| 79 | double *acc = malloc(hd * sizeof *acc); |
| 80 | for (size_t h = 0; h < nqh; h++) { |
| 81 | size_t kvh = h / ratio; const float *qh = q + h * hd; |
| 82 | double m = -INFINITY, l = 0.0; for (size_t d = 0; d < hd; d++) acc[d] = 0.0; |
| 83 | for (size_t t = 0; t < ctx; t++) { |
| 84 | size_t bi = t / bs; if (bi >= nids) break; |
| 85 | const uint16_t *kh = kv_head(pool, ly, ids[bi], layer, 0, t % bs, kvh, hd); |
| 86 | const uint16_t *vh = kv_head(pool, ly, ids[bi], layer, 1, t % bs, kvh, hd); |
| 87 | double dot = 0.0; for (size_t d = 0; d < hd; d++) dot += (double)qh[d] * (double)bf2f(kh[d]); |
| 88 | double score = dot * inv_sqrt_d, mn = m > score ? m : score, alpha = exp(m - mn), beta = exp(score - mn); |
| 89 | l = l * alpha + beta; |
| 90 | for (size_t d = 0; d < hd; d++) acc[d] = acc[d] * alpha + beta * (double)bf2f(vh[d]); |
| 91 | m = mn; |
| 92 | } |
| 93 | double inv = l > 0.0 ? 1.0 / l : 0.0; |
| 94 | for (size_t d = 0; d < hd; d++) out[h * hd + d] = (float)(acc[d] * inv); |
| 95 | } |
| 96 | free(acc); |
| 97 | } |
| 98 | 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, |
| 99 | size_t layer, size_t nqh, size_t nkv, size_t hd, float *out) { |
| 100 | size_t stride = nqh * hd; |
| 101 | uint32_t *ids = malloc((maxb ? maxb : 1) * sizeof *ids); |
| 102 | for (size_t s = 0; s < nseq; s++) { |
| 103 | size_t ctx = ctxs[s] > 0 ? (size_t)ctxs[s] : 0, n = 0; |
| 104 | size_t total = nseq * maxb; /* the table array length as the trait sees it */ |
| 105 | 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]; |
| 106 | oracle_paged(q + s * stride, pool, ly, ids, n, ctx, layer, nqh, nkv, hd, out + s * stride); |
| 107 | } |
| 108 | free(ids); |
| 109 | } |
| 110 | |
| 111 | static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { |
| 112 | size_t bad = 0; *worst = 0.0; |
| 113 | for (size_t i = 0; i < n; i++) { |
| 114 | double d = fabs((double)got[i] - (double)want[i]); |
| 115 | double lim = rel * fabs((double)want[i]) + abs_tol; |
| 116 | double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); |
| 117 | if (scaled > *worst) *worst = scaled; |
| 118 | if (!(d <= lim)) bad++; /* NaN counts as bad */ |
| 119 | } |
| 120 | return bad; |
| 121 | } |
| 122 | |
| 123 | /* ------------------------------------------------------------- host only */ |
| 124 | static int nvdisasm_check(bool f32, const char *name, const char *const *must, int n_must) { |
| 125 | const char *nv = "/usr/local/cuda/bin/nvdisasm"; |
| 126 | if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } |
| 127 | OmegaBlackwellKernel k; memset(&k, 0, sizeof k); |
| 128 | if (omega_gpu_attention_codegen(f32, f32 ? 12 : 4, 3, &k) != OMEGA_GPU_ATTN_OK) return -1; |
| 129 | char bin[64], lst[64], cmd[256]; |
| 130 | snprintf(bin, sizeof bin, "/tmp/omega_attn_%s_%d.bin", name, (int)getpid()); |
| 131 | snprintf(lst, sizeof lst, "/tmp/omega_attn_%s_%d.sass", name, (int)getpid()); |
| 132 | FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; } |
| 133 | fwrite(k.code, 1, k.code_size, f); fclose(f); |
| 134 | snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); |
| 135 | int rc = system(cmd); |
| 136 | free(k.code); |
| 137 | FILE *l = fopen(lst, "r"); if (!l) return -1; |
| 138 | char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; |
| 139 | while (fgets(line, sizeof line, l)) { |
| 140 | if (!strstr(line, "/*0")) continue; |
| 141 | insns++; |
| 142 | if (strstr(line, "?") || strstr(line, "error")) bad++; |
| 143 | for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; |
| 144 | } |
| 145 | fclose(l); |
| 146 | int miss = 0; |
| 147 | for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } |
| 148 | printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); |
| 149 | unlink(bin); |
| 150 | return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; |
| 151 | } |
| 152 | |
| 153 | static OmegaGpuKvLayout layout_for(uint32_t num_blocks, uint32_t bs, uint32_t layers, uint32_t nkv, uint32_t hd) { |
| 154 | OmegaGpuKvLayout ly; |
| 155 | ly.head_stride_bytes = (uint64_t)hd * 2; ly.token_stride_bytes = ly.head_stride_bytes * nkv; |
| 156 | ly.kv_plane_stride_bytes = ly.token_stride_bytes * bs; ly.layer_stride_bytes = 2 * ly.kv_plane_stride_bytes; |
| 157 | ly.block_stride_bytes = ly.layer_stride_bytes * layers; ly.pool_bytes = ly.block_stride_bytes * num_blocks; |
| 158 | ly.num_blocks = num_blocks; ly.num_layers = layers; ly.block_size = bs; |
| 159 | return ly; |
| 160 | } |
| 161 | |
| 162 | static void host_only(void) { |
| 163 | float q[64 * 64], kv[64 * 64], out[64 * 64]; memset(q, 0, sizeof q); memset(kv, 0, sizeof kv); |
| 164 | CHECK(omega_gpu_gqa_attention_f32(NULL, kv, kv, 1, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "NULL q refused"); |
| 165 | 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)"); |
| 166 | CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 6, 2, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "gqa ratio 3 refused"); |
| 167 | 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"); |
| 168 | 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"); |
| 169 | for (int i = 0; i < 64; i++) out[i] = 1.0f; |
| 170 | OmegaGpuAttnInfo info; |
| 171 | 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"); |
| 172 | OmegaGpuKvLayout ly = layout_for(8, 16, 2, 4, 64); |
| 173 | uint8_t *pool = calloc(ly.pool_bytes, 1); |
| 174 | uint32_t ids[2] = { 1, 9 }; |
| 175 | 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"); |
| 176 | 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"); |
| 177 | OmegaGpuKvLayout bad = ly; bad.block_size = 12; |
| 178 | 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"); |
| 179 | bad = ly; bad.head_stride_bytes = 256; |
| 180 | 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"); |
| 181 | free(pool); |
| 182 | CHECK(strcmp(omega_gpu_attention_rc_name(OMEGA_GPU_ATTN_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); |
| 183 | |
| 184 | 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" }; |
| 185 | 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" }; |
| 186 | for (int f32 = 0; f32 < 2; f32++) { |
| 187 | const char *name = f32 ? "gqa_f32" : "paged_bf16"; |
| 188 | OmegaBlackwellKernel k; memset(&k, 0, sizeof k); |
| 189 | int rc = omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k); |
| 190 | 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); |
| 191 | printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", name, k.insn_count, k.gpr_count, k.uniform_gpr_count); |
| 192 | uint8_t digest[32]; memcpy(digest, k.code_digest, 32); |
| 193 | free(k.code); |
| 194 | for (int m = 1; m <= 4; m++) { |
| 195 | omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)m); |
| 196 | memset(&k, 0, sizeof k); |
| 197 | CHECK(omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k) == OMEGA_GPU_ATTN_OK && memcmp(digest, k.code_digest, 32) != 0, "%s mutant %d kernel differs", name, m); |
| 198 | free(k.code); |
| 199 | omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); |
| 200 | } |
| 201 | /* a different shape key is a different kernel */ |
| 202 | memset(&k, 0, sizeof k); |
| 203 | CHECK(omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 2, &k) == OMEGA_GPU_ATTN_OK && memcmp(digest, k.code_digest, 32) != 0, "%s log2gqa 2 kernel differs from log2gqa 3", name); |
| 204 | free(k.code); |
| 205 | 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); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* ------------------------------------------------------------- chip gate */ |
| 210 | 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; |
| 211 | int mutants_run, mutants_caught; char mutant_note[160]; } Case; |
| 212 | static Case g_cases[32]; static int g_ncases; |
| 213 | static int g_mutant_caught[5], g_mutant_run[5]; |
| 214 | |
| 215 | static void record(Case c, FILE *out) { |
| 216 | g_cases[g_ncases++] = c; |
| 217 | CHECK(c.pass, "%s rc=%s violations=%zu worst=%g", c.name, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst); |
| 218 | 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); |
| 219 | printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " call_ns=%" PRIu64 " calls=%u unwritten=%u | mutants %d/%d %s\n", |
| 220 | 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, |
| 221 | c.mutants_caught, c.mutants_run, c.mutant_note); |
| 222 | 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,\"mutants_run\":%d,\"mutants_caught\":%d,\"mutant_note\":\"%s\"}", |
| 223 | 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, |
| 224 | c.mutants_run, c.mutants_caught, c.mutant_note); |
| 225 | } |
| 226 | |
| 227 | static const char *mutant_name(int m) { static const char *n[5] = { "none", "NO_MAX", "KV_HEAD", "SLOT", "NO_RESCALE" }; return n[m]; } |
| 228 | |
| 229 | /* Runs the mutants listed in `ms` (terminated by 0) through `run` and counts the catches. */ |
| 230 | typedef int (*RunFn)(void *ctx, float *got, OmegaGpuAttnInfo *info); |
| 231 | 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) { |
| 232 | size_t pos = 0; |
| 233 | for (int i = 0; ms[i]; i++) { |
| 234 | OmegaGpuAttnInfo info; double w; |
| 235 | omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)ms[i]); |
| 236 | int rc = run(ctx, got, &info); |
| 237 | size_t bad = compare(got, want, n, rel, abs_tol, &w); |
| 238 | int caught = rc != OMEGA_GPU_ATTN_OK || bad > 0; |
| 239 | omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); |
| 240 | c->mutants_run++; c->mutants_caught += caught; g_mutant_run[ms[i]]++; g_mutant_caught[ms[i]] += caught; |
| 241 | 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); |
| 242 | if (pos >= sizeof c->mutant_note) break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | typedef struct { const float *q, *kc, *vc; uint32_t seq, nqh, nkv; } GqaCtx; |
| 247 | 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); } |
| 248 | typedef struct { const float *q; const uint8_t *pool; const OmegaGpuKvLayout *ly; const uint32_t *ids; uint32_t nids, ctx, layer, nqh, nkv; } PagedCtx; |
| 249 | 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); } |
| 250 | 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; |
| 251 | 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); } |
| 252 | |
| 253 | static int chip(const char *out_path) { |
| 254 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 255 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 256 | 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\":["); |
| 257 | uint32_t seed = 0xfb1c0005u; |
| 258 | const uint32_t hd = 64; |
| 259 | const struct { uint32_t nqh, nkv; const char *model; } shapes[2] = { { 32, 4, "tinyllama" }, { 32, 8, "llama32_1b" } }; |
| 260 | const uint32_t ctxs[4] = { 1, 17, 256, 2048 }; |
| 261 | const int no_mutants[1] = { 0 }; |
| 262 | |
| 263 | /* 1. contiguous f32 KV (gqa_attention) */ |
| 264 | for (int sh = 0; sh < 2; sh++) for (int ci = 0; ci < 4; ci++) { |
| 265 | uint32_t nqh = shapes[sh].nqh, nkv = shapes[sh].nkv, seq = ctxs[ci]; |
| 266 | size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 267 | 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); |
| 268 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 269 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 270 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 271 | char name[96]; snprintf(name, sizeof name, "gqa_f32_%s_%uq%ukv_ctx%u", shapes[sh].model, nqh, nkv, seq); |
| 272 | Case c = { .name = strdup(name), .n = n }; |
| 273 | OmegaGpuAttnInfo info; |
| 274 | GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; |
| 275 | c.rc = run_gqa(&gc, got, &info); |
| 276 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 277 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 278 | if (ci == 3) { |
| 279 | OmegaGpuAttnInfo i2; int rc2 = run_gqa(&gc, again, &i2); |
| 280 | 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); |
| 281 | /* negative control: the comparison must reject a wrong expectation (kv heads swapped) */ |
| 282 | float *wrong = malloc(n * 4); double w2; |
| 283 | for (uint32_t h = 0; h < nqh; h++) memcpy(wrong + (size_t)h * hd, want + (size_t)((h + nqh / nkv) % nqh) * hd, hd * 4); |
| 284 | size_t neg = compare(got, wrong, n, 2e-4, 2e-5, &w2); |
| 285 | CHECK(neg > n / 2, "%s negative control: wrong expectation flagged (%zu of %zu)", name, neg, n); |
| 286 | free(wrong); |
| 287 | static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, 0 }; |
| 288 | run_mutants(&c, ms, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 289 | } else if (ci == 1 && sh == 1) { |
| 290 | static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; |
| 291 | run_mutants(&c, ms, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 292 | } else run_mutants(&c, no_mutants, run_gqa, &gc, got, want, n, 2e-4, 2e-5); |
| 293 | record(c, out); free(q); free(kc); free(vc); free(got); free(want); free(again); |
| 294 | } |
| 295 | /* 2. large scores (q scaled by 200): the softmax without the running max overflows */ |
| 296 | { |
| 297 | uint32_t nqh = 32, nkv = 4, seq = 256; size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 298 | float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 299 | 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) */ |
| 300 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 301 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 302 | Case c = { .name = "gqa_f32_large_scores_32q4kv_ctx256", .n = n }; |
| 303 | OmegaGpuAttnInfo info; GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; |
| 304 | c.rc = run_gqa(&gc, got, &info); |
| 305 | c.bad = compare(got, want, n, 1e-3, 1e-4, &c.worst); |
| 306 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 307 | static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_NO_MAX, 0 }; |
| 308 | run_mutants(&c, ms, run_gqa, &gc, got, want, n, 1e-3, 1e-4); |
| 309 | record(c, out); free(q); free(kc); free(vc); free(got); free(want); |
| 310 | } |
| 311 | /* 3. paged bf16 KV, block size 16, 2 layers (layer 1 under test), scattered block ids */ |
| 312 | for (int sh = 0; sh < 2; sh++) for (int ci = 0; ci < 4; ci++) { |
| 313 | uint32_t nqh = shapes[sh].nqh, nkv = shapes[sh].nkv, ctx = ctxs[ci], bs = 16, layers = 2, layer = 1; |
| 314 | uint32_t nblk = (ctx + bs - 1) / bs, pool_blocks = nblk + 7; |
| 315 | OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); |
| 316 | uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; |
| 317 | for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); |
| 318 | uint32_t *ids = malloc(nblk * 4); /* a scattered permutation of the pool blocks */ |
| 319 | for (uint32_t b = 0; b < nblk; b++) ids[b] = (b * 7 + 3) % pool_blocks; |
| 320 | size_t n = (size_t)nqh * hd; |
| 321 | float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 322 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 323 | oracle_paged(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, want); |
| 324 | char name[96]; snprintf(name, sizeof name, "paged_bf16_%s_%uq%ukv_bs%u_ctx%u", shapes[sh].model, nqh, nkv, bs, ctx); |
| 325 | Case c = { .name = strdup(name), .n = n }; |
| 326 | OmegaGpuAttnInfo info; PagedCtx pc = { q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv }; |
| 327 | c.rc = run_paged(&pc, got, &info); |
| 328 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 329 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 330 | if (ci == 3) { |
| 331 | /* NO_MAX cannot overflow on unit-scale scores (softmax is shift invariant); it is caught by the large-score gqa case */ |
| 332 | static const int ms3[4] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, 0 }; |
| 333 | run_mutants(&c, ms3, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 334 | } else if (ci == 1) { |
| 335 | static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; |
| 336 | run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 337 | } else if (ci == 0) { |
| 338 | /* one token: its softmax weight is 1 whatever K says, so a K-only slot shift is invisible; KV_HEAD still shows */ |
| 339 | static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; |
| 340 | run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 341 | } else run_mutants(&c, no_mutants, run_paged, &pc, got, want, n, 2e-4, 2e-5); |
| 342 | record(c, out); free(pool); free(ids); free(q); free(got); free(want); |
| 343 | } |
| 344 | /* 4. batch: three sequences (short, empty, truncated by its table), a negative table entry, table rows of 16 */ |
| 345 | { |
| 346 | uint32_t nqh = 32, nkv = 8, bs = 16, layers = 3, layer = 2, maxb = 16, nseq = 3, pool_blocks = 40; |
| 347 | OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); |
| 348 | uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; |
| 349 | for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); |
| 350 | int32_t *tables = malloc((size_t)nseq * maxb * 4); |
| 351 | for (size_t i = 0; i < (size_t)nseq * maxb; i++) tables[i] = -1; |
| 352 | /* seq 0: ctx 40 over blocks 5, 17 (-1 in the middle closes up), 29 */ |
| 353 | tables[0] = 5; tables[1] = -1; tables[2] = 17; tables[3] = 29; |
| 354 | /* seq 1: ctx 0 with a table */ |
| 355 | tables[maxb + 0] = 1; |
| 356 | /* seq 2: ctx 100 but only 4 blocks (64 tokens used) */ |
| 357 | tables[2 * maxb + 0] = 30; tables[2 * maxb + 1] = 2; tables[2 * maxb + 2] = 33; tables[2 * maxb + 3] = 11; |
| 358 | int32_t ctxs2[3] = { 40, 0, 100 }; |
| 359 | size_t n = (size_t)nseq * nqh * hd; |
| 360 | float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 361 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 362 | oracle_batch(q, pool, &ly, tables, ctxs2, maxb, nseq, layer, nqh, nkv, hd, want); |
| 363 | Case c = { .name = "paged_bf16_batch_3seq_32q8kv_ctx40_0_100", .n = n }; |
| 364 | OmegaGpuAttnInfo info; BatchCtx bc = { q, pool, &ly, tables, ctxs2, maxb, nseq, layer, nqh, nkv }; |
| 365 | c.rc = run_batch(&bc, got, &info); |
| 366 | c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); |
| 367 | c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 368 | 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; |
| 369 | CHECK(zeros, "batch: the empty sequence is all zeros"); |
| 370 | static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; |
| 371 | run_mutants(&c, ms, run_batch, &bc, got, want, n, 2e-4, 2e-5); |
| 372 | record(c, out); free(pool); free(tables); free(q); free(got); free(want); |
| 373 | } |
| 374 | for (int m = 1; m <= 4; m++) { |
| 375 | 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]); |
| 376 | printf("mutant %s: caught %d of %d\n", mutant_name(m), g_mutant_caught[m], g_mutant_run[m]); |
| 377 | } |
| 378 | 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()); |
| 379 | printf("device_opens=%u\n", omega_gpu_session_open_count()); |
| 380 | if (out) { |
| 381 | fprintf(out, "],\"mutants\":{"); |
| 382 | for (int m = 1; m <= 4; m++) fprintf(out, "%s\"%s\":{\"run\":%d,\"caught\":%d}", m > 1 ? "," : "", mutant_name(m), g_mutant_run[m], g_mutant_caught[m]); |
| 383 | 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"); |
| 384 | fclose(out); |
| 385 | } |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* ------------------------------------------------------------ host simulator |
| 390 | * Executes the allocated IR program (physical registers, so the register allocator's |
| 391 | * loop handling is under test too) for every CTA of a launch: 64 threads, 1 KB shared, |
| 392 | * host pointers as addresses, SHFL and BAR as lockstep points (all threads of the CTA |
| 393 | * must sit at the same instruction). EX2 and RCP are exact here; the chip's are approximate. */ |
| 394 | typedef struct { uint32_t r[256]; int pc; int p0; int done; } SimThr; |
| 395 | static uint32_t sim_cbank[256]; |
| 396 | static uint8_t sim_shared[1024]; |
| 397 | #define SIM_T 64 |
| 398 | static int g_sim_err; |
| 399 | static int g_trace, g_trace_launch; static uint32_t g_trace_cx; |
| 400 | 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]; } |
| 401 | 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); } |
| 402 | 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; } |
| 403 | 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); } |
| 404 | static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 405 | static uint32_t f2u_(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 406 | /* run one thread until it reaches a SHFL or BAR (not executed) or EXIT; returns 1 if parked at a sync op */ |
| 407 | static int sim_step(SimThr *t, const BlackwellIRProgram *p, uint32_t tid, uint32_t cx, uint32_t cy) { |
| 408 | for (int guard = 0; guard < 50000000; guard++) { |
| 409 | if (t->done) return 0; |
| 410 | if (t->pc < 0 || (size_t)t->pc >= p->count) { g_sim_err = 1; t->done = 1; return 0; } |
| 411 | const BlackwellIRInsn *in = &p->insns[t->pc]; |
| 412 | uint32_t a = rd(t, in->src1_vreg, p), b = rd(t, in->src2_vreg, p), c = rd(t, in->src3_vreg, p); |
| 413 | switch (in->op) { |
| 414 | case BW_IR_SHFL_DOWN: case BW_IR_BAR_SYNC: return 1; |
| 415 | case BW_IR_EXIT: if (!in->predicate_p0 || t->p0) { t->done = 1; return 0; } break; |
| 416 | case BW_IR_BRA: { |
| 417 | int take = !in->predicate_p0 || (in->predicate_not ? !t->p0 : t->p0); |
| 418 | if (take) { if ((int32_t)in->imm == 0) { g_sim_err = 2; t->done = 1; return 0; } t->pc += (int32_t)in->imm; continue; } |
| 419 | break; } |
| 420 | case BW_IR_MOV_IMM: wr(t, in->dst_vreg, in->imm, p); break; |
| 421 | case BW_IR_MOV_RZ: wr(t, in->dst_vreg, 0, p); break; |
| 422 | 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; |
| 423 | case BW_IR_LDC: wr(t, in->dst_vreg, sim_cbank[in->imm / 4], p); break; |
| 424 | 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; |
| 425 | case BW_IR_LDCU64: break; /* memory descriptor: not modelled */ |
| 426 | case BW_IR_IMAD: wr(t, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; |
| 427 | case BW_IR_IMAD_WIDE: wr64(t, in->dst_vreg, (uint64_t)a * in->imm + rd64(t, in->src3_vreg, p), p); break; |
| 428 | case BW_IR_IADD3: wr(t, in->dst_vreg, a + b, p); break; |
| 429 | case BW_IR_SHF_R: wr(t, in->dst_vreg, a >> (in->imm & 31), p); break; |
| 430 | case BW_IR_LOP3_AND: wr(t, in->dst_vreg, a & in->imm, p); break; |
| 431 | case BW_IR_LOP3_XOR: wr(t, in->dst_vreg, a ^ b, p); break; |
| 432 | case BW_IR_ISETP_GE_U32: t->p0 = a >= b; break; |
| 433 | 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; } |
| 434 | 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; } |
| 435 | case BW_IR_STG_E: memcpy((void *)(uintptr_t)rd64(t, in->src1_vreg, p), &b, 4); break; |
| 436 | 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; } |
| 437 | 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; |
| 438 | case BW_IR_FADD: wr(t, in->dst_vreg, f2u_(u2f(a) + u2f(b)), p); break; |
| 439 | case BW_IR_FSUB: wr(t, in->dst_vreg, f2u_(u2f(a) - u2f(b)), p); break; |
| 440 | case BW_IR_FMUL: wr(t, in->dst_vreg, f2u_(u2f(a) * u2f(b)), p); break; |
| 441 | case BW_IR_FFMA: wr(t, in->dst_vreg, f2u_(fmaf(u2f(a), u2f(b), u2f(c))), p); break; |
| 442 | case BW_IR_FMNMX_MAX: wr(t, in->dst_vreg, f2u_(fmaxf(u2f(a), u2f(b))), p); break; |
| 443 | case BW_IR_MUFU_EX2: wr(t, in->dst_vreg, f2u_(exp2f(u2f(a))), p); break; |
| 444 | case BW_IR_MUFU_RCP: wr(t, in->dst_vreg, f2u_(1.0f / u2f(a)), p); break; |
| 445 | default: g_sim_err = 100 + (int)in->op; t->done = 1; return 0; |
| 446 | } |
| 447 | 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); } |
| 448 | t->pc++; |
| 449 | } |
| 450 | g_sim_err = 4; t->done = 1; return 0; |
| 451 | } |
| 452 | static int sim_launch(const void *progv, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info) { |
| 453 | const BlackwellIRProgram *p = progv; |
| 454 | memset(sim_cbank, 0, sizeof sim_cbank); |
| 455 | uint64_t qa = (uintptr_t)L->q, pa = (uintptr_t)L->pool, oa = (uintptr_t)L->out, ta = (uintptr_t)L->tab; |
| 456 | sim_cbank[0x380 / 4] = (uint32_t)qa; sim_cbank[0x380 / 4 + 1] = (uint32_t)(qa >> 32); |
| 457 | sim_cbank[0x388 / 4] = (uint32_t)pa; sim_cbank[0x388 / 4 + 1] = (uint32_t)(pa >> 32); |
| 458 | sim_cbank[0x390 / 4] = (uint32_t)oa; sim_cbank[0x390 / 4 + 1] = (uint32_t)(oa >> 32); |
| 459 | sim_cbank[0x398 / 4] = (uint32_t)ta; sim_cbank[0x398 / 4 + 1] = (uint32_t)(ta >> 32); |
| 460 | memcpy(&sim_cbank[0x3a0 / 4], L->params, sizeof L->params); |
| 461 | uint32_t *outw = L->out; for (size_t i = 0; i < L->out_bytes / 4; i++) outw[i] = 0xffbadbadu; |
| 462 | g_sim_err = 0; |
| 463 | static SimThr th[SIM_T]; |
| 464 | g_trace_launch++; if (getenv("ATTN_SIM_TRACE")) { g_trace = 1; g_trace_cx = (uint32_t)atoi(getenv("ATTN_SIM_TRACE")); } |
| 465 | 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++) { |
| 466 | memset(th, 0, sizeof th); memset(sim_shared, 0, sizeof sim_shared); |
| 467 | for (;;) { |
| 468 | int parked = 0, alive = 0; |
| 469 | 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); } |
| 470 | if (g_sim_err) break; |
| 471 | if (alive == 0) break; |
| 472 | if (parked == 0) continue; /* every live thread exited this round */ |
| 473 | if (parked != alive) { g_sim_err = 5; break; } /* a thread exited while others wait at a barrier */ |
| 474 | int pc = -1; |
| 475 | 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; } |
| 476 | if (g_sim_err) break; |
| 477 | const BlackwellIRInsn *in = &p->insns[pc]; |
| 478 | if (in->op == BW_IR_SHFL_DOWN) { |
| 479 | uint32_t src[SIM_T]; |
| 480 | for (int i = 0; i < SIM_T; i++) src[i] = rd(&th[i], in->src1_vreg, p); |
| 481 | 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); } |
| 482 | } |
| 483 | for (int i = 0; i < SIM_T; i++) th[i].pc++; |
| 484 | } |
| 485 | } |
| 486 | uint32_t unwritten = 0; for (size_t i = 0; i < L->out_bytes / 4; i++) if (outw[i] == 0xffbadbadu) unwritten++; |
| 487 | 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; } |
| 488 | if (g_sim_err) { printf("simulator error %d\n", g_sim_err); return OMEGA_GPU_ATTN_CHIP_FAIL; } |
| 489 | return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /* ---------------------------------------------------- timing (cut 4b) */ |
| 494 | /* One query step of TinyLlama attention at context 2048 (32 q heads, 4 kv heads, |
| 495 | * f32 KV) 100 times through the persistent session, every call checked against the |
| 496 | * oracle; gate: median wall time per call under 3 ms, one device open per process. */ |
| 497 | static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } |
| 498 | 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; } |
| 499 | static int timing(const char *out_path) { |
| 500 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 501 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 502 | enum { N = 100 }; |
| 503 | const uint32_t hd = 64, nqh = 32, nkv = 4, seq = 2048; |
| 504 | const char *name = "gqa_f32_tinyllama_32q4kv_ctx2048"; |
| 505 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_TIMING_V1\",\"gate_median_ms\":3.0,\"calls\":%d,\"cases\":[", N); |
| 506 | uint32_t seed = 0xfb1c004bu; |
| 507 | size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; |
| 508 | float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 509 | for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); |
| 510 | for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } |
| 511 | oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); |
| 512 | double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; |
| 513 | OmegaGpuAttnInfo info; |
| 514 | /* warm-up: the first call opens the device and generates the kernel (not timed) */ |
| 515 | int rc0 = omega_gpu_gqa_attention_f32(q, kc, vc, seq, nqh, nkv, hd, got, &info); |
| 516 | double first_ms = info.call_ns / 1e6; |
| 517 | 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()); |
| 518 | for (int i = 0; i < N; i++) { |
| 519 | double t0 = now_ms(); |
| 520 | int rc = omega_gpu_gqa_attention_f32(q, kc, vc, seq, nqh, nkv, hd, got, &info); |
| 521 | samples[i] = now_ms() - t0; |
| 522 | double w; size_t bad = compare(got, want, n, 2e-4, 2e-5, &w); |
| 523 | if (w > worst) worst = w; |
| 524 | bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; |
| 525 | if (rc == OMEGA_GPU_ATTN_OK && bad == 0) ok_calls++; |
| 526 | } |
| 527 | qsort(samples, N, sizeof samples[0], cmp_d); |
| 528 | double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; |
| 529 | uint32_t opens = omega_gpu_session_open_count(); |
| 530 | int pass = ok_calls == N && bad_total == 0 && median < 3.0 && opens == 1; |
| 531 | 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()); |
| 532 | 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", |
| 533 | pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); |
| 534 | 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}", |
| 535 | name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); |
| 536 | free(q); free(kc); free(vc); free(got); free(want); |
| 537 | 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); } |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | int main(int argc, char **argv) { |
| 542 | const char *out_path = NULL; int host = 0, sim = 0, timing_mode = 0; |
| 543 | for (int i = 1; i < argc; i++) { |
| 544 | if (strcmp(argv[i], "--host-only") == 0) host = 1; |
| 545 | else if (strcmp(argv[i], "--sim") == 0) sim = 1; |
| 546 | else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; |
| 547 | else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; |
| 548 | } |
| 549 | if (sim) { g_sim_mode = 1; omega_gpu_attention_test_set_simulator(sim_launch); } |
| 550 | if (host) host_only(); |
| 551 | else if (timing_mode) { if (timing(out_path) != 0) return 2; } |
| 552 | else if (chip(out_path) != 0) return 2; |
| 553 | printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); |
| 554 | return g_failed ? 1 : 0; |
| 555 | } |