okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-2a (7c09c4a75) into f-own-int: the Strata quant layer and the x86 CPU rows9c143c403

main
4.4 KiB80 linesraw
1// tests/quant/quant_selftest.cpp - the layer without ggml: type table, probe, and every kernel against the reference (selfcheck.hpp).
2#include <cstdio>
3#include <cstring>
4
5#include "strata/quant/act.hpp"
6#include "strata/quant/dot.hpp"
7#include "strata/quant/isa.hpp"
8#include "strata/quant/selfcheck.hpp"
9#include "strata/quant/types.hpp"
10
11namespace sq = strata::quant;
12
13static int g_fail = 0;
14#define CHECK(c, ...)                                  \
15    do {                                               \
16        if (!(c)) {                                    \
17            ++g_fail;                                  \
18            std::printf("FAIL line %d: ", __LINE__);   \
19            std::printf(__VA_ARGS__);                  \
20            std::printf("\n");                         \
21        }                                              \
22    } while (0)
23
24int main() {
25    const sq::Caps& c = sq::caps();
26    std::printf("quant_selftest: best isa %s, default contract %s (avx=%d avx2=%d avx512=%d vnni=%d neon=%d)\n", sq::isa_name(sq::best_isa()),
27                sq::arith_name(sq::default_arith()), c.avx, c.avx2, c.avx512, c.avx512_vnni, c.neon);
28
29    // the table: sizes the GGUF format fixes, unknown ids answered nullptr
30    CHECK(sq::row_bytes(12, 256) == 144, "q4_K row of 256 is 144 bytes");
31    CHECK(sq::row_bytes(15, 512) == 584, "q8_K row of 512");
32    CHECK(sq::row_bytes(8, 64) == 68, "q8_0 row of 64");
33    CHECK(sq::row_bytes(42, 128) == 36, "q2_0 row of 128");
34    CHECK(sq::row_bytes(12, 100) == 0, "not whole blocks");
35    CHECK(sq::type_info(43) == nullptr && sq::type_info(-1) == nullptr && sq::type_info(100) == nullptr && sq::type_info(4) == nullptr, "unknown ids");
36    CHECK(sq::type_info(sq::Type::IQ4_NL)->act == sq::Type::Q8_0, "iq4_nl takes q8_0");
37    CHECK(sq::type_info(sq::Type::Q5_1)->act == sq::Type::Q8_1, "q5_1 takes q8_1");
38    CHECK(sq::type_info(sq::Type::IQ2_S)->act == sq::Type::Q8_K, "iq2_s takes q8_K");
39
40    // every dot kernel the layer has, that this CPU can run, against the Generic reference on exact data
41    int dots = 0, acts = 0;
42    for (int t = 0; t < 150; ++t) {
43        const sq::TypeInfo* ti = sq::type_info(t);
44        if (ti == nullptr || !ti->has_dot) continue;
45        for (sq::Arith a : {sq::Arith::Generic, sq::Arith::Avx, sq::Arith::Avx2, sq::Arith::Neon}) {
46            const sq::DotKernel* ks[16];
47            const int n = sq::dot_kernels(ti->type, a, ks, 16);
48            for (int i = 0; i < n; ++i) {
49                if (!sq::isa_ok(ks[i]->isa)) continue;
50                std::string why;
51                const bool ok = sq::selfcheck(*ks[i], &why);
52                CHECK(ok, "%s", why.c_str());
53                ++dots;
54            }
55        }
56        CHECK(sq::dot_kernel(ti->type, sq::Arith::Generic) != nullptr, "%s has no Generic dot", ti->name);
57        CHECK(sq::checked_dot_kernel(ti->type, sq::Arith::Generic) != nullptr, "%s has no checked Generic kernel", ti->name);
58        CHECK(sq::checked_dot_kernel(ti->type, sq::default_arith()) != nullptr, "%s has no checked kernel of the default contract", ti->name);
59    }
60    for (sq::Type t : {sq::Type::Q8_0, sq::Type::Q8_1, sq::Type::Q8_K})
61        for (sq::Arith a : {sq::Arith::Generic, sq::Arith::Avx, sq::Arith::Avx2, sq::Arith::Neon}) {
62            const sq::ActKernel* k = sq::act_kernel(t, a);
63            if (k == nullptr || k->arith != a) continue;
64            std::string why;
65            CHECK(sq::selfcheck(*k, &why), "%s", why.c_str());
66            ++acts;
67        }
68    for (sq::Type t : {sq::Type::Q8_0, sq::Type::Q8_1, sq::Type::Q8_K}) { CHECK(sq::checked_act_kernel(t, sq::Arith::Generic) != nullptr, "no checked Generic activation kernel"); CHECK(sq::checked_act_kernel(t, sq::default_arith()) != nullptr, "no checked activation kernel of the default contract"); }
69
70    // the Q4_0 requantizer: a ramp of 32 values 0..31 -> d = max/-8 with max = 31 -> d = -3.875, ids from the ggml rule
71    float ramp[32];
72    for (int i = 0; i < 32; ++i) ramp[i] = (float) i;
73    unsigned char blk[18];
74    sq::quantize_q4_0_ref(ramp, blk, 32);
75    CHECK(blk[0] == 0xC0 && blk[1] == 0xC3, "q4_0 scale of the ramp is fp16 -3.875 (0xC3C0), got %02x%02x", blk[1], blk[0]);
76    CHECK(blk[2] == 0x48, "q4_0 first byte of the ramp is 0x48 (x=0 -> 8, x=16 -> 4), got %02x", blk[2]);
77
78    std::printf("quant_selftest: %d dot kernels, %d activation kernels checked, %d failures -> %s\n", dots, acts, g_fail, g_fail ? "FAIL" : "PASS");
79    return g_fail ? 1 : 0;
80}