okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c0380157d
main
1"""Tests for the owner's rule in setup.py - it recommends, it never forces: a choice bigger than the recommendation is 2kept and its risk said (#406's 256K on 64 GB, #364/#384's two GPUs in the low-RAM mode, #403's RAM budget), and the 3stops that stay are the ones a user can confirm (confirm_risk: --yes with an explicit flag, or y). The defaults are 4tools/test_setup_golden.py's. Mocked - no GPU, no downloads, nothing written outside a temp folder. 5 6python -m unittest tools.test_setup_risk 7""" 8from __future__import annotations 9 10import contextlib 11import io 12import itertools 13import json 14import os 15import sys 16import tempfile 17import unittest 18from pathlib import Path 19from unittest import mock 20 21ROOT = Path (__file__ ).resolve ().parents [1 ] 22sys .path .insert (0 ,str (ROOT )) 23sys .path .insert (0 ,str (ROOT / "tools" )) 24import setup # noqa: E402 25from test_setup_golden import PROFILES ,card ,install # noqa: E402 26 27 28def arg (cfg ,flag ): 29a = cfg ["args" ] 30return a [a .index (flag )+ 1 ]if flag in a else None 31 32 33def run (fn ,* args ,stdin = None ): 34"""-> (exit code or None, printed text, questions asked); stdin answers every input() (None: never asked).""" 35out ,asked ,code = io .StringIO (), [],None 36 37def fake_input (prompt = "" ): 38asked .append (prompt ) 39if stdin is None : 40raise AssertionError (f"asked { prompt !r } with --yes" ) 41return stdin 42 43with contextlib .redirect_stdout (out ),mock .patch ("builtins.input" ,fake_input ): 44try : 45fn (* args ) 46except SystemExit as e : 47code = e .code 48return code ,out .getvalue (),asked 49 50 51class ConfirmRisk (unittest .TestCase ): 52"""S6: one helper for the stops a user may take on.""" 53 54def go (self ,explicit ,yes ,stdin = None ): 55return run (setup .confirm_risk ,"it may not fit" ,explicit ,yes ,"it does not fit" ,"a smaller size" , 56stdin = stdin ) 57 58def test_yes_with_an_explicit_choice_is_consent (self ): 59code ,out ,asked = self .go (True ,True ) 60self .assertIsNone (code ) 61self .assertIn ("it may not fit" ,out ) 62self .assertEqual (asked , []) 63 64def test_yes_alone_keeps_the_stop (self ): 65code ,out ,asked = self .go (False ,True ) 66self .assertEqual (code ,1 ) 67self .assertIn ("it does not fit" ,out ) 68self .assertIn ("a smaller size" ,out ) 69 70def test_interactive_default_is_no (self ): 71for explicit in (True ,False ): 72code ,out ,asked = self .go (explicit ,False ,stdin = "" ) 73self .assertEqual (code ,1 ) 74self .assertIn ("[n]" ,asked [0 ]) 75self .assertIsNone (self .go (explicit ,False ,stdin = "y" )[0 ]) 76 77def test_paging_with_an_explicit_model (self ): 78code ,out ,asked = run (setup .confirm_paging ,"IQ3_XXS" ,54 ,"auto" ,True ,True ) 79self .assertIsNone (code ,out ) 80self .assertIn ("installing IQ3_XXS with 54 GB of RAM, as you chose (--model)" ,out ) 81 82 83class Context (unittest .TestCase ): 84"""S1 (#406 #364): the RAM rule recommends, an explicit or picked context is kept with a note.""" 85RAM64 ,GPU32 = PROFILES ["64GB-1x32GB" ] 86 87def test_the_ram_rule (self ): 88self .assertEqual (setup .ram_ctx ("IQ3_S" ,63.7 ),131072 ) 89self .assertEqual (setup .ram_ctx ("IQ3_XXS" ,63.7 ),131072 ) 90self .assertEqual (setup .ram_ctx ("IQ3_XXS" ,95.8 ),524288 )# 43 + 7.2 + 24 GB fits 96 91self .assertEqual (setup .ram_ctx ("IQ3_S" ,76.5 ),131072 ) 92self .assertEqual (setup .ram_ctx ("IQ3_S" ,78.5 ),262144 ) 93# #608: where the 200K estimate fits and the 256K one does not, the rule stays at 128K (200K is a menu step) 94self .assertLessEqual (setup .ctx_ram_need ("IQ3_S" ,204800 ),78.5 ) 95self .assertGreater (setup .ctx_ram_need ("IQ3_S" ,262144 ),77.5 ) 96self .assertEqual (setup .ram_ctx ("IQ3_S" ,77.5 ),131072 ) 97for ram in range (16 ,200 ): 98for model in ("IQ3_S" ,"IQ3_XXS" ,"Q2_0" ): 99self .assertNotEqual (setup .ram_ctx (model ,float (ram )),204800 ) 100self .assertEqual (setup .ram_ctx ("Q2_0" ,31.9 ),524288 )# other sizes: no RAM rule 101self .assertEqual (setup .ram_ctx ("IQ3_XXS" ,31.9 ,low_ram = True ),524288 )# the KV cache is in VRAM there 102self .assertIsNone (setup .ctx_ram_need ("IQ3_XXS" ,262144 ,low_ram = True )) 103self .assertAlmostEqual (setup .ctx_ram_need ("IQ3_S" ,262144 ),77.9 ,places = 1 ) 104 105def test_an_explicit_256k_is_kept (self ): 106code ,out ,cfg ,_ = install (self .RAM64 ,self .GPU32 , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" , 107"--context" ,"262144" ]) 108self .assertEqual (code ,0 ,out ) 109self .assertEqual (arg (cfg ,"--max-context" ),"262144" ) 110self .assertNotIn ("--rope-scaling" ,cfg ["args" ]) 111self .assertIn ("256K with IQ3_S needs ~78 GB of RAM by setup's estimate" ,out ) 112self .assertIn ("this PC has 64. Kept as you chose: it may be slower or run out of RAM under load. 128K is the " 113"recommended size." ,out ) 114 115def test_a_picked_256k_is_kept_and_the_menu_says_the_risk (self ): 116code ,out ,cfg ,asked = install (self .RAM64 ,self .GPU32 , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" ], 117answers = {"Context?" :"6" }) 118self .assertEqual (code ,0 ,out ) 119self .assertEqual (arg (cfg ,"--max-context" ),"262144" ) 120self .assertIn ("256K tokens (needs ~78 GB RAM, this PC has 64: may run out of memory)" ,out ) 121self .assertIn ("128K tokens (recommended for your GPU)\n" ,out ) 122self .assertIn ("Kept as you chose" ,out ) 123 124def test_a_200k_choice_is_kept (self ): 125"""#406: 200K was added between the 128K rule and 256K - it is inside the trained 262144, so no rope scaling, 126and a pick of it is kept with the note 256K gets instead of being capped to 128K.""" 127code ,out ,cfg ,_ = install (self .RAM64 ,self .GPU32 , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" ], 128answers = {"Context?" :"5" }) 129self .assertEqual (code ,0 ,out ) 130self .assertEqual (arg (cfg ,"--max-context" ),"204800" ) 131self .assertNotIn ("--rope-scaling" ,cfg ["args" ]) 132self .assertIn ("200K tokens (needs ~77 GB RAM, this PC has 64: may run out of memory)" ,out ) 133self .assertIn ("200K with IQ3_S needs ~77 GB of RAM by setup's estimate" ,out ) 134self .assertIn ("Kept as you chose" ,out ) 135 136def test_low_ram_mode_keeps_262k_without_a_ram_note (self ): 137ram ,found = PROFILES ["32GB-2x24GB" ]# #364 138code ,out ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"IQ3_XXS" ,"--no-start" , 139"--context" ,"262144" ]) 140self .assertEqual (code ,0 ,out ) 141self .assertEqual (arg (cfg ,"--max-context" ),"262144" ) 142self .assertNotIn ("Kept as you chose: it may be slower" ,out ) 143 144def test_the_earlier_install_keeps_its_context (self ): 145"""An update (a new copy of Strata) set up like the earlier install keeps its 256K instead of capping it.""" 146with tempfile .TemporaryDirectory ()as d : 147prev = Path (d )/ "strata-iq3_s.json" 148prev .write_text (json .dumps ({"args" : ["--max-context" ,"262144" ,"--kv" ,"int8" ],"port" :8080 })) 149started = mock .Mock (return_value = 0 ) 150code ,out ,cfg ,_ = install (self .RAM64 ,self .GPU32 , [],extra = [ 151mock .patch .object (setup ,"previous_config" ,lambda * a :prev ), 152mock .patch .object (setup ,"start" ,started )]) 153self .assertEqual (code ,0 ,out ) 154self .assertEqual (arg (cfg ,"--max-context" ),"262144" ) 155self .assertIn ("Kept as you chose" ,out ) 156self .assertTrue (started .called ) 157 158 159class BrokenEarlierConfig (unittest .TestCase ): 160"""#459: a new copy of Strata set up like an earlier install skips an earlier config that does not parse (an 161empty strata-*.json crashed START-HERE with JSONDecodeError) and goes on as a fresh install; configs are written 162whole (a temporary file moved over the old one).""" 163RAM64 ,GPU32 = PROFILES ["64GB-1x32GB" ] 164 165def setup_with (self ,files ): 166with tempfile .TemporaryDirectory ()as d : 167for i , (name ,text )in enumerate (files ):# in order, oldest first 168 (Path (d )/ name ).write_text (text ,encoding = "utf-8" ) 169os .utime (Path (d )/ name , (1_700_000_000 + i ,1_700_000_000 + i )) 170return install (self .RAM64 ,self .GPU32 , [],extra = [ 171mock .patch .object (setup ,"other_installs" ,lambda settings : [Path (d )]), 172mock .patch .object (setup ,"start" ,mock .Mock (return_value = 0 ))]) 173 174def test_an_empty_config_is_skipped (self ): 175for text ,why in (("" ,"the file is empty" ), ("{\"args\": [" ,"not valid JSON" ), ("[1]" ,"not a JSON object" )): 176with self .subTest (text = text ): 177code ,out ,cfg ,_ = self .setup_with ([("strata-iq3_s.json" ,text )]) 178self .assertEqual (code ,0 ,out ) 179self .assertIn ("skipped the earlier config " ,out ) 180self .assertIn (f"strata-iq3_s.json ( { why } ): setting this copy up without it" ,out ) 181self .assertNotIn ("Found your earlier install" ,out ) 182self .assertIsNotNone (cfg )# the fresh install's config 183 184def test_the_newest_readable_config_is_used (self ): 185good = json .dumps ({"args" : ["--max-context" ,"262144" ,"--kv" ,"int8" ],"port" :8080 }) 186code ,out ,cfg ,_ = self .setup_with ([("strata-iq3_s.json" ,good ), ("strata-q2_0.json" ,"" )]) 187self .assertEqual (code ,0 ,out ) 188self .assertIn ("strata-q2_0.json (the file is empty)" ,out ) 189self .assertIn ("Found your earlier install" ,out ) 190self .assertIn ("(iq3_s)" ,out ) 191self .assertEqual (arg (cfg ,"--max-context" ),"262144" ) 192 193def test_write_config_leaves_no_partial_file (self ): 194with tempfile .TemporaryDirectory ()as d : 195p = Path (d )/ "strata-iq3_s.json" 196p .write_text ("old" ,encoding = "utf-8" ) 197setup .write_config (p , {"args" : ["--kv" ,"int8" ]}) 198self .assertEqual (p .read_text (encoding = "utf-8" ),json .dumps ({"args" : ["--kv" ,"int8" ]},indent = 1 )) 199self .assertEqual ([f .name for f in Path (d ).iterdir ()], ["strata-iq3_s.json" ])# no .tmp left 200with mock .patch .object (Path ,"write_text" ,side_effect = OSError (28 ,"No space left on device" )): 201with self .assertRaises (OSError ): 202setup .write_config (p , {"args" : []}) 203self .assertEqual (json .loads (p .read_text (encoding = "utf-8" )), {"args" : ["--kv" ,"int8" ]})# kept whole 204 205 206class LowRamGpus (unittest .TestCase ): 207"""S2/S3 (#364 #384): one GPU recommended in the low-RAM mode, all of them when asked for.""" 208 209def setUp (self ): 210# the behaviour before RESIDENT_SPLIT_ENGINE (an engine from before 0.1.40); the tests of the newer engine 211# patch MIN_ENGINE themselves, and the default (MIN_ENGINE = 0.1.40) is covered by test_setup_golden 212p = mock .patch .object (setup ,"MIN_ENGINE" , (0 ,1 ,39 )) 213p .start () 214self .addCleanup (p .stop ) 215 216def test_explicit_gpus_are_kept_with_the_mapped_variant (self ): 217for prof ,model in (("32GB-2x24GB" ,"IQ3_XXS" ), ("32GB-2x24GB" ,"Q2_0" ), ("47GB-2x16GB" ,"IQ3_XXS" )): 218with self .subTest (prof = prof ,model = model ): 219ram ,found = PROFILES [prof ] 220code ,out ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--model" ,model ,"--no-start" , 221"--gpus" ,"0,1" ]) 222self .assertEqual (code ,0 ,out ) 223self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 224self .assertEqual (cfg ["layer_split" ],"auto" ) 225self .assertIn ("--mmap-experts" ,cfg ["args" ]) 226self .assertNotIn ("--resident-experts" ,cfg ["args" ]) 227self .assertIn ("as you chose (--gpus)" ,out ) 228self .assertIn ("RAM can fill up to 0 free" ,out ) 229self .assertIn ("low-RAM mode on 2 GPUs" ,out ) 230 231def test_the_share_counts_every_card (self ): 232ram ,found = PROFILES ["47GB-2x16GB" ]# #384: 2 x 16 GB, 47 GB, IQ3_XXS at 64K 233code ,out ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"IQ3_XXS" ,"--no-start" , 234"--gpus" ,"0,1" ]) 235one = setup .low_ram_gpu_gb ("IQ3_XXS" ,15.9 ,131072 ,"int8" )# the 128K default 236self .assertIn (f"the GPUs hold ~ { 100 * 2 * one / setup . MODELS [ 'IQ3_XXS' ][ 'arena_gb' ]:.0f } % of them" ,out ) 237 238def test_asked_and_answered_two (self ): 239ram ,found = PROFILES ["32GB-2x24GB" ] 240code ,out ,cfg ,asked = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ], 241answers = {"Low-RAM mode: which GPUs?" :"2" })# the roles are the default (Main last); the low-RAM question still defaults to one card 242self .assertEqual (code ,0 ,out ) 243self .assertTrue (any ("Low-RAM mode: which GPUs? [1]" in q for q in asked ),asked ) 244self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 245self .assertIn ("--mmap-experts" ,cfg ["args" ]) 246self .assertIn ("1) GPU 0 (NVIDIA GeForce RTX 3090, 24 GB) only" ,out ) 247 248def test_yes_and_enter_keep_one_gpu (self ): 249ram ,found = PROFILES ["32GB-2x24GB" ] 250for answers in (None ,"" ): 251code ,out ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ], 252answers = answers ) 253self .assertEqual (code ,0 ,out ) 254self .assertEqual (cfg ["gpu" ],0 ) 255self .assertIn ("--resident-experts" ,cfg ["args" ]) 256 257def test_low_ram_resident_with_gpus (self ): 258ram ,found = PROFILES ["32GB-2x24GB" ] 259code ,out ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,"--gpus" ,"0,1" , 260"--low-ram" ,"resident" ]) 261self .assertEqual (code ,0 ,out ) 262self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 263self .assertIn ("--mmap-experts" ,cfg ["args" ]) 264self .assertIn ("--low-ram resident has no layer split yet" ,out ) 265 266def test_low_ram_resident_alone_keeps_one_gpu_without_asking (self ): 267ram ,found = PROFILES ["32GB-2x24GB" ] 268code ,out ,cfg ,asked = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" , 269"--low-ram" ,"resident" ],answers = "" ) 270self .assertEqual (code ,0 ,out ) 271self .assertEqual (cfg ["gpu" ],0 ) 272self .assertFalse (any ("Low-RAM mode" in q for q in asked ),asked ) 273 274def test_low_ram_on_gpus_with_a_resident_split_engine (self ): 275"""#642: an engine that runs the resident variant on a layer split keeps the cards together, resident.""" 276ram ,found = PROFILES ["32GB-2x24GB" ] 277with mock .patch .object (setup ,"MIN_ENGINE" ,setup .RESIDENT_SPLIT_ENGINE ): 278code ,out ,cfg ,asked = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ], 279answers = {"Use these roles?" :"y" })# the recommended roles: Main 0 (last), Helper 1 280self .assertEqual (code ,0 ,out ) 281self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 282self .assertIn ("--resident-experts" ,cfg ["args" ]) 283self .assertFalse (any ("Low-RAM mode" in q for q in asked ),asked ) 284self .assertIn ("stay in RAM" ,out ) 285code ,out ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" , 286"--gpus" ,"0,1" ,"--low-ram" ,"resident" ]) 287self .assertEqual (code ,0 ,out ) 288self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 289self .assertIn ("--resident-experts" ,cfg ["args" ]) 290self .assertNotIn ("no layer split yet" ,out ) 291 292 293class StartOnSeveralGpus (unittest .TestCase ): 294"""A resident low-RAM config started on several GPUs reads the experts through the file cache (#364 #384).""" 295 296def setUp (self ): 297# the behaviour before RESIDENT_SPLIT_ENGINE (an engine from before 0.1.40); the tests of the newer engine 298# patch MIN_ENGINE themselves, and the default (MIN_ENGINE = 0.1.40) is covered by test_setup_golden 299p = mock .patch .object (setup ,"MIN_ENGINE" , (0 ,1 ,39 )) 300p .start () 301self .addCleanup (p .stop ) 302 303def test_split_mmap (self ): 304cfg = {"args" : ["--pack" ,"p" ,"--resident-experts" ,"--kv" ,"int8" ]} 305out = io .StringIO () 306with contextlib .redirect_stdout (out ): 307self .assertTrue (setup .split_mmap (cfg )) 308self .assertFalse (setup .split_mmap (cfg )) 309self .assertFalse (setup .split_mmap ({"args" : ["--mmap-experts" ]})) 310self .assertEqual (cfg ["args" ], ["--pack" ,"p" ,"--mmap-experts" ,"--kv" ,"int8" ]) 311self .assertIn ("no layer split yet" ,out .getvalue ()) 312 313def offer (self ,args ,stdin ): 314found = PROFILES ["32GB-2x24GB" ][1 ] 315with tempfile .TemporaryDirectory ()as d : 316p = Path (d )/ "strata-q2_0.json" 317cfg = {"args" :list (args )} 318with mock .patch .object (setup ,"gpus" ,lambda :found ), \ 319mock .patch .object (setup ,"engine_runs_on" ,lambda g :True ): 320code ,out ,asked = run (setup .offer_together ,p ,cfg ,stdin is None ,stdin = stdin ) 321return code ,out ,asked ,json .loads (p .read_text ()) 322 323def test_offer_together_recommends_one_gpu_for_a_resident_config (self ): 324code ,out ,asked ,cfg = self .offer (["--resident-experts" ],None )# --yes: the recommendation 325self .assertIsNone (code ,out ) 326self .assertNotIn ("gpu" ,cfg ) 327self .assertEqual (cfg ["args" ], ["--resident-experts" ]) 328code ,out ,asked ,cfg = self .offer (["--resident-experts" ],"y" ) 329self .assertIn ("[n]" ,asked [0 ]) 330self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 331self .assertEqual (cfg ["args" ], ["--mmap-experts" ])# #1447: no helper cache, no --remote-expert-opt 332code ,out ,asked ,cfg = self .offer (["--mmap-experts" ],None )# other configs: as before 333self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 334 335def test_resident_split_engine_keeps_a_resident_config (self ): 336"""#642: from RESIDENT_SPLIT_ENGINE a resident config is offered both cards as any other, and stays resident.""" 337with mock .patch .object (setup ,"MIN_ENGINE" ,setup .RESIDENT_SPLIT_ENGINE ): 338cfg = {"args" : ["--pack" ,"p" ,"--resident-experts" ,"--kv" ,"int8" ]} 339self .assertFalse (setup .split_mmap (cfg )) 340self .assertEqual (cfg ["args" ], ["--pack" ,"p" ,"--resident-experts" ,"--kv" ,"int8" ]) 341code ,out ,asked ,cfg = self .offer (["--resident-experts" ],None )# --yes: the recommendation 342self .assertIsNone (code ,out ) 343self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 344self .assertEqual (cfg ["args" ], ["--resident-experts" ]) 345self .assertNotIn ("OS file cache" ,out ) 346 347def test_start_with_gpus (self ): 348found = PROFILES ["32GB-2x24GB" ][1 ] 349with tempfile .TemporaryDirectory ()as d : 350exe = Path (d )/ "strata.exe" 351exe .write_bytes (b"" ) 352p = Path (d )/ "strata-q2_0.json" 353p .write_text (json .dumps ({"exe" :str (exe ),"args" : ["--resident-experts" ],"gpu" :0 ,"gpus_asked" :True })) 354call = mock .Mock (return_value = 0 ) 355with mock .patch .object (setup ,"gpus" ,lambda :found ), \ 356mock .patch .object (setup ,"ensure_engine_for" ,lambda cards ,path ,cfg ,yes :cfg ), \ 357mock .patch .object (setup ,"is_wsl" ,lambda :False ), \ 358mock .patch .object (setup .subprocess ,"call" ,call ): 359code ,out ,_ = run (setup .start ,p ,None , [0 ,1 ],False ,True ) 360cfg = json .loads (p .read_text ()) 361self .assertIsNone (code ,out ) 362self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 363self .assertEqual (cfg ["args" ], ["--mmap-experts" ])# #1447: no helper cache, no --remote-expert-opt 364self .assertIn ("no layer split yet" ,out ) 365self .assertTrue (call .called ) 366 367 368class SmallCard (unittest .TestCase ): 369"""S9: a card under 8 GB named with --gpus is a risk to confirm, not a stop.""" 370FOUND = [card (0 ,"NVIDIA GeForce RTX 3090" ,24.0 ,"86" ),card (1 ,"NVIDIA GeForce RTX 2060" ,6.0 ,"75" )] 371 372def test_named_with_yes (self ): 373code ,out ,cfg ,_ = install (63.7 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" , 374"--gpus" ,"0,1" ]) 375self .assertEqual (code ,0 ,out ) 376self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 377self .assertIn ("GPU 1 (NVIDIA GeForce RTX 2060) has 6 GB of VRAM" ,out ) 378self .assertIn ("is used together with the others, as you chose" ,out ) 379 380def test_named_and_enter_stops (self ): 381code ,out ,cfg ,asked = install (63.7 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" , 382"--gpus" ,"0,1" ],answers = "" ) 383self .assertEqual (code ,1 ) 384self .assertTrue (any ("Use it anyway?" in q for q in asked ),asked ) 385 386def test_not_named_is_not_offered (self ): 387code ,out ,cfg ,_ = install (63.7 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ]) 388self .assertEqual (code ,0 ,out ) 389self .assertEqual (cfg ["gpu" ],0 )# one card: as before 390 391 392class SplitShortCard (unittest .TestCase ): 393"""#448: a second card too small to lend a split's prompt chunk: the first card alone recommended, the pair kept 394when named.""" 395FOUND = [card (0 ,"NVIDIA RTX PRO 4500 Blackwell" ,31.8 ,"120" ),card (1 ,"NVIDIA GeForce RTX 3080" ,10.0 ,"86" )] 396 397def test_rule (self ): 398self .assertEqual ([g ["index" ]for g in setup .split_short (self .FOUND )], [1 ]) 399self .assertEqual (setup .split_short (PROFILES ["32GB-2x24GB" ][1 ]), []) 400self .assertEqual (setup .split_short (PROFILES ["47GB-2x16GB" ][1 ]), []) 401eights = [card (i ,"NVIDIA GeForce RTX 3070" ,8.0 ,"86" )for i in range (2 )] 402self .assertEqual (setup .split_short (eights ), [])# neither card alone reads more 403 404def test_yes_takes_the_recommended_roles (self ): 405"""The 10 GB card is above the 8 GB line, so it starts as a Helper (the owner's rule); the first card is Main and goes last.""" 406code ,out ,cfg ,_ = install (127.8 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ]) 407self .assertEqual (code ,0 ,out ) 408self .assertEqual (cfg ["gpu" ], [1 ,0 ]) 409self .assertEqual (cfg ["gpu_roles" ], {"0" :"main" ,"1" :"helper" }) 410 411def test_named_pair_is_kept (self ): 412code ,out ,cfg ,_ = install (127.8 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" , 413"--gpus" ,"0,1" ]) 414self .assertEqual (code ,0 ,out ) 415self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 416 417def test_offer_together_defaults_to_one (self ): 418with tempfile .TemporaryDirectory ()as d : 419p = Path (d )/ "strata-q2_0.json" 420with mock .patch .object (setup ,"gpus" ,lambda :self .FOUND ), \ 421mock .patch .object (setup ,"engine_runs_on" ,lambda g :True ): 422code ,out ,asked = run (setup .offer_together ,p , {"args" : ["--mmap-experts" ]},True ) 423cfg = json .loads (p .read_text ()) 424self .assertIsNone (code ,out ) 425self .assertNotIn ("gpu" ,cfg ) 426self .assertIn ("#448" ,out ) 427 428 429class RamFloor (unittest .TestCase ): 430"""S7: less RAM than the smallest model needs: a stop with --yes alone, a risk with --model.""" 431FOUND = [card (0 ,"NVIDIA GeForce RTX 3060" ,8.0 ,"86" )] 432 433def test_yes_alone_stops (self ): 434code ,out ,cfg ,_ = install (19.9 ,self .FOUND , ["--family" ,"qwen" ,"--no-start" ]) 435self .assertEqual (code ,1 ) 436self .assertIn ("--model NAME --yes" ,out ) 437 438def test_an_explicit_model_goes_on (self ): 439code ,out ,cfg ,_ = install (19.9 ,self .FOUND , ["--family" ,"coder" ,"--model" ,"IQ1_M" ,"--no-start" ]) 440self .assertEqual (code ,0 ,out ) 441self .assertIn ("going on with 20 GB of RAM, as you chose" ,out ) 442 443 444class KvStreaming (unittest .TestCase ): 445"""8: --kv-streaming on|off|auto (auto: the RAM test, as before).""" 446RAM ,FOUND = PROFILES ["64GB-1x32GB" ] 447 448def go (self ,model ,* flags ): 449return install (self .RAM ,self .FOUND , ["--family" ,"qwen" ,"--model" ,model ,"--no-start" ,* flags ]) 450 451def test_off (self ): 452code ,out ,cfg ,_ = self .go ("IQ3_XXS" ,"--kv-streaming" ,"off" ) 453self .assertEqual (code ,0 ,out ) 454self .assertNotIn ("--kv-resident" ,cfg ["args" ]) 455self .assertIn ("KV streaming off, as you chose" ,out ) 456 457def test_on_past_the_ram_test (self ): 458# auto leaves it off on 58 GB: IQ3_S needs 50.3 (experts) + 10 (room) + 1.8 (the cache) = 62.1 GB 459code ,out ,cfg ,_ = install (58.0 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" ,"--kv-streaming" ,"on" ]) 460self .assertEqual (code ,0 ,out ) 461self .assertEqual (arg (cfg ,"--kv-resident" ),"32768" ) 462self .assertIn ("Kept as you chose (--kv-streaming on)" ,out ) 463self .assertIn ("KV streaming needs ~62 GB of RAM for IQ3_S" ,out ) 464 465def test_auto_streams_a_64gb_pc_with_few_experts_in_vram (self ): 466"""KV-128: the owner's PC reads 63.1 GB; the all-in-RAM rule asked 64.8 and left the cache in VRAM.""" 467card5070 = [card (0 ,"NVIDIA GeForce RTX 5070" ,11.9 ,"120" )] 468code ,out ,cfg ,_ = install (63.1 ,card5070 , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" ,"--context" ,"131072" ]) 469self .assertEqual (code ,0 ,out ) 470self .assertEqual (arg (cfg ,"--kv-resident" ),"32768" ) 471self .assertIn ("KV streaming on: the context's KV cache lives in RAM" ,out ) 472self .assertNotIn ("Kept as you chose" ,out ) 473 474def test_auto_stays_off_just_below_the_rule (self ): 475code ,out ,cfg ,_ = install (61.9 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" ,"--context" ,"131072" ]) 476self .assertEqual (code ,0 ,out ) 477self .assertNotIn ("--kv-resident" ,cfg ["args" ]) 478self .assertIn ("KV streaming off: it needs ~62 GB of RAM for IQ3_S" ,out ) 479 480def test_below_64k_is_never_streamed_on_auto (self ): 481code ,out ,cfg ,_ = install (63.1 ,self .FOUND , ["--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--no-start" ,"--context" ,"32768" ]) 482self .assertEqual (code ,0 ,out ) 483self .assertNotIn ("--kv-resident" ,cfg ["args" ]) 484 485def test_k8v4_streams (self ): 486code ,out ,cfg ,_ = self .go ("Q2_0" ,"--kv-streaming" ,"on" ,"--kv" ,"k8v4" ) 487self .assertEqual (code ,0 ,out ) 488self .assertEqual (arg (cfg ,"--kv-resident" ),"32768" ) 489self .assertEqual (arg (cfg ,"--kv" ),"k8v4" ) 490 491def test_on_where_it_cannot (self ): 492code ,out ,cfg ,_ = self .go ("Q2_0" ,"--kv-streaming" ,"on" ,"--context" ,"32768" ) 493self .assertNotIn ("--kv-resident" ,cfg ["args" ]) 494self .assertIn ("a context under 64K is not streamed" ,out ) 495 496def test_budget_flag_on_another_size (self ): 497code ,out ,cfg ,_ = self .go ("Q2_0" ,"--resident-budget-gib" ,"30" ) 498self .assertEqual (code ,0 ,out ) 499self .assertNotIn ("--resident-budget-gib" ,cfg ["args" ]) 500self .assertIn ("--resident-budget-gib is for UD-Q4_K_XL" ,out ) 501 502 503class KvStreamingNeed (unittest .TestCase ): 504"""KV-128: the RAM setup asks before it streams the KV cache by itself (`kv_streaming_need_gb`).""" 505 506def test_the_numbers (self ): 507need = lambda m ,kv = "int8" ,ctx = 131072 :round (setup .kv_streaming_need_gb (m ,ctx ,kv ),1 ) 508self .assertEqual (need ("IQ3_S" ),62.1 )# 50.3 + 10 + 1.8; the all-in-RAM rule said 62 + 1.7 + 1 = 64.7 509self .assertEqual (need ("IQ3_XXS" ),54.7 )# 42.9 + 10 + 1.8 510self .assertEqual (need ("Q2_0" ),45.8 )# 34 + 10 + 1.8 511self .assertEqual (need ("IQ3_S" ,"q4_0" ),61.3 ) 512 513def test_never_asks_more_than_before (self ): 514for model ,ctx ,kv in itertools .product (setup .MODELS , (65536 ,131072 ,262144 ), ("int8" ,"q4_0" ,"k8v4" )): 515before = setup .MODELS [model ]["ram_gb" ]+ setup .kv_streaming_ram_gb (ctx ,kv )+ 1 516self .assertLessEqual (setup .kv_streaming_need_gb (model ,ctx ,kv ),before , (model ,ctx ,kv )) 517 518@ mock . patch . object ( setup , "is_wsl" , lambda : False ) 519def test_the_rule (self ): 520want = lambda ram ,ctx = 131072 ,choice = "auto" :setup .kv_streaming_wanted ("IQ3_S" ,ctx ,"int8" ,ram ,choice ) 521self .assertTrue (want (63.1 )) 522self .assertTrue (want (62.1 )) 523self .assertFalse (want (62.0 )) 524self .assertFalse (want (31.9 ))# a PC in the low-RAM mode keeps the KV in VRAM 525self .assertFalse (want (63.1 ,32768 )) 526self .assertFalse (want (63.1 ,choice = "off" )) 527self .assertTrue (want (31.9 ,choice = "on" )) 528 529@ mock . patch . object ( setup , "is_wsl" , lambda : True ) 530def test_wsl_never_streams (self ): 531self .assertFalse (setup .kv_streaming_wanted ("IQ3_S" ,131072 ,"int8" ,128.0 )) 532 533 534class RulesAgreeWithTheWrittenConfig (unittest .TestCase ): 535"""The launcher's preset diff asks setup's rules (`kv_streaming_wanted`, `low_ram_wanted`) what "auto" decides on 536this PC, instead of comparing the word "auto" with a config's flags. That is only honest if those rules are 537exactly what setup writes into the model's config: the same PC, the same flags, every size and every 538--kv-streaming / --low-ram choice.""" 539RAM64 ,CARDS64 = PROFILES ["64GB-1x32GB" ] 540RAM32 ,CARDS32 = PROFILES ["32GB-2x24GB" ] 541 542def go (self ,ram ,cards ,model ,* flags ): 543return install (ram ,cards , ["--family" ,"qwen" ,"--model" ,model ,"--no-start" ,* flags ]) 544 545@ mock . patch . object ( setup , "is_wsl" , lambda : False ) # #974: not WSL, as in install() 546def test_the_streaming_rule_is_the_flag_setup_writes (self ): 547for model ,ctx ,kv ,choice in itertools .product (("Q2_0" ,"IQ3_XXS" ,"IQ3_S" ), ("32768" ,"131072" ), 548 ("int8" ,"q4_0" ,"k8v4" ), ("auto" ,"on" ,"off" )): 549code ,out ,cfg ,_ = self .go (self .RAM64 ,self .CARDS64 ,model ,"--context" ,ctx ,"--kv" ,kv , 550"--kv-streaming" ,choice ) 551self .assertEqual (code ,0 ,out ) 552self .assertEqual ("--kv-resident" in cfg ["args" ], 553setup .kv_streaming_wanted (model ,int (ctx ),kv ,self .RAM64 ,choice ), 554f" { model } at { ctx } with { kv } , --kv-streaming { choice } : setup wrote { cfg [ 'args' ] } " ) 555 556def test_the_low_ram_rule_is_the_flag_setup_writes (self ): 557pcs = [(self .RAM64 ,self .CARDS64 ), (self .RAM32 ,self .CARDS32 )]# auto is on here, off there 558for (ram ,cards ),model ,choice in itertools .product (pcs , ("Q2_0" ,"IQ3_XXS" ,"IQ3_S" ), 559 ("auto" ,"on" ,"off" ,"mmap" )): 560code ,out ,cfg ,_ = self .go (ram ,cards ,model ,"--low-ram" ,choice ) 561self .assertEqual (code ,0 ,out ) 562low = "--resident-experts" in cfg ["args" ]or "--mmap-experts" in cfg ["args" ] 563self .assertEqual (low ,setup .low_ram_wanted (model ,ram ,choice ), 564f" { model } on { ram :.0f } GB of RAM, --low-ram { choice } : setup wrote { cfg [ 'args' ] } " ) 565 566 567if __name__ == "__main__" : 568unittest .main ()