okami.mom/strata

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

Niko1221Merge f-027-gh-ctl: GitHub setup, Docker, docs and bench items for 0.2.7538acd098

main
4.9 KiB97 linesraw
1"""#3895: STRATA_SH_STREAM=1 (the shared expert on its own stream) together with --kv-resident failed the engine on its first request on one
2Windows RX 6900 XT ("verify: unspecified launch failure"). AMD runs without the fork by default. Calibrate does not measure the combination, the
3tuner never picks it, and a person who set it by hand gets one warning line at the start. Nothing is refused.
4"""
5from __future__ import annotations
6
7import sys
8import unittest
9from pathlib import Path
10from unittest import mock
11
12ROOT = Path(__file__).resolve().parents[2]
13sys.path.insert(0, str(ROOT / "tools"))
14sys.path.insert(0, str(ROOT))
15import calibrate as CAL  # noqa: E402
16from serve import server  # noqa: E402
17from strata_control import tune_knobs  # noqa: E402
18from tools.test_calibrate import BASE, FakeEngine  # noqa: E402
19
20
21class CalibrateSkipsTheCombination(unittest.TestCase):
22    def measure(self, platform, args):
23        starts, envs, said = [], [], []
24
25        def start(a, env=None):
26            envs.append(dict(env or {}))
27            return FakeEngine(a, lambda f, p, w: 50.0, 6, starts)
28
29        with mock.patch.object(CAL.sys, "platform", platform):
30            res = CAL.measure(args, [[1, 2, 3]] * 3, start, say=said.append, hip=True)
31        return res, envs, said
32
33    def test_windows_with_kv_streaming_does_not_measure_the_fork(self):
34        args = CAL.with_arg(BASE, "--kv-resident", "32768")
35        res, envs, said = self.measure("win32", args)
36        self.assertEqual(res["env"], {})
37        self.assertNotIn("sh_stream", res["report"])
38        self.assertEqual(res["report"]["sh_stream_skipped"], "windows with --kv-resident")
39        self.assertTrue(all(CAL.SH_STREAM not in e for e in envs))
40        self.assertTrue(any("#3895" in line and "STRATA_SH_STREAM=1" in line for line in said))
41
42    def test_linux_with_kv_streaming_and_windows_without_it_still_measure_it(self):
43        for platform, args in (("linux", CAL.with_arg(BASE, "--kv-resident", "32768")), ("win32", BASE)):
44            with self.subTest(platform=platform):
45                res, envs, _ = self.measure(platform, args)
46                self.assertEqual(set(res["report"]["sh_stream"]), {"0", "1"})
47                self.assertNotIn("sh_stream_skipped", res["report"])
48
49
50class TunerNeverPicksIt(unittest.TestCase):
51    def ctx(self, platform, backend="hip", args=("--kv-resident", "32768")):
52        context = tune_knobs.Context(backend=backend, arch="gfx1030", gpu_count=1, vram_mib=16384, ram_gib=64, model_gib=40.0, base_args=list(args))
53        return context, mock.patch.object(tune_knobs.sys, "platform", platform)
54
55    def test_windows_amd_with_kv_streaming(self):
56        ctx, patch = self.ctx("win32")
57        with patch:
58            problems = tune_knobs.check_setting({}, {"STRATA_SH_STREAM": "1"}, ctx)
59        self.assertEqual(len(problems), 1)
60        self.assertIn("#3895", problems[0])
61
62    def test_other_cases_stay_allowed(self):
63        for platform, backend, args in (("linux", "hip", ("--kv-resident", "32768")), ("win32", "hip", ()), ("win32", "cuda", ("--kv-resident", "32768"))):
64            ctx, patch = self.ctx(platform, backend, args)
65            with patch, self.subTest(platform=platform, backend=backend, args=args):
66                self.assertEqual(tune_knobs.check_setting({}, {"STRATA_SH_STREAM": "1"}, ctx), [])
67
68
69class StartWarning(unittest.TestCase):
70    CFG = {"backend": "hip", "args": ["--kv", "int8", "--kv-resident", "32768"]}
71
72    def test_a_hand_set_fork_with_kv_streaming_on_windows_warns_once_per_start(self):
73        with mock.patch.object(server.os, "name", "nt"):
74            note = server.sh_stream_warning({"STRATA_SH_STREAM": "1"}, self.CFG)
75        self.assertIn("#3895", note)
76        self.assertIn("WARNING", note)
77        self.assertIn("take STRATA_SH_STREAM out", note)
78
79    def test_no_warning_without_the_combination(self):
80        with mock.patch.object(server.os, "name", "nt"):
81            self.assertIsNone(server.sh_stream_warning({}, self.CFG))
82            self.assertIsNone(server.sh_stream_warning({"STRATA_SH_STREAM": "0"}, self.CFG))
83            self.assertIsNone(server.sh_stream_warning({"STRATA_SH_STREAM": "1"}, {"backend": "hip", "args": ["--kv", "int8"]}))
84        with mock.patch.object(server.os, "name", "posix"):
85            self.assertIsNone(server.sh_stream_warning({"STRATA_SH_STREAM": "1"}, self.CFG))
86
87    def test_child_env_prints_it_and_still_passes_the_variable(self):
88        cfg = {**self.CFG, "env": {"STRATA_SH_STREAM": "1"}}
89        with mock.patch.object(server.os, "name", "nt"), mock.patch("builtins.print") as shown, \
90                mock.patch.object(server, "hip_env_guard", return_value=[]), mock.patch.object(server, "hip_auto_devices", return_value=None):
91            env = server.child_env(cfg)
92        self.assertEqual(env["STRATA_SH_STREAM"], "1")                       # nothing is taken away
93        self.assertTrue(any("#3895" in str(call.args[0]) for call in shown.call_args_list if call.args))
94
95
96if __name__ == "__main__":
97    unittest.main()