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
3.8 KiB72 linesraw
1// tools/vision/own/oracle_prep.cpp - mtmd's side of the picture-preparation check (built in tools/vision's mtmd build only, EXCLUDE_FROM_ALL).
2//
3//   strata-vision-prep-oracle <mmproj.gguf> <out dir> <min_tokens> <max_tokens> <image> [<image> ...]
4// For each image: decodes it with mtmd's own helper, runs mtmd's Qwen preprocessor (the same class mtmd_tokenize uses) with the given token
5// limits (0 = the tower's default), and writes <out dir>/<image file name>.<min>.<max>.prep:  int32 {w, h}, then float32 [h][w][3] (what the
6// tower is given: resized, padded, normalised).  `own_prep_check` (test_image_prep.cpp) compares its own result with these files, bit for bit.
7#include "clip.h"
8#include "clip-impl.h"
9#include "mtmd.h"
10#include "mtmd-helper.h"
11#include "mtmd-image.h"
12
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <string>
17#include <vector>
18
19static std::string base_name(const std::string& p) {
20    const size_t s = p.find_last_of("/\\");
21    return s == std::string::npos ? p : p.substr(s + 1);
22}
23
24int main(int argc, char** argv) {
25    if (argc < 6) { std::fprintf(stderr, "usage: strata-vision-prep-oracle <mmproj> <out dir> <min_tokens> <max_tokens> <image>...\n"); return 2; }
26    const std::string mmproj = argv[1], out_dir = argv[2];
27    const int min_t = std::atoi(argv[3]), max_t = std::atoi(argv[4]);
28
29    clip_context_params cp{};
30    cp.use_gpu = false;
31    cp.flash_attn_type = CLIP_FLASH_ATTN_TYPE_DISABLED;
32    cp.image_min_tokens = min_t > 0 ? min_t : -1;
33    cp.image_max_tokens = max_t > 0 ? max_t : -1;
34    cp.warmup = false;
35    clip_init_result r = clip_init(mmproj.c_str(), cp);
36    if (!r.ctx_v) { std::fprintf(stderr, "cannot load the vision encoder %s\n", mmproj.c_str()); return 1; }
37    mtmd_image_preprocessor_dyn_size pre(r.ctx_v);
38
39    // a bitmap decoder context is not needed for stb images: the helper takes a null mtmd_context only for audio/video, which these are not
40    int rc = 0;
41    for (int a = 5; a < argc; ++a) {
42        const std::string img = argv[a];
43        // mtmd's own decode (stb_image, 3 channels)
44        FILE* f = std::fopen(img.c_str(), "rb");
45        if (!f) { std::fprintf(stderr, "cannot open %s\n", img.c_str()); rc = 1; continue; }
46        std::vector<unsigned char> buf;
47        unsigned char tmp[1 << 16];
48        size_t k;
49        while ((k = std::fread(tmp, 1, sizeof tmp, f)) > 0) buf.insert(buf.end(), tmp, tmp + k);
50        std::fclose(f);
51        mtmd_helper_bitmap_wrapper bw = mtmd_helper_bitmap_init_from_buf(nullptr, buf.data(), buf.size(), false, mtmd_helper_init_opt_default());
52        if (!bw.bitmap) { std::fprintf(stderr, "cannot decode %s\n", img.c_str()); rc = 1; continue; }
53        clip_image_u8 u8;
54        u8.set_size({(int) mtmd_bitmap_get_nx(bw.bitmap), (int) mtmd_bitmap_get_ny(bw.bitmap)}, false);
55        u8.cpy_buf(std::vector<uint8_t>(mtmd_bitmap_get_data(bw.bitmap), mtmd_bitmap_get_data(bw.bitmap) + mtmd_bitmap_get_n_bytes(bw.bitmap)));
56        mtmd_image_preproc_out out = pre.preprocess(u8);
57        if (out.entries.size() != 1) { std::fprintf(stderr, "%s: %zu entries\n", img.c_str(), out.entries.size()); rc = 1; continue; }
58        const clip_image_f32& e = out.entries[0];
59        const std::string path = out_dir + "/" + base_name(img) + "." + std::to_string(min_t) + "." + std::to_string(max_t) + ".prep";
60        FILE* o = std::fopen(path.c_str(), "wb");
61        if (!o) { std::fprintf(stderr, "cannot write %s\n", path.c_str()); rc = 1; continue; }
62        const int32_t hdr[2] = {e.nx(), e.ny()};
63        std::fwrite(hdr, sizeof hdr, 1, o);
64        const auto& b = e.get_ro_buf();
65        std::fwrite(b.data(), sizeof(float), b.size(), o);
66        std::fclose(o);
67        std::printf("%s: %dx%d -> %dx%d\n", img.c_str(), u8.get_size().width, u8.get_size().height, e.nx(), e.ny());
68        mtmd_bitmap_free(bw.bitmap);
69    }
70    clip_free(r.ctx_v);
71    return rc;
72}