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 / q38-stress-gb10-20261120T143359Z / q38_stress_bench.c

220 lines · 12814 bytes Raw Blame History
1#include "omega_gpu_cmdlist.h"
2#include "omega_gpu_q38_graph.h"
3#include "omega_gpu_fp8_api.h"
4#include "omega_gpu_bf16_gemv_api.h"
5#include "omega_gpu_session.h"
6#include <inttypes.h>
7#include <math.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define H OMEGA_GPU_Q38_HIDDEN
13#define I OMEGA_GPU_Q38_INTER
14#define VOCAB 248320u
15#define LAYERS 4u
16#define MASK 0x8ull /* 3 GDN (0,1,2) + 1 full attention (3) */
17#define MAX_HEADS 8u
18#define MAX_POS 256u
19#define EPS 1e-6f
20#define SCALE_2M64 5.42101086242752217e-20f
21
22static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
23static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
24static 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); }
25static float bf16_f32(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; memcpy(&f, &x, 4); return f; }
26static float e4m3_f32(uint8_t u) {
27 int s = (u >> 7) & 1, e = (u >> 3) & 0xf, m = u & 7;
28 float v = e == 0 ? ldexpf((float)m, -9) : ldexpf((float)(8 + m), e - 10);
29 return s ? -v : v;
30}
31static 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; }
32static uint64_t median_u64(uint64_t *v, unsigned n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; }
33
34static int fp8_random(uint32_t seed, uint32_t n, uint32_t k, OmegaGpuFp8Tensor **out) {
35 size_t wb = (size_t)n * k, sb = (size_t)(n / 128) * (k / 128);
36 uint8_t *w = malloc(wb); uint16_t *s = malloc(sb * 2);
37 uint32_t st = seed;
38 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); }
39 const float sc = 1.5f / sqrtf((float)k);
40 for (size_t i = 0; i < sb; i++) s[i] = bf16_rne(frand(&st, 0.5f * sc, sc));
41 int rc = omega_gpu_fp8_tensor_upload(1, n, k, w, s, out);
42 free(w); free(s); return rc;
43}
44static int bf16_random(uint32_t seed, uint32_t n, uint32_t k, float lo, float hi, float mul, OmegaGpuBf16Tensor **out, uint16_t **keep) {
45 uint16_t *w = malloc((size_t)n * k * 2);
46 uint32_t st = seed;
47 for (size_t i = 0; i < (size_t)n * k; i++) w[i] = bf16_rne(frand(&st, lo, hi) * mul);
48 int rc = omega_gpu_bf16_tensor_upload(n, k, w, out);
49 if (keep) *keep = w; else free(w);
50 return rc;
51}
52static int build_exact_screen(const uint16_t *lm_w, OmegaGpuFp8Tensor **screen_out, float **coef_out) {
53 const uint32_t nb = VOCAB / 128u, kb = H / 128u;
54 uint8_t *qw = malloc((size_t)VOCAB * H);
55 uint16_t *qs = malloc((size_t)nb * kb * 2);
56 float *coef = calloc(VOCAB, 4);
57 for (uint32_t b0 = 0; b0 < nb; b0++) {
58 for (uint32_t b1 = 0; b1 < kb; b1++) {
59 float amax = 0.0f;
60 for (uint32_t r = 0; r < 128; r++) {
61 const uint16_t *row = lm_w + (size_t)(b0 * 128 + r) * H + b1 * 128;
62 for (uint32_t c = 0; c < 128; c++) { float v = fabsf(bf16_f32(row[c])); if (v > amax) amax = v; }
63 }
64 float sc = amax > 0.0f ? amax / 448.0f : 1e-6f;
65 uint16_t sc16 = bf16_rne(sc);
66 sc = bf16_f32(sc16);
67 qs[(size_t)b0 * kb + b1] = sc16;
68 float inv = 1.0f / sc;
69 for (uint32_t r = 0; r < 128; r++) {
70 const uint16_t *row = lm_w + (size_t)(b0 * 128 + r) * H + b1 * 128;
71 uint8_t *dst = qw + (size_t)(b0 * 128 + r) * H + b1 * 128;
72 double err2 = 0.0;
73 for (uint32_t c = 0; c < 128; c++) {
74 float vf = bf16_f32(row[c]), qf = vf * inv;
75 int neg = qf < 0.0f; float a = fabsf(qf);
76 uint8_t best = 0; float bd = a;
77 for (uint8_t code = 1; code < 127; code++) {
78 float ev = e4m3_f32(code); float d = fabsf(ev - a);
79 if (d < bd) { bd = d; best = code; }
80 }
81 uint8_t enc = (uint8_t)(best | (neg ? 0x80u : 0u));
82 dst[c] = enc;
83 double diff = (double)vf - (double)e4m3_f32(enc) * (double)sc;
84 err2 += diff * diff;
85 }
86 coef[b0 * 128 + r] += (float)err2;
87 }
88 }
89 }
90 for (uint32_t r = 0; r < VOCAB; r++) coef[r] = sqrtf(coef[r]) * 1.01f;
91 int rc = omega_gpu_fp8_tensor_upload(1, VOCAB, H, qw, qs, screen_out);
92 free(qw); free(qs); *coef_out = coef;
93 return rc;
94}
95
96static void print_ptimer(const char *label, OmegaGpuQ38Graph *g) {
97 static uint64_t st[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1];
98 static uint8_t kd[OMEGA_GPU_CMDLIST_MAX_LAUNCHES];
99 int n = omega_gpu_q38_graph_stamps(g, st, kd, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1);
100 double sum[OMEGA_GPU_Q38_K_COUNT] = { 0 };
101 uint32_t cnt[OMEGA_GPU_Q38_K_COUNT] = { 0 };
102 double total = 0, trunk = 0;
103 for (int i = 0; i < n; i++) {
104 double us = (double)(st[i + 1] - st[i]) / 1e3;
105 sum[kd[i]] += us; cnt[kd[i]]++; total += us;
106 if (kd[i] != OMEGA_GPU_Q38_K_LM_HEAD && kd[i] != OMEGA_GPU_Q38_K_ARGMAX) trunk += us;
107 }
108 printf("PTIMER [%s]: launches=%d total=%.1f us trunk=%.1f us\n", label, n, total, trunk);
109 for (uint8_t k = 0; k < OMEGA_GPU_Q38_K_COUNT; k++)
110 if (cnt[k]) printf(" %-10s %3u launches %9.1f us\n", omega_gpu_q38_graph_kind_name(k), cnt[k], sum[k]);
111}
112
113int main(void) {
114 struct { OmegaGpuFp8Tensor *mix, *o, *gu, *dn; OmegaGpuBf16Tensor *ab; float in_n[H], po_n[H]; } L[LAYERS];
115 uint32_t seed = 1337;
116 for (uint32_t l = 0; l < LAYERS; l++) {
117 int attn = (int)((MASK >> l) & 1u);
118 if (fp8_random(seed++, attn ? 14336u : 16384u, H, &L[l].mix) != 0) return 2;
119 if (fp8_random(seed++, H, 6144u, &L[l].o) != 0) return 2;
120 if (fp8_random(seed++, 2 * I, H, &L[l].gu) != 0) return 2;
121 if (fp8_random(seed++, H, I, &L[l].dn) != 0) return 2;
122 L[l].ab = NULL;
123 if (!attn && bf16_random(seed++, 96u, H, -0.5f, 0.5f, SCALE_2M64, &L[l].ab, NULL) != 0) return 2;
124 for (unsigned i = 0; i < H; i++) { L[l].in_n[i] = frand(&seed, 0.5f, 1.5f); L[l].po_n[i] = frand(&seed, 0.5f, 1.5f); }
125 }
126 OmegaGpuBf16Tensor *lm = NULL; uint16_t *lm_w = NULL;
127 if (bf16_random(seed++, VOCAB, H, -0.05f, 0.05f, 1.0f, &lm, &lm_w) != 0) return 2;
128 OmegaGpuFp8Tensor *screen = NULL; float *coef = NULL;
129 if (build_exact_screen(lm_w, &screen, &coef) != 0) return 2;
130 free(lm_w);
131 float fnorm[H]; for (unsigned i = 0; i < H; i++) fnorm[i] = frand(&seed, 0.5f, 1.5f);
132
133 OmegaGpuGdn *gdn[MAX_HEADS]; OmegaGpuDecodeAttn256Cache *att[MAX_HEADS];
134 for (uint32_t h = 0; h < MAX_HEADS; h++) {
135 if (omega_gpu_gdn_alloc(3, &gdn[h]) != 0) return 3;
136 if (omega_gpu_decode_attn256_cache_alloc(1, MAX_POS, 10000000.0, OMEGA_GPU_Q38_ROTARY, &att[h]) != 0) return 3;
137 float conv[OMEGA_GDN_CONV_DIM * 4], alog[48], dt[48], nw[128], qn[256], kn[256];
138 for (size_t i = 0; i < sizeof conv / 4; i++) conv[i] = frand(&seed, -0.25f, 0.25f);
139 for (unsigned i = 0; i < 48; i++) { alog[i] = frand(&seed, -2.0f, 0.2f); dt[i] = frand(&seed, -1.0f, 1.0f); }
140 for (unsigned i = 0; i < 128; i++) nw[i] = frand(&seed, 0.5f, 1.5f);
141 for (uint32_t g = 0; g < 3; g++) omega_gpu_gdn_set_layer(gdn[h], g, conv, alog, dt, nw);
142 for (unsigned i = 0; i < 256; i++) { qn[i] = frand(&seed, 0.5f, 1.5f); kn[i] = frand(&seed, 0.5f, 1.5f); }
143 omega_gpu_decode_attn256_set_norms(att[h], 0, qn, kn);
144 }
145
146 OmegaGpuQ38GraphConfig cfg; memset(&cfg, 0, sizeof cfg);
147 cfg.layers = LAYERS; cfg.attn_mask = MASK; cfg.vocab = VOCAB; cfg.eps = EPS; cfg.eps_l2 = EPS;
148 cfg.gdn = gdn[0]; cfg.attn = att[0]; cfg.lm_head = lm;
149 OmegaGpuQ38Graph *g = NULL;
150 if (omega_gpu_q38_graph_create_heads(&cfg, MAX_HEADS, &g) != 0) { printf("create_heads fail: %s\n", omega_gpu_session_last_error()); return 4; }
151 for (uint32_t h = 0; h < MAX_HEADS; h++) omega_gpu_q38_graph_set_head_state(g, h, gdn[h], att[h]);
152 for (uint32_t l = 0; l < LAYERS; l++) omega_gpu_q38_graph_set_layer(g, l, L[l].mix, L[l].ab, L[l].o, L[l].gu, L[l].dn, L[l].in_n, L[l].po_n);
153 omega_gpu_q38_graph_set_final_norm(g, fnorm);
154 if (omega_gpu_q38_graph_set_exact_head(g, screen, coef, 10384u) != 0) return 5;
155 omega_gpu_q38_graph_set_exact_head_on(g, 0);
156
157 float *x = malloc((size_t)MAX_HEADS * H * 4);
158 for (size_t i = 0; i < (size_t)MAX_HEADS * H; i++) x[i] = frand(&seed, -1.0f, 1.0f);
159 uint32_t pos[MAX_HEADS] = { 1, 1, 1, 1, 1, 1, 1, 1 }, tok[MAX_HEADS] = { 0 }, tok_ref[MAX_HEADS] = { 0 };
160
161 /* Parity check: N=8 batched exact_head vs plain bf16 */
162 omega_gpu_q38_graph_set_batch(g, 0); omega_gpu_q38_graph_set_exact_head_on(g, 0);
163 for (uint32_t h = 0; h < MAX_HEADS; h++) omega_gpu_gdn_reset(gdn[h]);
164 if (omega_gpu_q38_graph_stroke(g, x, pos, MAX_HEADS, tok_ref, NULL) != 0) { printf("ref stroke fail: %s\n", omega_gpu_session_last_error()); return 6; }
165
166 omega_gpu_q38_graph_set_batch(g, 1); omega_gpu_q38_graph_set_exact_head_on(g, 0);
167 for (uint32_t h = 0; h < MAX_HEADS; h++) omega_gpu_gdn_reset(gdn[h]);
168 if (omega_gpu_q38_graph_stroke(g, x, pos, MAX_HEADS, tok, NULL) != 0) { printf("bat plain fail: %s\n", omega_gpu_session_last_error()); return 6; }
169 int bad_plain = 0; for (uint32_t h = 0; h < MAX_HEADS; h++) if (tok[h] != tok_ref[h]) bad_plain++;
170 printf("PARITY N=8 batched plain bf16 (XB=4) vs unbatched: %d/8 exact\n", 8 - bad_plain);
171
172 omega_gpu_q38_graph_set_batch(g, 1); omega_gpu_q38_graph_set_exact_head_on(g, 1);
173 for (uint32_t h = 0; h < MAX_HEADS; h++) omega_gpu_gdn_reset(gdn[h]);
174 if (omega_gpu_q38_graph_stroke(g, x, pos, MAX_HEADS, tok, NULL) != 0) { printf("bat exact fail: %s\n", omega_gpu_session_last_error()); return 6; }
175 int bad_ex = 0; for (uint32_t h = 0; h < MAX_HEADS; h++) if (tok[h] != tok_ref[h]) bad_ex++;
176 printf("PARITY N=8 batched exact_head vs plain bf16: %d/8 exact\n", 8 - bad_ex);
177 if (bad_plain || bad_ex) return 7;
178
179 /* Sweep N = 1, 2, 4, 6, 8: plain bf16 unbatched vs batched + exact_head batched */
180 const uint32_t ns[] = { 1, 2, 4, 6, 8 };
181 for (unsigned ni = 0; ni < 5; ni++) {
182 uint32_t N = ns[ni];
183 uint64_t tu[7], tb[7], te[7];
184 omega_gpu_q38_graph_set_exact_head_on(g, 0); omega_gpu_q38_graph_set_batch(g, 0);
185 for (unsigned r = 0; r < 7; r++) { OmegaGpuDecodeGraphStats st; omega_gpu_q38_graph_stroke(g, x, pos, N, tok, &st); tu[r] = st.chip_ns; }
186 omega_gpu_q38_graph_set_batch(g, 1);
187 for (unsigned r = 0; r < 7; r++) { OmegaGpuDecodeGraphStats st; omega_gpu_q38_graph_stroke(g, x, pos, N, tok, &st); tb[r] = st.chip_ns; }
188 omega_gpu_q38_graph_set_exact_head_on(g, 1);
189 for (unsigned r = 0; r < 7; r++) { OmegaGpuDecodeGraphStats st; omega_gpu_q38_graph_stroke(g, x, pos, N, tok, &st); te[r] = st.chip_ns; }
190 printf("SWEEP N=%u: unb_bf16=%.3f ms | bat_bf16=%.3f ms (%.2fx) | bat_exact=%.3f ms (%.2fx)\n",
191 N, median_u64(tu, 7)/1e6, median_u64(tb, 7)/1e6, (double)median_u64(tu,7)/(double)median_u64(tb,7),
192 median_u64(te, 7)/1e6, (double)median_u64(tu,7)/(double)median_u64(te,7));
193 }
194
195 /* PTIMER breakdowns */
196 omega_gpu_q38_graph_set_stamps(g, 1);
197 omega_gpu_q38_graph_set_batch(g, 0); omega_gpu_q38_graph_set_exact_head_on(g, 0);
198 omega_gpu_q38_graph_stroke(g, x, pos, 1, tok, NULL); print_ptimer("N=1 unb plain_bf16", g);
199 omega_gpu_q38_graph_set_exact_head_on(g, 1);
200 omega_gpu_q38_graph_stroke(g, x, pos, 1, tok, NULL); print_ptimer("N=1 unb exact_head", g);
201 omega_gpu_q38_graph_set_batch(g, 1); omega_gpu_q38_graph_set_exact_head_on(g, 0);
202 omega_gpu_q38_graph_stroke(g, x, pos, 8, tok, NULL); print_ptimer("N=8 bat plain_bf16", g);
203 omega_gpu_q38_graph_set_exact_head_on(g, 1);
204 omega_gpu_q38_graph_stroke(g, x, pos, 8, tok, NULL); print_ptimer("N=8 bat exact_head", g);
205 omega_gpu_q38_graph_set_stamps(g, 0);
206
207 /* Sustained 64 x 8 stress (batched + exact_head ON) */
208 uint64_t ts[64], sum_ns = 0;
209 for (unsigned s = 0; s < 64; s++) {
210 uint32_t ps[MAX_HEADS]; for (uint32_t h = 0; h < MAX_HEADS; h++) ps[h] = 2u + (s % 64u);
211 OmegaGpuDecodeGraphStats st;
212 if (omega_gpu_q38_graph_stroke(g, x, ps, MAX_HEADS, tok, &st) != 0) { printf("stress step %u fail\n", s); return 8; }
213 ts[s] = st.chip_ns; sum_ns += st.chip_ns;
214 }
215 qsort(ts, 64, sizeof *ts, cmp_u64);
216 printf("STRESS 64x8 (bat_exact): min=%.3f p50=%.3f p95=%.3f max=%.3f mean=%.3f ms (%.2f tok/s)\n",
217 ts[0]/1e6, ts[32]/1e6, ts[60]/1e6, ts[63]/1e6, (sum_ns/64.0)/1e6, 8.0 / ((sum_ns/64.0)/1e9));
218 printf("Q38_STRESS_BENCH: PASS\n");
219 return 0;
220}