okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
049ae1c67
main
1"""strata_control/intel_spec.py: --spec 2 for a run config on exactly two Arc B-series (Battlemage) cards. 2 3The rule changes only the generated default (--spec 4). Single cards, Alchemist cards, three cards, a config with no draft 4layer and anything the person set stay as they are. Also checks the two places that apply it: sycl/setup_intel.py's 5config rewrite (through apply_to_config) and the Intel server's engine_args wrapper.""" 6import importlib .util 7import json 8import sys 9from pathlib import Path 10 11ROOT = Path (__file__ ).resolve ().parent .parent .parent 12sys .path .insert (0 ,str (ROOT )) 13from strata_control import intel_spec # noqa: E402 14 15B65 = {"driver" :"xe" ,"devid" :"e222" } 16B70 = {"driver" :"xe" ,"devid" :"e223" } 17B50 = {"driver" :"xe" ,"devid" :"e212" } 18B580 = {"driver" :"xe" ,"devid" :"e20b" } 19A770 = {"driver" :"xe" ,"devid" :"56a0" } 20A750 = {"driver" :"i915" ,"devid" :"56a1" } 21IGPU = {"driver" :"xe" ,"devid" :"64a0" } 22ARGS = ["--pack" ,"/p" ,"--native" ,"/m.gguf" ,"--prefill" ,"auto" ,"--spec" ,"4" ,"--spec-min-p" ,"0.5" ,"--mtp" ,"/m/rt" , 23"--max-context" ,"32768" ,"--kv" ,"int8" ] 24 25 26def spec_of (args ): 27return args [args .index ("--spec" )+ 1 ] 28 29 30def test_battlemage_by_pci_id (): 31for c in (B65 ,B70 ,B50 ,B580 , {"driver" :"xe" ,"devid" :"0xE223" }, {"driver" :"xe" ,"devid" :"E20C" }): 32assert intel_spec .is_battlemage (c ["driver" ],c ["devid" ]),c 33assert not intel_spec .is_battlemage ("xe" ,"56a0" )# an A770 on xe is Alchemist 34assert not intel_spec .is_battlemage ("i915" ,"56a1" )# the A750 35assert not intel_spec .is_battlemage ("i915" ,"e223" )# the driver decides too 36assert not intel_spec .is_battlemage ("xe" ,"64a0" )# an integrated Xe2 part is not a B-series card 37assert not intel_spec .is_battlemage ("xe" ,"" ) 38assert not intel_spec .is_battlemage ("xe" ,"zzzz" ) 39 40 41def test_two_b_series_cards_get_spec_2 (): 42for pair in ([B65 ,B65 ], [B70 ,B70 ], [B65 ,B50 ], [B580 ,B580 ]): 43args ,note = intel_spec .plan (ARGS ,None ,pair ) 44assert spec_of (args )== "2" and note ,pair 45assert spec_of (ARGS )== "4" # the input list is not changed 46# nothing else moved 47args ,_ = intel_spec .plan (ARGS ,None , [B65 ,B65 ]) 48assert [a for i ,a in enumerate (args )if args [i - 1 :i ]!= ["--spec" ]]== [a for i ,a in enumerate (ARGS )if ARGS [i - 1 :i ]!= ["--spec" ]] 49 50 51def test_everything_else_stays (): 52for cards in ([], [B65 ], [A750 ], [A770 ], [A750 ,A750 ], [A770 ,A770 ], [B65 ,A770 ], [A750 ,B65 ], [B65 ,IGPU ], 53 [B65 ,B65 ,B65 ], [B65 ,B65 ,B65 ,B65 ]): 54args ,note = intel_spec .plan (ARGS ,None ,cards ) 55assert args == ARGS and note == "" ,cards 56 57 58def test_the_persons_own_spec_wins (): 59# in user_args, whatever its value 60for val in ("4" ,"3" ,"2" ,"6" ): 61args ,note = intel_spec .plan (ARGS , ["--spec" ,val ], [B65 ,B65 ]) 62assert args == ARGS and note == "" ,val 63# unrelated user options do not stop the rule 64args ,note = intel_spec .plan (ARGS , ["--vram-reserve-mib" ,"800" ], [B65 ,B65 ]) 65assert spec_of (args )== "2" and note 66# a value in args that is not setup's own 4 was chosen by somebody 67for val in ("3" ,"2" ,"5" ,"8" ): 68mine = list (ARGS ) 69mine [mine .index ("--spec" )+ 1 ]= val 70args ,note = intel_spec .plan (mine ,None , [B65 ,B65 ]) 71assert args == mine and note == "" ,val 72# no --spec at all: nothing is added 73no_spec = [a for a in ARGS if a not in ("--spec" ,"4" )] 74args ,note = intel_spec .plan (no_spec ,None , [B65 ,B65 ]) 75assert args == no_spec and note == "" 76 77 78def test_no_draft_layer_no_change (): 79no_mtp = ARGS [:ARGS .index ("--mtp" )]+ ARGS [ARGS .index ("--mtp" )+ 2 :] 80assert intel_spec .plan (no_mtp ,None , [B65 ,B65 ])== (no_mtp ,"" ) 81assert intel_spec .plan (ARGS ,None , [B65 ,B65 ],mtp_off = True )== (ARGS ,"" ) 82 83 84def test_apply_to_config_keeps_the_written_copy_in_step (): 85cfg = {"args" :list (ARGS ),"args_written" :list (ARGS ),"env" : {}} 86note = intel_spec .apply_to_config (cfg , [B65 ,B65 ]) 87assert note and spec_of (cfg ["args" ])== "2" and spec_of (cfg ["args_written" ])== "2" 88# a second call (setup run again) changes nothing and says nothing 89assert intel_spec .apply_to_config (cfg , [B65 ,B65 ])== "" 90# a config of the person's: user_args with --spec, "mtp": false, one card 91own = {"args" :list (ARGS ),"args_written" :list (ARGS ),"user_args" : ["--spec" ,"4" ]} 92assert intel_spec .apply_to_config (own , [B65 ,B65 ])== "" and spec_of (own ["args" ])== "4" 93off = {"args" :list (ARGS ),"mtp" :False } 94assert intel_spec .apply_to_config (off , [B65 ,B65 ])== "" and spec_of (off ["args" ])== "4" 95one = {"args" :list (ARGS )} 96assert intel_spec .apply_to_config (one , [B65 ])== "" and spec_of (one ["args" ])== "4" 97 98 99def test_sysfs_cards_in_card_order (tmp_path ): 100def card (n ,vendor ,devid ,driver ): 101d = tmp_path / f"card { n } " / "device" 102d .mkdir (parents = True ) 103 (d / "vendor" ).write_text (vendor + "\n" ) 104 (d / "device" ).write_text (devid + "\n" ) 105drv = tmp_path / "drivers" / driver 106drv .mkdir (parents = True ,exist_ok = True ) 107try : 108 (d / "driver" ).symlink_to (drv ,target_is_directory = True ) 109except OSError :# no symlinks (Windows without rights): nothing to read 110return False 111return True 112ok = card (0 ,"0x8086" ,"0xe222" ,"xe" ) 113card (1 ,"0x8086" ,"0xe222" ,"xe" ) 114card (2 ,"0x10de" ,"0x2d05" ,"nvidia" ) 115 (tmp_path / "card0-DP-1" ).mkdir () 116if not ok : 117return 118cards = intel_spec .sysfs_cards (tmp_path ) 119assert [c ["devid" ]for c in cards ]== ["e222" ,"e222" ]and [c ["index" ]for c in cards ]== [0 ,1 ] 120assert intel_spec .exactly_two_battlemage (cards ) 121 122 123def _server_intel (): 124sys .path .insert (0 ,str (ROOT / "sycl" / "serve" )) 125spec = importlib .util .spec_from_file_location ("server_intel_under_test" ,ROOT / "sycl" / "serve" / "server_intel.py" ) 126mod = importlib .util .module_from_spec (spec ) 127spec .loader .exec_module (mod ) 128return mod 129 130 131def _base_engine_args (cfg ,exe = None ,* ,announce = True ): 132"""The base server's signature (serve/server.py engine_args); test_wrapper_keeps_the_base_signature checks that it is still that.""" 133return list (cfg ["args" ]) 134 135 136def test_wrapper_keeps_the_base_signature_and_forwards_exe_and_announce (monkeypatch ,capsys ): 137"""#5086: every SYCL start died with TypeError (unexpected keyword 'announce'): the wrapper took only cfg.""" 138import inspect 139mod = _server_intel () 140S = mod .S 141shape = lambda f : [(p .name ,p .kind ,p .default )for p in inspect .signature (f ).parameters .values ()]# noqa: E731 142base = shape (S .engine_args ) 143seen = [] 144 145def spy (cfg ,exe = None ,* ,announce = True ): 146seen .append ((exe ,announce )) 147return list (cfg ["args" ]) 148monkeypatch .setattr (S ,"engine_args" ,spy ) 149monkeypatch .setattr (intel_spec ,"sysfs_cards" ,lambda * a ,** k : [dict (B65 ,index = 0 ),dict (B65 ,index = 1 )]) 150mod .install_two_card_spec () 151assert shape (S .engine_args )== base 152cfg = {"backend" :"sycl" ,"gpu" : [0 ,1 ],"args" :list (ARGS )} 153assert spec_of (S .engine_args (cfg ,"/x/strata-engine" ))== "2" 154capsys .readouterr () 155quiet = S .engine_args (cfg ,"/x/strata-engine" ,announce = False )# the budget check 156assert spec_of (quiet )== "2" and capsys .readouterr ().out == "" ,"a quiet build prints no line" 157assert spec_of (S .engine_args (cfg ,exe = "/y/e" ))== "2" 158assert seen == [("/x/strata-engine" ,True ), ("/x/strata-engine" ,False ), ("/y/e" ,True )] 159 160 161def test_the_real_base_function_runs_under_the_wrapper_without_a_model (monkeypatch ,capsys ): 162"""The ordinary start and the quiet check, through serve/server.py's own engine_args (no model, no engine process).""" 163from unittest import mock 164mod = _server_intel () 165S = mod .S 166monkeypatch .setattr (S ,"engine_args" ,S .engine_args )# restored after the test 167monkeypatch .setattr (intel_spec ,"sysfs_cards" ,lambda * a ,** k : [dict (B65 ,index = 0 ),dict (B65 ,index = 1 )]) 168mod .install_two_card_spec () 169cfg = {"model_name" :"qwen3.8-flash-next-iq3_s" ,"backend" :"sycl" ,"gpu" : [0 ,1 ],"args" :list (ARGS ),"drafting" :"mtp" } 170with mock .patch .object (S .drafting ,"detect_hardware" ,return_value = ("sycl" , [])),mock .patch .object (S ,"drafting_vram_mib" ,return_value = None ),mock .patch .object (S .drafting ,"mtp_ready" ,return_value = True ): 171started = S .engine_args (cfg ,"/x/strata-engine" ) 172out = capsys .readouterr ().out 173quiet = S .engine_args (cfg ,"/x/strata-engine" ,announce = False ) 174quiet_out = capsys .readouterr ().out 175assert spec_of (started )== "2" and spec_of (quiet )== "2" 176assert "[strata]" in out and quiet_out == "" 177 178 179def test_server_wrapper_applies_the_rule_at_start (monkeypatch ): 180mod = _server_intel () 181S = mod .S 182seen = [] 183monkeypatch .setattr (S ,"engine_args" ,_base_engine_args ) 184monkeypatch .setattr (intel_spec ,"sysfs_cards" ,lambda * a ,** k : [dict (B65 ,index = 0 ),dict (B65 ,index = 1 )]) 185mod .install_two_card_spec () 186cfg = {"backend" :"sycl" ,"gpu" : [0 ,1 ],"args" :list (ARGS )} 187assert spec_of (S .engine_args (cfg ))== "2" 188assert spec_of (cfg ["args" ])== "4" # the config in memory is not rewritten by a start 189assert spec_of (S .engine_args ({** cfg ,"user_args" : ["--spec" ,"4" ]}))== "4" 190assert spec_of (S .engine_args ({** cfg ,"gpu" :0 }))== "4" # one card 191assert spec_of (S .engine_args ({** cfg ,"backend" :"hip" }))== "4" 192assert spec_of (S .engine_args ({** cfg ,"args" :ARGS + ["--peer-device" ,"1" ]}))== "4" # not a layer split 193assert spec_of (S .engine_args ({** cfg ,"mtp" :False }))== "4" 194 195 196def test_server_wrapper_one_a_series_card_stays (monkeypatch ): 197mod = _server_intel () 198S = mod .S 199monkeypatch .setattr (S ,"engine_args" ,_base_engine_args ) 200monkeypatch .setattr (intel_spec ,"sysfs_cards" ,lambda * a ,** k : [dict (B65 ,index = 0 ),dict (A750 ,index = 1 )]) 201mod .install_two_card_spec () 202cfg = {"backend" :"sycl" ,"gpu" : [0 ,1 ],"args" :list (ARGS )} 203assert spec_of (S .engine_args (cfg ))== "4" 204 205 206 207def _derive (base ,settings ,cards ): 208from unittest import mock 209from strata_cli .launch .runner import derive_config 210with mock .patch .object (intel_spec ,"sysfs_cards" ,lambda * a ,** k :cards ): 211return derive_config (base ,settings ,bool_flags = frozenset ()) 212 213 214def test_launcher_writes_spec_2_for_two_b_series_cards (): 215from strata_cli .launch import settings as L 216base = {"backend" :"sycl" ,"gpu" : [0 ,1 ],"gpus_asked" :True ,"layer_split" :"auto" ,"exe" :"component:strata-engine" , 217"args" :list (ARGS ),"port" :8095 } 218two = [dict (B65 ,index = 0 ),dict (B65 ,index = 1 )] 219cfg = _derive (base ,L .LaunchSettings (),two ) 220assert spec_of (cfg ["args" ])== "2" and spec_of (base ["args" ])== "4" # the installed config is never edited 221# the person's choices come after the rule and win: --set --spec 4 on the command line, user_args in the config 222assert spec_of (_derive (base ,L .LaunchSettings (extra = {"--spec" :"4" }),two )["args" ])== "4" 223assert spec_of (_derive (base ,L .LaunchSettings (extra = {"--spec" :"3" }),two )["args" ])== "3" 224assert spec_of (_derive ({** base ,"user_args" : ["--spec" ,"4" ]},L .LaunchSettings (),two )["args" ])== "4" 225# no draft layer for this launch: nothing to pipeline, nothing changed 226assert "--spec" not in _derive (base ,L .LaunchSettings (mtp = "off" ),two )["args" ]or spec_of (_derive (base ,L .LaunchSettings (mtp = "off" ),two )["args" ])== "4" 227# one card, an A-series card in the pair, another backend 228assert spec_of (_derive ({** base ,"gpu" :0 },L .LaunchSettings (),two )["args" ])== "4" 229assert spec_of (_derive (base ,L .LaunchSettings (), [dict (B65 ,index = 0 ),dict (A750 ,index = 1 )])["args" ])== "4" 230assert spec_of (_derive ({** base ,"backend" :"hip" },L .LaunchSettings (),two )["args" ])== "4"