okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2503b206b
main
1"""A picture reader that cannot start because its card has no room (serve/server.py vision_start_error, VisionNoRoom): the server names the cause 2instead of the last line of whatever a debugger printed. The readers are small Python scripts run as real processes; their stderr is a log file, as the 3engine's log is in the product. 4 5python -m serve.test_vision_no_room 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 18 19ALLOC = "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 865.48 MiB on device 0: cudaMalloc failed: out of memory" 20BUFFER = "alloc_tensor_range: failed to allocate CUDA0 buffer of size 907524736" 21ASSERT = "ggml-backend.cpp:188: GGML_ASSERT(buffer) failed" 22 23 24def plain_popen (what ,args ,** kw ): 25kw .pop ("role" ,None ) 26return subprocess .Popen (args ,** kw ) 27 28 29class NoRoom (unittest .TestCase ): 30def setUp (self ): 31self .tmp = tempfile .TemporaryDirectory () 32self .addCleanup (self .tmp .cleanup ) 33self .procs = [] 34self .logs = [] 35self .addCleanup (self .stop_all ) 36 37def stop_all (self ): 38for p in self .procs : 39if p .poll ()is None : 40p .kill () 41p .wait (timeout = 10 ) 42for stream in (p .stdin ,p .stdout ): 43with contextlib .suppress (OSError ,ValueError ): 44stream .close () 45for f in self .logs : 46with contextlib .suppress (OSError ): 47f .close () 48 49def log (self ,text = "" ): 50path = Path (self .tmp .name )/ ("engine%d.log" % len (self .logs )) 51path .write_text (text ,encoding = "utf-8" ) 52f = open (path ,"a" ,encoding = "utf-8" ) 53self .logs .append (f ) 54return f 55 56def reader (self ,body :str ,log ): 57script = Path (self .tmp .name )/ ("reader%d.py" % len (list (Path (self .tmp .name ).glob ("reader*.py" )))) 58script .write_text ("import os, sys, time\n" + textwrap .dedent (body ),encoding = "utf-8" ) 59v = server .Vision .__new__ (server .Vision ) 60v .spawn = ([sys .executable ,"-u" ,str (script )],log ,None ) 61v .dir = Path (self .tmp .name ) 62v .stopped ,v .proc = True ,None 63return v 64 65def start (self ,v ): 66error = None 67 68def keep (what ,args ,** kw ): 69p = plain_popen (what ,args ,** kw ) 70self .procs .append (p ) 71return p 72 73with mock .patch .object (server ,"popen" ,keep ),contextlib .redirect_stdout (io .StringIO ()): 74try : 75v ._start_reader () 76except RuntimeError as e : 77error = e 78return error 79 80OOM_READER = """ 81print("[New LWP 477206]", flush=True) 82sys.stderr.write(%r + "\\n" + %r + "\\n" + %r + "\\n") 83sys.stderr.flush() 84print("Python Exception <class 'gdb.error'>: Couldn't get registers: No such process.", flush=True) 85sys.exit(1) 86""" % (ALLOC ,BUFFER ,ASSERT ) 87 88def test_the_v100_launch_in_miniature (self ): 89v = self .reader (self .OOM_READER ,self .log ()) 90error = self .start (v ) 91self .assertIsInstance (error ,server .VisionNoRoom ) 92self .assertEqual (error .room .device ,0 ) 93self .assertAlmostEqual (error .room .need_mib ,865.48 ,places = 2 ) 94text = str (error ) 95self .assertIn ("the vision encoder did not start: the graphics card has no room for it" ,text ) 96self .assertIn ("865 MiB" ,text ) 97self .assertIn ("its card 0" ,text ) 98self .assertIn ("cudaMalloc failed: out of memory" ,text ) 99 100def test_a_reader_without_a_log_that_prints_it_on_its_output_is_found_too (self ): 101v = self .reader (""" 102print(%r, flush=True) 103sys.exit(1) 104""" % ALLOC ,None ) 105error = self .start (v ) 106self .assertIsInstance (error ,server .VisionNoRoom ) 107 108def test_lines_an_earlier_start_left_in_the_shared_log_are_not_this_starts (self ): 109log = self .log (ALLOC + "\n" + BUFFER + "\n" + ASSERT + "\n" )# the last launch's failure, still in the file 110v = self .reader (""" 111print("ERR cannot open the text model x.gguf (no such file)", flush=True) 112sys.exit(1) 113""" ,log ) 114error = self .start (v ) 115self .assertNotIsInstance (error ,server .VisionNoRoom ) 116self .assertIn ("ERR cannot open the text model" ,str (error )) 117self .assertNotIn ("no room" ,str (error )) 118 119def test_a_card_that_is_not_there_is_not_a_full_card (self ): 120v = self .reader (""" 121sys.stderr.write("ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device\\n") 122sys.exit(1) 123""" ,self .log ()) 124error = self .start (v ) 125self .assertNotIsInstance (error ,server .VisionNoRoom ) 126self .assertNotIn ("no room" ,str (error )) 127 128def test_the_message_function_alone (self ): 129log = self .log (ALLOC + "\n" + ASSERT + "\n" ) 130text = server .vision_start_error ("" ,log ,134 ) 131self .assertIn ("the graphics card has no room for it (it asked for 865 MiB on its card 0)" ,text ) 132self .assertEqual (server .vision_start_error ("ERR no mmproj\n" ),"the vision encoder did not start: ERR no mmproj" )# unchanged 133 134def test_a_cause_the_server_names_already_keeps_its_own_words (self ): 135busy = server .vision_start_error ("cudaMalloc failed: CUDA-capable device(s) is/are busy or unavailable" ) 136self .assertIn ("Exclusive_Process" ,busy ) 137 138 139if __name__ == "__main__" : 140unittest .main ()