mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-06 00:08:52 +00:00
New files (replacements, same interfaces):
engine/src/quant/x86/dot_avx2_clean.cpp table + six _opt kernels + three x4 kernels, bit-identical to the plain kernels of the TU
engine/src/quant/x86/act_avx512_clean.cpp AVX-512 Q8_0 / Q8_1 / Q8_K quantisers, byte-identical to the Avx2 contract
engine/src/kernels/cpu/kq_dots_avx2.{hpp,cpp} Q4_K / Q5_1 / Q8_0 multi-token dots, bit-identical per token to the single-token Avx2 dots
Float orders were found by black-box probing of the current kernels (tests/clean_p2/probe_*.cpp, model_*.cpp), then
confirmed on random rows. Tests: dot_avx2_verify, act_avx512_verify (needs AVX-512), kq_dots_avx2_test, plus the probes,
a manual build script for the quant layer and Windows (MSVC) drivers.
55 lines
2.9 KiB
C++
55 lines
2.9 KiB
C++
// model_q51: float composition of the Avx2 Q5_1 x Q8_1 dot: lanes (4 adjacent products), acc[l] = fma(D, lane, acc[l]), the min part m_w * s_a, and how they join.
|
|
#include "probe_core.hpp"
|
|
|
|
static float tree8(const float* v) { return ((v[0] + v[4]) + (v[2] + v[6])) + ((v[1] + v[5]) + (v[3] + v[7])); }
|
|
|
|
int main(int argc, char** argv) {
|
|
const long rows = argc > 1 ? atol(argv[1]) : 50000;
|
|
ProbeType* t = find_probe_type("q5_1");
|
|
const DotKernel* k = dot_kernel(t->type, Arith::Avx2, Isa::Avx2);
|
|
std::mt19937_64 rng(51);
|
|
long bad[2][2][2] = {}; // acc fused, min fused, join order (A + m, m + A is the same in IEEE; second slot: A - (-m) irrelevant) -> kept for symmetry
|
|
long cnt = 0;
|
|
for (long it = 0; it < rows; ++it) {
|
|
const int nb = 1 + (int)(rng() % 9);
|
|
ProbeRow r(*t, nb);
|
|
for (int b = 0; b < nb; ++b) {
|
|
for (int i = 0; i < 24; ++i) r.wb(b)[i] = (uint8_t)rng();
|
|
put_half(r.wb(b), rand_half(rng));
|
|
put_half(r.wb(b) + 2, rand_half(rng));
|
|
put_half(r.ab(b), rand_half(rng));
|
|
put_half(r.ab(b) + 2, rand_half(rng));
|
|
const int mode = (int)(rng() % 3);
|
|
for (int i = 0; i < 32; ++i) r.ab(b)[4 + i] = (uint8_t)(int8_t)(mode == 0 ? (int)(rng() % 256) - 128 : mode == 1 ? (int)(rng() % 31) - 15 : (rng() % 4 == 0 ? (int)(rng() % 255) - 127 : 0));
|
|
}
|
|
float oracle = 0;
|
|
k->fn(nb * 32, &oracle, r.w.data(), r.a.data());
|
|
++cnt;
|
|
for (int af = 0; af < 2; ++af)
|
|
for (int mf = 0; mf < 2; ++mf) {
|
|
float acc[8] = {};
|
|
float summs = 0.f;
|
|
for (int b = 0; b < nb; ++b) {
|
|
const uint8_t* w = r.wb(b);
|
|
const uint8_t* a = r.ab(b);
|
|
const uint32_t qh = (uint32_t)(w[4] | (w[5] << 8) | (w[6] << 16) | ((uint32_t)w[7] << 24));
|
|
int lane[8] = {};
|
|
for (int i = 0; i < 32; ++i) {
|
|
const int j = i % 16;
|
|
const int q5 = ((i >= 16 ? (w[8 + j] >> 4) : (w[8 + j] & 15))) | (int)(((qh >> i) & 1) << 4);
|
|
lane[i / 4] += q5 * (int)(int8_t)a[4 + i];
|
|
}
|
|
const float D = fp16_to_fp32(get_half(w)) * fp16_to_fp32(get_half(a));
|
|
for (int l = 0; l < 8; ++l) acc[l] = af ? std::fmaf(D, (float)lane[l], acc[l]) : acc[l] + D * (float)lane[l];
|
|
const float mw = fp16_to_fp32(get_half(w + 2)), sa = fp16_to_fp32(get_half(a + 2));
|
|
summs = mf ? std::fmaf(mw, sa, summs) : summs + mw * sa;
|
|
}
|
|
const float res = tree8(acc) + summs;
|
|
if (std::memcmp(&res, &oracle, 4)) ++bad[af][mf][0];
|
|
}
|
|
}
|
|
for (int af = 0; af < 2; ++af)
|
|
for (int mf = 0; mf < 2; ++mf) printf("acc %s, min %s: mismatches %ld / %ld\n", af ? "fma" : "mul+add", mf ? "fma" : "mul+add", bad[af][mf][0], cnt);
|
|
return 0;
|
|
}
|