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.5 KiB97 linesraw
1// src/image/te/qwen2_tok_main.cpp - test program strata-te-tokens (not shipped) for include/strata/image/qwen2_tok.hpp.
2//
3//   strata-te-tokens run   <merges> <cases.txt> <out.txt>   cases.txt: one case per line, "<name> <prompt as hex>";
4//                                                         out.txt: "<name> ok <n_system> <ids,...> <weights as f32 bits>"
5//                                                         or "<name> error <message>"
6//   strata-te-tokens subset <te.gguf> <cases.txt> <out merges> <merges.txt>   run the cases with the GGUF's merges
7//                                                         (merges.txt: the same list, one per line) and keep only
8//                                                         the merges they use (rank<TAB>merge), for a test that runs
9//                                                         without the 5 GB file
10// <merges>: a .gguf (tokenizer.ggml.merges) or a subset file whose first line is "n_merges <N>".
11#include "strata/image/qwen2_tok.hpp"
12
13#include <cstdio>
14#include <cstring>
15#include <fstream>
16#include <set>
17#include <sstream>
18#include <string>
19#include <vector>
20
21using namespace strata::image;
22
23static std::string unhex(const std::string& h) {
24    std::string s;
25    for (size_t i = 0; i + 1 < h.size(); i += 2) s += (char) std::stoi(h.substr(i, 2), nullptr, 16);
26    return s;
27}
28
29static std::unique_ptr<Qwen2Tok> load(const std::string& path, std::string* err) {
30    if (path.size() > 5 && path.substr(path.size() - 5) == ".gguf") return Qwen2Tok::from_gguf(path, err);
31    std::ifstream f(path, std::ios::binary);
32    std::string line;
33    int n = 0;
34    std::vector<std::pair<int, std::string>> ranked;
35    while (std::getline(f, line)) {
36        if (line.rfind("n_merges ", 0) == 0) { n = std::stoi(line.substr(9)); continue; }
37        const size_t tab = line.find('\t');
38        if (tab == std::string::npos) continue;
39        ranked.emplace_back(std::stoi(line.substr(0, tab)), line.substr(tab + 1));
40    }
41    return Qwen2Tok::from_ranked(ranked, n, err);
42}
43
44int main(int argc, char** argv) {
45    if (argc < 5) {
46        std::fprintf(stderr, "usage: strata-te-tokens run|subset <merges|gguf> <cases.txt> <out>\n");
47        return 2;
48    }
49    const std::string mode = argv[1];
50    std::string err;
51    auto tok = load(argv[2], &err);
52    if (!tok) { std::fprintf(stderr, "%s\n", err.c_str()); return 2; }
53    std::vector<int> ranks;
54    std::vector<std::pair<std::string, int>> emitted;
55    if (mode == "subset") tok->set_trace(&ranks, &emitted);
56    std::ifstream cf(argv[3], std::ios::binary);
57    std::ofstream out(argv[4], std::ios::binary);
58    std::string line;
59    int n = 0;
60    while (std::getline(cf, line)) {
61        const size_t sp = line.find(' ');
62        if (sp == std::string::npos) continue;
63        const std::string name = line.substr(0, sp), prompt = unhex(line.substr(sp + 1));
64        QwenImageTokens t;
65        const std::string e = qwen_image_tokens(*tok, prompt, &t);
66        ++n;
67        if (mode != "run") continue;
68        if (!e.empty()) { out << name << " error " << e << "\n"; continue; }
69        out << name << " ok " << t.n_system << " ";
70        for (size_t i = 0; i < t.ids.size(); ++i) out << (i ? "," : "") << t.ids[i];
71        out << " ";
72        for (size_t i = 0; i < t.weights.size(); ++i) {
73            uint32_t b;
74            std::memcpy(&b, &t.weights[i], 4);
75            char hx[16];
76            std::snprintf(hx, sizeof hx, "%s%08x", i ? "," : "", b);
77            out << hx;
78        }
79        out << "\n";
80    }
81    if (mode == "subset") {
82        // the merges the cases chose, plus, for each emitted merge token, the merge that gives it its id (the last
83        // merge spelling that string); that is all a later run over the same cases looks up successfully
84        std::set<int> keep(ranks.begin(), ranks.end());
85        for (const auto& [s, id] : emitted)
86            if (id >= 256 && id < 151643) keep.insert(id - 256);
87        // the merge texts come from argv[5]: the GGUF's merges, one per line in rank order (written by the caller)
88        if (argc < 6) { std::fprintf(stderr, "subset needs the merges as text (argv 5)\n"); return 2; }
89        std::ifstream mt(argv[5], std::ios::binary);
90        std::vector<std::string> all;
91        while (std::getline(mt, line)) all.push_back(line);
92        out << "n_merges " << all.size() << "\n";
93        for (int r : keep) out << r << "\t" << all.at((size_t) r) << "\n";
94        std::fprintf(stderr, "%d cases, %zu merges kept of %zu\n", n, keep.size(), all.size());
95    }
96    return 0;
97}