okami.mom/strata

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

Niko1221Merge f-own-3c 971bff01a into f-p4-int870ea9763

main
16.2 KiB251 linesraw
1// tools/vision/own/test_gguf_geometry.cpp - the GGUF reader and the geometry, on files made here and (given paths) on real mmproj files.
2//
3//   strata-vision-own-test [mmproj.gguf ...]
4// Exit 0 when every check passes.  A real mmproj given on the command line is parsed and its geometry printed; the two known Qwen
5// towers (27 layers, width 1152, out 2560 or 2048) are also checked against the numbers read from their headers.
6#include "own_reader.h"
7#include "vit_geometry.h"
8#include "vit_gpu.h"
9
10#include <cmath>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <string>
15#include <vector>
16
17using namespace strata_vit;
18
19static int g_fail = 0;
20#define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++g_fail; } } while (0)
21
22// ---- a tiny GGUF writer ----
23struct W {
24    std::vector<unsigned char> b;
25    template <class T> void put(T v) { const unsigned char* p = (const unsigned char*) &v; b.insert(b.end(), p, p + sizeof v); }
26    void str(const std::string& s) { put<uint64_t>(s.size()); b.insert(b.end(), s.begin(), s.end()); }
27    void raw(const void* p, size_t n) { b.insert(b.end(), (const unsigned char*) p, (const unsigned char*) p + n); }
28};
29
30static uint16_t f32_to_f16(float f) {   // good enough for the values used here (normal range, round toward zero)
31    uint32_t u; std::memcpy(&u, &f, 4);
32    const uint32_t sign = (u >> 16) & 0x8000u;
33    int exp = (int) ((u >> 23) & 0xFF) - 127 + 15;
34    const uint32_t man = (u >> 13) & 0x3FFu;
35    if (f == 0.0f) return (uint16_t) sign;
36    return (uint16_t) (sign | ((uint32_t) exp << 10) | man);
37}
38
39int main(int argc, char** argv) {
40    // --- 0. the GPU classes where `--tower auto` keeps mtmd (vit_gpu.h) ---
41    for (int mt : {0, 300, 1024}) {      // at or below the default limit: Pascal 6.1 / 6.2 keep mtmd (not measured); cc 6.0 takes ours with F16 and BF16 files
42        CHECK(vit_gpu_slower_nvidia(6, 1, false, mt) && vit_gpu_slower_nvidia(6, 1, true, mt) && vit_gpu_slower_nvidia(6, 2, false, mt) && vit_gpu_slower_nvidia(6, 2, true, mt));
43        CHECK(!vit_gpu_slower_nvidia(6, 0, true, mt) && !vit_gpu_slower_nvidia(6, 0, false, mt));
44        CHECK(!vit_gpu_slower_nvidia(5, 2, false, mt) && !vit_gpu_slower_nvidia(7, 0, false, mt) && !vit_gpu_slower_nvidia(8, 6, true, mt) && !vit_gpu_slower_nvidia(12, 0, false, mt));
45    }
46    for (int mt : {1025, 4096, 16384}) {   // above it: ours on every CUDA class
47        CHECK(!vit_gpu_slower_nvidia(6, 0, false, mt) && !vit_gpu_slower_nvidia(6, 1, false, mt) && !vit_gpu_slower_nvidia(6, 1, true, mt) && !vit_gpu_slower_nvidia(6, 2, true, mt));
48        CHECK(!vit_gpu_slower_nvidia(6, 0, true, mt) && !vit_gpu_slower_nvidia(8, 6, true, mt) && !vit_gpu_slower_nvidia(12, 0, false, mt));
49    }
50    CHECK(vit_gpu_is_gfx1151("gfx1151") && vit_gpu_is_gfx1151("gfx1151:xnack-"));
51    CHECK(!vit_gpu_is_gfx1151("gfx1150") && !vit_gpu_is_gfx1151("gfx1201") && !vit_gpu_is_gfx1151("gfx11510") && !vit_gpu_is_gfx1151("gfx115") && !vit_gpu_is_gfx1151(""));
52    // Windows HIP keeps mtmd under auto except on gfx1151 (Strix, measured with that package; owner 11-11); Linux HIP never
53    CHECK(!vit_gpu_hip_windows_not_measured(true, "gfx1151") && !vit_gpu_hip_windows_not_measured(true, "gfx1151:xnack-"));
54    CHECK(vit_gpu_hip_windows_not_measured(true, "gfx1201") && vit_gpu_hip_windows_not_measured(true, "gfx1100") && vit_gpu_hip_windows_not_measured(true, "gfx1150"));
55    CHECK(!vit_gpu_hip_windows_not_measured(false, "gfx1151") && !vit_gpu_hip_windows_not_measured(false, "gfx1201"));
56    {   // the context retry (vit_gpu.h): only "out of memory", only an integrated GPU, only a Linux CUDA build, at most kVitCtxRetries times
57        CHECK(kVitCtxRetries == 12 && kVitCtxWaitMs == 1000);
58        for (int done = 0; done < kVitCtxRetries; ++done) CHECK(vit_gpu_retry_context(true, true, true, done));
59        CHECK(!vit_gpu_retry_context(true, true, true, kVitCtxRetries) && !vit_gpu_retry_context(true, true, true, kVitCtxRetries + 5) && !vit_gpu_retry_context(true, true, true, -1));
60        CHECK(!vit_gpu_retry_context(false, true, true, 0));    // HIP, Windows, Metal: no retry
61        CHECK(!vit_gpu_retry_context(true, false, true, 0));    // a discrete card: the failure is real
62        CHECK(!vit_gpu_retry_context(true, true, false, 0));    // any other error (no device, driver too old): no retry
63        int tries = 0;                                          // a start that fails 3 times and then works waits 3 times; one that never works gives up after 8
64        for (int fails = 3; vit_gpu_retry_context(true, true, true, tries) && tries < fails; ) ++tries;
65        CHECK(tries == 3);
66        tries = 0;
67        while (vit_gpu_retry_context(true, true, true, tries)) ++tries;
68        CHECK(tries == kVitCtxRetries);
69    }
70    {   // the default tower (no --tower): auto, or STRATA_VISION_TOWER's value when it is a known one
71        std::string note;
72        CHECK(vision_tower_default(nullptr, note) == "auto" && note.empty());
73        CHECK(vision_tower_default("", note) == "auto" && note.empty());
74        CHECK(vision_tower_default("mtmd", note) == "mtmd" && vision_tower_default("own", note) == "own" && vision_tower_default("auto", note) == "auto" && note.empty());
75        CHECK(vision_tower_default("llama", note) == "auto" && note.find("STRATA_VISION_TOWER=llama") != std::string::npos);
76    }
77    {   // `--tower auto` on the CPU path by CPU class (owner 11-11: ours below AVX2 too), for both reader files' geometry; the explicit settings are never overruled
78        VitGeometry g38;      // the Qwen3.8 tower (projection 2560) and the Qwen3.6 tower (2048): the two public mmproj files' numbers
79        g38.projector = "qwen3vl_merger"; g38.layers = 27; g38.width = 1152; g38.heads = 16; g38.head_dim = 72; g38.ffn = 4304; g38.patch = 16; g38.merge = 2;
80        g38.image_size = 768; g38.pos_grid = 48; g38.out_dim = 2560; g38.patch_taps = 2; g38.merger_in = 4608; g38.merger_hidden = 4608;
81        VitGeometry g36 = g38;
82        g36.out_dim = 2048;
83        quant::Caps none_;                                               // an x86-64 CPU older than SSE4.1: the scalar tier
84        quant::Caps nehalem = none_;  nehalem.sse41 = true;              // no AVX: Nehalem, Westmere
85        quant::Caps sandy = nehalem;  sandy.avx = true;                  // AVX, no F16C: Sandy Bridge
86        quant::Caps ivy = sandy;      ivy.f16c = true;                   // AVX with F16C: Ivy Bridge
87        quant::Caps haswell = ivy;    haswell.avx2 = true; haswell.fma = true;
88        quant::Caps skylake_x = haswell; skylake_x.avx512 = true;
89        struct Cls { const char* name; const quant::Caps* c; quant::Isa tier; };
90        const Cls classes[] = {{"no SSE4.1", &none_, quant::Isa::Scalar}, {"no AVX", &nehalem, quant::Isa::Sse41}, {"AVX without F16C", &sandy, quant::Isa::Avx},
91                               {"AVX with F16C", &ivy, quant::Isa::Avx}, {"AVX2", &haswell, quant::Isa::Avx2}, {"AVX-512", &skylake_x, quant::Isa::Avx512}};
92        VitGeometry g_rms = g38;
93        g_rms.norm = NormKind::RMSNorm;      // a tower this code cannot run: `auto` keeps mtmd on every tier
94        for (const Cls& k : classes) {
95            std::string why;
96#if defined(__x86_64__) || defined(_M_X64)
97            const quant::Isa tier = quant::best_isa_for(*k.c, nullptr);
98            if (tier != k.tier) { std::printf("FAIL tier of the %s class: %s\n", k.name, quant::isa_name(tier)); ++g_fail; }
99#else
100            const quant::Isa tier = k.tier;
101#endif
102            for (const VitGeometry* g : {&g38, &g36}) {
103                CHECK(own_cpu_reader_supported(*g, tier, why));          // ours on every tier, both reader files
104                CHECK(vision_tower_resolve("auto", own_cpu_reader_supported(*g, tier, why)) == "own");
105            }
106            CHECK(!own_cpu_reader_supported(g_rms, tier, why) && !why.empty());
107            CHECK(vision_tower_resolve("auto", own_cpu_reader_supported(g_rms, tier, why)) == "mtmd");
108        }
109#if defined(__x86_64__) || defined(_M_X64)
110        // the tier mask (STRATA_FORCE_ISA) caps the tier a CPU runs, never raises it
111        CHECK(quant::best_isa_for(skylake_x, "sse41") == quant::Isa::Sse41 && quant::best_isa_for(skylake_x, "avx") == quant::Isa::Avx && quant::best_isa_for(skylake_x, "avx2") == quant::Isa::Avx2);
112        CHECK(quant::best_isa_for(skylake_x, "scalar") == quant::Isa::Scalar && quant::best_isa_for(sandy, "avx2") == quant::Isa::Avx && quant::best_isa_for(nehalem, "avx512") == quant::Isa::Sse41);
113        CHECK(quant::isa_ok_for(ivy, quant::Isa::Avx) && !quant::isa_ok_for(ivy, quant::Isa::Avx2) && !quant::isa_ok_for(nehalem, quant::Isa::Avx) && !quant::isa_ok_for(none_, quant::Isa::Sse41));
114#endif
115        // the explicit settings, both ways, whatever `own_ok` says
116        CHECK(vision_tower_resolve("mtmd", true) == "mtmd" && vision_tower_resolve("mtmd", false) == "mtmd");
117        CHECK(vision_tower_resolve("own", true) == "own" && vision_tower_resolve("own", false) == "own");
118        CHECK(vision_tower_resolve("auto", true) == "own" && vision_tower_resolve("auto", false) == "mtmd");
119        std::string note;
120        CHECK(vision_tower_resolve(vision_tower_default("mtmd", note), true) == "mtmd" && vision_tower_resolve(vision_tower_default("own", note), false) == "own");
121        CHECK(vision_tower_resolve(vision_tower_default(nullptr, note), true) == "own");     // no setting at all: auto
122    }
123    {   // a start failure under auto comes back as kOwnStartFailed with its reason (nothing on stdout), so strata-vision can start mtmd's reader
124        OwnReaderOptions o;
125        o.mmproj = "/nonexistent/strata-own-test-mmproj.gguf";
126        std::string why;
127        o.start_error = &why;
128        CHECK(own_reader_run(o) == kOwnStartFailed && why.find("cannot load the vision encoder") != std::string::npos);
129    }
130    {   // the test hook names one point at a time
131#ifdef _WIN32
132        _putenv_s("STRATA_VIT_TEST_FAIL", "load");
133#else
134        setenv("STRATA_VIT_TEST_FAIL", "load", 1);
135#endif
136        CHECK(own_reader_inject_failure("load") && !own_reader_inject_failure("gpu") && !own_reader_inject_failure("warmup"));
137#ifdef _WIN32
138        _putenv_s("STRATA_VIT_TEST_FAIL", "");
139#else
140        unsetenv("STRATA_VIT_TEST_FAIL");
141#endif
142        CHECK(!own_reader_inject_failure("load"));
143    }
144    const std::string tmp = std::string(std::getenv("TMPDIR") ? std::getenv("TMPDIR") : ".") + "/strata_own_test_" + std::to_string((long) std::rand()) + ".gguf";
145
146    // --- 1. a file with every value kind and the four tensor types ---
147    {
148        W w;
149        w.raw("GGUF", 4); w.put<uint32_t>(3); w.put<uint64_t>(4);   // tensors
150        w.put<uint64_t>(9);                                          // keys
151        auto kv_u32 = [&](const char* k, uint32_t v) { w.str(k); w.put<uint32_t>(4); w.put<uint32_t>(v); };
152        kv_u32("a.u32", 7);
153        w.str("a.neg"); w.put<uint32_t>(5); w.put<int32_t>(-3);
154        w.str("a.f32"); w.put<uint32_t>(6); w.put<float>(2.5f);
155        w.str("a.bool"); w.put<uint32_t>(7); w.put<uint8_t>(1);
156        w.str("a.str"); w.put<uint32_t>(8); w.str("hello");
157        w.str("a.u64"); w.put<uint32_t>(10); w.put<uint64_t>(5000000000ull);
158        w.str("a.arr_f"); w.put<uint32_t>(9); w.put<uint32_t>(6); w.put<uint64_t>(3); w.put<float>(0.5f); w.put<float>(1.5f); w.put<float>(-2.0f);
159        w.str("a.arr_b"); w.put<uint32_t>(9); w.put<uint32_t>(7); w.put<uint64_t>(4); w.put<uint8_t>(0); w.put<uint8_t>(1); w.put<uint8_t>(1); w.put<uint8_t>(0);
160        w.str("a.arr_s"); w.put<uint32_t>(9); w.put<uint32_t>(8); w.put<uint64_t>(2); w.str("x"); w.str("yz");
161        // tensor directory: f32 [4], f16 [4], bf16 [4], q8_0 [32, 2]
162        uint64_t off = 0;
163        auto dir = [&](const char* n, std::vector<int64_t> ne, uint32_t type, uint64_t nbytes) {
164            w.str(n); w.put<uint32_t>((uint32_t) ne.size()); for (int64_t d : ne) w.put<uint64_t>((uint64_t) d); w.put<uint32_t>(type); w.put<uint64_t>(off);
165            off += (nbytes + 31) / 32 * 32;
166        };
167        dir("t.f32", {4}, GT_F32, 16);
168        dir("t.f16", {4}, GT_F16, 8);
169        dir("t.bf16", {4}, GT_BF16, 8);
170        dir("t.q8", {32, 2}, GT_Q8_0, 68);
171        while (w.b.size() % 32) w.b.push_back(0);
172        auto pad = [&]() { while (w.b.size() % 32) w.b.push_back(0); };
173        const float f[4] = {1.0f, -2.0f, 0.25f, 3.5f};
174        w.raw(f, 16); pad();
175        for (float x : f) w.put<uint16_t>(f32_to_f16(x));
176        pad();
177        for (float x : f) { uint32_t u; std::memcpy(&u, &x, 4); w.put<uint16_t>((uint16_t) (u >> 16)); }
178        pad();
179        for (int blk = 0; blk < 2; ++blk) {
180            w.put<uint16_t>(f32_to_f16(0.5f * (blk + 1)));
181            for (int j = 0; j < 32; ++j) w.put<int8_t>((int8_t) (j - 16));
182        }
183        pad();
184        FILE* fp = std::fopen(tmp.c_str(), "wb");
185        CHECK(fp != nullptr);
186        if (fp) { std::fwrite(w.b.data(), 1, w.b.size(), fp); std::fclose(fp); }
187
188        Gguf g;
189        std::string err;
190        CHECK(g.open(tmp, err));
191        uint32_t u = 0; float fl = 0; bool bo = false; std::string s;
192        CHECK(g.get_u32("a.u32", u) && u == 7);
193        CHECK(!g.get_u32("a.neg", u));
194        CHECK(!g.get_u32("a.u64", u));
195        CHECK(!g.get_u32("a.f32", u));
196        CHECK(g.get_f32("a.f32", fl) && fl == 2.5f);
197        CHECK(g.get_f32("a.neg", fl) && fl == -3.0f);
198        CHECK(g.get_bool("a.bool", bo) && bo);
199        CHECK(g.get_str("a.str", s) && s == "hello");
200        CHECK(!g.get_str("a.u32", s));
201        std::vector<float> af; std::vector<int64_t> ai;
202        CHECK(g.get_f32_array("a.arr_f", af) && af.size() == 3 && af[1] == 1.5f && af[2] == -2.0f);
203        CHECK(g.get_int_array("a.arr_b", ai) && ai.size() == 4 && ai[1] == 1 && ai[3] == 0);
204        CHECK(!g.get_int_array("a.arr_f", ai));
205        CHECK(!g.get_int_array("a.arr_s", ai));
206        CHECK(g.tensors().size() == 4);
207        const GgufTensorInfo* t = g.tensor("t.f16");
208        CHECK(t && t->nbytes == 8 && t->ne.size() == 1 && t->ne[0] == 4);
209        float v[64];
210        CHECK(t && g.read_f32(*t, v, err) && v[0] == 1.0f && v[1] == -2.0f && v[2] == 0.25f && v[3] == 3.5f);
211        t = g.tensor("t.f32");
212        CHECK(t && g.read_f32(*t, v, err) && v[3] == 3.5f);
213        t = g.tensor("t.bf16");
214        CHECK(t && g.read_f32(*t, v, err) && v[1] == -2.0f && v[2] == 0.25f);
215        t = g.tensor("t.q8");
216        CHECK(t && t->nbytes == 68 && g.read_f32(*t, v, err));
217        CHECK(std::fabs(v[0] - 0.5f * -16) < 1e-6 && std::fabs(v[31] - 0.5f * 15) < 1e-6 && std::fabs(v[32] - 1.0f * -16) < 1e-6);
218        CHECK(g.tensor("nope") == nullptr);
219        CHECK(std::fabs(gguf_f16_to_f32(0x0001) - 5.9604645e-08f) < 1e-12f);   // the smallest subnormal half
220        CHECK(gguf_f16_to_f32(0x7C00) > 1e30f);                                // inf
221        // not a GGUF / cut short
222        FILE* bad = std::fopen(tmp.c_str(), "wb");
223        if (bad) { std::fwrite("GGUF", 1, 4, bad); std::fclose(bad); }
224        Gguf g2;
225        CHECK(!g2.open(tmp, err));
226        CHECK(!g2.open(tmp + ".missing", err));
227        std::remove(tmp.c_str());
228    }
229
230    // --- 2. real mmproj files given on the command line ---
231    for (int a = 1; a < argc; ++a) {
232        Gguf g;
233        std::string err, why;
234        VitGeometry geo;
235        if (!g.open(argv[a], err)) { std::printf("FAIL %s: %s\n", argv[a], err.c_str()); ++g_fail; continue; }
236        if (!vit_geometry_from_gguf(g, geo, err)) { std::printf("FAIL %s: geometry: %s\n", argv[a], err.c_str()); ++g_fail; continue; }
237        const bool sup = vit_geometry_supported(geo, why);
238        std::printf("%s: %s layers %d width %d heads %d (head %d) ffn %d patch %d merge %d pos grid %d (image %d) out %d eps %g taps %d "
239                    "deepstack %zu supported %s%s%s\n", argv[a], geo.projector.c_str(), geo.layers, geo.width, geo.heads, geo.head_dim, geo.ffn,
240                    geo.patch, geo.merge, geo.pos_grid, geo.image_size, geo.out_dim, (double) geo.eps, geo.patch_taps, geo.deepstack.size(),
241                    sup ? "yes" : "no", sup ? "" : ": ", sup ? "" : why.c_str());
242        CHECK(sup);
243        CHECK(geo.layers == 27 && geo.width == 1152 && geo.heads == 16 && geo.head_dim == 72 && geo.ffn == 4304);
244        CHECK(geo.patch == 16 && geo.merge == 2 && geo.pos_grid == 48 && geo.patch_taps == 2);
245        CHECK(geo.out_dim == 2560 || geo.out_dim == 2048);
246        CHECK(geo.merger_in == 4608 && geo.merger_hidden == 4608);
247    }
248
249    std::printf(g_fail ? "strata-vision-own-test: %d FAILED\n" : "strata-vision-own-test: ok\n", g_fail);
250    return g_fail ? 1 : 0;
251}