okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b749503db
main
1"""tools/test_te_tokenizer.py - our Qwen-Image text-encoder tokenizer (src/image/te/qwen2_tok.cpp) against the token ids 2stable-diffusion.cpp 2988060 built for the same prompts. 3 4The expected ids were recorded from sd.cpp itself (tools/image/gate/te_tok_collect.py: a stage-hook build of sd-cli that 5dumps the ids LLMEmbedder::encode_prompt hands its text encoder) for every case of tools/image/gate/te_tok_cases.py and 6stored in tools/image/gate/data/te_tok_expected.json. The test needs no GPU, no sd.cpp and no model file: it compiles 7the tokenizer with the system C++ compiler and gives it only the merges those cases use 8(tools/image/gate/data/te_tok_merges.txt, with their ranks in the full 151,387-merge list). 9 10python -m pytest tools/test_te_tokenizer.py""" 11import base64 12import json 13import shutil 14import struct 15import subprocess 16import tempfile 17import unittest 18from pathlib import Path 19 20ROOT = Path (__file__ ).resolve ().parents [1 ] 21DATA = ROOT / "tools" / "image" / "gate" / "data" 22CXX = shutil .which ("g++" )or shutil .which ("c++" )or shutil .which ("clang++" ) 23 24 25@ unittest . skipUnless ( CXX , "no C++ compiler" ) 26class TokenizerMatchesSdcpp (unittest .TestCase ): 27@classmethod 28def setUpClass (cls ): 29cls .tmp = tempfile .TemporaryDirectory () 30d = Path (cls .tmp .name ) 31cls .exe = d / "strata-te-tokens" 32r = subprocess .run ([CXX ,"-std=c++17" ,"-O2" ,"-I" ,str (ROOT / "include" ),"-I" ,str (ROOT / "src" / "image" / "te" ), 33str (ROOT / "src" / "image" / "te" / "qwen2_tok.cpp" ), 34str (ROOT / "src" / "image" / "te" / "qwen2_tok_main.cpp" ),"-o" ,str (cls .exe )], 35capture_output = True ,text = True ,timeout = 600 ) 36if r .returncode != 0 : 37raise AssertionError ("build failed:\n" + r .stderr [- 4000 :]) 38cls .cases = json .loads ((DATA / "te_tok_expected.json" ).read_text ()) 39lines = [f" { c [ 'name' ] } { base64 . b64decode ( c [ 'prompt_b64' ]). hex () } " for c in cls .cases ] 40 (d / "cases.txt" ).write_text ("\n" .join (lines )+ "\n" ,newline = "\n" )# read byte for byte: no CR on Windows 41r = subprocess .run ([str (cls .exe ),"run" ,str (DATA / "te_tok_merges.txt" ),str (d / "cases.txt" ),str (d / "out.txt" )], 42capture_output = True ,text = True ,timeout = 600 ) 43if r .returncode != 0 : 44raise AssertionError ("run failed:\n" + r .stderr [- 4000 :]) 45cls .got = {} 46for line in (d / "out.txt" ).read_text ().splitlines (): 47parts = line .split (" " ,2 ) 48cls .got [parts [0 ]]= parts [1 :] 49 50@classmethod 51def tearDownClass (cls ): 52cls .tmp .cleanup () 53 54def test_case_set_is_wide (self ): 55names = [c ["name" ]for c in self .cases ] 56self .assertGreaterEqual (len (names ),380 ) 57for prefix in ("gate_" ,"long_" ,"special_" ,"gguf_literal_" ,"w_" ,"u_" ,"e_" ,"l_" ,"x_" ,"r_" ): 58self .assertTrue (any (n .startswith (prefix )for n in names ),prefix ) 59 60def test_every_case_matches (self ): 61bad = [] 62for c in self .cases : 63g = self .got .get (c ["name" ]) 64if g is None : 65bad .append ((c ["name" ],"no output" )) 66continue 67if "error" in c : 68if g [0 ]!= "error" : 69bad .append ((c ["name" ],"sd.cpp refused the prompt, we did not" )) 70continue 71if g [0 ]!= "ok" : 72bad .append ((c ["name" ],f"we refused: { g [ 1 ] } " )) 73continue 74n_sys ,ids ,w = g [1 ].split (" " ) 75ids = [int (x )for x in ids .split ("," )]if ids else [] 76want_w = [struct .unpack ("<I" ,struct .pack ("<f" ,x ))[0 ]for x in c ["weights" ]] 77got_w = [int (x ,16 )for x in w .split ("," )]if w else [] 78if int (n_sys )!= c ["n_system" ]: 79bad .append ((c ["name" ],f"n_system { n_sys } vs { c [ 'n_system' ] } " )) 80elif ids != c ["ids" ]: 81k = next ((i for i , (a ,b )in enumerate (zip (ids ,c ["ids" ]))if a != b ),min (len (ids ),len (c ["ids" ]))) 82bad .append ((c ["name" ],f"ids differ at { k } : ours { ids [ k : k + 5 ] } sd.cpp { c [ 'ids' ][ k : k + 5 ] } " 83f"( { len ( ids ) } vs { len ( c [ 'ids' ]) } ids)" )) 84elif got_w != want_w : 85bad .append ((c ["name" ],"weights differ" )) 86self .assertEqual (bad , [],f" { len ( bad ) } of { len ( self . cases ) } cases differ" ) 87 88 89if __name__ == "__main__" : 90unittest .main ()