okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9a62d969f
main
1"""Auto with 2 requests at once on one large Blackwell card that holds every expert starts DFlash2 slot drafts (``--draft dflash2 --batch N``, no MTP layer); 2every other class and every other number of requests keeps the rule it had (the MTP loaded, slots with no drafts). strata_control/drafting.py slot_drafts_by_default.""" 3 4from __future__import annotations 5 6import os 7import tempfile 8import unittest 9from pathlib import Path 10from types import SimpleNamespace 11from unittest import mock 12 13from strata_control import drafting 14 15GYRO = "qwen3.8-flash-next-gyro-gyro-s" 16IQ3S = "qwen3.8-flash-next-iq3_s" 17PRO5000 = 73728.0 # MiB, the 72 GB card the rule was measured on (RTX PRO 5000) 18BASE = dict (mode = "auto" ,backend = "cuda" ,archs = ["sm_120" ],vram_mib = PRO5000 ,args = ["--expert-cache" ,"auto" ],slots = 2 ) 19 20 21def rule (** over )-> bool : 22kw = {** BASE ,** over } 23return drafting .slot_drafts_by_default (kw .pop ("mode" ),** kw ) 24 25 26_HARDWARE = mock .patch .object (drafting ,"detect_hardware" ,return_value = ("cuda" , ["sm_120" ])) 27 28 29def setUpModule (): 30_HARDWARE .start () 31 32 33def tearDownModule (): 34_HARDWARE .stop () 35 36 37class TheRule (unittest .TestCase ): 38def setUp (self ): 39env = {k :v for k ,v in os .environ .items ()if k not in ("STRATA_BATCH_DF2" ,"STRATA_BATCH_MTP" )} 40patch = mock .patch .dict (os .environ ,env ,clear = True ) 41patch .start () 42self .addCleanup (patch .stop ) 43 44def test_two_requests_on_the_measured_class_get_slot_drafts (self ): 45self .assertTrue (rule (slots = 2 )) 46self .assertFalse (rule (slots = 3 ),"3 clients: DFlash2 slot drafts were level with plain slots (73.5 / 74.5 against 74.1 / 73.3 requests per minute)" ) 47 48def test_one_request_and_three_or_more_keep_their_rule (self ): 49for n in (0 ,1 ,3 ,4 ,5 ,8 ): 50self .assertFalse (rule (slots = n ),n ) 51 52def test_the_number_can_be_in_the_arguments (self ): 53for flag in ("--batch" ,"--slots" ,"--parallel" ): 54self .assertTrue (rule (slots = 0 ,args = ["--expert-cache" ,"auto" ,flag ,"2" ]),flag ) 55self .assertFalse (rule (slots = 0 ,args = ["--expert-cache" ,"auto" ,flag ,"3" ]),flag ) 56self .assertEqual (drafting .request_count (["--batch" ,"2" ],3 ),3 ,"the larger of the two" ) 57self .assertEqual (drafting .request_count ([],None ),0 ) 58self .assertEqual (drafting .request_count (["--batch" ,"x" ],True ),0 ) 59 60def test_the_other_classes_stay_as_they_are (self ): 61self .assertFalse (rule (archs = ["sm_121" ],vram_mib = None ),"the DGX Spark: one pool of memory" ) 62self .assertFalse (rule (vram_mib = None ),"several cards or a card that does not answer" ) 63self .assertFalse (rule (archs = ["sm_86" ],vram_mib = 49152.0 ),"Ampere: DFlash2 was slower there" ) 64self .assertFalse (rule (archs = ["sm_89" ],vram_mib = 49152.0 )) 65self .assertFalse (rule (vram_mib = 32768.0 ),"a consumer Blackwell card under 48 GB" ) 66self .assertFalse (rule (args = ["--expert-cache" ,"1000" ]),"an expert cache that does not hold every expert (the streaming class)" ) 67for backend ,archs in (("hip" , ["gfx1151" ]), ("hip" , ["gfx1100" ]), ("hip" , ["gfx1201" ]), ("metal" , []), ("sycl" , ["arc_a" ]), ("cpu" , []), (None ,None )): 68self .assertFalse (rule (backend = backend ,archs = archs ,vram_mib = PRO5000 if backend == "hip" else None ), (backend ,archs )) 69 70def test_only_auto_is_decided_here (self ): 71for mode in ("mtp" ,"off" ,"dflash2" ): 72self .assertFalse (rule (mode = mode ),mode )# (DFlash2 by name already runs its slots: blocked_by named) 73self .assertTrue (rule (mode = None ),"no choice saved is Auto" ) 74 75def test_a_layer_split_the_person_choices_and_the_way_back_stay_as_before (self ): 76self .assertFalse (rule (split = True )) 77for extra in (["--layer-split" ,"auto" ], ["--batch-mtp" ], ["--pipeline-windows" ,"2" ]): 78self .assertFalse (rule (args = ["--expert-cache" ,"auto" ,* extra ]),extra ) 79with mock .patch .dict (os .environ , {"STRATA_BATCH_MTP" :"1" }): 80self .assertFalse (rule (),"the person asked for the MTP's slot drafts" ) 81with mock .patch .dict (os .environ , {"STRATA_BATCH_MTP" :"0" }): 82self .assertTrue (rule ()) 83with mock .patch .dict (os .environ , {"STRATA_BATCH_DF2" :"0" }): 84self .assertFalse (rule (),"STRATA_BATCH_DF2=0 is the way back" ) 85with mock .patch .dict (os .environ , {"STRATA_BATCH_DF2" :"1" }): 86self .assertTrue (rule ()) 87 88def test_no_drafter_on_this_computer_changes_nothing (self ): 89self .assertFalse (rule (drafter_here = False ),"no pack: the MTP stays, with its usual line" ) 90 91def test_blocked_by_lets_it_through_like_a_named_dflash2 (self ): 92self .assertTrue (drafting .blocked_by ([],slots = 2 ,backend = "cuda" )) 93self .assertFalse (drafting .blocked_by ([],slots = 2 ,backend = "cuda" ,slot_drafts = True )) 94self .assertTrue (drafting .blocked_by (["--pipeline-windows" ,"2" ],slots = 2 ,backend = "cuda" ,slot_drafts = True )) 95self .assertTrue (drafting .blocked_by ([],slots = 2 ,backend = "metal" ,slot_drafts = True ),"Metal has no per-request drafters" ) 96 97 98class TheStart (unittest .TestCase ): 99ARGS = ["--pack" ,"/d/packs/gyro" ,"--native" ,"/d/m.gguf" ,"--mtp" ,"" ,"--spec" ,"4" ,"--spec-min-p" ,"0.5" ,"--expert-cache" ,"auto" ] 100 101def setUp (self ): 102env = {k :v for k ,v in os .environ .items ()if k not in ("STRATA_BATCH_DF2" ,"STRATA_BATCH_MTP" )} 103patch = mock .patch .dict (os .environ ,env ,clear = True ) 104patch .start () 105self .addCleanup (patch .stop ) 106self .tmp = tempfile .TemporaryDirectory () 107self .addCleanup (self .tmp .cleanup ) 108self .data = Path (self .tmp .name ) 109self .mtp = self .data / "mtp" / "rt" 110self .args = list (self .ARGS ) 111self .args [self .args .index ("--mtp" )+ 1 ]= str (self .mtp ) 112self .mtp .mkdir (parents = True ) 113 (self .mtp / "experts.bin" ).write_bytes (b"e" ) 114 (self .mtp / "draft_vocab.bin" ).write_bytes (b"v" ) 115 116def pack (self ): 117pack = drafting .pack_path (self .data ) 118pack .parent .mkdir (parents = True ) 119pack .write_bytes (b"x" * (2 << 20 )) 120return pack 121 122def cfg (self ,** extra ): 123return {"model_name" :GYRO ,"args" :list (self .args ),** extra } 124 125def apply (self ,cfg ,** kw ): 126return drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],vram_mib = PRO5000 ,** kw ) 127 128def test_auto_with_two_requests_is_dflash2_with_a_drafter_per_request (self ): 129pack = self .pack () 130for n in (2 ,): 131c = self .cfg () 132d = self .apply (c ,slots = n ) 133self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" ),n ) 134a = c ["args" ] 135self .assertEqual ((a [a .index ("--draft" )+ 1 ],a [a .index ("--draft-model" )+ 1 ],a [a .index ("--draft-fallback" )+ 1 ]), ("dflash2" ,str (pack ),"none" ),n ) 136for flag in ("--mtp" ,"--spec-min-p" ,"--batch-mtp" ): 137self .assertNotIn (flag ,a , (n ,flag ,"never the MTP layer beside DFlash2" )) 138 139def test_auto_with_other_numbers_of_requests_is_the_mtp_as_before (self ): 140self .pack () 141for n in (3 ,4 ,8 ): 142c = self .cfg () 143d = self .apply (c ,slots = n ) 144self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NONE_IN_SLOTS ),n ) 145self .assertEqual (c ["args" ],self .args ,n ) 146 147def test_no_pack_is_the_mtp_with_the_usual_line (self ): 148c = self .cfg () 149d = self .apply (c ,slots = 2 ) 150self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NONE_IN_SLOTS )) 151self .assertEqual (c ["args" ],self .args ) 152 153def test_the_ways_back (self ): 154self .pack () 155for kw ,want in (({"drafting" :"mtp" }, ("mtp" ,"" )), ({"drafting" :"off" }, ("off" ,"" ))): 156c = self .cfg (** kw ) 157d = self .apply (c ,slots = 2 ) 158self .assertEqual ((d .drafter ,d .note ),want ,kw ) 159self .assertNotIn ("--draft" ,c ["args" ],kw ) 160c = self .cfg () 161with mock .patch .dict (os .environ , {"STRATA_BATCH_DF2" :"0" }): 162d = self .apply (c ,slots = 2 ) 163self .assertEqual ((d .drafter ,d .note ,c ["args" ]), ("mtp" ,drafting .NONE_IN_SLOTS ,self .args )) 164 165def test_the_server_adds_the_slots_and_says_nothing_new (self ): 166from serve .server import engine_args 167self .pack () 168with mock .patch ("serve.server.drafting_vram_mib" ,return_value = PRO5000 ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 169for n in (2 ,): 170got = engine_args (self .cfg (parallel = n )) 171self .assertEqual ((got [got .index ("--draft" )+ 1 ],got [got .index ("--draft-fallback" )+ 1 ],got [got .index ("--batch" )+ 1 ]), ("dflash2" ,"none" ,str (n ))) 172self .assertNotIn ("--mtp" ,got ) 173self .assertEqual (drafting .take_note (),"" ,"no note: DFlash2 runs" ) 174for n in (3 ,4 ): 175got = engine_args (self .cfg (parallel = n )) 176self .assertEqual ((got [:len (self .args )],got [- 2 :]), (self .args , ["--batch" ,str (n )]),"%d requests: the MTP loaded, slots with no drafts, as today" % n ) 177self .assertEqual (drafting .take_note (),drafting .NONE_IN_SLOTS ) 178# one request at a time: the same arguments with the rule on or off, byte for byte 179for parallel in (None ,1 ): 180cfg = self .cfg ()if parallel is None else self .cfg (parallel = parallel ) 181on = engine_args (cfg ) 182with mock .patch .dict (os .environ , {"STRATA_BATCH_DF2" :"0" }): 183off = engine_args (cfg ) 184self .assertEqual (on ,off ,parallel ) 185self .assertNotIn ("--batch" ,on ) 186# a card that does not say its memory (several cards): as before 187with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ): 188got = engine_args (self .cfg (parallel = 2 )) 189self .assertEqual (got [:len (self .args )],self .args ) 190drafting .take_note () 191 192def lines (self ,cfg ,** env ): 193"""The `[strata]` lines the server prints while it builds the engine arguments for this config (nothing is started).""" 194import contextlib 195import io 196from serve .server import engine_args 197out = io .StringIO () 198with mock .patch ("serve.server.drafting_vram_mib" ,return_value = PRO5000 ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ), \ 199mock .patch .dict (os .environ ,env ),contextlib .redirect_stdout (out ): 200engine_args (cfg ) 201drafting .take_note () 202return [x for x in out .getvalue ().splitlines ()if "rafting" in x ] 203 204def test_the_server_says_what_each_kind_of_start_drafts (self ): 205self .pack () 206old_slots ,old_none = "[strata] Drafting: MTP (DFlash2 isn't available in this mode)" ,"[strata] drafting: DFlash2 alone" 207# request slots that draft with DFlash2 (Auto on the measured class: 2 requests; or DFlash2 by name with any number of requests) 208want = ["[strata] Drafting: DFlash2, one guess window per request slot (spare rows only)" ] 209self .assertEqual ([x for x in self .lines (self .cfg (parallel = 2 ))if "vocab" not in x ],want ) 210self .assertEqual ([x for x in self .lines (self .cfg (parallel = 4 ,drafting = "dflash2" ))if "vocab" not in x ],want ) 211# request slots with no drafts: Auto with 3 or more requests, and the way back (STRATA_BATCH_DF2=0) with 2 212none = ["[strata] Drafting: none in parallel slots (one token per request and step); a single request still drafts" ] 213self .assertEqual (self .lines (self .cfg (parallel = 3 )),none ) 214self .assertEqual (self .lines (self .cfg (parallel = 2 ),STRATA_BATCH_DF2 = "0" ),none ) 215# request slots whose slots carry MTP drafts (--batch-mtp, or STRATA_BATCH_MTP): the line that was always printed there stays, and it is true (the MTP drafts, DFlash2 is not the drafter) 216self .assertEqual (self .lines (self .cfg (parallel = 3 ,args = [* self .args ,"--batch-mtp" ])), [old_slots ]) 217self .assertEqual (self .lines (self .cfg (parallel = 3 ),STRATA_BATCH_MTP = "1" ), [old_slots ]) 218# one request at a time: today's DFlash2 line, word for word (the conf is the card's) 219for cfg in (self .cfg (),self .cfg (parallel = 1 ),self .cfg (drafting = "dflash2" )): 220got = [x for x in self .lines (cfg )if "vocab" not in x ] 221self .assertEqual (len (got ),1 ,cfg ) 222self .assertRegex (got [0 ],r"^\[strata\] drafting: DFlash2 alone \(--draft dflash2, --draft-conf [0-9.]+\)$" ) 223self .assertNotIn ("slot" ,got [0 ]) 224self .assertNotIn (old_none ,"" .join (self .lines (self .cfg (parallel = 2 ))),"slots with drafts no longer say 'DFlash2 alone'" ) 225 226def test_the_words_come_from_pure_functions (self ): 227flag = ["--batch-mtp" ] 228self .assertTrue (drafting .slots_draft_nothing ([],2 )and drafting .slots_draft_nothing (["--batch" ,"4" ],0 )) 229self .assertFalse (drafting .slots_draft_nothing ([],1 )or drafting .slots_draft_nothing ([],0 ),"one request at a time is not slots" ) 230self .assertFalse (drafting .slots_draft_nothing (flag ,3 ),"--batch-mtp: the slots carry MTP drafts" ) 231self .assertFalse (drafting .slots_draft_nothing (["--pipeline-windows" ,"2" ],3 )) 232with mock .patch .dict (os .environ , {"STRATA_BATCH_MTP" :"1" }): 233self .assertFalse (drafting .slots_draft_nothing ([],3 )) 234with mock .patch .dict (os .environ , {"STRATA_BATCH_MTP" :"0" }): 235self .assertTrue (drafting .slots_draft_nothing ([],3 )) 236self .assertTrue (drafting .slot_drafts_run ([],2 )and drafting .slot_drafts_run (["--batch" ,"8" ],0 )) 237self .assertFalse (drafting .slot_drafts_run ([],1 )or drafting .slot_drafts_run ([],0 )) 238self .assertFalse (drafting .slot_drafts_run ([],2 ,split = True )or drafting .slot_drafts_run (["--layer-split" ,"auto" ],2 )or drafting .slot_drafts_run (["--pipeline-windows" ,"2" ],2 )) 239with mock .patch .dict (os .environ , {"STRATA_BATCH_DF2" :"0" }): 240self .assertFalse (drafting .slot_drafts_run ([],2 )) 241self .assertEqual (drafting .line ("mtp" ,drafting .NONE_IN_SLOTS ),"Drafting: none in parallel slots (one token per request and step); a single request still drafts" ) 242self .assertEqual (drafting .line ("mtp" ,drafting .NOT_HERE ),"Drafting: MTP (DFlash2 isn't available in this mode)" ,"the other reasons keep their form" ) 243self .assertEqual (drafting .engine_info (["--mtp" ,"/m" ,"--batch" ,"3" ],note = drafting .NONE_IN_SLOTS )["drafting_note" ],drafting .line ("mtp" ,drafting .NONE_IN_SLOTS ),"/metrics and the app" ) 244 245def test_mtp_slot_drafts_keep_their_line_through_apply (self ): 246self .pack () 247c = self .cfg (args = [* self .args ,"--batch-mtp" ]) 248d = self .apply (c ,slots = 3 ) 249self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_HERE )) 250self .assertIn ("--batch-mtp" ,c ["args" ]) 251 252def plan_by_command (self ,parallel ,args = None ,** env )-> str : 253"""The plan line `strata drafting plan` shows for this config (strata_cli/drafting_cmd.py: _inputs, then decide).""" 254from strata_cli import drafting_cmd 255cfg = {** self .cfg (),** ({}if parallel is None else {"parallel" :parallel }),"args" :list (args if args is not None else self .args )} 256path = self .data / "plan.json" 257path .write_text (__import__ ("json" ).dumps (cfg ),encoding = "utf-8" ) 258a = SimpleNamespace (config = str (path ),data_dir = str (self .data ),mode = None ,family = "qwen" ,model = None ,no_split = True ,engines_unloading = False ,drafting_command = "plan" ) 259with mock .patch .object (drafting ,"card_vram_mib" ,return_value = PRO5000 ),mock .patch .object (drafting_cmd ,"_hardware" ,return_value = ("cuda" , ["sm_120" ])), \ 260mock .patch .object (drafting_cmd ,"_split_expected" ,return_value = False ),mock .patch .dict (os .environ ,env ): 261want = drafting_cmd ._inputs (a ) 262sig = drafting_cmd ._signals (a ,want ) 263return drafting .summary_line (drafting .decide (** want ,** sig )) 264 265def plan_by_launch (self ,parallel ,args = None ,** env )-> str : 266"""The plan line the Terminal's `strata launch --plan` shows (runner.drafting_plan_line) for an installed config like this one.""" 267from strata_cli .launch import settings as S 268from strata_cli .launch .runner import Runner 269from tests .terminal .launch_support import HW_12 ,Home ,catalog 270cfg = {** self .cfg (),"args" :list (args if args is not None else self .args )} 271with Home (): 272runner = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_S" ,context = 32768 ,vision = "off" ,parallel = parallel ),catalog = catalog (HW_12 )) 273runner ._split_expected = False 274with mock .patch .object (Runner ,"installed_state" ,return_value = (None ,cfg )),mock .patch .object (drafting ,"detect_hardware" ,return_value = ("cuda" , ["sm_120" ])), \ 275mock .patch .object (drafting ,"card_vram_mib" ,return_value = PRO5000 ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ),mock .patch .dict (os .environ ,env ): 276return runner .drafting_plan_line () 277 278def test_the_plan_line_says_what_the_server_says (self ): 279self .pack () 280none = "Drafting: none in parallel slots (one token per request and step); a single request still drafts" 281mtp_slots = "Drafting: MTP (DFlash2 isn't available in this mode)" 282for how in (self .plan_by_command ,self .plan_by_launch ): 283name = how .__name__ 284self .assertEqual (how (2 ),"Drafting: DFlash2" , (name ,"2 requests on the measured class: DFlash2 slot drafts" )) 285self .assertEqual (how (3 ),none , (name ,"slots with no drafts" )) 286self .assertEqual (how (2 ,STRATA_BATCH_DF2 = "0" ),none , (name ,"the way back from the slot drafts" )) 287self .assertEqual (how (3 ,args = [* self .args ,"--batch-mtp" ]),mtp_slots , (name ,"MTP slot drafts keep their line" )) 288self .assertEqual (how (3 ,STRATA_BATCH_MTP = "1" ),mtp_slots , (name ,"MTP slot drafts through the environment" )) 289self .assertEqual (how (1 ),"Drafting: DFlash2" , (name ,"one request at a time" )) 290self .assertEqual (how (None ),"Drafting: DFlash2" , (name ,"the number of requests is not known: today's line" )) 291# the plan and the server print the same words in the same case 292self .assertEqual ("[strata] " + self .plan_by_command (3 ),self .lines (self .cfg (parallel = 3 ))[0 ]) 293 294def test_the_plan_agrees_with_the_start (self ): 295from strata_cli import drafting_cmd 296self .pack () 297cfg_path = self .data / "run.json" 298for parallel ,expect_blocked in ((2 ,False ), (3 ,True ), (4 ,True ), (1 ,False )): 299cfg_path .write_text (__import__ ("json" ).dumps ({** self .cfg (),"parallel" :parallel }),encoding = "utf-8" ) 300a = SimpleNamespace (config = str (cfg_path ),data_dir = str (self .data ),mode = None ,family = "qwen" ,model = None ,no_split = True ,engines_unloading = False ,drafting_command = "plan" ) 301with mock .patch .object (drafting ,"card_vram_mib" ,return_value = PRO5000 ),mock .patch .object (drafting_cmd ,"_hardware" ,return_value = ("cuda" , ["sm_120" ])), \ 302mock .patch .object (drafting_cmd ,"_split_expected" ,return_value = False ): 303want = drafting_cmd ._inputs (a ) 304self .assertEqual (want ["blocked" ],expect_blocked ,parallel ) 305 306 307if __name__ == "__main__" : 308unittest .main ()