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_mathlibs_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_mathlibs_test.c` 2604 lines · 147724 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* gpu_mathlibs_test.c -- CUDA audit rows 29 (cuFFT-class) and 30 (cuRAND-class): the GB10 xoshiro256** and 3 * Philox4x32-10 generators and the f32 FFTs (complex 1-D, R2C / C2R, 2-D / 3-D) of src/omega_gpu_mathlibs_api.h, 4 * kernels and pass plans from om/mathlibs/mathlibs_ir.om. 5 * 6 * --host-only no device: the xoshiro reference against the aien-math fixture (792 rows); the Philox reference 7 * against the Random123 known-answer vectors (a 9-round control must fail all three) and against 8 * the cuRAND fixture (1024 rows); the Philox distribution mirror (curand_uniform, _uniform_double, 9 * _uniform2_double bit-exact; curand_normal within 2^-21 sqrt(-2 log u)) against the cuRAND 10 * distribution fixture (4480 rows, the 896 normal_double rows counted, not compared) with three 11 * negative controls; refusals; every kernel through the unit, the allocator and the 12 * .om encoder, digest and listing checks; the twiddle tables up to n = 4096; the unit's pass plans 13 * and the launches they become; the normal transform's f32 model swept over every one of its 2^23 14 * inputs against an f64 erfinv (Newton on libm erf/erfc) within the Wolfram bound 2.87e-6 15 * --sim --host-only plus the real kernels through the warp simulator (tests/bw_warp_sim.h): xoshiro raw 16 * and uniform outputs bit-exact against the reference, normal outputs bit-exact against the f32 17 * model and within the bound; Philox raw words bit-exact against the KAT, the reference and every 18 * cuRAND fixture row, every distribution at offsets of every residue mod 4, partial groups and the 19 * counter carry bit-exact against the mirror and covering every cuRAND distribution row; FFT N = 4..2048, 20 * R2C and C2R N = 8..4096 and 2-D / 3-D shapes within the Wolfram 21 * normwise tolerances of an f64 DFT under three warp orders; every mutant must be caught 22 * (no flag) the GB10: 4096 streams x 17 at offset 0 and x 16 at offset 1000 for bases 0, 1729, 2^64-4 23 * against the fixture and the reference, uniform and normal runs, a 131072-stream run, the FFT at 24 * every N both ways, a batch of 2050 (three launches); Philox against the reference and the cuRAND 25 * fixture and a 131072-stream run; R2C / C2R at every N, the 2-D / 3-D shapes; every mutant must be 26 * caught. --out FILE writes the receipt lines there as well. 27 * --fixture F the aien-math fixture (default tests/fixtures/mathlibs_rng_aienmath.txt) 28 * --philox-fixture F the cuRAND fixture (default tests/fixtures/philox_curand_oracle.txt) 29 * --dist-fixture F the cuRAND distribution fixture (default tests/fixtures/philox_curand_dist_oracle.txt) 30 * Last line: "mathlibs : PASS ( checks)" or "mathlibs : FAIL ( of )". */ 31 #include "omega_gpu_mathlibs_api.h" 32 #include "omega_gpu_om_ir.h" 33 #include "omega_sm121_disasm.h" 34 #define WS_MAX_THREADS 1024 /* the real split / merge's N / 4 threads at N = 4096 */ 35 #include "bw_warp_sim.h" 36 #include "test_out_file.h" 37 #include 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 #include 46 47 static int g_checks, g_failed; 48 static FILE *g_rcpt; 49 50 static void check(int ok, const char *fmt, ...) __attribute__((format(printf, 2, 3))); 51 static void check(int ok, const char *fmt, ...) { 52 g_checks++; 53 if (ok) return; 54 g_failed++; 55 va_list ap; 56 va_start(ap, fmt); 57 printf("FAIL: "); 58 vprintf(fmt, ap); 59 printf("\n"); 60 va_end(ap); 61 } 62 63 static void receipt(const char *fmt, ...) __attribute__((format(printf, 1, 2))); 64 static void receipt(const char *fmt, ...) { 65 va_list ap; 66 va_start(ap, fmt); 67 vprintf(fmt, ap); 68 va_end(ap); 69 printf("\n"); 70 if (g_rcpt) { 71 va_start(ap, fmt); 72 vfprintf(g_rcpt, fmt, ap); 73 va_end(ap); 74 fprintf(g_rcpt, "\n"); 75 } 76 } 77 78 static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 79 static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 80 81 /* ------------------------------------------------------------------ reference generator */ 82 83 #define GOLDEN 0x9E3779B97F4A7C15ull 84 typedef struct { uint64_t s[4]; } Xo; 85 static uint64_t rotl64(uint64_t x, int k) { return (x << k) | (x >> (64 - k)); } 86 static void xo_seed(Xo *x, uint64_t seed) { 87 for (int i = 0; i < 4; i++) { 88 seed += GOLDEN; 89 uint64_t z = seed; 90 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; 91 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; 92 x->s[i] = z ^ (z >> 31); 93 } 94 } 95 static uint64_t xo_next(Xo *x) { 96 uint64_t *s = x->s, r = rotl64(s[1] * 5, 7) * 9, t = s[1] << 17; 97 s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl64(s[3], 45); 98 return r; 99 } 100 /* out[j * ns + g] = output offset + j of the stream seeded with seed + stream0 + g */ 101 static void ref_raw(uint64_t seed, uint64_t stream0, uint32_t ns, uint32_t offset, uint32_t per, uint64_t *out) { 102 for (uint32_t g = 0; g < ns; g++) { 103 Xo x; 104 xo_seed(&x, seed + stream0 + g); 105 for (uint32_t i = 0; i < offset; i++) (void)xo_next(&x); 106 for (uint32_t j = 0; j < per; j++) out[(size_t)j * ns + g] = xo_next(&x); 107 } 108 } 109 110 /* ------------------------------------------------------------------ normal transform: f32 model and f64 truth */ 111 112 /* f32 constants, bit patterns from evidence/MATHLIBS-RNG-FFT-2026-10-27/wolfram-constants.txt section 2 */ 113 static const uint32_t GC_BITS[9] = { 0x3fc02e2f, 0x3e7c8f63, 0xbb88e4ef, 0xbaa45408, 0x396532db, 114 0xb6935ac1, 0xb66c7357, 0x34b84b36, 0x32f16588 }; 115 static const uint32_t GT_BITS[9] = { 0x40354f7e, 0x3f8036db, 0x3c1aa57e, 0xbbf9c5d7, 0x3bbc127b, 116 0xbb70bde7, 0x3ab0dc72, 0x38d3b56b, 0xb951f09b }; 117 #define NLN2_BITS 0xbf317218u 118 #define RT2_BITS 0x3fb504f3u 119 #define NORMAL_TOL 2.87e-6 120 121 static float uniform_of(uint64_t r) { return (float)(uint32_t)(r >> 40) * 0x1p-24f; } 122 123 /* The kernel's operation order with exact LG2 and RSQ (what the simulator executes): A = (r >> 41) * 2 + 1. */ 124 static float normal_model(uint32_t A) { 125 uint32_t b = 16777216u - A; 126 int32_t xi = (int32_t)(2u * A - 16777216u); 127 float q = (float)(int32_t)A * (float)(int32_t)b; 128 q = q * 0x1p-46f; 129 q = log2f(q); 130 float w = q * u2f(NLN2_BITS); 131 float t = w - 2.5f, pc = u2f(GC_BITS[8]); 132 for (int k = 7; k >= 0; k--) pc = fmaf(pc, t, u2f(GC_BITS[k])); 133 t = w * (1.0f / sqrtf(w)); 134 t = t - 3.0f; 135 float pt = u2f(GT_BITS[8]); 136 for (int k = 7; k >= 0; k--) pt = fmaf(pt, t, u2f(GT_BITS[k])); 137 uint32_t mask = (uint32_t)0 - (f2u(w - 5.0f) >> 31); 138 float p = u2f((mask & f2u(pc)) | (~mask & f2u(pt))); 139 float x = (float)xi * 0x1p-24f; 140 return p * x * u2f(RT2_BITS); 141 } 142 143 /* sqrt(2) erfinv(y), y = (2A - 2^24) / 2^24: Winitzki's start, Newton on erf (|y| <= 1/2) or erfc (tail) */ 144 static double normal_ref(uint32_t A) { 145 double y = ((double)(2.0 * A) - `16777216`.0) * 0x1p-24; 146 double omy2 = (double)A * (double)(16777216u - A) * 0x1p-46; /* 1 - y^2, exact */ 147 double c = 1.0 - fabs(y); /* exact */ 148 const double a = 0.147, l = log(omy2), u = 2.0 / (M_PI * a) + l / 2.0; 149 double x = sqrt(sqrt(u * u - l / a) - u); 150 for (int it = 0; it < 12; it++) { 151 double d = M_2_SQRTPI * exp(-x * x), step = fabs(y) > 0.5 ? -(erfc(x) - c) / d : (erf(x) - fabs(y)) / d; 152 x -= step; 153 if (fabs(step) <= 1e-17 * x) break; 154 } 155 return copysign(M_SQRT2 * x, y); 156 } 157 static uint32_t normal_A(uint64_t r) { return (uint32_t)(r >> 41) * 2u + 1u; } 158 static double rel_err(float z, double ref) { return fabs((double)z - ref) / fabs(ref); } 159 160 /* ------------------------------------------------------------------ FFT reference */ 161 162 static const double FFT_TOL[12] = { 0, 0, 1.`19209296e`-7, 4.`66610727e`-7, 5.`26215405e`-7, 8.`73616990e`-7, 9.`33221726e`-7, 163 1.`28062354e`-6, 1.`34022856e`-6, 1.`68763126e`-6, 1.`74723836e`-6, 2.`09464702e`-6 }; 164 static int lg2u(uint32_t n) { int k = 0; while ((1u << k) < n) k++; return k; } 165 166 /* f64 DFT of one transform, twiddles exp(-+2 pi i (l m mod n) / n) from libm */ 167 static void dft64(const float *x, double *y, uint32_t n, int inverse, const double *cs, const double *sn) { 168 for (uint32_t m = 0; m < n; m++) { 169 double re = 0, im = 0; 170 for (uint32_t l = 0; l < n; l++) { 171 uint32_t k = (uint32_t)(((uint64_t)l * m) % n); 172 double c = cs[k], s = inverse ? sn[k] : -sn[k], xr = x[2 * l], xm = x[2 * l + 1]; 173 re += xr * c - xm * s; 174 im += xr * s + xm * c; 175 } 176 y[2 * m] = re; y[2 * m + 1] = im; 177 } 178 } 179 /* worst normwise ||y - ref|| / ||ref|| over the batch */ 180 static double fft_err(const float *in, const float *out, uint32_t n, uint32_t batch, int inverse) { 181 double *cs = malloc(n * sizeof *cs), *sn = malloc(n * sizeof *sn), *ref = malloc(2 * n * sizeof *ref), worst = 0; 182 for (uint32_t k = 0; k < n; k++) { cs[k] = cos(2.0 * M_PI * k / n); sn[k] = sin(2.0 * M_PI * k / n); } 183 for (uint32_t b = 0; b < batch; b++) { 184 dft64(in + (size_t)2 * n * b, ref, n, inverse, cs, sn); 185 double num = 0, den = 0; 186 for (uint32_t i = 0; i < 2 * n; i++) { 187 double d = (double)out[(size_t)2 * n * b + i] - ref[i]; 188 num += d * d; den += ref[i] * ref[i]; 189 } 190 double e = sqrt(num / den); 191 if (!(e <= worst)) worst = e; /* a NaN stays */ 192 } 193 free(cs); free(sn); free(ref); 194 return worst; 195 } 196 static uint64_t g_lcg = 0x243F6A8885A308D3ull; 197 static float rnd_pm1(void) { 198 g_lcg = g_lcg * 6364136223846793005ull + 1442695040888963407ull; 199 return (float)(int32_t)(g_lcg >> 40 & 0xffffff) * 0x1p-23f - 1.0f; 200 } 201 static void fill_rand(float *x, size_t n) { for (size_t i = 0; i < n; i++) x[i] = rnd_pm1(); } 202 203 /* ------------------------------------------------------------------ R2C, C2R and 2-D / 3-D references */ 204 205 /* R2C tolerances by log2 N and the 2-D / 3-D ones by shape: WolframLanguageEvaluator, 2026-11-06, the query text in 206 * evidence/MATHLIBS-PHILOX-RFFT-ND-2026-11-06/wolfram-tolerances.txt (normwise, against an f64 DFT) */ 207 static const double R2C_TOL[13] = { 0, 0, 0, 8.`561065560e`-7, 1.`550909633e`-6, 1.`670119030e`-6, 2.`364922423e`-6, 208 2.`484131961e`-6, 3.`178935896e`-6, 3.`298146214e`-6, 3.`992952496e`-6, 4.`112168690e`-6, 209 4.`806991727e`-6 }; 210 static const double C2R_TOL[13] = { 0, 0, 0, 7.`368972629e`-7, 1.`084298912e`-6, 1.`143903636e`-6, 1.`491305459e`-6, 211 1.`550910301e`-6, 1.`898312525e`-6, 1.`957918122e`-6, 2.`305322551e`-6, 2.`364934000e`-6, 212 2.`712355043e`-6 }; 213 static const struct { uint32_t nd, d[3]; double tol; } ND_SHAPES[] = { 214 { 1, { 256, 0, 0 }, 1.`34022856e`-6 }, /* FFT_TOL[8]: the 1-D plan */ 215 { 2, { 4, 4, 0 }, 2.`384186066e`-7 }, { 2, { 8, 16, 0 }, 9.`928263778e`-7 }, { 2, { 32, 64, 0 }, 1.`806839531e`-6 }, 216 { 2, { 16, 2048, 0 }, 2.`620863531e`-6 }, { 2, { 2048, 4, 0 }, 2.`213856570e`-6 }, { 3, { 4, 4, 4 }, 3.`576279313e`-7 }, 217 { 3, { 4, 8, 16 }, 1.`112035792e`-6 }, { 3, { 16, 16, 16 }, 1.`578647047e`-6 }, { 3, { 8, 4, 32 }, 1.`459437580e`-6 }, 218 }; 219 #define N_ND_SHAPES (sizeof ND_SHAPES / sizeof ND_SHAPES[0]) 220 221 static void trig(uint32_t n, double **cs, double **sn) { 222 *cs = malloc(n * sizeof **cs); 223 *sn = malloc(n * sizeof **sn); 224 for (uint32_t k = 0; k < n; k++) { (*cs)[k] = cos(2.0 * M_PI * k / n); (*sn)[k] = sin(2.0 * M_PI * k / n); } 225 } 226 static double normwise(const float *got, const double *ref, size_t len) { 227 double num = 0, den = 0; 228 for (size_t i = 0; i < len; i++) { 229 double d = (double)got[i] - ref[i]; 230 num += d * d; den += ref[i] * ref[i]; 231 } 232 return sqrt(num / den); 233 } 234 /* R2C: X_m, m <= n / 2, of every row of n reals, against out rows of n / 2 + 1 complex; worst row */ 235 static double r2c_err(const float *in, const float *out, uint32_t n, uint32_t batch) { 236 double *cs, *sn, *ref = malloc((n + 2) * sizeof *ref), worst = 0; 237 trig(n, &cs, &sn); 238 for (uint32_t b = 0; b < batch; b++) { 239 const float *x = in + (size_t)n * b; 240 for (uint32_t m = 0; m <= n / 2; m++) { 241 double re = 0, im = 0; 242 for (uint32_t l = 0; l < n; l++) { 243 uint32_t k = (uint32_t)(((uint64_t)l * m) % n); 244 re += x[l] * cs[k]; 245 im -= x[l] * sn[k]; 246 } 247 ref[2 * m] = re; ref[2 * m + 1] = im; 248 } 249 double e = normwise(out + (size_t)(n + 2) * b, ref, n + 2); 250 if (!(e <= worst)) worst = e; /* a NaN stays */ 251 } 252 free(cs); free(sn); free(ref); 253 return worst; 254 } 255 /* C2R of Hermitian rows (n / 2 + 1 complex, Im X_0 = Im X_(n/2) = 0): y_l = sum over all n bins, unnormalised */ 256 static double c2r_err(const float *in, const float *out, uint32_t n, uint32_t batch) { 257 double *cs, *sn, *ref = malloc(n * sizeof *ref), worst = 0; 258 trig(n, &cs, &sn); 259 for (uint32_t b = 0; b < batch; b++) { 260 const float *X = in + (size_t)(n + 2) * b; 261 for (uint32_t l = 0; l < n; l++) { 262 double y = (double)X[0] + ((l & 1) ? -1.0 : 1.0) * X[n]; 263 for (uint32_t m = 1; m < n / 2; m++) { 264 uint32_t k = (uint32_t)(((uint64_t)l * m) % n); 265 y += 2.0 * (X[2 * m] * cs[k] - X[2 * m + 1] * sn[k]); 266 } 267 ref[l] = y; 268 } 269 double e = normwise(out + (size_t)n * b, ref, n); 270 if (!(e <= worst)) worst = e; 271 } 272 free(cs); free(sn); free(ref); 273 return worst; 274 } 275 /* f64 DFT in place along one axis of complex x: outer lines of n points at a stride of inner */ 276 static void dft_axis(double *x, size_t outer, uint32_t n, size_t inner, int inverse) { 277 double *cs, *sn, *ln = malloc(2 * n * sizeof *ln), *res = malloc(2 * n * sizeof *res); 278 trig(n, &cs, &sn); 279 for (size_t o = 0; o < outer; o++) 280 for (size_t i = 0; i < inner; i++) { 281 double *p = x + 2 * (o * n * inner + i); 282 for (uint32_t l = 0; l < n; l++) { ln[2 * l] = p[2 * l * inner]; ln[2 * l + 1] = p[2 * l * inner + 1]; } 283 for (uint32_t m = 0; m < n; m++) { 284 double re = 0, im = 0; 285 for (uint32_t l = 0; l < n; l++) { 286 uint32_t k = (uint32_t)(((uint64_t)l * m) % n); 287 double c = cs[k], s = inverse ? sn[k] : -sn[k]; 288 re += ln[2 * l] * c - ln[2 * l + 1] * s; 289 im += ln[2 * l] * s + ln[2 * l + 1] * c; 290 } 291 res[2 * m] = re; res[2 * m + 1] = im; 292 } 293 for (uint32_t m = 0; m < n; m++) { p[2 * m * inner] = res[2 * m]; p[2 * m * inner + 1] = res[2 * m + 1]; } 294 } 295 free(cs); free(sn); free(ln); free(res); 296 } 297 /* the separable f64 DFT of each row-major [d0][d1][d2] array of the batch; worst normwise error */ 298 static double nd_err(const float *in, const float *out, uint32_t nd, const uint32_t *dims, uint32_t batch, int inverse) { 299 size_t tot = 1; 300 for (uint32_t d = 0; d < nd; d++) tot *= dims[d]; 301 double *x = malloc(2 * tot * batch * sizeof *x), worst = 0; 302 for (size_t i = 0; i < 2 * tot * batch; i++) x[i] = in[i]; 303 for (uint32_t ax = 0; ax < nd; ax++) { 304 size_t outer = batch, inner = 1; 305 for (uint32_t d = 0; d < ax; d++) outer *= dims[d]; 306 for (uint32_t d = ax + 1; d < nd; d++) inner *= dims[d]; 307 dft_axis(x, outer, dims[ax], inner, inverse); 308 } 309 for (uint32_t b = 0; b < batch; b++) { 310 double e = normwise(out + 2 * tot * b, x + 2 * tot * b, 2 * tot); 311 if (!(e <= worst)) worst = e; 312 } 313 free(x); 314 return worst; 315 } 316 317 /* ------------------------------------------------------------------ cufftPlanMany: layouts and the f64 reference */ 318 319 static size_t many_prod(uint32_t rank, const uint32_t *n) { 320 size_t p = 1; 321 for (uint32_t i = 0; i < rank; i++) p *= n[i]; 322 return p; 323 } 324 /* one side of a many plan, recomputed here from the header's formula (not from the library): extents, embeds, 325 * stride, dist and whether its elements are complex */ 326 typedef struct { uint32_t rank, ext[3], e[3], stride, dist, cplx; } TLay; 327 static void tlay(const OmegaGpuFftMany *p, int in_place, int out, TLay *t) { 328 uint32_t r = p->rank, h = p->n[r - 1] / 2 + 1; 329 const uint32_t *emb = out ? p->onembed : p->inembed; 330 memset(t, 0, sizeof *t); 331 t->rank = r; 332 t->cplx = p->type == OMEGA_GPU_FFT_TYPE_C2C || (p->type == OMEGA_GPU_FFT_TYPE_R2C ? out : !out); 333 memcpy(t->ext, p->n, sizeof t->ext); 334 if (p->type != OMEGA_GPU_FFT_TYPE_C2C && t->cplx) t->ext[r - 1] = h; 335 if (p->inembed[0] == 0 && p->inembed[1] == 0 && p->inembed[2] == 0 && p->onembed[0] == 0 && p->onembed[1] == 0 && 336 p->onembed[2] == 0) { 337 memcpy(t->e, t->ext, sizeof t->e); 338 if (in_place && p->type != OMEGA_GPU_FFT_TYPE_C2C && !t->cplx) t->e[r - 1] = 2 * h; 339 t->stride = 1; 340 t->dist = 1; 341 for (uint32_t i = 0; i < r; i++) t->dist *= t->e[i]; 342 } else { 343 memcpy(t->e, emb, sizeof t->e); 344 t->stride = out ? p->ostride : p->istride; 345 t->dist = out ? p->odist : p->idist; 346 } 347 } 348 static size_t tlay_points(const TLay *t) { 349 size_t p = 1; 350 for (uint32_t i = 0; i < t->rank; i++) p *= t->ext[i]; 351 return p; 352 } 353 /* float index of dense point i (row-major over ext) of transform b */ 354 static size_t tlay_at(const TLay *t, uint32_t b, size_t i) { 355 uint32_t x[3]; 356 for (uint32_t d = t->rank; d-- > 0;) { x[d] = (uint32_t)(i % t->ext[d]); i /= t->ext[d]; } 357 size_t a = 0; 358 for (uint32_t d = 0; d < t->rank; d++) a = a * t->e[d] + x[d]; 359 a = (size_t)b * t->dist + (size_t)t->stride * a; 360 return t->cplx ? 2 * a : a; 361 } 362 /* floats spanned by the side: one past its highest addressed float */ 363 static size_t tlay_span(const TLay *t, uint32_t batch) { 364 size_t pts = tlay_points(t), top = 0; 365 for (uint32_t b = 0; b < batch; b++) 366 for (size_t i = 0; i < pts; i++) { 367 size_t f = tlay_at(t, b, i) + (t->cplx ? 2 : 1); 368 if (f > top) top = f; 369 } 370 return top; 371 } 372 /* index of the point mirrored through the origin on the outer axes (all but the last) of a [ext] row-major array */ 373 static size_t outer_mirror(uint32_t rank, const uint32_t *n, size_t o) { 374 uint32_t x[3] = { 0, 0, 0 }; 375 for (uint32_t d = rank - 1; d-- > 0;) { x[d] = (uint32_t)(o % n[d]); o /= n[d]; } 376 size_t m = 0; 377 for (uint32_t d = 0; d + 1 < rank; d++) m = m * n[d] + (x[d] ? n[d] - x[d] : 0); 378 return m; 379 } 380 /* random dense logical input of a plan; for C2R a Hermitian half spectrum (the points of the last-axis bins 0 and 381 * n / 2 that are their own mirrors are real, the others equal the conjugate of their mirror) */ 382 static void many_input(const OmegaGpuFftMany *p, float *x) { 383 uint32_t r = p->rank, l = p->n[r - 1], h = l / 2 + 1; 384 size_t P = many_prod(r, p->n), outer = P / l; 385 if (p->type == OMEGA_GPU_FFT_TYPE_C2C) { fill_rand(x, 2 * P * p->batch); return; } 386 if (p->type == OMEGA_GPU_FFT_TYPE_R2C) { fill_rand(x, P * p->batch); return; } 387 fill_rand(x, 2 * outer * h * p->batch); 388 for (uint32_t b = 0; b < p->batch; b++) 389 for (uint32_t k = 0; k <= l / 2; k += (l % 2 == 0 && l / 2 > 0) ? l / 2 : l) { 390 float *H = x + 2 * outer * h * b; 391 for (size_t o = 0; o < outer; o++) { 392 size_t i = o * h + k, m = outer_mirror(r, p->n, o) * h + k; 393 if (m == i) H[2 * i + 1] = 0; 394 else if (m < i) { H[2 * i] = H[2 * m]; H[2 * i + 1] = -H[2 * m + 1]; } 395 } 396 } 397 } 398 /* f64 reference of a many plan over dense logical arrays (batch-major, last index fastest): y gets the dense 399 * logical output (C2C complex, R2C the half rows, C2R reals, unnormalised) */ 400 static void many_ref(const OmegaGpuFftMany *p, const float *x, double *y) { 401 uint32_t r = p->rank, l = p->n[r - 1], h = l / 2 + 1; 402 size_t P = many_prod(r, p->n), outer = P / l; 403 double *z = malloc(2 * P * sizeof *z); 404 for (uint32_t b = 0; b < p->batch; b++) { 405 if (p->type == OMEGA_GPU_FFT_TYPE_C2C) 406 for (size_t i = 0; i < 2 * P; i++) z[i] = x[2 * P * b + i]; 407 else if (p->type == OMEGA_GPU_FFT_TYPE_R2C) 408 for (size_t i = 0; i < P; i++) { z[2 * i] = x[P * b + i]; z[2 * i + 1] = 0; } 409 else { 410 const float *H = x + 2 * outer * h * b; 411 for (size_t o = 0; o < outer; o++) 412 for (uint32_t k = 0; k < l; k++) { 413 size_t s = k < h ? o * h + k : outer_mirror(r, p->n, o) * h + (l - k); 414 z[2 * (o * l + k)] = H[2 * s]; 415 z[2 * (o * l + k) + 1] = k < h ? H[2 * s + 1] : -H[2 * s + 1]; 416 } 417 } 418 int inv = p->type == OMEGA_GPU_FFT_TYPE_C2C ? p->inverse : p->type == OMEGA_GPU_FFT_TYPE_C2R; 419 for (uint32_t ax = 0; ax < r; ax++) { 420 size_t ou = 1, in = 1; 421 for (uint32_t d = 0; d < ax; d++) ou *= p->n[d]; 422 for (uint32_t d = ax + 1; d < r; d++) in *= p->n[d]; 423 dft_axis(z, ou, p->n[ax], in, inv); 424 } 425 if (p->type == OMEGA_GPU_FFT_TYPE_C2C) 426 for (size_t i = 0; i < 2 * P; i++) y[2 * P * b + i] = z[i]; 427 else if (p->type == OMEGA_GPU_FFT_TYPE_R2C) 428 for (size_t o = 0; o < outer; o++) 429 for (uint32_t k = 0; k < h; k++) { 430 y[2 * (outer * h * b + o * h + k)] = z[2 * (o * l + k)]; 431 y[2 * (outer * h * b + o * h + k) + 1] = z[2 * (o * l + k) + 1]; 432 } 433 else 434 for (size_t i = 0; i < P; i++) y[P * b + i] = z[2 * i]; 435 } 436 free(z); 437 } 438 439 /* ------------------------------------------------------------------ Philox4x32-10 reference (Random123, cuRAND) */ 440 441 #define PH_M0 0xD2511F53u 442 #define PH_M1 0xCD9E8D57u 443 #define PH_W0 0x9E3779B9u 444 #define PH_W1 0xBB67AE85u 445 /* rounds of Philox4x32 on ctr with key (key lo, key hi): the round, then the Weyl step of the key */ 446 static void philox_block(const uint32_t ctr[4], uint64_t key, int rounds, uint32_t out[4]) { 447 uint32_t c0 = ctr[0], c1 = ctr[1], c2 = ctr[2], c3 = ctr[3], k0 = (uint32_t)key, k1 = (uint32_t)(key >> 32); 448 for (int r = 0; r < rounds; r++) { 449 uint64_t p0 = (uint64_t)PH_M0 * c0, p1 = (uint64_t)PH_M1 * c2; 450 uint32_t n0 = (uint32_t)(p1 >> 32) ^ c1 ^ k0, n2 = (uint32_t)(p0 >> 32) ^ c3 ^ k1; 451 c0 = n0; c1 = (uint32_t)p1; c2 = n2; c3 = (uint32_t)p0; 452 k0 += PH_W0; k1 += PH_W1; 453 } 454 out[0] = c0; out[1] = c1; out[2] = c2; out[3] = c3; 455 } 456 /* Random123 tests/kat_vectors, philox4x32 10: zero, all ones, pi digits (ctr, key, expected output) */ 457 static const struct { uint32_t ctr[4], key[2], want[4]; } PH_KAT[3] = { 458 { { 0, 0, 0, 0 }, { 0, 0 }, { 0x6627e8d5, 0xe169c58d, 0xbc57ac4c, 0x9b00dbd8 } }, 459 { { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }, { 0xffffffff, 0xffffffff }, 460 { 0x408f276d, 0x41c83b0e, 0xa20bc7c6, 0x6d5451fd } }, 461 { { 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344 }, { 0xa4093822, 0x299f31d0 }, 462 { 0xd16cfe09, 0x94fdcceb, 0x5001e420, 0x24126ea1 } }, 463 }; 464 /* cuRAND's k-th curand() output after curand_init(seed, subseq, offset), offset % 4 == 0: counter 465 * (block lo, block hi, subseq lo, subseq hi), block = offset / 4 + k / 4, key (seed lo, seed hi) */ 466 static void philox_ctr(uint64_t blk, uint64_t sub, uint32_t ctr[4]) { 467 ctr[0] = (uint32_t)blk; ctr[1] = (uint32_t)(blk >> 32); ctr[2] = (uint32_t)sub; ctr[3] = (uint32_t)(sub >> 32); 468 } 469 static uint32_t philox_ref(uint64_t seed, uint64_t sub, uint64_t offset, uint32_t k) { 470 uint32_t ctr[4], o[4]; 471 philox_ctr(offset / 4 + k / 4, sub, ctr); 472 philox_block(ctr, seed, 10, o); 473 return o[k % 4]; 474 } 475 476 /* ------------------------------------------------------------------ fixture */ 477 478 typedef struct { uint64_t seed; uint32_t pos; uint64_t val; } FixRow; 479 static FixRow g_fix[1024]; 480 static size_t g_nfix; 481 static int load_fixture(const char *path) { 482 FILE *f = fopen(path, "r"); 483 if (!f) return -1; 484 char line[256]; 485 while (fgets(line, sizeof line, f) && g_nfix < sizeof g_fix / sizeof g_fix[0]) { 486 if (line[0] == '#' || line[0] == '\n') continue; 487 char *e1, *e2, *e3; 488 uint64_t s = strtoull(line, &e1, 10); 489 unsigned long p = strtoul(e1, &e2, 10); 490 uint64_t v = strtoull(e2, &e3, 10); 491 if (e1 == line || e2 == e1 || e3 == e2) { fclose(f); return -2; } 492 g_fix[g_nfix++] = (FixRow){ s, (uint32_t)p, v }; 493 } 494 fclose(f); 495 return 0; 496 } 497 static const uint64_t BASES[3] = { 0, 1729, 0xFFFFFFFFFFFFFFFCull }; 498 499 /* fixture rows with seed = base + g (g < ns) and offset <= pos < offset + per, checked against out; g_fix_hit marks 500 * every row compared (runs overlap, so the distinct count is what says the whole fixture was covered) */ 501 static unsigned char g_fix_hit[1024]; 502 static size_t fixture_compare(const uint64_t *out, uint64_t base, uint32_t ns, uint32_t offset, uint32_t per, 503 size_t *bad) { 504 size_t used = 0; 505 for (size_t i = 0; i < g_nfix; i++) { 506 uint64_t g = g_fix[i].seed - base; 507 if (g >= ns || g_fix[i].pos < offset || g_fix[i].pos >= offset + per) continue; 508 used++; 509 g_fix_hit[i] = 1; 510 if (out[(size_t)(g_fix[i].pos - offset) * ns + g] != g_fix[i].val) (*bad)++; 511 } 512 return used; 513 } 514 static size_t fixture_distinct(void) { 515 size_t n = 0; 516 for (size_t i = 0; i < g_nfix; i++) n += g_fix_hit[i]; 517 return n; 518 } 519 520 /* the cuRAND fixture: "seed subseq offset k value_hex8", decimal but the value (tests/fixtures/philox_curand_oracle.txt 521 * carries the generator source and command; the oracle is not a build dependency) */ 522 typedef struct { uint64_t seed, sub, off; uint32_t k, val; } PhRow; 523 static PhRow g_ph[1024]; 524 static size_t g_nph; 525 static unsigned char g_ph_hit[1024]; 526 static int load_philox_fixture(const char *path) { 527 FILE *f = fopen(path, "r"); 528 if (!f) return -1; 529 char line[256]; 530 int rc = 0; 531 while (rc == 0 && fgets(line, sizeof line, f)) { 532 if (line[0] == '#' || line[0] == '\n') continue; 533 char *e0, *e1, *e2, *e3, *e4; 534 uint64_t s = strtoull(line, &e0, 10); 535 uint64_t sub = strtoull(e0, &e1, 10); 536 uint64_t off = strtoull(e1, &e2, 10); 537 unsigned long k = strtoul(e2, &e3, 10); 538 unsigned long v = strtoul(e3, &e4, 16); 539 if (e0 == line || e1 == e0 || e2 == e1 || e3 == e2 || e4 == e3 || k > 7 || v > 0xffffffffUL) rc = -2; 540 else if (g_nph == sizeof g_ph / sizeof g_ph[0]) rc = -3; 541 else g_ph[g_nph++] = (PhRow){ s, sub, off, (uint32_t)k, (uint32_t)v }; 542 } 543 fclose(f); 544 return rc; 545 } 546 /* rows of this seed and offset with subseq0 <= sub < subseq0 + ns and k < per, against the API layout */ 547 static size_t ph_compare(const uint32_t *out, uint64_t seed, uint64_t sub0, uint32_t ns, uint64_t offset, uint32_t per, 548 size_t *bad) { 549 size_t used = 0; 550 for (size_t i = 0; i < g_nph; i++) { 551 uint64_t g = g_ph[i].sub - sub0; 552 if (g_ph[i].seed != seed || g_ph[i].off != offset || g >= ns || g_ph[i].k >= per) continue; 553 used++; 554 g_ph_hit[i] = 1; 555 if (out[((size_t)(g_ph[i].k / 4) * ns + g) * 4 + g_ph[i].k % 4] != g_ph[i].val) (*bad)++; 556 } 557 return used; 558 } 559 static size_t ph_distinct(void) { 560 size_t n = 0; 561 for (size_t i = 0; i < g_nph; i++) n += g_ph_hit[i]; 562 return n; 563 } 564 static const uint64_t PH_SEEDS[4] = { 0, 1729, 0x123456789abcdefull, 0xffffffffffffffffull }; 565 static const uint64_t PH_OFFS[4] = { 0, 4, 1000, 0x400000000ull }; 566 567 /* ------------------------------------------------------------------ cuRAND Philox distributions */ 568 569 /* The kernel's arithmetic, operation for operation in f32 (the file builds with -ffp-contract=off; every fused step 570 * is an explicit fmaf), so the simulator must match it bit for bit. Against cuRAND (the host-built oracle 571 * tests/fixtures/philox_curand_dist_oracle.txt) the uniforms are bit-exact and the normal is within 572 * 2^-21 sqrt(-2 log u) (the bound is derived in the evidence README; the formulas are cuRAND's: curand_kernel.h, 573 * curand_uniform.h, curand_normal.h of cuRAND 10.4.0). */ 574 static uint64_t ph_d2u(double d) { uint64_t u; memcpy(&u, &d, 8); return u; } 575 /* (float)x by two exact halves and one rounding */ 576 static float ph_frn(uint32_t x) { return fmaf((float)(int32_t)(x >> 16), 65536.0f, (float)(int32_t)(x & 0xffff)); } 577 /* _curand_uniform: x * 2^-32 + 2^-33, one FFMA */ 578 static float ph_unif(uint32_t x) { return fmaf(ph_frn(x), 0x1p-32f, 0x1p-33f); } 579 /* logf of u in (0, 1]: u = 2^k m, m in [sqrt(1/2), sqrt(2)), log1p(m - 1) by the degree-10 polynomial */ 580 static float ph_log(float u) { 581 static const uint32_t LC[9] = { 0x3eaaaaaa, 0xbe7ffffc, 0x3e4cceac, 0xbe2aae50, 0x3e11e9bf, 582 0xbdfe5d4f, 0x3def251a, 0xbdebd1b6, 0x3d9021ba }; 583 uint32_t t = f2u(u) + 0x004afb0du; 584 float m = u2f((t & 0x7fffffu) + 0x3f3504f3u); 585 float f = m + u2f(0xbf800000u); 586 float k = (float)(int32_t)(t >> 23) + u2f(0xc2fe0000u); 587 float r = u2f(LC[8]); 588 for (int i = 7; i >= 0; i--) r = fmaf(r, f, u2f(LC[i])); 589 float p = fmaf(f, r, -0.5f); 590 p = fmaf(f * f, p, f); 591 return fmaf(k, u2f(0x3f317180u), fmaf(k, u2f(0x3717f7d2u), p)); 592 } 593 /* sqrtf by the chip's MUFU.RSQ and one Newton correction */ 594 static float ph_sqrt(float w) { 595 float y0 = ws_mufu_rsq(fmaxf(w, u2f(0x00800000u))); 596 float s0 = w * y0; 597 float r = fmaf(s0 * -1.0f, s0, w); 598 return fmaf(r, y0 * 0.5f, s0); 599 } 600 /* sincosf of v in [pi 2^-32 / 2, 2 pi]: quadrant q by round-to-nearest of v 2/pi, Cody-Waite in three parts, 601 * the sin and cos polynomials, then the quadrant's signs without a branch */ 602 static void ph_sincos(float v, float *sn, float *cs) { 603 float kf = fmaf(v, u2f(0x3f22f983u), u2f(0x4b400000u)) + u2f(0xcb400000u); 604 float r = fmaf(kf, u2f(0xbfc90fdbu), v); 605 r = fmaf(kf, u2f(0x333bbd2eu), r); 606 r = fmaf(kf, u2f(0x26f72cedu), r); 607 float r2 = r * r; 608 float ps = fmaf(r2, u2f(0x3638ef1du), u2f(0xb9500d01u)); 609 ps = fmaf(r2, ps, u2f(0x3c088889u)); 610 ps = fmaf(r2, ps, u2f(0xbe2aaaabu)); 611 float sinr = fmaf(r * r2, ps, r); 612 float pc = fmaf(r2, u2f(0xb493f27eu), u2f(0x37d00d01u)); 613 pc = fmaf(r2, pc, u2f(0xbab60b61u)); 614 pc = fmaf(r2, pc, u2f(0x3d2aaaabu)); 615 pc = fmaf(r2, pc, -0.5f); 616 float cosr = fmaf(r2, pc, 1.0f); 617 float q = kf + -2.0f; 618 float aq = fmaxf(q, q * -1.0f); 619 float a = aq + -1.0f, b = q * (aq + -2.0f); 620 *sn = fmaf(sinr, a, cosr * b); 621 *cs = fmaf(cosr, a, sinr * (b * -1.0f)); 622 } 623 /* _curand_box_muller(x, y): u = uniform(x), v = y 2pi 2^-32 + pi 2^-32, s = sqrt(-2 log u); (sin v s, cos v s) */ 624 static void ph_box_muller(uint32_t x, uint32_t y, float *n0, float *n1) { 625 float v = ph_frn(y) * u2f(0x30c90fdbu) + u2f(0x30490fdbu); 626 float s = ph_sqrt(ph_log(ph_unif(x)) * -2.0f); 627 float sn, cs; 628 ph_sincos(v, &sn, &cs); 629 *n0 = sn * s; 630 *n1 = cs * s; 631 } 632 /* _curand_uniform_double_hq(x, y): z = x ^ (y << 21), z 2^-53 + 2^-54 */ 633 static double ph_hq(uint32_t x, uint32_t y) { return (double)((uint64_t)x ^ (uint64_t)y << 21) * 0x1p-53 + 0x1p-54; } 634 /* the scale of the normal tolerance: sqrt(-2 log u) in f64 for the u of the pair at word p */ 635 static double ph_normal_scale(uint32_t x) { return sqrt(-2.0 * log((double)ph_unif(x))); } 636 static uint32_t ph_word(uint64_t seed, uint64_t sub, uint64_t pos) { 637 return philox_ref(seed, sub, pos - pos % 4, (uint32_t)(pos % 4)); 638 } 639 /* output k of a stream curand_init(seed, sub, off) as bits (u32 or f32 in the low word, f64 whole) */ 640 static uint64_t ph_dist_ref(int d, uint64_t seed, uint64_t sub, uint64_t off, uint64_t k) { 641 if (d == OMEGA_GPU_PHILOX_RAW) return ph_word(seed, sub, off + k); 642 if (d == OMEGA_GPU_PHILOX_UNIFORM) return f2u(ph_unif(ph_word(seed, sub, off + k))); 643 if (d == OMEGA_GPU_PHILOX_UNIFORM_DOUBLE) return ph_d2u(((double)ph_word(seed, sub, off + k) + 1.0) * 0x1p-32); 644 if (d == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE) { 645 uint64_t p = off + 4 * (k / 2) + 2 * (k & 1); 646 return ph_d2u(ph_hq(ph_word(seed, sub, p), ph_word(seed, sub, p + 1))); 647 } 648 uint64_t p = off + 2 * (k / 2); 649 float a, b; 650 ph_box_muller(ph_word(seed, sub, p), ph_word(seed, sub, p + 1), &a, &b); 651 return f2u(k & 1 ? b : a); 652 } 653 static uint32_t ph_esz(int d) { return d == OMEGA_GPU_PHILOX_UNIFORM_DOUBLE || d == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE ? 2 : 1; } 654 /* the API layout: G outputs per group of four words; whole groups interleave across streams, partial groups follow */ 655 static size_t ph_index(int d, uint32_t ns, uint32_t per, uint32_t g, uint32_t k) { 656 uint32_t G = d == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE ? 2 : 4, J = per / G; 657 if (k < J * G) return ((size_t)(k / G) * ns + g) * G + k % G; 658 return (size_t)J * ns * G + (size_t)g * (per % G) + (k - J * G); 659 } 660 /* a whole run in the API layout, as 32-bit words (2 per f64 element) */ 661 static void ref_dist(int d, uint64_t seed, uint64_t sub0, uint32_t ns, uint64_t off, uint32_t per, uint32_t *out) { 662 uint32_t esz = ph_esz(d); 663 for (uint32_t g = 0; g < ns; g++) 664 for (uint32_t k = 0; k < per; k++) { 665 uint64_t v = ph_dist_ref(d, seed, sub0 + g, off, k); 666 size_t i = ph_index(d, ns, per, g, k); 667 if (esz == 1) out[i] = (uint32_t)v; 668 else { out[2 * i] = (uint32_t)v; out[2 * i + 1] = (uint32_t)(v >> 32); } 669 } 670 } 671 static void ref_philox(uint64_t seed, uint64_t sub0, uint32_t ns, uint64_t offset, uint32_t per, uint32_t *out) { 672 ref_dist(OMEGA_GPU_PHILOX_RAW, seed, sub0, ns, offset, per, out); 673 } 674 675 /* the distribution fixture: "dist seed subseq offset k value_hex16" (dist 5, curand_normal_double, is not provided: 676 * its rows are loaded and counted, never compared) */ 677 typedef struct { uint64_t seed, sub, off, val; uint32_t d, k; } PdRow; 678 #define PD_ROWS 4480 679 static PdRow g_pd[PD_ROWS]; 680 static size_t g_npd; 681 static unsigned char g_pd_hit[PD_ROWS]; 682 static int load_dist_fixture(const char *path) { 683 FILE *f = fopen(path, "r"); 684 if (!f) return -1; 685 char line[256]; 686 int rc = 0; 687 while (rc == 0 && fgets(line, sizeof line, f)) { 688 if (line[0] == '#' || line[0] == '\n') continue; 689 char *e[6]; 690 unsigned long d = strtoul(line, &e[0], 10); 691 uint64_t s = strtoull(e[0], &e[1], 10); 692 uint64_t sub = strtoull(e[1], &e[2], 10); 693 uint64_t off = strtoull(e[2], &e[3], 10); 694 unsigned long k = strtoul(e[3], &e[4], 10); 695 uint64_t v = strtoull(e[4], &e[5], 16); 696 if (e[0] == line || e[1] == e[0] || e[2] == e[1] || e[3] == e[2] || e[4] == e[3] || e[5] == e[4] || d < 1 || 697 d > 5 || k > 7 || (d != 2 && d != 3 && d != 5 && v > 0xffffffffull)) 698 rc = -2; 699 else if (g_npd == PD_ROWS) rc = -3; 700 else g_pd[g_npd++] = (PdRow){ s, sub, off, v, (uint32_t)d, (uint32_t)k }; 701 } 702 fclose(f); 703 return rc; 704 } 705 /* one value against a fixture row: bit-exact for the uniforms, within 2^-21 sqrt(-2 log u) for the normal (both 0 706 * when that scale is 0) */ 707 static int pd_agrees(const PdRow *r, uint64_t got) { 708 if (r->d != OMEGA_GPU_PHILOX_NORMAL) return got == r->val; 709 double s = ph_normal_scale(ph_word(r->seed, r->sub, r->off + 2 * (r->k / 2))); 710 double e = fabs((double)u2f((uint32_t)got) - (double)u2f((uint32_t)r->val)); 711 return s > 0 ? e <= 0x1p-21 * s : got == r->val; 712 } 713 /* fixture rows of dist d, this seed and offset, subseq0 <= sub < subseq0 + ns and k < per against a run's output */ 714 static size_t pd_compare(int d, const uint32_t *out, uint64_t seed, uint64_t sub0, uint32_t ns, uint64_t off, 715 uint32_t per, size_t *bad) { 716 size_t used = 0; 717 for (size_t i = 0; i < g_npd; i++) { 718 const PdRow *r = &g_pd[i]; 719 uint64_t g = r->sub - sub0; 720 if ((int)r->d != d || r->seed != seed || r->off != off || g >= ns || r->k >= per) continue; 721 size_t j = ph_index(d, ns, per, (uint32_t)g, r->k); 722 uint64_t v = ph_esz(d) == 1 ? out[j] : out[2 * j] | (uint64_t)out[2 * j + 1] << 32; 723 used++; 724 g_pd_hit[i] = 1; 725 *bad += !pd_agrees(r, v); 726 } 727 return used; 728 } 729 static size_t pd_distinct(int d) { 730 size_t n = 0; 731 for (size_t i = 0; i < g_npd; i++) n += g_pd_hit[i] && (int)g_pd[i].d == d; 732 return n; 733 } 734 static const uint64_t PD_OFFS[7] = { 0, 1, 2, 3, 6, 1001, 0x400000003ull }; 735 736 /* ------------------------------------------------------------------ host tier */ 737 738 static int build(uint64_t kind, uint64_t a, uint64_t b, uint64_t c, uint32_t threads, BlackwellIRProgram *prog, 739 uint8_t *code, OmegaBlackwellKernel *k) { 740 uint32_t declared = 0; 741 uint64_t emit_err = 0; 742 return omega_gpu_om_ir_build(kind, a, b, c, threads, prog, code, OMEGA_BW_CODE_CAP_MAX, k, &declared, &emit_err); 743 } 744 745 static char *listing_of(const OmegaBlackwellKernel *k) { 746 char *buf = NULL; 747 size_t len = 0; 748 FILE *f = open_memstream(&buf, &len); 749 if (!f) return NULL; 750 int rc = omega_sm121_listing(k->code, k->code_size, f); 751 fclose(f); 752 if (rc != 0) { 753 printf("listing: %d words match no known form\n", rc); 754 for (char *l = buf; l && (l = strstr(l, "??")); l++) { 755 char *e = strchr(l, '\n'); 756 printf(" %.*s\n", e ? (int)(e - l) : 60, l); 757 } 758 free(buf); 759 return NULL; 760 } 761 return buf; 762 } 763 764 static void host_fixture(void) { 765 check(g_nfix == 792, "fixture rows %zu, expected 792", g_nfix); 766 size_t bad = 0; 767 for (size_t i = 0; i < g_nfix; i++) { 768 Xo x; 769 xo_seed(&x, g_fix[i].seed); 770 for (uint32_t p = 0; p < g_fix[i].pos; p++) (void)xo_next(&x); 771 if (xo_next(&x) != g_fix[i].val) bad++; 772 } 773 check(bad == 0, "C reference differs from aien-math in %zu of %zu fixture rows", bad, g_nfix); 774 receipt("host rng_reference_vs_aienmath rows=%zu mismatches=%zu", g_nfix, bad); 775 } 776 777 static void host_refusals(void) { 778 uint64_t buf[4]; 779 float fb[64], fo[64]; 780 check(omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0, 0, 0, 0, 1, buf, NULL) == OMEGA_GPU_ML_BAD_ARGS, "rng ns=0"); 781 check(omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0, 0, 1, 0, 0, buf, NULL) == OMEGA_GPU_ML_BAD_ARGS, "rng per=0"); 782 check(omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0, 0, 1, 0, 1, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, "rng out NULL"); 783 check(omega_gpu_rng_generate((OmegaGpuRngKind)3, 0, 0, 1, 0, 1, buf, NULL) == OMEGA_GPU_ML_BAD_ARGS, "rng kind 3"); 784 check(omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0, 0, OMEGA_GPU_RNG_MAX_STREAMS + 1, 0, 1, buf, NULL) == 785 OMEGA_GPU_ML_TOO_LARGE, "rng streams past 131072"); 786 check(omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0, 0, OMEGA_GPU_RNG_MAX_STREAMS, 0, 2049, buf, NULL) == 787 OMEGA_GPU_ML_TOO_LARGE, "rng outputs past 2^28"); 788 check(omega_gpu_rng_codegen((OmegaGpuRngKind)3, NULL) == OMEGA_GPU_ML_BAD_ARGS, "rng codegen kind 3"); 789 /* lengths no transform takes (the others in 2..4096 run through omega_gpu_fft_many; never opened here) */ 790 const uint32_t badn[] = { 0, 1, 4097, 8192 }, badcg[] = { 0, 1, 2, 3, 6, 1000, 4096, 8192 }; 791 for (size_t i = 0; i < sizeof badn / sizeof badn[0]; i++) 792 check(omega_gpu_fft_c2c(fb, fo, badn[i], 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft n=%u", badn[i]); 793 for (size_t i = 0; i < sizeof badcg / sizeof badcg[0]; i++) 794 check(omega_gpu_fft_codegen(badcg[i], 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft codegen n=%u", badcg[i]); 795 check(omega_gpu_fft_c2c(fb, fb, 12, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft n=12 in == out"); 796 check(omega_gpu_fft_c2c(fb, fb + 4, 3, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft n=3 overlapping in/out"); 797 check(omega_gpu_fft_c2c(fb, fo, 12, 1, 2, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft n=12 inverse=2"); 798 check(omega_gpu_fft_c2c(fb, fo, 12, 0, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft n=12 batch=0"); 799 check(omega_gpu_fft_c2c(fb, fo, 4095, 65536, 0, NULL) == OMEGA_GPU_ML_TOO_LARGE, "fft n=4095 past 2^27 elements"); 800 check(omega_gpu_fft_c2c(fb, fo, 4, 0, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft batch=0"); 801 check(omega_gpu_fft_c2c(fb, fo, 4, 1, 2, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft inverse=2"); 802 check(omega_gpu_fft_c2c(NULL, fo, 4, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft in NULL"); 803 check(omega_gpu_fft_c2c(fb, NULL, 4, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft out NULL"); 804 check(omega_gpu_fft_c2c(fb, fb + 4, 4, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "fft overlapping in/out"); 805 check(omega_gpu_fft_c2c(fb, fo, 2048, 65537, 0, NULL) == OMEGA_GPU_ML_TOO_LARGE, "fft past 2^28 floats"); 806 check(omega_gpu_fft_twiddles(8, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "twiddles NULL"); 807 uint32_t pw[8]; 808 check(omega_gpu_philox_generate(0, 0, 0, 0, 4, pw, NULL) == OMEGA_GPU_ML_BAD_ARGS, "philox ns=0"); 809 check(omega_gpu_philox_generate(0, 0, 1, 0, 0, pw, NULL) == OMEGA_GPU_ML_BAD_ARGS, "philox per=0"); 810 check(omega_gpu_philox_generate_dist((OmegaGpuPhiloxDist)5, 0, 0, 1, 0, 4, pw, NULL) == OMEGA_GPU_ML_BAD_ARGS, 811 "philox dist 5 (normal_double, not provided)"); 812 check(omega_gpu_philox_generate_dist((OmegaGpuPhiloxDist)-1, 0, 0, 1, 0, 4, pw, NULL) == OMEGA_GPU_ML_BAD_ARGS, 813 "philox dist -1"); 814 check(omega_gpu_philox_generate_dist(OMEGA_GPU_PHILOX_NORMAL, 0, 0, 1, 0, 4, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, 815 "philox dist out NULL"); 816 check(omega_gpu_philox_generate_dist(OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE, 0, 0, 1, 3, 0, pw, NULL) == 817 OMEGA_GPU_ML_BAD_ARGS, "philox dist per=0"); 818 check(omega_gpu_philox_generate_dist(OMEGA_GPU_PHILOX_UNIFORM, 0, 0, OMEGA_GPU_PHILOX_MAX_STREAMS, 1, 2049, pw, 819 NULL) == OMEGA_GPU_ML_TOO_LARGE, "philox dist outputs past 2^28"); 820 check(omega_gpu_philox_generate(0, 0, 1, 0, 4, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, "philox out NULL"); 821 check(omega_gpu_philox_generate(0, 0, OMEGA_GPU_PHILOX_MAX_STREAMS + 1, 0, 4, pw, NULL) == OMEGA_GPU_ML_TOO_LARGE, 822 "philox streams past 131072"); 823 check(omega_gpu_philox_generate(0, 0, OMEGA_GPU_PHILOX_MAX_STREAMS, 0, 2052, pw, NULL) == OMEGA_GPU_ML_TOO_LARGE, 824 "philox outputs past 2^28"); 825 const uint32_t badr[] = { 0, 1, 4097, 8192 }; 826 for (size_t i = 0; i < sizeof badr / sizeof badr[0]; i++) { 827 check(omega_gpu_fft_r2c(fb, fo, badr[i], 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "r2c n=%u", badr[i]); 828 check(omega_gpu_fft_c2r(fb, fo, badr[i], 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "c2r n=%u", badr[i]); 829 } 830 check(omega_gpu_fft_r2c(fb, fo, 8, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "r2c batch=0"); 831 check(omega_gpu_fft_r2c(NULL, fo, 8, 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "r2c in NULL"); 832 check(omega_gpu_fft_c2r(fb, NULL, 8, 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "c2r out NULL"); 833 check(omega_gpu_fft_r2c(fb, fb + 4, 8, 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "r2c overlapping in/out"); 834 check(omega_gpu_fft_c2r(fb + 4, fb, 8, 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "c2r overlapping in/out"); 835 check(omega_gpu_fft_r2c(fb, fo, 4096, 32769, NULL) == OMEGA_GPU_ML_TOO_LARGE, "r2c past 2^27 elements"); 836 check(omega_gpu_fft_r2c(fb, fb + 2, 12, 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "r2c n=12 overlapping in/out"); 837 check(omega_gpu_fft_c2r(fb, fb, 7, 1, NULL) == OMEGA_GPU_ML_BAD_ARGS, "c2r n=7 in == out"); 838 check(omega_gpu_fft_r2c(fb, fo, 4095, 65536, NULL) == OMEGA_GPU_ML_TOO_LARGE, "r2c n=4095 past 2^27 elements"); 839 const uint32_t d2[] = { 8, 16 }, d4[] = { 4, 4, 4, 4 }, dbig[] = { 2048, 2048, 64 }; 840 const uint32_t dbad[][3] = { { 1, 4, 4 }, { 4097, 4, 4 }, { 4, 8192, 4 }, { 4, 4, 0 } }; 841 check(omega_gpu_fft_c2c_nd(fb, fo, 0, d2, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd ndim=0"); 842 check(omega_gpu_fft_c2c_nd(fb, fo, 4, d4, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd ndim=4"); 843 check(omega_gpu_fft_c2c_nd(fb, fo, 2, NULL, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd dims NULL"); 844 for (size_t i = 0; i < sizeof dbad / sizeof dbad[0]; i++) 845 check(omega_gpu_fft_c2c_nd(fb, fo, 3, dbad[i], 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd dims %u %u %u", 846 dbad[i][0], dbad[i][1], dbad[i][2]); 847 check(omega_gpu_fft_c2c_nd(fb, fo, 2, d2, 1, 2, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd inverse=2"); 848 check(omega_gpu_fft_c2c_nd(fb, fo, 2, d2, 0, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd batch=0"); 849 check(omega_gpu_fft_c2c_nd(fb, fo, 3, dbig, 1, 0, NULL) == OMEGA_GPU_ML_TOO_LARGE, "nd 2^28 elements"); 850 check(omega_gpu_fft_c2c_nd(fb, fo, 2, d2, (1u << 20) + 1, 0, NULL) == OMEGA_GPU_ML_TOO_LARGE, 851 "nd batch past 2^27 elements"); 852 check(omega_gpu_fft_c2c_nd(fb, fb + 2, 2, d2, 1, 0, NULL) == OMEGA_GPU_ML_BAD_ARGS, "nd overlapping in/out"); 853 size_t by[3]; 854 uint32_t n8 = 8; 855 check(omega_gpu_fft_plan_walk((OmegaGpuFftPlanMode)3, 1, &n8, 1, 0, by, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, 856 "plan mode 3"); 857 check(omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_R2C, 1, &n8, 1, 1, by, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, 858 "plan r2c inverse 1"); 859 check(omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_C2R, 2, d2, 1, 0, by, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, 860 "plan c2r ndim 2"); 861 check(omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_R2C, 1, &n8, 1, 0, NULL, NULL, NULL) == OMEGA_GPU_ML_BAD_ARGS, 862 "plan bytes NULL"); 863 /* the unit's own refusals */ 864 BlackwellIRProgram *prog = calloc(1, sizeof *prog); 865 uint8_t *code = malloc(OMEGA_BW_CODE_CAP_MAX); 866 OmegaBlackwellKernel k; 867 const uint64_t refuse[][4] = { 868 { 0, 3, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 3 }, { 1, 1, 0, 0 }, { 1, 12, 0, 0 }, { 1, 4, 2, 0 }, 869 { 1, 4, 0, 3 }, { 2, 1, 0, 0 }, { 2, 4, 0, 1 }, { 2, 13, 0, 0 }, { 2, 12, 2, 0 }, { 3, 5, 0, 0 }, 870 { 3, 0, 4, 0 }, { 3, 0, 0, 7 }, { 4, 2, 0, 0 }, { 4, 13, 0, 0 }, { 4, 4, 2, 0 }, { 4, 4, 0, 3 }, 871 { 5, 1, 4, 0 }, { 5, 4, 1, 0 }, { 5, 4, 12, 0 }, { 5, 20, 8, 0 }, { 5, 26, 2, 0 }, { 5, 4, 4, 3 }, 872 { 7, 0, 0, 0 }, { 7, 6, 0, 0 }, { 7, 8, 0, 0 }, { 7, 2, 2, 0 }, { 7, 2, 0, 3 }, { 8, 4, 5, 0 }, 873 { 8, 0, 4, 0 }, { 8, 0, 14, 0 }, { 8, 0, 5, 3 }, { 9, 4, 0, 0 }, { 9, 0, 1, 0 }, { 9, 0, 0, 3 }, 874 }; 875 for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) 876 check(build(refuse[i][0], refuse[i][1], refuse[i][2], refuse[i][3], 128, prog, code, &k) == OMEGA_OM_IR_REFUSED, 877 "unit accepted kind %" PRIu64 " a %" PRIu64 " b %" PRIu64 " c %" PRIu64, refuse[i][0], refuse[i][1], 878 refuse[i][2], refuse[i][3]); 879 /* kind 6 (the pass plan) through the data entry: a = mode | ndim << 4, b = log2 dims (4 bits each), c = dir */ 880 static uint64_t words[OMEGA_OM_IR_WORDS]; 881 const uint64_t refuse6[][3] = { 882 { 3 | 1 << 4, 4, 0 }, { 1 | 1 << 4, 13, 0 }, { 1 | 1 << 4, 2, 0 }, { 1 | 2 << 4, 4 | 4 << 4, 0 }, 883 { 1 | 1 << 4, 4, 1 }, { 2 | 1 << 4, 12, 1 }, { 0, 0, 0 }, { 4 << 4, 0x2222, 0 }, 884 { 1 << 4, 1, 0 }, { 1 << 4, 12, 0 }, { 1 << 4, 4 | 4 << 4, 0 }, { 3 << 4, 11 | 11 << 4 | 6 << 8, 0 }, 885 { 2 << 4, 4 | 4 << 4, 2 }, { 1 << 4 | 1 << 8, 4, 0 }, 886 }; 887 for (size_t i = 0; i < sizeof refuse6 / sizeof refuse6[0]; i++) 888 check(omega_gpu_om_ir_data(6, refuse6[i][0], refuse6[i][1], refuse6[i][2], words) == OMEGA_OM_IR_REFUSED, 889 "unit planned a %#" PRIx64 " b %#" PRIx64 " c %" PRIu64, refuse6[i][0], refuse6[i][1], refuse6[i][2]); 890 free(prog); 891 free(code); 892 } 893 894 static void host_codegen(void) { 895 OmegaGpuMathInfo a, b; 896 uint8_t base_rng[3][32]; 897 omega_gpu_mathlibs_test_set_mutant(0, 0); 898 for (int kind = 0; kind < 3; kind++) { 899 int rc = omega_gpu_rng_codegen((OmegaGpuRngKind)kind, &a); 900 check(rc == OMEGA_GPU_ML_OK, "rng codegen kind %d rc %d (%s)", kind, rc, omega_gpu_mathlibs_last_error()); 901 rc = omega_gpu_rng_codegen((OmegaGpuRngKind)kind, &b); 902 check(rc == OMEGA_GPU_ML_OK && b.kernel_cache_hit && !memcmp(a.code_digest, b.code_digest, 32), 903 "rng kind %d: second codegen not a cache hit with the same digest", kind); 904 memcpy(base_rng[kind], a.code_digest, 32); 905 receipt("host rng_codegen kind=%d insns=%u gpr_peak=%u gpr_declared=%u", kind, a.insn_count, a.gpr_peak, 906 a.gpr_declared); 907 } 908 uint8_t base_fft[12][2][32]; 909 for (int lg = 2; lg <= 11; lg++) 910 for (int inv = 0; inv < 2; inv++) { 911 int rc = omega_gpu_fft_codegen(1u << lg, inv, &a); 912 check(rc == OMEGA_GPU_ML_OK, "fft codegen n=%u inv=%d rc %d (%s)", 1u << lg, inv, rc, 913 omega_gpu_mathlibs_last_error()); 914 check(a.threads_per_cta == (1u << lg) / 4, "fft n=%u threads %u", 1u << lg, a.threads_per_cta); 915 memcpy(base_fft[lg][inv], a.code_digest, 32); 916 receipt("host fft_codegen n=%u inverse=%d insns=%u gpr_peak=%u gpr_declared=%u", 1u << lg, inv, 917 a.insn_count, a.gpr_peak, a.gpr_declared); 918 } 919 for (int lg = 2; lg <= 11; lg++) 920 check(memcmp(base_fft[lg][0], base_fft[lg][1], 32) != 0, "fft n=%u forward and inverse share code", 1u << lg); 921 /* mutants change the code they claim to change, and only that */ 922 for (int lvl = 1; lvl <= 2; lvl++) { 923 omega_gpu_mathlibs_test_set_mutant(1, lvl); 924 for (int kind = 0; kind < 3; kind++) { 925 check(omega_gpu_rng_codegen((OmegaGpuRngKind)kind, &a) == OMEGA_GPU_ML_OK, "rng mutant %d kind %d", lvl, kind); 926 int differs = memcmp(a.code_digest, base_rng[kind], 32) != 0; 927 int want = lvl == 1 || kind == OMEGA_GPU_RNG_NORMAL; 928 check(differs == want, "rng mutant %d kind %d: digest %s", lvl, kind, differs ? "differs" : "unchanged"); 929 } 930 omega_gpu_mathlibs_test_set_mutant(2, lvl); 931 for (int lg = 2; lg <= 11; lg++) { 932 check(omega_gpu_fft_codegen(1u << lg, 0, &a) == OMEGA_GPU_ML_OK, "fft mutant %d n=%u", lvl, 1u << lg); 933 int differs = memcmp(a.code_digest, base_fft[lg][0], 32) != 0; 934 int want = lvl == 1 || lg >= 3; 935 check(differs == want, "fft mutant %d n=%u: digest %s", lvl, 1u << lg, differs ? "differs" : "unchanged"); 936 } 937 } 938 /* Philox: one kernel; its two mutants change its digest */ 939 uint8_t base_ph[32]; 940 int rc = omega_gpu_philox_codegen(&a); 941 check(rc == OMEGA_GPU_ML_OK, "philox codegen rc %d (%s)", rc, omega_gpu_mathlibs_last_error()); 942 rc = omega_gpu_philox_codegen(&b); 943 check(rc == OMEGA_GPU_ML_OK && b.kernel_cache_hit && !memcmp(a.code_digest, b.code_digest, 32), 944 "philox: second codegen not a cache hit with the same digest"); 945 check(a.threads_per_cta == OMEGA_GPU_PHILOX_THREADS, "philox threads %u", a.threads_per_cta); 946 memcpy(base_ph, a.code_digest, 32); 947 receipt("host philox_codegen insns=%u gpr_peak=%u gpr_declared=%u", a.insn_count, a.gpr_peak, a.gpr_declared); 948 for (int lvl = 1; lvl <= 2; lvl++) { 949 omega_gpu_mathlibs_test_set_mutant(3, lvl); 950 check(omega_gpu_philox_codegen(&a) == OMEGA_GPU_ML_OK, "philox mutant %d codegen", lvl); 951 check(memcmp(a.code_digest, base_ph, 32) != 0, "philox mutant %d: digest unchanged", lvl); 952 } 953 omega_gpu_mathlibs_test_set_mutant(0, 0); 954 /* the distribution kernels (a = dist, b = offset % 4): each builds, lists, and differs under exactly the mutants 955 * that reach it (1, 2 all; 3 the uniform of dists 1 and 4; 4 b > 0; 5 dist 4; 6 dist 3) */ 956 { 957 BlackwellIRProgram *pp = calloc(1, sizeof *pp); 958 uint8_t *pc0 = malloc(OMEGA_BW_CODE_CAP_MAX), *pc = malloc(OMEGA_BW_CODE_CAP_MAX); 959 OmegaBlackwellKernel pk0, pk; 960 size_t wrong = 0, built = 0; 961 for (uint64_t d = 0; d <= 4; d++) 962 for (uint64_t bb = 0; bb < 4; bb++) { 963 rc = build(3, d, bb, 0, OMEGA_GPU_PHILOX_THREADS, pp, pc0, &pk0); 964 check(rc == OMEGA_OM_IR_OK, "philox build dist %" PRIu64 " b %" PRIu64 ": %s", d, bb, 965 omega_gpu_om_ir_rc_name(rc)); 966 if (rc != OMEGA_OM_IR_OK) continue; 967 built++; 968 char *ls = listing_of(&pk0); 969 check(ls != NULL, "philox dist %" PRIu64 " b %" PRIu64 ": listing has unknown words", d, bb); 970 free(ls); 971 receipt("host philox_dist_codegen dist=%" PRIu64 " b=%" PRIu64 " code_bytes=%zu", d, bb, pk0.code_size); 972 for (uint64_t lvl = 1; lvl <= 6; lvl++) { 973 rc = build(3, d, bb, lvl, OMEGA_GPU_PHILOX_THREADS, pp, pc, &pk); 974 int differs = rc == OMEGA_OM_IR_OK && 975 (pk.code_size != pk0.code_size || memcmp(pc, pc0, pk0.code_size) != 0); 976 int want = lvl <= 2 || (lvl == 3 && (d == 1 || d == 4)) || (lvl == 4 && bb > 0) || 977 (lvl == 5 && d == 4) || (lvl == 6 && d == 3); 978 if (differs != want) { 979 wrong++; 980 printf("philox dist %" PRIu64 " b %" PRIu64 " mutant %" PRIu64 ": rc %s, code %s\n", d, bb, lvl, 981 omega_gpu_om_ir_rc_name(rc), differs ? "differs" : "unchanged"); 982 } 983 } 984 } 985 check(built == 20 && wrong == 0, "philox dist kernels: %zu of 20 built, %zu mutant targeting errors", built, 986 wrong); 987 receipt("host philox_dist_mutant_targeting kernels=%zu errors=%zu", built, wrong); 988 free(pp); free(pc0); free(pc); 989 } 990 omega_gpu_mathlibs_test_set_mutant(0, 0); 991 /* split / merge and transpose: built through the unit directly; both mutants change the code at every shape */ 992 BlackwellIRProgram *prog = calloc(1, sizeof *prog); 993 uint8_t *code = malloc(OMEGA_BW_CODE_CAP_MAX), *code0 = malloc(OMEGA_BW_CODE_CAP_MAX); 994 OmegaBlackwellKernel k, k0; 995 const struct { uint64_t kind, a, b; uint32_t threads; } mk[] = { 996 { 4, 3, 0, 2 }, { 4, 3, 1, 2 }, { 4, 6, 0, 16 }, { 4, 6, 1, 16 }, { 4, 12, 0, 1024 }, 997 { 4, 12, 1, 1024 }, { 5, 2, 2, 256 }, { 5, 4, 3, 256 }, { 5, 25, 2, 256 }, { 5, 16, 11, 256 }, 998 }; 999 for (size_t i = 0; i < sizeof mk / sizeof mk[0]; i++) { 1000 rc = build(mk[i].kind, mk[i].a, mk[i].b, 0, mk[i].threads, prog, code0, &k0); 1001 check(rc == OMEGA_OM_IR_OK, "build kind %" PRIu64 " a %" PRIu64 " b %" PRIu64 ": %s", mk[i].kind, mk[i].a, mk[i].b, 1002 omega_gpu_om_ir_rc_name(rc)); 1003 if (rc != OMEGA_OM_IR_OK) continue; 1004 receipt("host unit_codegen kind=%" PRIu64 " a=%" PRIu64 " b=%" PRIu64 " threads=%u code_bytes=%zu", mk[i].kind, 1005 mk[i].a, mk[i].b, mk[i].threads, k0.code_size); 1006 for (uint64_t lvl = 1; lvl <= 2; lvl++) { 1007 rc = build(mk[i].kind, mk[i].a, mk[i].b, lvl, mk[i].threads, prog, code, &k); 1008 int differs = rc == OMEGA_OM_IR_OK && (k.code_size != k0.code_size || memcmp(code, code0, k0.code_size) != 0); 1009 check(differs, "kind %" PRIu64 " a %" PRIu64 " b %" PRIu64 " mutant %" PRIu64 ": rc %s, code %s", mk[i].kind, 1010 mk[i].a, mk[i].b, lvl, omega_gpu_om_ir_rc_name(rc), differs ? "differs" : "unchanged"); 1011 } 1012 } 1013 free(code0); 1014 /* our own disassembler finds the instructions the encoder was asked for */ 1015 const struct { uint64_t kind, a, b; uint32_t threads; const char *need[4]; } want[] = { 1016 { 0, 0, 0, 128, { "STG.E", "+0x4]", "IMAD.WIDE", NULL } }, 1017 { 0, 2, 0, 128, { "MUFU.LG2", "MUFU.RSQ", "I2FP.F32.S32", "LOP3.LUT" } }, 1018 { 1, 11, 1, 512, { "BAR.SYNC", "LDS", "STS", "LDG.E.64" } }, 1019 { 3, 0, 0, 128, { "IMAD.WIDE", "LOP3.LUT", "STG.E", NULL } }, 1020 { 4, 12, 0, 1024, { "LDG.E.64", "STG.E", "FMUL", NULL } }, 1021 { 5, 4, 3, 256, { "LDG.E.64", "STG.E", NULL, NULL } }, 1022 }; 1023 for (size_t i = 0; i < sizeof want / sizeof want[0]; i++) { 1024 int rc = build(want[i].kind, want[i].a, want[i].b, 0, want[i].threads, prog, code, &k); 1025 check(rc == OMEGA_OM_IR_OK, "build kind %" PRIu64 " a %" PRIu64 ": %s", want[i].kind, want[i].a, 1026 omega_gpu_om_ir_rc_name(rc)); 1027 if (rc != OMEGA_OM_IR_OK) continue; 1028 char *l = listing_of(&k); 1029 check(l != NULL, "listing of kind %" PRIu64 " a %" PRIu64 " failed", want[i].kind, want[i].a); 1030 for (int j = 0; l && j < 4 && want[i].need[j]; j++) 1031 check(strstr(l, want[i].need[j]) != NULL, "kind %" PRIu64 " a %" PRIu64 ": listing lacks %s", want[i].kind, 1032 want[i].a, want[i].need[j]); 1033 free(l); 1034 } 1035 free(prog); 1036 free(code); 1037 } 1038 1039 static void host_twiddles(void) { 1040 static float tw[2 * 2048]; 1041 size_t bad = 0, total = 0; 1042 for (int lg = 2; lg <= 11; lg++) 1043 for (int inv = 0; inv < 2; inv++) { 1044 uint32_t n = 1u << lg; 1045 check(omega_gpu_fft_twiddles(n, inv, tw) == OMEGA_GPU_ML_OK, "twiddles n=%u", n); 1046 for (uint32_t m = 0; m < n; m++) { 1047 double c = cos(2.0 * M_PI * m / n), s = (inv ? 1.0 : -1.0) * sin(2.0 * M_PI * m / n); 1048 /* within half an f32 ulp of the libm value (plus the libm error) */ 1049 double tc = tw[2 * m], ts = tw[2 * m + 1]; 1050 double uc = fabs(nextafterf(tw[2 * m], INFINITY) - tw[2 * m]); 1051 double us = fabs(nextafterf(tw[2 * m + 1], INFINITY) - tw[2 * m + 1]); 1052 if (fabs(tc - c) > 0.5 * uc + 1e-15 || fabs(ts - s) > 0.5 * us + 1e-15) bad++; 1053 total++; 1054 } 1055 } 1056 /* n = 4096 (the real transforms' merge and split tables) through the unit's data entry */ 1057 static uint64_t words[OMEGA_OM_IR_WORDS]; 1058 for (int inv = 0; inv < 2; inv++) { 1059 check(omega_gpu_om_ir_data(2, 12, (uint64_t)inv, 0, words) == OMEGA_OM_IR_OK, "unit twiddles n=4096"); 1060 for (uint32_t m = 0; m < 4096; m++) { 1061 uint32_t rb = (uint32_t)words[m], ib = (uint32_t)(words[m] >> 32); 1062 float tc, ts; 1063 memcpy(&tc, &rb, 4); 1064 memcpy(&ts, &ib, 4); 1065 double c = cos(2.0 * M_PI * m / 4096), s = (inv ? 1.0 : -1.0) * sin(2.0 * M_PI * m / 4096); 1066 double uc = fabs(nextafterf(tc, INFINITY) - tc), us = fabs(nextafterf(ts, INFINITY) - ts); 1067 if (fabs(tc - c) > 0.5 * uc + 1e-15 || fabs(ts - s) > 0.5 * us + 1e-15) bad++; 1068 total++; 1069 } 1070 } 1071 check(bad == 0, "%zu of %zu twiddles not the correctly rounded f32 of w^m", bad, total); 1072 receipt("host fft_twiddles entries=%zu off_by_more_than_half_ulp=%zu", total, bad); 1073 } 1074 1075 static void host_normal_sweep(void) { 1076 double worst = 0; 1077 uint32_t worst_a = 0; 1078 size_t over = 0, central = 0; 1079 for (uint32_t A = 1; A < 16777216u; A += 2) { 1080 float z = normal_model(A); 1081 double ref = normal_ref(A), e = rel_err(z, ref); 1082 if (!(e <= NORMAL_TOL)) over++; 1083 if (!(e <= worst)) { worst = e; worst_a = A; } 1084 float w = -log2f((float)A * (float)(16777216u - A) * 0x1p-46f) * 0.`69314718f`; 1085 central += w < 5.0f; 1086 } 1087 check(over == 0, "normal f32 model: %zu of 2^23 inputs past %.3g (worst %.4g at A=%u)", over, NORMAL_TOL, worst, 1088 worst_a); 1089 receipt("host normal_model_sweep inputs=`8388608` over_tol=%zu worst_rel=%.6e at_A=%u tol=%.3g central~%zu", over, 1090 worst, worst_a, NORMAL_TOL, central); 1091 /* the f64 reference itself against WolframLanguageEvaluator, 2026-10-27: 1092 * Table[{A, N[Sqrt[2] InverseErf[(2 A - 2^24)/2^24], 20]}, {A, {1, `8388607`, `8388609`, `16777215`, `12345679`, `5000001`}}] */ 1093 const struct { uint32_t A; double z; } wl[] = { 1094 { 1u, -5.`2947040848545980574` }, 1095 { 8388607u, -1.`4940668789333163667e`-7 }, 1096 { 8388609u, 1.`4940668789333163667e`-7 }, 1097 { 16777215u, 5.`2947040848545980574` }, 1098 { 12345679u, 0.`63063321267521096414` }, 1099 { 5000001u, -0.`53009427681848114495` }, 1100 }; 1101 for (size_t i = 0; i < sizeof wl / sizeof wl[0]; i++) { 1102 double r = normal_ref(wl[i].A); 1103 check(fabs(r - wl[i].z) <= 1e-13 * fabs(wl[i].z), "normal_ref(A=%u) = %.17g, Wolfram %.17g", wl[i].A, r, wl[i].z); 1104 } 1105 } 1106 1107 static void host_philox(void) { 1108 size_t kat_bad = 0, ctl_bad = 0; 1109 for (int i = 0; i < 3; i++) { 1110 uint32_t o[4]; 1111 uint64_t key = PH_KAT[i].key[0] | (uint64_t)PH_KAT[i].key[1] << 32; 1112 philox_block(PH_KAT[i].ctr, key, 10, o); 1113 kat_bad += memcmp(o, PH_KAT[i].want, 16) != 0; 1114 philox_block(PH_KAT[i].ctr, key, 9, o); 1115 ctl_bad += memcmp(o, PH_KAT[i].want, 16) != 0; 1116 } 1117 check(kat_bad == 0, "philox reference: %zu of 3 Random123 KAT vectors differ", kat_bad); 1118 check(ctl_bad == 3, "negative control: the 9-round reference matches %zu KAT vectors", 3 - ctl_bad); 1119 receipt("host philox_reference_vs_random123_kat vectors=3 mismatches=%zu nine_round_control_mismatches=%zu", kat_bad, 1120 ctl_bad); 1121 size_t bad = 0; 1122 for (size_t i = 0; i < g_nph; i++) 1123 bad += philox_ref(g_ph[i].seed, g_ph[i].sub, g_ph[i].off, g_ph[i].k) != g_ph[i].val; 1124 check(g_nph == 1024, "cuRAND fixture: %zu rows, want 1024", g_nph); 1125 check(bad == 0, "philox reference: %zu of %zu cuRAND fixture rows differ", bad, g_nph); 1126 receipt("host philox_reference_vs_curand rows=%zu mismatches=%zu", g_nph, bad); 1127 /* the distribution mirror against cuRAND, with three negative controls: the uniform of the next word (one 1128 * position late), the normal with sin and cos swapped, uniform2_double without its 2^-54 half step */ 1129 size_t rows[6] = { 0 }, dbad[6] = { 0 }, exact4 = 0, ctl_u = 0, ctl_n = 0, ctl_h = 0; 1130 double worst = 0; 1131 for (size_t i = 0; i < g_npd; i++) { 1132 const PdRow *r = &g_pd[i]; 1133 rows[r->d]++; 1134 if (r->d == 5) continue; 1135 uint64_t v = ph_dist_ref((int)r->d, r->seed, r->sub, r->off, r->k); 1136 dbad[r->d] += !pd_agrees(r, v); 1137 if (r->d == OMEGA_GPU_PHILOX_UNIFORM) { 1138 ctl_u += f2u(ph_unif(ph_word(r->seed, r->sub, r->off + r->k + 1))) != r->val; 1139 } else if (r->d == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE) { 1140 uint64_t p = r->off + 4 * (r->k / 2) + 2 * (r->k & 1); 1141 uint64_t z = (uint64_t)ph_word(r->seed, r->sub, p) ^ (uint64_t)ph_word(r->seed, r->sub, p + 1) << 21; 1142 ctl_h += ph_d2u((double)z * 0x1p-53) != r->val; 1143 } else if (r->d == OMEGA_GPU_PHILOX_NORMAL) { 1144 exact4 += v == r->val; 1145 double s = ph_normal_scale(ph_word(r->seed, r->sub, r->off + 2 * (r->k / 2))); 1146 double e = fabs((double)u2f((uint32_t)v) - (double)u2f((uint32_t)r->val)); 1147 if (s > 0 && e / s > worst) worst = e / s; 1148 PdRow sw = *r; 1149 sw.k ^= 1u; /* the other half of the pair at the same position: what a sin / cos swap would store */ 1150 ctl_n += !pd_agrees(r, ph_dist_ref(OMEGA_GPU_PHILOX_NORMAL, sw.seed, sw.sub, sw.off, sw.k)); 1151 } 1152 } 1153 check(g_npd == PD_ROWS && rows[1] == 896 && rows[2] == 896 && rows[3] == 896 && rows[4] == 896 && rows[5] == 896, 1154 "cuRAND distribution fixture: %zu rows (%zu %zu %zu %zu %zu), want 5 x 896", g_npd, rows[1], rows[2], rows[3], 1155 rows[4], rows[5]); 1156 for (int d = 1; d <= 4; d++) 1157 check(dbad[d] == 0, "philox dist %d reference: %zu of %zu cuRAND rows disagree", d, dbad[d], rows[d]); 1158 check(ctl_u > rows[1] / 2 && ctl_n > rows[4] / 2 && ctl_h > rows[3] / 4, 1159 "negative controls: next word %zu of %zu, sin/cos swapped %zu of %zu, no 2^-54 %zu of %zu rows caught", ctl_u, 1160 rows[1], ctl_n, rows[4], ctl_h, rows[3]); 1161 receipt("host philox_dist_reference_vs_curand uniform=%zu/%zu uniform_double=%zu/%zu uniform2_double=%zu/%zu " 1162 "normal_within_2^-21s=%zu/%zu normal_bit_exact=%zu worst_err_over_s=%.3f*2^-24 normal_double_rows=%zu " 1163 "(not compared: no f64 IR)", 1164 rows[1] - dbad[1], rows[1], rows[2] - dbad[2], rows[2], rows[3] - dbad[3], rows[3], rows[4] - dbad[4], rows[4], 1165 exact4, worst * 0x1p24, rows[5]); 1166 receipt("host philox_dist_negative_controls uniform_next_word=%zu/%zu normal_sin_cos_swapped=%zu/%zu " 1167 "uniform2_no_half_step=%zu/%zu (must be caught)", ctl_u, rows[1], ctl_n, rows[4], ctl_h, rows[3]); 1168 } 1169 1170 static uint64_t pstep(uint64_t k, uint64_t a, uint64_t b, uint64_t s, uint64_t d, uint64_t r) { 1171 return k | a << 8 | b << 16 | s << 24 | d << 32 | r << 40; 1172 } 1173 typedef struct { OmegaGpuFftLaunch l[64]; uint32_t n; } PlanLog; 1174 static int plan_record(void *ctx, const OmegaGpuFftLaunch *l) { 1175 PlanLog *p = ctx; 1176 if (p->n < 64) p->l[p->n] = *l; 1177 p->n++; 1178 return 0; 1179 } 1180 1181 static void host_plans(void) { 1182 static uint64_t words[OMEGA_OM_IR_WORDS]; 1183 /* the unit's step words: R2C / C2R, 1-D, 2-D {8, 16} forward, 3-D {4, 8, 16} inverse */ 1184 const struct { uint64_t a, b, c; uint32_t n; uint64_t s[6]; } P[] = { 1185 { 1 | 1 << 4, 12, 0, 2, { pstep(1, 11, 0, 0, 2, 0), pstep(4, 12, 0, 2, 1, 0) } }, 1186 { 2 | 1 << 4, 3, 0, 2, { pstep(4, 3, 1, 0, 2, 0), pstep(1, 2, 1, 2, 1, 0) } }, 1187 { 1 << 4, 8, 1, 1, { pstep(1, 8, 1, 0, 1, 0) } }, 1188 { 2 << 4, 3 | 4 << 4, 0, 4, 1189 { pstep(1, 4, 0, 0, 2, 3), pstep(5, 3, 4, 2, 1, 0), pstep(1, 3, 0, 1, 2, 4), pstep(5, 4, 3, 2, 1, 0) } }, 1190 { 3 << 4, 2 | 3 << 4 | 4 << 8, 1, 6, 1191 { pstep(1, 4, 1, 0, 2, 5), pstep(5, 5, 4, 2, 1, 0), pstep(1, 3, 1, 1, 2, 6), pstep(5, 6, 3, 2, 1, 0), 1192 pstep(1, 2, 1, 1, 2, 7), pstep(5, 7, 2, 2, 1, 0) } }, 1193 }; 1194 size_t bad = 0; 1195 for (size_t i = 0; i < sizeof P / sizeof P[0]; i++) { 1196 int rc = omega_gpu_om_ir_data(6, P[i].a, P[i].b, P[i].c, words); 1197 int ok = rc == OMEGA_OM_IR_OK && words[0] == P[i].n; 1198 for (uint32_t s = 0; ok && s < P[i].n; s++) ok = words[1 + s] == P[i].s[s]; 1199 bad += !ok; 1200 check(ok, "unit plan a %#" PRIx64 " b %#" PRIx64 " c %" PRIu64 ": rc %s, %" PRIu64 " steps, step 1 %#" PRIx64, 1201 P[i].a, P[i].b, P[i].c, omega_gpu_om_ir_rc_name(rc), words[0], words[1]); 1202 } 1203 receipt("host fft_plan_words plans=%zu mismatches=%zu", sizeof P / sizeof P[0], bad); 1204 /* the launches: R2C n 4096 batch 2500 (three chunks per pass) */ 1205 static PlanLog L; 1206 size_t by[3]; 1207 uint32_t n = 4096; 1208 memset(&L, 0, sizeof L); 1209 int rc = omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_R2C, 1, &n, 2500, 0, by, plan_record, &L); 1210 check(rc == OMEGA_GPU_ML_OK && L.n == 6, "plan r2c 4096 x 2500: rc %d, %u launches", rc, L.n); 1211 check(by[0] == (size_t)4096 * 4 * 2500 && by[1] == (size_t)2049 * 8 * 2500 && by[2] == by[0], 1212 "plan r2c 4096 x 2500 bytes %zu %zu %zu", by[0], by[1], by[2]); 1213 if (L.n == 6) { 1214 const struct { uint32_t kind, grid, threads, shared, tw_n, src, dst; uint64_t src_off, dst_off; size_t el; } W[6] = { 1215 { 1, 1024, 512, 16384, 2048, 0, 2, 0, 0, (size_t)2 * 2048 * 1024 }, 1216 { 1, 1024, 512, 16384, 2048, 0, 2, (uint64_t)1024 * 2048 * 8, (uint64_t)1024 * 2048 * 8, (size_t)2 * 2048 * 1024 }, 1217 { 1, 452, 512, 16384, 2048, 0, 2, (uint64_t)2048 * 2048 * 8, (uint64_t)2048 * 2048 * 8, (size_t)2 * 2048 * 452 }, 1218 { 4, 1024, 1024, 0, 4096, 2, 1, 0, 0, (size_t)2 * 2049 * 1024 }, 1219 { 4, 1024, 1024, 0, 4096, 2, 1, (uint64_t)1024 * 2048 * 8, (uint64_t)1024 * 2049 * 8, (size_t)2 * 2049 * 1024 }, 1220 { 4, 452, 1024, 0, 4096, 2, 1, (uint64_t)2048 * 2048 * 8, (uint64_t)2048 * 2049 * 8, (size_t)2 * 2049 * 452 }, 1221 }; 1222 for (int i = 0; i < 6; i++) { 1223 const OmegaGpuFftLaunch *l = &L.l[i]; 1224 check(l->kind == W[i].kind && l->grid == W[i].grid && l->threads == W[i].threads && 1225 l->shared_bytes == W[i].shared && l->tw_n == W[i].tw_n && l->tw != NULL && l->src == W[i].src && 1226 l->dst == W[i].dst && l->src_off == W[i].src_off && l->dst_off == W[i].dst_off && 1227 l->elements == W[i].el && l->tw_inverse == 0, 1228 "plan r2c launch %d: kind %u grid %u threads %u shared %u tw %u src %u+%" PRIu64 " dst %u+%" PRIu64 1229 " elements %zu", 1230 i, l->kind, l->grid, l->threads, l->shared_bytes, l->tw_n, l->src, l->src_off, l->dst, l->dst_off, 1231 l->elements); 1232 } 1233 } 1234 /* 2-D {4, 2048} batch 4: FFT 2048 over 16 rows, transpose, FFT 4 over 8192 rows (8 chunks), transpose */ 1235 const uint32_t d[2] = { 4, 2048 }; 1236 memset(&L, 0, sizeof L); 1237 rc = omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_C2C_ND, 2, d, 4, 0, by, plan_record, &L); 1238 check(rc == OMEGA_GPU_ML_OK && L.n == 11, "plan 2-D {4, 2048} x 4: rc %d, %u launches", rc, L.n); 1239 check(by[0] == (size_t)8 * 8192 * 4 && by[1] == by[0] && by[2] == by[0], "plan 2-D bytes %zu %zu %zu", by[0], by[1], 1240 by[2]); 1241 if (L.n == 11) { 1242 int ok = L.l[0].kind == 1 && L.l[0].a == 11 && L.l[0].grid == 16 && L.l[0].src == 0 && L.l[0].dst == 2; 1243 ok &= L.l[1].kind == 5 && L.l[1].a == 2 && L.l[1].b == 11 && L.l[1].total == 32768 && L.l[1].first == 0 && 1244 L.l[1].grid == 128 && L.l[1].threads == 256 && L.l[1].src == 2 && L.l[1].dst == 1; 1245 for (int i = 0; i < 8; i++) 1246 ok &= L.l[2 + i].kind == 1 && L.l[2 + i].a == 2 && L.l[2 + i].grid == 1024 && L.l[2 + i].threads == 1 && 1247 L.l[2 + i].src == 1 && L.l[2 + i].dst == 2 && L.l[2 + i].src_off == (uint64_t)i * 1024 * 4 * 8; 1248 ok &= L.l[10].kind == 5 && L.l[10].a == 11 && L.l[10].b == 2 && L.l[10].src == 2 && L.l[10].dst == 1; 1249 check(ok, "plan 2-D {4, 2048} x 4: launch sequence differs"); 1250 } 1251 /* a transpose past one launch: {16, 2048} batch 16 = 2^19 elements, 2048 CTAs, two launches per transpose */ 1252 const uint32_t d3[2] = { 16, 2048 }; 1253 memset(&L, 0, sizeof L); 1254 rc = omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_C2C_ND, 2, d3, 16, 1, by, plan_record, &L); 1255 uint32_t tr = 0, tr_ok = 0; 1256 for (uint32_t i = 0; i < L.n && i < 64; i++) 1257 if (L.l[i].kind == 5) { 1258 tr_ok += L.l[i].grid == 1024 && L.l[i].total == (1u << 19) && L.l[i].first == (tr & 1) * 262144u; 1259 tr++; 1260 } 1261 check(rc == OMEGA_GPU_ML_OK && tr == 4 && tr_ok == 4, "plan 2-D {16, 2048} x 16: rc %d, %u transposes, %u as expected", 1262 rc, tr, tr_ok); 1263 receipt("host fft_plan_launches r2c_4096x2500=%u nd_4x2048x4=%u nd_16x2048x16_transposes=%u", 6u, 11u, tr); 1264 } 1265 1266 /* A walk callback that calls back into the API (it takes the session lock). Before the walks released the lock 1267 * around their callbacks this deadlocked; the child below gets 20 s and SIGALRM ends a hung walk. */ 1268 static int reenter_cb(void *ctx, const OmegaGpuFftLaunch *l) { 1269 (void)l; 1270 float tw[16]; 1271 int *calls = ctx; 1272 if (omega_gpu_fft_twiddles(8, 0, tw) != OMEGA_GPU_ML_OK) return 99; 1273 (*calls)++; 1274 return 0; 1275 } 1276 static int reenter_many_cb(void *ctx, const OmegaGpuFftManyLaunch *l) { 1277 (void)l; 1278 float tw[16]; 1279 int *calls = ctx; 1280 if (omega_gpu_fft_twiddles(8, 0, tw) != OMEGA_GPU_ML_OK) return 99; 1281 (*calls)++; 1282 return 0; 1283 } 1284 static void host_reentry(void) { 1285 for (int which = 0; which < 2; which++) { 1286 fflush(stdout); 1287 pid_t pid = fork(); 1288 if (pid == 0) { 1289 alarm(20); 1290 int calls = 0, rc; 1291 if (which == 0) { 1292 size_t bytes[3]; 1293 uint32_t n = 8; 1294 rc = omega_gpu_fft_plan_walk(OMEGA_GPU_FFT_R2C, 1, &n, 1, 0, bytes, reenter_cb, &calls); 1295 } else { 1296 size_t bytes[8]; 1297 OmegaGpuFftMany p = { .rank = 1, .n = { 12 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2C }; 1298 rc = omega_gpu_fft_many_walk(&p, 0, bytes, reenter_many_cb, &calls); 1299 } 1300 _exit(rc == 0 && calls > 0 ? 0 : 3); 1301 } 1302 int st = 0; 1303 check(pid > 0 && waitpid(pid, &st, 0) == pid, "reentry %d: fork/wait", which); 1304 check(WIFEXITED(st) && WEXITSTATUS(st) == 0, "reentry %d: walk callback calling the API %s (status %#x)", which, 1305 WIFSIGNALED(st) && WTERMSIG(st) == SIGALRM ? "deadlocked" : "failed", st); 1306 } 1307 } 1308 1309 /* kind 12 of every length against the rule in the header: radices 4, at most one 2, then 3, 5, 7 when they divide n, 1310 * else Bluestein over the least power of two M >= 2n - 1 */ 1311 static void host_many_plans(void) { 1312 static uint64_t w[OMEGA_OM_IR_WORDS]; 1313 size_t bad = 0, blue = 0; 1314 for (uint32_t n = 2; n <= OMEGA_GPU_FFT_MANY_MAX_N; n++) { 1315 if (omega_gpu_om_ir_data(12, n, 0, 0, w) != OMEGA_OM_IR_OK) { bad++; continue; } 1316 uint32_t rest = n; 1317 while (rest % 2 == 0) rest /= 2; 1318 while (rest % 3 == 0) rest /= 3; 1319 while (rest % 5 == 0) rest /= 5; 1320 while (rest % 7 == 0) rest /= 7; 1321 uint32_t M = n, flag = 0; 1322 if (rest != 1) { for (M = 1; M < 2 * n - 1; M *= 2) {} flag = 1; blue++; } 1323 uint32_t want[32] = { 0 }, np = 0, x = M; 1324 while (x % 4 == 0) { want[np++] = 4; x /= 4; } 1325 if (x % 2 == 0) { want[np++] = 2; x /= 2; } 1326 for (uint32_t rd = 3; rd <= 7; rd += 2) 1327 while (x % rd == 0) { want[np++] = rd; x /= rd; } 1328 int ok = x == 1 && w[0] == M && w[1] == flag && w[2] == np; 1329 for (uint32_t i = 0; ok && i <= np; i++) ok = w[3 + i] == want[i]; 1330 if (!ok) bad++; 1331 } 1332 check(bad == 0, "%zu of 4095 kind 12 factor plans wrong", bad); 1333 static const uint32_t refused[] = { 0, 1, 4097, 8192 }; 1334 for (size_t i = 0; i < sizeof refused / sizeof refused[0]; i++) 1335 check(omega_gpu_om_ir_data(12, refused[i], 0, 0, w) == OMEGA_OM_IR_REFUSED, "kind 12 n=%u refused", refused[i]); 1336 receipt("host many_plans lengths=4095 bluestein=%zu wrong=%zu", blue, bad); 1337 } 1338 1339 /* kind 13 and the unit's division (om/mathlibs mdiv): q0 = hi32(x * mg), t = x - (q0 + 1) d, q = q0 + (t >= 2^31 ? 1340 * 0 : 1) gives floor(x / d) for every x < 2^32 and d in 1..2^31 - 1 (mg = min(floor(2^32 / d), 2^32 - 1)); swept 1341 * over every d the plans use with x < 2^16, the multiples of d near 2^28 and 2^32, and random x */ 1342 static uint32_t mdiv_model(uint32_t x, uint32_t d, uint32_t mg) { 1343 uint32_t q0 = (uint32_t)(((uint64_t)x * mg) >> 32), t = x - (q0 + 1u) * d; 1344 return q0 + ((~t) >> 31); 1345 } 1346 static void host_many_magic(void) { 1347 static uint64_t w[OMEGA_OM_IR_WORDS]; 1348 size_t bad = 0, tried = 0; 1349 Xo rng; 1350 xo_seed(&rng, 0x6D646976ull); 1351 static const uint32_t big[] = { 4097, 65535, 65536, 1u << 20, 268435455u, 268435456u, 2147483647u }; 1352 for (uint32_t i = 0; i < 8192u + sizeof big / sizeof big[0]; i++) { 1353 uint32_t d = i < 8192 ? i + 1 : big[i - 8192]; 1354 if (omega_gpu_om_ir_data(13, d, 0, 0, w) != OMEGA_OM_IR_OK) { bad++; continue; } 1355 uint64_t want = d == 1 ? 0xffffffffull : (1ull << 32) / d; 1356 if (w[0] != want) { bad++; continue; } 1357 uint32_t mg = (uint32_t)w[0]; 1358 for (uint32_t x = 0; x < (i < 4096 ? 65536u : 4096u); x++, tried++) bad += mdiv_model(x, d, mg) != x / d; 1359 for (int k = -2; k <= 2; k++) 1360 for (int e = 0; e < 2; e++) { 1361 uint64_t base = e ? 0xffffffffull : (1ull << 28); 1362 uint64_t q = base / d; 1363 for (int64_t j = -1; j <= 1; j++) { 1364 int64_t x = (int64_t)((q + (uint64_t)(k + 2)) * d) - 2 * (int64_t)d + j; 1365 if (x < 0 || x > 0xffffffffll) continue; 1366 tried++; 1367 bad += mdiv_model((uint32_t)x, d, mg) != (uint32_t)x / d; 1368 } 1369 } 1370 for (int k = 0; k < 64; k++, tried++) { 1371 uint32_t x = (uint32_t)xo_next(&rng); 1372 bad += mdiv_model(x, d, mg) != x / d; 1373 } 1374 tried++; 1375 bad += mdiv_model(0xffffffffu, d, mg) != 0xffffffffu / d; 1376 } 1377 check(bad == 0, "%zu of %zu magic divisions wrong", bad, tried); 1378 check(omega_gpu_om_ir_data(13, 0, 0, 0, w) == OMEGA_OM_IR_REFUSED, "kind 13 d=0 refused"); 1379 check(omega_gpu_om_ir_data(13, 2147483648ull, 0, 0, w) == OMEGA_OM_IR_REFUSED, "kind 13 d=2^31 refused"); 1380 receipt("host many_magic divisors=%u divisions=%zu wrong=%zu", 8192u + (unsigned)(sizeof big / sizeof big[0]), tried, 1381 bad); 1382 } 1383 1384 /* kinds 10 (w^k of n points, n 2..8192) and 11 (chirp exp(-+ i pi k^2 / n), n 2..4096): each part within half an 1385 * f32 ulp of the libm value. The reference reduces the angle to its quadrant in integers first: libm cos of the 1386 * rounded double 3 pi / 2 is -1.07e-15, not the exact 0 the unit returns. */ 1387 static void ref_w(uint64_t k, uint64_t n, uint32_t inv, double *c, double *s) { 1388 uint64_t q = (4 * k) / n, r = 4 * k - q * n; 1389 double th = (M_PI / 2) * (double)r / (double)n, c0 = cos(th), s0 = sin(th); 1390 double cc[4] = { c0, -s0, -c0, s0 }, ss[4] = { s0, c0, -s0, -c0 }; 1391 *c = cc[q & 3]; 1392 *s = (inv ? 1.0 : -1.0) * ss[q & 3]; 1393 } 1394 static int tw_off(uint64_t word, double c, double s) { 1395 uint32_t rb = (uint32_t)word, ib = (uint32_t)(word >> 32); 1396 float tc, ts; 1397 memcpy(&tc, &rb, 4); 1398 memcpy(&ts, &ib, 4); 1399 double uc = fabs(nextafterf(tc, INFINITY) - tc), us = fabs(nextafterf(ts, INFINITY) - ts); 1400 return fabs(tc - c) > 0.5 * uc + 1e-15 || fabs(ts - s) > 0.5 * us + 1e-15; 1401 } 1402 static void host_many_tables(void) { 1403 static uint64_t w[OMEGA_OM_IR_WORDS]; 1404 size_t bad = 0, total = 0, cbad = 0, ctotal = 0; 1405 for (uint32_t n = 2; n <= 8192; n++) 1406 for (uint32_t inv = 0; inv < 2; inv++) { 1407 if (omega_gpu_om_ir_data(10, n, inv, 0, w) != OMEGA_OM_IR_OK) { bad++; continue; } 1408 double c, s; 1409 for (uint32_t k = 0; k < n; k++, total++) { 1410 ref_w(k, n, inv, &c, &s); 1411 bad += tw_off(w[k], c, s); 1412 } 1413 if (n > 4096) continue; 1414 if (omega_gpu_om_ir_data(11, n, inv, 0, w) != OMEGA_OM_IR_OK) { cbad++; continue; } 1415 for (uint32_t k = 0; k < n; k++, ctotal++) { 1416 ref_w(((uint64_t)k * k) % (2ull * n), 2ull * n, inv, &c, &s); 1417 cbad += tw_off(w[k], c, s); 1418 } 1419 } 1420 check(bad == 0, "%zu of %zu any-length twiddles not the correctly rounded f32", bad, total); 1421 check(cbad == 0, "%zu of %zu chirp entries not the correctly rounded f32", cbad, ctotal); 1422 static const uint32_t r10[][2] = { { 1, 0 }, { 8193, 0 }, { 8, 2 } }, r11[][2] = { { 1, 0 }, { 4097, 0 }, { 8, 2 } }; 1423 for (int i = 0; i < 3; i++) { 1424 check(omega_gpu_om_ir_data(10, r10[i][0], r10[i][1], 0, w) == OMEGA_OM_IR_REFUSED, "kind 10 %u/%u refused", 1425 r10[i][0], r10[i][1]); 1426 check(omega_gpu_om_ir_data(11, r11[i][0], r11[i][1], 0, w) == OMEGA_OM_IR_REFUSED, "kind 11 %u/%u refused", 1427 r11[i][0], r11[i][1]); 1428 } 1429 check(omega_gpu_om_ir_data(10, 8, 0, 1, w) == OMEGA_OM_IR_REFUSED, "kind 10 c=1 refused"); 1430 receipt("host many_tables twiddles=%zu chirps=%zu off_by_more_than_half_ulp=%zu", total, ctotal, bad + cbad); 1431 } 1432 1433 /* the walk: refusals, buffer sizes against the layouts recomputed here, and launch shapes */ 1434 typedef struct { size_t launches, words; uint32_t bad, kinds; } WalkStat; 1435 static int many_stat_cb(void *ctx, const OmegaGpuFftManyLaunch *l) { 1436 WalkStat *s = ctx; 1437 s->launches++; 1438 s->kinds |= 1u << l->kind; 1439 uint32_t np = l->kind == 9 ? 2 : 3, nw = l->kind == 7 ? 8 : l->kind == 8 ? 3 : 18; 1440 if (l->kind < 7 || l->kind > 9 || l->nptr != np || l->nwords != nw || l->threads != OMEGA_GPU_FFT_MANY_THREADS || 1441 l->grid == 0 || l->grid > OMEGA_GPU_FFT_MAX_CTAS || l->table == NULL || 1442 (uint64_t)l->grid * l->threads < 1 || l->words[1] % l->threads != 0 || 1443 (uint64_t)l->words[1] + (uint64_t)l->grid * l->threads < l->words[0] || 1444 (uint64_t)l->words[1] >= l->words[0]) 1445 s->bad++; 1446 for (uint32_t i = 0; i < l->nptr; i++) 1447 if (l->buf[i] >= OMEGA_GPU_FFT_MANY_BUFS) s->bad++; 1448 return 0; 1449 } 1450 static void host_many_walk(void) { 1451 typedef struct { OmegaGpuFftMany p; int in_place, rc; const char *what; } Ref; 1452 const int BAD = OMEGA_GPU_ML_BAD_ARGS, BIG = OMEGA_GPU_ML_TOO_LARGE; 1453 static const Ref refs[] = { 1454 { { .rank = 0, .n = { 8 }, .batch = 1 }, 0, -1, "rank 0" }, 1455 { { .rank = 4, .n = { 8, 8, 8 }, .batch = 1 }, 0, -1, "rank 4" }, 1456 { { .rank = 1, .n = { 1 }, .batch = 1 }, 0, -1, "n 1" }, 1457 { { .rank = 1, .n = { 4097 }, .batch = 1 }, 0, -1, "n 4097" }, 1458 { { .rank = 2, .n = { 8, 0 }, .batch = 1 }, 0, -1, "n 0" }, 1459 { { .rank = 1, .n = { 8 }, .batch = 0 }, 0, -1, "batch 0" }, 1460 { { .rank = 1, .n = { 8 }, .batch = 1, .type = (OmegaGpuFftType)3 }, 0, -1, "type 3" }, 1461 { { .rank = 1, .n = { 8 }, .batch = 1, .inverse = 2 }, 0, -1, "C2C inverse 2" }, 1462 { { .rank = 1, .n = { 8 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_R2C, .inverse = 1 }, 0, -1, "R2C inverse 1" }, 1463 { { .rank = 1, .n = { 8 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2R, .inverse = 1 }, 0, -1, "C2R inverse 1" }, 1464 { { .rank = 1, .n = { 8 }, .inembed = { 8 }, .istride = 1, .idist = 8, .batch = 1 }, 0, -1, "inembed only" }, 1465 { { .rank = 2, .n = { 4, 8 }, .inembed = { 4, 7 }, .istride = 1, .idist = 32, .onembed = { 4, 8 }, 1466 .ostride = 1, .odist = 32, .batch = 1 }, 0, -1, "inembed[1] < n[1]" }, 1467 { { .rank = 1, .n = { 8 }, .inembed = { 8 }, .istride = 0, .idist = 8, .onembed = { 8 }, .ostride = 1, 1468 .odist = 8, .batch = 1 }, 0, -1, "istride 0" }, 1469 { { .rank = 1, .n = { 8 }, .inembed = { 8 }, .istride = 1, .idist = 8, .onembed = { 8 }, .ostride = 1, 1470 .odist = 4, .batch = 2 }, 0, -1, "output batches overlap" }, 1471 { { .rank = 2, .n = { 4, 8 }, .inembed = { 4, 8 }, .istride = 1, .idist = 32, .onembed = { 4, 8 }, 1472 .ostride = 0, .odist = 32, .batch = 1 }, 0, -1, "ostride 0" }, 1473 { { .rank = 1, .n = { 8 }, .inembed = { 8 }, .istride = 1, .idist = 0x80000000u, .onembed = { 8 }, 1474 .ostride = 1, .odist = 8, .batch = 3 }, 0, -2, "input index >= 2^32" }, 1475 { { .rank = 1, .n = { 8 }, .inembed = { 8 }, .istride = 1, .idist = 0x10000000u, .onembed = { 8 }, 1476 .ostride = 1, .odist = 8, .batch = 2 }, 0, -2, "input extent > 2^28" }, 1477 { { .rank = 1, .n = { 4096 }, .batch = 32769 }, 0, -2, "batch * n > 2^27" }, 1478 }; 1479 size_t bytes[OMEGA_GPU_FFT_MANY_BUFS]; 1480 for (size_t i = 0; i < sizeof refs / sizeof refs[0]; i++) { 1481 int want = refs[i].rc == -1 ? BAD : BIG; 1482 int rc = omega_gpu_fft_many_walk(&refs[i].p, refs[i].in_place, bytes, NULL, NULL); 1483 check(rc == want, "many walk %s: rc %d, want %d", refs[i].what, rc, want); 1484 } 1485 OmegaGpuFftMany ok = { .rank = 1, .n = { 12 }, .batch = 1 }; 1486 check(omega_gpu_fft_many_walk(NULL, 0, bytes, NULL, NULL) == BAD, "many walk NULL plan"); 1487 check(omega_gpu_fft_many_walk(&ok, 0, NULL, NULL, NULL) == BAD, "many walk NULL bytes"); 1488 /* omega_gpu_fft_many refuses partial overlap and NULL before it opens a device */ 1489 static float fb[64]; 1490 OmegaGpuMathInfo info; 1491 check(omega_gpu_fft_many(&ok, fb, fb + 2, &info) == BAD, "many partial overlap"); 1492 check(omega_gpu_fft_many(&ok, NULL, fb, &info) == BAD, "many NULL in"); 1493 check(omega_gpu_fft_many(&ok, fb, NULL, &info) == BAD, "many NULL out"); 1494 OmegaGpuFftMany bad1 = ok; 1495 bad1.n[0] = 1; 1496 check(omega_gpu_fft_many(&bad1, fb, fb + 32, &info) == BAD, "many n 1"); 1497 /* sizes: every user buffer covers the side's span, in place the larger one; launches well formed */ 1498 static const OmegaGpuFftMany shapes[] = { 1499 { .rank = 1, .n = { 12 }, .batch = 3, .type = OMEGA_GPU_FFT_TYPE_R2C }, 1500 { .rank = 1, .n = { 97 }, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_C2R }, 1501 { .rank = 3, .n = { 3, 4, 5 }, .batch = 2 }, 1502 { .rank = 2, .n = { 6, 10 }, .inembed = { 6, 13 }, .istride = 2, .idist = 161, .onembed = { 7, 11 }, 1503 .ostride = 1, .odist = 77, .batch = 2 }, 1504 { .rank = 1, .n = { 4095 }, .batch = 1 }, 1505 }; 1506 size_t worst_short = 0; 1507 WalkStat st = { 0, 0, 0, 0 }; 1508 for (size_t i = 0; i < sizeof shapes / sizeof shapes[0]; i++) 1509 for (int ip = 0; ip < 2; ip++) { 1510 int rc = omega_gpu_fft_many_walk(&shapes[i], ip, bytes, many_stat_cb, &st); 1511 check(rc == OMEGA_GPU_ML_OK, "many walk shape %zu in_place %d: rc %d", i, ip, rc); 1512 if (rc != OMEGA_GPU_ML_OK) continue; 1513 TLay li, lo; 1514 tlay(&shapes[i], ip, 0, &li); 1515 tlay(&shapes[i], ip, 1, &lo); 1516 size_t si = 4 * tlay_span(&li, shapes[i].batch), so = 4 * tlay_span(&lo, shapes[i].batch); 1517 size_t need_in = ip ? (si > so ? si : so) : si, need_out = ip ? 0 : so; 1518 if (bytes[OMEGA_GPU_FFT_BUF_IN] < need_in) worst_short++; 1519 check(ip ? bytes[OMEGA_GPU_FFT_BUF_OUT] == 0 : bytes[OMEGA_GPU_FFT_BUF_OUT] >= need_out, 1520 "many walk shape %zu in_place %d: out bytes %zu", i, ip, bytes[OMEGA_GPU_FFT_BUF_OUT]); 1521 check(bytes[OMEGA_GPU_FFT_BUF_S0] > 0 && bytes[OMEGA_GPU_FFT_BUF_TABLE] > 0, 1522 "many walk shape %zu: scratch and table sized", i); 1523 } 1524 check(worst_short == 0, "%zu many in buffers smaller than the layout span", worst_short); 1525 check(st.bad == 0 && st.launches > 0 && st.kinds == (1u << 7 | 1u << 8 | 1u << 9), 1526 "many launches well formed (bad %u, launches %zu, kinds %#x)", st.bad, st.launches, st.kinds); 1527 receipt("host many_walk refusals=%zu shapes=%zu launches=%zu malformed=%u", sizeof refs / sizeof refs[0], 1528 2 * (sizeof shapes / sizeof shapes[0]), st.launches, st.bad); 1529 } 1530 1531 static void host_many(void) { 1532 host_many_plans(); 1533 host_many_magic(); 1534 host_many_tables(); 1535 host_many_walk(); 1536 } 1537 1538 static void host_tier(void) { 1539 host_fixture(); 1540 host_philox(); 1541 host_refusals(); 1542 host_codegen(); 1543 host_twiddles(); 1544 host_plans(); 1545 host_normal_sweep(); 1546 host_reentry(); 1547 host_many(); 1548 } 1549 1550 /* ------------------------------------------------------------------ simulator tier */ 1551 1552 static BlackwellIRProgram *g_prog; 1553 static uint8_t *g_code; 1554 static WsSim g_sim; 1555 1556 static int sim_prog(uint64_t kind, uint64_t a, uint64_t b, uint64_t mut, uint32_t threads) { 1557 OmegaBlackwellKernel k; 1558 int rc = build(kind, a, b, mut, threads, g_prog, g_code, &k); 1559 check(rc == OMEGA_OM_IR_OK, "sim build kind %" PRIu64 " a %" PRIu64 " b %" PRIu64 " mut %" PRIu64 ": %s", kind, a, b, 1560 mut, omega_gpu_om_ir_rc_name(rc)); 1561 return rc == OMEGA_OM_IR_OK ? 0 : -1; 1562 } 1563 1564 static void put64(uint32_t *w, uint64_t v) { w[0] = (uint32_t)v; w[1] = (uint32_t)(v >> 32); } 1565 1566 /* the API's argument block (src/omega_gpu_mathlibs_api.c) against host memory */ 1567 static int sim_rng(int kind, int mut, uint64_t seed, uint64_t stream0, uint32_t ns, uint32_t offset, uint32_t per, 1568 void *out) { 1569 if (sim_prog(0, (uint64_t)kind, 0, (uint64_t)mut, OMEGA_GPU_RNG_THREADS) != 0) return -1; 1570 size_t bytes = (size_t)ns * per * (kind == 0 ? 8u : 4u); 1571 memset(out, 0xa5, bytes); 1572 memset(&g_sim, 0, sizeof g_sim); 1573 g_sim.p = g_prog; 1574 g_sim.threads = OMEGA_GPU_RNG_THREADS; 1575 uint32_t *cb = &g_sim.cbank[0x380 / 4]; 1576 put64(&cb[0], (uint64_t)(uintptr_t)out); 1577 uint64_t x = seed + stream0; 1578 for (int i = 0; i < 4; i++) { x += GOLDEN; put64(&cb[2 + 2 * i], x); } 1579 cb[10] = offset; cb[11] = per; cb[12] = ns; 1580 g_sim.ranges[0] = (WsRange){ (uintptr_t)out, (uintptr_t)out + bytes }; 1581 g_sim.nranges = 1; 1582 int rc = ws_run(&g_sim, (ns + OMEGA_GPU_RNG_THREADS - 1) / OMEGA_GPU_RNG_THREADS, 1); 1583 check(rc == 0, "sim rng kind %d: %s", kind, g_sim.msg); 1584 return rc; 1585 } 1586 1587 static int sim_fft(uint32_t n, int inverse, int mut, uint32_t sched, const float *in, float *out, uint32_t batch) { 1588 int lg = lg2u(n); 1589 if (sim_prog(1, (uint64_t)lg, (uint64_t)inverse, (uint64_t)mut, n / 4) != 0) return -1; 1590 static float tw[2 * 2048]; 1591 static uint8_t smem[8 * 2048]; 1592 if (omega_gpu_fft_twiddles(n, inverse, tw) != OMEGA_GPU_ML_OK) return -1; 1593 memset(out, 0xa5, (size_t)8 * n * batch); 1594 memset(&g_sim, 0, sizeof g_sim); 1595 g_sim.p = g_prog; 1596 g_sim.threads = n / 4; 1597 g_sim.smem = smem; 1598 g_sim.smem_bytes = 8 * n; 1599 g_sim.warp_sched = sched; 1600 uint32_t *cb = &g_sim.cbank[0x380 / 4]; 1601 put64(&cb[0], (uint64_t)(uintptr_t)in); 1602 put64(&cb[2], (uint64_t)(uintptr_t)out); 1603 put64(&cb[4], (uint64_t)(uintptr_t)tw); 1604 g_sim.ranges[0] = (WsRange){ (uintptr_t)in, (uintptr_t)in + (size_t)8 * n * batch }; 1605 g_sim.ranges[1] = (WsRange){ (uintptr_t)out, (uintptr_t)out + (size_t)8 * n * batch }; 1606 g_sim.ranges[2] = (WsRange){ (uintptr_t)tw, (uintptr_t)tw + (size_t)8 * n }; 1607 g_sim.nranges = 3; 1608 int rc = ws_run(&g_sim, batch, 1); 1609 check(rc == 0, "sim fft n=%u inv=%d mut=%d: %s", n, inverse, mut, g_sim.msg); 1610 return rc; 1611 } 1612 1613 static void sim_rng_tier(void) { 1614 const uint32_t ns = 130; /* two CTAs, the second with 2 live threads and 126 that exit */ 1615 static uint64_t got[130 * 17], ref[130 * 17]; 1616 static float fgot[130 * 17]; 1617 const struct { uint64_t seed, stream0; uint32_t offset, per; } runs[] = { 1618 { 0, 0, 0, 17 }, { 0, 0, 1000, 16 }, { 0xFFFFFFFFFFFFFFFCull, 0, 0, 17 }, { 0xFFFFFFFFFFFFFFFCull, 0, 1000, 16 }, 1619 { 1729, 0, 0, 17 }, { 1729, 0xFFFFFFFFFFFFFF80ull, 3, 5 }, 1620 }; 1621 size_t fix_used = 0, fix_bad = 0; 1622 memset(g_fix_hit, 0, sizeof g_fix_hit); 1623 for (size_t r = 0; r < sizeof runs / sizeof runs[0]; r++) { 1624 uint32_t cnt = ns * runs[r].per; 1625 ref_raw(runs[r].seed, runs[r].stream0, ns, runs[r].offset, runs[r].per, ref); 1626 if (sim_rng(0, 0, runs[r].seed, runs[r].stream0, ns, runs[r].offset, runs[r].per, got) == 0) { 1627 size_t bad = 0; 1628 for (uint32_t i = 0; i < cnt; i++) bad += got[i] != ref[i]; 1629 check(bad == 0, "sim raw seed %" PRIu64 " offset %u: %zu of %u differ", runs[r].seed, runs[r].offset, bad, cnt); 1630 if (runs[r].stream0 == 0) fix_used += fixture_compare(got, runs[r].seed, ns, runs[r].offset, runs[r].per, &fix_bad); 1631 receipt("sim rng_raw seed=%" PRIu64 " stream0=%" PRIu64 " ns=%u offset=%u per=%u mismatches=%zu", runs[r].seed, 1632 runs[r].stream0, ns, runs[r].offset, runs[r].per, bad); 1633 } 1634 if (runs[r].offset == 1000) continue; /* uniform and normal share the skip loop */ 1635 if (sim_rng(1, 0, runs[r].seed, runs[r].stream0, ns, runs[r].offset, runs[r].per, fgot) == 0) { 1636 size_t bad = 0; 1637 for (uint32_t i = 0; i < cnt; i++) bad += f2u(fgot[i]) != f2u(uniform_of(ref[i])); 1638 check(bad == 0, "sim uniform seed %" PRIu64 ": %zu of %u differ", runs[r].seed, bad, cnt); 1639 receipt("sim rng_uniform seed=%" PRIu64 " n=%u mismatches=%zu", runs[r].seed, cnt, bad); 1640 } 1641 if (sim_rng(2, 0, runs[r].seed, runs[r].stream0, ns, runs[r].offset, runs[r].per, fgot) == 0) { 1642 size_t bad = 0, over = 0; 1643 double worst = 0; 1644 for (uint32_t i = 0; i < cnt; i++) { 1645 uint32_t A = normal_A(ref[i]); 1646 bad += f2u(fgot[i]) != f2u(normal_model(A)); 1647 double e = rel_err(fgot[i], normal_ref(A)); 1648 over += !(e <= NORMAL_TOL); 1649 if (!(e <= worst)) worst = e; 1650 } 1651 check(bad == 0, "sim normal seed %" PRIu64 ": %zu of %u differ from the f32 model", runs[r].seed, bad, cnt); 1652 check(over == 0, "sim normal seed %" PRIu64 ": %zu of %u past the bound", runs[r].seed, over, cnt); 1653 receipt("sim rng_normal seed=%" PRIu64 " n=%u model_mismatches=%zu over_tol=%zu worst_rel=%.4e", runs[r].seed, 1654 cnt, bad, over, worst); 1655 } 1656 } 1657 check(fix_used > 0 && fix_bad == 0, "sim: %zu of %zu fixture comparisons differ", fix_bad, fix_used); 1658 receipt("sim rng_fixture rows_covered=%zu of %zu comparisons=%zu mismatches=%zu", fixture_distinct(), g_nfix, 1659 fix_used, fix_bad); 1660 /* mutants: rotation 8 breaks the raw stream; GC0 = 1.5 breaks the normal bound */ 1661 ref_raw(0, 0, ns, 0, 17, ref); 1662 if (sim_rng(0, 1, 0, 0, ns, 0, 17, got) == 0) { 1663 size_t bad = 0; 1664 for (uint32_t i = 0; i < ns * 17; i++) bad += got[i] != ref[i]; 1665 check(bad > 0, "sim rng mutant 1 not caught"); 1666 receipt("sim rng_mutant1 mismatches=%zu of %u (must be > 0)", bad, ns * 17); 1667 } 1668 if (sim_rng(2, 2, 0, 0, ns, 0, 17, fgot) == 0) { 1669 size_t over = 0; 1670 for (uint32_t i = 0; i < ns * 17; i++) over += !(rel_err(fgot[i], normal_ref(normal_A(ref[i]))) <= NORMAL_TOL); 1671 check(over > 0, "sim rng mutant 2 not caught"); 1672 receipt("sim rng_mutant2 over_tol=%zu of %u (must be > 0)", over, ns * 17); 1673 } 1674 } 1675 1676 static void sim_fft_tier(void) { 1677 const uint32_t batch = 3; 1678 float *in = malloc((size_t)8 * 2048 * batch), *out = malloc((size_t)8 * 2048 * batch), 1679 *out2 = malloc((size_t)8 * 2048 * batch); 1680 const uint32_t sched[3] = { 0, 1, 0x9E3779B9u }; 1681 for (int lg = 2; lg <= 11; lg++) 1682 for (int inv = 0; inv < 2; inv++) { 1683 uint32_t n = 1u << lg; 1684 fill_rand(in, (size_t)2 * n * batch); 1685 double e = -1; 1686 int same = 1; 1687 for (int s = 0; s < 3; s++) { 1688 if (sim_fft(n, inv, 0, sched[s], in, s ? out2 : out, batch) != 0) { same = 0; break; } 1689 if (s == 0) e = fft_err(in, out, n, batch, inv); 1690 else same &= !memcmp(out, out2, (size_t)8 * n * batch); 1691 } 1692 check(e >= 0 && e <= FFT_TOL[lg], "sim fft n=%u inv=%d: error %.4e, tolerance %.4e", n, inv, e, FFT_TOL[lg]); 1693 check(same, "sim fft n=%u inv=%d: output depends on the warp order", n, inv); 1694 receipt("sim fft n=%u inverse=%d batch=%u normwise_err=%.4e tol=%.4e warp_orders_identical=%d", n, inv, batch, e, 1695 FFT_TOL[lg], same); 1696 } 1697 /* mutants: direction flip at every N, dropped twiddles at N >= 8 (N = 4 has no twiddled stage) */ 1698 const struct { uint32_t n; int mut; int caught; } mm[] = { { 4, 1, 1 }, { 64, 1, 1 }, { 2048, 1, 1 }, 1699 { 8, 2, 1 }, { 16, 2, 1 }, { 2048, 2, 1 }, { 4, 2, 0 } }; 1700 for (size_t i = 0; i < sizeof mm / sizeof mm[0]; i++) { 1701 uint32_t n = mm[i].n; 1702 int lg = lg2u(n); 1703 fill_rand(in, (size_t)2 * n * batch); 1704 if (sim_fft(n, 0, mm[i].mut, 0, in, out, batch) != 0) continue; 1705 double e = fft_err(in, out, n, batch, 0); 1706 int caught = !(e <= FFT_TOL[lg]); 1707 check(caught == mm[i].caught, "sim fft mutant %d n=%u: error %.4e, %s", mm[i].mut, n, e, 1708 caught ? "caught" : "not caught"); 1709 receipt("sim fft_mutant%d n=%u normwise_err=%.4e caught=%d expected=%d", mm[i].mut, n, e, caught, mm[i].caught); 1710 } 1711 free(in); free(out); free(out2); 1712 } 1713 1714 /* Philox through the API's argument block (omega_gpu_philox_generate_dist): out, key, subseq0, first counter block 1715 * q0, groups T = ceil(per / G), streams, tail delta. q0 is passed whole so a KAT counter block 2^64 - 1 fits. */ 1716 static int sim_philox(int dist, int mut, uint64_t key, uint64_t sub0, uint64_t q0, uint32_t b, uint32_t ns, 1717 uint32_t per, uint32_t *out) { 1718 if (sim_prog(3, (uint64_t)dist, b, (uint64_t)mut, OMEGA_GPU_PHILOX_THREADS) != 0) return -1; 1719 uint32_t G = dist == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE ? 2 : 4, r = per % G; 1720 size_t bytes = (size_t)ns * per * ph_esz(dist) * 4; 1721 memset(out, 0xa5, bytes); 1722 memset(&g_sim, 0, sizeof g_sim); 1723 g_sim.p = g_prog; 1724 g_sim.threads = OMEGA_GPU_PHILOX_THREADS; 1725 uint32_t *cb = &g_sim.cbank[0x380 / 4]; 1726 put64(&cb[0], (uint64_t)(uintptr_t)out); 1727 put64(&cb[2], key); 1728 put64(&cb[4], sub0); 1729 put64(&cb[6], q0); 1730 cb[8] = per / G + (r != 0); 1731 cb[9] = ns; 1732 cb[10] = r ? r - G : 0; 1733 g_sim.ranges[0] = (WsRange){ (uintptr_t)out, (uintptr_t)out + bytes }; 1734 g_sim.nranges = 1; 1735 int rc = ws_run(&g_sim, (ns + OMEGA_GPU_PHILOX_THREADS - 1) / OMEGA_GPU_PHILOX_THREADS, 1); 1736 check(rc == 0, "sim philox dist=%d mut=%d: %s", dist, mut, g_sim.msg); 1737 return rc; 1738 } 1739 1740 static size_t diff_words(const uint32_t *a, const uint32_t *b, size_t n) { 1741 size_t bad = 0; 1742 for (size_t i = 0; i < n; i++) bad += a[i] != b[i]; 1743 return bad; 1744 } 1745 /* one run against the mirror: the number of differing words (all of them when the run fails) */ 1746 static size_t sim_dist_run(int d, int mut, uint64_t seed, uint64_t sub0, uint32_t ns, uint64_t off, uint32_t per, 1747 uint32_t *got, uint32_t *ref) { 1748 size_t n = (size_t)ns * per * ph_esz(d); 1749 ref_dist(d, seed, sub0, ns, off, per, ref); 1750 if (sim_philox(d, mut, seed, sub0, off / 4, (uint32_t)(off % 4), ns, per, got) != 0) return n; 1751 return diff_words(got, ref, n); 1752 } 1753 1754 static void sim_philox_tier(void) { 1755 static uint32_t got[130 * 68], ref[130 * 68]; 1756 /* the three KAT vectors through the argument block: ctr = (block lo, block hi, subseq lo, subseq hi) */ 1757 size_t kat_bad = 0; 1758 for (int i = 0; i < 3; i++) { 1759 const uint32_t *c = PH_KAT[i].ctr; 1760 uint64_t key = PH_KAT[i].key[0] | (uint64_t)PH_KAT[i].key[1] << 32; 1761 if (sim_philox(OMEGA_GPU_PHILOX_RAW, 0, key, c[2] | (uint64_t)c[3] << 32, c[0] | (uint64_t)c[1] << 32, 0, 1, 4, 1762 got) != 0) { kat_bad++; continue; } 1763 kat_bad += memcmp(got, PH_KAT[i].want, 16) != 0; 1764 } 1765 check(kat_bad == 0, "sim philox: %zu of 3 KAT vectors differ", kat_bad); 1766 receipt("sim philox_kat vectors=3 mismatches=%zu", kat_bad); 1767 /* raw words: 4 seeds x 4 block-aligned offsets x (130 streams from subseq 0, 2 streams across subseq 2^32) */ 1768 size_t bad = 0, words = 0, used = 0, fbad = 0, runs = 0; 1769 memset(g_ph_hit, 0, sizeof g_ph_hit); 1770 for (int s = 0; s < 4; s++) 1771 for (int o = 0; o < 4; o++) 1772 for (int r = 0; r < 2; r++) { 1773 uint64_t sub0 = r ? 0xFFFFFFFFull : 0; 1774 uint32_t ns = r ? 2 : 130; 1775 bad += sim_dist_run(OMEGA_GPU_PHILOX_RAW, 0, PH_SEEDS[s], sub0, ns, PH_OFFS[o], 8, got, ref); 1776 words += (size_t)ns * 8; 1777 runs++; 1778 used += ph_compare(got, PH_SEEDS[s], sub0, ns, PH_OFFS[o], 8, &fbad); 1779 } 1780 check(bad == 0, "sim philox streams: %zu of %zu words differ from the reference", bad, words); 1781 check(ph_distinct() == g_nph && g_nph == 1024 && fbad == 0, 1782 "sim philox cuRAND fixture: %zu of %zu rows covered, %zu of %zu comparisons differ", ph_distinct(), g_nph, fbad, 1783 used); 1784 receipt("sim philox_streams runs=%zu words=%zu mismatches=%zu", runs, words, bad); 1785 receipt("sim philox_curand_fixture rows_covered=%zu of %zu comparisons=%zu mismatches=%zu", ph_distinct(), g_nph, 1786 used, fbad); 1787 /* every distribution: 4 seeds x the fixture's 7 offsets (any offset mod 4) x the same two stream ranges, 8 outputs 1788 * each, bit for bit against the mirror; the cuRAND rows they cover against cuRAND */ 1789 memset(g_pd_hit, 0, sizeof g_pd_hit); 1790 for (int d = 0; d <= 4; d++) { 1791 size_t dbad = 0, dwords = 0, dused = 0, dfbad = 0; 1792 for (int s = 0; s < 4; s++) 1793 for (int o = 0; o < 7; o++) 1794 for (int r = 0; r < 2; r++) { 1795 uint64_t sub0 = r ? 0xFFFFFFFFull : 0; 1796 uint32_t ns = r ? 2 : 130; 1797 dbad += sim_dist_run(d, 0, PH_SEEDS[s], sub0, ns, PD_OFFS[o], 8, got, ref); 1798 dwords += (size_t)ns * 8 * ph_esz(d); 1799 if (d > 0) dused += pd_compare(d, got, PH_SEEDS[s], sub0, ns, PD_OFFS[o], 8, &dfbad); 1800 } 1801 check(dbad == 0, "sim philox dist %d: %zu of %zu words differ from the mirror", d, dbad, dwords); 1802 if (d > 0) 1803 check(pd_distinct(d) == 896 && dfbad == 0, 1804 "sim philox dist %d cuRAND fixture: %zu of 896 rows covered, %zu of %zu comparisons disagree", d, 1805 pd_distinct(d), dfbad, dused); 1806 receipt("sim philox_dist dist=%d runs=56 words=%zu mismatches=%zu curand_rows_covered=%zu curand_disagree=%zu", d, 1807 dwords, dbad, d > 0 ? pd_distinct(d) : 0, dfbad); 1808 } 1809 /* partial groups: per 1, 3, 5, 7, 9 at offsets 0 and 3, every distribution */ 1810 bad = 0; 1811 runs = 0; 1812 for (int d = 0; d <= 4; d++) 1813 for (uint32_t per = 1; per <= 9; per += 2) 1814 for (uint64_t off = 0; off <= 3; off += 3) { 1815 size_t b1 = sim_dist_run(d, 0, 1729, 5, 130, off, per, got, ref); 1816 if (b1) printf("sim philox tail dist %d per %u offset %" PRIu64 ": %zu differ\n", d, per, off, b1); 1817 bad += b1; 1818 runs++; 1819 } 1820 check(bad == 0, "sim philox partial groups: %zu words differ", bad); 1821 receipt("sim philox_partial_groups runs=%zu mismatches=%zu", runs, bad); 1822 /* 17 blocks per stream; the block counter carrying from its low word into its high word at every offset mod 4 */ 1823 bad = sim_dist_run(OMEGA_GPU_PHILOX_RAW, 0, 1729, 5, 130, 1000, 68, got, ref); 1824 check(bad == 0, "sim philox 17 blocks: %zu differ", bad); 1825 receipt("sim philox seed=1729 subseq0=5 ns=130 offset=1000 per=68 mismatches=%zu", bad); 1826 bad = 0; 1827 for (int d = 0; d <= 4; d++) 1828 for (uint64_t b = 0; b < 4; b++) bad += sim_dist_run(d, 0, 7, 0, 3, 4 * 0xFFFFFFFDull + b, 31, got, ref); 1829 check(bad == 0, "sim philox block carry: %zu differ", bad); 1830 receipt("sim philox_carry seed=7 ns=3 offset=4*(2^32-3)+0..3 per=31 dists=5 mismatches=%zu", bad); 1831 /* mutants: 1 key Weyl step W0 for the second word, 2 nine rounds, 3 the uniform without its 2^-33 half step (at 1832 * a position whose word is below 2^23, where the step is representable), 4 the unaligned offset ignoring the 1833 * previous block, 5 the normal pair swapped, 6 uniform2_double without its round-to-even half step */ 1834 uint64_t small = 0; 1835 while (ph_word(0x123456789abcdefull, 0, small) >= 1u << 23) small++; 1836 const struct { int d; uint64_t off; } mt[7] = { 1837 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 }, { 4, 0 }, { 3, 0 }, 1838 }; 1839 for (int mut = 1; mut <= 6; mut++) { 1840 uint64_t off = mut == 3 ? small : mt[mut].off; 1841 size_t n = (size_t)130 * 8 * ph_esz(mt[mut].d); 1842 ref_dist(mt[mut].d, 0x123456789abcdefull, 0, 130, off, 8, ref); 1843 size_t mb = sim_philox(mt[mut].d, mut, 0x123456789abcdefull, 0, off / 4, (uint32_t)(off % 4), 130, 8, got) == 0 1844 ? diff_words(got, ref, n) : 0; 1845 check(mb > 0, "sim philox mutant %d not caught", mut); 1846 receipt("sim philox_mutant%d dist=%d offset=%" PRIu64 " mismatches=%zu of %zu (must be > 0)", mut, mt[mut].d, off, 1847 mb, n); 1848 } 1849 } 1850 1851 /* the API's plan walked on the simulator: every launch of omega_gpu_fft_plan_walk built by the unit and run */ 1852 typedef struct { void *buf[3]; size_t bytes[3]; uint32_t mkind; int mlvl; uint32_t sched, launches; } SimPlan; 1853 static int sim_launch(void *ctx, const OmegaGpuFftLaunch *l) { 1854 SimPlan *r = ctx; 1855 static uint8_t smem[8 * 2048]; 1856 uint64_t mut = l->kind == r->mkind ? (uint64_t)r->mlvl : 0; 1857 if (l->shared_bytes > sizeof smem) { check(0, "sim plan: %u shared bytes", l->shared_bytes); return -1; } 1858 if (sim_prog(l->kind, l->a, l->b, mut, l->threads) != 0) return -1; 1859 memset(&g_sim, 0, sizeof g_sim); 1860 g_sim.p = g_prog; 1861 g_sim.threads = l->threads; 1862 g_sim.warp_sched = r->sched; 1863 if (l->shared_bytes) { g_sim.smem = smem; g_sim.smem_bytes = l->shared_bytes; } 1864 uint32_t *cb = &g_sim.cbank[0x380 / 4]; 1865 put64(&cb[0], (uint64_t)(uintptr_t)r->buf[l->src] + l->src_off); 1866 put64(&cb[2], (uint64_t)(uintptr_t)r->buf[l->dst] + l->dst_off); 1867 if (l->kind == 5) { cb[4] = l->total; cb[5] = l->first; } 1868 else put64(&cb[4], (uint64_t)(uintptr_t)l->tw); 1869 for (int i = 0; i < 3; i++) 1870 g_sim.ranges[i] = (WsRange){ (uintptr_t)r->buf[i], (uintptr_t)r->buf[i] + r->bytes[i] }; 1871 g_sim.nranges = 3; 1872 if (l->tw_n) 1873 g_sim.ranges[g_sim.nranges++] = (WsRange){ (uintptr_t)l->tw, (uintptr_t)l->tw + (size_t)8 * l->tw_n }; 1874 int rc = ws_run(&g_sim, l->grid, 1); 1875 r->launches++; 1876 check(rc == 0, "sim plan launch kind %u a %u b %u grid %u: %s", l->kind, l->a, l->b, l->grid, g_sim.msg); 1877 return rc == 0 ? 0 : -1; 1878 } 1879 /* out and scratch start as 0xff bytes (a NaN); *unwritten counts the out words still holding it */ 1880 static int sim_plan(OmegaGpuFftPlanMode mode, uint32_t nd, const uint32_t *dims, uint32_t batch, int inverse, 1881 const float *in, float *out, uint32_t mkind, int mlvl, uint32_t sched, size_t *unwritten) { 1882 SimPlan r; 1883 memset(&r, 0, sizeof r); 1884 int rc = omega_gpu_fft_plan_walk(mode, nd, dims, batch, inverse, r.bytes, NULL, NULL); 1885 check(rc == OMEGA_GPU_ML_OK, "sim plan mode %d ndim %u: rc %d (%s)", (int)mode, nd, rc, omega_gpu_mathlibs_last_error()); 1886 if (rc != OMEGA_GPU_ML_OK) return -1; 1887 float *scr = malloc(r.bytes[2]); 1888 memset(out, 0xff, r.bytes[1]); 1889 memset(scr, 0xff, r.bytes[2]); 1890 r.buf[0] = (void *)(uintptr_t)in; 1891 r.buf[1] = out; 1892 r.buf[2] = scr; 1893 r.mkind = mkind; 1894 r.mlvl = mlvl; 1895 r.sched = sched; 1896 rc = omega_gpu_fft_plan_walk(mode, nd, dims, batch, inverse, r.bytes, sim_launch, &r); 1897 check(rc == 0, "sim plan mode %d ndim %u: walk rc %d", (int)mode, nd, rc); 1898 size_t u = 0; 1899 const uint32_t *w = (const uint32_t *)out; 1900 for (size_t i = 0; i < r.bytes[1] / 4; i++) u += w[i] == 0xffffffffu; 1901 *unwritten = u; 1902 free(scr); 1903 return rc == 0 ? 0 : -1; 1904 } 1905 1906 static void hermitian(float *x, uint32_t n, uint32_t batch) { 1907 for (uint32_t b = 0; b < batch; b++) { x[(size_t)(n + 2) * b + 1] = 0; x[(size_t)(n + 2) * b + n + 1] = 0; } 1908 } 1909 1910 static void sim_real_tier(void) { 1911 const uint32_t batch = 3; 1912 size_t cap = (size_t)(4096 + 2) * batch; 1913 float *in = malloc(cap * 4), *out = malloc(cap * 4), *out2 = malloc(cap * 4); 1914 const uint32_t sched[3] = { 0, 1, 0x9E3779B9u }; 1915 for (int lg = 3; lg <= 12; lg++) 1916 for (int c2r = 0; c2r < 2; c2r++) { 1917 uint32_t n = 1u << lg; 1918 size_t nin = c2r ? (size_t)(n + 2) * batch : (size_t)n * batch; 1919 size_t nout = c2r ? (size_t)n * batch : (size_t)(n + 2) * batch; 1920 OmegaGpuFftPlanMode mode = c2r ? OMEGA_GPU_FFT_C2R : OMEGA_GPU_FFT_R2C; 1921 fill_rand(in, nin); 1922 if (c2r) hermitian(in, n, batch); 1923 double e = -1; 1924 int same = 1; 1925 size_t unw = 0, u; 1926 for (int s = 0; s < 3; s++) { 1927 if (sim_plan(mode, 1, &n, batch, 0, in, s ? out2 : out, 0, 0, sched[s], &u) != 0) { same = 0; break; } 1928 unw += u; 1929 if (s == 0) e = c2r ? c2r_err(in, out, n, batch) : r2c_err(in, out, n, batch); 1930 else same &= !memcmp(out, out2, nout * 4); 1931 } 1932 double tol = c2r ? C2R_TOL[lg] : R2C_TOL[lg]; 1933 const char *nm = c2r ? "c2r" : "r2c"; 1934 check(e >= 0 && e <= tol, "sim %s n=%u: error %.4e, tolerance %.4e", nm, n, e, tol); 1935 check(same, "sim %s n=%u: output depends on the warp order", nm, n); 1936 check(unw == 0, "sim %s n=%u: %zu words unwritten", nm, n, unw); 1937 receipt("sim %s n=%u batch=%u normwise_err=%.4e tol=%.4e warp_orders_identical=%d unwritten=%zu", nm, n, batch, 1938 e, tol, same, unw); 1939 } 1940 /* mutants of the split (R2C) and merge (C2R) pass: the dropped twiddle, the skipped bin n / 4 */ 1941 const struct { int c2r; uint32_t n; int mut; } mm[] = { { 0, 64, 1 }, { 0, 8, 2 }, { 0, 4096, 2 }, 1942 { 1, 64, 1 }, { 1, 8, 2 }, { 1, 4096, 2 } }; 1943 for (size_t i = 0; i < sizeof mm / sizeof mm[0]; i++) { 1944 uint32_t n = mm[i].n; 1945 int lg = lg2u(n), c2r = mm[i].c2r; 1946 size_t u; 1947 fill_rand(in, c2r ? (size_t)(n + 2) * batch : (size_t)n * batch); 1948 if (c2r) hermitian(in, n, batch); 1949 if (sim_plan(c2r ? OMEGA_GPU_FFT_C2R : OMEGA_GPU_FFT_R2C, 1, &n, batch, 0, in, out, 4, mm[i].mut, 0, &u) != 0) 1950 continue; 1951 double e = c2r ? c2r_err(in, out, n, batch) : r2c_err(in, out, n, batch); 1952 double tol = c2r ? C2R_TOL[lg] : R2C_TOL[lg]; 1953 int caught = !(e <= tol); 1954 check(caught, "sim %s mutant %d n=%u: error %.4e not caught", c2r ? "c2r" : "r2c", mm[i].mut, n, e); 1955 receipt("sim %s_mutant%d n=%u normwise_err=%.4e unwritten=%zu caught=%d", c2r ? "c2r" : "r2c", mm[i].mut, n, e, u, 1956 caught); 1957 } 1958 free(in); free(out); free(out2); 1959 } 1960 1961 static void sim_nd_tier(void) { 1962 const uint32_t batch = 2; 1963 size_t cap = (size_t)2 * 32768 * batch; 1964 float *in = malloc(cap * 4), *out = malloc(cap * 4), *out2 = malloc(cap * 4); 1965 const uint32_t sched[3] = { 0, 1, 0x9E3779B9u }; 1966 for (size_t i = 0; i < N_ND_SHAPES; i++) 1967 for (int inv = 0; inv < 2; inv++) { 1968 const uint32_t *d = ND_SHAPES[i].d; 1969 uint32_t nd = ND_SHAPES[i].nd; 1970 size_t tot = (size_t)d[0] * (nd > 1 ? d[1] : 1) * (nd > 2 ? d[2] : 1), nf = 2 * tot * batch; 1971 fill_rand(in, nf); 1972 double e = -1; 1973 int same = 1; 1974 size_t unw = 0, u; 1975 for (int s = 0; s < 3; s++) { 1976 if (sim_plan(OMEGA_GPU_FFT_C2C_ND, nd, d, batch, inv, in, s ? out2 : out, 0, 0, sched[s], &u) != 0) { 1977 same = 0; 1978 break; 1979 } 1980 unw += u; 1981 if (s == 0) e = nd_err(in, out, nd, d, batch, inv); 1982 else same &= !memcmp(out, out2, nf * 4); 1983 } 1984 double tol = ND_SHAPES[i].tol; 1985 check(e >= 0 && e <= tol, "sim nd {%u %u %u} inv=%d: error %.4e, tolerance %.4e", d[0], d[1], d[2], inv, e, tol); 1986 check(same, "sim nd {%u %u %u} inv=%d: output depends on the warp order", d[0], d[1], d[2], inv); 1987 check(unw == 0, "sim nd {%u %u %u} inv=%d: %zu words unwritten", d[0], d[1], d[2], inv, unw); 1988 receipt("sim nd ndim=%u dims=%u,%u,%u inverse=%d batch=%u normwise_err=%.4e tol=%.4e warp_orders_identical=%d", 1989 nd, d[0], d[1], d[2], inv, batch, e, tol, same); 1990 } 1991 /* mutants: the transpose's identity copy and its dropped batch index (invisible with one transform: the 1992 * expected miss), the complex pass's direction flip through a 3-D plan */ 1993 const struct { uint32_t nd, d[3], batch, kind; int mut, caught; double tol; } mm[] = { 1994 { 2, { 8, 16, 0 }, 2, 5, 1, 1, 9.`928263778e`-7 }, 1995 { 2, { 8, 16, 0 }, 2, 5, 2, 1, 9.`928263778e`-7 }, 1996 { 2, { 8, 16, 0 }, 1, 5, 2, 0, 9.`928263778e`-7 }, 1997 { 3, { 4, 8, 16 }, 2, 1, 1, 1, 1.`112035792e`-6 }, 1998 }; 1999 for (size_t i = 0; i < sizeof mm / sizeof mm[0]; i++) { 2000 size_t tot = (size_t)mm[i].d[0] * mm[i].d[1] * (mm[i].nd > 2 ? mm[i].d[2] : 1), u; 2001 fill_rand(in, 2 * tot * mm[i].batch); 2002 if (sim_plan(OMEGA_GPU_FFT_C2C_ND, mm[i].nd, mm[i].d, mm[i].batch, 0, in, out, mm[i].kind, mm[i].mut, 0, &u) != 0) 2003 continue; 2004 double e = nd_err(in, out, mm[i].nd, mm[i].d, mm[i].batch, 0); 2005 int caught = !(e <= mm[i].tol); 2006 check(caught == mm[i].caught, "sim nd mutant kind %u level %d batch %u: error %.4e, %s", mm[i].kind, mm[i].mut, 2007 mm[i].batch, e, caught ? "caught" : "not caught"); 2008 receipt("sim nd_mutant kind=%u level=%d ndim=%u batch=%u normwise_err=%.4e caught=%d expected=%d", mm[i].kind, 2009 mm[i].mut, mm[i].nd, mm[i].batch, e, caught, mm[i].caught); 2010 } 2011 free(in); free(out); free(out2); 2012 } 2013 2014 /* cufftPlanMany through the simulator: the plan walk drives every launch against host buffers (scratch poisoned 2015 * with 0xff), the logical input is scattered into its layout with gaps of NaN, and the output is gathered back. 2016 * Tolerances: evidence/MATHLIBS-GAPS-2026-11-06/wolfram-many-tolerances.txt (normwise relative error of each 2017 * transform against the f64 reference; per-pass bounds of radices 2, 4, 3, 5, 7 and of Bluestein, then the 2018 * R2C and C2R and multi-axis compositions). */ 2019 static const struct { OmegaGpuFftType type; uint32_t rank, n[3]; double tol; } MANY_TOL[] = { 2020 { OMEGA_GPU_FFT_TYPE_C2C, 1, { 3 }, 5.`840040057e`-7 }, { OMEGA_GPU_FFT_TYPE_C2C, 1, { 5 }, 9.`424324677e`-7 }, 2021 { OMEGA_GPU_FFT_TYPE_C2C, 1, { 6 }, 9.`314055970e`-7 }, { OMEGA_GPU_FFT_TYPE_C2C, 1, { 7 }, 1.`338121440e`-6 }, 2022 { OMEGA_GPU_FFT_TYPE_C2C, 1, { 12 }, 9.`910103014e`-7 }, { OMEGA_GPU_FFT_TYPE_C2C, 1, { 100 }, 2.`579670142e`-6 }, 2023 { OMEGA_GPU_FFT_TYPE_C2C, 1, { 97 }, 2.`717869932e`-5 }, { OMEGA_GPU_FFT_TYPE_C2C, 1, { 1000 }, 4.`157308909e`-6 }, 2024 { OMEGA_GPU_FFT_TYPE_C2C, 1, { 4095 }, 2.`486110600e`-4 }, { OMEGA_GPU_FFT_TYPE_C2C, 1, { 11 }, 9.`883007381e`-6 }, 2025 { OMEGA_GPU_FFT_TYPE_C2C, 1, { 26 }, 1.`284078720e`-5 }, { OMEGA_GPU_FFT_TYPE_C2C, 2, { 3, 5 }, 1.`526437024e`-6 }, 2026 { OMEGA_GPU_FFT_TYPE_C2C, 3, { 6, 7, 12 }, 3.`260540834e`-6 }, { OMEGA_GPU_FFT_TYPE_C2C, 2, { 12, 97 }, 2.`816973655e`-5 }, 2027 { OMEGA_GPU_FFT_TYPE_C2C, 2, { 6, 10 }, 2.`221240984e`-6 }, { OMEGA_GPU_FFT_TYPE_C2C, 3, { 3, 4, 5 }, 1.`645646502e`-6 }, 2028 { OMEGA_GPU_FFT_TYPE_R2C, 1, { 3 }, 8.`259063854e`-7 }, { OMEGA_GPU_FFT_TYPE_R2C, 1, { 5 }, 1.`332800778e`-6 }, 2029 { OMEGA_GPU_FFT_TYPE_R2C, 1, { 6 }, 1.`317206428e`-6 }, { OMEGA_GPU_FFT_TYPE_R2C, 1, { 7 }, 1.`892389489e`-6 }, 2030 { OMEGA_GPU_FFT_TYPE_R2C, 1, { 12 }, 1.`401500209e`-6 }, { OMEGA_GPU_FFT_TYPE_R2C, 1, { 100 }, 3.`648204501e`-6 }, 2031 { OMEGA_GPU_FFT_TYPE_R2C, 1, { 97 }, 3.`843648518e`-5 }, { OMEGA_GPU_FFT_TYPE_R2C, 1, { 1000 }, 5.`879322642e`-6 }, 2032 { OMEGA_GPU_FFT_TYPE_R2C, 1, { 4095 }, 3.`515891328e`-4 }, { OMEGA_GPU_FFT_TYPE_R2C, 1, { 11 }, 1.`397668308e`-5 }, 2033 { OMEGA_GPU_FFT_TYPE_R2C, 1, { 26 }, 1.`815961541e`-5 }, { OMEGA_GPU_FFT_TYPE_R2C, 2, { 6, 10 }, 2.`755508296e`-6 }, 2034 { OMEGA_GPU_FFT_TYPE_R2C, 3, { 5, 4, 6 }, 2.`378849705e`-6 }, { OMEGA_GPU_FFT_TYPE_R2C, 2, { 12, 9 }, 3.`049832107e`-6 }, 2035 { OMEGA_GPU_FFT_TYPE_R2C, 2, { 5, 8 }, 1.`602320310e`-6 }, { OMEGA_GPU_FFT_TYPE_C2R, 1, { 3 }, 5.`840040057e`-7 }, 2036 { OMEGA_GPU_FFT_TYPE_C2R, 1, { 5 }, 9.`424324677e`-7 }, { OMEGA_GPU_FFT_TYPE_C2R, 1, { 6 }, 9.`314055970e`-7 }, 2037 { OMEGA_GPU_FFT_TYPE_C2R, 1, { 7 }, 1.`338121440e`-6 }, { OMEGA_GPU_FFT_TYPE_C2R, 1, { 12 }, 9.`910103014e`-7 }, 2038 { OMEGA_GPU_FFT_TYPE_C2R, 1, { 100 }, 2.`579670142e`-6 }, { OMEGA_GPU_FFT_TYPE_C2R, 1, { 97 }, 2.`717869932e`-5 }, 2039 { OMEGA_GPU_FFT_TYPE_C2R, 1, { 1000 }, 4.`157308909e`-6 }, { OMEGA_GPU_FFT_TYPE_C2R, 1, { 4095 }, 2.`486110600e`-4 }, 2040 { OMEGA_GPU_FFT_TYPE_C2R, 1, { 11 }, 9.`883007381e`-6 }, { OMEGA_GPU_FFT_TYPE_C2R, 1, { 26 }, 1.`284078720e`-5 }, 2041 { OMEGA_GPU_FFT_TYPE_C2R, 2, { 6, 10 }, 2.`607042310e`-6 }, { OMEGA_GPU_FFT_TYPE_C2R, 3, { 5, 4, 6 }, 2.`432795333e`-6 }, 2042 { OMEGA_GPU_FFT_TYPE_C2R, 2, { 12, 9 }, 2.`857307661e`-6 }, 2043 }; 2044 static double many_tol(const OmegaGpuFftMany *p) { 2045 for (size_t i = 0; i < sizeof MANY_TOL / sizeof MANY_TOL[0]; i++) 2046 if (MANY_TOL[i].type == p->type && MANY_TOL[i].rank == p->rank && 2047 !memcmp(MANY_TOL[i].n, p->n, p->rank * sizeof p->n[0])) 2048 return MANY_TOL[i].tol; 2049 check(0, "no tolerance for many type %d rank %u n %u", (int)p->type, p->rank, p->n[0]); 2050 return 0; 2051 } 2052 2053 typedef struct { 2054 uint8_t *buf[OMEGA_GPU_FFT_MANY_BUFS]; 2055 size_t bytes[OMEGA_GPU_FFT_MANY_BUFS]; 2056 uint32_t mut_kind, mut, sched, have, kind, a, b, m, table_done; 2057 size_t launches; 2058 } SimMany; 2059 static int sim_many_cb(void *ctx, const OmegaGpuFftManyLaunch *l) { 2060 SimMany *s = ctx; 2061 uint32_t mut = l->kind == s->mut_kind ? s->mut : 0; 2062 if (!s->have || s->kind != l->kind || s->a != l->a || s->b != l->b || s->m != mut) { 2063 if (sim_prog(l->kind, l->a, l->b, mut, l->threads) != 0) return -1; 2064 s->have = 1; s->kind = l->kind; s->a = l->a; s->b = l->b; s->m = mut; 2065 } 2066 if (!s->table_done && s->bytes[OMEGA_GPU_FFT_BUF_TABLE]) { 2067 memcpy(s->buf[OMEGA_GPU_FFT_BUF_TABLE], l->table, s->bytes[OMEGA_GPU_FFT_BUF_TABLE]); 2068 s->table_done = 1; 2069 } 2070 memset(&g_sim, 0, sizeof g_sim); 2071 g_sim.p = g_prog; 2072 g_sim.threads = l->threads; 2073 g_sim.warp_sched = s->sched; 2074 uint32_t *cb = &g_sim.cbank[0x380 / 4]; 2075 for (uint32_t k = 0; k < l->nptr; k++) put64(&cb[2 * k], (uint64_t)(uintptr_t)s->buf[l->buf[k]] + l->off[k]); 2076 for (uint32_t w = 0; w < l->nwords; w++) cb[2 * l->nptr + w] = l->words[w]; 2077 for (int i = 0; i < OMEGA_GPU_FFT_MANY_BUFS; i++) 2078 if (s->bytes[i]) 2079 g_sim.ranges[g_sim.nranges++] = (WsRange){ (uintptr_t)s->buf[i], (uintptr_t)s->buf[i] + s->bytes[i] }; 2080 int rc = ws_run(&g_sim, l->grid, 1); 2081 check(rc == 0, "sim many kind %u a %u b %u: %s", l->kind, l->a, l->b, g_sim.msg); 2082 s->launches++; 2083 return rc == 0 ? 0 : -1; 2084 } 2085 /* result of one simulated many plan: dense logical output, worst per-transform error, poison accounting */ 2086 typedef struct { double err; size_t unwritten, gaps_changed, input_changed, launches; int rc; } ManyRun; 2087 static ManyRun sim_many_run(const OmegaGpuFftMany *p, int in_place, uint32_t mut_kind, uint32_t mut, uint32_t sched, 2088 const float *x, const double *ref, float *y) { 2089 ManyRun r = { -1, 0, 0, 0, 0, 0 }; 2090 SimMany s; 2091 memset(&s, 0, sizeof s); 2092 s.mut_kind = mut_kind; s.mut = mut; s.sched = sched; 2093 r.rc = omega_gpu_fft_many_walk(p, in_place, s.bytes, NULL, NULL); 2094 check(r.rc == OMEGA_GPU_ML_OK, "sim many walk sizes: %d", r.rc); 2095 if (r.rc != OMEGA_GPU_ML_OK) return r; 2096 for (int i = 0; i < OMEGA_GPU_FFT_MANY_BUFS; i++) 2097 if (s.bytes[i]) { s.buf[i] = malloc(s.bytes[i]); memset(s.buf[i], 0xff, s.bytes[i]); } 2098 TLay ti, to; 2099 tlay(p, in_place, 0, &ti); 2100 tlay(p, in_place, 1, &to); 2101 float *in = (float *)s.buf[OMEGA_GPU_FFT_BUF_IN]; 2102 float *out = in_place ? in : (float *)s.buf[OMEGA_GPU_FFT_BUF_OUT]; 2103 size_t ipts = tlay_points(&ti), opts = tlay_points(&to), iw = ti.cplx ? 2 : 1, ow = to.cplx ? 2 : 1; 2104 for (uint32_t b = 0; b < p->batch; b++) 2105 for (size_t i = 0; i < ipts; i++) 2106 for (size_t c = 0; c < iw; c++) in[tlay_at(&ti, b, i) + c] = x[(b * ipts + i) * iw + c]; 2107 uint8_t *before = NULL, *addressed = NULL; 2108 size_t onf = in_place ? 0 : s.bytes[OMEGA_GPU_FFT_BUF_OUT] / 4; 2109 if (!in_place) { 2110 before = malloc(s.bytes[OMEGA_GPU_FFT_BUF_IN]); 2111 memcpy(before, in, s.bytes[OMEGA_GPU_FFT_BUF_IN]); 2112 addressed = calloc(onf, 1); 2113 } 2114 r.rc = omega_gpu_fft_many_walk(p, in_place, s.bytes, sim_many_cb, &s); 2115 r.launches = s.launches; 2116 if (r.rc == OMEGA_GPU_ML_OK) { 2117 double worst = 0; 2118 for (uint32_t b = 0; b < p->batch; b++) { 2119 double num = 0, den = 0; 2120 for (size_t i = 0; i < opts; i++) 2121 for (size_t c = 0; c < ow; c++) { 2122 size_t f = tlay_at(&to, b, i) + c, d = (b * opts + i) * ow + c; 2123 uint32_t bits; 2124 memcpy(&bits, &out[f], 4); 2125 if (!in_place) { r.unwritten += bits == 0xffffffffu; addressed[f] = 1; } 2126 y[d] = out[f]; 2127 double e = (double)out[f] - ref[d]; 2128 num += e * e; den += ref[d] * ref[d]; 2129 } 2130 double e = sqrt(num / den); 2131 if (!(e <= worst)) worst = e; 2132 } 2133 r.err = worst; 2134 if (!in_place) { 2135 uint32_t *ow32 = (uint32_t *)out; 2136 for (size_t f = 0; f < onf; f++) r.gaps_changed += !addressed[f] && ow32[f] != 0xffffffffu; 2137 r.input_changed = memcmp(before, in, s.bytes[OMEGA_GPU_FFT_BUF_IN]) != 0; 2138 } 2139 } 2140 free(before); free(addressed); 2141 for (int i = 0; i < OMEGA_GPU_FFT_MANY_BUFS; i++) free(s.buf[i]); 2142 return r; 2143 } 2144 2145 typedef struct { OmegaGpuFftMany p; int in_place; const char *what; } ManyCase; 2146 #define MC1(T, N, INV) { { .rank = 1, .n = { N }, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_##T, .inverse = INV }, 0, \ 2147 #T " " #N " inverse=" #INV } 2148 static void sim_many_tier(void) { 2149 static const ManyCase cases[] = { 2150 #define MCL(N) MC1(C2C, N, 0), MC1(C2C, N, 1), MC1(R2C, N, 0), MC1(C2R, N, 0) 2151 MCL(3), MCL(5), MCL(6), MCL(7), MCL(12), MCL(100), MCL(97), MCL(1000), MCL(4095), MCL(11), MCL(26), 2152 #undef MCL 2153 { { .rank = 2, .n = { 3, 5 }, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_C2C }, 0, "C2C {3,5}" }, 2154 { { .rank = 3, .n = { 6, 7, 12 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2C }, 0, "C2C {6,7,12}" }, 2155 { { .rank = 3, .n = { 6, 7, 12 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2C, .inverse = 1 }, 0, 2156 "C2C {6,7,12} inverse" }, 2157 { { .rank = 2, .n = { 12, 97 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2C }, 0, "C2C {12,97}" }, 2158 { { .rank = 2, .n = { 6, 10 }, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_R2C }, 0, "R2C {6,10}" }, 2159 { { .rank = 3, .n = { 5, 4, 6 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_R2C }, 0, "R2C {5,4,6}" }, 2160 { { .rank = 2, .n = { 12, 9 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_R2C }, 0, "R2C {12,9}" }, 2161 { { .rank = 2, .n = { 6, 10 }, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_C2R }, 0, "C2R {6,10}" }, 2162 { { .rank = 3, .n = { 5, 4, 6 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2R }, 0, "C2R {5,4,6}" }, 2163 { { .rank = 2, .n = { 12, 9 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2R }, 0, "C2R {12,9}" }, 2164 /* layouts */ 2165 { { .rank = 1, .n = { 12 }, .batch = 3, .type = OMEGA_GPU_FFT_TYPE_R2C }, 1, "L1 R2C 12 basic in place" }, 2166 { { .rank = 1, .n = { 12 }, .batch = 3, .type = OMEGA_GPU_FFT_TYPE_C2R }, 1, "L2 C2R 12 basic in place" }, 2167 { { .rank = 2, .n = { 6, 10 }, .inembed = { 6, 13 }, .istride = 2, .idist = 161, .onembed = { 7, 11 }, 2168 .ostride = 1, .odist = 77, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_C2C }, 2169 0, "L3 C2C {6,10} embedded strided" }, 2170 { { .rank = 1, .n = { 7 }, .inembed = { 7 }, .istride = 4, .idist = 1, .onembed = { 7 }, .ostride = 1, 2171 .odist = 7, .batch = 4, .type = OMEGA_GPU_FFT_TYPE_C2C, .inverse = 1 }, 2172 0, "L4 C2C 7 interleaved batches" }, 2173 { { .rank = 2, .n = { 5, 8 }, .inembed = { 5, 9 }, .istride = 1, .idist = 50, .onembed = { 5, 5 }, 2174 .ostride = 3, .odist = 80, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_R2C }, 2175 0, "L5 R2C {5,8} embedded" }, 2176 { { .rank = 1, .n = { 97 }, .inembed = { 49 }, .istride = 2, .idist = 100, .onembed = { 97 }, .ostride = 1, 2177 .odist = 101, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_C2R }, 2178 0, "L6 C2R 97 strided" }, 2179 { { .rank = 3, .n = { 3, 4, 5 }, .batch = 2, .type = OMEGA_GPU_FFT_TYPE_C2C }, 1, "L7 C2C {3,4,5} in place" }, 2180 }; 2181 const size_t ncase = sizeof cases / sizeof cases[0]; 2182 const uint32_t sched[3] = { 0, 1, 0x9E3779B9u }; 2183 for (size_t c = 0; c < ncase; c++) { 2184 const OmegaGpuFftMany *p = &cases[c].p; 2185 TLay ti, to; 2186 tlay(p, cases[c].in_place, 0, &ti); 2187 tlay(p, cases[c].in_place, 1, &to); 2188 size_t nx = tlay_points(&ti) * (ti.cplx ? 2 : 1) * p->batch, ny = tlay_points(&to) * (to.cplx ? 2 : 1) * p->batch; 2189 float *x = malloc(nx * 4), *y = malloc(ny * 4), *y2 = malloc(ny * 4); 2190 double *ref = malloc(ny * sizeof *ref); 2191 many_input(p, x); 2192 many_ref(p, x, ref); 2193 double tol = many_tol(p); 2194 ManyRun r = sim_many_run(p, cases[c].in_place, 0, 0, sched[0], x, ref, y); 2195 int same = 1; 2196 for (int s = 1; s < 3 && r.rc == OMEGA_GPU_ML_OK; s++) { 2197 ManyRun r2 = sim_many_run(p, cases[c].in_place, 0, 0, sched[s], x, ref, y2); 2198 same &= r2.rc == OMEGA_GPU_ML_OK && !memcmp(y, y2, ny * 4); 2199 } 2200 check(r.rc == OMEGA_GPU_ML_OK && r.err >= 0 && r.err <= tol, "sim many %s: rc %d error %.4e, tolerance %.4e", 2201 cases[c].what, r.rc, r.err, tol); 2202 check(same, "sim many %s: output depends on the warp order", cases[c].what); 2203 check(r.unwritten == 0 && r.gaps_changed == 0 && r.input_changed == 0, 2204 "sim many %s: %zu outputs unwritten, %zu gap words changed, input changed %zu", cases[c].what, r.unwritten, 2205 r.gaps_changed, r.input_changed); 2206 receipt("sim many case=\"%s\" batch=%u in_place=%d launches=%zu normwise_err=%.4e tol=%.4e unwritten=%zu " 2207 "gaps_changed=%zu input_changed=%zu warp_orders_identical=%d", 2208 cases[c].what, p->batch, cases[c].in_place, r.launches, r.err, tol, r.unwritten, r.gaps_changed, 2209 r.input_changed, same); 2210 free(x); free(y); free(y2); free(ref); 2211 } 2212 /* mutants of kinds 7, 8 and 9 (see the header): each must break its case; 9/2 drops the source batch stride, 2213 * invisible with one transform (the expected miss) */ 2214 static const struct { ManyCase c; uint32_t kind, mut; int caught; } mm[] = { 2215 { MC1(C2C, 12, 0), 7, 1, 1 }, 2216 { MC1(C2C, 12, 0), 7, 2, 1 }, 2217 { MC1(C2C, 97, 0), 8, 1, 1 }, 2218 { MC1(C2C, 97, 0), 8, 2, 1 }, 2219 { MC1(C2R, 12, 0), 9, 1, 1 }, 2220 { MC1(C2C, 6, 0), 9, 2, 1 }, 2221 { { { .rank = 1, .n = { 6 }, .batch = 1, .type = OMEGA_GPU_FFT_TYPE_C2C }, 0, "C2C 6 batch 1" }, 9, 2, 0 }, 2222 }; 2223 for (size_t i = 0; i < sizeof mm / sizeof mm[0]; i++) { 2224 const OmegaGpuFftMany *p = &mm[i].c.p; 2225 TLay ti, to; 2226 tlay(p, 0, 0, &ti); 2227 tlay(p, 0, 1, &to); 2228 size_t nx = tlay_points(&ti) * (ti.cplx ? 2 : 1) * p->batch, ny = tlay_points(&to) * (to.cplx ? 2 : 1) * p->batch; 2229 float *x = malloc(nx * 4), *y = malloc(ny * 4); 2230 double *ref = malloc(ny * sizeof *ref); 2231 many_input(p, x); 2232 many_ref(p, x, ref); 2233 double tol = many_tol(p); 2234 ManyRun r = sim_many_run(p, 0, mm[i].kind, mm[i].mut, 0, x, ref, y); 2235 int caught = r.rc != OMEGA_GPU_ML_OK || !(r.err <= tol); 2236 check(caught == mm[i].caught, "sim many mutant %u/%u on %s: error %.4e, %s", mm[i].kind, mm[i].mut, 2237 mm[i].c.what, r.err, caught ? "caught" : "not caught"); 2238 receipt("sim many_mutant kind=%u level=%u case=\"%s\" batch=%u normwise_err=%.4e tol=%.4e caught=%d expected=%d", 2239 mm[i].kind, mm[i].mut, mm[i].c.what, p->batch, r.err, tol, caught, mm[i].caught); 2240 free(x); free(y); free(ref); 2241 } 2242 } 2243 #undef MC1 2244 2245 static void sim_tier(void) { 2246 g_prog = calloc(1, sizeof *g_prog); 2247 g_code = malloc(OMEGA_BW_CODE_CAP_MAX); 2248 sim_rng_tier(); 2249 sim_fft_tier(); 2250 sim_philox_tier(); 2251 sim_real_tier(); 2252 sim_nd_tier(); 2253 sim_many_tier(); 2254 free(g_prog); 2255 free(g_code); 2256 } 2257 2258 /* ------------------------------------------------------------------ chip tier */ 2259 2260 static void chip_rng(void) { 2261 const uint32_t ns = 4096; 2262 uint64_t *got = malloc((size_t)ns * 17 * 8), *ref = malloc((size_t)ns * 17 * 8); 2263 float *fgot = malloc((size_t)ns * 17 * 4); 2264 OmegaGpuMathInfo info; 2265 size_t fix_used = 0, fix_bad = 0; 2266 memset(g_fix_hit, 0, sizeof g_fix_hit); 2267 for (int b = 0; b < 3; b++) 2268 for (int o = 0; o < 2; o++) { 2269 uint32_t offset = o ? 1000 : 0, per = o ? 16 : 17, cnt = ns * per; 2270 ref_raw(BASES[b], 0, ns, offset, per, ref); 2271 int rc = omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, BASES[b], 0, ns, offset, per, got, &info); 2272 check(rc == OMEGA_GPU_ML_OK, "chip raw base %" PRIu64 " offset %u: rc %d (%s)", BASES[b], offset, rc, 2273 omega_gpu_mathlibs_last_error()); 2274 if (rc != OMEGA_GPU_ML_OK) continue; 2275 size_t bad = 0; 2276 for (uint32_t i = 0; i < cnt; i++) bad += got[i] != ref[i]; 2277 fix_used += fixture_compare(got, BASES[b], ns, offset, per, &fix_bad); 2278 check(bad == 0, "chip raw base %" PRIu64 " offset %u: %zu of %u differ", BASES[b], offset, bad, cnt); 2279 receipt("chip rng_raw base=%" PRIu64 " ns=%u offset=%u per=%u mismatches=%zu unwritten=%" PRIu64 2280 " elapsed_ns=%" PRIu64 " marker=%u", 2281 BASES[b], ns, offset, per, bad, info.unwritten_words, info.elapsed_ns, info.completion_marker); 2282 } 2283 size_t fix_distinct = fixture_distinct(); 2284 check(fix_distinct == g_nfix && fix_bad == 0, "chip fixture: %zu of %zu rows covered, %zu of %zu comparisons differ", 2285 fix_distinct, g_nfix, fix_bad, fix_used); 2286 receipt("chip rng_fixture rows_covered=%zu of %zu comparisons=%zu mismatches=%zu", fix_distinct, g_nfix, fix_used, 2287 fix_bad); 2288 /* a wrapping stream0 */ 2289 ref_raw(0x123, 0xFFFFFFFFFFFFFF9Cull, 300, 7, 5, ref); 2290 if (omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0x123, 0xFFFFFFFFFFFFFF9Cull, 300, 7, 5, got, &info) == OMEGA_GPU_ML_OK) { 2291 size_t bad = 0; 2292 for (uint32_t i = 0; i < 1500; i++) bad += got[i] != ref[i]; 2293 check(bad == 0, "chip raw wrapping stream0: %zu differ", bad); 2294 receipt("chip rng_raw seed=291 stream0=2^64-100 ns=300 offset=7 per=5 mismatches=%zu", bad); 2295 } else check(0, "chip raw wrapping stream0 failed (%s)", omega_gpu_mathlibs_last_error()); 2296 /* uniform and normal at base 1729 */ 2297 ref_raw(1729, 0, ns, 0, 17, ref); 2298 if (omega_gpu_rng_generate(OMEGA_GPU_RNG_UNIFORM, 1729, 0, ns, 0, 17, fgot, &info) == OMEGA_GPU_ML_OK) { 2299 size_t bad = 0; 2300 for (uint32_t i = 0; i < ns * 17; i++) bad += f2u(fgot[i]) != f2u(uniform_of(ref[i])); 2301 check(bad == 0, "chip uniform: %zu differ", bad); 2302 receipt("chip rng_uniform base=1729 n=%u mismatches=%zu", ns * 17, bad); 2303 } else check(0, "chip uniform failed (%s)", omega_gpu_mathlibs_last_error()); 2304 if (omega_gpu_rng_generate(OMEGA_GPU_RNG_NORMAL, 1729, 0, ns, 0, 17, fgot, &info) == OMEGA_GPU_ML_OK) { 2305 size_t over = 0, differ = 0; 2306 double worst = 0; 2307 for (uint32_t i = 0; i < ns * 17; i++) { 2308 uint32_t A = normal_A(ref[i]); 2309 double e = rel_err(fgot[i], normal_ref(A)); 2310 over += !(e <= NORMAL_TOL); 2311 if (!(e <= worst)) worst = e; 2312 differ += f2u(fgot[i]) != f2u(normal_model(A)); 2313 } 2314 check(over == 0, "chip normal: %zu past the bound (worst %.4e)", over, worst); 2315 receipt("chip rng_normal base=1729 n=%u over_tol=%zu worst_rel=%.6e tol=%.3g differ_from_exact_mufu_model=%zu", 2316 ns * 17, over, worst, NORMAL_TOL, differ); 2317 } else check(0, "chip normal failed (%s)", omega_gpu_mathlibs_last_error()); 2318 free(got); free(ref); free(fgot); 2319 /* the full launch: 131072 streams x 64 normals, and the same as uniforms */ 2320 const uint32_t big = OMEGA_GPU_RNG_MAX_STREAMS, per = 64; 2321 size_t cnt = (size_t)big * per; 2322 uint64_t *r2 = malloc(cnt * 8); 2323 float *f2 = malloc(cnt * 4); 2324 ref_raw(42, 0, big, 0, per, r2); 2325 for (int kind = 1; kind <= 2; kind++) { 2326 int rc = omega_gpu_rng_generate((OmegaGpuRngKind)kind, 42, 0, big, 0, per, f2, &info); 2327 check(rc == OMEGA_GPU_ML_OK, "chip big kind %d rc %d (%s)", kind, rc, omega_gpu_mathlibs_last_error()); 2328 if (rc != OMEGA_GPU_ML_OK) continue; 2329 size_t bad = 0; 2330 double worst = 0; 2331 for (size_t i = 0; i < cnt; i++) { 2332 if (kind == 1) { bad += f2u(f2[i]) != f2u(uniform_of(r2[i])); continue; } 2333 double e = rel_err(f2[i], normal_ref(normal_A(r2[i]))); 2334 bad += !(e <= NORMAL_TOL); 2335 if (!(e <= worst)) worst = e; 2336 } 2337 check(bad == 0, "chip big kind %d: %zu bad", kind, bad); 2338 receipt("chip rng_big kind=%d ns=%u per=%u n=%zu bad=%zu worst_rel=%.6e elapsed_ns=%" PRIu64 " ctas=%u marker=%u", 2339 kind, big, per, cnt, bad, worst, info.elapsed_ns, info.ctas_last_launch, info.completion_marker); 2340 } 2341 free(r2); free(f2); 2342 /* mutants */ 2343 uint64_t *mg = malloc(256 * 17 * 8), *mr = malloc(256 * 17 * 8); 2344 float *mf = malloc(256 * 17 * 4); 2345 ref_raw(0, 0, 256, 0, 17, mr); 2346 omega_gpu_mathlibs_test_set_mutant(1, 1); 2347 if (omega_gpu_rng_generate(OMEGA_GPU_RNG_U64, 0, 0, 256, 0, 17, mg, NULL) == OMEGA_GPU_ML_OK) { 2348 size_t bad = 0; 2349 for (uint32_t i = 0; i < 256 * 17; i++) bad += mg[i] != mr[i]; 2350 check(bad > 0, "chip rng mutant 1 not caught"); 2351 receipt("chip rng_mutant1 mismatches=%zu of %u (must be > 0)", bad, 256 * 17); 2352 } else check(0, "chip rng mutant 1 run failed (%s)", omega_gpu_mathlibs_last_error()); 2353 omega_gpu_mathlibs_test_set_mutant(1, 2); 2354 if (omega_gpu_rng_generate(OMEGA_GPU_RNG_NORMAL, 0, 0, 256, 0, 17, mf, NULL) == OMEGA_GPU_ML_OK) { 2355 size_t over = 0; 2356 for (uint32_t i = 0; i < 256 * 17; i++) over += !(rel_err(mf[i], normal_ref(normal_A(mr[i]))) <= NORMAL_TOL); 2357 check(over > 0, "chip rng mutant 2 not caught"); 2358 receipt("chip rng_mutant2 over_tol=%zu of %u (must be > 0)", over, 256 * 17); 2359 } else check(0, "chip rng mutant 2 run failed (%s)", omega_gpu_mathlibs_last_error()); 2360 omega_gpu_mathlibs_test_set_mutant(0, 0); 2361 free(mg); free(mr); free(mf); 2362 } 2363 2364 static void chip_fft(void) { 2365 const uint32_t batch = 8; 2366 size_t cap = (size_t)2 * 256 * 2050; 2367 float *in = malloc(cap * 4), *out = malloc(cap * 4); 2368 OmegaGpuMathInfo info; 2369 for (int lg = 2; lg <= 11; lg++) 2370 for (int inv = 0; inv < 2; inv++) { 2371 uint32_t n = 1u << lg; 2372 fill_rand(in, (size_t)2 * n * batch); 2373 int rc = omega_gpu_fft_c2c(in, out, n, batch, inv, &info); 2374 check(rc == OMEGA_GPU_ML_OK, "chip fft n=%u inv=%d rc %d (%s)", n, inv, rc, omega_gpu_mathlibs_last_error()); 2375 if (rc != OMEGA_GPU_ML_OK) continue; 2376 double e = fft_err(in, out, n, batch, inv); 2377 check(e <= FFT_TOL[lg], "chip fft n=%u inv=%d: error %.4e, tolerance %.4e", n, inv, e, FFT_TOL[lg]); 2378 receipt("chip fft n=%u inverse=%d batch=%u normwise_err=%.4e tol=%.4e unwritten=%" PRIu64 " elapsed_ns=%" PRIu64, 2379 n, inv, batch, e, FFT_TOL[lg], info.unwritten_words, info.elapsed_ns); 2380 } 2381 /* a batch past one launch: 2050 transforms = 1024 + 1024 + 2 CTAs */ 2382 fill_rand(in, cap); 2383 int rc = omega_gpu_fft_c2c(in, out, 256, 2050, 0, &info); 2384 check(rc == OMEGA_GPU_ML_OK, "chip fft batch 2050 rc %d (%s)", rc, omega_gpu_mathlibs_last_error()); 2385 if (rc == OMEGA_GPU_ML_OK) { 2386 double e = fft_err(in, out, 256, 2050, 0); 2387 check(e <= FFT_TOL[8], "chip fft batch 2050: error %.4e", e); 2388 check(info.chip_calls == 3 && info.ctas_last_launch == 2, "chip fft batch 2050: %" PRIu64 " launches, last %u CTAs", 2389 info.chip_calls, info.ctas_last_launch); 2390 receipt("chip fft n=256 batch=2050 launches=%" PRIu64 " last_ctas=%u normwise_err=%.4e tol=%.4e elapsed_ns=%" PRIu64, 2391 info.chip_calls, info.ctas_last_launch, e, FFT_TOL[8], info.elapsed_ns); 2392 } 2393 const struct { uint32_t n; int mut; int caught; } mm[] = { { 64, 1, 1 }, { 2048, 1, 1 }, { 16, 2, 1 }, { 4, 2, 0 } }; 2394 for (size_t i = 0; i < sizeof mm / sizeof mm[0]; i++) { 2395 uint32_t n = mm[i].n; 2396 omega_gpu_mathlibs_test_set_mutant(2, mm[i].mut); 2397 fill_rand(in, (size_t)2 * n * batch); 2398 rc = omega_gpu_fft_c2c(in, out, n, batch, 0, NULL); 2399 check(rc == OMEGA_GPU_ML_OK, "chip fft mutant %d n=%u rc %d", mm[i].mut, n, rc); 2400 if (rc != OMEGA_GPU_ML_OK) continue; 2401 double e = fft_err(in, out, n, batch, 0); 2402 int caught = !(e <= FFT_TOL[lg2u(n)]); 2403 check(caught == mm[i].caught, "chip fft mutant %d n=%u: error %.4e", mm[i].mut, n, e); 2404 receipt("chip fft_mutant%d n=%u normwise_err=%.4e caught=%d expected=%d", mm[i].mut, n, e, caught, mm[i].caught); 2405 } 2406 omega_gpu_mathlibs_test_set_mutant(0, 0); 2407 free(in); free(out); 2408 } 2409 2410 static void chip_philox(void) { 2411 const uint32_t ns = 4096; 2412 uint32_t *got = malloc((size_t)ns * 68 * 4), *ref = malloc((size_t)ns * 68 * 4); 2413 OmegaGpuMathInfo info; 2414 size_t used = 0, fbad = 0; 2415 memset(g_ph_hit, 0, sizeof g_ph_hit); 2416 for (int s = 0; s < 4; s++) 2417 for (int o = 0; o < 4; o++) 2418 for (int r = 0; r < 2; r++) { 2419 uint64_t sub0 = r ? 0xFFFFFFFFull : 0; 2420 uint32_t n = r ? 2 : ns, per = r ? 8 : 68; 2421 ref_philox(PH_SEEDS[s], sub0, n, PH_OFFS[o], per, ref); 2422 int rc = omega_gpu_philox_generate(PH_SEEDS[s], sub0, n, PH_OFFS[o], per, got, &info); 2423 check(rc == OMEGA_GPU_ML_OK, "chip philox seed %" PRIu64 " offset %" PRIu64 ": rc %d (%s)", PH_SEEDS[s], 2424 PH_OFFS[o], rc, omega_gpu_mathlibs_last_error()); 2425 if (rc != OMEGA_GPU_ML_OK) continue; 2426 size_t bad = diff_words(got, ref, (size_t)n * per); 2427 used += ph_compare(got, PH_SEEDS[s], sub0, n, PH_OFFS[o], per, &fbad); 2428 check(bad == 0, "chip philox seed %" PRIu64 " offset %" PRIu64 ": %zu differ", PH_SEEDS[s], PH_OFFS[o], bad); 2429 receipt("chip philox seed=%" PRIu64 " subseq0=%" PRIu64 " ns=%u offset=%" PRIu64 " per=%u mismatches=%zu" 2430 " unwritten=%" PRIu64 " elapsed_ns=%" PRIu64, 2431 PH_SEEDS[s], sub0, n, PH_OFFS[o], per, bad, info.unwritten_words, info.elapsed_ns); 2432 } 2433 check(ph_distinct() == g_nph && fbad == 0, "chip philox cuRAND fixture: %zu of %zu rows covered, %zu of %zu differ", 2434 ph_distinct(), g_nph, fbad, used); 2435 receipt("chip philox_curand_fixture rows_covered=%zu of %zu comparisons=%zu mismatches=%zu", ph_distinct(), g_nph, 2436 used, fbad); 2437 /* the distributions: dists 0..3 bit for bit against the mirror; the normal within 2^-21 sqrt(-2 log u) of it (the 2438 * chip's MUFU.RSQ is modelled, not proven, bit-exact); covered cuRAND rows against cuRAND */ 2439 memset(g_pd_hit, 0, sizeof g_pd_hit); 2440 for (int d = 1; d <= 4; d++) { 2441 size_t dbad = 0, dused = 0, dfbad = 0; 2442 for (int s = 0; s < 4; s++) 2443 for (int o = 0; o < 7; o++) 2444 for (int r = 0; r < 2; r++) { 2445 uint64_t sub0 = r ? 0xFFFFFFFFull : 0; 2446 uint32_t n = r ? 2 : ns, per = r ? 8 : 34; 2447 ref_dist(d, PH_SEEDS[s], sub0, n, PD_OFFS[o], per, ref); 2448 int rc = omega_gpu_philox_generate_dist((OmegaGpuPhiloxDist)d, PH_SEEDS[s], sub0, n, PD_OFFS[o], per, 2449 got, &info); 2450 check(rc == OMEGA_GPU_ML_OK, "chip philox dist %d: rc %d (%s)", d, rc, omega_gpu_mathlibs_last_error()); 2451 if (rc != OMEGA_GPU_ML_OK) continue; 2452 if (d != OMEGA_GPU_PHILOX_NORMAL) dbad += diff_words(got, ref, (size_t)n * per * ph_esz(d)); 2453 else 2454 for (uint32_t g = 0; g < n; g++) 2455 for (uint32_t k = 0; k < per; k++) { 2456 size_t i = ph_index(d, n, per, g, k); 2457 PdRow row = { PH_SEEDS[s], sub0 + g, PD_OFFS[o], ref[i], (uint32_t)d, k }; 2458 dbad += !pd_agrees(&row, got[i]); 2459 } 2460 dused += pd_compare(d, got, PH_SEEDS[s], sub0, n, PD_OFFS[o], per, &dfbad); 2461 } 2462 check(dbad == 0 && pd_distinct(d) == 896 && dfbad == 0, 2463 "chip philox dist %d: %zu differ from the mirror; cuRAND %zu of 896 rows covered, %zu of %zu disagree", d, 2464 dbad, pd_distinct(d), dfbad, dused); 2465 receipt("chip philox_dist dist=%d mismatches=%zu curand_rows_covered=%zu curand_disagree=%zu", d, dbad, 2466 pd_distinct(d), dfbad); 2467 } 2468 free(got); free(ref); 2469 /* the full launch: 131072 streams x 64 words */ 2470 const uint32_t big = OMEGA_GPU_PHILOX_MAX_STREAMS, per = 64; 2471 uint32_t *g2 = malloc((size_t)big * per * 4), *r2 = malloc((size_t)big * per * 4); 2472 ref_philox(42, 0, big, 0, per, r2); 2473 int rc = omega_gpu_philox_generate(42, 0, big, 0, per, g2, &info); 2474 check(rc == OMEGA_GPU_ML_OK, "chip philox big rc %d (%s)", rc, omega_gpu_mathlibs_last_error()); 2475 if (rc == OMEGA_GPU_ML_OK) { 2476 size_t bad = diff_words(g2, r2, (size_t)big * per); 2477 check(bad == 0, "chip philox big: %zu differ", bad); 2478 receipt("chip philox_big ns=%u per=%u mismatches=%zu elapsed_ns=%" PRIu64 " ctas=%u marker=%u", big, per, bad, 2479 info.elapsed_ns, info.ctas_last_launch, info.completion_marker); 2480 } 2481 /* mutants */ 2482 for (int mut = 1; mut <= 6; mut++) { 2483 static const int md[7] = { 0, 0, 0, 1, 0, 4, 3 }; 2484 uint64_t off = 0; 2485 if (mut == 3) while (ph_word(42, 0, off) >= 1u << 23) off++; 2486 if (mut == 4) off = 1; 2487 omega_gpu_mathlibs_test_set_mutant(3, mut); 2488 rc = omega_gpu_philox_generate_dist((OmegaGpuPhiloxDist)md[mut], 42, 0, 256, off, 8, g2, NULL); 2489 ref_dist(md[mut], 42, 0, 256, off, 8, r2); 2490 size_t n = (size_t)256 * 8 * ph_esz(md[mut]); 2491 size_t bad = rc == OMEGA_GPU_ML_OK ? diff_words(g2, r2, n) : 0; 2492 check(bad > 0, "chip philox mutant %d not caught (rc %d)", mut, rc); 2493 receipt("chip philox_mutant%d dist=%d offset=%" PRIu64 " mismatches=%zu of %zu (must be > 0)", mut, md[mut], off, 2494 bad, n); 2495 } 2496 omega_gpu_mathlibs_test_set_mutant(0, 0); 2497 free(g2); free(r2); 2498 } 2499 2500 static void chip_real_nd(void) { 2501 const uint32_t batch = 8; 2502 size_t cap = (size_t)(4096 + 2) * 2050; 2503 float *in = malloc(cap * 4), *out = malloc(cap * 4); 2504 OmegaGpuMathInfo info; 2505 for (int lg = 3; lg <= 12; lg++) 2506 for (int c2r = 0; c2r < 2; c2r++) { 2507 uint32_t n = 1u << lg; 2508 fill_rand(in, (size_t)(n + 2) * batch); 2509 if (c2r) hermitian(in, n, batch); 2510 int rc = c2r ? omega_gpu_fft_c2r(in, out, n, batch, &info) : omega_gpu_fft_r2c(in, out, n, batch, &info); 2511 const char *nm = c2r ? "c2r" : "r2c"; 2512 check(rc == OMEGA_GPU_ML_OK, "chip %s n=%u rc %d (%s)", nm, n, rc, omega_gpu_mathlibs_last_error()); 2513 if (rc != OMEGA_GPU_ML_OK) continue; 2514 double e = c2r ? c2r_err(in, out, n, batch) : r2c_err(in, out, n, batch), tol = c2r ? C2R_TOL[lg] : R2C_TOL[lg]; 2515 check(e <= tol, "chip %s n=%u: error %.4e, tolerance %.4e", nm, n, e, tol); 2516 receipt("chip %s n=%u batch=%u normwise_err=%.4e tol=%.4e elapsed_ns=%" PRIu64, nm, n, batch, e, tol, 2517 info.elapsed_ns); 2518 } 2519 /* past one launch: 2050 rows of 4096 = 1024 + 1024 + 2 CTAs per pass */ 2520 fill_rand(in, (size_t)4096 * 2050); 2521 int rc = omega_gpu_fft_r2c(in, out, 4096, 2050, &info); 2522 check(rc == OMEGA_GPU_ML_OK, "chip r2c batch 2050 rc %d (%s)", rc, omega_gpu_mathlibs_last_error()); 2523 if (rc == OMEGA_GPU_ML_OK) { 2524 double e = r2c_err(in, out, 4096, 2050); 2525 check(e <= R2C_TOL[12] && info.chip_calls == 6 && info.ctas_last_launch == 2, 2526 "chip r2c batch 2050: error %.4e, %" PRIu64 " launches, last %u CTAs", e, info.chip_calls, 2527 info.ctas_last_launch); 2528 receipt("chip r2c n=4096 batch=2050 launches=%" PRIu64 " normwise_err=%.4e elapsed_ns=%" PRIu64, info.chip_calls, e, 2529 info.elapsed_ns); 2530 } 2531 for (size_t i = 0; i < N_ND_SHAPES; i++) 2532 for (int inv = 0; inv < 2; inv++) { 2533 const uint32_t *d = ND_SHAPES[i].d; 2534 uint32_t nd = ND_SHAPES[i].nd; 2535 size_t tot = (size_t)d[0] * (nd > 1 ? d[1] : 1) * (nd > 2 ? d[2] : 1); 2536 fill_rand(in, 2 * tot * 2); 2537 rc = omega_gpu_fft_c2c_nd(in, out, nd, d, 2, inv, &info); 2538 check(rc == OMEGA_GPU_ML_OK, "chip nd {%u %u %u} rc %d (%s)", d[0], d[1], d[2], rc, omega_gpu_mathlibs_last_error()); 2539 if (rc != OMEGA_GPU_ML_OK) continue; 2540 double e = nd_err(in, out, nd, d, 2, inv); 2541 check(e <= ND_SHAPES[i].tol, "chip nd {%u %u %u} inv=%d: error %.4e", d[0], d[1], d[2], inv, e); 2542 receipt("chip nd ndim=%u dims=%u,%u,%u inverse=%d normwise_err=%.4e tol=%.4e launches=%" PRIu64 2543 " elapsed_ns=%" PRIu64, 2544 nd, d[0], d[1], d[2], inv, e, ND_SHAPES[i].tol, info.chip_calls, info.elapsed_ns); 2545 } 2546 /* mutants: split twiddle, skipped bin (refused as unwritten), merge skipped bin, transposes */ 2547 const struct { int which, lvl, c2r; uint32_t n; } rm[] = { { 4, 1, 0, 64 }, { 4, 2, 0, 4096 }, { 4, 2, 1, 4096 } }; 2548 for (size_t i = 0; i < sizeof rm / sizeof rm[0]; i++) { 2549 uint32_t n = rm[i].n; 2550 omega_gpu_mathlibs_test_set_mutant(rm[i].which, rm[i].lvl); 2551 fill_rand(in, (size_t)(n + 2) * batch); 2552 if (rm[i].c2r) hermitian(in, n, batch); 2553 rc = rm[i].c2r ? omega_gpu_fft_c2r(in, out, n, batch, NULL) : omega_gpu_fft_r2c(in, out, n, batch, NULL); 2554 double e = rc != OMEGA_GPU_ML_OK ? -1 2555 : rm[i].c2r ? c2r_err(in, out, n, batch) 2556 : r2c_err(in, out, n, batch); 2557 double tol = rm[i].c2r ? C2R_TOL[lg2u(n)] : R2C_TOL[lg2u(n)]; 2558 int caught = rc == OMEGA_GPU_ML_UNWRITTEN || (rc == OMEGA_GPU_ML_OK && !(e <= tol)); 2559 check(caught, "chip real mutant %d/%d n=%u: rc %d error %.4e not caught", rm[i].which, rm[i].lvl, n, rc, e); 2560 receipt("chip real_mutant%d c2r=%d n=%u rc=%d normwise_err=%.4e caught=%d", rm[i].lvl, rm[i].c2r, n, rc, e, caught); 2561 } 2562 const uint32_t d2[2] = { 8, 16 }; 2563 for (int lvl = 1; lvl <= 2; lvl++) { 2564 omega_gpu_mathlibs_test_set_mutant(5, lvl); 2565 fill_rand(in, 2 * 128 * 2); 2566 rc = omega_gpu_fft_c2c_nd(in, out, 2, d2, 2, 0, NULL); 2567 double e = rc == OMEGA_GPU_ML_OK ? nd_err(in, out, 2, d2, 2, 0) : -1; 2568 int caught = rc == OMEGA_GPU_ML_UNWRITTEN || (rc == OMEGA_GPU_ML_OK && !(e <= 9.`928263778e`-7)); 2569 check(caught, "chip transpose mutant %d: rc %d error %.4e not caught", lvl, rc, e); 2570 receipt("chip transpose_mutant%d rc=%d normwise_err=%.4e caught=%d", lvl, rc, e, caught); 2571 } 2572 omega_gpu_mathlibs_test_set_mutant(0, 0); 2573 free(in); free(out); 2574 } 2575 2576 int main(int argc, char **argv) { 2577 const char *tier = "chip", *fixture = "tests/fixtures/mathlibs_rng_aienmath.txt", *out = NULL, 2578 *phfix = "tests/fixtures/philox_curand_oracle.txt", *pdfix = "tests/fixtures/philox_curand_dist_oracle.txt"; 2579 for (int i = 1; i < argc; i++) { 2580 if (!strcmp(argv[i], "--host-only")) tier = "host"; 2581 else if (!strcmp(argv[i], "--sim")) tier = "sim"; 2582 else if (!strcmp(argv[i], "--fixture") && i + 1 < argc) fixture = argv[++i]; 2583 else if (!strcmp(argv[i], "--philox-fixture") && i + 1 < argc) phfix = argv[++i]; 2584 else if (!strcmp(argv[i], "--dist-fixture") && i + 1 < argc) pdfix = argv[++i]; 2585 else if (!strcmp(argv[i], "--out") && i + 1 < argc) out = argv[++i]; 2586 else { 2587 fprintf(stderr, "usage: %s [--host-only | --sim] [--fixture F] [--philox-fixture F] [--dist-fixture F] [--out FILE]\n", 2588 argv[0]); 2589 return 2; 2590 } 2591 } 2592 if (out && !(g_rcpt = test_out_open(out))) { fprintf(stderr, "cannot write %s\n", out); return 2; } 2593 if (load_fixture(fixture) != 0) { fprintf(stderr, "cannot read fixture %s\n", fixture); return 2; } 2594 if (load_philox_fixture(phfix) != 0) { fprintf(stderr, "cannot read Philox fixture %s\n", phfix); return 2; } 2595 if (load_dist_fixture(pdfix) != 0) { fprintf(stderr, "cannot read Philox distribution fixture %s\n", pdfix); return 2; } 2596 receipt("mathlibs tier=%s fixture=%s philox_fixture=%s dist_fixture=%s", tier, fixture, phfix, pdfix); 2597 host_tier(); 2598 if (!strcmp(tier, "sim")) sim_tier(); 2599 else if (!strcmp(tier, "chip")) { chip_rng(); chip_fft(); chip_philox(); chip_real_nd(); } 2600 if (g_failed) receipt("mathlibs %s: FAIL (%d of %d)", tier, g_failed, g_checks); 2601 else receipt("mathlibs %s: PASS (%d checks)", tier, g_checks); 2602 if (g_rcpt) fclose(g_rcpt); 2603 return g_failed ? 1 : 0; 2604 }