okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c65b9f8be
main
1"""GUI-FB3 item 2(b): an Auto start that picked DFlash2 and ended before the engine was ready is started once more with the MTP, in one quiet line. A start the person 2named DFlash2 for fails with the engine's own reason. A real process and a real pipe (a fake engine program), like serve/test_fatal_recovery.py.""" 3import os 4import sys 5import tempfile 6import unittest 7from pathlib import Path 8from unittest import mock 9 10from serve .server import EngineDied ,StrataEngine 11from strata_control import drafting 12 13FAKE = r''' 14import sys 15from pathlib import Path 16args = sys.argv[1:] 17Path(args[args.index('--starts') + 1]).open('a').write(' '.join(args) + '\n') 18if '--draft' in args and '--die-on-draft' in args: 19sys.stderr.write('strata generate: --draft dflash2 drafts without the MTP layer and needs --draft-model PACK, --spec 2 or more and --mtp DIR (for DIR/draft_vocab.bin)\n') 20sys.stderr.flush() 21raise SystemExit(2) 22if '--die-short' in args and ('--draft' in args or '--mtp' in args): 23sys.stderr.write('ggml_cuda_init: failed to initialize CUDA: no CUDA-capable device is detected'+chr(10)) 24sys.stderr.write('strata generate: layer split, CUDA1 expert cache: no room'+chr(10)) 25sys.stderr.flush() 26raise SystemExit(1) 27if '--die-always' in args: 28sys.stderr.write('strata generate: something else is wrong\n') 29sys.stderr.flush() 30raise SystemExit(2) 31print('READY 4096 stop', flush=True) 32for line in sys.stdin: 33if line.startswith('QUIT'): 34break 35''' 36 37 38class AutoRetriesWithTheMtp (unittest .TestCase ): 39def setUp (self ): 40self .tmp = tempfile .TemporaryDirectory () 41self .addCleanup (self .tmp .cleanup ) 42self .root = Path (self .tmp .name ) 43py = self .root / "native.py" 44if os .name == "nt" : 45py .write_text (FAKE ) 46self .script = self .root / "native.cmd" 47self .script .write_text (f'@" { sys . executable } " " { py } " %*\r\n' ) 48else : 49self .script = py 50py .write_text ("#!" + sys .executable + "\n" + FAKE ) 51py .chmod (0o700 ) 52self .starts = self .root / "starts.txt" 53self .log = str (self .root / "engine.log" ) 54self .first = ["--pack" ,"/d/packs/g" ,"--starts" ,str (self .starts ),"--die-on-draft" ,"--draft" ,"dflash2" ,"--draft-model" ,"/p" ,"--draft-fallback" ,"none" , 55"--mtp-draft-vocab" ,"/d/mtp/rt/draft_vocab.bin" ] 56self .mtp = ["--pack" ,"/d/packs/g" ,"--starts" ,str (self .starts ),"--die-on-draft" ,"--mtp" ,"/d/mtp/rt" ,"--spec" ,"4" ] 57drafting .take_note () 58self .addCleanup (drafting .take_note ) 59 60def started (self ): 61return self .starts .read_text ().splitlines ()if self .starts .exists ()else [] 62 63def test_auto_that_picked_dflash2_starts_again_with_the_mtp_and_says_so_quietly (self ): 64engine = StrataEngine (str (self .script ),self .first ,log = self .log ,retry_args = self .mtp ) 65self .addCleanup (engine .close ) 66self .assertTrue (engine .alive ()) 67starts = self .started () 68self .assertEqual (len (starts ),2 ,"one start with DFlash2, one more with the MTP, never a third" ) 69self .assertIn ("--draft dflash2" ,starts [0 ]) 70self .assertNotIn ("--draft" ,starts [1 ]) 71self .assertEqual (engine .spawn [1 ],self .mtp ,"a later restart of this engine uses the arguments that worked" ) 72self .assertEqual (engine .info ["drafting" ],"mtp" ) 73self .assertEqual (engine .info ["drafting_note" ],"Drafting: MTP (DFlash2 didn't start)" ,"the one quiet line the Models screen shows" ) 74text = Path (self .log ).read_text (encoding = "utf-8" ) 75self .assertIn ("starting it again with the MTP" ,text ) 76self .assertIn ("needs --draft-model PACK" ,text ,"the engine's own reason stays in the log" ) 77 78def test_a_start_with_no_retry_arguments_fails_with_the_engines_reason (self ): 79"""Named by the person (or nothing to retry with): a real error that says why.""" 80with self .assertRaises (EngineDied )as caught : 81StrataEngine (str (self .script ),self .first ,log = self .log ) 82self .assertIn ("exited before it was ready" ,str (caught .exception )) 83self .assertIn ("needs --draft-model PACK" ,str (caught .exception )) 84self .assertEqual (len (self .started ()),1 ) 85 86def test_the_retry_is_one_start_only (self ): 87both = [* self .first ,"--die-always" ] 88retry = [* self .mtp ,"--die-always" ] 89with self .assertRaises (EngineDied )as caught : 90StrataEngine (str (self .script ),both ,log = self .log ,retry_args = retry ) 91self .assertIn ("something else is wrong" ,str (caught .exception ),"the second start's reason, not the first one's" ) 92self .assertEqual (len (self .started ()),2 ) 93 94def test_a_start_that_works_is_not_started_twice (self ): 95ok = ["--pack" ,"/d/packs/g" ,"--starts" ,str (self .starts )] 96engine = StrataEngine (str (self .script ),ok ,log = self .log ,retry_args = self .mtp ) 97self .addCleanup (engine .close ) 98self .assertEqual (len (self .started ()),1 ) 99self .assertEqual (engine .info .get ("drafting_note" ,"" ),"" ) 100 101def test_a_start_that_ran_out_of_memory_is_repeated_with_no_drafter_never_with_the_mtp (self ): 102"""2 small cards on a layer split: the engine says a card has no room. The old retry swapped DFlash2 for the MTP, which takes MORE memory from a card that is short.""" 103first = [a for a in self .first if a != "--die-on-draft" ]+ ["--die-short" ] 104mtp = [a for a in self .mtp if a != "--die-on-draft" ]+ ["--die-short" ] 105nodraft = ["--pack" ,"/d/packs/g" ,"--starts" ,str (self .starts ),"--die-short" ] 106engine = StrataEngine (str (self .script ),first ,log = self .log ,retry_args = mtp ,retry_vram_args = nodraft ) 107self .addCleanup (engine .close ) 108self .assertTrue (engine .alive ()) 109starts = self .started () 110self .assertEqual (len (starts ),2 ,"one start with DFlash2, one without a drafter, never the MTP in between" ) 111self .assertIn ("--draft dflash2" ,starts [0 ]) 112self .assertNotIn ("--mtp" ,starts [1 ]) 113self .assertNotIn ("--draft" ,starts [1 ]) 114self .assertEqual (engine .spawn [1 ],nodraft ) 115self .assertEqual (engine .info ["drafting" ],"off" ) 116self .assertEqual (engine .info ["drafting_note" ],"Drafting: off (the cards were short of memory)" ) 117text = Path (self .log ).read_text (encoding = "utf-8" ) 118self .assertIn ("short of memory" ,text ) 119self .assertIn ("expert cache: no room" ,text ,"the engine's own reason stays in the log" ) 120 121def test_a_memory_stop_without_a_no_drafter_retry_fails_with_the_engines_reason_and_does_not_try_the_mtp (self ): 122first = [a for a in self .first if a != "--die-on-draft" ]+ ["--die-short" ] 123with self .assertRaises (EngineDied )as caught : 124StrataEngine (str (self .script ),first ,log = self .log ,retry_args = [* self .mtp ,"--die-short" ]) 125self .assertIn ("expert cache: no room" ,str (caught .exception )) 126self .assertEqual (len (self .started ()),1 ) 127 128def test_the_same_arguments_are_not_a_retry (self ): 129with self .assertRaises (EngineDied ): 130StrataEngine (str (self .script ),self .first ,log = self .log ,retry_args = list (self .first )) 131self .assertEqual (len (self .started ()),1 ) 132 133 134if __name__ == "__main__" : 135unittest .main ()