okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c7ea3ac6a
main
1"""The picture reader choice for an AMD engine folder (strata_control/vision_reader.py) and the server's fallback from the GPU 2reader to the CPU reader (serve/server.py, Vision._start): hip present, hip missing, hip start failure, then cpu. 3 4python -m unittest tools.test_vision_reader 5""" 6from __future__import annotations 7 8import io 9import tempfile 10import unittest 11from pathlib import Path 12from types import SimpleNamespace 13from unittest import mock 14 15from strata_control import vision_reader as VR 16import serve .server as server 17 18 19def folder (* names :str )-> Path : 20d = Path (tempfile .mkdtemp (prefix = "vr-" )) 21for n in names : 22 (d / n ).write_bytes (b"x" ) 23return d 24 25 26class Choose (unittest .TestCase ): 27def test_the_default_reader_by_card_memory_is_one_rule_for_every_make (self ): 28self .assertEqual (VR .default_mode (12 ),"cpu" ) 29self .assertEqual (VR .default_mode (16 ),"cpu" ) 30self .assertEqual (VR .default_mode (23.5 ),"gpu" ) 31self .assertEqual (VR .default_mode (24 ),"gpu" ) 32self .assertEqual (VR .default_mode (32 ),"gpu" ) 33self .assertEqual (VR .default_mode (None ),"gpu" )# memory unknown: the GPU reader falls back by itself 34for asked ,vram ,want in ((None ,16 ,"cpu" ), ("yes" ,12 ,"cpu" ), ("on" ,32 ,"gpu" ), ("gpu" ,12 ,"gpu" ), ("cpu" ,32 ,"cpu" ), 35 ("no" ,32 ,"none" ), ("off" ,12 ,"none" )): 36self .assertEqual (VR .wanted (asked ,vram ),want , (asked ,vram )) 37 38def test_an_intel_arc_keeps_the_cpu_reader_by_default_and_gpu_is_asked_for (self ): 39"""The one exception to the 24 GB rule: the SYCL reader has run on a 8 GB A750 only (INTEL_GPU_READER_BY_DEFAULT).""" 40self .assertFalse (VR .INTEL_GPU_READER_BY_DEFAULT ) 41for vram in (8 ,16 ,24 ,32 ): 42self .assertEqual (VR .default_mode (vram ,"intel" ),"cpu" ) 43self .assertEqual (VR .wanted ("yes" ,vram ,"intel" ),"cpu" ) 44self .assertEqual (VR .wanted ("gpu" ,vram ,"intel" ),"gpu" ) 45self .assertEqual (VR .wanted ("yes" ,32 ),"gpu" )# other makes: unchanged 46with mock .patch .object (VR ,"INTEL_GPU_READER_BY_DEFAULT" ,True ): 47self .assertEqual (VR .default_mode (32 ,"intel" ),"gpu" ) 48self .assertEqual (VR .default_mode (8 ,"intel" ),"cpu" )# with the exception off, the 24 GB rule is the same as for every make 49 50def test_the_sycl_reader_is_the_gpu_reader_of_an_intel_folder (self ): 51d = folder ("strata-vision" ,"strata-vision-sycl" ) 52r = VR .choose (d ,"gpu" ,windows = False ) 53self .assertEqual ((r .mode ,r .exe .name ,r .cpu_exe .name ), ("gpu" ,"strata-vision-sycl" ,"strata-vision" )) 54self .assertEqual (VR .vision_section (r , {"mmproj" :"m" })["gpu" ],True ) 55r = VR .choose (folder ("strata-vision" ),"gpu" ,windows = False )# no SYCL reader built: the CPU one, said 56self .assertEqual ((r .mode ,r .exe .name ), ("cpu" ,"strata-vision" )) 57self .assertIn ("no GPU picture reader" ,r .note ) 58self .assertEqual (VR .choose (d ,"cpu" ,windows = False ).exe .name ,"strata-vision" ) 59 60def test_hip_present_gives_the_gpu_reader_with_the_cpu_one_to_fall_back_on (self ): 61d = folder ("strata-vision" ,"strata-vision-hip" ) 62for ask in ("yes" ,"on" ,"gpu" ): 63r = VR .choose (d ,ask ,windows = False ,gpu_arch = "gfx1201" ,archs = ["gfx1200" ,"gfx1201" ]) 64self .assertEqual ((r .mode ,r .exe .name ,r .cpu_exe .name ), ("gpu" ,"strata-vision-hip" ,"strata-vision" ),ask ) 65self .assertEqual (r .note ,"" ) 66 67def test_hip_missing_gives_the_cpu_reader (self ): 68d = folder ("strata-vision" ) 69r = VR .choose (d ,"yes" ,windows = False ,gpu_arch = "gfx1201" ) 70self .assertEqual ((r .mode ,r .exe .name ,r .cpu_exe ), ("cpu" ,"strata-vision" ,None )) 71self .assertEqual (r .note ,"" )# "yes" never asked for the GPU in particular 72self .assertIn ("no GPU picture reader" ,VR .choose (d ,"gpu" ,windows = False ).note )# --vision gpu: said once 73 74def test_card_not_in_the_build_gives_the_cpu_reader (self ): 75d = folder ("strata-vision" ,"strata-vision-hip" ) 76r = VR .choose (d ,"yes" ,windows = False ,gpu_arch = "gfx1100" ,archs = ["gfx1200" ,"gfx1201" ]) 77self .assertEqual ((r .mode ,r .exe .name ), ("cpu" ,"strata-vision" )) 78self .assertIn ("gfx1100" ,r .note ) 79 80def test_unknown_card_tries_the_gpu_reader (self ): 81d = folder ("strata-vision" ,"strata-vision-hip" ) 82self .assertEqual (VR .choose (d ,"yes" ,windows = False ,gpu_arch = "" ,archs = ["gfx1201" ]).mode ,"gpu" ) 83self .assertEqual (VR .choose (d ,"yes" ,windows = False ,gpu_arch = "gfx1100" ,archs = None ).mode ,"gpu" ) 84 85def test_gcn_arch_names_with_features_match (self ): 86self .assertEqual (VR .gfx_of ("gfx1201:sramecc-:xnack-" ),"gfx1201" ) 87d = folder ("strata-vision" ,"strata-vision-hip" ) 88self .assertEqual (VR .choose (d ,"yes" ,windows = False ,gpu_arch = "gfx1151:sramecc+:xnack-" ,archs = ["gfx1151" ]).mode ,"gpu" ) 89 90def test_cpu_asked_and_off (self ): 91d = folder ("strata-vision" ,"strata-vision-hip" ) 92self .assertEqual (VR .choose (d ,"cpu" ,windows = False ).exe .name ,"strata-vision" ) 93for ask in ("no" ,"none" ,"off" ,None ): 94self .assertEqual (VR .choose (d ,ask ,windows = False ).mode ,"none" ) 95 96def test_no_reader_at_all (self ): 97d = folder ("strata" ) 98for ask in ("yes" ,"cpu" ,"gpu" ): 99r = VR .choose (d ,ask ,windows = False ) 100self .assertEqual (r .mode ,"none" ,ask ) 101self .assertIn ("no picture reader" ,r .note ) 102 103def test_windows_names (self ): 104d = folder ("strata-vision.exe" ,"strata-vision-hip.exe" ) 105r = VR .choose (d ,"yes" ,windows = True ,gpu_arch = "gfx1201" ,archs = ["gfx1201" ]) 106self .assertEqual ((r .mode ,r .exe .name ,r .cpu_exe .name ), ("gpu" ,"strata-vision-hip.exe" ,"strata-vision.exe" )) 107self .assertEqual (VR .choose (folder ("strata-vision-hip" ),"yes" ,windows = True ).mode ,"none" )# a Linux name on Windows 108 109def test_run_config_section (self ): 110d = folder ("strata-vision" ,"strata-vision-hip" ) 111base = {"mmproj" :"m.gguf" ,"model" :"t.gguf" ,"max_tokens" :1024 } 112sec = VR .vision_section (VR .choose (d ,"yes" ,windows = False ),base ) 113self .assertTrue (sec ["gpu" ]) 114self .assertEqual (Path (sec ["exe" ]).name ,"strata-vision-hip" ) 115self .assertEqual (Path (sec ["cpu_exe" ]).name ,"strata-vision" ) 116cpu = VR .cpu_fallback (sec ) 117self .assertFalse (cpu ["gpu" ]) 118self .assertEqual (Path (cpu ["exe" ]).name ,"strata-vision" ) 119self .assertNotIn ("cpu_exe" ,cpu ) 120self .assertEqual (cpu ["mmproj" ],"m.gguf" ) 121cpu_only = VR .vision_section (VR .choose (folder ("strata-vision" ),"yes" ,windows = False ),base ) 122self .assertFalse (cpu_only ["gpu" ]) 123self .assertNotIn ("cpu_exe" ,cpu_only ) 124self .assertIsNone (VR .cpu_fallback (cpu_only )) 125self .assertIsNone (VR .cpu_fallback ({** sec ,"cpu_exe" :"" })) 126self .assertIsNone (VR .cpu_fallback ({"exe" :"strata-vision" ,"gpu" :True }))# an NVIDIA config has no fallback 127# an on-demand reader (vision.elastic) always has one: the NVIDIA program without --gpu, with the same picture tokens 128nv = VR .cpu_fallback ({"exe" :"/e/strata-vision" ,"gpu" :True ,"max_tokens" :1024 ,"elastic" :True },same_program = True ) 129self .assertEqual ((nv ["exe" ],nv ["gpu" ],nv ["max_tokens" ],"elastic" in nv ), ("/e/strata-vision" ,False ,1024 ,False )) 130self .assertIsNotNone (VR .cpu_fallback ({"exe" :"C:/e/strata-vision.exe" ,"gpu" :True },same_program = True )) 131self .assertIsNone (VR .cpu_fallback ({"exe" :"/e/strata-vision-hip" ,"gpu" :True },same_program = True ))# AMD names its own 132self .assertIsNone (VR .cpu_fallback ({"exe" :"/e/strata-vision" ,"gpu" :False },same_program = True )) 133 134 135class MacReader (unittest .TestCase ): 136"""A Mac has one picture program, strata-vision with Metal built in: --gpu reads on the GPU, without it on the CPU.""" 137 138def test_a_mac_reads_on_the_gpu_by_default_whatever_its_memory (self ): 139for ram in (16 ,32 ,64 ,128 ,None ): 140self .assertEqual (VR .default_mode (ram ,unified = True ),"gpu" ,ram ) 141self .assertEqual (VR .wanted (None ,ram ,unified = True ),"gpu" ,ram ) 142self .assertEqual (VR .wanted ("yes" ,ram ,unified = True ),"gpu" ,ram ) 143self .assertEqual (VR .wanted ("cpu" ,32 ,unified = True ),"cpu" )# asked for: kept 144self .assertEqual (VR .wanted ("off" ,32 ,unified = True ),"none" ) 145self .assertEqual (VR .default_mode (16 ),"cpu" )# a 16 GB card of a PC still follows the 24 GB rule 146 147def test_the_one_program_is_both_readers_and_its_own_fallback (self ): 148d = folder ("strata" ,"strata-vision" ) 149for ask in ("yes" ,"on" ,"gpu" ): 150r = VR .choose (d ,ask ,windows = False ,mac = True ) 151self .assertEqual ((r .mode ,r .exe .name ,r .cpu_exe .name ,r .note ), ("gpu" ,"strata-vision" ,"strata-vision" ,"" ),ask ) 152sec = VR .vision_section (VR .choose (d ,"yes" ,windows = False ,mac = True ), {"mmproj" :"m.gguf" ,"model" :"t.gguf" ,"max_tokens" :1024 }) 153self .assertTrue (sec ["gpu" ]) 154self .assertEqual (sec ["exe" ],sec ["cpu_exe" ]) 155fallback = VR .cpu_fallback (sec ) 156self .assertFalse (fallback ["gpu" ]) 157self .assertEqual (fallback ["exe" ],sec ["exe" ]) 158 159def test_cpu_asked_is_kept_with_the_time_it_takes (self ): 160r = VR .choose (folder ("strata-vision" ),"cpu" ,windows = False ,mac = True ) 161self .assertEqual ((r .mode ,r .exe .name ,r .cpu_exe ), ("cpu" ,"strata-vision" ,None )) 162self .assertIn ("50 seconds" ,r .note ) 163self .assertFalse (VR .vision_section (r , {})["gpu" ]) 164 165def test_an_engine_without_the_reader_gets_pictures_off (self ): 166for ask in ("yes" ,"gpu" ,"cpu" ): 167r = VR .choose (folder ("strata" ),ask ,windows = False ,mac = True ) 168self .assertEqual (r .mode ,"none" ,ask ) 169self .assertIn ("no picture reader" ,r .note ) 170self .assertEqual (VR .choose (folder ("strata-vision" ),"off" ,windows = False ,mac = True ).mode ,"none" ) 171 172def test_a_hip_program_is_not_a_mac_reader (self ): 173r = VR .choose (folder ("strata-vision-hip" ),"yes" ,windows = False ,mac = True ) 174self .assertEqual (r .mode ,"none" ) 175 176 177class FakeProc : 178def __init__ (self ,first_line :str ): 179self .stdin ,self .stdout = io .StringIO (),io .StringIO (first_line ) 180self .killed = False 181 182def kill (self ): 183self .killed = True 184 185def wait (self ,timeout = None ): 186return 0 187 188def poll (self ): 189return None 190 191 192def make_vision (cfg :dict )-> "server.Vision" : 193with mock .patch .object (server .Vision ,"work_dir" ,staticmethod (lambda :Path (tempfile .gettempdir ()))): 194return server .Vision (cfg ,lazy = True ) 195 196 197class Fallback (unittest .TestCase ): 198CFG = {"exe" :"/e/strata-vision-hip" ,"cpu_exe" :"/e/strata-vision" ,"gpu" :True ,"mmproj" :"/m/mm.gguf" , 199"model" :"/m/t.gguf" ,"max_tokens" :1024 ,"threads" :6 } 200 201def run_start (self ,answers :dict ,cfg :dict | None = None ): 202"""answers: exe name -> its first stdout line, or an exception to raise from popen.""" 203started = [] 204 205def popen (what ,args ,** kw ): 206name = Path (args [0 ]).name 207started .append (args ) 208a = answers [name ] 209if isinstance (a ,Exception ): 210raise a 211return FakeProc (a ) 212 213v = make_vision (cfg or self .CFG ) 214out = io .StringIO () 215with mock .patch .object (server ,"popen" ,popen ),mock .patch ("builtins.print" ,lambda * a ,** k :out .write (" " .join (map (str ,a ))+ "\n" )): 216err = None 217try : 218v ._start () 219except RuntimeError as e : 220err = e 221return v ,started ,out .getvalue (),err 222 223def test_hip_present_and_starts (self ): 224v ,started ,log ,err = self .run_start ({"strata-vision-hip" :"READY 2048\n" }) 225self .assertIsNone (err ) 226self .assertEqual ([Path (a [0 ]).name for a in started ], ["strata-vision-hip" ]) 227self .assertIn ("--gpu" ,started [0 ]) 228self .assertEqual (log ,"" ) 229self .assertFalse (v .stopped ) 230 231def test_hip_start_failure_falls_back_to_cpu_with_one_line (self ): 232v ,started ,log ,err = self .run_start ({"strata-vision-hip" :"ERR this GPU (gfx1100) is not in the picture reader's build (gfx1200,gfx1201)\n" , 233"strata-vision" :"READY 2048\n" }) 234self .assertIsNone (err ) 235self .assertEqual ([Path (a [0 ]).name for a in started ], ["strata-vision-hip" ,"strata-vision" ]) 236self .assertNotIn ("--gpu" ,started [1 ]) 237self .assertIn ("--threads" ,started [1 ]) 238self .assertEqual (log .count ("\n" ),1 ) 239self .assertIn ("gfx1100" ,log ) 240self .assertIn ("CPU" ,log ) 241self .assertFalse (v .stopped ) 242self .assertIsNone (v .fallback ) 243 244def test_hip_process_that_dies_silently_falls_back (self ):# a GPU runtime that aborts: stdout closes empty 245v ,started ,log ,err = self .run_start ({"strata-vision-hip" :"" ,"strata-vision" :"READY 2048\n" }) 246self .assertIsNone (err ) 247self .assertEqual (len (started ),2 ) 248self .assertEqual (log .count ("\n" ),1 ) 249 250def test_hip_missing_file_falls_back (self ):# popen raises (exe gone, not executable) 251v ,started ,log ,err = self .run_start ({"strata-vision-hip" :FileNotFoundError ("strata-vision-hip" ), 252"strata-vision" :"READY 2048\n" }) 253self .assertIsNone (err ) 254self .assertEqual (len (started ),2 ) 255 256def test_restart_after_a_fallback_stays_on_the_cpu_reader (self ): 257v ,started ,log ,err = self .run_start ({"strata-vision-hip" :"ERR nope\n" ,"strata-vision" :"READY 2048\n" }) 258started .clear () 259with mock .patch .object (server ,"popen" ,lambda what ,args ,** kw :started .append (args )or FakeProc ("READY 2048\n" )): 260v .restart () 261self .assertEqual ([Path (a [0 ]).name for a in started ], ["strata-vision" ]) 262 263def test_cpu_failure_after_the_fallback_is_an_error (self ): 264v ,started ,log ,err = self .run_start ({"strata-vision-hip" :"ERR nope\n" ,"strata-vision" :"ERR cannot load the vision encoder\n" }) 265self .assertIsNotNone (err ) 266self .assertIn ("cannot load" ,str (err )) 267 268def test_a_config_without_cpu_exe_has_no_fallback (self ):# NVIDIA: the error is shown as before 269cfg = {k :v for k ,v in self .CFG .items ()if k != "cpu_exe" } 270v ,started ,log ,err = self .run_start ({"strata-vision-hip" :"ERR nope\n" },cfg ) 271self .assertIsNotNone (err ) 272self .assertEqual (len (started ),1 ) 273self .assertEqual (log ,"" ) 274 275def test_the_ready_line_names_the_reader_and_older_readers_still_start (self ): 276"""strata-vision says which reader runs ("READY <n> own" / "READY <n> mtmd"); an older one says "READY <n>" and starts as before.""" 277cpu = {"exe" :"/e/strata-vision" ,"gpu" :False ,"mmproj" :"/m/mm.gguf" ,"model" :"/m/t.gguf" } 278for line ,reader in (("READY 2560 own\n" ,"own" ), ("READY 2560 mtmd\n" ,"mtmd" ), ("READY 2048\n" ,None )): 279v ,started ,log ,err = self .run_start ({"strata-vision" :line },cpu ) 280self .assertIsNone (err ,line ) 281self .assertEqual (v .reader ,reader ,line ) 282self .assertEqual (log ,"" ,line )# nothing said when the chosen reader started 283 284def test_a_fallback_to_llama_cpps_reader_is_said_once (self ): 285cpu = {"exe" :"/e/strata-vision" ,"gpu" :False ,"mmproj" :"/m/mm.gguf" ,"model" :"/m/t.gguf" } 286v ,started ,log ,err = self .run_start ({"strata-vision" :"READY 2560 mtmd fallback\n" },cpu ) 287self .assertIsNone (err ) 288self .assertEqual ((v .reader ,len (started )), ("mtmd" ,1 ))# the same process: no second start 289self .assertEqual (log .count ("[strata] Strata's picture reader did not start; pictures are read with llama.cpp's reader." ),1 ) 290 291def test_the_reader_choice_reaches_the_reader_through_the_run_configs_env (self ): 292"""The settings write STRATA_VISION_TOWER=mtmd into the model's run config "env" (strata_control/speed_switches.py picture_reader); the server 293hands that env to the picture reader as to the engine, so no reader argument changes (an older reader ignores the variable).""" 294cfg = {"env" : {"STRATA_VISION_TOWER" :"mtmd" },"vision" : {"exe" :"/e/strata-vision" ,"mmproj" :"/m/mm.gguf" ,"model" :"/m/t.gguf" }} 295env = server .vision_env (cfg ,server .child_env (cfg )) 296self .assertEqual (env .get ("STRATA_VISION_TOWER" ),"mtmd" ) 297self .assertNotIn ("--tower" ,server .Vision .reader_args (cfg ["vision" ])) 298with mock .patch .dict ("os.environ" , {},clear = False ): 299import os 300os .environ .pop ("STRATA_VISION_TOWER" ,None ) 301self .assertNotIn ("STRATA_VISION_TOWER" ,server .vision_env ({},server .child_env ({})))# a default file carries nothing 302 303def test_cpu_config_is_unchanged (self ): 304cfg = {"exe" :"/e/strata-vision" ,"gpu" :False ,"mmproj" :"/m/mm.gguf" ,"model" :"/m/t.gguf" } 305v ,started ,log ,err = self .run_start ({"strata-vision" :"READY 2048\n" },cfg ) 306self .assertIsNone (err ) 307self .assertNotIn ("--gpu" ,started [0 ]) 308 309 310if __name__ == "__main__" : 311unittest .main ()