okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
76e4d7040
main
1"""The shared-memory page drop (STRATA_UMA_DROP_PAGES) runs and prints only in a Linux CUDA build. 2 3src/program/generate.cpp gives the GGUF's cached expert pages back on a GPU that reports cudaDevAttrIntegrated (the DGX Spark). 4The same attribute is true on AMD APUs under HIP and on every Mac under the Metal layer, the gfx906 layer has no such attribute, 5and Windows has no page drop, so the whole block (the call and its log line) must sit inside the one guard that excludes them. 6""" 7import re 8import unittest 9from pathlib import Path 10 11ROOT = Path (__file__ ).resolve ().parents [1 ] 12GEN = ROOT / "src" / "program" / "generate.cpp" 13GUARD = ("#if defined(__linux__) && !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) && !defined(STRATA_GPU_METAL)" 14" // SPARK-UMA-DROP" ) 15 16 17def guarded_span (lines ): 18"""(first, last) line index of the guarded block: the guard line and its matching #endif.""" 19start = next (i for i ,l in enumerate (lines )if l .strip ()== GUARD .strip ()) 20depth = 0 21for i in range (start ,len (lines )): 22s = lines [i ].lstrip () 23if re .match (r"#\s*if" ,s ): 24depth += 1 25elif re .match (r"#\s*endif" ,s ): 26depth -= 1 27if depth == 0 : 28return start ,i 29raise AssertionError ("the guard has no matching #endif" ) 30 31 32class UmaDropGuard (unittest .TestCase ): 33def setUp (self ): 34self .lines = GEN .read_text (encoding = "utf-8" ).splitlines () 35 36def test_guard_present_once (self ): 37self .assertEqual (sum (1 for l in self .lines if l .strip ()== GUARD .strip ()),1 ) 38 39def test_call_and_line_inside_the_guard (self ): 40first ,last = guarded_span (self .lines ) 41for needle in ("drop_cached_pages(" ,"holds every expert, so the" ,"getenv(\"STRATA_UMA_DROP_PAGES\")" ): 42at = [i for i ,l in enumerate (self .lines )if needle in l ] 43self .assertTrue (at ,needle ) 44for i in at : 45self .assertTrue (first < i < last ,f" { needle !r } at line { i + 1 } is outside the Linux-CUDA guard ( { first + 1 } - { last + 1 } )" ) 46 47def test_line_only_when_pages_were_given_back (self ): 48first ,last = guarded_span (self .lines ) 49block = "\n" .join (self .lines [first :last + 1 ]) 50self .assertRegex (block ,r"if \(freed > 0\)\s*\n\s*std::fprintf\(stderr, \"strata serve: this GPU shares" ) 51 52 53if __name__ == "__main__" : 54unittest .main ()