okami.mom/strata

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

waveringanaport: keep upstream MMQ_GROUP=16 and staged-walk setup; latent paths use LAT_GROUP=64 and their own staging; geometry test names edge074d12d663

main
16.4 KiB283 linesraw
1// tests/core/gguf_geometry_test.cpp - the model's geometry from its GGUF metadata (strata::read_geometry) and the
2// check against the shape the kernels are compiled for (strata::geometry_unsupported, check_architecture), on
3// synthetic GGUFs: no model, no GPU.
4//
5// The full key set is the one Unsloth's Qwen3.8-Flash-Next UD-IQ4_XS carries (read from its first shard).  Then
6// the cases: a file with only the six keys the guard has always required, a pruned expert count, keys that
7// describe one thing twice and disagree, and a shape the kernels are not compiled for.
8//
9// `gguf_geometry_test --real SHARD1`: the geometry a real model's metadata shard declares, and the verdict.
10#include "gguf_fixture.hpp"
11
12#include <chrono>
13#include <cstdio>
14#include <filesystem>
15#include <functional>
16#include <string>
17#include <vector>
18
19namespace fs = std::filesystem;
20using strata::core::ModelGeometry;
21
22namespace {
23int g_fail = 0;
24void check(bool ok, const std::string& what) {
25    std::printf("  %-90s %s\n", what.c_str(), ok ? "ok" : "FAIL");
26    if (!ok) ++g_fail;
27}
28
29struct TempDir {
30    fs::path path;
31    TempDir() {
32        path = fs::temp_directory_path() /
33               ("strata-gguf-geometry-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
34        fs::create_directories(path);
35    }
36    ~TempDir() {
37        std::error_code ignored;
38        fs::remove_all(path, ignored);
39    }
40};
41
42std::vector<uint64_t> ratios(int64_t layers, int64_t interval, uint64_t block) {
43    std::vector<uint64_t> r((size_t) layers, 0);
44    for (int64_t l = 0; l < layers; ++l)
45        if (l % interval == interval - 1) r[(size_t) l] = block;
46    return r;
47}
48
49/// Qwen3.8-Flash-Next's metadata as its GGUF carries it (the PLE and rope-section keys the reader does not use
50/// are left out), with a vocabulary of `vocab` tokens.
51std::vector<fixture::Kv> full_keys(size_t vocab = 1000) {
52    using namespace fixture;
53    const std::string p = "qwen4exp.";
54    std::vector<std::string> tokens(vocab);
55    for (size_t i = 0; i < vocab; ++i) tokens[i] = "t" + std::to_string(i);
56    return {str("general.architecture", "qwen4exp"),
57            u32(p + "block_count", 48),
58            u32(p + "context_length", 262144),
59            u32(p + "embedding_length", 2560),
60            u32(p + "attention.head_count", 24),
61            u32(p + "attention.head_count_kv", 2),
62            f32(p + "rope.freq_base", 1e7f),
63            f32(p + "attention.layer_norm_rms_epsilon", 1e-6f),
64            u32(p + "expert_count", 512),
65            u32(p + "expert_used_count", 10),
66            u32(p + "attention.key_length", 256),
67            u32(p + "attention.value_length", 256),
68            u32(p + "expert_feed_forward_length", 640),
69            u32(p + "expert_shared_feed_forward_length", 640),
70            u32(p + "ssm.conv_kernel", 4),
71            u32(p + "ssm.state_size", 128),
72            u32(p + "ssm.group_count", 16),
73            u32(p + "ssm.time_step_rank", 48),
74            u32(p + "ssm.inner_size", 6144),
75            u32(p + "full_attention_interval", 4),
76            u32(p + "rope.dimension_count", 64),
77            u32(p + "hyper_connection.count", 4),
78            u32(p + "hyper_connection.low_rank", 320),
79            u32(p + "attention.indexer.head_count", 4),
80            u32(p + "attention.indexer.key_length", 128),
81            u32(p + "attention.indexer.top_k", 2048),
82            u32s(p + "attention.compress_ratios", ratios(48, 4, 4)),
83            str("tokenizer.ggml.model", "gpt2"),
84            strs("tokenizer.ggml.tokens", tokens)};
85}
86
87/// `kv` with `key` replaced by `v` (or removed, when `v` is null).
88std::vector<fixture::Kv> with(std::vector<fixture::Kv> kv, const std::string& key, const fixture::Kv* v) {
89    for (auto it = kv.begin(); it != kv.end(); ++it)
90        if (it->key == key) {
91            if (v) *it = *v;
92            else kv.erase(it);
93            return kv;
94        }
95    if (v) kv.push_back(*v);
96    return kv;
97}
98
99struct Result {
100    std::string read_err, unsupported, guard;
101    ModelGeometry g;
102};
103Result run(const fs::path& dir, const std::vector<fixture::Kv>& kv) {
104    static int n = 0;
105    const fs::path p = dir / ("m" + std::to_string(n++) + ".gguf");
106    fixture::write(p, kv, {});
107    Result r;
108    const strata::GgufFile f(p.string());
109    r.read_err = strata::read_geometry(f, r.g);
110    if (r.read_err.empty()) r.unsupported = strata::geometry_unsupported(r.g);
111    r.guard = strata::check_architecture(f);
112    return r;
113}
114bool has(const std::string& s, const char* part) { return s.find(part) != std::string::npos; }
115
116bool same_shape(const ModelGeometry& a, const ModelGeometry& b) {
117    return a.n_embd == b.n_embd && a.n_layers == b.n_layers && a.qsa_interval == b.qsa_interval &&
118           a.n_vocab == b.n_vocab && a.ssm_state_size == b.ssm_state_size && a.ssm_k_heads == b.ssm_k_heads &&
119           a.ssm_v_heads == b.ssm_v_heads && a.ssm_d_conv == b.ssm_d_conv &&
120           a.ssm_conv_channels == b.ssm_conv_channels && a.ssm_value_dim == b.ssm_value_dim && a.n_head == b.n_head &&
121           a.n_head_kv == b.n_head_kv && a.head_dim == b.head_dim && a.n_rot == b.n_rot &&
122           a.idx_q_heads == b.idx_q_heads && a.idx_key_dim == b.idx_key_dim && a.idx_block == b.idx_block &&
123           a.idx_top_k == b.idx_top_k && a.hc == b.hc && a.hc_lr == b.hc_lr && a.n_expert == b.n_expert &&
124           a.n_expert_used == b.n_expert_used && a.n_ff == b.n_ff;
125}
126
127int real_model(const std::string& shard) {
128    try {
129        const strata::GgufFile f(shard);
130        ModelGeometry g;
131        const std::string err = strata::read_geometry(f, g);
132        if (!err.empty()) {
133            std::printf("  refused: %s\n", err.c_str());
134            return 1;
135        }
136        std::printf("  n_embd %lld, layers %lld (attention every %lld), vocab %lld, rms_eps %g\n", (long long) g.n_embd,
137                    (long long) g.n_layers, (long long) g.qsa_interval, (long long) g.n_vocab, g.rms_eps);
138        std::printf("  GDN: state %lld, k heads %lld, v heads %lld, conv %lld (%lld channels, values %lld)\n",
139                    (long long) g.ssm_state_size, (long long) g.ssm_k_heads, (long long) g.ssm_v_heads,
140                    (long long) g.ssm_d_conv, (long long) g.ssm_conv_channels, (long long) g.ssm_value_dim);
141        std::printf("  QSA: heads %lld/%lld x %lld, rope %lld, indexer %lld x %lld, block %lld, top-k %lld\n",
142                    (long long) g.n_head, (long long) g.n_head_kv, (long long) g.head_dim, (long long) g.n_rot,
143                    (long long) g.idx_q_heads, (long long) g.idx_key_dim, (long long) g.idx_block,
144                    (long long) g.idx_top_k);
145        std::printf("  HC %lld (rank %lld); MoE %lld experts, %lld per token, ff %lld\n", (long long) g.hc,
146                    (long long) g.hc_lr, (long long) g.n_expert, (long long) g.n_expert_used, (long long) g.n_ff);
147        const std::string why = strata::geometry_unsupported(g);
148        std::printf("  the compiled kernels: %s\n", why.empty() ? "can run it" : why.c_str());
149        return why.empty() ? 0 : 1;
150    } catch (const std::exception& e) {
151        std::printf("  refused: %s\n", e.what());
152        return 1;
153    }
154}
155}  // namespace
156
157int main(int argc, char** argv) {
158    if (argc == 3 && std::string(argv[1]) == "--real") return real_model(argv[2]);
159    std::printf("gguf_geometry_test\n");
160    TempDir d;
161    const std::string p = "qwen4exp.";
162    {
163        const Result r = run(d.path, full_keys());
164        ModelGeometry want = strata::core::kCompiledGeometry;
165        want.n_vocab = 1000;
166        check(r.read_err.empty(), "the full key set reads" + (r.read_err.empty() ? "" : ": " + r.read_err));
167        check(same_shape(r.g, want), "  and gives the compiled shape (vocabulary from tokenizer.ggml.tokens)");
168        check(r.g.ssm_conv_channels == 10240, "  conv channels derived: 2 x 128 x 16 + 6144 = 10240");
169        check(r.unsupported.empty() && r.guard.empty(), "  which the kernels run, and the guard passes");
170    }
171    {
172        // the six keys tests/core/native_dense_ple_key_test.cpp writes: everything else is the compiled value
173        const std::vector<fixture::Kv> six = {
174            fixture::str("general.architecture", "qwen4exp"), fixture::u32(p + "block_count", 48),
175            fixture::u32(p + "embedding_length", 2560),       fixture::u32(p + "expert_count", 512),
176            fixture::u32(p + "expert_used_count", 10),        fixture::u32(p + "attention.head_count", 24),
177            fixture::u32(p + "attention.head_count_kv", 2)};
178        const Result r = run(d.path, six);
179        check(r.read_err.empty() && same_shape(r.g, strata::core::kCompiledGeometry) && r.guard.empty(),
180              "only the six required keys: the rest keeps the compiled values");
181        for (const char* key : {"block_count", "embedding_length", "expert_count", "expert_used_count",
182                                "attention.head_count", "attention.head_count_kv"}) {
183            const Result m = run(d.path, with(six, p + key, nullptr));
184            check(has(m.read_err, "missing") && has(m.read_err, key) && m.guard == m.read_err,
185                  std::string("a file without ") + key + " is refused, naming it");
186        }
187    }
188    {
189        // GSQ-RCO Coder: fewer experts, read from the file and run by the same kernels
190        const fixture::Kv e = fixture::u32(p + "expert_count", 384), k = fixture::u32(p + "expert_used_count", 8);
191        const Result r = run(d.path, with(with(full_keys(), e.key, &e), k.key, &k));
192        check(r.read_err.empty() && r.g.n_expert == 384 && r.g.n_expert_used == 8 && r.guard.empty(),
193              "a pruned variant: 384 experts, 8 per token, read and accepted");
194        const fixture::Kv k65 = fixture::u32(p + "expert_used_count", 65);
195        check(has(run(d.path, with(full_keys(), k65.key, &k65)).read_err, "expert_used_count = 65"),
196              "65 experts per token is refused (the router takes 1..64)");
197        const fixture::Kv k0 = fixture::u32(p + "expert_used_count", 0);
198        check(has(run(d.path, with(full_keys(), k0.key, &k0)).read_err, "must be positive"),
199              "0 experts per token is refused");
200    }
201    {
202        // keys that describe one thing twice must agree
203        const fixture::Kv vl = fixture::u32(p + "attention.value_length", 128);
204        check(has(run(d.path, with(full_keys(), vl.key, &vl)).read_err, "attention.value_length = 128"),
205              "a value head width other than the key head width is refused");
206        const fixture::Kv sh = fixture::u32(p + "expert_shared_feed_forward_length", 1280);
207        check(has(run(d.path, with(full_keys(), sh.key, &sh)).read_err, "expert_shared_feed_forward_length = 1280"),
208              "a shared expert wider than the routed ones is refused");
209        const fixture::Kv in = fixture::u32(p + "ssm.inner_size", 4096);
210        check(has(run(d.path, with(full_keys(), in.key, &in)).read_err, "square heads"),
211              "ssm.inner_size other than state_size x time_step_rank is refused");
212        std::vector<uint64_t> shifted = ratios(48, 4, 4);
213        shifted[3] = 0;
214        shifted[2] = 4;
215        const fixture::Kv cr = fixture::u32s(p + "attention.compress_ratios", shifted);
216        check(has(run(d.path, with(full_keys(), cr.key, &cr)).read_err, "compress_ratios[2] = 4"),
217              "compress_ratios that disagree with full_attention_interval are refused at the first layer");
218        const fixture::Kv short_cr = fixture::u32s(p + "attention.compress_ratios", ratios(40, 4, 4));
219        check(has(run(d.path, with(full_keys(), short_cr.key, &short_cr)).read_err, "one entry for each of the 48"),
220              "compress_ratios of another length are refused");
221        std::vector<uint64_t> mixed = ratios(48, 4, 4);
222        mixed[7] = 8;
223        const fixture::Kv mx = fixture::u32s(p + "attention.compress_ratios", mixed);
224        check(has(run(d.path, with(full_keys(), mx.key, &mx)).read_err, "compress_ratios[7] = 8"),
225              "two pooling blocks in one model are refused");
226        // #2396: 48 zeros are a converter's placeholder (orcarouter's Uncensored GGUFs): read as absent, the compiled block stays
227        const fixture::Kv zeros = fixture::u32s(p + "attention.compress_ratios", std::vector<uint64_t>(48, 0));
228        const Result z = run(d.path, with(full_keys(), zeros.key, &zeros));
229        check(z.read_err.empty() && z.guard.empty() && z.g.idx_block == 4, "an all-zero compress_ratios list is treated as absent");
230        const fixture::Kv zshort = fixture::u32s(p + "attention.compress_ratios", std::vector<uint64_t>(40, 0));
231        check(has(run(d.path, with(full_keys(), zshort.key, &zshort)).read_err, "one entry for each of the 48"),
232              "an all-zero list of another length is still refused");
233        std::vector<uint64_t> one_set(48, 0);
234        one_set[3] = 4;                      // a real (but partial) list: layer 7 would be full attention with a 0
235        const fixture::Kv partial = fixture::u32s(p + "attention.compress_ratios", one_set);
236        check(has(run(d.path, with(full_keys(), partial.key, &partial)).read_err, "compress_ratios[7] = 0"),
237              "a list with one non-zero entry is checked in full");
238        const fixture::Kv fl = fixture::f32(p + "block_count", 48.0f);
239        check(has(run(d.path, with(full_keys(), fl.key, &fl)).read_err, "is not an integer"),
240              "a float where a count belongs is refused");
241    }
242    {
243        // a consistent file of another shape reads, and the kernels refuse it naming the key
244        const fixture::Kv bc = fixture::u32(p + "block_count", 40);
245        const fixture::Kv cr = fixture::u32s(p + "attention.compress_ratios", ratios(40, 4, 4));
246        const Result r = run(d.path, with(with(full_keys(), bc.key, &bc), cr.key, &cr));
247        check(r.read_err.empty() && r.g.n_layers == 40 && r.g.n_qsa_layers() == 10, "40 layers read: 10 QSA, 30 GDN");
248        check(r.unsupported == "qwen4exp.block_count = 40, the kernels are compiled for 48" && r.guard == r.unsupported,
249              "  and refused: " + r.unsupported);
250        const fixture::Kv hd = fixture::u32(p + "attention.key_length", 128), vd = fixture::u32(p + "attention.value_length", 128);
251        const Result h = run(d.path, with(with(full_keys(), hd.key, &hd), vd.key, &vd));
252        check(h.read_err.empty() && h.g.head_dim == 128 && has(h.guard, "attention.key_length = 128"),
253              "head width 128 reads and is refused: " + h.guard);
254        const fixture::Kv tk = fixture::u32(p + "attention.indexer.top_k", 1024);
255        check(has(run(d.path, with(full_keys(), tk.key, &tk)).guard, "attention.indexer.top_k = 1024"),
256              "an indexer budget of 1024 is refused");
257        const fixture::Kv eps = fixture::f32(p + "attention.layer_norm_rms_epsilon", 1e-5f);
258        check(has(run(d.path, with(full_keys(), eps.key, &eps)).guard, "layer_norm_rms_epsilon = 1e-05"),
259              "an rms epsilon of 1e-5 is refused");
260        const fixture::Kv vocab = fixture::u32(p + "embedding_length", 2560);
261        check(run(d.path, with(full_keys(151936), vocab.key, &vocab)).guard.empty(),
262              "another vocabulary size is accepted (the head and embedding take their tensors' shapes)");
263        const fixture::Kv arch = fixture::str("general.architecture", "glm5next");
264        const Result a = run(d.path, with(full_keys(), arch.key, &arch));
265        check(a.read_err == "architecture is 'glm5next', this engine requires 'qwen4exp' or 'edge0'" && a.guard == a.read_err,
266              "another architecture is refused by name, by the reader and the guard");
267    }
268    {
269        // Qwen3.8-27B's own keys (qwen35, dense): the reader names the architecture, not the first qwen4exp key it
270        // misses (which is what generate printed before the reader checked the name)
271        const std::vector<fixture::Kv> dense = {
272            fixture::str("general.architecture", "qwen35"), fixture::u32("qwen35.block_count", 65),
273            fixture::u32("qwen35.embedding_length", 5120), fixture::u32("qwen35.feed_forward_length", 17408),
274            fixture::u32("qwen35.attention.head_count", 24), fixture::u32("qwen35.attention.head_count_kv", 4)};
275        const Result r = run(d.path, dense);
276        check(r.read_err == "architecture is 'qwen35', this engine requires 'qwen4exp' or 'edge0'",
277              "a dense qwen35 file is refused by its architecture: " + r.read_err);
278        const Result none = run(d.path, with(dense, "general.architecture", nullptr));
279        check(none.read_err == "missing general.architecture", "a file without general.architecture says so");
280    }
281    std::printf(g_fail ? "gguf_geometry_test: %d FAILED\n" : "gguf_geometry_test: all passed\n", g_fail);
282    return g_fail ? 1 : 0;
283}