okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e28889d12
main
1"""serve/server.py: the run settings' "gpu_roles" (Main / Helper / Off) become the engine's card list, Main LAST, Off left out.""" 2from __future__import annotations 3 4import sys 5import unittest 6from pathlib import Path 7 8sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 9from serve .server import card_roles_lines ,child_env ,gpu_list ,ordered_gpus # noqa: E402 10 11 12class Roles (unittest .TestCase ): 13def test_roles_alone_give_the_order_main_last_and_off_left_out (self ): 14cfg = {"gpu_roles" : {"0" :"helper" ,"1" :"main" ,"2" :"off" },"args" : []} 15self .assertEqual (gpu_list (cfg ), [0 ,1 ]) 16self .assertEqual (gpu_list ({"gpu_roles" : {"0" :"main" ,"1" :"helper" },"args" : []}), [1 ,0 ]) 17env = child_env ({"gpu_roles" : {"0" :"main" ,"1" :"helper" ,"2" :"off" },"args" : []}) 18self .assertEqual (env ["CUDA_VISIBLE_DEVICES" ],"1,0" ) 19 20def test_the_mirror_is_used_and_never_re_sorted_by_speed (self ): 21cfg = {"gpu_roles" : {"0" :"main" ,"1" :"helper" },"gpu" : [1 ,0 ],"gpu_order" :"as_given" ,"args" : []} 22self .assertEqual (ordered_gpus (cfg , {0 :1.0 ,1 :9.0 }), [1 ,0 ])# the faster card is Helper on purpose 23cfg2 = {"gpu_roles" : {"0" :"main" ,"1" :"helper" },"gpu" : [1 ,0 ],"args" : []}# even without gpu_order 24self .assertEqual (ordered_gpus (cfg2 , {0 :1.0 ,1 :9.0 }), [1 ,0 ]) 25 26def test_one_card_on (self ): 27cfg = {"gpu_roles" : {"0" :"off" ,"1" :"main" },"gpu" :1 ,"gpu_order" :"as_given" ,"args" : []} 28self .assertEqual (gpu_list (cfg ), [1 ]) 29self .assertEqual (child_env (cfg )["CUDA_VISIBLE_DEVICES" ],"1" ) 30 31def test_a_hand_edited_gpu_wins (self ): 32cfg = {"gpu_roles" : {"0" :"helper" ,"1" :"main" },"gpu" : [1 ,0 ],"gpu_order" :"as_given" ,"args" : []} 33self .assertEqual (gpu_list (cfg ), [1 ,0 ]) 34self .assertTrue (any ("edited by hand" in l for l in card_roles_lines (cfg ))) 35self .assertEqual (ordered_gpus ({** cfg ,"gpu_order" :None }, {0 :1.0 ,1 :9.0 }), [0 ,1 ])# the legacy rule again 36 37def test_bad_roles_are_ignored (self ): 38for bad in ({"0" :"boss" }, {"0" :"main" ,"1" :"main" },"x" , {}): 39self .assertEqual (gpu_list ({"gpu_roles" :bad ,"gpu" :1 ,"args" : []}), [1 ]) 40 41def test_legacy_settings_are_untouched (self ): 42self .assertEqual (gpu_list ({"gpu" :"0,2" ,"args" : []}), [0 ,2 ]) 43self .assertEqual (gpu_list ({"args" : []}), []) 44self .assertEqual (card_roles_lines ({"gpu" : [1 ,0 ],"args" : []}), []) 45 46def test_the_log_line_names_every_role (self ): 47lines = card_roles_lines ({"gpu_roles" : {"0" :"helper" ,"1" :"main" ,"2" :"off" },"gpu" : [0 ,1 ],"args" : []}) 48self .assertEqual (lines , ["[strata] card roles: Main GPU 1, Helper GPU 0, Off GPU 2 (numbers as nvidia-smi gives them)" ]) 49 50def test_every_card_off (self ): 51cfg = {"gpu_roles" : {"0" :"off" ,"1" :"off" },"gpu" : [],"gpu_none" :True ,"args" : []} 52self .assertEqual (gpu_list (cfg ), []) 53env = child_env (cfg ) 54self .assertEqual ((env ["CUDA_VISIBLE_DEVICES" ],env ["HIP_VISIBLE_DEVICES" ]), ("" ,"" )) 55with self .assertRaises (SystemExit )as e : 56card_roles_lines (cfg )# a CUDA engine cannot start without a card: said plainly 57self .assertIn ("Turn a card on" ,str (e .exception )) 58cpu = card_roles_lines ({** cfg ,"backend" :"cpu" }) 59self .assertIn ("Performance is degraded on CPU only." ,cpu [0 ]) 60 61 62if __name__ == "__main__" : 63unittest .main ()