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.
88 lines
4.1 KiB
C++
88 lines
4.1 KiB
C++
// neon_dot_bench: one-thread speed of the oracle dot_<N> and dot_<N>_opt of the four optimised NEON kernels, interleaved rounds.
|
|
// neon_dot_bench [rounds] [rows] [k_elems] prints one line per kernel: median GB/s (weights) of oracle and opt over the rounds, pairs where opt was faster
|
|
// Build it against two libraries (old / new tier file) and compare the opt columns: run the two executables alternately.
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
namespace strata::quant::neon {
|
|
#define DECL(N) void dot_##N(int, float*, const void*, const void*); void dot_##N##_opt(int, float*, const void*, const void*);
|
|
DECL(q8_0_q8_0) DECL(q4_K_q8_K) DECL(q5_K_q8_K) DECL(iq4_xs_q8_K)
|
|
} // namespace strata::quant::neon
|
|
using namespace strata::quant;
|
|
|
|
struct K {
|
|
const char* name;
|
|
int wbytes, abytes, wblock;
|
|
void (*oracle)(int, float*, const void*, const void*);
|
|
void (*opt)(int, float*, const void*, const void*);
|
|
};
|
|
|
|
static double run(void (*fn)(int, float*, const void*, const void*), int k, int rows, size_t rowb, const uint8_t* W, const uint8_t* A, int reps, float& sink) {
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
for (int r = 0; r < reps; ++r)
|
|
for (int i = 0; i < rows; ++i) {
|
|
float s;
|
|
fn(k, &s, W + (size_t)i * rowb, A);
|
|
sink += s;
|
|
}
|
|
const auto t1 = std::chrono::steady_clock::now();
|
|
return std::chrono::duration<double>(t1 - t0).count();
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
const int rounds = argc > 1 ? std::atoi(argv[1]) : 9;
|
|
const int rows = argc > 2 ? std::atoi(argv[2]) : 1024;
|
|
const int kel = argc > 3 ? std::atoi(argv[3]) : 4096;
|
|
const K ks[] = {
|
|
{"q8_0_q8_0", 34, 34, 32, neon::dot_q8_0_q8_0, neon::dot_q8_0_q8_0_opt},
|
|
{"q4_K_q8_K", 144, 292, 256, neon::dot_q4_K_q8_K, neon::dot_q4_K_q8_K_opt},
|
|
{"q5_K_q8_K", 176, 292, 256, neon::dot_q5_K_q8_K, neon::dot_q5_K_q8_K_opt},
|
|
{"iq4_xs_q8_K", 136, 292, 256, neon::dot_iq4_xs_q8_K, neon::dot_iq4_xs_q8_K_opt},
|
|
};
|
|
std::mt19937_64 rng(3);
|
|
float sink = 0;
|
|
for (const K& k : ks) {
|
|
const int nb = kel / k.wblock;
|
|
const size_t rowb = (size_t)nb * k.wbytes;
|
|
std::vector<uint8_t> W(rowb * rows), A((size_t)nb * k.abytes);
|
|
for (auto& b : W) b = (uint8_t)rng();
|
|
for (auto& b : A) b = (uint8_t)rng();
|
|
// finite scales
|
|
for (int i = 0; i < rows; ++i)
|
|
for (int b = 0; b < nb; ++b) {
|
|
uint8_t* w = &W[(size_t)i * rowb + (size_t)b * k.wbytes];
|
|
const uint16_t h = (uint16_t)(0x2c00 + (rng() & 0x3ff));
|
|
std::memcpy(w, &h, 2);
|
|
if (k.wbytes == 144 || k.wbytes == 176) std::memcpy(w + 2, &h, 2);
|
|
}
|
|
for (int b = 0; b < nb; ++b) {
|
|
uint8_t* a = &A[(size_t)b * k.abytes];
|
|
if (k.abytes == 34) { const uint16_t h = 0x2c00; std::memcpy(a, &h, 2); }
|
|
else { const float f = 0.01f; std::memcpy(a, &f, 4); int16_t* bs = (int16_t*)(a + 260); for (int t = 0; t < 16; ++t) bs[t] = (int16_t)(rng() % 400); }
|
|
}
|
|
const int reps = std::max(1, (int)(2.0e8 / ((double)rowb * rows))); // ~200 MB of weights per timing
|
|
std::vector<double> to, tn;
|
|
int faster = 0;
|
|
run(k.oracle, nb * k.wblock, rows, rowb, W.data(), A.data(), 1, sink); // warm up
|
|
for (int r = 0; r < rounds; ++r) {
|
|
const double a = run(k.oracle, nb * k.wblock, rows, rowb, W.data(), A.data(), reps, sink);
|
|
const double b = run(k.opt, nb * k.wblock, rows, rowb, W.data(), A.data(), reps, sink);
|
|
to.push_back(a);
|
|
tn.push_back(b);
|
|
faster += b < a;
|
|
}
|
|
std::sort(to.begin(), to.end());
|
|
std::sort(tn.begin(), tn.end());
|
|
const double bytes = (double)rowb * rows * reps;
|
|
std::printf("%-12s oracle %6.2f GB/s opt %6.2f GB/s opt/oracle %.2fx pairs faster %d/%d\n", k.name, bytes / to[to.size() / 2] / 1e9, bytes / tn[tn.size() / 2] / 1e9,
|
|
to[to.size() / 2] / tn[tn.size() / 2], faster, rounds);
|
|
}
|
|
std::fprintf(stderr, "(sink %g)\n", sink);
|
|
return 0;
|
|
}
|