Files
Niko1221 2ca83480cd clean-p2: P-7 x86 tier from the specs (dot_avx2, act_avx512, kq dots; old files untouched)
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.
2026-12-01 01:57:16 +01:00

59 lines
3.0 KiB
C++

// model_scan: random full rows of a K-quant type against the lane model of model_ref.hpp + float composition options; counts mismatches with the oracle kernel.
// usage: model_scan <q6_K|q4_K|q5_K|iq4_xs> [rows] [mins 0/1]
#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] : "q6_K";
const long rows = argc > 2 ? atol(argv[2]) : 50000;
const int mins = argc > 3 ? atoi(argv[3]) : 0;
ProbeType* t = find_probe_type(name);
const DotKernel* k = dot_kernel(t->type, Arith::Avx2, Isa::Avx2);
std::mt19937_64 rng(2026);
long bad[2][2] = {}; // fused, NA(1,2)
long cnt = 0;
for (long it = 0; it < rows; ++it) {
const int nb = 1 + (int)(rng() % 6);
ProbeRow r(*t, nb);
for (int b = 0; b < nb; ++b) {
for (auto& x : std::vector<uint8_t*>{r.wb(b)}) for (int i = 0; i < t->wbytes; ++i) x[i] = (uint8_t)rng();
// halves finite and moderate
const int dpos = (t->pt == PQ6_K) ? 208 : 0;
put_half(r.wb(b) + dpos, rand_half(rng));
if (t->pt == PQ4_K || t->pt == PQ5_K) put_half(r.wb(b) + 2, mins ? rand_half(rng) : 0);
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() % 3);
for (int i = 0; i < 256; ++i) {
int v = mode == 0 ? (int)(rng() % 256) - 128 : mode == 1 ? (int)(rng() % 31) - 15 : (rng() % 4 == 0 ? (int)(rng() % 255) - 127 : 0);
r.set_act(b, i, (int8_t)v);
}
if ((rng() % 4) == 0) for (int t2 = 0; t2 < 16; ++t2) { int16_t s = (int16_t)((int)(rng() % 4001) - 2000); if (rng() % 2) s = 0; std::memcpy(r.ab(b) + 260 + 2 * t2, &s, 2); if (!mins && t->pt != PQ6_K) {} }
}
float oracle = 0;
k->fn(nb * 256, &oracle, r.w.data(), r.a.data());
++cnt;
for (int fused = 0; fused < 2; ++fused)
for (int na = 0; na < 2; ++na) {
float acc[2][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.D(b);
float* ac = acc[na ? b % 2 : 0];
for (int l = 0; l < 8; ++l) ac[l] = fused ? std::fmaf(D, (float)lane[l], ac[l]) : ac[l] + D * (float)lane[l];
}
float tot[8];
for (int l = 0; l < 8; ++l) tot[l] = na ? acc[0][l] + acc[1][l] : acc[0][l];
const float res = tree8(tot);
if (std::memcmp(&res, &oracle, 4)) ++bad[fused][na];
}
}
printf("%s (mins %d): rows %ld; mismatches: fused NA1 %ld, fused NA2 %ld, unfused NA1 %ld, unfused NA2 %ld\n", name.c_str(), mins, cnt, bad[1][0], bad[1][1], bad[0][0], bad[0][1]);
return 0;
}