okami.mom/strata

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

Niko1221Merge branch 'f-142-small' into f-142f5aa356f1

main
5.4 KiB129 linesraw
1// src/artifact/gguf_reader_test.cpp - the reader refuses what it cannot arbitrate (synthetic GGUF, no model,
2// no GPU).
3//
4//   1. two tensors with distinct names: the file opens and find() sees both;
5//   2. the same name twice: refused at open, and the error names the tensor and the file - GGUF has no
6//      index to say which of the two a name means, and find() is first-match, so the alternative is a
7//      lookup that silently picks by position.
8//   3. no tensors, and the file ends at its header, before the 32-byte-aligned data start: the file opens,
9//      with data_start() clamped to the file size.  A split model's first shard can hold metadata only and
10//      end unpadded (unsloth's UD-Q5_K_XL shard 1 ends 6 bytes before its aligned data start; llama.cpp's
11//      gguf-py reads it); nothing is ever read from its data section.
12//   4. one tensor, and the file ends at its header: still refused at open ("data section starts past EOF").
13//
14// The fixture is a minimal GGUF v3 written here (header, no metadata, two F32[8] tensors, 32-byte
15// alignment), so the test needs neither gguf-py nor a shard.  The file is closed before it is removed:
16// on Windows an open mapping keeps it.
17#include "strata/artifact/gguf_reader.hpp"
18
19#include <cstdint>
20#include <cstdio>
21#include <cstring>
22#include <filesystem>
23#include <fstream>
24#include <string>
25#include <vector>
26
27namespace {
28int g_fail = 0;
29void check(bool ok, const char* what) {
30    std::printf("  %-66s %s\n", what, ok ? "ok" : "FAIL");
31    if (!ok) ++g_fail;
32}
33
34template <class T> void put(std::vector<uint8_t>& b, T v) {
35    const auto n = b.size();
36    b.resize(n + sizeof(T));
37    std::memcpy(b.data() + n, &v, sizeof(T));
38}
39void put_str(std::vector<uint8_t>& b, const std::string& s) {
40    put<uint64_t>(b, s.size());
41    b.insert(b.end(), s.begin(), s.end());
42}
43
44// A GGUF v3 file of F32[8] tensors named `names`, laid out 32 bytes apart from a 32-byte-aligned data start.
45// `header_only`: the file ends right after the tensor directory - no alignment padding, no data.
46std::filesystem::path write_gguf(const std::vector<std::string>& names, bool header_only = false) {
47    std::vector<uint8_t> b;
48    put<uint32_t>(b, 0x46554747u);   // "GGUF"
49    put<uint32_t>(b, 3);
50    put<uint64_t>(b, names.size());  // n_tensors
51    put<uint64_t>(b, 0);             // n_kv
52    for (size_t i = 0; i < names.size(); ++i) {
53        put_str(b, names[i]);
54        put<uint32_t>(b, 1);         // n_dims
55        put<uint64_t>(b, 8);
56        put<uint32_t>(b, 0);         // F32
57        put<uint64_t>(b, 32 * i);    // offset from data_start
58    }
59    if (!header_only) b.resize((b.size() + 31) / 32 * 32 + 32 * names.size(), 0);
60    const auto path = std::filesystem::temp_directory_path() / "strata_gguf_reader_test.gguf";
61    std::ofstream(path, std::ios::binary).write(reinterpret_cast<const char*>(b.data()), (std::streamsize)b.size());
62    return path;
63}
64}  // namespace
65
66int main() {
67    std::printf("gguf_reader_test\n");
68    {
69        const auto path = write_gguf({"blk.0.attn_q.weight", "blk.0.attn_k.weight"});
70        std::string err;
71        size_t n = 0;
72        bool both = false;
73        try {
74            strata::GgufFile g(path.string());
75            n = g.tensors().size();
76            both = g.find("blk.0.attn_q.weight") && g.find("blk.0.attn_k.weight");
77        } catch (const std::exception& e) {
78            err = e.what();
79        }
80        std::filesystem::remove(path);
81        check(err.empty() && n == 2 && both, "distinct names: the file opens and find() sees both tensors");
82    }
83    {
84        const auto path = write_gguf({"blk.0.attn_q.weight", "blk.0.attn_q.weight"});
85        std::string err;
86        try {
87            strata::GgufFile g(path.string());
88        } catch (const std::exception& e) {
89            err = e.what();
90        }
91        std::filesystem::remove(path);
92        check(!err.empty(), "the same name twice: refused at open");
93        check(err.find("blk.0.attn_q.weight") != std::string::npos, "  the error names the tensor");
94        check(err.find(path.filename().string()) != std::string::npos, "  the error names the file");
95        if (!err.empty()) std::printf("  (%s)\n", err.c_str());
96    }
97    {
98        const auto path = write_gguf({}, true);   // 24 bytes: the aligned data start (32) is past EOF
99        std::string err;
100        uint64_t size = 0, start = 1;
101        size_t n = 1;
102        try {
103            strata::GgufFile g(path.string());
104            size = g.file_size();
105            start = g.data_start();
106            n = g.tensors().size();
107        } catch (const std::exception& e) {
108            err = e.what();
109        }
110        std::filesystem::remove(path);
111        check(err.empty() && n == 0, "no tensors, ends before the aligned data start: the file opens");
112        check(err.empty() && start == size, "  data_start() is the file size (nothing to read past it)");
113        if (!err.empty()) std::printf("  (%s)\n", err.c_str());
114    }
115    {
116        const auto path = write_gguf({"blk.0.attn_q.weight"}, true);
117        std::string err;
118        try {
119            strata::GgufFile g(path.string());
120        } catch (const std::exception& e) {
121            err = e.what();
122        }
123        std::filesystem::remove(path);
124        check(err.find("data section starts past EOF") != std::string::npos,
125              "one tensor, ends before the aligned data start: refused at open");
126    }
127    std::printf(g_fail ? "gguf_reader_test: %d FAILED\n" : "gguf_reader_test: all passed\n", g_fail);
128    return g_fail ? 1 : 0;
129}