mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-06 00:08:52 +00:00
The old header is replaced (guard renamed), the transition comparison test and the one-off build scripts that compared old and new are removed.
211 lines
8.6 KiB
C++
211 lines
8.6 KiB
C++
// dequant_vectors_test: the dequantisers (artifact/dequant.hpp) against the format-spec vectors.
|
|
// - phaseA-tests/clean/p2 vectors (random blocks, float32 expected, %.9g): compared EXACTLY (bits; +0 and -0 count equal)
|
|
// - hy4gguf vectors (real blocks, float64 expected): compared within 4 float32 ulps (the float32 chain rounds at each step)
|
|
// Plus synthetic edge blocks (subnormal / maximal halves, extreme quants) checked against an independent float64 evaluation.
|
|
#include "strata/artifact/dequant.hpp"
|
|
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vec_util.hpp"
|
|
|
|
using namespace strata;
|
|
|
|
namespace {
|
|
|
|
int g_fail = 0;
|
|
|
|
struct Case {
|
|
const char* dir;
|
|
const char* name;
|
|
int block_bytes;
|
|
int block_elems;
|
|
std::function<void(const uint8_t*, float*)> fn;
|
|
bool exact;
|
|
};
|
|
|
|
void run_case(const std::string& root, const Case& c) {
|
|
std::vector<uint8_t> bin;
|
|
std::vector<double> exp;
|
|
const std::string base = root + "/" + c.dir + "/" + c.name;
|
|
if (!cp2::read_file(base + ".bin", bin) || !cp2::read_values(base + ".expected.txt", exp)) {
|
|
std::printf("FAIL %s: cannot read vectors at %s\n", c.name, base.c_str());
|
|
++g_fail;
|
|
return;
|
|
}
|
|
const size_t nb = bin.size() / c.block_bytes;
|
|
if (bin.size() % c.block_bytes || exp.size() != nb * c.block_elems) {
|
|
std::printf("FAIL %s: size mismatch (bin %zu, expected %zu values)\n", c.name, bin.size(), exp.size());
|
|
++g_fail;
|
|
return;
|
|
}
|
|
std::vector<float> out(c.block_elems);
|
|
int64_t worst = 0;
|
|
size_t bad = 0;
|
|
for (size_t b = 0; b < nb; ++b) {
|
|
c.fn(bin.data() + b * c.block_bytes, out.data());
|
|
for (int i = 0; i < c.block_elems; ++i) {
|
|
const float want = (float)exp[b * c.block_elems + i];
|
|
const int64_t d = cp2::ulp_diff(out[i], want);
|
|
if (d > worst) worst = d;
|
|
if (c.exact ? d != 0 : d > 4) ++bad;
|
|
}
|
|
}
|
|
std::printf("%s %-12s %zu blocks, max ulp diff %lld\n", bad ? "FAIL" : "ok ", c.name, nb, (long long)worst);
|
|
if (bad) ++g_fail;
|
|
}
|
|
|
|
// ---- independent float64 check of edge blocks -------------------------------------------------------------
|
|
void put_half(uint8_t* p, uint16_t h) {
|
|
p[0] = (uint8_t)(h & 255);
|
|
p[1] = (uint8_t)(h >> 8);
|
|
}
|
|
|
|
double half64(uint16_t h) {
|
|
const int s = h >> 15, e = (h >> 10) & 31, m = h & 1023;
|
|
double v = e == 0 ? std::ldexp((double)m, -24) : std::ldexp((double)(1024 + m), e - 25);
|
|
return s ? -v : v;
|
|
}
|
|
|
|
void check_close(const char* what, const float* got, const double* want, int n) {
|
|
for (int i = 0; i < n; ++i) {
|
|
const float w = (float)want[i];
|
|
if (cp2::ulp_diff(got[i], w) > 3 && std::fabs((double)got[i] - want[i]) > 1e-30) {
|
|
std::printf("FAIL %s: elem %d got %.9g want %.9g\n", what, i, got[i], want[i]);
|
|
++g_fail;
|
|
return;
|
|
}
|
|
}
|
|
std::printf("ok %s\n", what);
|
|
}
|
|
|
|
void edge_blocks() {
|
|
const uint16_t halves[] = {0x0001, 0x03FF, 0x0400, 0x3C00, 0x7BFF, 0x8001, 0xFBFF, 0x0000};
|
|
// Q8_0: every half scale x extreme quants
|
|
for (uint16_t h : halves) {
|
|
uint8_t blk[34];
|
|
put_half(blk, h);
|
|
for (int i = 0; i < 32; ++i) blk[2 + i] = (uint8_t)(i == 0 ? 0x80 : i == 1 ? 0x7F : i * 8 + 3);
|
|
float out[32];
|
|
double want[32];
|
|
dequantize_q8_0(blk, out);
|
|
for (int i = 0; i < 32; ++i) want[i] = half64(h) * (int8_t)blk[2 + i];
|
|
check_close("q8_0 edge", out, want, 32);
|
|
}
|
|
// Q6_K: scales -128 / 127, q extremes -32 / 31
|
|
for (uint16_t h : halves) {
|
|
uint8_t blk[210];
|
|
std::memset(blk, 0, sizeof blk);
|
|
for (int i = 0; i < 128; ++i) blk[i] = (i & 1) ? 0xFF : 0x00;
|
|
for (int i = 0; i < 64; ++i) blk[128 + i] = (i & 2) ? 0xFF : 0x00;
|
|
for (int s = 0; s < 16; ++s) blk[192 + s] = (uint8_t)((s & 1) ? 127 : 0x80);
|
|
put_half(blk + 208, h);
|
|
float out[256];
|
|
dequantize_q6_K(blk, out);
|
|
double want[256];
|
|
for (int n = 0; n < 2; ++n)
|
|
for (int p = 0; p < 4; ++p)
|
|
for (int l = 0; l < 32; ++l) {
|
|
const int a = blk[64 * n + (p & 1) * 32 + l];
|
|
const int low4 = p < 2 ? (a & 15) : (a >> 4);
|
|
const int high2 = (blk[128 + 32 * n + l] >> (2 * p)) & 3;
|
|
const int q = low4 + 16 * high2 - 32;
|
|
const int s = 8 * n + 2 * p + (l >> 4);
|
|
want[128 * n + 32 * p + l] = half64(h) * (int8_t)blk[192 + s] * q;
|
|
}
|
|
check_close("q6_K edge", out, want, 256);
|
|
}
|
|
// Q4_K: sc = m = 63 and nibbles 0 / 15
|
|
for (uint16_t h : halves) {
|
|
uint8_t blk[144];
|
|
std::memset(blk, 0xFF, sizeof blk);
|
|
put_half(blk, h);
|
|
put_half(blk + 2, 0x7BFF);
|
|
for (int i = 0; i < 128; ++i) blk[16 + i] = (i & 1) ? 0x0F : 0xF0;
|
|
float out[256];
|
|
dequantize_q4_K(blk, out);
|
|
double want[256];
|
|
for (int g = 0; g < 4; ++g)
|
|
for (int hh = 0; hh < 2; ++hh)
|
|
for (int l = 0; l < 32; ++l) {
|
|
const int byte = blk[16 + 32 * g + l];
|
|
const int q = hh ? (byte >> 4) : (byte & 15);
|
|
want[64 * g + 32 * hh + l] = half64(h) * 63 * q - half64(0x7BFF) * 63;
|
|
}
|
|
check_close("q4_K edge", out, want, 256);
|
|
}
|
|
// fp16 conversion: every half pattern against the float64 value (finite) and class for inf / nan
|
|
int bad = 0;
|
|
for (uint32_t h = 0; h < 65536; ++h) {
|
|
const float f = fp16_to_fp32((uint16_t)h);
|
|
const int e = (h >> 10) & 31;
|
|
if (e == 31) {
|
|
const uint32_t u = cp2::f2u(f);
|
|
const uint32_t want = ((h & 0x8000u) << 16) | 0x7F800000u | ((h & 0x3FFu) << 13);
|
|
if (u != want) ++bad;
|
|
} else if ((double)f != half64((uint16_t)h) || std::signbit(f) != (bool)(h >> 15)) {
|
|
++bad;
|
|
}
|
|
// bf16
|
|
if (cp2::f2u(bf16_to_fp32((uint16_t)h)) != ((uint32_t)h << 16)) ++bad;
|
|
}
|
|
std::printf("%s fp16/bf16 conversion over all 65536 patterns\n", bad ? "FAIL" : "ok ");
|
|
if (bad) ++g_fail;
|
|
uint8_t le[2] = {0x34, 0x12};
|
|
if (read_u16(le) != 0x1234) { std::printf("FAIL read_u16\n"); ++g_fail; }
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
const std::string root = argc > 1 ? argv[1] : cp2::data_dir();
|
|
const Case cases[] = {
|
|
{"format-vectors", "q2_0", 18, 64, dequantize_q2_0, true},
|
|
{"format-vectors", "q4_0", 18, 32, dequantize_q4_0, true},
|
|
{"format-vectors", "q5_0", 22, 32, dequantize_q5_0, true},
|
|
{"format-vectors", "q5_1", 24, 32, dequantize_q5_1, true},
|
|
{"format-vectors", "q5_k", 176, 256, dequantize_q5_K, true},
|
|
{"format-vectors", "iq4_nl", 18, 32, dequantize_iq4_nl, true},
|
|
{"format-vectors", "iq4_xs", 136, 256, dequantize_iq4_xs, true},
|
|
{"hy4-vectors", "q8_0_ffn", 34, 32, dequantize_q8_0, false},
|
|
{"hy4-vectors", "q8_0_embd", 34, 32, dequantize_q8_0, false},
|
|
{"hy4-vectors", "q4k_gate", 144, 256, dequantize_q4_K, false},
|
|
{"hy4-vectors", "q4k_down", 144, 256, dequantize_q4_K, false},
|
|
{"hy4-vectors", "q6k_down", 210, 256, dequantize_q6_K, false},
|
|
{"hy4-vectors", "q3k_gate", 110, 256, dequantize_q3_K, false},
|
|
};
|
|
for (const Case& c : cases) run_case(root, c);
|
|
|
|
// BF16 vector
|
|
{
|
|
std::vector<uint8_t> bin;
|
|
std::vector<double> exp;
|
|
if (!cp2::read_file(root + "/hy4-vectors/bf16_out.bin", bin) || !cp2::read_values(root + "/hy4-vectors/bf16_out.expected.txt", exp)) {
|
|
std::printf("FAIL bf16 vectors unreadable\n");
|
|
++g_fail;
|
|
} else {
|
|
std::vector<float> out(exp.size());
|
|
dequantize_bf16(bin.data(), out.data(), (int)exp.size());
|
|
size_t bad = 0;
|
|
for (size_t i = 0; i < exp.size(); ++i) bad += (out[i] != (float)exp[i]);
|
|
std::printf("%s bf16_out %zu values\n", bad ? "FAIL" : "ok ", exp.size());
|
|
if (bad) ++g_fail;
|
|
// f32 copy and f16 path
|
|
std::vector<float> c(exp.size());
|
|
dequantize_f32((const uint8_t*)out.data(), c.data(), (int)out.size());
|
|
if (std::memcmp(c.data(), out.data(), out.size() * 4)) { std::printf("FAIL f32 copy\n"); ++g_fail; }
|
|
const uint8_t h16[4] = {0x00, 0x3C, 0x01, 0x00};
|
|
float f2[2];
|
|
dequantize_f16(h16, f2, 2);
|
|
if (f2[0] != 1.0f || f2[1] != std::ldexp(1.0f, -24)) { std::printf("FAIL f16 path\n"); ++g_fail; }
|
|
}
|
|
}
|
|
edge_blocks();
|
|
std::printf("%s (%d failure(s))\n", g_fail ? "FAILED" : "PASSED", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|