okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
1"""strata_control/split_engine.py (when the all-cards split engine may run) and src/split (no peer access in any path).""" 2 3from __future__import annotations 4 5import re 6import unittest 7from pathlib import Path 8 9from strata_control import split_engine as S 10 11ROOT = Path (__file__ ).resolve ().parents [2 ] 12GIB = 1 << 30 13 14A6000 = dict (name = "NVIDIA RTX A6000" ,vram_mib = 49140 ,cc = 86 ) 15 16 17def cards (n = 2 ,** kw ): 18spec = {** A6000 ,** kw } 19return [S .Card (index = i ,** spec )for i in range (n )] 20 21 22def udq4 (** kw ): 23"""UD-Q4_K_XL as measured: 71.7 GiB of routed experts, Q8_0 embeddings, a 27.5 GiB embedding table in RAM.""" 24f = dict (routed_types = {"Q4_K" :97 ,"Q5_K" :2 ,"Q5_1" :43 ,"Q8_0" :5 },routed_bytes = int (71.7 * GIB ),embd_type = "Q8_0" , 25shexp_types = {"Q8_0" :144 },embd_ram_bytes = int (27.5 * GIB ),layers = 48 ,experts = 512 ) 26f .update (kw ) 27return S .ModelFacts (** f ) 28 29 30def decide (** kw ): 31a = dict (enabled = True ,system = "linux" ,cards = cards (),ram_total_gib = 125 ,ram_available_gib = 80 ,model = udq4 ()) 32a .update (kw ) 33return S .decide (** a ) 34 35 36class Rule (unittest .TestCase ): 37def test_runs_on_the_tested_setup (self ): 38d = decide () 39self .assertTrue (d .use_split ,d .reasons ) 40self .assertEqual (d .cards ,2 ) 41self .assertEqual (d .untested , []) 42self .assertIn ("2 cards" ,d .line ) 43 44def test_off_unless_asked (self ): 45d = decide (enabled = False ) 46self .assertFalse (d .use_split ) 47self .assertIn ("opt-in" ,d .line ) 48 49def test_linux_nvidia_only (self ): 50for system in ("windows" ,"darwin" ): 51self .assertFalse (decide (system = system ).use_split ) 52 53def test_card_count_and_sameness (self ): 54self .assertFalse (decide (cards = cards (1 )).use_split ) 55self .assertFalse (decide (cards = cards (5 )).use_split ) 56mixed = cards (1 )+ [S .Card (index = 1 ,name = "NVIDIA RTX A5000" ,vram_mib = 24564 ,cc = 86 )] 57d = decide (cards = mixed ) 58self .assertFalse (d .use_split ) 59self .assertIn ("same model" ,d .line ) 60 61def test_untested_counts_archs_and_contexts_stay_off_unless_a_tester_allows_them (self ): 62for c in (cards (3 ),cards (2 ,cc = 89 ),cards (2 ,cc = 90 )): 63d = decide (cards = c ) 64self .assertFalse (d .use_split ) 65self .assertIn ("only been run" ,d .line ) 66d = decide (cards = cards (3 ,vram_mib = 49140 ),allow_untested = True ) 67self .assertTrue (d .use_split ,d .reasons ) 68self .assertIn ("3 cards" ,d .untested ) 69self .assertIn ("not tested" ,d .line ) 70self .assertFalse (decide (context = 262144 ).use_split ) 71self .assertTrue (decide (context = 262144 ,allow_untested = True ).use_split ) 72 73def test_all_the_experts_must_fit_in_the_cards (self ): 74d = decide (cards = cards (2 ,vram_mib = 24564 ,name = "NVIDIA RTX A5000" )) 75self .assertFalse (d .use_split ) 76self .assertIn ("do not fit" ,d .line ) 77self .assertFalse (decide (cards = cards (2 ,vram_mib = 40960 )).use_split )# 71.7 / 2 + 9 = 44.9 GiB needed 78self .assertTrue (decide (cards = cards (2 ,vram_mib = 46000 )).use_split ) 79 80def test_ram_that_stays_must_be_free (self ): 81self .assertTrue (decide (ram_available_gib = 52 ).use_split )# 27.5 + 3 + 4 + 8 spare = 42.5 82d = decide (ram_available_gib = 30 ) 83self .assertFalse (d .use_split ) 84self .assertIn ("RAM" ,d .line ) 85 86def test_only_the_quant_types_it_reads (self ): 87for model in (udq4 (layers = 64 ),udq4 (routed_types = {}), 88udq4 (routed_types = {"Q6_K" :48 }),# UD-Q6_K_XL-like experts: no kernel 89udq4 (embd_type = "IQ4_XS" ),# a K-quant set is not converted at load 90udq4 (shexp_types = {"BF16" :48 ,"IQ4_NL" :47 }), 91udq4 (output_type = "Q6_K" ), 92udq4 (ple_type = "Q6_K" )): 93d = decide (model = model ) 94self .assertFalse (d .use_split ,model ) 95self .assertTrue (d .line .startswith ("Using the normal layer split" )) 96 97def test_the_iq3_s_set_and_ud_iq4_xs_run (self ): 98iq3s = S .ModelFacts (routed_types = {"IQ2_S" :40 ,"IQ3_XXS" :34 ,"IQ3_S" :20 ,"IQ4_XS" :2 ,"IQ4_NL" :39 ,"Q2_0" :9 }, 99routed_bytes = int (49 * GIB ),embd_type = "IQ4_XS" ,shexp_types = {"IQ4_XS" :60 ,"Q4_K" :40 ,"Q5_K" :20 ,"IQ4_NL" :24 }, 100embd_ram_bytes = int (27.2 * GIB ),layers = 48 ,experts = 512 ,ple_type = "IQ4_NL" ,output_type = "Q6_K" ) 101d = decide (model = iq3s ) 102self .assertTrue (d .use_split ,d .reasons ) 103ud_iq4_xs = S .ModelFacts (routed_types = {"IQ3_S" :94 ,"IQ4_XS" :2 ,"IQ4_NL" :43 ,"Q8_0" :5 },routed_bytes = int (59.5e9 ), 104embd_type = "Q8_0" ,shexp_types = {"Q8_0" :144 },embd_ram_bytes = int (27.5 * GIB ),layers = 48 ,experts = 512 , 105ple_type = "IQ4_NL" ,output_type = "Q6_K" ) 106d = decide (model = ud_iq4_xs ) 107self .assertTrue (d .use_split ,d .reasons ) 108self .assertTrue (d .need_vram_gib < 48 ,d .need_vram_gib ) 109 110def test_a_type_the_loader_cannot_convert_is_a_plain_refusal (self ): 111for model ,word in ((udq4 (routed_types = {"IQ3_S" :94 },embd_type = "Q3_K" ),"cannot convert" ), 112 (udq4 (routed_types = {"IQ3_S" :94 },shexp_types = {"Q3_K" :5 }),"cannot read" ), 113 (udq4 (routed_types = {"IQ3_S" :94 },ple_type = "Q6_K" ),"per-layer" ), 114 (udq4 (routed_types = {"IQ3_S" :94 ,"TQ2_0" :2 }),"TQ2_0" )): 115d = decide (model = model ) 116self .assertFalse (d .use_split ) 117self .assertIn (word ,d .line ) 118self .assertNotIn (chr (10 ),d .line )# one plain line 119 120def test_scan_counts_the_q2_0_repack_twice (self ): 121from unittest import mock 122 123class T : 124def __init__ (self ,name ,type_name ,shape ,nbytes ): 125self .name ,self .type_name ,self .shape ,self ._n = name ,type_name ,shape ,nbytes 126def expected_bytes (self ): 127return self ._n 128 129class F : 130def __init__ (self ,path ): 131self .tensors = [T ("blk.0.ffn_down_exps.weight" ,"Q2_0" , (640 ,2560 ,512 ),1000 ),T ("blk.1.ffn_gate_exps.weight" ,"IQ3_S" , (2560 ,640 ,512 ),5000 ), 132T ("token_embd.weight" ,"IQ4_XS" , (2560 ,248320 ),10 ),T ("per_layer_token_embd.weight" ,"IQ4_NL" , (1 ,2 ),20 ), 133T ("output.weight" ,"Q6_K" , (2560 ,248320 ),30 )] 134with mock .patch ("tools.gguf_reader.GGUFFile" ,F ): 135m = S .scan_model ("/x/model-00001-of-00001.gguf" ) 136self .assertEqual (m .routed_bytes ,2 * 1000 + 5000 ) 137self .assertEqual ((m .embd_type ,m .ple_type ,m .output_type ), ("IQ4_XS" ,"IQ4_NL" ,"Q6_K" )) 138self .assertEqual (m .embd_ram_bytes ,30 ) 139 140def test_the_reasons_are_all_reported (self ): 141d = decide (system = "windows" ,cards = cards (1 ),model = udq4 (embd_type = "Q6_K" )) 142self .assertGreaterEqual (len (d .reasons ),3 ) 143 144 145class Config (unittest .TestCase ): 146def base (self ): 147return {"exe" :"engine/strata" ,"args" : ["--pack" ,"p" ,"--native" ,"m.gguf" ,"--layer-split" ,"auto" ],"cwd" :"/x" , 148"tokenizer" :"tok" ,"port" :8080 ,"model_name" :"m" ,"log" :"l.log" ,"gpu" : [0 ,1 ],"env" : {"STRATA_X" :"1" }, 149"layer_split" :"auto" } 150 151def test_split_config_keeps_the_front_end_and_the_fallback (self ): 152d = decide () 153cfg = S .split_config (self .base (),d ,exe = "/e/strata-split-2" ,native = "/m/shard1.gguf" ,mtp_dir = "/d/mtp/rt" , 154draft_vocab = "/d/draft_vocab_en.bin" ,context = 131072 ,lanes = 6 ,vision = True ) 155self .assertEqual (cfg ["exe" ],"/e/strata-split-2" ) 156self .assertEqual (cfg ["tokenizer" ],"tok" ) 157self .assertEqual (cfg ["port" ],8080 ) 158self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 159self .assertIn ("--vision" ,cfg ["args" ]) 160self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--batch" )+ 1 ],"6" ) 161self .assertEqual (cfg ["env" ]["FNX_ARENA" ],"0" ) 162self .assertEqual (cfg ["env" ]["FNX_EXPERT_RAM_GIB" ],"off" ) 163self .assertEqual (cfg ["env" ]["STRATA_X" ],"1" ) 164self .assertNotIn ("layer_split" ,cfg ) 165fb = cfg ["split_fallback" ] 166self .assertEqual (fb ["exe" ],"engine/strata" ) 167self .assertEqual (fb ["args" ],self .base ()["args" ]) 168self .assertEqual (fb ["layer_split" ],"auto" ) 169self .assertEqual (self .base ()["args" ], ["--pack" ,"p" ,"--native" ,"m.gguf" ,"--layer-split" ,"auto" ])# the base is not changed 170 171def test_no_config_for_a_refusal (self ): 172with self .assertRaises (ValueError ): 173S .split_config (self .base (),decide (enabled = False ),exe = "e" ,native = "n" ,mtp_dir = "m" ,draft_vocab = "v" ,context = 1 ) 174 175 176class Apply (unittest .TestCase ): 177"""apply_to_config: the launcher's side (facts of this PC and model in, a run config out).""" 178 179def cfg (self ,tmp ): 180 (Path (tmp )/ "data" ).mkdir (exist_ok = True ) 181 (Path (tmp )/ "data" / "draft_vocab_en.bin" ).write_bytes (b"x" ) 182return {"exe" :"component:strata-engine" ,"args" : ["--pack" ,"p" ,"--native" ,"/m/a-00001-of-00004.gguf" ,"--mtp" ,"/d/mtp/rt" , 183"--max-context" ,"131072" ,"--layer-split" ,"auto" ], 184"cwd" :tmp ,"tokenizer" :"tok" ,"port" :8080 ,"gpu" : [0 ,1 ],"env" : {},"split_engine" :True } 185 186def run_apply (self ,cfg ,* ,cards_ = None ,avail = 80.0 ,model = None ,exe = "/e/strata-split-2" ,** kw ): 187return S .apply_to_config (cfg ,exe_for_cards = lambda n ,cc = None :exe ,system = kw .pop ("system" ,"linux" ),probe = lambda idx :cards_ or cards (), 188ram = lambda : (125.0 ,avail ),scan = lambda path :model or udq4 (),** kw ) 189 190def test_a_good_pc_gets_the_split_config (self ): 191import tempfile 192 193with tempfile .TemporaryDirectory ()as tmp : 194cfg = self .cfg (tmp ) 195d = self .run_apply (cfg ) 196self .assertTrue (d .use_split ,d .reasons ) 197self .assertEqual (cfg ["exe" ],"/e/strata-split-2" ) 198self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--native" )+ 1 ],"/m/a-00001-of-00004.gguf" ) 199self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--max-context" )+ 1 ],"131072" ) 200self .assertEqual (cfg ["split_fallback" ]["exe" ],"component:strata-engine" ) 201self .assertEqual (cfg ["tokenizer" ],"tok" ) 202 203def test_every_refusal_leaves_the_config_alone (self ): 204import copy 205import tempfile 206 207with tempfile .TemporaryDirectory ()as tmp : 208for kw in (dict (cards_ = cards (1 )),dict (avail = 20.0 ),dict (exe = None ),dict (system = "windows" ), 209dict (model = udq4 (embd_type = "Q6_K" ))): 210cfg = self .cfg (tmp ) 211before = copy .deepcopy (cfg ) 212d = self .run_apply (cfg ,** kw ) 213self .assertFalse (d .use_split ,kw ) 214self .assertEqual (cfg ,before ,kw ) 215self .assertTrue (d .line .startswith ("Using the normal layer split" ),d .line ) 216 217def test_a_model_file_it_cannot_read_is_a_reason_not_an_error (self ): 218import tempfile 219 220def boom (path ): 221raise OSError ("gone" ) 222 223with tempfile .TemporaryDirectory ()as tmp : 224cfg = self .cfg (tmp ) 225d = S .apply_to_config (cfg ,exe_for_cards = lambda n ,cc = None :"/e/x" ,system = "linux" ,probe = lambda i :cards (),ram = lambda : (125.0 ,80.0 ),scan = boom ) 226self .assertFalse (d .use_split ) 227self .assertIn ("could not be read" ,d .line ) 228 229def test_modes_auto_is_the_default_and_off_is_the_switch (self ): 230self .assertEqual (S .mode ({}, {}),"auto" ) 231for raw ,want in ((True ,"on" ), (False ,"off" ), ("on" ,"on" ), ("off" ,"off" ), ("auto" ,"auto" ), ("OFF" ,"off" ), ("nonsense" ,"auto" ), (None ,"auto" )): 232self .assertEqual (S .mode ({"split_engine" :raw }, {}),want ,raw ) 233self .assertEqual (S .mode ({"split_engine" :"on" }, {"STRATA_SPLIT_ENGINE" :"0" }),"off" )# the environment wins (testers) 234self .assertEqual (S .mode ({"split_engine" :"off" }, {"STRATA_SPLIT_ENGINE" :"1" }),"on" ) 235self .assertEqual (S .mode ({}, {"STRATA_SPLIT_ENGINE" :"off" }),"off" ) 236self .assertEqual (S .mode ({"split_engine" :"off" }, {"STRATA_SPLIT_ENGINE" :"" }),"off" )# an empty variable is no variable 237self .assertTrue (S .wanted ({}, {})) 238self .assertFalse (S .wanted ({"split_engine" :"off" }, {})) 239 240def test_auto_uses_the_split_where_it_is_supported_and_says_nothing_otherwise (self ): 241import copy 242import tempfile 243 244with tempfile .TemporaryDirectory ()as tmp : 245cfg = self .cfg (tmp ) 246del cfg ["split_engine" ]# nothing asked: auto 247d = self .run_apply (cfg ) 248self .assertTrue (d .use_split ,d .reasons ) 249self .assertFalse (d .quiet ) 250self .assertEqual (cfg ["exe" ],"/e/strata-split-2" ) 251for kw in (dict (cards_ = cards (1 )),dict (avail = 20.0 ),dict (exe = None ),dict (system = "windows" ),dict (model = udq4 (routed_types = {"Q6_K" :48 })), 252dict (cards_ = cards (3 )),dict (cards_ = cards (2 ,cc = 89 ))): 253cfg = self .cfg (tmp ) 254del cfg ["split_engine" ] 255before = copy .deepcopy (cfg ) 256d = self .run_apply (cfg ,** kw ) 257self .assertFalse (d .use_split ,kw ) 258self .assertTrue (d .quiet ,kw )# silent: the launch goes on with the layer split 259self .assertEqual (cfg ,before ,kw ) 260 261def test_on_says_why_it_cannot_run (self ): 262import tempfile 263 264with tempfile .TemporaryDirectory ()as tmp : 265cfg = self .cfg (tmp ) 266cfg ["split_engine" ]= "on" 267d = self .run_apply (cfg ,cards_ = cards (1 )) 268self .assertFalse (d .use_split ) 269self .assertFalse (d .quiet ) 270self .assertIn ("two or more cards" ,d .line ) 271 272def test_off_never_runs_and_changes_nothing (self ): 273import copy 274import tempfile 275 276with tempfile .TemporaryDirectory ()as tmp : 277for off in ("off" ,False ): 278cfg = self .cfg (tmp ) 279cfg ["split_engine" ]= off 280before = copy .deepcopy (cfg ) 281d = self .run_apply (cfg ) 282self .assertFalse (d .use_split ) 283self .assertTrue (d .quiet ) 284self .assertEqual (cfg ,before ) 285 286def test_choices_the_split_engine_does_not_honour_keep_the_layer_split (self ): 287import tempfile 288 289with tempfile .TemporaryDirectory ()as tmp : 290for change in (lambda c :c .update (drafting = "dflash2" ),lambda c :c .update (user_args = ["--x" ]),lambda c :c .update (replicas = 2 ), 291lambda c :c ["args" ].extend (["--kv" ,"q4_0" ])): 292cfg = self .cfg (tmp ) 293del cfg ["split_engine" ] 294change (cfg ) 295d = self .run_apply (cfg ) 296self .assertFalse (d .use_split ) 297self .assertTrue (d .quiet ) 298cfg = self .cfg (tmp ) 299cfg ["args" ].extend (["--kv" ,"int8" ]) 300self .assertTrue (self .run_apply (cfg ).use_split ) 301 302def df2_files (self ,tmp ,* ,taking = True ,pack = True ): 303"""A split engine program that carries (or lacks) the text of `--draft dflash2`, and a data folder with (or without) the DFlash2 pack.""" 304from strata_control import drafting 305 306exe = Path (tmp )/ "strata-split-2" 307exe .write_bytes (b"\0--draft dflash2 needs --native, --draft-model PACK and --draft-vocab FILE\0" if taking else b"\0--native and --mtp are required\0" ) 308data = Path (tmp )/ "models" 309if pack : 310target = drafting .target_path (data ) 311target .parent .mkdir (parents = True ,exist_ok = True ) 312with open (target ,"wb" )as f : 313f .truncate ((1 << 20 )+ 1 ) 314return str (exe ),data 315 316def test_dflash2_chosen_by_name_runs_on_the_split_engine_only_with_the_persons_own_on (self ): 317"""Owner 2026-11-03: the person's own choice wins. DFlash2 named + --split-engine on = the split engine with `--draft dflash2 --draft-model PACK` (no MTP layer); 318Auto, or DFlash2 named and the split engine on Auto, are as they were (Auto the MTP split; DFlash2 the layer split).""" 319import tempfile 320 321with tempfile .TemporaryDirectory ()as tmp : 322exe ,data = self .df2_files (tmp ) 323cfg = self .cfg (tmp ) 324cfg ["drafting" ]= "dflash2" 325cfg ["args" ]= [a for a in cfg ["args" ]if a != "--mtp" and a != "/d/mtp/rt" ]# no draft layer on this computer at all 326before = dict (cfg ,args = list (cfg ["args" ])) 327d = self .run_apply (cfg ,exe = exe ,data_dir = str (data ),mode_ = "on" ) 328self .assertTrue (d .use_split ,d .reasons ) 329a = cfg ["args" ] 330self .assertEqual ((a [a .index ("--draft" )+ 1 ],a [a .index ("--draft-model" )+ 1 ]), ("dflash2" ,str (data / "dflash2" / "qwen3.8-flash-next-dflash2.q4_0.pack" ))) 331self .assertNotIn ("--mtp" ,a ) 332self .assertEqual (a [a .index ("--draft-vocab" )+ 1 ],str (Path (tmp )/ "data" / "draft_vocab_en.bin" )) 333self .assertEqual (a [a .index ("--batch" )+ 1 ],"6" ) 334self .assertEqual (cfg ["drafting" ],"dflash2" ) 335self .assertEqual (cfg ["split_fallback" ]["drafting" ],"dflash2" ,"the layer split that starts if this engine refuses keeps the person's word" ) 336self .assertEqual (cfg ["split_fallback" ]["args" ],before ["args" ]) 337# the same choice with the split engine on Auto (or nothing) stays on the layer split, quietly 338for mode in ("auto" ,None ): 339cfg = self .cfg (tmp ) 340cfg ["drafting" ]= "dflash2" 341del cfg ["split_engine" ] 342if mode : 343cfg ["split_engine" ]= mode 344d = self .run_apply (cfg ,exe = exe ,data_dir = str (data )) 345self .assertFalse (d .use_split ,mode ) 346self .assertTrue (d .quiet ,mode ) 347self .assertNotIn ("split_fallback" ,cfg ) 348# Auto and the MTP on the split engine "on" are the MTP split as before 349for chosen in (None ,"auto" ,"mtp" ): 350cfg = self .cfg (tmp ) 351if chosen : 352cfg ["drafting" ]= chosen 353d = self .run_apply (cfg ,exe = exe ,data_dir = str (data )) 354self .assertTrue (d .use_split ,chosen ) 355self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--mtp" )+ 1 ],"/d/mtp/rt" ,chosen ) 356self .assertNotIn ("--draft" ,cfg ["args" ],chosen ) 357self .assertEqual (cfg ["drafting" ],"mtp" ,chosen ) 358 359def test_dflash2_on_the_split_needs_an_engine_that_takes_it_and_its_pack (self ): 360import tempfile 361 362with tempfile .TemporaryDirectory ()as tmp : 363for i , (kw ,why )in enumerate ((({"taking" :False },"does not run" ), ({"pack" :False },S .NO_DF2_PACK ))): 364 (Path (tmp )/ f"case { i } " ).mkdir () 365exe ,data = self .df2_files (Path (tmp )/ f"case { i } " ,** kw ) 366cfg = self .cfg (tmp ) 367cfg ["drafting" ]= "dflash2" 368before = dict (cfg ,args = list (cfg ["args" ])) 369d = self .run_apply (cfg ,exe = exe ,data_dir = str (data ),mode_ = "on" ) 370self .assertFalse (d .use_split ,kw ) 371self .assertIn (why ,d .reasons [0 ],kw ) 372self .assertEqual (cfg ,before ,"a refusal leaves the config alone: the layer split starts with DFlash2" ) 373self .assertFalse (S .exe_takes_dflash2 (None )) 374self .assertFalse (S .exe_takes_dflash2 ("/no/such/strata-split-2" )) 375 376def test_expected_follows_the_same_rule (self ): 377for chosen ,mode ,want in (("dflash2" ,"on" ,True ), ("dflash2" ,"auto" ,False ), ("dflash2" ,None ,False ), ("mtp" ,"on" ,True ), ("auto" ,"on" ,True ), ("off" ,"on" ,False )): 378cfg = {"drafting" :chosen ,"split_engine" :mode } 379got = S .expected (cfg ,indexes = [0 ,1 ],native = None ,probe = lambda idx :cards (),ram = lambda : (125.0 ,80.0 ),system = "linux" ) 380self .assertEqual (got ,want , (chosen ,mode )) 381 382def test_drafting_auto_resolves_to_the_split_with_its_mtp_and_the_fallback_keeps_the_layer_split_choice (self ): 383import tempfile 384 385with tempfile .TemporaryDirectory ()as tmp : 386for chosen ,split in ((None ,True ), ("auto" ,True ), ("mtp" ,True ), ("dflash2" ,False ), ("off" ,False )): 387cfg = self .cfg (tmp ) 388del cfg ["split_engine" ] 389if chosen : 390cfg ["drafting" ]= chosen 391before_fallback = {k :v for k ,v in cfg .items ()if k == "drafting" } 392d = self .run_apply (cfg ) 393self .assertEqual (d .use_split ,split ,chosen ) 394if split : 395# Auto/MTP: the split engine runs its MTP; the config says so, so serve/server.py's drafter step (Auto = DFlash2) leaves its arguments alone 396self .assertEqual (cfg ["drafting" ],"mtp" ,chosen ) 397self .assertEqual (cfg ["split_fallback" ].get ("drafting" ),before_fallback .get ("drafting" ),chosen )# the layer split keeps the person's word, or Auto 398self .assertNotIn ("--draft" ,cfg ["args" ]) 399else : 400self .assertEqual (cfg .get ("drafting" ),chosen )# untouched: the layer split gets DFlash2 as chosen 401self .assertNotIn ("split_fallback" ,cfg ) 402 403def test_auto_needs_a_card_where_the_split_decodes_faster_than_the_layer_split_with_dflash2 (self ): 404import tempfile 405 406with tempfile .TemporaryDirectory ()as tmp : 407cfg = self .cfg (tmp ) 408del cfg ["split_engine" ] 409d = self .run_apply (cfg ,cards_ = cards (2 ,cc = 89 ),allow_untested = True )# a tester allowed it, but nobody measured it there 410self .assertFalse (d .use_split ) 411self .assertIn ("DFlash2" ,d .line ) 412cfg ["drafting" ]= "mtp" # the person asked for the MTP: no comparison to win 413self .assertTrue (self .run_apply (cfg ,cards_ = cards (2 ,cc = 89 ),allow_untested = True ).use_split ) 414 415def test_the_engine_is_picked_by_card_count_and_architecture (self ): 416import tempfile 417 418seen = [] 419 420def exe_for (n ,cc = None ): 421seen .append ((n ,cc )) 422return "/e/strata-split-2-sm120" 423 424with tempfile .TemporaryDirectory ()as tmp : 425cfg = self .cfg (tmp ) 426d = S .apply_to_config (cfg ,exe_for_cards = exe_for ,system = "linux" ,probe = lambda i :cards (2 ,cc = 120 ,name = "NVIDIA RTX PRO 6000" ,vram_mib = 97887 ), 427ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 428self .assertTrue (d .use_split ,d .reasons ) 429self .assertEqual (seen , [(2 ,120 )]) 430self .assertEqual (cfg ["exe" ],"/e/strata-split-2-sm120" ) 431 432 433class AmdDefault (unittest .TestCase ): 434"""AMD gfx1201 (R9700): the split is the default on two or more cards (owner, 11-02), `off` turns it off, `on` says why when it cannot run; 435one constant (AUTO_SPLIT_AMD_ARCHS) says which AMD architectures start it by themselves, every other one keeps the layer split silently.""" 436 437R9700 = dict (name = "AMD Radeon AI PRO R9700" ,vram_mib = 32624 ,cc = 0 ,vendor = "amd" ,arch = "gfx1201" ) 438cfg = Apply .cfg 439 440def amd (self ,tmp ,** kw ): 441cfg = self .cfg (tmp ) 442cfg ["gpu" ]= [0 ,1 ,2 ,3 ] 443for k ,v in kw .items (): 444cfg [k ]= v 445return cfg 446 447def run_amd (self ,cfg ,seen = None ,** kw ): 448def exe_for (n ,cc = None ,arch = None ): 449if seen is not None : 450seen .append ((n ,cc ,arch )) 451return f"/e/strata-split- { n } - { arch } " if arch else None 452 453return S .apply_to_config (cfg ,exe_for_cards = exe_for ,system = "linux" ,probe = lambda i :cards (4 ,** self .R9700 ),ram = lambda : (125.0 ,80.0 ), 454scan = lambda path :udq4 (routed_bytes = int (71.7 * GIB )),** kw ) 455 456def test_auto_starts_the_split_on_gfx1201 (self ): 457import tempfile 458 459self .assertEqual (S .AUTO_SPLIT_AMD_ARCHS , ("gfx1201" ,)) 460with tempfile .TemporaryDirectory ()as tmp : 461cfg = self .amd (tmp ) 462del cfg ["split_engine" ] 463seen = [] 464d = self .run_amd (cfg ,seen ) 465self .assertTrue (d .use_split ,d .reasons ) 466self .assertEqual (seen , [(4 ,0 ,"gfx1201" )]) 467self .assertEqual (cfg ["exe" ],"/e/strata-split-4-gfx1201" ) 468 469def test_amd_default_lanes_are_eight_and_a_number_the_person_chose_stays (self ): 470import tempfile 471 472self .assertEqual ((S .AMD_DEFAULT_LANES ,S .DEFAULT_LANES ), (8 ,6 )) 473with tempfile .TemporaryDirectory ()as tmp : 474cfg = self .amd (tmp ) 475del cfg ["split_engine" ] 476self .assertTrue (self .run_amd (cfg ).use_split ) 477self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--batch" )+ 1 ],"8" ) 478cfg = self .amd (tmp ) 479del cfg ["split_engine" ] 480cfg ["parallel" ]= 3 481self .assertTrue (self .run_amd (cfg ).use_split ) 482self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--batch" )+ 1 ],"3" ) 483 484def test_auto_on_amd_starts_the_ud_q4_family_and_the_iq3s_set (self ): 485import tempfile 486 487iq3s = dict (routed_types = {"IQ4_NL" :39 ,"IQ3_XXS" :34 ,"Q2_0" :9 ,"IQ2_S" :40 ,"IQ3_S" :20 ,"IQ4_XS" :2 },routed_bytes = int (48.8 * GIB ),embd_type = "IQ4_XS" , 488shexp_types = {"Q4_K" :30 ,"Q8_0" :1 ,"BF16" :48 },ple_type = "IQ4_NL" ) 489with tempfile .TemporaryDirectory ()as tmp : 490cfg = self .amd (tmp ) 491del cfg ["split_engine" ] 492before = dict (cfg ) 493d = S .apply_to_config (cfg ,exe_for_cards = lambda n_ ,cc = None ,arch = None :"/e/strata-split-4-amd" ,system = "linux" ,probe = lambda i :cards (4 ,** self .R9700 ), 494ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 (** iq3s )) 495self .assertTrue (d .use_split ,d .reasons ) 496cfg = self .amd (tmp ,split_engine = "on" ) 497d = S .apply_to_config (cfg ,exe_for_cards = lambda n_ ,cc = None ,arch = None :"/e/strata-split-4-amd" ,system = "linux" ,probe = lambda i :cards (4 ,** self .R9700 ), 498ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 (** iq3s )) 499self .assertTrue (d .use_split ,d .reasons ) 500 501def test_auto_is_four_cards_only_today_and_on_runs_two_and_three (self ): 502import tempfile 503 504self .assertEqual (S .AUTO_SPLIT_AMD_CARD_COUNTS , (4 ,)) 505with tempfile .TemporaryDirectory ()as tmp : 506for n in (2 ,3 ): 507cfg = self .amd (tmp ) 508del cfg ["split_engine" ] 509before = dict (cfg ) 510d = S .apply_to_config (cfg ,exe_for_cards = lambda n_ ,cc = None ,arch = None :f"/e/strata-split- { n_ } -amd" ,system = "linux" , 511probe = lambda i :cards (n ,** self .R9700 ),ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 (routed_bytes = int (30 * GIB ))) 512self .assertFalse (d .use_split ) 513self .assertTrue (d .quiet ) 514self .assertEqual (cfg ,before ) 515cfg = self .amd (tmp ,split_engine = "on" ) 516d = S .apply_to_config (cfg ,exe_for_cards = lambda n_ ,cc = None ,arch = None :f"/e/strata-split- { n_ } -amd" ,system = "linux" , 517probe = lambda i :cards (n ,** self .R9700 ),ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 (routed_bytes = int (30 * GIB ))) 518self .assertTrue (d .use_split , (n ,d .reasons )) 519self .assertEqual (cfg ["exe" ],f"/e/strata-split- { n } -amd" ) 520 521def test_the_card_count_constant_limits_auto_but_not_on (self ): 522import tempfile 523 524old = S .AUTO_SPLIT_AMD_CARD_COUNTS 525S .AUTO_SPLIT_AMD_CARD_COUNTS = (4 ,) 526try : 527with tempfile .TemporaryDirectory ()as tmp : 528for n ,auto_runs in ((3 ,False ), (4 ,True )): 529cfg = self .amd (tmp ) 530del cfg ["split_engine" ] 531d = S .apply_to_config (cfg ,exe_for_cards = lambda n_ ,cc = None ,arch = None :f"/e/strata-split- { n_ } -amd" ,system = "linux" , 532probe = lambda i :cards (n ,** {** self .R9700 }),ram = lambda : (125.0 ,80.0 ), 533scan = lambda path :udq4 (routed_bytes = int (40 * GIB ))) 534self .assertEqual (d .use_split ,auto_runs , (n ,d .reasons )) 535if not auto_runs : 536self .assertTrue (d .quiet ) 537cfg = self .amd (tmp ,split_engine = "on" ) 538self .assertTrue (S .apply_to_config (cfg ,exe_for_cards = lambda n_ ,cc = None ,arch = None :"/e/x" ,system = "linux" ,probe = lambda i :cards (3 ,** self .R9700 ), 539ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 (routed_bytes = int (40 * GIB ))).use_split ) 540finally : 541S .AUTO_SPLIT_AMD_CARD_COUNTS = old 542 543def test_off_turns_it_off_by_every_route (self ): 544import os 545import tempfile 546 547for how in ("key" ,"preset" ,"env" ): 548with tempfile .TemporaryDirectory ()as tmp : 549cfg = self .amd (tmp ) 550del cfg ["split_engine" ] 551if how == "key" : 552cfg ["split_engine" ]= "off" 553elif how == "preset" : 554cfg ["split_engine" ]= False 555else : 556os .environ ["STRATA_SPLIT_ENGINE" ]= "0" 557before = dict (cfg ) 558try : 559d = self .run_amd (cfg ) 560finally : 561os .environ .pop ("STRATA_SPLIT_ENGINE" ,None ) 562self .assertFalse (d .use_split ) 563self .assertTrue (d .quiet ) 564self .assertEqual (cfg ,before )# the layer split's config, untouched 565 566def test_an_untested_amd_architecture_runs_only_on_an_explicit_on_and_says_it_is_untested (self ): 567import tempfile 568 569for arch in ("gfx1100" ,"gfx1101" ,"gfx1102" ,"gfx1103" ,"gfx1200" ): 570with tempfile .TemporaryDirectory ()as tmp : 571cfg = self .amd (tmp ,split_engine = "on" ) 572seen = [] 573 574def exe_for (n ,cc = None ,arch_ = None ): 575seen .append ((n ,arch_ )) 576return f"/e/strata-split- { n } -amd" 577 578d = S .apply_to_config (cfg ,exe_for_cards = exe_for ,system = "linux" ,probe = lambda i :cards (4 ,** {** self .R9700 ,"arch" :arch }), 579ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 580self .assertTrue (d .use_split , (arch ,d .reasons )) 581self .assertEqual (d .untested , [f"card architecture { arch } " ]) 582self .assertIn ("not tested" ,d .line ) 583self .assertEqual (seen , [(4 ,arch )]) 584self .assertEqual (cfg ["exe" ],"/e/strata-split-4-amd" ) 585# the same card in auto: the layer split, silently 586cfg = self .amd (tmp ) 587del cfg ["split_engine" ] 588before = dict (cfg ) 589d = S .apply_to_config (cfg ,exe_for_cards = exe_for ,system = "linux" ,probe = lambda i :cards (4 ,** {** self .R9700 ,"arch" :arch }), 590ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 591self .assertFalse (d .use_split ) 592self .assertTrue (d .quiet ) 593self .assertEqual (cfg ,before ) 594 595def test_architectures_without_a_code_object_are_refused_even_with_on (self ): 596import tempfile 597 598for arch in ("gfx1030" ,"gfx1151" ,"gfx90a" ,"gfx942" ):# RDNA2, Strix Halo, CDNA: not built 599with tempfile .TemporaryDirectory ()as tmp : 600cfg = self .amd (tmp ,split_engine = "on" ) 601d = S .apply_to_config (cfg ,exe_for_cards = lambda n ,cc = None ,arch_ = None :"/e/x" ,system = "linux" , 602probe = lambda i :cards (4 ,** {** self .R9700 ,"arch" :arch }),ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 603self .assertFalse (d .use_split ,arch ) 604 605def test_an_amd_architecture_not_in_the_list_keeps_the_layer_split_silently (self ): 606import tempfile 607 608for arch in ("gfx1030" ,"gfx1151" ): 609with tempfile .TemporaryDirectory ()as tmp : 610cfg = self .amd (tmp ) 611del cfg ["split_engine" ] 612before = dict (cfg ) 613d = S .apply_to_config (cfg ,exe_for_cards = lambda n ,cc = None ,arch = None :"/e/x" ,system = "linux" , 614probe = lambda i :cards (4 ,** {** self .R9700 ,"arch" :arch }),ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 615self .assertFalse (d .use_split ) 616self .assertTrue (d .quiet )# the can-run rule, not a refusal message 617self .assertEqual (cfg ,before ) 618 619def test_explicit_on_starts_the_split_with_the_gfx1201_engine (self ): 620import tempfile 621 622for on in (True ,"on" ): 623with tempfile .TemporaryDirectory ()as tmp : 624cfg = self .amd (tmp ,split_engine = on ) 625seen = [] 626d = self .run_amd (cfg ,seen ) 627self .assertTrue (d .use_split ,d .reasons ) 628self .assertEqual (d .cards ,4 ) 629self .assertEqual (d .untested , [])# four R9700 have been run 630self .assertEqual (seen , [(4 ,0 ,"gfx1201" )]) 631self .assertEqual (cfg ["exe" ],"/e/strata-split-4-gfx1201" ) 632self .assertEqual (cfg ["drafting" ],"mtp" ) 633self .assertEqual (cfg ["split_fallback" ]["exe" ],"component:strata-engine" ) 634 635def test_on_without_the_gfx1201_binary_says_so_and_off_is_off (self ): 636import tempfile 637 638with tempfile .TemporaryDirectory ()as tmp : 639cfg = self .amd (tmp ,split_engine = "on" ) 640d = S .apply_to_config (cfg ,exe_for_cards = lambda n ,cc = None ,arch = None :None ,system = "linux" ,probe = lambda i :cards (4 ,** self .R9700 ), 641ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 642self .assertFalse (d .use_split ) 643self .assertFalse (d .quiet ) 644self .assertIn ("strata-split-4-gfx1201" ,d .line ) 645cfg = self .amd (tmp ,split_engine = "off" ) 646before = dict (cfg ) 647self .assertFalse (self .run_amd (cfg ).use_split ) 648self .assertEqual (cfg ,before ) 649 650def test_two_r9700_cannot_hold_the_experts_and_other_amd_cards_are_refused (self ): 651import tempfile 652 653with tempfile .TemporaryDirectory ()as tmp : 654cfg = self .amd (tmp ,split_engine = "on" ) 655d = S .apply_to_config (cfg ,exe_for_cards = lambda n ,cc = None ,arch = None :"/e/x" ,system = "linux" ,probe = lambda i :cards (2 ,** self .R9700 ), 656ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 657self .assertFalse (d .use_split ) 658d = S .apply_to_config (self .amd (tmp ,split_engine = "on" ),exe_for_cards = lambda n ,cc = None ,arch = None :"/e/x" ,system = "linux" , 659probe = lambda i :cards (4 ,** {** self .R9700 ,"arch" :"gfx1030" }),ram = lambda : (125.0 ,80.0 ),scan = lambda path :udq4 ()) 660self .assertFalse (d .use_split ) 661self .assertIn ("gfx1030" ,d .line ) 662 663def test_taking_the_architecture_out_of_the_constant_makes_auto_keep_the_layer_split_but_on_still_runs (self ): 664import tempfile 665 666old = S .AUTO_SPLIT_AMD_ARCHS 667S .AUTO_SPLIT_AMD_ARCHS = () 668try : 669with tempfile .TemporaryDirectory ()as tmp : 670cfg = self .amd (tmp ) 671del cfg ["split_engine" ] 672d = self .run_amd (cfg ) 673self .assertFalse (d .use_split ) 674self .assertTrue (d .quiet ) 675self .assertTrue (self .run_amd (self .amd (tmp ,split_engine = "on" )).use_split ) 676finally : 677S .AUTO_SPLIT_AMD_ARCHS = old 678 679 680class HipArchSources (unittest .TestCase ): 681"""The AMD architecture rules that live in the engine sources (the launcher side is AmdDefault): pinned so a refactor cannot drop the guard.""" 682 683SRC = ROOT / "src" / "split" 684 685def text (self ,rel ): 686return (self .SRC / rel ).read_text (encoding = "utf-8" ) 687 688def test_the_self_test_runs_before_the_model_loads_on_a_card_nobody_has_run (self ): 689model = self .text ("src/model.cpp" ) 690i = model .index ("void check_cards()" ) 691j = model .index ("selftest_decide(" ,i ) 692self .assertLess (j ,model .index ("float f16_to_f32" ,i ))# inside check_cards, which Model::Model calls first 693self .assertIn ("FNX_SELFTEST_FAIL" ,self .text ("src/hip/selftest_policy.hpp" )) 694self .assertIn ("selftest.cu" ,self .text ("CMakeLists.txt" )) 695self .assertRegex (self .text ("src/hip/selftest_policy.hpp" ),r'arch_tested\([^)]*\)\s*\{\s*return gcn_arch\.rfind\("gfx1201", 0\) == 0;' ) 696 697def test_the_code_objects_and_the_card_check_agree (self ): 698cmake = self .text ("CMakeLists.txt" ) 699for arch in ("gfx1201" ,"gfx1200" ,"gfx11-generic" ): 700self .assertIn (arch ,cmake ) 701build = (ROOT / "packaging" / "build_split_hip.sh" ).read_text (encoding = "utf-8" ) 702self .assertIn ("gfx1201;gfx1200;gfx11-generic" ,build ) 703self .assertIn ("strata-split-$n-amd" ,build ) 704self .assertRegex (build ,r"NEEDED\.\*rccl" )# RCCL is dlopen'ed, never linked 705model = self .text ("src/model.cpp" ) 706for arch in ("gfx1201" ,"gfx1200" ,"gfx1100" ,"gfx1101" ,"gfx1102" ,"gfx1103" ): 707self .assertIn ('"%s"' % arch ,model ) 708 709def test_the_issue_template_exists_and_the_engine_line_points_at_it (self ): 710tpl = ROOT / ".github" / "ISSUE_TEMPLATE" / "amd_split_report.yml" 711self .assertTrue (tpl .is_file ()) 712self .assertIn ("AMD split engine report" ,tpl .read_text (encoding = "utf-8" )) 713self .assertIn ("template=amd_split_report.yml" ,self .text ("src/hip/selftest_policy.hpp" )) 714 715 716class NoPeerAccess (unittest .TestCase ): 717"""Pictures of the rule 'no peer access in any path' for the NVIDIA split: nothing under src/split may call the CUDA peer APIs, and the HIP peer APIs 718appear in exactly one file, src/split/src/hip/peer.h (AMD only: the owner allowed peer access on the R9700 "if faster, with fallback", 2026-11-02; it runs only 719after a start probe of every directed pair, and FNX_PEER=0 forces the host exchange). The built NVIDIA engine is also checked with nm in 720packaging/build_split_cuda.sh.""" 721 722FORBIDDEN = re .compile (r"(cudaDeviceEnablePeerAccess|cudaDeviceDisablePeerAccess|cuCtxEnablePeerAccess|cudaMemcpyPeer(?:Async)?|" 723r"cudaMemcpy3DPeer(?:Async)?|cuMemcpyPeer(?:Async)?|cuMemcpy3DPeer(?:Async)?)" ) 724FORBIDDEN_HIP = re .compile (r"(hipDeviceEnablePeerAccess|hipDeviceDisablePeerAccess|hipMemcpyPeer(?:Async)?|hipMemcpy3DPeer(?:Async)?|hipCtxEnablePeerAccess)" ) 725HIP_PEER_FILE = "src/split/src/hip/peer.h" 726 727def _code_lines (self ): 728for p in sorted ((ROOT / "src" / "split" ).rglob ("*" )): 729if p .suffix not in (".cu" ,".cuh" ,".cpp" ,".hpp" ,".h" ): 730continue 731for n ,line in enumerate (p .read_text (encoding = "utf-8" ,errors = "replace" ).splitlines (),1 ): 732yield p ,n ,line ,line .split ("//" ,1 )[0 ] 733 734def test_no_cuda_peer_call_in_the_engine_sources (self ): 735bad = [f" { p . relative_to ( ROOT ) } : { n } : { line . strip ()[: 100 ] } " for p ,n ,line ,code in self ._code_lines ()if self .FORBIDDEN .search (code )] 736self .assertEqual (bad , [],"CUDA peer access in the split engine:\n" + "\n" .join (bad )) 737 738def test_hip_peer_calls_only_in_the_one_probed_file (self ): 739bad = [f" { p . relative_to ( ROOT ). as_posix () } : { n } : { line . strip ()[: 100 ] } " for p ,n ,line ,code in self ._code_lines () 740if self .FORBIDDEN_HIP .search (code )and p .relative_to (ROOT ).as_posix ()!= self .HIP_PEER_FILE ] 741self .assertEqual (bad , [],"HIP peer access outside " + self .HIP_PEER_FILE + ":\n" + "\n" .join (bad )) 742 743def test_the_hip_peer_file_enables_access_but_never_uses_the_copy_engine (self ): 744text = (ROOT / self .HIP_PEER_FILE ).read_text (encoding = "utf-8" ) 745code = "\n" .join (l .split ("//" ,1 )[0 ]for l in text .splitlines ()) 746self .assertIn ("hipDeviceEnablePeerAccess" ,code ) 747self .assertNotRegex (code ,r"hipMemcpy(?:3D)?Peer" )# kernel stores only (the owner's condition), no peer copies 748 749def test_the_peer_file_is_only_compiled_into_the_hip_build_and_the_default_is_pinned (self ): 750engine = (ROOT / "src" / "split" / "src" / "engine.cu" ).read_text (encoding = "utf-8" ) 751i = engine .index ('#include "hip/peer.h"' ) 752self .assertEqual (engine [:i ].rstrip ().splitlines ()[- 1 ].strip (),"#if FNX_HIP" ) 753policy = (ROOT / "src" / "split" / "src" / "hip" / "peer_policy.hpp" ).read_text (encoding = "utf-8" ) 754self .assertRegex (policy ,r"constexpr bool PEER_DEFAULT_ON = (false|true);" )# flipped to true only on screens that show it wins 755 756def test_rccl_is_loaded_at_run_time_never_linked_and_its_default_is_pinned (self ): 757# the packaging rule (coordinator, 11-02): dlopen only, no link; the split must start without librccl, falling back with one line 758rd = (ROOT / "src" / "split" / "src" / "hip" / "rccl_dl.hpp" ).read_text (encoding = "utf-8" ) 759self .assertIn ("dlopen(" ,rd ) 760self .assertNotIn ("hip_runtime" ,rd )# CPU only: tools/rccl_policy_test.cpp forces a missing library without a card 761self .assertRegex (rd ,r"constexpr bool RCCL_DEFAULT_ON = (false|true);" ) 762cmake = (ROOT / "src" / "split" / "CMakeLists.txt" ).read_text (encoding = "utf-8" ) 763self .assertNotRegex (cmake .replace ("rccl_policy_test" ,"" ),r"(?i)rccl" )# no find_package(rccl), no -lrccl 764for p in (ROOT / "src" / "split" / "src" ).rglob ("*" ): 765if p .suffix in (".cu" ,".cuh" ,".cpp" ,".hpp" ,".h" )and p .name not in ("rccl.h" ,"rccl_dl.hpp" ,"prefill.cu" ,"engine.cu" ,"engine.hpp" ): 766self .assertNotIn ("rccl.h>" ,p .read_text (encoding = "utf-8" ,errors = "ignore" ),p .name ) 767 768def test_the_peer_probe_and_the_27b_files_are_gone (self ): 769names = {p .name for p in (ROOT / "src" / "split" / "src" ).iterdir ()} 770self .assertNotIn ("peer_probe.cu" ,names ) 771self .assertFalse ([n for n in names if n .startswith ("s27_" )]) 772 773def test_the_licence_and_the_credit_travel_with_the_code (self ): 774lic = (ROOT / "third_party" / "sfnx" / "LICENSE" ).read_text (encoding = "utf-8" ) 775self .assertIn ("John Fink" ,lic ) 776self .assertIn ("MIT License" ,lic ) 777notice = (ROOT / "third_party" / "sfnx" / "NOTICE.md" ).read_text (encoding = "utf-8" ) 778self .assertIn ("John Fink" ,notice ) 779self .assertIn ("github.com/johnfink8/sfnx" ,notice ) 780 781 782class LaunchManifest (unittest .TestCase ): 783"""P0.1 (lite): STRATA_MANIFEST=<file> records the engine choice and why in <file>.launch.json. Off unless set; the same launch gives the same text; 784the AMD auto rule is recorded condition by condition, so a two- or three-card setup is not read as the four-card one.""" 785 786R9700 = dict (name = "AMD Radeon AI PRO R9700" ,vram_mib = 32624 ,cc = 0 ,vendor = "amd" ,arch = "gfx1201" ) 787IQ3S = dict (routed_types = {"IQ2_S" :40 ,"IQ3_XXS" :34 ,"IQ3_S" :20 ,"IQ4_XS" :2 ,"IQ4_NL" :39 ,"Q2_0" :9 },routed_bytes = int (49 * GIB ),embd_type = "IQ4_XS" , 788shexp_types = {"IQ4_XS" :60 ,"Q4_K" :40 ,"Q5_K" :20 ,"IQ4_NL" :24 },embd_ram_bytes = int (27.2 * GIB ),layers = 48 ,experts = 512 , 789ple_type = "IQ4_NL" ,output_type = "Q6_K" ) 790 791def decided (self ,n ,model = None ,** card ): 792spec = dict (self .R9700 ) 793spec .update (card ) 794return S .decide (enabled = True ,system = "linux" ,cards = cards (n ,** spec ),ram_total_gib = 125 ,ram_available_gib = 80 , 795model = model or S .ModelFacts (** self .IQ3S ),allow_untested = True ) 796 797def test_off_unless_the_variable_is_set (self ): 798import tempfile 799 800d = self .decided (4 ) 801with tempfile .TemporaryDirectory ()as tmp : 802self .assertFalse (S .record_launch_manifest (d ,"auto" ,environ = {})) 803self .assertFalse (S .record_launch_manifest (d ,"auto" ,environ = {"STRATA_MANIFEST" :"" })) 804self .assertEqual (list (Path (tmp ).iterdir ()), []) 805 806def test_four_gfx1201_with_the_iq3_s_set_is_recognised (self ): 807import json 808import tempfile 809 810d = self .decided (4 ) 811self .assertTrue (d .use_split ,d .reasons ) 812with tempfile .TemporaryDirectory ()as tmp : 813target = str (Path (tmp )/ "m.json" ) 814self .assertTrue (S .record_launch_manifest (d ,"auto" ,environ = {"STRATA_MANIFEST" :target })) 815doc = json .loads (Path (target + ".launch.json" ).read_text (encoding = "utf-8" )) 816launch = doc ["launch" ] 817self .assertEqual ((launch ["engine" ],launch ["cards" ],launch ["card_arch" ]), ("all-cards-split" ,4 ,"gfx1201" )) 818self .assertEqual (launch ["amd_auto_rule" ], {"architecture" :True ,"card_count" :True ,"expert_types" :True }) 819self .assertIn ("IQ3_S" ,launch ["routed_types" ]) 820 821def test_two_and_three_cards_are_not_promoted_by_the_four_card_rule (self ): 822for n in (2 ,3 ): 823rule = S .auto_rule (self .decided (n )) 824self .assertEqual (rule , {"architecture" :True ,"card_count" :False ,"expert_types" :True },n ) 825 826def test_another_expert_type_or_an_nvidia_card_misses_a_condition (self ): 827ud = S .ModelFacts (** {** self .IQ3S ,"routed_types" : {"IQ3_S" :90 ,"Q3_K" :4 }}) 828d = self .decided (4 ,model = ud ) 829self .assertFalse (d .use_split )# the loader reads no Q3_K experts: a plain refusal, with the reason recorded 830self .assertTrue (any ("Q3_K" in r for r in d .reasons ),d .reasons ) 831self .assertEqual (S .auto_rule (d ), {"architecture" :True ,"card_count" :True ,"expert_types" :False }) 832nv = S .decide (enabled = True ,system = "linux" ,cards = cards (2 ),ram_total_gib = 125 ,ram_available_gib = 80 ,model = udq4 ()) 833self .assertIsNone (S .auto_rule (nv )) 834 835def test_matched_launches_give_the_same_text (self ): 836import tempfile 837 838with tempfile .TemporaryDirectory ()as tmp : 839texts = [] 840for name in ("a" ,"b" ): 841target = str (Path (tmp )/ name ) 842S .record_launch_manifest (self .decided (4 ),"auto" ,environ = {"STRATA_MANIFEST" :target }) 843texts .append (Path (target + ".launch.json" ).read_text (encoding = "utf-8" )) 844self .assertEqual (texts [0 ],texts [1 ]) 845self .assertFalse (list (Path (tmp ).glob ("*.tmp" ))) 846 847def test_an_unwritable_target_never_stops_a_start (self ): 848self .assertFalse (S .record_launch_manifest (self .decided (4 ),"auto" ,environ = {"STRATA_MANIFEST" :"/no/such/dir/m.json" })) 849 850 851if __name__ == "__main__" : 852unittest .main ()