mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-06 00:08:52 +00:00
engine/src/quant/arm/dot_neon_clean.cpp table + optimised Q8_0, Q4_K, Q5_K, IQ4_XS dots (replaces dot_neon.cpp) engine/src/quant/arm/arm_act_clean.inc Q8_0 / Q8_1 / Q8_K quantisers (replaces arm_act.inc) engine/cmake/backends/clean_p2_neon.cmake tests for arm64 engine/tests/clean_p2/neon_* probes (FPRev-style tree discovery, model scans), comparison tests, bench, build script The float orders were found by black-box probing of the current kernels on an Apple M4 (Apple clang): 1.2 million random rows per kernel plus non-finite rows are bit-identical to the in-TU oracles, the quantisers are byte-identical to the current ones on 600000 cases. Written from phaseA-tests/clean/P2-INTERFACES.md, FORMAT-SPEC.md and the allowed glue headers only.
119 lines
6.0 KiB
C++
119 lines
6.0 KiB
C++
// neon_act_test: the NEON activation quantisers (arm_act_clean.inc, as strata::quant::neon::quantize_q8_{0,1,K}) against
|
|
// Q8_0 / Q8_1 : a scalar model of FORMAT-SPEC Part II section 19 (row Neon): d = amax / 127, id = 1 / d (0 for d = 0), q = half-even(x * id), d stored as half(d),
|
|
// Q8_1 adds s = half(d * sum q)
|
|
// Q8_K : quantize_q8_K_ref of the layer (the Generic reference; the NEON code is bit-exact to it for the build's compiler)
|
|
// on random blocks with ties, zeros, signed zeros, subnormals, equal maxima of both signs, huge values and infinities. Buffers are zero-filled so a block whose
|
|
// bsums the all-zero case leaves alone compares equal. Exit 0 = every byte of every case equal.
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
#include "strata/quant/act.hpp"
|
|
|
|
namespace strata::quant::neon {
|
|
void quantize_q8_0(const float*, void*, int64_t);
|
|
void quantize_q8_1(const float*, void*, int64_t);
|
|
void quantize_q8_K(const float*, void*, int64_t);
|
|
} // namespace strata::quant::neon
|
|
|
|
using namespace strata::quant;
|
|
|
|
static uint16_t f2h(float f) { __fp16 t = (__fp16)f; uint16_t u; std::memcpy(&u, &t, 2); return u; }
|
|
|
|
static void model_q8_0_1(const float* x, uint8_t* y, int64_t k, bool q81) {
|
|
const int bytes = q81 ? 36 : 34;
|
|
for (int64_t b = 0; b < k / 32; ++b) {
|
|
const float* xb = x + 32 * b;
|
|
float amax = 0;
|
|
for (int i = 0; i < 32; ++i) amax = std::fmax(amax, std::fabs(xb[i]));
|
|
const float d = amax / 127.0f;
|
|
const float id = d != 0.0f ? 1.0f / d : 0.0f;
|
|
uint8_t* yb = y + bytes * b;
|
|
const uint16_t dh = f2h(d);
|
|
std::memcpy(yb, &dh, 2);
|
|
int sum = 0;
|
|
for (int i = 0; i < 32; ++i) {
|
|
const int q = (int)std::nearbyintf(xb[i] * id);
|
|
yb[(q81 ? 4 : 2) + i] = (uint8_t)(int8_t)q;
|
|
sum += q;
|
|
}
|
|
if (q81) { const uint16_t sh = f2h(d * (float)sum); std::memcpy(yb + 2, &sh, 2); }
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
const long N = argc > 1 ? std::atol(argv[1]) : 300000;
|
|
std::mt19937_64 rng(99);
|
|
std::normal_distribution<float> nd(0.f, 1.f);
|
|
long bad[3] = {0, 0, 0}, cnt[3] = {0, 0, 0};
|
|
const ActKernel* k0 = act_kernel(Type::Q8_0, Arith::Neon, Isa::Neon);
|
|
const ActKernel* k1 = act_kernel(Type::Q8_1, Arith::Neon, Isa::Neon);
|
|
const ActKernel* kK = act_kernel(Type::Q8_K, Arith::Neon, Isa::Neon);
|
|
if (!k0 || !k1 || !kK) { std::printf("FAIL: the dispatcher has no Neon activation kernel\n"); return 1; }
|
|
for (long it = 0; it < N; ++it) {
|
|
const int kind = (int)(rng() % 3);
|
|
const int bs = kind == 2 ? 256 : 32;
|
|
const int nblk = 1 + (int)(rng() % 3);
|
|
std::vector<float> x((size_t)bs * nblk);
|
|
const int mode = (int)(rng() % 14);
|
|
for (int b = 0; b < nblk; ++b) {
|
|
float* xb = &x[(size_t)bs * b];
|
|
const float scale = std::ldexp(1.0f, (int)(rng() % 40) - 25) * (1.0f + (float)(rng() % 1000) / 777.0f);
|
|
for (int i = 0; i < bs; ++i) {
|
|
switch (mode) {
|
|
case 0: xb[i] = nd(rng) * scale; break;
|
|
case 1: xb[i] = 0.0f; break;
|
|
case 2: xb[i] = (i & 1) ? -0.0f : 0.0f; break;
|
|
case 3: xb[i] = ((float)((int)(rng() % 255) - 127) + 0.5f * (float)(rng() % 2)) * scale; break;
|
|
case 4: xb[i] = scale; break;
|
|
case 5: xb[i] = nd(rng) * scale; break;
|
|
case 6: xb[i] = std::ldexp(nd(rng), -140); break;
|
|
case 7: xb[i] = nd(rng) * 1e30f; break;
|
|
case 8: xb[i] = ((float)((int)(rng() % 255) - 127)) * scale + 0.5f * scale; break;
|
|
case 9: xb[i] = (rng() % 8 == 0) ? nd(rng) * scale : 0.0f; break;
|
|
case 10: xb[i] = nd(rng) * scale; break;
|
|
case 11: xb[i] = nd(rng) * scale; break;
|
|
case 12: xb[i] = nd(rng) * scale; break;
|
|
default: xb[i] = (float)((int)(rng() % 255) - 127) * 0.37f * scale; break;
|
|
}
|
|
}
|
|
if (mode == 5) { xb[3] = 5.0f * scale; xb[bs - 2] = -5.0f * scale; } // maximum twice, opposite signs
|
|
if (mode == 10) xb[(int)(rng() % bs)] = -9.0f * scale;
|
|
if (mode == 11) xb[(int)(rng() % bs)] = INFINITY;
|
|
if (mode == 12) xb[(int)(rng() % bs)] = -INFINITY;
|
|
}
|
|
const size_t ybytes = (size_t)(kind == 0 ? 34 : kind == 1 ? 36 : 292) * nblk;
|
|
std::vector<uint8_t> got(ybytes, 0), want(ybytes, 0);
|
|
if (kind == 0) { k0->fn(x.data(), got.data(), (int64_t)x.size()); model_q8_0_1(x.data(), want.data(), (int64_t)x.size(), false); }
|
|
else if (kind == 1) { k1->fn(x.data(), got.data(), (int64_t)x.size()); model_q8_0_1(x.data(), want.data(), (int64_t)x.size(), true); }
|
|
else { kK->fn(x.data(), got.data(), (int64_t)x.size()); quantize_q8_K_ref(x.data(), want.data(), (int64_t)x.size()); }
|
|
++cnt[kind];
|
|
if (got != want && ++bad[kind] <= 2) std::printf(" MISMATCH kind %d mode %d case %ld\n", kind, mode, it);
|
|
}
|
|
const char* nm[3] = {"Q8_0", "Q8_1", "Q8_K"};
|
|
int fail = 0;
|
|
for (int i = 0; i < 3; ++i) {
|
|
std::printf("%s %s: %ld cases, %ld differ\n", bad[i] ? "FAIL" : "ok ", nm[i], cnt[i], bad[i]);
|
|
fail += bad[i] != 0;
|
|
}
|
|
// the SIMD Q8_K quantiser itself (not only the dispatcher's pick) against the reference
|
|
{
|
|
long b2 = 0;
|
|
for (int t = 0; t < 20000; ++t) {
|
|
std::vector<float> x(256);
|
|
for (auto& v : x) v = nd(rng) * std::ldexp(1.0f, (int)(rng() % 20) - 10);
|
|
std::vector<uint8_t> a(292, 0), b(292, 0);
|
|
neon::quantize_q8_K(x.data(), a.data(), 256);
|
|
quantize_q8_K_ref(x.data(), b.data(), 256);
|
|
b2 += a != b;
|
|
}
|
|
std::printf("%s neon::quantize_q8_K vs quantize_q8_K_ref: %ld / 20000 differ\n", b2 ? "FAIL" : "ok ", b2);
|
|
fail += b2 != 0;
|
|
}
|
|
std::printf("%s\n", fail ? "FAILED" : "PASSED");
|
|
return fail ? 1 : 0;
|
|
}
|