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.
90 lines
5.0 KiB
C++
90 lines
5.0 KiB
C++
// model_mins: the dmin term of the Avx2 Q4_K / Q5_K dots. Rows with dmin != 0; the A part (scales x quants) follows the model of model_scan; the
|
|
// min part is -dmin*da*sum_j m_j*(bsums[2j] + bsums[2j+1]). Options scanned: how the integer min sums are laid out (scalar / 4 lanes of sub-block pairs /
|
|
// 8 lanes), the float scale (fl(dmin*da)), accumulate fused / unfused, the 4-lane or 8-lane final tree, and how it joins the A result.
|
|
#include "model_ref.hpp"
|
|
#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 std::string name = argc > 1 ? argv[1] : "q4_K";
|
|
const long rows = argc > 2 ? atol(argv[2]) : 30000;
|
|
ProbeType* t = find_probe_type(name);
|
|
const DotKernel* k = dot_kernel(t->type, Arith::Avx2, Isa::Avx2);
|
|
std::mt19937_64 rng(99);
|
|
const int NOPT = 2 * 3 * 2 * 3; // layout(S,L4,L8) x fused x tree(L4: movehl+shuf / adjacent) x join(A-m, A+(-m), m first)
|
|
std::vector<long> bad(NOPT, 0);
|
|
long cnt = 0;
|
|
for (long it = 0; it < rows; ++it) {
|
|
const int nb = 1 + (int)(rng() % 5);
|
|
ProbeRow r(*t, nb);
|
|
for (int b = 0; b < nb; ++b) {
|
|
for (int i = 0; i < t->wbytes; ++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));
|
|
const float d = std::ldexp(0.5f + (float)(rng() % 1000) / 1000.0f, (int)(rng() % 7) - 9) * ((rng() & 1) ? 1.f : -1.f);
|
|
std::memcpy(r.ab(b), &d, 4);
|
|
const int mode = (int)(rng() % 4); // mode 3: no products at all, only bsums (isolates the min term)
|
|
for (int i = 0; i < 256; ++i) {
|
|
int v = mode == 0 ? (int)(rng() % 255) - 127 : mode == 1 ? (int)(rng() % 31) - 15 : mode == 2 ? (rng() % 4 == 0 ? (int)(rng() % 255) - 127 : 0) : 0;
|
|
r.set_act(b, i, (int8_t)v);
|
|
}
|
|
if (mode == 3)
|
|
for (int t2 = 0; t2 < 16; ++t2) {
|
|
const int16_t s = (int16_t)((int)(rng() % 4001) - 2000);
|
|
std::memcpy(r.ab(b) + 260 + 2 * t2, &s, 2);
|
|
}
|
|
}
|
|
float oracle = 0;
|
|
k->fn(nb * 256, &oracle, r.w.data(), r.a.data());
|
|
++cnt;
|
|
float accA[8] = {};
|
|
std::vector<int> m8(nb * 8);
|
|
for (int b = 0; b < nb; ++b) {
|
|
int lane[8];
|
|
lane_ints(t->pt, r.wb(b), r.ab(b), lane);
|
|
const float D = r.wd(b) * r.act_d(b);
|
|
for (int l = 0; l < 8; ++l) accA[l] = std::fmaf(D, (float)lane[l], accA[l]);
|
|
const uint8_t* s12 = r.wb(b) + 4;
|
|
const int16_t* bs = (const int16_t*)(r.ab(b) + 260);
|
|
for (int j = 0; j < 8; ++j) {
|
|
const int mj = j < 4 ? (s12[j + 4] & 63) : ((s12[j + 4] >> 4) | ((s12[j] >> 6) << 4));
|
|
m8[b * 8 + j] = mj * ((int)bs[2 * j] + (int)bs[2 * j + 1]);
|
|
}
|
|
}
|
|
const float A = tree8(accA);
|
|
int o = 0;
|
|
for (int layout = 0; layout < 3; ++layout)
|
|
for (int fused = 0; fused < 2; ++fused)
|
|
for (int tr = 0; tr < 2; ++tr)
|
|
for (int join = 0; join < 3; ++join, ++o) {
|
|
// scalar: one lane; L4: lane k = pair (2k, 2k+1); L8: lane j
|
|
float acc[8] = {};
|
|
for (int b = 0; b < nb; ++b) {
|
|
const float dm = fp16_to_fp32(get_half(r.wb(b) + 2)) * r.act_d(b);
|
|
int v[8] = {};
|
|
if (layout == 0) { int s = 0; for (int j = 0; j < 8; ++j) s += m8[b * 8 + j]; v[0] = s; }
|
|
else if (layout == 1) for (int j = 0; j < 8; ++j) v[j / 2] += m8[b * 8 + j];
|
|
else for (int j = 0; j < 8; ++j) v[j] = m8[b * 8 + j];
|
|
for (int l = 0; l < 8; ++l) acc[l] = fused ? std::fmaf(dm, (float)v[l], acc[l]) : acc[l] + dm * (float)v[l];
|
|
}
|
|
float mt;
|
|
if (layout == 0) mt = acc[0];
|
|
else if (layout == 1) mt = tr == 0 ? (acc[0] + acc[2]) + (acc[1] + acc[3]) : (acc[0] + acc[1]) + (acc[2] + acc[3]);
|
|
else mt = tr == 0 ? tree8(acc) : ((acc[0] + acc[1]) + (acc[2] + acc[3])) + ((acc[4] + acc[5]) + (acc[6] + acc[7]));
|
|
float res;
|
|
if (join == 0) res = A - mt;
|
|
else if (join == 1) res = A + (-mt);
|
|
else res = (-mt) + A;
|
|
if (std::memcmp(&res, &oracle, 4)) ++bad[o];
|
|
}
|
|
}
|
|
int o = 0;
|
|
for (int layout = 0; layout < 3; ++layout)
|
|
for (int fused = 0; fused < 2; ++fused)
|
|
for (int tr = 0; tr < 2; ++tr)
|
|
for (int join = 0; join < 3; ++join, ++o)
|
|
if (bad[o] < cnt / 3) printf("layout %s %s tree%d join%d: mismatches %ld / %ld\n", layout == 0 ? "scalar" : layout == 1 ? "L4" : "L8", fused ? "fma" : "mul+add", tr, join, bad[o], cnt);
|
|
return 0;
|
|
}
|