Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/gpu_decode_graph_test.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / gpu_decode_graph_test.c` 371 lines ยท 28134 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * gpu_decode_graph_test: OWN-30B W-L2a, one Qwen3-30B-A3B decode token as ONE command list (omega_gpu_decode_graph.h). 19 * --host-only glue-kernel specs the graph builds (codegen, no device) and the record entry points' argument refusals 20 * (default) chip, caller holds the GB10 lock. A synthetic Qwen3-30B-A3B-shaped step on random weights (hidden 2048, 21 * 128 experts top-8, intermediate 768, head_dim 128, 32/4 heads; --layers N (2), --vocab V (151936), 22 * pos 300 over a random KV prefix: 2 attention splits): 23 * A baseline = today's path: the four APIs one call at a time, host glue in C mirroring sc 24 * qwen3_moe_omega.rs step() (rmsnorm f64, x += o, device router, gate_up from the router's table, 25 * SwiGLU expf, down, combine, final rmsnorm, lm_head, argmax strict >) 26 * C main = the same step recorded as one list (omega_gpu_decode_graph_token) 27 * B the same list submitted one launch per submission (flush_each): batching comparison 28 * Checks: C deterministic over the reps; B == C bit for bit (x, hidden, logits, token); A vs C token 29 * identical and logits within tolerance; negative controls (SWIGLU mutant, COMBINE mutant) must fail 30 * the A-vs-C comparison. Per-token medians over --reps (20). --out receipt.json 31 * --red the C path with the wrong-wiring mutant 3 (attention at scale 1, not 2^64): this run MUST FAIL 32 * (red-before-green receipt). 33 * Verdict: exit 0 when every CHECK passed. 34 */ 35 #include "omega_gpu_decode_graph.h" 36 #include "omega_gpu_cmdlist.h" 37 #include "omega_blackwell_codegen_dec.h" 38 #include "omega_gpu_session.h" 39 #include 40 #include 41 #include 42 #include 43 #include 44 45 #define H 2048u 46 #define QKV 5120u 47 #define ATT 4096u 48 #define NE 128u 49 #define TK 8u 50 #define I 768u 51 #define EPS 1e-6f 52 #define POS 300u 53 #define MAX_POS 512u 54 #define SCALE_2P64 `18446744073709551616`.0f 55 /* A vs C logits: |dl| <= TOL_REL * max|l_A|. Sources of difference (header): RMSNorm f32 FFMA + MUFU.RSQ vs f64 host, 56 * SwiGLU MUFU EX2/RCP vs expf (~1e-7 relative each), carried through 2 layers of random weights and 2048-term sums; 57 * 1e-4 is ~100x that. Mutants move logits by O(1e-1..1) of max|l|. */ 58 #define TOL_REL 1e-4 59 60 static int g_checks, g_failed; 61 #define CHECK(c, ...) do { g_checks++; if (!(c)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } else { printf("ok: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 62 63 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 64 static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / `16777216`.0f); } 65 static uint16_t bf16_rne(float f) { uint32_t x; memcpy(&x, &f, 4); uint32_t r = x + 0x7fffu + ((x >> 16) & 1u); return (uint16_t)(r >> 16); } 66 static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } 67 static uint64_t median_u64(uint64_t *v, unsigned n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; } 68 69 /* ---- host glue: the Rust step()'s operations ---- */ 70 static void rmsnorm(const float *x, const float *w, float *out) { /* tensor.rs rmsnorm */ 71 double sq = 0; for (unsigned i = 0; i < H; i++) sq += (double)x[i] * x[i]; 72 float s = (float)(1.0 / sqrt(sq / H + (double)EPS)); 73 for (unsigned i = 0; i < H; i++) out[i] = x[i] * s * w[i]; 74 } 75 static void swiglu(const float *gu, float *act) { /* qwen3_moe_omega.rs swiglu: g / (1 + exp(-g)) * up */ 76 for (unsigned g = 0; g < TK; g++) for (unsigned j = 0; j < I; j++) { 77 float v = gu[g * 2 * I + j]; 78 act[g * I + j] = v / (1.0f + expf(-v)) * gu[g * 2 * I + I + j]; 79 } 80 } 81 static uint32_t argmax(const float *v, uint32_t n) { uint32_t b = 0; for (uint32_t i = 1; i < n; i++) if (v[i] > v[b]) b = i; return b; } 82 83 /* ---- the synthetic model ---- */ 84 typedef struct { 85 uint32_t layers, vocab; 86 OmegaGpuFp8Tensor *qkv, *o, *gu, *dn; /* shared by every layer (memory); norms, router and KV differ per layer */ 87 OmegaGpuRouter *router; 88 OmegaGpuDecodeAttnCache *attn; 89 OmegaGpuBf16Tensor *lm; 90 float *in_norm, *fnorm, *x0; 91 } Model; 92 93 static int fp8_random(uint32_t seed, uint32_t e, uint32_t n, uint32_t k, OmegaGpuFp8Tensor **out) { 94 size_t wb = (size_t)e * n * k, sb = (size_t)e * (n / 128) * (k / 128); 95 uint8_t *w = malloc(wb); uint16_t *s = malloc(sb * 2); 96 if (!w || !s) { free(w); free(s); return -1; } 97 uint32_t st = seed; 98 for (size_t i = 0; i < wb; i += 4) { uint32_t r = lcg(&st); for (unsigned j = 0; j < 4 && i + j < wb; j++) w[i + j] = (uint8_t)((r >> (8 * j)) & 0xbfu); } /* |v| <= 1.875: never a NaN code */ 99 for (size_t i = 0; i < sb; i++) s[i] = bf16_rne(frand(&st, 0.02f, 0.04f)); 100 int rc = omega_gpu_fp8_tensor_upload(e, n, k, w, s, out); 101 free(w); free(s); 102 return rc; 103 } 104 105 static int model_build(Model *m, uint32_t layers, uint32_t vocab) { 106 memset(m, 0, sizeof *m); m->layers = layers; m->vocab = vocab; 107 uint32_t s = 1234; 108 if (fp8_random(11, 1, QKV, H, &m->qkv) || fp8_random(12, 1, H, ATT, &m->o) || fp8_random(13, NE, 2 * I, H, &m->gu) || fp8_random(14, NE, H, I, &m->dn)) { printf("fp8 upload: %s\n", omega_gpu_fp8_last_error()); return -1; } 109 if (omega_gpu_router_alloc(layers, &m->router) != 0) { printf("router alloc: %s\n", omega_gpu_router_last_error()); return -1; } 110 uint16_t *gate = malloc((size_t)NE * H * 2); float *nw = malloc(H * 4); 111 m->in_norm = malloc((size_t)layers * H * 4); m->fnorm = malloc(H * 4); m->x0 = malloc(H * 4); 112 if (!gate || !nw || !m->in_norm || !m->fnorm || !m->x0) return -1; 113 for (uint32_t l = 0; l < layers; l++) { 114 for (size_t i = 0; i < (size_t)NE * H; i++) gate[i] = bf16_rne(frand(&s, -0.05f, 0.05f)); 115 for (unsigned i = 0; i < H; i++) nw[i] = frand(&s, 0.8f, 1.2f); 116 if (omega_gpu_router_set_layer(m->router, l, gate, nw) != 0 || omega_gpu_router_set_directory_fp8(m->router, l, m->gu, m->dn) != 0) { printf("router layer: %s\n", omega_gpu_router_last_error()); return -1; } 117 for (unsigned i = 0; i < H; i++) m->in_norm[(size_t)l * H + i] = frand(&s, 0.8f, 1.2f); 118 } 119 free(gate); free(nw); 120 for (unsigned i = 0; i < H; i++) { m->fnorm[i] = frand(&s, 0.8f, 1.2f); m->x0[i] = frand(&s, -1.0f, 1.0f); } 121 if (omega_gpu_decode_attn_cache_alloc(layers, MAX_POS, `1000000`.0, &m->attn) != 0) { printf("attn alloc: %s\n", omega_gpu_decode_attn_last_error()); return -1; } 122 float qn[128], kn[128]; uint16_t *kr = malloc((size_t)POS * 128 * 2), *vr = malloc((size_t)POS * 128 * 2); 123 if (!kr || !vr) return -1; 124 for (uint32_t l = 0; l < layers; l++) { 125 for (unsigned i = 0; i < 128; i++) { qn[i] = frand(&s, 0.8f, 1.2f); kn[i] = frand(&s, 0.8f, 1.2f); } 126 if (omega_gpu_decode_attn_set_norms(m->attn, l, qn, kn) != 0) return -1; 127 for (uint32_t h = 0; h < 4; h++) { 128 for (size_t i = 0; i < (size_t)POS * 128; i++) { kr[i] = bf16_rne(frand(&s, -1, 1)); vr[i] = bf16_rne(frand(&s, -1, 1)); } 129 if (omega_gpu_decode_attn_cache_write(m->attn, l, h, 0, POS, kr, vr) != 0) { printf("kv write: %s\n", omega_gpu_decode_attn_last_error()); return -1; } 130 } 131 } 132 free(kr); free(vr); 133 uint16_t *lw = malloc((size_t)vocab * H * 2); 134 if (!lw) return -1; 135 for (size_t i = 0; i < (size_t)vocab * H; i++) lw[i] = bf16_rne(frand(&s, -0.05f, 0.05f)); 136 int rc = omega_gpu_bf16_tensor_upload(vocab, H, lw, &m->lm); 137 free(lw); 138 if (rc != 0) { printf("lm_head upload: %s\n", omega_gpu_bf16_last_error()); return -1; } 139 return 0; 140 } 141 142 /* A: today's path, one API call at a time, host glue */ 143 static int step_host(const Model *m, float *x, float *hidden, float *logits, uint32_t *tok, uint32_t *launches) { 144 static float xn[H], qkv[QKV], att[ATT], op[H], gu[TK * 2 * I], act[TK * I], dn[TK * H], mo[H]; 145 uint32_t ids[TK], zero = 0; float wts[TK]; uint64_t xout = 0, tgu = 0, tdn = 0; 146 memcpy(x, m->x0, H * 4); *launches = 0; 147 for (uint32_t l = 0; l < m->layers; l++) { 148 rmsnorm(x, m->in_norm + (size_t)l * H, xn); 149 if (omega_gpu_fp8_gemv_grouped_f32(m->qkv, 1, &zero, xn, 1, qkv, NULL)) return printf("A qkv: %s\n", omega_gpu_fp8_last_error()), -1; 150 if (omega_gpu_decode_attn(m->attn, l, POS, qkv, EPS, 1.0f, att, NULL)) return printf("A attn: %s\n", omega_gpu_decode_attn_last_error()), -1; 151 if (omega_gpu_fp8_gemv_grouped_f32(m->o, 1, &zero, att, 1, op, NULL)) return printf("A o: %s\n", omega_gpu_fp8_last_error()), -1; 152 for (unsigned i = 0; i < H; i++) x[i] += op[i]; 153 if (omega_gpu_router(m->router, l, x, EPS, 1, SCALE_2P64, ids, wts, NULL)) return printf("A router: %s\n", omega_gpu_router_last_error()), -1; 154 omega_gpu_router_device_outputs(m->router, &xout, &tgu, &tdn); 155 if (omega_gpu_fp8_gemv_grouped_dev(m->gu, TK, tgu, xout, gu, NULL)) return printf("A gate_up: %s\n", omega_gpu_fp8_last_error()), -1; 156 swiglu(gu, act); 157 if (omega_gpu_fp8_gemv_grouped_f32(m->dn, TK, ids, act, TK, dn, NULL)) return printf("A down: %s\n", omega_gpu_fp8_last_error()), -1; 158 for (unsigned i = 0; i < H; i++) mo[i] = 0.0f; 159 for (unsigned g = 0; g < TK; g++) for (unsigned i = 0; i < H; i++) mo[i] += wts[g] * dn[g * H + i]; 160 for (unsigned i = 0; i < H; i++) x[i] += mo[i]; 161 *launches += 6; 162 } 163 rmsnorm(x, m->fnorm, hidden); 164 if (omega_gpu_bf16_gemv_f32(m->lm, hidden, logits, NULL)) return printf("A lm_head: %s\n", omega_gpu_bf16_last_error()), -1; 165 *launches += 1; 166 *tok = argmax(logits, m->vocab); 167 return 0; 168 } 169 170 typedef struct { float x[H], hidden[H]; float *logits; uint32_t tok; } Out; 171 static int out_alloc(Out *o, uint32_t vocab) { memset(o, 0, sizeof *o); o->logits = malloc((size_t)vocab * 4); return o->logits ? 0 : -1; } 172 173 static int run_graph(OmegaGpuDecodeGraph *gr, const Model *m, Out *o, OmegaGpuDecodeGraphStats *st) { 174 int rc = omega_gpu_decode_graph_token(gr, m->x0, POS, &o->tok, st); 175 if (rc != 0) { printf("graph token rc %d: %s\n", rc, omega_gpu_session_last_error()); return rc; } 176 return omega_gpu_decode_graph_read(gr, o->x, o->hidden, o->logits); 177 } 178 179 typedef struct { double max_abs, scale; int tok_same; } Cmp; 180 static Cmp compare(const Out *a, const Out *c, uint32_t vocab) { 181 Cmp r = { 0, 0, a->tok == c->tok }; 182 for (uint32_t i = 0; i < vocab; i++) { double d = fabs((double)a->logits[i] - c->logits[i]); if (d > r.max_abs) r.max_abs = d; if (fabs(a->logits[i]) > r.scale) r.scale = fabs(a->logits[i]); } 183 return r; 184 } 185 static int cmp_pass(Cmp c) { return c.tok_same && c.max_abs <= TOL_REL * c.scale; } 186 static double max_rel(const float *a, const float *b, unsigned n) { double mx = 0, sc = 0; for (unsigned i = 0; i < n; i++) { double d = fabs((double)a[i] - b[i]); if (d > mx) mx = d; if (fabs(a[i]) > sc) sc = fabs(a[i]); } return sc > 0 ? mx / sc : mx; } 187 188 static int host_only(void) { 189 static const struct { OmegaDecKind kind; uint32_t mode; int mutant; const char *name; } sp[] = { 190 { OMEGA_DEC_RES, OMEGA_DEC_RES_NORM, 0, "RES NORM" }, { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0, "RES ADD" }, 191 { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0, "RES COMBINE|NORM" }, { OMEGA_DEC_SWIGLU, 0, 0, "SWIGLU" }, 192 { OMEGA_DEC_ARGMAX, 0, 0, "ARGMAX identity" }, { OMEGA_DEC_ARGMAX, 1, 0, "ARGMAX indexed" }, 193 { OMEGA_DEC_SWIGLU, 0, 1, "SWIGLU mutant 1" }, { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 2, "RES COMBINE|NORM mutant 2" }, 194 }; 195 for (unsigned i = 0; i < sizeof sp / sizeof sp[0]; i++) { 196 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = sp[i].kind; s.mode = sp[i].mode; s.mutant = sp[i].mutant; 197 uint32_t ins = 0, gpr = 0; int rc = omega_dec_prog_stats(&s, &ins, &gpr); 198 CHECK(rc == 0 && ins > 0, "%s builds (%u instructions, peak %u registers)", sp[i].name, ins, gpr); 199 } 200 OmegaDecSpec bad; memset(&bad, 0, sizeof bad); bad.kind = OMEGA_DEC_RES; bad.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE; 201 CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "RES ADD|COMBINE refused by codegen"); 202 /* record entry points refuse a missing list or missing device addresses before touching the device */ 203 CHECK(omega_gpu_fp8_gemv_grouped_record(NULL, NULL, 1, 1, 1, 1, 1) == OMEGA_GPU_FP8_BAD_ARGS, "fp8 record without a list: BAD_ARGS"); 204 CHECK(omega_gpu_bf16_gemv_record(NULL, NULL, 1, 1) == OMEGA_GPU_BF16_BAD_ARGS, "bf16 record without a list: BAD_ARGS"); 205 CHECK(omega_gpu_router_record(NULL, NULL, 0, 1, EPS, 1, 1.0f, NULL) == OMEGA_GPU_ROUTER_BAD_ARGS, "router record without a list: BAD_ARGS"); 206 CHECK(omega_gpu_decode_attn_record(NULL, NULL, 0, 0, 1, EPS, 1.0f, 1) == OMEGA_GPU_DATTN_BAD_ARGS, "decode attn record without a list: BAD_ARGS"); 207 OmegaGpuCmdList *fake = (OmegaGpuCmdList *)(uintptr_t)16; /* never dereferenced: the argument checks come first */ 208 CHECK(omega_gpu_fp8_gemv_grouped_record(fake, NULL, 1, 0, 1, 1, 1) == OMEGA_GPU_FP8_BAD_ARGS, "fp8 record with tab_va 0: BAD_ARGS"); 209 CHECK(omega_gpu_bf16_gemv_record(fake, NULL, 0, 1) == OMEGA_GPU_BF16_BAD_ARGS, "bf16 record with x_va 0: BAD_ARGS"); 210 CHECK(omega_gpu_router_record(fake, NULL, 0, 0, EPS, 1, 1.0f, NULL) == OMEGA_GPU_ROUTER_BAD_ARGS, "router record with a NULL router: BAD_ARGS"); 211 CHECK(omega_gpu_decode_attn_record(fake, NULL, 0, 0, 0, EPS, 1.0f, 1) == OMEGA_GPU_DATTN_BAD_ARGS, "decode attn record with a NULL cache: BAD_ARGS"); 212 OmegaGpuDecodeGraphConfig cfg; memset(&cfg, 0, sizeof cfg); 213 OmegaGpuDecodeGraph *gr = NULL; 214 CHECK(omega_gpu_decode_graph_create(&cfg, &gr) == OMEGA_GPU_DGRAPH_BAD_ARGS && !gr, "graph create with an empty config: BAD_ARGS"); 215 return g_failed ? 1 : 0; 216 } 217 218 static int chip_run(uint32_t layers, uint32_t vocab, uint32_t reps, int red, int cached, const char *out_path) { 219 FILE *out = out_path ? fopen(out_path, "w") : NULL; 220 if (out) fprintf(out, "{\"layers\":%u,\"vocab\":%u,\"pos\":%u,\"reps\":%u,\"red\":%d,\"cached_weights\":%d,", layers, vocab, POS, reps, red, cached); 221 omega_gpu_fp8_set_weight_cache(cached); /* one variable: the FP8 tensors uploaded below live GPU-cached or not */ 222 Model m; 223 uint64_t t0 = omega_gpu_session_now_ns(); 224 if (model_build(&m, layers, vocab) != 0) { CHECK(0, "synthetic model build"); return 1; } 225 printf("model built in %.1f s (layers %u, vocab %u, pos %u)\n", (omega_gpu_session_now_ns() - t0) / 1e9, layers, vocab, POS); 226 OmegaGpuDecodeGraphConfig cfg = { .layers = layers, .vocab = vocab, .eps = EPS, .norm_topk = 1, .router = m.router, .attn = m.attn, .lm_head = m.lm }; 227 OmegaGpuDecodeGraph *gr = NULL; 228 int rc = omega_gpu_decode_graph_create(&cfg, &gr); 229 if (rc != 0) { CHECK(0, "graph create rc %d: %s", rc, omega_gpu_session_last_error()); return 1; } 230 for (uint32_t l = 0; l < layers; l++) 231 if (omega_gpu_decode_graph_set_layer(gr, l, m.qkv, m.o, m.gu, m.dn, m.in_norm + (size_t)l * H) != 0) { CHECK(0, "set_layer %u", l); return 1; } 232 if (omega_gpu_decode_graph_set_final_norm(gr, m.fnorm) != 0) { CHECK(0, "set_final_norm"); return 1; } 233 234 Out A, A0, C, C0, B; uint64_t *ta = calloc(reps, 8), *tc = calloc(reps, 8), *tb = calloc(reps, 8), *cc = calloc(reps, 8), *cb = calloc(reps, 8), *rcn = calloc(reps, 8); 235 if (out_alloc(&A, vocab) || out_alloc(&A0, vocab) || out_alloc(&C, vocab) || out_alloc(&C0, vocab) || out_alloc(&B, vocab) || !ta || !tc || !tb || !cc || !cb || !rcn) return 1; 236 237 /* A: baseline first (also fills every API's kernel cache, so no record has to build) */ 238 uint32_t la = 0; int a_ok = 1, a_det = 1; 239 for (uint32_t r = 0; r < reps && a_ok; r++) { 240 uint64_t s = omega_gpu_session_now_ns(); 241 if (step_host(&m, A.x, A.hidden, A.logits, &A.tok, &la) != 0) { a_ok = 0; break; } 242 ta[r] = omega_gpu_session_now_ns() - s; 243 if (r == 0) { memcpy(A0.x, A.x, sizeof A.x); memcpy(A0.hidden, A.hidden, sizeof A.hidden); memcpy(A0.logits, A.logits, (size_t)vocab * 4); A0.tok = A.tok; } 244 else if (A.tok != A0.tok || memcmp(A.logits, A0.logits, (size_t)vocab * 4)) a_det = 0; 245 } 246 CHECK(a_ok, "A (baseline, host glue) %u reps ran", reps); 247 if (!a_ok) return 1; 248 CHECK(a_det, "A deterministic over %u reps (token %u)", reps, A0.tok); 249 /* top-2 gap of the baseline: how much margin the argmax identity has */ 250 uint32_t t1 = A0.tok, t2 = t1 == 0 ? 1 : 0; for (uint32_t i = 0; i < vocab; i++) if (i != t1 && A0.logits[i] > A0.logits[t2]) t2 = i; 251 printf("A: token %u logit %.6f, runner-up %u logit %.6f (gap %.3g)\n", t1, A0.logits[t1], t2, A0.logits[t2], (double)A0.logits[t1] - A0.logits[t2]); 252 253 if (red) omega_gpu_decode_graph_test_set_mutant(gr, 3); 254 /* C: one list per token */ 255 OmegaGpuDecodeGraphStats st; memset(&st, 0, sizeof st); uint32_t lc = 0, sc = 0; int c_ok = 1, c_det = 1; 256 for (uint32_t r = 0; r < reps && c_ok; r++) { 257 if (run_graph(gr, &m, &C, &st) != 0) { c_ok = 0; break; } 258 tc[r] = st.total_ns; cc[r] = st.chip_ns; rcn[r] = st.record_ns; lc = st.launches; sc = st.submits; 259 if (r == 0) { C0 = C; C0.logits = malloc((size_t)vocab * 4); memcpy(C0.logits, C.logits, (size_t)vocab * 4); } 260 else if (C.tok != C0.tok || memcmp(C.logits, C0.logits, (size_t)vocab * 4) || memcmp(C.x, C0.x, sizeof C.x)) c_det = 0; 261 } 262 CHECK(c_ok, "C (one list per token) %u reps ran: %u launches in %u submission(s)", reps, lc, sc); 263 if (!c_ok) return 1; 264 CHECK(c_det, "C deterministic over %u reps (token %u)", reps, C0.tok); 265 Cmp ac = compare(&A0, &C0, vocab); 266 double xr = max_rel(A0.x, C0.x, H), hr = max_rel(A0.hidden, C0.hidden, H); 267 printf("A vs C: token %u vs %u, logits max |d| %.3g (scale %.3g, rel %.3g, tol %.0e), x max rel %.3g, hidden max rel %.3g\n", 268 A0.tok, C0.tok, ac.max_abs, ac.scale, ac.max_abs / ac.scale, TOL_REL, xr, hr); 269 CHECK(ac.tok_same, "argmax token identical: A %u, C %u", A0.tok, C0.tok); 270 CHECK(ac.max_abs <= TOL_REL * ac.scale, "logits A vs C within %.0e of max|logit| (max |d| %.3g, scale %.3g)", TOL_REL, ac.max_abs, ac.scale); 271 if (out) fprintf(out, "\"A\":{\"token\":%u,\"runner_up\":%u,\"gap\":%.6g,\"launches\":%u},\"C\":{\"token\":%u,\"launches\":%u,\"submits\":%u},\"A_vs_C\":{\"tok_same\":%d,\"logit_max_abs\":%.6g,\"logit_scale\":%.6g,\"x_max_rel\":%.6g,\"hidden_max_rel\":%.6g,\"tol_rel\":%g},", 272 t1, t2, (double)A0.logits[t1] - A0.logits[t2], la, C0.tok, lc, sc, ac.tok_same, ac.max_abs, ac.scale, xr, hr, TOL_REL); 273 274 if (!red) { 275 /* B: same list, one launch per submission: bit-identical to C */ 276 omega_gpu_decode_graph_set_flush_each(gr, 1); 277 uint32_t lb = 0, sb = 0; int b_ok = 1, b_same = 1; 278 for (uint32_t r = 0; r < reps && b_ok; r++) { 279 if (run_graph(gr, &m, &B, &st) != 0) { b_ok = 0; break; } 280 tb[r] = st.total_ns; cb[r] = st.chip_ns; lb = st.launches; sb = st.submits; 281 if (B.tok != C0.tok || memcmp(B.logits, C0.logits, (size_t)vocab * 4) || memcmp(B.x, C0.x, sizeof B.x) || memcmp(B.hidden, C0.hidden, sizeof B.hidden)) b_same = 0; 282 } 283 omega_gpu_decode_graph_set_flush_each(gr, 0); 284 CHECK(b_ok, "B (one launch per submission) %u reps ran: %u launches in %u submissions", reps, lb, sb); 285 CHECK(b_ok && b_same, "B bit-identical to C (x, hidden, logits, token) in every rep"); 286 287 /* negative controls: a mutant glue kernel in C must fail the A-vs-C comparison */ 288 for (int mut = 1; mut <= 2; mut++) { 289 omega_gpu_decode_graph_test_set_mutant(gr, mut); 290 Out M; out_alloc(&M, vocab); 291 int mrc = run_graph(gr, &m, &M, &st); 292 Cmp mc = compare(&A0, &M, vocab); 293 CHECK(mrc == 0 && !cmp_pass(mc), "negative control: %s mutant fails the comparison (token %u vs %u, max |d| %.3g = %.3g of scale)", 294 mut == 1 ? "SWIGLU" : "COMBINE", M.tok, A0.tok, mc.max_abs, mc.max_abs / mc.scale); 295 if (out) fprintf(out, "\"mutant%d\":{\"rc\":%d,\"token\":%u,\"logit_max_abs\":%.6g,\"caught\":%d},", mut, mrc, M.tok, mc.max_abs, mrc == 0 && !cmp_pass(mc)); 296 free(M.logits); 297 } 298 omega_gpu_decode_graph_test_set_mutant(gr, 0); 299 /* after the mutants the real list must still agree (state not poisoned) */ 300 int prc = run_graph(gr, &m, &C, &st); 301 CHECK(prc == 0 && C.tok == C0.tok && !memcmp(C.logits, C0.logits, (size_t)vocab * 4), "C after the mutants bit-identical to the first C"); 302 303 if (out) { uint64_t h = 1469598103934665603ull; const unsigned char *b = (const unsigned char *)C0.logits; for (size_t i = 0; i < (size_t)vocab * 4; i++) { h ^= b[i]; h *= 1099511628211ull; } 304 fprintf(out, "\"C0_token\":%u,\"C0_logits_fnv\":\"%016" PRIx64 "\",", C0.tok, h); } 305 /* timeline instrument: stamps on -> one timestamp per launch, monotonic, span within the chip time; token unchanged; off -> refuses */ 306 { 307 static uint64_t sts[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; static uint8_t kinds[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; 308 omega_gpu_decode_graph_set_stamps(gr, 1); 309 Out S; out_alloc(&S, vocab); 310 int src_ = run_graph(gr, &m, &S, &st); 311 int n = omega_gpu_decode_graph_stamps(gr, sts, kinds, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); 312 int mono = 1; uint64_t per_kind[OMEGA_GPU_DGRAPH_K_COUNT] = { 0 }; uint32_t cnt_kind[OMEGA_GPU_DGRAPH_K_COUNT] = { 0 }; 313 for (int i = 0; i < n; i++) { if (sts[i + 1] < sts[i]) mono = 0; if (kinds[i] < OMEGA_GPU_DGRAPH_K_COUNT) { per_kind[kinds[i]] += sts[i + 1] - sts[i]; cnt_kind[kinds[i]]++; } } 314 const uint64_t span = n > 0 ? sts[n] - sts[0] : 0; 315 CHECK(src_ == 0 && n == (int)st.launches && mono && span > 0 && span <= st.chip_ns && S.tok == C0.tok && !memcmp(S.logits, C0.logits, (size_t)vocab * 4), 316 "timeline: rc %d, %d stamps for %u launches, monotonic %d, span %.1f us <= chip %.1f us, token and logits unchanged", src_, n, st.launches, mono, span / 1e3, st.chip_ns / 1e3); 317 printf("TIMELINE (%u layers, pos %u):", layers, POS); 318 for (unsigned k = 0; k < OMEGA_GPU_DGRAPH_K_COUNT; k++) if (cnt_kind[k]) printf(" %s x%u %.1f us", omega_gpu_decode_graph_kind_name((uint8_t)k), cnt_kind[k], per_kind[k] / 1e3); 319 printf("\n"); 320 if (out) { fprintf(out, "\"timeline\":{\"launches\":%d,\"span_ns\":%" PRIu64 ",\"chip_ns\":%" PRIu64 ",\"per_kind_ns\":{", n, span, st.chip_ns); 321 int first = 1; for (unsigned k = 0; k < OMEGA_GPU_DGRAPH_K_COUNT; k++) if (cnt_kind[k]) { fprintf(out, "%s\"%s\":[%u,%" PRIu64 "]", first ? "" : ",", omega_gpu_decode_graph_kind_name((uint8_t)k), cnt_kind[k], per_kind[k]); first = 0; } 322 fprintf(out, "}},"); } 323 omega_gpu_decode_graph_set_stamps(gr, 0); 324 int orc = run_graph(gr, &m, &S, &st); 325 int n2 = omega_gpu_decode_graph_stamps(gr, sts, kinds, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); 326 CHECK(orc == 0 && n2 < 0, "timeline off: token rc %d, stamps accessor refuses (%d)", orc, n2); 327 free(S.logits); 328 } 329 uint64_t ma = median_u64(ta, reps), mc_ = median_u64(tc, reps), mb = median_u64(tb, reps), mcc = median_u64(cc, reps), mcb = median_u64(cb, reps), mr = median_u64(rcn, reps); 330 printf("PER TOKEN (median of %u, %u layers): A host-glue %u launches %.1f us | C one list %u launches %.1f us (chip %.1f us, record %.1f us) | B per-launch submits %u launches %.1f us (chip sum %.1f us)\n", 331 reps, layers, la, ma / 1e3, lc, mc_ / 1e3, mcc / 1e3, mr / 1e3, lb, mb / 1e3, mcb / 1e3); 332 if (out) fprintf(out, "\"median_ns\":{\"A_total\":%" PRIu64 ",\"C_total\":%" PRIu64 ",\"C_chip\":%" PRIu64 ",\"C_record\":%" PRIu64 ",\"B_total\":%" PRIu64 ",\"B_chip\":%" PRIu64 "},\"B\":{\"launches\":%u,\"submits\":%u,\"identical\":%d},", 333 ma, mc_, mcc, mr, mb, mcb, lb, sb, b_ok && b_same); 334 } 335 omega_gpu_decode_graph_free(gr); 336 if (out) { fprintf(out, "\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); } 337 return g_failed ? 1 : 0; 338 } 339 340 int main(int argc, char **argv) { 341 int host = 0, red = 0, cached = 0, variant = 0, barrier = -1, prefetch = 0, prefetch_red = 0; uint32_t cta_budget = 0, sh_lane = 0, sh_rows = 0, sh_warps = 0; uint32_t reps = 20, layers = 2, vocab = 151936; const char *out = NULL; 342 for (int i = 1; i < argc; i++) { 343 if (strcmp(argv[i], "--host-only") == 0) host = 1; 344 else if (strcmp(argv[i], "--red") == 0) red = 1; 345 else if (strcmp(argv[i], "--cached-weights") == 0) cached = 1; 346 else if (strcmp(argv[i], "--prefetch") == 0) prefetch = 1; /* W-L2d: o_proj weight prefetch beside the attention */ 347 else if (strcmp(argv[i], "--prefetch-red") == 0) prefetch = prefetch_red = 1; /* its red: o_proj unordered, MUST FAIL */ 348 else if (strcmp(argv[i], "--cta-budget") == 0 && i + 1 < argc) cta_budget = (uint32_t)atoi(argv[++i]); 349 else if (strcmp(argv[i], "--variant") == 0 && i + 1 < argc) variant = atoi(argv[++i]); /* FP8 GEMV generator: 1 (default) or 2 (W-L2c) */ 350 else if (strcmp(argv[i], "--barrier") == 0 && i + 1 < argc) barrier = atoi(argv[++i]); /* per-launch barrier mode (W-L2c): 0 wfi+inval_all, 2 wfi_only, 3 wfi+inval_data, 4 flush_data */ 351 else if (strcmp(argv[i], "--shape") == 0 && i + 3 < argc) { sh_lane = (uint32_t)atoi(argv[++i]); sh_rows = (uint32_t)atoi(argv[++i]); sh_warps = (uint32_t)atoi(argv[++i]); } 352 else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); 353 else if (strcmp(argv[i], "--layers") == 0 && i + 1 < argc) layers = (uint32_t)atoi(argv[++i]); 354 else if (strcmp(argv[i], "--vocab") == 0 && i + 1 < argc) vocab = (uint32_t)atoi(argv[++i]); 355 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out = argv[++i]; 356 else { printf("usage: %s [--host-only] [--red] [--reps N] [--layers N] [--vocab V] [--out receipt.json]\n", argv[0]); return 2; } 357 } 358 if (reps == 0) reps = 1; 359 if (layers == 0 || layers > 8) layers = 2; 360 if (variant) omega_gpu_fp8_set_variant(variant); 361 omega_gpu_decode_graph_set_prefetch(prefetch); omega_gpu_decode_graph_test_set_prefetch_red(prefetch_red); 362 if (barrier == 1) omega_gpu_decode_graph_test_set_barrier_none(); /* negative control: this run MUST FAIL */ 363 else if (barrier >= 0) omega_gpu_decode_graph_set_barrier(barrier); 364 if (cta_budget) omega_gpu_fp8_set_cta_budget(cta_budget); /* one variable per run (timeline sweeps) */ 365 if (sh_lane) omega_gpu_fp8_set_shape(sh_lane, sh_rows, sh_warps); 366 printf("dgraph prefetch %d (red %d)\n", omega_gpu_decode_graph_prefetch(), prefetch_red); 367 printf("fp8 knobs: variant %d barrier %d (%s) cta_budget %u shape %u/%u/%u cached %d\n", omega_gpu_fp8_variant(), omega_gpu_decode_graph_barrier(), omega_gpu_cmdlist_barrier_name((OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier()), cta_budget, sh_lane, sh_rows, sh_warps, cached); 368 int rc = host ? host_only() : chip_run(layers, vocab, reps, red, cached, out); 369 printf("%s: %d checks, %d failed: %s\n", host ? "host-only" : red ? "chip-red" : "chip", g_checks, g_failed, rc ? "FAIL" : "PASS"); 370 return rc; 371 }