Files
Strata/engine/tests/ling32/ling32_codec_test.cpp

126 lines
6.1 KiB
C++

// tests/ling32/ling32_codec_test.cpp - Strata's S_K codecs against the hand-off pack's own, the FWHT-128, Q8_1, and the Section 5 self-check.
// Usage: ling32_codec_test DATA_DIR [FIXTURE.strata] [NUMPY_VECTORS_DIR]
// DATA_DIR holds pack_s{4,5,6,8}.bin (superblocks encoded by the pack's ling32_squant_codecs_v11.encode_sk_gpu) and pack_s*.f32
// (the pack's decode_sk_gpu of them): strata::quant::sk::dequant_sk_row must reproduce them BIT FOR BIT.
#include "strata/ling32/ling32_selfcheck.hpp"
#include "strata/ling32/ling32_sk.hpp"
#include <bit>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <random>
#include <string>
#include <vector>
using namespace strata::ling32;
using strata::quant::sk::SkCode;
namespace {
int g_fail = 0;
void check(bool ok, const std::string& what) {
std::printf(" %-100s %s\n", what.c_str(), ok ? "ok" : "FAIL");
if (!ok) ++g_fail;
}
bool slurp(const std::string& p, std::vector<uint8_t>& out) {
std::ifstream f(p, std::ios::binary);
if (!f) return false;
out.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
return true;
}
void codec_vs_files(const std::string& dir, const std::string& prefix, const char* who) {
for (int cn : {4, 5, 6, 8}) {
std::vector<uint8_t> bin, ref;
const std::string b = dir + "/" + prefix + "s" + std::to_string(cn) + ".bin", r = dir + "/" + prefix + "s" + std::to_string(cn) + ".f32";
if (!slurp(b, bin) || !slurp(r, ref)) { check(false, std::string(who) + " S" + std::to_string(cn) + "_K: vector files missing in " + dir); continue; }
const size_t n = ref.size() / 4;
std::vector<float> ours(n);
strata::quant::sk::dequant_sk_row(bin.data(), static_cast<SkCode>(cn), ours.data(), n);
size_t bad = 0;
double maxd = 0;
for (size_t i = 0; i < n; ++i) {
float rf;
std::memcpy(&rf, ref.data() + 4 * i, 4);
uint32_t a, bb;
std::memcpy(&a, &ours[i], 4);
std::memcpy(&bb, &rf, 4);
if (a != bb) { ++bad; maxd = std::fmax(maxd, std::fabs(ours[i] - rf)); }
}
check(bin.size() == n / 256 * sk_superblock_bytes(cn) && bad == 0,
std::string(who) + " S" + std::to_string(cn) + "_K: " + std::to_string(n / 256) + " superblocks decode bit-for-bit (" + std::to_string(bad) + " differ, max " + std::to_string(maxd) + ")");
}
}
} // namespace
int main(int argc, char** argv) {
const std::string dir = argc > 1 ? argv[1] : ".";
std::printf("[S_K codecs: strata::quant::sk vs the pack's codec]\n");
codec_vs_files(dir, "pack_", "pack");
if (argc > 3) codec_vs_files(argv[3], "", "numpy");
std::printf("[FWHT-128, Q8_1, fp16]\n");
{
std::mt19937 rng(5);
std::normal_distribution<float> nd(0, 1);
std::vector<float> x(2560), y(2560), z(2560);
for (auto& v : x) v = nd(rng);
fwht128_f32(x.data(), y.data(), x.size());
fwht128_f32(y.data(), z.data(), x.size());
double dm = 0, en = 0, ey = 0;
for (size_t i = 0; i < x.size(); ++i) { dm = std::fmax(dm, std::fabs(x[i] - z[i])); en += double(x[i]) * x[i]; ey += double(y[i]) * y[i]; }
check(dm < 1e-5 && std::fabs(en - ey) / en < 1e-6, "FWHT-128 is an orthonormal involution (H H x = x, |Hx| = |x|)");
// against the Sylvester definition H[i][j] = (-1)^popcount(i & j) / sqrt(128), in double
double md = 0;
for (int blk = 0; blk < 20; ++blk)
for (int i = 0; i < 128; ++i) {
double a = 0;
for (int j = 0; j < 128; ++j) a += ((std::popcount(static_cast<unsigned>(i & j)) & 1) ? -1.0 : 1.0) * x[blk * 128 + j];
md = std::fmax(md, std::fabs(a / std::sqrt(128.0) - y[blk * 128 + i]));
}
check(md < 1e-5, "FWHT-128 equals the Sylvester Hadamard matrix / sqrt(128)");
std::vector<strata::quant::sk::BlockQ8_1_G64> q(2560 / 64);
fwht128_quantize_q8_1(x.data(), q.data(), x.size());
bool qok = true;
for (size_t b = 0; b < q.size(); ++b) {
int s = 0;
for (int i = 0; i < 64; ++i) { s += q[b].qs[i]; qok = qok && std::fabs(q[b].qs[i] * q[b].d - y[b * 64 + i]) <= q[b].d * 0.5001f; }
qok = qok && std::fabs(q[b].s - q[b].d * s) < 1e-4f * (1 + std::fabs(q[b].s));
}
check(qok, "Q8_1(G64) of the FWHT output: |q d - y| <= d/2, s = d * sum(q)");
bool hok = true;
for (uint32_t h = 0; h < 65536; ++h) {
const float f = f16_bits_to_f32(static_cast<uint16_t>(h));
if (std::isnan(f)) continue;
hok = hok && f32_to_f16_bits(f) == h;
}
check(hok, "fp16 <-> float round trip over all 65536 halves");
}
if (argc > 2) {
std::printf("[Section 5 self-check of %s]\n", argv[2]);
Ling32File f;
std::string err;
if (!f.open(argv[2], err)) { check(false, "opens: " + err); }
else {
const SelfcheckResult r = verify_section5_selfcheck(f);
check(r.ok, "Section 5 golden vectors reproduce: " + r.error);
check(r.records.size() == 4, "four codec records (S4_K S5_K S6_K S8_K)");
for (const auto& rec : r.records)
std::printf(" code %d: %zu weights, fp16 mismatches %zu, dot err %.2e, fwht dot err %.2e | Q8_1 activation path: %.2e, %.2e\n", rec.code_id, rec.n, rec.dec_mismatch,
rec.err_dot, rec.err_dot_fwht, rec.err_q8_dot, rec.err_q8_fwht);
check(r.max_rel_err < 1e-5, "max relative error of the exact checks < 1e-5 (startup aborts above 1e-3)");
// a corrupted Section 5 must fail
std::vector<uint8_t> s5;
f.reader().read_section(5, s5, err);
uint32_t jl;
std::memcpy(&jl, s5.data(), 4);
s5[4 + jl + 200] ^= 0x5A; // a byte inside the first record's encoded weights
check(!verify_section5_selfcheck(s5).ok, "a flipped byte in the encoded weights fails the self-check");
}
}
std::printf("%s (%d failed)\n", g_fail ? "FAIL" : "PASS", g_fail);
return g_fail ? 1 : 0;
}