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/ggml-omega/blob/main/tests/kernels.c # ggml-omega aien-dev/ggml-omega 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 `ggml-omega / tests / kernels.c` 138 lines ยท 7051 bytes Raw Blame History 1 // tests/kernels.c: test infrastructure (not shipped). Checks the Omega math units against independent 2 // oracles on this host: the C compiler's _Float16 conversions (AArch64 FCVT), glibc libm in double 3 // precision, and plain C reference loops. Prints one line per check; exit 1 on any failure. 4 #include 5 #include 6 #include 7 #include 8 9 #define ARR(p) const void *p, uint64_t p##_n 10 #define OUT(p) void *p, uint64_t p##_n 11 uint32_t omega_h2fv_1(uint64_t *r, ARR(s), OUT(d)); 12 uint32_t omega_f2hv_1(uint64_t *r, ARR(s), OUT(d)); 13 uint32_t omega_sexp_1(uint64_t *r, uint64_t acc, OUT(w), uint64_t mx); 14 uint32_t omega_rtsc(uint64_t *r, uint64_t base, uint64_t nd); 15 uint32_t omega_rope(uint64_t *r, uint64_t xy, uint64_t pos, uint64_t i, uint64_t ts, uint64_t fa); 16 uint32_t omega_silu_1(uint64_t *r, ARR(a), OUT(d)); 17 uint32_t omega_rmsc(uint64_t *r, uint64_t acc, uint64_t n, uint64_t eps); 18 uint32_t omega_dq8_1(uint64_t *r, uint64_t acc, ARR(w), ARR(x), uint64_t fin); 19 uint32_t omega_bfs_1(uint64_t *r, ARR(s), OUT(h), OUT(l)); 20 21 static int fails; 22 static uint32_t fb(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 23 static float bf(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 24 // distance in f32 ulps (ordered integer line), NaN only matches NaN 25 static int64_t ulps(float a, float b) { 26 if (isnan(a) || isnan(b)) return isnan(a) && isnan(b) ? 0 : INT64_MAX; 27 int64_t x = (int32_t) fb(a), y = (int32_t) fb(b); 28 if (x < 0) x = INT32_MIN - x; 29 if (y < 0) y = INT32_MIN - y; 30 return x > y ? x - y : y - x; 31 } 32 static void report(const char *name, long n, long bad, int64_t worst, int64_t limit) { 33 printf("%-28s n=%-9ld bad=%-6ld worst_ulp=%lld limit=%lld %s\n", name, n, bad, (long long) worst, (long long) limit, 34 bad ? "FAIL" : "PASS"); 35 if (bad) fails++; 36 } 37 #define OK(c) do { if ((c) != 0) { printf("status fail: %s\n", #c); return 1; } } while (0) 38 39 int main(void) { 40 uint64_t r; 41 long n = 0, bad = 0; int64_t worst = 0; 42 // f16 -> f32, all 65536 inputs, exact 43 for (uint32_t h = 0; h < 65536; h++) { 44 uint16_t hv = (uint16_t) h; _Float16 hf; memcpy(&hf, &hv, 2); 45 float o; OK(omega_h2fv_1(&r, &hv, 1, &o, 1)); 46 float want = (float) hf; n++; 47 if (!(isnan(want) ? isnan(o) : fb(o) == fb(want))) bad++; 48 } 49 report("h2f all f16", n, bad, 0, 0); 50 // f32 -> f16 (round to nearest even), every 1021st f32 bit pattern plus all specials, exact (NaN: any NaN) 51 n = bad = 0; 52 for (uint64_t u = 0; u < 4294967296ull; u += 1021) { 53 float x = bf((uint32_t) u); uint16_t o; OK(omega_f2hv_1(&r, &x, 1, &o, 1)); 54 _Float16 w = (_Float16) x; uint16_t wv; memcpy(&wv, &w, 2); n++; 55 if (isnan(x) ? ((o & 0x7c00) != 0x7c00 || (o & 0x3ff) == 0) : o != wv) bad++; 56 } 57 report("f2h f32 stride 1021", n, bad, 0, 0); 58 // exp: the sexp unit returns f32(exp(x - 0)); oracle double exp rounded to f32 59 n = bad = 0; worst = 0; 60 for (float x = -110.0f; x < 89.0f; x += 0.00731f) { 61 float w = x; uint64_t s = 0; OK(omega_sexp_1(&s, 0, &w, 1, 0)); 62 float want = (float) exp((double) x); int64_t d = ulps(w, want); n++; 63 if (d > worst) worst = d; 64 if (d > 1) bad++; 65 } 66 report("exp vs libm (f32 ulp)", n, bad, worst, 1); 67 // powf(base, -2/nd) for rope theta scale 68 n = bad = 0; worst = 0; 69 const float bases[] = { 10000.0f, `1000000`.0f, 500000.0f, 10.0f, 1.5f }; 70 for (int b = 0; b < 5; b++) 71 for (uint64_t nd = 2; nd <= 512; nd += 2) { 72 uint64_t ts; OK(omega_rtsc(&ts, fb(bases[b]), nd)); 73 float want = (float) pow((double) bases[b], (double) (-2.0f / (float) nd)); 74 int64_t d = ulps(bf((uint32_t) ts), want); n++; 75 if (d > worst) worst = d; 76 if (d > 1) bad++; 77 } 78 report("pow vs libm (f32 ulp)", n, bad, worst, 1); 79 // rope pair: oracle = same f32 theta recurrence, double cos/sin rounded to f32 80 n = bad = 0; worst = 0; 81 const float ts = 0.`8659643f`, fs = 1.0f, af = 1.0f, x0 = 0.75f, x1 = -1.25f; 82 for (int32_t pos = -3; pos < 4100; pos += 7) 83 for (uint64_t i = 0; i < 64; i++) { 84 float t = (float) pos; 85 for (uint64_t k = 0; k < i; k++) t *= ts; 86 t *= fs; 87 float c = (float) cos((double) t) * af, s = (float) sin((double) t) * af; 88 float w0 = x0 * c - x1 * s, w1 = x0 * s + x1 * c; 89 uint64_t o; OK(omega_rope(&o, fb(x0) | ((uint64_t) fb(x1) << 32), (uint32_t) pos, i, fb(ts), fb(fs) | ((uint64_t) fb(af) << 32))); 90 int64_t d0 = ulps(bf((uint32_t) o), w0), d1 = ulps(bf((uint32_t) (o >> 32)), w1); 91 int64_t d = d0 > d1 ? d0 : d1; n++; 92 if (d > worst) worst = d; 93 if (d > 0) bad++; 94 } 95 report("rope vs libm sin/cos", n, bad, worst, 0); 96 // silu: x/(1+expf(-x)) with libm-exact expf 97 n = bad = 0; worst = 0; 98 for (float x = -90.0f; x < 90.0f; x += 0.0137f) { 99 float o; OK(omega_silu_1(&r, &x, 1, &o, 1)); 100 float want = x / (1.0f + (float) exp(-(double) x)); int64_t d = ulps(o, want); n++; 101 if (d > worst) worst = d; 102 if (d > 1) bad++; 103 } 104 report("silu vs libm", n, bad, worst, 1); 105 // rms scale: 1/sqrtf(f32(sum/n) + eps) 106 n = bad = 0; 107 for (int k = 1; k < 5000; k++) { 108 double sum = k * 0.37; uint64_t sb; memcpy(&sb, &sum, 8); uint64_t o; 109 OK(omega_rmsc(&o, sb, 896, fb(1e-6f))); 110 float want = 1.0f / sqrtf((float) (sum / 896) + 1e-6f); n++; 111 if (fb(bf((uint32_t) o)) != fb(want)) bad++; 112 } 113 report("rms scale exact", n, bad, 0, 0); 114 // q8_0 dot: one block, reference f64 accumulation of f32(q)*x then times f16 d 115 n = bad = 0; 116 for (int t = 0; t < 2000; t++) { 117 unsigned char blk[34]; float x[32]; uint32_t seed = 2654435761u * (uint32_t) (t + 1); 118 _Float16 d = (_Float16) (0.001f * (float) (t % 97 + 1)); memcpy(blk, &d, 2); 119 for (int j = 0; j < 32; j++) { seed = seed * 1103515245u + 12345u; blk[2 + j] = (unsigned char) (seed >> 16); x[j] = (float) ((int) (seed >> 8) % 2001 - 1000) / 333.0f; } 120 float bs = 0.0f; for (int j = 0; j < 32; j++) bs += (float) (int8_t) blk[2 + j] * x[j]; 121 float want = (float) (double) ((float) d * bs); 122 uint64_t o; OK(omega_dq8_1(&o, 0, blk, 34, x, 32, 1)); n++; 123 if (fb(bf((uint32_t) o)) != fb(want)) bad++; 124 } 125 report("q8_0 block dot exact", n, bad, 0, 0); 126 // f32 -> bf16 hi + lo split: plain C reference (truncation), every 1021st finite f32 bit pattern, exact 127 n = bad = 0; 128 for (uint64_t u = 0; u < 4294967296ull; u += 1021) { 129 float x = bf((uint32_t) u); 130 if (!isfinite(x)) continue; 131 uint16_t h, l; OK(omega_bfs_1(&r, &x, 1, &h, 1, &l, 1)); 132 uint16_t wh = (uint16_t) (u >> 16), wl = (uint16_t) (fb(x - bf((uint32_t) u & 0xffff0000u)) >> 16); n++; 133 if (h != wh || l != wl) bad++; 134 } 135 report("bf16 hi/lo split exact", n, bad, 0, 0); 136 printf("%s\n", fails ? "KERNELS FAIL" : "KERNELS PASS"); 137 return fails ? 1 : 0; 138 }