okami.mom/strata

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

Niko1221Int3 merge f-0291-term-small (da8c7f4a4): Terminal fixes and tests54ef8615b

main
6.3 KiB117 linesraw
1"""A fixed expert cache beside a RAM budget on a Strix Halo (STRIX-2Q, 2026-11-01): UD-Q4_K_XL with --expert-cache 10000 or 5000 ended with
2exit code 1 after 41 s and no error text.  The estimate (strata_control/uma_pool.py) is the measured one of the 128 GB box; setup lowers its
3own budget with the person's cache and only warns about a budget the person set; a launch whose engine stopped says what to change.
4No GPU, no network.
5
6    python -m unittest tools.test_setup_uma_pool
7"""
8from __future__ import annotations
9
10import io
11import json
12import sys
13import tempfile
14import unittest
15from contextlib import redirect_stdout
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
22from strata_control import uma_pool  # noqa: E402
23
24Q4_GB = setup.MODELS["UD-Q4_K_XL"]["arena_gb"]
25STRIX = {"uma": True, "vram_gb": 96.0, "dedicated_gb": 64.0, "shared_gb": 32.0, "name": "Radeon 8060S"}   # the measured box: 92,058 MiB of pool
26
27
28class Estimate(unittest.TestCase):
29    def test_it_reproduces_the_measured_free_memory(self):
30        # MiB free at the first window (STRIX-2Q-RESULTS): cache 14007 + budget 12 -> 8.6 GiB, 12735 + 12 -> 12.4, 12735 + 10 -> 16.5
31        pool = 92058 / 1024
32        for cache, budget, free in ((14007, 12, 8.6), (12735, 12, 12.4), (12735, 10, 16.5), (15283, 12, 4.8)):
33            self.assertAlmostEqual(pool - uma_pool.demand_gib(cache, budget, Q4_GB), free, delta=0.7, msg=(cache, budget))
34
35    def test_the_failing_settings_are_too_much_and_the_measured_ones_are_not(self):
36        for cache in (10000, 5000):
37            self.assertTrue(uma_pool.too_much(cache, 40, Q4_GB, 96.0), cache)         # exit code 1 after 41 s
38        for cache, budget in ((10000, 12), (12735, 12), (14007, 12), (16223, 12), (12735, 10)):
39            self.assertFalse(uma_pool.too_much(cache, budget, Q4_GB, 96.0), (cache, budget))   # started, no fault in 10+ starts each
40
41    def test_the_budget_that_fits_is_below_the_failing_one_and_above_the_floor(self):
42        for cache in (10000, 5000, 2000):
43            fit = uma_pool.safe_budget_gib(cache, Q4_GB, 96.0)
44            self.assertLess(fit, 40)
45            self.assertGreaterEqual(fit, uma_pool.MIN_BUDGET_GIB)
46            self.assertFalse(uma_pool.too_much(cache, fit, Q4_GB, 96.0))
47        self.assertEqual(uma_pool.safe_budget_gib(24000, Q4_GB, 40.0), uma_pool.MIN_BUDGET_GIB)    # a tiny pool: the floor, never zero or negative
48
49    def test_only_a_number_beside_a_budget_counts(self):
50        self.assertEqual(uma_pool.fixed_cache_and_budget(["--expert-cache", "10000", "--resident-budget-gib", "40"]), (10000, 40.0))
51        self.assertIsNone(uma_pool.fixed_cache_and_budget(["--expert-cache", "auto", "--resident-budget-gib", "40"]))
52        self.assertIsNone(uma_pool.fixed_cache_and_budget(["--expert-cache", "10000"]))
53        self.assertIsNone(uma_pool.stop_advice(["--expert-cache", "auto", "--resident-budget-gib", "40"]))
54
55
56def run_guard(args, user=None, gpu=None):
57    cfg = {"args": list(args)}
58    if user is not None:
59        cfg[setup.user_args.USER_KEY] = user
60    out = io.StringIO()
61    with redirect_stdout(out):
62        setup.uma_cache_guard(cfg, "UD-Q4_K_XL", gpu or STRIX)
63    return cfg, out.getvalue()
64
65
66class SetupGuard(unittest.TestCase):
67    def test_a_smaller_cache_lowers_setups_own_budget_and_says_so(self):
68        cfg, text = run_guard(["--expert-cache", "10000", "--resident-budget-gib", "40", "--vision"])
69        budget = float(cfg["args"][cfg["args"].index("--resident-budget-gib") + 1])
70        self.assertLess(budget, 40)
71        self.assertEqual(cfg["args"][cfg["args"].index("--expert-cache") + 1], "10000", "the person's cache is kept")
72        self.assertEqual(cfg["args"][-1], "--vision", "nothing else moves")
73        self.assertIn("10000 slots", text)
74        self.assertIn("instead of 40", text)
75        self.assertIn("--expert-cache auto", text)
76
77    def test_a_budget_the_person_set_is_only_warned_about(self):
78        cfg, text = run_guard(["--expert-cache", "10000", "--resident-budget-gib", "40"], user=["--expert-cache", "10000", "--resident-budget-gib", "40"])
79        self.assertEqual(cfg["args"][cfg["args"].index("--resident-budget-gib") + 1], "40")
80        self.assertIn("kept as you chose", text)
81
82    def test_what_already_fits_and_other_pcs_are_left_alone(self):
83        for args, gpu in ((["--expert-cache", "10000", "--resident-budget-gib", "12"], None),
84                          (["--expert-cache", "auto", "--resident-budget-gib", "40"], None),
85                          (["--expert-cache", "10000", "--resident-budget-gib", "40"], {**STRIX, "uma": False})):
86            cfg, text = run_guard(args, gpu=gpu)
87            self.assertEqual(cfg["args"], args)
88            self.assertEqual(text, "")
89
90
91class LaunchExplains(unittest.TestCase):
92    def config(self, d, args, user=None):
93        log = Path(d) / "engine.log"
94        log.write_text("prefill: copy stream\n", encoding="utf-8")
95        cfg = Path(d) / "run.json"
96        cfg.write_text(json.dumps({"log": str(log), "args": args, **({"user_args": user} if user else {})}), encoding="utf-8")
97        return cfg
98
99    def test_a_silent_exit_code_1_gets_a_plain_next_step_on_a_shared_memory_pc(self):
100        from strata_cli.launch import runner as R
101        result = {"state": "failed", "error": "server_exited", "engine_exit_code": 1}
102        with tempfile.TemporaryDirectory() as d:
103            cfg = self.config(d, ["--expert-cache", "4000", "--resident-budget-gib", "40"], user=["--expert-cache", "10000"])
104            text, nxt = R.explain_engine_exit(result, cfg, shared_memory=True)
105            self.assertNotIn("copy stream", text, "a progress line is not a reason")
106            self.assertIn("exit code 1", text)
107            self.assertIn("10000 slots", nxt, "the person's own value is the one that counts")
108            self.assertIn("--resident-budget-gib", nxt)
109            self.assertIn("--expert-cache auto", nxt)
110            plain_text, plain_next = R.explain_engine_exit(result, cfg, shared_memory=False)    # another PC: the old answer
111            self.assertIn("report the log", plain_next)
112            auto = self.config(d, ["--expert-cache", "auto", "--resident-budget-gib", "40"])
113            self.assertIn("report the log", R.explain_engine_exit(result, auto, shared_memory=True)[1])
114
115
116if __name__ == "__main__":
117    unittest.main()