okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1637559fd
main
1"""The tuned DFlash2 drafter and the Mac default (round 0216, DF-SHIP). 2 3On Metal the two 4-bit sets (UD-Q4_K_XL, UD-IQ4_XS-v31) start DFlash2 under Auto: ``--spec 6``, no MTP, ``--draft-fallback none``, with the tuned pack at ``--draft-conf 0.7`` 4when it is here and verified, else the stock pack at 0.5, else (offline, nothing fetched) the MTP exactly as before. IQ3_S stays on the MTP until the one table entry 5(drafting.SET_DEFAULTS) says otherwise. The pinned pack is replaced by a small synthetic file whose size and SHA-256 are patched in, so nothing here needs 340 MB. 6""" 7 8from __future__import annotations 9 10import hashlib 11import json 12import os 13import tempfile 14import unittest 15from pathlib import Path 16from unittest import mock 17 18import sys 19 20from strata_control import acquisition ,drafting ,drafting_fetch ,transfer 21 22sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [2 ]/ "packaging" )) 23import feed_tool as ft # noqa: E402 24 25ROOT = Path (__file__ ).resolve ().parents [2 ] 26Q4 = "qwen3.8-flash-next-unsloth-ud-q4_k_xl" 27V31 = "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31" 28IQ3S = "qwen3.8-flash-next-iq3_s" 29Q5 = "qwen3.8-flash-next-unsloth-ud-q5_k_xl" 30TUNED_BYTES = b"tuned-pack-" * 200_000 # 2.2 MB, stands in for the 340 MB file 31STOCK_BYTES = b"s" * (2 << 20 ) 32 33 34def flag (args ,name ): 35return args [args .index (name )+ 1 ]if name in args and args .index (name )+ 1 < len (args )else None 36 37 38class Base (unittest .TestCase ): 39def setUp (self ): 40self .tmp = tempfile .TemporaryDirectory () 41self .addCleanup (self .tmp .cleanup ) 42self .root = Path (self .tmp .name ) 43self .data = self .root / "data" 44for sub in ("dflash2" ,"mtp/rt" ,"packs/tag" ): 45 (self .data / sub ).mkdir (parents = True ) 46 (self .data / "dflash2" / drafting .VOCAB_NAME ).write_bytes (b"v" * 100 ) 47 (self .data / "mtp" / "rt" / "experts.bin" ).write_bytes (b"e" * 100 ) 48 (self .data / "mtp" / "rt" / drafting .VOCAB_NAME ).write_bytes (b"v" * 100 ) 49self .exe = self .root / "strata" 50self .exe .write_bytes (b"--mtp-draft-vocab FILE (or --mtp DIR ... --draft-fallback ... selector codebooks: Q4_0 rows" )# a Metal engine of this tree 51self .args = ["--native" ,"m.gguf" ,"--pack" ,str (self .data / "packs" / "tag" ),"--mtp" ,str (self .data / "mtp" / "rt" ),"--spec" ,"2" ,"--max-context" ,"65536" ] 52self .pins = mock .patch .multiple (drafting ,TUNED_BYTES = len (TUNED_BYTES ),TUNED_SHA256 = hashlib .sha256 (TUNED_BYTES ).hexdigest ()) 53self .pins .start () 54self .addCleanup (self .pins .stop ) 55patcher = mock .patch .object (drafting ,"ENGINE_SERVES_DFLASH2" ,True ) 56patcher .start () 57self .addCleanup (patcher .stop ) 58 59def put_tuned (self ,content = TUNED_BYTES ): 60 (self .data / "dflash2" / drafting .TUNED_NAME ).write_bytes (content ) 61 62def put_stock (self ): 63 (self .data / "dflash2" / drafting .PACK_NAME ).write_bytes (STOCK_BYTES ) 64 65def cfg (self ,name = Q4 ,** extra ): 66return {"model_name" :name ,"args" :list (self .args ),** extra } 67 68def apply (self ,cfg ,backend = "metal" ,** kw ): 69decision = drafting .apply (cfg ,data_dir = self .data ,backend = backend ,archs = [],engine = self .exe ,** kw ) 70return decision ,cfg ["args" ] 71 72 73class MetalPerSet (Base ): 74def test_the_two_4bit_sets_start_dflash2_with_the_tuned_pack_at_0_7 (self ): 75self .put_tuned () 76self .put_stock () 77for name in (Q4 ,V31 ): 78for mode in (None ,"auto" ): 79d ,args = self .apply (self .cfg (name ,** ({}if mode is None else {"drafting" :mode }))) 80where = (name ,mode ) 81self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" ),where ) 82self .assertEqual (flag (args ,"--draft" ),"dflash2" ,where ) 83self .assertEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),where ) 84self .assertEqual (flag (args ,"--draft-conf" ),"0.7" ,where ) 85self .assertEqual (flag (args ,"--spec" ),"6" ,where ) 86self .assertEqual (flag (args ,"--draft-fallback" ),"none" ,where ) 87self .assertNotIn ("--mtp" ,args ,where ) 88self .assertNotIn ("--spec-min-p" ,args ,where ) 89 90def test_the_stock_pack_is_the_fallback_at_the_conf_it_was_measured_with (self ): 91self .put_stock ()# the tuned pack was never fetched 92for name in (Q4 ,V31 ): 93d ,args = self .apply (self .cfg (name )) 94self .assertEqual (d .drafter ,"dflash2" ,name ) 95self .assertEqual (flag (args ,"--draft-model" ),str (drafting .pack_path (self .data ,self .exe )),name ) 96self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),name ) 97self .assertEqual ((flag (args ,"--draft-conf" ),flag (args ,"--spec" )), ("0.5" ,"6" ),name ) 98 99def test_offline_and_nothing_fetched_is_the_mtp_exactly_as_today (self ): 100for name in (Q4 ,V31 ): 101cfg = self .cfg (name ) 102before = list (cfg ["args" ]) 103d ,args = self .apply (cfg ) 104self .assertEqual ((d .drafter ,d .note ), ("mtp" ,drafting .NOT_DOWNLOADED ),name ) 105self .assertEqual (args ,before ,"the MTP arguments are untouched" ) 106self .assertFalse ([a for a in args if a .startswith ("--draft" )]) 107 108def test_no_mtp_layer_either_is_off_with_a_note (self ): 109 (self .data / "mtp" / "rt" / "experts.bin" ).unlink () 110d ,args = self .apply (self .cfg (Q4 )) 111self .assertEqual ((d .drafter ,d .note ), ("off" ,drafting .NONE_HERE )) 112 113def test_a_wrong_tuned_file_is_not_used (self ): 114self .put_tuned (b"x" * len (TUNED_BYTES ))# the pinned size, other bytes 115self .assertFalse (drafting .tuned_ok (self .data )) 116d ,args = self .apply (self .cfg (Q4 )) 117self .assertEqual (d .drafter ,"mtp" ,"nothing else here: the MTP" ) 118self .put_stock () 119d ,args = self .apply (self .cfg (Q4 )) 120self .assertEqual ((d .drafter ,flag (args ,"--draft-conf" )), ("dflash2" ,"0.5" ),"the stock pack runs at its own conf" ) 121self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data ))) 122self .put_tuned (TUNED_BYTES [:- 1 ])# a short file 123self .assertFalse (drafting .tuned_ok (self .data )) 124 125def test_the_marker_is_not_trusted_for_a_changed_file (self ): 126self .put_tuned () 127self .assertTrue (drafting .tuned_ok (self .data )) 128self .assertTrue ((self .data / "dflash2" / (drafting .TUNED_NAME + ".ok" )).is_file ()) 129path = self .data / "dflash2" / drafting .TUNED_NAME 130before = path .stat () 131self .put_tuned (b"y" * len (TUNED_BYTES )) 132# the marker is keyed on size and mtime: a rewrite in the same tick of the file clock (about 16 ms on Windows, ~4 ms on Linux) keeps the 133# time, which a real change of the file (a download, a corruption) never does that soon after the check; let the clock move on, as 134# test_drafting_selector_q4 does for the converted pack 135os .utime (path ,ns = (before .st_atime_ns ,before .st_mtime_ns + 1_000_000_000 )) 136self .assertFalse (drafting .tuned_ok (self .data )) 137 138def test_the_users_own_draft_conf_and_spec_are_respected (self ): 139self .put_tuned () 140d ,args = self .apply (self .cfg (Q4 ,user_args = ["--draft-conf" ,"0.4" ])) 141self .assertEqual ((flag (args ,"--draft-conf" ),flag (args ,"--spec" )), ("0.4" ,"6" )) 142mine = ["--draft-conf" ,"0.9" ,"--spec" ,"4" ] 143cfg = self .cfg (V31 ,user_args = mine ) 144cfg ["args" ]= [x for x in cfg ["args" ]if x not in ("--spec" ,"2" )]+ mine # (serve/server.py merges user_args into the arguments before drafting.apply) 145d ,args = self .apply (cfg ) 146self .assertEqual ((flag (args ,"--draft-conf" ),flag (args ,"--spec" )), ("0.9" ,"4" )) 147self .assertEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data ))) 148 149def test_iq3_s_stays_on_the_mtp_until_its_table_entry_changes (self ): 150self .put_tuned () 151self .put_stock () 152for mode in (None ,"auto" ): 153d ,args = self .apply (self .cfg (IQ3S ,** ({}if mode is None else {"drafting" :mode }))) 154self .assertEqual (d .drafter ,"mtp" ,mode ) 155self .assertFalse ([a for a in args if a .startswith ("--draft" )]) 156d ,args = self .apply (self .cfg (IQ3S ,drafting = "dflash2" )) 157self .assertEqual ((d .drafter ,flag (args ,"--draft-conf" )), ("dflash2" ,"0.5" ),"named by the person: the stock pack at the Mac's 0.5" ) 158with mock .patch .dict (drafting .SET_DEFAULTS , {("metal" ,"iq3_s" ):drafting .SetDefault ("dflash2" )}):# the one entry, switched 159d ,args = self .apply (self .cfg (IQ3S )) 160self .assertEqual ((d .drafter ,flag (args ,"--draft-conf" )), ("dflash2" ,"0.5" )) 161self .assertEqual (flag (args ,"--draft-model" ),str (drafting .pack_path (self .data ,self .exe )),"no tuned entry: the stock pack" ) 162with mock .patch .dict (drafting .SET_DEFAULTS , {("metal" ,"unsloth-ud-q4_k_xl" ):drafting .SetDefault ("mtp" )}):# and the other way 163d ,args = self .apply (self .cfg (Q4 )) 164self .assertEqual (d .drafter ,"mtp" ) 165 166def test_a_set_with_no_entry_keeps_the_general_rule (self ): 167self .put_stock () 168d ,args = self .apply (self .cfg (Q5 )) 169self .assertEqual ((d .drafter ,flag (args ,"--draft-conf" )), ("dflash2" ,"0.5" )) 170self .assertIsNone (drafting .default_for ("metal" ,Q5 )) 171 172def test_the_tuned_pack_needs_an_engine_that_reads_the_q4_selector (self ): 173self .put_tuned () 174old = self .root / "strata-old" 175old .write_bytes (b"--mtp-draft-vocab FILE (or --mtp DIR ... --draft-fallback" )# no selector text 176cfg = self .cfg (Q4 ) 177d = drafting .apply (cfg ,data_dir = self .data ,backend = "metal" ,archs = [],engine = old ) 178self .assertEqual (d .drafter ,"mtp" ,"only the tuned pack is here and this engine cannot read it" ) 179self .assertEqual (d .note ,drafting .NOT_DOWNLOADED ) 180self .put_stock () 181cfg = self .cfg (Q4 ) 182d = drafting .apply (cfg ,data_dir = self .data ,backend = "metal" ,archs = [],engine = old ) 183self .assertEqual ((d .drafter ,flag (cfg ["args" ],"--draft-model" )), ("dflash2" ,str (drafting .pack_path (self .data ,old )))) 184self .assertNotEqual (flag (cfg ["args" ],"--draft-model" ),str (drafting .tuned_path (self .data ))) 185 186def test_the_other_cards_keep_the_stock_pack_and_their_conf (self ): 187self .put_tuned () 188self .put_stock () 189for backend ,archs ,vram in (("sycl" , ["arc_a" ],None ), ("hip" , ["gfx1201" ],32768 ), ("cuda" , ["sm_86" ],12288 ), ("hip" , ["gfx1100" ],24576 )): 190cfg = self .cfg (Q4 ) 191drafting .apply (cfg ,data_dir = self .data ,backend = backend ,archs = archs ,engine = self .exe ,vram_mib = vram ) 192self .assertNotEqual (flag (cfg ["args" ],"--draft-model" ),str (drafting .tuned_path (self .data )), (backend ,archs )) 193self .assertNotEqual (flag (cfg ["args" ],"--draft-conf" ),"0.7" , (backend ,archs )) 194self .assertIsNone (drafting .tuned_conf_for ("cuda" ,Q4 )) 195self .assertEqual (drafting .tuned_conf_for ("metal" ,Q4 ),0.7 ) 196self .assertIsNone (drafting .tuned_conf_for ("cuda" ,IQ3S ),"CUDA keeps its own conf: only the pack changes" ) 197 198def test_needs_tuned_and_needs_pack (self ): 199kw = dict (model_name = Q4 ,backend = "metal" ,archs = [],data_dir = self .data ) 200self .assertTrue (drafting .needs_pack ("auto" ,** kw )) 201self .assertTrue (drafting .needs_tuned ("auto" ,** kw )) 202self .put_stock () 203self .assertFalse (drafting .needs_pack ("auto" ,** kw )) 204self .assertTrue (drafting .needs_tuned ("auto" ,** kw ),"the stock pack is here, the tuned one is still worth fetching" ) 205self .put_tuned () 206self .assertFalse (drafting .needs_tuned ("auto" ,** kw )) 207self .assertFalse (drafting .needs_tuned ("auto" ,** {** kw ,"model_name" :IQ3S })) 208self .assertFalse (drafting .needs_tuned ("mtp" ,** kw )) 209self .assertFalse (drafting .needs_tuned ("auto" ,** {** kw ,"backend" :"cuda" })) 210 211def test_the_metal_line_per_set (self ): 212h = drafting .hardware_support 213for name in (Q4 ,V31 ): 214s = h ("metal" , [],name ) 215self .assertEqual ((s .can_run ,s .level ,s .line ), (True ,"tested" ,drafting .LINE_METAL ),name ) 216for name in (IQ3S ,Q5 ,None ): 217s = h ("metal" , [],name ) 218self .assertEqual ((s .can_run ,s .level ,s .line ), (True ,"untested" ,drafting .LINE_UNTESTED ),name ) 219self .assertEqual (h ("metal" , []).line ,drafting .LINE_UNTESTED ,"the old two-argument call" ) 220self .assertNotIn ("untested" ,drafting .LINE_METAL ) 221 222 223class Fetch (Base ): 224def setUp (self ): 225super ().setUp () 226self .host = self .root / "host" 227self .host .mkdir () 228 (self .host / drafting .TUNED_NAME ).write_bytes (TUNED_BYTES ) 229self .url = (self .host / drafting .TUNED_NAME ).as_uri () 230env = {k :v for k ,v in __import__ ("os" ).environ .items ()if k not in (drafting .TUNED_URL_ENV ,"STRATA_UPDATE_FEED" ,"STRATA_UPDATE_TRUSTED_KEYS" )} 231env ["STRATA_UPDATE_FEED" ]= str (self .root / "no-feed-here" )# nothing in these tests reaches the network 232env ["STRATA_COMPONENTS_DIR" ]= str (self .root / "components" )# the feed sequence this computer accepted is the test's, not the PC's own 233patcher = mock .patch .dict ("os.environ" ,env ,clear = True ) 234patcher .start () 235self .addCleanup (patcher .stop ) 236self .keys = self .root / "keys" 237self .pub = ft .keygen (self .keys ,allow_git = True ) 238 (self .root / "trust.json" ).write_text ((self .keys / "feed-key.pub.json" ).read_text (encoding = "utf-8" ),encoding = "utf-8" ) 239__import__ ("os" ).environ ["STRATA_UPDATE_TRUSTED_KEYS" ]= str (self .root / "trust.json" ) 240 241def make_feed (self ,sub = "feed" ,listed = True ,backends = ("metal" ,),pin_sha = None ,sequence = 1 ): 242"""A local, test-signed release folder: the feed and the drafter beside it as a flat asset (what the GitHub release would hold).""" 243out = self .root / sub 244entries = self .root / (sub + "-entries" ) 245if listed : 246entry = ft .pack_drafter (self .host / drafting .TUNED_NAME ,entries ,version = "1" ,backends = list (backends )) 247if pin_sha : 248entry ["sha256" ]= pin_sha 249 (entries / (drafting .TUNED_NAME + ".entry.json" )).write_text (json .dumps (entry ),encoding = "utf-8" ) 250out .mkdir (parents = True ,exist_ok = True ) 251if listed : 252 (out / drafting .TUNED_NAME ).write_bytes (TUNED_BYTES ) 253ft .build_feed (out ,key = ft ._load_key (self .keys / "feed-key.json" ),sequence = sequence ,drafters_dir = entries if listed else None ) 254return out 255 256def accept (self ,sequence ): 257"""What `strata update` leaves after it accepted a feed: <components>/.updates/state.json feed.sequence.""" 258from strata_control import updater 259state = updater .State (self .root / "components" ) 260state .section ("feed" ).update ({"sequence" :sequence ,"key_id" :"k" ,"source" :"x" ,"channel" :"stable" }) 261state .save () 262return state .path 263 264def test_a_feed_older_than_the_one_the_update_accepted_is_refused_like_the_update_refuses_it (self ): 265# l620 C2, 11-15: the update accepted feed 126, then the drafter came from a copy of feed 125 without a word 266old = self .make_feed (sub = "old" ,sequence = 125 ) 267state_file = self .accept (126 ) 268with self .assertRaises (drafting_fetch .DraftingError )as caught : 269drafting_fetch .ensure_tuned (self .data ,feed = str (old )) 270text = str (caught .exception ) 271self .assertIn ("older than the one already accepted on this computer (its sequence is 125, the accepted one is 126" ,text ) 272self .assertIn (str (state_file ),text ) 273self .assertFalse (drafting .tuned_path (self .data ).exists ()) 274with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" )): 275self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ,feed = str (old )),Path ("/stock" ),"the stock drafter takes over" ) 276 277def test_the_accepted_feed_and_newer_ones_give_the_drafter_and_the_lookup_never_moves_the_sequence (self ): 278state_file = self .accept (126 ) 279same = self .make_feed (sub = "same" ,sequence = 126 ) 280self .assertEqual (drafting_fetch .tuned_from_feed (feed = str (same ))[1 :], (len (TUNED_BYTES ),hashlib .sha256 (TUNED_BYTES ).hexdigest ())) 281newer = self .make_feed (sub = "newer" ,sequence = 127 ) 282self .assertEqual (drafting_fetch .ensure_tuned (self .data ,feed = str (newer )),drafting .tuned_path (self .data )) 283self .assertEqual (json .loads (state_file .read_text (encoding = "utf-8" ))["feed" ]["sequence" ],126 ,"only `strata update` accepts a feed" ) 284 285def test_nothing_accepted_yet_takes_any_sequence_and_offline_stays_silent_with_one_accepted (self ): 286self .assertIsNotNone (drafting_fetch .tuned_from_feed (feed = str (self .make_feed (sub = "first" ,sequence = 5 )))) 287self .accept (126 ) 288from tests .terminal import net_dead 289for kind in net_dead .KINDS : 290with net_dead .dead_feed (kind )as dead : 291self .assertIsNone (drafting_fetch .tuned_from_feed (feed = dead ),kind ) 292 293def test_a_fetch_from_a_file_address_checks_places_and_marks_the_file_and_copies_the_licence (self ): 294seen = [] 295out = drafting_fetch .ensure_tuned (self .data ,url = self .url ,progress = lambda * a :seen .append (a )) 296self .assertEqual (out ,drafting .tuned_path (self .data )) 297self .assertTrue (drafting .tuned_ok (self .data )) 298self .assertEqual (seen [- 1 ][1 :], (len (TUNED_BYTES ),len (TUNED_BYTES ))) 299self .assertFalse ((self .data / "dflash2" / (drafting .TUNED_NAME + ".part" )).exists ()) 300for name in ("LICENSE-dflash2-tuned" ,"NOTICE-dflash2-tuned.md" ): 301self .assertTrue ((self .data / "dflash2" / name ).is_file (),name ) 302licence = (self .data / "dflash2" / "LICENSE-dflash2-tuned" ).read_text (encoding = "utf-8" ) 303notice = (self .data / "dflash2" / "NOTICE-dflash2-tuned.md" ).read_text (encoding = "utf-8" ) 304self .assertIn ("Apache License" ,licence ) 305self .assertIn ("Version 2.0" ,licence ) 306for word in ("modified copy" ,"a22a1744e85c17364dcc54a6a420af7135d4c7f6" ,"Inco AI" ,"Strata changed" ): 307self .assertIn (word ,notice ) 308again = drafting_fetch .ensure_tuned (self .data ,url = "file:///no/such/file" )# already here: no download 309self .assertEqual (again ,out ) 310 311def test_the_normal_way_is_the_signed_feed (self ): 312feed = self .make_feed () 313seen = [] 314out = drafting_fetch .ensure_tuned (self .data ,feed = str (feed ),progress = lambda * a :seen .append (a )) 315self .assertEqual (out ,drafting .tuned_path (self .data )) 316self .assertTrue (drafting .tuned_ok (self .data )) 317self .assertEqual (out .read_bytes (),TUNED_BYTES ) 318self .assertTrue ((self .data / "dflash2" / "LICENSE-dflash2-tuned" ).is_file ()) 319# the environment names the feed too, and ensure_for takes the tuned pack from it without touching the stock download 320out .unlink () 321out .with_name (out .name + ".ok" ).unlink () 322with mock .patch .dict ("os.environ" , {"STRATA_UPDATE_FEED" :str (feed )}),mock .patch .object (drafting_fetch ,"ensure" ,side_effect = AssertionError ("stock fetched" )): 323self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ),out ) 324 325def test_the_feed_entry_is_a_drafters_member_and_older_clients_ignore_it (self ): 326feed = self .make_feed () 327doc = json .loads ((feed / "strata-feed.json" ).read_text (encoding = "utf-8" )) 328entry = doc ["drafters" ]["packages" ][0 ] 329self .assertEqual ((entry ["name" ],entry ["backends" ],entry ["size" ],entry ["sha256" ]), 330 (drafting .TUNED_NAME , ["metal" ],len (TUNED_BYTES ),hashlib .sha256 (TUNED_BYTES ).hexdigest ())) 331self .assertEqual (entry ["url" ],drafting .TUNED_NAME ,"a flat release asset" ) 332parsed = drafting_fetch .feedmod .parse ((feed / "strata-feed.json" ).read_bytes (),source = str (feed / "strata-feed.json" ),key_id = "k" ) 333self .assertEqual ([d .name for d in parsed .drafters ], [drafting .TUNED_NAME ]) 334doc .pop ("drafters" ) 335self .assertEqual (drafting_fetch .feedmod .parse (json .dumps (doc ).encode (),source = "x" ,key_id = "k" ).drafters , ()) 336 337def test_a_later_per_platform_switch_is_a_table_entry_and_a_feed_backend (self ): 338feed = self .make_feed (sub = "both" ,backends = ("metal" ,"cuda" )) 339card = dict (archs = ["sm_120" ],args = ["--max-context" ,"65536" ],vram_mib = 73728 ) 340with mock .patch .dict (drafting .SET_DEFAULTS , {("cuda" ,"unsloth-ud-q4_k_xl" ):drafting .SetDefault ("dflash2" ,True ,None ,"resident" )}): 341self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "cuda" ,feed = str (feed ),** card ),drafting .tuned_path (self .data )) 342self .put_stock () 343self .assertFalse (drafting .tuned_applies ("cuda" ,Q4 ,** card ),"without the entry CUDA is back to the stock pack" ) 344 345def test_a_feed_that_does_not_list_it_or_lists_other_bytes_gives_nothing (self ): 346empty = self .make_feed (listed = False ) 347self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = str (empty ))) 348other = self .make_feed (sub = "other" ,pin_sha = "0" * 64 ) 349self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = str (other )),"other bytes than the ones this Strata measured" ) 350elsewhere = self .make_feed (sub = "mac-only" ,backends = ["cuda" ]) 351self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = str (elsewhere ))) 352self .assertFalse (drafting .tuned_path (self .data ).exists ()) 353 354def test_a_feed_with_a_bad_signature_is_refused_in_a_line (self ): 355feed = self .make_feed () 356text = (feed / "strata-feed.json" ).read_text (encoding = "utf-8" ) 357 (feed / "strata-feed.json" ).write_text (text .replace ('"sequence": 1' ,'"sequence": 2' ),encoding = "utf-8" ) 358with self .assertRaises (drafting_fetch .DraftingError )as caught : 359drafting_fetch .ensure_tuned (self .data ,feed = str (feed )) 360self .assertIn ("could not be trusted" ,str (caught .exception )) 361self .assertFalse (drafting .tuned_path (self .data ).exists ()) 362with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" )): 363self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ,feed = str (feed )),Path ("/stock" ),"the stock drafter takes over" ) 364 365def test_a_feed_that_cannot_be_read_or_a_build_with_no_key_is_silent (self ): 366self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = str (self .root / "no-such-feed" ))) 367feed = self .make_feed () 368with mock .patch .dict ("os.environ" , {"STRATA_UPDATE_TRUSTED_KEYS" :"" }),mock .patch ("strata_control.signing.load_trusted_keys" ,return_value = {}): 369self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = str (feed ))) 370 371def test_a_feed_nobody_answers_for_is_silent_offline_refused_or_never_answering (self ): 372from tests .terminal import net_dead 373for kind in net_dead .KINDS :# no network at all / a refused port / a server that never answers 374with net_dead .dead_feed (kind )as dead : 375self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = dead ),kind ) 376self .assertIsNone (drafting_fetch .tuned_from_feed (feed = dead ),kind ) 377self .assertFalse (drafting .tuned_path (self .data ).exists (),kind ) 378 379def test_a_launch_that_got_nothing_does_not_ask_again_for_twelve_hours_but_setup_always_asks (self ): 380nofeed = str (self .root / "no-feed-here" ) 381self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = nofeed ,throttle = True )) 382self .assertTrue ((self .data / "dflash2" / drafting_fetch .MISS_MARK ).is_file ()) 383with mock .patch .object (drafting_fetch ,"tuned_from_feed" ,side_effect = AssertionError ("asked again" )): 384self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,feed = nofeed ,throttle = True ),"a launch a moment later: no wait" ) 385feed = self .make_feed () 386self .assertEqual (drafting_fetch .ensure_tuned (self .data ,feed = str (feed )),drafting .tuned_path (self .data ),"setup (no throttle) asks and gets it" ) 387self .assertFalse ((self .data / "dflash2" / drafting_fetch .MISS_MARK ).exists (),"and the mark is gone" ) 388old = self .data / "dflash2" / drafting_fetch .MISS_MARK 389old .write_text ("x" ) 390import os 391os .utime (old , (1 ,1 )) 392self .assertFalse (drafting_fetch ._recent_miss (self .data ),"a mark older than the interval is ignored" ) 393 394def test_with_no_feed_named_the_projects_release_feed_is_read (self ): 395from strata_cli import update_cmd 396asked = [] 397 398def fetch (source ,trusted ,** kw ): 399asked .append (source ) 400raise drafting_fetch .feedmod .FeedError ("the update feed could not be read: offline" ,6 ) 401env = {k :v for k ,v in __import__ ("os" ).environ .items ()if k != update_cmd .ENV_FEED } 402with mock .patch .dict ("os.environ" ,env ,clear = True ),mock .patch ("strata_control.feed.fetch" ,fetch ): 403self .assertIsNone (drafting_fetch .ensure_tuned (self .data )) 404self .assertEqual (asked , [update_cmd .default_feed ()]) 405self .assertTrue (update_cmd .default_feed ().startswith ("https://github.com/Niko1221/Strata/releases/" )) 406 407def test_a_download_that_fails_the_safety_check_is_deleted_and_said (self ): 408 (self .host / drafting .TUNED_NAME ).write_bytes (b"z" * len (TUNED_BYTES )) 409with self .assertRaises (drafting_fetch .DraftingError )as caught : 410drafting_fetch .ensure_tuned (self .data ,url = self .url ) 411self .assertIn ("safety check" ,str (caught .exception )) 412self .assertFalse (drafting .tuned_path (self .data ).exists ()) 413self .assertFalse ((self .data / "dflash2" / (drafting .TUNED_NAME + ".part" )).exists ()) 414 415def test_an_interrupted_download_resumes (self ): 416partial = self .data / "dflash2" / (drafting .TUNED_NAME + ".part" ) 417partial .write_bytes (TUNED_BYTES [:123_456 ]) 418out = drafting_fetch .ensure_tuned (self .data ,url = self .url ) 419self .assertTrue (drafting .tuned_ok (self .data )) 420self .assertEqual (out .read_bytes (),TUNED_BYTES ) 421partial .write_bytes (TUNED_BYTES [:99 ]) 422out .unlink () 423calls = [] 424real = transfer ._transfer 425 426def spy (location ,part ,have ,size ,progress ,cancel ,timeout ,** more ): 427calls .append (have ) 428return real (location ,part ,have ,size ,progress ,cancel ,timeout ,** more ) 429with mock .patch .object (transfer ,"_transfer" ,spy ): 430drafting_fetch .ensure_tuned (self .data ,url = self .url ) 431self .assertEqual (calls , [99 ],"it continued at byte 99, not from the start" ) 432 433def test_no_internet_is_silent (self ): 434said = [] 435with mock .patch .object (drafting_fetch ,"online" ,return_value = False ): 436self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,url = "https://example.invalid/x.pack" ,progress = lambda * a :said .append (a ))) 437self .assertIsNone (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ,progress = lambda * a :said .append (a ))) 438self .assertEqual (said , [],"nothing is reported and nothing is written" ) 439self .assertFalse ((self .data / "dflash2" / drafting .TUNED_NAME ).exists ()) 440self .assertFalse ((self .data / "dflash2" / (drafting .TUNED_NAME + ".part" )).exists ()) 441 442def test_a_host_that_answers_with_an_error_page_is_silent_too (self ): 443def not_found (* _a ,** _k ): 444raise transfer .TransferError ("the update server answered 404 for x" ) 445with mock .patch .object (drafting_fetch ,"online" ,return_value = True ),mock .patch .object (transfer ,"download" ,not_found ): 446self .assertIsNone (drafting_fetch .ensure_tuned (self .data ,url = "https://example.invalid/x.pack" )) 447 448def test_a_start_never_downloads (self ): 449with acquisition .forbidden (): 450with self .assertRaises (acquisition .AcquisitionRefused ): 451drafting_fetch .ensure_tuned (self .data ,url = self .url ) 452 453def test_the_set_gets_the_tuned_pack_and_not_the_stock_download (self ): 454with mock .patch .dict ("os.environ" , {drafting .TUNED_URL_ENV :self .url }),mock .patch .object (drafting_fetch ,"ensure" ,side_effect = AssertionError ("stock fetched" )): 455got = drafting_fetch .ensure_for (self .data ,model_name = V31 ,backend = "metal" ) 456self .assertEqual (got ,drafting .tuned_path (self .data )) 457 458def test_when_the_tuned_pack_cannot_be_had_the_stock_drafter_is_fetched (self ): 459with mock .patch .dict ("os.environ" , {drafting .TUNED_URL_ENV :"file:///no/such/host/file.pack" }), \ 460mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" ))as stock : 461got = drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ) 462self .assertEqual (got ,Path ("/stock" )) 463stock .assert_called_once () 464 465def test_a_failed_tuned_check_falls_back_and_only_a_double_failure_is_said (self ): 466 (self .host / drafting .TUNED_NAME ).write_bytes (b"z" * len (TUNED_BYTES )) 467with mock .patch .dict ("os.environ" , {drafting .TUNED_URL_ENV :self .url }),mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" )): 468self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ),Path ("/stock" )) 469 (self .host / drafting .TUNED_NAME ).write_bytes (b"z" * len (TUNED_BYTES )) 470with mock .patch .dict ("os.environ" , {drafting .TUNED_URL_ENV :self .url }), \ 471mock .patch .object (drafting_fetch ,"ensure" ,side_effect = drafting_fetch .OfflineError ("no internet" )): 472with self .assertRaises (drafting_fetch .DraftingError )as caught : 473drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ) 474self .assertIn ("safety check" ,str (caught .exception )) 475 476def test_a_stock_pack_already_here_is_kept_when_the_tuned_one_is_offline (self ): 477self .put_stock () 478with mock .patch .object (drafting_fetch ,"online" ,return_value = False ),mock .patch .object (drafting_fetch ,"ensure" ,side_effect = AssertionError ("fetched" )): 479got = drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ) 480self .assertEqual (got ,drafting .pack_path (self .data )) 481 482def test_a_set_without_a_tuned_entry_is_fetched_as_before (self ): 483with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" ))as stock : 484self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = IQ3S ,backend = "metal" ),Path ("/stock" )) 485self .assertEqual (drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "cuda" ,archs = ["sm_120" ],vram_mib = 73728 ),Path ("/stock" )) 486self .assertEqual (stock .call_count ,2 ) 487 488 489OLD_XS = "qwen3.8-flash-next-unsloth-ud-iq4_xs" 490GYRO = "qwen3.8-flash-next-gyro-gyro-s" 491#: (name, backend, archs, the one card's memory in MiB or None) of the boxes DF-VAL measured and of the ones it must not reach 492L620 = ("l620 RTX PRO 5000 72 GB" ,"cuda" , ["sm_120" ],73728 ) 493SPARK = ("DGX Spark GB10" ,"cuda" , ["sm_121" ],None ) 494STRIX = ("Strix Halo" ,"hip" , ["gfx1151" ],None ) 495P2003 = ("RTX 3060 12 GB" ,"cuda" , ["sm_86" ],12288 ) 496CARD24 = ("a 24 GB card" ,"cuda" , ["sm_89" ],24576 ) 497R9700 = ("R9700 32 GB" ,"hip" , ["gfx1201" ],32768 ) 498RX7900 = ("RX 7900 XTX" ,"hip" , ["gfx1100" ],24576 ) 499ARC = ("Arc A750" ,"sycl" , ["arc_a" ],None ) 500TWO_CARDS = ("two RTX PRO cards" ,"cuda" , ["sm_120" ,"sm_120" ],None ) 501 502 503class CudaAndHip (Fetch ): 504"""Owner 11-06 ~10:00: the tuned pack is the DFlash2 pack at each platform's CURRENT confidence, (a) on CUDA when every expert is resident (the CONF_STEPS first step, 0.35: l620, the Spark), 505(b) on Strix Halo (gfx1151); for the sets DF-VAL measured there. Not a 12 GB or streaming card, not gfx1201, not SYCL, not several cards.""" 506 507def apply_on (self ,hardware ,name ,** extra ): 508_label ,backend ,archs ,vram = hardware 509cfg = self .cfg (name ,** extra ) 510decision = drafting .apply (cfg ,data_dir = self .data ,backend = backend ,archs = archs ,engine = self .exe ,vram_mib = vram ) 511return decision ,cfg ["args" ] 512 513def drop_tuned (self ): 514 (self .data / "dflash2" / drafting .TUNED_NAME ).unlink (missing_ok = True ) 515 (self .data / "dflash2" / (drafting .TUNED_NAME + ".ok" )).unlink (missing_ok = True ) 516 517def test_the_measured_cells_get_the_tuned_pack_at_the_conf_the_backend_already_had (self ): 518self .put_tuned () 519self .put_stock () 520cells = ((L620 ,IQ3S ), (SPARK ,IQ3S ), (SPARK ,V31 ), (STRIX ,IQ3S ), (STRIX ,Q4 ), (STRIX ,OLD_XS ), (STRIX ,V31 )) 521for hardware ,name in cells : 522self .put_tuned () 523d ,args = self .apply_on (hardware ,name ) 524where = (hardware [0 ],name ) 525self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" ),where ) 526self .assertEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),where ) 527self .drop_tuned ()# the same start without the tuned file: the stock pack, everything else the same 528d2 ,args2 = self .apply_on (hardware ,name ) 529self .assertEqual (flag (args2 ,"--draft-model" ),str (drafting .pack_path (self .data ,self .exe )),where ) 530self .assertNotEqual (flag (args2 ,"--draft-model" ),flag (args ,"--draft-model" ),where ) 531self .assertEqual (flag (args2 ,"--draft-conf" ),"0.35" ,where ) 532self .assertEqual (flag (args ,"--draft-conf" ),"0.55" if (hardware is L620 and name == IQ3S )else "0.35" ,where )# IQ3_S on l620's class with the tuned pack: 0.55 (11-08); the stock pack keeps 0.35 533self .assertEqual (flag (args ,"--spec" ),flag (args2 ,"--spec" ),where ) 534strip = lambda a : [x for i ,x in enumerate (a )if not (i and a [i - 1 ]in ("--draft-model" ,"--draft-conf" ))]# noqa: E731 (the conf is compared above) 535self .assertEqual (strip (args ),strip (args2 ),where ) 536 537def test_the_cells_nobody_measured_are_not_enabled (self ): 538self .put_tuned () 539self .put_stock () 540for hardware ,name in ((L620 ,Q4 ), (L620 ,OLD_XS ), (SPARK ,Q4 ), (L620 ,Q5 ), (STRIX ,Q5 ), (L620 ,GYRO ), (STRIX ,GYRO )): 541d ,args = self .apply_on (hardware ,name ) 542self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )), (hardware [0 ],name )) 543self .assertFalse (drafting .tuned_applies (hardware [1 ],name ,hardware [2 ],args = [],vram_mib = hardware [3 ]), (hardware [0 ],name )) 544 545def test_a_12_gb_card_an_r9700_sycl_several_cards_and_other_amd_cards_never_get_it (self ): 546self .put_tuned () 547self .put_stock () 548for hardware in (P2003 ,CARD24 ,R9700 ,RX7900 ,ARC ,TWO_CARDS ): 549for name in (IQ3S ,Q4 ,V31 ,OLD_XS ): 550if hardware is P2003 and name == V31 : 551continue # the 12 GB class has its own entry for v31 (test_drafting_streaming_class.py) 552d ,args = self .apply_on (hardware ,name ) 553where = (hardware [0 ],name ) 554self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),where ) 555self .assertFalse (drafting .tuned_applies (hardware [1 ],name ,hardware [2 ],args = self .args ,vram_mib = hardware [3 ]),where ) 556self .assertFalse (drafting .needs_tuned ("auto" ,model_name = name ,backend = hardware [1 ],archs = hardware [2 ],data_dir = self .data ,args = self .args ,vram_mib = hardware [3 ]),where ) 557 558def test_a_sycl_start_with_the_tuned_file_present_is_exactly_what_it_is_today (self ): 559"""The SYCL 0.2.11 engine reads bf16 selector codebooks only and refuses the Q4_0-selector pack (DF-VAL): SYCL must never see the tuned file. Arc, if it is enabled later, needs a 560second, bf16-selector tuned pack with its own name, size and SHA-256 in the feed (not added now).""" 561self .put_stock () 562sycl_exe = self .root / "strata-sycl" 563sycl_exe .write_bytes (b"--mtp-draft-vocab FILE (or --mtp DIR ... --draft-fallback" )# this build has no Q4_0-selector text 564for name in (IQ3S ,Q4 ,V31 ,OLD_XS ): 565runs = [] 566for tuned_here in (False ,True ): 567if tuned_here : 568self .put_tuned () 569cfg = self .cfg (name ) 570d = drafting .apply (cfg ,data_dir = self .data ,backend = "sycl" ,archs = ["arc_a" ],engine = sycl_exe ) 571runs .append ((d ,cfg ["args" ])) 572self .assertEqual (runs [0 ][1 ],runs [1 ][1 ],f"the tuned file changes nothing on SYCL ( { name } )" ) 573self .assertEqual ((runs [0 ][0 ].drafter ,runs [0 ][0 ].note ), (runs [1 ][0 ].drafter ,runs [1 ][0 ].note )) 574self .assertEqual (flag (runs [1 ][1 ],"--draft-conf" ),"0.6" ,"BACKEND_CONF['sycl']" ) 575self .assertFalse (drafting .tuned_applies ("sycl" ,name , ["arc_a" ],args = self .args ,vram_mib = None )) 576self .assertFalse (drafting .tuned_applies ("sycl" ,name , ["arc_a" ],args = self .args ,vram_mib = 8192 )) 577self .drop_tuned () 578self .assertNotIn (("sycl" ,"iq3_s" ),drafting .SET_DEFAULTS ) 579 580def test_their_confidences_are_exactly_what_they_were (self ): 581"""The conf of every box class, with the tuned file here and without it, against the numbers CONF_STEPS and BACKEND_CONF have always given.""" 582self .assertEqual (drafting .CONF_STEPS , ((12000 ,0.35 ), (7500 ,0.5 ), (3000 ,0.7 ), (0 ,0.8 ))) 583self .assertEqual (drafting .BACKEND_CONF , {"sycl" :0.6 ,"metal" :0.5 }) 584self .assertEqual ((drafting .DRAFT_CONF ,drafting .SPEC_WINDOW ), (0.35 ,6 )) 585self .put_stock () 586want = {L620 [0 ]:"0.55" ,SPARK [0 ]:"0.35" ,STRIX [0 ]:"0.35" ,P2003 [0 ]:"0.8" ,ARC [0 ]:"0.6" } 587for hardware in (L620 ,SPARK ,STRIX ,P2003 ,ARC ): 588name = V31 if hardware is P2003 else IQ3S # (IQ3S on the 12 GB class is the MTP now: no DFlash2 conf to compare) 589before = flag (self .apply_on (hardware ,name )[1 ],"--draft-conf" ) 590self .put_tuned () 591after = flag (self .apply_on (hardware ,name )[1 ],"--draft-conf" ) 592self .drop_tuned () 593self .assertEqual (before ,"0.35" if hardware is L620 else after ,hardware [0 ])# (l620's IQ3_S: 0.55 only with the tuned pack) 594self .assertEqual (after ,want [hardware [0 ]],hardware [0 ]) 595for hardware in (L620 ,SPARK ,STRIX ): 596self .assertEqual (drafting .conf_for (hardware [1 ],drafting .slots_of (self .args ,hardware [3 ])),0.35 ,hardware [0 ]) 597cuda_hip = {k :v for k ,v in drafting .SET_DEFAULTS .items ()if k [0 ]in ("cuda" ,"hip" )} 598self .assertEqual ({k :v .tuned_conf for k ,v in cuda_hip .items ()}, {k :None for k in cuda_hip },"no CUDA / HIP entry names a conf" ) 599self .assertEqual (set (cuda_hip ), {("cuda" ,"iq3_s" ), ("cuda" ,"unsloth-ud-iq4_xs-v31" ), ("hip" ,"iq3_s" ), ("hip" ,"unsloth-ud-q4_k_xl" ), ("hip" ,"unsloth-ud-iq4_xs" ), ("hip" ,"unsloth-ud-iq4_xs-v31" )}) 600self .assertEqual ({k :v .where for k ,v in cuda_hip .items ()if k [0 ]== "cuda" }, {("cuda" ,"iq3_s" ):"resident" , ("cuda" ,"unsloth-ud-iq4_xs-v31" ):"resident" }) 601 602def test_the_resident_rule_reads_the_signals_the_confidence_reads (self ): 603for given ,qualifies in (("10000" ,True ), ("9400" ,True ), ("9000" ,False ), ("3000" ,False ), ("64" ,False )):# an --expert-cache N is 1.3 slots per N; the first step is 12,000 slots 604args = ["--max-context" ,"65536" ,"--expert-cache" ,given ] 605self .assertEqual (drafting .tuned_applies ("cuda" ,IQ3S , ["sm_120" ],args = args ,vram_mib = 12288 ),qualifies ,given ) 606self .assertEqual (drafting .conf_for ("cuda" ,drafting .slots_of (args ,12288 ))== drafting .DRAFT_CONF ,qualifies ,"the same boundary as the 0.35 step" ) 607for vram in (73728 ,49152 ,32768 ,24576 ,16384 ,12288 ,8192 ): 608slots = drafting .slots_of (list (self .args ),vram ) 609self .assertEqual (drafting .tuned_applies ("cuda" ,IQ3S , ["sm_120" ],args = list (self .args ),vram_mib = vram ),slots >= 12000 ,vram ) 610self .assertEqual (drafting .conf_for ("cuda" ,slots )== 0.35 ,drafting .tuned_applies ("cuda" ,IQ3S , ["sm_120" ],args = list (self .args ),vram_mib = vram ),vram ) 611self .assertFalse (drafting .tuned_applies ("cuda" ,IQ3S , ["sm_120" ],args = list (self .args ),vram_mib = None ),"a card whose memory is not known is not resident" ) 612self .assertTrue (drafting .tuned_applies ("cuda" ,IQ3S , ["sm_121" ],args = [],vram_mib = None ),"the Spark: one pool of memory" ) 613self .assertTrue (drafting .tuned_applies ("hip" ,IQ3S , ["gfx1151" ])) 614self .assertFalse (drafting .tuned_applies ("hip" ,IQ3S , ["gfx1150" ])) 615self .assertFalse (drafting .tuned_applies ("hip" ,IQ3S , []),"an unknown AMD card" ) 616self .assertFalse (drafting .tuned_applies ("cuda" ,IQ3S , []),"an unknown NVIDIA card" ) 617 618def test_fetch_follows_the_rule_and_reads_the_feed_only_when_it_applies (self ): 619feed = self .make_feed (sub = "all" ,backends = ("metal" ,"cuda" ,"hip" )) 620for hardware ,name ,fetched in ((L620 ,IQ3S ,True ), (SPARK ,V31 ,True ), (STRIX ,Q4 ,True ), (STRIX ,V31 ,True ), (P2003 ,IQ3S ,False ), (R9700 ,IQ3S ,False ), (ARC ,IQ3S ,False ), 621 (TWO_CARDS ,IQ3S ,False ), (L620 ,Q4 ,False )): 622 (self .data / "dflash2" ).mkdir (parents = True ,exist_ok = True ) 623for f in (self .data / "dflash2" ).glob ("*strata1*" ): 624f .unlink () 625_label ,backend ,archs ,vram = hardware 626where = (hardware [0 ],name ) 627self .assertEqual (drafting .needs_tuned ("auto" ,model_name = name ,backend = backend ,archs = archs ,data_dir = self .data ,args = self .args ,vram_mib = vram ),fetched ,where ) 628with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" ))as stock : 629got = drafting_fetch .ensure_for (self .data ,model_name = name ,backend = backend ,feed = str (feed ),archs = archs ,args = self .args ,vram_mib = vram ) 630if fetched : 631self .assertEqual (got ,drafting .tuned_path (self .data ),where ) 632stock .assert_not_called () 633else : 634self .assertEqual (got ,Path ("/stock" ),where ) 635self .assertFalse (drafting .tuned_path (self .data ).exists (),where ) 636for name in (IQ3S ,Q4 ):# the feed was never read for the ones that do not qualify 637with mock .patch .object (drafting_fetch ,"tuned_from_feed" ,side_effect = AssertionError ("feed read" )),mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" )): 638for hardware in (P2003 ,R9700 ,ARC ,TWO_CARDS ): 639drafting_fetch .ensure_for (self .data ,model_name = name ,backend = hardware [1 ],archs = hardware [2 ],args = self .args ,vram_mib = hardware [3 ]) 640 641def test_a_feed_that_lists_only_metal_gives_cuda_and_hip_the_stock_drafter (self ): 642feed = self .make_feed (sub = "mac-only" ,backends = ("metal" ,)) 643for hardware ,name in ((L620 ,IQ3S ), (STRIX ,Q4 )): 644with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" ))as stock : 645got = drafting_fetch .ensure_for (self .data ,model_name = name ,backend = hardware [1 ],feed = str (feed ),archs = hardware [2 ],args = self .args ,vram_mib = hardware [3 ]) 646self .assertEqual (got ,Path ("/stock" ),hardware [0 ]) 647stock .assert_called_once () 648 649def test_the_fallback_order_is_tuned_then_stock_then_the_mtp_and_offline_is_silent (self ): 650said = [] 651with mock .patch .object (drafting_fetch ,"ensure" ,side_effect = drafting_fetch .OfflineError ("no internet" )): 652for hardware ,name in ((L620 ,IQ3S ), (SPARK ,IQ3S ), (STRIX ,Q4 )): 653got = drafting_fetch .ensure_for (self .data ,model_name = name ,backend = hardware [1 ],archs = hardware [2 ],args = self .args ,vram_mib = hardware [3 ], 654feed = str (self .root / "no-feed-here" ),progress = lambda * a :said .append (a )) 655self .assertIsNone (got ,hardware [0 ]) 656self .assertEqual (said , []) 657self .assertEqual (self .apply_on (L620 ,IQ3S )[0 ].drafter ,"mtp" ,"nothing fetched: the MTP, exactly as before" ) 658self .put_stock () 659d ,args = self .apply_on (L620 ,IQ3S ) 660self .assertEqual ((d .drafter ,flag (args ,"--draft-model" )), ("dflash2" ,str (drafting .pack_path (self .data ,self .exe ))),"the stock pack" ) 661 662def test_the_plan_says_the_small_download_only_where_it_applies (self ): 663self .assertEqual (drafting_fetch .plan (self .data ,model_name = IQ3S ,backend = "cuda" ,archs = ["sm_121" ],args = self .args )["gb" ],drafting .TUNED_GB ) 664self .assertEqual (drafting_fetch .plan (self .data ,model_name = Q4 ,backend = "hip" ,archs = ["gfx1151" ],args = self .args )["gb" ],drafting .TUNED_GB ) 665self .assertEqual (drafting_fetch .plan (self .data ,model_name = IQ3S ,backend = "cuda" ,archs = ["sm_86" ],args = self .args ,vram_mib = 12288 )["gb" ],drafting .DOWNLOAD_GB ) 666self .assertEqual (drafting_fetch .plan (self .data ,model_name = IQ3S ,backend = "hip" ,archs = ["gfx1201" ],args = self .args ,vram_mib = 32768 )["gb" ],drafting .DOWNLOAD_GB ) 667 668def test_the_card_memory_read (self ): 669class Reader : 670def __init__ (self ,total ): 671self .total = total 672 673def ok (self ): 674return self .total is not None 675 676def read (self ): 677return {"mem_total" :self .total } 678gib = 1 << 30 679with mock .patch ("serve.telemetry.gpu_reader" ,lambda index ,amd = False :Reader (72 * gib )): 680self .assertEqual (drafting .card_vram_mib ({"gpu" :0 },"cuda" , ["sm_120" ]),72 * 1024 ) 681self .assertEqual (drafting .card_vram_mib ({},"cuda" , ["sm_120" ]),72 * 1024 ,"no card named: card 0, as the server asks" ) 682self .assertIsNone (drafting .card_vram_mib ({"gpu" : [0 ,1 ]},"cuda" , ["sm_120" ]),"several cards" ) 683self .assertIsNone (drafting .card_vram_mib ({"gpu" :"0,1" },"cuda" , ["sm_120" ])) 684self .assertIsNone (drafting .card_vram_mib ({"gpu" :0 },"cuda" , ["sm_121" ]),"the Spark: one pool" ) 685self .assertIsNone (drafting .card_vram_mib ({"gpu" :0 },"hip" , ["gfx1151" ]),"Strix Halo" ) 686with mock .patch ("serve.telemetry.gpu_reader" ,lambda index ,amd = False :Reader (None )): 687self .assertIsNone (drafting .card_vram_mib ({"gpu" :0 },"cuda" , ["sm_120" ]),"a driver that does not answer" ) 688 689 690IQ3XXS = "qwen3.8-flash-next-iq3_xxs" 691BF16_BYTES = b"tuned-bf16sel-pack-" * 150_000 # 2.85 MB, stands in for the 522 MB file 692 693 694class Arc (Fetch ): 695"""Owner 11-06 ~11:30 "Yes for Arc": the SYCL 0.2.11 engine refuses the Q4_0-selector pack, so Arc gets its OWN tuned file (bf16 selector codebooks, 522,458,112 bytes) for IQ3_XXS, the set 696DF-VAL measured on the A750, at Arc's current conf 0.6. Every other Arc set stays on the stock pack; CUDA / HIP / Metal never see the bf16sel file, SYCL never sees the selq4 one.""" 697 698def setUp (self ): 699super ().setUp () 700pins = mock .patch .multiple (drafting ,TUNED_BF16_BYTES = len (BF16_BYTES ),TUNED_BF16_SHA256 = hashlib .sha256 (BF16_BYTES ).hexdigest ()) 701pins .start () 702self .addCleanup (pins .stop ) 703self .sycl_exe = self .root / "strata-sycl" 704self .sycl_exe .write_bytes (b"--mtp-draft-vocab FILE (or --mtp DIR ... --draft-fallback" )# reads bf16 selector codebooks only 705 706def put_bf16 (self ,content = BF16_BYTES ): 707 (self .data / "dflash2" / drafting .TUNED_BF16_NAME ).write_bytes (content ) 708 709def arc (self ,name = IQ3XXS ,** extra ): 710cfg = self .cfg (name ,** extra ) 711decision = drafting .apply (cfg ,data_dir = self .data ,backend = "sycl" ,archs = ["arc_a" ],engine = self .sycl_exe ) 712return decision ,cfg ["args" ] 713 714def two_entry_feed (self ,sub = "two" ,bf16_backends = ("sycl" ,),selq4_backends = ("metal" ,"cuda" ,"hip" )): 715out = self .root / sub 716entries = self .root / (sub + "-entries" ) 717out .mkdir (parents = True ,exist_ok = True ) 718for name ,content ,backends in ((drafting .TUNED_NAME ,TUNED_BYTES ,selq4_backends ), (drafting .TUNED_BF16_NAME ,BF16_BYTES ,bf16_backends )): 719if not backends : 720continue 721 (out / name ).write_bytes (content ) 722ft .pack_drafter (out / name ,entries ,version = "1" ,backends = list (backends )) 723 (entries / name ).unlink (missing_ok = True ) 724ft .build_feed (out ,key = ft ._load_key (self .keys / "feed-key.json" ),sequence = 1 ,drafters_dir = entries ) 725return out 726 727def test_the_pins (self ): 728self .assertEqual (drafting .tuned_pack ("bf16sel" ).name ,"qwen3.8-flash-next-dflash2.strata1.q4_0-bf16sel.pack" ) 729self .assertNotEqual (drafting .tuned_pack ("bf16sel" ).name ,drafting .tuned_pack ("selq4" ).name ) 730self .assertEqual (drafting .tuned_pack ("selq4" ).name ,drafting .TUNED_NAME ) 731self .assertEqual (drafting .tuned_pack (None ),drafting .tuned_pack ("selq4" )) 732self .assertTrue (drafting .tuned_pack ("selq4" ).selector_q4 and not drafting .tuned_pack ("bf16sel" ).selector_q4 ) 733 734def test_the_real_pins (self ): 735with mock .patch .multiple (drafting ,TUNED_BF16_BYTES = 522458112 ,TUNED_BF16_SHA256 = "34ca17fee701ff088b6069acc35a26661d237953f5d114be0a3f7dae3ec5cda9" ): 736pin = drafting .tuned_pack ("bf16sel" ) 737self .assertEqual ((pin .size ,pin .sha256 ,pin .gb ), (522458112 ,"34ca17fee701ff088b6069acc35a26661d237953f5d114be0a3f7dae3ec5cda9" ,0.52 )) 738self .assertEqual (pin .size ,drafting .PACK_BYTES ,"the same format and size as the stock Q4_0 pack (bf16 selector)" ) 739self .assertNotEqual (pin .sha256 ,drafting .PACK_SHA256 ,"other weights: not the stock pack" ) 740self .assertNotEqual (pin .sha256 ,drafting .tuned_pack ("selq4" ).sha256 ) 741 742def test_iq3_xxs_on_arc_uses_the_bf16sel_pack_at_arcs_own_conf (self ): 743self .put_stock () 744self .put_bf16 () 745d ,args = self .arc () 746self .assertEqual ((d .drafter ,d .note ), ("dflash2" ,"" )) 747self .assertEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data ,"bf16sel" ))) 748self .assertEqual ((flag (args ,"--draft-conf" ),flag (args ,"--spec" )), ("0.6" ,"6" )) 749self .assertEqual (drafting .BACKEND_CONF ["sycl" ],0.6 ) 750 (self .data / "dflash2" / drafting .TUNED_BF16_NAME ).unlink () 751 (self .data / "dflash2" / (drafting .TUNED_BF16_NAME + ".ok" )).unlink () 752d2 ,args2 = self .arc () 753self .assertEqual (flag (args2 ,"--draft-model" ),str (self .data / "dflash2" / drafting .PACK_NAME ),"without the file: the stock pack" ) 754self .assertEqual (flag (args ,"--draft-conf" ),flag (args2 ,"--draft-conf" ),"only the pack changes" ) 755strip = lambda a : [x for i ,x in enumerate (a )if not (i and a [i - 1 ]== "--draft-model" )]# noqa: E731 756self .assertEqual (strip (args ),strip (args2 )) 757 758def test_no_other_arc_set_and_no_other_arc_class_gets_it (self ): 759self .put_stock () 760self .put_bf16 () 761self .put_tuned () 762for name in (IQ3S ,Q4 ,V31 ,OLD_XS ,Q5 ,GYRO ,"qwen3.8-flash-next-q2_0" ): 763d ,args = self .arc (name ) 764self .assertEqual (flag (args ,"--draft-model" ),str (self .data / "dflash2" / drafting .PACK_NAME ),name ) 765self .assertFalse (drafting .tuned_applies ("sycl" ,name , ["arc_a" ]),name ) 766for arch in ("arc_b" ,"arc_a_pro" ,"intel_igpu" ,"" ,"sm_120" ): 767self .assertFalse (drafting .tuned_applies ("sycl" ,IQ3XXS , [arch ]if arch else []),arch ) 768 769def test_sycl_never_gets_the_selq4_file (self ): 770self .put_stock () 771self .put_tuned ()# only the selq4 file is here 772for name in (IQ3XXS ,IQ3S ,Q4 ,V31 ): 773d ,args = self .arc (name ) 774self .assertEqual (flag (args ,"--draft-model" ),str (self .data / "dflash2" / drafting .PACK_NAME ),name ) 775self .assertNotEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data )),name ) 776self .put_bf16 ()# both are here: the bf16sel one 777d ,args = self .arc () 778self .assertEqual (flag (args ,"--draft-model" ),str (drafting .tuned_path (self .data ,"bf16sel" ))) 779# even an engine that reads the Q4_0 selector gets the bf16sel pack on SYCL, never the other 780cfg = self .cfg (IQ3XXS ) 781drafting .apply (cfg ,data_dir = self .data ,backend = "sycl" ,archs = ["arc_a" ],engine = self .exe ) 782self .assertEqual (flag (cfg ["args" ],"--draft-model" ),str (drafting .tuned_path (self .data ,"bf16sel" ))) 783self .assertEqual (drafting .tuned_variant ("sycl" ,IQ3XXS ),"bf16sel" ) 784 785def test_cuda_hip_and_metal_never_get_the_bf16sel_file (self ): 786self .put_stock () 787self .put_bf16 () 788bf16 = str (drafting .tuned_path (self .data ,"bf16sel" )) 789for hardware ,name in ((L620 ,IQ3S ), (SPARK ,V31 ), (STRIX ,Q4 ), (P2003 ,IQ3S ), (R9700 ,IQ3S ), (L620 ,IQ3XXS ), (STRIX ,IQ3XXS ), (SPARK ,IQ3XXS )): 790d ,args = self .apply_on_hw (hardware ,name ) 791self .assertNotEqual (flag (args ,"--draft-model" ),bf16 , (hardware [0 ],name )) 792self .assertFalse (drafting .tuned_applies (hardware [1 ],name ,hardware [2 ],args = self .args ,vram_mib = hardware [3 ])and drafting .tuned_variant (hardware [1 ],name )== "bf16sel" ) 793cfg = self .cfg (Q4 ) 794drafting .apply (cfg ,data_dir = self .data ,backend = "metal" ,archs = [],engine = self .exe ) 795self .assertNotEqual (flag (cfg ["args" ],"--draft-model" ),bf16 ) 796self .assertEqual ({k [0 ]for k ,v in drafting .SET_DEFAULTS .items ()if v .tuned and v .pack == "bf16sel" }, {"sycl" }) 797self .assertEqual ({k [0 ]for k ,v in drafting .SET_DEFAULTS .items ()if v .tuned and v .pack == "selq4" }, {"metal" ,"cuda" ,"hip" }) 798# and pack_path itself never returns it for a selq4 entry, whatever the engine reads 799self .put_tuned () 800for backend ,name in (("cuda" ,IQ3S ), ("hip" ,Q4 ), ("metal" ,Q4 )): 801self .assertEqual (drafting .pack_path (self .data ,self .exe ,model_name = name ,backend = backend ,tuned = True ),drafting .tuned_path (self .data ),backend ) 802 803def apply_on_hw (self ,hardware ,name ): 804_label ,backend ,archs ,vram = hardware 805cfg = self .cfg (name ) 806d = drafting .apply (cfg ,data_dir = self .data ,backend = backend ,archs = archs ,engine = self .exe ,vram_mib = vram ) 807return d ,cfg ["args" ] 808 809def test_the_fetch_for_arc (self ): 810feed = self .two_entry_feed () 811 (self .data / "dflash2" ).mkdir (parents = True ,exist_ok = True ) 812self .assertTrue (drafting .needs_tuned ("auto" ,model_name = IQ3XXS ,backend = "sycl" ,archs = ["arc_a" ],data_dir = self .data )) 813for name in (IQ3S ,Q4 ,V31 ): 814self .assertFalse (drafting .needs_tuned ("auto" ,model_name = name ,backend = "sycl" ,archs = ["arc_a" ],data_dir = self .data ),name ) 815seen = [] 816with mock .patch .object (drafting_fetch ,"ensure" ,side_effect = AssertionError ("stock fetched" )): 817got = drafting_fetch .ensure_for (self .data ,model_name = IQ3XXS ,backend = "sycl" ,archs = ["arc_a" ],feed = str (feed ),progress = lambda * a :seen .append (a )) 818self .assertEqual (got ,drafting .tuned_path (self .data ,"bf16sel" )) 819self .assertTrue (drafting .tuned_ok (self .data ,"bf16sel" )) 820self .assertFalse (drafting .tuned_path (self .data ).exists (),"the selq4 file was not fetched for SYCL" ) 821self .assertEqual (got .read_bytes (),BF16_BYTES ) 822self .assertTrue ((self .data / "dflash2" / "LICENSE-dflash2-tuned" ).is_file ()) 823self .assertFalse (drafting .needs_tuned ("auto" ,model_name = IQ3XXS ,backend = "sycl" ,archs = ["arc_a" ],data_dir = self .data )) 824 825def test_the_other_platforms_fetch_their_own_file_from_the_same_feed (self ): 826feed = self .two_entry_feed (sub = "both" ) 827for hardware ,name in ((L620 ,IQ3S ), (STRIX ,Q4 )): 828for f in (self .data / "dflash2" ).glob ("*strata1*" ): 829f .unlink () 830got = drafting_fetch .ensure_for (self .data ,model_name = name ,backend = hardware [1 ],archs = hardware [2 ],args = self .args ,vram_mib = hardware [3 ],feed = str (feed )) 831self .assertEqual (got ,drafting .tuned_path (self .data ),hardware [0 ]) 832self .assertFalse (drafting .tuned_path (self .data ,"bf16sel" ).exists (),hardware [0 ]) 833for f in (self .data / "dflash2" ).glob ("*strata1*" ): 834f .unlink () 835mac = drafting_fetch .ensure_for (self .data ,model_name = Q4 ,backend = "metal" ,feed = str (feed )) 836self .assertEqual (mac ,drafting .tuned_path (self .data )) 837self .assertFalse (drafting .tuned_path (self .data ,"bf16sel" ).exists ()) 838 839def test_a_feed_without_the_sycl_entry_gives_arc_the_stock_drafter (self ): 840feed = self .two_entry_feed (sub = "no-sycl" ,bf16_backends = ()) 841with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" ))as stock : 842got = drafting_fetch .ensure_for (self .data ,model_name = IQ3XXS ,backend = "sycl" ,archs = ["arc_a" ],feed = str (feed )) 843self .assertEqual (got ,Path ("/stock" )) 844stock .assert_called_once () 845only_selq4_for_sycl = self .two_entry_feed (sub = "wrong" ,bf16_backends = (),selq4_backends = ("sycl" ,)) 846with mock .patch .object (drafting_fetch ,"ensure" ,return_value = Path ("/stock" )): 847got = drafting_fetch .ensure_for (self .data ,model_name = IQ3XXS ,backend = "sycl" ,archs = ["arc_a" ],feed = str (only_selq4_for_sycl )) 848self .assertEqual (got ,Path ("/stock" ),"a feed that (wrongly) lists the selq4 file for sycl is not what Arc asks for: it asks for the bf16sel name" ) 849self .assertFalse (drafting .tuned_path (self .data ).exists ()) 850 851def test_a_wrong_bf16sel_file_is_not_used_and_a_bad_download_is_deleted (self ): 852self .put_stock () 853self .put_bf16 (b"x" * len (BF16_BYTES )) 854self .assertFalse (drafting .tuned_ok (self .data ,"bf16sel" )) 855d ,args = self .arc () 856self .assertEqual (flag (args ,"--draft-model" ),str (self .data / "dflash2" / drafting .PACK_NAME )) 857 (self .data / "dflash2" / drafting .TUNED_BF16_NAME ).unlink () 858feed = self .two_entry_feed (sub = "bad" ) 859 (feed / drafting .TUNED_BF16_NAME ).write_bytes (b"z" * len (BF16_BYTES )) 860with self .assertRaises (drafting_fetch .DraftingError )as caught : 861drafting_fetch .ensure_tuned (self .data ,feed = str (feed ),backend = "sycl" ,variant = "bf16sel" ) 862self .assertIn ("safety check" ,str (caught .exception )) 863self .assertFalse (drafting .tuned_path (self .data ,"bf16sel" ).exists ()) 864 865def test_the_plan_says_0_52_gb_for_arc_and_the_conf_tables_are_unchanged (self ): 866self .assertEqual (drafting_fetch .plan (self .data ,model_name = IQ3XXS ,backend = "sycl" ,archs = ["arc_a" ])["gb" ],0.52 ) 867self .assertEqual (drafting_fetch .plan (self .data ,model_name = IQ3S ,backend = "sycl" ,archs = ["arc_a" ])["gb" ],drafting .DOWNLOAD_GB ) 868self .assertEqual (drafting_fetch .plan (self .data ,model_name = IQ3S ,backend = "cuda" ,archs = ["sm_121" ])["gb" ],0.34 ) 869self .assertEqual (drafting .CONF_STEPS , ((12000 ,0.35 ), (7500 ,0.5 ), (3000 ,0.7 ), (0 ,0.8 ))) 870self .assertEqual (drafting .BACKEND_CONF , {"sycl" :0.6 ,"metal" :0.5 }) 871self .assertEqual ((drafting .DRAFT_CONF ,drafting .SPEC_WINDOW ), (0.35 ,6 )) 872self .assertIsNone (drafting .tuned_conf_for ("sycl" ,IQ3XXS ),"no conf of its own: Arc's 0.6 stays" ) 873 874 875class SetupOnAMac (Base ): 876"""metal/setup_mac.fetch_drafter: the line appears when a download starts, never offline; the MTP stays the fallback.""" 877 878@classmethod 879def setUpClass (cls ): 880import importlib .util 881import sys 882spec = importlib .util .spec_from_file_location ("t_setup_mac" ,ROOT / "metal" / "setup_mac.py" ) 883cls .mac = importlib .util .module_from_spec (spec ) 884sys .modules .setdefault ("t_setup_mac" ,cls .mac ) 885try : 886spec .loader .exec_module (cls .mac ) 887except SystemExit : 888raise unittest .SkipTest ("setup.py would not import here" ) 889 890def setUp (self ): 891super ().setUp () 892self .host = self .root / "host" 893self .host .mkdir () 894 (self .host / drafting .TUNED_NAME ).write_bytes (TUNED_BYTES ) 895self .said = [] 896for name in ("say" ,"ok" ,"warn" ): 897patcher = mock .patch .object (self .mac .S ,name ,lambda msg = "" ,_n = name :self .said .append ((_n ,msg ))) 898patcher .start () 899self .addCleanup (patcher .stop ) 900 901def test_it_fetches_the_tuned_drafter_with_one_line_before_and_one_after (self ): 902with mock .patch .dict ("os.environ" , {drafting .TUNED_URL_ENV : (self .host / drafting .TUNED_NAME ).as_uri ()}): 903got = self .mac .fetch_drafter (Q4 ,self .data ) 904self .assertEqual (got ,drafting .tuned_path (self .data )) 905self .assertEqual ([k for k ,_ in self .said ], ["say" ,"ok" ]) 906self .assertIn ("Downloading the DFlash2 drafter (0.34 GB, once)" ,self .said [0 ][1 ]) 907 908def test_offline_it_says_nothing (self ): 909from tests .terminal import net_dead 910with mock .patch .object (drafting_fetch ,"online" ,return_value = False ),net_dead .no_name_resolution ():# (the feed lookup is offline too) 911self .assertIsNone (self .mac .fetch_drafter (Q4 ,self .data )) 912self .assertEqual (self .said , []) 913 914def test_iq3_s_and_other_sets_fetch_nothing (self ): 915with mock .patch .object (drafting_fetch ,"ensure_for" ,side_effect = AssertionError ("fetched" )): 916self .assertIsNone (self .mac .fetch_drafter (IQ3S ,self .data )) 917self .assertEqual (self .said , []) 918 919def test_a_failed_safety_check_is_one_warning_and_the_setup_goes_on (self ): 920 (self .host / drafting .TUNED_NAME ).write_bytes (b"z" * len (TUNED_BYTES )) 921with mock .patch .dict ("os.environ" , {drafting .TUNED_URL_ENV : (self .host / drafting .TUNED_NAME ).as_uri ()}), \ 922mock .patch .object (drafting_fetch ,"ensure" ,side_effect = drafting_fetch .OfflineError ("no internet" )): 923self .assertIsNone (self .mac .fetch_drafter (Q4 ,self .data )) 924self .assertEqual ([k for k ,_ in self .said ], ["say" ,"warn" ]) 925self .assertIn ("safety check" ,self .said [1 ][1 ]) 926self .assertIn ("keeps using the MTP" ,self .said [1 ][1 ]) 927 928 929class Pins (unittest .TestCase ): 930def test_the_pinned_numbers_are_the_final_pack (self ): 931self .assertEqual (drafting .TUNED_BYTES ,339694592 ) 932self .assertEqual (drafting .TUNED_BYTES ,drafting .CONVERTED_BYTES ,"the same format and size as the stock Q4_0-selector pack" ) 933self .assertEqual (drafting .TUNED_SHA256 ,"4df2e5ab2b47c064afbe662e16e4f06569877125c2d0e2123a4ac755266c664c" ) 934self .assertNotEqual (drafting .TUNED_SHA256 ,drafting .CONVERTED_SHA256 ) 935self .assertNotEqual (drafting .TUNED_NAME ,drafting .CONVERTED_NAME ) 936 937def test_the_licence_files_ship_in_data (self ): 938folder = ROOT / drafting .TUNED_LICENCE_FOLDER 939self .assertIn ("Apache License" , (folder / "LICENSE" ).read_text (encoding = "utf-8" )) 940notice = (folder / "NOTICE.md" ).read_text (encoding = "utf-8" ) 941self .assertIn (drafting .TUNED_SHA256 ,notice ) 942self .assertIn ("a22a1744e85c17364dcc54a6a420af7135d4c7f6" ,notice ) 943self .assertIn ("dflash2-tuned" , (ROOT / "docs" / "DETAILS.md" ).read_text (encoding = "utf-8" ),"the third-party notices name it" ) 944 945def test_the_base_revision_is_the_one_the_stock_drafter_is_pinned_to (self ): 946self .assertIn (drafting .REVISION ,drafting .TUNED_BASE ) 947 948def test_set_names (self ): 949self .assertEqual (drafting .set_of (Q4 ),"unsloth-ud-q4_k_xl" ) 950self .assertEqual (drafting .set_of (V31 ),"unsloth-ud-iq4_xs-v31" ) 951self .assertEqual (drafting .set_of ("Qwen3.8-Flash-Next-IQ3_S" ),"iq3_s" ) 952self .assertEqual (drafting .set_of (None ),"" ) 953self .assertTrue (drafting .supported_model (V31 ),"the v31 name passes the supported-model pattern" ) 954 955 956if __name__ == "__main__" : 957unittest .main ()