okami.mom/strata

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

Niko1221Merge branch 'f-021-iD' into f-021-int83027df58

main
6.5 KiB126 linesraw
1"""#642: setup recommends the expert pool's worker count on a hybrid CPU (the P-cores but the host loop's one, plus
2half of the E-cores) and writes nothing new anywhere else; a calibration's measured count wins over the rule.
3
4    python -m unittest tools.test_setup_hybrid
5"""
6from __future__ import annotations
7
8import sys
9import tempfile
10import unittest
11from pathlib import Path
12from unittest import mock
13
14ROOT = Path(__file__).resolve().parents[1]
15sys.path.insert(0, str(ROOT))
16sys.path.insert(0, str(ROOT / "tools"))
17import setup  # noqa: E402
18from test_setup_golden import PROFILES, install  # noqa: E402
19
20
21def fake_sys(root: Path, caps: list, pmu=None, smt=False) -> None:
22    """A /sys tree: cpu N's capacity (and, with smt, a sibling CPU per core), and the hybrid PMU lists when given."""
23    cpu_dir = root / "devices" / "system" / "cpu"
24    n = 0
25    for core, cap in enumerate(caps):
26        for _ in range(2 if smt and cap >= 1000 else 1):
27            d = cpu_dir / f"cpu{n}"
28            (d / "topology").mkdir(parents=True)
29            (d / "topology" / "physical_package_id").write_text("0")
30            (d / "topology" / "core_id").write_text(str(core))
31            (d / "cpu_capacity").write_text(str(cap))
32            n += 1
33    if pmu:
34        for name, lst in zip(("cpu_core", "cpu_atom", "cpu_lowpower"), pmu):
35            (root / "devices" / name).mkdir(parents=True)
36            (root / "devices" / name / "cpus").write_text(lst + "\n")
37
38
39class LinuxCores(unittest.TestCase):
40    """#798: Turbo Boost Max 3.0's "favored" P-cores have a slightly higher cpu_capacity."""
41
42    def classes(self, caps, **kw):
43        with tempfile.TemporaryDirectory() as d:
44            fake_sys(Path(d), caps, **kw)
45            cl = setup.linux_core_classes(d)
46        if not cl or max(cl) == min(cl):
47            return None
48        p = sum(1 for c in cl if c == max(cl))
49        return p, len(cl) - p
50
51    def test_favored_cores_do_not_hide_the_p_cores(self):
52        caps = [1024, 1012, 1024, 1012, 1012, 1012, 1012, 1012] + [768] * 16        # Core Ultra 7 270K Plus
53        self.assertEqual(self.classes(caps), (8, 16))                               # by capacity: >= 90% of the largest
54        self.assertEqual(self.classes(caps, pmu=("0-7", "8-23")), (8, 16))          # by the PMU lists
55        self.assertEqual(setup.hybrid_pool_workers((8, 16)), 15)
56
57    def test_lp_e_cores_are_efficiency_cores(self):
58        """#3262: Core Ultra 7 255H (6P + 8E + 2 LP-E): the LP-E cores are in /sys/devices/cpu_lowpower/cpus, in neither
59        of the other two lists; they were counted as P-cores (8P + 8E, so the engine's every-core default of 15 stayed)."""
60        caps = [1024] * 6 + [774] * 8 + [312] * 2
61        self.assertEqual(self.classes(caps, pmu=("0-5", "6-13", "14-15")), (6, 10))
62        self.assertEqual(self.classes(caps), (6, 10))                               # no PMU lists: capacity says the same
63        self.assertEqual(setup.hybrid_pool_workers((6, 10)), 10)                    # not None (8, 8 gave every core: 15)
64        self.assertEqual(self.classes(caps, pmu=("0-5", "6-13")), (6, 10))          # a kernel without the third list: capacity
65
66    def test_plain_capacities_and_plain_cpus(self):
67        self.assertEqual(self.classes([1024] * 8 + [512] * 8), (8, 8))
68        self.assertIsNone(self.classes([1024] * 8))                                 # no E-cores
69        self.assertIsNone(self.classes([1024, 1012, 1012, 1012]))                   # favored cores only: not hybrid
70
71    def test_smt_siblings_count_once(self):
72        self.assertEqual(self.classes([1024, 1012] * 4 + [768] * 8, smt=True, pmu=("0-15", "16-23")), (8, 8))
73
74    def test_no_capacity_files_and_no_pmu_says_nothing(self):
75        with tempfile.TemporaryDirectory() as d:
76            fake_sys(Path(d), [1024, 768])
77            (Path(d) / "devices/system/cpu/cpu1/cpu_capacity").unlink()
78            self.assertIsNone(setup.linux_core_classes(d))
79
80
81class HybridWorkers(unittest.TestCase):
82    def test_rule(self):
83        self.assertIsNone(setup.hybrid_pool_workers(None))
84        self.assertEqual(setup.hybrid_pool_workers((8, 16)), 15)   # i9-14900K: the fork's measured 15
85        self.assertEqual(setup.hybrid_pool_workers((6, 8)), 9)     # i5-13600K
86        self.assertEqual(setup.hybrid_pool_workers((4, 8)), 7)     # Ryzen AI 9 HX 370 (Zen 5 + Zen 5c)
87        self.assertIsNone(setup.hybrid_pool_workers((8, 8)))       # i7-13700K: all cores measured faster (AMD_HIP.md)
88        self.assertIsNone(setup.hybrid_pool_workers((8, 4)))
89
90    def test_recommend_keeps_a_given_count(self):
91        with mock.patch.object(setup, "cpu_cores", lambda: (8, 16)), mock.patch.object(setup, "ok", lambda *a: None):
92            self.assertEqual(setup.recommend_pool_workers(["--spec", "4"]), ["--spec", "4", "--pool-workers", "15"])
93            given = ["--pool-workers", "12"]
94            self.assertEqual(setup.recommend_pool_workers(given), given)
95        with mock.patch.object(setup, "cpu_cores", lambda: None):
96            self.assertEqual(setup.recommend_pool_workers(["--spec", "4"]), ["--spec", "4"])
97
98    def test_install_on_a_hybrid_cpu(self):
99        ram, found = PROFILES["96GB-1x16GB"]
100        hybrid = [mock.patch.object(setup, "cpu_cores", lambda: (8, 16))]
101        code, text, cfg, _ = install(ram, found, ["--family", "qwen", "--no-start", "--model", "Q2_0"], extra=hybrid)
102        self.assertEqual(code, 0, text)
103        a = cfg["args"]
104        self.assertEqual(a[a.index("--pool-workers") + 1], "15")
105        self.assertIn("hybrid CPU (8 performance + 16 efficiency cores)", text)
106        # the same PC without the hybrid CPU: the config is the one it always was (no --pool-workers)
107        code, text, plain, _ = install(ram, found, ["--family", "qwen", "--no-start", "--model", "Q2_0"])
108        self.assertEqual(code, 0, text)
109        self.assertNotIn("--pool-workers", plain["args"])
110        i = a.index("--pool-workers")
111        self.assertEqual(a[:i] + a[i + 2:], plain["args"])
112
113    def test_calibration_wins(self):
114        ram, found = PROFILES["96GB-1x16GB"]
115        cal = {"settings": {"--pool-workers": "11"}, "date": "2026-10-03"}
116        extra = [mock.patch.object(setup, "cpu_cores", lambda: (8, 16)),
117                 mock.patch.object(setup, "saved_calibration", lambda cfg: cal)]
118        code, text, cfg, _ = install(ram, found, ["--family", "qwen", "--no-start", "--model", "Q2_0"], extra=extra)
119        self.assertEqual(code, 0, text)
120        a = cfg["args"]
121        self.assertEqual(a[a.index("--pool-workers") + 1], "11")
122        self.assertEqual(a.count("--pool-workers"), 1)
123
124
125if __name__ == "__main__":
126    unittest.main()