okami.mom/strata

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

Niko1221Merge f-split-pics2 (ac658f54a) into f-split-int28a66da38a

main
4.8 KiB110 linesraw
1// tools/vision/gguf_meta_test.cpp - strata-vision opens a GGUF whose weight tensors have types llama.cpp does not know.
2//
3// Writes a small GGUF by hand (a tokenizer, two tensors of the Strata-private types TQK6 = 145 and TQK7 = 146, and split
4// keys that point at shards which are not there), then checks that
5//   - llama.cpp itself cannot load it (the reason the picture reader used to say "cannot open the text model"),
6//   - strata_open_text_model() can, and sees the same vocabulary.
7// Exit code 0 = all checks passed.
8#include "gguf_meta.h"
9
10#include <cstdint>
11#include <cstdio>
12#include <string>
13#include <vector>
14
15namespace {
16
17struct Out {
18    std::vector<uint8_t> b;
19    void raw(const void* p, size_t n) { const auto* c = (const uint8_t*) p; b.insert(b.end(), c, c + n); }
20    template <class T> void num(T v) { raw(&v, sizeof v); }
21    void str(const std::string& s) { num<uint64_t>(s.size()); raw(s.data(), s.size()); }
22    void kv_str(const std::string& k, const std::string& v) { str(k); num<uint32_t>(GGUF_TYPE_STRING); str(v); }
23    void kv_u32(const std::string& k, uint32_t v) { str(k); num<uint32_t>(GGUF_TYPE_UINT32); num<uint32_t>(v); }
24    void kv_strs(const std::string& k, const std::vector<std::string>& v) {
25        str(k); num<uint32_t>(GGUF_TYPE_ARRAY); num<uint32_t>(GGUF_TYPE_STRING); num<uint64_t>(v.size());
26        for (const auto& s : v) str(s);
27    }
28    void kv_i32s(const std::string& k, const std::vector<int32_t>& v) {
29        str(k); num<uint32_t>(GGUF_TYPE_ARRAY); num<uint32_t>(GGUF_TYPE_INT32); num<uint64_t>(v.size());
30        raw(v.data(), v.size() * 4);
31    }
32};
33
34int failures = 0;
35void check(bool ok, const char* what) {
36    std::printf("%s  %s\n", ok ? "ok  " : "FAIL", what);
37    if (!ok) ++failures;
38}
39
40bool write_fixture(const std::string& path, size_t& n_vocab, bool split) {
41    std::vector<std::string> tokens;
42    for (int i = 0; i < 256; ++i) tokens.push_back(std::string("t") + std::to_string(i));
43    tokens.push_back("<|endoftext|>");
44    n_vocab = tokens.size();
45    std::vector<int32_t> types(tokens.size(), 1);
46    types.back() = 3;
47    Out o;
48    o.raw("GGUF", 4);
49    o.num<uint32_t>(3);
50    o.num<uint64_t>(2);                       // two tensors
51    const uint64_t n_kv = split ? 9 : 6;
52    o.num<uint64_t>(n_kv);
53    o.kv_str("general.architecture", "llama");
54    o.kv_str("tokenizer.ggml.model", "gpt2");
55    o.kv_str("tokenizer.ggml.pre", "default");
56    o.kv_strs("tokenizer.ggml.tokens", tokens);
57    o.kv_i32s("tokenizer.ggml.token_type", types);
58    o.kv_strs("tokenizer.ggml.merges", {"t1 t2"});
59    if (split) {
60        o.kv_u32("split.no", 0);
61        o.kv_u32("split.count", 2);
62        o.kv_u32("split.tensors.count", 4);
63    }
64    // two tensors of types llama.cpp's ggml does not have (145 = TQK6, 146 = TQK7), 128 elements each
65    for (int i = 0; i < 2; ++i) {
66        o.str(i == 0 ? "blk.0.ffn_gate_exps.weight" : "blk.0.ffn_up_exps.weight");
67        o.num<uint32_t>(1);
68        o.num<uint64_t>(128);
69        o.num<uint32_t>(145 + i);
70        o.num<uint64_t>(i * 64);
71    }
72    while (o.b.size() % 32) o.b.push_back(0);
73    o.b.resize(o.b.size() + 128, 0);
74    FILE* f = std::fopen(path.c_str(), "wb");
75    if (!f) return false;
76    const bool ok = std::fwrite(o.b.data(), 1, o.b.size(), f) == o.b.size();
77    std::fclose(f);
78    return ok;
79}
80
81}  // namespace
82
83int main(int argc, char** argv) {
84    const std::string dir = argc > 1 ? argv[1] : ".";
85    llama_backend_init();
86    for (int split = 0; split < 2; ++split) {
87        const std::string label = split ? " (a first shard whose other shards are missing)" : "";
88        const std::string path = dir + "/strata-vision-gguf-test-" + std::to_string(split) + ".gguf";
89        size_t n_vocab = 0;
90        check(write_fixture(path, n_vocab, split == 1), ("wrote a GGUF with tensor types 145 and 146" + label).c_str());
91        llama_model_params mp = llama_model_default_params();
92        mp.vocab_only = true;
93        llama_model* plain = llama_model_load_from_file(path.c_str(), mp);
94        std::printf("note  llama.cpp itself %s this file\n", plain ? "accepts" : "refuses");
95        if (plain) llama_model_free(plain);
96        std::string how;
97        llama_model* m = strata_open_text_model(path, &how);
98        check(m != nullptr, ("strata_open_text_model opens it (" + how + ")" + label).c_str());
99        if (m) {
100            check((size_t) llama_vocab_n_tokens(llama_model_get_vocab(m)) == n_vocab, "and sees the whole vocabulary");
101            llama_model_free(m);
102        }
103        check(how.find("header only") == 0, "by reading the header, not the weights");
104        std::remove(path.c_str());
105    }
106    check(strata_open_text_model(dir + "/does-not-exist.gguf") == nullptr, "a file that is not there gives no model");
107    llama_backend_free();
108    std::printf("%s\n", failures ? "GGUF-META FAIL" : "GGUF-META PASS");
109    return failures ? 1 : 0;
110}