Skip to content

The agent that grows with you

fix(platform): RTX Spark needs the N1X marker, not just an NVIDIA GPU

alt-glitch840c2d9dff41
diff --git a/hermes_platform/host/facts.py b/hermes_platform/host/facts.py
index 87deaac12e5..209f78692a4 100644
--- a/hermes_platform/host/facts.py
+++ b/hermes_platform/host/facts.py
@@ -11,6 +11,7 @@ import os
import platform
import sys
import sysconfig
+from typing import NamedTuple
from hermes_platform.host import darwin_pci
@@ -272,23 +273,28 @@ def _linux_gpu_vendor_ids() -> list[str] | None:
return [_read_text(f"{root}/{card}/device/vendor", 16).strip() for card in cards]
-def parse_windows_display_adapters(rows: list[tuple[str, str, str]]) -> list[tuple[str, str]]:
- """``(vendor id, description)`` per adapter from display-class ``(subkey, MatchingDeviceId, DriverDesc)``.
+def _hex_after(text: str, tag: str) -> str:
+ return text.split(tag, 1)[1][:4] if tag in text else ""
+
+
+def parse_windows_display_adapters(rows: list[tuple[str, str, str]]) -> list[tuple[str, str, str]]:
+ """``(vendor id, device id, description)`` per adapter from ``(subkey, MatchingDeviceId, DriverDesc)`` rows.
Virtual adapters (no ``VEN_`` id, such as the remote-display driver) are skipped; an
- adapter subkey without a readable id keeps an empty vendor so it counts as unreadable.
+ adapter subkey without a readable id keeps empty ids so it counts as unreadable.
"""
adapters = []
- for subkey, device_id, description in rows:
- if "VEN_" in device_id.upper():
- adapters.append((device_id.upper().split("VEN_", 1)[1][:4], description))
- elif subkey.isdigit() and not device_id:
- adapters.append(("", description))
+ for subkey, matching_id, description in rows:
+ upper = matching_id.upper()
+ if "VEN_" in upper:
+ adapters.append((_hex_after(upper, "VEN_"), _hex_after(upper, "DEV_"), description))
+ elif subkey.isdigit() and not matching_id:
+ adapters.append(("", "", description))
return adapters
@functools.cache
-def _windows_display_adapters() -> tuple[tuple[str, str], ...] | None:
+def _windows_display_adapters() -> tuple[tuple[str, str, str], ...] | None:
import winreg
rows = []
@@ -324,7 +330,7 @@ def gpu_class() -> str:
vendors = darwin_pci.display_vendor_ids()
elif sys.platform == "win32":
adapters = _windows_display_adapters()
- vendors = None if adapters is None else [vendor for vendor, _ in adapters]
+ vendors = None if adapters is None else [vendor for vendor, _, _ in adapters]
elif os.path.exists("/proc/driver/nvidia/version") or os.path.exists(_WSL_NVIDIA_CUDA):
return "nvidia"
else:
@@ -335,6 +341,8 @@ def gpu_class() -> str:
_NVIDIA_PROC_GPUS = "/proc/driver/nvidia/gpus"
+_NVIDIA_PCI_VENDOR = 0x10DE
+_SYS_PCI_DEVICES = "/sys/bus/pci/devices"
def parse_nvidia_gpu_model(information: str) -> str:
@@ -345,23 +353,45 @@ def parse_nvidia_gpu_model(information: str) -> str:
return ""
+class NvidiaGpu(NamedTuple):
+ name: str
+ pci_id: int | None # NVML packing: device id above the 16-bit vendor id
+
+
+def _pci_id(device_hex: str) -> int | None:
+ try:
+ return int(device_hex, 16) << 16 | _NVIDIA_PCI_VENDOR
+ except ValueError:
+ return None
+
+
+def nvidia_gpus_from_adapters(adapters: list[tuple[str, str, str]]) -> tuple[NvidiaGpu, ...]:
+ """The NVIDIA entries of parsed Windows display adapters."""
+ return tuple(NvidiaGpu(description, _pci_id(device)) for vendor, device, description in adapters
+ if vendor.lower() == "10de")
+
+
+def _linux_nvidia_gpus() -> tuple[NvidiaGpu, ...]:
+ try:
+ buses = sorted(os.listdir(_NVIDIA_PROC_GPUS))[:_GPU_SCAN_LIMIT]
+ except OSError:
+ return ()
+ gpus = (NvidiaGpu(parse_nvidia_gpu_model(_read_text(f"{_NVIDIA_PROC_GPUS}/{bus}/information", 4096)),
+ _pci_id(_read_text(f"{_SYS_PCI_DEVICES}/{bus}/device", 16).strip().removeprefix("0x")))
+ for bus in buses)
+ return tuple(gpu for gpu in gpus if gpu.name)
+
+
@functools.cache
-def nvidia_gpu_names() -> tuple[str, ...]:
- """Names of the NVIDIA GPUs: Windows display-class ``DriverDesc``, Linux driver procfs ``Model``; else empty."""
+def nvidia_gpus() -> tuple[NvidiaGpu, ...]:
+ """Name and PCI id of each NVIDIA GPU: the Windows display-class registry, or Linux driver procfs
+ with sysfs PCI ids; empty elsewhere or without the driver."""
if sys.platform == "win32":
try:
- adapters = _windows_display_adapters() or ()
+ return nvidia_gpus_from_adapters(list(_windows_display_adapters() or ()))
except (AttributeError, OSError, TypeError, ValueError):
return ()
- return tuple(name for vendor, name in adapters if vendor.lower() == "10de" and name)
- if not sys.platform.startswith("linux"):
- return ()
- try:
- buses = sorted(os.listdir(_NVIDIA_PROC_GPUS))[:_GPU_SCAN_LIMIT]
- except OSError:
- return ()
- names = (parse_nvidia_gpu_model(_read_text(f"{_NVIDIA_PROC_GPUS}/{bus}/information", 4096)) for bus in buses)
- return tuple(name for name in names if name)
+ return _linux_nvidia_gpus() if sys.platform.startswith("linux") else ()
def _windows_interactive_session() -> bool:
@@ -395,5 +425,5 @@ def interactive_session() -> bool:
def clear_caches() -> None:
"""Clear every cached host fact."""
for fact in (os_family, process_arch, native_arch, cpu_model, cpu_vendor, ram_total_bytes, gpu_class,
- nvidia_gpu_names, _windows_display_adapters):
+ nvidia_gpus, _windows_display_adapters):
fact.cache_clear()
diff --git a/hermes_platform/host/products.py b/hermes_platform/host/products.py
index 1acdb6339ae..ebabd83e17a 100644
--- a/hermes_platform/host/products.py
+++ b/hermes_platform/host/products.py
@@ -21,22 +21,22 @@ def is_nvidia_n1x_pci_id(pci_id: int) -> bool:
def classify_nvidia_platform(*, os_family: str, native_arch: str, cpu_model: str, gpu_class: str,
- nvidia_gpu_names: tuple[str, ...]) -> NvidiaPlatform | None:
+ nvidia_gpus: tuple[facts.NvidiaGpu, ...]) -> NvidiaPlatform | None:
"""Return the NVIDIA platform the supplied host facts describe, or ``None`` without an NVIDIA GPU.
- - ``rtx_spark``: Windows on arm64 with an NVIDIA GPU or the N1X CPU marker. NVIDIA ships no
- Windows-on-Arm driver for a discrete card, so an NVIDIA adapter there is the N1X; a GPU in
- ``is_nvidia_n1x_pci_id``'s range is therefore always this platform.
+ - ``rtx_spark``: Windows on arm64 whose CPU name carries the N1X marker or whose GPU is in
+ ``is_nvidia_n1x_pci_id``'s range, the same range the local catalog's measured row uses.
- ``dgx_spark``: Linux on arm64 whose NVIDIA driver names a GB10 GPU, or whose CPU or
device-tree model names GB10 or DGX Spark. Other arm64 NVIDIA servers are ``discrete``.
- ``discrete``: any other host with an NVIDIA GPU.
"""
cpu = cpu_model.replace("_", " ").upper()
if native_arch == "arm64" and os_family == "win32":
- if gpu_class == "nvidia" or _RTX_SPARK_CPU_MARKER in cpu:
+ n1x_gpu = any(gpu.pci_id is not None and is_nvidia_n1x_pci_id(gpu.pci_id) for gpu in nvidia_gpus)
+ if n1x_gpu or _RTX_SPARK_CPU_MARKER in cpu:
return "rtx_spark"
if native_arch == "arm64" and os_family.startswith("linux"):
- names = " ".join(nvidia_gpu_names).upper()
+ names = " ".join(gpu.name for gpu in nvidia_gpus).upper()
if any(marker in names or marker in cpu for marker in _DGX_SPARK_MARKERS):
return "dgx_spark"
return "discrete" if gpu_class == "nvidia" else None
@@ -54,4 +54,4 @@ def nvidia_platform() -> NvidiaPlatform | None:
"""
return classify_nvidia_platform(
os_family=facts.os_family(), native_arch=facts.native_arch(), cpu_model=facts.cpu_model(),
- gpu_class=facts.gpu_class(), nvidia_gpu_names=facts.nvidia_gpu_names())
+ gpu_class=facts.gpu_class(), nvidia_gpus=facts.nvidia_gpus())
diff --git a/tests/hermes_platform/test_facts.py b/tests/hermes_platform/test_facts.py
index f6a4c031f0e..d6cb24294b2 100644
--- a/tests/hermes_platform/test_facts.py
+++ b/tests/hermes_platform/test_facts.py
@@ -54,7 +54,7 @@ _N1X_DISPLAY_ROWS = [
)
def test_windows_display_adapters_classify_the_gpu(rows, gpu_class) -> None:
adapters = facts.parse_windows_display_adapters(rows)
- assert facts.classify_gpu_vendors([vendor for vendor, _ in adapters]) == gpu_class
+ assert facts.classify_gpu_vendors([vendor for vendor, _, _ in adapters]) == gpu_class
@pytest.mark.parametrize(
diff --git a/tests/hermes_platform/test_products.py b/tests/hermes_platform/test_products.py
index c8a33b7b8a4..931f5b24de6 100644
--- a/tests/hermes_platform/test_products.py
+++ b/tests/hermes_platform/test_products.py
@@ -5,44 +5,59 @@ import subprocess
import pytest
from hermes_platform.host import facts, products
-from hermes_platform.host.facts import parse_nvidia_gpu_model
-
-_N1X_CPU = f"NVIDIA {products._RTX_SPARK_CPU_MARKER}"
+from hermes_platform.host.facts import NvidiaGpu
+
+# Values measured on an NVIDIA N1X laptop: the registry CPU name and its display-class rows.
+_N1X_CPU = "NVIDIA RTX Spark N1X (5120-core GPU, 18-core CPU)"
+_N1X_DISPLAY_ROWS = [
+ ("0000", r"pci\ven_10de&dev_2e03&subsys_33091043", "NVIDIA RTX Spark N1X (6144-core Blackwell RTX GPU)"),
+ ("0001", r"acpi\ven_nvda&dev_200a&rev_0034", "NVIDIA NPU"),
+ ("0002", r"pci\ven_10de&dev_2e06&subsys_33091043", "NVIDIA RTX Spark N1X (5120-core Blackwell RTX GPU)"),
+ ("0003", "RdpIdd_IndirectDisplay", "Microsoft Remote Display Adapter"),
+]
+_N1X_GPUS = facts.nvidia_gpus_from_adapters(facts.parse_windows_display_adapters(_N1X_DISPLAY_ROWS))
+_OTHER_NVIDIA_GPU = (NvidiaGpu("NVIDIA GeForce RTX 4090", 0x268410DE),)
_GB10_INFORMATION = "Model: \t\t NVIDIA GB10\nIRQ: \t\t 196\nGPU UUID: \t GPU-0\n"
+_GB10 = (NvidiaGpu(facts.parse_nvidia_gpu_model(_GB10_INFORMATION), None),)
@pytest.mark.parametrize(
- ("os_family", "native_arch", "cpu_model", "gpu_class", "gpu_information", "expected"),
+ ("os_family", "native_arch", "cpu_model", "gpu_class", "gpus", "expected"),
[
- ("darwin", "arm64", "Apple M3 Max", "apple_silicon", "", None),
- ("win32", "amd64", "AMD Ryzen 9 7950X", "nvidia", "", "discrete"),
- ("win32", "arm64", _N1X_CPU, "nvidia", "", "rtx_spark"),
- ("win32", "arm64", _N1X_CPU, "unknown", "", "rtx_spark"),
- ("win32", "arm64", "Snapdragon X Elite", "none", "", None),
- ("linux", "arm64", "", "nvidia", _GB10_INFORMATION, "dgx_spark"),
- ("linux", "arm64", "NVIDIA DGX Spark", "nvidia", "", "dgx_spark"),
- ("linux", "arm64", "", "nvidia", "Model: \t\t NVIDIA GH200 480GB\n", "discrete"),
- ("linux", "amd64", "Intel Xeon", "nvidia", "Model: \t\t NVIDIA GeForce RTX 4090\n", "discrete"),
- ("linux", "amd64", "Intel Xeon", "none", "", None),
+ ("darwin", "arm64", "Apple M3 Max", "apple_silicon", (), None),
+ ("win32", "amd64", "AMD Ryzen 9 7950X", "nvidia", _OTHER_NVIDIA_GPU, "discrete"),
+ ("win32", "arm64", _N1X_CPU, "nvidia", _N1X_GPUS, "rtx_spark"),
+ ("win32", "arm64", "NVIDIA ARM CPU", "nvidia", _N1X_GPUS, "rtx_spark"),
+ ("win32", "arm64", _N1X_CPU, "unknown", (), "rtx_spark"),
+ ("win32", "arm64", "Snapdragon X Elite", "nvidia", _OTHER_NVIDIA_GPU, "discrete"),
+ ("win32", "arm64", "Snapdragon X Elite", "none", (), None),
+ ("linux", "arm64", "", "nvidia", _GB10, "dgx_spark"),
+ ("linux", "arm64", "NVIDIA DGX Spark", "nvidia", (), "dgx_spark"),
+ ("linux", "arm64", "", "nvidia", (NvidiaGpu("NVIDIA GH200 480GB", None),), "discrete"),
+ ("linux", "amd64", "Intel Xeon", "nvidia", _OTHER_NVIDIA_GPU, "discrete"),
+ ("linux", "amd64", "Intel Xeon", "none", (), None),
],
)
-def test_nvidia_platform_from_host_facts(os_family, native_arch, cpu_model, gpu_class, gpu_information, expected):
- names = tuple(filter(None, [parse_nvidia_gpu_model(gpu_information)]))
+def test_nvidia_platform_from_host_facts(os_family, native_arch, cpu_model, gpu_class, gpus, expected):
assert products.classify_nvidia_platform(
os_family=os_family, native_arch=native_arch, cpu_model=cpu_model, gpu_class=gpu_class,
- nvidia_gpu_names=names) == expected
+ nvidia_gpus=gpus) == expected
@pytest.mark.platforms("linux")
-def test_linux_gpu_names_come_from_the_driver_files_without_a_process(monkeypatch, tmp_path):
- (tmp_path / "000f:01:00.0").mkdir()
- (tmp_path / "000f:01:00.0" / "information").write_text(_GB10_INFORMATION)
- (tmp_path / "0000:02:00.0").mkdir() # a bus directory without an information file is skipped
- monkeypatch.setattr(facts, "_NVIDIA_PROC_GPUS", str(tmp_path))
+def test_linux_nvidia_gpus_come_from_the_driver_files_without_a_process(monkeypatch, tmp_path):
+ proc, sysfs = tmp_path / "proc", tmp_path / "sys"
+ (proc / "000f:01:00.0").mkdir(parents=True)
+ (proc / "000f:01:00.0" / "information").write_text(_GB10_INFORMATION, encoding="utf-8")
+ (proc / "0000:02:00.0").mkdir() # a bus directory without an information file is skipped
+ (sysfs / "000f:01:00.0").mkdir(parents=True)
+ (sysfs / "000f:01:00.0" / "device").write_text("0x2e12\n", encoding="utf-8")
+ monkeypatch.setattr(facts, "_NVIDIA_PROC_GPUS", str(proc))
+ monkeypatch.setattr(facts, "_SYS_PCI_DEVICES", str(sysfs))
monkeypatch.setattr(subprocess, "Popen", lambda *a, **k: pytest.fail("nvidia_platform spawned a process"))
facts.clear_caches()
try:
- assert facts.nvidia_gpu_names() == ("NVIDIA GB10",)
+ assert facts.nvidia_gpus() == (NvidiaGpu("NVIDIA GB10", 0x2E1210DE),)
assert products.nvidia_platform() in ("rtx_spark", "dgx_spark", "discrete", None)
finally:
facts.clear_caches()