okami.mom/strata

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

Niko1221Strata: Qwen3.8-Flash-Next on a 12-24 GB NVIDIA GPU + 64 GB RAM, one-click install01529aaa8

main
5.6 KiB123 linesraw
1// src/artifact/dequant.cpp - the `strata-dequant` CLI (dequantize a GGUF tensor and dump F32).
2// The dequantizers themselves are header-only in include/strata/artifact/dequant.hpp.
3#include "strata/artifact/dequant.hpp"
4
5#ifdef STRATA_DEQUANT_SELFTEST
6int main(int argc, char** argv) {
7    if (argc < 2) {
8        std::printf("usage: dequant <file.gguf>\n");
9        return 2;
10    }
11    try {
12        strata::GgufFile g(argv[1]);
13        int checked = 0, bad = 0;
14        for (const auto& t : g.tensors()) {
15            if (t.type != 42) continue; // Q2_0 only for now
16            if (checked >= 8) break;
17            const uint8_t* base = g.tensor_data(t);
18            const uint64_t nblocks = t.elements() / 64;
19            for (uint64_t b = 0; b < nblocks && b < 64; ++b) {
20                const uint8_t* blk = base + b * 18;
21                float out[64];
22                strata::dequantize_q2_0(blk, out);
23                const float d = strata::fp16_to_fp32(strata::read_u16(blk));
24                // STRUCTURAL INVARIANT: every value must be exactly one of {-d, 0, d, 2d}.
25                for (int j = 0; j < 64; ++j) {
26                    const float v = out[j];
27                    const bool ok = (v == -d) || (v == 0.0f) || (v == d) || (v == 2.0f * d) || (d == 0.0f);
28                    if (!ok) {
29                        ++bad;
30                        if (bad < 4)
31                            std::printf("  VIOLATION %s block %llu elem %d: v=%g d=%g\n", t.name.c_str(),
32                                        (unsigned long long)b, j, v, d);
33                    }
34                }
35            }
36            if (checked == 0) {
37                // A folded checksum over the FIRST 64 blocks, for cross-checking against the Python
38                // implementation on identical bytes. A single implementation being self-consistent
39                // proves little - two disagreeing is what finds bugs (round 67).
40                double sum = 0, sumabs = 0;
41                const uint8_t* base = g.tensor_data(t);
42                for (uint64_t b = 0; b < nblocks && b < 64; ++b) {
43                    float out[64];
44                    strata::dequantize_q2_0(base + b * 18, out);
45                    for (int j = 0; j < 64; ++j) {
46                        sum += out[j];
47                        sumabs += std::fabs(out[j]);
48                    }
49                }
50                std::printf("  CHECKSUM %s sum=%.10g sumabs=%.10g\n", t.name.c_str(), sum, sumabs);
51            }
52            ++checked;
53        }
54        // Q5_0 cross-check on the same 64-block window gguf-py is given, using the first Q5_0 tensor.
55        for (const auto& t : g.tensors()) {
56            if (t.type != 6 && t.type != 20 && t.type != 14 && t.type != 12 && t.type != 13 && t.type != 11 &&
57                t.type != 23)
58                continue;
59            const uint8_t* base = g.tensor_data(t);
60            const int bsz =
61                (t.type == 20)
62                    ? 18
63                    : (t.type == 14
64                           ? 210
65                           : (t.type == 12
66                                  ? 144
67                                  : (t.type == 13 ? 176 : (t.type == 11 ? 110 : (t.type == 23 ? 136 : 22)))));
68            const int nel =
69                (t.type == 14 || t.type == 12 || t.type == 13 || t.type == 11 || t.type == 23) ? 256 : 32;
70            const uint64_t nb = t.elements() / (uint64_t)nel;
71            double sum = 0, sumabs = 0;
72            for (uint64_t b = 0; b < nb && b < 64; ++b) {
73                float out[256];
74                if (t.type == 20)
75                    strata::dequantize_iq4_nl(base + b * bsz, out);
76                else if (t.type == 14)
77                    strata::dequantize_q6_K(base + b * bsz, out);
78                else if (t.type == 12)
79                    strata::dequantize_q4_K(base + b * bsz, out);
80                else if (t.type == 13)
81                    strata::dequantize_q5_K(base + b * bsz, out);
82                else if (t.type == 11)
83                    strata::dequantize_q3_K(base + b * bsz, out);
84                else if (t.type == 23)
85                    strata::dequantize_iq4_xs(base + b * bsz, out);
86                else
87                    strata::dequantize_q5_0(base + b * bsz, out);
88                for (int j = 0; j < nel; ++j) {
89                    sum += out[j];
90                    sumabs += std::fabs(out[j]);
91                }
92            }
93            std::printf("  CHECKSUM %s %s sum=%.10g sumabs=%.10g\n", t.type_name(), t.name.c_str(), sum,
94                        sumabs);
95            float f8[256];
96            if (t.type == 20)
97                strata::dequantize_iq4_nl(base, f8);
98            else if (t.type == 14)
99                strata::dequantize_q6_K(base, f8);
100            else if (t.type == 12)
101                strata::dequantize_q4_K(base, f8);
102            else if (t.type == 13)
103                strata::dequantize_q5_K(base, f8);
104            else if (t.type == 11)
105                strata::dequantize_q3_K(base, f8);
106            else if (t.type == 23)
107                strata::dequantize_iq4_xs(base, f8);
108            else
109                strata::dequantize_q5_0(base, f8);
110            std::printf("  REF first8:");
111            for (int j = 0; j < 8; ++j) std::printf(" %.6g", f8[j]);
112            std::printf("\n");
113            if (t.type == 23) break; // emit Q5_0 first, then IQ4_NL
114        }
115        std::printf("%s\n  Q2_0 tensors spot-checked: %d\n  symbolic invariant violations: %d -> %s\n",
116                    argv[1], checked, bad, bad ? "FAIL" : "PASS");
117        return bad ? 1 : 0;
118    } catch (const std::exception& e) {
119        std::printf("ERROR: %s\n", e.what());
120        return 1;
121    }
122}
123#endif