interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / fp8_sim_gemv.h

97 lines · 7267 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * Host warp-simulator harness for the FP8 GEMV builders, shared by tests/codegen_moe_ragged_test.c and tests/flash_expert_real_test.c.
4 * Functional execution only (NOT scoreboard timing, NOT the chip). Needs the includes of those tests (bw_warp_sim.h, the codegen headers,
5 * omega_gpu_fp8_api.h, math.h, stdio.h, stdlib.h, string.h) before this header.
6 */
7#ifndef FP8_SIM_GEMV_H
8#define FP8_SIM_GEMV_H
9static inline uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
10static inline float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
11static inline uint8_t rand_e4m3(uint32_t *s) { uint8_t b = (uint8_t)(lcg(s) >> 24); if (omega_fp8_e4m3_is_nan(b)) b = (uint8_t)(b - 1); return b; }
12static inline uint16_t bf16_of(float f) { uint32_t u; memcpy(&u, &f, 4); return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); }
13
14typedef struct { uint32_t experts, n, k; uint8_t *w; uint16_t *s; } Weights;
15static inline void weights_make(Weights *W, uint32_t experts, uint32_t n, uint32_t k, uint32_t seed) {
16 W->experts = experts; W->n = n; W->k = k;
17 const size_t wb = (size_t)experts * n * k, sc = (size_t)experts * (n / 128) * (k / 128);
18 W->w = aligned_alloc(64, (wb + 4096 + 63) & ~(size_t)63); W->s = malloc(sc * 2); /* 4096 bytes of slack behind the weights */
19 uint32_t st = seed;
20 for (size_t i = 0; i < wb + 4096; i++) W->w[i] = rand_e4m3(&st); /* the slack holds finite non-zero codes */
21 for (size_t i = 0; i < sc; i++) W->s[i] = bf16_of(frand(&st, 0.001f, 0.1f) * ((lcg(&st) & 1) ? 1.0f : -1.0f));
22}
23static inline void weights_free(Weights *W) { free(W->w); free(W->s); }
24
25#define TOL 3e-5
26/* worst |y - oracle| / sum|terms| over the selected experts; *bad counts rows at or past TOL or NaN */
27static inline double parity(const Weights *W, uint32_t groups, const uint32_t *ids, const float *x, uint32_t x_rows, const float *y, size_t *bad) {
28 double worst = 0.0; size_t nbad = 0;
29 double *ref = malloc(W->n * sizeof *ref), *mag = malloc(W->n * sizeof *mag);
30 for (uint32_t g = 0; g < groups; g++) {
31 omega_fp8_gemv_oracle(W->w + (size_t)ids[g] * W->n * W->k, W->s + (size_t)ids[g] * (W->n / 128) * (W->k / 128), W->n, W->k,
32 x + (size_t)(x_rows == 1 ? 0 : g) * W->k, ref, mag);
33 for (uint32_t r = 0; r < W->n; r++) {
34 const double yy = (double)y[(size_t)g * W->n + r], d = fabs(ref[r] - yy), rel = d / (mag[r] > 1e-30 ? mag[r] : 1e-30);
35 if (rel > worst) worst = rel;
36 if (rel >= TOL || isnan(yy)) nbad++;
37 }
38 }
39 free(ref); free(mag);
40 if (bad) *bad = nbad;
41 return worst;
42}
43
44/* Runs spec on the warp simulator in both fragment orders; 0 and y on success, -1 on a codegen refusal or simulator error or an
45 * order-dependent result. Every global access must land in a declared buffer: with slack = 0 the buffers end exactly where the
46 * tensor ends (an unmasked lane past the row's last column faults); with slack = 1 they extend over finite non-zero junk behind
47 * the weights, scales and x' (an unmasked lane then reads junk and computes a wrong value instead of faulting). */
48static inline int sim_gemv(const OmegaFp8GemvSpec *spec, const Weights *W, uint32_t groups, const uint32_t *ids, const float *x,
49 uint32_t x_rows, float *y, uint32_t cta_budget, int slack, char *msg, size_t msglen) {
50 static BlackwellIRProgram prog;
51 if (omega_blackwell_codegen_fp8_gemv_ragged_prog(spec, &prog) != 0) { snprintf(msg, msglen, "codegen refused"); return -1; }
52 const uint32_t n = W->n, k = W->k, RG = n / spec->rows;
53 const size_t sc = (size_t)W->experts * (n / 128) * (k / 128);
54 float *sf = aligned_alloc(16, ((sc * 4 + 1024) + 15) & ~(size_t)15);
55 if (omega_fp8_pack_scales(W->s, sc, sf, 0) != 0) { free(sf); snprintf(msg, msglen, "scale pack refused"); return -1; }
56 for (size_t i = 0; i < 256; i++) sf[sc + i] = 1.0e10f * (float)(i + 1); /* scale slack: finite, large */
57 float *xf = aligned_alloc(16, ((size_t)x_rows * k * 4 + 4096 + 15) & ~(size_t)15);
58 for (uint32_t r = 0; r < x_rows; r++) omega_fp8_pack_x_f32(x + (size_t)r * k, k, xf + (size_t)r * k);
59 for (size_t i = 0; i < 1024; i++) xf[(size_t)x_rows * k + i] = 1.0e19f * (float)((i % 7) + 1); /* x' slack: finite, non-zero */
60 uint64_t *tab = aligned_alloc(16, 16 * (size_t)groups);
61 for (uint32_t g = 0; g < groups; g++) {
62 tab[2 * g] = (uintptr_t)(W->w + (size_t)ids[g] * n * k);
63 tab[2 * g + 1] = (uintptr_t)(sf + (size_t)ids[g] * (n / 128) * (k / 128));
64 }
65 float *yd = aligned_alloc(16, ((size_t)groups * n * 4 + 15) & ~(size_t)15), *copy = malloc((size_t)groups * n * 4);
66 uint32_t grid_x = (RG + spec->warps - 1) / spec->warps; if (grid_x > cta_budget) grid_x = cta_budget; if (!grid_x) grid_x = 1;
67 int err = 0;
68 for (int run = 0; run < 4 && !err; run++) {
69 WsSim s; memset(&s, 0, sizeof s);
70 s.p = &prog; s.threads = 32 * spec->warps; s.order = run & 1; s.warp_sched = run >= 2 ? 0x9e3779b9u : 0u;
71 static uint8_t smem[OMEGA_FP8_MAX_K * 4 + 4096]; s.smem = smem; s.smem_bytes = omega_fp8_gemv_ragged_shared_bytes(spec);
72 const uint64_t ta = (uintptr_t)tab, xa = (uintptr_t)xf, ya = (uintptr_t)yd;
73 s.cbank[OMEGA_FP8_ARG_TAB / 4] = (uint32_t)ta; s.cbank[OMEGA_FP8_ARG_TAB / 4 + 1] = (uint32_t)(ta >> 32);
74 s.cbank[OMEGA_FP8_ARG_X / 4] = (uint32_t)xa; s.cbank[OMEGA_FP8_ARG_X / 4 + 1] = (uint32_t)(xa >> 32);
75 s.cbank[OMEGA_FP8_ARG_Y / 4] = (uint32_t)ya; s.cbank[OMEGA_FP8_ARG_Y / 4 + 1] = (uint32_t)(ya >> 32);
76 s.cbank[OMEGA_FP8_ARG_RG / 4] = RG; s.cbank[OMEGA_FP8_ARG_XSTRIDE / 4] = x_rows == 1 ? 0 : k * 4;
77 s.cbank[OMEGA_FP8_ARG_YSTRIDE / 4] = n * 4; s.cbank[OMEGA_FP8_ARG_STRIDE / 4] = grid_x * spec->warps;
78 s.nranges = 5;
79 s.ranges[0].lo = (uintptr_t)W->w; s.ranges[0].hi = (uintptr_t)W->w + (size_t)W->experts * n * k + (slack ? 4096 : 0);
80 s.ranges[1].lo = (uintptr_t)sf; s.ranges[1].hi = (uintptr_t)sf + sc * 4 + (slack ? 1024 : 0);
81 s.ranges[2].lo = (uintptr_t)xf; s.ranges[2].hi = (uintptr_t)xf + (size_t)x_rows * k * 4 + (slack ? 4096 : 0);
82 s.ranges[3].lo = (uintptr_t)yd; s.ranges[3].hi = (uintptr_t)yd + (size_t)groups * n * 4;
83 s.ranges[4].lo = (uintptr_t)tab; s.ranges[4].hi = (uintptr_t)tab + 16 * (size_t)groups;
84 for (size_t i = 0; i < (size_t)groups * n; i++) yd[i] = -999.0f;
85 err = ws_run(&s, grid_x, groups);
86 if (err) { snprintf(msg, msglen, "run %d: %s", run, s.msg); break; }
87 size_t unwritten = 0; for (size_t i = 0; i < (size_t)groups * n; i++) if (yd[i] == -999.0f) unwritten++;
88 if (unwritten) { err = -1; snprintf(msg, msglen, "%zu output words never written", unwritten); break; }
89 if (run == 0) memcpy(copy, yd, (size_t)groups * n * 4);
90 else if (memcmp(copy, yd, (size_t)groups * n * 4) != 0) { err = -1; snprintf(msg, msglen, "run %d changed the result", run); }
91 }
92 if (!err) memcpy(y, yd, (size_t)groups * n * 4);
93 free(sf); free(xf); free(tab); free(yd); free(copy);
94 return err ? -1 : 0;
95}
96
97#endif