interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / gpu_tern_test.c

700 lines · 48028 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_tern_test: KT1, the W2A8 ternary GEMV (omega_gpu_tern_api.h), ternary engine stage T0 (2026-10-15).
19 * --host-only the word arithmetic (accp >> 16, accm >> 17) against the direct dot on random and extreme words,
20 * trit packing round trip (both layouts), segment boundaries (explicit vs the r / (n / count) mapping),
21 * codegen budgets at the four BitNet shapes, refusals (no device)
22 * --sim warp-simulator parity: int32 output bit-equal to oma_rz_oracle over layouts, rows, depths, multi-CTA,
23 * padded n, two x chunks (k > 8192); f32 epilogue with three different segment scales bit-equal to the
24 * host formula; mutants 1 (flipped plane), 3 (dropped pair) and 2 (r / (n / count) scale map) caught
25 * (default) the chip: 20,000 random rows plus the MA grid plus the four BitNet shapes bit-equal to oma_rz_oracle,
26 * extremes, both layouts, second call identical, f32 q|k|v segment test, mutants 1 / 2 / 3 caught, then
27 * cold bandwidth (L2 defeated by rotating weight copies totalling >= 96 MiB in one command list, GPU
28 * timestamps per launch) and the in-list launch floor. --out receipt.json, --reps N, --no-bw, --bw-only,
29 * --mutant M (runs the correctness battery with mutant M forced: the verdict must be FAIL),
30 * --layout L --block-rows R --rows R --warps W --depth D --ctas C (shape knobs for sweeps), --weights cached|uncached, --hot (diagnostic)
31 * Speeds are reported only as fractions of the physical limit: 273 GB/s (spec) and 237.2 GB/s (practical rate).
32 * Verdict line last: "gpu_tern_test: N checks, F failed: PASS|FAIL".
33 */
34#include "omega_gpu_tern_api.h"
35#include "omega_gpu_session.h"
36#include "omega_gpu_cmdlist.h"
37#include "algebra/realize_common.h"
38#include "bw_warp_sim_k.h"
39#include "omega_path_check.h"
40#include <fcntl.h>
41#include <inttypes.h>
42#include <math.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48/* 0644, not fopen's 0666 & ~umask: a receipt is never world-writable */
49static FILE *create_0644(const char *path) {
50 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
51 FILE *f = fd >= 0 ? fdopen(fd, "w") : NULL;
52 if (fd >= 0 && !f) close(fd);
53 return f;
54}
55
56#define SPEC_GBS 273.0
57#define PRACTICAL_GBS 237.2
58
59/* The layout the bandwidth runs use: the API default (row-major) unless --layout chose another. Restored after the
60 * correctness loop, which sets both layouts in turn. */
61static uint32_t g_lay = OMEGA_TERN_LAYOUT_ROWMAJOR, g_brows;
62static int g_checks, g_failed;
63#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
64static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
65static 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; }
66
67/* trits: zero with probability sparsity, else +1 / -1 evenly */
68static int8_t *make_w(size_t count, double sparsity, uint32_t seed) {
69 int8_t *w = malloc(count ? count : 1);
70 const uint32_t zcut = (uint32_t)(sparsity * 4294967295.0);
71 for (size_t i = 0; i < count; i++) { uint32_t r = lcg(&seed); w[i] = r < zcut ? 0 : (lcg(&seed) >> 31) ? 1 : -1; }
72 return w;
73}
74static int8_t *make_x(uint32_t k, uint32_t seed) {
75 int8_t *x = aligned_alloc(16, (k + 15u) & ~15u);
76 for (uint32_t i = 0; i < k; i++) x[i] = (int8_t)(lcg(&seed) >> 24);
77 return x;
78}
79static int32_t *oracle(const int8_t *w, uint32_t n, uint32_t k, const int8_t *x) {
80 int32_t *y = malloc((size_t)n * 4);
81 if (oma_rz_oracle(w, n, k, x, y) != OMA_RZ_OK) { free(y); return NULL; }
82 return y;
83}
84/* f32 epilogue as the header specifies: ((float)y * a) * scale[seg], seg from explicit boundaries (this file is built with
85 * -ffp-contract=off, so each product rounds to nearest f32 as FMUL does) */
86static void expect_f32(const int32_t *y, uint32_t n, const uint32_t b[3], const float scale4[4], float a, float *out) {
87 for (uint32_t r = 0; r < n; r++) {
88 uint32_t seg = (r >= b[0]) + (r >= b[1]) + (r >= b[2]);
89 float f = (float)y[r];
90 f = f * a;
91 f = f * scale4[seg];
92 out[r] = f;
93 }
94}
95
96/* ------------------------------------------------------------- host-only */
97/* the kernel's arithmetic for one 16-trit word, mod 2^32, as emitted (mutant 1: staged X negated; 3: pair 7 dropped) */
98static uint32_t emu_word(uint32_t W, const int8_t *x16, int mutant) {
99 uint32_t accp = 0x80008000u, accm = 0x80008000u;
100 for (uint32_t j = 0; j < (mutant == 3 ? 7u : 8u); j++) {
101 uint32_t X = (uint32_t)(int32_t)x16[j] * 65536u + (uint32_t)(int32_t)x16[j + 8];
102 if (mutant == 1) X = 0u - X;
103 uint32_t U = W >> (2u * j);
104 accp += (U & 0x00010001u) * X;
105 accm += (U & 0x00020002u) * X;
106 }
107 return (accp >> 16) - (accm >> 17); /* 2^14 + dot */
108}
109static int32_t direct_word(uint32_t W, const int8_t *x16) {
110 int32_t s = 0;
111 for (uint32_t i = 0; i < 16; i++) { uint32_t c = (W >> (2u * i)) & 3u; s += (c == 1u ? 1 : c == 2u ? -1 : 0) * x16[i]; }
112 return s;
113}
114static uint32_t rand_word(uint32_t *st, int mode) {
115 uint32_t W = 0;
116 for (uint32_t i = 0; i < 16; i++) {
117 uint32_t c = mode == 1 ? 1u : mode == 2 ? 2u : (lcg(st) >> 30) % 3u;
118 W |= c << (2u * i);
119 }
120 return W;
121}
122/* Independent stall checker (a different walk from the generator's pass): for every fixed-latency write, scan forward
123 * summing stalls; any instruction that touches one of its registers, any control instruction and any branch target
124 * reached before OMEGA_TERN_FIXED_LAT cycles is a violation. */
125static int chk_fixed(BlackwellIROpcode op) {
126 return op == BW_IR_IMAD || op == BW_IR_IMAD_WIDE || op == BW_IR_SHF_R || op == BW_IR_LOP3_AND || op == BW_IR_MOV_IMM ||
127 op == BW_IR_MOV_RZ || op == BW_IR_FMUL || op == BW_IR_I2FP;
128}
129static int chk_overlap(const BlackwellIRProgram *p, int a, int b) {
130 if (a < 0 || b < 0) return 0;
131 const int pa = p->regalloc.vreg_to_phys[a], pb = p->regalloc.vreg_to_phys[b];
132 if (pa < 0 || pb < 0) return 0;
133 const int na = (int)(p->regalloc.intervals[a].bundle_size ? p->regalloc.intervals[a].bundle_size : 1u);
134 const int nb = (int)(p->regalloc.intervals[b].bundle_size ? p->regalloc.intervals[b].bundle_size : 1u);
135 return pa < pb + nb && pb < pa + na;
136}
137static long stall_violations(const BlackwellIRProgram *p, uint64_t *cycles) {
138 static uint8_t tgt[BW_MAX_IR_INSNS + 1];
139 memset(tgt, 0, sizeof tgt);
140 long bad = 0; uint64_t cyc = 0;
141 for (size_t i = 0; i < p->count; i++) {
142 const BlackwellIRInsn *in = &p->insns[i];
143 cyc += (in->control >> 9) & 0xfu;
144 if (in->op == BW_IR_BRA || in->op == BW_IR_BSSY) { long t = (long)i + (long)(int32_t)in->imm; if (t >= 0 && t <= (long)p->count) tgt[t] = 1; }
145 }
146 for (size_t i = 0; i < p->count; i++) {
147 const BlackwellIRInsn *in = &p->insns[i];
148 if (!chk_fixed(in->op) || in->dst_vreg < 0) continue;
149 uint32_t dist = 0;
150 for (size_t j = i + 1; j < p->count; j++) {
151 dist += (p->insns[j - 1].control >> 9) & 0xfu;
152 if (dist >= OMEGA_TERN_FIXED_LAT) break;
153 const BlackwellIRInsn *u = &p->insns[j];
154 const BlackwellIROpcode o = u->op;
155 if (o == BW_IR_BRA || o == BW_IR_EXIT || o == BW_IR_BSSY || o == BW_IR_BSYNC || o == BW_IR_BAR_SYNC || tgt[j]) { bad++; break; }
156 if (chk_overlap(p, in->dst_vreg, u->dst_vreg) || chk_overlap(p, in->dst_vreg, u->src1_vreg) ||
157 chk_overlap(p, in->dst_vreg, u->src2_vreg) || chk_overlap(p, in->dst_vreg, u->src3_vreg)) { bad++; break; }
158 }
159 }
160 if (cycles) *cycles = cyc;
161 return bad;
162}
163static void host_battery(void) {
164 uint32_t st = 0x7e57u; size_t bad = 0, bad1 = 0, bad3 = 0, n = 0;
165 int8_t x16[16];
166 const int8_t ext[4] = { -128, 127, -1, 0 };
167 for (int mode = 0; mode < 3; mode++)
168 for (int xm = 0; xm < 6; xm++)
169 for (int rep = 0; rep < (mode == 0 && xm == 0 ? 400000 : 2000); rep++) {
170 for (int i = 0; i < 16; i++) x16[i] = xm == 0 ? (int8_t)(lcg(&st) >> 24) : xm == 5 ? (int8_t)((i & 1) ? -128 : 127) : ext[xm - 1];
171 uint32_t W = rand_word(&st, mode);
172 int32_t want = direct_word(W, x16);
173 n++;
174 if (emu_word(W, x16, 0) != (1u << 14) + (uint32_t)want) bad++;
175 if (emu_word(W, x16, 1) == (1u << 14) + (uint32_t)want && want != 0) bad1++;
176 if (emu_word(W, x16, 3) != (1u << 14) + (uint32_t)want) bad3++;
177 }
178 CHECK(bad == 0, "word arithmetic: %zu of %zu words differ from the direct dot", bad, n);
179 printf("host: word arithmetic exact on %zu words (random, all +1, all -1; x random, -128, 127, -1, 0, alternating)\n", n);
180 CHECK(bad1 == 0 && bad3 > 0, "mutant arithmetic not distinguishable (flipped equal on %zu nonzero words, dropped pair differs on %zu)", bad1, bad3);
181
182 /* packing round trip, both layouts, padded n */
183 for (int lay = 0; lay < 2; lay++) for (uint32_t R = (lay ? 2u : 1u); R <= 4u; R *= 2u) {
184 const uint32_t nn = 13, k = 512, np = omega_gpu_tern_padded_rows(nn);
185 int8_t *w = make_w((size_t)nn * k, 0.3, 11u + R);
186 uint32_t *pk = calloc((size_t)np * k / 16u, 4);
187 int rc = omega_gpu_tern_pack(nn, k, w, (uint32_t)lay, R, pk, np);
188 size_t diff = 0;
189 for (uint32_t r = 0; r < np; r++) for (uint32_t c = 0; c < k / 16u; c++) {
190 uint32_t word = pk[omega_tern_word_offset((uint32_t)lay, R, k, r, c) / 4u];
191 for (uint32_t i = 0; i < 16; i++) {
192 uint32_t code = (word >> (2u * i)) & 3u;
193 int v = code == 1u ? 1 : code == 2u ? -1 : code == 0u ? 0 : 9;
194 int want = r < nn ? w[(size_t)r * k + 16u * c + i] : 0;
195 if (v != want) diff++;
196 }
197 }
198 CHECK(rc == 0 && diff == 0, "pack layout %d R %u: rc %d, %zu trits differ", lay, R, rc, diff);
199 free(w); free(pk);
200 }
201 { int8_t w2[512]; memset(w2, 0, sizeof w2); w2[300] = 2; uint32_t pk[8 * 512 / 16]; /* n_padded 8 rows of 32 words */
202 CHECK(omega_gpu_tern_pack(1, 512, w2, 0, 1, pk, 8) == OMEGA_GPU_TERN_BAD_TRIT, "pack refuses a non-trit value"); }
203
204 /* segments: explicit boundaries vs the r / (n / count) map (mutant 2) at q|k|v 3840 */
205 OmegaGpuTernSegments s = { 3, { 2560, 3200, 3840, 0 }, { 0.5f, 2.0f, 3.0f, 0 } };
206 uint32_t b[3], bm[3];
207 CHECK(omega_gpu_tern_segment_bounds(3840, &s, 0, b) == 0 && b[0] == 2560 && b[1] == 3200 && b[2] == 3840, "explicit q|k|v boundaries");
208 CHECK(omega_gpu_tern_segment_bounds(3840, &s, 2, bm) == 0 && bm[0] == 1280 && bm[1] == 2560, "mutant-2 boundaries are r / (n / 3)");
209 OmegaGpuTernSegments one = { 1, { 2560 }, { 1.0f } };
210 CHECK(omega_gpu_tern_segment_bounds(2560, &one, 0, b) == 0 && b[0] == 2560 && b[2] == 2560, "one segment: every boundary at n");
211
212 /* codegen budgets at the BitNet shapes, both layouts */
213 const uint32_t ks[] = { 2560, 6912, 16384 };
214 for (unsigned i = 0; i < 3; i++) for (int lay = 0; lay < 2; lay++) for (int epi = 0; epi < 2; epi++) {
215 if (epi && ks[i] > OMEGA_TERN_CHUNK_MAX) continue;
216 OmegaTernGemvSpec sp = { ks[i], (uint32_t)lay, 4, 8, 0, (uint32_t)epi, 0 };
217 omega_gpu_tern_shape_for(ks[i], (uint32_t)lay, 4, &sp.rows, &sp.warps, &sp.depth, NULL);
218 uint32_t insns = 0, gprs = 0, decl = 0;
219 int rc = omega_tern_gemv_prog_stats(&sp, &insns, &gprs);
220 CHECK(rc == 0, "codegen k %u layout %d epi %d rows %u depth %u", sp.k, lay, epi, sp.rows, sp.depth);
221 CHECK(rc != 0 || omega_gpu_tern_kernel_fits(&sp, &decl) == 0, "register file k %u layout %d", sp.k, lay);
222 if (rc == 0) printf("host: codegen k %u layout %s epi %s rows %u depth %u warps %u: %u instructions, peak %u registers (declared %u), shared %u B\n",
223 sp.k, lay ? "block" : "rowmajor", epi ? "f32" : "i32", sp.rows, sp.depth, sp.warps, insns, gprs, decl, omega_tern_gemv_shared_bytes(&sp));
224 {
225 static BlackwellIRProgram cp; uint64_t cyc = 0;
226 const int ok = rc == 0 && omega_blackwell_codegen_tern_gemv_prog(&sp, &cp) == 0;
227 const long v = ok ? stall_violations(&cp, &cyc) : -1;
228 CHECK(v == 0, "stall distances k %u layout %d epi %d: %ld users closer than %u cycles to a fixed-latency write", sp.k, lay, epi, v, OMEGA_TERN_FIXED_LAT);
229 /* negative control: a fixed-latency stall above 1 is binding by construction, so shortening it must be caught */
230 size_t at = cp.count;
231 for (size_t q = 0; ok && q < cp.count; q++) if (chk_fixed(cp.insns[q].op) && ((cp.insns[q].control >> 9) & 0xfu) > 1u) { at = q; break; }
232 if (ok && at < cp.count) cp.insns[at].control = (cp.insns[at].control & ~(0xfu << 9)) | (1u << 9);
233 CHECK(ok && at < cp.count && stall_violations(&cp, NULL) > 0, "stall checker catches one shortened stall (k %u layout %d epi %d)", sp.k, lay, epi);
234 if (ok) printf("host: stall distances >= %u cycles hold; %" PRIu64 " stall cycles in program order; shortened-stall control caught\n", OMEGA_TERN_FIXED_LAT, cyc);
235 }
236 }
237 /* refusals */
238 OmegaTernGemvSpec r1 = { 200, 0, 1, 8, 1, 0, 0 }, r2 = { 8448, 0, 1, 8, 1, 0, 0 }, r3 = { 16896, 0, 1, 8, 1, 0, 0 },
239 r4 = { 2560, 0, 3, 8, 1, 0, 0 }, r5 = { 2560, 1, 1, 8, 1, 0, 0 }, r6 = { 2560, 0, 1, 8, 3, 0, 0 },
240 r7 = { 16384, 0, 1, 8, 1, 1, 0 }, r8 = { 2560, 0, 4, 8, 5, 0, 0 };
241 CHECK(!omega_tern_gemv_spec_ok(&r1) && !omega_tern_gemv_spec_ok(&r2) && !omega_tern_gemv_spec_ok(&r3) && !omega_tern_gemv_spec_ok(&r4)
242 && !omega_tern_gemv_spec_ok(&r5) && !omega_tern_gemv_spec_ok(&r6) && !omega_tern_gemv_spec_ok(&r7) && !omega_tern_gemv_spec_ok(&r8),
243 "spec refusals: k 200, k 8448 (two chunks need k %% 512), k 16896, rows 3, block rows 1, depth 3 at k 2560, f32 with two chunks, rows*depth > 16");
244 OmegaTernGemvSpec okk = { 16384, 0, 1, 8, 16, 0, 0 };
245 CHECK(omega_tern_gemv_spec_ok(&okk) && omega_tern_gemv_chunks(&okk) == 2 && omega_tern_gemv_shared_bytes(&okk) == 16384, "k 16384: two chunks of 8192, 16 KiB shared");
246 int8_t badw[512]; memset(badw, 0, sizeof badw); badw[7] = 2;
247 OmegaGpuTernTensor *t = NULL;
248 CHECK(omega_gpu_tern_tensor_upload(1, 256, badw, NULL, &t) == OMEGA_GPU_TERN_BAD_TRIT && !t, "a non-trit weight is refused before any device use");
249 CHECK(omega_gpu_tern_tensor_upload(1, 200, badw, NULL, &t) == OMEGA_GPU_TERN_BAD_ARGS, "k 200 refused at upload");
250 OmegaGpuTernSegments badseg = { 2, { 1, 3 }, { 1, 1 } };
251 badw[7] = 0;
252 CHECK(omega_gpu_tern_tensor_upload(2, 256, badw, &badseg, &t) == OMEGA_GPU_TERN_BAD_ARGS, "segments whose last end is not n are refused");
253}
254
255/* ------------------------------------------------------------- simulator */
256static uint64_t g_sim_issues;
257/* Runs spec on the simulator; y_out n words. bounds: the boundaries to pass (NULL: one segment). */
258static uint32_t g_sim_wsched; /* warp visit order per barrier phase (WsSim.warp_sched): 0 ascending, 1 descending, else a shuffle seed */
259static int sim_run(const OmegaTernGemvSpec *spec, uint32_t n, const int8_t *w, const int8_t *x, const uint32_t *bounds, const float *scale4,
260 float a, uint32_t grid_x, int order, uint32_t *y_out, char *msg, size_t msgn) {
261 static BlackwellIRProgram prog;
262 if (omega_blackwell_codegen_tern_gemv_prog(spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
263 if (stall_violations(&prog, NULL) != 0) { snprintf(msg, msgn, "stall distance violation"); return -1; }
264 const uint32_t k = spec->k, np = omega_gpu_tern_padded_rows(n);
265 const size_t wbytes = (size_t)np * k / 4u;
266 uint32_t *pk = aligned_alloc(16, wbytes);
267 if (omega_gpu_tern_pack(n, k, w, spec->layout, spec->rows, pk, np) != 0) { free(pk); snprintf(msg, msgn, "pack failed"); return -1; }
268 int8_t *xd = aligned_alloc(16, (k + 15u) & ~15u); memcpy(xd, x, k);
269 uint32_t *y = aligned_alloc(16, ((size_t)n * 4 + 15) & ~(size_t)15);
270 float *ws = aligned_alloc(16, 16), *as = aligned_alloc(16, 16);
271 float one4[4] = { 1, 1, 1, 1 };
272 const float *sc = scale4 ? scale4 : one4;
273 for (int i = 0; i < 4; i++) ws[i] = sc[3 - i];
274 as[0] = a;
275 const uint32_t sh = omega_tern_gemv_shared_bytes(spec);
276 uint8_t *smem = aligned_alloc(16, sh);
277 for (uint32_t i = 0; i < n; i++) y[i] = 0xdeadbeefu;
278 WsSim s; memset(&s, 0, sizeof s);
279 s.p = &prog; s.threads = 32u * spec->warps; s.order = order; s.warp_sched = g_sim_wsched; s.smem = smem; s.smem_bytes = sh;
280 uint64_t A[5] = { (uintptr_t)pk, (uintptr_t)xd, (uintptr_t)y, (uintptr_t)ws, (uintptr_t)as };
281 for (unsigned i = 0; i < 5; i++) { s.cbank[(OMEGA_TERN_ARG_W + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(OMEGA_TERN_ARG_W + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); }
282 s.cbank[OMEGA_TERN_ARG_U / 4] = np / (2u * spec->rows);
283 s.cbank[OMEGA_TERN_ARG_STRIDE / 4] = grid_x * spec->warps;
284 s.cbank[OMEGA_TERN_ARG_NEGN / 4] = 0u - n;
285 for (int i = 0; i < 3; i++) s.cbank[OMEGA_TERN_ARG_NEGB / 4 + i] = 0u - (bounds ? bounds[i] : n);
286 s.nranges = 5;
287 s.ranges[0].lo = (uintptr_t)pk; s.ranges[0].hi = (uintptr_t)pk + wbytes;
288 s.ranges[1].lo = (uintptr_t)xd; s.ranges[1].hi = (uintptr_t)xd + k;
289 s.ranges[2].lo = (uintptr_t)y; s.ranges[2].hi = (uintptr_t)y + (size_t)n * 4;
290 s.ranges[3].lo = (uintptr_t)ws; s.ranges[3].hi = (uintptr_t)ws + 16;
291 s.ranges[4].lo = (uintptr_t)as; s.ranges[4].hi = (uintptr_t)as + 4;
292 int err = ws_run(&s, grid_x, 1);
293 if (err) snprintf(msg, msgn, "%s (pc %d)", s.msg, s.err_pc);
294 else {
295 g_sim_issues += s.warp_issues;
296 size_t unwritten = 0; for (uint32_t i = 0; i < n; i++) if (y[i] == 0xdeadbeefu) unwritten++;
297 if (unwritten) { err = -1; snprintf(msg, msgn, "%zu output words never written", unwritten); }
298 memcpy(y_out, y, (size_t)n * 4);
299 }
300 free(pk); free(xd); free(y); free(ws); free(as); free(smem);
301 return err;
302}
303/* one int32 point: returns rows differing from the oracle (or -1 on a sim error) */
304static long sim_i32(uint32_t k, uint32_t n, uint32_t layout, uint32_t rows, uint32_t warps, uint32_t depth, uint32_t grid, int mutant, uint32_t seed, int order) {
305 OmegaTernGemvSpec sp = { k, layout, rows, warps, depth, OMEGA_TERN_EPI_I32, mutant };
306 int8_t *w = make_w((size_t)n * k, 0.3, seed), *x = make_x(k, seed + 7);
307 int32_t *ref = oracle(w, n, k, x);
308 uint32_t *y = malloc((size_t)n * 4);
309 char msg[240];
310 long bad = -1;
311 if (sim_run(&sp, n, w, x, NULL, NULL, 1.0f, grid, order, y, msg, sizeof msg) != 0) printf("sim k %u n %u: %s\n", k, n, msg);
312 else { bad = 0; for (uint32_t i = 0; i < n; i++) if ((int32_t)y[i] != ref[i]) bad++; }
313 free(w); free(x); free(ref); free(y);
314 return bad;
315}
316static void sim_battery(void) {
317 struct { uint32_t k, n, layout, rows, warps, depth, grid; } pts[] = {
318 { 256, 16, 0, 1, 2, 1, 1 }, { 512, 13, 0, 2, 2, 2, 2 }, { 1024, 40, 0, 4, 4, 4, 2 }, { 2560, 24, 0, 2, 2, 5, 1 },
319 { 512, 16, 1, 2, 2, 2, 1 }, { 1024, 37, 1, 4, 2, 4, 3 }, { 2560, 32, 1, 4, 4, 2, 1 }, { 2560, 8, 1, 2, 1, 5, 1 },
320 { 768, 64, 1, 4, 2, 1, 2 }, { 8704, 16, 0, 2, 1, 1, 1 }, { 8704, 16, 1, 2, 2, 1, 1 },
321 { 2560, 20, 0, 1, 2, 10, 2 }, { 6912, 9, 0, 1, 2, 9, 1 }, { 16384, 8, 0, 1, 1, 16, 1 }, /* the API defaults (row-major, rows 1) */
322 { 1024, 18, 0, 1, 5, 2, 1 }, /* coalesced path: 12 units (24 padded rows) over 5 warps = 2 full rounds and a partial one, 2 K iterations */
323 { 512, 64, 0, 1, 2, 2, 1 }, /* coalesced path: 32 units over 2 warps = 16 rounds through the two slot sets */
324 };
325 const uint32_t wscheds[3] = { 0, 1, 12345u };
326 for (unsigned i = 0; i < sizeof pts / sizeof pts[0]; i++)
327 for (int order = 0; order < 2; order++)
328 for (int ws = 0; ws < 3; ws++) {
329 g_sim_wsched = wscheds[ws];
330 long bad = sim_i32(pts[i].k, pts[i].n, pts[i].layout, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, 0, 100u + i, order);
331 CHECK(bad == 0, "sim k %u n %u layout %u rows %u warps %u depth %u grid %u order %d warp order %u: %ld rows differ from oma_rz_oracle",
332 pts[i].k, pts[i].n, pts[i].layout, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, order, wscheds[ws], bad);
333 }
334 g_sim_wsched = 0;
335 printf("sim: %u int32 points x 2 fragment orders x 3 warp orders bit-equal to oma_rz_oracle (%" PRIu64 " warp issues)\n",
336 (unsigned)(sizeof pts / sizeof pts[0]), g_sim_issues);
337 /* the second output slot set: with one set (mutant 4) a warp that reaches the next round before warp 0 has stored
338 * this one overwrites its slot. The ascending warp order hides it (warp 0 always runs first), the descending and
339 * shuffled orders must catch it; the production kernel must stay exact under all three. */
340 {
341 long hidden = -1, caught_desc = -1, caught_shuf = -1;
342 g_sim_wsched = 0; hidden = sim_i32(512, 64, 0, 1, 2, 2, 1, 4, 500u, 0);
343 g_sim_wsched = 1; caught_desc = sim_i32(512, 64, 0, 1, 2, 2, 1, 4, 500u, 0);
344 g_sim_wsched = 12345u; caught_shuf = sim_i32(512, 64, 0, 1, 2, 2, 1, 4, 500u, 0);
345 g_sim_wsched = 0;
346 CHECK(caught_desc != 0, "sim mutant 4 (one slot set) not caught under the descending warp order");
347 CHECK(caught_shuf != 0, "sim mutant 4 (one slot set) not caught under the shuffled warp order");
348 printf("sim: mutant 4 (one slot set): ascending warp order %ld rows differ (the order that hid it), descending %ld, shuffled %ld\n",
349 hidden, caught_desc, caught_shuf);
350 }
351 /* the unit clamp: without it (mutant 5) the warps of a partial last round read past the tensor (the simulator refuses
352 * the access, -1) or store wrong rows */
353 {
354 long bad = sim_i32(1024, 18, 0, 1, 5, 2, 1, 5, 501u, 0);
355 CHECK(bad != 0, "sim mutant 5 (no unit clamp) not caught on a partial last round");
356 if (bad != 0) printf("sim: mutant 5 (no unit clamp) caught (%s)\n", bad < 0 ? "out-of-range access refused" : "rows differ");
357 }
358 /* extremes: x all -128 / 127 with all-+1 and all--1 rows */
359 {
360 const uint32_t k = 1024, n = 8;
361 int8_t *w = malloc((size_t)n * k), *x = make_x(k, 1);
362 for (uint32_t r = 0; r < n; r++) for (uint32_t c = 0; c < k; c++) w[(size_t)r * k + c] = (int8_t)((r & 1) ? -1 : 1);
363 for (int xv = 0; xv < 2; xv++) {
364 for (uint32_t c = 0; c < k; c++) x[c] = (int8_t)(xv ? 127 : -128);
365 int32_t *ref = oracle(w, n, k, x); uint32_t y[8]; char msg[240];
366 OmegaTernGemvSpec sp = { k, 1, 4, 1, 4, 0, 0 };
367 int e = sim_run(&sp, n, w, x, NULL, NULL, 1.0f, 1, 0, y, msg, sizeof msg);
368 size_t bad = 0; for (uint32_t i = 0; i < n && !e; i++) if ((int32_t)y[i] != ref[i]) bad++;
369 CHECK(!e && !bad, "sim extremes x=%d: %s, %zu rows differ (want %d / %d)", xv ? 127 : -128, e ? msg : "ok", bad, ref[0], ref[1]);
370 free(ref);
371 }
372 free(w); free(x);
373 }
374 /* mutants 1 and 3 must be caught */
375 for (int m = 1; m <= 3; m += 2) {
376 long bad = sim_i32(1024, 16, 1, 2, 2, 2, 1, m, 900u + (uint32_t)m, 0);
377 CHECK(bad > 0, "sim mutant %d not caught (%ld rows differ)", m, bad);
378 printf("sim: mutant %d caught (%ld of 16 rows differ)\n", m, bad);
379 }
380 /* f32 epilogue: q|k|v-shaped segments with three different scales, small rows: 48 rows split 32 | 8 | 8 */
381 {
382 const uint32_t k = 512, n = 48;
383 int8_t *w = make_w((size_t)n * k, 0.3, 77), *x = make_x(k, 78);
384 int32_t *ref = oracle(w, n, k, x);
385 OmegaGpuTernSegments seg = { 3, { 32, 40, 48 }, { 0.5f, 2.0f, 3.0f } };
386 float sc4[4] = { 0.5f, 2.0f, 3.0f, 0 }, a = 0.0123f, want[48];
387 uint32_t b[3], bm[3], y[48]; char msg[240];
388 b[0] = 32; b[1] = 40; b[2] = 48; /* written out, not taken from the API under test */
389 omega_gpu_tern_segment_bounds(n, &seg, 2, bm);
390 expect_f32(ref, n, b, sc4, a, want);
391 OmegaTernGemvSpec sp = { k, 1, 2, 2, 2, OMEGA_TERN_EPI_F32, 0 };
392 for (int pass = 0; pass < 2; pass++) {
393 int e = sim_run(&sp, n, w, x, pass ? bm : b, sc4, a, 2, 0, y, msg, sizeof msg);
394 size_t bad = 0; for (uint32_t i = 0; i < n && !e; i++) if (memcmp(&y[i], &want[i], 4)) bad++;
395 if (!pass) CHECK(!e && !bad, "sim f32 segments: %s, %zu of %u rows differ from ((float)y * a) * scale[seg]", e ? msg : "ok", bad, n);
396 else { CHECK(!e && bad > 0, "sim mutant 2 (r / (n / count) scale map) not caught"); printf("sim: mutant 2 caught (%zu of %u rows take the wrong scale)\n", bad, n); }
397 }
398 free(w); free(x); free(ref);
399 }
400}
401
402/* ------------------------------------------------------------- chip */
403static char g_rcpt[65536]; static size_t g_rl;
404#define RC(...) do { g_rl += (size_t)snprintf(g_rcpt + g_rl, sizeof g_rcpt - g_rl, __VA_ARGS__); } while (0)
405
406/* upload + one i32 call + oracle compare; returns rows that differ (-1 on an API error) */
407static long chip_point(uint32_t n, uint32_t k, double sp, uint32_t seed, int second, uint64_t *rows_checked) {
408 int8_t *w = make_w((size_t)n * k, sp, seed), *x = make_x(k, seed + 3);
409 int32_t *ref = oracle(w, n, k, x), *y = malloc((size_t)n * 4), *y2 = malloc((size_t)n * 4);
410 OmegaGpuTernTensor *t = NULL; long bad = -1;
411 int rc = omega_gpu_tern_tensor_upload(n, k, w, NULL, &t);
412 if (rc == 0) rc = omega_gpu_tern_gemv_i32(t, x, y, NULL);
413 if (rc != 0) printf("chip n %u k %u: %s (%s)\n", n, k, omega_gpu_tern_rc_name(rc), omega_gpu_tern_last_error());
414 else {
415 bad = 0; for (uint32_t i = 0; i < n; i++) if (y[i] != ref[i]) bad++;
416 if (rows_checked) *rows_checked += n;
417 if (second) { rc = omega_gpu_tern_gemv_i32(t, x, y2, NULL); if (rc != 0 || memcmp(y, y2, (size_t)n * 4)) bad = bad ? bad : 1, printf("chip n %u k %u: second call differs\n", n, k); }
418 }
419 omega_gpu_tern_tensor_free(t);
420 free(w); free(x); free(ref); free(y); free(y2);
421 return bad;
422}
423
424static void chip_correctness(void) {
425 uint64_t rows = 0; long bad;
426 /* 20,000 random rows: 10 tensors x 2000 rows, k and sparsity drawn per tensor */
427 uint32_t st = 0x20000u; uint64_t r_rows = 0; long r_bad = 0;
428 for (int i = 0; i < 10; i++) {
429 uint32_t k = 512u * (1u + lcg(&st) % 32u); double sp = (double)(lcg(&st) % 1000u) / 1000.0;
430 bad = chip_point(2000, k, sp, 5000u + (uint32_t)i, i == 0, &r_rows);
431 CHECK(bad == 0, "random tensor %d (2000 x %u, sparsity %.3f): %ld rows differ from oma_rz_oracle", i, k, sp, bad);
432 r_bad += bad < 0 ? 2000 : bad;
433 }
434 rows += r_rows;
435 printf("chip: random rows %" PRIu64 ", rows differing from oma_rz_oracle %ld\n", r_rows, r_bad);
436 RC(" \"random_rows\": {\"rows\": %" PRIu64 ", \"differing\": %ld},\n", r_rows, r_bad);
437 /* MA grid: width {1024, 4096, 16384} x rows {1, 64, 4096} x sparsity {0, 0.3, 0.6, 0.9}, x uniform int8 */
438 const uint32_t widths[3] = { 1024, 4096, 16384 }, mrows[3] = { 1, 64, 4096 }; const double sps[4] = { 0, 0.3, 0.6, 0.9 };
439 long g_bad = 0; int pts = 0; uint64_t g_rows = 0;
440 for (int a = 0; a < 3; a++) for (int b = 0; b < 3; b++) for (int c = 0; c < 4; c++) {
441 bad = chip_point(mrows[b], widths[a], sps[c], 7000u + (uint32_t)(a * 100 + b * 10 + c), 0, &g_rows);
442 CHECK(bad == 0, "MA grid width %u rows %u sparsity %.1f: %ld rows differ", widths[a], mrows[b], sps[c], bad);
443 g_bad += bad < 0 ? (long)mrows[b] : bad; pts++;
444 }
445 rows += g_rows;
446 printf("chip: MA grid %d points (%" PRIu64 " rows), rows differing %ld\n", pts, g_rows, g_bad);
447 RC(" \"ma_grid\": {\"points\": %d, \"rows\": %" PRIu64 ", \"differing\": %ld},\n", pts, g_rows, g_bad);
448 /* BitNet shapes, both layouts */
449 const uint32_t shp[4][2] = { { 3840, 2560 }, { 2560, 2560 }, { 13824, 2560 }, { 2560, 6912 } };
450 long s_bad = 0;
451 for (int lay = 0; lay < 2; lay++) {
452 omega_gpu_tern_set_layout((uint32_t)lay, 4);
453 for (int i = 0; i < 4; i++) {
454 bad = chip_point(shp[i][0], shp[i][1], 1.0 / 3.0, 9000u + (uint32_t)i, 1, &rows);
455 CHECK(bad == 0, "BitNet shape %ux%u layout %d: %ld rows differ", shp[i][0], shp[i][1], lay, bad);
456 s_bad += bad < 0 ? (long)shp[i][0] : bad;
457 }
458 }
459 omega_gpu_tern_set_layout(g_lay, g_brows);
460 printf("chip: BitNet shapes x 2 layouts, rows differing %ld\n", s_bad);
461 RC(" \"bitnet_shapes\": {\"layouts\": 2, \"differing\": %ld},\n", s_bad);
462 /* extremes: all +1 / all -1 rows, x all -128 and all 127, k 16384 (|y| = 2^21) */
463 {
464 const uint32_t n = 16, k = 16384;
465 int8_t *w = malloc((size_t)n * k), *x = make_x(k, 3);
466 for (uint32_t r = 0; r < n; r++) for (uint32_t c = 0; c < k; c++) w[(size_t)r * k + c] = (int8_t)((r & 1) ? -1 : 1);
467 OmegaGpuTernTensor *t = NULL; long eb = 0;
468 int rc = omega_gpu_tern_tensor_upload(n, k, w, NULL, &t);
469 for (int xv = 0; xv < 2 && rc == 0; xv++) {
470 for (uint32_t c = 0; c < k; c++) x[c] = (int8_t)(xv ? 127 : -128);
471 int32_t *ref = oracle(w, n, k, x), y[16];
472 rc = omega_gpu_tern_gemv_i32(t, x, y, NULL);
473 for (uint32_t i = 0; i < n && rc == 0; i++) if (y[i] != ref[i]) eb++;
474 free(ref);
475 }
476 CHECK(rc == 0 && eb == 0, "extremes (|y| = 128 * 16384): %s, %ld rows differ", omega_gpu_tern_rc_name(rc), eb);
477 omega_gpu_tern_tensor_free(t); free(w); free(x);
478 }
479 printf("chip: rows checked against oma_rz_oracle %" PRIu64 "\n", rows);
480}
481
482/* f32 q|k|v: three different scales at the real boundaries 2560 | 3200 | 3840; returns rows off the host formula */
483static long chip_qkv(int mutant2, int *rc_out) {
484 const uint32_t n = 3840, k = 2560;
485 int8_t *w = make_w((size_t)n * k, 1.0 / 3.0, 4242), *x = make_x(k, 4243);
486 int32_t *ref = oracle(w, n, k, x);
487 OmegaGpuTernSegments seg = { 3, { 2560, 3200, 3840 }, { 0.75f, 1.5f, 2.25f } };
488 float sc4[4] = { 0.75f, 1.5f, 2.25f, 0 }, *want = malloc((size_t)n * 4), *y = malloc((size_t)n * 4);
489 int mx = 0; for (uint32_t i = 0; i < k; i++) mx = abs(x[i]) > mx ? abs(x[i]) : mx;
490 const float a = (float)mx / 127.0f; /* 1 / s_a, s_a = 127 / max|x| */
491 const uint32_t b[3] = { 2560, 3200, 3840 }; /* written out, not taken from the API under test */
492 expect_f32(ref, n, b, sc4, a, want);
493 if (mutant2) omega_gpu_tern_test_set_mutant(2);
494 OmegaGpuTernTensor *t = NULL; long bad = -1;
495 int rc = omega_gpu_tern_tensor_upload(n, k, w, &seg, &t);
496 if (rc == 0) rc = omega_gpu_tern_gemv_f32(t, x, a, y, NULL);
497 if (rc == 0) { bad = 0; for (uint32_t i = 0; i < n; i++) if (memcmp(&y[i], &want[i], 4)) bad++; }
498 if (mutant2) omega_gpu_tern_test_set_mutant(0);
499 *rc_out = rc;
500 omega_gpu_tern_tensor_free(t); free(w); free(x); free(ref); free(want); free(y);
501 return bad;
502}
503
504static void chip_mutants(void) {
505 for (int m = 1; m <= 3; m += 2) {
506 omega_gpu_tern_test_set_mutant(m);
507 long bad = chip_point(512, 2560, 1.0 / 3.0, 31337u + (uint32_t)m, 0, NULL);
508 omega_gpu_tern_test_set_mutant(0);
509 CHECK(bad > 0, "chip mutant %d not caught (%ld rows differ)", m, bad);
510 printf("chip: mutant %d caught (%ld of 512 rows differ from oma_rz_oracle)\n", m, bad);
511 RC(" \"mutant_%d\": {\"rows\": 512, \"differing\": %ld, \"caught\": %s},\n", m, bad, bad > 0 ? "true" : "false");
512 }
513 int rc = 0;
514 long bad = chip_qkv(0, &rc);
515 CHECK(rc == 0 && bad == 0, "f32 q|k|v segments (scales 0.75 / 1.5 / 2.25): %s, %ld rows differ from ((float)y * a) * scale[seg]", omega_gpu_tern_rc_name(rc), bad);
516 printf("chip: f32 q|k|v segments, rows differing from the host formula %ld\n", bad);
517 RC(" \"f32_qkv\": {\"rows\": 3840, \"differing\": %ld},\n", bad);
518 long bm = chip_qkv(1, &rc);
519 CHECK(rc == 0 && bm > 0, "chip mutant 2 (r / (n / 3) scale map) not caught (%ld rows differ)", bm);
520 printf("chip: mutant 2 caught (%ld of 3840 rows take the wrong scale)\n", bm);
521 RC(" \"mutant_2\": {\"rows\": 3840, \"differing\": %ld, \"caught\": %s},\n", bm, bm > 0 ? "true" : "false");
522}
523
524/* cold per-launch time: `copies` uploads of the same n x k tensor, one command list reading each once, GPU stamps per
525 * launch; median over reps lists. Returns median ns (0 on failure). */
526/* --hot (diagnostic, never a gate): one tensor launched copies times, so after the first launch it is read from L2;
527 * the gap between hot and cold separates instruction issue from memory. */
528static int g_hot;
529/* --shape NxK (repeatable, diagnostic, never a gate): bandwidth at these shapes instead of the four BitNet gate shapes */
530static uint32_t g_shape[8][2]; static int g_shape_n;
531/* omega-mv#556: the GB/s gates judge the chip, so they only count when this run has the machine to itself: under
532 * `quietlock hold` (QUIETLOCK_HOLD set) or inside an exclusive `spark-window` (SPARK_WINDOW_DIR/gb10 = exclusive), or
533 * when forced with AIEN_SPEED_GATE=1. On a shared machine the numbers are still measured and printed, marked NOT JUDGED,
534 * and the receipt says "pass": null: other jobs' load moved these gates 4/68 with the old encoder too. */
535static int g_speed_judged; static char g_speed_why[96];
536static void speed_judged_init(void) {
537 const char *f = getenv("AIEN_SPEED_GATE"), *q = getenv("QUIETLOCK_HOLD"), *w = getenv("SPARK_WINDOW_DIR");
538 if (f && !strcmp(f, "1")) { g_speed_judged = 1; snprintf(g_speed_why, sizeof g_speed_why, "forced (AIEN_SPEED_GATE=1)"); return; }
539 if (q && *q) { g_speed_judged = 1; snprintf(g_speed_why, sizeof g_speed_why, "quiet hold %.60s", q); return; }
540 if (w && *w) {
541 /* Only a checked SPARK_WINDOW_DIR (src/omega_path_check.h) is read; a refused one is not an exclusive window. */
542 char d[4000], p[4096], mode[16] = {0};
543 if (omega_path_copy(d, sizeof d, w) == 0) snprintf(p, sizeof p, "%s/gb10", d); else p[0] = 0;
544 FILE *fp = p[0] ? fopen(p, "r") : NULL; if (fp) { if (!fgets(mode, sizeof mode, fp)) mode[0] = 0; fclose(fp); }
545 if (!strncmp(mode, "exclusive", 9)) { g_speed_judged = 1; snprintf(g_speed_why, sizeof g_speed_why, "exclusive spark-window"); return; }
546 }
547 g_speed_judged = 0; snprintf(g_speed_why, sizeof g_speed_why, "shared machine: no quiet hold, no exclusive spark-window");
548}
549static uint64_t cold_ns(uint32_t n, uint32_t k, uint32_t copies, uint32_t reps, uint64_t *bytes_out, uint64_t *min_out) {
550 int8_t *w = make_w((size_t)n * k, 1.0 / 3.0, 555u + n + k), *x = make_x(k, 556);
551 int32_t *ref = oracle(w, n, k, x), *y = malloc((size_t)n * 4);
552 OmegaGpuTernTensor **ts = calloc(copies, sizeof(OmegaGpuTernTensor *));
553 uint64_t *d = calloc((size_t)copies * reps, 8), med = 0; size_t nd = 0;
554 OmegaGpuCmdList *cl = NULL; OmegaGpuMem xd, yd; memset(&xd, 0, sizeof xd); memset(&yd, 0, sizeof yd);
555 int ok = 1;
556 const uint32_t nt = g_hot ? 1u : copies;
557 /* Each launch writes its own output slice (n rounded up to 8 words), all poisoned before the list and each checked against
558 * oma_rz_oracle after it, so a launch that did no work cannot hide inside the timing. */
559 const size_t ys = ((size_t)n + 7u) & ~(size_t)7u;
560 for (uint32_t i = 0; i < nt && ok; i++) if (omega_gpu_tern_tensor_upload(n, k, w, NULL, &ts[i]) != 0) ok = 0;
561 if (ok && omega_gpu_tern_gemv_i32(ts[0], x, y, NULL) != 0) ok = 0; /* builds the kernel */
562 if (ok && memcmp(y, ref, (size_t)n * 4)) { printf("cold %ux%u: reference call differs from oma_rz_oracle\n", n, k); ok = 0; }
563 if (ok && omega_gpu_cmdlist_create(copies, &cl) != 0) ok = 0;
564 omega_gpu_session_lock();
565 if (ok && (omega_gpu_session_alloc((k + 15u) & ~15u, &xd) != 0 || omega_gpu_session_alloc(ys * 4u * copies, &yd) != 0)) ok = 0;
566 omega_gpu_session_unlock();
567 if (ok) { memcpy(xd.cpu, x, k); if (omega_gpu_session_sync_to_gpu(&xd) != 0) ok = 0; }
568 if (bytes_out) *bytes_out = ok ? omega_gpu_tern_tensor_bytes(ts[0]) : 0;
569 for (uint32_t r = 0; r < reps && ok; r++) {
570 int32_t *yy = yd.cpu; for (size_t i = 0; i < ys * copies; i++) yy[i] = OMEGA_GPU_TERN_POISON_I32;
571 if (omega_gpu_session_sync_to_gpu(&yd) != 0) { ok = 0; break; }
572 omega_gpu_session_lock();
573 omega_gpu_cmdlist_reset(cl);
574 for (uint32_t i = 0; i < copies && ok; i++) if (omega_gpu_tern_gemv_record(cl, ts[g_hot ? 0 : i], xd.va, yd.va + (uint64_t)i * ys * 4u, 0) != 0) { printf("record: %s\n", omega_gpu_tern_last_error()); ok = 0; }
575 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = OMEGA_GPU_CMDLIST_BARRIER_WFI; o.stamps = 1;
576 OmegaGpuCmdListStats s; memset(&s, 0, sizeof s);
577 uint64_t *sts = calloc(copies + 1u, 8); int cnt = -1;
578 if (ok && omega_gpu_cmdlist_submit(cl, &o, &s) != 0) { printf("submit: %s\n", omega_gpu_session_last_error()); ok = 0; }
579 if (ok) cnt = omega_gpu_cmdlist_stamps(cl, sts, copies + 1u);
580 omega_gpu_session_unlock();
581 if (ok && cnt == (int)copies + 1) for (uint32_t i = 0; i < copies; i++) d[nd++] = sts[i + 1] - sts[i]; else ok = 0;
582 free(sts);
583 if (ok && omega_gpu_session_sync_to_cpu(&yd) != 0) ok = 0;
584 for (uint32_t i = 0; i < copies && ok; i++)
585 if (memcmp((const int32_t *)yd.cpu + i * ys, ref, (size_t)n * 4)) { printf("cold %ux%u: launch %u of the list differs from oma_rz_oracle\n", n, k, i); ok = 0; }
586 }
587 if (ok && nd) { qsort(d, nd, 8, cmp_u64); med = d[nd / 2]; if (min_out) *min_out = d[0]; }
588 CHECK(ok, "cold run %ux%u completed with results equal to oma_rz_oracle", n, k);
589 omega_gpu_cmdlist_free(cl);
590 omega_gpu_session_lock(); if (xd.cpu) omega_gpu_session_free(&xd); if (yd.cpu) omega_gpu_session_free(&yd); omega_gpu_session_unlock();
591 for (uint32_t i = 0; i < nt; i++) omega_gpu_tern_tensor_free(ts[i]);
592 free(ts); free(d); free(w); free(x); free(ref); free(y);
593 return ok ? med : 0;
594}
595
596static void chip_bandwidth(uint32_t reps) {
597 /* launch floor in the same list form: 32 launches of an 8 x 256 tensor */
598 speed_judged_init();
599 printf("chip: speed gates %s (%s)\n", g_speed_judged ? "JUDGED" : "NOT JUDGED", g_speed_why);
600 RC(" \"speed_judged\": %s, \"speed_judged_why\": \"%s\",\n", g_speed_judged ? "true" : "false", g_speed_why);
601 uint64_t fb = 0, fmin = 0;
602 uint64_t floor_ns = cold_ns(8, 256, 32, reps, &fb, &fmin);
603 printf("chip: in-list launch floor (8 x 256, %" PRIu64 " B) median %.2f us, min %.2f us\n", fb, floor_ns / 1e3, fmin / 1e3);
604 RC(" \"launch_floor_in_list\": {\"median_us\": %.3f, \"min_us\": %.3f},\n", floor_ns / 1e3, fmin / 1e3);
605 static const uint32_t gate_shp[4][2] = { { 3840, 2560 }, { 2560, 2560 }, { 13824, 2560 }, { 2560, 6912 } };
606 /* omega-mv#556: the 180/200 GB/s targets were never met on the chip, quiet machine included (evidence/TERN-T0-KT1-18185404-20261016T0151Z:
607 * 0 of 4, and at 2560 x 2560 the floor-counted rate cannot reach 180 even at the 273 GB/s spec). They stay printed as targets.
608 * The judged check is a regression floor over every machine-alone chip run on record: the three of 2026-10-16 (main, repeat2,
609 * repeat3) and the two exclusive-window runs of evidence/TERN-556-REGRESSION-FLOOR-20261024; floor = lowest - (highest - lowest)
610 * = 2 * min - max, GB/s minus the launch floor:
611 * 3840x2560 2*109.87-119.63 = 100.11 calc:b48ce168 2560x2560 2*126.42-136.17 = 116.67 calc:a3d96dd9
612 * 13824x2560 2*175.65-183.34 = 167.96 calc:e3e5f8ca 2560x6912 2*164.57-170.25 = 158.89 calc:6926fc37 */
613 static const double regress_floor[4] = { 100.11, 116.67, 167.96, 158.89 };
614 const uint32_t (*shp)[2] = g_shape_n ? (const uint32_t (*)[2])g_shape : gate_shp;
615 const int nshp = g_shape_n ? g_shape_n : 4;
616 RC(" \"%s\": [\n", g_hot ? "hot_diagnostic" : "cold");
617 for (int i = 0; i < nshp; i++) {
618 const uint32_t n = shp[i][0], k = shp[i][1];
619 const uint64_t bytes = (uint64_t)omega_gpu_tern_padded_rows(n) * k / 4u, need = 96ull << 20;
620 uint32_t copies = (uint32_t)((need + bytes - 1) / bytes); if (copies < 4) copies = 4;
621 uint64_t b = 0, mn = 0;
622 uint64_t ns = cold_ns(n, k, copies, reps, &b, &mn);
623 const double gbs = ns ? (double)b / (double)ns : 0, floor_gate = (n == 13824 ? 200.0 : 180.0);
624 const double kern_ns = ns > floor_ns ? (double)(ns - floor_ns) : 0;
625 printf("chip: %s %ux%u (%" PRIu64 " B x %u launches = %.1f MiB per list): %.2f us per launch -> %.1f GB/s = %.3f of 273 spec, %.3f of 237.2 practical; "
626 "minus the launch floor %.2f us -> %.1f GB/s (%.3f of 237.2)\n",
627 g_hot ? "hot (diagnostic, L2 resident)" : "cold", n, k, b, copies, (double)b * copies / 1048576.0, ns / 1e3, gbs, gbs / SPEC_GBS, gbs / PRACTICAL_GBS,
628 kern_ns / 1e3, kern_ns > 0 ? (double)b / kern_ns : 0, kern_ns > 0 ? (double)b / kern_ns / PRACTICAL_GBS : 0);
629 /* The gate is judged on the per-launch time minus the in-list launch floor. The floor is a real 8 x 256 launch (setup,
630 * x staging, barrier, one small weight read), so this is a marginal rate, not an isolated kernel time. With the floor
631 * counted, even the 273 GB/s spec cannot reach 180 GB/s at 2560 x 2560 (evidence README, calc ids there). */
632 const double kgbs = kern_ns > 0 ? (double)b / kern_ns : 0;
633 if (!g_hot && !g_shape_n && !g_speed_judged)
634 printf("chip: speed gate NOT JUDGED at %ux%u (%s): %.1f GB/s minus the floor, regression floor %.2f, target %.0f\n", n, k, g_speed_why, kgbs, regress_floor[i], floor_gate);
635 if (!g_hot && !g_shape_n && g_speed_judged) {
636 CHECK(kgbs >= regress_floor[i], "cold GB/s minus the launch floor at %ux%u: %.1f (%.1f with it), regression floor %.2f (machine-alone chip runs of 2026-10-16 and 2026-10-24)", n, k, kgbs, gbs, regress_floor[i]);
637 printf("chip: %ux%u %.1f GB/s minus the floor = %.3f of the %.0f target, %.3f of 237.2 practical (target not gated, omega-mv#556)\n", n, k, kgbs, kgbs / floor_gate, floor_gate, kgbs / PRACTICAL_GBS);
638 }
639 RC(" {\"n\": %u, \"k\": %u, \"bytes\": %" PRIu64 ", \"copies\": %u, \"median_ns\": %" PRIu64 ", \"min_ns\": %" PRIu64 ", \"gbs\": %.2f, "
640 "\"of_spec_273\": %.4f, \"of_practical_237_2\": %.4f, \"minus_floor_ns\": %.0f, \"minus_floor_gbs\": %.2f, \"minus_floor_of_spec_273\": %.4f, \"minus_floor_of_practical_237_2\": %.4f, \"target_gbs\": %.0f, \"regression_floor_gbs\": %.2f, \"gate_on\": \"minus_floor_gbs\", \"pass\": %s}%s\n",
641 n, k, b, copies, ns, mn, gbs, gbs / SPEC_GBS, gbs / PRACTICAL_GBS, kern_ns, kgbs, kgbs / SPEC_GBS, kgbs / PRACTICAL_GBS, floor_gate, g_shape_n ? 0.0 : regress_floor[i], (!g_speed_judged || g_hot || g_shape_n) ? "null" : kgbs >= regress_floor[i] ? "true" : "false", i < nshp - 1 ? "," : "");
642 }
643 RC(" ],\n");
644}
645
646int main(int argc, char **argv) {
647 int host_only = 0, sim = 0, no_bw = 0, bw_only = 0, mutant = 0; uint32_t reps = 5;
648 uint32_t layout = 99, brows = 0, rows = 0, warps = 0, depth = 0, ctas = 0;
649 const char *wmode = NULL; /* --weights cached|uncached: the weight memory type (default: the API default) */
650 const char *out = NULL;
651 for (int i = 1; i < argc; i++) {
652 if (!strcmp(argv[i], "--host-only")) host_only = 1;
653 else if (!strcmp(argv[i], "--sim")) sim = 1;
654 else if (!strcmp(argv[i], "--no-bw")) no_bw = 1;
655 else if (!strcmp(argv[i], "--bw-only")) bw_only = 1;
656 else if (!strcmp(argv[i], "--hot")) { g_hot = 1; bw_only = 1; }
657 else if (!strcmp(argv[i], "--shape") && i + 1 < argc && g_shape_n < 8) {
658 unsigned a = 0, b = 0;
659 if (sscanf(argv[++i], "%ux%u", &a, &b) != 2) { fprintf(stderr, "--shape wants NxK\n"); return 2; }
660 g_shape[g_shape_n][0] = a; g_shape[g_shape_n][1] = b; g_shape_n++; bw_only = 1;
661 }
662 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]);
663 else if (!strcmp(argv[i], "--mutant") && i + 1 < argc) mutant = atoi(argv[++i]);
664 else if (!strcmp(argv[i], "--layout") && i + 1 < argc) layout = (uint32_t)atoi(argv[++i]);
665 else if (!strcmp(argv[i], "--block-rows") && i + 1 < argc) brows = (uint32_t)atoi(argv[++i]);
666 else if (!strcmp(argv[i], "--rows") && i + 1 < argc) rows = (uint32_t)atoi(argv[++i]);
667 else if (!strcmp(argv[i], "--warps") && i + 1 < argc) warps = (uint32_t)atoi(argv[++i]);
668 else if (!strcmp(argv[i], "--depth") && i + 1 < argc) depth = (uint32_t)atoi(argv[++i]);
669 else if (!strcmp(argv[i], "--ctas") && i + 1 < argc) ctas = (uint32_t)atoi(argv[++i]);
670 else if (!strcmp(argv[i], "--weights") && i + 1 < argc) wmode = argv[++i];
671 else if (!strcmp(argv[i], "--out") && i + 1 < argc) out = argv[++i];
672 else { fprintf(stderr, "usage: %s [--host-only | --sim] [--no-bw | --bw-only] [--reps N] [--mutant M] [--layout L] [--block-rows R] [--rows R] [--warps W] [--depth D] [--ctas C] [--weights cached|uncached] [--hot] [--out receipt.json]\n", argv[0]); return 2; }
673 }
674 if (reps < 1) reps = 1;
675 if (reps > 64) reps = 64;
676 if (host_only) host_battery();
677 else if (sim) sim_battery();
678 else {
679 if (layout != 99) { g_lay = layout; g_brows = brows; omega_gpu_tern_set_layout(layout, brows); }
680 if (rows || warps || depth) omega_gpu_tern_set_shape(rows, warps, depth);
681 if (ctas) omega_gpu_tern_set_cta_budget(ctas);
682 if (wmode) omega_gpu_tern_set_weight_cache(strcmp(wmode, "uncached") != 0);
683 RC("{\n \"test\": \"gpu_tern_test\",\n \"stage\": \"TERN-T0 KT1\",\n \"mutant_forced\": %d,\n", mutant);
684 uint32_t sr = 0, sw = 0, sd = 0, sc = 0; omega_gpu_tern_shape_for(2560, g_lay, g_brows ? g_brows : 4, &sr, &sw, &sd, &sc);
685 RC(" \"shape_k2560\": {\"layout\": %u, \"rows\": %u, \"warps\": %u, \"depth\": %u, \"ctas\": %u},\n", g_lay, sr, sw, sd, sc);
686 if (mutant) {
687 if (mutant == 2) { int rc = 0; long bad = chip_qkv(1, &rc); /* the f32 q|k|v test with the r / (n / 3) map forced */
688 CHECK(rc == 0 && bad == 0, "f32 q|k|v with mutant 2 forced: %s, %ld of 3840 rows differ from ((float)y * a) * scale[seg]", omega_gpu_tern_rc_name(rc), bad);
689 RC(" \"forced_mutant_2_differing\": %ld,\n", bad); }
690 else { omega_gpu_tern_test_set_mutant(mutant); chip_correctness(); omega_gpu_tern_test_set_mutant(0); }
691 } else {
692 if (!bw_only) { chip_correctness(); chip_mutants(); }
693 if (!no_bw) chip_bandwidth(reps);
694 }
695 RC(" \"checks\": %d, \"failed\": %d, \"verdict\": \"%s\"\n}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
696 if (out) { FILE *f = create_0644(out); if (f) { fputs(g_rcpt, f); fclose(f); printf("receipt: %s\n", out); } else CHECK(0, "cannot write %s", out); }
697 }
698 printf("gpu_tern_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
699 return g_failed ? 1 : 0;
700}