okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01f8c3255
main
1"""strata_control/gpu_fit.py: the one rule for which graphics cards a model uses (app, strata launch and setup share it).""" 2 3from __future__import annotations 4 5import re 6import unittest 7from pathlib import Path 8 9from strata_control import gpu_fit as G 10 11ROOT = Path (__file__ ).resolve ().parents [2 ] 12 13 14def card (index ,name ,vram ,** kw ): 15return {"index" :index ,"name" :name ,"vram_gb" :vram ,"usable" :True ,** kw } 16 17 18class GpuFitRule (unittest .TestCase ): 19def test_one_card_is_the_only_option (self ): 20r = G .options (44.0 ,62.0 , [card (0 ,"NVIDIA GeForce RTX 3070 Ti" ,8 )]) 21self .assertEqual ([o ["key" ]for o in r ["options" ]], ["single" ]) 22self .assertEqual (r ["recommended" ],"single" ) 23self .assertEqual (r ["options" ][0 ]["verdict" ],"fits" ) 24self .assertIn ("RTX 3070 Ti + RAM" ,r ["options" ][0 ]["label" ]) 25 26def test_two_unequal_cards_and_the_model_fits_one_defaults_to_single_and_offers_the_split (self ): 27r = G .options (44.0 ,62.0 , [card (1 ,"NVIDIA GeForce RTX 3060 Ti" ,8 ),card (0 ,"NVIDIA GeForce RTX 3070 Ti" ,16 )]) 28keys = [o ["key" ]for o in r ["options" ]] 29self .assertEqual (keys [:2 ], ["single" ,"split" ])# the largest card first, then the split 30self .assertEqual (r ["recommended" ],"single" ) 31self .assertEqual (r ["options" ][0 ]["cards" ], [0 ])# the largest card is the 16 GB one 32split = r ["options" ][1 ] 33self .assertEqual (split ["cards" ], [0 ,1 ]) 34self .assertIn ("layer split" ,split ["label" ]) 35self .assertTrue (any ("20% faster" in n for n in split ["notes" ])) 36self .assertEqual (sum (1 for o in r ["options" ]if o ["recommended" ]),1 ) 37 38def test_two_small_cards_and_the_model_does_not_fit_one_defaults_to_the_split (self ): 39r = G .options (44.0 ,32.0 , [card (0 ,"NVIDIA GeForce RTX 3060" ,10 ),card (1 ,"NVIDIA GeForce RTX 3060" ,10 )]) 40single ,split = r ["options" ][0 ],r ["options" ][1 ] 41self .assertEqual (single ["verdict" ],"spills" ) 42self .assertLess (split ["spill_gb" ],single ["spill_gb" ])# the second card takes part of the SSD reading away 43self .assertEqual (r ["recommended" ],"split" ) 44self .assertEqual (split ["cards" ], [0 ,1 ]) 45 46def test_a_tight_single_card_moves_the_default_to_the_split_when_that_fits_with_room (self ): 47# 44 GB model, 46 GB of RAM: one 12 GB card + RAM holds it but leaves no RAM spare (tight); two cards leave room 48r = G .options (44.0 ,46.0 , [card (0 ,"RTX 4070" ,12 ),card (1 ,"RTX 4070" ,12 )]) 49self .assertEqual (r ["options" ][0 ]["verdict" ],"tight" ) 50self .assertEqual (r ["options" ][1 ]["verdict" ],"fits" ) 51self .assertEqual (r ["recommended" ],"split" ) 52 53def test_an_integrated_chip_and_a_tiny_card_are_ignored (self ): 54cards = [card (0 ,"NVIDIA GeForce RTX 3070 Ti" ,8 ),card (1 ,"AMD Radeon Graphics" ,16 ,integrated = True ),card (2 ,"Old display card" ,2 )] 55r = G .options (44.0 ,62.0 ,cards ) 56self .assertEqual ([o ["key" ]for o in r ["options" ]], ["single" ]) 57self .assertEqual ({c ["index" ]for c in r ["ignored" ]}, {1 ,2 }) 58self .assertTrue (all (c ["why_ignored" ]for c in r ["ignored" ])) 59self .assertEqual (G .recommended_cards (44.0 ,62.0 ,cards ), [0 ]) 60 61def test_a_unified_memory_chip_counts_when_it_is_the_only_device (self ): 62mac = {"index" :0 ,"name" :"Apple M4" ,"vendor" :"apple" ,"vram_gb" :0.0 ,"unified_gb" :32.0 ,"usable" :True } 63keep ,ignored = G .usable_cards ([mac ]) 64self .assertEqual ((len (keep ),ignored ), (1 , [])) 65igpu = {"index" :0 ,"name" :"AMD Radeon 8060S" ,"vram_gb" :64.0 ,"uma" :True ,"usable" :True } 66self .assertEqual (len (G .usable_cards ([igpu ])[0 ]),1 )# a Strix Halo PC: the integrated chip IS the device 67keep ,ignored = G .usable_cards ([card (0 ,"RTX 3070 Ti" ,8 ),igpu ])# beside a real card it adds nothing 68self .assertEqual ([c ["index" ]for c in keep ], [0 ]if keep [0 ]["name" ]== "RTX 3070 Ti" else []) 69self .assertEqual (len (ignored ),1 ) 70 71def test_a_strix_halo_counts_its_memory_once (self ): 72"""The 'shared' pool of an APU is the RAM: a 70 GB model on a 128 GB Strix Halo with a 2 GB carve-out is RAM-resident, not 'VRAM + RAM'.""" 73igpu = {"index" :0 ,"name" :"AMD Radeon 8060S" ,"vram_gb" :66.0 ,"dedicated_gb" :2.0 ,"uma" :True ,"usable" :True } 74self .assertEqual (G .own_memory_gb (igpu ),2.0 ) 75self .assertEqual (G .own_memory_gb ({"index" :1 ,"vram_gb" :32.0 }),32.0 ) 76self .assertEqual (G .own_memory_gb ({"index" :0 ,"vram_gb" :66.0 ,"uma" :True }),0.0 )# no carve-out figure: none counted 77# 64 GB PC, a 90 GB model: counted twice (66 GB 'VRAM' + 54 GB RAM) it looked like it fit; once, 36 GB come from the SSD 78r = G .options (90.0 ,64.0 , [dict (igpu ,vram_gb = 34.0 )]) 79self .assertEqual (r ["options" ][0 ]["verdict" ],"spills" ) 80self .assertGreater (r ["options" ][0 ]["spill_gb" ],30.0 ) 81# 128 GB, 90 GB model: fits in RAM 82self .assertEqual (G .options (90.0 ,128.0 , [dict (igpu ,vram_gb = 66.0 )])["options" ][0 ]["verdict" ],"fits" ) 83 84def test_an_unusable_card_is_ignored_with_its_own_reason (self ): 85r = G .options (44.0 ,62.0 , [card (0 ,"RTX 3070 Ti" ,8 ),card (1 ,"GTX 1060" ,6 ,usable = False ,problem = "older than the RTX 20 series" )]) 86self .assertEqual ([o ["key" ]for o in r ["options" ]], ["single" ]) 87self .assertIn ("older than the RTX 20 series" ,r ["ignored" ][0 ]["why_ignored" ]) 88 89def test_a_card_under_the_split_minimum_is_offered_alone_but_not_in_the_split (self ): 90r = G .options (44.0 ,32.0 , [card (0 ,"RTX 3060" ,12 ),card (1 ,"GTX 1660" ,6 )]) 91keys = [o ["key" ]for o in r ["options" ]] 92self .assertNotIn ("split" ,keys ) 93self .assertEqual (r ["recommended" ],"single" ) 94 95def test_three_cards_offer_the_pair_and_all_of_them (self ): 96r = G .options (70.0 ,32.0 , [card (i ,"RTX 3060" ,12 )for i in range (3 )]) 97keys = [o ["key" ]for o in r ["options" ]] 98self .assertEqual (keys [:3 ], ["single" ,"split" ,"split-all" ]) 99 100def test_no_card_gives_no_options (self ): 101r = G .options (44.0 ,62.0 , []) 102self .assertEqual ((r ["options" ],r ["recommended" ]), ([],None )) 103 104def test_unknown_ram_is_unknown_not_a_guess (self ): 105r = G .options (44.0 ,None , [card (0 ,"RTX 3070 Ti" ,8 ),card (1 ,"RTX 3060 Ti" ,8 )]) 106self .assertEqual (r ["options" ][0 ]["verdict" ],"unknown" ) 107self .assertEqual (r ["recommended" ],"single" ) 108 109def test_plain_numbers_are_in_the_words (self ): 110r = G .options (44.0 ,62.0 , [card (0 ,"NVIDIA GeForce RTX 3070 Ti" ,8 )]) 111self .assertIn ("needs 44 GB" ,r ["options" ][0 ]["words" ]) 112self .assertIn ("RTX 3070 Ti + RAM" ,r ["options" ][0 ]["summary" ]) 113r2 = G .options (44.0 ,32.0 , [card (0 ,"RTX 3060" ,10 )]) 114self .assertIn ("read from the SSD" ,r2 ["options" ][0 ]["words" ]) 115 116def test_the_constants_equal_the_apps (self ): 117text = (ROOT / "apps" / "host" / "src" / "lib" / "native-fit.ts" ).read_text (encoding = "utf-8" ) 118m = re .search (r"const DENSE_GB = (\d+), SYSTEM_ROOM_GB = (\d+), DENSE_EXTRA_GB = (\d+)" ,text ) 119self .assertIsNotNone (m ) 120self .assertEqual ((G .DENSE_GB ,G .SYSTEM_ROOM_GB ,G .DENSE_EXTRA_GB ),tuple (float (x )for x in m .groups ())) 121 122def test_fit_matches_the_launch_catalog_port_of_native_fit (self ): 123from strata_cli .launch .catalog import fit_on 124for ram ,vram in ((62 ,16 ), (32 ,10 ), (16 ,0 ), (128 ,24 )): 125a = fit_on ({"memory_gb" :44.0 },ram ,vram ) 126b = G .fit (44.0 ,ram ,vram ) 127self .assertAlmostEqual (a ["spillGb" ],b ["spill_gb" ]) 128self .assertAlmostEqual (a ["vramUseGb" ],b ["vram_use_gb" ]) 129self .assertAlmostEqual (a ["ramUseGb" ],b ["ram_use_gb" ]) 130 131 132class GpuFitInTheModelList (unittest .TestCase ): 133"""The app reads the options from the model list (tools/strata_mcp.py model_table): the same rule, nothing duplicated in the UI.""" 134 135def setUp (self ): 136from tests .terminal .launch_support import Home 137self .home = Home ().__enter__ () 138self .addCleanup (self .home .__exit__ ,None ,None ,None ) 139 140def sizes (self ,hw ): 141from tests .terminal .launch_support import catalog 142table = catalog (hw ).strata .model_table (hw ) 143return {s ["model" ]:s for f in table for s in f ["sizes" ]} 144 145def test_two_cards_give_options_on_every_model (self ): 146from tests .terminal .launch_support import HW_2X16 147s = self .sizes (HW_2X16 )["IQ3_XXS" ] 148res = s ["gpu_options" ] 149self .assertEqual ([o ["key" ]for o in res ["options" ]][:2 ], ["single" ,"split" ]) 150self .assertEqual (res ["recommended" ],"single" ) 151 152def test_one_card_has_no_choice_to_show (self ): 153from tests .terminal .launch_support import HW_12 154self .assertIsNone (self .sizes (HW_12 )["IQ3_XXS" ]["gpu_options" ]) 155 156def test_an_integrated_chip_beside_one_card_is_no_choice (self ): 157from tests .terminal .launch_support import HW_2X16 158hw = dict (HW_2X16 ) 159hw ["gpus" ]= [dict (HW_2X16 ["gpus" ][0 ]), {"index" :1 ,"name" :"AMD Radeon Graphics" ,"vram_gb" :16.0 ,"usable" :False ,"uma" :True ,"problem" :"integrated graphics" }] 160self .assertIsNone (self .sizes (hw )["IQ3_XXS" ]["gpu_options" ]) 161 162 163if __name__ == "__main__" : 164unittest .main ()