diff --git a/serve/telemetry.py b/serve/telemetry.py index b0d1bae2..3232c63c 100644 --- a/serve/telemetry.py +++ b/serve/telemetry.py @@ -251,6 +251,63 @@ def gpu_reader(index=0, amd=False): return _Amd(index) if amd else _Nvml(index) +_DISPLAY_CLASS = r"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}" + + +def windows_display_adapters(vendor=None) -> list: + """#1587: the display adapters Windows has drivers for, from the registry's display class (no subprocess, no vendor + library): [{"name", "provider", "driver", "vram_bytes"}] in the class's order. `vendor`: "amd", "nvidia" or + "intel" keeps that vendor's cards. [] off Windows or when nothing can be read.""" + if os.name != "nt": + return [] + try: + import winreg + root = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _DISPLAY_CLASS) + except OSError: + return [] + out = [] + i = 0 + while True: + try: + sub = winreg.EnumKey(root, i) + except OSError: + break + i += 1 + if not sub.isdigit(): + continue + try: + k = winreg.OpenKey(root, sub) + except OSError: + continue + + def val(name, k=k): + try: + return winreg.QueryValueEx(k, name)[0] + except OSError: + return None + name = val("DriverDesc") + if not isinstance(name, str) or not name.strip(): + continue + mem = val("HardwareInformation.qwMemorySize") + if mem is None: + mem = val("HardwareInformation.MemorySize") + if isinstance(mem, (bytes, bytearray)): + mem = int.from_bytes(bytes(mem[:8]), "little") if mem else None + out.append({"name": name.strip(), "provider": str(val("ProviderName") or ""), "driver": val("DriverVersion"), + "vram_bytes": mem if isinstance(mem, int) and mem > 0 else None}) + if vendor: + keys = {"amd": ("advanced micro devices", "amd", "radeon"), "nvidia": ("nvidia",), "intel": ("intel",)}[vendor] + out = [a for a in out if any(s in (a["provider"] + " " + a["name"]).lower() for s in keys)] + return out + + +def windows_amd_cards(adapters=None) -> list: + """The AMD cards HIP runs on, as windows_display_adapters lists them. A Ryzen's integrated Radeon is listed too + (512 MiB on a Ryzen 5 7600): beside a card with 2 GiB or more it is left out (an APU alone keeps it).""" + cards = windows_display_adapters("amd") if adapters is None else adapters + return [c for c in cards if (c["vram_bytes"] or 0) >= 2 << 30] or cards + + def free_vram_mib(index=0, amd=False): """Free VRAM of a card in MiB, or None when it cannot be read.""" g = gpu_reader(index, amd) @@ -348,8 +405,18 @@ class Telemetry: except ImportError: self.ps = None self.fallback = _CpuRamFallback() + gpu_name = " + ".join(g.name() or "?" for _, g in self.gpus) if self.gpu.ok() else None + win_amd = None + if gpu_name is None and amd and os.name == "nt": + # #1587: no readings for an AMD card on Windows yet, but its name (and VRAM size) are in the registry, so the + # dashboard and strata_bench do not call the run "CPU only". The class order is taken as HIP's order. + cards = windows_amd_cards() + win_amd = [cards[i] for i in idx if 0 <= i < len(cards)] or cards[:1] + gpu_name = " + ".join(c["name"] for c in win_amd) if win_amd else None self.static = { - "gpu_name": " + ".join(g.name() or "?" for _, g in self.gpus) if self.gpu.ok() else None, + "gpu_name": gpu_name, + "gpu_vram_total_mib": (sum(c["vram_bytes"] for c in win_amd) >> 20 + if win_amd and all(c["vram_bytes"] for c in win_amd) else None), "gpu_count": len(self.gpus), # #1380: the AMD readings are the amdgpu driver's Linux sysfs files; a Windows AMD card has none yet, and the # dashboard said "not readable (NVML)" or showed empty tiles with no word why diff --git a/tools/strata_bench.py b/tools/strata_bench.py index 67bd63a0..63c66c7b 100644 --- a/tools/strata_bench.py +++ b/tools/strata_bench.py @@ -500,6 +500,17 @@ def gpu_info_local() -> dict: m2 = re.search(r"Driver version:\s*(\S+)", v) if m2: info["rocm_driver"] = m2.group(1) + if not info["gpus"] and os.name == "nt": + # #1587: Windows with only an AMD card has neither nvidia-smi nor rocm-smi; the display class in the registry + # names the card, its VRAM and its driver (the run was labelled "CPU only" before) + try: + if str(ROOT) not in sys.path: + sys.path.insert(0, str(ROOT)) + from serve.telemetry import windows_amd_cards + info["gpus"] = [{"name": c["name"], "vram_mib": (c["vram_bytes"] >> 20) if c["vram_bytes"] else None, + "driver": c["driver"]} for c in windows_amd_cards()] + except Exception: # noqa: BLE001 - a report without the card's name is still a report + pass return info diff --git a/tools/test_windows_amd_gpu.py b/tools/test_windows_amd_gpu.py new file mode 100644 index 00000000..e6802d58 --- /dev/null +++ b/tools/test_windows_amd_gpu.py @@ -0,0 +1,98 @@ +"""#1587: a Windows PC with only an AMD card - the server's hardware_static names the card from the registry's display +class, and strata_bench no longer labels the run "CPU only". No GPU, no registry needed (a fake winreg). + + python -m unittest tools.test_windows_amd_gpu +""" +from __future__ import annotations + +import sys +import types +import unittest +from pathlib import Path +from unittest import mock + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +sys.path.insert(0, str(ROOT / "tools")) +from serve import telemetry as T # noqa: E402 + +GB = 1 << 30 + + +def fake_winreg(adapters): + """A winreg with the display class holding `adapters` (dicts of value name -> value) under 0000, 0001, ...""" + w = types.ModuleType("winreg") + w.HKEY_LOCAL_MACHINE = "HKLM" + keys = {f"{i:04d}": a for i, a in enumerate(adapters)} + subs = list(keys) + ["Properties"] + + def OpenKey(root, sub): + if root == "HKLM": + return "CLASS" + if root == "CLASS" and sub in keys: + return keys[sub] + raise OSError("denied") + + def EnumKey(k, i): + if i >= len(subs): + raise OSError("no more") + return subs[i] + + def QueryValueEx(k, name): + if name not in k: + raise OSError("no value") + return k[name], 1 + w.OpenKey, w.EnumKey, w.QueryValueEx = OpenKey, EnumKey, QueryValueEx + return w + + +IGPU = {"DriverDesc": "AMD Radeon(TM) Graphics", "ProviderName": "Advanced Micro Devices, Inc.", + "DriverVersion": "32.0.13066.12", "HardwareInformation.qwMemorySize": 512 << 20} +CARD = {"DriverDesc": "AMD Radeon RX 6900 XT", "ProviderName": "Advanced Micro Devices, Inc.", + "DriverVersion": "32.0.21001.9024", "HardwareInformation.MemorySize": (16 * GB).to_bytes(8, "little")} +GEFORCE = {"DriverDesc": "NVIDIA GeForce RTX 5070", "ProviderName": "NVIDIA", "DriverVersion": "32.0.16.1047", + "HardwareInformation.qwMemorySize": 12 * GB} + + +class Adapters(unittest.TestCase): + def read(self, adapters, vendor=None): + with mock.patch.dict(sys.modules, {"winreg": fake_winreg(adapters)}), mock.patch.object(T.os, "name", "nt"): + return T.windows_display_adapters(vendor) + + def test_reads_names_vram_and_drivers(self): + a = self.read([IGPU, CARD, GEFORCE]) + self.assertEqual([x["name"] for x in a], ["AMD Radeon(TM) Graphics", "AMD Radeon RX 6900 XT", "NVIDIA GeForce RTX 5070"]) + self.assertEqual(a[1]["vram_bytes"], 16 * GB) # the binary form of the memory size + self.assertEqual(a[2]["vram_bytes"], 12 * GB) + self.assertEqual([x["name"] for x in self.read([IGPU, CARD, GEFORCE], "amd")], + ["AMD Radeon(TM) Graphics", "AMD Radeon RX 6900 XT"]) + + def test_the_integrated_radeon_is_left_out_beside_a_card(self): + self.assertEqual([c["name"] for c in T.windows_amd_cards(self.read([IGPU, CARD], "amd"))], ["AMD Radeon RX 6900 XT"]) + self.assertEqual([c["name"] for c in T.windows_amd_cards(self.read([IGPU], "amd"))], ["AMD Radeon(TM) Graphics"]) + + def test_off_windows_nothing(self): + with mock.patch.object(T.os, "name", "posix"): + self.assertEqual(T.windows_display_adapters(), []) + + def test_hardware_static_names_the_card(self): + cards = [{"name": "AMD Radeon RX 6900 XT", "provider": "AMD", "driver": "x", "vram_bytes": 16 * GB}] + with mock.patch.object(T.os, "name", "nt"), mock.patch.object(T, "windows_amd_cards", return_value=cards), \ + mock.patch.object(T, "gpu_reader", return_value=mock.Mock(ok=lambda: False)), \ + mock.patch.object(T, "_CpuRamFallback"), mock.patch.object(T, "_cpu_name", return_value="cpu"), \ + mock.patch.object(T.threading, "Thread"): + t = T.Telemetry(amd=True) + self.assertEqual(t.static["gpu_name"], "AMD Radeon RX 6900 XT") + self.assertEqual(t.static["gpu_vram_total_mib"], 16384) + + def test_bench_labels_the_card(self): + import strata_bench as B + cards = [{"name": "AMD Radeon RX 6900 XT", "provider": "AMD", "driver": "32.0.21001.9024", "vram_bytes": 16 * GB}] + with mock.patch.object(B, "run_cmd", return_value=""), mock.patch.object(B.os, "name", "nt"), \ + mock.patch.object(T, "windows_amd_cards", return_value=cards): + info = B.gpu_info_local() + self.assertEqual(info["gpus"], [{"name": "AMD Radeon RX 6900 XT", "vram_mib": 16384, "driver": "32.0.21001.9024"}]) + + +if __name__ == "__main__": + unittest.main()