The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(local-models): detect discrete Vulkan VRAM
mbeattietex1-star4cb33b1dc47f
diff --git a/hermes_cli/local_runtime/hardware.py b/hermes_cli/local_runtime/hardware.pyindex 7f2e62c534e..ccba506c45a 100644--- a/hermes_cli/local_runtime/hardware.py+++ b/hermes_cli/local_runtime/hardware.py@@ -56,8 +56,11 @@ _POOL_NEGATIVE_TTL_S = 60.0_pool_probe_cache: tuple[float, "tuple[int, bool | None] | None"] | None = None# ' CUDA0: NVIDIA Example Device (1234-core Example GPU) (46464 MiB, 46284 MiB free)'-# — greedy .* pins the LAST parenthesized group, so device names with parentheses parse.-_DEVICE_LINE_RE = re.compile(r"CUDA\d+:.*\((\d+)\s*MiB,\s*\d+\s*MiB free\)\s*$")+# ' Vulkan0: AMD Radeon RX 6800 XT (16368 MiB, 14692 MiB free)'+# — greedy name pins the LAST parenthesized group, so names with parentheses parse.+_DEVICE_LINE_RE = re.compile(+ r"^\s*(?P<backend>[A-Za-z]+)\d+:\s*(?P<name>.*)\s+"+ r"\((?P<total>\d+)\s*MiB,\s*(?P<free>\d+)\s*MiB free\)\s*$")def _stdout(*argv: str) -> str:@@ -296,10 +299,8 @@ def _cuda_driver_pool() -> "tuple[int, bool | None] | None":return None-def _engine_device_pool() -> "tuple[int, bool | None] | None":- """(engine_total_bytes, None) from the installed runtime's own --list-devices, or None. The- fallback when the driver API is unreachable: asks the exact binary that will do the- allocating. Carries no integrated verdict — callers must gate it."""+def _engine_device_info() -> "tuple[int, int, str, str] | None":+ """Return total, free, backend and name from an accelerated runtime."""with suppress(Exception): # a probe miss must never block budgetingfrom hermes_cli.config import get_config_valuefrom hermes_cli.local_runtime.binaries import installed_engine@@ -313,12 +314,19 @@ def _engine_device_pool() -> "tuple[int, bool | None] | None":if out.returncode != 0:return Nonefor line in (out.stdout + out.stderr).splitlines():- m = _DEVICE_LINE_RE.search(line)- if m:- return int(m.group(1)) << 20, None+ if (match := _DEVICE_LINE_RE.search(line)) is not None:+ return (int(match.group("total")) << 20,+ int(match.group("free")) << 20,+ match.group("backend"), match.group("name").strip())return None+def _engine_device_pool() -> "tuple[int, bool | None] | None":+ """Allocator total from an accelerated runtime, without an integrated verdict."""+ info = _engine_device_info()+ return (info[0], None) if info is not None else None++def _device_pool_view() -> "tuple[int, bool | None] | None":"""Best available allocator-side view, cached: a hit is permanent for the process, a missretries after a short TTL (the engine binary can appear mid-session via a pane install)."""@@ -368,14 +376,23 @@ def probe_budget(*, planning: bool = False) -> HardwareBudget:large. The managed server unloads/relaunches itself, so capacity is real."""ram_total, ram_avail = _ram_bytes()- vram = _nvidia_vram()+ nvidia_vram = _nvidia_vram()+ vram = nvidia_vram+ engine_info = _engine_device_info() if vram is None else None+ if engine_info is not None and engine_info[2].lower() in ("vulkan", "hip"):+ # llama.cpp is allocator truth for discrete AMD/Intel devices. WMI AdapterRAM is a+ # legacy 32-bit field and cannot represent modern cards accurately.+ vram = engine_info[0], engine_info[1], engine_info[3], None+ unified = None+ else:+ # Preserve NVIDIA's no-smi path: the CUDA driver can still identify UMA.+ unified = _unified_pool_bytes(nvidia_vram[0] if nvidia_vram else 0, ram_total)# Unified-memory NVIDIA: the CUDA allocator pool is the real capacity. Classification comes# from the driver API/engine and must not require nvidia-smi (stripped-PATH sessions lose smi# but nvcuda loads via the system loader). Crossing the carve-out costs nothing — it is an OS# accounting knob, not a GPU limit. Deliberately NOT clamped to OS RAM: carved-out memory is# invisible to GlobalMemoryStatusEx, so a RAM clamp would throw away exactly that capacity.- unified = _unified_pool_bytes(vram[0] if vram else 0, ram_total)if unified is not None:logger.info("unified-memory NVIDIA device: allocator pool %.1f GiB "@@ -394,8 +411,7 @@ def probe_budget(*, planning: bool = False) -> HardwareBudget:gpu_pci_id=vram[3] if vram else None)if vram is None:- # No NVIDIA device visible: Metal/Vulkan/CPU paths budget from RAM as UMA (Apple- # Silicon) — conservative for discrete AMD until a vendor probe lands.+ # Metal and CPU paths budget from RAM as UMA; unknown accelerators stay conservative.return _uma_budget(ram_total if planning else ram_avail, ram_total)total, free, gpu_name, gpu_pci_id = vramdiff --git a/tests/hermes_cli/test_unified_pool_quirk.py b/tests/hermes_cli/test_unified_pool_quirk.pyindex 439dc4c73e2..16f3b985f5d 100644--- a/tests/hermes_cli/test_unified_pool_quirk.py+++ b/tests/hermes_cli/test_unified_pool_quirk.py@@ -180,6 +180,23 @@ def test_engine_fallback_without_smi_stays_conservative(monkeypatch):assert b.total_device_bytes == UMA_RAM # RAM path, not the pool+def test_vulkan_device_is_budgeted_as_discrete_vram(monkeypatch):+ """An accelerated Vulkan runtime is allocator truth for a discrete AMD card;+ system RAM must not be relabeled as its graphics memory."""+ total, free = 16368 << 20, 7347 << 20+ monkeypatch.setattr(hw, "_nvidia_vram", lambda: None)+ monkeypatch.setattr(hw, "_ram_bytes", lambda: (32 * GIB, 20 * GIB))+ monkeypatch.setattr(hw, "_engine_device_info", lambda: (+ total, free, "Vulkan", "AMD Radeon RX 6800 XT"))++ planning = hw.probe_budget(planning=True)+ live = hw.probe_budget(planning=False)++ assert planning.uma is False+ assert planning.total_device_bytes == total+ assert planning.ram_available_bytes == 32 * GIB+ assert live.total_device_bytes == total+ assert live.usable_vram_bytes == max(0, free - hw._MARGIN_FLOOR)@pytest.mark.platforms("linux")@@ -248,10 +265,33 @@ def test_pool_probe_miss_retries_after_ttl(monkeypatch):# ── device-line parsing (engine fallback) ────────────────────+def test_vulkan_device_line_reports_total_and_free_vram():+ line = " Vulkan0: AMD Radeon RX 6800 XT (16368 MiB, 7347 MiB free)"+ match = hw._DEVICE_LINE_RE.search(line)+ assert match+ assert match.group("backend") == "Vulkan"+ assert match.group("name") == "AMD Radeon RX 6800 XT"+ assert int(match.group("total")) == 16368+ assert int(match.group("free")) == 7347+++def test_metal_device_stays_uma(monkeypatch):+ monkeypatch.setattr(hw, "_nvidia_vram", lambda: None)+ monkeypatch.setattr(hw, "_ram_bytes", lambda: (32 * GIB, 20 * GIB))+ monkeypatch.setattr(hw, "_engine_device_info", lambda: (+ 32 * GIB, 20 * GIB, "Metal", "Apple GPU"))+ monkeypatch.setattr(hw, "_device_pool_view", lambda: None)++ budget = hw.probe_budget(planning=True)++ assert budget.uma is True+ assert budget.total_device_bytes == 32 * GIB++def test_device_line_regex_handles_parenthesized_names():"""Device names may contain their own parentheses — the LASTparenthesized group must win."""line = (" CUDA0: NVIDIA Example Device (1234-core Example GPU) ""(46464 MiB, 46284 MiB free)")m = hw._DEVICE_LINE_RE.search(line)- assert m and int(m.group(1)) == 46464+ assert m and int(m.group("total")) == 46464