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