okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
11c8c701a
main
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 ; 25for (size_t i = 0 ;i + 1 < h .size ();i += 2 )s += (char ) std::stoi (h .substr (i ,2 ),nullptr ,16 ); 26return s ; 27} 28 29static std::unique_ptr < Qwen2Tok > load (const std::string & path , std::string * err ) { 30if (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 ; 33int n = 0 ; 34 std::vector < std::pair < int , std::string >> ranked ; 35while (std::getline (f ,line )) { 36if (line .rfind ("n_merges " ,0 )== 0 ) {n = std::stoi (line .substr (9 ));continue ; } 37const size_t tab = line .find ('\t' ); 38if (tab == std::string::npos )continue ; 39ranked .emplace_back (std::stoi (line .substr (0 ,tab )),line .substr (tab + 1 )); 40 } 41return Qwen2Tok ::from_ranked (ranked ,n ,err ); 42} 43 44int main (int argc ,char ** argv ) { 45if (argc < 5 ) { 46 std::fprintf (stderr ,"usage: strata-te-tokens run|subset <merges|gguf> <cases.txt> <out>\n" ); 47return 2 ; 48 } 49const std::string mode = argv [1 ]; 50 std::string err ; 51auto tok = load (argv [2 ],& err ); 52if (!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 ; 55if (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 ; 59int n = 0 ; 60while (std::getline (cf ,line )) { 61const size_t sp = line .find (' ' ); 62if (sp == std::string::npos )continue ; 63const std::string name = line .substr (0 ,sp ),prompt = unhex (line .substr (sp + 1 )); 64QwenImageTokens t ; 65const std::string e = qwen_image_tokens (* tok ,prompt ,& t ); 66++ n ; 67if (mode != "run" )continue ; 68if (!e .empty ()) {out <<name <<" error " <<e <<"\n" ;continue ; } 69out <<name <<" ok " <<t .n_system <<" " ; 70for (size_t i = 0 ;i < t .ids .size ();++ i )out << (i ?"," :"" ) <<t .ids [i ]; 71out <<" " ; 72for (size_t i = 0 ;i < t .weights .size ();++ i ) { 73uint32_t b ; 74 std::memcpy (& b ,& t .weights [i ],4 ); 75char hx [16 ]; 76 std::snprintf (hx ,sizeof hx ,"%s%08x" ,i ?"," :"" ,b ); 77out <<hx ; 78 } 79out <<"\n" ; 80 } 81if (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 ()); 85for (const auto & [s ,id ] :emitted ) 86if (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) 88if (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 ; 91while (std::getline (mt ,line ))all .push_back (line ); 92out <<"n_merges " <<all .size () <<"\n" ; 93for (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 } 96return 0 ; 97}