okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e28889d12
main
1"""strata_control/gpu_roles.py: Main / Helper / Off for every graphics card (the rules the app, `strata launch`, setup and the server share).""" 2 3from __future__import annotations 4 5import json 6import unittest 7from pathlib import Path 8 9from strata_control import gpu_roles as R 10 11ROOT = Path (__file__ ).resolve ().parents [2 ] 12V = json .loads ((ROOT / "tests" / "data" / "gpu_roles_vectors.json" ).read_text (encoding = "utf-8" )) 13 14 15def roles (d ): 16return None if d is None else {int (k ):v for k ,v in d .items ()} 17 18 19class Vectors (unittest .TestCase ): 20"""tests/data/gpu_roles_vectors.json is also run by apps/host/tests/gpu-roles.test.mjs against gpu-roles.ts.""" 21 22def test_recommend (self ): 23for c in V ["recommend" ]: 24with self .subTest (c ["hw" ]): 25r = R .recommend (V ["hw" ][c ["hw" ]]) 26self .assertEqual (r ["roles" ],roles (c ["roles" ])) 27self .assertEqual ((r ["main" ],r ["why" ]), (c ["main" ],c ["why" ])) 28 29def test_set_role (self ): 30for c in V ["set_role" ]: 31with self .subTest (c ): 32out ,note = R .set_role (roles (c ["roles" ]),V ["hw" ][c ["hw" ]],c ["card" ],c ["role" ]) 33self .assertEqual (out ,roles (c ["after" ])) 34self .assertEqual (note ,c ["note" ]) 35 36def test_order (self ): 37for c in V ["order" ]: 38with self .subTest (c ): 39self .assertEqual (R .order (roles (c ["roles" ]),V ["hw" ][c ["hw" ]]),c ["gpu" ]) 40 41def test_warnings (self ): 42for c in V ["warnings" ]: 43with self .subTest (c ): 44w = R .warnings (roles (c ["roles" ]),V ["hw" ][c ["hw" ]],spill_gb = c .get ("spill_gb" ,0.0 )) 45self .assertEqual ([x ["code" ]for x in w ],c ["codes" ]) 46if c .get ("fix" ): 47self .assertEqual (w [0 ]["fix" ],c ["fix" ]) 48 49def test_saved (self ): 50for c in V ["saved" ]: 51with self .subTest (c ): 52got ,problem = R .saved_roles (c ["cfg" ]) 53self .assertEqual (got ,roles (c ["roles" ])) 54if c .get ("problem" ): 55self .assertIn (c ["problem" ],problem or "" ) 56 57def test_legacy (self ): 58for c in V ["legacy" ]: 59with self .subTest (c ): 60self .assertEqual (R .from_legacy (c ["gpu" ],V ["hw" ][c ["hw" ]],c .get ("gpu_order" )),roles (c ["roles" ])) 61 62 63class Words (unittest .TestCase ): 64def test_warnings_are_one_short_line_and_say_only_what_a_person_cannot_know (self ): 65two = V ["hw" ]["two" ] 66# a slower card as Main is NOT warned about: people understand it (owner rule) 67self .assertEqual (R .warnings ({0 :"main" ,1 :"helper" },two ), []) 68for case in V ["warnings" ]: 69for w in R .warnings (roles (case ["roles" ]),V ["hw" ][case ["hw" ]],spill_gb = case .get ("spill_gb" ,0.0 )): 70self .assertLess (len (w ["text" ]),90 ,w ["text" ]) 71self .assertNotIn ("slower" ,w ["text" ].replace ("is slow as Main" ,"" )) 72 73def test_cpu_only_line (self ): 74w = R .warnings ({0 :"off" ,1 :"off" },V ["hw" ]["two" ],engine_runs_cpu_only = True ) 75self .assertEqual (w [0 ]["text" ],"Performance is degraded on CPU only." ) 76w = R .warnings ({0 :"off" ,1 :"off" },V ["hw" ]["two" ]) 77self .assertEqual (w [0 ]["text" ],R .NO_CARD_LINE ) 78 79def test_sentence_and_describe (self ): 80self .assertEqual (R .sentence (V ["hw" ]["one" ]),"Strata will use your RTX 4070 Ti." ) 81s = R .sentence (V ["hw" ]["three" ]) 82self .assertIn ("RTX 4070 Ti is Main: it is faster." ,s ) 83self .assertIn ("RTX 2060 starts Off: 8 GB or less." ,s ) 84self .assertEqual (R .describe ({0 :"helper" ,1 :"main" },V ["hw" ]["two" ]),"Main: GPU 2 RTX 4070 Ti. Helper: GPU 1 RTX 3060." ) 85 86def test_cards_with_8_gb_or_less_start_off_but_a_lone_8_gb_card_is_main (self ): 87self .assertEqual (R .recommend ([{"index" :0 ,"name" :"RTX 4060" ,"vram_gb" :8 }])["roles" ], {0 :"main" }) 88r = R .recommend ([{"index" :0 ,"name" :"A" ,"vram_gb" :12 ,"speed" :9 }, {"index" :1 ,"name" :"B" ,"vram_gb" :8 ,"speed" :5 }, {"index" :2 ,"name" :"C" ,"vram_gb" :10 ,"speed" :5 }]) 89self .assertEqual (r ["roles" ], {0 :"main" ,1 :"off" ,2 :"helper" }) 90 91 92class MirrorAndParse (unittest .TestCase ): 93def test_mirror_writes_roles_and_the_legacy_fields_with_main_last (self ): 94m = R .mirror ({0 :"helper" ,1 :"main" },V ["hw" ]["two" ]) 95self .assertEqual (m ["gpu_roles" ], {"0" :"helper" ,"1" :"main" }) 96self .assertEqual ((m ["gpu" ],m ["gpu_order" ]), ([0 ,1 ],"as_given" )) 97self .assertNotIn ("gpu_none" ,m ) 98one = R .mirror ({0 :"off" ,1 :"main" },V ["hw" ]["two" ]) 99self .assertEqual (one ["gpu" ],1 )# one card is a number, never a list (#3203) 100none = R .mirror ({0 :"off" ,1 :"off" },V ["hw" ]["two" ]) 101self .assertEqual ((none ["gpu" ],none ["gpu_none" ]), ([],True )) 102 103def test_parse_roles (self ): 104cards = V ["hw" ]["three" ] 105self .assertEqual (R .parse_roles ("1=main, 0=helper,2=off" ,cards ), {1 :"main" ,0 :"helper" ,2 :"off" }) 106self .assertEqual (R .roles_from_text ("1=main,0=helper" ,cards ), {0 :"helper" ,1 :"main" ,2 :"off" })# unnamed cards are Off 107self .assertEqual (R .roles_from_text ("0=helper" ,cards ), {0 :"main" ,1 :"off" ,2 :"off" })# a lone card on is Main 108for bad in ("" ,"1" ,"1=boss" ,"x=main" ,"0=main,1=main" ,"7=main" ): 109with self .subTest (bad ),self .assertRaises (ValueError ): 110R .parse_roles (bad ,cards ) 111 112def test_normalize (self ): 113two = V ["hw" ]["two" ] 114self .assertEqual (R .normalize ({0 :"helper" ,1 :"helper" },two ), {0 :"helper" ,1 :"main" }) 115self .assertEqual (R .normalize ({0 :"main" ,1 :"main" },two ), {0 :"main" ,1 :"helper" }) 116self .assertEqual (R .normalize ({0 :"helper" ,1 :"off" },two ), {0 :"main" ,1 :"off" }) 117 118def test_spill_uses_the_cards_that_are_on (self ): 119two = [{"index" :0 ,"name" :"A" ,"vram_gb" :12 }, {"index" :1 ,"name" :"B" ,"vram_gb" :12 }] 120a = R .spill_for ({0 :"main" ,1 :"helper" },two ,44.0 ,32.0 ) 121b = R .spill_for ({0 :"main" ,1 :"off" },two ,44.0 ,32.0 ) 122self .assertLess (a ,b ) 123self .assertEqual (R .spill_for ({0 :"main" ,1 :"off" },two ,None ,32.0 ),0.0 ) 124 125 126if __name__ == "__main__" : 127unittest .main ()