okami.mom/strata

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

Niko1221Merge branch 'f-140' into f-140-j0fff115df

main
3.5 KiB86 linesraw
1// Real-artifact Q5_0 / Q5_1 / Q8_0 PLE rows against ggml's reference dequantizer, through both readers (the mapped
2// one and the direct SSD reader).
3#define NOMINMAX
4#include "strata/artifact/gguf_reader.hpp"
5#include "strata/kernels/ngram.hpp"
6#include "ggml.h"
7
8#include <algorithm>
9#include <cmath>
10#include <cstdio>
11#include <string>
12#include <vector>
13
14namespace {
15
16// Largest absolute difference between the table's rows and ggml's dequantization of the same bytes, single rows
17// and one 16-row batch; -1 when the table cannot be opened or read.
18double check(const char* path, strata::kernels::PleIo mode, const uint8_t* bytes, size_t row_bytes,
19             const ggml_type_traits* traits, std::string& format, size_t& n_rows) {
20    strata::kernels::PleIoOptions options;
21    options.mode = mode;
22    strata::kernels::PleTable table;
23    std::string err;
24    if (!table.open(path, err, options)) {
25        std::fprintf(stderr, "open: %s\n", err.c_str());
26        return -1.0;
27    }
28    format = table.format();
29    n_rows = (size_t) table.rows();
30    const uint32_t probes[] = {0, 1, 12345, 20000003, (uint32_t) (table.rows() - 1)};
31    double max_abs = 0.0;
32    for (uint32_t row : probes) {
33        float got[160], want[160];
34        table.read_row(row, got);
35        traits->to_float(bytes + (size_t) row * row_bytes, want, 160);
36        for (int i = 0; i < 160; ++i)
37            max_abs = std::max(max_abs, (double) std::fabs(got[i] - want[i]));
38    }
39    uint32_t rows[16];
40    for (int i = 0; i < 16; ++i) rows[i] = probes[i % 5];
41    float batch[16 * 160];
42    if (!table.issue(rows) || !table.collect(batch, err)) {
43        std::fprintf(stderr, "collect: %s\n", err.c_str());
44        return -1.0;
45    }
46    for (int h = 0; h < 16; ++h) {
47        float want[160];
48        traits->to_float(bytes + (size_t) rows[h] * row_bytes, want, 160);
49        for (int i = 0; i < 160; ++i)
50            max_abs = std::max(max_abs, (double) std::fabs(batch[h * 160 + i] - want[i]));
51    }
52    return max_abs;
53}
54
55}  // namespace
56
57int main(int argc, char** argv) {
58    if (argc != 2) {
59        std::fprintf(stderr, "usage: ple_q5_parity <gguf-containing-ple>\n");
60        return 2;
61    }
62    strata::GgufFile gguf(argv[1]);
63    const auto* tensor = gguf.find("per_layer_token_embd.weight");
64    // Q5_0 (#296: OrcaRouter's Q4_K_S), Q5_1 (a Q5_K_M GGUF), Q8_0 (UD-Q6_K_XL, Swift-1.5 Q4_K_L): 5 blocks of
65    // 32 per row
66    if (!tensor || (tensor->type != GGML_TYPE_Q5_0 && tensor->type != GGML_TYPE_Q5_1 && tensor->type != GGML_TYPE_Q8_0) ||
67        tensor->shape.size() != 2 || tensor->shape[0] != 160) {
68        std::fprintf(stderr, "expected a Q5_0, Q5_1 or Q8_0 PLE [160, N]\n");
69        return 2;
70    }
71    const auto type = (ggml_type) tensor->type;
72    const size_t row_bytes = type == GGML_TYPE_Q5_0 ? 110 : type == GGML_TYPE_Q5_1 ? 120 : 170;
73    const auto* traits = ggml_get_type_traits(type);
74    const uint8_t* bytes = gguf.tensor_data(*tensor);
75    bool ok = true;
76    for (auto mode : {strata::kernels::PleIo::Mmap, strata::kernels::PleIo::Direct}) {
77        std::string format;
78        size_t n_rows = 0;
79        const double max_abs = check(argv[1], mode, bytes, row_bytes, traits, format, n_rows);
80        const bool pass = max_abs >= 0.0 && max_abs <= 1e-6;
81        std::printf("%s PLE, %s reader: %zu rows, max_abs %.3e %s\n", format.c_str(),
82                    mode == strata::kernels::PleIo::Mmap ? "mapped" : "direct", n_rows, max_abs, pass ? "PASS" : "FAIL");
83        ok = ok && pass;
84    }
85    return ok ? 0 : 1;
86}