okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9741a61d7
main
1"""setup.py and the person's own model files (--gguf-dir): setup checks them, it never deletes them (0.2.2). 2 3python -m unittest tools.test_setup_ownfiles 4""" 5from __future__import annotations 6 7import contextlib 8import hashlib 9import io 10import sys 11import tempfile 12import unittest 13from pathlib import Path 14 15ROOT = Path (__file__ ).resolve ().parents [1 ] 16sys .path .insert (0 ,str (ROOT )) 17sys .path .insert (0 ,str (ROOT / "tools" )) 18import setup # noqa: E402 19 20 21def run (fn ,* args ,** kw ): 22out = io .StringIO () 23with contextlib .redirect_stdout (out ): 24try : 25fn (* args ,** kw ) 26code = 0 27except SystemExit as e : 28code = e .code 29return code ,out .getvalue () 30 31 32class OwnFileChecks (unittest .TestCase ): 33def setUp (self ): 34self .tmp = tempfile .TemporaryDirectory () 35self .addCleanup (self .tmp .cleanup ) 36self .file = Path (self .tmp .name )/ "model-00001-of-00002.gguf" 37self .file .write_bytes (b"abcdefgh" * 100 ) 38self .sha = hashlib .sha256 (self .file .read_bytes ()).hexdigest () 39 40def test_a_downloaded_file_of_the_wrong_size_asks_to_delete_it (self ): 41code ,text = run (setup .verify_sha256 ,self .file ,801 ,self .sha ) 42self .assertEqual (code ,1 ) 43self .assertIn ("delete it and run setup again" ,text ) 44 45def test_an_own_file_of_the_wrong_size_is_left_and_the_text_says_so (self ): 46code ,text = run (setup .verify_sha256 ,self .file ,801 ,self .sha ,own = True ) 47self .assertEqual (code ,1 ) 48self .assertIn ("your file was left as it is" ,text ) 49self .assertTrue (self .file .exists ()) 50 51def test_an_own_file_with_another_hash_is_not_deleted (self ): 52code ,text = run (setup .verify_sha256 ,self .file ,800 ,"0" * 64 ,own = True ) 53self .assertEqual (code ,1 ) 54self .assertIn ("does not match the published file" ,text ) 55self .assertTrue (self .file .exists ()) 56 57def test_a_downloaded_file_with_another_hash_is_deleted_as_before (self ): 58code ,_text = run (setup .verify_sha256 ,self .file ,800 ,"0" * 64 ) 59self .assertEqual (code ,1 ) 60self .assertFalse (self .file .exists ()) 61 62def test_a_complete_own_gguf_of_another_size_or_hash_is_used_with_a_warning (self ):# #3777 63import importlib .util # by path: another `tests` package may already be loaded 64spec = importlib .util .spec_from_file_location ("own_files_helper" ,Path (__file__ ).resolve ().parents [1 ]/ "tests" / "terminal" / "test_own_files.py" ) 65helper = importlib .util .module_from_spec (spec ) 66sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 67spec .loader .exec_module (helper ) 68gguf_bytes = helper .gguf_bytes 69gguf = Path (self .tmp .name )/ "fine-tune.gguf" 70gguf .write_bytes (gguf_bytes (["Q8_0" ])) 71for size ,sha in ((gguf .stat ().st_size + 96 ,"0" * 64 ), (gguf .stat ().st_size ,"0" * 64 )): 72code ,text = run (setup .verify_sha256 ,gguf ,size ,sha ,own = True ) 73self .assertEqual (code ,0 ,text ) 74self .assertIn ("Strata uses it as it is" ,text ) 75cut = Path (self .tmp .name )/ "cut.gguf" 76cut .write_bytes (gguf .read_bytes ()[:- 40 ])# cut short: the tensor payload is not all there 77code ,text = run (setup .verify_sha256 ,cut ,gguf .stat ().st_size ,"0" * 64 ,own = True ) 78self .assertEqual (code ,1 ) 79self .assertIn ("your file was left as it is" ,text ) 80code ,text = run (setup .verify_sha256 ,gguf ,gguf .stat ().st_size + 96 ,"0" * 64 )# a download is still checked 81self .assertEqual (code ,1 ) 82 83def test_a_matching_own_file_passes (self ): 84code ,_text = run (setup .verify_sha256 ,self .file ,800 ,self .sha ,own = True ) 85self .assertEqual (code ,0 ) 86 87 88class CheckingShowsItsProgress (unittest .TestCase ): 89def test_a_big_file_is_hashed_with_a_progress_line_of_its_own (self ): 90"""Hashing a 60 GB shard takes minutes: the app and the Terminal show it as 'checking', with a figure (not a download stuck at 99%).""" 91with tempfile .TemporaryDirectory ()as tmp : 92f = Path (tmp )/ "big-00001-of-00002.gguf" 93size = (1 << 30 )+ 4096 94with open (f ,"wb" )as g : 95g .truncate (size ) 96sha = hashlib .sha256 () 97with open (f ,"rb" )as g : 98for block in iter (lambda :g .read (16 << 20 ),b"" ): 99sha .update (block ) 100import time 101original = time .time 102ticks = iter (range (0 ,10_000 ,3 )) 103setup .time .time = lambda :float (next (ticks ))# every read looks 3 s later: the progress line prints 104try : 105code ,text = run (setup .verify_sha256 ,f ,size ,sha .hexdigest ()) 106finally : 107setup .time .time = original 108self .assertEqual (code ,0 ) 109self .assertIn ("checking big-00001-of-00002.gguf (SHA-256" ,text ) 110self .assertRegex (text ,r"checking big-00001-of-00002\.gguf: +\d+\.\d\d / 1\.07 GB \(\d+%\)" ) 111 112 113if __name__ == "__main__" : 114unittest .main ()