okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
83027df58
main
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 4python -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.""" 23cpu_dir = root / "devices" / "system" / "cpu" 24n = 0 25for core ,cap in enumerate (caps ): 26for _ in range (2 if smt and cap >= 1000 else 1 ): 27d = 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 )) 32n += 1 33if pmu : 34for 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 42def classes (self ,caps ,** kw ): 43with tempfile .TemporaryDirectory ()as d : 44fake_sys (Path (d ),caps ,** kw ) 45cl = setup .linux_core_classes (d ) 46if not cl or max (cl )== min (cl ): 47return None 48p = sum (1 for c in cl if c == max (cl )) 49return p ,len (cl )- p 50 51def test_favored_cores_do_not_hide_the_p_cores (self ): 52caps = [1024 ,1012 ,1024 ,1012 ,1012 ,1012 ,1012 ,1012 ]+ [768 ]* 16 # Core Ultra 7 270K Plus 53self .assertEqual (self .classes (caps ), (8 ,16 ))# by capacity: >= 90% of the largest 54self .assertEqual (self .classes (caps ,pmu = ("0-7" ,"8-23" )), (8 ,16 ))# by the PMU lists 55self .assertEqual (setup .hybrid_pool_workers ((8 ,16 )),15 ) 56 57def 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 59of the other two lists; they were counted as P-cores (8P + 8E, so the engine's every-core default of 15 stayed).""" 60caps = [1024 ]* 6 + [774 ]* 8 + [312 ]* 2 61self .assertEqual (self .classes (caps ,pmu = ("0-5" ,"6-13" ,"14-15" )), (6 ,10 )) 62self .assertEqual (self .classes (caps ), (6 ,10 ))# no PMU lists: capacity says the same 63self .assertEqual (setup .hybrid_pool_workers ((6 ,10 )),10 )# not None (8, 8 gave every core: 15) 64self .assertEqual (self .classes (caps ,pmu = ("0-5" ,"6-13" )), (6 ,10 ))# a kernel without the third list: capacity 65 66def test_plain_capacities_and_plain_cpus (self ): 67self .assertEqual (self .classes ([1024 ]* 8 + [512 ]* 8 ), (8 ,8 )) 68self .assertIsNone (self .classes ([1024 ]* 8 ))# no E-cores 69self .assertIsNone (self .classes ([1024 ,1012 ,1012 ,1012 ]))# favored cores only: not hybrid 70 71def test_smt_siblings_count_once (self ): 72self .assertEqual (self .classes ([1024 ,1012 ]* 4 + [768 ]* 8 ,smt = True ,pmu = ("0-15" ,"16-23" )), (8 ,8 )) 73 74def test_no_capacity_files_and_no_pmu_says_nothing (self ): 75with tempfile .TemporaryDirectory ()as d : 76fake_sys (Path (d ), [1024 ,768 ]) 77 (Path (d )/ "devices/system/cpu/cpu1/cpu_capacity" ).unlink () 78self .assertIsNone (setup .linux_core_classes (d )) 79 80 81class HybridWorkers (unittest .TestCase ): 82def test_rule (self ): 83self .assertIsNone (setup .hybrid_pool_workers (None )) 84self .assertEqual (setup .hybrid_pool_workers ((8 ,16 )),15 )# i9-14900K: the fork's measured 15 85self .assertEqual (setup .hybrid_pool_workers ((6 ,8 )),9 )# i5-13600K 86self .assertEqual (setup .hybrid_pool_workers ((4 ,8 )),7 )# Ryzen AI 9 HX 370 (Zen 5 + Zen 5c) 87self .assertIsNone (setup .hybrid_pool_workers ((8 ,8 )))# i7-13700K: all cores measured faster (AMD_HIP.md) 88self .assertIsNone (setup .hybrid_pool_workers ((8 ,4 ))) 89 90def test_recommend_keeps_a_given_count (self ): 91with mock .patch .object (setup ,"cpu_cores" ,lambda : (8 ,16 )),mock .patch .object (setup ,"ok" ,lambda * a :None ): 92self .assertEqual (setup .recommend_pool_workers (["--spec" ,"4" ]), ["--spec" ,"4" ,"--pool-workers" ,"15" ]) 93given = ["--pool-workers" ,"12" ] 94self .assertEqual (setup .recommend_pool_workers (given ),given ) 95with mock .patch .object (setup ,"cpu_cores" ,lambda :None ): 96self .assertEqual (setup .recommend_pool_workers (["--spec" ,"4" ]), ["--spec" ,"4" ]) 97 98def test_install_on_a_hybrid_cpu (self ): 99ram ,found = PROFILES ["96GB-1x16GB" ] 100hybrid = [mock .patch .object (setup ,"cpu_cores" ,lambda : (8 ,16 ))] 101code ,text ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--no-start" ,"--model" ,"Q2_0" ],extra = hybrid ) 102self .assertEqual (code ,0 ,text ) 103a = cfg ["args" ] 104self .assertEqual (a [a .index ("--pool-workers" )+ 1 ],"15" ) 105self .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) 107code ,text ,plain ,_ = install (ram ,found , ["--family" ,"qwen" ,"--no-start" ,"--model" ,"Q2_0" ]) 108self .assertEqual (code ,0 ,text ) 109self .assertNotIn ("--pool-workers" ,plain ["args" ]) 110i = a .index ("--pool-workers" ) 111self .assertEqual (a [:i ]+ a [i + 2 :],plain ["args" ]) 112 113def test_calibration_wins (self ): 114ram ,found = PROFILES ["96GB-1x16GB" ] 115cal = {"settings" : {"--pool-workers" :"11" },"date" :"2026-10-03" } 116extra = [mock .patch .object (setup ,"cpu_cores" ,lambda : (8 ,16 )), 117mock .patch .object (setup ,"saved_calibration" ,lambda cfg :cal )] 118code ,text ,cfg ,_ = install (ram ,found , ["--family" ,"qwen" ,"--no-start" ,"--model" ,"Q2_0" ],extra = extra ) 119self .assertEqual (code ,0 ,text ) 120a = cfg ["args" ] 121self .assertEqual (a [a .index ("--pool-workers" )+ 1 ],"11" ) 122self .assertEqual (a .count ("--pool-workers" ),1 ) 123 124 125if __name__ == "__main__" : 126unittest .main ()