okami.mom/strata

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

Niko1221Merge f-mmq-default-off: build the engine without llama.cpp's MMQ kernels by default (-DSTRATA_MMQ=ON keeps the old path; linux-aarch64 unchanged)5649b9e52

main
8.7 KiB164 linesraw
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
8    python -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:
27    return 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):
31    def test_reads_the_compiled_in_version(self):
32        with tempfile.TemporaryDirectory() as d, mock.patch.object(setup, "source_version", lambda: "0.1.43"):
33            exe = Path(d) / "strata"
34            exe.write_bytes(binary("0.1.42"))
35            self.assertEqual(setup.binary_engine_version(exe), "0.1.42")
36            self.assertTrue(setup.stale_binary(exe))
37            exe.write_bytes(binary("0.1.43"))
38            self.assertFalse(setup.stale_binary(exe))
39
40    def test_four_part_version_and_unknowns(self):
41        with tempfile.TemporaryDirectory() as d, mock.patch.object(setup, "source_version", lambda: "0.1.43.1"):
42            exe = Path(d) / "strata"
43            exe.write_bytes(binary("0.1.43.1"))
44            self.assertFalse(setup.stale_binary(exe))
45            exe.write_bytes(binary("0.1.43"))
46            self.assertTrue(setup.stale_binary(exe))
47            exe.write_bytes(binary(None))                 # a binary that carries no version: not judged
48            self.assertFalse(setup.stale_binary(exe))
49            self.assertFalse(setup.stale_binary(Path(d) / "missing"))
50
51
52class BuildEngineStamp(unittest.TestCase):
53    def setUp(self):
54        tmp = tempfile.TemporaryDirectory()
55        self.addCleanup(tmp.cleanup)
56        self.root = Path(tmp.name)
57        self.eng = self.root / "engine"
58        self.eng.mkdir()
59        (self.eng / setup.EXE).write_bytes(binary("0.1.42"))
60        self.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))
63        self.built = binary("0.1.43")                     # what the fake compile leaves in the build folder
64        self.compiles = 0
65        patches = [mock.patch.object(setup, "ROOT", self.root),
66                   mock.patch.object(setup, "source_hash", lambda parts: "newhash"),
67                   mock.patch.object(setup, "source_version", lambda: "0.1.43"),
68                   mock.patch.object(setup, "install_build_tools", lambda gpu, yes: ("nvcc", None)),
69                   mock.patch.object(setup, "toolkit_root_defs", lambda nvcc: []),
70                   mock.patch.object(setup, "host_compiler_defs", lambda cuda: []),
71                   mock.patch.object(setup, "engine_defs", lambda archs, tk=13: []),
72                   mock.patch.object(setup, "isa_floor_defs", lambda *a: []),
73                   mock.patch.object(setup, "cpu_floor", lambda avx2: ""),
74                   mock.patch.object(setup, "cpu_info", lambda: ("cpu", True)),
75                   mock.patch.object(setup, "cmake_build", self.fake_build),
76                   mock.patch.object(setup, "gpus", lambda: []),          # no real card of the box the test runs on (#4662: the build also covers every other card)
77                   mock.patch.object(setup, "say", lambda *a, **k: None),
78                   mock.patch.object(setup, "ok", lambda *a, **k: None)]
79        for p in patches:
80            p.start()
81            self.addCleanup(p.stop)
82
83    def fake_build(self, src, bdir, target, defs, vcvars, bat):
84        self.compiles += 1
85        self.defs = list(defs)
86        bdir.mkdir(parents=True, exist_ok=True)
87        (bdir / setup.EXE).write_bytes(self.built)
88
89    def stamp(self):
90        return json.loads((self.eng / "BUILD.json").read_text())
91
92    def test_a_good_build_is_stamped(self):
93        setup.build_engine(GPU, "none", True, Path("llama"), toolkit=13)
94        self.assertEqual(self.compiles, 1)
95        self.assertEqual(self.stamp()["src"], "newhash")
96        self.assertEqual(self.stamp()["version"], "0.1.43")
97        self.assertEqual((self.eng / setup.EXE).read_bytes(), self.built)
98
99    def 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;
101        the configure line names it, so Qwen3.6 UD-Q4_K_XL (Q4_K gate/up, Q6_K down) still loads."""
102        setup.build_engine(GPU, "none", True, Path("llama"), toolkit=13)
103        self.assertIn("-DSTRATA_Q6K_EXPERTS=ON", self.defs)
104        self.assertNotIn("-DSTRATA_Q6K_EXPERTS=OFF", self.defs)
105        self.assertIn("-DSTRATA_MMQ_KQUANTS=ON", self.defs)  # flipped OFF -> ON in the same commit (465cd61d)
106
107    def 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.
109        ARM64 (DGX Spark) keeps the build with MMQ (not measured without it)."""
110        for arm, want in ((False, "-DSTRATA_MMQ=OFF"), (True, "-DSTRATA_MMQ=ON")):
111            with mock.patch.object(setup, "is_arm64", lambda arm=arm: arm):
112                (self.eng / "BUILD.json").write_text(json.dumps(self.old_stamp))
113                setup.build_engine(GPU, "none", True, Path("llama"), toolkit=13)
114            self.assertIn(want, self.defs)
115            self.assertEqual(sum(d.startswith("-DSTRATA_MMQ=") for d in self.defs), 1)
116
117    def test_a_build_that_leaves_the_old_binary_is_not_stamped_or_installed(self):
118        self.built = binary("0.1.42")                     # the build "succeeded" without producing the new engine
119        with self.assertRaises(SystemExit):
120            setup.build_engine(GPU, "none", True, Path("llama"), toolkit=13)
121        self.assertEqual(self.stamp(), self.old_stamp)     # still the old hash: the next update compiles again
122        self.assertEqual((self.eng / setup.EXE).read_bytes(), binary("0.1.42"))
123
124    def 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"}))
127        setup.build_engine(GPU, "none", True, Path("llama"), toolkit=13)
128        self.assertEqual(self.compiles, 1)
129        self.assertEqual((self.eng / setup.EXE).read_bytes(), self.built)
130
131    def 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"}))
134        setup.build_engine(GPU, "none", True, Path("llama"), toolkit=13)
135        self.assertEqual(self.compiles, 0)
136
137
138class UpdateInstalledEngine(unittest.TestCase):
139    def test_a_poisoned_stamp_makes_the_update_compile(self):
140        with tempfile.TemporaryDirectory() as d:
141            root = Path(d)
142            eng = root / "engine"
143            eng.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"}))
147            built = []
148            with mock.patch.object(setup, "ROOT", root), \
149                    mock.patch.object(setup, "source_hash", lambda parts: "newhash"), \
150                    mock.patch.object(setup, "source_version", lambda: "0.1.43"), \
151                    mock.patch.object(setup, "update_engine_gpu", lambda tk: dict(GPU)), \
152                    mock.patch.object(setup, "get_llama_cpp", lambda: Path("llama")), \
153                    mock.patch.object(setup, "build_engine", lambda *a, **k: built.append(a)), \
154                    mock.patch.object(setup, "say", lambda *a, **k: None):
155                setup.update_installed_engine("http://x", 13)
156                self.assertEqual(len(built), 1)
157                built.clear()
158                (eng / setup.EXE).write_bytes(binary("0.1.43"))
159                setup.update_installed_engine("http://x", 13)
160                self.assertEqual(built, [])
161
162
163if __name__ == "__main__":
164    unittest.main()