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 / evidence / WIDEPF-460-CHIP-7e041401 / widepf_repro.c

125 lines · 7291 bytes Raw Blame History
1/* widepf_repro: omega #460 on the chip. The wide matmul (N 151936, lm_head width) at row counts whose
2 * 16-row tile count is even. Pre-fix (df9bd9c4) the shape rule picked rt > 1, which the blocked generator
3 * refuses (C store offset past the 24-bit STG immediate) -> CODEGEN_FAIL. Post-fix (7e041401) rt steps down.
4 *
5 * Per shape: shape rule (rt x ct), call 1 (rc, grid, launches, chip ns), call 2 (cache hit, bit-identical
6 * to call 1), host reference (bf16-rounded inputs, double accumulation, rel to sum|terms|, bad if >= 1e-5:
7 * the documented contract of omega_gpu_matmul_api.h:100-102), poison survivors.
8 * Instrument negative controls: (a) one perturbed output element must be flagged by the same checker;
9 * (b) --mutant shapes (API mutant kernel: last K step dropped) must be flagged by the checker.
10 * The API's own single-thread oracle is switched off (minutes per shape); the reference here is the same
11 * formula, threaded. Links the candidate's build/libomega_gpu.a unchanged.
12 * usage: widepf_repro "m,k,n;..." [--mutant "m,k,n;..."] */
13#include "omega_gpu_matmul_api.h"
14#include "omega_blackwell_codegen.h"
15#include "omega_blackwell_matmul.h"
16#include <inttypes.h>
17#include <math.h>
18#include <pthread.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <time.h>
23
24static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
25static void fill(float *a, size_t n, uint32_t *seed) { for (size_t i = 0; i < n; i++) a[i] = ((float)(lcg(seed) % 2001) - 1000.0f) / 1000.0f; }
26static float bf16r(float f) { uint16_t h = omega_fp32_to_bf16(f); uint32_t u = (uint32_t)h << 16; float o; memcpy(&o, &u, 4); return o; }
27static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
28
29typedef struct { uint32_t m, k, n, r0, r1; const float *ar, *bt, *c; double worst; size_t bad, poison; } Job;
30
31static void *ref_rows(void *p) {
32 Job *j = p;
33 for (uint32_t i = j->r0; i < j->r1; i++) {
34 const float *ai = j->ar + (size_t)i * j->k;
35 for (uint32_t c = 0; c < j->n; c++) {
36 const float *bc = j->bt + (size_t)c * j->k;
37 double acc = 0.0, scale = 0.0;
38 for (uint32_t t = 0; t < j->k; t++) { double pr = (double)ai[t] * (double)bc[t]; acc += pr; scale += fabs(pr); }
39 float got = j->c[(size_t)i * j->n + c];
40 if (got == -12345.0f) j->poison++;
41 double rel = fabs(acc - (double)got) / (scale > 1e-6 ? scale : 1e-6);
42 if (!(rel < 1e-5)) j->bad++;
43 if (rel > j->worst || rel != rel) j->worst = rel;
44 }
45 }
46 return NULL;
47}
48
49#define NT 16
50static void check(uint32_t m, uint32_t k, uint32_t n, const float *ar, const float *bt, const float *c, double *worst, size_t *bad, size_t *poison) {
51 pthread_t th[NT]; Job jb[NT];
52 for (int t = 0; t < NT; t++) {
53 jb[t] = (Job){ m, k, n, (uint32_t)((uint64_t)m * t / NT), (uint32_t)((uint64_t)m * (t + 1) / NT), ar, bt, c, 0.0, 0, 0 };
54 pthread_create(&th[t], NULL, ref_rows, &jb[t]);
55 }
56 *worst = 0; *bad = 0; *poison = 0;
57 for (int t = 0; t < NT; t++) { pthread_join(th[t], NULL); if (jb[t].worst > *worst) *worst = jb[t].worst; *bad += jb[t].bad; *poison += jb[t].poison; }
58}
59
60static int failed;
61
62static void run(const char *tok, int mutant) {
63 unsigned m, k, n;
64 if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); failed++; return; }
65 size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n;
66 float *a = malloc(na * 4), *b = malloc(nb * 4), *c = malloc(nc * 4), *c2 = malloc(nc * 4), *ar = malloc(na * 4), *bt = malloc(nb * 4);
67 if (!a || !b || !c || !c2 || !ar || !bt) { printf("out of host memory %s\n", tok); failed++; return; }
68 uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n;
69 fill(a, na, &seed); fill(b, nb, &seed);
70 for (size_t i = 0; i < na; i++) ar[i] = bf16r(a[i]);
71 for (size_t t = 0; t < k; t++) for (size_t j = 0; j < n; j++) bt[j * k + t] = bf16r(b[t * n + j]);
72 uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u, rt = 0, ct = 0;
73 omega_matmul_frag_block_shape(mt, nt, &rt, &ct);
74 for (size_t i = 0; i < nc; i++) c[i] = c2[i] = -12345.0f;
75 omega_gpu_matmul_test_set_mutant(mutant);
76 OmegaGpuMatmulInfo i1, i2; memset(&i1, 0, sizeof i1); memset(&i2, 0, sizeof i2);
77 double t0 = now_ms();
78 int rc = omega_gpu_matmul_f32(m, k, n, a, b, c, &i1);
79 double w1 = now_ms() - t0;
80 char err1[200]; snprintf(err1, sizeof err1, "%s", rc ? omega_gpu_matmul_last_error() : "");
81 int rc2 = rc == OMEGA_GPU_MATMUL_OK ? omega_gpu_matmul_f32(m, k, n, a, b, c2, &i2) : rc;
82 size_t rep = 0;
83 if (rc == OMEGA_GPU_MATMUL_OK && rc2 == OMEGA_GPU_MATMUL_OK) for (size_t i = 0; i < nc; i++) if (memcmp(&c[i], &c2[i], 4)) rep++;
84 double worst = -1; size_t bad = 0, poison = 0, nbad = 0; double nworst = 0;
85 double tr = now_ms();
86 if (rc == OMEGA_GPU_MATMUL_OK) {
87 check(m, k, n, ar, bt, c, &worst, &bad, &poison);
88 /* negative control (a): perturb one element of a copy, re-check that row only */
89 size_t r = m / 2, col = n / 2;
90 float save = c[r * n + col]; c[r * n + col] = save + 1.0f;
91 Job one = { m, k, n, (uint32_t)r, (uint32_t)r + 1, ar, bt, c, 0.0, 0, 0 };
92 ref_rows(&one); nbad = one.bad; nworst = one.worst;
93 c[r * n + col] = save;
94 }
95 double ref_ms = now_ms() - tr;
96 int pass;
97 if (mutant) pass = rc == OMEGA_GPU_MATMUL_OK && bad > 0;
98 else pass = rc == OMEGA_GPU_MATMUL_OK && rc2 == OMEGA_GPU_MATMUL_OK && i2.kernel_cache_hit && rep == 0 && bad == 0 && poison == 0 && nbad == 1;
99 if (!pass) failed++;
100 printf("%s %s %ux%ux%u mt=%u nt=%u rule_rt=%u rule_ct=%u rc=%s err=\"%s\" launches=%u rows_per_call=%u grid=%ux%u chip_ns=%" PRIu64
101 " call_ms=%.1f wall_ms=%.1f rc2=%s hit2=%d repeat_mismatch=%zu ref_bad=%zu ref_max_rel=%.3g poison_left=%zu negctl_bad=%zu negctl_rel=%.3g ref_ms=%.0f\n",
102 mutant ? (pass ? "MUTANT_CAUGHT" : "MUTANT_MISSED") : (pass ? "PASS" : "FAIL"), mutant ? "mutant" : "normal", m, k, n, mt, nt, rt, ct,
103 omega_gpu_matmul_rc_name(rc), err1, i1.chip_calls, i1.rows_per_call, i1.grid_x, i1.grid_y, i1.elapsed_ns, i1.call_ns / 1e6, w1,
104 omega_gpu_matmul_rc_name(rc2), i2.kernel_cache_hit, rep, bad, worst, poison, nbad, nworst, ref_ms);
105 fflush(stdout);
106 omega_gpu_matmul_test_set_mutant(0);
107 free(a); free(b); free(c); free(c2); free(ar); free(bt);
108}
109
110int main(int argc, char **argv) {
111 if (argc < 2) { fprintf(stderr, "usage: %s \"m,k,n;...\" [--mutant \"m,k,n;...\"]\n", argv[0]); return 2; }
112 omega_gpu_matmul_set_oracle(0);
113 for (int a = 1; a < argc; a++) {
114 int mutant = 0;
115 if (strcmp(argv[a], "--mutant") == 0 && a + 1 < argc) { mutant = 1; a++; }
116 char *copy = strdup(argv[a]), *save = NULL;
117 for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) {
118 run(tok, mutant);
119 if (omega_gpu_matmul_is_blocked()) { printf("device latched after an uncertain completion; stopping\n"); free(copy); return 3; }
120 }
121 free(copy);
122 }
123 printf("WIDEPF_REPRO: %s (failed %d)\n", failed ? "FAIL" : "PASS", failed);
124 return failed ? 1 : 0;
125}