interplane.Get Omega
Read in your language

omega-dnn

aien-dev/omega-dnn

Brought by drakestapleton

branch main Branches Tags History

omega-dnn / evidence / STACK7-GPU-20261026 / gpu-bench / omega_dnn_bench.c

159 lines · 10342 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * omega_dnn_bench: THROWAWAY measurement driver (lane OmegaDnn.GpuDnn, 2026-10-26), not product code.
3 * Links omega-mv e5b32567a build/libomega_gpu.a (INTERIM: C codegen emitting SASS, not .om).
4 * Usage: omega_dnn_bench OUTDIR [--mutant] [--warm 10] [--reps 50]
5 * For every predeclared case: generate inputs with a fixed LCG, write inputs + first output to OUTDIR,
6 * run `warm` untimed calls then `reps` timed calls; per timed call record the API wall time (clock_gettime
7 * around the synchronous call: staging, launch, marker wait, copy-out) and info.elapsed_ns (submit to
8 * completion marker). Every timed output is compared bitwise with the first output. One JSON line per case.
9 * --mutant: arm the library's TEST-ONLY mutant kernel for each op (negative control), one call, no timing. */
10#include "omega_gpu_elementwise_api.h"
11#include "omega_gpu_attention_api.h"
12#include "omega_gpu_session.h"
13#include <math.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <time.h>
19
20static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
21static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
22static uint16_t f2bf(float f) { uint32_t u; memcpy(&u, &f, 4); uint32_t r = ((u >> 16) & 1u) + 0x7fffu; return (uint16_t)((u + r) >> 16); }
23static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; }
24
25static const char *g_dir;
26static void dump(const char *cs, const char *what, const void *p, size_t bytes) {
27 char path[512]; snprintf(path, sizeof path, "%s/%s.%s.bin", g_dir, cs, what);
28 FILE *f = fopen(path, "wb"); if (!f || fwrite(p, 1, bytes, f) != bytes) { fprintf(stderr, "write %s failed\n", path); exit(3); } fclose(f);
29}
30
31typedef struct {
32 int kind; /* 0 rmsnorm 1 rope 2 swiglu 3 gqa 4 rows 5 paged */
33 uint32_t a, b, c, d, e; /* shape words, meaning per kind */
34 float *x, *w, *y; void *pool; uint32_t *ids; size_t out_n;
35} Case;
36
37static int call(Case *k, float *out, uint64_t *chip_ns) {
38 int rc;
39 if (k->kind <= 2) {
40 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
41 if (k->kind == 0) rc = omega_gpu_rmsnorm_f32(k->a, k->b, k->x, k->w, 1e-5f, out, &info);
42 else if (k->kind == 1) rc = omega_gpu_rope_f32(k->a, k->b, k->x, k->w, k->y, out, &info);
43 else rc = omega_gpu_swiglu_f32(k->a, k->x, k->w, out, &info);
44 *chip_ns = info.elapsed_ns; return rc;
45 }
46 OmegaGpuAttnInfo info; memset(&info, 0, sizeof info);
47 if (k->kind == 3) rc = omega_gpu_gqa_attention_f32(k->x, k->w, k->y, k->d, k->a, k->b, k->c, out, &info);
48 else if (k->kind == 4) rc = omega_gpu_gqa_attention_rows_f32(k->x, k->w, k->y, 0, k->d, k->a, k->b, k->c, out, &info);
49 else {
50 /* resident pool: block_size e, one layer */
51 OmegaGpuKvLayout ly; uint32_t hd = k->c, nkv = k->b, bs = k->e, nblk = (k->d + bs - 1) / bs;
52 ly.head_stride_bytes = (uint64_t)hd * 2; ly.token_stride_bytes = ly.head_stride_bytes * nkv;
53 ly.kv_plane_stride_bytes = ly.token_stride_bytes * bs; ly.layer_stride_bytes = 2 * ly.kv_plane_stride_bytes;
54 ly.block_stride_bytes = ly.layer_stride_bytes; ly.pool_bytes = ly.block_stride_bytes * nblk;
55 ly.num_blocks = nblk; ly.num_layers = 1; ly.block_size = bs;
56 rc = omega_gpu_paged_attention_bf16(k->x, k->pool, &ly, k->ids, nblk, k->d, 0, k->a, k->b, hd, out, &info);
57 if (rc == 0 && info.kv_source != OMEGA_GPU_ATTN_KV_RESIDENT) { fprintf(stderr, "paged call did not take the resident path\n"); return -99; }
58 }
59 *chip_ns = info.elapsed_ns; return rc;
60}
61
62int main(int argc, char **argv) {
63 if (argc < 2) { fprintf(stderr, "usage: %s OUTDIR [--mutant] [--warm N] [--reps N]\n", argv[0]); return 2; }
64 g_dir = argv[1]; int mutant = 0, warm = 10, reps = 50;
65 for (int i = 2; i < argc; i++) {
66 if (!strcmp(argv[i], "--mutant")) mutant = 1;
67 else if (!strcmp(argv[i], "--warm") && i + 1 < argc) warm = atoi(argv[++i]);
68 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]);
69 /* POST-HOC reference-only arm (not in the frozen protocol): marker-poll spin window, omega#328 */
70 else if (!strcmp(argv[i], "--spin-us") && i + 1 < argc) {
71 if (omega_gpu_session_set_spin_us((uint32_t)atoi(argv[++i])) != 0) { fprintf(stderr, "spin_us refused\n"); return 2; }
72 }
73 }
74 if (mutant) { warm = 0; reps = 0; }
75 /* predeclared cases (GPU.md section 2); names are parsed by the Python side */
76 static const struct { const char *name; int kind; uint32_t a, b, c, d, e; } C[] = {
77 { "rmsnorm_1x2048", 0, 1, 2048, 0, 0, 0 },
78 { "rmsnorm_64x2048", 0, 64, 2048, 0, 0, 0 },
79 { "rope_32x128", 1, 32, 128, 0, 0, 0 },
80 { "swiglu_5632", 2, 5632, 0, 0, 0, 0 },
81 { "swiglu_131072", 2, 131072, 0, 0, 0, 0 },
82 { "gqa_f32_32q8kv_hd128_ctx2048", 3, 32, 8, 128, 2048, 0 },
83 { "gqa_f32_32q4kv_hd64_ctx2048", 3, 32, 4, 64, 2048, 0 },
84 { "gqa_rows_f32_32q8kv_hd128_m256", 4, 32, 8, 128, 256, 0 },
85 { "paged_bf16_32q8kv_hd128_ctx2048_bs16", 5, 32, 8, 128, 2048, 16 },
86 };
87 uint32_t seed = 0x0d1ec0deu;
88 for (size_t ci = 0; ci < sizeof C / sizeof C[0]; ci++) {
89 Case k; memset(&k, 0, sizeof k);
90 k.kind = C[ci].kind; k.a = C[ci].a; k.b = C[ci].b; k.c = C[ci].c; k.d = C[ci].d; k.e = C[ci].e;
91 const char *cs = C[ci].name;
92 if (k.kind == 0) {
93 size_t n = (size_t)k.a * k.b; k.out_n = n;
94 k.x = malloc(n * 4); k.w = malloc(k.b * 4);
95 for (size_t i = 0; i < n; i++) k.x[i] = frand(&seed, -3.0f, 3.0f);
96 for (size_t i = 0; i < k.b; i++) k.w[i] = frand(&seed, 0.5f, 1.5f);
97 dump(cs, "x", k.x, n * 4); dump(cs, "w", k.w, k.b * 4);
98 } else if (k.kind == 1) {
99 size_t n = (size_t)k.a * k.b, half = k.b / 2; k.out_n = n;
100 k.x = malloc(n * 4); k.w = malloc(half * 4); k.y = malloc(half * 4);
101 for (size_t i = 0; i < n; i++) k.x[i] = frand(&seed, -1.0f, 1.0f);
102 for (size_t i = 0; i < half; i++) { double rot = 1234.0 / pow(1e6, (double)(2 * i) / (double)k.b); k.w[i] = (float)cos(rot); k.y[i] = (float)sin(rot); }
103 dump(cs, "x", k.x, n * 4); dump(cs, "cos", k.w, half * 4); dump(cs, "sin", k.y, half * 4);
104 } else if (k.kind == 2) {
105 size_t n = k.a; k.out_n = n;
106 k.x = malloc(n * 4); k.w = malloc(n * 4);
107 for (size_t i = 0; i < n; i++) { k.x[i] = frand(&seed, -3.0f, 3.0f); k.w[i] = frand(&seed, -2.0f, 2.0f); }
108 dump(cs, "gate", k.x, n * 4); dump(cs, "up", k.w, n * 4);
109 } else if (k.kind == 3 || k.kind == 4) {
110 size_t rows = k.kind == 3 ? 1 : k.d, ctx = k.d;
111 size_t nq = rows * k.a * k.c, nkv = ctx * k.b * k.c; k.out_n = nq;
112 k.x = malloc(nq * 4); k.w = malloc(nkv * 4); k.y = malloc(nkv * 4);
113 for (size_t i = 0; i < nq; i++) k.x[i] = frand(&seed, -1.0f, 1.0f);
114 for (size_t i = 0; i < nkv; i++) { k.w[i] = frand(&seed, -1.0f, 1.0f); k.y[i] = frand(&seed, -1.0f, 1.0f); }
115 dump(cs, "q", k.x, nq * 4); dump(cs, "k", k.w, nkv * 4); dump(cs, "v", k.y, nkv * 4);
116 } else {
117 uint32_t bs = k.e, nblk = (k.d + bs - 1) / bs; size_t nq = (size_t)k.a * k.c; k.out_n = nq;
118 size_t pool_bytes = (size_t)nblk * 2 * bs * k.b * k.c * 2;
119 if (omega_gpu_attention_pool_alloc(pool_bytes, &k.pool) != 0) { fprintf(stderr, "pool_alloc failed: %s\n", omega_gpu_attention_last_error()); return 4; }
120 uint16_t *pw = k.pool;
121 for (size_t i = 0; i < pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f));
122 k.ids = malloc(nblk * 4);
123 for (uint32_t b = 0; b < nblk; b++) k.ids[b] = (b * 37u + 5u) % nblk; /* scattered, a permutation (37 odd, nblk power of two) */
124 k.x = malloc(nq * 4);
125 for (size_t i = 0; i < nq; i++) k.x[i] = frand(&seed, -1.0f, 1.0f);
126 dump(cs, "q", k.x, nq * 4); dump(cs, "pool", k.pool, pool_bytes); dump(cs, "ids", k.ids, nblk * 4);
127 }
128 if (mutant) {
129 if (k.kind == 0) omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_RMSNORM);
130 else if (k.kind == 1) omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_ROPE);
131 else if (k.kind == 2) omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_SWIGLU);
132 else omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_KV_HEAD);
133 }
134 float *first = malloc(k.out_n * 4), *got = malloc(k.out_n * 4);
135 uint64_t chip; uint64_t t0 = now_ns();
136 int rc = call(&k, first, &chip);
137 uint64_t first_ns = now_ns() - t0;
138 if (mutant) { omega_gpu_elementwise_test_set_mutant(0); omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); }
139 dump(cs, "out", first, k.out_n * 4);
140 int bad_rc = rc != 0, diff = 0;
141 for (int i = 0; i < warm && !bad_rc; i++) { if (call(&k, got, &chip) != 0) bad_rc = 1; }
142 uint64_t *wall = calloc((size_t)reps + 1, 8), *chp = calloc((size_t)reps + 1, 8);
143 for (int i = 0; i < reps && !bad_rc; i++) {
144 uint64_t s = now_ns(); int r = call(&k, got, &chp[i]); wall[i] = now_ns() - s;
145 if (r != 0) bad_rc = 1;
146 if (memcmp(got, first, k.out_n * 4) != 0) diff++;
147 }
148 printf("{\"case\":\"%s\",\"rc\":%d,\"mutant\":%d,\"first_call_ns\":%llu,\"warm\":%d,\"reps\":%d,\"timed_outputs_differing_from_first\":%d,\"wall_ns\":[",
149 cs, rc, mutant, (unsigned long long)first_ns, warm, reps, diff);
150 for (int i = 0; i < reps; i++) printf("%s%llu", i ? "," : "", (unsigned long long)wall[i]);
151 printf("],\"chip_ns\":[");
152 for (int i = 0; i < reps; i++) printf("%s%llu", i ? "," : "", (unsigned long long)chp[i]);
153 printf("]}\n"); fflush(stdout);
154 if (rc != 0) fprintf(stderr, "%s rc=%d err=%s\n", cs, rc, k.kind <= 2 ? omega_gpu_elementwise_last_error() : omega_gpu_attention_last_error());
155 free(first); free(got); free(wall); free(chp); free(k.x); free(k.w); free(k.y); free(k.ids);
156 if (k.pool) omega_gpu_attention_pool_free();
157 }
158 return 0;
159}