mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-06 00:08:52 +00:00
171 lines
9.5 KiB
C++
171 lines
9.5 KiB
C++
// dot_avx2_verify: the optimised Avx2 kernels of dot_avx2.cpp against the plain kernels of the same translation unit (bit identical), and the 4-row
|
|
// kernels against the one-row kernels (bit identical per row). Weights and activations are random bytes (all quants, -128 activations, subnormal /
|
|
// negative / zero scales), plus real Q8_K activations made by the reference quantiser. Also a float64 sanity bound against the exact value.
|
|
// Built against the library of the *new* dot_avx2 (tests/clean_p2/build_quant_manual.sh with USE_CLEAN=1); the plain kernels are called by their
|
|
// namespace-qualified names.
|
|
// usage: dot_avx2_verify [rows per type]
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "model_ref.hpp"
|
|
#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);
|
|
|
|
struct T {
|
|
const char* name;
|
|
PT pt;
|
|
int wb, ab, elems;
|
|
Fn plain, opt;
|
|
Fn4 x4;
|
|
};
|
|
|
|
static uint16_t special_half(std::mt19937_64& rng) {
|
|
static const uint16_t sp[] = {0x0000, 0x8000, 0x0001, 0x8001, 0x03ff, 0x83ff, 0x0400, 0x7bff, 0xfbff, 0x3c00, 0xbc00, 0x2e66, 0x5640};
|
|
return (rng() % 3 == 0) ? sp[rng() % (sizeof sp / sizeof sp[0])] : rand_half(rng, -14, 8);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
const long rows = argc > 1 ? atol(argv[1]) : 100000;
|
|
using namespace strata::quant::avx2;
|
|
const T types[] = {
|
|
{"q8_0", PQ8_0, 34, 34, 32, dot_q8_0_q8_0, dot_q8_0_q8_0_opt, dot_q8_0_q8_0_x4_avx2},
|
|
{"q4_K", PQ4_K, 144, 292, 256, dot_q4_K_q8_K, dot_q4_K_q8_K_opt, dot_q4_K_q8_K_x4_avx2},
|
|
{"q5_K", PQ5_K, 176, 292, 256, dot_q5_K_q8_K, dot_q5_K_q8_K_opt, nullptr},
|
|
{"q6_K", PQ6_K, 210, 292, 256, dot_q6_K_q8_K, dot_q6_K_q8_K_opt, nullptr},
|
|
{"iq3_xxs", PIQ3_XXS, 98, 292, 256, dot_iq3_xxs_q8_K, dot_iq3_xxs_q8_K_opt, nullptr},
|
|
{"iq4_xs", PIQ4_XS, 136, 292, 256, dot_iq4_xs_q8_K, dot_iq4_xs_q8_K_opt, dot_iq4_xs_q8_K_x4_avx2},
|
|
};
|
|
std::mt19937_64 rng(424242);
|
|
int fail = 0;
|
|
for (const T& t : types) {
|
|
long bad = 0, bad_x4 = 0, bad_val = 0, nx4 = 0;
|
|
double worst_rel = 0;
|
|
for (long it = 0; it < rows; ++it) {
|
|
const int nb = 1 + (int)(rng() % 9);
|
|
const int n = nb * t.elems;
|
|
const int nrow = t.x4 ? 4 : 1;
|
|
std::vector<uint8_t> w((size_t)nrow * nb * t.wb), a((size_t)nb * t.ab);
|
|
const int style = (int)(rng() % 6);
|
|
for (auto& b : w) b = (uint8_t)rng();
|
|
if (style == 1) for (auto& b : w) b = (rng() & 1) ? 0xff : 0x00; // extreme quants
|
|
if (style == 2) for (auto& b : w) b = (rng() & 3) ? 0 : (uint8_t)rng();
|
|
// scales
|
|
for (int r = 0; r < nrow; ++r)
|
|
for (int b = 0; b < nb; ++b) {
|
|
uint8_t* p = &w[((size_t)r * nb + b) * t.wb];
|
|
put_half(p + (t.pt == PQ6_K ? 208 : 0), special_half(rng));
|
|
if (t.pt == PQ4_K || t.pt == PQ5_K) put_half(p + 2, special_half(rng));
|
|
}
|
|
// activation
|
|
const bool real = (it & 1) == 0 && t.pt != PQ8_0;
|
|
if (real) {
|
|
std::vector<float> x(n);
|
|
std::normal_distribution<float> nd(0.f, (rng() % 4 == 0) ? 1e-3f : 1.f);
|
|
for (auto& v : x) v = nd(rng);
|
|
quantize_q8_K_ref(x.data(), a.data(), n);
|
|
} else {
|
|
for (int b = 0; b < nb; ++b) {
|
|
uint8_t* p = &a[(size_t)b * t.ab];
|
|
if (t.pt == PQ8_0) {
|
|
put_half(p, special_half(rng));
|
|
for (int i = 0; i < 32; ++i) p[2 + i] = (style == 3) ? 0x80 : (uint8_t)rng();
|
|
} else {
|
|
const float d = (rng() % 8 == 0) ? 0.f : std::ldexp(0.5f + (float)(rng() % 1000) / 1000.f, (int)(rng() % 20) - 14) * ((rng() & 1) ? 1.f : -1.f);
|
|
std::memcpy(p, &d, 4);
|
|
for (int i = 0; i < 256; ++i) p[4 + i] = (style == 3) ? 0x80 : (style == 4 ? 0x7f : (uint8_t)rng());
|
|
for (int k = 0; k < 16; ++k) { // consistent bsums
|
|
int16_t s = 0;
|
|
for (int i = 0; i < 16; ++i) s = (int16_t)(s + (int8_t)p[4 + 16 * k + i]);
|
|
std::memcpy(p + 260 + 2 * k, &s, 2);
|
|
}
|
|
if (style == 5) for (int k = 0; k < 16; ++k) { int16_t s = (int16_t)rng(); std::memcpy(p + 260 + 2 * k, &s, 2); } // bsums not tied to qs
|
|
}
|
|
}
|
|
}
|
|
float rp = 0, ro = 0;
|
|
t.plain(n, &rp, w.data(), a.data());
|
|
t.opt(n, &ro, w.data(), a.data());
|
|
if (std::memcmp(&rp, &ro, 4) && !(rp != rp && ro != ro)) {
|
|
if (++bad <= 3) printf(" %s MISMATCH n=%d style=%d plain %.9g (%08x) opt %.9g (%08x)\n", t.name, n, style, rp, *(uint32_t*)&rp, ro, *(uint32_t*)&ro);
|
|
}
|
|
if (t.x4) {
|
|
float o4[4];
|
|
t.x4(n, o4, w.data(), (size_t)nb * t.wb, a.data());
|
|
for (int r = 0; r < 4; ++r) {
|
|
float one = 0;
|
|
t.plain(n, &one, &w[(size_t)r * nb * t.wb], a.data());
|
|
++nx4;
|
|
if (std::memcmp(&one, &o4[r], 4) && !(one != one && o4[r] != o4[r])) ++bad_x4;
|
|
}
|
|
}
|
|
// float64 sanity: |opt - V| <= 2^-20 * sum |terms|, V from the exact integer lanes (types with a lane model)
|
|
if (t.pt != PQ8_0 && real) {
|
|
ProbeType pt{t.name, t.pt, strata::quant::Type::Q8_0, t.elems, t.wb, t.ab};
|
|
ProbeRow row(pt, nb);
|
|
std::memcpy(row.w.data(), w.data(), (size_t)nb * t.wb);
|
|
std::memcpy(row.a.data(), a.data(), (size_t)nb * t.ab);
|
|
double V = 0, S = 0;
|
|
for (int b = 0; b < nb; ++b) {
|
|
int lane[8];
|
|
lane_ints(t.pt, row.wb(b), row.ab(b), lane);
|
|
int tot = 0;
|
|
for (int l = 0; l < 8; ++l) tot += lane[l];
|
|
const double term = (double)row.D(b) * (double)tot;
|
|
V += term;
|
|
for (int l = 0; l < 8; ++l) S += std::fabs((double)row.D(b) * (double)lane[l]); // the error scale is the lane magnitudes, not their cancelling sum
|
|
if (t.pt == PQ4_K || t.pt == PQ5_K) {
|
|
const uint8_t* s12 = row.wb(b) + 4;
|
|
const int16_t* bs = (const int16_t*)(row.ab(b) + 260);
|
|
long M = 0, Mk[4] = {0, 0, 0, 0};
|
|
for (int j = 0; j < 8; ++j) {
|
|
const int mj = j < 4 ? (s12[j + 4] & 63) : ((s12[j + 4] >> 4) | ((s12[j] >> 6) << 4));
|
|
M += (long)mj * (bs[2 * j] + bs[2 * j + 1]);
|
|
Mk[j / 2] += (long)mj * (bs[2 * j] + bs[2 * j + 1]);
|
|
}
|
|
const double dmv = (double)fp16_to_fp32(get_half(row.wb(b) + 2)) * (double)row.act_d(b);
|
|
V -= dmv * (double)M;
|
|
if (t.pt == PQ4_K) for (int k = 0; k < 4; ++k) S += std::fabs(dmv * (double)Mk[k]); // four min lanes
|
|
else S += std::fabs(dmv * (double)M);
|
|
}
|
|
}
|
|
if (S > 1e-20 && S < 1e30 && std::isfinite(ro)) { // outside the subnormal / overflow ranges the relative bound is meaningful
|
|
const double rel = std::fabs((double)ro - V) / S;
|
|
if (rel > worst_rel) worst_rel = rel;
|
|
if (rel > std::ldexp(1.0, -20)) ++bad_val;
|
|
}
|
|
}
|
|
}
|
|
const bool ok = bad == 0 && bad_x4 == 0 && bad_val == 0;
|
|
printf("%s %-8s rows %ld: opt vs plain mismatches %ld, x4 vs one-row %ld/%ld, float64 bound violations %ld (worst %.3g of 2^-20)\n", ok ? "ok " : "FAIL", t.name, rows, bad,
|
|
bad_x4, nx4, bad_val, worst_rel / std::ldexp(1.0, -20));
|
|
if (!ok) ++fail;
|
|
}
|
|
printf("%s\n", fail ? "FAILED" : "PASSED");
|
|
return fail ? 1 : 0;
|
|
}
|