interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / gpu_tc_probe_test.c

202 lines · 14032 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* gpu_tc_probe_test.c (OWN-30B T3, 2026-10-13): characterise the chip's HMMA.16816.F32.BF16 arithmetic.
3 * --host-only model self-checks (the model family reproduces exact sums on exactly representable cases)
4 * (default) chip: CASES single-HMMA cases (one warp each) with operand classes chosen to separate the models
5 * (wide exponent spread, cancellation, accumulator dominance, bf16 subnormals); prints, per model of
6 * tests/bw_hmma_model.h, how many of the 128 * CASES outputs match the chip bit for bit; negative control:
7 * mutant 1 (C dropped) must differ on the cases with C != 0.
8 * Env TC_PROBE_SEED: a different random data set (confirmation runs). Verdict line: "TC_PROBE: model=... exact N/N" when one model matches every output, else "TC_PROBE: NO EXACT MODEL".
9 * Chip runs only under flock /tmp/aien-gb10.lock. */
10#include "omega_blackwell_codegen_tc.h"
11#include "omega_gpu_session.h"
12#include "bw_hmma_model.h"
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include "test_out_file.h"
17
18#define CASES 2048u
19
20static uint64_t rng = 0x9e3779b97f4a7c15ull;
21static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); }
22static uint16_t bf16_of(float f) { uint32_t u; memcpy(&u, &f, 4); return (uint16_t)(u >> 16); } /* truncation: the value is then exactly a bf16 */
23static float f_of_bf16(uint16_t h) { uint32_t u = (uint32_t)h << 16; float f; memcpy(&f, &u, 4); return f; }
24/* a random bf16 with exponent in [lo, hi] */
25static float rbf(int lo, int hi) {
26 float m = 1.0f + (float)(rnd() & 127) / 128.0f;
27 int e = lo + (int)(rnd() % (uint32_t)(hi - lo + 1));
28 float v = ldexpf(m, e);
29 if (rnd() & 1) v = -v;
30 return f_of_bf16(bf16_of(v));
31}
32
33typedef struct { float A[16][16], B[16][8], C[16][8]; } Case;
34
35static void gen_case(Case *c, unsigned idx) {
36 unsigned cls = idx % 6;
37 for (int r = 0; r < 16; r++) for (int k = 0; k < 16; k++) {
38 float v;
39 switch (cls) {
40 case 0: v = rbf(-2, 2); break; /* narrow range */
41 case 1: v = rbf(-20, 20); break; /* wide spread: alignment truncation shows */
42 case 2: v = (k & 1) ? rbf(-1, 1) : rbf(-12, 0); break;
43 case 3: v = rbf(-4, 4); break; /* with a big accumulator (below) */
44 case 4: v = rbf(-130, -126); break; /* bf16 near / below the normal limit (subnormal products) */
45 default: v = rbf(-8, 8); break;
46 }
47 if (cls == 4 && (rnd() & 3) == 0) { uint16_t h = (uint16_t)((rnd() & 0x7f) | (rnd() & 0x8000)); v = f_of_bf16(h); } /* bf16 subnormal */
48 c->A[r][k] = v;
49 }
50 for (int k = 0; k < 16; k++) for (int n = 0; n < 8; n++) c->B[k][n] = cls == 4 ? rbf(-2, 2) : rbf(cls == 1 ? -20 : -3, cls == 1 ? 20 : 3);
51 for (int r = 0; r < 16; r++) for (int n = 0; n < 8; n++) {
52 float v = idx % 7 == 6 ? 0.0f : rbf(-6, 6);
53 if (cls == 3) v = rbf(10, 14);
54 if (cls == 2 && (rnd() & 1)) { /* cancellation: C = -(exact sum rounded) */
55 double s = 0; for (int k = 0; k < 16; k++) s += (double)c->A[r][k] * c->B[k][n];
56 v = (float)-s;
57 }
58 c->C[r][n] = v;
59 }
60}
61static uint32_t pack2(float lo, float hi) { return (uint32_t)bf16_of(lo) | ((uint32_t)bf16_of(hi) << 16); }
62static void to_frag(const Case *c, uint32_t *in) {
63 uint32_t *A = in, *B = in + 128; float *C = (float *)(in + 192);
64 for (int L = 0; L < 32; L++) {
65 int g = L / 4, t = L % 4;
66 A[L * 4 + 0] = pack2(c->A[g][2 * t], c->A[g][2 * t + 1]);
67 A[L * 4 + 1] = pack2(c->A[g + 8][2 * t], c->A[g + 8][2 * t + 1]);
68 A[L * 4 + 2] = pack2(c->A[g][2 * t + 8], c->A[g][2 * t + 9]);
69 A[L * 4 + 3] = pack2(c->A[g + 8][2 * t + 8], c->A[g + 8][2 * t + 9]);
70 B[L * 2 + 0] = pack2(c->B[2 * t][g], c->B[2 * t + 1][g]);
71 B[L * 2 + 1] = pack2(c->B[2 * t + 8][g], c->B[2 * t + 9][g]);
72 C[L * 4 + 0] = c->C[g][2 * t]; C[L * 4 + 1] = c->C[g][2 * t + 1];
73 C[L * 4 + 2] = c->C[g + 8][2 * t]; C[L * 4 + 3] = c->C[g + 8][2 * t + 1];
74 }
75}
76/* output word (lane L, q) -> (row, col) */
77static void out_rc(int L, int q, int *r, int *n) { int g = L / 4, t = L % 4; *r = g + (q >= 2 ? 8 : 0); *n = 2 * t + (q & 1); }
78
79static float model_out(const HmmaModel *m, const Case *c, int r, int n) {
80 float a[16], b[16]; for (int k = 0; k < 16; k++) { a[k] = c->A[r][k]; b[k] = c->B[k][n]; }
81 return hm_dot16(m, a, b, c->C[r][n]);
82}
83
84static int host_only(void) {
85 int fail = 0;
86 /* exactly representable sums must come out exact in every model */
87 for (int i = 0; i < 1000; i++) {
88 float a[16], b[16]; for (int k = 0; k < 16; k++) { a[k] = (float)((int)(rnd() % 17) - 8); b[k] = (float)((int)(rnd() % 9) - 4); }
89 float c = (float)((int)(rnd() % 33) - 16); double ex = c; for (int k = 0; k < 16; k++) ex += (double)a[k] * b[k];
90 int groups[] = { 0, 1, 2, 4, 8, 16 };
91 for (int gi = 0; gi < 6; gi++) for (int rn = 0; rn < 2; rn++) { HmmaModel m = { groups[gi], 0, rn, 0, 0 }; if (hm_dot16(&m, a, b, c) != (float)ex) fail++; }
92 }
93 /* truncation is visible: 1 + 2^-30 (one product) in G=16, extra 0 truncates to 1, extra 6 keeps it only in double, RZ -> 1 */
94 { float a[16] = { 1.0f, ldexpf(1.0f, -30) }, b[16] = { 1.0f, 1.0f }; HmmaModel m = { 16, 0, 0, 0, 0 }; if (hm_dot16(&m, a, b, 0.0f) != 1.0f) fail++; }
95 /* the probe's case generator produces bf16 values and its fragment packing round-trips */
96 { static Case c; gen_case(&c, 7); uint32_t in[320]; to_frag(&c, in); if ((in[0] & 0xffff) != bf16_of(c.A[0][0])) fail++; }
97 printf("host-only model self-checks: %s (%d failures)\n", fail ? "FAIL" : "PASS", fail);
98 return fail ? 1 : 0;
99}
100
101static int run_chip(int mutant, uint32_t *out_words, const uint32_t *in_words) {
102 BlackwellIRProgram *prog = calloc(1, sizeof *prog);
103 if (!prog || omega_blackwell_codegen_tc_probe_prog(mutant, prog) != 0) { printf("codegen failed\n"); free(prog); return -1; }
104 int rc = -1;
105 omega_gpu_session_lock();
106 OmegaGpuMem code = {0}, in = {0}, out = {0};
107 size_t need = omega_bw_program_code_bytes(prog);
108 OmegaBlackwellKernel k;
109 if (!omega_gpu_session_open()) { printf("open failed: %s\n", omega_gpu_session_last_error()); goto done; }
110 if (omega_gpu_session_alloc_code(need, &code) != 0 || omega_blackwell_kernel_encode_into(prog, code.cpu, need, &k) != 0 || omega_gpu_session_sync_to_gpu(&code) != 0) { printf("code upload failed\n"); goto done; }
111 if (omega_gpu_session_alloc((size_t)CASES * OMEGA_TC_PROBE_IN_BYTES, &in) != 0 || omega_gpu_session_alloc((size_t)CASES * OMEGA_TC_PROBE_OUT_BYTES, &out) != 0) { printf("alloc failed\n"); goto done; }
112 memcpy(in.cpu, in_words, (size_t)CASES * OMEGA_TC_PROBE_IN_BYTES);
113 { float *o = out.cpu; for (size_t i = 0; i < (size_t)CASES * 128u; i++) o[i] = -999.0f; }
114 if (omega_gpu_session_sync_to_gpu(&in) != 0 || omega_gpu_session_sync_to_gpu(&out) != 0) goto done;
115 {
116 uint32_t args[4] = { (uint32_t)in.va, (uint32_t)(in.va >> 32), (uint32_t)out.va, (uint32_t)(out.va >> 32) };
117 uint32_t peak = prog->regalloc.peak_gpr_usage, decl = omega_bw_gpr_declared(peak);
118 OmegaGpuLaunch L = { .code_va = code.va, .gpr_count = decl < 64 ? 64 : decl, .threads_x = 32, .threads_y = 1, .grid_x = CASES, .grid_y = 1,
119 .num_elements = (size_t)CASES * 128u, .args = args, .n_args = 4, .timeout_ms = 10000 };
120 uint64_t ns = 0;
121 if (omega_gpu_session_launch(&L, &ns, NULL) != 0) { printf("launch failed: %s\n", omega_gpu_session_last_error()); goto done; }
122 if (omega_gpu_session_sync_to_cpu(&out) != 0) goto done;
123 printf("chip launch mutant %d: peak %u declared %u, %llu ns\n", mutant, peak, L.gpr_count, (unsigned long long)ns);
124 }
125 memcpy(out_words, out.cpu, (size_t)CASES * OMEGA_TC_PROBE_OUT_BYTES);
126 rc = 0;
127done:
128 if (code.cpu) omega_gpu_session_free(&code);
129 if (in.cpu) omega_gpu_session_free(&in);
130 if (out.cpu) omega_gpu_session_free(&out);
131 omega_gpu_session_unlock();
132 free(prog);
133 return rc;
134}
135
136int main(int argc, char **argv) {
137 if (argc > 2 && strcmp(argv[1], "--dump") == 0) { /* raw code words for nvdisasm -b SM121 (offline check only) */
138 static BlackwellIRProgram p; static uint8_t buf[OMEGA_BW_CODE_CAP_MAX]; size_t len = 0;
139 if (omega_blackwell_codegen_tc_probe_prog(argc > 3 ? atoi(argv[3]) : 0, &p) != 0 || omega_bw_encode_program(&p, buf, sizeof buf, &len) != 0) return 1;
140 FILE *f = test_out_open(argv[2]); if (!f) return 1; fwrite(buf, 1, len, f); fclose(f); printf("peak %u, %zu bytes\n", p.regalloc.peak_gpr_usage, len); return 0;
141 }
142 int host = argc > 1 && strcmp(argv[1], "--host-only") == 0;
143 if (getenv("TC_PROBE_SEED")) rng = strtoull(getenv("TC_PROBE_SEED"), NULL, 0) | 1u; /* fresh data for a confirmation run */
144 if (host_only() != 0) return 1;
145 if (host) return 0;
146 static Case cases[CASES];
147 uint32_t *in = calloc((size_t)CASES, OMEGA_TC_PROBE_IN_BYTES), *out = calloc((size_t)CASES, OMEGA_TC_PROBE_OUT_BYTES), *outm = calloc((size_t)CASES, OMEGA_TC_PROBE_OUT_BYTES);
148 for (unsigned i = 0; i < CASES; i++) { gen_case(&cases[i], i); to_frag(&cases[i], in + (size_t)i * (OMEGA_TC_PROBE_IN_BYTES / 4)); }
149 const char *replay = argc > 2 && strcmp(argv[1], "--replay") == 0 ? argv[2] : NULL, *save = argc > 2 && strcmp(argv[1], "--save") == 0 ? argv[2] : NULL;
150 if (replay) { /* model search over saved chip words (offline; the words came from a sealed chip run) */
151 FILE *rf = fopen(replay, "rb"); if (!rf) return 1;
152 size_t got = fread(out, 1, (size_t)CASES * OMEGA_TC_PROBE_OUT_BYTES, rf) + fread(outm, 1, (size_t)CASES * OMEGA_TC_PROBE_OUT_BYTES, rf); fclose(rf);
153 if (got != 2u * CASES * OMEGA_TC_PROBE_OUT_BYTES) { printf("short replay file\n"); return 1; }
154 printf("replaying chip words from %s\n", replay);
155 } else {
156 if (run_chip(0, out, in) != 0) { printf("TC_PROBE: FAIL chip\n"); return 1; }
157 if (run_chip(1, outm, in) != 0) { printf("TC_PROBE: FAIL chip (mutant)\n"); return 1; }
158 if (save) { FILE *sf = test_out_open(save); if (sf) { fwrite(out, 1, (size_t)CASES * OMEGA_TC_PROBE_OUT_BYTES, sf); fwrite(outm, 1, (size_t)CASES * OMEGA_TC_PROBE_OUT_BYTES, sf); fclose(sf); } }
159 }
160 /* poison and plausibility: relative error vs the exact double sum */
161 size_t poison = 0, far = 0, total = (size_t)CASES * 128u;
162 for (unsigned i = 0; i < CASES; i++) for (int L = 0; L < 32; L++) for (int q = 0; q < 4; q++) {
163 float v; memcpy(&v, &out[(size_t)i * 128 + L * 4 + q], 4);
164 if (v == -999.0f) poison++;
165 int r, n; out_rc(L, q, &r, &n);
166 double ex = cases[i].C[r][n], mag = fabs(ex); for (int k = 0; k < 16; k++) { double p = (double)cases[i].A[r][k] * cases[i].B[k][n]; ex += p; mag += fabs(p); }
167 if (fabs(v - ex) > 1e-5 * mag + 1e-37) far++;
168 }
169 printf("plausibility: %zu poison, %zu of %zu outputs farther than 1e-5 of the term magnitude from the exact sum\n", poison, far, total);
170 /* negative control: mutant 1 drops C; it must differ wherever C is nonzero and large enough to matter */
171 size_t mdiff = 0, mcand = 0;
172 /* counted only where C is clearly significant: |C| >= 2^-8 |D| */
173 for (unsigned i = 0; i < CASES; i++) for (int L = 0; L < 32; L++) for (int q = 0; q < 4; q++) {
174 int r, n; out_rc(L, q, &r, &n); float dv; memcpy(&dv, &out[(size_t)i * 128 + L * 4 + q], 4);
175 if (fabsf(cases[i].C[r][n]) < ldexpf(fabsf(dv), -8) || cases[i].C[r][n] == 0.0f) continue;
176 mcand++; if (out[(size_t)i * 128 + L * 4 + q] != outm[(size_t)i * 128 + L * 4 + q]) mdiff++;
177 }
178 printf("negative control (C dropped): %zu of %zu outputs with significant C differ (%s)\n", mdiff, mcand, mcand && mdiff == mcand ? "caught" : "NOT CAUGHT");
179 /* model search */
180 int groups[] = { 0, 1, 2, 4, 8, 16 }, best_i = -1; size_t best = 0; HmmaModel bestm = { 0, 0, 0, 0, 0 };
181 static const char *clsname[6] = { "narrow", "wide", "mixed+cancel", "bigC", "subnormal", "medium" };
182 for (int gi = 0; gi < 6; gi++) for (int ex = -1; ex <= 8; ex++) for (int rn = 0; rn < 2; rn++) for (int nom = 0; nom < 3; nom++) for (int cs = 0; cs < 2; cs++) {
183 if (groups[gi] == 0 && (ex || rn || nom || cs)) continue;
184 HmmaModel m = { groups[gi], ex, rn, nom, cs };
185 size_t match = 0, cls_miss[6] = {0};
186 for (unsigned i = 0; i < CASES; i++) for (int L = 0; L < 32; L++) for (int q = 0; q < 4; q++) {
187 int r, n; out_rc(L, q, &r, &n);
188 float mv = model_out(&m, &cases[i], r, n); uint32_t mu; memcpy(&mu, &mv, 4);
189 if (mu == out[(size_t)i * 128 + L * 4 + q]) match++; else cls_miss[i % 6]++;
190 }
191 if (match * 100 >= total * 90 || match == total)
192 printf("model group %2d extra %d %s nominal %d csep %d: %zu of %zu match (misses by class: %s %zu, %s %zu, %s %zu, %s %zu, %s %zu, %s %zu)\n", m.group, m.extra, m.rn ? "RN" : "RZ", m.nominal, m.csep, match, total,
193 clsname[0], cls_miss[0], clsname[1], cls_miss[1], clsname[2], cls_miss[2], clsname[3], cls_miss[3], clsname[4], cls_miss[4], clsname[5], cls_miss[5]);
194 if (match > best) { best = match; bestm = m; best_i = 1; }
195 }
196 if (best_i > 0) printf("best model: group %d extra %d %s: %zu of %zu\n", bestm.group, bestm.extra, bestm.rn ? "RN" : "RZ", best, total);
197 int ok = poison == 0 && far == 0 && mcand && mdiff == mcand;
198 if (ok && best == total) printf("TC_PROBE: model=group %d extra %d %s nominal %d csep %d exact %zu/%zu\n", bestm.group, bestm.extra, bestm.rn ? "RN" : "RZ", bestm.nominal, bestm.csep, best, total);
199 else printf("TC_PROBE: NO EXACT MODEL (best %zu/%zu)%s\n", best, total, ok ? "" : " and plausibility/control FAILED");
200 free(in); free(out); free(outm);
201 return ok ? 0 : 1;
202}