Skip to content

The agent that grows with you

fix(local-runtime): price runtime overhead into fit decisions

emozilla3f7621432669
diff --git a/hermes_cli/local_runtime/catalog.py b/hermes_cli/local_runtime/catalog.py
index 3d53f1fe039..4e602729572 100644
--- a/hermes_cli/local_runtime/catalog.py
+++ b/hermes_cli/local_runtime/catalog.py
@@ -48,7 +48,7 @@ import re
from dataclasses import dataclass, field
from pathlib import PurePosixPath
-from hermes_cli.local_runtime.context_policy import FLOOR
+from hermes_cli.local_runtime.context_policy import FLOOR, RUNTIME_OVERHEAD_BYTES
from hermes_cli.local_runtime.estimator import (
HardwareBudget,
LayerKind,
@@ -165,16 +165,18 @@ def select_variant(entry: CatalogEntry, budget: HardwareBudget) -> VariantChoice
3. None: even Q4 fails physics (true refusal).
"""
floor_kv = None
+ overhead = RUNTIME_OVERHEAD_BYTES + (entry.mmproj.size_bytes if entry.mmproj else 0)
for variant in entry.variants:
profile = entry.profile(variant)
if floor_kv is None:
floor_kv = ctx_bytes(profile, min(FLOOR, entry.n_ctx_train or FLOOR))
- if variant.weights_bytes + floor_kv <= budget.usable_vram_bytes:
+ if (variant.weights_bytes + floor_kv + overhead
+ <= budget.usable_vram_bytes):
return VariantChoice(variant=variant, zero_spill=True,
reason_key="best-fits")
smallest = min(entry.variants, key=lambda v: v.size_bytes)
- needed = smallest.weights_bytes + (floor_kv or 0)
+ needed = smallest.weights_bytes + (floor_kv or 0) + overhead
if needed <= budget.usable_vram_bytes + budget.ram_available_bytes:
return VariantChoice(variant=smallest, zero_spill=False,
reason_key="smallest-fits-spilled")
diff --git a/hermes_cli/local_runtime/context_policy.py b/hermes_cli/local_runtime/context_policy.py
index 9de467e31f3..dd4db1d3e7c 100644
--- a/hermes_cli/local_runtime/context_policy.py
+++ b/hermes_cli/local_runtime/context_policy.py
@@ -51,6 +51,14 @@ SPEED_FLOOR_TOK_S = 6.0 # deepest measured spill bottomed near thi
_UMA_CTX_FRACTION = 0.25 # UMA guard: ctx mem <= 25% of unified
_EARLY_COST_CTX_FRACTION = 0.15 # bounded early cost when weights spill
+# What a load really costs beyond weights + KV: CUDA contexts and compute
+# buffers. Measured on a 32 GiB card: a model estimated at 29.3 GiB
+# (weights+KV) loaded at ~31.2 GiB resident and the server's own fit still
+# shaved a layer to CPU — ~1.5 GiB of runtime overhead plus the vision
+# projector when present. A fit that ignores this passes on paper and
+# spills in practice. Callers add mmproj bytes on top.
+RUNTIME_OVERHEAD_BYTES = int(1.5 * (1 << 30))
+
def ladder(native: int) -> list[int]:
"""64K -> 96K -> 128K -> ... -> native (native always the last rung)."""
@@ -90,13 +98,18 @@ def _uma_cap(profile: ModelProfile, budget: HardwareBudget) -> int | None:
def initial_window(profile: ModelProfile, budget: HardwareBudget,
- *, flash_attention: bool = True) -> WindowDecision | PhysicsRefusal:
+ *, flash_attention: bool = True,
+ overhead_bytes: int = 0) -> WindowDecision | PhysicsRefusal:
"""The launch decision: largest cheap rung, never below the floor.
- Zero-spill rung: weights + ctx fit usable VRAM entirely.
+ Zero-spill rung: weights + ctx + overhead fit usable VRAM entirely.
Bounded-early-cost rung: weights already exceed VRAM; take the largest
rung whose ctx stays <= ~15% of usable VRAM.
Floor everywhere, capped at native and the UMA guard.
+
+ ``overhead_bytes``: runtime cost beyond weights+KV (RUNTIME_OVERHEAD
+ plus the vision projector when one loads). Zero keeps this function
+ pure physics for decision-table tests; production callers pass it.
"""
refusal = physics_check(profile, budget, FLOOR, flash_attention=flash_attention)
if refusal:
@@ -111,8 +124,8 @@ def initial_window(profile: ModelProfile, budget: HardwareBudget,
reasons: list[str] = []
best_zero_spill: int | None = None
for rung in rungs:
- need = profile.weights_bytes + ctx_bytes(profile, rung,
- flash_attention=flash_attention)
+ need = (profile.weights_bytes + overhead_bytes
+ + ctx_bytes(profile, rung, flash_attention=flash_attention))
if need <= budget.usable_vram_bytes:
best_zero_spill = rung
else:
diff --git a/hermes_cli/local_runtime/presets.py b/hermes_cli/local_runtime/presets.py
index 9ee60ca3f08..f518df2cbdb 100644
--- a/hermes_cli/local_runtime/presets.py
+++ b/hermes_cli/local_runtime/presets.py
@@ -13,6 +13,7 @@ from dataclasses import dataclass
from pathlib import Path
from hermes_cli.local_runtime.context_policy import (
+ RUNTIME_OVERHEAD_BYTES,
WindowDecision,
initial_window,
launch_args,
@@ -86,7 +87,17 @@ def generate_presets(models_dir: Path, budget: HardwareBudget,
except (ValueError, OSError) as exc:
logger.warning("preset skip %s: %s", gguf.name, exc)
continue
- decision = initial_window(profile, budget)
+ # Overhead beyond weights+KV: runtime buffers, plus the vision
+ # projector when this model ships one (it loads beside the weights).
+ entry = find_entry_for_model(model_id)
+ mmproj_bytes = 0
+ if entry is not None and entry.mmproj is not None:
+ mmproj_path = assets_dir() / entry.mmproj.local_name
+ if mmproj_path.exists():
+ mmproj_bytes = entry.mmproj.size_bytes
+ decision = initial_window(
+ profile, budget,
+ overhead_bytes=RUNTIME_OVERHEAD_BYTES + mmproj_bytes)
if isinstance(decision, PhysicsRefusal):
entries.append(PresetEntry(model_id=model_id, window=0,
spilled=False, refusal=decision.message))
@@ -105,9 +116,10 @@ def generate_presets(models_dir: Path, budget: HardwareBudget,
if override and override > decision.window:
target = min(int(override), native)
kv = ctx_bytes(profile, target)
- if (profile.weights_bytes + kv
- <= budget.usable_vram_bytes + budget.ram_available_bytes):
- spill = max(0, profile.weights_bytes + kv - budget.usable_vram_bytes)
+ need = (profile.weights_bytes + kv
+ + RUNTIME_OVERHEAD_BYTES + mmproj_bytes)
+ if need <= budget.usable_vram_bytes + budget.ram_available_bytes:
+ spill = max(0, need - budget.usable_vram_bytes)
decision = WindowDecision(
window=target, spill_bytes=spill,
kv_on_gpu=kv <= budget.usable_vram_bytes,