Skip to content

The agent that grows with you

fix(local-runtime): keep the prompt cache beside a one-slot draft

emozilla7fcc8e2bfc48
diff --git a/hermes_cli/local_runtime/hardware.py b/hermes_cli/local_runtime/hardware.py
index 1565cf12fc2..a8516150b54 100644
--- a/hermes_cli/local_runtime/hardware.py
+++ b/hermes_cli/local_runtime/hardware.py
@@ -456,6 +456,13 @@ def prompt_cache_bytes(live: HardwareBudget | None, model_bytes: int, residents:
return cache if cache >= _PROMPT_CACHE_MIN else 0
+def usable_beside_prompt_cache(live: HardwareBudget, residents: int = 1) -> int:
+ """Device memory a model may plan with on Windows unified memory and still leave each of the
+ ``residents`` models the smallest useful prompt cache: the inverse of ``prompt_cache_bytes``."""
+ share = live.commit_room_bytes // max(1, residents) - _PROMPT_CACHE_MIN
+ return min(live.usable_vram_bytes, max(0, int(share * (1 - _UMA_HEADROOM_FRACTION))))
+
+
def _gpu_engine_runs_the_model() -> bool:
"""Whether a Vulkan/HIP engine runs the model: the serving or installed engine, else the one
setup would install for ``local_runtime.backend``. False when the engine can't be resolved."""
diff --git a/hermes_cli/local_runtime/presets.py b/hermes_cli/local_runtime/presets.py
index d0164c41357..065e08a3cee 100644
--- a/hermes_cli/local_runtime/presets.py
+++ b/hermes_cli/local_runtime/presets.py
@@ -127,13 +127,15 @@ def _runs_dflash(entry) -> Path | None:
def _dflash_plan(entry, profile, budget: HardwareBudget, live: HardwareBudget | None, *,
fixed_overhead: int, requested_window: int | None, engine_tag: str | None,
- window_without: int):
+ window_without: int, residents: int = 1):
"""The launch plan with the entry's DFlash draft in place of MTP, or None.
The draft runs on one slot: each slot keeps a recurrent-state copy per draft position, and
the router's default four slots hold enough copies to push weights off the GPU. It may
shrink the window to half native, never below what the model gets without it when that is
- smaller; a requested (grown) window it can't hold keeps the model as it was.
+ smaller; a requested (grown) window it can't hold keeps the model as it was. On one slot a
+ side request takes the chat's slot and the prompt cache brings the chat back, so where free
+ commit sizes that cache the draft keeps its smallest useful size.
"""
if _runs_dflash(entry) is None:
return None
@@ -143,6 +145,10 @@ def _dflash_plan(entry, profile, budget: HardwareBudget, live: HardwareBudget |
lean = replace(profile, kv_scale=1.0)
overhead = fixed_overhead + _dflash_extra_bytes(entry, lean)
plan = plan_launch(lean, budget, fixed_overhead=overhead, requested_window=requested_window)
+ if live is not None and live.commit_room_bytes is not None:
+ from hermes_cli.local_runtime.hardware import usable_beside_prompt_cache
+
+ live = replace(live, usable_vram_bytes=usable_beside_prompt_cache(live, residents))
if live is not None:
# fit_to_free_memory holds a too-busy card at the floor rather than move weights off it on a
# live reading. The draft never rides on that hold: it runs only where it fits now.
@@ -200,7 +206,7 @@ def preset_for_model(gguf: Path, budget: HardwareBudget,
dflash = None if decision.spilled else _dflash_plan(
entry, profile, budget, live, fixed_overhead=fixed_overhead, requested_window=requested,
- engine_tag=engine_tag, window_without=decision.window)
+ engine_tag=engine_tag, window_without=decision.window, residents=residents)
if dflash is not None:
plan, decision = dflash, dflash.decision
# Router discovery is preset-only: refused files must never autoload with stock fit.
diff --git a/tests/hermes_cli/test_local_external_draft.py b/tests/hermes_cli/test_local_external_draft.py
index 00354fe93bc..b7ea977e171 100644
--- a/tests/hermes_cli/test_local_external_draft.py
+++ b/tests/hermes_cli/test_local_external_draft.py
@@ -126,6 +126,27 @@ def test_the_draft_runs_only_where_it_fits_beside_other_programs(model):
assert busy and drafted
+def test_on_windows_unified_memory_the_draft_keeps_the_smallest_prompt_cache(model):
+ """On one slot a title or other side request takes the chat's slot, and llama-server's prompt
+ cache is what brings the chat back. Where free commit sizes that cache, the draft runs only
+ with the smallest useful cache beside it."""
+ from hermes_cli.local_runtime import hardware
+
+ gguf, _, _ = model
+ capacity = HardwareBudget(usable_vram_bytes=48 * GIB, total_device_bytes=48 * GIB,
+ ram_available_bytes=64 * GIB, uma=True)
+ drafted = 0
+ for quarters in range(64, 160):
+ room = int(quarters / 4 * GIB)
+ live = replace(capacity, usable_vram_bytes=min(capacity.usable_vram_bytes, int(room * 0.8)),
+ commit_room_bytes=room)
+ preset = presets.preset_for_model(gguf, capacity, set(), live=live, engine_tag="b11671")
+ if _drafts(preset):
+ drafted += 1
+ assert int(preset.keys["cache-ram"]) << 20 >= hardware._PROMPT_CACHE_MIN, quarters / 4
+ assert drafted
+
+
def test_a_grown_window_wins_over_the_draft(model):
"""Growth restored the native window on a roomy card, and other programs now hold part of it:
whatever window still fits goes to the model as it was, not to the draft."""