Files

103 lines
5.0 KiB
C++

// dot_avx2_bench: ns per row-dot of the optimised Avx2 kernels and of the plain kernel of the same translation unit, plus the 4-row kernels.
// Built twice (against the old library and against the new one) and run interleaved by tests/clean_p2/run_dot_bench.sh.
// output lines: "<type> opt <ns> plain <ns> [x4 <ns per row>]" (best of several repetitions, one thread)
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <vector>
#include "probe_blocks.hpp"
#include "strata/quant/act.hpp"
#include "strata/quant/dot.hpp"
#if defined(_MSC_VER)
#define STRATA_RESTRICT __restrict // MSVC mangles it into the symbol name
#else
#define STRATA_RESTRICT __restrict__
#endif
namespace strata::quant::avx2 {
#define DECL(N) \
void dot_##N(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT x, const void* STRATA_RESTRICT y); \
void dot_##N##_opt(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT x, const void* STRATA_RESTRICT y);
DECL(q8_0_q8_0) DECL(q4_K_q8_K) DECL(q5_K_q8_K) DECL(q6_K_q8_K) DECL(iq3_xxs_q8_K) DECL(iq4_xs_q8_K)
void dot_q8_0_q8_0_x4_avx2(int n, float out[4], const void* STRATA_RESTRICT w, size_t rb, const void* STRATA_RESTRICT a);
void dot_q4_K_q8_K_x4_avx2(int n, float out[4], const void* STRATA_RESTRICT w, size_t rb, const void* STRATA_RESTRICT a);
void dot_iq4_xs_q8_K_x4_avx2(int n, float out[4], const void* STRATA_RESTRICT w, size_t rb, const void* STRATA_RESTRICT a);
} // namespace strata::quant::avx2
using namespace strata::quant;
typedef void (*Fn)(int, float* STRATA_RESTRICT, const void* STRATA_RESTRICT, const void* STRATA_RESTRICT);
typedef void (*Fn4)(int, float*, const void* STRATA_RESTRICT, size_t, const void* STRATA_RESTRICT);
static double now() { return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
int main(int argc, char** argv) {
const int reps = argc > 1 ? atoi(argv[1]) : 15;
const int rowsN = argc > 2 ? atoi(argv[2]) : 1024;
using namespace strata::quant::avx2;
struct T { const char* name; int wb, ab, elems; Fn plain, opt; Fn4 x4; bool k; } types[] = {
{"q8_0", 34, 34, 32, dot_q8_0_q8_0, dot_q8_0_q8_0_opt, dot_q8_0_q8_0_x4_avx2, false},
{"q4_K", 144, 292, 256, dot_q4_K_q8_K, dot_q4_K_q8_K_opt, dot_q4_K_q8_K_x4_avx2, true},
{"q5_K", 176, 292, 256, dot_q5_K_q8_K, dot_q5_K_q8_K_opt, nullptr, true},
{"q6_K", 210, 292, 256, dot_q6_K_q8_K, dot_q6_K_q8_K_opt, nullptr, true},
{"iq3_xxs", 98, 292, 256, dot_iq3_xxs_q8_K, dot_iq3_xxs_q8_K_opt, nullptr, true},
{"iq4_xs", 136, 292, 256, dot_iq4_xs_q8_K, dot_iq4_xs_q8_K_opt, dot_iq4_xs_q8_K_x4_avx2, true},
};
std::mt19937_64 rng(1);
const int K = 4096;
for (const T& t : types) {
const int nb = K / t.elems;
std::vector<uint8_t> w((size_t)rowsN * nb * t.wb), a((size_t)nb * t.ab);
for (auto& b : w) b = (uint8_t)rng();
for (int r = 0; r < rowsN; ++r)
for (int b = 0; b < nb; ++b) {
uint8_t* p = &w[((size_t)r * nb + b) * t.wb];
put_half(p + (t.wb == 210 ? 208 : 0), rand_half(rng));
if (t.wb == 144 || t.wb == 176) put_half(p + 2, rand_half(rng));
}
if (t.k) {
std::vector<float> x(K);
std::normal_distribution<float> nd(0.f, 1.f);
for (auto& v : x) v = nd(rng);
quantize_q8_K_ref(x.data(), a.data(), K);
} else {
for (int b = 0; b < nb; ++b) {
put_half(&a[(size_t)b * 34], rand_half(rng));
for (int i = 0; i < 32; ++i) a[(size_t)b * 34 + 2 + i] = (uint8_t)(int8_t)((int)(rng() % 255) - 127);
}
}
const size_t rb = (size_t)nb * t.wb;
volatile float sink = 0;
auto time_one = [&](Fn f) {
double best = 1e30;
for (int rep = 0; rep < reps; ++rep) {
const double t0 = now();
float s = 0, acc = 0;
for (int r = 0; r < rowsN; ++r) { f(K, &s, &w[r * rb], a.data()); acc += s; }
best = std::min(best, now() - t0);
sink = acc;
}
return best / rowsN * 1e9;
};
const double opt = time_one(t.opt), plain = time_one(t.plain);
double x4 = -1;
if (t.x4) {
double best = 1e30;
for (int rep = 0; rep < reps; ++rep) {
const double t0 = now();
float o[4], acc = 0;
for (int r = 0; r + 3 < rowsN; r += 4) { t.x4(K, o, &w[r * rb], rb, a.data()); acc += o[0]; }
best = std::min(best, now() - t0);
sink = acc;
}
x4 = best / rowsN * 1e9;
}
(void)sink;
printf("%s opt %.1f plain %.1f x4 %.1f\n", t.name, opt, plain, x4);
}
return 0;
}