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.4 KiB90 linesraw
1"""`bench.sh` (tools/strata_bench.py) has its own GPU probe, so the ROCm 7+ fix in strata_cli/bench_facts.py did not
2reach it: with no `rocm-smi` on the box it wrote the server's vendor stub as the card ("AMD Radeon",
3`...-community-unknown-...`) and `GPU driver: not read`. The strings below are the real answers of `amd-smi static
4--asic --json`, `static --driver --json` and `list --json` on one AI PRO R9700 (gfx1201) plus a Ryzen iGPU, with ROCm
510.1, trimmed to the fields the probe reads."""
6from __future__ import annotations
7
8import json
9import os
10import sys
11import tempfile
12import unittest
13from pathlib import Path
14from unittest import mock
15
16from tools import strata_bench as sb
17
18ASIC = {"gpu_data": [{"gpu": 0, "asic": {"market_name": "AMD Radeon AI PRO R9700", "target_graphics_version": "gfx1201"}},
19                     {"gpu": 1, "asic": {"market_name": "AMD Radeon Graphics", "target_graphics_version": "gfx1036"}}]}
20DRIVER = {"gpu_data": [{"gpu": 0, "driver": {"name": "amdgpu", "version": "N/A",
21                                             "os_kernel_version": "7.3.0-rc6-2-cachyos-rc"}},
22                       {"gpu": 1, "driver": {"name": "amdgpu", "version": "6.16.4"}}]}
23LIST = [{"gpu": 0, "bdf": "0000:03:00.0", "node_id": 1}, {"gpu": 1, "bdf": "0000:7c:00.0", "node_id": 2}]
24BDF_TO_TOTAL = {"0000:03:00.0": "34208743424", "0000:7c:00.0": "805306368"}
25
26
27def run_cmd(cmd: list[str], timeout: float = 15) -> str:
28    """ROCm 10.1 answers, with `rocm-smi` missing entirely."""
29    if cmd[:1] == ["rocm-smi"] or cmd[:1] == ["nvidia-smi"]:
30        return ""
31    if cmd[:3] == ["amd-smi", "list", "--json"]:
32        return json.dumps(LIST)
33    if cmd[:2] == ["amd-smi", "static"]:
34        return json.dumps(DRIVER if "--driver" in cmd else ASIC)
35    return ""
36
37
38@unittest.skipIf(os.name == "nt", "the probe is the non-Windows branch")
39class AmdSmiProbe(unittest.TestCase):
40    def setUp(self):
41        self.tmp = tempfile.TemporaryDirectory()
42        self.addCleanup(self.tmp.cleanup)
43        # /sys/class/drm/cardN/device is a symlink whose target ends in the PCI address; the fake tree repeats the
44        # shape, and the probe matches that address against `amd-smi list`
45        root = Path(self.tmp.name)
46        drm = root / "drm"
47        for card, bdf in (("card1", "0000:03:00.0"), ("card2", "0000:7c:00.0")):
48            real = root / "devices" / bdf
49            real.mkdir(parents=True)
50            (real / "mem_info_vram_total").write_text(BDF_TO_TOTAL[bdf], encoding="utf-8")
51            drm.mkdir(parents=True, exist_ok=True)
52            (drm / card).mkdir(exist_ok=True)
53            os.symlink(real, drm / card / "device")
54        self.drm = str(drm)
55        real_path = Path
56
57        class DrmPath(real_path):
58            def __new__(cls, *a, **kw):
59                return real_path(self.drm) if a == ("/sys/class/drm",) else real_path(*a, **kw)
60
61        self.drm_patch = mock.patch.object(sb, "Path", DrmPath)
62        self.drm_patch.start()
63        self.addCleanup(self.drm_patch.stop)
64        self.cmd_patch = mock.patch.object(sb, "run_cmd", run_cmd)
65        self.cmd_patch.start()
66        self.addCleanup(self.cmd_patch.stop)
67
68    def test_the_probe_names_both_cards(self):
69        info = sb.gpu_info_local()
70        self.assertEqual([g["name"] for g in info["gpus"]], ["AMD Radeon AI PRO R9700", "AMD Radeon Graphics"])
71        self.assertEqual([g["vram_mib"] for g in info["gpus"]], [34208743424 >> 20, 805306368 >> 20])
72        self.assertEqual([g["driver"] for g in info["gpus"]], [None, "6.16.4"])
73        self.assertEqual(info.get("rocm_driver"), "6.16.4")
74
75    def test_the_folder_names_the_card_it_used(self):
76        # the suite reports the card the engine runs on: the words gpu_slug drops are the vendor ones only
77        self.assertEqual(sb.gpu_slug({"gpu": ["AMD Radeon AI PRO R9700"]}), "ai-pro-r9700")
78
79    def test_the_vendor_stub_does_not_replace_the_card(self):
80        metrics = {"hardware_static": {"gpu_name": "AMD Radeon", "cpu_name": "AMD Ryzen 9 9950X3D",
81                                       "cores": 16, "threads": 32}, "engine": {}}
82        with mock.patch.object(sb, "try_json", lambda base, path, key="", timeout=20: metrics if path == "/metrics" else {}):
83            m = sb.collect_machine("http://127.0.0.1:1", "", None)
84        self.assertEqual(m["gpu"], ["AMD Radeon AI PRO R9700"])
85        self.assertEqual(m["gpu_vram_mib"], 34208743424 >> 20)
86        self.assertEqual(m["gpu_driver"], None)
87
88
89if __name__ == "__main__":
90    unittest.main()