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/flash_expert_real_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 / flash_expert_real_test.c` 165 lines · 11910 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * One routed expert of one layer of the Qwen3.8-Flash-Next FP8 checkpoint (gate_proj, up_proj, down_proj and their BF16 weight_scale_inv 4 * grids, read from disk by tests/flash_expert/reference.py) through the FP8 GEMV builders on the host warp simulator, compared with 5 * (a) the independent Python float64 computation that wrote the reference files, and 6 * (b) omega_fp8_gemv_oracle, the repository's double-precision C oracle. 7 * Gate and up are laid out as one [1280][2560] tensor (rows of gate, then rows of up; scale grids stacked), the layout of the repository's 8 * GATE_UP tensors; down is [2560][640] (k = 640: the ragged kernel). The specs are the ones the API picks for a grouped launch. 9 * Functional simulation only: NOT scoreboard timing, NOT the chip. Host only. 10 * 11 * flash_expert_real_test DIR (DIR written by tests/flash_expert/reference.py) 12 */ 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include "bw_warp_sim.h" 19 #include "omega_blackwell_codegen_moe.h" 20 #include "omega_blackwell_codegen_moe2.h" 21 #include "omega_gpu_fp8_api.h" 22 23 static int g_checks, g_failed; 24 #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 25 26 #include "fp8_sim_gemv.h" 27 28 static void *slurp(const char *dir, const char *name, size_t want, size_t pad) { 29 char path[512]; snprintf(path, sizeof path, "%s/%s", dir, name); 30 FILE *f = fopen(path, "rb"); 31 if (!f) { printf("NOT_RUN: cannot open %s\n", path); exit(3); } 32 uint8_t *buf = aligned_alloc(64, (want + pad + 63) & ~(size_t)63); 33 uint32_t st = 0x1badb002u; 34 for (size_t i = 0; i < want + pad; i++) buf[i] = rand_e4m3(&st); /* slack behind the data holds finite junk */ 35 const size_t got = fread(buf, 1, want, f); 36 fclose(f); 37 if (got != want) { printf("NOT_RUN: %s has %zu bytes, wanted %zu\n", path, got, want); exit(3); } 38 return buf; 39 } 40 41 /* worst |y - ref| / mag over n rows and the count at or past TOL */ 42 static double vs_ref(const float *y, const double *ref, const double *mag, size_t n, size_t *bad) { 43 double worst = 0; size_t nb = 0; 44 for (size_t r = 0; r < n; r++) { 45 const double rel = fabs((double)y[r] - ref[r]) / (mag[r] > 1e-30 ? mag[r] : 1e-30); 46 if (rel > worst) worst = rel; 47 if (rel >= TOL || isnan((double)y[r])) nb++; 48 } 49 if (bad) *bad = nb; 50 return worst; 51 } 52 53 int main(int argc, char **argv) { 54 if (argc != 2) { printf("usage: flash_expert_real_test DIR\n"); return 2; } 55 const char *dir = argv[1]; 56 const uint32_t GN = 1280, GK = 2560, DN = 2560, DK = 640; 57 uint8_t *gw = slurp(dir, "gate_w.bin", 640u * 2560u, 0), *uw = slurp(dir, "up_w.bin", 640u * 2560u, 0), *dw = slurp(dir, "down_w.bin", (size_t)DN * DK, 4096); 58 uint16_t *gs = slurp(dir, "gate_s.bin", 200, 0), *us = slurp(dir, "up_s.bin", 200, 0), *ds = slurp(dir, "down_s.bin", 200, 0); 59 float *x = slurp(dir, "x.f32", GK * 4u, 0), *a = slurp(dir, "a.f32", DK * 4u, 0); 60 double *gu_ref = slurp(dir, "gu_ref.f64", GN * 8u, 0), *gu_mag = slurp(dir, "gu_mag.f64", GN * 8u, 0); 61 double *dn_ref = slurp(dir, "dn_ref.f64", DN * 8u, 0), *dn_mag = slurp(dir, "dn_mag.f64", DN * 8u, 0); 62 63 /* gate and up stacked: the repository's GATE_UP layout */ 64 Weights GU = { .experts = 1, .n = GN, .k = GK }; 65 GU.w = aligned_alloc(64, (size_t)GN * GK + 4096); memcpy(GU.w, gw, 640u * 2560u); memcpy(GU.w + 640u * 2560u, uw, 640u * 2560u); 66 uint32_t st = 99; for (size_t i = (size_t)GN * GK; i < (size_t)GN * GK + 4096; i++) GU.w[i] = rand_e4m3(&st); 67 GU.s = malloc(400 * 2); memcpy(GU.s, gs, 200); memcpy(GU.s + 100, us, 200); 68 Weights DW = { .experts = 1, .n = DN, .k = DK, .w = dw, .s = ds }; 69 70 CHECK(omega_fp8_pack_scan_nan(GU.w, (size_t)GN * GK, NULL) == 0 && omega_fp8_pack_scan_nan(dw, (size_t)DN * DK, NULL) == 0, "no e4m3 NaN code in the bytes read (the upload would accept them)"); 71 72 /* (b) the C double oracle against the independent Python float64 reference: the two references agree */ 73 { 74 double *y = malloc(GN * 8), *m = malloc(GN * 8); 75 omega_fp8_gemv_oracle(GU.w, GU.s, GN, GK, x, y, m); 76 double worst = 0; for (uint32_t r = 0; r < GN; r++) { const double d = fabs(y[r] - gu_ref[r]) / (gu_mag[r] > 1e-30 ? gu_mag[r] : 1e-30); if (d > worst) worst = d; } 77 CHECK(worst < 1e-12, "gate/up: C oracle vs Python reference worst relative difference %.3e", worst); 78 printf("gate/up: C double oracle vs Python float64 reference: worst relative difference %.3e\n", worst); 79 double *y2 = malloc(DN * 8), *m2 = malloc(DN * 8); 80 float *a_pad = a; 81 omega_fp8_gemv_oracle(dw, ds, DN, DK, a_pad, y2, m2); 82 worst = 0; for (uint32_t r = 0; r < DN; r++) { const double d = fabs(y2[r] - dn_ref[r]) / (dn_mag[r] > 1e-30 ? dn_mag[r] : 1e-30); if (d > worst) worst = d; } 83 CHECK(worst < 1e-12, "down: C oracle vs Python reference worst relative difference %.3e", worst); 84 printf("down: C double oracle vs Python float64 reference: worst relative difference %.3e\n", worst); 85 free(y); free(m); free(y2); free(m2); 86 } 87 88 char msg[240]; 89 const uint32_t ids[3] = { 0, 0, 0 }; 90 /* gate/up through the grouped-launch spec (plain kernel) and the single-projection spec (the deep kernel) */ 91 for (unsigned form = 0; form < 2; form++) { 92 OmegaFp8GemvSpec sp; omega_gpu_fp8_spec_for(GK, GN, form ? 1u : 10u, &sp); 93 if (sp.warps > 8) sp.warps = 8; /* the host simulator runs at most 8 warps per CTA; the launch uses 32 (register fit: tests/fp8_ragged_api_test.c) */ 94 const uint32_t groups = form ? 1u : 3u; 95 float *y = malloc((size_t)groups * GN * 4); 96 const int rc = sim_gemv(&sp, &GU, groups, ids, x, 1, y, 3, 0, msg, sizeof msg); 97 size_t bad = 0; double worst = 1; 98 CHECK(rc == 0, "gate/up %s: simulator run failed: %s", form ? "single projection" : "grouped launch", msg); 99 if (rc == 0) { 100 for (uint32_t g = 0; g < groups; g++) { size_t b = 0; const double w = vs_ref(y + (size_t)g * GN, gu_ref, gu_mag, GN, &b); if (w > worst || g == 0) worst = w; bad += b; } 101 CHECK(bad == 0 && worst < TOL, "gate/up %s: %zu rows at or past %.0e, worst relative error %.3e", form ? "single projection" : "grouped launch", bad, TOL, worst); 102 for (uint32_t g = 1; g < groups; g++) CHECK(!memcmp(y, y + (size_t)g * GN, GN * 4), "gate/up grouped launch: group %u equals group 0 bit for bit (same expert, same x)", g); 103 } 104 printf("gate/up %-17s k=2560 n=1280: lane_bytes %u rows %u warps %u (sim) depth %u coal %u: worst relative error %.3e over %u group(s)\n", 105 form ? "single projection" : "grouped launch", sp.lane_bytes, sp.rows, sp.warps, sp.depth, sp.coal, worst, groups); 106 free(y); 107 } 108 /* down, k = 640: the ragged kernel the device and record forms run */ 109 OmegaFp8GemvSpec dsp; omega_gpu_fp8_spec_for(DK, DN, 10, &dsp); 110 CHECK(omega_gpu_fp8_ragged_for(&dsp, 1) == 1 && omega_fp8_gemv_steps(&dsp) == 0, "down k=640 grouped launch picks the ragged kernel (plain steps %u)", omega_fp8_gemv_steps(&dsp)); 111 { 112 const uint32_t groups = 3; 113 OmegaFp8GemvSpec dsp8 = dsp; if (dsp8.warps > 8) dsp8.warps = 8; /* simulator limit, as above */ 114 float *arows = malloc((size_t)groups * DK * 4); for (uint32_t g = 0; g < groups; g++) memcpy(arows + (size_t)g * DK, a, DK * 4); 115 float *y = malloc((size_t)groups * DN * 4); 116 const int rc = sim_gemv(&dsp8, &DW, groups, ids, arows, groups, y, 3, 0, msg, sizeof msg); 117 CHECK(rc == 0, "down: simulator run failed: %s", msg); 118 double worst = 1; size_t bad = 0; 119 if (rc == 0) { 120 for (uint32_t g = 0; g < groups; g++) { size_t b = 0; const double w = vs_ref(y + (size_t)g * DN, dn_ref, dn_mag, DN, &b); if (w > worst || g == 0) worst = w; bad += b; } 121 CHECK(bad == 0 && worst < TOL, "down: %zu rows at or past %.0e, worst relative error %.3e", bad, TOL, worst); 122 } 123 printf("down grouped launch k=640 n=2560: ragged, lane_bytes %u rows %u warps %u (launch uses %u): worst relative error %.3e over %u groups\n", dsp8.lane_bytes, dsp8.rows, dsp8.warps, dsp.warps, worst, groups); 124 125 uint32_t *control = slurp(dir, "control.u32", 8, 0); 126 const uint32_t row = control[0], col = control[1]; 127 free(control); 128 if (row >= 128 || col < 512 || col >= DK) return 2; 129 const double noise = rc == 0 ? fabs((double)y[row] - dn_ref[row]) / dn_mag[row] : INFINITY; 130 131 /* negative controls on the same bytes, each beside the passing run above */ 132 OmegaFp8GemvSpec m7 = dsp8; m7.mutant = 7; /* tail step never run */ 133 const int r7 = sim_gemv(&m7, &DW, 1, ids, arows, 1, y, 3, 0, msg, sizeof msg); 134 size_t b7 = 0; const double w7 = r7 == 0 ? vs_ref(y, dn_ref, dn_mag, DN, &b7) : 1; 135 CHECK(r7 == 0 && b7 > DN / 2, "mutant 7 (tail step never run) must fail on the real bytes: %zu rows wrong, worst %.3e", b7, w7); 136 printf("mutant 7 on the real bytes: %zu of %u rows wrong (worst relative error %.3e)\n", b7, DN, w7); 137 uint8_t keep = dw[row * DK + col]; 138 /* The float64 reference selects a tail weight whose flipped exponent bit is detectable 139 * above roundoff but below TOL. Replacing the same weight with 448 must cross TOL. */ 140 dw[row * DK + col] = (uint8_t)(keep ^ 0x08); 141 const int r1 = sim_gemv(&dsp8, &DW, 1, ids, arows, 1, y, 3, 0, msg, sizeof msg); 142 const double d1 = r1 == 0 ? fabs((double)y[row] - dn_ref[row]) / dn_mag[row] : 0; 143 CHECK(r1 == 0 && d1 > 1e-6 && d1 < TOL && noise < 1e-7, "one flipped tail exponent bit: the selected row differs by %.3e of sum|terms| (kernel noise %.3e, tolerance %.0e)", d1, noise, TOL); 144 printf("one flipped tail exponent bit: the selected row differs by %.3e (kernel noise %.3e): visible to the kernel, below the parity tolerance %.0e\n", d1, noise, TOL); 145 dw[row * DK + col] = 0x7e; /* one weight in the tail columns of the selected row becomes 448 (the largest finite e4m3 code) */ 146 const int rb = sim_gemv(&dsp8, &DW, 1, ids, arows, 1, y, 3, 0, msg, sizeof msg); 147 size_t bb = 0; const double wb = rb == 0 ? vs_ref(y, dn_ref, dn_mag, DN, &bb) : 1; 148 CHECK(rb == 0 && bb >= 1 && fabs((double)y[row] - dn_ref[row]) / dn_mag[row] >= TOL, "one changed tail weight must be caught on the selected row: %zu rows wrong, worst %.3e", bb, wb); 149 printf("one changed weight in the tail columns: %zu row(s) wrong (worst %.3e)\n", bb, wb); 150 dw[row * DK + col] = keep; 151 const uint16_t keep_s = ds[4]; /* scale grid [20][5]: row block 0, column block 4 (the tail block), rows 0..127 */ 152 ds[4] ^= 0x0040; /* top fraction bit of that BF16 scale */ 153 const int rs = sim_gemv(&dsp8, &DW, 1, ids, arows, 1, y, 3, 0, msg, sizeof msg); 154 size_t bs = 0; const double ws = rs == 0 ? vs_ref(y, dn_ref, dn_mag, DN, &bs) : 1; 155 CHECK(rs == 0 && bs >= 100, "a changed tail-block scale must be caught: %zu rows wrong, worst %.3e", bs, ws); 156 printf("one changed tail-block scale: %zu rows wrong (worst %.3e)\n", bs, ws); 157 ds[4] = keep_s; 158 const int rt = sim_gemv(&dsp8, &DW, 1, ids, arows, 1, y, 3, 0, msg, sizeof msg); 159 size_t bt = 0; const double wt = rt == 0 ? vs_ref(y, dn_ref, dn_mag, DN, &bt) : 1; 160 CHECK(rt == 0 && bt == 0, "restored bytes pass again (the controls changed only their one byte): %zu bad, worst %.3e", bt, wt); 161 free(arows); free(y); 162 } 163 printf("%s: %d checks, %d failed\n", g_failed ? "FLASH EXPERT REAL FAIL" : "FLASH EXPERT REAL PASS", g_checks, g_failed); 164 return g_failed ? 1 : 0; 165 }