okami.mom/strata

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

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
12.1 KiB211 linesraw
1"""0.2.13 user-facing gaps of the tuned DFlash2 drafter (release 0.2.12 checks, RESULTS-DF2-DL): the plan names the drafter Auto will really fetch and its size, the
2Terminal's plan sees a stock pack from 0.2.11 as "the tuned one is still to come", and a load nobody asked the drafter for can fetch with the 12 hour miss rule and no
3words when the feed is not there."""
4
5from __future__ import annotations
6
7import json
8import tempfile
9import unittest
10from pathlib import Path
11from unittest import mock
12
13from strata_cli.launch import settings as S
14from strata_control import drafting, drafting_fetch
15from tests.terminal.launch_support import HW_12, Home, catalog, run_cli
16
17STRIX = ("hip", ["gfx1151"])
18SPARK = ("cuda", ["sm_121"])
19ARC = ("sycl", ["arc_a"])
20Q4 = "qwen3.8-flash-next-unsloth-ud-q4_k_xl"
21
22
23def hardware(pair):
24    return mock.patch.object(drafting, "detect_hardware", return_value=pair)
25
26
27def serving():
28    return mock.patch.object(drafting, "ENGINE_SERVES_DFLASH2", True)
29
30
31class PlanNamesTheRealDrafter(unittest.TestCase):
32    def setUp(self):
33        self.tmp = tempfile.TemporaryDirectory()
34        self.addCleanup(self.tmp.cleanup)
35        self.data = Path(self.tmp.name) / "data"
36        (self.data / "dflash2").mkdir(parents=True)
37
38    def plan(self, *argv):
39        code, out, err = run_cli(["drafting", "plan", "--data-dir", str(self.data), "--output", "json", *argv])
40        self.assertEqual(code, 0, err)
41        return json.loads(out)
42
43    def test_with_no_model_the_plan_is_the_old_generic_one(self):
44        with serving(), hardware(STRIX):
45            got = self.plan("--family", "unsloth", "--mode", "auto")
46        self.assertEqual((got["gb"], got["tuned"]), (1.2, False))
47
48    def test_a_model_not_installed_yet_gets_the_tuned_size(self):
49        with serving(), hardware(STRIX):
50            for mode in ("auto", "dflash2"):
51                got = self.plan("--family", "unsloth", "--model", "UD-Q4_K_XL", "--mode", mode)
52                self.assertEqual((got["needed"], got["tuned_needed"], got["tuned"], got["gb"], got["drafter_gb"]), (True, True, True, 0.34, 0.34), mode)
53                self.assertEqual(got["file"], drafting.TUNED_NAME)
54                self.assertIn("0.34 GB", got["text"])
55                self.assertNotIn("1.2 GB", got["text"])
56
57    def test_the_spark_and_arc_and_a_full_model_name(self):
58        with serving(), hardware(SPARK):
59            self.assertEqual(self.plan("--family", "qwen", "--model", "IQ3_S", "--mode", "auto")["gb"], 0.34)
60        with serving(), hardware(ARC):
61            got = self.plan("--family", "qwen", "--model", "IQ3_XXS", "--mode", "auto")
62            self.assertEqual((got["gb"], got["file"]), (0.52, drafting.TUNED_BF16_NAME))
63            self.assertEqual(self.plan("--model", "qwen3.8-flash-next-iq3_xxs", "--mode", "auto")["gb"], 0.52, "a run config's own name is taken as it is")
64
65    def test_a_set_the_tuned_pack_is_not_for_keeps_the_stock_size(self):
66        with serving(), hardware(STRIX):
67            got = self.plan("--family", "qwen", "--model", "Q2_0", "--mode", "auto")
68        self.assertEqual((got["tuned"], got["gb"], got["needed"]), (False, 1.2, True))
69
70    def test_mtp_says_nothing_is_needed_whatever_the_model(self):
71        with serving(), hardware(STRIX):
72            got = self.plan("--family", "unsloth", "--model", "UD-Q4_K_XL", "--mode", "mtp")
73            self.assertEqual((got["needed"], got["gb"], got["text"]), (False, 0.0, "Nothing to download."))
74            _code, out, _err = run_cli(["drafting", "plan", "--data-dir", str(self.data), "--family", "unsloth", "--model", "UD-Q4_K_XL", "--mode", "mtp"])
75            self.assertEqual(out.strip(), "Nothing to download.")
76
77    def test_a_stock_pack_from_0_2_11_does_not_hide_the_tuned_download(self):
78        with serving(), hardware(STRIX), mock.patch.object(drafting, "pack_ready", return_value=True):
79            got = self.plan("--family", "unsloth", "--model", "UD-Q4_K_XL", "--mode", "auto")
80        self.assertEqual((got["tuned_needed"], got["needed"], got["gb"]), (True, True, 0.34))
81
82
83class TheLaunchPlan(unittest.TestCase):
84    """`strata launch --plan` for a model that is not installed yet: the number is the tuned drafter's."""
85
86    def setUp(self):
87        from tests.terminal.launch_support import not_intel
88        not_intel(self)
89
90    def test_the_download_list_and_the_plan_line_use_the_tuned_pack(self):
91        from strata_cli.launch.runner import Runner
92        with Home():
93            with serving(), hardware(STRIX):
94                runner = Runner(S.LaunchSettings(family="unsloth", model="UD-Q4_K_XL", context=32768, vision="off"), catalog=catalog(HW_12))
95                part = next(p for p in runner.download_plan()["parts"] if p["kind"] == "dflash2")
96                self.assertEqual(part["gb"], 0.34)
97                self.assertIn("the DFlash2 drafter (0.34 GB)", runner.download_plan()["text"])
98                self.assertEqual(runner.drafting_plan_line(), "Drafting: DFlash2 (its drafter is downloaded first)")
99                stock = Runner(S.LaunchSettings(family="qwen", model="Q2_0", context=32768, vision="off"), catalog=catalog(HW_12))
100                self.assertEqual(next(p for p in stock.download_plan()["parts"] if p["kind"] == "dflash2")["gb"], 1.2)
101                mtp = Runner(S.LaunchSettings(family="unsloth", model="UD-Q4_K_XL", context=32768, vision="off", drafting="mtp"), catalog=catalog(HW_12))
102                self.assertNotIn("dflash2", [p["kind"] for p in mtp.download_plan()["parts"]])
103
104    def test_a_stock_pack_already_here_still_lists_the_tuned_one(self):
105        from strata_cli.launch.runner import Runner
106        with Home():
107            with serving(), hardware(STRIX), mock.patch.object(drafting, "pack_ready", return_value=True):
108                runner = Runner(S.LaunchSettings(family="unsloth", model="UD-Q4_K_XL", context=32768, vision="off"), catalog=catalog(HW_12))
109                self.assertEqual(next(p for p in runner.download_plan()["parts"] if p["kind"] == "dflash2")["gb"], 0.34)
110
111
112class AnAutomaticFetch(unittest.TestCase):
113    """`strata drafting fetch --if-needed --throttle`: what the app's plain load, restart and tuning run call."""
114
115    def setUp(self):
116        self.tmp = tempfile.TemporaryDirectory()
117        self.addCleanup(self.tmp.cleanup)
118        self.data = Path(self.tmp.name) / "data"
119        (self.data / "dflash2").mkdir(parents=True)
120        self.cfg = Path(self.tmp.name) / "run.json"
121        self.cfg.write_text(json.dumps({"model_name": Q4, "args": []}), encoding="utf-8")
122
123    def fetch(self, *extra):
124        return run_cli(["drafting", "fetch", "--config", str(self.cfg), "--data-dir", str(self.data), "--if-needed", "--output", "jsonl", *extra])
125
126    def test_the_feed_not_answering_is_silent_with_throttle_and_an_error_without(self):
127        with serving(), hardware(STRIX), mock.patch.object(drafting_fetch, "ensure_for", return_value=None) as ensure:
128            code, out, _ = self.fetch("--throttle")
129            self.assertEqual(code, 0)
130            last = json.loads(out.strip().splitlines()[-1])
131            self.assertEqual((last["ok"], last["skipped"], last["pack"]), (True, True, None))
132            self.assertTrue(ensure.call_args.kwargs["throttle"])
133            code, out, _ = self.fetch()
134            self.assertEqual(code, 5, "the Models screen's own Load keeps saying so")
135            self.assertFalse(ensure.call_args.kwargs["throttle"])
136
137    def test_a_miss_less_than_twelve_hours_old_skips_the_feed(self):
138        (self.data / "dflash2" / drafting_fetch.MISS_MARK).write_text("x")
139        # After the skipped tuned lookup the fetch falls back to the STOCK drafter (ensure): on a box with internet that is a real 1.2 GB download from
140        # Hugging Face, or a hash and pack of the person's own Hugging Face cache (17 minutes and killed on the owner PC, 11-10). This test is about the
141        # feed being skipped, so the stock path is kept offline here: no network, no Hugging Face cache.
142        with serving(), hardware(STRIX), mock.patch.object(drafting_fetch, "tuned_from_feed") as feed, mock.patch.object(drafting_fetch.acquisition, "require"), \
143                mock.patch.object(drafting_fetch, "online", return_value=False) as reach, mock.patch.object(drafting_fetch, "_hub_snapshots", return_value=[]):
144            code, _out, _ = self.fetch("--throttle")
145        reach.assert_called()                               # (the stock path was reached and stayed offline: nothing was downloaded)
146        self.assertEqual(code, 0)
147        feed.assert_not_called()
148
149    def test_a_real_fault_is_still_reported(self):
150        fault = drafting_fetch.DraftingError("The tuned DFlash2 drafter did not pass the safety check, so it was deleted.")
151        with serving(), hardware(STRIX), mock.patch.object(drafting_fetch, "ensure_for", side_effect=fault):
152            code, out, _ = self.fetch("--throttle")
153        self.assertEqual(code, 5)
154        self.assertIn("safety check", json.loads(out.strip().splitlines()[-1])["error"]["message"])
155
156    def test_mtp_chosen_downloads_nothing(self):
157        with serving(), hardware(STRIX), mock.patch.object(drafting_fetch, "ensure_for") as ensure:
158            code, _out, _ = run_cli(["drafting", "fetch", "--config", str(self.cfg), "--data-dir", str(self.data), "--mode", "mtp", "--if-needed", "--throttle", "--output", "jsonl"])
159        self.assertEqual(code, 0)
160        ensure.assert_not_called()
161
162
163class ABareFetch(AnAutomaticFetch):
164    """`strata drafting fetch` with nothing named: the one installed model's run config decides (coordinator 11-07)."""
165
166    def installed(self, *names):
167        rows = [{"path": str(self.cfg), "model_name": n} for n in names]
168        return mock.patch("strata_cli.launch.catalog.Catalog.installed_configs", return_value=rows)
169
170    def bare(self):
171        return run_cli(["drafting", "fetch", "--data-dir", str(self.data), "--output", "jsonl"])
172
173    def test_one_installed_model_the_tuned_pack_is_fetched(self):
174        with serving(), hardware(STRIX), self.installed(Q4), mock.patch.object(drafting_fetch, "ensure_for", return_value=self.data / "x") as ensure:
175            code, _out, _ = self.bare()
176        self.assertEqual(code, 0)
177        self.assertEqual(ensure.call_args.kwargs["model_name"], Q4)
178        self.assertEqual(ensure.call_args.kwargs["backend"], "hip")
179
180    def test_none_installed_keeps_the_generic_fetch(self):
181        with serving(), hardware(STRIX), self.installed(), mock.patch.object(drafting_fetch, "ensure_for", return_value=self.data / "x") as ensure:
182            self.bare()
183        self.assertIsNone(ensure.call_args.kwargs["model_name"])
184
185    def test_several_models_ask_which_one_and_download_nothing(self):
186        """Owner-approved wording (11-07)."""
187        with serving(), hardware(STRIX), self.installed(Q4, "qwen3.8-flash-next-iq3_s"), mock.patch.object(drafting_fetch, "ensure_for") as ensure:
188            code, out, _ = self.bare()
189            code_text, _o, err = run_cli(["drafting", "fetch", "--data-dir", str(self.data)])
190        self.assertEqual(code, 2)
191        ensure.assert_not_called()
192        said = json.loads(out.strip().splitlines()[-1])
193        self.assertFalse(said["ok"])
194        self.assertEqual(said["error"]["message"], "Several models are set up here. Say which one: strata drafting fetch --model <name>, or --config <file>.")
195        self.assertEqual(code_text, 2)
196        self.assertIn("Several models are set up here. Say which one: strata drafting fetch --model <name>, or --config <file>.", err)
197
198    def test_naming_the_model_still_fetches_with_several_set_up(self):
199        with serving(), hardware(STRIX), self.installed(Q4, "qwen3.8-flash-next-iq3_s"), mock.patch.object(drafting_fetch, "ensure_for", return_value=self.data / "x") as ensure:
200            code, _out, _ = run_cli(["drafting", "fetch", "--config", str(self.cfg), "--data-dir", str(self.data), "--output", "jsonl"])
201        self.assertEqual(code, 0)
202        ensure.assert_called_once()
203
204    def test_a_model_dflash2_is_not_for_is_not_picked(self):
205        with serving(), hardware(STRIX), self.installed("qwen3.8-flash-next-coder-iq1_m"), mock.patch.object(drafting_fetch, "ensure_for", return_value=self.data / "x") as ensure:
206            self.bare()
207        self.assertIsNone(ensure.call_args.kwargs["model_name"])
208
209
210if __name__ == "__main__":
211    unittest.main()