Files
leleka/engine/strata/tools/test_setup_ownfiles.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

115 lines
4.9 KiB
Python

"""setup.py and the person's own model files (--gguf-dir): setup checks them, it never deletes them (0.2.2).
python -m unittest tools.test_setup_ownfiles
"""
from __future__ import annotations
import contextlib
import hashlib
import io
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tools"))
import setup # noqa: E402
def run(fn, *args, **kw):
out = io.StringIO()
with contextlib.redirect_stdout(out):
try:
fn(*args, **kw)
code = 0
except SystemExit as e:
code = e.code
return code, out.getvalue()
class OwnFileChecks(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.file = Path(self.tmp.name) / "model-00001-of-00002.gguf"
self.file.write_bytes(b"abcdefgh" * 100)
self.sha = hashlib.sha256(self.file.read_bytes()).hexdigest()
def test_a_downloaded_file_of_the_wrong_size_asks_to_delete_it(self):
code, text = run(setup.verify_sha256, self.file, 801, self.sha)
self.assertEqual(code, 1)
self.assertIn("delete it and run setup again", text)
def test_an_own_file_of_the_wrong_size_is_left_and_the_text_says_so(self):
code, text = run(setup.verify_sha256, self.file, 801, self.sha, own=True)
self.assertEqual(code, 1)
self.assertIn("your file was left as it is", text)
self.assertTrue(self.file.exists())
def test_an_own_file_with_another_hash_is_not_deleted(self):
code, text = run(setup.verify_sha256, self.file, 800, "0" * 64, own=True)
self.assertEqual(code, 1)
self.assertIn("does not match the published file", text)
self.assertTrue(self.file.exists())
def test_a_downloaded_file_with_another_hash_is_deleted_as_before(self):
code, _text = run(setup.verify_sha256, self.file, 800, "0" * 64)
self.assertEqual(code, 1)
self.assertFalse(self.file.exists())
def test_a_complete_own_gguf_of_another_size_or_hash_is_used_with_a_warning(self): # #3777
import importlib.util # by path: another `tests` package may already be loaded
spec = importlib.util.spec_from_file_location("own_files_helper", Path(__file__).resolve().parents[1] / "tests" / "terminal" / "test_own_files.py")
helper = importlib.util.module_from_spec(spec)
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
spec.loader.exec_module(helper)
gguf_bytes = helper.gguf_bytes
gguf = Path(self.tmp.name) / "fine-tune.gguf"
gguf.write_bytes(gguf_bytes(["Q8_0"]))
for size, sha in ((gguf.stat().st_size + 96, "0" * 64), (gguf.stat().st_size, "0" * 64)):
code, text = run(setup.verify_sha256, gguf, size, sha, own=True)
self.assertEqual(code, 0, text)
self.assertIn("Strata uses it as it is", text)
cut = Path(self.tmp.name) / "cut.gguf"
cut.write_bytes(gguf.read_bytes()[:-40]) # cut short: the tensor payload is not all there
code, text = run(setup.verify_sha256, cut, gguf.stat().st_size, "0" * 64, own=True)
self.assertEqual(code, 1)
self.assertIn("your file was left as it is", text)
code, text = run(setup.verify_sha256, gguf, gguf.stat().st_size + 96, "0" * 64) # a download is still checked
self.assertEqual(code, 1)
def test_a_matching_own_file_passes(self):
code, _text = run(setup.verify_sha256, self.file, 800, self.sha, own=True)
self.assertEqual(code, 0)
class CheckingShowsItsProgress(unittest.TestCase):
def test_a_big_file_is_hashed_with_a_progress_line_of_its_own(self):
"""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%)."""
with tempfile.TemporaryDirectory() as tmp:
f = Path(tmp) / "big-00001-of-00002.gguf"
size = (1 << 30) + 4096
with open(f, "wb") as g:
g.truncate(size)
sha = hashlib.sha256()
with open(f, "rb") as g:
for block in iter(lambda: g.read(16 << 20), b""):
sha.update(block)
import time
original = time.time
ticks = iter(range(0, 10_000, 3))
setup.time.time = lambda: float(next(ticks)) # every read looks 3 s later: the progress line prints
try:
code, text = run(setup.verify_sha256, f, size, sha.hexdigest())
finally:
setup.time.time = original
self.assertEqual(code, 0)
self.assertIn("checking big-00001-of-00002.gguf (SHA-256", text)
self.assertRegex(text, r"checking big-00001-of-00002\.gguf: +\d+\.\d\d / 1\.07 GB \(\d+%\)")
if __name__ == "__main__":
unittest.main()