okami.mom/strata

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

Niko1221Merge f-own-3v (aec86299d) into f-own-int: our own Qwen vision tower (mtmd stays the default)0479b72e9

main
4.9 KiB95 linesraw
1// tools/vision/own/test_image_prep.cpp - own_prep_check: our picture preparation against mtmd's, bit for bit.
2//
3//   strata-vision-own-prep-check <mmproj.gguf> <oracle dir> <min_tokens> <max_tokens> <image> [<image> ...]
4// The oracle files come from strata-vision-prep-oracle (oracle_prep.cpp) run with the same limits.  For every image: our decode, smart_resize,
5// bicubic resize with padding and normalisation must reproduce mtmd's float buffer exactly (same size, every value equal).
6// Without images it only runs the self-checks of smart_resize.
7#include "image_prep.h"
8
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <string>
14#include <vector>
15
16using namespace strata_vit;
17
18static int g_fail = 0;
19#define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++g_fail; } } while (0)
20
21static std::string base_name(const std::string& p) {
22    const size_t s = p.find_last_of("/\\");
23    return s == std::string::npos ? p : p.substr(s + 1);
24}
25
26int main(int argc, char** argv) {
27    // --- smart_resize self-checks (the 32-aligned sides and the pixel bounds) ---
28    {
29        const int align = 32, mn = 8 * 1024, mx = 4096 * 1024;
30        for (int w : {1, 2, 31, 33, 64, 100, 640, 800, 1000, 1024, 3000, 4000, 8000, 20000})
31            for (int h : {1, 3, 32, 45, 90, 400, 450, 517, 1024, 3000, 8000}) {
32                const Size2 s = smart_resize(w, h, align, mn, mx);
33                CHECK(s.w >= align && s.h >= align && s.w % align == 0 && s.h % align == 0);
34                CHECK((long long) s.w * s.h <= (long long) mx + 2LL * align * (s.w + s.h));   // the floor of each side can only shrink it
35                if ((long long) w * h * 4 >= mn && (long long) w * h <= mx / 2) CHECK((long long) s.w * s.h >= mn / 2);
36            }
37        CHECK(smart_resize(640, 400, 32, 8192, 4194304) == (Size2{640, 416}));
38        CHECK(smart_resize(1024, 1024, 32, 8192, 4194304) == (Size2{1024, 1024}));
39        CHECK(smart_resize(64, 48, 32, 8192, 4194304).w * smart_resize(64, 48, 32, 8192, 4194304).h >= 8192);
40        CHECK(smart_resize(0, 5, 32, 8192, 4194304) == (Size2{0, 0}));
41    }
42    if (argc < 6) {
43        std::printf(g_fail ? "own_prep_check: %d FAILED\n" : "own_prep_check: self-checks ok\n", g_fail);
44        return g_fail ? 1 : 0;
45    }
46
47    const std::string mmproj = argv[1], dir = argv[2];
48    const int min_t = std::atoi(argv[3]), max_t = std::atoi(argv[4]);
49    Gguf gg;
50    std::string err;
51    VitGeometry geo;
52    if (!gg.open(mmproj, err) || !vit_geometry_from_gguf(gg, geo, err)) { std::printf("FAIL %s: %s\n", mmproj.c_str(), err.c_str()); return 1; }
53
54    int same = 0, total = 0;
55    for (int a = 5; a < argc; ++a) {
56        const std::string img = argv[a];
57        ++total;
58        ImageU8 src, resized;
59        PreparedImage prep;
60        if (!image_load_file(img, src, err) || !prepare_image(src, geo, min_t, max_t, prep, err, &resized)) {
61            std::printf("FAIL %s: %s\n", img.c_str(), err.c_str());
62            ++g_fail;
63            continue;
64        }
65        const std::string path = dir + "/" + base_name(img) + "." + std::to_string(min_t) + "." + std::to_string(max_t) + ".prep";
66        FILE* f = std::fopen(path.c_str(), "rb");
67        if (!f) { std::printf("FAIL %s: no oracle file %s\n", img.c_str(), path.c_str()); ++g_fail; continue; }
68        int32_t hdr[2] = {0, 0};
69        if (std::fread(hdr, sizeof hdr, 1, f) != 1) { std::printf("FAIL %s: oracle header\n", img.c_str()); ++g_fail; std::fclose(f); continue; }
70        std::vector<float> ref((size_t) hdr[0] * hdr[1] * 3);
71        const bool got = std::fread(ref.data(), sizeof(float), ref.size(), f) == ref.size();
72        std::fclose(f);
73        if (!got) { std::printf("FAIL %s: oracle data short\n", img.c_str()); ++g_fail; continue; }
74        if (hdr[0] != prep.w || hdr[1] != prep.h) {
75            std::printf("FAIL %s: %dx%d -> ours %dx%d, mtmd %dx%d\n", img.c_str(), src.w, src.h, prep.w, prep.h, hdr[0], hdr[1]);
76            ++g_fail;
77            continue;
78        }
79        const size_t n = (size_t) prep.w * prep.h;
80        size_t differ = 0;
81        double maxd = 0;
82        for (size_t i = 0; i < n; ++i)
83            for (int c = 0; c < 3; ++c) {
84                const float x = prep.chw[c * n + i], y = ref[i * 3 + c];
85                if (std::memcmp(&x, &y, 4) != 0) { ++differ; maxd = std::fmax(maxd, std::fabs((double) x - y)); }
86            }
87        std::printf("%-28s %5dx%-5d -> %4dx%-4d (%d x %d tokens)  %s", base_name(img).c_str(), src.w, src.h, prep.w, prep.h, prep.w / geo.align(),
88                    prep.h / geo.align(), differ ? "DIFFERS" : "identical");
89        if (differ) std::printf(": %zu of %zu values, max |diff| %g", differ, n * 3, maxd);
90        std::printf("\n");
91        if (differ) ++g_fail; else ++same;
92    }
93    std::printf("own_prep_check: %d of %d pictures identical to mtmd (min_tokens %d, max_tokens %d)%s\n", same, total, min_t, max_t, g_fail ? ", FAILED" : "");
94    return g_fail ? 1 : 0;
95}