okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2503b206b
main
1"""A launch onto a card another program has filled: the picture reader's out-of-memory lines are read as what they are (strata_control/reader_room.py), 2and the launch's error says so in plain words with the next step, not the last line of whatever a debugger printed. Lane 10 (V100 box), 11-10: 3"Python Exception <class 'gdb.error'>: Couldn't get registers: No such process". 4""" 5import json 6import tempfile 7import unittest 8from pathlib import Path 9from unittest import mock 10 11from strata_cli .launch import runner as R 12from strata_control import reader_room ,split_engine 13 14ALLOC = "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 865.48 MiB on device 0: cudaMalloc failed: out of memory" 15BUFFER = "alloc_tensor_range: failed to allocate CUDA0 buffer of size 907524736" 16ASSERT = "/data/split-pkg/llama.cpp/ggml/src/ggml-backend.cpp:188: GGML_ASSERT(buffer) failed" 17GDB = ["[New LWP 477206]" ,"❌️ Couldn't get registers: No such process." ,"Python Exception <class 'gdb.error'>: Couldn't get registers: No such process." ] 18MAC = ["WARNING: Using native backtrace. Set GGML_BACKTRACE_LLDB for more info." ,"3 strata-vision 0x0000000102a4c9b8 ggml_abort + 132" , 19"4 strata-vision 0x0000000102a2f1c0 ggml_backend_alloc_ctx_tensors_from_buft + 220" ] 20 21 22class Find (unittest .TestCase ): 23def test_the_three_lines_of_the_v100_launch (self ): 24room = reader_room .find ([ALLOC ,BUFFER ,ASSERT ]) 25self .assertEqual (room .device ,0 ) 26self .assertAlmostEqual (room .need_mib ,865.48 ,places = 2 ) 27 28def test_the_buffer_line_alone_gives_the_size_in_mib (self ): 29room = reader_room .find ([BUFFER ]) 30self .assertEqual (room .device ,0 ) 31self .assertAlmostEqual (room .need_mib ,865.48 ,places = 1 ) 32 33def test_amd_buffer_names (self ): 34self .assertEqual (reader_room .find (["failed to allocate ROCm2 buffer of size 1048576" ]).device ,2 ) 35 36def test_the_text_may_come_as_one_string (self ): 37self .assertEqual (reader_room .find ("\n" .join ([ALLOC ,ASSERT ])).device ,0 ) 38 39def test_a_plain_out_of_memory_without_numbers_is_still_found (self ): 40room = reader_room .find (["CUDA error: cudaMalloc failed: out of memory" ]) 41self .assertEqual ((room .device ,room .need_mib ), (None ,None )) 42 43def test_the_last_failed_allocation_is_the_one_reported (self ): 44later = "ggml_backend_cuda_buffer_type_alloc_buffer: allocating 12.00 MiB on device 1: cudaMalloc failed: out of memory" 45room = reader_room .find ([ALLOC ,later ]) 46self .assertEqual ((room .device ,room .need_mib ), (1 ,12.0 )) 47 48def test_lines_that_are_not_about_memory_are_not_found (self ): 49for line in ("ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device" ,# the harmless one: the reader runs on the processor then 50"CUDA error: device kernel image is invalid" ,"strata generate: CUDA error: out of memory" , 51"cudaMalloc failed: CUDA-capable device(s) is/are busy or unavailable" ,"" ): 52with self .subTest (line = line ): 53self .assertIsNone (reader_room .find ([line ])) 54self .assertIsNone (reader_room .find ([])) 55self .assertIsNone (reader_room .find (None )) 56 57def test_the_readers_card_number_is_looked_up_in_the_list_it_was_shown (self ): 58self .assertEqual (reader_room .physical_card (0 ,"2,3" ),2 ) 59self .assertEqual (reader_room .physical_card (1 , [2 ,3 ]),3 ) 60self .assertEqual (reader_room .physical_card (1 ,"2,3" ),3 ) 61self .assertEqual (reader_room .physical_card (0 ,None ),0 )# it saw every card: its number is the card's 62self .assertEqual (reader_room .physical_card (0 ,"" ),0 ) 63self .assertIsNone (reader_room .physical_card (5 ,"2,3" )) 64self .assertIsNone (reader_room .physical_card (0 ,"GPU-8c3f" ))# a UUID: not a number we can print 65self .assertIsNone (reader_room .physical_card (None ,"0" )) 66 67 68class Need (unittest .TestCase ): 69def test_the_whole_need_at_a_limit (self ): 70self .assertEqual (reader_room .reader_needs_mib (1024 ),1536 ) 71self .assertEqual (reader_room .reader_needs_mib (None ),1536 ) 72self .assertEqual (reader_room .reader_needs_mib (300 ),1536 )# below the default limit it is not smaller: the warm-up picture is the same 73self .assertAlmostEqual (reader_room .reader_needs_mib (16384 ),3674 ) 74self .assertAlmostEqual (reader_room .reader_needs_mib (0 ),3674 )# no limit: the largest the Terminal offers 75self .assertAlmostEqual (reader_room .reader_needs_mib (99999 ),3674 ) 76self .assertTrue (1536 < reader_room .reader_needs_mib (4096 )< 3674 ) 77 78def test_what_the_person_is_told_is_the_larger_of_the_failed_allocation_and_that (self ): 79self .assertEqual (reader_room .whole_need_mib (865.48 ,1024 ),1536 ) 80self .assertEqual (reader_room .whole_need_mib (None ,1024 ),1536 ) 81self .assertEqual (reader_room .whole_need_mib (2600 ,1024 ),2600 ) 82self .assertAlmostEqual (reader_room .whole_need_mib (865.48 ,16384 ),3674 ) 83 84def test_in_the_sentence (self ): 85said ,_ = reader_room .could_not_start (0 ,reader_room .whole_need_mib (865.48 ),600 ) 86self .assertEqual (said ,"The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB; 0.6 GB are free)." ) 87 88 89class Words (unittest .TestCase ): 90"""The owner's wording (11-10), exactly.""" 91 92def test_the_summary_line_when_the_processor_reads (self ): 93self .assertEqual (reader_room .on_processor (0 ,865.48 ,114 ), 94"Pictures are read on the processor, which is slower: graphics card 0 has no room for the picture reader " 95"(it needs about 0.8 GB; 0.1 GB are free). To read them on the card, close the other programs that use it and start again." ) 96 97def test_the_error_when_the_launch_ends (self ): 98said ,nxt = reader_room .could_not_start (0 ,865.48 ,114 ) 99self .assertEqual (said ,"The picture reader could not start: graphics card 0 has no room for it (it needs about 0.8 GB; 0.1 GB are free)." ) 100self .assertEqual (nxt ,"Close the other programs that use the card and start again, or add --vision off to start without pictures." ) 101 102def test_what_is_not_known_is_left_out (self ): 103self .assertEqual (reader_room .could_not_start (2 ,865.48 ,None )[0 ],"The picture reader could not start: graphics card 2 has no room for it (it needs about 0.8 GB)." ) 104self .assertEqual (reader_room .could_not_start (2 ,None ,3000 )[0 ],"The picture reader could not start: graphics card 2 has no room for it (2.9 GB are free)." ) 105self .assertEqual (reader_room .could_not_start (None ,None ,None )[0 ],"The picture reader could not start: the graphics card has no room for it." ) 106self .assertEqual (reader_room .could_not_start (None ,865.48 ,114 )[0 ], 107"The picture reader could not start: the graphics card has no room for it (it needs about 0.8 GB; 0.1 GB are free)." ) 108self .assertIn ("the graphics card has no room for the picture reader." ,reader_room .on_processor (None )) 109 110def test_the_summary_line_for_any_other_reason (self ): 111self .assertEqual (reader_room .not_started_on_processor (0 ,"/home/u/engine.log" ), 112"Pictures are read on the processor, which is slower: the picture reader could not start on graphics card 0. " 113"The reason is in the engine's log: /home/u/engine.log." ) 114self .assertEqual (reader_room .not_started_on_processor (None ,"/home/u/engine.log" ), 115"Pictures are read on the processor, which is slower: the picture reader could not start. The reason is in the engine's log: /home/u/engine.log." ) 116self .assertEqual (reader_room .not_started_on_processor (2 ,None ), 117"Pictures are read on the processor, which is slower: the picture reader could not start on graphics card 2." ) 118self .assertEqual (reader_room .not_started_on_processor (),"Pictures are read on the processor, which is slower: the picture reader could not start." ) 119 120def test_a_card_with_next_to_nothing_free_is_not_called_a_tenth (self ): 121self .assertIn ("(less than 0.1 GB are free)" ,reader_room .on_processor (0 ,None ,10 )) 122self .assertIn ("(less than 0.1 GB are free)" ,reader_room .on_processor (0 ,None ,0 )) 123 124 125class LaunchSaysIt (unittest .TestCase ): 126"""explain_engine_exit on the engine log a full card leaves, on every platform's version of it.""" 127 128def setUp (self ): 129self .free = [114 ] 130patcher = mock .patch .object (split_engine ,"probe_cards" ,lambda indexes = None : [split_engine .Card (0 ,"Tesla" ,16384 ,60 ,free_mib = self .free [0 ])]if self .free else []) 131patcher .start () 132self .addCleanup (patcher .stop ) 133 134def log (self ,lines ,cfg = None ): 135folder = tempfile .TemporaryDirectory () 136self .addCleanup (folder .cleanup ) 137log = Path (folder .name )/ "engine.log" 138log .write_text ("\n" .join (lines )+ "\n" ,encoding = "utf-8" ) 139path = Path (folder .name )/ "run.json" 140path .write_text (json .dumps ({"log" :str (log ),** (cfg or {})}),encoding = "utf-8" ) 141return path ,log 142 143def explain (self ,lines ,cfg = None ,code = 1 ): 144path ,log = self .log (lines ,cfg ) 145text ,nxt = R .explain_engine_exit ({"state" :"failed" ,"error" :"server_exited" ,"engine_exit_code" :code },path ) 146return text ,nxt ,log 147 148NEW2 = "The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB; 0.1 GB are free)." # 865 MiB failed; the reader needs 1,536 in all 149STEP = "Close the other programs that use the card and start again, or add --vision off to start without pictures." 150 151def test_linux_with_gdb_as_seen_on_the_v100_box (self ): 152text ,nxt ,log = self .explain (["loading the model" ]+ [ALLOC ,BUFFER ,ASSERT ]+ GDB ) 153self .assertEqual (text ,self .NEW2 ) 154self .assertTrue (nxt .startswith (self .STEP ),nxt ) 155self .assertIn (str (log ),nxt ) 156self .assertNotIn ("gdb" ,text + nxt ) 157self .assertNotIn ("Exception" ,text + nxt ) 158 159def test_linux_without_a_debugger_the_backtrace_frames_follow (self ): 160frames = ["/opt/strata/strata-vision(ggml_abort+0xd0) [0x55d7c4a1f0d0]" ,"/opt/strata/strata-vision(+0x3f1a2) [0x55d7c4a3f1a2]" ] 161text ,nxt ,_ = self .explain ([ALLOC ,BUFFER ,ASSERT ]+ frames ) 162self .assertEqual (text ,self .NEW2 ) 163self .assertTrue (nxt .startswith (self .STEP )) 164 165def test_macos_style_frames_and_warnings (self ): 166text ,nxt ,_ = self .explain ([ALLOC ,BUFFER ,ASSERT ]+ MAC ) 167self .assertEqual (text ,self .NEW2 ) 168 169def test_windows_the_assert_is_the_last_line_and_the_exit_code_is_3 (self ): 170text ,nxt ,_ = self .explain ([ALLOC ,BUFFER ,ASSERT ],code = 3 ) 171self .assertEqual (text ,self .NEW2 ) 172self .assertNotIn ("GGML_ASSERT" ,text + nxt ) 173 174def test_the_free_memory_is_left_out_when_it_cannot_be_read (self ): 175self .free .clear () 176text ,_ ,_ = self .explain ([ALLOC ,BUFFER ,ASSERT ]+ GDB ) 177self .assertEqual (text ,"The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB)." ) 178 179def test_the_card_is_the_one_in_the_config_in_the_engines_order (self ): 180text ,_ ,_ = self .explain ([ALLOC .replace ("device 0" ,"device 1" ),ASSERT ],cfg = {"gpu" : [2 ,3 ],"gpu_order" :"as_given" }) 181self .assertIn ("graphics card 3 has no room" ,text ) 182 183def test_a_reader_on_its_own_card_names_that_card (self ): 184text ,_ ,_ = self .explain ([ALLOC ,ASSERT ],cfg = {"gpu" : [0 ,1 ],"vision" : {"cuda_device" :1 }}) 185self .assertIn ("graphics card 1 has no room" ,text ) 186 187def test_without_a_number_it_says_the_card (self ): 188for cfg in (None , {"gpu" : [2 ,3 ],"gpu_order" :"as_given" }): 189with self .subTest (cfg = cfg ): 190text ,_ ,_ = self .explain (["CUDA error: cudaMalloc failed: out of memory" ,"GGML_ASSERT failed" ],cfg = cfg ) 191self .assertEqual (text ,"The picture reader could not start: the graphics card has no room for it (it needs about 1.5 GB)." )# no figure in the line: the reader's known need 192 193def test_the_need_is_the_readers_whole_need_not_the_one_allocation_that_failed (self ): 194self .free [0 ]= 600 195text ,_ ,_ = self .explain ([ALLOC ,BUFFER ,ASSERT ]+ GDB )# 865 MiB failed, 600 MiB free 196self .assertEqual (text ,"The picture reader could not start: graphics card 0 has no room for it (it needs about 1.5 GB; 0.6 GB are free)." ) 197 198def test_the_need_grows_with_the_readers_image_limit (self ): 199for limit ,said in ((None ,"1.5 GB" ), (1024 ,"1.5 GB" ), (4096 ,"1.9 GB" ), (16384 ,"3.6 GB" ), (0 ,"3.6 GB" )): 200with self .subTest (limit = limit ): 201cfg = {"vision" : {"max_tokens" :limit }}if limit is not None else None 202text ,_ ,_ = self .explain ([ALLOC ,BUFFER ,ASSERT ],cfg = cfg ) 203self .assertIn ("(it needs about " + said + ";" ,text ) 204 205def test_a_bigger_failed_allocation_is_the_need (self ): 206text ,_ ,_ = self .explain ([ALLOC .replace ("865.48 MiB" ,"2600.00 MiB" ),ASSERT ]) 207self .assertIn ("(it needs about 2.5 GB;" ,text ) 208 209def test_the_engines_own_out_of_memory_later_in_the_log_wins (self ): 210text ,nxt ,_ = self .explain ([ALLOC ,BUFFER ,"strata serve: the GPU has too little free VRAM for the prompt path: turn images off (setup: --vision no)" ]) 211self .assertIn ("too little free VRAM" ,text ) 212self .assertEqual (nxt .split (" The engine's log" )[0 ],R .VRAM_STOP_NEXT ) 213 214def test_other_reader_trouble_is_not_called_a_full_card (self ): 215text ,nxt ,_ = self .explain (["ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device" ]) 216self .assertNotIn ("no room" ,text ) 217self .assertIn ("The engine stopped while it was loading the model" ,text ) 218 219 220if __name__ == "__main__" : 221unittest .main ()