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