okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9a62d969f
main
1"""Which drafter a model uses (MTP, DFlash2 or none): the suggestion table, the Terminal flag, the engine arguments, the download and the 2fallback words. strata_control/drafting.py is the one place; the Terminal and the app read it.""" 3 4from __future__import annotations 5 6import http .server 7import importlib .util 8import json 9import sys 10import tempfile 11import threading 12import unittest 13from pathlib import Path 14from unittest import mock 15 16from strata_cli .launch import settings as S 17from strata_cli .launch .runner import derive_config 18from strata_control import acquisition ,drafting ,drafting_fetch 19from tests .terminal .launch_support import run_cli 20 21ROOT = Path (__file__ ).resolve ().parents [2 ] 22GYRO = "qwen3.8-flash-next-gyro-gyro-s" 23_HARDWARE = mock .patch .object (drafting ,"detect_hardware" ,return_value = ("cuda" , ["sm_120" ])) 24 25 26def setUpModule (): 27"""These tests are about the table, the flags and the download, on a computer whose engine can run DFlash2 (the box that runs them may have an Intel or no card; 28tests/terminal/test_drafting_hardware.py has the hardware matrix).""" 29_HARDWARE .start () 30 31 32def tearDownModule (): 33_HARDWARE .stop () 34 35 36def serving_dflash2 (): 37"""The tests turn the switch on explicitly (drafting.ENGINE_SERVES_DFLASH2).""" 38return mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,True ) 39 40 41class AutoAndTheBackends (unittest .TestCase ): 42def test_auto_is_dflash2_where_the_engine_has_it (self ): 43"""Owner 2026-11-02: DFlash2 is the primary drafter on every system whose engine has it (CUDA, HIP, Metal, Intel SYCL since the DF2-SYCL engine).""" 44self .assertEqual (drafting .DFLASH2_BACKENDS , ("cuda" ,"hip" ,"metal" ,"sycl" )) 45for backend in ("cuda" ,"hip" ,"metal" ,"sycl" ): 46for family in ("qwen" ,"gyro" ,"unsloth" ): 47self .assertEqual (drafting .choice_of ("auto" ,family = family ,backend = backend ),"dflash2" , (backend ,family )) 48self .assertEqual (drafting .choice_of ("auto" ,family = "qwen" ,backend = None ),"mtp" ,"an unknown computer is the MTP" ) 49self .assertEqual (drafting .choice_of ("auto" ,family = "qwen" ,backend = "cpu" ),"mtp" ,"an engine without the code is the MTP" ) 50for family in ("coder" ,"swift" ,"nvfp4" ,"deepseek41" ): 51self .assertEqual (drafting .choice_of ("auto" ,family = family ,backend = "cuda" ),"mtp" ,family ) 52 53def test_intel_runs_dflash2_and_a_build_without_the_code_does_not (self ): 54self .assertTrue (drafting .hardware_support ("sycl" , ["8086" ]).can_run ) 55with mock .patch .object (drafting ,"DFLASH2_BACKENDS" , ("cuda" ,"hip" ,"metal" )): 56self .assertEqual (drafting .choice_of ("auto" ,family = "qwen" ,backend = "sycl" ),"mtp" ) 57self .assertFalse (drafting .hardware_support ("sycl" ).can_run ) 58 59def test_the_choice_is_the_choice (self ): 60for backend in ("cuda" ,"sycl" ): 61self .assertEqual (drafting .choice_of ("mtp" ,family = "qwen" ,backend = backend ),"mtp" ) 62self .assertEqual (drafting .choice_of ("off" ,family = "qwen" ,backend = backend ),"off" ) 63self .assertEqual (drafting .choice_of ("dflash2" ,family = "qwen" ,backend = "cuda" ),"dflash2" ) 64self .assertEqual (drafting .choice_of ("dflash2" ,family = "qwen" ,backend = "sycl" ),"dflash2" ) 65self .assertEqual (drafting .choice_of ("dflash2" ,family = "qwen" ,backend = "cpu" ),"mtp" ,"named, but the engine has none" ) 66self .assertEqual (drafting .choice_of ("dflash2" ,family = "coder" ,backend = "cuda" ),"mtp" ) 67 68def test_the_two_drafters_have_their_own_memory_figure (self ): 69self .assertEqual ((drafting .DRAFTER_GB ["dflash2" ],drafting .DRAFTER_GB ["mtp" ],drafting .DRAFTER_GB ["off" ]), (0.89 ,1.08 ,0.0 )) 70self .assertEqual (drafting .VRAM_GB ,0.89 ) 71 72def test_cards_are_classified_by_their_architecture (self ): 73self .assertEqual (drafting .card_class (["sm_120" ]),"blackwell" ) 74self .assertEqual (drafting .card_class (["gfx1151" ]),"strix_halo" ) 75self .assertEqual (drafting .card_class (["gfx1201" ]),"rdna4" ) 76self .assertEqual (drafting .card_class (["sm_86" ]),"ampere" ) 77self .assertEqual (drafting .card_class ([]),"other" ) 78self .assertEqual (drafting .card_class ("SM_120" ),"blackwell" ) 79 80def test_both_sides_know_the_same_modes (self ): 81self .assertEqual (drafting .MODES ,S .DRAFTING_MODES ) 82self .assertEqual (drafting .normalize (None ),"auto" ) 83self .assertEqual (drafting .normalize ("DFlash2" ),"dflash2" ) 84self .assertEqual (drafting .normalize ("fastest" ),"auto" ,"an unknown value is Auto, never an error" ) 85 86 87class Decisions (unittest .TestCase ): 88def setUp (self ): 89self .tmp = tempfile .TemporaryDirectory () 90self .data = Path (self .tmp .name ) 91self .addCleanup (self .tmp .cleanup ) 92 93def ready (self ): 94pack = drafting .pack_path (self .data ) 95pack .parent .mkdir (parents = True ) 96pack .write_bytes (b"x" * (2 << 20 )) 97 98def decide (self ,mode ,** kw ): 99return drafting .decide (mode ,model_name = GYRO ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],** kw ) 100 101def test_auto_and_dflash2_run_dflash2_alone_when_the_pack_is_here (self ): 102self .ready () 103for mode in ("auto" ,"dflash2" ,None ): 104d = self .decide (mode ) 105self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" ),mode ) 106self .assertEqual (d .flags , ("--draft" ,"dflash2" ,"--draft-model" ,str (drafting .pack_path (self .data )),"--draft-fallback" ,"none" , 107"--spec" ,"6" ,"--draft-conf" ,"0.35" ),"no --mtp, and the engine is told there is no fallback" ) 108self .assertNotIn ("--mtp" ,self .decide ("auto" ).flags ) 109 110def test_mtp_is_the_mtp_with_no_dflash2_flag (self ): 111d = self .decide ("mtp" ) 112self .assertEqual ((d .drafter ,d .flags ,d .note ), ("mtp" , (),"" )) 113 114def test_off_is_off (self ): 115self .assertEqual (self .decide ("off" ).drafter ,"off" ) 116 117def test_no_pack_and_the_mtp_layer_here_runs_the_mtp_and_says_so (self ): 118d = self .decide ("auto" ,mtp_here = True ) 119self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_DOWNLOADED )) 120self .assertTrue (drafting .needs_pack ("auto" ,model_name = GYRO ,data_dir = self .data ,backend = "cuda" )) 121 122def test_no_pack_and_no_mtp_layer_runs_none_and_says_so (self ): 123d = self .decide ("dflash2" ,mtp_here = False ) 124self .assertEqual ((d .drafter ,d .note ,d .flags ), ("off" ,drafting .NONE_HERE , ())) 125self .assertEqual (drafting .line (d .drafter ,d .note ),"Drafting: off (no drafter is downloaded yet)" ) 126 127def test_mtp_chosen_without_its_layer_runs_dflash2_if_it_is_here_else_none (self ): 128d = self .decide ("mtp" ,mtp_here = False ) 129self .assertEqual ((d .drafter ,d .note ), ("off" ,drafting .NONE_HERE )) 130self .ready () 131d = self .decide ("mtp" ,mtp_here = False ) 132self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,drafting .NO_MTP )) 133self .assertEqual (drafting .line (d .drafter ,d .note ),"Drafting: DFlash2 (the MTP drafter isn't downloaded yet)" ) 134self .assertTrue (drafting .needs_mtp ("mtp" ,model_name = GYRO ,backend = "cuda" ,mtp_here = False ),"the switch fetches the layer once" ) 135self .assertFalse (drafting .needs_mtp ("mtp" ,model_name = GYRO ,backend = "cuda" ,mtp_here = True )) 136self .assertFalse (drafting .needs_mtp ("auto" ,model_name = GYRO ,backend = "cuda" ,mtp_here = False ),"Auto is DFlash2: it needs no layer" ) 137 138def test_several_requests_at_once_load_only_the_mtp_and_say_so (self ): 139self .ready () 140d = self .decide ("dflash2" ,mtp_here = True ,blocked = True ) 141self .assertEqual ((d .drafter ,d .note ,d .flags ), ("mtp" ,drafting .NOT_HERE , ())) 142d = self .decide ("auto" ,mtp_here = True ,blocked = True ) 143self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_HERE ),"Auto says it too: nothing changes silently" ) 144self .assertTrue (drafting .blocked_by ([],slots = 4 )) 145self .assertTrue (drafting .blocked_by (["--batch" ,"4" ])and drafting .blocked_by (["--pipeline-windows" ,"2" ])) 146# DFlash2 chosen by name (owner 2026-11-03: the person's own choice wins): several requests are their word too, only --pipeline-windows still blocks 147for args ,kw in (([], {"slots" :4 }), (["--batch" ,"4" ], {}), (["--slots" ,"2" ], {}), (["--parallel" ,"3" ], {}), (["--batch" ,"4" ], {"slots" :4 ,"split" :True })): 148self .assertFalse (drafting .blocked_by (args ,named = True ,** kw ), (args ,kw )) 149self .assertTrue (drafting .blocked_by (args ,** kw ), (args ,kw ,"Auto and the MTP keep their rule" )) 150self .assertTrue (drafting .blocked_by (["--pipeline-windows" ,"2" ],named = True ),"the MTP's speculative next window has no DFlash2 counterpart" ) 151self .assertFalse (drafting .blocked_by ([],split = True ),"a layer split over several cards runs DFlash2 (owner 2026-11-02; the 0.1.56 engine does)" ) 152self .assertFalse (drafting .blocked_by (["--layer-split" ,"auto" ])) 153self .assertFalse (drafting .blocked_by (["--spec" ,"4" ],slots = 1 )) 154 155def test_dflash2_is_for_qwen38_flash_next_only (self ): 156self .ready () 157for name in ("qwen3.6-35b-a3b-q4" ,"qwen3.8-flash-next-coder-iq3_xxs" ,"qwen3.8-flash-next-swift-iq2_xs" ,None ): 158d = drafting .decide ("dflash2" ,model_name = name ,data_dir = self .data ,backend = "cuda" ) 159self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_SUPPORTED ),name ) 160self .assertFalse (drafting .needs_pack ("dflash2" ,model_name = name ,data_dir = self .data ,backend = "cuda" )) 161self .assertEqual (drafting .decide ("auto" ,model_name = name ,data_dir = self .data ,backend = "cuda" ).note ,"" ,"Auto never nags about a model DFlash2 is not for" ) 162for name in (GYRO ,"qwen3.8-flash-next-q2_0" ,"qwen3.8-flash-next-iq3_xxs" ): 163self .assertEqual (drafting .decide ("dflash2" ,model_name = name ,data_dir = self .data ,backend = "cuda" ).drafter ,"dflash2" ,name ) 164self .assertTrue (drafting .supported_model (family = "qwen" )and drafting .supported_model (family = "gyro" )) 165self .assertFalse (drafting .supported_model (family = "coder" )or drafting .supported_model (family = "swift" )or drafting .supported_model (family = "nvfp4" )) 166 167def test_the_engine_of_this_branch_serves_dflash2 (self ): 168self .assertTrue (drafting .ENGINE_SERVES_DFLASH2 ,"src/program/generate.cpp: --draft dflash2 runs with --serve (one card, one request at a time)" ) 169 170def test_while_the_engine_cannot_serve_it_the_choice_says_so_and_nothing_is_downloaded (self ): 171with mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,False ): 172d = self .decide ("dflash2" ) 173self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_SERVED )) 174self .assertFalse (drafting .needs_pack ("dflash2" ,model_name = GYRO ,data_dir = self .data ,backend = "cuda" )) 175self .assertEqual (self .decide ("auto" ).note ,"" ) 176self .assertEqual (self .decide ("auto" ).drafter ,"mtp" ) 177 178def test_intel_is_dflash2_and_fetches_its_pack (self ): 179self .assertTrue (drafting .needs_pack ("auto" ,model_name = GYRO ,data_dir = self .data ,backend = "sycl" )) 180d = drafting .decide ("auto" ,model_name = GYRO ,data_dir = self .data ,backend = "sycl" ) 181self .assertEqual (d .drafter ,"mtp" ,"the pack is not downloaded yet: the MTP until it is" ) 182d = drafting .decide ("auto" ,model_name = GYRO ,data_dir = self .data ,backend = "cpu" ) 183self .assertEqual ((d .drafter ,d .note ), ("mtp" ,"" )) 184self .assertFalse (drafting .needs_pack ("auto" ,model_name = GYRO ,data_dir = self .data ,backend = "cpu" )) 185 186 187class EngineArguments (unittest .TestCase ): 188ARGS = ["--pack" ,"/d/packs/gyro" ,"--native" ,"/d/m.gguf" ,"--mtp" ,"/d/mtp/rt" ,"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 189 190def setUp (self ): 191self .tmp = tempfile .TemporaryDirectory () 192self .data = Path (self .tmp .name ) 193self .addCleanup (self .tmp .cleanup ) 194self .vocab = self .data / "draft_vocab.bin" 195self .vocab .write_bytes (b"v" ) 196self .mtp = self .data / "mtp" / "rt" 197self .args = ["--pack" ,"/d/packs/gyro" ,"--native" ,"/d/m.gguf" ,"--mtp" ,str (self .mtp ),"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 198 199def layer (self ): 200self .mtp .mkdir (parents = True ) 201 (self .mtp / "experts.bin" ).write_bytes (b"e" ) 202 (self .mtp / "draft_vocab.bin" ).write_bytes (b"v" ) 203 204def pack (self ): 205pack = drafting .pack_path (self .data ) 206pack .parent .mkdir (parents = True ) 207pack .write_bytes (b"x" * (2 << 20 )) 208return pack 209 210def cfg (self ,** extra ): 211return {"model_name" :GYRO ,"args" :list (self .args ),** extra } 212 213def apply (self ,cfg ,** kw ): 214return drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],** kw ) 215 216def test_dflash2_alone_has_no_mtp_option_and_says_there_is_no_fallback (self ): 217pack = self .pack () 218self .layer () 219for drafting_mode in ("dflash2" ,"auto" ,None ): 220c = self .cfg (args = [* self .args ,"--batch-mtp" ,"--mtp-q4" ,"all" ],** ({}if drafting_mode is None else {"drafting" :drafting_mode })) 221d = self .apply (c ) 222self .assertEqual (d .drafter ,"dflash2" ,drafting_mode ) 223a = c ["args" ] 224for flag in ("--mtp" ,"--spec-min-p" ,"--batch-mtp" ,"--mtp-q4" ,"--draft-mtp-spec" ): 225self .assertNotIn (flag ,a , (drafting_mode ,flag )) 226self .assertEqual (a [a .index ("--draft" )+ 1 ],"dflash2" ) 227self .assertEqual (a [a .index ("--draft-model" )+ 1 ],str (pack )) 228self .assertEqual (a [a .index ("--draft-fallback" )+ 1 ],"none" ) 229self .assertEqual (a [a .index ("--mtp-draft-vocab" )+ 1 ],str (self .mtp / "draft_vocab.bin" ),"the subset the person chose in setup" ) 230self .assertEqual ((a [a .index ("--spec" )+ 1 ],a [a .index ("--draft-conf" )+ 1 ]), ("6" ,"0.35" )) 231self .assertEqual (a .count ("--spec" ),1 ) 232 233def test_dflash2_alone_works_when_no_mtp_layer_was_ever_downloaded (self ): 234self .pack () 235with mock .patch .object (drafting ,"vocab_path" ,return_value = self .vocab ): 236c = self .cfg (drafting = "dflash2" ) 237d = self .apply (c ) 238self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" )) 239self .assertEqual (c ["args" ][c ["args" ].index ("--mtp-draft-vocab" )+ 1 ],str (self .vocab ),"Strata's own default subset" ) 240self .assertNotIn ("--mtp" ,c ["args" ]) 241 242def test_mtp_has_no_dflash2_option (self ): 243self .pack () 244self .layer () 245c = self .cfg (drafting = "mtp" ,args = [* self .args ,"--draft" ,"dflash2" ,"--draft-model" ,"/p" ,"--draft-fallback" ,"mtp" ,"--draft-conf" ,"0.3" ,"--draft-mtp-spec" ,"4" , 246"--draft-cap" ,"5" ,"--draft-rule" ,"df2" ]) 247d = self .apply (c ) 248self .assertEqual ((d .drafter ,c ["args" ]), ("mtp" ,self .args ),"exactly the MTP's own arguments" ) 249 250def test_a_config_that_names_both_starts_with_the_one_chosen (self ): 251pack = self .pack () 252self .layer () 253both = [* self .args ,"--draft" ,"dflash2" ,"--draft-model" ,str (pack ),"--draft-fallback" ,"mtp" ,"--spec" ,"6" ] 254mtp = self .cfg (drafting = "mtp" ,args = list (both )) 255self .apply (mtp ) 256self .assertEqual (mtp ["args" ].count ("--mtp" ),1 ) 257self .assertNotIn ("--draft" ,mtp ["args" ]) 258two = self .cfg (drafting = "dflash2" ,args = list (both )) 259self .apply (two ) 260self .assertNotIn ("--mtp" ,two ["args" ]) 261self .assertEqual (two ["args" ][two ["args" ].index ("--draft-fallback" )+ 1 ],"none" ) 262self .assertEqual (two ["args" ].count ("--draft-fallback" ),1 ) 263# Auto picks DFlash2 for it 264auto = self .cfg (args = list (both )) 265self .apply (auto ) 266self .assertNotIn ("--mtp" ,auto ["args" ]) 267self .assertEqual (auto ["args" ][auto ["args" ].index ("--draft" )+ 1 ],"dflash2" ) 268 269def test_the_persons_own_spec_and_conf_stay (self ): 270self .pack () 271self .layer () 272c = self .cfg (drafting = "dflash2" ,args = [* self .args [:self .args .index ("--spec" )],"--spec" ,"5" ],user_args = ["--spec" ,"5" ,"--draft-conf" ,"0.2" ]) 273self .apply (c ) 274self .assertEqual ((c ["args" ][c ["args" ].index ("--spec" )+ 1 ],c ["args" ].count ("--spec" ),c ["args" ][c ["args" ].index ("--draft-conf" )+ 1 ]), ("5" ,1 ,"0.2" )) 275 276def test_parallel_with_auto_loads_only_the_mtp_and_says_so (self ): 277self .pack () 278self .layer () 279for kw in ({"slots" :4 },): 280c = self .cfg ()# Auto: unchanged, the MTP for several requests 281d = self .apply (c ,** kw ) 282self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NONE_IN_SLOTS ),kw ) 283self .assertEqual (c ["args" ],self .args ,kw ) 284c = self .cfg (drafting = "mtp" )# the MTP chosen: unchanged 285d = self .apply (c ,** kw ) 286self .assertEqual ((d .drafter ,d .note ), ("mtp" ,"" ),kw ) 287self .assertEqual (c ["args" ],self .args ,kw ) 288 289def test_dflash2_chosen_by_name_keeps_dflash2_for_the_requests_the_person_chose (self ): 290"""Owner 2026-11-03: DFlash2 chosen + a number of requests chosen = DFlash2 with that many (the engine runs a drafter per request).""" 291self .pack () 292self .layer () 293for kw in ({"slots" :2 }, {"slots" :4 }, {"slots" :8 }): 294c = self .cfg (drafting = "dflash2" ) 295d = self .apply (c ,** kw ) 296self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" ),kw ) 297a = c ["args" ] 298self .assertEqual ((a [a .index ("--draft" )+ 1 ],a [a .index ("--draft-fallback" )+ 1 ]), ("dflash2" ,"none" ),kw ) 299self .assertNotIn ("--mtp" ,a ) 300self .assertNotIn ("--batch-mtp" ,a ,"the MTP's slot switch is the MTP's; the slot drafts are the engine's default for --draft dflash2 --batch" ) 301# the number in the arguments themselves (Expert mode / the saved --batch) is the same word 302c = self .cfg (drafting = "dflash2" ,args = [* self .args ,"--batch" ,"4" ,"--batch-mtp" ]) 303d = self .apply (c ) 304self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" )) 305self .assertEqual (c ["args" ][c ["args" ].index ("--batch" )+ 1 ],"4" ) 306self .assertNotIn ("--batch-mtp" ,c ["args" ]) 307# on a layer split too 308c = self .cfg (drafting = "dflash2" ) 309self .assertEqual (self .apply (c ,split = True ,slots = 4 ).drafter ,"dflash2" ) 310# one request (1, or none given) was always DFlash2 311for kw in ({"slots" :0 }, {"slots" :1 }): 312self .assertEqual (self .apply (self .cfg (drafting = "dflash2" ),** kw ).drafter ,"dflash2" ,kw ) 313# --pipeline-windows is the one thing that still keeps it out 314c = self .cfg (drafting = "dflash2" ,args = [* self .args ,"--pipeline-windows" ,"2" ]) 315d = self .apply (c ,slots = 4 ) 316self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_HERE )) 317 318def test_no_pack_no_internet_runs_the_mtp_when_it_is_here_else_none (self ): 319self .layer () 320c = self .cfg (drafting = "dflash2" ) 321d = self .apply (c ) 322self .assertEqual ((d .drafter ,d .note ,c ["args" ]), ("mtp" ,drafting .NOT_DOWNLOADED ,self .args )) 323self .mtp .joinpath ("experts.bin" ).unlink () 324c = self .cfg (drafting = "dflash2" ) 325d = self .apply (c ) 326self .assertEqual ((d .drafter ,d .note ), ("off" ,drafting .NONE_HERE )) 327for flag in ("--mtp" ,"--draft" ,"--spec-min-p" ): 328self .assertNotIn (flag ,c ["args" ]) 329 330def test_the_mtp_switch_off_with_no_drafter_chosen_since_means_no_drafter (self ): 331self .pack () 332self .layer () 333c = self .cfg (mtp = False ) 334self .assertEqual (self .apply (c ).drafter ,"off" ) 335c = self .cfg (mtp = False ,drafting = "dflash2" ) 336self .assertEqual (self .apply (c ).drafter ,"dflash2" ,"a drafter chosen by name is the person's later word" ) 337 338def test_another_model_is_left_alone (self ): 339c = self .cfg (model_name = "qwen3.8-flash-next-coder-iq3_xxs" ) 340d = self .apply (c ) 341self .assertEqual ((d .drafter ,d .note ,c ["args" ]), ("mtp" ,"" ,self .args )) 342c = self .cfg (model_name = "qwen3.8-flash-next-coder-iq3_xxs" ,drafting = "dflash2" ,args = [* self .args ,"--draft" ,"dflash2" ,"--draft-model" ,"/p" ]) 343d = self .apply (c ) 344self .assertEqual ((d .drafter ,d .note ,c ["args" ]), ("mtp" ,drafting .NOT_SUPPORTED ,self .args )) 345 346def test_intel_gets_its_own_confidence_not_the_cuda_slot_steps (self ): 347self .layer () 348self .pack () 349for vram in (None ,8000 ,24000 ,96000 ): 350c = self .cfg () 351d = drafting .apply (c ,data_dir = self .data ,backend = "sycl" ,archs = [],vram_mib = vram ) 352a = c ["args" ] 353self .assertEqual ((d .drafter ,a [a .index ("--draft-conf" )+ 1 ],a [a .index ("--spec" )+ 1 ],"--mtp" in a ,a [a .index ("--draft-fallback" )+ 1 ]), ("dflash2" ,"0.6" ,"6" ,False ,"none" ),vram ) 354c = self .cfg (user_args = ["--draft-conf" ,"0.5" ],args = [* self .args ,"--draft-conf" ,"0.5" ]) 355drafting .apply (c ,data_dir = self .data ,backend = "sycl" ,archs = [],vram_mib = 8000 ) 356self .assertEqual (c ["args" ][c ["args" ].index ("--draft-conf" )+ 1 ],"0.5" ,"the person's own value stays" ) 357c = self .cfg () 358drafting .apply (c ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],vram_mib = 8000 ) 359self .assertEqual (c ["args" ][c ["args" ].index ("--draft-conf" )+ 1 ],"0.8" ,"CUDA keeps its slot steps" ) 360 361def test_a_mac_gets_its_own_confidence_not_the_cuda_slot_steps (self ): 362"""Metal maps every expert: the --expert-cache 64 the Mac setup writes is a slot count of 83 to the CUDA steps (0.8), which was not measured on a Mac; M5 Max measured 0.5.""" 363self .layer () 364self .pack () 365for vram in (None ,8000 ,96000 ): 366c = self .cfg () 367d = drafting .apply (c ,data_dir = self .data ,backend = "metal" ,archs = [],vram_mib = vram ) 368a = c ["args" ] 369self .assertEqual ((d .drafter ,a [a .index ("--draft-conf" )+ 1 ],a [a .index ("--spec" )+ 1 ],"--mtp" in a ,a [a .index ("--draft-fallback" )+ 1 ]), ("dflash2" ,"0.5" ,"6" ,False ,"none" ),vram ) 370c = self .cfg (user_args = ["--draft-conf" ,"0.4" ],args = [* self .args ,"--draft-conf" ,"0.4" ]) 371drafting .apply (c ,data_dir = self .data ,backend = "metal" ,archs = []) 372self .assertEqual (c ["args" ][c ["args" ].index ("--draft-conf" )+ 1 ],"0.4" ,"the person's own value stays" ) 373 374def test_off_is_the_draft_layers_own_switch (self ): 375c = self .cfg (drafting = "off" ) 376d = self .apply (c ) 377self .assertEqual (d .drafter ,"off" ) 378self .assertIs (c ["mtp" ],False ) 379for flag in ("--mtp" ,"--spec-min-p" ,"--batch-mtp" ,"--draft" ): 380self .assertNotIn (flag ,c ["args" ]) 381self .assertEqual (c ["args" ][c ["args" ].index ("--spec" )+ 1 ],"4" ,"prompt lookup keeps its windows" ) 382 383def test_the_server_applies_it_at_every_start (self ): 384from serve .server import engine_args 385self .pack () 386self .layer () 387with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 388for kw in ({"drafting" :"dflash2" }, {}):# chosen, and Auto 389args = engine_args (self .cfg (** kw )) 390self .assertNotIn ("--mtp" ,args ) 391self .assertEqual (args [args .index ("--draft" )+ 1 ],"dflash2" ) 392self .assertEqual (args [args .index ("--draft-fallback" )+ 1 ],"none" ) 393args = engine_args (self .cfg (drafting = "mtp" )) 394self .assertEqual (args ,self .args ) 395self .assertEqual (engine_args (self .cfg (parallel = 4 ))[:len (self .args )],self .args ,"Auto with several requests: the MTP alone" ) 396self .assertEqual (drafting .take_note (),drafting .NONE_IN_SLOTS ,"the engine that starts next is told why" ) 397self .assertEqual (engine_args (self .cfg (drafting = "mtp" ,parallel = 4 ))[:len (self .args )],self .args ,"the MTP chosen: unchanged" ) 398self .assertEqual (drafting .take_note (),"" ) 399# DFlash2 chosen by name with a number of requests the person chose: DFlash2 with that many, no note 400named = engine_args (self .cfg (drafting = "dflash2" ,parallel = 4 )) 401self .assertNotIn ("--mtp" ,named ) 402self .assertEqual ((named [named .index ("--draft" )+ 1 ],named [named .index ("--draft-fallback" )+ 1 ],named [named .index ("--batch" )+ 1 ]), ("dflash2" ,"none" ,"4" )) 403self .assertEqual (drafting .take_note (),"" ) 404one = engine_args (self .cfg (drafting = "dflash2" ,parallel = 1 )) 405self .assertNotIn ("--batch" ,one ) 406self .assertEqual (one [one .index ("--draft" )+ 1 ],"dflash2" ) 407# a 16 GB card holds about 3,700 slots with the drafter on: the server passes its memory and the confidence follows (drafting.CONF_STEPS) 408with mock .patch ("serve.server.drafting_vram_mib" ,return_value = 16384.0 ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 409args = engine_args (self .cfg (drafting = "dflash2" )) 410self .assertIn (args [args .index ("--draft-conf" )+ 1 ], ("0.7" ,"0.35" )) 411args = engine_args (self .cfg (drafting = "dflash2" ,args = [* self .args ,"--expert-cache" ,"1000" ])) 412self .assertEqual (args [args .index ("--draft-conf" )+ 1 ],"0.8" ,"the cache the person asked for decides, not the card" ) 413with mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,False ): 414self .assertEqual (engine_args (self .cfg ()),self .args ,"the engine cannot serve it: nothing changes" ) 415off = engine_args (self .cfg (drafting = "off" )) 416self .assertNotIn ("--mtp" ,off ) 417drafting .take_note () 418engine_args (self .cfg (drafting = "dflash2" )) 419self .assertEqual (drafting .take_note (),drafting .NOT_SERVED ) 420 421def test_data_folder_of_an_argument_list (self ): 422self .assertEqual (drafting .data_dir_of (["--pack" ,"/d/packs/x" ]),Path ("/d" )) 423self .assertEqual (drafting .data_dir_of (["--mtp" ,"/d/mtp/rt" ]),Path ("/d" )) 424self .assertIsNone (drafting .data_dir_of (["--mtp" ,"4" ])) 425self .assertIsNone (drafting .data_dir_of ([])) 426 427def test_the_engine_takes_the_vocabulary_file_alone (self ): 428"""src/program/generate.cpp: --draft dflash2 needs --mtp-draft-vocab FILE or --mtp DIR, and loads no MTP layer either way.""" 429src = (ROOT / "src" / "program" / "generate.cpp" ).read_text (encoding = "utf-8" ) 430self .assertIn ("(o.mtp.empty() && o.mtp_draft_vocab.empty())" ,src ) 431self .assertIn ('o.draft_fallback != "mtp"' ,src ) 432 433 434class UnslothAndTheOtherFlashNextFiles (unittest .TestCase ): 435"""DF2-METAL found that Auto never picked DFlash2 for Unsloth's quantizations: they are the same Qwen3.8-Flash-Next weights and the DFlash2 numbers (Strix, R9700, 436M5 Max) were measured on UD-Q4_K_XL. Swift (UkisAI's fine-tune) and Coder (ISTA-DASLab's coding version) are fine-tunes with no acceptance numbers: the MTP. 437The names are the ones setup.py and metal/setup_mac.py write: f"{family name}-{model.lower()}".""" 438 439UNSLOTH = ("qwen3.8-flash-next-unsloth-ud-q4_k_xl" ,"qwen3.8-flash-next-unsloth-ud-iq4_xs" ,"qwen3.8-flash-next-unsloth-ud-q5_k_xl" , 440"qwen3.8-flash-next-unsloth-ud-q6_k_xl" ,"qwen3.8-flash-next-unsloth-q8_0" ) 441FLASH_NEXT = ("qwen3.8-flash-next" ,"qwen3.8-flash-next-q2_0" ,"qwen3.8-flash-next-iq2_xs" ,"qwen3.8-flash-next-iq3_xxs" ,"qwen3.8-flash-next-iq3_s" , 442"qwen3.8-flash-next-gyro-gyro-s" ,"qwen3.8-flash-next-gyro-gyro-m" ) 443OTHERS = ("qwen3.8-flash-next-coder-iq1_m" ,"qwen3.8-flash-next-nvfp4-nvfp4" ,"qwen3.8-flash-next-nvfp4-nvfp4-nvidia" ,"qwen3.8-flash-next-nvfp4-nvfp4-qad" , 444"swift-1.5-iq2_xs" ,"swift-1.5-q4_0" ,"qwen3.6-35b-a3b-ud-q4_k_xl" ,"deepseek-v4.1-flash" ,"qwen3.8-flash-next-unsloth" ,"qwen3.8-flash-next-unslothx-q8_0" ) 445HARDWARE = (("cuda" , ["sm_120" ]), ("hip" , ["gfx1151" ]), ("metal" ,None ), ("sycl" , ["8086" ])) 446 447def test_the_names_the_setups_write (self ): 448for name in self .UNSLOTH + self .FLASH_NEXT : 449self .assertTrue (drafting .supported_model (name ),name ) 450self .assertTrue (drafting .supported_model (name .upper ()),name .upper ()) 451for name in self .OTHERS : 452self .assertFalse (drafting .supported_model (name ),name ) 453self .assertTrue (drafting .supported_model (None ,"unsloth" )) 454for family in ("swift" ,"coder" ,"nvfp4" ,"qwen36" ,"deepseek41" ): 455self .assertFalse (drafting .supported_model (None ,family ),family ) 456self .assertEqual (drafting .SUPPORTED_FAMILIES , ("qwen" ,"gyro" ,"unsloth" )) 457 458def test_the_names_are_the_ones_setup_writes (self ): 459"""Read from the code, not from memory: the family names and the way setup.py and metal/setup_mac.py make the run config's model_name.""" 460setup = (ROOT / "setup.py" ).read_text (encoding = "utf-8" )+ (ROOT / "strata_control" / "setup_config.py" ).read_text (encoding = "utf-8" )# (the run config's construction moved to the setup seam) 461mac = (ROOT / "metal" / "setup_mac.py" ).read_text (encoding = "utf-8" ) 462self .assertIn ('"name": "qwen3.8-flash-next-unsloth"' ,setup ) 463self .assertIn ('"name": "qwen3.8-flash-next-coder"' ,setup ) 464self .assertIn ('"name": "qwen3.8-flash-next-nvfp4"' ,setup ) 465self .assertGreaterEqual (setup .count ("\"model_name\": f\"{fam['name']}-{model.lower()}\"" ),3 ) 466self .assertIn ("f\"{fam['name']}-{model.lower()}\"" ,mac ) 467for model in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ): 468self .assertIn (f'" { model } ": {{"about"' ,setup ) 469self .assertTrue (drafting .supported_model (f"qwen3.8-flash-next-unsloth- { model . lower () } " ),model ) 470 471def run_config (self ,name ,tmp ,mode = None ): 472data = Path (tmp ) 473mtp = data / "mtp" / "rt" 474mtp .mkdir (parents = True ) 475 (mtp / "experts.bin" ).write_bytes (b"e" ) 476 (mtp / "draft_vocab.bin" ).write_bytes (b"v" ) 477pack = drafting .pack_path (data ) 478pack .parent .mkdir (parents = True ) 479pack .write_bytes (b"x" * (2 << 20 )) 480args = ["--pack" ,str (data / "packs" / "x" ),"--native" ,"/d/m.gguf" ,"--mtp" ,str (mtp ),"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 481cfg = {"model_name" :name ,"args" :args ,** ({}if mode is None else {"drafting" :mode })} 482return cfg ,data ,mtp 483 484def test_unsloth_resolves_to_dflash2_alone_with_the_vocabulary_on_every_backend (self ): 485for backend ,archs in self .HARDWARE : 486for name in self .UNSLOTH : 487with tempfile .TemporaryDirectory ()as tmp : 488cfg ,data ,mtp = self .run_config (name ,tmp ) 489decision = drafting .apply (cfg ,data_dir = data ,backend = backend ,archs = archs ) 490args = cfg ["args" ] 491self .assertEqual ((decision .drafter ,decision .note ), ("dflash2" ,"" ), (backend ,name )) 492self .assertEqual (args [args .index ("--draft" )+ 1 ],"dflash2" ) 493self .assertEqual (args [args .index ("--draft-fallback" )+ 1 ],"none" ) 494self .assertEqual (args [args .index ("--mtp-draft-vocab" )+ 1 ],str (mtp / "draft_vocab.bin" ),"the vocabulary the engine needs" ) 495for flag in ("--mtp" ,"--spec-min-p" ): 496self .assertNotIn (flag ,args , (backend ,name ,flag )) 497self .assertEqual (args [args .index ("--spec" )+ 1 ],"8" if (backend ,archs )== ("hip" , ["gfx1151" ])else "6" ,"Strix Halo 8 (RESULTS-CONF.md 11-08), every other card 6" ) 498 499def test_swift_coder_and_nvfp4_stay_on_the_mtp (self ): 500for backend ,archs in self .HARDWARE : 501for name in ("qwen3.8-flash-next-coder-iq1_m" ,"qwen3.8-flash-next-nvfp4-nvfp4" ,"swift-1.5-iq2_xs" ): 502for mode in (None ,"auto" ,"dflash2" ): 503with tempfile .TemporaryDirectory ()as tmp : 504cfg ,data ,mtp = self .run_config (name ,tmp ,mode ) 505original = list (cfg ["args" ]) 506decision = drafting .apply (cfg ,data_dir = data ,backend = backend ,archs = archs ) 507self .assertEqual (decision .drafter ,"mtp" , (backend ,name ,mode )) 508self .assertEqual (cfg ["args" ],original ,"its own MTP arguments, untouched" ) 509self .assertNotIn ("--draft" ,cfg ["args" ]) 510if mode == "dflash2" : 511self .assertEqual (decision .note ,drafting .NOT_SUPPORTED ) 512 513def test_by_family_the_terminal_and_the_app_agree (self ): 514for backend ,_ in self .HARDWARE : 515self .assertEqual (drafting .choice_of ("auto" ,family = "unsloth" ,backend = backend ),"dflash2" ,backend ) 516for family in ("swift" ,"coder" ,"nvfp4" ): 517self .assertEqual (drafting .choice_of ("auto" ,family = family ,backend = backend ),"mtp" , (backend ,family )) 518# the app's own copies of the rule (the host and the page) name the same families and the same names 519host = (ROOT / "apps" / "host" / "server" / "native-settings.mjs" ).read_text (encoding = "utf-8" ) 520page = (ROOT / "apps" / "host" / "src" / "lib" / "drafting.ts" ).read_text (encoding = "utf-8" ) 521generated = (ROOT / "apps" / "host" / "server" / "policy-generated.mjs" ).read_text (encoding = "utf-8" )# the host's copy is generated from drafting.py (strata_control/app_policy.py) 522self .assertIn ("DRAFTING_FAMILIES=GENERATED_DRAFTING_FAMILIES" ,host ) 523self .assertIn ('DRAFTING_FAMILIES = ["qwen", "gyro", "unsloth"]' ,generated ) 524self .assertIn ("DRAFTING_FAMILIES = ['qwen', 'gyro', 'unsloth']" ,page ) 525self .assertIn (r"-unsloth-[a-z0-9._-]+" ,generated ) 526 527 528class TheInstalledEngineDecidesTheFlags (unittest .TestCase ): 529"""GUI-FB3 item 2. Gyro-S with Auto sent `--mtp-draft-vocab FILE` and no `--mtp` to the engine 0.1.55.2 (f-0291-r5070-df2), which only takes `--mtp DIR` for the 530vocabulary and ended with exit code 2. The app reads what the engine program takes (it is never started for this) and sends only that.""" 531 532OLD = b"MZ\x00\x00 strata generate: --draft dflash2 drafts without the MTP layer (--draft-fallback mtp loads it too) and needs --draft-model PACK, --spec 2 or more and --mtp DIR (for DIR/draft_vocab.bin) \x00--draft-fallback\x00" 533NEW = b"MZ\x00\x00 strata generate: --draft dflash2 drafts without the MTP layer and needs --draft-model PACK, --spec 2 or more and --mtp-draft-vocab FILE (or --mtp DIR, for DIR/draft_vocab.bin) \x00--draft-fallback\x00" 534UNKNOWN = b"MZ\x00\x00 a program that has the flag and a message nobody knows \x00--draft-fallback\x00" 535NONE = b"MZ\x00\x00 an engine from before DFlash2 ran alone: --draft dflash2 --draft-model \x00" 536 537def setUp (self ): 538self .tmp = tempfile .TemporaryDirectory () 539self .addCleanup (self .tmp .cleanup ) 540self .root = Path (self .tmp .name ) 541self .data = self .root / "data" 542self .mtp = self .data / "mtp" / "rt" 543self .mtp .mkdir (parents = True ) 544 (self .mtp / "experts.bin" ).write_bytes (b"e" ) 545 (self .mtp / "draft_vocab.bin" ).write_bytes (b"v" ) 546self .pack = drafting .pack_path (self .data ) 547self .pack .parent .mkdir (parents = True ) 548self .pack .write_bytes (b"x" * (2 << 20 )) 549self .args = ["--pack" ,str (self .data / "packs" / "gyro" ),"--native" ,"/d/m.gguf" ,"--mtp" ,str (self .mtp ),"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 550 551def exe (self ,name :str ,body :bytes )-> Path : 552path = self .root / name 553path .write_bytes (body ) 554return path 555 556def apply (self ,engine ,** extra ): 557cfg = {"model_name" :GYRO ,"args" :list (self .args ),** extra } 558decision = drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],engine = engine ) 559return decision ,cfg ["args" ] 560 561def test_the_program_is_read_and_the_answer_kept_per_file (self ): 562self .assertEqual (drafting .engine_form (self .exe ("old.exe" ,self .OLD )),drafting .FORM_DIR ) 563self .assertEqual (drafting .engine_form (self .exe ("new.exe" ,self .NEW )),drafting .FORM_FILE ) 564self .assertIsNone (drafting .engine_form (self .exe ("odd.exe" ,self .UNKNOWN )),"the flag is there but the message is not known: the usual form" ) 565self .assertEqual (drafting .engine_form (self .exe ("none.exe" ,self .NONE )),drafting .FORM_NONE ,"no --draft-fallback: no DFlash2 on its own" ) 566self .assertIsNone (drafting .engine_form (None )) 567self .assertIsNone (drafting .engine_form (self .root / "missing.exe" )) 568self .assertIsNone (drafting .engine_form (self .exe ("empty.exe" ,b"" )),"an empty file cannot be read: the usual form" ) 569# a newer program in the same place is read again (size and time are part of the key) 570path = self .exe ("swap.exe" ,self .OLD ) 571self .assertEqual (drafting .engine_form (path ),drafting .FORM_DIR ) 572path .write_bytes (self .NEW + b"more" ) 573self .assertEqual (drafting .engine_form (path ),drafting .FORM_FILE ) 574 575def test_the_vocabulary_flag_follows_the_engine (self ): 576vocab = self .mtp / "draft_vocab.bin" 577usual = drafting .engine_flags ("/p" ,vocab ) 578self .assertEqual (usual ,drafting .engine_flags ("/p" ,vocab ,drafting .FORM_FILE )) 579self .assertIn ("--mtp-draft-vocab" ,usual ) 580self .assertNotIn ("--mtp" ,usual ) 581old = drafting .engine_flags ("/p" ,vocab ,drafting .FORM_DIR ) 582self .assertEqual (old [old .index ("--mtp" )+ 1 ],str (self .mtp ),"the folder that holds draft_vocab.bin" ) 583self .assertNotIn ("--mtp-draft-vocab" ,old ) 584self .assertEqual (old [:6 ], ("--draft" ,"dflash2" ,"--draft-model" ,"/p" ,"--draft-fallback" ,"none" ),"still no MTP layer: the fallback is none" ) 585self .assertEqual (old [- 4 :], ("--spec" ,"6" ,"--draft-conf" ,"0.35" )) 586self .assertNotIn ("--mtp" ,drafting .engine_flags ("/p" ,None ,drafting .FORM_DIR ),"no vocabulary, no flag" ) 587 588def test_an_engine_that_takes_mtp_dir_gets_dflash2_alone_that_way (self ): 589decision ,args = self .apply (self .exe ("old.exe" ,self .OLD )) 590self .assertEqual ((decision .drafter ,decision .note ), ("dflash2" ,"" )) 591self .assertNotIn ("--mtp-draft-vocab" ,args ) 592self .assertEqual (args [args .index ("--mtp" )+ 1 ],str (self .mtp )) 593self .assertEqual (args [args .index ("--draft-fallback" )+ 1 ],"none" ,"the MTP layer is still not loaded" ) 594for flag in ("--spec-min-p" ,"--batch-mtp" ,"--draft-mtp-spec" ): 595self .assertNotIn (flag ,args ) 596self .assertEqual (args .count ("--mtp" ),1 ) 597self .assertEqual (args .count ("--spec" ),1 ) 598# the same start for the newer engine and for one nobody can tell: the vocabulary file alone 599for body in (self .NEW ,self .UNKNOWN ): 600decision ,args = self .apply (self .exe ("x.exe" ,body )) 601self .assertEqual (decision .drafter ,"dflash2" ) 602self .assertEqual (args [args .index ("--mtp-draft-vocab" )+ 1 ],str (self .mtp / "draft_vocab.bin" )) 603self .assertNotIn ("--mtp" ,args ) 604decision ,args = self .apply (None ) 605self .assertIn ("--mtp-draft-vocab" ,args ) 606 607def test_with_no_mtp_folder_the_old_form_names_the_dflash2_folder_that_holds_the_vocabulary (self ): 608own = drafting .vocab_target (self .data ) 609own .parent .mkdir (parents = True ,exist_ok = True ) 610own .write_bytes (b"v" ) 611args = ["--pack" ,str (self .data / "packs" / "gyro" ),"--native" ,"/d/m.gguf" ] 612cfg = {"model_name" :GYRO ,"args" :args } 613decision = drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],engine = self .exe ("old.exe" ,self .OLD )) 614self .assertEqual (decision .drafter ,"dflash2" ) 615self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--mtp" )+ 1 ],str (own .parent )) 616 617def test_a_vocabulary_that_is_not_called_draft_vocab_cannot_go_by_mtp_dir (self ): 618custom = self .root / "my-subset.bin" 619custom .write_bytes (b"v" ) 620args = [* self .args ,"--mtp-draft-vocab" ,str (custom )] 621cfg = {"model_name" :GYRO ,"args" :args } 622decision = drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],engine = self .exe ("old.exe" ,self .OLD )) 623self .assertEqual ((decision .drafter ,decision .note ), ("mtp" ,drafting .NOT_HERE ),"said in one line; the MTP runs" ) 624self .assertNotIn ("--draft" ,cfg ["args" ]) 625cfg = {"model_name" :GYRO ,"args" :list (args )} 626decision = drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ],engine = self .exe ("new.exe" ,self .NEW )) 627self .assertEqual (decision .drafter ,"dflash2" ) 628self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--mtp-draft-vocab" )+ 1 ],str (custom ),"an engine that takes the file keeps the person's own subset" ) 629 630def test_an_engine_without_the_fallback_flag_never_gets_a_dflash2_flag (self ): 631for mode in ("auto" ,"dflash2" ): 632decision ,args = self .apply (self .exe ("none.exe" ,self .NONE ),drafting = mode ) 633self .assertEqual ((decision .drafter ,decision .note ), ("mtp" ,drafting .NOT_HERE ),mode ) 634for flag in ("--draft" ,"--draft-model" ,"--draft-fallback" ,"--draft-conf" ,"--mtp-draft-vocab" ): 635self .assertNotIn (flag ,args , (mode ,flag )) 636self .assertEqual (args [args .index ("--mtp" )+ 1 ],str (self .mtp ),"the MTP, as the config had it" ) 637 638def test_the_server_gives_the_engine_program_to_the_drafting_rule (self ): 639from serve .server import engine_args 640old ,new = self .exe ("old.exe" ,self .OLD ),self .exe ("new.exe" ,self .NEW ) 641cfg = {"model_name" :GYRO ,"args" :list (self .args ),"drafting" :"auto" } 642with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 643a_old ,a_new ,a_none = engine_args (cfg ,str (old )),engine_args (cfg ,str (new )),engine_args (cfg ) 644self .assertEqual (a_old [a_old .index ("--mtp" )+ 1 ],str (self .mtp )) 645self .assertNotIn ("--mtp-draft-vocab" ,a_old ) 646self .assertIn ("--mtp-draft-vocab" ,a_new ) 647self .assertIn ("--mtp-draft-vocab" ,a_none ,"no program given (a test, a tool): the usual form" ) 648# "mtp": false does not take the vocabulary folder out of a DFlash2 start 649off = {"model_name" :GYRO ,"args" :list (self .args ),"drafting" :"dflash2" ,"mtp" :False } 650with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 651a = engine_args (off ,str (old )) 652self .assertEqual (a [a .index ("--mtp" )+ 1 ],str (self .mtp )) 653 654def test_the_retry_arguments_exist_only_for_an_auto_start_that_picked_dflash2 (self ): 655from serve .server import mtp_retry_args 656old = str (self .exe ("old.exe" ,self .OLD )) 657base = {"model_name" :GYRO ,"args" :list (self .args )} 658with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 659alt = mtp_retry_args ({** base ,"drafting" :"auto" },old , ["--effort" ,"x" ]) 660self .assertIsNotNone (alt ) 661self .assertEqual (alt [:len (self .args )],self .args ,"the MTP, as the config had it" ) 662self .assertNotIn ("--draft" ,alt ) 663self .assertEqual (alt [- 2 :], ["--effort" ,"x" ],"what the start adds goes along" ) 664self .assertIsNotNone (mtp_retry_args (dict (base ),old ),"no choice saved means Auto" ) 665self .assertIsNone (mtp_retry_args ({** base ,"drafting" :"dflash2" },old ),"named by the person: it fails with its reason" ) 666self .assertIsNone (mtp_retry_args ({** base ,"drafting" :"mtp" },old ),"nothing to fall back from" ) 667self .assertIsNone (mtp_retry_args ({** base ,"drafting" :"off" },old )) 668self .assertIsNone (mtp_retry_args ({"model_name" :"qwen3.6-35b-a3b" ,"args" :list (self .args ),"drafting" :"auto" },old ),"another model has no DFlash2" ) 669# no MTP layer on this computer: the other start would be DFlash2 again, so there is nothing to retry with 670for name in ("experts.bin" ,"draft_vocab.bin" ): 671 (self .mtp / name ).unlink () 672self .assertIsNone (mtp_retry_args ({** base ,"drafting" :"auto" },old )) 673# the lines and the note of the real start are not touched by working the retry out 674drafting .take_note () 675with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 676mtp_retry_args ({** base ,"drafting" :"auto" },old ) 677self .assertEqual (drafting .take_note (),"" ) 678 679def test_the_no_drafter_retry_exists_for_an_auto_start_with_a_drafter_and_never_swaps_in_the_mtp (self ): 680"""A start that ended for lack of graphics memory is repeated with no drafter (multi-card walk): DFlash2 -> MTP would take MORE memory from a card that is short.""" 681from serve .server import nodraft_retry_args 682old = str (self .exe ("old.exe" ,self .OLD )) 683base = {"model_name" :GYRO ,"args" :list (self .args )} 684with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 685alt = nodraft_retry_args ({** base ,"drafting" :"auto" },old , ["--effort" ,"x" ]) 686self .assertIsNotNone (alt ) 687for flag in ("--draft" ,"--draft-model" ,"--draft-fallback" ,"--mtp" ,"--mtp-draft-vocab" ): 688self .assertNotIn (flag ,alt ,flag ) 689self .assertEqual (alt [- 2 :], ["--effort" ,"x" ],"what the start adds goes along" ) 690self .assertIn ("--native" ,alt ,"everything else is the same start" ) 691self .assertIsNotNone (nodraft_retry_args (dict (base ),old ),"no choice saved means Auto" ) 692self .assertIsNone (nodraft_retry_args ({** base ,"drafting" :"dflash2" },old ),"named by the person: it fails with its reason" ) 693self .assertIsNone (nodraft_retry_args ({** base ,"drafting" :"mtp" },old ),"named by the person: it fails with its reason" ) 694self .assertIsNone (nodraft_retry_args ({** base ,"drafting" :"off" },old ),"no drafter to drop" ) 695drafting .take_note () 696with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 697nodraft_retry_args ({** base ,"drafting" :"auto" },old ) 698self .assertEqual (drafting .take_note (),"" ,"working the retry out leaves the note of the real start alone" ) 699 700def test_the_engine_sources_carry_the_words_the_program_is_read_for (self ): 701src = (ROOT / "src" / "program" / "generate.cpp" ).read_text (encoding = "utf-8" ) 702self .assertIn (drafting ._MARK_FILE .decode (),src ,"rc1's start message is what marks an engine that takes the vocabulary file alone" ) 703self .assertIn (drafting ._MARK_FALLBACK .decode (),src ) 704 705 706class WhatTheEngineSaid (unittest .TestCase ): 707ARGS = ["--mtp" ,"/m" ,"--draft" ,"dflash2" ,"--draft-model" ,"/p" ] 708 709def test_no_room_in_graphics_memory (self ): 710log = ("strata generate: --draft dflash2: could not allocate the drafter's buffers. DFlash2 needs about 1.2 GiB of VRAM (the pack 0.76 GiB, " 711"its buffers and K/V ring 0.25 GiB, a draft head 0.2 GiB); start with --vram-reserve-mib 1900 to keep it. The MTP drafts instead.\n" ) 712info = drafting .engine_info (self .ARGS ,log ) 713self .assertEqual (info , {"drafting" :"mtp" ,"drafting_note" :"Drafting: MTP (DFlash2 didn't fit)" }) 714 715def test_a_mode_this_build_does_not_run (self ): 716log = "strata generate: --draft dflash2 is off: it is for one card and the CLI run (not --serve, --batch or a layer split) in this build; the MTP drafts instead\n" 717self .assertEqual (drafting .engine_info (self .ARGS ,log )["drafting_note" ],"Drafting: MTP (DFlash2 isn't available in this mode)" ) 718 719def test_it_ran (self ): 720log = "strata generate: DFlash2 drafter on (/p, graphs captured in 3 ms), the MTP layer stays as the fallback\n" 721self .assertEqual (drafting .engine_info (self .ARGS ,log ), {"drafting" :"dflash2" ,"drafting_note" :"" }) 722self .assertEqual (drafting .fallback_note (log ),"" ) 723 724def test_choices_that_never_reached_the_engine_keep_their_own_words (self ): 725self .assertEqual (drafting .engine_info (["--mtp" ,"/m" ],"" ,drafting .NOT_SUPPORTED )["drafting_note" ], 726"Drafting: MTP (DFlash2 is for Qwen3.8-Flash-Next only)" ) 727self .assertEqual (drafting .engine_info (["--mtp" ,"/m" ]), {"drafting" :"mtp" ,"drafting_note" :"" }) 728self .assertEqual (drafting .engine_info (["--spec" ,"4" ]), {"drafting" :"off" ,"drafting_note" :"" }) 729 730def test_summary_line (self ): 731self .assertEqual (drafting .summary_line (drafting .Decision ("auto" ,"mtp" )),"Drafting: MTP" ) 732self .assertEqual (drafting .summary_line (drafting .Decision ("dflash2" ,"dflash2" )),"Drafting: DFlash2" ) 733self .assertEqual (drafting .summary_line (drafting .Decision ("off" ,"off" )),"Drafting: Off" ) 734self .assertEqual (drafting .summary_line (drafting .Decision ("dflash2" ,"mtp" ,drafting .NOT_DOWNLOADED )), 735"Drafting: MTP (the DFlash2 drafter isn't downloaded yet)" ) 736 737 738class WordsMatchTheApp (unittest .TestCase ): 739def test_every_line_the_engine_start_can_say_is_in_the_apps_catalog_in_all_nine_languages (self ): 740folder = ROOT / "apps" / "host" / "src" / "locales" 741catalogs = {l : {** json .loads ((folder / f" { l } .json" ).read_text (encoding = "utf-8" )),** json .loads ((folder / f"df2- { l } .json" ).read_text (encoding = "utf-8" ))} 742for l in ("en" ,"de" ,"es" ,"fr" ,"ja" ,"ko" ,"pt" ,"ru" ,"zh" )} 743ids = {v :k for k ,v in catalogs ["en" ].items ()} 744self .assertEqual (len (drafting .REASONS ),10 ) 745lines = [drafting .mtp_line (r )for r in (drafting .DIDNT_FIT ,drafting .NOT_HERE ,drafting .NOT_SUPPORTED ,drafting .NOT_DOWNLOADED ,drafting .NOT_THIS_CARD ,drafting .DIDNT_START )] 746lines += [drafting .line ("dflash2" ,drafting .NO_MTP ),drafting .line ("off" ,drafting .NONE_HERE ),drafting .line ("off" ,drafting .SHORT_OF_VRAM ),drafting .line ("mtp" ,drafting .NONE_IN_SLOTS )] 747for line in lines : 748self .assertIn (line ,ids ,line ) 749for lang ,catalog in catalogs .items (): 750self .assertTrue (catalog [ids [line ]].strip (),f" { line } in { lang } " ) 751# and what the engine's log makes of them 752self .assertEqual (drafting .fallback_note ("--draft dflash2: out of memory" ),drafting .mtp_line (drafting .DIDNT_FIT )) 753self .assertEqual (drafting .fallback_note ("--draft dflash2 is off: it is for one card and the CLI run" ),drafting .mtp_line (drafting .NOT_HERE )) 754 755 756class TerminalFlag (unittest .TestCase ): 757BASE = {"exe" :"component:strata-engine" ,"args" : ["--pack" ,"/d/packs/x" ,"--mtp" ,"/d/mtp/rt" ],"port" :8080 ,"model_name" :GYRO } 758 759def test_the_flag_is_listed_planned_copyable_and_saved_in_the_launch_config (self ): 760code ,out ,_ = run_cli (["launch" ,"--help" ]) 761self .assertIn ("--drafting" ,out ) 762self .assertIn ("auto|mtp|dflash2|off" ,out ) 763cfg = derive_config (self .BASE ,S .LaunchSettings (drafting = "dflash2" ),bool_flags = frozenset ()) 764self .assertEqual (cfg ["drafting" ],"dflash2" ) 765self .assertNotIn ("drafting" ,derive_config (self .BASE ,S .LaunchSettings (),bool_flags = frozenset ()),"Auto writes nothing: nobody chose" ) 766self .assertIn ("--drafting dflash2" ,S .flags_text (S .LaunchSettings (drafting = "dflash2" ))) 767self .assertNotIn ("--drafting" ,S .flags_text (S .LaunchSettings ())) 768code ,out ,err = run_cli (["launch" ,"--plan" ,"--drafting" ,"fastest" ]) 769self .assertNotEqual (code ,0 ) 770 771def test_a_saved_preset_keeps_the_choice (self ): 772s = S .LaunchSettings .from_dict (S .LaunchSettings (drafting = "mtp" ).to_dict ()) 773self .assertEqual (s .drafting ,"mtp" ) 774self .assertIsNone (S .LaunchSettings .from_dict ({}).drafting ) 775 776 777class _Handler (http .server .BaseHTTPRequestHandler ): 778files :dict = {} 779hits :list = [] 780 781def do_GET (self ): 782body = self .files .get (self .path .rsplit ("/" ,1 )[- 1 ]) 783self .hits .append (self .path ) 784if body is None : 785self .send_error (404 ) 786return 787start = 0 788rng = self .headers .get ("Range" ) 789if rng and rng .startswith ("bytes=" ): 790start = int (rng [6 :].split ("-" )[0 ]) 791self .send_response (206 if start else 200 ) 792if start : 793self .send_header ("Content-Range" ,f"bytes { start } - { len ( body ) - 1 } / { len ( body ) } " ) 794self .send_header ("Content-Length" ,str (len (body )- start )) 795self .end_headers () 796self .wfile .write (body [start :]) 797 798def log_message (self ,* _ ): 799pass 800 801 802class FetchAndPack (unittest .TestCase ): 803@classmethod 804def setUpClass (cls ): 805sys .path .insert (0 ,str (ROOT / "tools" )) 806spec = importlib .util .spec_from_file_location ("t_pack" ,ROOT / "tools" / "test_dflash2_pack.py" ) 807cls .synth = importlib .util .module_from_spec (spec ) 808spec .loader .exec_module (cls .synth ) 809 810def setUp (self ): 811self .tmp = tempfile .TemporaryDirectory () 812self .addCleanup (self .tmp .cleanup ) 813self .dir = Path (self .tmp .name ) 814self .synth .write_safetensors (self .dir / "m.safetensors" ,self .synth .tensors ()) 815 (self .dir / "c.json" ).write_text (json .dumps (self .synth .CFG )) 816import hashlib 817self .blobs = {"config.json" : (self .dir / "c.json" ).read_bytes (),"model.safetensors" : (self .dir / "m.safetensors" ).read_bytes ()} 818self .files = tuple ((n ,len (b ),hashlib .sha256 (b ).hexdigest ())for n ,b in self .blobs .items ()) 819_Handler .files ,_Handler .hits = dict (self .blobs ), [] 820self .server = http .server .ThreadingHTTPServer (("127.0.0.1" ,0 ),_Handler ) 821threading .Thread (target = self .server .serve_forever ,daemon = True ).start () 822self .addCleanup (self .server .shutdown ) 823self .base = f"http://127.0.0.1: { self . server . server_address [ 1 ] } /" 824self .data = self .dir / "data" 825self .steps :list = [] 826# the packed test file is a few hundred kB, not the real 0.76 GB: "ready" asks for more than 1 MiB by default 827patcher = mock .patch .object (drafting ,"pack_ready" ,lambda d :bool (d )and drafting .pack_path (d ).is_file ()) 828patcher .start () 829self .addCleanup (patcher .stop ) 830 831def fetch (self ,** kw ): 832with mock .patch .dict ("os.environ" , {"STRATA_UPDATE_ALLOW_HTTP" :"1" }): 833return drafting_fetch .ensure (self .data ,progress = lambda * a :self .steps .append (a ),base_url = self .base ,files = self .files ,** kw ) 834 835def test_the_pinned_files_are_the_repositorys_own (self ): 836self .assertEqual (drafting .REVISION ,"a22a1744e85c17364dcc54a6a420af7135d4c7f6" ) 837self .assertTrue (drafting .BASE_URL .endswith (f"/ { drafting . REVISION } /" ),"a revision, never a branch name" ) 838names = {n : (size ,sha )for n ,size ,sha in drafting .FILES } 839self .assertEqual (names ["model.safetensors" ], (1206140008 ,"d6723f2a359eaf33ec0c95f22ae8edda896774020c552296a85e1913b4d3186f" )) 840self .assertEqual (len (names ["config.json" ][1 ]),64 ) 841self .assertEqual (drafting .LICENSE ,"Apache-2.0" ) 842 843def test_it_downloads_checks_packs_and_removes_the_source (self ): 844out = self .fetch () 845self .assertEqual (out ,drafting .pack_path (self .data )) 846self .assertEqual (out .read_bytes ()[:4 ],b"DF2P" ) 847self .assertFalse ((out .parent / "src" ).exists (),"the 1.2 GB source file does not stay on disk" ) 848words = [s [0 ]for s in self .steps ] 849self .assertIn ("Downloading the DFlash2 drafter…" ,words ) 850self .assertIn ("Packing the DFlash2 drafter for the engine…" ,words ) 851self .assertEqual (words [- 1 ],"The DFlash2 drafter is ready." ) 852self .assertEqual (max (s [1 ]for s in self .steps if s [0 ].startswith ("Downloading" )),sum (f [1 ]for f in self .files )) 853 854def test_a_drafter_that_is_here_is_not_fetched_again (self ): 855self .fetch () 856_Handler .hits .clear () 857self .steps .clear () 858self .fetch () 859self .assertEqual (_Handler .hits , []) 860self .assertEqual (self .steps [0 ][0 ],"The DFlash2 drafter is ready." ) 861 862def test_a_fresh_install_that_selected_only_dflash2_has_everything_the_engine_needs (self ): 863"""No MTP layer was ever downloaded (no mtp folder at all): the pack comes with the draft vocabulary, and the engine arguments carry 864--mtp-draft-vocab for it and no --mtp.""" 865self .assertFalse ((self .data / "mtp" ).exists ()) 866self .assertFalse (drafting .vocab_target (self .data ).exists ()) 867self .fetch () 868vocab = drafting .vocab_target (self .data ) 869self .assertEqual (vocab ,self .data / drafting .FOLDER / "draft_vocab.bin" ) 870self .assertTrue (vocab .is_file ()and vocab .stat ().st_size > 0 ) 871self .assertEqual (vocab .read_bytes (),drafting .shipped_vocab ().read_bytes (),"the subset Strata ships" ) 872args = ["--pack" ,str (self .data / "packs" / "gyro" ),"--native" ,"/m.gguf" ,"--mtp" ,str (self .data / "mtp" / "rt" ),"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 873# the Strata folder's own copy is not needed any more: the install keeps its own 874with mock .patch .object (drafting ,"shipped_vocab" ,return_value = self .dir / "gone.bin" ): 875cfg = {"model_name" :GYRO ,"args" :list (args )} 876d = drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ]) 877self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" )) 878a = cfg ["args" ] 879self .assertNotIn ("--mtp" ,a ) 880self .assertEqual (a [a .index ("--mtp-draft-vocab" )+ 1 ],str (vocab )) 881self .assertEqual (a [a .index ("--draft-model" )+ 1 ],str (drafting .pack_path (self .data ))) 882self .assertEqual (a [a .index ("--draft-fallback" )+ 1 ],"none" ) 883# and through the server, as a start builds it 884from serve .server import engine_args 885with mock .patch ("serve.server.drafting_vram_mib" ,return_value = None ),mock .patch .object (drafting ,"data_dir_of" ,return_value = self .data ): 886served = engine_args ({"model_name" :GYRO ,"args" :list (args )}) 887self .assertNotIn ("--mtp" ,served ) 888self .assertTrue (Path (served [served .index ("--mtp-draft-vocab" )+ 1 ]).is_file ()) 889 890def test_the_vocabulary_is_provided_once_and_never_overwritten (self ): 891out = drafting .provide_vocab (self .data ) 892self .assertEqual (out ,drafting .vocab_target (self .data )) 893out .write_bytes (b"mine" ) 894self .assertEqual (drafting .provide_vocab (self .data ).read_bytes (),b"mine" ) 895self .assertIsNone (drafting .provide_vocab (None )) 896with mock .patch .object (drafting ,"shipped_vocab" ,return_value = self .dir / "gone.bin" ): 897self .assertIsNone (drafting .provide_vocab (self .dir / "other" )) 898 899def test_without_any_vocabulary_dflash2_does_not_start_and_says_so (self ): 900"""The engine refuses a missing vocabulary file; the app never gets there: no file anywhere means the MTP (or none), in one line.""" 901self .fetch () 902args = ["--pack" ,str (self .data / "packs" / "gyro" ),"--mtp" ,str (self .data / "mtp" / "rt" )] 903drafting .vocab_target (self .data ).unlink () 904with mock .patch .object (drafting ,"shipped_vocab" ,return_value = self .dir / "gone.bin" ): 905cfg = {"model_name" :GYRO ,"args" :list (args )} 906d = drafting .apply (cfg ,data_dir = self .data ,backend = "cuda" ,archs = ["sm_120" ]) 907self .assertEqual ((d .drafter ,d .note ), ("off" ,drafting .NONE_HERE )) 908self .assertNotIn ("--draft" ,cfg ["args" ]) 909 910def test_a_file_that_does_not_match_its_hash_is_deleted_and_the_words_are_plain (self ): 911bad = tuple ((n ,size ,"0" * 64 )for n ,size ,_h in self .files ) 912with mock .patch .dict ("os.environ" , {"STRATA_UPDATE_ALLOW_HTTP" :"1" }): 913with self .assertRaises (drafting_fetch .DraftingError )as ctx : 914drafting_fetch .ensure (self .data ,base_url = self .base ,files = bad ) 915self .assertIn ("did not pass the safety check" ,str (ctx .exception )) 916self .assertFalse (drafting .pack_path (self .data ).exists ()) 917self .assertEqual (list ((drafting .pack_path (self .data ).parent / "src" ).glob ("*.part" )), []) 918 919def test_an_interrupted_download_is_continued (self ): 920name ,size ,_sha = self .files [1 ] 921src = drafting .pack_path (self .data ).parent / "src" 922src .mkdir (parents = True ) 923 (src / (name + ".part" )).write_bytes (self .blobs [name ][:size // 2 ]) 924self .fetch () 925ranges = [h for h in _Handler .hits if h .endswith (name )] 926self .assertEqual (len (ranges ),1 ) 927self .assertTrue (drafting .pack_path (self .data ).is_file ()) 928 929def test_a_start_never_downloads (self ): 930with acquisition .forbidden (): 931with self .assertRaises (acquisition .AcquisitionRefused ): 932drafting_fetch .ensure (self .data ,base_url = self .base ,files = self .files ) 933self .assertFalse (drafting .pack_path (self .data ).exists ()) 934 935def test_the_plan_names_the_size_and_where_it_comes_from (self ): 936with mock .patch .object (drafting ,"pack_ready" ,return_value = False ): 937plan = drafting_fetch .plan (self .data ) 938self .assertEqual (plan ["gb" ],1.2 ) 939self .assertIn ("1.2 GB to download" ,plan ["text" ]) 940self .assertIn ("0.52 GB" ,plan ["text" ]) 941self .assertIn ("incoai/Qwen3.8-Flash-Next-DFlash2" ,plan ["text" ]) 942self .assertIn ("Apache-2.0" ,plan ["text" ]) 943self .assertIn ("It needs about 0.9 GB of graphics memory" ,plan ["text" ],"the figure of DFlash2 alone" ) 944with mock .patch .object (drafting ,"pack_ready" ,return_value = True ): 945self .assertEqual (drafting_fetch .plan (self .data )["gb" ],0.0 ) 946 947def test_the_command_line_says_the_same (self ): 948code ,out ,err = run_cli (["drafting" ,"plan" ,"--data-dir" ,str (self .data ),"--output" ,"json" ]) 949self .assertEqual (code ,0 ,err ) 950payload = json .loads (out ) 951self .assertEqual ((payload ["ok" ],payload ["revision" ]), (True ,drafting .REVISION )) 952self .assertEqual (payload ["gb" ],1.2 ) 953 954 955class TheAppAsksControl (unittest .TestCase ): 956"""The app calls `strata drafting plan|fetch --config <run config> --mode <choice>` before a load; Control decides, the app never reads the table.""" 957 958def setUp (self ): 959self .tmp = tempfile .TemporaryDirectory () 960self .addCleanup (self .tmp .cleanup ) 961self .data = Path (self .tmp .name )/ "data" 962self .cfg = Path (self .tmp .name )/ "strata-gyro.json" 963self .cfg .write_text (json .dumps ({"model_name" :GYRO ,"args" : ["--pack" ,str (self .data / "packs" / "gyro" ),"--mtp" ,str (self .data / "mtp" / "rt" )]})) 964 965def plan (self ,* extra ): 966code ,out ,err = run_cli (["drafting" ,"plan" ,"--config" ,str (self .cfg ),"--output" ,"json" ,* extra ]) 967self .assertEqual (code ,0 ,err ) 968return json .loads (out ) 969 970def test_nothing_is_needed_for_mtp_or_off_and_the_pack_for_auto_and_dflash2 (self ): 971for mode in ("mtp" ,"off" ): 972plan = self .plan ("--mode" ,mode ) 973self .assertFalse (plan ["needed" ],mode ) 974self .assertEqual (plan ["gb" ],0.0 ) 975for mode in ("auto" ,"dflash2" ): 976plan = self .plan ("--mode" ,mode ) 977self .assertTrue (plan ["needed" ],mode ) 978self .assertEqual ((plan ["gb" ],plan ["choice" ]), (1.2 ,"dflash2" )) 979self .assertEqual (self .plan ("--mode" ,"off" )["drafter" ],"off" ) 980 981def test_choosing_the_mtp_without_its_layer_says_the_layer_is_needed (self ): 982plan = self .plan ("--mode" ,"mtp" ) 983self .assertTrue (plan ["mtp_needed" ]) 984self .assertEqual ((plan ["mtp_gb" ],plan ["choice" ]), (5.2 ,"mtp" )) 985self .assertFalse (self .plan ("--mode" ,"auto" )["mtp_needed" ]) 986 987def test_dflash2_is_needed_once_the_engine_can_serve_it_and_not_before (self ): 988 (self .data / "mtp" / "rt" ).mkdir (parents = True ) 989 (self .data / "mtp" / "rt" / "experts.bin" ).write_bytes (b"e" )# the MTP layer is on this computer 990with mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,False ): 991before = self .plan ("--mode" ,"dflash2" ) 992self .assertFalse (before ["needed" ],"while the engine cannot serve it nothing is downloaded" ) 993self .assertEqual (before ["line" ],"Drafting: MTP (DFlash2 isn't available in this mode)" ) 994with serving_dflash2 (): 995plan = self .plan ("--mode" ,"dflash2" ) 996self .assertTrue (plan ["needed" ]) 997self .assertEqual (plan ["gb" ],1.2 ) 998self .assertEqual (Path (plan ["pack" ]),drafting .pack_path (self .data ),"the data folder comes from the run config's own arguments" ) 999self .cfg .write_text (json .dumps ({"model_name" :"qwen3.8-flash-next-coder-iq3_xxs" ,"drafting" :"dflash2" ,"args" : ["--pack" ,str (self .data / "packs" / "c" )]})) 1000other = self .plan () 1001self .assertFalse (other ["needed" ]) 1002self .assertEqual (other ["note" ],drafting .NOT_SUPPORTED ) 1003 1004def test_a_model_that_is_not_installed_yet_is_asked_about_by_its_family (self ): 1005with serving_dflash2 (): 1006for family ,needed in (("gyro" ,True ), ("qwen" ,True ), ("coder" ,False ), ("swift" ,False )): 1007code ,out ,err = run_cli (["drafting" ,"plan" ,"--family" ,family ,"--mode" ,"dflash2" ,"--data-dir" ,str (self .data ),"--output" ,"json" ]) 1008self .assertEqual (code ,0 ,err ) 1009self .assertEqual (json .loads (out )["needed" ],needed ,family ) 1010 1011def test_fetch_if_needed_does_nothing_when_nothing_is_needed (self ):# (the MTP is chosen: no drafter file is needed) 1012code ,out ,err = run_cli (["drafting" ,"fetch" ,"--config" ,str (self .cfg ),"--mode" ,"mtp" ,"--if-needed" ,"--output" ,"jsonl" ]) 1013self .assertEqual (code ,0 ,err ) 1014last = json .loads (out .strip ().splitlines ()[- 1 ]) 1015self .assertEqual ((last ["ok" ],last ["skipped" ],last ["pack" ]), (True ,True ,None )) 1016self .assertFalse (drafting .pack_path (self .data ).exists ()) 1017 1018 1019class TheDownloadPlanListsIt (unittest .TestCase ): 1020"""`strata launch --plan --drafting dflash2` adds the drafter to the download when it would be used.""" 1021 1022def setUp (self ): 1023from tests .terminal .launch_support import not_intel 1024not_intel (self ) 1025 1026def test_the_plan_line_and_the_part (self ): 1027from tests .terminal .launch_support import HW_12 ,Home ,catalog 1028from strata_cli .launch .runner import Runner 1029with Home ()as home : 1030runner = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ,vision = "off" ,drafting = "dflash2" ),catalog = catalog (HW_12 )) 1031with mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,False ): 1032line = runner .drafting_plan_line () 1033self .assertEqual (line ,"Drafting: MTP (DFlash2 isn't available in this mode)" ) 1034self .assertNotIn ("dflash2" , [p ["kind" ]for p in runner .download_plan ()["parts" ]],"nothing to fetch while the engine cannot serve it" ) 1035with serving_dflash2 (): 1036kinds = {p ["kind" ]:p for p in runner .download_plan ()["parts" ]} 1037self .assertEqual (kinds ["dflash2" ]["gb" ],1.2 ) 1038self .assertIn ("the DFlash2 drafter (1.2 GB)" ,runner .download_plan ()["text" ]) 1039self .assertIn ("--drafting mtp" ,runner .download_plan ()["text" ]) 1040auto = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 )) 1041self .assertIn ("dflash2" , [p ["kind" ]for p in auto .download_plan ()["parts" ]],"Auto is DFlash2: its drafter is the download" ) 1042mtp = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ,vision = "off" ,drafting = "mtp" ),catalog = catalog (HW_12 )) 1043kinds = [p ["kind" ]for p in mtp .download_plan ()["parts" ]] 1044self .assertNotIn ("dflash2" ,kinds ) 1045self .assertIn ("mtp" ,kinds ,"the MTP is chosen: its layer is the download, DFlash2's is not" ) 1046self .assertNotIn ("mtp" , [p ["kind" ]for p in auto .download_plan ()["parts" ]],"one drafter per run: a new install fetches only the one it will use" ) 1047other = Runner (S .LaunchSettings (family = "coder" ,model = "IQ3_XXS" ,context = 32768 ,vision = "off" ,drafting = "dflash2" ),catalog = catalog (HW_12 )) 1048self .assertNotIn ("dflash2" , [p ["kind" ]for p in other .download_plan ()["parts" ]],"not for this model: hidden, not offered" ) 1049 1050 1051if __name__ == "__main__" : 1052unittest .main () 1053 1054 1055class ControlReportsWhetherTheEngineServesIt (unittest .TestCase ): 1056def test_the_plan_says_it_so_the_app_can_hide_the_choice (self ): 1057for served in (False ,True ): 1058with mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,served ): 1059code ,out ,err = run_cli (["drafting" ,"plan" ,"--mode" ,"auto" ,"--family" ,"gyro" ,"--data-dir" ,tempfile .gettempdir (),"--output" ,"json" ]) 1060self .assertEqual (code ,0 ,err ) 1061self .assertIs (json .loads (out )["serves" ],served )