okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""strata_control/replica_profile.py: the opt-in replica serving profile (one engine per card, shared expert arena).""" 2 3from __future__import annotations 4 5import unittest 6 7from strata_control import replica_profile as P 8 9R9700 = {"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :31.9 ,"vendor" :"amd" } 10 11 12def cards (n ,** kw ): 13return [{** R9700 ,** kw ,"index" :i }for i in range (n )] 14 15 16def cfg (** kw ): 17c = {"model_name" :"qwen3.8-flash-next-iq3_s" ,"gpu" : [0 ,1 ,2 ,3 ],"layer_split" :"auto" , 18"args" : ["--pack" ,"/p/iq3_s" ,"--spec" ,"4" ],"replica_profile" :True } 19c .update (kw ) 20return c 21 22 23class Request (unittest .TestCase ): 24def test_opt_in_only (self ): 25self .assertFalse (P .requested ({}, {})) 26self .assertTrue (P .requested ({"replica_profile" :True }, {})) 27self .assertTrue (P .requested ({}, {"STRATA_REPLICA_PROFILE" :"1" })) 28 29def test_an_explicit_no_wins (self ): 30self .assertFalse (P .requested ({"replica_profile" :False }, {"STRATA_REPLICA_PROFILE" :"1" })) 31 32def test_without_the_request_nothing_changes (self ): 33c = cfg (replica_profile = None ) 34c .pop ("replica_profile" ) 35before = dict (c ) 36self .assertEqual (P .apply (cards (4 ),c ,256.0 ,platform = "linux" ,environ = {}), []) 37self .assertEqual (c ,before ) 38 39 40class Count (unittest .TestCase ): 41def test_ram_sets_the_number_of_engines (self ): 42self .assertEqual (P .replicas_for (4 ,50.3 ,251.0 ,True ),4 ) 43self .assertEqual (P .replicas_for (4 ,50.3 ,128.0 ,True ),4 ) 44self .assertEqual (P .replicas_for (4 ,50.3 ,96.0 ,True ),4 ) 45self .assertEqual (P .replicas_for (4 ,50.3 ,80.0 ,True ),2 )# 80 - 12 - 50.3 = 17.7 -> 2 46self .assertEqual (P .replicas_for (4 ,50.3 ,64.0 ,True ),0 )# one engine's room only 47self .assertEqual (P .replicas_for (2 ,50.3 ,251.0 ,False ),2 )# private copies: 2 x 58.3 + 12 48self .assertEqual (P .replicas_for (4 ,50.3 ,200.0 ,False ),3 ) 49self .assertEqual (P .replicas_for (2 ,50.3 ,100.0 ,False ),1 ) 50 51 52class Apply (unittest .TestCase ): 53def test_four_cards_with_plenty_of_ram (self ): 54c = cfg () 55notes = P .apply (cards (4 ),c ,251.0 ,platform = "linux" ,environ = {}) 56self .assertTrue (notes ) 57self .assertEqual (c ["replicas" ], [{"gpus" : [0 ]}, {"gpus" : [1 ]}, {"gpus" : [2 ]}, {"gpus" : [3 ]}]) 58self .assertIs (c ["replica_shared_arena" ],True ) 59self .assertEqual (c ["parallel" ],4 ) 60self .assertEqual (c ["env" ]["STRATA_CKPT_POOL" ],"1" ) 61self .assertEqual (c ["env" ]["STRATA_QSA_SLOTS" ],"1" ) 62 63def test_two_cards_get_eight_slots_each (self ): 64c = cfg (gpu = [0 ,1 ]) 65P .apply (cards (2 ),c ,128.0 ,platform = "linux" ,environ = {}) 66self .assertEqual (c ["parallel" ],8 ) 67self .assertEqual (len (c ["replicas" ]),2 ) 68 69def test_each_engine_keeps_vram_free_for_the_prompt_capture (self ): 70c = cfg (gpu = [0 ,1 ]) 71P .apply (cards (2 ),c ,128.0 ,platform = "linux" ,environ = {}) 72self .assertEqual (c ["args" ][c ["args" ].index ("--vram-reserve-mib" )+ 1 ],str (P .REPLICA_RESERVE_MIB )) 73c = cfg (gpu = [0 ,1 ],args = ["--pack" ,"/p/iq3_s" ,"--vram-reserve-mib" ,"900" ]) 74P .apply (cards (2 ),c ,128.0 ,platform = "linux" ,environ = {}) 75self .assertEqual (c ["args" ].count ("--vram-reserve-mib" ),1 ) 76self .assertEqual (c ["args" ][c ["args" ].index ("--vram-reserve-mib" )+ 1 ],"900" ) 77 78def test_less_ram_drops_cards_and_says_so (self ): 79c = cfg () 80notes = P .apply (cards (4 ),c ,80.0 ,platform = "linux" ,environ = {}) 81self .assertEqual (c ["replicas" ], [{"gpus" : [0 ]}, {"gpus" : [1 ]}]) 82self .assertEqual (c ["gpu" ], [0 ,1 ]) 83self .assertTrue (any ("left out" in n for n in notes )) 84 85def test_too_little_ram_keeps_the_layer_split (self ): 86c = cfg () 87notes = P .apply (cards (4 ),c ,64.0 ,platform = "linux" ,environ = {}) 88self .assertNotIn ("replicas" ,c ) 89self .assertEqual (c ["layer_split" ],"auto" ) 90self .assertTrue (notes and "layer split stays" in notes [0 ]) 91 92def test_windows_shares_through_a_section (self ): 93c = cfg (gpu = [0 ,1 ]) 94P .apply (cards (2 ),c ,80.0 ,platform = "win32" ,environ = {}) 95self .assertEqual (len (c ["replicas" ]),2 ) 96self .assertIs (c ["replica_shared_arena" ],True ) 97 98def test_an_os_that_cannot_share_needs_the_private_copies_to_fit (self ): 99c = cfg (gpu = [0 ,1 ]) 100self .assertEqual (P .apply (cards (2 ),c ,100.0 ,platform = "darwin" ,environ = {})[0 ].split (":" )[0 ],"replica profile" ) 101self .assertNotIn ("replicas" ,c ) 102c = cfg (gpu = [0 ,1 ]) 103P .apply (cards (2 ),c ,160.0 ,platform = "darwin" ,environ = {}) 104self .assertEqual (len (c ["replicas" ]),2 ) 105self .assertNotIn ("replica_shared_arena" ,c ) 106 107def test_what_the_person_chose_wins (self ): 108for kw in ({"replicas" :2 }, {"layer_split" :"13,26,39" }, {"args" : ["--pack" ,"/p/iq3_s" ,"--peer-device" ,"1" ]}, 109 {"args" : ["--pack" ,"/p/iq3_s" ,"--layer-split" ,"20,40" ]}): 110c = cfg (** kw ) 111before = dict (c ) 112P .apply (cards (4 ),c ,251.0 ,platform = "linux" ,environ = {}) 113self .assertEqual (c .get ("replicas" ),before .get ("replicas" ),kw ) 114self .assertEqual (c .get ("layer_split" ),before .get ("layer_split" ),kw ) 115 116def test_parallel_and_environment_variables_of_the_person_win (self ): 117c = cfg (parallel = 6 ) 118P .apply (cards (4 ),c ,251.0 ,platform = "linux" ,environ = {"STRATA_CKPT_POOL" :"0" }) 119self .assertEqual (c ["parallel" ],6 ) 120self .assertNotIn ("STRATA_CKPT_POOL" ,c ["env" ]) 121c = cfg () 122P .apply (cards (4 ),c ,251.0 ,parallel_given = True ,platform = "linux" ,environ = {}) 123self .assertNotIn ("parallel" ,c ) 124 125def test_only_the_measured_hardware_and_model (self ): 126for kw in ({"model_name" :"qwen3.6-35b-a3b-q4" }, {"model_name" :"qwen3.8-flash-next-iq2_xs" ,"args" : ["--pack" ,"/p/x" ]}): 127c = cfg (** kw ) 128P .apply (cards (4 ),c ,251.0 ,platform = "linux" ,environ = {}) 129self .assertNotIn ("replicas" ,c ,kw ) 130c = cfg (gpu = [0 ,1 ]) 131P .apply (cards (2 ,vram_gb = 16.0 ),c ,251.0 ,platform = "linux" ,environ = {}) 132self .assertNotIn ("replicas" ,c ) 133c = cfg (gpu = [0 ]) 134P .apply (cards (1 ),c ,251.0 ,platform = "linux" ,environ = {}) 135self .assertNotIn ("replicas" ,c ) 136 137 138if __name__ == "__main__" : 139unittest .main ()