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.
89 lines
5.1 KiB
C++
89 lines
5.1 KiB
C++
// probe_tree: the shape of the horizontal reduction of the lane accumulators of a dot kernel (one block, Q8 activation).
|
|
// Three lanes carry non-zero float values, the rest are zero: the result is fl(fl(x_a + x_b) + x_c) when a and b merge before c, and so on.
|
|
// Every triple of lanes with random values tells which pair merges first, which fixes the tree (up to operand order). The lanes are found by
|
|
// probe_core.hpp's partition first (one representative element per lane).
|
|
// usage: probe_tree <type> [trials]
|
|
#include "probe_core.hpp"
|
|
|
|
int main(int argc, char** argv) {
|
|
const std::string name = argc > 1 ? argv[1] : "q8_0";
|
|
const int trials = argc > 2 ? atoi(argv[2]) : 200;
|
|
ProbeType* t = find_probe_type(name);
|
|
const DotKernel* k = dot_kernel(t->type, Arith::Avx2, Isa::Avx2);
|
|
std::mt19937_64 rng(11);
|
|
LanePartition lp = lane_partition(*t, k, rng, 24);
|
|
const int L = lp.nclasses;
|
|
printf("kernel %s: %d lanes, %d anomalous pairs\n", k->name, L, lp.anomalies);
|
|
std::vector<int> rep(L, -1);
|
|
for (int i = 0; i < t->elems; ++i) if (rep[lp.cls[i]] < 0) rep[lp.cls[i]] = i;
|
|
std::vector<int> merge((size_t)L * L * L, -1);
|
|
auto M = [&](int a, int b, int c) -> int& { return merge[((size_t)a * L + b) * L + c]; };
|
|
int unresolved = 0;
|
|
for (int a = 0; a < L; ++a)
|
|
for (int b = a + 1; b < L; ++b)
|
|
for (int c = b + 1; c < L; ++c) {
|
|
int votes[3] = {0, 0, 0}, none = 0, informative = 0;
|
|
for (int tr = 0; tr < trials; ++tr) {
|
|
ProbeRow r(*t, 1);
|
|
r.zero_bsums = true;
|
|
r.randomize_scales(rng);
|
|
const float D = r.D(0);
|
|
const int lanes[3] = {a, b, c};
|
|
float x[3];
|
|
for (int q = 0; q < 3; ++q) x[q] = D * (float)r.set_product(0, rep[lanes[q]], rng);
|
|
float oracle = 0;
|
|
k->fn(t->elems, &oracle, r.w.data(), r.a.data());
|
|
const float h[3] = {(x[0] + x[1]) + x[2], (x[0] + x[2]) + x[1], (x[1] + x[2]) + x[0]};
|
|
const bool m[3] = {memcmp(&oracle, &h[0], 4) == 0, memcmp(&oracle, &h[1], 4) == 0, memcmp(&oracle, &h[2], 4) == 0};
|
|
const int nm = m[0] + m[1] + m[2];
|
|
if (nm == 0) { ++none; continue; }
|
|
if (nm == 3) continue;
|
|
++informative;
|
|
for (int q = 0; q < 3; ++q) if (m[q]) votes[q] += 1;
|
|
}
|
|
int cand = -1, ncand = 0;
|
|
for (int q = 0; q < 3; ++q) if (votes[q] == informative && none == 0) { cand = q; ++ncand; }
|
|
M(a, b, c) = (ncand == 1) ? cand : -1;
|
|
if (ncand != 1) { ++unresolved; if (unresolved < 10) printf("triple %d %d %d unresolved: votes %d %d %d of %d informative, %d matching none\n", a, b, c, votes[0], votes[1], votes[2], informative, none); }
|
|
}
|
|
printf("unresolved triples: %d of %d\n", unresolved, L * (L - 1) * (L - 2) / 6);
|
|
// closest pairs: for a pair (a,b), the number of third lanes c for which (a,b) merge first
|
|
auto first_pair = [&](int a, int b, int c, int& p0, int& p1) {
|
|
int s[3] = {a, b, c};
|
|
std::sort(s, s + 3);
|
|
const int mm = M(s[0], s[1], s[2]);
|
|
if (mm == 0) { p0 = s[0]; p1 = s[1]; } else if (mm == 1) { p0 = s[0]; p1 = s[2]; } else if (mm == 2) { p0 = s[1]; p1 = s[2]; } else { p0 = p1 = -1; }
|
|
};
|
|
// build the tree by repeated merging: clusters; two clusters X, Y merge first if for all x in X, y in Y and every other lane c outside X u Y, (x,y) beats c
|
|
std::vector<std::vector<int>> cl;
|
|
std::vector<std::string> txt;
|
|
for (int i = 0; i < L; ++i) { cl.push_back({i}); txt.push_back(std::to_string(i)); }
|
|
while (cl.size() > 1) {
|
|
bool did = false;
|
|
for (size_t x = 0; x < cl.size() && !did; ++x)
|
|
for (size_t y = x + 1; y < cl.size() && !did; ++y) {
|
|
bool ok = true;
|
|
for (size_t z = 0; z < cl.size() && ok; ++z) {
|
|
if (z == x || z == y) continue;
|
|
for (int p : cl[x]) for (int q : cl[y]) for (int c : cl[z]) {
|
|
int p0, p1;
|
|
first_pair(p, q, c, p0, p1);
|
|
if (!((p0 == p && p1 == q) || (p0 == q && p1 == p))) ok = false;
|
|
}
|
|
}
|
|
if (ok && cl.size() >= 2) {
|
|
// also the sibling must not be beaten inside: both sides are already complete clusters
|
|
std::vector<int> u = cl[x];
|
|
u.insert(u.end(), cl[y].begin(), cl[y].end());
|
|
std::string s = "(" + txt[x] + "+" + txt[y] + ")";
|
|
cl[x] = u; txt[x] = s;
|
|
cl.erase(cl.begin() + y); txt.erase(txt.begin() + y);
|
|
did = true;
|
|
}
|
|
}
|
|
if (!did) { printf("could not build a complete binary tree (a non-binary or non-tree structure); clusters left: %zu\n", cl.size()); for (auto& s : txt) printf(" %s\n", s.c_str()); return 0; }
|
|
}
|
|
printf("tree: %s\n", txt[0].c_str());
|
|
return 0;
|
|
}
|