okami.mom/strata

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

Niko1221Merge f-own-5b 9b242bb5b into f-own-int (round 15: the own text encoder and tokenizer as test programs, the DiT reference tool and position tables; committed work only)11c8c701a

main
4.1 KiB93 linesraw
1// src/image/te/qwen3vl_te_encode_main.cpp - test program strata-te-encode (not shipped): Strata's text encoder on token
2// ids that stable-diffusion.cpp built (tools/image/gate/stage_hooks.py dumps tokens.bin + tokens.start).
3//
4//   strata-te-encode <te.gguf> <src dir> <dst dir> [--repeat N] [--mode auto|resident|streamed]
5//
6// For every <src dir>/dump_*/ with tokens.bin: the condition sd.cpp's LLMEmbedder would return for those ids (our
7// encoder's rows from tokens.start on; the gate prompts carry no token weights) -> <dst dir>/dump_*/cond.bin (float32,
8// [n][hidden]) and cond.shape ("hidden n", as the stage hooks write it).  With --repeat N each prompt is encoded N times
9// and every repeat must give the same bytes.  Prints the open time, per prompt the encode time and the peak device
10// memory, and TE-ENCODE PASS / FAIL.
11#include "strata/image/qwen3vl_te.hpp"
12
13#include <algorithm>
14#include <cstdio>
15#include <cstring>
16#include <filesystem>
17#include <fstream>
18#include <string>
19#include <vector>
20
21namespace fs = std::filesystem;
22using namespace strata::image;
23
24int main(int argc, char** argv) {
25    if (argc < 4) {
26        std::fprintf(stderr, "usage: strata-te-encode <te.gguf> <src dir> <dst dir> [--repeat N]\n");
27        return 2;
28    }
29    int repeat = 1, mode = 0;
30    for (int i = 4; i + 1 < argc; ++i) {
31        if (std::strcmp(argv[i], "--repeat") == 0) repeat = std::max(1, std::atoi(argv[i + 1]));
32        if (std::strcmp(argv[i], "--mode") == 0)
33            mode = std::strcmp(argv[i + 1], "resident") == 0 ? 1 : std::strcmp(argv[i + 1], "streamed") == 0 ? 2 : 0;
34    }
35    std::string err;
36    Qwen3VlTe* te = qwen3vl_te_open(argv[1], -1, &err, mode);
37    if (!te) {
38        std::fprintf(stderr, "%s\n", err.c_str());
39        return 2;
40    }
41    std::printf("open: %s\n", qwen3vl_te_open_report(te).c_str());
42    const int C = qwen3vl_te_hidden(te);
43    std::vector<fs::path> dirs;
44    for (const auto& e : fs::directory_iterator(argv[2]))
45        if (e.is_directory() && fs::exists(e.path() / "tokens.bin")) dirs.push_back(e.path());
46    std::sort(dirs.begin(), dirs.end());
47    int bad = 0;
48    for (const auto& d : dirs) {
49        std::vector<int32_t> ids;
50        std::vector<char> raw;
51        {
52            std::ifstream g(d / "tokens.bin", std::ios::binary);
53            raw.assign(std::istreambuf_iterator<char>(g), {});
54        }
55        ids.resize(raw.size() / 4);
56        std::memcpy(ids.data(), raw.data(), ids.size() * 4);
57        int start = 0;
58        std::ifstream(d / "tokens.start") >> start;
59        const int n = (int) ids.size();
60        std::vector<float> out((size_t) n * C), first;
61        bool same = true;
62        double ms0 = 0;
63        size_t peak = 0;
64        for (int r = 0; r < repeat; ++r) {
65            Qwen3VlTeStats st;
66            err = qwen3vl_te_encode(te, ids.data(), n, out.data(), &st);
67            if (!err.empty()) {
68                std::fprintf(stderr, "%s: %s\n", d.filename().string().c_str(), err.c_str());
69                qwen3vl_te_close(te);
70                return 2;
71            }
72            if (r == 0) {
73                first = out;
74                ms0 = st.encode_ms;
75                peak = st.peak_bytes;
76            } else {
77                same = same && std::memcmp(first.data(), out.data(), out.size() * sizeof(float)) == 0;
78                std::printf("%s repeat %d: %.1f ms\n", d.filename().string().c_str(), r, st.encode_ms);
79            }
80        }
81        if (!same) ++bad;
82        const fs::path od = fs::path(argv[3]) / d.filename();
83        fs::create_directories(od);
84        std::ofstream(od / "cond.bin", std::ios::binary)
85            .write(reinterpret_cast<const char*>(first.data() + (size_t) start * C), (std::streamsize) ((size_t) (n - start) * C * 4));
86        std::ofstream(od / "cond.shape") << C << " " << (n - start) << " \n";
87        std::printf("%s: %d tokens (start %d), encode %.1f ms, peak %.0f MiB, repeats identical %s\n",
88                    d.filename().string().c_str(), n, start, ms0, peak / 1048576.0, same ? "yes" : "NO");
89    }
90    qwen3vl_te_close(te);
91    std::printf("TE-ENCODE %s (%zu prompts)\n", bad || dirs.empty() ? "FAIL" : "PASS", dirs.size());
92    return bad || dirs.empty() ? 1 : 0;
93}