okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e1d62961
main
1"""Importing a model together with its picture reader (mmproj), and the plain words when a model type is not ours.""" 2 3import io 4import json 5import struct 6import tempfile 7import unittest 8from contextlib import redirect_stdout 9from pathlib import Path 10 11from unittest import mock 12 13from strata_cli .cli import main 14from strata_control import models 15from tests .terminal .test_models import gguf_fixture 16 17 18def projector_fixture (projector_type = "qwen3vl_merger" ,architecture = "clip" )-> bytes : 19def string (value ): 20raw = value .encode () 21return struct .pack ("<Q" ,len (raw ))+ raw 22 23pairs = [("general.architecture" ,8 ,string (architecture )), ("clip.projector_type" ,8 ,string (projector_type ))] 24header = (b"GGUF" + struct .pack ("<IQQ" ,3 ,1 ,len (pairs ))+ 25b"" .join (string (key )+ struct .pack ("<I" ,kind )+ value for key ,kind ,value in pairs )) 26header += string ("v.blk.0.attn_q.weight" )+ struct .pack ("<IQIQ" ,1 ,64 ,1 ,0 ) 27return header + b"\x00" * (- len (header )% 32 )+ b"\x00" * 128 28 29 30class ImportProjectorTests (unittest .TestCase ): 31def setUp (self ): 32self .temp = tempfile .TemporaryDirectory () 33self .addCleanup (self .temp .cleanup ) 34self .source = Path (self .temp .name )/ "model" 35self .source .mkdir () 36 37def plan (self ,path ): 38output = io .StringIO () 39with redirect_stdout (output ): 40main (["models" ,"import" ,str (path ),"--plan" ,"--output" ,"json" ]) 41return json .loads (output .getvalue ())["plan" ] 42 43def model (self ,** kwargs ): 44first = self .source / "Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00001.gguf" 45first .write_bytes (gguf_fixture (** kwargs )) 46return first 47 48def test_pinned_projector_next_to_a_model_means_pictures (self ): 49self .model () 50file = self .source / "mmproj-Qwen3.8-Flash-Next-BF16.gguf" 51file .write_bytes (projector_fixture ()) 52with mock .patch .dict (models .PINNED_PROJECTORS , {file .name .lower ():file .stat ().st_size }): 53plan = self .plan (self .source ) 54self .assertTrue (plan ["reads_pictures" ]) 55self .assertEqual (plan ["projectors" ][0 ]["status" ],"ok" ) 56self .assertIn ("pictures will work" ,plan ["messages" ][0 ]) 57 58def test_same_name_but_another_size_is_not_called_official (self ): 59self .model () 60 (self .source / "mmproj-Qwen3.8-Flash-Next-BF16.gguf" ).write_bytes (projector_fixture ()) 61plan = self .plan (self .source ) 62self .assertEqual (plan ["projectors" ][0 ]["status" ],"other_name" ) 63 64def test_projector_beside_a_chosen_model_file_is_found (self ): 65first = self .model () 66 (self .source / "mmproj-F16.gguf" ).write_bytes (projector_fixture ()) 67plan = self .plan (first ) 68self .assertEqual ([p ["name" ]for p in plan ["projectors" ]], ["mmproj-F16.gguf" ]) 69self .assertTrue (plan ["reads_pictures" ]) 70 71def test_projector_of_another_kind_is_not_used (self ): 72self .model () 73 (self .source / "mmproj-other.gguf" ).write_bytes (projector_fixture ("mlp" )) 74plan = self .plan (self .source ) 75self .assertFalse (plan ["reads_pictures" ]) 76self .assertEqual (plan ["projectors" ][0 ]["status" ],"unsupported" ) 77self .assertIn ("Custom model API" ,plan ["projectors" ][0 ]["plain" ]) 78 79def test_other_name_projector_of_our_kind_is_explained (self ): 80self .model () 81 (self .source / "mmproj-mine.gguf" ).write_bytes (projector_fixture ()) 82plan = self .plan (self .source ) 83self .assertEqual (plan ["projectors" ][0 ]["status" ],"other_name" ) 84self .assertTrue (plan ["reads_pictures" ]) 85 86def test_unsupported_model_type_says_custom_api (self ): 87first = self .source / "SmolVLM-256M-Q8_0.gguf" 88first .write_bytes (gguf_fixture (architecture = "llama" )) 89 (self .source / "mmproj-SmolVLM-256M-Instruct-Q8_0.gguf" ).write_bytes (projector_fixture ("idefics3" )) 90plan = self .plan (self .source ) 91self .assertEqual (plan ["file_set_status" ],"invalid_or_incomplete" ) 92self .assertFalse (plan ["reads_pictures" ]) 93self .assertEqual (plan ["messages" ], ["This model type isn't supported by the Strata engine. " 94"Add it as a Custom model API (llama-server) instead." ]) 95 96def test_choosing_the_projector_itself_says_so (self ): 97projector = self .source / "mmproj-F16.gguf" 98projector .write_bytes (projector_fixture ()) 99output = io .StringIO () 100with redirect_stdout (output ): 101code = main (["models" ,"import" ,str (projector ),"--plan" ,"--output" ,"json" ]) 102self .assertEqual (code ,5 ) 103self .assertIn ("picture reader" ,json .dumps (json .loads (output .getvalue ()))) 104 105 106if __name__ == "__main__" : 107unittest .main ()