okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1"""0.2.13 user-facing gaps of the tuned DFlash2 drafter (release 0.2.12 checks, RESULTS-DF2-DL): the plan names the drafter Auto will really fetch and its size, the 2Terminal's plan sees a stock pack from 0.2.11 as "the tuned one is still to come", and a load nobody asked the drafter for can fetch with the 12 hour miss rule and no 3words when the feed is not there.""" 4 5from __future__import annotations 6 7import json 8import tempfile 9import unittest 10from pathlib import Path 11from unittest import mock 12 13from strata_cli .launch import settings as S 14from strata_control import drafting ,drafting_fetch 15from tests .terminal .launch_support import HW_12 ,Home ,catalog ,run_cli 16 17STRIX = ("hip" , ["gfx1151" ]) 18SPARK = ("cuda" , ["sm_121" ]) 19ARC = ("sycl" , ["arc_a" ]) 20Q4 = "qwen3.8-flash-next-unsloth-ud-q4_k_xl" 21 22 23def hardware (pair ): 24return mock .patch .object (drafting ,"detect_hardware" ,return_value = pair ) 25 26 27def serving (): 28return mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,True ) 29 30 31class PlanNamesTheRealDrafter (unittest .TestCase ): 32def setUp (self ): 33self .tmp = tempfile .TemporaryDirectory () 34self .addCleanup (self .tmp .cleanup ) 35self .data = Path (self .tmp .name )/ "data" 36 (self .data / "dflash2" ).mkdir (parents = True ) 37 38def plan (self ,* argv ): 39code ,out ,err = run_cli (["drafting" ,"plan" ,"--data-dir" ,str (self .data ),"--output" ,"json" ,* argv ]) 40self .assertEqual (code ,0 ,err ) 41return json .loads (out ) 42 43def test_with_no_model_the_plan_is_the_old_generic_one (self ): 44with serving (),hardware (STRIX ): 45got = self .plan ("--family" ,"unsloth" ,"--mode" ,"auto" ) 46self .assertEqual ((got ["gb" ],got ["tuned" ]), (1.2 ,False )) 47 48def test_a_model_not_installed_yet_gets_the_tuned_size (self ): 49with serving (),hardware (STRIX ): 50for mode in ("auto" ,"dflash2" ): 51got = self .plan ("--family" ,"unsloth" ,"--model" ,"UD-Q4_K_XL" ,"--mode" ,mode ) 52self .assertEqual ((got ["needed" ],got ["tuned_needed" ],got ["tuned" ],got ["gb" ],got ["drafter_gb" ]), (True ,True ,True ,0.34 ,0.34 ),mode ) 53self .assertEqual (got ["file" ],drafting .TUNED_NAME ) 54self .assertIn ("0.34 GB" ,got ["text" ]) 55self .assertNotIn ("1.2 GB" ,got ["text" ]) 56 57def test_the_spark_and_arc_and_a_full_model_name (self ): 58with serving (),hardware (SPARK ): 59self .assertEqual (self .plan ("--family" ,"qwen" ,"--model" ,"IQ3_S" ,"--mode" ,"auto" )["gb" ],0.34 ) 60with serving (),hardware (ARC ): 61got = self .plan ("--family" ,"qwen" ,"--model" ,"IQ3_XXS" ,"--mode" ,"auto" ) 62self .assertEqual ((got ["gb" ],got ["file" ]), (0.52 ,drafting .TUNED_BF16_NAME )) 63self .assertEqual (self .plan ("--model" ,"qwen3.8-flash-next-iq3_xxs" ,"--mode" ,"auto" )["gb" ],0.52 ,"a run config's own name is taken as it is" ) 64 65def test_a_set_the_tuned_pack_is_not_for_keeps_the_stock_size (self ): 66with serving (),hardware (STRIX ): 67got = self .plan ("--family" ,"qwen" ,"--model" ,"Q2_0" ,"--mode" ,"auto" ) 68self .assertEqual ((got ["tuned" ],got ["gb" ],got ["needed" ]), (False ,1.2 ,True )) 69 70def test_mtp_says_nothing_is_needed_whatever_the_model (self ): 71with serving (),hardware (STRIX ): 72got = self .plan ("--family" ,"unsloth" ,"--model" ,"UD-Q4_K_XL" ,"--mode" ,"mtp" ) 73self .assertEqual ((got ["needed" ],got ["gb" ],got ["text" ]), (False ,0.0 ,"Nothing to download." )) 74_code ,out ,_err = run_cli (["drafting" ,"plan" ,"--data-dir" ,str (self .data ),"--family" ,"unsloth" ,"--model" ,"UD-Q4_K_XL" ,"--mode" ,"mtp" ]) 75self .assertEqual (out .strip (),"Nothing to download." ) 76 77def test_a_stock_pack_from_0_2_11_does_not_hide_the_tuned_download (self ): 78with serving (),hardware (STRIX ),mock .patch .object (drafting ,"pack_ready" ,return_value = True ): 79got = self .plan ("--family" ,"unsloth" ,"--model" ,"UD-Q4_K_XL" ,"--mode" ,"auto" ) 80self .assertEqual ((got ["tuned_needed" ],got ["needed" ],got ["gb" ]), (True ,True ,0.34 )) 81 82 83class TheLaunchPlan (unittest .TestCase ): 84"""`strata launch --plan` for a model that is not installed yet: the number is the tuned drafter's.""" 85 86def setUp (self ): 87from tests .terminal .launch_support import not_intel 88not_intel (self ) 89 90def test_the_download_list_and_the_plan_line_use_the_tuned_pack (self ): 91from strata_cli .launch .runner import Runner 92with Home (): 93with serving (),hardware (STRIX ): 94runner = Runner (S .LaunchSettings (family = "unsloth" ,model = "UD-Q4_K_XL" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 )) 95part = next (p for p in runner .download_plan ()["parts" ]if p ["kind" ]== "dflash2" ) 96self .assertEqual (part ["gb" ],0.34 ) 97self .assertIn ("the DFlash2 drafter (0.34 GB)" ,runner .download_plan ()["text" ]) 98self .assertEqual (runner .drafting_plan_line (),"Drafting: DFlash2 (its drafter is downloaded first)" ) 99stock = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 )) 100self .assertEqual (next (p for p in stock .download_plan ()["parts" ]if p ["kind" ]== "dflash2" )["gb" ],1.2 ) 101mtp = Runner (S .LaunchSettings (family = "unsloth" ,model = "UD-Q4_K_XL" ,context = 32768 ,vision = "off" ,drafting = "mtp" ),catalog = catalog (HW_12 )) 102self .assertNotIn ("dflash2" , [p ["kind" ]for p in mtp .download_plan ()["parts" ]]) 103 104def test_a_stock_pack_already_here_still_lists_the_tuned_one (self ): 105from strata_cli .launch .runner import Runner 106with Home (): 107with serving (),hardware (STRIX ),mock .patch .object (drafting ,"pack_ready" ,return_value = True ): 108runner = Runner (S .LaunchSettings (family = "unsloth" ,model = "UD-Q4_K_XL" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 )) 109self .assertEqual (next (p for p in runner .download_plan ()["parts" ]if p ["kind" ]== "dflash2" )["gb" ],0.34 ) 110 111 112class AnAutomaticFetch (unittest .TestCase ): 113"""`strata drafting fetch --if-needed --throttle`: what the app's plain load, restart and tuning run call.""" 114 115def setUp (self ): 116self .tmp = tempfile .TemporaryDirectory () 117self .addCleanup (self .tmp .cleanup ) 118self .data = Path (self .tmp .name )/ "data" 119 (self .data / "dflash2" ).mkdir (parents = True ) 120self .cfg = Path (self .tmp .name )/ "run.json" 121self .cfg .write_text (json .dumps ({"model_name" :Q4 ,"args" : []}),encoding = "utf-8" ) 122 123def fetch (self ,* extra ): 124return run_cli (["drafting" ,"fetch" ,"--config" ,str (self .cfg ),"--data-dir" ,str (self .data ),"--if-needed" ,"--output" ,"jsonl" ,* extra ]) 125 126def test_the_feed_not_answering_is_silent_with_throttle_and_an_error_without (self ): 127with serving (),hardware (STRIX ),mock .patch .object (drafting_fetch ,"ensure_for" ,return_value = None )as ensure : 128code ,out ,_ = self .fetch ("--throttle" ) 129self .assertEqual (code ,0 ) 130last = json .loads (out .strip ().splitlines ()[- 1 ]) 131self .assertEqual ((last ["ok" ],last ["skipped" ],last ["pack" ]), (True ,True ,None )) 132self .assertTrue (ensure .call_args .kwargs ["throttle" ]) 133code ,out ,_ = self .fetch () 134self .assertEqual (code ,5 ,"the Models screen's own Load keeps saying so" ) 135self .assertFalse (ensure .call_args .kwargs ["throttle" ]) 136 137def test_a_miss_less_than_twelve_hours_old_skips_the_feed (self ): 138 (self .data / "dflash2" / drafting_fetch .MISS_MARK ).write_text ("x" ) 139# After the skipped tuned lookup the fetch falls back to the STOCK drafter (ensure): on a box with internet that is a real 1.2 GB download from 140# Hugging Face, or a hash and pack of the person's own Hugging Face cache (17 minutes and killed on the owner PC, 11-10). This test is about the 141# feed being skipped, so the stock path is kept offline here: no network, no Hugging Face cache. 142with serving (),hardware (STRIX ),mock .patch .object (drafting_fetch ,"tuned_from_feed" )as feed ,mock .patch .object (drafting_fetch .acquisition ,"require" ), \ 143mock .patch .object (drafting_fetch ,"online" ,return_value = False )as reach ,mock .patch .object (drafting_fetch ,"_hub_snapshots" ,return_value = []): 144code ,_out ,_ = self .fetch ("--throttle" ) 145reach .assert_called ()# (the stock path was reached and stayed offline: nothing was downloaded) 146self .assertEqual (code ,0 ) 147feed .assert_not_called () 148 149def test_a_real_fault_is_still_reported (self ): 150fault = drafting_fetch .DraftingError ("The tuned DFlash2 drafter did not pass the safety check, so it was deleted." ) 151with serving (),hardware (STRIX ),mock .patch .object (drafting_fetch ,"ensure_for" ,side_effect = fault ): 152code ,out ,_ = self .fetch ("--throttle" ) 153self .assertEqual (code ,5 ) 154self .assertIn ("safety check" ,json .loads (out .strip ().splitlines ()[- 1 ])["error" ]["message" ]) 155 156def test_mtp_chosen_downloads_nothing (self ): 157with serving (),hardware (STRIX ),mock .patch .object (drafting_fetch ,"ensure_for" )as ensure : 158code ,_out ,_ = run_cli (["drafting" ,"fetch" ,"--config" ,str (self .cfg ),"--data-dir" ,str (self .data ),"--mode" ,"mtp" ,"--if-needed" ,"--throttle" ,"--output" ,"jsonl" ]) 159self .assertEqual (code ,0 ) 160ensure .assert_not_called () 161 162 163class ABareFetch (AnAutomaticFetch ): 164"""`strata drafting fetch` with nothing named: the one installed model's run config decides (coordinator 11-07).""" 165 166def installed (self ,* names ): 167rows = [{"path" :str (self .cfg ),"model_name" :n }for n in names ] 168return mock .patch ("strata_cli.launch.catalog.Catalog.installed_configs" ,return_value = rows ) 169 170def bare (self ): 171return run_cli (["drafting" ,"fetch" ,"--data-dir" ,str (self .data ),"--output" ,"jsonl" ]) 172 173def test_one_installed_model_the_tuned_pack_is_fetched (self ): 174with serving (),hardware (STRIX ),self .installed (Q4 ),mock .patch .object (drafting_fetch ,"ensure_for" ,return_value = self .data / "x" )as ensure : 175code ,_out ,_ = self .bare () 176self .assertEqual (code ,0 ) 177self .assertEqual (ensure .call_args .kwargs ["model_name" ],Q4 ) 178self .assertEqual (ensure .call_args .kwargs ["backend" ],"hip" ) 179 180def test_none_installed_keeps_the_generic_fetch (self ): 181with serving (),hardware (STRIX ),self .installed (),mock .patch .object (drafting_fetch ,"ensure_for" ,return_value = self .data / "x" )as ensure : 182self .bare () 183self .assertIsNone (ensure .call_args .kwargs ["model_name" ]) 184 185def test_several_models_ask_which_one_and_download_nothing (self ): 186"""Owner-approved wording (11-07).""" 187with serving (),hardware (STRIX ),self .installed (Q4 ,"qwen3.8-flash-next-iq3_s" ),mock .patch .object (drafting_fetch ,"ensure_for" )as ensure : 188code ,out ,_ = self .bare () 189code_text ,_o ,err = run_cli (["drafting" ,"fetch" ,"--data-dir" ,str (self .data )]) 190self .assertEqual (code ,2 ) 191ensure .assert_not_called () 192said = json .loads (out .strip ().splitlines ()[- 1 ]) 193self .assertFalse (said ["ok" ]) 194self .assertEqual (said ["error" ]["message" ],"Several models are set up here. Say which one: strata drafting fetch --model <name>, or --config <file>." ) 195self .assertEqual (code_text ,2 ) 196self .assertIn ("Several models are set up here. Say which one: strata drafting fetch --model <name>, or --config <file>." ,err ) 197 198def test_naming_the_model_still_fetches_with_several_set_up (self ): 199with serving (),hardware (STRIX ),self .installed (Q4 ,"qwen3.8-flash-next-iq3_s" ),mock .patch .object (drafting_fetch ,"ensure_for" ,return_value = self .data / "x" )as ensure : 200code ,_out ,_ = run_cli (["drafting" ,"fetch" ,"--config" ,str (self .cfg ),"--data-dir" ,str (self .data ),"--output" ,"jsonl" ]) 201self .assertEqual (code ,0 ) 202ensure .assert_called_once () 203 204def test_a_model_dflash2_is_not_for_is_not_picked (self ): 205with serving (),hardware (STRIX ),self .installed ("qwen3.8-flash-next-coder-iq1_m" ),mock .patch .object (drafting_fetch ,"ensure_for" ,return_value = self .data / "x" )as ensure : 206self .bare () 207self .assertIsNone (ensure .call_args .kwargs ["model_name" ]) 208 209 210if __name__ == "__main__" : 211unittest .main ()