okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0250-tui into f-0243-rel9741a61d7

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