okami.mom/strata

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

Niko1221test_uma_drop_guard: the GPU-shares line is looked for without its draft tag76e4d7040

main
2.3 KiB54 linesraw
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."""
19    start = next(i for i, l in enumerate(lines) if l.strip() == GUARD.strip())
20    depth = 0
21    for i in range(start, len(lines)):
22        s = lines[i].lstrip()
23        if re.match(r"#\s*if", s):
24            depth += 1
25        elif re.match(r"#\s*endif", s):
26            depth -= 1
27            if depth == 0:
28                return start, i
29    raise AssertionError("the guard has no matching #endif")
30
31
32class UmaDropGuard(unittest.TestCase):
33    def setUp(self):
34        self.lines = GEN.read_text(encoding="utf-8").splitlines()
35
36    def test_guard_present_once(self):
37        self.assertEqual(sum(1 for l in self.lines if l.strip() == GUARD.strip()), 1)
38
39    def test_call_and_line_inside_the_guard(self):
40        first, last = guarded_span(self.lines)
41        for needle in ("drop_cached_pages(", "holds every expert, so the", "getenv(\"STRATA_UMA_DROP_PAGES\")"):
42            at = [i for i, l in enumerate(self.lines) if needle in l]
43            self.assertTrue(at, needle)
44            for i in at:
45                self.assertTrue(first < i < last, f"{needle!r} at line {i + 1} is outside the Linux-CUDA guard ({first + 1}-{last + 1})")
46
47    def test_line_only_when_pages_were_given_back(self):
48        first, last = guarded_span(self.lines)
49        block = "\n".join(self.lines[first:last + 1])
50        self.assertRegex(block, r"if \(freed > 0\)\s*\n\s*std::fprintf\(stderr, \"strata serve: this GPU shares")
51
52
53if __name__ == "__main__":
54    unittest.main()