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

93 lines
5.0 KiB
C++

// probe_q8_0: black-box scan of the float composition of the Avx2 Q8_0 x Q8_0 dot (dot_kernel(Q8_0, Avx2, Avx2)).
// Candidate models = lane partition x accumulate (fused / unfused) x number of accumulators x combine order x horizontal-sum tree.
// Mismatch counts are listed per block count nb, so a structure that only shows from nb = 2 or nb = 4 on is visible.
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <random>
#include <string>
#include <vector>
#include "strata/quant/dot.hpp"
#include "strata/quant/fp16.hpp"
using namespace strata::quant;
static void put16(uint8_t* p, uint16_t h) { p[0] = h & 255; p[1] = h >> 8; }
typedef std::function<float(const float*)> Tree;
int main(int argc, char** argv) {
const long iters = argc > 1 ? atol(argv[1]) : 20000;
const int NBMAX = 9;
const DotKernel* k = dot_kernel(Type::Q8_0, Arith::Avx2, Isa::Avx2);
printf("kernel %s\n", k->name);
std::vector<std::pair<std::string, Tree>> trees = {
{"lohi,movehl,shuf", [](const float* v) { float a[4]; for (int i = 0; i < 4; ++i) a[i] = v[i] + v[i + 4]; float b0 = a[0] + a[2], b1 = a[1] + a[3]; return b0 + b1; }},
{"lohi,adj", [](const float* v) { float a[4]; for (int i = 0; i < 4; ++i) a[i] = v[i] + v[i + 4]; return (a[0] + a[1]) + (a[2] + a[3]); }},
{"sequential", [](const float* v) { float s = v[0]; for (int i = 1; i < 8; ++i) s += v[i]; return s; }},
};
// acc count NA in {1,2,4}; combine: 0 sequential lane-wise, 1 pairwise; fused 0/1
const int NAs[3] = {1, 2, 4};
std::mt19937_64 rng(77);
static long bad[3][2][2][3][10]; // NA, comb, fused, tree, nb
static long cnt[10];
for (long it = 0; it < iters; ++it) {
int nb = 1 + (int)(rng() % NBMAX);
std::vector<uint8_t> w(nb * 34), a(nb * 34);
std::vector<float> dw(nb), da(nb);
for (int b = 0; b < nb; ++b) {
uint16_t hw = fp32_to_fp16((float)std::ldexp(0.5 + (rng() % 1000) / 1000.0, (int)(rng() % 9) - 6) * ((rng() & 1) ? 1 : -1));
uint16_t ha = fp32_to_fp16((float)std::ldexp(0.5 + (rng() % 1000) / 1000.0, (int)(rng() % 9) - 6) * ((rng() & 1) ? 1 : -1));
put16(&w[b * 34], hw);
put16(&a[b * 34], ha);
dw[b] = fp16_to_fp32(hw);
da[b] = fp16_to_fp32(ha);
for (int i = 0; i < 32; ++i) {
int mode = it % 3;
w[b * 34 + 2 + i] = (uint8_t)(int8_t)(mode == 0 ? (int)(rng() % 256) - 128 : (int)(rng() % 41) - 20);
a[b * 34 + 2 + i] = (uint8_t)(int8_t)(mode == 0 ? (int)(rng() % 256) - 128 : (int)(rng() % 41) - 20);
}
}
float oracle = 0;
k->fn(nb * 32, &oracle, w.data(), a.data());
++cnt[nb];
for (int na = 0; na < 3; ++na)
for (int comb = 0; comb < 2; ++comb)
for (int fused = 0; fused < 2; ++fused) {
float acc[4][8] = {};
const int NA = NAs[na];
for (int b = 0; b < nb; ++b) {
int lane[8] = {};
for (int i = 0; i < 32; i += 2) { int pr = 0; for (int h = 0; h < 2; ++h) { const int wv = (int8_t)w[b * 34 + 2 + i + h], av = (int8_t)a[b * 34 + 2 + i + h]; const int ax = wv < 0 ? -wv : wv; int sy = wv < 0 ? -av : (wv == 0 ? 0 : av); sy = (int8_t)sy; pr += ax * sy; } if (pr > 32767) pr = 32767; if (pr < -32768) pr = -32768; lane[i / 4] += pr; }
const float D = dw[b] * da[b];
float* ac = acc[b % NA];
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) {
if (NA == 1) tot[l] = acc[0][l];
else if (NA == 2) tot[l] = acc[0][l] + acc[1][l];
else tot[l] = comb == 0 ? ((acc[0][l] + acc[1][l]) + acc[2][l]) + acc[3][l] : (acc[0][l] + acc[1][l]) + (acc[2][l] + acc[3][l]);
}
for (size_t t = 0; t < trees.size(); ++t) {
float r = trees[t].second(tot);
if (memcmp(&r, &oracle, 4) != 0) ++bad[na][comb][fused][t][nb];
}
}
}
for (int na = 0; na < 3; ++na)
for (int comb = 0; comb < 2; ++comb)
for (int fused = 0; fused < 2; ++fused)
for (size_t t = 0; t < trees.size(); ++t) {
long tot = 0;
for (int nb = 1; nb <= NBMAX; ++nb) tot += bad[na][comb][fused][t][nb];
if (tot > (long)iters / 4) continue;
printf("NA=%d comb=%d %-5s %-18s total %5ld by nb:", NAs[na], comb, fused ? "fma" : "mul+add", trees[t].first.c_str(), tot);
for (int nb = 1; nb <= NBMAX; ++nb) printf(" %ld/%ld", bad[na][comb][fused][t][nb], cnt[nb]);
printf("\n");
}
return 0;
}