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
omega-mv / tests / gpu_gdn_multi_test.c
| 1 | /* |
| 2 | * gpu_gdn_multi_test: the multi-token Gated DeltaNet decode (L369, omega_gpu_gdn_api.h) must be the decode step T times, |
| 3 | * bit for bit: every output row, the conv tail and the state after the last token. |
| 4 | * --sim warp simulator over the whole grid: T decode launches against one multi launch (both fragment orders), |
| 5 | * T 1, 2, 5; controls: a zero row stride and mutant 4 (tail not shifted) must differ |
| 6 | * (default) the chip: T decode steps (omega_gpu_gdn_step) against one recorded multi launch from the same nonzero |
| 7 | * state, T 1, 2, 37, 128, two input scales; the same controls must differ |
| 8 | * Rows as the 35B graph lays them out: token t's qkv | z in one row (stride (conv + vdim) * 4 bytes), ab rows of vh * 8 |
| 9 | * bytes, out rows of vdim * 4 bytes. Verdict line last: "gpu_gdn_multi_test: N checks, F failed: PASS|FAIL". |
| 10 | */ |
| 11 | #include "omega_gpu_gdn_api.h" |
| 12 | #include "omega_gpu_session.h" |
| 13 | #include "omega_gpu_cmdlist.h" |
| 14 | #include "bw_warp_sim_k.h" |
| 15 | #include <math.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #ifndef GDN_TEST_RATIO |
| 21 | #define GDN_TEST_RATIO 2u |
| 22 | #endif |
| 23 | #define RT (GDN_TEST_RATIO ? GDN_TEST_RATIO : 3u) |
| 24 | #define KH OMEGA_GDN_KEY_HEADS |
| 25 | #define HD OMEGA_GDN_HEAD_DIM |
| 26 | #define VH (KH * RT) |
| 27 | #define VD (VH * HD) |
| 28 | #define CD (2u * KH * HD + VD) |
| 29 | #define NS (VH * HD * HD) |
| 30 | #define NT (3u * CD) |
| 31 | #define ROW ((CD + VD) * 4u) /* qkv | z */ |
| 32 | #define ABROW (VH * 8u) |
| 33 | #define OROW (VD * 4u) |
| 34 | #define P_SCAL (CD * OMEGA_GDN_TAPS * 4u) |
| 35 | #define P_NORM (P_SCAL + VH * 8u) |
| 36 | #define P_BYTES (P_NORM + HD * 4u) |
| 37 | #define TAIL_BYTES (VH * OMEGA_GDN_TAIL_HEAD_BYTES) |
| 38 | #define STATE_BYTES (VH * OMEGA_GDN_STATE_HEAD_BYTES) |
| 39 | static const float EPS_L2 = 1e-6f, EPS_N = 1e-6f; |
| 40 | |
| 41 | static int g_checks, g_failed; |
| 42 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 43 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 44 | static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } |
| 45 | |
| 46 | typedef struct { |
| 47 | float *conv_w, a_log[VH], dtb[VH], nw[HD]; |
| 48 | float *tail0, *state0; /* reference layouts: tail [3][conv], state [vh][128][128] */ |
| 49 | float *rows, *ab; /* T rows of qkv | z (true values / in_scale), T rows of {a, b} per head */ |
| 50 | } Case; |
| 51 | static void case_make(Case *c, uint32_t T, uint32_t seed, float in_scale) { |
| 52 | uint32_t s = seed; |
| 53 | c->conv_w = malloc((size_t)CD * 16); c->tail0 = malloc((size_t)NT * 4); c->state0 = malloc((size_t)NS * 4); |
| 54 | c->rows = malloc((size_t)T * ROW); c->ab = malloc((size_t)T * ABROW); |
| 55 | for (size_t i = 0; i < (size_t)CD * 4; i++) c->conv_w[i] = frand(&s, -0.6f, 0.6f); |
| 56 | for (unsigned h = 0; h < VH; h++) { c->a_log[h] = frand(&s, -1.5f, 1.5f); c->dtb[h] = frand(&s, -3.0f, 1.0f); } |
| 57 | for (unsigned i = 0; i < HD; i++) c->nw[i] = frand(&s, 0.4f, 1.6f); |
| 58 | for (size_t i = 0; i < NT; i++) c->tail0[i] = frand(&s, -2.0f, 2.0f); |
| 59 | for (size_t i = 0; i < NS; i++) c->state0[i] = frand(&s, -0.2f, 0.2f); |
| 60 | for (uint32_t t = 0; t < T; t++) { |
| 61 | float *r = c->rows + (size_t)t * (CD + VD); |
| 62 | for (unsigned i = 0; i < CD; i++) r[i] = frand(&s, -2.5f, 2.5f) / in_scale; |
| 63 | for (unsigned i = 0; i < VD; i++) r[CD + i] = frand(&s, -3.0f, 3.0f) / in_scale; |
| 64 | float *a = c->ab + (size_t)t * VH * 2; |
| 65 | for (unsigned i = 0; i < 2 * VH; i++) a[i] = frand(&s, -4.0f, 4.0f); |
| 66 | if (t % 3 == 0) { a[0] = 25.0f; a[2] = 19.9f; a[4] = -30.0f; a[3] = 12.0f; a[5] = -12.0f; } /* softplus threshold, beta ~ 0 / 1 */ |
| 67 | } |
| 68 | } |
| 69 | static void case_free(Case *c) { free(c->conv_w); free(c->tail0); free(c->state0); free(c->rows); free(c->ab); } |
| 70 | static size_t first_diff(const void *a, const void *b, size_t words) { |
| 71 | const uint32_t *x = a, *y = b; |
| 72 | for (size_t i = 0; i < words; i++) if (x[i] != y[i]) return i; |
| 73 | return (size_t)-1; |
| 74 | } |
| 75 | |
| 76 | /* ---- simulator ---- */ |
| 77 | typedef struct { uint32_t off; void *p; size_t bytes; } SimBuf; |
| 78 | static int sim_run(const BlackwellIRProgram *prog, int order, const SimBuf *b, int nb, const uint32_t *words, uint32_t nwords, |
| 79 | char *msg, size_t n) { |
| 80 | static uint8_t smem[OMEGA_GDN_SHARED_BYTES]; |
| 81 | WsSim s; memset(&s, 0, sizeof s); |
| 82 | s.p = prog; s.threads = OMEGA_GDN_THREADS; s.order = order; |
| 83 | memset(smem, 0xcd, sizeof smem); |
| 84 | s.smem = smem; s.smem_bytes = OMEGA_GDN_SHARED_BYTES; |
| 85 | for (int i = 0; i < nb; i++) { |
| 86 | const uint64_t a = (uintptr_t)b[i].p; |
| 87 | s.cbank[b[i].off / 4] = (uint32_t)a; s.cbank[b[i].off / 4 + 1] = (uint32_t)(a >> 32); |
| 88 | s.ranges[i].lo = a; s.ranges[i].hi = a + b[i].bytes; |
| 89 | } |
| 90 | s.nranges = nb; |
| 91 | for (uint32_t i = 0; i < nwords; i++) s.cbank[OMEGA_GDN_ARG_EPS_L2 / 4 + i] = words[i]; |
| 92 | int err = ws_run(&s, RT, OMEGA_GDN_GRID_Y); |
| 93 | if (err) snprintf(msg, n, "%.150s (pc %d)", s.msg, s.err_pc); |
| 94 | return err; |
| 95 | } |
| 96 | /* T decode launches (decode = 1) or one multi launch over the case; out [T][VD], tail and state device layouts */ |
| 97 | static int sim_path(const Case *c, uint32_t T, int multi, int mutant, uint32_t xstride, float in_scale, int order, float *out, |
| 98 | uint8_t *tail, uint8_t *state, char *msg, size_t n) { |
| 99 | static BlackwellIRProgram prog; |
| 100 | OmegaGdnSpec spec = { .mutant = mutant, .ratio = GDN_TEST_RATIO }; |
| 101 | if ((multi ? omega_blackwell_codegen_gdn_multi_prog(&spec, &prog) : omega_blackwell_codegen_gdn_prog(&spec, &prog)) != 0) { |
| 102 | snprintf(msg, n, "codegen refused"); return -1; |
| 103 | } |
| 104 | uint8_t *par = aligned_alloc(256, (P_BYTES + 255) & ~255u); |
| 105 | omega_gdn_pack_params_r(GDN_TEST_RATIO, c->conv_w, c->a_log, c->dtb, c->nw, par); |
| 106 | omega_gdn_pack_tail_r(GDN_TEST_RATIO, c->tail0, (float *)tail); |
| 107 | memcpy(state, c->state0, STATE_BYTES); |
| 108 | const float one = 1.0f; |
| 109 | uint32_t w[10]; |
| 110 | memcpy(&w[0], &EPS_L2, 4); memcpy(&w[1], &EPS_N, 4); memcpy(&w[2], &in_scale, 4); memcpy(&w[3], &one, 4); |
| 111 | int err = 0; |
| 112 | for (uint32_t t = 0; t < (multi ? 1u : T) && !err; t++) { |
| 113 | const uint8_t *row = (const uint8_t *)c->rows + (size_t)t * ROW; |
| 114 | SimBuf b[7] = { |
| 115 | { OMEGA_GDN_ARG_QKV, (void *)row, (size_t)(multi ? T : 1) * ROW }, { OMEGA_GDN_ARG_Z, (void *)(row + CD * 4u), (size_t)(multi ? T : 1) * ROW - CD * 4u }, |
| 116 | { OMEGA_GDN_ARG_AB, (uint8_t *)c->ab + (size_t)t * ABROW, (size_t)(multi ? T : 1) * ABROW }, { OMEGA_GDN_ARG_PAR, par, P_BYTES }, |
| 117 | { OMEGA_GDN_ARG_TAIL, tail, TAIL_BYTES }, { OMEGA_GDN_ARG_STATE, state, STATE_BYTES }, |
| 118 | { OMEGA_GDN_ARG_OUT, (uint8_t *)out + (size_t)t * OROW, (size_t)(multi ? T : 1) * OROW }, |
| 119 | }; |
| 120 | w[4] = T; w[5] = xstride; w[6] = ABROW; w[7] = OROW; /* the multi words follow OUT_SCALE (0x3c4): 0x3c8.. */ |
| 121 | err = sim_run(&prog, order, b, 7, w, multi ? 8u : 4u, msg, n); |
| 122 | } |
| 123 | free(par); |
| 124 | return err; |
| 125 | } |
| 126 | static void sim_battery(void) { |
| 127 | const uint32_t Ts[] = { 1, 2, 5 }; |
| 128 | float *oa = malloc((size_t)5 * OROW), *ob = malloc((size_t)5 * OROW); |
| 129 | uint8_t *ta = malloc(TAIL_BYTES), *tb = malloc(TAIL_BYTES), *sa = malloc(STATE_BYTES), *sb = malloc(STATE_BYTES); |
| 130 | char msg[256] = ""; |
| 131 | for (unsigned i = 0; i < 3; i++) { |
| 132 | const uint32_t T = Ts[i]; |
| 133 | Case c; case_make(&c, T, 77 + T, 1.0f); |
| 134 | int e1 = sim_path(&c, T, 0, 0, ROW, 1.0f, 0, oa, ta, sa, msg, sizeof msg); |
| 135 | CHECK(e1 == 0, "sim decode x %u ran (%s)", T, msg); |
| 136 | for (int order = 0; order < 2 && e1 == 0; order++) { |
| 137 | int e2 = sim_path(&c, T, 1, 0, ROW, 1.0f, order, ob, tb, sb, msg, sizeof msg); |
| 138 | const size_t dO = first_diff(oa, ob, (size_t)T * VD), dT = first_diff(ta, tb, TAIL_BYTES / 4), dS = first_diff(sa, sb, STATE_BYTES / 4); |
| 139 | printf("sim T %u order %d: multi %s | out %s tail %s state %s\n", T, order, e2 ? msg : "ran", dO == (size_t)-1 ? "identical" : "DIFFER", |
| 140 | dT == (size_t)-1 ? "identical" : "DIFFER", dS == (size_t)-1 ? "identical" : "DIFFER"); |
| 141 | CHECK(e2 == 0 && dO == (size_t)-1 && dT == (size_t)-1 && dS == (size_t)-1, "sim T %u order %d: multi == %u decode steps bit for bit", T, order, T); |
| 142 | } |
| 143 | if (T >= 2 && e1 == 0) { |
| 144 | int e3 = sim_path(&c, T, 1, 0, 0, 1.0f, 0, ob, tb, sb, msg, sizeof msg); |
| 145 | CHECK(e3 == 0 && first_diff(oa, ob, (size_t)T * VD) != (size_t)-1, "sim T %u control: row stride 0 differs", T); |
| 146 | int e4 = sim_path(&c, T, 1, 4, ROW, 1.0f, 0, ob, tb, sb, msg, sizeof msg); |
| 147 | CHECK(e4 == 0 && first_diff(oa, ob, (size_t)T * VD) != (size_t)-1, "sim T %u control: mutant 4 (tail not shifted) differs", T); |
| 148 | } |
| 149 | case_free(&c); |
| 150 | } |
| 151 | free(oa); free(ob); free(ta); free(tb); free(sa); free(sb); |
| 152 | } |
| 153 | |
| 154 | /* ---- chip ---- */ |
| 155 | /* the decode path: T omega_gpu_gdn_step calls; out [T][VD], tail (reference layout) and state after the last */ |
| 156 | static int chip_decode(OmegaGpuGdn *g, const Case *c, uint32_t T, float in_scale, float *out, float *tail, float *state) { |
| 157 | if (omega_gpu_gdn_set_state(g, 0, c->tail0, c->state0) != 0) return -1; |
| 158 | for (uint32_t t = 0; t < T; t++) { |
| 159 | const float *r = c->rows + (size_t)t * (CD + VD); |
| 160 | if (omega_gpu_gdn_step(g, 0, r, r + CD, c->ab + (size_t)t * VH * 2, EPS_L2, EPS_N, in_scale, 1.0f, out + (size_t)t * VD, NULL) != 0) return -1; |
| 161 | } |
| 162 | return omega_gpu_gdn_get_state(g, 0, tail, state); |
| 163 | } |
| 164 | static int chip_multi(OmegaGpuGdn *g, const Case *c, uint32_t T, float in_scale, uint32_t xstride, float *out, float *tail, float *state) { |
| 165 | if (omega_gpu_gdn_set_state(g, 0, c->tail0, c->state0) != 0 || omega_gpu_gdn_multi_prepare(g) != 0) return -1; |
| 166 | OmegaGpuCmdList *cl = NULL; |
| 167 | if (omega_gpu_cmdlist_create(4, &cl) != 0) return -1; |
| 168 | omega_gpu_session_lock(); |
| 169 | OmegaGpuMem m; memset(&m, 0, sizeof m); |
| 170 | const size_t s_rows = (size_t)T * ROW, s_ab = ((size_t)T * ABROW + 255) & ~(size_t)255, s_out = (size_t)T * OROW; |
| 171 | int ok = omega_gpu_session_open() != NULL && omega_gpu_session_alloc(s_rows + s_ab + s_out, &m) == 0; |
| 172 | if (ok) { |
| 173 | uint8_t *p = m.cpu; |
| 174 | memcpy(p, c->rows, s_rows); memcpy(p + s_rows, c->ab, (size_t)T * ABROW); |
| 175 | float *o = (float *)(p + s_rows + s_ab); |
| 176 | for (size_t i = 0; i < (size_t)T * VD; i++) o[i] = NAN; |
| 177 | ok = omega_gpu_session_sync_to_gpu(&m) == 0; |
| 178 | } |
| 179 | if (ok) ok = omega_gpu_gdn_multi_record(cl, g, 0, T, m.va, m.va + CD * 4u, xstride, m.va + s_rows, ABROW, m.va + s_rows + s_ab, OROW, |
| 180 | EPS_L2, EPS_N, in_scale, 1.0f) == 0; |
| 181 | OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts); |
| 182 | OmegaGpuCmdListStats st; memset(&st, 0, sizeof st); |
| 183 | if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &st) == 0 && st.sems_done == st.launches; |
| 184 | if (ok) ok = omega_gpu_session_sync_to_cpu(&m) == 0; |
| 185 | if (ok) memcpy(out, (uint8_t *)m.cpu + s_rows + s_ab, s_out); |
| 186 | if (m.cpu) omega_gpu_session_free(&m); |
| 187 | omega_gpu_session_unlock(); |
| 188 | omega_gpu_cmdlist_free(cl); |
| 189 | if (!ok) return -1; |
| 190 | return omega_gpu_gdn_get_state(g, 0, tail, state); |
| 191 | } |
| 192 | static void chip_battery(void) { |
| 193 | OmegaGpuGdn *g = NULL; |
| 194 | CHECK(omega_gpu_gdn_alloc_ratio(1, GDN_TEST_RATIO, &g) == 0, "alloc (%s)", omega_gpu_gdn_last_error()); |
| 195 | if (!g) return; |
| 196 | const uint32_t Ts[] = { 1, 2, 37, 128 }; |
| 197 | const float scales[] = { 1.0f, 0.0009765625f }; |
| 198 | float *oa = malloc((size_t)128 * OROW), *ob = malloc((size_t)128 * OROW); |
| 199 | float *ta = malloc((size_t)NT * 4), *tb = malloc((size_t)NT * 4), *sa = malloc(STATE_BYTES), *sb = malloc(STATE_BYTES); |
| 200 | for (unsigned si = 0; si < 2; si++) for (unsigned i = 0; i < 4; i++) { |
| 201 | const uint32_t T = Ts[i]; const float sc = scales[si]; |
| 202 | Case c; case_make(&c, T, 900 + 7 * T + si, sc); |
| 203 | CHECK(omega_gpu_gdn_set_layer(g, 0, c.conv_w, c.a_log, c.dtb, c.nw) == 0, "set layer"); |
| 204 | const int ra = chip_decode(g, &c, T, sc, oa, ta, sa); |
| 205 | const int rb = chip_multi(g, &c, T, sc, ROW, ob, tb, sb); |
| 206 | const size_t dO = first_diff(oa, ob, (size_t)T * VD), dT = first_diff(ta, tb, NT), dS = first_diff(sa, sb, NS); |
| 207 | printf("chip T %3u in_scale %g: decode %s multi %s | out %s tail %s state %s\n", T, (double)sc, ra ? "FAILED" : "ran", rb ? "FAILED" : "ran", |
| 208 | dO == (size_t)-1 ? "identical" : "DIFFER", dT == (size_t)-1 ? "identical" : "DIFFER", dS == (size_t)-1 ? "identical" : "DIFFER"); |
| 209 | CHECK(ra == 0 && rb == 0, "chip T %u: both paths ran (%s)", T, omega_gpu_gdn_last_error()); |
| 210 | CHECK(ra == 0 && rb == 0 && dO == (size_t)-1 && dT == (size_t)-1 && dS == (size_t)-1, "chip T %u in_scale %g: multi == %u decode steps bit for bit", T, (double)sc, T); |
| 211 | if (T >= 37 && si == 0) { |
| 212 | const int r0 = chip_multi(g, &c, T, sc, 0, ob, tb, sb); |
| 213 | CHECK(r0 == 0 && first_diff(oa, ob, (size_t)T * VD) != (size_t)-1, "chip T %u control: row stride 0 differs", T); |
| 214 | omega_gpu_gdn_test_set_mutant(4); |
| 215 | const int r4 = chip_multi(g, &c, T, sc, ROW, ob, tb, sb); |
| 216 | omega_gpu_gdn_test_set_mutant(0); |
| 217 | CHECK(r4 == 0 && first_diff(oa, ob, (size_t)T * VD) != (size_t)-1, "chip T %u control: mutant 4 (tail not shifted) differs", T); |
| 218 | } |
| 219 | case_free(&c); |
| 220 | } |
| 221 | free(oa); free(ob); free(ta); free(tb); free(sa); free(sb); |
| 222 | omega_gpu_gdn_free(g); |
| 223 | } |
| 224 | |
| 225 | int main(int argc, char **argv) { |
| 226 | int sim = argc > 1 && strcmp(argv[1], "--sim") == 0; |
| 227 | uint32_t insns = 0, gprs = 0; |
| 228 | OmegaGdnSpec spec = { .ratio = GDN_TEST_RATIO }; |
| 229 | CHECK(omega_gdn_multi_prog_stats(&spec, &insns, &gprs) == 0 && gprs > 0 && OMEGA_GDN_THREADS * (gprs + 2u) <= 65536u, |
| 230 | "multi program builds and fits the register file (%u insns, %u registers)", insns, gprs); |
| 231 | if (sim) sim_battery(); |
| 232 | else chip_battery(); |
| 233 | printf("gpu_gdn_multi_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); |
| 234 | return g_failed ? 1 : 0; |
| 235 | } |