okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4ba852d03
main
1"""Auto defaults for the 12 GB / streaming CUDA class (owner 11-06 ~15:30 "1 yes"; drafting.CLASS_DEFAULTS, one removable entry per set). 2 3The class is the one the confidence already reads: a CUDA card whose slots_of() is under the last-but-one CONF_STEPS step, so conf_for() gives 0.8 (P2003, RTX 3060 12 GB). 4IQ3_S -> Auto is the MTP. UD-IQ4_XS-v31 -> Auto is DFlash2 with the tuned selq4 pack at the class's current 0.8 (it depends on RTX2's file-tier fix in the same release). 5Anything chosen by name wins; the resident-CUDA, Strix, gfx1201, Arc and Metal entries and the conf tables are unchanged. 6""" 7 8from __future__import annotations 9 10import hashlib 11import tempfile 12import unittest 13from pathlib import Path 14from unittest import mock 15 16from strata_control import drafting ,drafting_fetch 17 18IQ3S = "qwen3.8-flash-next-iq3_s" 19Q4 = "qwen3.8-flash-next-unsloth-ud-q4_k_xl" 20V31 = "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31" 21OLD_XS = "qwen3.8-flash-next-unsloth-ud-iq4_xs" 22GYRO = "qwen3.8-flash-next-gyro-gyro-s" 23IQ3XXS = "qwen3.8-flash-next-iq3_xxs" 24Q2 = "qwen3.8-flash-next-q2_0" 25TUNED = b"tuned-pack-" * 200_000 26P2003 = ("cuda" , ["sm_86" ],12288 )# RTX 3060 12 GB 27CARD16 = ("cuda" , ["sm_89" ],16384 )# conf 0.7 step: not the class 28CARD24 = ("cuda" , ["sm_89" ],24576 ) 29L620 = ("cuda" , ["sm_120" ],73728 ) 30SPARK = ("cuda" , ["sm_121" ],None ) 31STRIX = ("hip" , ["gfx1151" ],None ) 32R9700 = ("hip" , ["gfx1201" ],32768 ) 33ARC = ("sycl" , ["arc_a" ],None ) 34 35 36def flag (args ,name ): 37return args [args .index (name )+ 1 ]if name in args and args .index (name )+ 1 < len (args )else None 38 39 40class Base (unittest .TestCase ): 41def setUp (self ): 42self .tmp = tempfile .TemporaryDirectory () 43self .addCleanup (self .tmp .cleanup ) 44self .root = Path (self .tmp .name ) 45self .data = self .root / "data" 46for sub in ("dflash2" ,"mtp/rt" ,"packs/tag" ): 47 (self .data / sub ).mkdir (parents = True ) 48 (self .data / "dflash2" / drafting .PACK_NAME ).write_bytes (b"s" * (2 << 20 )) 49 (self .data / "dflash2" / drafting .VOCAB_NAME ).write_bytes (b"v" * 100 ) 50 (self .data / "mtp" / "rt" / "experts.bin" ).write_bytes (b"e" * 100 ) 51 (self .data / "mtp" / "rt" / drafting .VOCAB_NAME ).write_bytes (b"v" * 100 ) 52self .exe = self .root / "strata" 53self .exe .write_bytes (b"--mtp-draft-vocab FILE (or --mtp DIR ... --draft-fallback ... selector codebooks: Q4_0 rows" ) 54self .args = ["--native" ,"m.gguf" ,"--pack" ,str (self .data / "packs" / "tag" ),"--mtp" ,str (self .data / "mtp" / "rt" ),"--spec" ,"4" ,"--max-context" ,"8192" ] 55for patcher in (mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,True ), 56mock .patch .multiple (drafting ,TUNED_BYTES = len (TUNED ),TUNED_SHA256 = hashlib .sha256 (TUNED ).hexdigest ())): 57patcher .start () 58self .addCleanup (patcher .stop ) 59 60def put_tuned (self ): 61 (self .data / "dflash2" / drafting .TUNED_NAME ).write_bytes (TUNED ) 62 63def apply (self ,name ,hardware ,** extra ): 64backend ,archs ,vram = hardware 65cfg = {"model_name" :name ,"args" :list (self .args ),** extra } 66decision = drafting .apply (cfg ,data_dir = self .data ,backend = backend ,archs = archs ,engine = self .exe ,vram_mib = vram ) 67return decision ,cfg ["args" ] 68 69 70class TheClass (Base ): 71def test_it_is_the_card_class_the_confidence_calls_0_8 (self ): 72for hardware in (P2003 , ("cuda" , ["sm_86" ],8192 ), ("cuda" , ["sm_75" ],6144 )): 73slots = drafting .slots_of (self .args ,hardware [2 ]) 74self .assertEqual (drafting .conf_for ("cuda" ,slots ),0.8 ,hardware ) 75self .assertTrue (drafting .streaming_class ("cuda" ,hardware [1 ],args = self .args ,vram_mib = hardware [2 ]),hardware ) 76for hardware in (CARD16 ,CARD24 ,L620 ,SPARK , ("cuda" , ["sm_120" ],None ), ("cuda" , ["sm_86" ],None )): 77self .assertFalse (drafting .streaming_class (hardware [0 ],hardware [1 ],args = self .args ,vram_mib = hardware [2 ]),hardware ) 78for backend ,archs in (("hip" , ["gfx1100" ]), ("hip" , ["gfx1201" ]), ("sycl" , ["arc_a" ]), ("metal" , [])): 79self .assertFalse (drafting .streaming_class (backend ,archs ,args = self .args ,vram_mib = 12288 ),backend ) 80# the boundary is the 0.8 step itself, and a person's own --expert-cache counts as the confidence counts it 81for given ,inside in (("2300" ,True ), ("2400" ,False ), ("64" ,True )): 82args = self .args + ["--expert-cache" ,given ] 83self .assertEqual (drafting .streaming_class ("cuda" , ["sm_120" ],args = args ,vram_mib = 73728 ),inside ,given ) 84self .assertEqual (drafting .conf_for ("cuda" ,drafting .slots_of (args ,73728 ))== 0.8 ,inside ,given ) 85 86 87class IQ3SOnTheClass (Base ): 88def test_auto_is_the_mtp (self ): 89self .put_tuned () 90for extra in ({}, {"drafting" :"auto" }): 91before = list (self .args ) 92d ,args = self .apply (IQ3S ,P2003 ,** extra ) 93self .assertEqual ((d .drafter ,d .note ), ("mtp" ,"" ),extra ) 94self .assertFalse ([a for a in args if a .startswith ("--draft" )],extra ) 95self .assertEqual (args ,before ,"the MTP arguments exactly as saved" ) 96 97def test_dflash2_by_name_wins_with_the_class_conf (self ): 98d ,args = self .apply (IQ3S ,P2003 ,drafting = "dflash2" ) 99self .assertEqual ((d .drafter ,flag (args ,"--draft" )), ("dflash2" ,"dflash2" )) 100self .assertEqual (flag (args ,"--draft-conf" ),"0.8" ) 101self .assertEqual (flag (args ,"--draft-model" ),str (self .data / "dflash2" / drafting .PACK_NAME ),"the stock pack: IQ3_S has no tuned entry on this class" ) 102d ,args = self .apply (IQ3S ,P2003 ,drafting = "dflash2" ,user_args = ["--draft-conf" ,"0.5" ]) 103self .assertEqual (flag (args ,"--draft-conf" ),"0.5" ) 104for mode ,drafter in (("mtp" ,"mtp" ), ("off" ,"off" )): 105self .assertEqual (self .apply (IQ3S ,P2003 ,drafting = mode )[0 ].drafter ,drafter ) 106 107def test_without_the_mtp_layer_auto_starts_the_dflash2_drafter_on_disk (self ): 108# one drafter is picked; when its file is missing the other one stands in where it is on disk (never no drafter unless Off was chosen; 0.2.14) 109 (self .data / "mtp" / "rt" / "experts.bin" ).unlink () 110d ,args = self .apply (IQ3S ,P2003 ) 111self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,drafting .NO_MTP )) 112self .assertEqual (self .apply (IQ3S ,P2003 ,drafting = "dflash2" )[0 ].drafter ,"dflash2" ) 113 114def test_the_fetch (self ): 115kw = dict (model_name = IQ3S ,backend = "cuda" ,archs = ["sm_86" ],data_dir = self .root / "empty" ,args = self .args ,vram_mib = 12288 ) 116self .assertFalse (drafting .needs_pack ("auto" ,** kw )) 117self .assertFalse (drafting .needs_tuned ("auto" ,** kw )) 118self .assertTrue (drafting .needs_pack ("dflash2" ,** kw )) 119self .assertTrue (drafting .needs_mtp ("auto" ,mtp_here = False ,** {k :v for k ,v in kw .items ()if k != "data_dir" })) 120self .assertFalse (drafting .wants_one_request ("auto" ,model_name = IQ3S ,backend = "cuda" ,archs = ["sm_86" ],args = self .args ,vram_mib = 12288 )) 121self .assertEqual (drafting .choice_of ("auto" ,model_name = IQ3S ,backend = "cuda" ,archs = ["sm_86" ],args = self .args ,vram_mib = 12288 ),"mtp" ) 122 123def test_every_other_card_keeps_dflash2_for_iq3_s (self ): 124for hardware in (CARD16 ,CARD24 ,L620 ,SPARK ,STRIX ,ARC ): 125d ,args = self .apply (IQ3S ,hardware ) 126self .assertEqual ((d .drafter ,flag (args ,"--draft" )), ("dflash2" ,"dflash2" ),hardware ) 127 128 129class V31OnTheClass (Base ): 130def test_auto_is_dflash2_with_the_tuned_pack_at_the_class_conf (self ): 131self .put_tuned () 132for extra in ({}, {"drafting" :"auto" }): 133d ,args = self .apply (V31 ,P2003 ,** extra ) 134self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" ),extra ) 135self .assertEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data ))) 136self .assertEqual (flag (args ,"--draft-conf" ),"0.8" ,"the class's current conf, unchanged" ) 137self .assertEqual (flag (args ,"--spec" ),"6" ) 138self .assertNotIn ("--mtp" ,args ) 139# only the pack changes against the same start without the tuned file 140 (self .data / "dflash2" / drafting .TUNED_NAME ).unlink () 141 (self .data / "dflash2" / (drafting .TUNED_NAME + ".ok" )).unlink () 142d2 ,args2 = self .apply (V31 ,P2003 ) 143self .assertEqual (flag (args2 ,"--draft-conf" ),"0.8" ) 144self .assertEqual (flag (args2 ,"--draft-model" ),str (drafting .pack_path (self .data ,self .exe ))) 145 146def test_by_name_and_the_users_own_conf_win (self ): 147self .put_tuned () 148d ,args = self .apply (V31 ,P2003 ,drafting = "dflash2" ,user_args = ["--draft-conf" ,"0.6" ]) 149self .assertEqual ((d .drafter ,flag (args ,"--draft-conf" )), ("dflash2" ,"0.6" )) 150self .assertEqual (self .apply (V31 ,P2003 ,drafting = "mtp" )[0 ].drafter ,"mtp" ) 151self .assertEqual (self .apply (V31 ,P2003 ,drafting = "off" )[0 ].drafter ,"off" ) 152 153def test_nothing_fetched_is_the_mtp_as_before (self ): 154d ,args = self .apply (V31 ,P2003 ) 155self .assertEqual (d .drafter ,"dflash2" ,"the stock pack is on this disk" ) 156 (self .data / "dflash2" / drafting .PACK_NAME ).unlink () 157before = list (self .args ) 158d ,args = self .apply (V31 ,P2003 ) 159self .assertEqual ((d .drafter ,d .note ,args ), ("mtp" ,drafting .NOT_DOWNLOADED ,before )) 160 161def test_it_is_fetched_for_this_class_only (self ): 162kw = dict (model_name = V31 ,backend = "cuda" ,data_dir = self .data ,args = self .args ) 163self .assertTrue (drafting .needs_tuned ("auto" ,archs = ["sm_86" ],vram_mib = 12288 ,** kw )) 164self .assertTrue (drafting .tuned_applies ("cuda" ,V31 , ["sm_86" ],args = self .args ,vram_mib = 12288 )) 165self .assertTrue (drafting_fetch .plan (self .data ,model_name = V31 ,backend = "cuda" ,archs = ["sm_86" ],args = self .args ,vram_mib = 12288 )["gb" ]== drafting .TUNED_GB ) 166# the resident rule of the same set is untouched 167self .assertTrue (drafting .tuned_applies ("cuda" ,V31 , ["sm_120" ],args = self .args ,vram_mib = 73728 )) 168self .assertTrue (drafting .tuned_applies ("cuda" ,V31 , ["sm_121" ],args = [],vram_mib = None )) 169self .assertFalse (drafting .tuned_applies ("cuda" ,V31 , ["sm_89" ],args = self .args ,vram_mib = 16384 ),"the 0.7 step is neither class" ) 170self .assertFalse (drafting .tuned_applies ("cuda" ,V31 , ["sm_120" ,"sm_120" ],args = self .args ,vram_mib = None ),"several cards" ) 171 172def test_the_support_line_for_sm_86_reads_the_set (self ): 173h = drafting .hardware_support 174self .assertEqual (h ("cuda" , ["sm_86" ],IQ3S ).line ,drafting .LINE_SLOWER ,"IQ3_S on sm_86 keeps the line it had" ) 175self .assertEqual (h ("cuda" , ["sm_86" ]).line ,drafting .LINE_SLOWER ) 176self .assertEqual (h ("cuda" , ["sm_86" ],V31 ).line ,drafting .LINE_TESTED ,"v31 is DFlash2 by default there: faster on some text, slower on other" ) 177self .assertEqual (h ("cuda" , ["sm_120" ],V31 ).line ,drafting .LINE_TESTED ) 178 179 180class OtherSetsAndEntriesAreUnchanged (Base ): 181def test_other_sets_on_the_class_follow_the_general_rule (self ): 182self .put_tuned () 183for name in (Q4 ,OLD_XS ,GYRO ,IQ3XXS ,Q2 ): 184d ,args = self .apply (name ,P2003 ) 185self .assertEqual ((d .drafter ,flag (args ,"--draft" )), ("dflash2" ,"dflash2" ),name ) 186self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),name ) 187self .assertEqual (flag (args ,"--draft-conf" ),"0.8" ,name ) 188 189def test_the_other_entries_and_the_conf_tables (self ): 190self .assertEqual (drafting .CONF_STEPS , ((12000 ,0.35 ), (7500 ,0.5 ), (3000 ,0.7 ), (0 ,0.8 ))) 191self .assertEqual (drafting .BACKEND_CONF , {"sycl" :0.6 ,"metal" :0.5 }) 192self .assertEqual ((drafting .DRAFT_CONF ,drafting .SPEC_WINDOW ), (0.35 ,6 )) 193self .assertEqual (set (drafting .CLASS_DEFAULTS ), {("cuda" ,"iq3_s" ,"streaming" ), ("cuda" ,"unsloth-ud-iq4_xs-v31" ,"streaming" )}) 194self .assertEqual (set (drafting .SET_DEFAULTS ), { 195 ("metal" ,"unsloth-ud-q4_k_xl" ), ("metal" ,"unsloth-ud-iq4_xs-v31" ), ("metal" ,"iq3_s" ), 196 ("cuda" ,"iq3_s" ), ("cuda" ,"unsloth-ud-iq4_xs-v31" ), 197 ("hip" ,"iq3_s" ), ("hip" ,"unsloth-ud-q4_k_xl" ), ("hip" ,"unsloth-ud-iq4_xs" ), ("hip" ,"unsloth-ud-iq4_xs-v31" ), 198 ("sycl" ,"iq3_xxs" )}) 199self .assertEqual (list (drafting .AUTO_MTP ), [("hip" ,"gfx1201" )]) 200self .assertEqual (drafting .choice_of ("auto" ,model_name = IQ3S ,backend = "metal" ),"mtp" ) 201self .assertEqual (drafting .choice_of ("auto" ,model_name = IQ3S ,backend = "hip" ,archs = ["gfx1201" ]),"mtp" ) 202self .assertEqual (drafting .choice_of ("auto" ,model_name = IQ3S ,backend = "hip" ,archs = ["gfx1151" ]),"dflash2" ) 203self .assertEqual (drafting .choice_of ("auto" ,model_name = IQ3S ,backend = "sycl" ,archs = ["arc_a" ]),"dflash2" ) 204self .assertEqual (drafting .choice_of ("auto" ,model_name = IQ3S ,backend = "cuda" ,archs = ["sm_86" ]),"dflash2" ,"no card signal given: the general rule, as before" ) 205 206def test_the_resident_strix_arc_and_metal_starts_are_what_they_were (self ): 207self .put_tuned () 208for hardware ,name ,conf in ((L620 ,IQ3S ,"0.55" ), (SPARK ,V31 ,"0.35" ), (STRIX ,IQ3S ,"0.35" )):# l620: 0.55 since 11-08 209d ,args = self .apply (name ,hardware ) 210self .assertEqual ((d .drafter ,flag (args ,"--draft-model" ),flag (args ,"--draft-conf" )), ("dflash2" ,str (drafting .tuned_path (self .data )),conf ), (hardware ,name )) 211d ,args = self .apply (Q4 , ("metal" , [],None )) 212self .assertEqual ((flag (args ,"--draft-model" ),flag (args ,"--draft-conf" )), (str (drafting .tuned_path (self .data )),"0.7" )) 213self .assertEqual (self .apply (IQ3S ,R9700 )[0 ].drafter ,"mtp" ) 214self .assertEqual (self .apply (IQ3S ,R9700 ,drafting = "dflash2" )[0 ].drafter ,"dflash2" ) 215 216def test_one_removable_entry_per_set (self ): 217self .put_tuned () 218with mock .patch .dict (drafting .CLASS_DEFAULTS , {k :v for k ,v in drafting .CLASS_DEFAULTS .items ()if k [1 ]!= "iq3_s" },clear = True ): 219self .assertEqual (self .apply (IQ3S ,P2003 )[0 ].drafter ,"dflash2" ) 220self .assertEqual (flag (self .apply (V31 ,P2003 )[1 ],"--draft-model" ),str (drafting .tuned_path (self .data ))) 221with mock .patch .dict (drafting .CLASS_DEFAULTS , {k :v for k ,v in drafting .CLASS_DEFAULTS .items ()if k [1 ]== "iq3_s" },clear = True ): 222self .assertEqual (self .apply (IQ3S ,P2003 )[0 ].drafter ,"mtp" ) 223d ,args = self .apply (V31 ,P2003 ) 224self .assertEqual (flag (args ,"--draft-conf" ),"0.8" ) 225self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),"without its entry the 12 GB card gets the stock pack" ) 226 227 228if __name__ == "__main__" : 229unittest .main ()