The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(platform): one nvidia_platform() answer for the backend
alt-glitch5c479698c269
diff --git a/hermes_platform/host/facts.py b/hermes_platform/host/facts.pyindex 8696b259d14..b609a91e370 100644--- a/hermes_platform/host/facts.py+++ b/hermes_platform/host/facts.py@@ -295,6 +295,30 @@ def gpu_class() -> str:return "unknown" if vendors is None else classify_gpu_vendors(vendors)+_NVIDIA_PROC_GPUS = "/proc/driver/nvidia/gpus"+++def parse_nvidia_gpu_model(information: str) -> str:+ """Return the ``Model:`` value of a ``/proc/driver/nvidia/gpus/<bus>/information`` file."""+ for line in information.splitlines():+ if line.startswith("Model:"):+ return line.split(":", 1)[1].strip()+ return ""+++def nvidia_gpu_names() -> tuple[str, ...]:+ """Model names the Linux NVIDIA driver reports per GPU; empty elsewhere or without the driver."""+ 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)++def _windows_interactive_session() -> bool:import ctypesfrom ctypes import wintypes@@ -325,5 +349,6 @@ 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):+ for fact in (os_family, process_arch, native_arch, cpu_model, cpu_vendor, ram_total_bytes, gpu_class,+ nvidia_gpu_names):fact.cache_clear()diff --git a/hermes_platform/host/products.py b/hermes_platform/host/products.pyindex 3fde5fd1a36..67e315004cc 100644--- a/hermes_platform/host/products.py+++ b/hermes_platform/host/products.py@@ -1,15 +1,18 @@-"""Vendor SoC recognizers derived from host facts.+"""NVIDIA platform recognition derived from host facts.-Recognition uses OS-reported CPU or PCI identity without environment-variable input.+Recognition uses OS-reported CPU, GPU or PCI identity without environment-variable input."""from __future__ import annotations+from typing import Literal+from hermes_platform.host import facts-# Match the CPU string because the chassis vendor does not identify the SoC.-_NVIDIA_SOC_VENDOR = "NVIDIA"-_NVIDIA_SOC_MODEL_MARKERS = ("N1X", "SPARK")+NvidiaPlatform = Literal["rtx_spark", "dgx_spark", "discrete"]++_RTX_SPARK_CPU_MARKER = "N1X"+_DGX_SPARK_MARKERS = ("GB10", "DGX SPARK")def is_nvidia_n1x_pci_id(pci_id: int) -> bool:@@ -17,16 +20,41 @@ def is_nvidia_n1x_pci_id(pci_id: int) -> bool:return pci_id & 0xFFFF == 0x10DE and 0x2E00 <= pci_id >> 16 <= 0x2E3F-def looks_like_nvidia_arm_soc(*, native_arch: str, cpu_model: str, cpu_vendor: str) -> bool:- """Return whether the supplied facts identify an NVIDIA ARM SoC."""- if native_arch != "arm64":- return False- model = cpu_model.upper()- vendor_match = _NVIDIA_SOC_VENDOR in model or cpu_vendor.strip().upper() == _NVIDIA_SOC_VENDOR- return vendor_match and any(marker in model for marker in _NVIDIA_SOC_MODEL_MARKERS)+def classify_nvidia_platform(*, os_family: str, native_arch: str, cpu_model: str, gpu_class: str,+ nvidia_gpu_names: tuple[str, ...]) -> 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.+ - ``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:+ return "rtx_spark"+ if native_arch == "arm64" and os_family.startswith("linux"):+ names = " ".join(nvidia_gpu_names).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+++def nvidia_platform() -> NvidiaPlatform | None:+ """The NVIDIA platform of this host; ``classify_nvidia_platform`` holds the rules.++ Reads only cached host facts (registry, sysfs, procfs, sysctl): no subprocess, no driver+ library, no network. The N1X PCI range is the one the local catalog's measured row uses. Not+ verified on hardware: the N1X CPU marker in the Windows registry, the GB10 model name in+ ``/proc/driver/nvidia/gpus/*/information``, and the DGX Spark device-tree model.+ """+ 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())def is_nvidia_arm_soc() -> bool:- """Return whether this host has an NVIDIA ARM SoC."""- return looks_like_nvidia_arm_soc(- native_arch=facts.native_arch(), cpu_model=facts.cpu_model(), cpu_vendor=facts.cpu_vendor())+ """Return whether this host is an RTX Spark or a DGX Spark."""+ return nvidia_platform() in ("rtx_spark", "dgx_spark")diff --git a/tests/hermes_platform/test_products.py b/tests/hermes_platform/test_products.pyindex 833f564d503..c84bec0a78c 100644--- a/tests/hermes_platform/test_products.py+++ b/tests/hermes_platform/test_products.py@@ -1,24 +1,40 @@from __future__ import annotations+import subprocess+import pytest-from hermes_platform.host import products-from hermes_platform.host.products import looks_like_nvidia_arm_soc+from hermes_platform.host import facts, products+from hermes_platform.host.facts import parse_nvidia_gpu_model-_MARKER = products._NVIDIA_SOC_MODEL_MARKERS[0]-_VENDOR = products._NVIDIA_SOC_VENDOR+_N1X_CPU = f"NVIDIA {products._RTX_SPARK_CPU_MARKER}"+_GB10_INFORMATION = "Model: \t\t NVIDIA GB10\nIRQ: \t\t 196\nGPU UUID: \t GPU-0\n"@pytest.mark.parametrize(- ("native_arch", "cpu_model", "cpu_vendor", "expected"),+ ("os_family", "native_arch", "cpu_model", "gpu_class", "gpu_information", "expected"),[- ("arm64", f"{_VENDOR} {_MARKER} (18-core CPU)", _VENDOR, True),- ("arm64", f"{_MARKER} (18-core CPU)", _VENDOR, True),- ("arm64", f"{_VENDOR} {_MARKER}", "", True),- ("amd64", f"{_VENDOR} {_MARKER}", _VENDOR, False),- ("arm64", "Snapdragon X Elite", "Qualcomm", False),- ("arm64", f"{_VENDOR} GPU only", _VENDOR, False),+ ("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),],)-def test_looks_like_nvidia_arm_soc(native_arch: str, cpu_model: str, cpu_vendor: str, expected: bool) -> None:- assert looks_like_nvidia_arm_soc(native_arch=native_arch, cpu_model=cpu_model, cpu_vendor=cpu_vendor) is expected+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)]))+ 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+++def test_nvidia_platform_spawns_no_process(monkeypatch):+ facts.clear_caches()+ monkeypatch.setattr(subprocess, "Popen", lambda *a, **k: pytest.fail("nvidia_platform spawned a process"))+ assert products.nvidia_platform() in ("rtx_spark", "dgx_spark", "discrete", None)+ facts.clear_caches()