okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""strata_control/vram_fit.py: the fit math with mock card sizes, and its use in setup (sycl/setup_intel.py) and strata launch.""" 2 3from __future__import annotations 4 5import importlib .util 6import sys 7import unittest 8from pathlib import Path 9 10ROOT = Path (__file__ ).resolve ().parents [2 ] 11sys .path .insert (0 ,str (ROOT )) 12 13from strata_control import vram_fit as V # noqa: E402 14 15 16class Measured (unittest .TestCase ): 17"""The estimate against the 12 starts on the Arc A750 (8 GB): engine slots / MiB short, from /data/022-arcgyro/out/fa_*.txt.""" 18 19def slack (self ,ctx ,reserve ,prefill ): 20return V .slack_mib ("Gyro-M" ,8.0 ,ctx ,"int8" ,reserve ,prefill ,"intel" ) 21 22def test_the_session_matches_the_engines_three_numbers (self ): 23for ctx ,mib in ((8192 ,234 ), (16384 ,347 ), (32768 ,573 )): 24self .assertAlmostEqual (V .session_mib (ctx ),mib ,delta = 8 ) 25 26def test_the_default_does_not_start_and_the_estimate_says_how_short (self ): 27self .assertAlmostEqual (self .slack (32768 ,300 ,"auto" ),- 747 ,delta = 40 )# engine: about 748 MiB more 28self .assertAlmostEqual (self .slack (16384 ,300 ,"auto" ),- 497 ,delta = 40 )# 497 29self .assertAlmostEqual (self .slack (8192 ,100 ,"auto" ),- 171 ,delta = 40 )# 171 30 31def test_the_known_good_start_has_room_for_its_slots (self ): 32self .assertAlmostEqual (self .slack (8192 ,100 ,1024 ),412 * V .SLOT_MIB ,delta = 60 )# 412 slots 33self .assertAlmostEqual (self .slack (32768 ,100 ,1024 ),107 * V .SLOT_MIB ,delta = 60 )# 107 slots 34self .assertAlmostEqual (self .slack (8192 ,100 ,512 ),683 * V .SLOT_MIB ,delta = 60 )# 683 slots 35self .assertAlmostEqual (self .slack (8192 ,100 ,256 ),820 * V .SLOT_MIB ,delta = 60 )# 820 slots 36 37def test_gyro_s_default_fits_as_it_did (self ): 38self .assertTrue (V .fits ("Gyro-S" ,8.0 ,32768 ,"int8" ,300 ,"auto" ,"intel" ))# 410 slots measured 39 40 41class Plan (unittest .TestCase ): 42"""The rule (owner, 2026-11-02): a fit never changes the context or the number of requests; it moves work off the GPU, and says so.""" 43 44def test_gyro_m_on_8gb_reads_prompts_in_smaller_chunks_and_keeps_its_context (self ): 45f = V .plan ("Gyro-M" ,8.0 ,32768 ,"int8" ,300 ,"auto" ,"intel" ,title = "Gyro-M" ) 46self .assertTrue (f .changed and f .fits ) 47self .assertEqual ((f .ctx ,f .reserve_mib ,f .prefill ), (32768 ,300 ,"512" ))# 32K stays; the chunk is the cheap knob, the reserve is not touched 48self .assertTrue (f .line .startswith ("Gyro-M on this 8 GB card: " + V .NOTE_SLOW ),f .line ) 49self .assertGreaterEqual (f .slack_mib ,V .needed_mib ()) 50 51def test_the_context_is_never_changed_for_any_card (self ): 52for model in ("Gyro-M" ,"Gyro-S" ,"Qwen3.8-Flash-Next-IQ3_S" ): 53for vendor in ("intel" ,"cuda" ,"other" ): 54for gb in (5.0 ,6.0 ,8.0 ,9.0 ,10.0 ,12.0 ,16.0 ,24.0 ,48.0 ): 55for ctx in (8192 ,32768 ,131072 ,262144 ): 56for slots in (0 ,2 ,4 ): 57f = V .plan (model ,gb ,ctx ,"int8" ,700 ,"auto" ,vendor ,slots = slots ,kv_resident = 32768 if ctx >= 65536 else 0 ) 58self .assertEqual (f .ctx ,ctx , (model ,vendor ,gb ,ctx ,slots )) 59self .assertEqual (f .slots ,slots , (model ,vendor ,gb ,ctx ,slots )) 60 61def test_a_chosen_reserve_is_kept (self ): 62f = V .plan ("Gyro-M" ,8.0 ,32768 ,"int8" ,500 ,"auto" ,"intel" ,reserve_fixed = True ) 63self .assertEqual (f .reserve_mib ,500 ) 64 65def test_a_chosen_chunk_is_kept (self ): 66f = V .plan ("Gyro-M" ,8.0 ,32768 ,"int8" ,300 ,"2048" ,"intel" ,prefill_fixed = True ) 67self .assertIsNone (f .prefill ) 68 69def test_when_nothing_more_can_move_the_start_goes_as_far_as_it_can_and_says_so (self ): 70f = V .plan ("Gyro-M" ,6.0 ,32768 ,"int8" ,300 ,"auto" ,"intel" ) 71self .assertEqual (f .ctx ,32768 ) 72self .assertEqual ((f .prefill ,f .reserve_mib ), ("256" ,100 ))# every knob to its end 73self .assertFalse (f .fits ) 74self .assertIn (V .NOTE_SLOW ,f .line ) 75 76def test_a_config_that_fits_comes_back_unchanged (self ): 77for model ,gb ,ctx in (("Gyro-M" ,12.0 ,32768 ), ("Gyro-M" ,16.0 ,65536 ), ("Gyro-S" ,8.0 ,32768 ), ("Gyro-S" ,12.0 ,131072 ), 78 ("Qwen3.8-Flash-Next-IQ3_XXS" ,8.0 ,32768 ), ("Qwen3.8-Flash-Next-IQ3_XXS" ,12.0 ,131072 ), 79 ("Qwen3.8-Flash-Next-IQ3_XXS" ,16.0 ,262144 ), ("Qwen3.8-Flash-Next-IQ2_XS" ,24.0 ,262144 )): 80f = V .plan (model ,gb ,ctx ,"int8" ,300 ,"auto" ,"nvidia" ) 81self .assertEqual ((f .ctx ,f .reserve_mib ,f .prefill ,f .changed ,f .line ), (ctx ,300 ,None ,False ,"" ), (model ,gb ,ctx )) 82 83def test_unknown_card_size_and_tiny_cards_are_left_alone (self ): 84for gb in (0 ,None ,4.0 ): 85f = V .plan ("Gyro-M" ,gb ,32768 ,"int8" ,300 ,"auto" ,"intel" ) 86self .assertFalse (f .changed ) 87 88def test_fit_context_is_the_context_asked_for (self ): 89for ctx in (8192 ,32768 ,131072 ): 90self .assertEqual (V .fit_context ("Gyro-M" ,8.0 ,ctx ),ctx ) 91 92def test_generic_model_is_found_short_only_when_far_short (self ): 93self .assertFalse (V .plan ("Qwen3.8-Flash-Next-IQ3_XXS" ,8.0 ,32768 ,"int8" ,300 ,"auto" ,"intel" ).changed ) 94self .assertTrue (V .plan ("Qwen3.8-Flash-Next-IQ3_XXS" ,6.0 ,131072 ,"int8" ,300 ,"auto" ,"intel" ).changed ) 95 96 97class SetupIntel (unittest .TestCase ): 98@staticmethod 99def mod (): 100spec = importlib .util .spec_from_file_location ("setup_intel_fit" ,ROOT / "sycl" / "setup_intel.py" ) 101m = importlib .util .module_from_spec (spec ) 102spec .loader .exec_module (m ) 103return m 104 105def cfg (self ,m ,native = "Gyro-M-TQ2_0.gguf" ,extra = ()): 106base = m .MOUNT 107return {"exe" :"x" ,"cwd" :"y" ,"args" : ["--pack" ,str (base / "d" / "pack" ),"--native" ,str (base / "d" / native ), 108"--expert-cache" ,"auto" ,"--prefill" ,"auto" ,"--max-context" ,"32768" , 109"--kv" ,"int8" ,"--vram-reserve-mib" ,"300" ,* extra ]} 110 111def val (self ,c ,flag ): 112return c ["args" ][c ["args" ].index (flag )+ 1 ] 113 114def run_it (self ,m ,cfg ,vram ,drv = "i915" ,** kw ): 115notes = [] 116c = m .to_sycl (cfg ,m .ROOT / "build-sycl" / "strata" ,64.0 , {},vram ,drv ,"56a1" ,fit_notes = notes ,** kw ) 117return c ,notes 118 119def test_gyro_m_on_the_a750_gets_a_config_that_starts_and_one_line (self ): 120m = self .mod () 121c ,notes = self .run_it (m ,self .cfg (m ),8.0 ,auto_reserve = True ) 122self .assertEqual (self .val (c ,"--max-context" ),"32768" )# the context is the person's: it stays 123self .assertEqual (self .val (c ,"--vram-reserve-mib" ),"300" ) 124self .assertEqual (self .val (c ,"--prefill" ),"512" ) 125self .assertEqual (c ["args" ].count ("--vram-reserve-mib" ),1 ) 126self .assertEqual (len (notes ),1 ) 127self .assertIn ("Gyro-M on this 8 GB card: short of GPU memory" ,notes [0 ]) 128 129def test_a_reserve_the_user_asked_for_is_kept (self ): 130m = self .mod () 131c ,notes = self .run_it (m ,self .cfg (m ),8.0 ,auto_reserve = False ) 132self .assertEqual (self .val (c ,"--vram-reserve-mib" ),"300" ) 133self .assertEqual (self .val (c ,"--max-context" ),"32768" ) 134 135def test_a_context_the_user_asked_for_is_kept (self ): 136m = self .mod () 137c ,notes = self .run_it (m ,self .cfg (m ),8.0 ,auto_reserve = True ,ctx_asked = True ) 138self .assertEqual (self .val (c ,"--max-context" ),"32768" ) 139 140def test_configs_that_fit_are_byte_identical (self ): 141m = self .mod () 142for vram ,drv in ((8.0 ,"i915" ), (12.0 ,"xe" ), (16.0 ,"i915" ), (24.0 ,"xe" ), (32.0 ,"xe" )): 143for native in ("a.gguf" ,"Gyro-S-TQ1_0.gguf" ): 144off ,_ = self .run_it (m ,self .cfg (m ,native ),vram ,drv ,fit = False ) 145on ,notes = self .run_it (m ,self .cfg (m ,native ),vram ,drv ,auto_reserve = True ) 146self .assertEqual (off ,on , (vram ,drv ,native )) 147self .assertEqual (notes , []) 148 149 150FN = "qwen3.8-flash-next-iq3_xxs" 151 152 153class SlotSessions (unittest .TestCase ): 154"""Request slots ("Concurrent requests" 2 or more, the engine's --batch N): each slot is a session of its own, out of the expert cache's room. 155The 8 GB Arc A750 figures are the engine's: --batch 2 at 8192 tokens with the MTP layer and a 1024 MiB reserve left 2519 MiB free and the cache needed 99 MiB 156more (0 slots, a refused start; a SIGSEGV before ab7baa5b1).""" 157 158def test_the_slot_session_is_the_main_sessions_size (self ): 159self .assertAlmostEqual (V .session_mib (8192 ),234 ,delta = 8 )# the engine: "2 slot sessions (0.23 GiB each)" 160 161def test_kv_streaming_caps_what_a_session_keeps_in_vram (self ): 162full = V .session_mib (131072 ) 163streamed = V .session_mib (131072 ,"int8" ,32768 ) 164self .assertAlmostEqual (full ,1929 ,delta = 15 ) 165self .assertAlmostEqual (streamed ,748 ,delta = 15 )# measured 0.73 GiB on the CUDA engine (DF2-MEM): the 32K pool (573 MiB with its fixed part) plus what scales with the whole context 166self .assertEqual (V .session_mib (8192 ,"int8" ,32768 ),V .session_mib (8192 ))# a resident window larger than the context changes nothing 167self .assertAlmostEqual (V .session_mib (131072 ,"int8" ,4096 ),V .session_mib (131072 ,"int8" ,V .KV_RESIDENT_MIN ),delta = 0.01 )# the engine raises a smaller window to its minimum (4096) 168 169def test_without_slots_nothing_changes (self ): 170a = V .slack_mib (FN ,8.0 ,8192 ,"int8" ,700 ,"auto" ,"intel" ) 171self .assertEqual (a ,V .slack_mib (FN ,8.0 ,8192 ,"int8" ,700 ,"auto" ,"intel" ,0 )) 172self .assertEqual (a ,V .slack_mib (FN ,8.0 ,8192 ,"int8" ,700 ,"auto" ,"intel" ,1 )) 173self .assertEqual (V .needed_mib (),V .needed_mib (FN ,1 )) 174 175def test_the_engines_own_minimum_is_the_threshold (self ): 176self .assertAlmostEqual (V .needed_mib (FN ,2 ,"intel" ),V .SLOT_RUN_MIN * V .SLOT_SLOTTED_MIB + V .MARGIN_MIB ,delta = 0.01 )# one slot and the margin, not 128 slots 177self .assertGreater (V .needed_mib (FN ,0 ),128 * 1.2 )# a single request keeps its old rule 178 179def test_an_8gb_arc_with_two_requests_and_the_refused_reserve_reads_prompts_in_smaller_chunks (self ): 180# 8192 with a 1024 MiB reserve is the start the engine refused (the estimate says 137 MiB short; the engine 99) 181self .assertLess (V .slack_mib (FN ,8.0 ,8192 ,"int8" ,1024 ,"auto" ,"intel" ,2 ),0 ) 182f = V .plan (FN ,8.0 ,8192 ,"int8" ,1024 ,"auto" ,"intel" ,title = FN ,reserve_fixed = True ,slots = 2 ) 183self .assertTrue (f .changed and f .fits ) 184self .assertEqual ((f .ctx ,f .slots ,f .reserve_mib ,f .prefill ), (8192 ,2 ,1024 ,"1024" ))# the context and the 2 requests stay 185self .assertGreaterEqual (f .slack_mib ,V .needed_mib (FN ,2 ,"intel" )) 186self .assertIn (V .NOTE_SLOW ,f .line ) 187 188def test_an_8gb_arc_whose_start_the_engine_runs_is_not_touched (self ): 189# a 700 MiB reserve at 8192 leaves the cache about 84 slots: the engine runs it 190f = V .plan (FN ,8.0 ,8192 ,"int8" ,700 ,"auto" ,"intel" ,title = FN ,reserve_fixed = True ,prefill_fixed = True ,slots = 2 ) 191self .assertFalse (f .changed ) 192self .assertEqual ((f .ctx ,f .slots ), (8192 ,2 )) 193 194def test_not_even_every_knob_fits_then_the_start_goes_as_far_as_it_can_with_the_same_requests (self ): 195f = V .plan (FN ,6.0 ,8192 ,"int8" ,1500 ,"auto" ,"intel" ,title = FN ,reserve_fixed = True ,slots = 2 ) 196self .assertEqual ((f .ctx ,f .slots ), (8192 ,2 )) 197self .assertEqual (f .prefill ,"256" ) 198self .assertFalse (f .fits ) 199 200def test_a_12gb_card_keeps_128k_with_streamed_kv (self ): 201# setup writes --kv-resident 32768 at 128K: each of the 3 sessions keeps about 750 MiB (measured), not 1.9 GB 202for vendor in ("intel" ,"cuda" ): 203f = V .plan (FN ,12.0 ,131072 ,"int8" ,700 ,"auto" ,vendor ,title = FN ,reserve_fixed = True ,prefill_fixed = True ,slots = 2 ,kv_resident = 32768 ) 204self .assertFalse (f .changed ,vendor ) 205self .assertEqual ((f .ctx ,f .slots ), (131072 ,2 )) 206self .assertGreater (V .slack_mib (FN ,12.0 ,131072 ,"int8" ,700 ,"auto" ,"intel" ,2 ,32768 ),2000 ) 207# charging each slot the whole 128K would have lowered it (the mistake of 2a1042877) 208self .assertLess (V .slack_mib (FN ,12.0 ,131072 ,"int8" ,700 ,"auto" ,"intel" ,2 ,0 ),0 ) 209 210def test_a_card_with_no_measured_figure_is_left_to_the_engine (self ): 211f = V .plan (FN ,8.0 ,8192 ,"int8" ,1024 ,"auto" ,"other" ,title = FN ,reserve_fixed = True ,prefill_fixed = True ,slots = 2 ) 212self .assertFalse (f .changed )# HIP: no measured base yet, so no claim; the engine sizes its cache and says what is short 213self .assertEqual (f .ctx ,8192 ) 214 215def test_a_24gb_cuda_card_is_unchanged (self ): 216f = V .plan (FN ,24.0 ,131072 ,"int8" ,700 ,"auto" ,"cuda" ,title = FN ,reserve_fixed = True ,prefill_fixed = True ,slots = 4 ,kv_resident = 32768 ) 217self .assertFalse (f .changed ) 218self .assertEqual ((f .ctx ,f .slots ,f .line ), (131072 ,4 ,"" )) 219 220 221class FitStart (unittest .TestCase ): 222"""fit_start: the engine arguments of one start (serve/server.py engine_args calls it). It never changes --max-context and never the requests at once.""" 223 224ARGS = ["--native" ,"/m/Qwen3.8-Flash-Next-IQ3_XXS-00001-of-00002.gguf" ,"--expert-cache" ,"auto" ,"--prefill" ,"auto" ,"--max-context" ,"8192" , 225"--kv" ,"int8" ,"--vram-reserve-mib" ,"1024" ,"--mtp" ,"/m/rt" ,"--spec" ,"4" ] 226 227def cfg (self ,** kw ): 228c = {"model_name" :FN ,"parallel" :2 } 229c .update (kw ) 230return c 231 232def test_an_8gb_arc_reads_prompts_in_smaller_chunks_and_keeps_context_and_requests (self ): 233args ,par ,note = V .fit_start (self .cfg (),list (self .ARGS ),8192 ,"sycl" ) 234self .assertEqual (args [args .index ("--max-context" )+ 1 ],"8192" ) 235self .assertEqual (args [args .index ("--prefill" )+ 1 ],"1024" ) 236self .assertIsNone (par )# still 2 requests at once 237self .assertIn (V .NOTE_SLOW ,note ) 238self .assertEqual (self .ARGS [self .ARGS .index ("--prefill" )+ 1 ],"auto" )# the input is not touched 239 240def test_the_persons_own_reserve_and_chunk_are_kept (self ): 241args ,par ,note = V .fit_start (self .cfg (user_args = ["--vram-reserve-mib" ,"1024" ]),list (self .ARGS ),8192 ,"sycl" ) 242self .assertEqual (args [args .index ("--vram-reserve-mib" )+ 1 ],"1024" ) 243args2 = [("2048" if a == "auto" else a )for a in self .ARGS ] 244self .assertEqual (V .fit_start (self .cfg (),list (args2 ),8192 ,"sycl" )[0 ][args2 .index ("--prefill" )+ 1 ],"2048" ) 245 246def test_a_12gb_card_at_128k_with_streamed_kv_is_unchanged (self ): 247a = ["--native" ,"/m/Qwen3.8-Flash-Next-IQ3_S-00001-of-00002.gguf" ,"--max-context" ,"131072" ,"--kv" ,"int8" ,"--kv-resident" ,"32768" , 248"--vram-reserve-mib" ,"700" ] 249for backend in ("cuda" ,"sycl" ): 250self .assertEqual (V .fit_start (self .cfg (model_name = "qwen3.8-flash-next-iq3_s" ),list (a ),12288 ,backend ), (a ,None ,"" ),backend ) 251 252def test_a_24gb_cuda_card_and_one_request_are_unchanged (self ): 253big = ["--native" ,"/m/q.gguf" ,"--max-context" ,"131072" ,"--kv" ,"int8" ,"--vram-reserve-mib" ,"700" ] 254self .assertEqual (V .fit_start (self .cfg (parallel = 4 ),list (big ),24576 ,"cuda" ), (big ,None ,"" )) 255self .assertEqual (V .fit_start (self .cfg (parallel = 1 ),list (self .ARGS ),8192 ,"sycl" ), (self .ARGS ,None ,"" )) 256self .assertEqual (V .fit_start ({"model_name" :FN },list (self .ARGS ),8192 ,"sycl" ), (self .ARGS ,None ,"" )) 257 258def test_nothing_without_one_cards_memory_or_on_a_split_or_metal (self ): 259self .assertEqual (V .fit_start (self .cfg (),list (self .ARGS ),None ,"sycl" ), (self .ARGS ,None ,"" )) 260self .assertEqual (V .fit_start (self .cfg (),list (self .ARGS ),8192 ,"metal" ), (self .ARGS ,None ,"" )) 261self .assertEqual (V .fit_start (self .cfg (layer_split = "auto" ),list (self .ARGS ),8192 ,"sycl" ), (self .ARGS ,None ,"" )) 262 263def test_a_start_that_cannot_fit_keeps_its_requests_and_its_context (self ): 264tight = [a if a != "1024" else "1500" for a in self .ARGS ] 265args ,par ,note = V .fit_start (self .cfg (),tight ,8192 ,"sycl" ) 266self .assertIsNone (par ) 267self .assertEqual (args [args .index ("--max-context" )+ 1 ],"8192" ) 268self .assertIn (V .NOTE_SLOW ,note ) 269 270def test_no_context_in_the_args_is_never_added (self ): 271no_ctx = list (self .ARGS ) 272i = no_ctx .index ("--max-context" ) 273del no_ctx [i :i + 2 ] 274args ,par ,note = V .fit_start (self .cfg (),no_ctx ,8192 ,"sycl" ) 275self .assertNotIn ("--max-context" ,args ) 276 277 278class CudaSpill (unittest .TestCase ): 279"""The CUDA engine, measured on the RTX PRO 5000 held to 7942 / 8966 / 9990 / 12000 MiB by a memory hog (DF2-MEM, RESULTS-DF2MEM.md): an 8 GB NVIDIA card does not start 280the default 128K with the KV window setup writes (32768), and starts with the engine's minimum window (20480). The knob is the window; the context and the requests stay.""" 281 282M = "qwen3.8-flash-next-iq3_s" 283A = ["--native" ,"/m/Qwen3.8-Flash-Next-IQ3_S-00001-of-00002.gguf" ,"--max-context" ,"131072" ,"--kv" ,"int8" ,"--kv-resident" ,"32768" , 284"--vram-reserve-mib" ,"700" ,"--prefill" ,"auto" ] 285DF2 = ["--draft" ,"dflash2" ,"--draft-model" ,"/p/q4_0.pack" ,"--draft-fallback" ,"none" ,"--spec" ,"6" ,"--draft-conf" ,"0.8" ] 286MTP = ["--mtp" ,"/m/rt" ,"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 287 288def start (self ,vram_mib ,n = None ,drafter = None ,user = None ,backend = "cuda" ): 289cfg = {"model_name" :self .M } 290if n : 291cfg ["parallel" ]= n 292if user : 293cfg ["user_args" ]= user 294return V .fit_start (cfg ,self .A + (drafter if drafter is not None else self .DF2 ),vram_mib ,backend ) 295 296@staticmethod 297def flag (args ,name ): 298return args [args .index (name )+ 1 ]if name in args else None 299 300def test_the_base_reproduces_the_engines_cache_room (self ): 301g = lambda free : (free + V .GAP_OTHER_MIB )/ 1024.0 302sl = lambda free ,n :V .slack_mib (self .M ,g (free ),131072 ,"int8" ,700 ,"auto" ,"cuda" ,n ,32768 ) 303self .assertAlmostEqual (sl (12000 ,2 ),3615 - 700 - 218 - 256 ,delta = 60 )# "expert cache auto: 3.53 GiB free ... -> 961 slots" 304self .assertAlmostEqual (sl (12000 ,4 ),2120 - 700 - 218 - 256 ,delta = 60 )# "2.07 GiB free ... -> 371 slots" 305self .assertAlmostEqual (sl (7800 ,2 ),850 - 835 - 598 - 700 - 218 - 256 ,delta = 60 )# 0.83 GiB after the sessions, then the MTP layer and the head: the OOM 306 307def test_an_8gb_card_with_dflash2_alone_gets_the_small_kv_window_and_keeps_128k (self ): 308for mib ,window in ((8192 ,"20480" ), (8050 ,None )): 309args ,par ,note = self .start (mib ) 310self .assertEqual (self .flag (args ,"--max-context" ),"131072" ,mib ) 311if window : 312self .assertEqual (self .flag (args ,"--kv-resident" ),window ,mib )# measured: starts and decodes at 69-73 tok/s; 32768 does not start 313else : 314self .assertIn (self .flag (args ,"--kv-resident" ), ("20480" ,"8192" ,"4096" ),mib )# a card a little smaller walks further down 315self .assertEqual (self .flag (args ,"--prefill" ),"auto" )# the engine steps its own chunk down 316self .assertEqual (self .flag (args ,"--vram-reserve-mib" ),"700" )# a lower reserve starves the DFlash2 prompt staging (measured) 317self .assertIsNone (par ) 318self .assertIn (V .NOTE_SLOW ,note ) 319 320def test_an_8gb_card_with_requests_at_once_keeps_the_requests_and_the_context (self ): 321for n in (2 ,4 ): 322args ,par ,note = self .start (8192 ,n ,self .MTP ) 323self .assertEqual (self .flag (args ,"--max-context" ),"131072" ,n ) 324self .assertIsNone (par ,n )# the number of requests is the person's 325self .assertEqual (self .flag (args ,"--kv-resident" ),"4096" ,n )# measured: 2 requests start at 4096 (277 MiB free, 46 tok/s per user) and 8192; 32768 and 20480 do not 326self .assertIn (V .NOTE_SLOW ,note ) 327 328def test_the_window_is_walked_down_only_as_far_as_the_card_needs (self ): 329win = lambda mib ,n ,drafter :self .flag (self .start (mib ,n ,drafter )[0 ],"--kv-resident" ) 330self .assertEqual (win (8192 ,None ,self .DF2 ),"20480" )# DFlash2 alone fits at 20480 331self .assertEqual (win (8192 ,None ,self .MTP ),"8192" )# the MTP layer needs a smaller one 332self .assertEqual (win (10240 ,2 ,self .MTP ),"20480" ) 333self .assertEqual (win (10240 ,4 ,self .MTP ),"4096" ) 334self .assertEqual (win (12288 ,8 ,self .MTP ),"8192" ) 335self .assertEqual (win (12288 ,4 ,self .MTP ),"32768" )# nothing moves where it fits 336 337def test_the_window_floor_and_steps_match_the_engine (self ): 338self .assertEqual (V .KV_RESIDENT_MIN ,4096 )# qsa_kv_resident_min() 339self .assertEqual (V .KV_RESIDENT_STEPS , (20480 ,8192 ,4096 )) 340self .assertTrue (all (w >= V .KV_RESIDENT_MIN for w in V .KV_RESIDENT_STEPS )) 341 342def test_12_16_and_24gb_keep_everything_for_1_2_and_4_requests (self ): 343for mib in (12288 ,12282 ,16384 ,24576 ): 344for n ,drafter in ((None ,self .DF2 ), (None ,self .MTP ), (2 ,self .MTP ), (4 ,self .MTP )): 345a = self .A + drafter 346self .assertEqual (V .fit_start ({"model_name" :self .M ,** ({"parallel" :n }if n else {})},list (a ),mib ,"cuda" ), (a ,None ,"" ), (mib ,n )) 347 348def test_the_8gb_figures_behind_it (self ): 349gb = (7942 + V .GAP_OTHER_MIB )/ 1024.0 350one = V .SLOT_RUN_MIN * V .SLOT_SLOTTED_MIB + V .MARGIN_MIB 351sl = lambda kvr ,drafter :V .slack_mib (self .M ,gb ,131072 ,"int8" ,700 ,"auto" ,"cuda" ,0 ,kvr ,drafter ) 352self .assertLess (sl (32768 ,"dflash2" ),one )# did not start: "the device arena (75 MiB) does not fit" 353self .assertGreaterEqual (sl (20480 ,"dflash2" ),one )# started 354self .assertLess (sl (32768 ,"mtp" ),one ) 355 356def test_a_window_the_person_set_is_kept (self ): 357args ,par ,note = self .start (8192 ,user = ["--kv-resident" ,"32768" ]) 358self .assertEqual (self .flag (args ,"--kv-resident" ),"32768" ) 359 360def test_metal_and_unknown_backends_are_not_claimed (self ): 361for backend in ("metal" ,"vulkan" ,None ,"" ): 362a = self .A + self .DF2 363self .assertEqual (V .fit_start ({"model_name" :self .M },list (a ),8192 ,backend ), (a ,None ,"" ),backend ) 364 365def test_the_hip_base_is_its_own_measurement (self ): 366hip ,cuda = V .BASE_SLOTTED_MIB [("hip" ,"flash-next" )],V .BASE_SLOTTED_MIB [("cuda" ,"flash-next" )] 367self .assertNotEqual (hip ,cuda ) 368self .assertAlmostEqual (cuda - hip ,1090 ,delta = 200 )# the R9700 start holds about 1.1 GiB less before the cache than the CUDA one (FREE - 5045 against FREE - 6324, less the context) 369self .assertTrue (V .HIP_BASE_NOTE .startswith ("measured on an R9700" )) 370 371def test_an_8gb_discrete_amd_card_starts_as_set_up_and_a_6gb_one_walks_the_window (self ): 372# measured on the R9700 held to 7942 MiB: DFlash2 alone starts at 128K with setup's window (787 slots, 864 MiB free); at 6400 MiB it needs the loan that keeps 16 373for mib in (8192 ,8176 ): 374a = self .A + self .DF2 375self .assertEqual (V .fit_start ({"model_name" :self .M },list (a ),mib ,"hip" ), (a ,None ,"" ),mib ) 376args ,par ,note = self .start (6144 ,backend = "hip" ) 377self .assertEqual (self .flag (args ,"--max-context" ),"131072" ) 378self .assertIn (self .flag (args ,"--kv-resident" ), ("20480" ,"8192" ,"4096" )) 379self .assertIsNone (par ) 380self .assertIn (V .NOTE_SLOW ,note ) 381for mib in (12288 ,16384 ,24576 ,32768 ): 382a = self .A + self .DF2 383self .assertEqual (V .fit_start ({"model_name" :self .M },list (a ),mib ,"hip" ), (a ,None ,"" ),mib ) 384for n in (2 ,4 ): 385args ,par ,note = self .start (8192 ,n ,self .MTP ,backend = "hip" ) 386self .assertEqual (self .flag (args ,"--max-context" ),"131072" ,n ) 387self .assertIsNone (par ,n )# the number of requests is never touched 388 389def test_an_integrated_amd_chip_is_left_alone (self ): 390# serve/server.py drafting_vram_mib() returns None for Strix Halo and the other APUs (one pool with the host's RAM): fit_start does nothing without a card's memory 391a = self .A + self .DF2 392self .assertEqual (V .fit_start ({"model_name" :self .M },list (a ),None ,"hip" ), (a ,None ,"" )) 393 394def test_the_drafter_is_read_from_the_arguments (self ): 395self .assertEqual (V .drafter_of (self .A + self .DF2 ),"dflash2" ) 396self .assertEqual (V .drafter_of (self .A + ["--mtp" ,"/m" ,"--draft" ,"dflash2" ,"--draft-model" ,"/p" ]),"dflash2+mtp" ) 397self .assertEqual (V .drafter_of (self .A + self .MTP ),"mtp" ) 398self .assertEqual (V .drafter_of (self .A ),"none" ) 399 400 401 402class Reserve3750 (unittest .TestCase ): 403"""#3750: 256K context + Q4 KV on a small card. The app's reserve for a card of 16 GB or less is 1536 MiB (apps/host native-resources.mjs), no longer a fifth of the 404card (3256 on the reporter's RX 9070 XT). The fit arithmetic prices the expert cache that is left, and it is positive at 12 GiB with the app's reserve on both 405discrete backends; the old 3256 left the CUDA engine's measured base at its threshold (a start on the edge, which the reporter's 83 GB model fell over).""" 406 407M = "Qwen3.8-Flash-Next-IQ3_S" 408 409def slack (self ,gb ,reserve ,vendor ,kv_resident = 0 ,ctx = 262144 ): 410return V .slack_mib (self .M ,gb ,ctx ,"q4_0" ,reserve ,"auto" ,vendor ,0 ,kv_resident ,"mtp" ) 411 412def test_a_12_gib_card_at_256k_q4_has_a_positive_cache_with_the_apps_reserve (self ): 413for vendor in ("cuda" ,"hip" ): 414s = self .slack (12.0 ,1536 ,vendor ) 415self .assertGreater (s ,V .needed_mib (self .M ,0 ,vendor ,"mtp" )+ 1000 , (vendor ,s )) 416self .assertTrue (V .fits (self .M ,12.0 ,262144 ,"q4_0" ,1536 ,"auto" ,vendor ,0 ,0 ,"mtp" ),vendor ) 417 418def test_the_old_fifth_of_the_card_takes_the_room_the_reserve_cap_gives_back (self ): 419for gb ,old in ((12.0 ,2457 ), (15.9 ,3256 )): 420for vendor in ("cuda" ,"hip" ): 421self .assertAlmostEqual (self .slack (gb ,1536 ,vendor )- self .slack (gb ,old ,vendor ),old - 1536 ,delta = 1 ) 422# on a 12 GiB CUDA card the old reserve left the cache nothing beyond the working threshold 423self .assertLess (self .slack (12.0 ,3256 ,"cuda" )- V .needed_mib (self .M ,0 ,"cuda" ,"mtp" ),100 ) 424 425def test_a_fit_never_changes_the_context_and_a_fitting_start_is_returned_byte_identical (self ): 426args = ["--native" ,"m.gguf" ,"--max-context" ,"262144" ,"--kv" ,"q4_0" ,"--vram-reserve-mib" ,"1536" ,"--mtp" ,"mtp.gguf" ] 427for mib in (12288 ,16282 ,24576 ): 428out = V .fit_start ({"model_name" :self .M },list (args ),mib ,"cuda" ) 429self .assertEqual (out [0 ],args ,mib ) 430self .assertEqual (out [0 ][out [0 ].index ("--max-context" )+ 1 ],"262144" ) 431# a start that would be short keeps its context and its reserve and moves KV to RAM instead 432out = V .fit_start ({"model_name" :self .M },list (args ),6144 ,"cuda" ) 433self .assertEqual (out [0 ][out [0 ].index ("--max-context" )+ 1 ],"262144" ) 434self .assertEqual (out [0 ][out [0 ].index ("--vram-reserve-mib" )+ 1 ],"1536" ) 435 436if __name__ == "__main__" : 437unittest .main ()