okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2503b206b
main
1"""The picture reader reads on the processor instead of ending the launch (owner, 2026-11-10): the NVIDIA reader (strata-vision) is the CPU reader too, 2without --gpu, so a GPU reader that does not start - resident or on demand, whatever the cause - falls back to it, with one log line; when the cause was a 3card with no room, the words for the Terminal's summary go with it (Vision.cpu_note, /metrics engine.vision_note, Runner.report_vision). 4 5python -m serve.test_vision_cpu_fallback 6""" 7import contextlib 8import io 9import subprocess 10import sys 11import tempfile 12import textwrap 13import unittest 14from pathlib import Path 15from unittest import mock 16 17import serve .server as server 18from strata_cli .launch import runner as R 19 20ALLOC = "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 865.48 MiB on device 0: cudaMalloc failed: out of memory" 21NOTE = ("Pictures are read on the processor, which is slower: graphics card 2 has no room for the picture reader " 22"(it needs about 1.5 GB; 0.1 GB are free). To read them on the card, close the other programs that use it and start again." ) 23 24 25def plain_popen (what ,args ,** kw ): 26kw .pop ("role" ,None ) 27return subprocess .Popen (args ,** kw ) 28 29 30class Construction (unittest .TestCase ): 31def make (self ,** vision ): 32cfg = {"exe" :"/engine/strata-vision" ,"mmproj" :"/m/mmproj.gguf" ,"model" :"/m/model.gguf" ,"gpu" :True ,** vision } 33with mock .patch .object (server .Vision ,"work_dir" ,return_value = Path (tempfile .gettempdir ())): 34return server .Vision (cfg ,lazy = True ) 35 36def test_the_nvidia_reader_has_the_cpu_reader_in_every_mode (self ): 37for extra in ({}, {"elastic" :True }, {"elastic" :False }, {"cuda_device" :1 }): 38with self .subTest (extra = extra ): 39v = self .make (** extra ) 40self .assertIn ("--gpu" ,v .spawn [0 ]) 41self .assertIsNotNone (v .fallback ) 42self .assertNotIn ("--gpu" ,v .fallback [0 ]) 43self .assertTrue (v .fallback [0 ][0 ].endswith ("strata-vision" )) 44self .assertIsNone (v .cpu_note ) 45 46def test_the_persons_image_limit_goes_with_it (self ): 47v = self .make (max_tokens = 1024 ) 48self .assertEqual (v .fallback [0 ][v .fallback [0 ].index ("--max-tokens" )+ 1 ],"1024" ) 49 50def test_a_cpu_reader_has_nothing_to_fall_back_to (self ): 51self .assertIsNone (self .make (gpu = False ).fallback ) 52 53def test_amd_readers_keep_their_own_cpu_reader (self ): 54self .assertIsNone (self .make (exe = "/engine/strata-vision-hip" ).fallback )# no cpu_exe next to it: nothing to fall back to 55v = self .make (exe = "/engine/strata-vision-hip" ,cpu_exe = "/engine/strata-vision" ) 56self .assertTrue (v .fallback [0 ][0 ].endswith ("strata-vision" )) 57 58 59class Fallback (unittest .TestCase ): 60def setUp (self ): 61self .tmp = tempfile .TemporaryDirectory () 62self .addCleanup (self .tmp .cleanup ) 63self .procs ,self .logs = [], [] 64self .addCleanup (self .stop_all ) 65 66def stop_all (self ): 67for p in self .procs : 68if p .poll ()is None : 69p .kill () 70p .wait (timeout = 10 ) 71for stream in (p .stdin ,p .stdout ): 72with contextlib .suppress (OSError ,ValueError ): 73stream .close () 74for f in self .logs : 75with contextlib .suppress (OSError ): 76f .close () 77 78def script (self ,body ): 79path = Path (self .tmp .name )/ ("reader%d.py" % len (list (Path (self .tmp .name ).glob ("reader*.py" )))) 80path .write_text ("import os, sys, time\n" + textwrap .dedent (body ),encoding = "utf-8" ) 81return [sys .executable ,"-u" ,str (path )] 82 83def vision (self ,gpu_body ,cpu_body ,env = None ): 84path = Path (self .tmp .name )/ "engine.log" 85path .write_text ("" ,encoding = "utf-8" ) 86log = open (path ,"a" ,encoding = "utf-8" ) 87self .logs .append (log ) 88v = server .Vision .__new__ (server .Vision ) 89v .spawn = (self .script (gpu_body ),log ,env ) 90v .fallback = (self .script (cpu_body ),log ,env ) 91v .gpu_spawn ,v .cpu_spawn = v .spawn ,v .fallback 92v .dir = Path (self .tmp .name ) 93v .stopped ,v .proc ,v .on_cpu ,v .cpu_note ,v .elastic = True ,None ,False ,None ,False 94return v 95 96def start (self ,v ): 97out ,error = io .StringIO (),None 98 99def keep (what ,args ,** kw ): 100p = plain_popen (what ,args ,** kw ) 101self .procs .append (p ) 102return p 103 104with mock .patch .object (server ,"popen" ,keep ),mock .patch .object (server ,"card_free_mib" ,lambda index ,amd = False :114 ),contextlib .redirect_stdout (out ): 105try : 106v ._start () 107except RuntimeError as e : 108error = e 109return error ,out .getvalue () 110 111OOM = """ 112sys.stderr.write(%r + "\\n") 113sys.stderr.flush() 114print("[New LWP 5]", flush=True) 115sys.exit(1) 116""" % ALLOC 117READY = """ 118print("READY 2560", flush=True) 119sys.stdin.readline() 120""" 121 122def test_a_full_card_reads_on_the_processor_and_says_so (self ): 123v = self .vision (self .OOM ,self .READY ,env = {"CUDA_VISIBLE_DEVICES" :"2,3" }) 124error ,said = self .start (v ) 125self .assertIsNone (error ) 126self .assertTrue (v .on_cpu ) 127self .assertTrue (v .alive ()) 128self .assertEqual (v .cpu_note ,NOTE )# card 2: the reader's card 0 of "2,3"; 114 MiB free read after it failed 129self .assertIn ("[strata] the GPU picture reader did not start" ,said ) 130self .assertIn ("pictures are read on the CPU" ,said ) 131 132def test_the_need_in_the_note_follows_the_image_limit (self ): 133v = self .vision (self .OOM ,self .READY ,env = {"CUDA_VISIBLE_DEVICES" :"2,3" }) 134v .max_tokens = 16384 135self .start (v ) 136self .assertIn ("(it needs about 3.6 GB; 0.1 GB are free)" ,v .cpu_note ) 137 138def test_a_reader_that_starts_changes_nothing (self ): 139v = self .vision (self .READY ,self .OOM ) 140error ,said = self .start (v ) 141self .assertIsNone (error ) 142self .assertFalse (v .on_cpu ) 143self .assertIsNone (v .cpu_note ) 144self .assertNotIn ("did not start" ,said ) 145 146def test_another_cause_falls_back_too_without_the_full_card_words (self ): 147v = self .vision (""" 148print("ERR cannot load the vision encoder x.gguf", flush=True) 149sys.exit(1) 150""" ,self .READY ) 151error ,said = self .start (v ) 152self .assertIsNone (error ) 153self .assertTrue (v .on_cpu ) 154self .assertIn ("ERR cannot load the vision encoder" ,said ) 155self .assertEqual (v .cpu_note ,"Pictures are read on the processor, which is slower: the picture reader could not start on graphics card 0. " 156"The reason is in the engine's log: " + self .logs [0 ].name + "." ) 157 158def test_the_card_in_that_sentence_is_the_first_one_the_reader_is_shown (self ): 159v = self .vision (""" 160print("ERR cannot load the vision encoder x.gguf", flush=True) 161sys.exit(1) 162""" ,self .READY ,env = {"CUDA_VISIBLE_DEVICES" :"3,1" }) 163self .start (v ) 164self .assertIn ("could not start on graphics card 3. The reason" ,v .cpu_note ) 165 166def test_on_a_mac_there_is_no_card_number (self ): 167v = self .vision (""" 168print("ERR cannot load the vision encoder x.gguf", flush=True) 169sys.exit(1) 170""" ,self .READY ) 171with mock .patch .object (server .sys ,"platform" ,"darwin" ): 172self .start (v ) 173self .assertTrue (v .cpu_note .startswith ("Pictures are read on the processor, which is slower: the picture reader could not start. The reason is in" )) 174 175def test_the_cpu_reader_failing_too_ends_it_with_its_own_error (self ): 176v = self .vision (self .OOM ,""" 177print("ERR cannot open the text model x.gguf (no such file)", flush=True) 178sys.exit(1) 179""" ) 180error ,_ = self .start (v ) 181self .assertIsNotNone (error ) 182self .assertIn ("ERR cannot open the text model" ,str (error )) 183 184def test_the_card_is_unknown_when_the_reader_saw_no_such_card (self ): 185v = self .vision (self .OOM ,self .READY ,env = {"CUDA_VISIBLE_DEVICES" :"" }) 186v .spawn = (v .spawn [0 ],v .spawn [1 ], {"CUDA_VISIBLE_DEVICES" :"GPU-8c3f" }) 187error ,_ = self .start (v ) 188self .assertIsNone (error ) 189self .assertEqual (v .cpu_note ,"Pictures are read on the processor, which is slower: the graphics card has no room for the picture reader " 190"(it needs about 1.5 GB). To read them on the card, close the other programs that use it and start again." ) 191 192def test_an_on_demand_reader_that_goes_up_again_loses_the_note (self ): 193v = self .vision (self .OOM ,self .READY ,env = {"CUDA_VISIBLE_DEVICES" :"2,3" }) 194v .elastic = True 195self .start (v ) 196self .assertEqual (v .cpu_note ,NOTE ) 197v .close = lambda :None 198v .unload () 199self .assertIsNone (v .cpu_note ) 200 201 202class ShownInTheSummary (unittest .TestCase ): 203def runner (self ): 204r = R .Runner .__new__ (R .Runner ) 205r .notices ,r .events = [], [] 206r .emit = lambda stage ,text ,pct = None ,level = "info" ,** data :r .events .append ((stage ,text ,level )) 207return r 208 209def test_the_note_from_the_server_is_a_notice_and_an_event (self ): 210r = self .runner () 211r .report_vision ({"vision_note" :NOTE }) 212self .assertEqual (r .notices , [NOTE ]) 213self .assertEqual (r .events , [("start" ,NOTE ,"warn" )]) 214 215def test_it_is_said_once (self ): 216r = self .runner () 217r .report_vision ({"vision_note" :NOTE }) 218r .report_vision ({"vision_note" :NOTE }) 219self .assertEqual (len (r .notices ),1 ) 220 221def test_no_note_no_line (self ): 222r = self .runner () 223r .report_vision ({}) 224r .report_vision ({"vision_note" :None }) 225self .assertEqual ((r .notices ,r .events ), ([], [])) 226 227 228if __name__ == "__main__" : 229unittest .main ()