okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5649b9e52
main
1"""#2054: BUILD.json is trusted only beside a binary of the source's version. 2 3A build that stopped (nvcc rejecting a newer host gcc) once left the old engine next to a stamp of the new source, and 4every later update said "engine already built for this PC" for good. `stale_binary` reads the version compiled into the 5binary; `build_engine` refuses to stamp or install a binary of another version and compiles again when the installed 6one is stale. 7 8python -m unittest tools.test_setup_stale_stamp 9""" 10from __future__import annotations 11 12import json 13import sys 14import tempfile 15import unittest 16from pathlib import Path 17from unittest import mock 18 19ROOT = Path (__file__ ).resolve ().parents [1 ] 20sys .path .insert (0 ,str (ROOT )) 21import setup # noqa: E402 22 23GPU = {"arch" :86 ,"archs" : [86 ],"name" :"RTX 3060" } 24 25 26def binary (version :str | None )-> bytes : 27return b"\x7fELF....tail_role_token=0 vram_elastic=0 engine=" + version .encode ()+ b"\n...." if version else b"old binary" 28 29 30class StaleBinary (unittest .TestCase ): 31def test_reads_the_compiled_in_version (self ): 32with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"source_version" ,lambda :"0.1.43" ): 33exe = Path (d )/ "strata" 34exe .write_bytes (binary ("0.1.42" )) 35self .assertEqual (setup .binary_engine_version (exe ),"0.1.42" ) 36self .assertTrue (setup .stale_binary (exe )) 37exe .write_bytes (binary ("0.1.43" )) 38self .assertFalse (setup .stale_binary (exe )) 39 40def test_four_part_version_and_unknowns (self ): 41with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"source_version" ,lambda :"0.1.43.1" ): 42exe = Path (d )/ "strata" 43exe .write_bytes (binary ("0.1.43.1" )) 44self .assertFalse (setup .stale_binary (exe )) 45exe .write_bytes (binary ("0.1.43" )) 46self .assertTrue (setup .stale_binary (exe )) 47exe .write_bytes (binary (None ))# a binary that carries no version: not judged 48self .assertFalse (setup .stale_binary (exe )) 49self .assertFalse (setup .stale_binary (Path (d )/ "missing" )) 50 51 52class BuildEngineStamp (unittest .TestCase ): 53def setUp (self ): 54tmp = tempfile .TemporaryDirectory () 55self .addCleanup (tmp .cleanup ) 56self .root = Path (tmp .name ) 57self .eng = self .root / "engine" 58self .eng .mkdir () 59 (self .eng / setup .EXE ).write_bytes (binary ("0.1.42" )) 60self .old_stamp = {"source" :"local" ,"version" :"0.1.42" ,"archs" : [86 ],"vision" :"none" , 61"cuda_dirs" : [],"src" :"oldhash" ,"vision_src" :None } 62 (self .eng / "BUILD.json" ).write_text (json .dumps (self .old_stamp )) 63self .built = binary ("0.1.43" )# what the fake compile leaves in the build folder 64self .compiles = 0 65patches = [mock .patch .object (setup ,"ROOT" ,self .root ), 66mock .patch .object (setup ,"source_hash" ,lambda parts :"newhash" ), 67mock .patch .object (setup ,"source_version" ,lambda :"0.1.43" ), 68mock .patch .object (setup ,"install_build_tools" ,lambda gpu ,yes : ("nvcc" ,None )), 69mock .patch .object (setup ,"toolkit_root_defs" ,lambda nvcc : []), 70mock .patch .object (setup ,"host_compiler_defs" ,lambda cuda : []), 71mock .patch .object (setup ,"engine_defs" ,lambda archs ,tk = 13 : []), 72mock .patch .object (setup ,"isa_floor_defs" ,lambda * a : []), 73mock .patch .object (setup ,"cpu_floor" ,lambda avx2 :"" ), 74mock .patch .object (setup ,"cpu_info" ,lambda : ("cpu" ,True )), 75mock .patch .object (setup ,"cmake_build" ,self .fake_build ), 76mock .patch .object (setup ,"gpus" ,lambda : []),# no real card of the box the test runs on (#4662: the build also covers every other card) 77mock .patch .object (setup ,"say" ,lambda * a ,** k :None ), 78mock .patch .object (setup ,"ok" ,lambda * a ,** k :None )] 79for p in patches : 80p .start () 81self .addCleanup (p .stop ) 82 83def fake_build (self ,src ,bdir ,target ,defs ,vcvars ,bat ): 84self .compiles += 1 85self .defs = list (defs ) 86bdir .mkdir (parents = True ,exist_ok = True ) 87 (bdir / setup .EXE ).write_bytes (self .built ) 88 89def stamp (self ): 90return json .loads ((self .eng / "BUILD.json" ).read_text ()) 91 92def test_a_good_build_is_stamped (self ): 93setup .build_engine (GPU ,"none" ,True ,Path ("llama" ),toolkit = 13 ) 94self .assertEqual (self .compiles ,1 ) 95self .assertEqual (self .stamp ()["src" ],"newhash" ) 96self .assertEqual (self .stamp ()["version" ],"0.1.43" ) 97self .assertEqual ((self .eng / setup .EXE ).read_bytes (),self .built ) 98 99def test_the_q6k_expert_kernels_are_pinned_on (self ): 100"""PR #2515: a build/ folder first configured while STRATA_Q6K_EXPERTS defaulted to OFF keeps it in CMakeCache.txt; 101the configure line names it, so Qwen3.6 UD-Q4_K_XL (Q4_K gate/up, Q6_K down) still loads.""" 102setup .build_engine (GPU ,"none" ,True ,Path ("llama" ),toolkit = 13 ) 103self .assertIn ("-DSTRATA_Q6K_EXPERTS=ON" ,self .defs ) 104self .assertNotIn ("-DSTRATA_Q6K_EXPERTS=OFF" ,self .defs ) 105self .assertIn ("-DSTRATA_MMQ_KQUANTS=ON" ,self .defs )# flipped OFF -> ON in the same commit (465cd61d) 106 107def test_the_build_without_mmq_is_named_on_the_configure_line (self ): 108"""STRATA_MMQ's default became OFF; a build/ folder configured before keeps ON in CMakeCache.txt, so the line names it. 109ARM64 (DGX Spark) keeps the build with MMQ (not measured without it).""" 110for arm ,want in ((False ,"-DSTRATA_MMQ=OFF" ), (True ,"-DSTRATA_MMQ=ON" )): 111with mock .patch .object (setup ,"is_arm64" ,lambda arm = arm :arm ): 112 (self .eng / "BUILD.json" ).write_text (json .dumps (self .old_stamp )) 113setup .build_engine (GPU ,"none" ,True ,Path ("llama" ),toolkit = 13 ) 114self .assertIn (want ,self .defs ) 115self .assertEqual (sum (d .startswith ("-DSTRATA_MMQ=" )for d in self .defs ),1 ) 116 117def test_a_build_that_leaves_the_old_binary_is_not_stamped_or_installed (self ): 118self .built = binary ("0.1.42" )# the build "succeeded" without producing the new engine 119with self .assertRaises (SystemExit ): 120setup .build_engine (GPU ,"none" ,True ,Path ("llama" ),toolkit = 13 ) 121self .assertEqual (self .stamp (),self .old_stamp )# still the old hash: the next update compiles again 122self .assertEqual ((self .eng / setup .EXE ).read_bytes (),binary ("0.1.42" )) 123 124def test_a_poisoned_stamp_is_compiled_again (self ): 125"""The state #2054 found: the new source hash and version in BUILD.json, the old binary beside it.""" 126 (self .eng / "BUILD.json" ).write_text (json .dumps ({** self .old_stamp ,"version" :"0.1.43" ,"src" :"newhash" })) 127setup .build_engine (GPU ,"none" ,True ,Path ("llama" ),toolkit = 13 ) 128self .assertEqual (self .compiles ,1 ) 129self .assertEqual ((self .eng / setup .EXE ).read_bytes (),self .built ) 130 131def test_a_healthy_install_is_left_alone (self ): 132 (self .eng / setup .EXE ).write_bytes (binary ("0.1.43" )) 133 (self .eng / "BUILD.json" ).write_text (json .dumps ({** self .old_stamp ,"version" :"0.1.43" ,"src" :"newhash" })) 134setup .build_engine (GPU ,"none" ,True ,Path ("llama" ),toolkit = 13 ) 135self .assertEqual (self .compiles ,0 ) 136 137 138class UpdateInstalledEngine (unittest .TestCase ): 139def test_a_poisoned_stamp_makes_the_update_compile (self ): 140with tempfile .TemporaryDirectory ()as d : 141root = Path (d ) 142eng = root / "engine" 143eng .mkdir () 144 (eng / setup .EXE ).write_bytes (binary ("0.1.42" )) 145 (eng / "BUILD.json" ).write_text (json .dumps ({"source" :"local" ,"version" :"0.1.43" ,"archs" : [86 ], 146"vision" :"none" ,"src" :"newhash" })) 147built = [] 148with mock .patch .object (setup ,"ROOT" ,root ), \ 149mock .patch .object (setup ,"source_hash" ,lambda parts :"newhash" ), \ 150mock .patch .object (setup ,"source_version" ,lambda :"0.1.43" ), \ 151mock .patch .object (setup ,"update_engine_gpu" ,lambda tk :dict (GPU )), \ 152mock .patch .object (setup ,"get_llama_cpp" ,lambda :Path ("llama" )), \ 153mock .patch .object (setup ,"build_engine" ,lambda * a ,** k :built .append (a )), \ 154mock .patch .object (setup ,"say" ,lambda * a ,** k :None ): 155setup .update_installed_engine ("http://x" ,13 ) 156self .assertEqual (len (built ),1 ) 157built .clear () 158 (eng / setup .EXE ).write_bytes (binary ("0.1.43" )) 159setup .update_installed_engine ("http://x" ,13 ) 160self .assertEqual (built , []) 161 162 163if __name__ == "__main__" : 164unittest .main ()