The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(local-runtime): calibrated MTP memory model + a real desktop reserve — big windows without the edge
emozilla78a7efa5cef0
diff --git a/hermes_cli/local_runtime/catalog.py b/hermes_cli/local_runtime/catalog.pyindex d089fec4c57..713dfeab117 100644--- a/hermes_cli/local_runtime/catalog.py+++ b/hermes_cli/local_runtime/catalog.py@@ -165,7 +165,8 @@ class CatalogEntry:name=variant.model_id, weights_bytes=variant.weights_bytes,embd_table_bytes=0, n_ctx_train=self.n_ctx_train,layers=layers, swa_window=self.swa_window, moe=self.moe,- n_vocab=self.n_vocab)+ n_vocab=self.n_vocab,+ kv_scale=1.2 if self.mtp else 1.0)def download_files(self, variant: QuantVariant) -> tuple:"""Everything a download job fetches for this variant, in order."""diff --git a/hermes_cli/local_runtime/context_policy.py b/hermes_cli/local_runtime/context_policy.pyindex 558ed630f8a..1f08ae354b3 100644--- a/hermes_cli/local_runtime/context_policy.py+++ b/hermes_cli/local_runtime/context_policy.py@@ -267,9 +267,20 @@ def launch_args(profile: ModelProfile, decision: WindowDecision, *,def ub_logits_bytes(n_vocab: int, *, mtp_capable: bool,mtp_prefill: bool = False) -> int:- """GPU logits-buffer cost of the microbatch choice made by- launch_args, priced from the model's own vocab: ubatch x vocab x fp32,- doubled by MTP's draft context. Callers add this to RUNTIME_OVERHEAD- per model — the flag and its price travel together or the fit lies."""- ubatch = 2048 if (not mtp_capable or mtp_prefill) else 512- return ubatch * max(0, int(n_vocab)) * 4 * (2 if mtp_capable else 1)+ """GPU logits/compute-buffer cost of the microbatch posture chosen by+ launch_args, priced from the model's own vocab and calibrated against+ measured server RSS (Qwen3.8 Q4, both postures, three windows):++ stacked (MTP + ub2048): ubatch x vocab x fp32 x 1.5 (~2.9 GiB at+ 248K vocab; fitted 2.5, rounded up)+ decode (MTP + ub512): ubatch x vocab x fp32 x 2 (~1.0 GiB)+ plain (ub2048): ubatch x vocab x fp32 (~1.9 GiB)++ Callers add this to RUNTIME_OVERHEAD per model — the flag and its+ price travel together or the fit lies."""+ v = max(0, int(n_vocab))+ if mtp_capable and mtp_prefill:+ return int(2048 * v * 4 * 1.5)+ if mtp_capable:+ return 512 * v * 4 * 2+ return 2048 * v * 4diff --git a/hermes_cli/local_runtime/estimator.py b/hermes_cli/local_runtime/estimator.pyindex 8df6dcd3136..6778b88ed45 100644--- a/hermes_cli/local_runtime/estimator.py+++ b/hermes_cli/local_runtime/estimator.py@@ -61,6 +61,12 @@ class ModelProfile:moe: bool = Falsearchitecture: str = ""n_vocab: int = 0 # prices logits buffers (ubatch x vocab)+ # Context-cost multiplier. MTP spec decode keeps a small draft+ # context beside the main one. Calibrated against four measured+ # server-RSS points on Qwen3.8 Q4 (128K/221K/256K, both postures):+ # the draft adds ~17% to per-token KV; 1.2 rounds up so the error+ # stays on the safe side (+250 MiB at 256K, never negative).+ kv_scale: float = 1.0@propertydef per_token_kv_f16(self) -> int:@@ -133,7 +139,8 @@ def kv_dtype_factor(flash_attention: bool) -> float:def ctx_bytes(profile: ModelProfile, window: int, *,flash_attention: bool = True) -> int:"""Context memory for one window: full layers linear in T, SWA layers- capped at the sliding window, recurrent layers constant."""+ capped at the sliding window, recurrent layers constant. Scaled by+ profile.kv_scale (MTP draft context)."""factor = kv_dtype_factor(flash_attention)total = 0.0for kind, per_token_f16 in profile.layers:@@ -143,7 +150,7 @@ def ctx_bytes(profile: ModelProfile, window: int, *,total += per_token_f16 * factor * min(window, profile.swa_window)else:total += per_token_f16 * factor * window- return int(total)+ return int(total * profile.kv_scale)@dataclassdiff --git a/hermes_cli/local_runtime/hardware.py b/hermes_cli/local_runtime/hardware.pyindex 430721551bb..c4a3b3b71fb 100644--- a/hermes_cli/local_runtime/hardware.py+++ b/hermes_cli/local_runtime/hardware.py@@ -39,10 +39,16 @@ from hermes_cli.local_runtime.estimator import HardwareBudgetlogger = logging.getLogger(__name__)_GIB = 1 << 30-# Fit margin per design: max(512 MiB, ~7% of device) — the upstream flat-# 1 GiB is regressive on 8 GB cards and generous on 24 GB ones.-_MARGIN_FLOOR = 512 << 20-_MARGIN_FRACTION = 0.07+# Reserve carved off the card before any grant: the desktop's own+# co-residents (compositor, browser, Electron) measure ~2-2.5 GiB on a+# working machine, and a window granted into that space demotes silently+# under WDDM. 7% covers big cards; the 2 GiB floor is what the margin's+# old 512 MiB floor failed to cover in practice (a 221K grant measured+# 31.9/32.6 GiB with the desktop running — 'fits' by the math, demoted+# in reality). Small cards give up window to this; spill mode is their+# path to big models regardless.+_MARGIN_FLOOR = 2 << 30+_MARGIN_FRACTION = 0.09# UMA headroom: on unified-memory machines (Apple Silicon, unified-memory# NVIDIA) the model shares physical memory with the OS and every app, so# budget from RAM minus this fraction.diff --git a/hermes_cli/local_runtime/presets.py b/hermes_cli/local_runtime/presets.pyindex 8bc9739f6fd..51c09bca317 100644--- a/hermes_cli/local_runtime/presets.py+++ b/hermes_cli/local_runtime/presets.py@@ -100,27 +100,44 @@ def generate_presets(models_dir: Path, budget: HardwareBudget,entry = hit[0] if hit is not None else Noneis_mtp = (entry.mtp if entry is not Noneelse model_id in (mtp_capable or set()))+ if is_mtp and profile.kv_scale == 1.0:+ # Header-derived profiles don't know about MTP's draft+ # context; apply the calibrated KV multiplier here so the+ # launch fit prices what the server will actually allocate.+ import dataclasses++ profile = dataclasses.replace(profile, kv_scale=1.2)mmproj_bytes = 0if entry is not None and entry.mmproj is not None:mmproj_path = assets_dir() / entry.mmproj.local_nameif mmproj_path.exists():mmproj_bytes = entry.mmproj.size_bytes- # MTP posture ladder: try the stacked posture first (MTP + large- # prefill microbatch — best on both axes where it fits), fall back- # to the decode-only posture (ub512), whose smaller logits buffer- # may fit where the stack doesn't. Whichever survives is priced- # into the window decision — flag and cost from the same call.+ # MTP posture ladder — window first, prefill second: price the+ # launch under both postures and keep whichever grants the larger+ # window (the stacked posture's bigger compute buffer buys ~3x+ # short-prompt prefill but costs ~2 GiB that would otherwise be+ # window; measured at 256K the ub512 posture still prefills at+ # 2.7K tok/s, so window wins ties only one way: never trade+ # context away for prefill). Same window -> stacked.mtp_prefill = Falselogits_bytes = ub_logits_bytes(profile.n_vocab, mtp_capable=is_mtp)if is_mtp:- stacked = ub_logits_bytes(profile.n_vocab, mtp_capable=True,- mtp_prefill=True)- probe = initial_window(+ stacked_logits = ub_logits_bytes(profile.n_vocab, mtp_capable=True,+ mtp_prefill=True)+ stacked_probe = initial_window(+ profile, budget,+ overhead_bytes=(RUNTIME_OVERHEAD_BYTES + mmproj_bytes+ + stacked_logits))+ plain_probe = initial_window(profile, budget,- overhead_bytes=RUNTIME_OVERHEAD_BYTES + mmproj_bytes + stacked)- if (not isinstance(probe, PhysicsRefusal) and not probe.spilled):+ overhead_bytes=(RUNTIME_OVERHEAD_BYTES + mmproj_bytes+ + logits_bytes))+ if (not isinstance(stacked_probe, PhysicsRefusal)+ and not stacked_probe.spilled+ and (isinstance(plain_probe, PhysicsRefusal)+ or stacked_probe.window >= plain_probe.window)):mtp_prefill = True- logits_bytes = stacked+ logits_bytes = stacked_logitsdecision = initial_window(profile, budget,overhead_bytes=RUNTIME_OVERHEAD_BYTES + mmproj_bytes + logits_bytes)diff --git a/tests/hermes_cli/test_context_policy.py b/tests/hermes_cli/test_context_policy.pyindex ccc4d10a10e..b98bcd49c10 100644--- a/tests/hermes_cli/test_context_policy.py+++ b/tests/hermes_cli/test_context_policy.py@@ -341,7 +341,7 @@ def test_launch_args_contract():assert "--spec-type" in sv = 248320assert ub_logits_bytes(v, mtp_capable=True) == 512 * v * 4 * 2- assert ub_logits_bytes(v, mtp_capable=True, mtp_prefill=True) == 2048 * v * 4 * 2+ assert ub_logits_bytes(v, mtp_capable=True, mtp_prefill=True) == int(2048 * v * 4 * 1.5)c = launch_args(p, resident, mtp_capable=False)assert "-ub" in c and c[c.index("-ub") + 1] == "2048" # prefill hint@@ -390,3 +390,21 @@ def test_no_refusal_branch_past_physics():for vram in (4, 6, 8, 12):d = initial_window(dense(weights_gib=20), card(vram, ram_gib=64))assert isinstance(d, WindowDecision)+++def test_kv_scale_prices_mtp_draft_context():+ """MTP profiles carry kv_scale > 1 (the draft context's KV share,+ calibrated from measured server RSS); ctx_bytes must scale with it so+ every consumer — launch fit, catalog rows, growth — prices what the+ server actually allocates. Four-point calibration held within+ +1.4 GiB conservative, never optimistic."""+ import dataclasses++ p = moe()+ base = ctx_bytes(p, 131072)+ scaled = ctx_bytes(dataclasses.replace(p, kv_scale=1.2), 131072)+ assert scaled == int(base * 1.2)++ # The safety direction: the estimate must never be BELOW measured.+ # (Calibration receipts: predicted-measured was +233..+1400 MiB.)+ assert scaled > base