okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""strata_control/amd_four_card.py: the serving profile of four Radeon AI PRO R9700 cards, and the places that apply it.""" 2 3from __future__import annotations 4 5import os 6import unittest 7from unittest import mock 8 9from strata_control import amd_four_card as F 10 11R9700 = {"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :31.9 ,"vendor" :"amd" ,"usable" :True } 12CARDS = [{** R9700 ,"index" :i }for i in range (4 )] 13CFG = {"model_name" :"qwen3.8-flash-next-iq3_s" ,"backend" :"hip" ,"gpu" : [0 ,1 ,2 ,3 ],"layer_split" :"auto" , 14"args" : ["--pack" ,"/p/iq3_s" ,"--spec" ,"4" ,"--max-context" ,"32768" ]} 15 16 17def fresh (** kw ): 18c = {** CFG ,"args" :list (CFG ["args" ]),** kw } 19return c 20 21 22class Class (unittest .TestCase ): 23def test_four_r9700_are_the_class (self ): 24self .assertTrue (F .card_class (CARDS )) 25 26def test_other_hardware_is_not (self ): 27self .assertFalse (F .card_class (CARDS [:3 ]))# three cards 28self .assertFalse (F .card_class (CARDS + [CARDS [0 ]]))# five 29self .assertFalse (F .card_class ([{** c ,"name" :"AMD Radeon RX 7900 XTX" ,"vram_gb" :24.0 }for c in CARDS ])) 30self .assertFalse (F .card_class ([{** c ,"vendor" :"nvidia" }for c in CARDS ])) 31self .assertFalse (F .card_class ([{** c ,"vram_gb" :16.0 }for c in CARDS ]))# a 16 GB card with the same name 32self .assertFalse (F .card_class ([* CARDS [:3 ], {** R9700 ,"name" :"AMD Radeon RX 9070" }])) 33 34def test_only_the_measured_model (self ): 35for name in ("qwen3.8-flash-next-iq3_xxs" ,"qwen3.6-35b-a3b-q4" ,"deepseek-v4.1-flash-2.00bpw" ): 36with self .subTest (name ): 37self .assertFalse (F .applies (CARDS ,fresh (model_name = name ,args = ["--pack" ,"/p/x" ]))) 38self .assertTrue (F .applies (CARDS ,fresh ())) 39self .assertFalse (F .applies (CARDS ,fresh (backend = "cuda" ))) 40self .assertFalse (F .applies (CARDS ,fresh (args = CFG ["args" ]+ ["--peer-device" ,"1" ]))) 41 42 43class Apply (unittest .TestCase ): 44def test_fills_the_profile_on_a_default_config (self ): 45c = fresh () 46with mock .patch .dict (os .environ , {},clear = False ): 47for k in list (F .BATCH_ENV )+ ["STRATA_FOUR_CARD_PROFILE" ]: 48os .environ .pop (k ,None ) 49notes = F .apply (CARDS ,c ) 50self .assertTrue (notes ) 51self .assertEqual (c ["layer_split" ],"13,26,39" ) 52self .assertEqual (c ["parallel" ],32 ) 53self .assertEqual (c ["args" ][c ["args" ].index ("--batch-groups" )+ 1 ],"4" ) 54self .assertEqual (c ["args" ][c ["args" ].index ("--prefill" )+ 1 ],"2048" ) 55self .assertEqual (c ["env" ],F .BATCH_ENV ) 56self .assertNotIn ("--prompt-cache" ,c ["args" ]) 57 58def test_prefill_auto_from_setup_is_not_a_choice (self ): 59c = fresh (args = CFG ["args" ]+ ["--prefill" ,"auto" ]) 60F .apply (CARDS ,c ) 61self .assertEqual (c ["args" ][c ["args" ].index ("--prefill" )+ 1 ],"2048" ) 62self .assertEqual (c ["args" ].count ("--prefill" ),1 ) 63c = fresh (args = CFG ["args" ]+ ["--prefill" ,"auto:32768" ]) 64F .apply (CARDS ,c ) 65self .assertEqual (c ["args" ][c ["args" ].index ("--prefill" )+ 1 ],"auto:32768" ) 66 67def test_the_persons_choices_win (self ): 68c = fresh (layer_split = "12,25,37" ,parallel = 8 ,args = CFG ["args" ]+ ["--prefill" ,"8192" ,"--batch-groups" ,"2" ], 69env = {"STRATA_QSA_SLOTS" :"0" }) 70F .apply (CARDS ,c ) 71self .assertEqual (c ["layer_split" ],"12,25,37" ) 72self .assertEqual (c ["parallel" ],8 ) 73self .assertEqual (c ["args" ][c ["args" ].index ("--prefill" )+ 1 ],"8192" ) 74self .assertEqual (c ["args" ].count ("--batch-groups" ),1 ) 75self .assertEqual (c ["env" ]["STRATA_QSA_SLOTS" ],"0" ) 76 77def test_a_number_of_requests_asked_for_keeps_the_groups_to_the_engine (self ): 78c = fresh () 79F .apply (CARDS ,c ,parallel_given = True ) 80self .assertNotIn ("parallel" ,c ) 81self .assertNotIn ("--batch-groups" ,c ["args" ]) 82self .assertEqual (c ["layer_split" ],"13,26,39" ) 83 84def test_an_explicit_batch_in_the_args_is_kept (self ): 85c = fresh (args = CFG ["args" ]+ ["--batch" ,"6" ]) 86F .apply (CARDS ,c ) 87self .assertNotIn ("parallel" ,c ) 88self .assertNotIn ("--batch-groups" ,c ["args" ]) 89 90def test_an_environment_variable_of_the_persons_own_wins (self ): 91c = fresh () 92with mock .patch .dict (os .environ , {"STRATA_BATCH_SELF_COMMIT" :"0" }): 93F .apply (CARDS ,c ) 94self .assertNotIn ("STRATA_BATCH_SELF_COMMIT" ,c ["env" ]) 95self .assertEqual (c ["env" ]["STRATA_QSA_SLOTS" ],"1" ) 96 97def test_off_switches (self ): 98c = fresh () 99with mock .patch .dict (os .environ , {"STRATA_FOUR_CARD_PROFILE" :"0" }): 100self .assertEqual (F .apply (CARDS ,c ), []) 101self .assertEqual (c ,fresh ()) 102c = fresh (four_card_profile = False ) 103self .assertEqual (F .apply (CARDS ,c ), []) 104self .assertNotIn ("parallel" ,c ) 105 106def test_other_hardware_is_untouched (self ): 107for cards in (CARDS [:2 ], [{** x ,"name" :"NVIDIA RTX A4000" ,"vendor" :"nvidia" }for x in CARDS ]): 108c = fresh () 109self .assertEqual (F .apply (cards ,c ), []) 110self .assertEqual (c ,fresh ()) 111 112 113class Launcher (unittest .TestCase ): 114def test_the_runner_fills_the_profile_and_the_profile_override (self ): 115from strata_cli .launch import runner as R ,settings as S 116from tests .terminal .launch_support import HW_12 ,Home ,catalog 117hw = {** HW_12 ,"gpus" : [{** x ,"usable" :True }for x in CARDS ]} 118with Home (): 119r = R .Runner (S .LaunchSettings (),catalog = catalog (hw )) 120cfg = fresh () 121r .apply_four_card (cfg ) 122self .assertEqual (cfg ["layer_split" ],"13,26,39" ) 123self .assertEqual (r .four_card_parallel ,32 ) 124r2 = R .Runner (S .LaunchSettings (parallel = 3 ),catalog = catalog (hw )) 125cfg2 = fresh () 126r2 .apply_four_card (cfg2 ) 127self .assertNotIn ("parallel" ,cfg2 ) 128self .assertIsNone (r2 .four_card_parallel ) 129 130def test_the_runner_leaves_other_computers_alone (self ): 131from strata_cli .launch import runner as R ,settings as S 132from tests .terminal .launch_support import HW_2X16 ,Home ,catalog 133with Home (): 134r = R .Runner (S .LaunchSettings (),catalog = catalog (HW_2X16 )) 135cfg = {** fresh (),"gpu" : [0 ,1 ]} 136r .apply_four_card (cfg ) 137self .assertEqual (cfg , {** fresh (),"gpu" : [0 ,1 ]}) 138self .assertIsNone (r .four_card_parallel ) 139 140 141 142class ServerMessage (unittest .TestCase ): 143def test_more_than_eight_slots_on_a_layer_split_is_not_cut_in_the_message (self ): 144import contextlib 145import io 146from serve import server as S 147split = {"parallel" :32 ,"gpu" : [0 ,1 ,2 ,3 ],"layer_split" :"13,26,39" ,"args" : ["--pack" ,"/p" ]} 148one = {"parallel" :32 ,"gpu" :0 ,"args" : ["--pack" ,"/p" ]} 149for cfg ,said in ((split ,False ), (one ,True )): 150with self .subTest (gpus = len (S .gpu_list (cfg ))): 151out = io .StringIO () 152with contextlib .redirect_stdout (out ): 153self .assertEqual (S .parallel_args (cfg ,cfg ["args" ]), ["--batch" ,"32" ]) 154self .assertEqual ("runs at most" in out .getvalue (),said ) 155 156if __name__ == "__main__" : 157unittest .main ()