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
3.5 KiB67 linesraw
1// src/artifact/gguf_reader.cpp - the `strata-gguf` CLI.  The reader itself is header-only in
2// include/strata/artifact/gguf_reader.hpp; this file is just the entry point, so it cannot drift
3// from the library it exercises.
4#include "strata/artifact/gguf_reader.hpp"
5
6#ifndef STRATA_GGUF_MAIN_DISABLED
7int main(int argc, char** argv) {
8    if (argc < 2) {
9        std::printf("usage: gguf_reader <file.gguf> [--check]\n");
10        return 2;
11    }
12    const bool check = (argc > 2 && std::string(argv[2]) == "--check");
13    try {
14        strata::GgufFile g(argv[1]);
15        std::printf("%s\n", argv[1]);
16        std::printf("  version %u   tensors %zu   metadata %zu   data_start %llu   size %llu\n", g.version(),
17                    g.tensors().size(), g.metadata().size(), (unsigned long long)g.data_start(),
18                    (unsigned long long)g.file_size());
19
20        if (check) {
21            // Independent expectations, all established by earlier phases.
22            size_t n_blk = 0;
23            for (const auto& t : g.tensors())
24                if (t.name.rfind("blk.", 0) == 0) ++n_blk;
25            std::printf("  blk.* tensors %zu   globals %zu\n", n_blk, g.tensors().size() - n_blk);
26            std::printf("  Q2_0 %llu   BF16 %llu   F32 %llu\n", (unsigned long long)g.count_type("Q2_0"),
27                        (unsigned long long)g.count_type("BF16"), (unsigned long long)g.count_type("F32"));
28
29            // Every tensor's byte size must land inside the gap to the next tensor's offset, since
30            // GGUF aligns every tensor. That is the same bracket test tools/verify_q2_0_geometry.py
31            // uses, and it validates block_geometry() against the file rather than asserting it.
32            std::vector<const strata::TensorInfo*> ordered;
33            for (const auto& t : g.tensors()) ordered.push_back(&t);
34            std::sort(ordered.begin(), ordered.end(), [](auto a, auto b) { return a->offset < b->offset; });
35            size_t bad = 0, unknown = 0;
36            for (size_t i = 0; i < ordered.size(); ++i) {
37                const auto* t = ordered[i];
38                int el = 0, by = 0;
39                if (!strata::block_geometry(t->type, el, by)) {
40                    ++unknown;
41                    continue;
42                }
43                if (t->elements() % (uint64_t)el) {
44                    ++bad;
45                    continue;
46                }
47                const uint64_t nb = t->elements() / (uint64_t)el * (uint64_t)by;
48                // The bracket is on the GAP to the next tensor's offset, not on that offset itself -
49                // offsets are relative to data_start, so the gap is a difference. Comparing against
50                // the raw offset flagged almost every tensor (213/214) while the same test in Python
51                // (tools/verify_q2_0_geometry.py) passed.
52                const uint64_t end = (i + 1 < ordered.size()) ? ordered[i + 1]->offset - t->offset
53                                                              : g.file_size() - g.data_start() - t->offset;
54                if (!(nb <= end && end < nb + 32)) ++bad;
55            }
56            std::printf("  geometry bracket: %zu out of range, %zu types unknown\n", bad, unknown);
57            const std::string err = strata::check_architecture(g);
58            std::printf("  architecture guard: %s\n", err.empty() ? "PASS" : ("FAIL - " + err).c_str());
59            if (bad) return 1;
60        }
61        return 0;
62    } catch (const std::exception& e) {
63        std::printf("ERROR: %s\n", e.what());
64        return 1;
65    }
66}
67#endif // STRATA_GGUF_MAIN_DISABLED