okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
abd9b4581
main
1"""serve/server.py start_engine_or_fallback: a config for the all-cards split engine starts the normal layer split, with one plain line, 2when the split engine refuses or fails to come up; any other config starts exactly as before and its failures stay failures.""" 3from __future__import annotations 4 5import contextlib 6import io 7import os 8import sys 9import unittest 10from pathlib import Path 11from unittest import mock 12 13sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 14import serve .server as S # noqa: E402 15 16 17NORMAL = os .path .abspath ("/e/strata" ) 18 19 20class FakeEngine : 21started = [] 22fail_exes = set () 23 24def __init__ (self ,exe ,args ,cwd = None ,log = None ,env = None ,lazy = False ,** kw ): 25FakeEngine .started .append ((exe ,list (args ),cwd ,dict (env or {}))) 26if exe in FakeEngine .fail_exes : 27raise S .EngineDied ("the engine exited before it was ready: card 0 is compute capability 8.9 and this build is for sm_86" ) 28self .info = {} 29 30 31def split_cfg (): 32return {"exe" :"/e/strata-split-2" ,"args" : ["--native" ,"/m/a.gguf" ,"--batch" ,"6" ],"cwd" :"/x" ,"tokenizer" :"/t" ,"port" :8080 , 33"env" : {"FNX_ARENA" :"0" ,"FNX_EXPERT_RAM_GIB" :"off" }, 34"split_fallback" : {"exe" :NORMAL ,"args" : ["--pack" ,"/p" ,"--native" ,"/m/a.gguf" ,"--layer-split" ,"auto" ], 35"env" : {"STRATA_X" :"1" },"layer_split" :"auto" }} 36 37 38class Fallback (unittest .TestCase ): 39def setUp (self ): 40FakeEngine .started ,FakeEngine .fail_exes = [],set () 41self .p = [mock .patch .object (S ,"StrataEngine" ,FakeEngine ),mock .patch .object (S ,"effort_end_args" ,lambda cfg ,exe ,tok :None ), 42mock .patch .object (S ,"configure_batch_telemetry_env" ,lambda env ,cfg ,override :None )] 43for p in self .p : 44p .start () 45 46def tearDown (self ): 47for p in self .p : 48p .stop () 49 50def run_start (self ,cfg ): 51out = io .StringIO () 52with contextlib .redirect_stdout (out ): 53engine = S .start_engine_or_fallback (cfg ,cfg ["exe" ], [], {"A" :"1" },False ,None ) 54return engine ,out .getvalue () 55 56def test_a_split_engine_that_starts_is_used (self ): 57engine ,out = self .run_start (split_cfg ()) 58self .assertEqual (len (FakeEngine .started ),1 ) 59self .assertEqual (FakeEngine .started [0 ][0 ],"/e/strata-split-2" ) 60self .assertNotIn ("split_fallback" ,engine .info ) 61self .assertEqual (out ,"" ) 62 63def test_a_split_engine_that_fails_is_replaced_by_the_normal_layer_split_with_one_line (self ): 64FakeEngine .fail_exes = {"/e/strata-split-2" } 65engine ,out = self .run_start (split_cfg ()) 66self .assertEqual ([s [0 ]for s in FakeEngine .started ], ["/e/strata-split-2" ,NORMAL ]) 67exe ,args ,cwd ,env = FakeEngine .started [1 ] 68self .assertIn ("--layer-split" ,args ) 69self .assertNotIn ("--batch" ,args ) 70self .assertEqual (env .get ("STRATA_X" ),"1" ) 71self .assertNotIn ("FNX_ARENA" ,env ) 72self .assertTrue (engine .info .get ("split_fallback" )) 73lines = [ln for ln in out .splitlines ()if ln .strip ()] 74self .assertEqual (len (lines ),1 ) 75self .assertIn ("all-cards split engine could not start" ,lines [0 ]) 76self .assertIn ("normal layer split" ,lines [0 ]) 77 78def test_the_engines_own_reason_is_in_the_line_and_its_log_is_kept (self ): 79import tempfile 80log = os .path .join (tempfile .mkdtemp (),"engine.log" ) 81Path (log ).write_text ("sfnx: failed: the all-cards split engine does not read this model's routed experts (blk.0.ffn_gate_exps.weight is ggml type 14); use the normal layer split\n" ) 82cfg = split_cfg () 83cfg ["log" ]= log 84 85class Refusing (FakeEngine ): 86def __init__ (self ,exe ,args ,cwd = None ,log = None ,env = None ,lazy = False ,** kw ): 87FakeEngine .started .append ((exe ,list (args ),cwd ,dict (env or {}))) 88if exe == "/e/strata-split-2" : 89raise S .EngineDied ("the engine exited before it was ready (see x)\n" + Path (log ).read_text ()) 90self .info = {} 91with mock .patch .object (S ,"StrataEngine" ,Refusing ): 92engine ,out = self .run_start (cfg ) 93lines = [ln for ln in out .splitlines ()if ln .strip ()] 94self .assertEqual (len (lines ),1 ) 95self .assertIn ("does not read this model's routed experts" ,lines [0 ]) 96self .assertNotIn ("exited before it was ready" ,lines [0 ]) 97self .assertTrue (os .path .exists (log + ".split-engine" ))# the normal engine will overwrite the log; the refusal stays readable 98self .assertEqual ([s [0 ]for s in FakeEngine .started ], ["/e/strata-split-2" ,NORMAL ]) 99 100def test_a_split_config_keeps_its_arguments_through_the_drafter_step (self ): 101"""strata_control/split_engine.split_config writes "drafting": "mtp" (what the split engine runs), so the drafter step, which resolves Auto to 102DFlash2 where the engine has it, adds no DFlash2 flag to the split engine's arguments (it would refuse them and every start would fall back).""" 103from strata_control import split_engine as SE 104 105base = {"exe" :NORMAL ,"args" : ["--pack" ,"/p" ,"--native" ,"/m/a.gguf" ,"--mtp" ,"/d/mtp/rt" ,"--layer-split" ,"auto" ],"cwd" :"/x" , 106"tokenizer" :"/t" ,"port" :8080 } 107decision = SE .Decision (True , [],2 ) 108cfg = SE .split_config (base ,decision ,exe = "/e/strata-split-2" ,native = "/m/a.gguf" ,mtp_dir = "/d/mtp/rt" ,draft_vocab = "/d/v.bin" ,context = 131072 ) 109args = S .engine_args (cfg ) 110self .assertEqual (args [:2 ], ["--native" ,"/m/a.gguf" ]) 111self .assertNotIn ("--draft" ,args ) 112self .assertNotIn ("--draft-model" ,args ) 113self .assertEqual (args [args .index ("--mtp" )+ 1 ],"/d/mtp/rt" ) 114self .assertEqual (args [args .index ("--spec" )+ 1 ],"6" ) 115self .assertNotIn ("--layer-split" ,args ) 116# the layer split that starts instead keeps the person's drafting word, or none (Auto) 117self .assertNotIn ("drafting" ,cfg ["split_fallback" ]) 118cfg = SE .split_config ({** base ,"drafting" :"auto" },decision ,exe = "/e/strata-split-2" ,native = "/m/a.gguf" ,mtp_dir = "/d/mtp/rt" , 119draft_vocab = "/d/v.bin" ,context = 131072 ) 120self .assertEqual (cfg ["split_fallback" ]["drafting" ],"auto" ) 121self .assertEqual (cfg ["drafting" ],"mtp" ) 122 123def test_without_a_fallback_the_failure_stays_a_failure (self ): 124cfg = split_cfg () 125del cfg ["split_fallback" ] 126FakeEngine .fail_exes = {"/e/strata-split-2" } 127with self .assertRaises (S .EngineDied ): 128self .run_start (cfg ) 129self .assertEqual (len (FakeEngine .started ),1 ) 130 131def test_a_normal_config_is_untouched (self ): 132cfg = {"exe" :"/e/strata" ,"args" : ["--pack" ,"/p" ],"cwd" :"/x" ,"tokenizer" :"/t" } 133FakeEngine .fail_exes = {"/e/strata" } 134with self .assertRaises (S .EngineDied ): 135self .run_start (cfg ) 136self .assertEqual (len (FakeEngine .started ),1 ) 137 138 139if __name__ == "__main__" : 140unittest .main ()