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_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_test.c` 1020 lines · 67062 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * gpu_gdn_test: the one-launch Gated DeltaNet decode step of Qwen3.8-27B (omega_gpu_gdn_api.h), omega#542. 4 * --host-only layout helpers, codegen budgets and refusals, the double oracle's delta-rule property 5 * --sim warp-simulator parity over the whole grid (48 CTAs x 256 threads, both fragment orders bit-identical), 6 * three chained steps against the double oracle, mutants 1-4 caught 7 * (default) the chip: two layers (addressing), five chained steps each against the oracle (output, tail, state), 8 * a rerun from the same state bit-identical, reset, mutants 1-4 caught, the record path (two steps in 9 * one command list: the second reads the first's state), chip medians (--reps N, --out receipt.json) 10 * The oracle is sc crates/aien-inference-abi/src/qwen38.rs `delta_net` in double, from the projections on. 11 * Verdict line last: "gpu_gdn_test: N checks, F failed: PASS|FAIL". 12 */ 13 #include "omega_gpu_gdn_api.h" 14 #include "omega_gpu_hostmath.h" 15 #include "omega_gpu_session.h" 16 #include "omega_gpu_cmdlist.h" 17 #include "omega_blackwell_codegen_tc.h" 18 #include "bw_warp_sim_k.h" 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 #include "test_out_file.h" 26 27 static int g_checks, g_failed; 28 #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 29 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 30 static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / `16777216`.0f); } 31 static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } 32 static 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; } 33 34 /* GDN_TEST_RATIO: value heads per key head under test (OmegaGdnSpec.ratio; 0 = 3, Qwen3.8-27B; -DGDN_TEST_RATIO=2 builds the 35 * Qwen3.6-35B-A3B shape, sc L306). Every size below follows it. */ 36 #ifndef GDN_TEST_RATIO 37 #define GDN_TEST_RATIO 0u 38 #endif 39 #define RT (GDN_TEST_RATIO ? GDN_TEST_RATIO : 3u) 40 #define KH OMEGA_GDN_KEY_HEADS 41 #define HD OMEGA_GDN_HEAD_DIM 42 #define VH (KH * RT) 43 #define VD (VH * HD) 44 #define CD (2u * KH * HD + VD) 45 #define NS (VH * HD * HD) 46 #define NT (3u * CD) 47 #define T_PAR_SCAL (CD * OMEGA_GDN_TAPS * 4u) 48 #define T_PAR_NORM (T_PAR_SCAL + VH * 8u) 49 #define T_PAR_BYTES (T_PAR_NORM + HD * 4u) 50 #define T_TAIL_BYTES (VH * OMEGA_GDN_TAIL_HEAD_BYTES) 51 #define T_STATE_BYTES (VH * OMEGA_GDN_STATE_HEAD_BYTES) 52 static const float EPS_L2 = 1e-6f, EPS_N = 1e-6f; 53 /* tolerances, normalised by the largest reference magnitude of the vector (EX2 / RCP / RSQ / LG2 are approximate on 54 * the chip, the oracle is double; the mutants miss by orders of magnitude more, see the receipts) */ 55 #define TOL_OUT 2e-4 56 #define TOL_STATE 2e-5 57 #define TOL_TAIL 1e-6 58 59 typedef struct { 60 float *conv_w, a_log[VH], dtb[VH], nw[HD]; 61 float *tail0, *state0; /* initial state, reference layouts */ 62 } Layer; 63 typedef struct { float *qkv, *z, ab[2 * VH]; } Inp; 64 65 static void layer_make(Layer *L, uint32_t seed) { 66 uint32_t s = seed; 67 L->conv_w = malloc((size_t)CD * 4 * 4); L->tail0 = malloc((size_t)NT * 4); L->state0 = malloc((size_t)NS * 4); 68 for (size_t i = 0; i < (size_t)CD * 4; i++) L->conv_w[i] = frand(&s, -0.6f, 0.6f); 69 for (unsigned h = 0; h < VH; h++) { L->a_log[h] = frand(&s, -1.5f, 1.5f); L->dtb[h] = frand(&s, -3.0f, 1.0f); } 70 for (unsigned i = 0; i < HD; i++) L->nw[i] = frand(&s, 0.4f, 1.6f); 71 for (size_t i = 0; i < NT; i++) L->tail0[i] = frand(&s, -2.0f, 2.0f); 72 for (size_t i = 0; i < NS; i++) L->state0[i] = frand(&s, -0.2f, 0.2f); 73 } 74 static void layer_free(Layer *L) { free(L->conv_w); free(L->tail0); free(L->state0); } 75 /* in_scale: the kernel multiplies qkv and z by it, so the stored rows are the true rows / in_scale */ 76 static void inp_make(Inp *in, uint32_t seed, float in_scale) { 77 uint32_t s = seed; 78 in->qkv = malloc((size_t)CD * 4); in->z = malloc((size_t)VD * 4); 79 for (unsigned i = 0; i < CD; i++) in->qkv[i] = frand(&s, -2.5f, 2.5f) / in_scale; 80 for (unsigned i = 0; i < VD; i++) in->z[i] = frand(&s, -3.0f, 3.0f) / in_scale; 81 for (unsigned h = 0; h < VH; h++) { in->ab[2 * h] = frand(&s, -4.0f, 4.0f); in->ab[2 * h + 1] = frand(&s, -4.0f, 4.0f); } 82 /* a few heads at the softplus threshold and past it, and beta near 0 / 1 */ 83 if (!getenv("GDN_NOFORCE")) { in->ab[0] = 25.0f; in->ab[2] = 19.9f; in->ab[4] = -30.0f; in->ab[3] = 12.0f; in->ab[5] = -12.0f; } 84 } 85 static void inp_free(Inp *in) { free(in->qkv); free(in->z); } 86 87 /* ---- the double oracle ---- */ 88 typedef struct { double *tail, *state; } DState; 89 static void dstate_init(DState *d, const Layer *L) { 90 d->tail = malloc((size_t)NT * 8); d->state = malloc((size_t)NS * 8); 91 for (size_t i = 0; i < NT; i++) d->tail[i] = L->tail0[i]; 92 for (size_t i = 0; i < NS; i++) d->state[i] = L->state0[i]; 93 } 94 static void dstate_free(DState *d) { free(d->tail); free(d->state); } 95 static double sigm(double x) { return 1.0 / (1.0 + exp(-x)); } 96 static double softplus_ref(double x) { return x > 20.0 ? x : log1p(exp(x)); } 97 static void oracle_step(const Layer *L, DState *d, const Inp *in, float in_scale, float out_scale, double *y) { 98 static double mixed[CD], q[KH * HD], k[KH * HD]; 99 for (unsigned ch = 0; ch < CD; ch++) { 100 const double x = (double)(in->qkv[ch] * in_scale); /* the kernel's f32 product */ 101 const float *w = &L->conv_w[(size_t)ch * 4]; 102 double acc = w[0] * d->tail[ch] + w[1] * d->tail[CD + ch] + w[2] * d->tail[2 * CD + ch] + w[3] * x; 103 mixed[ch] = acc * sigm(acc); 104 d->tail[ch] = d->tail[CD + ch]; d->tail[CD + ch] = d->tail[2 * CD + ch]; d->tail[2 * CD + ch] = x; 105 } 106 for (unsigned hk = 0; hk < KH; hk++) { 107 double sq = 0, sk = 0; 108 for (unsigned i = 0; i < HD; i++) { sq += mixed[hk * HD + i] * mixed[hk * HD + i]; sk += mixed[KH * HD + hk * HD + i] * mixed[KH * HD + hk * HD + i]; } 109 const double iq = 1.0 / sqrt(sq + EPS_L2), ik = 1.0 / sqrt(sk + EPS_L2); 110 for (unsigned i = 0; i < HD; i++) { q[hk * HD + i] = mixed[hk * HD + i] * iq / sqrt((double)HD); k[hk * HD + i] = mixed[KH * HD + hk * HD + i] * ik; } 111 } 112 for (unsigned h = 0; h < VH; h++) { 113 const unsigned hk = h / (VH / KH); 114 const double negA = -(double)(float)exp((double)L->a_log[h]); /* the API's float negA (double exp, rounded) */ 115 const double g = (double)(float)negA * softplus_ref((double)in->ab[2 * h] + L->dtb[h]); 116 const double decay = exp(g), beta = sigm(in->ab[2 * h + 1]); 117 double *S = d->state + (size_t)h * HD * HD, kv[HD], o[HD], dl[HD]; 118 const double *kk = &k[hk * HD], *qq = &q[hk * HD], *v = &mixed[2 * KH * HD + h * HD]; 119 for (size_t i = 0; i < (size_t)HD * HD; i++) S[i] *= decay; 120 for (unsigned c = 0; c < HD; c++) kv[c] = 0; 121 for (unsigned r = 0; r < HD; r++) for (unsigned c = 0; c < HD; c++) kv[c] += S[r * HD + c] * kk[r]; 122 for (unsigned c = 0; c < HD; c++) dl[c] = (v[c] - kv[c]) * beta; 123 for (unsigned r = 0; r < HD; r++) for (unsigned c = 0; c < HD; c++) S[r * HD + c] += kk[r] * dl[c]; 124 for (unsigned c = 0; c < HD; c++) o[c] = 0; 125 for (unsigned r = 0; r < HD; r++) for (unsigned c = 0; c < HD; c++) o[c] += S[r * HD + c] * qq[r]; 126 double ms = 0; for (unsigned c = 0; c < HD; c++) ms += o[c] * o[c]; 127 const double inv = 1.0 / sqrt(ms / HD + EPS_N); 128 for (unsigned c = 0; c < HD; c++) { 129 const double zz = (double)(in->z[h * HD + c] * in_scale); 130 y[h * HD + c] = o[c] * inv * L->nw[c] * (zz * sigm(zz)) * out_scale; 131 } 132 } 133 } 134 /* max |got - ref| / max |ref| */ 135 static double nerr(const float *got, const double *ref, size_t n) { 136 double e = 0, m = 1e-30; 137 for (size_t i = 0; i < n; i++) { 138 const double a = fabs((double)got[i] - ref[i]); 139 if (!isfinite(a)) { if (getenv("GDN_DBG")) printf(" non-finite at %zu: got %g ref %g\n", i, (double)got[i], ref[i]); return INFINITY; } /* a NaN or inf on either side never passes */ 140 if (a > e) e = a; 141 if (fabs(ref[i]) > m) m = fabs(ref[i]); 142 } 143 return e / m; 144 } 145 typedef struct { double out, tail, state; } Errs; 146 static int errs_ok(const Errs *e) { return e->out <= TOL_OUT && e->tail <= TOL_TAIL && e->state <= TOL_STATE; } 147 148 /* ---- host-only ---- */ 149 static void host_only(void) { 150 /* layout round trip, and the copy check */ 151 float *t = malloc((size_t)NT * 4), *t2 = malloc((size_t)NT * 4), *dev = malloc(T_TAIL_BYTES); 152 uint32_t s = 5; 153 for (size_t i = 0; i < NT; i++) t[i] = frand(&s, -1, 1); 154 omega_gdn_pack_tail_r(GDN_TEST_RATIO, t, dev); 155 CHECK(omega_gdn_unpack_tail_r(GDN_TEST_RATIO, dev, t2) == 0 && memcmp(t, t2, (size_t)NT * 4) == 0, "tail pack/unpack round trip"); 156 dev[(1u * OMEGA_GDN_SLOTS + 7u) * 4u + 1u] += 1.0f; /* head 1 (a second copy of key head 0), q slot 7 */ 157 CHECK(omega_gdn_unpack_tail_r(GDN_TEST_RATIO, dev, t2) == -1, "unpack refuses disagreeing copies"); 158 free(t); free(t2); free(dev); 159 /* parameter block */ 160 Layer L; layer_make(&L, 9); 161 uint8_t *blk = malloc(T_PAR_BYTES); 162 CHECK(omega_gdn_pack_params_r(GDN_TEST_RATIO, L.conv_w, L.a_log, L.dtb, L.nw, blk) == 0, "pack params"); 163 const float *sc = (const float *)(blk + T_PAR_SCAL); 164 CHECK(sc[2 * 5] == -(float)omega_hostmath_exp((double)L.a_log[5]) && sc[2 * 5 + 1] == L.dtb[5], "negA and dt_bias interleaved"); 165 for (unsigned h = 0; h < VH; h++) { /* the libm-free exp agrees with libm to within one float ulp */ 166 const float want = -(float)exp((double)L.a_log[h]), got = sc[2 * h]; 167 CHECK(fabsf(got - want) <= fabsf(want) * 0x1p-23f, "negA head %u: %.9g vs libm %.9g", h, (double)got, (double)want); 168 } 169 CHECK(memcmp(blk + T_PAR_NORM, L.nw, HD * 4) == 0, "norm weight at its offset"); 170 CHECK(omega_gdn_pack_params_r(GDN_TEST_RATIO, NULL, L.a_log, L.dtb, L.nw, blk) == -1, "pack params refuses NULL"); 171 free(blk); 172 /* codegen budgets */ 173 for (int m = 0; m <= 4; m++) { 174 OmegaGdnSpec sp = { .mutant = m, .ratio = GDN_TEST_RATIO }; uint32_t ni = 0, ng = 0; 175 int rc = omega_gdn_prog_stats(&sp, &ni, &ng); 176 CHECK(rc == 0 && ni <= BW_MAX_IR_INSNS && ng + 2 <= BW_PHYS_GPR_MAX, "mutant %d codegen: rc %d, %u insns, %u registers", m, rc, ni, ng); 177 if (m == 0) printf("kernel: %u instructions, peak %u registers (%u declared, %u threads)\n", ni, ng, omega_bw_gpr_declared(ng), OMEGA_GDN_THREADS); 178 } 179 OmegaGdnSpec bad = { .mutant = 5, .ratio = GDN_TEST_RATIO }; 180 CHECK(omega_gdn_prog_stats(&bad, NULL, NULL) == -1, "mutant 5 refused"); 181 /* the state copy / zero programs (cached tail and state) */ 182 { 183 static BlackwellIRProgram cp; 184 OmegaGdnSpec c1 = { .copy = OMEGA_GDN_COPY }, c2 = { .copy = OMEGA_GDN_ZERO }, c3 = { .copy = 3 }, cm = { .mutant = 1, .copy = OMEGA_GDN_COPY }; 185 CHECK(omega_blackwell_codegen_gdn_copy_prog(&c1, &cp) == 0 && cp.regalloc.peak_gpr_usage + 2 <= BW_PHYS_GPR_MAX, "copy codegen"); 186 CHECK(omega_blackwell_codegen_gdn_copy_prog(&c2, &cp) == 0, "zero codegen"); 187 CHECK(omega_blackwell_codegen_gdn_copy_prog(&c3, &cp) == -1 && omega_blackwell_codegen_gdn_copy_prog(&cm, &cp) == -1, "copy codegen refuses kind 3 and a mutant"); 188 CHECK(omega_gdn_prog_stats(&c1, NULL, NULL) == -1, "decode codegen refuses a copy spec"); 189 } 190 /* the oracle's delta rule: from S = 0 with beta ~ 1, the step writes v along k, so S^T k = v (|k| = 1) */ 191 { 192 DState d; dstate_init(&d, &L); 193 memset(d.state, 0, (size_t)NS * 8); 194 Inp in; inp_make(&in, 77, 1.0f); 195 for (unsigned h = 0; h < VH; h++) in.ab[2 * h + 1] = 40.0f; 196 double *y = malloc((size_t)VD * 8); 197 static double mixed_v[CD]; 198 /* the expected v and k: recompute the conv of this step from the oracle's tail before the step */ 199 for (unsigned ch = 0; ch < CD; ch++) { 200 const float *w = &L.conv_w[(size_t)ch * 4]; 201 double acc = w[0] * d.tail[ch] + w[1] * d.tail[CD + ch] + w[2] * d.tail[2 * CD + ch] + w[3] * (double)in.qkv[ch]; 202 mixed_v[ch] = acc * sigm(acc); 203 } 204 oracle_step(&L, &d, &in, 1.0f, 1.0f, y); 205 double worst = 0; 206 for (unsigned h = 0; h < VH; h += 7) { 207 const unsigned hk = h / RT; 208 double nk = 0; for (unsigned i = 0; i < HD; i++) nk += mixed_v[KH * HD + hk * HD + i] * mixed_v[KH * HD + hk * HD + i]; 209 nk = sqrt(nk + EPS_L2); 210 for (unsigned c = 0; c < HD; c++) { 211 double kv = 0; 212 for (unsigned r = 0; r < HD; r++) kv += d.state[(size_t)h * HD * HD + r * HD + c] * mixed_v[KH * HD + hk * HD + r] / nk; 213 const double want = mixed_v[2 * KH * HD + h * HD + c]; 214 if (fabs(kv - want) > worst) worst = fabs(kv - want); 215 } 216 } 217 CHECK(worst < 1e-6, "oracle delta rule: S^T k = v after one step from zero (worst %.3g)", worst); 218 free(y); inp_free(&in); dstate_free(&d); 219 } 220 layer_free(&L); 221 /* the prefill kernels encode; the scan and post kernels use native SHFL.BFLY butterflies */ 222 for (unsigned k = 0; k < 3; k++) { 223 static BlackwellIRProgram p; 224 OmegaGdnSpec spec = { .ratio = GDN_TEST_RATIO }; 225 int rc = k == 0 ? omega_blackwell_codegen_gdn_prep_prog(&spec, &p) : k == 1 ? omega_blackwell_codegen_gdn_scan_prog(&spec, &p) 226 : omega_blackwell_codegen_gdn_post_prog(&spec, &p); 227 size_t need = rc == 0 ? omega_bw_program_code_bytes(&p) : 0; 228 uint8_t *code = need ? malloc(need) : NULL; 229 OmegaBlackwellKernel kern; 230 int enc = code ? omega_blackwell_kernel_encode_into(&p, code, need, &kern) : -1; 231 uint32_t bfly = 0; 232 for (size_t i = 0; i < p.count; i++) if (p.insns[i].op == BW_IR_SHFL_BFLY) bfly++; 233 if (getenv("GDN_DUMP") && code && atoi(getenv("GDN_DUMP")) == (int)k) { FILE *f = test_out_open("gdn_pf.bin"); if (f) { fwrite(code, 1, need, f); fclose(f); } } 234 CHECK(rc == 0 && enc == 0 && (k == 0 || bfly > 0), 235 "prefill kernel %u encodes: %zu insns, %u registers, %u butterflies", k, p.count, p.regalloc.peak_gpr_usage, bfly); 236 free(code); 237 } 238 } 239 240 /* ---- simulator ---- */ 241 enum { SB_QKV, SB_Z, SB_AB, SB_PAR, SB_TAIL, SB_STATE, SB_OUT, SB_N }; 242 static uint64_t g_sim_issues; 243 static int sim_run_steps(int mutant, uint32_t steps, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) { 244 static BlackwellIRProgram prog; 245 OmegaGdnSpec spec = { .mutant = mutant, .ratio = GDN_TEST_RATIO }; 246 if (omega_blackwell_codegen_gdn_prog(&spec, &prog) != 0) { snprintf(msg, n, "codegen refused"); return -1; } 247 Layer L; layer_make(&L, 21); 248 DState d; dstate_init(&d, &L); 249 size_t sz[SB_N] = { CD * 4u, VD * 4u, VH * 8u, T_PAR_BYTES, T_TAIL_BYTES, T_STATE_BYTES, VD * 4u }; 250 uint8_t *buf[SB_N]; 251 for (int i = 0; i < SB_N; i++) buf[i] = aligned_alloc(256, (sz[i] + 255) & ~(size_t)255); 252 omega_gdn_pack_params_r(GDN_TEST_RATIO, L.conv_w, L.a_log, L.dtb, L.nw, buf[SB_PAR]); 253 omega_gdn_pack_tail_r(GDN_TEST_RATIO, L.tail0, (float *)buf[SB_TAIL]); 254 memcpy(buf[SB_STATE], L.state0, T_STATE_BYTES); 255 uint8_t *tail_save = malloc(T_TAIL_BYTES), *state_save = malloc(T_STATE_BYTES), *out_copy = malloc(VD * 4u); 256 uint8_t *tail_copy = malloc(T_TAIL_BYTES), *state_copy = malloc(T_STATE_BYTES); 257 double *y = malloc((size_t)VD * 8); 258 float *tref = malloc((size_t)NT * 4); 259 int err = 0; 260 memset(worst, 0, sizeof *worst); 261 for (uint32_t st = 0; st < steps && !err; st++) { 262 Inp in; inp_make(&in, 1000 + st, in_scale); 263 memcpy(buf[SB_QKV], in.qkv, sz[SB_QKV]); memcpy(buf[SB_Z], in.z, sz[SB_Z]); memcpy(buf[SB_AB], in.ab, sz[SB_AB]); 264 memcpy(tail_save, buf[SB_TAIL], T_TAIL_BYTES); memcpy(state_save, buf[SB_STATE], T_STATE_BYTES); 265 for (int order = 0; order < 2 && !err; order++) { 266 if (order == 1) { memcpy(buf[SB_TAIL], tail_save, T_TAIL_BYTES); memcpy(buf[SB_STATE], state_save, T_STATE_BYTES); } 267 float *yo = (float *)buf[SB_OUT]; 268 for (unsigned i = 0; i < VD; i++) yo[i] = -999.0f; 269 WsSim s; memset(&s, 0, sizeof s); 270 s.p = &prog; s.threads = OMEGA_GDN_THREADS; s.order = order; 271 static uint8_t smem[OMEGA_GDN_SHARED_BYTES]; s.smem = smem; s.smem_bytes = OMEGA_GDN_SHARED_BYTES; 272 for (int i = 0; i < SB_N; i++) { 273 const uint64_t a = (uintptr_t)buf[i]; 274 s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a >> 32); 275 s.ranges[i].lo = (uintptr_t)buf[i]; s.ranges[i].hi = (uintptr_t)buf[i] + sz[i]; 276 } 277 s.nranges = SB_N; 278 memcpy(&s.cbank[OMEGA_GDN_ARG_EPS_L2 / 4], &EPS_L2, 4); memcpy(&s.cbank[OMEGA_GDN_ARG_EPS_NORM / 4], &EPS_N, 4); 279 memcpy(&s.cbank[OMEGA_GDN_ARG_IN_SCALE / 4], &in_scale, 4); memcpy(&s.cbank[OMEGA_GDN_ARG_OUT_SCALE / 4], &out_scale, 4); 280 err = ws_run(&s, RT, OMEGA_GDN_GRID_Y); 281 if (err) { snprintf(msg, n, "step %u order %d: %s (pc %d)", st, order, s.msg, s.err_pc); break; } 282 g_sim_issues += s.warp_issues; 283 size_t unwritten = 0; for (unsigned i = 0; i < VD; i++) if (yo[i] == -999.0f) unwritten++; 284 if (unwritten) { err = -1; snprintf(msg, n, "%zu output words never written", unwritten); break; } 285 if (order == 0) { memcpy(out_copy, yo, VD * 4u); memcpy(tail_copy, buf[SB_TAIL], T_TAIL_BYTES); memcpy(state_copy, buf[SB_STATE], T_STATE_BYTES); } 286 else if (memcmp(out_copy, yo, VD * 4u) || memcmp(tail_copy, buf[SB_TAIL], T_TAIL_BYTES) || memcmp(state_copy, buf[SB_STATE], T_STATE_BYTES)) { 287 err = -1; snprintf(msg, n, "step %u: the fragment order changed the result", st); 288 } 289 } 290 if (err) { inp_free(&in); break; } 291 oracle_step(&L, &d, &in, in_scale, out_scale, y); 292 double *ts = malloc((size_t)NT * 8); 293 for (size_t i = 0; i < NT; i++) ts[i] = d.tail[i]; 294 Errs e; 295 e.out = nerr((const float *)buf[SB_OUT], y, VD); 296 if (omega_gdn_unpack_tail_r(GDN_TEST_RATIO, (const float *)buf[SB_TAIL], tref) != 0) e.tail = INFINITY; 297 else e.tail = nerr(tref, ts, NT); 298 e.state = nerr((const float *)buf[SB_STATE], d.state, NS); 299 free(ts); 300 if (e.out > worst->out) worst->out = e.out; 301 if (e.tail > worst->tail) worst->tail = e.tail; 302 if (e.state > worst->state) worst->state = e.state; 303 inp_free(&in); 304 } 305 for (int i = 0; i < SB_N; i++) free(buf[i]); 306 free(tail_save); free(state_save); free(out_copy); free(tail_copy); free(state_copy); free(y); free(tref); 307 dstate_free(&d); layer_free(&L); 308 return err; 309 } 310 static void sim_battery(void) { 311 struct { int mutant; uint32_t steps; float in_scale, out_scale; const char *name; } pts[] = { 312 { 0, 3, 1.0f, 1.0f, "3 steps" }, 313 { 0, 2, 0.`0009765625f`, `18446744073709551616`.0f, "2 steps, in_scale 2^-10, out_scale 2^64" }, 314 { 1, 2, 1.0f, 1.0f, "mutant 1 (no decay)" }, { 2, 2, 1.0f, 1.0f, "mutant 2 (beta 1)" }, 315 { 3, 2, 1.0f, 1.0f, "mutant 3 (gate z, not silu z)" }, { 4, 2, 1.0f, 1.0f, "mutant 4 (tail not shifted)" }, 316 }; 317 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { 318 char msg[300] = ""; Errs w = { 0, 0, 0 }; 319 double t0 = now_ms(); 320 int err = sim_run_steps(pts[i].mutant, pts[i].steps, pts[i].in_scale, pts[i].out_scale, &w, msg, sizeof msg); 321 printf("sim %-44s err out %.3g tail %.3g state %.3g (%.0f ms)%s%s\n", pts[i].name, w.out, w.tail, w.state, now_ms() - t0, err ? " ERROR " : "", msg); 322 CHECK(err == 0, "sim %s ran (%s)", pts[i].name, msg); 323 if (pts[i].mutant == 0) CHECK(errs_ok(&w), "sim %s within tolerance", pts[i].name); 324 else CHECK(!errs_ok(&w), "sim %s caught", pts[i].name); 325 } 326 printf("sim warp issues: %" PRIu64 "\n", g_sim_issues); 327 } 328 329 /* ---- L313 bf16 state on the simulator: the bf16 program equals the f32 program run from the widened state, with the 330 * stored state rounded to nearest even: outputs and tails bit-identical, every stored bf16 equal to bf16_rn of the f32 331 * program's stored state. Chained over steps (the f32 run restarts each step from the bf16 state). ---- */ 332 static uint16_t t_bf16_rn(float f) { uint32_t u; memcpy(&u, &f, 4); if ((u & 0x7fffffffu) > 0x7f800000u) return 0x7fc0u; return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); } 333 static float t_bf16_f(uint16_t b) { uint32_t u = (uint32_t)b << 16; float f; memcpy(&f, &u, 4); return f; } 334 static int sim_one(const BlackwellIRProgram *prog, uint8_t **buf, const size_t *sz, float in_scale, float out_scale, char *msg, size_t n) { 335 WsSim s; memset(&s, 0, sizeof s); 336 s.p = prog; s.threads = OMEGA_GDN_THREADS; s.order = 0; 337 static uint8_t smem[OMEGA_GDN_SHARED_BYTES]; s.smem = smem; s.smem_bytes = OMEGA_GDN_SHARED_BYTES; 338 for (int i = 0; i < SB_N; i++) { 339 const uint64_t a = (uintptr_t)buf[i]; 340 s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a >> 32); 341 s.ranges[i].lo = (uintptr_t)buf[i]; s.ranges[i].hi = (uintptr_t)buf[i] + sz[i]; 342 } 343 s.nranges = SB_N; 344 memcpy(&s.cbank[OMEGA_GDN_ARG_EPS_L2 / 4], &EPS_L2, 4); memcpy(&s.cbank[OMEGA_GDN_ARG_EPS_NORM / 4], &EPS_N, 4); 345 memcpy(&s.cbank[OMEGA_GDN_ARG_IN_SCALE / 4], &in_scale, 4); memcpy(&s.cbank[OMEGA_GDN_ARG_OUT_SCALE / 4], &out_scale, 4); 346 int err = ws_run(&s, RT, OMEGA_GDN_GRID_Y); 347 if (err) snprintf(msg, n, "%s (pc %d)", s.msg, s.err_pc); 348 g_sim_issues += s.warp_issues; 349 return err; 350 } 351 static void sim_bf16_battery(void) { 352 static BlackwellIRProgram p32, p16; 353 OmegaGdnSpec s32 = { .ratio = GDN_TEST_RATIO }, s16 = { .ratio = GDN_TEST_RATIO, .state_bf16 = 1 }; 354 OmegaGdnShape sh16; 355 CHECK(omega_gdn_shape(&s16, &sh16) == 0 && sh16.state_bytes == T_STATE_BYTES / 2u, "bf16 shape: half the state bytes"); 356 CHECK(omega_blackwell_codegen_gdn_prog(&s32, &p32) == 0 && omega_blackwell_codegen_gdn_prog(&s16, &p16) == 0, "bf16 codegen"); 357 uint32_t f16 = 0, f32 = 0, b16 = 0, b32 = 0; 358 for (size_t i = 0; i < p16.count; i++) { f16 += p16.insns[i].op == BW_IR_F2FP_BF16_PACK; b16 += p16.insns[i].op == BW_IR_SHFL_BFLY; } 359 for (size_t i = 0; i < p32.count; i++) { f32 += p32.insns[i].op == BW_IR_F2FP_BF16_PACK; b32 += p32.insns[i].op == BW_IR_SHFL_BFLY; } 360 CHECK(f16 == f32 + 32u && b16 == b32, "bf16: 32 more F2FP.BF16 packs than the f32 state, same butterflies (%u vs %u, %u vs %u)", f16, f32, b16, b32); 361 Layer L; layer_make(&L, 21); 362 size_t sz[SB_N] = { CD * 4u, VD * 4u, VH * 8u, T_PAR_BYTES, T_TAIL_BYTES, T_STATE_BYTES, VD * 4u }; 363 size_t sz16[SB_N]; memcpy(sz16, sz, sizeof sz); sz16[SB_STATE] = T_STATE_BYTES / 2u; 364 uint8_t *a[SB_N], *b[SB_N]; 365 for (int i = 0; i < SB_N; i++) { a[i] = aligned_alloc(256, (sz[i] + 255) & ~(size_t)255); b[i] = aligned_alloc(256, (sz[i] + 255) & ~(size_t)255); } 366 omega_gdn_pack_params_r(GDN_TEST_RATIO, L.conv_w, L.a_log, L.dtb, L.nw, b[SB_PAR]); 367 omega_gdn_pack_tail_r(GDN_TEST_RATIO, L.tail0, (float *)b[SB_TAIL]); 368 uint16_t *st16 = (uint16_t *)b[SB_STATE]; 369 for (size_t i = 0; i < NS; i++) st16[i] = t_bf16_rn(L.state0[i]); 370 memcpy(a[SB_PAR], b[SB_PAR], T_PAR_BYTES); 371 int err = 0; uint32_t bad_out = 0, bad_tail = 0, bad_state = 0, rounded = 0; 372 char msg[300] = ""; 373 for (uint32_t st = 0; st < 3 && !err; st++) { 374 Inp in; inp_make(&in, 1000 + st, 1.0f); 375 for (int k = 0; k < 2; k++) { 376 uint8_t **x = k ? b : a; 377 memcpy(x[SB_QKV], in.qkv, sz[SB_QKV]); memcpy(x[SB_Z], in.z, sz[SB_Z]); memcpy(x[SB_AB], in.ab, sz[SB_AB]); 378 } 379 memcpy(a[SB_TAIL], b[SB_TAIL], T_TAIL_BYTES); 380 for (size_t i = 0; i < NS; i++) ((float *)a[SB_STATE])[i] = t_bf16_f(st16[i]); 381 err = sim_one(&p32, a, sz, 1.0f, 1.0f, msg, sizeof msg); 382 if (!err) err = sim_one(&p16, b, sz16, 1.0f, 1.0f, msg, sizeof msg); 383 if (!err) { 384 for (size_t i = 0; i < VD; i++) bad_out += memcmp(&((float *)a[SB_OUT])[i], &((float *)b[SB_OUT])[i], 4) != 0; 385 bad_tail += memcmp(a[SB_TAIL], b[SB_TAIL], T_TAIL_BYTES) != 0; 386 for (size_t i = 0; i < NS; i++) { 387 const float f = ((float *)a[SB_STATE])[i]; 388 bad_state += st16[i] != t_bf16_rn(f); 389 rounded += t_bf16_f(st16[i]) != f; 390 } 391 } 392 inp_free(&in); 393 } 394 printf("sim bf16 state: 3 steps, outputs differing %u, tails differing %u, stored states != bf16_rn(f32) %u, rounded %u of %u%s%s\n", 395 bad_out, bad_tail, bad_state, rounded, 3u * NS, err ? " ERROR " : "", msg); 396 CHECK(err == 0, "sim bf16 ran (%s)", msg); 397 CHECK(bad_out == 0 && bad_tail == 0 && bad_state == 0, "sim bf16 == f32 program + round to nearest even on store"); 398 CHECK(rounded > NS, "sim bf16 really rounds (%u of %u values changed)", rounded, 3u * NS); 399 for (int i = 0; i < SB_N; i++) { free(a[i]); free(b[i]); } 400 layer_free(&L); 401 } 402 403 /* ---- prefill on the simulator (omega#543): prep, scan, post over T tokens against T oracle steps ---- */ 404 typedef struct { uint32_t off; void *p; size_t bytes; } SimBuf; 405 typedef struct { uint32_t off; uint32_t word; } SimWord; 406 static int sim_launch(const BlackwellIRProgram *prog, uint32_t threads, uint32_t gx, uint32_t gy, uint32_t smem_bytes, int order, 407 const SimBuf *b, int nb, const SimWord *w, int nw, char *msg, size_t n) { 408 static uint8_t smem[OMEGA_GDN_SCAN_SHARED_BYTES]; 409 WsSim s; memset(&s, 0, sizeof s); 410 s.p = prog; s.threads = threads; s.order = order; 411 memset(smem, 0xcd, sizeof smem); /* poison: a read before a write shows */ 412 s.smem = smem; s.smem_bytes = smem_bytes ? smem_bytes : 4u; 413 for (int i = 0; i < nb; i++) { 414 const uint64_t a = (uintptr_t)b[i].p; 415 s.cbank[b[i].off / 4] = (uint32_t)a; s.cbank[b[i].off / 4 + 1] = (uint32_t)(a >> 32); 416 s.ranges[i].lo = a; s.ranges[i].hi = a + b[i].bytes; 417 } 418 s.nranges = nb; 419 for (int i = 0; i < nw; i++) s.cbank[w[i].off / 4] = w[i].word; 420 int err = ws_run(&s, gx, gy); 421 if (err) snprintf(msg, n, "%.150s (pc %d)", s.msg, s.err_pc); 422 g_sim_issues += s.warp_issues; 423 return err; 424 } 425 static uint32_t fw(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 426 static int sim_prefill(int mutant, uint32_t T, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) { 427 static BlackwellIRProgram pp, ps, pq; 428 OmegaGdnSpec spec = { .mutant = mutant, .ratio = GDN_TEST_RATIO }; 429 if (omega_blackwell_codegen_gdn_prep_prog(&spec, &pp) != 0 || omega_blackwell_codegen_gdn_scan_prog(&spec, &ps) != 0 || 430 omega_blackwell_codegen_gdn_post_prog(&spec, &pq) != 0) { snprintf(msg, n, "codegen refused"); return -1; } 431 Layer L; layer_make(&L, 21); 432 DState d; dstate_init(&d, &L); 433 const uint32_t T8 = (T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK; 434 const size_t s_qkv = (size_t)T * CD * 4u, s_z = (size_t)T * VD * 4u, s_ab = (size_t)T * VH * 8u; 435 const size_t s_rec = (size_t)T8 * VH * OMEGA_GDN_REC_BYTES, s_o = (size_t)T * VD * 4u; 436 float *qkv = aligned_alloc(256, s_qkv), *z = aligned_alloc(256, s_z), *ab = aligned_alloc(256, s_ab); 437 uint8_t *par = aligned_alloc(256, T_PAR_BYTES), *tail = aligned_alloc(256, T_TAIL_BYTES); 438 uint8_t *state = aligned_alloc(256, T_STATE_BYTES), *rec = aligned_alloc(256, s_rec); 439 float *o = aligned_alloc(256, s_o), *out = aligned_alloc(256, s_o), *out0 = malloc(s_o); 440 uint8_t *tail0 = malloc(T_TAIL_BYTES), *state0 = malloc(T_STATE_BYTES); 441 double *y = malloc((size_t)T * VD * 8); 442 float *tref = malloc((size_t)NT * 4); 443 for (uint32_t t = 0; t < T; t++) { 444 Inp in; inp_make(&in, 1000 + t, in_scale); 445 memcpy(qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(z + (size_t)t * VD, in.z, VD * 4u); memcpy(ab + (size_t)t * VH * 2u, in.ab, VH * 8u); 446 oracle_step(&L, &d, &in, in_scale, out_scale, y + (size_t)t * VD); 447 inp_free(&in); 448 } 449 omega_gdn_pack_params_r(GDN_TEST_RATIO, L.conv_w, L.a_log, L.dtb, L.nw, par); 450 int err = 0; 451 for (int order = 0; order < 2 && !err; order++) { 452 omega_gdn_pack_tail_r(GDN_TEST_RATIO, L.tail0, (float *)tail); 453 memcpy(state, L.state0, T_STATE_BYTES); 454 memset(rec, 0xcd, s_rec); 455 for (size_t i = 0; i < (size_t)T * VD; i++) { o[i] = -999.0f; out[i] = -999.0f; } 456 const SimBuf bp[] = { { OMEGA_GDN_PREP_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_PREP_ARG_AB, ab, s_ab }, { OMEGA_GDN_PREP_ARG_PAR, par, T_PAR_BYTES }, 457 { OMEGA_GDN_PREP_ARG_TAIL, tail, T_TAIL_BYTES }, { OMEGA_GDN_PREP_ARG_REC, rec, s_rec } }; 458 const SimWord wp[] = { { OMEGA_GDN_PREP_ARG_T, T }, { OMEGA_GDN_PREP_ARG_EPS_L2, fw(EPS_L2) }, { OMEGA_GDN_PREP_ARG_IN_SCALE, fw(in_scale) } }; 459 const SimBuf bs[] = { { OMEGA_GDN_SCAN_ARG_REC, rec, s_rec }, { OMEGA_GDN_SCAN_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_SCAN_ARG_TAIL, tail, T_TAIL_BYTES }, 460 { OMEGA_GDN_SCAN_ARG_STATE, state, T_STATE_BYTES }, { OMEGA_GDN_SCAN_ARG_O, o, s_o } }; 461 const SimWord wsc[] = { { OMEGA_GDN_SCAN_ARG_T, T }, { OMEGA_GDN_SCAN_ARG_IN_SCALE, fw(in_scale) } }; 462 const SimBuf bq[] = { { OMEGA_GDN_POST_ARG_O, o, s_o }, { OMEGA_GDN_POST_ARG_Z, z, s_z }, { OMEGA_GDN_POST_ARG_PAR, par, T_PAR_BYTES }, 463 { OMEGA_GDN_POST_ARG_OUT, out, s_o } }; 464 const SimWord wq[] = { { OMEGA_GDN_POST_ARG_EPS_NORM, fw(EPS_N) }, { OMEGA_GDN_POST_ARG_IN_SCALE, fw(in_scale) }, { OMEGA_GDN_POST_ARG_OUT_SCALE, fw(out_scale) } }; 465 char m2[200] = ""; 466 if (sim_launch(&pp, OMEGA_GDN_PREP_THREADS, (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, KH, OMEGA_GDN_PREP_SHARED_BYTES, order, bp, 5, wp, 3, m2, sizeof m2)) { snprintf(msg, n, "prep: %s", m2); err = -1; break; } 467 if (sim_launch(&ps, OMEGA_GDN_THREADS, RT, OMEGA_GDN_GRID_Y, OMEGA_GDN_SCAN_SHARED_BYTES, order, bs, 5, wsc, 2, m2, sizeof m2)) { snprintf(msg, n, "scan: %s", m2); err = -1; break; } 468 if (sim_launch(&pq, OMEGA_GDN_THREADS, T, (VH / 8u), 0, order, bq, 4, wq, 3, m2, sizeof m2)) { snprintf(msg, n, "post: %s", m2); err = -1; break; } 469 size_t unwritten = 0; for (size_t i = 0; i < (size_t)T * VD; i++) if (out[i] == -999.0f) unwritten++; 470 if (unwritten) { snprintf(msg, n, "%zu output words never written", unwritten); err = -1; break; } 471 if (order == 0) { memcpy(out0, out, s_o); memcpy(tail0, tail, T_TAIL_BYTES); memcpy(state0, state, T_STATE_BYTES); } 472 else if (memcmp(out0, out, s_o) || memcmp(tail0, tail, T_TAIL_BYTES) || memcmp(state0, state, T_STATE_BYTES)) { 473 snprintf(msg, n, "the warp order changed the result"); err = -1; 474 } 475 } 476 if (!err) { 477 double *ts = malloc((size_t)NT * 8); 478 for (size_t i = 0; i < NT; i++) ts[i] = d.tail[i]; 479 worst->out = nerr(out, y, (size_t)T * VD); 480 worst->tail = omega_gdn_unpack_tail_r(GDN_TEST_RATIO, (const float *)tail, tref) != 0 ? INFINITY : nerr(tref, ts, NT); 481 worst->state = nerr((const float *)state, d.state, NS); 482 free(ts); 483 } 484 free(qkv); free(z); free(ab); free(par); free(tail); free(state); free(rec); free(o); free(out); free(out0); 485 free(tail0); free(state0); free(y); free(tref); 486 dstate_free(&d); layer_free(&L); 487 return err; 488 } 489 /* L313: the bf16 prefill scan equals the f32 scan run from the widened state, its final state rounded to nearest even 490 * (the scan holds S in registers for the whole prompt: one widen on load, one round on store). */ 491 static void sim_prefill_bf16(uint32_t T) { 492 static BlackwellIRProgram pp, p32, p16; 493 OmegaGdnSpec s32 = { .ratio = GDN_TEST_RATIO }, s16 = { .ratio = GDN_TEST_RATIO, .state_bf16 = 1 }; 494 CHECK(omega_blackwell_codegen_gdn_prep_prog(&s32, &pp) == 0 && omega_blackwell_codegen_gdn_scan_prog(&s32, &p32) == 0 && 495 omega_blackwell_codegen_gdn_scan_prog(&s16, &p16) == 0, "bf16 prefill codegen"); 496 Layer L; layer_make(&L, 21); 497 const uint32_t T8 = (T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK; 498 const size_t s_qkv = (size_t)T * CD * 4u, s_ab = (size_t)T * VH * 8u, s_rec = (size_t)T8 * VH * OMEGA_GDN_REC_BYTES, s_o = (size_t)T * VD * 4u; 499 float *qkv = aligned_alloc(256, s_qkv), *ab = aligned_alloc(256, s_ab); 500 uint8_t *par = aligned_alloc(256, T_PAR_BYTES), *tail = aligned_alloc(256, T_TAIL_BYTES), *tail2 = aligned_alloc(256, T_TAIL_BYTES); 501 uint8_t *rec = aligned_alloc(256, s_rec), *s32b = aligned_alloc(256, T_STATE_BYTES), *s16b = aligned_alloc(256, T_STATE_BYTES / 2u); 502 float *o32 = aligned_alloc(256, s_o), *o16 = aligned_alloc(256, s_o); 503 for (uint32_t t = 0; t < T; t++) { 504 Inp in; inp_make(&in, 1000 + t, 1.0f); 505 memcpy(qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(ab + (size_t)t * VH * 2u, in.ab, VH * 8u); 506 inp_free(&in); 507 } 508 omega_gdn_pack_params_r(GDN_TEST_RATIO, L.conv_w, L.a_log, L.dtb, L.nw, par); 509 omega_gdn_pack_tail_r(GDN_TEST_RATIO, L.tail0, (float *)tail); 510 memcpy(tail2, tail, T_TAIL_BYTES); 511 uint16_t *st16 = (uint16_t *)s16b; float *st32 = (float *)s32b; 512 for (size_t i = 0; i < NS; i++) { st16[i] = t_bf16_rn(L.state0[i]); st32[i] = t_bf16_f(st16[i]); } 513 memset(rec, 0xcd, s_rec); 514 char m2[200] = ""; int err = 0; 515 const SimBuf bp[] = { { OMEGA_GDN_PREP_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_PREP_ARG_AB, ab, s_ab }, { OMEGA_GDN_PREP_ARG_PAR, par, T_PAR_BYTES }, 516 { OMEGA_GDN_PREP_ARG_TAIL, tail, T_TAIL_BYTES }, { OMEGA_GDN_PREP_ARG_REC, rec, s_rec } }; 517 const SimWord wp[] = { { OMEGA_GDN_PREP_ARG_T, T }, { OMEGA_GDN_PREP_ARG_EPS_L2, fw(EPS_L2) }, { OMEGA_GDN_PREP_ARG_IN_SCALE, fw(1.0f) } }; 518 const SimWord wsc[] = { { OMEGA_GDN_SCAN_ARG_T, T }, { OMEGA_GDN_SCAN_ARG_IN_SCALE, fw(1.0f) } }; 519 err = sim_launch(&pp, OMEGA_GDN_PREP_THREADS, (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, KH, OMEGA_GDN_PREP_SHARED_BYTES, 0, bp, 5, wp, 3, m2, sizeof m2); 520 for (int k = 0; k < 2 && !err; k++) { 521 const SimBuf bs[] = { { OMEGA_GDN_SCAN_ARG_REC, rec, s_rec }, { OMEGA_GDN_SCAN_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_SCAN_ARG_TAIL, k ? tail2 : tail, T_TAIL_BYTES }, 522 { OMEGA_GDN_SCAN_ARG_STATE, k ? s16b : s32b, k ? T_STATE_BYTES / 2u : T_STATE_BYTES }, { OMEGA_GDN_SCAN_ARG_O, k ? o16 : o32, s_o } }; 523 err = sim_launch(k ? &p16 : &p32, OMEGA_GDN_THREADS, RT, OMEGA_GDN_GRID_Y, OMEGA_GDN_SCAN_SHARED_BYTES, 0, bs, 5, wsc, 2, m2, sizeof m2); 524 } 525 uint32_t bad_state = 0; 526 for (size_t i = 0; i < NS && !err; i++) bad_state += st16[i] != t_bf16_rn(st32[i]); 527 const int same_o = !err && memcmp(o32, o16, s_o) == 0, same_tail = !err && memcmp(tail, tail2, T_TAIL_BYTES) == 0; 528 printf("sim prefill bf16 T %u: scan output %s, tail %s, final states != bf16_rn(f32) %u%s%s\n", T, same_o ? "identical" : "DIFFERS", 529 same_tail ? "identical" : "DIFFERS", bad_state, err ? " ERROR " : "", m2); 530 CHECK(err == 0 && same_o && same_tail && bad_state == 0, "sim prefill bf16 T %u == f32 scan + round on store", T); 531 free(qkv); free(ab); free(par); free(tail); free(tail2); free(rec); free(s32b); free(s16b); free(o32); free(o16); 532 layer_free(&L); 533 } 534 static void sim_prefill_battery(void) { 535 struct { int mutant; uint32_t T; float in_scale, out_scale; const char *name; } pts[] = { 536 { 0, 37, 1.0f, 1.0f, "prefill T 37" }, { 0, 3, 1.0f, 1.0f, "prefill T 3" }, 537 { 0, 17, 0.`0009765625f`, `18446744073709551616`.0f, "prefill T 17, in 2^-10, out 2^64" }, 538 { 1, 19, 1.0f, 1.0f, "prefill mutant 1 (no decay)" }, { 2, 19, 1.0f, 1.0f, "prefill mutant 2 (beta 1)" }, 539 { 3, 19, 1.0f, 1.0f, "prefill mutant 3 (gate z)" }, { 4, 19, 1.0f, 1.0f, "prefill mutant 4 (tail kept)" }, 540 }; 541 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { 542 char msg[300] = ""; Errs w = { 0, 0, 0 }; 543 double t0 = now_ms(); 544 int err = sim_prefill(pts[i].mutant, pts[i].T, pts[i].in_scale, pts[i].out_scale, &w, msg, sizeof msg); 545 printf("sim %-44s err out %.3g tail %.3g state %.3g (%.0f ms)%s%s\n", pts[i].name, w.out, w.tail, w.state, now_ms() - t0, err ? " ERROR " : "", msg); 546 CHECK(err == 0, "sim %s ran (%s)", pts[i].name, msg); 547 if (pts[i].mutant == 0) CHECK(errs_ok(&w), "sim %s within tolerance", pts[i].name); 548 else CHECK(!errs_ok(&w), "sim %s caught", pts[i].name); 549 } 550 } 551 552 /* ---- the state copy / zero on the simulator: 3 CTAs; a 2-CTA launch leaves the last 1024 words (negative control) ---- */ 553 static void sim_copy_battery(void) { 554 static BlackwellIRProgram cp; 555 enum { W = 3 * OMEGA_GDN_COPY_WORDS_PER_CTA }; 556 static uint32_t src[W], dst[W]; 557 uint32_t x = 12345u; 558 for (unsigned i = 0; i < W; i++) { x = x * 1664525u + 1013904223u; src[i] = x; } 559 char msg[200] = ""; 560 for (int kind = OMEGA_GDN_COPY; kind <= OMEGA_GDN_ZERO; kind++) { 561 OmegaGdnSpec sp = { .copy = kind }; 562 CHECK(omega_blackwell_codegen_gdn_copy_prog(&sp, &cp) == 0, "copy kind %d codegen", kind); 563 for (uint32_t gx = 3; gx >= 2; gx--) { 564 memset(dst, 0xcd, sizeof dst); 565 SimBuf b[2] = { { OMEGA_GDN_COPY_ARG_SRC, src, sizeof src }, { OMEGA_GDN_COPY_ARG_DST, dst, sizeof dst } }; 566 int err = sim_launch(&cp, OMEGA_GDN_COPY_THREADS, gx, 1, 0, 0, b, 2, NULL, 0, msg, sizeof msg); 567 unsigned bad = 0; 568 for (unsigned i = 0; i < W; i++) if (dst[i] != (kind == OMEGA_GDN_COPY ? src[i] : 0u)) bad++; 569 CHECK(err == 0, "sim copy kind %d grid %u ran (%s)", kind, gx, msg); 570 if (gx == 3) CHECK(bad == 0, "sim copy kind %d: every word (%u wrong)", kind, bad); 571 else CHECK(bad == OMEGA_GDN_COPY_WORDS_PER_CTA, "sim copy kind %d grid 2 caught: %u words untouched", kind, bad); 572 } 573 } 574 } 575 576 /* ---- chip ---- */ 577 static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, uint32_t steps, uint32_t seed, float in_scale, 578 float out_scale, Errs *worst, float *last_out) { 579 float *out = malloc((size_t)VD * 4), *tail = malloc((size_t)NT * 4), *state = malloc((size_t)NS * 4); 580 double *y = malloc((size_t)VD * 8), *td = malloc((size_t)NT * 8); 581 int rc = 0; 582 memset(worst, 0, sizeof *worst); 583 for (uint32_t st = 0; st < steps && rc == 0; st++) { 584 Inp in; inp_make(&in, seed + st, in_scale); 585 rc = omega_gpu_gdn_step(g, layer, in.qkv, in.z, in.ab, EPS_L2, EPS_N, in_scale, out_scale, out, NULL); 586 if (rc == 0) rc = omega_gpu_gdn_get_state(g, layer, tail, state); 587 if (rc == 0) { 588 oracle_step(L, d, &in, in_scale, out_scale, y); 589 for (size_t i = 0; i < NT; i++) td[i] = d->tail[i]; 590 Errs e = { nerr(out, y, VD), nerr(tail, td, NT), nerr(state, d->state, NS) }; 591 if (e.out > worst->out) worst->out = e.out; 592 if (e.tail > worst->tail) worst->tail = e.tail; 593 if (e.state > worst->state) worst->state = e.state; 594 } 595 inp_free(&in); 596 } 597 if (last_out) memcpy(last_out, out, (size_t)VD * 4); 598 free(out); free(tail); free(state); free(y); free(td); 599 return rc; 600 } 601 /* ---- prefill on the chip (omega#543) ---- */ 602 typedef struct { float *qkv, *z, *ab; } PfIn; 603 static void pf_in_make(PfIn *p, uint32_t T, uint32_t seed, float in_scale) { 604 p->qkv = malloc((size_t)T * CD * 4); p->z = malloc((size_t)T * VD * 4); p->ab = malloc((size_t)T * VH * 8); 605 for (uint32_t t = 0; t < T; t++) { 606 Inp in; inp_make(&in, seed + t, in_scale); 607 memcpy(p->qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(p->z + (size_t)t * VD, in.z, VD * 4u); memcpy(p->ab + (size_t)t * VH * 2u, in.ab, VH * 8u); 608 inp_free(&in); 609 } 610 } 611 static void pf_in_free(PfIn *p) { free(p->qkv); free(p->z); free(p->ab); } 612 /* oracle over the same T rows; y [T][VD] */ 613 static void pf_oracle(const Layer *L, DState *d, uint32_t T, uint32_t seed, float in_scale, float out_scale, double *y) { 614 for (uint32_t t = 0; t < T; t++) { Inp in; inp_make(&in, seed + t, in_scale); oracle_step(L, d, &in, in_scale, out_scale, y + (size_t)t * VD); inp_free(&in); } 615 } 616 /* prefill T tokens on `layer` from its current state, then `dec` decode steps; worst errors over every row, the tail and 617 * the state after the prefill and after each step */ 618 static int chip_prefill_chain(OmegaGpuGdn *g, uint32_t layer, const Layer *L, uint32_t T, uint32_t dec, uint32_t seed, float in_scale, 619 float out_scale, Errs *worst, float *out_copy) { 620 DState d; dstate_init(&d, L); 621 PfIn p; pf_in_make(&p, T, seed, in_scale); 622 float *out = malloc((size_t)T * VD * 4), *tail = malloc((size_t)NT * 4), *state = malloc((size_t)NS * 4); 623 double *y = malloc((size_t)T * VD * 8), *td = malloc((size_t)NT * 8); 624 memset(worst, 0, sizeof *worst); 625 int rc = omega_gpu_gdn_prefill(g, layer, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, in_scale, out_scale, out, NULL); 626 if (rc == 0) rc = omega_gpu_gdn_get_state(g, layer, tail, state); 627 if (rc == 0) { 628 pf_oracle(L, &d, T, seed, in_scale, out_scale, y); 629 for (size_t i = 0; i < NT; i++) td[i] = d.tail[i]; 630 worst->out = nerr(out, y, (size_t)T * VD); worst->tail = nerr(tail, td, NT); worst->state = nerr(state, d.state, NS); 631 if (out_copy) memcpy(out_copy, out, (size_t)T * VD * 4); 632 } 633 if (rc == 0 && dec) { 634 Errs e; 635 rc = chip_steps(g, layer, L, &d, dec, seed + T, in_scale, out_scale, &e, NULL); 636 if (e.out > worst->out) worst->out = e.out; 637 if (e.tail > worst->tail) worst->tail = e.tail; 638 if (e.state > worst->state) worst->state = e.state; 639 } 640 free(out); free(tail); free(state); free(y); free(td); 641 pf_in_free(&p); dstate_free(&d); 642 return rc; 643 } 644 static void chip_prefill(OmegaGpuGdn *g, Layer *L, FILE *out, int *first, uint32_t reps) { 645 #define PPOINT(name, e, extra) do { if (out) { fprintf(out, "%s{\"name\":\"%s\",\"err_out\":%.6g,\"err_tail\":%.6g,\"err_state\":%.6g%s}", *first ? "" : ",", name, (e).out, (e).tail, (e).state, extra); *first = 0; } } while (0) 646 struct { uint32_t layer, T, dec, seed; float in_scale, out_scale; const char *name; } pp[] = { 647 { 1, 37, 3, 2000, 1.0f, 1.0f, "prefill layer 1, T 37, then 3 decode steps" }, 648 { 0, 3, 1, 2100, 1.0f, 1.0f, "prefill layer 0, T 3, then 1 decode step" }, 649 { 0, 41, 0, 2200, 0.`0009765625f`, `18446744073709551616`.0f, "prefill layer 0, T 41, in 2^-10 out 2^64" }, 650 }; 651 for (size_t i = 0; i < sizeof pp / sizeof pp[0]; i++) { 652 CHECK(omega_gpu_gdn_set_state(g, pp[i].layer, L[pp[i].layer].tail0, L[pp[i].layer].state0) == 0, "set state for %s", pp[i].name); 653 Errs e; 654 int rc = chip_prefill_chain(g, pp[i].layer, &L[pp[i].layer], pp[i].T, pp[i].dec, pp[i].seed, pp[i].in_scale, pp[i].out_scale, &e, NULL); 655 printf("chip %-48s rc=%s err out %.3g tail %.3g state %.3g %s\n", pp[i].name, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state, rc ? omega_gpu_gdn_last_error() : ""); 656 CHECK(rc == 0 && errs_ok(&e), "chip %s", pp[i].name); 657 PPOINT(pp[i].name, e, ""); 658 } 659 /* rerun from the same state: bit-identical rows */ 660 { 661 float *a = malloc((size_t)37 * VD * 4), *b = malloc((size_t)37 * VD * 4); 662 Errs e1, e2; 663 CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun"); 664 int rc = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e1, a); 665 CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun 2"); 666 int rc2 = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e2, b); 667 CHECK(rc == 0 && rc2 == 0 && memcmp(a, b, (size_t)37 * VD * 4) == 0, "prefill rerun from the same state is bit-identical"); 668 free(a); free(b); 669 } 670 /* mutants */ 671 for (int m = 1; m <= 4; m++) { 672 omega_gpu_gdn_test_set_mutant(m); 673 CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state prefill mutant %d", m); 674 Errs e; 675 int rc = chip_prefill_chain(g, 1, &L[1], 19, 0, 2300, 1.0f, 1.0f, &e, NULL); 676 char nm[40]; snprintf(nm, sizeof nm, "prefill mutant %d", m); 677 printf("chip %-48s rc=%s err out %.3g tail %.3g state %.3g\n", nm, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state); 678 CHECK(rc == 0 && !errs_ok(&e), "chip prefill mutant %d caught", m); 679 PPOINT(nm, e, ""); 680 } 681 omega_gpu_gdn_test_set_mutant(0); 682 /* the record path: prefill 37 tokens and one decode step in one command list on layer 0 */ 683 { 684 const uint32_t T = 37; 685 CHECK(omega_gpu_gdn_set_state(g, 0, L[0].tail0, L[0].state0) == 0, "set state for prefill record"); 686 CHECK(omega_gpu_gdn_prefill_reserve(g, 64) == 0, "prefill reserve 64 tokens"); 687 PfIn p; pf_in_make(&p, T, 2400, 1.0f); 688 Inp nx; inp_make(&nx, 2400 + T, 1.0f); 689 DState d; dstate_init(&d, &L[0]); 690 double *y = malloc((size_t)T * VD * 8), *y1 = malloc((size_t)VD * 8); 691 pf_oracle(&L[0], &d, T, 2400, 1.0f, 1.0f, y); 692 oracle_step(&L[0], &d, &nx, 1.0f, 1.0f, y1); 693 /* warm all four kernels on layer 1 (a cache miss inside the record is refused) */ 694 float *tmp = malloc((size_t)T * VD * 4); 695 CHECK(omega_gpu_gdn_prefill(g, 1, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "prefill warm-up"); 696 CHECK(omega_gpu_gdn_step(g, 1, nx.qkv, nx.z, nx.ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "decode warm-up"); 697 OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */ 698 const int cl_ok = omega_gpu_cmdlist_create(8, &cl) == 0; 699 omega_gpu_session_lock(); 700 OmegaGpuMem inb; memset(&inb, 0, sizeof inb); 701 const size_t s_qkv = (size_t)T * CD * 4, s_z = (size_t)T * VD * 4, s_ab = (size_t)T * VH * 8; 702 const size_t o_nx = (s_qkv + s_z + s_ab + 255) & ~(size_t)255; 703 int ok = cl_ok && omega_gpu_session_open() != NULL && omega_gpu_session_alloc(o_nx + (size_t)CD * 4 + VD * 4 + VH * 8, &inb) == 0; 704 if (ok) { 705 uint8_t *c = inb.cpu; 706 memcpy(c, p.qkv, s_qkv); memcpy(c + s_qkv, p.z, s_z); memcpy(c + s_qkv + s_z, p.ab, s_ab); 707 memcpy(c + o_nx, nx.qkv, (size_t)CD * 4); memcpy(c + o_nx + CD * 4, nx.z, (size_t)VD * 4); memcpy(c + o_nx + CD * 4 + VD * 4, nx.ab, VH * 8); 708 ok = omega_gpu_session_sync_to_gpu(&inb) == 0; 709 } 710 uint64_t pf_out = 0, dec_out = 0; 711 if (ok) ok = omega_gpu_gdn_prefill_record(cl, g, 0, T, inb.va, inb.va + s_qkv, inb.va + s_qkv + s_z, EPS_L2, EPS_N, 1.0f, 1.0f, &pf_out) == 0; 712 if (ok) ok = omega_gpu_gdn_record(cl, g, 0, inb.va + o_nx, inb.va + o_nx + CD * 4, inb.va + o_nx + CD * 4 + VD * 4, EPS_L2, EPS_N, 1.0f, 1.0f, &dec_out) == 0; 713 OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts); 714 OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc); 715 if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0; 716 if (inb.cpu) omega_gpu_session_free(&inb); 717 omega_gpu_session_unlock(); 718 if (cl) omega_gpu_cmdlist_free(cl); 719 CHECK(ok, "record prefill + decode step and submit (%s)", ok ? "" : omega_gpu_gdn_last_error()); 720 if (ok) { 721 float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)T * VD * 4), *y1o = malloc((size_t)VD * 4); 722 double *td = malloc((size_t)NT * 8); 723 ok = omega_gpu_gdn_get_state(g, 0, t, s) == 0 && omega_gpu_gdn_prefill_read_out(g, T, yo) == 0 && omega_gpu_gdn_read_out(g, y1o) == 0; 724 for (size_t i = 0; i < NT; i++) td[i] = d.tail[i]; 725 Errs e = { nerr(yo, y, (size_t)T * VD), nerr(t, td, NT), nerr(s, d.state, NS) }; 726 const double e1 = nerr(y1o, y1, VD); 727 if (e1 > e.out) e.out = e1; 728 printf("chip %-48s err out %.3g tail %.3g state %.3g\n", "record path, prefill 37 + 1 decode step", e.out, e.tail, e.state); 729 CHECK(ok && errs_ok(&e), "record path: prefill then a decode step match the oracle"); 730 PPOINT("record path, prefill 37 + 1 decode step", e, ""); 731 free(t); free(s); free(yo); free(y1o); free(td); 732 } 733 free(tmp); free(y); free(y1); dstate_free(&d); pf_in_free(&p); inp_free(&nx); 734 /* the reservation pins the scratch: a longer prefill is refused, not reallocated */ 735 PfIn q; pf_in_make(&q, 65, 2500, 1.0f); 736 float *o65 = malloc((size_t)65 * VD * 4); 737 CHECK(omega_gpu_gdn_prefill(g, 1, 65, q.qkv, q.z, q.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o65, NULL) == OMEGA_GPU_GDN_TOO_LARGE, "prefill past the reservation is refused"); 738 free(o65); pf_in_free(&q); 739 } 740 /* timing per launch at three prompt lengths (a fresh, unreserved scratch per length) */ 741 const uint32_t Ts[3] = { 512, 2048, 8192 }; 742 for (unsigned k = 0; k < 3; k++) { 743 const uint32_t T = Ts[k], nrep = reps < 5 ? reps : 5; 744 CHECK(omega_gpu_gdn_prefill_reserve(g, T) == 0, "reserve %u tokens", T); 745 PfIn p; pf_in_make(&p, T, 3000, 1.0f); 746 float *o = malloc((size_t)T * VD * 4); 747 uint64_t e[3][5], c[5]; 748 uint32_t done = 0; 749 OmegaGpuGdnPfInfo info; 750 for (uint32_t r = 0; r < nrep; r++) { 751 if (omega_gpu_gdn_prefill(g, 0, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o, &info) != 0) break; 752 for (unsigned j = 0; j < 3; j++) e[j][done] = info.elapsed_ns[j]; 753 c[done] = info.call_ns; done++; 754 } 755 CHECK(done == nrep, "prefill T %u timing reps ran (%u of %u): %s", T, done, nrep, done == nrep ? "" : omega_gpu_gdn_last_error()); 756 if (done) { 757 for (unsigned j = 0; j < 3; j++) qsort(e[j], done, 8, cmp_u64); 758 qsort(c, done, 8, cmp_u64); 759 const double us[3] = { e[0][done / 2] / 1e3, e[1][done / 2] / 1e3, e[2][done / 2] / 1e3 }, tot = us[0] + us[1] + us[2]; 760 printf("timing prefill T %u (medians of %u): prep %.1f us, scan %.1f us, post %.1f us, chip total %.1f us = %.2f us/token, call %.1f us; insns %u/%u/%u, registers %u/%u/%u\n", 761 T, done, us[0], us[1], us[2], tot, tot / T, c[done / 2] / 1e3, info.insns[0], info.insns[1], info.insns[2], info.gprs[0], info.gprs[1], info.gprs[2]); 762 if (out) fprintf(out, "%s{\"name\":\"timing prefill\",\"tokens\":%u,\"reps\":%u,\"prep_us\":%.3f,\"scan_us\":%.3f,\"post_us\":%.3f,\"chip_us\":%.3f,\"us_per_token\":%.4f,\"call_us\":%.3f,\"insns\":[%u,%u,%u],\"gprs\":[%u,%u,%u]}", 763 *first ? "" : ",", T, done, us[0], us[1], us[2], tot, tot / T, c[done / 2] / 1e3, info.insns[0], info.insns[1], info.insns[2], info.gprs[0], info.gprs[1], info.gprs[2]); 764 *first = 0; 765 } 766 free(o); pf_in_free(&p); 767 } 768 #undef PPOINT 769 } 770 /* ---- L313 bf16 state on the chip: a bf16 GDN (AIEN_OMEGA_GDN_STATE_BF16=1 at alloc) against an f32 GDN started from the 771 * same (bf16-representable) state, 4 decode steps then a 19-token prefill, re-synced after every call: outputs and tails 772 * bit-identical, the bf16 state equal to bf16_rn of the f32 state. Then the chip time of one decode step of each, interleaved. ---- */ 773 static int chip_bf16(const char *out_path, uint32_t reps) { 774 OmegaGpuGdn *g16 = NULL, *g32 = NULL; 775 setenv("AIEN_OMEGA_GDN_STATE_BF16", "1", 1); 776 int rc = omega_gpu_gdn_alloc_ratio(1, GDN_TEST_RATIO, &g16); 777 unsetenv("AIEN_OMEGA_GDN_STATE_BF16"); 778 if (rc == 0) rc = omega_gpu_gdn_alloc_ratio(1, GDN_TEST_RATIO, &g32); 779 CHECK(rc == 0, "bf16 + f32 gdn alloc (%s)", rc ? omega_gpu_gdn_last_error() : ""); 780 if (rc) return 1; 781 Layer L; layer_make(&L, 41); 782 float *st = malloc((size_t)NS * 4), *s16 = malloc((size_t)NS * 4), *s32 = malloc((size_t)NS * 4); 783 float *t16 = malloc((size_t)NT * 4), *t32 = malloc((size_t)NT * 4); 784 for (size_t i = 0; i < NS; i++) st[i] = t_bf16_f(t_bf16_rn(L.state0[i])); 785 for (int k = 0; k < 2; k++) { 786 OmegaGpuGdn *g = k ? g32 : g16; 787 CHECK(omega_gpu_gdn_set_layer(g, 0, L.conv_w, L.a_log, L.dtb, L.nw) == 0 && omega_gpu_gdn_set_state(g, 0, L.tail0, st) == 0, "bf16 setup %d", k); 788 } 789 const uint32_t T = 19; 790 float *o16 = malloc((size_t)T * VD * 4), *o32 = malloc((size_t)T * VD * 4); 791 uint32_t bad_out = 0, bad_tail = 0, bad_state = 0, rounded = 0, calls = 0; 792 double us16 = 0, us32 = 0; 793 for (uint32_t c = 0; c < 5 && rc == 0; c++) { 794 const uint32_t n = c < 4 ? 1u : T; 795 PfIn p; pf_in_make(&p, n, 900 + 37 * c, 1.0f); 796 for (int k = 0; k < 2 && rc == 0; k++) { 797 OmegaGpuGdn *g = k ? g32 : g16; 798 rc = n == 1 ? omega_gpu_gdn_step(g, 0, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, k ? o32 : o16, NULL) 799 : omega_gpu_gdn_prefill(g, 0, n, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, k ? o32 : o16, NULL); 800 } 801 if (rc == 0) rc = omega_gpu_gdn_get_state(g16, 0, t16, s16); 802 if (rc == 0) rc = omega_gpu_gdn_get_state(g32, 0, t32, s32); 803 if (rc == 0) { 804 calls++; 805 for (size_t i = 0; i < (size_t)n * VD; i++) bad_out += memcmp(&o16[i], &o32[i], 4) != 0; 806 bad_tail += memcmp(t16, t32, (size_t)NT * 4) != 0; 807 for (size_t i = 0; i < NS; i++) { bad_state += t_bf16_rn(s32[i]) != t_bf16_rn(s16[i]) || t_bf16_f(t_bf16_rn(s16[i])) != s16[i]; rounded += s16[i] != s32[i]; } 808 rc = omega_gpu_gdn_set_state(g32, 0, t16, s16); /* re-sync: the f32 GDN continues from the bf16 state */ 809 } 810 pf_in_free(&p); 811 } 812 /* chip time of one decode step, bf16 vs f32 state, interleaved (the live server shares the chip): medians of reps */ 813 if (rc == 0) { 814 PfIn p; pf_in_make(&p, 1, 950, 1.0f); 815 uint64_t *k16 = malloc((size_t)reps * 8), *k32 = malloc((size_t)reps * 8), b16 = 0, b32 = 0; 816 uint32_t done = 0; 817 OmegaGpuGdnInfo i16, i32; 818 for (uint32_t r = 0; r < reps; r++) { 819 if (omega_gpu_gdn_step(g16, 0, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o16, &i16) != 0) break; 820 if (omega_gpu_gdn_step(g32, 0, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o32, &i32) != 0) break; 821 k16[done] = i16.elapsed_ns; k32[done] = i32.elapsed_ns; b16 = i16.state_bytes; b32 = i32.state_bytes; done++; 822 } 823 CHECK(done == reps, "bf16 timing reps ran (%u of %u)", done, reps); 824 CHECK(done == 0 || 2 * b16 == b32, "bf16 step moves half the state bytes (%" PRIu64 " vs %" PRIu64 ")", b16, b32); 825 if (done) { 826 qsort(k16, done, 8, cmp_u64); qsort(k32, done, 8, cmp_u64); 827 const double u16 = k16[done / 2] / 1e3, u32 = k32[done / 2] / 1e3; 828 printf("timing bf16 vs f32 state (medians of %u, interleaved): bf16 chip %.1f us (state read + write %.2f MB -> %.1f GB/s), " 829 "f32 chip %.1f us (%.2f MB -> %.1f GB/s); 273 GB/s nominal; bf16 %u insns %u registers\n", 830 done, u16, b16 / 1e6, b16 / (u16 * 1e3), u32, b32 / 1e6, b32 / (u32 * 1e3), i16.insns, i16.gprs); 831 us16 = u16; us32 = u32; 832 } 833 free(k16); free(k32); pf_in_free(&p); 834 } 835 printf("chip bf16 state: %u calls (4 decode steps, 1 prefill of %u), rc=%s, outputs differing %u, tails differing %u, " 836 "states != bf16_rn(f32) %u, rounded %u of %u; decode step chip us (median) bf16 %.1f f32 %.1f %s\n", 837 calls, T, omega_gpu_gdn_rc_name(rc), bad_out, bad_tail, bad_state, rounded, calls * NS, us16, us32, rc ? omega_gpu_gdn_last_error() : ""); 838 CHECK(rc == 0 && calls == 5, "chip bf16 ran"); 839 CHECK(bad_out == 0 && bad_tail == 0 && bad_state == 0, "chip bf16 == f32 + round to nearest even on store"); 840 CHECK(rounded > NS, "chip bf16 really rounds"); 841 if (out_path) { 842 FILE *f = test_out_open(out_path); 843 if (f) { 844 fprintf(f, "{\"receipt\":\"OMEGA_GPU_GDN_BF16_V1\",\"ratio\":%u,\"calls\":%u,\"prefill_tokens\":%u,\"outputs_differing\":%u," 845 "\"tails_differing\":%u,\"states_not_rn\":%u,\"rounded\":%u,\"values\":%u,\"chip_us_bf16\":%.3f,\"chip_us_f32\":%.3f,\"verdict\":\"%s\"}\n", 846 (unsigned)RT, calls, T, bad_out, bad_tail, bad_state, rounded, calls * NS, us16, us32, g_failed ? "FAIL" : "PASS"); 847 fclose(f); 848 } 849 } 850 free(st); free(s16); free(s32); free(t16); free(t32); free(o16); free(o32); 851 layer_free(&L); omega_gpu_gdn_free(g16); omega_gpu_gdn_free(g32); 852 return g_failed ? 1 : 0; 853 } 854 static int chip_run(const char *out_path, uint32_t reps) { 855 FILE *out = out_path ? test_out_open(out_path) : NULL; 856 if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; } 857 if (out) fprintf(out, "{\"receipt\":\"OMEGA_GPU_GDN_V1\",\"reps\":%u,\"tol\":{\"out\":%g,\"tail\":%g,\"state\":%g},\"points\":[", reps, TOL_OUT, TOL_TAIL, TOL_STATE); 858 OmegaGpuGdn *g = NULL; 859 double t0 = now_ms(); 860 int rc = omega_gpu_gdn_alloc_ratio(2, GDN_TEST_RATIO, &g); 861 printf("gdn alloc (2 layers): rc=%s %.1f ms (%s)\n", omega_gpu_gdn_rc_name(rc), now_ms() - t0, rc ? omega_gpu_gdn_last_error() : ""); 862 CHECK(rc == OMEGA_GPU_GDN_OK, "gdn alloc"); 863 if (rc) { if (out) fprintf(out, "],\"verdict\":\"FAIL\"}\n"); return 1; } 864 Layer L[2]; layer_make(&L[0], 31); layer_make(&L[1], 32); 865 for (uint32_t l = 0; l < 2; l++) { 866 CHECK(omega_gpu_gdn_set_layer(g, l, L[l].conv_w, L[l].a_log, L[l].dtb, L[l].nw) == 0, "set layer %u", l); 867 CHECK(omega_gpu_gdn_set_state(g, l, L[l].tail0, L[l].state0) == 0, "set state %u", l); 868 } 869 int first = 1; 870 #define POINT(name, e, extra) do { if (out) { fprintf(out, "%s{\"name\":\"%s\",\"err_out\":%.6g,\"err_tail\":%.6g,\"err_state\":%.6g%s}", first ? "" : ",", name, (e).out, (e).tail, (e).state, extra); first = 0; } } while (0) 871 /* parity: layer 1 first (addressing), then layer 0 with in/out scales */ 872 struct { uint32_t layer, steps, seed; float in_scale, out_scale; const char *name; } pp[] = { 873 { 1, 5, 500, 1.0f, 1.0f, "layer 1, 5 steps" }, 874 { 0, 5, 600, 0.`0009765625f`, `18446744073709551616`.0f, "layer 0, 5 steps, in 2^-10 out 2^64" }, 875 }; 876 float *o1 = malloc((size_t)VD * 4), *o2 = malloc((size_t)VD * 4); 877 for (size_t i = 0; i < 2; i++) { 878 DState d; dstate_init(&d, &L[pp[i].layer]); 879 Errs e; 880 rc = chip_steps(g, pp[i].layer, &L[pp[i].layer], &d, pp[i].steps, pp[i].seed, pp[i].in_scale, pp[i].out_scale, &e, o1); 881 printf("chip %-40s rc=%s err out %.3g tail %.3g state %.3g %s\n", pp[i].name, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state, rc ? omega_gpu_gdn_last_error() : ""); 882 CHECK(rc == 0 && errs_ok(&e), "chip %s", pp[i].name); 883 POINT(pp[i].name, e, ""); 884 dstate_free(&d); 885 } 886 /* rerun from the same state: bit-identical */ 887 { 888 Inp in; inp_make(&in, 700, 1.0f); 889 CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for rerun"); 890 rc = omega_gpu_gdn_step(g, 1, in.qkv, in.z, in.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o1, NULL); 891 CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for rerun 2"); 892 int rc2 = omega_gpu_gdn_step(g, 1, in.qkv, in.z, in.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o2, NULL); 893 CHECK(rc == 0 && rc2 == 0 && memcmp(o1, o2, (size_t)VD * 4) == 0, "rerun from the same state is bit-identical"); 894 inp_free(&in); 895 } 896 /* reset: state and tail zero */ 897 { 898 float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4); 899 rc = omega_gpu_gdn_reset(g); 900 int z = rc == 0 && omega_gpu_gdn_get_state(g, 0, t, s) == 0; 901 for (size_t i = 0; z && i < NT; i++) if (t[i] != 0.0f) z = 0; 902 for (size_t i = 0; z && i < NS; i++) if (s[i] != 0.0f) z = 0; 903 CHECK(z, "reset zeroes the tail and the state"); 904 free(t); free(s); 905 } 906 /* mutants: each must miss */ 907 for (int m = 1; m <= 4; m++) { 908 omega_gpu_gdn_test_set_mutant(m); 909 CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state mutant %d", m); 910 DState d; dstate_init(&d, &L[1]); 911 Errs e; 912 rc = chip_steps(g, 1, &L[1], &d, 2, 800, 1.0f, 1.0f, &e, NULL); 913 char nm[32]; snprintf(nm, sizeof nm, "mutant %d", m); 914 printf("chip %-40s rc=%s err out %.3g tail %.3g state %.3g\n", nm, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state); 915 CHECK(rc == 0 && !errs_ok(&e), "chip mutant %d caught", m); 916 POINT(nm, e, ""); 917 dstate_free(&d); 918 } 919 omega_gpu_gdn_test_set_mutant(0); 920 /* the record path: two chained steps in one command list on layer 0 */ 921 { 922 CHECK(omega_gpu_gdn_set_state(g, 0, L[0].tail0, L[0].state0) == 0, "set state for record"); 923 Inp in[2]; inp_make(&in[0], 900, 1.0f); inp_make(&in[1], 901, 1.0f); 924 DState d; dstate_init(&d, &L[0]); 925 double *y = malloc((size_t)VD * 8); 926 oracle_step(&L[0], &d, &in[0], 1.0f, 1.0f, y); oracle_step(&L[0], &d, &in[1], 1.0f, 1.0f, y); 927 /* warm the kernel cache outside the record (a miss inside is refused) */ 928 float *tmp = malloc((size_t)VD * 4); 929 CHECK(omega_gpu_gdn_step(g, 1, in[0].qkv, in[0].z, in[0].ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "warm-up step"); 930 OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */ 931 const int cl_ok = omega_gpu_cmdlist_create(4, &cl) == 0; 932 omega_gpu_session_lock(); 933 OmegaGpuMem inb[2]; memset(inb, 0, sizeof inb); 934 int ok = cl_ok && omega_gpu_session_open() != NULL; 935 const size_t ib = (size_t)CD * 4 + (size_t)VD * 4 + VH * 8; 936 for (int i = 0; ok && i < 2; i++) { 937 ok = omega_gpu_session_alloc(ib, &inb[i]) == 0; 938 if (ok) { 939 memcpy(inb[i].cpu, in[i].qkv, (size_t)CD * 4); memcpy((uint8_t *)inb[i].cpu + CD * 4, in[i].z, (size_t)VD * 4); 940 memcpy((uint8_t *)inb[i].cpu + CD * 4 + VD * 4, in[i].ab, VH * 8); 941 ok = omega_gpu_session_sync_to_gpu(&inb[i]) == 0; 942 } 943 } 944 uint64_t out_va = 0; 945 for (int i = 0; ok && i < 2; i++) 946 ok = omega_gpu_gdn_record(cl, g, 0, inb[i].va, inb[i].va + CD * 4, inb[i].va + CD * 4 + VD * 4, EPS_L2, EPS_N, 1.0f, 1.0f, &out_va) == 0; 947 OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts); 948 OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc); 949 if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0; 950 for (int i = 0; i < 2; i++) if (inb[i].cpu) omega_gpu_session_free(&inb[i]); 951 omega_gpu_session_unlock(); 952 if (cl) omega_gpu_cmdlist_free(cl); 953 CHECK(ok, "record two steps and submit (%s)", ok ? "" : omega_gpu_gdn_last_error()); 954 if (ok) { 955 float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)VD * 4); 956 double *td = malloc((size_t)NT * 8); 957 ok = omega_gpu_gdn_get_state(g, 0, t, s) == 0 && omega_gpu_gdn_read_out(g, yo) == 0; /* the second step's output */ 958 for (size_t i = 0; i < NT; i++) td[i] = d.tail[i]; 959 Errs e = { nerr(yo, y, VD), nerr(t, td, NT), nerr(s, d.state, NS) }; 960 printf("chip %-40s err out %.3g tail %.3g state %.3g\n", "record path, 2 chained steps", e.out, e.tail, e.state); 961 CHECK(ok && errs_ok(&e), "record path: two chained steps match the oracle"); 962 POINT("record path, 2 chained steps", e, ""); 963 free(t); free(s); free(yo); free(td); 964 } 965 free(tmp); free(y); dstate_free(&d); inp_free(&in[0]); inp_free(&in[1]); 966 } 967 chip_prefill(g, L, out, &first, reps); 968 /* timing */ 969 { 970 Inp in; inp_make(&in, 950, 1.0f); 971 uint64_t *ec = malloc(reps * 8), *ek = malloc(reps * 8); 972 uint32_t done = 0; 973 OmegaGpuGdnInfo info; 974 for (uint32_t r = 0; r < reps; r++) { 975 if (omega_gpu_gdn_step(g, 0, in.qkv, in.z, in.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o1, &info) != 0) break; 976 ek[done] = info.elapsed_ns; ec[done] = info.call_ns; done++; 977 } 978 CHECK(done == reps, "timing reps ran (%u of %u)", done, reps); 979 if (done) { 980 qsort(ek, done, 8, cmp_u64); qsort(ec, done, 8, cmp_u64); 981 const double kus = ek[done / 2] / 1e3, cus = ec[done / 2] / 1e3, gbs = (double)info.state_bytes / (ek[done / 2] * 1e-9) / 1e9; 982 printf("timing (medians of %u): chip %.1f us (state read + write %.2f MB -> %.1f GB/s; 273 GB/s nominal), call %.1f us; %u insns, %u registers\n", 983 done, kus, info.state_bytes / 1e6, gbs, cus, info.insns, info.gprs); 984 if (out) fprintf(out, "%s{\"name\":\"timing\",\"reps\":%u,\"chip_us_median\":%.3f,\"call_us_median\":%.3f,\"state_bytes\":%" PRIu64 ",\"state_gbs\":%.2f,\"nominal_gbs\":273,\"insns\":%u,\"gprs\":%u}", 985 first ? "" : ",", done, kus, cus, info.state_bytes, gbs, info.insns, info.gprs); 986 first = 0; 987 } 988 free(ec); free(ek); inp_free(&in); 989 } 990 #undef POINT 991 free(o1); free(o2); 992 layer_free(&L[0]); layer_free(&L[1]); 993 omega_gpu_gdn_free(g); 994 if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); } 995 return 0; 996 } 997 998 int main(int argc, char **argv) { 999 setvbuf(stdout, NULL, _IOLBF, 0); /* a stuck chip run still leaves every finished line in its log */ 1000 int bf16 = 0; 1001 int host = 0, sim = 0; const char *out_path = NULL; uint32_t reps = 0, spin = 2000; 1002 for (int i = 1; i < argc; i++) { 1003 if (strcmp(argv[i], "--host-only") == 0) host = 1; 1004 else if (strcmp(argv[i], "--sim") == 0) sim = 1; 1005 else if (strcmp(argv[i], "--bf16") == 0) bf16 = 1; 1006 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; 1007 else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); 1008 else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) spin = (uint32_t)atoi(argv[++i]); 1009 else { printf("unknown argument %s\n", argv[i]); return 2; } 1010 } 1011 if (host) host_only(); 1012 if (sim) { sim_battery(); sim_bf16_battery(); sim_prefill_battery(); sim_prefill_bf16(3); sim_prefill_bf16(37); sim_copy_battery(); } 1013 if (!host && !sim) { 1014 (void)omega_gpu_session_set_spin_us(spin); 1015 if (bf16) chip_bf16(out_path, reps ? reps : 50); else chip_run(out_path, reps ? reps : 50); 1016 omega_gpu_session_close(); 1017 } 1018 printf("gpu_gdn_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); 1019 return g_failed ? 1 : 0; 1020 }