okami.mom/strata

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

Niko1221tests (#264 follow-up): iq_parity skips (exit 3) when no fixture was generated, as its generator does; a partly missing set still fails; the open handle of a half-present fixture is closeda37491ede

main
6.4 KiB125 linesraw
1// src/kernels/iq_parity.cpp - plan v0.3 P6: the i-quant kernels against gguf-py on generated rows.
2//
3//     python tools/iq_fixture.py --out <dir> && build/iq_parity <dir>
4//
5// The fixtures are GENERATED (deterministic; TODO 24) - tools/iq_fixture.py writes each type's raw block
6// bytes and the gguf-py dequantized reference (Q2_0, this repository's type 42, is dequantized by
7// tools/gguf_writer.py's codec instead - gguf-py has no such type).  Dependencies: python3 with numpy and
8// the vendored gguf-py that setup downloads (the pinned llama.cpp zip; third_party/ is gitignored).
9//
10// What is compared, and how: the dequant parity is a MEASURED relative error (sum|got-ref| / sum|ref|) -
11// bitwise equality is not claimed and not required; on the generated fixtures it measures 0.00e+00.  The
12// MMVQ dot (q8_1 activations) must match the float matrix-vector product over that reference within the
13// activation rounding (a few 1e-3 relative), for 1 to 8 columns, every column of a multi-column call bitwise
14// equal to a one-column call on it.
15#include "strata/kernels/iq_kernels.hpp"
16#include "strata/kernels/native_mmvq.hpp"
17
18#include <cuda_runtime.h>
19
20#include <cmath>
21#include <cstdio>
22#include <cstring>
23#include <random>
24#include <string>
25#include <vector>
26
27int main(int argc, char** argv) {
28    const std::string dir = argc > 1 ? argv[1] : "logs/iq_fixture";
29    const char* names[] = {"IQ2_XXS", "IQ2_XS", "IQ2_S", "IQ3_XXS", "IQ3_S", "IQ1_M", "IQ4_NL", "IQ4_XS", "Q2_0", "Q3_K"};
30    int failures = 0, missing = 0;
31    cudaStream_t s;
32    cudaStreamCreate(&s);
33    for (const char* nm : names) {
34        std::FILE* f = std::fopen((dir + "/" + nm + ".bin").c_str(), "rb");
35        std::FILE* g = std::fopen((dir + "/" + nm + ".f32").c_str(), "rb");
36        if (!f || !g) {
37            std::printf("%-8s missing fixture: %s/%s.bin or %s/%s.f32 - generate the fixtures first with "
38                        "python tools/iq_fixture.py --out %s (deterministic; see the tool's --help)\n",
39                        nm, dir.c_str(), nm, dir.c_str(), nm, dir.c_str());
40            if (f) std::fclose(f);
41            if (g) std::fclose(g);
42            ++failures;
43            ++missing;
44            continue;
45        }
46        int hdr[3];
47        std::fread(hdr, 4, 3, f);
48        const int type = hdr[0], rows = hdr[1], cols = hdr[2];
49        std::vector<uint8_t> raw;
50        { uint8_t buf[1 << 16]; size_t n; while ((n = std::fread(buf, 1, sizeof buf, f)) > 0) raw.insert(raw.end(), buf, buf + n); }
51        std::vector<float> ref((size_t) rows * cols);
52        std::fread(ref.data(), 4, ref.size(), g);
53        std::fclose(f);
54        std::fclose(g);
55        void* dw = nullptr;
56        float* dq = nullptr;
57        cudaMalloc(&dw, raw.size());
58        cudaMalloc(&dq, ref.size() * 4);
59        cudaMemcpy(dw, raw.data(), raw.size(), cudaMemcpyHostToDevice);
60        double dq_err = 0.0;
61        if (strata::kernels::iq_supported(type) && ((size_t) rows * cols) % 256 == 0) {
62            strata::kernels::iq_dequant_f32(type, dw, (int64_t) rows * cols, dq, s);
63            std::vector<float> got(ref.size());
64            cudaMemcpy(got.data(), dq, got.size() * 4, cudaMemcpyDeviceToHost);
65            double num = 0, den = 0;
66            for (size_t i = 0; i < ref.size(); ++i) { num += std::fabs(got[i] - ref[i]); den += std::fabs(ref[i]); }
67            dq_err = num / (den + 1e-30);
68        }
69        // the dot, ncols 1..8: each column of an ncols call bitwise equal to a one-column call on it (the
70        // exact multi-column layout, the default), all of them within the activation rounding of the float product
71        const int MC = 8;
72        std::mt19937 rng(7);
73        std::normal_distribution<float> nd(0.f, 1.f);
74        std::vector<float> x((size_t) MC * cols);
75        for (auto& v : x) v = nd(rng);
76        float* dx = nullptr;
77        void* xq = nullptr;
78        float* dy = nullptr;
79        cudaMalloc(&dx, x.size() * 4);
80        cudaMalloc(&xq, (size_t) MC * cols / 32 * 36);
81        cudaMalloc(&dy, (size_t) MC * rows * 4);
82        cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice);
83        strata::kernels::quantize_q8_1_rows(dx, MC, cols, xq, s);
84        std::vector<float> y((size_t) MC * rows), yn(y.size());
85        int multi_bad = 0;
86        try {
87            for (int c = 0; c < MC; ++c)
88                strata::kernels::native_mmvq(type, dw, (const uint8_t*) xq + (size_t) c * cols / 32 * 36,
89                                             dy + (size_t) c * rows, cols, rows, 1, s);
90            cudaStreamSynchronize(s);
91            cudaMemcpy(y.data(), dy, y.size() * 4, cudaMemcpyDeviceToHost);
92            for (int nc = 2; nc <= MC; ++nc) {
93                strata::kernels::native_mmvq(type, dw, xq, dy, cols, rows, nc, s);
94                cudaStreamSynchronize(s);
95                cudaMemcpy(yn.data(), dy, yn.size() * 4, cudaMemcpyDeviceToHost);
96                if (strata::kernels::native_mmvq_multi_exact() &&
97                    std::memcmp(yn.data(), y.data(), (size_t) nc * rows * 4) != 0) {
98                    std::printf("%-8s mmvq ncols %d: not bitwise equal to the one-column calls\n", nm, nc);
99                    ++multi_bad;
100                }
101            }
102        } catch (const std::exception& e) { std::printf("%-8s mmvq: %s\n", nm, e.what()); ++failures; continue; }
103        double num = 0, den = 0;
104        for (int c = 0; c < MC; ++c)
105            for (int r = 0; r < rows; ++r) {
106                double acc = 0;
107                for (int k = 0; k < cols; ++k) acc += (double) ref[(size_t) r * cols + k] * x[(size_t) c * cols + k];
108                num += std::fabs(y[(size_t) c * rows + r] - acc);
109                den += std::fabs(acc);
110            }
111        const double mm_err = num / (den + 1e-30);
112        const bool ok = dq_err < 1e-6 && mm_err < 2e-2 && multi_bad == 0;
113        std::printf("%-8s type %2d %4d x %5d  dequant rel %.2e  mmvq rel %.2e (ncols 1..%d)  %s\n", nm, type, rows, cols,
114                    dq_err, mm_err, MC, ok ? "ok" : "FAIL");
115        if (!ok) ++failures;
116        cudaFree(dw); cudaFree(dq); cudaFree(dx); cudaFree(xq); cudaFree(dy);
117    }
118    std::printf("iq_parity: %d failures\n", failures);
119    // no fixture at all: the generator was skipped (no numpy / vendored gguf-py) - a skip (3), not a failure
120    if (missing == (int) (sizeof names / sizeof names[0])) {
121        std::printf("iq_parity: no fixtures in %s - skipped\n", dir.c_str());
122        return 3;
123    }
124    return failures ? 1 : 0;
125}