okami.mom/strata

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

Niko1221Merge f-0213-shape (61d883315): a new-shape Qwen4 engine ships as its own component strata-q4-<shape> beside the Strata engine, picked by the model (owner 11-08: separate components; wording approved 'A ok, 1 yes, 2 later, 3 yes') - feed member shape_engines, setup/launch/update/Mac paths, strata --geometry checked against the signed manifest at install, older Controls never offer the model; l620 e2e (Flash-Next engine hash unchanged), Windows long-path install, Mac m1002 rund5d5b0837

main
13.5 KiB219 linesraw
1"""setup.py and a feed model of its own shape (strata_control/shape_engine.py, strata_control/feed_models.py).
2
3    python -m pytest tools/test_setup_shape.py
4
5A Qwen4 model whose geometry is not Flash-Next's runs on its own engine component. The model list puts it in the same tables as every feed model, with the shape; setup's engine
6step fetches that engine (not the Strata engine's) and the run config names it ("component:strata-q4-lite"); the Terminal's launch checks it in its first step. Every other model
7is exactly what it was. The engine fetch, downloads and processes are mocked; nothing is fetched from the network.
8"""
9from __future__ import annotations
10
11import argparse
12import json
13import sys
14import unittest
15from pathlib import Path
16from unittest import mock
17
18ROOT = Path(__file__).resolve().parents[1]
19sys.path.insert(0, str(ROOT))
20sys.path.insert(0, str(ROOT / "tools"))
21
22from strata_control import engine_component as ec, feed_models, shape_engine  # noqa: E402
23import test_setup_feed_models as base  # noqa: E402
24
25
26def shaped(**kw):
27    kw = {"id": "q4lite-test", "family": "qwen4exp_q4_lite", "drafter": "none", "pictures": False, "mmproj": None, "min_engine": "", **kw}
28    return base.flash_next(**kw)
29
30
31def clean(entry: dict) -> dict:
32    entry.pop("mmproj", None)
33    return entry
34
35
36class Registered(base.Base):
37    def setUp(self):
38        super().setUp()
39        self.result = feed_models.apply(self.setup, base.parsed(clean(shaped())), {"qwen4exp"})        # the Strata engine runs qwen4exp only: not what decides
40        self.fam = self.setup.FAMILIES["q4lite-test"]
41
42    def test_it_is_listed_and_installable_though_the_strata_engine_does_not_list_its_family(self):
43        self.assertEqual((self.result.registered, self.result.unavailable), (["q4lite-test"], {}))
44        self.assertEqual(self.fam["shape"], "q4_lite")
45        self.assertTrue(self.fam["feed"])
46        self.assertNotIn("q4lite-test", self.setup.FEED_UNAVAILABLE)
47
48    def test_its_min_engine_is_not_compared_with_the_strata_engine(self):
49        """The app marks a size "needs a newer engine" when the Strata engine is older than the size's `engine`; a shape model runs on its own engine."""
50        feed_models.apply(self.setup, base.parsed(clean(shaped(id="q4lite-min", min_engine="0.1.59"))), None)
51        sizes = [m for m in self.setup.MODELS.values() if "q4lite-min" in m.get("families", ())]
52        self.assertTrue(sizes)
53        self.assertTrue(all("engine" not in m for m in sizes))
54        feed_models.apply(self.setup, base.parsed(base.flash_next(id="plain-min", min_engine="0.1.59")), None)
55        self.assertTrue(all(m.get("engine") == (0, 1, 59) for m in self.setup.MODELS.values() if "plain-min" in m.get("families", ())))
56
57    def test_a_plain_qwen4_model_has_no_shape(self):
58        feed_models.apply(self.setup, base.parsed(base.flash_next(id="plain-one", title="Plain One")), {"qwen4exp"})
59        self.assertNotIn("shape", self.setup.FAMILIES["plain-one"])
60
61    def test_it_is_offered_on_a_mac_like_the_two_file_sizes(self):
62        from strata_control import mac_fit
63        self.assertIn("q4lite-test", mac_fit.FEED_FAMILIES)
64        rows = {k: v for k, v in mac_fit.MAC_MODELS.items() if v.get("family") == "q4lite-test"}
65        self.assertEqual(len(rows), 1)
66        (key, row), = rows.items()
67        self.assertTrue(row["no_mtp"], "its entry says drafter none")
68        self.assertEqual(mac_fit.app_sizes("q4lite-test"), [key])
69
70    def test_the_mac_arguments_for_a_one_file_model_without_a_draft_layer(self):
71        from strata_control import mac_fit
72        (key,) = [k for k, v in mac_fit.MAC_MODELS.items() if v.get("family") == "q4lite-test"]
73        shard = Path("model.gguf")
74        args = mac_fit.engine_args(Path("pack"), [shard], Path("rt"), 8192, False, 128.0, "profile.bin", key)
75        self.assertEqual(args[args.index("--ple-gguf") + 1], "model.gguf", "one file: its own PLE table")
76        self.assertNotIn("--mtp", args)
77        plain = mac_fit.engine_args(Path("pack"), [Path("a-1.gguf"), Path("a-2.gguf")], Path("rt"), 8192, False, 128.0, "profile.bin", "IQ3_S")
78        self.assertEqual(plain[plain.index("--ple-gguf") + 1], "a-2.gguf")
79        self.assertIn("--mtp", plain, "a built-in model is exactly what it was")
80
81    def test_the_same_list_without_an_engine_to_ask_gives_the_same_registration(self):
82        other = base.fresh_setup("setup_other")
83        feed_models.apply(other, base.parsed(clean(shaped())), None)
84        self.assertEqual(other.FAMILIES["q4lite-test"]["shape"], "q4_lite")
85
86    def test_a_family_older_controls_do_not_know_is_unavailable_for_them(self):
87        """What the Control before this does with the same entry: its install table lacks the family, so the model is listed as unavailable and never offered."""
88        from strata_control import feed as feedmod
89        self.assertNotIn("qwen4exp_q4_lite", ("qwen4exp", "qwen35moe"))
90        self.assertIsNone(feedmod.FeedModel.shape.fget(base.parsed(base.flash_next())[0]))
91
92
93class EngineStep(base.Base):
94    def setUp(self):
95        super().setUp()
96        feed_models.apply(self.setup, base.parsed(clean(shaped())), None)
97        self.addCleanup(self.setup.SHAPE_ENGINE.clear)
98
99    def test_the_run_config_names_the_shape_engine_only_for_a_model_of_that_shape(self):
100        self.assertEqual(self.setup.engine_exe(Path("engine")), "component:strata-engine" if self.setup.CONTROL else str(Path("engine") / self.setup.EXE))
101        self.setup.SHAPE_ENGINE["shape"] = "q4_lite"
102        self.assertEqual(self.setup.engine_exe(Path("engine")), "component:strata-q4-lite")
103        self.setup.SHAPE_ENGINE.clear()
104        self.assertNotEqual(self.setup.engine_exe(Path("engine")), "component:strata-q4-lite")
105
106    def test_engine_step_fetches_the_shape_engine_and_not_the_strata_engine(self):
107        self.setup.SHAPE_ENGINE["shape"] = "q4_lite"
108        folder = self.root / "components" / "shapes" / "q4_lite" / "engine" / "0.1.59-linux-x86_64-cuda"
109        folder.mkdir(parents=True)
110        found = mock.Mock(root=str(folder), version="0.1.59", backend="cuda", platform="linux-x86_64")
111        with mock.patch.object(shape_engine, "ensure", return_value=folder / "strata") as ensure, \
112                mock.patch.object(shape_engine, "find", return_value=found), \
113                mock.patch.object(self.setup, "llama_cpp_if_here", return_value=Path("llama")), \
114                mock.patch.object(self.setup, "get_llama_cpp", return_value=Path("llama")), \
115                mock.patch.object(self.setup, "control_engine_step", side_effect=AssertionError("the Strata engine step must not run")), \
116                mock.patch.object(ec, "resolve", side_effect=AssertionError("the Strata engine must not be resolved")):
117            llama, eng, meta, lib_dirs, gpu, vision = self.setup.engine_step(argparse.Namespace(feed="/some/feed"), {"index": 0}, False, False, 13, "none")
118        self.assertEqual(ensure.call_args.args[:1], ("q4_lite",))
119        self.assertEqual(ensure.call_args.args[1], "/some/feed")
120        self.assertEqual((eng, meta["version"], meta["shape"], lib_dirs, vision), (folder, "0.1.59", "q4_lite", [], "none"))
121
122    def test_the_lines_and_the_run_config_name_the_models_title(self):
123        """A person reads the model's title, not the geometry's name; the run config keeps the title for the refusal at a start (strata_control/shape_engine.py USE_TITLE)."""
124        self.setup.SHAPE_ENGINE.update(shape="q4_lite", title="Q4 Lite Shape")
125        folder = self.root / "components" / "shapes" / "q4_lite" / "engine" / "0.1.59-linux-x86_64-cuda"
126        folder.mkdir(parents=True)
127        found = mock.Mock(root=str(folder), version="0.1.59", backend="cuda", platform="linux-x86_64")
128        said = []
129        with mock.patch.object(shape_engine, "ensure", return_value=folder / "strata") as ensure, mock.patch.object(shape_engine, "find", return_value=found),                 mock.patch.object(self.setup, "llama_cpp_if_here", return_value=Path("llama")), mock.patch.object(self.setup, "get_llama_cpp", return_value=Path("llama")),                 mock.patch.object(self.setup, "ok", side_effect=said.append):
130            self.setup.engine_step(argparse.Namespace(feed=None), {"index": 0}, False, False, 13, "none")
131        self.assertEqual(ensure.call_args.kwargs["title"], "Q4 Lite Shape")
132        self.assertTrue(any(line.startswith("engine for Q4 Lite Shape: 0.1.59") for line in said), said)
133        self.assertFalse(any("q4_lite" in line.split(" in ")[0] for line in said), said)
134        cfg = {"exe": "component:strata-q4-lite", "args": [], "model_name": "x"}
135        target = self.root / "strata-q4lite.json"
136        self.setup.write_setup_config(target, cfg)
137        self.assertEqual(json.loads(target.read_text(encoding="utf-8"))["shape_title"], "Q4 Lite Shape")
138        plain = {"exe": "component:strata-engine", "args": [], "model_name": "y"}      # another model's config is exactly what it was
139        self.setup.write_setup_config(self.root / "strata-other.json", plain)
140        self.assertNotIn("shape_title", json.loads((self.root / "strata-other.json").read_text(encoding="utf-8")))
141
142    def test_without_a_shape_the_strata_engine_step_runs_as_before(self):
143        self.assertEqual(self.setup.SHAPE_ENGINE, {})
144        with mock.patch.object(self.setup, "control_engine_step", return_value=("L", "E", {}, [], {}, "none")) as step, mock.patch.object(self.setup, "CONTROL", True):
145            self.assertEqual(self.setup.engine_step(argparse.Namespace(), {}, False, False, 13, "none")[1], "E")
146        step.assert_called_once()
147
148    def test_no_engine_for_the_shape_stops_before_the_model_is_downloaded(self):
149        self.setup.SHAPE_ENGINE["shape"] = "q4_lite"
150        with mock.patch.object(shape_engine, "ensure", return_value=None), \
151                mock.patch.object(self.setup, "llama_cpp_if_here", return_value=Path("llama")), mock.patch.object(self.setup, "get_llama_cpp", return_value=Path("llama")), \
152                mock.patch.dict("os.environ", {"STRATA_ALLOW_NO_ENGINE": ""}):
153            with self.assertRaises(SystemExit):
154                self.setup.engine_step(argparse.Namespace(feed=None), {"index": 0}, False, False, 13, "none")
155
156    def test_the_person_may_go_on_without_the_engine_and_set_it_up_later(self):
157        self.setup.SHAPE_ENGINE["shape"] = "q4_lite"
158        with mock.patch.object(shape_engine, "ensure", return_value=None), \
159                mock.patch.object(self.setup, "llama_cpp_if_here", return_value=Path("llama")), mock.patch.object(self.setup, "get_llama_cpp", return_value=Path("llama")), \
160                mock.patch.dict("os.environ", {"STRATA_ALLOW_NO_ENGINE": "1"}):
161            out = self.setup.engine_step(argparse.Namespace(feed=None), {"index": 0}, False, False, 13, "none")
162        self.assertEqual(out[2]["component"], "missing")
163
164
165class Launch(unittest.TestCase):
166    """The Terminal's first step reads the shape from setup's table and checks that engine, not the Strata engine's."""
167
168    def runner(self, families):
169        from strata_cli.launch import runner as R
170        fake = mock.Mock()
171        fake.s.family = "q4lite-test"
172        fake.catalog.strata.setup_module.return_value = mock.Mock(FAMILIES=families)
173        fake.multi_file.return_value = False
174        return R, fake
175
176    def test_model_shape_reads_the_family_entry(self):
177        R, fake = self.runner({"q4lite-test": {"shape": "q4_lite"}})
178        self.assertEqual(R.Runner.model_shape(fake), "q4_lite")
179        R, fake = self.runner({"q4lite-test": {}})
180        self.assertIsNone(R.Runner.model_shape(fake))
181        R, fake = self.runner({})
182        self.assertIsNone(R.Runner.model_shape(fake))
183        fake.catalog.strata.setup_module.side_effect = RuntimeError("no table")
184        self.assertIsNone(R.Runner.model_shape(fake))
185
186    def test_step_one_checks_the_shape_engine_and_never_the_strata_engine(self):
187        R, fake = self.runner({"q4lite-test": {"shape": "q4_lite"}})
188        fake.model_shape.return_value = "q4_lite"
189        with mock.patch.object(ec, "status", side_effect=AssertionError("the Strata engine is not looked at")):
190            R.Runner.ensure_engine(fake)
191        fake.ensure_shape_engine.assert_called_once_with("q4_lite")
192
193    def test_a_missing_engine_is_the_launchs_no_engine_question(self):
194        R, fake = self.runner({})
195        fake.components_dir = None
196        fake.feed = "/feed"
197        with mock.patch.object(shape_engine, "ensure", side_effect=lambda shape, source, **kw: (kw["emit"]("There is no engine for this model (q4_lite)."), None)[1]):
198            R.Runner.ensure_shape_engine(fake, "q4_lite")
199        fake.no_engine.assert_called_once_with("There is no engine for this model (q4_lite).")
200
201    def test_the_launch_lines_name_the_title(self):
202        R, fake = self.runner({"q4lite-test": {"shape": "q4_lite", "title": "Q4 Lite Shape"}})
203        self.assertEqual(R.Runner.model_title(fake), "Q4 Lite Shape")
204        fake.model_title.return_value = "Q4 Lite Shape"
205        fake.components_dir = None
206        fake.feed = "/feed"
207        events = []
208        fake.emit.side_effect = lambda stage, text, *a, **k: events.append(text)
209        found = mock.Mock(version="0.1.59", backend="cuda")
210        with mock.patch.object(shape_engine, "ensure", return_value=Path("/x/strata")) as ensure, mock.patch.object(shape_engine, "find", return_value=found):
211            R.Runner.ensure_shape_engine(fake, "q4_lite")
212        self.assertEqual(ensure.call_args.kwargs["title"], "Q4 Lite Shape")
213        self.assertIn("Checking the engine for Q4 Lite Shape…", events)
214        self.assertIn("Engine for Q4 Lite Shape 0.1.59 (cuda) is installed.", events)
215        self.assertFalse([e for e in events if "q4_lite" in e], events)
216
217
218if __name__ == "__main__":
219    unittest.main()