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.
64 lines
4.2 KiB
C++
64 lines
4.2 KiB
C++
// neon_act_dump: deterministic activation-quantiser cases through strata::quant::neon::quantize_q8_{0,1,K}; prints one FNV-1a line per case.
|
|
// Build it against the old library and the new one and diff the outputs: they must be identical (byte-exact quantisers).
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
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
|
|
|
|
static uint64_t fnv(const uint8_t* p, size_t n, uint64_t h = 1469598103934665603ull) {
|
|
for (size_t i = 0; i < n; ++i) { h ^= p[i]; h *= 1099511628211ull; }
|
|
return h;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
const long N = argc > 1 ? std::atol(argv[1]) : 200000;
|
|
std::mt19937_64 rng(4242);
|
|
std::normal_distribution<float> nd(0.f, 1.f);
|
|
uint64_t total = 1469598103934665603ull;
|
|
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() % 15);
|
|
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);
|
|
switch (mode) {
|
|
case 0: for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * scale; break;
|
|
case 1: for (int i = 0; i < bs; ++i) xb[i] = 0.0f; break; // all zero
|
|
case 2: for (int i = 0; i < bs; ++i) xb[i] = (i & 1) ? -0.0f : 0.0f; break; // signed zeros
|
|
case 3: for (int i = 0; i < bs; ++i) xb[i] = ((float)((int)(rng() % 255) - 127) + 0.5f * (float)(rng() % 2)) * scale; break; // near ties
|
|
case 4: for (int i = 0; i < bs; ++i) xb[i] = scale; break; // all equal
|
|
case 5: for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * scale; xb[3] = 5.0f * scale; xb[bs - 2] = -5.0f * scale; break; // max twice, opposite signs
|
|
case 6: for (int i = 0; i < bs; ++i) xb[i] = std::ldexp(nd(rng), -140); break; // subnormals
|
|
case 7: for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * 1e30f; break; // huge
|
|
case 8: for (int i = 0; i < bs; ++i) xb[i] = ((float)((int)(rng() % 255) - 127)) * scale + 0.5f * scale; break; // exact half steps of the scale
|
|
case 9: for (int i = 0; i < bs; ++i) xb[i] = (rng() % 8 == 0) ? nd(rng) * scale : 0.0f; break; // sparse
|
|
case 10: { for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * scale; xb[(int)(rng() % bs)] = -9.0f * scale; break; }
|
|
case 11: for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * scale; xb[(int)(rng() % bs)] = INFINITY; break; // an infinity
|
|
case 12: for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * scale; xb[(int)(rng() % bs)] = -INFINITY; break;
|
|
case 13: for (int i = 0; i < bs; ++i) xb[i] = nd(rng) * scale; xb[(int)(rng() % bs)] = NAN; break; // a NaN
|
|
default: for (int i = 0; i < bs; ++i) xb[i] = (float)((int)(rng() % 255) - 127) * 0.37f * scale; break;
|
|
}
|
|
}
|
|
std::vector<uint8_t> y((size_t)(kind == 0 ? 34 : kind == 1 ? 36 : 292) * nblk, 0xA5);
|
|
if (kind == 0) strata::quant::neon::quantize_q8_0(x.data(), y.data(), (int64_t)x.size());
|
|
else if (kind == 1) strata::quant::neon::quantize_q8_1(x.data(), y.data(), (int64_t)x.size());
|
|
else strata::quant::neon::quantize_q8_K(x.data(), y.data(), (int64_t)x.size());
|
|
const uint64_t h = fnv(y.data(), y.size());
|
|
total = fnv((const uint8_t*)&h, 8, total);
|
|
if (argc > 2) std::printf("%ld %d %d %016llx\n", it, kind, mode, (unsigned long long)h);
|
|
}
|
|
std::printf("total %016llx over %ld cases\n", (unsigned long long)total, N);
|
|
return 0;
|
|
}
|