Skip to content

The agent that grows with you

feat(local-runtime): quant selection targets a large context window, not just the floor

emozilla0ba78a7ecda0
diff --git a/hermes_cli/local_runtime/catalog.py b/hermes_cli/local_runtime/catalog.py
index 4e602729572..724224a1eaa 100644
--- a/hermes_cli/local_runtime/catalog.py
+++ b/hermes_cli/local_runtime/catalog.py
@@ -48,7 +48,11 @@ import re
from dataclasses import dataclass, field
from pathlib import PurePosixPath
-from hermes_cli.local_runtime.context_policy import FLOOR, RUNTIME_OVERHEAD_BYTES
+from hermes_cli.local_runtime.context_policy import (
+ FLOOR,
+ RUNTIME_OVERHEAD_BYTES,
+ TARGET_WINDOW,
+)
from hermes_cli.local_runtime.estimator import (
HardwareBudget,
LayerKind,
@@ -152,28 +156,47 @@ class VariantChoice:
variant: QuantVariant
zero_spill: bool
- reason_key: str # "best-fits" | "smallest-fits-spilled"
+ reason_key: str # "best-large-window" | "best-fits" | "smallest-fits-spilled"
def select_variant(entry: CatalogEntry, budget: HardwareBudget) -> VariantChoice | None:
"""Best build for this hardware, per the ladder policy.
- 1. Highest-quality variant whose weights + 64K-floor KV zero-spill in
- usable VRAM (quality is free when it fits — take it).
- 2. Else Q4 spilled — the floor guarantee stands, fit spills weights,
- picker prices it honestly (slow beats lobotomized).
- 3. None: even Q4 fails physics (true refusal).
+ Ranked rules (each a guarantee the ones below may not break):
+ 1. Never below Q4 — the ladder's quality floor.
+ 2. Never below a 64K window — every session must be viable.
+ 3. Prefer reaching TARGET_WINDOW — compression should be the
+ exception, not the routine.
+ 4. Then maximize quality — free once the above hold.
+
+ Concretely: highest-quality variant that zero-spills at the target
+ window; else highest-quality at the 64K floor (small cards keep the
+ old behavior); else the smallest build spilled; else refusal. One
+ quality step is worth multiple context rungs (dynamic-quant deltas
+ between adjacent rungs are small; 64K vs 216K changes what a session
+ can do), but nothing outranks full residency.
"""
- floor_kv = None
overhead = RUNTIME_OVERHEAD_BYTES + (entry.mmproj.size_bytes if entry.mmproj else 0)
+ native = entry.n_ctx_train or FLOOR
+ floor_kv = None
+ target_kv = None
+ floor_pick: QuantVariant | None = None
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 + overhead
+ floor_kv = ctx_bytes(profile, min(FLOOR, native))
+ target_kv = ctx_bytes(profile, min(TARGET_WINDOW, native))
+ if (variant.weights_bytes + target_kv + overhead
<= budget.usable_vram_bytes):
return VariantChoice(variant=variant, zero_spill=True,
- reason_key="best-fits")
+ reason_key="best-large-window")
+ if floor_pick is None and (variant.weights_bytes + floor_kv + overhead
+ <= budget.usable_vram_bytes):
+ floor_pick = variant
+
+ if floor_pick is not None:
+ return VariantChoice(variant=floor_pick, 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) + overhead
diff --git a/hermes_cli/local_runtime/context_policy.py b/hermes_cli/local_runtime/context_policy.py
index dd4db1d3e7c..ac34b15d8e4 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
+# TARGET_WINDOW: the smallest ladder rung at which compression becomes the
+# exception rather than the routine. Measured over 161 real agentic
+# sessions: 66% complete uncompressed in 64K, 82% in 96K, 91% in 144K —
+# and the marginal gain past 144K (+6 points for 216K) falls below the
+# quality cost of stepping down another quant. Quant selection prefers
+# the best build that reaches this; the FLOOR remains the guarantee.
+TARGET_WINDOW = 144 * 1024
+
# 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
diff --git a/hermes_cli/web_routers/local_models.py b/hermes_cli/web_routers/local_models.py
index 612b4bc58a7..8f3de0fbea6 100644
--- a/hermes_cli/web_routers/local_models.py
+++ b/hermes_cli/web_routers/local_models.py
@@ -512,7 +512,14 @@ async def local_models_catalog():
"size_label": _human_gb(download_total),
"variant_count": len(entry.variants),
})
- if choice.reason_key == "best-fits":
+ if choice.reason_key == "best-large-window":
+ best = entry.variants[0]
+ row["quant_reason"] = (
+ "Best quality build — runs fully on your GPU with a large context window"
+ if variant.quant == best.quant
+ else (f"Best balance for your GPU ({variant.quant}) — a larger "
+ "build would shrink the context window"))
+ elif choice.reason_key == "best-fits":
best = entry.variants[0]
row["quant_reason"] = (
"Best quality build — runs fully on your GPU"
diff --git a/tests/hermes_cli/test_catalog_variants.py b/tests/hermes_cli/test_catalog_variants.py
index e69bd277f4f..c791af325db 100644
--- a/tests/hermes_cli/test_catalog_variants.py
+++ b/tests/hermes_cli/test_catalog_variants.py
@@ -52,12 +52,15 @@ def test_split_variants_have_coherent_parts():
def test_big_card_gets_best_quality():
- """A card with headroom takes the top rung — quality is free when it fits."""
+ """A card with real headroom takes the top rung — quality is free when
+ it fits AT THE TARGET WINDOW. Muse is dense (target KV ~18 GiB), so
+ 'real headroom' for its Q8 means ~56+ GiB usable."""
entry = catalog_by_id()["muse-glimmer-30b"]
- choice = select_variant(entry, budget(48))
+ choice = select_variant(entry, budget(60))
assert choice is not None
assert choice.zero_spill
assert choice.variant.quant == entry.variants[0].quant # UD-Q8_K_XL
+ assert choice.reason_key == "best-large-window"
def test_quality_monotone_in_vram():
@@ -93,9 +96,9 @@ def test_frontier_model_refused_on_consumer_card_offered_on_big_ram():
def test_selection_accounts_for_kv_not_just_weights():
- """The zero-spill check prices weights + 64K-floor KV, not weights
- alone: give a machine exactly enough VRAM for the Q8 weights of a
- dense model and it must step down a rung."""
+ """The zero-spill check prices weights + KV, not weights alone: give a
+ machine exactly enough VRAM for the Q8 weights of a dense model and it
+ must step down a rung."""
entry = catalog_by_id()["muse-glimmer-30b"]
q8 = entry.variants[0]
exactly_weights = HardwareBudget(
@@ -107,6 +110,48 @@ def test_selection_accounts_for_kv_not_just_weights():
assert choice.variant.quant != q8.quant, "KV cost ignored — Q8 can't fit with floor KV"
+def test_target_window_beats_one_quality_step():
+ """The usability rule: a quant that only clears the 64K floor loses to
+ the next rung down when that rung clears the target window. A real
+ 32 GiB card is ~29.6 GiB usable after margin: Qwen3.8-27B Q6 (needs
+ ~31.5 with target KV + overhead) misses, Q5 (~26.2) clears — the
+ selector must pick Q5 and say why."""
+ entry = catalog_by_id()["qwen3.8-27b"]
+ choice = select_variant(entry, budget(29.6))
+ assert choice is not None and choice.zero_spill
+ assert choice.variant.quant == "UD-Q5_K_XL", (
+ f"expected the target-window pick, got {choice.variant.quant}")
+ assert choice.reason_key == "best-large-window"
+
+
+def test_floor_fallback_when_no_variant_reaches_target():
+ """Cards where nothing clears the target keep the old rule: highest
+ quality that zero-spills at the 64K floor (reason 'best-fits'), never
+ a needless step down."""
+ entry = catalog_by_id()["qwen3.8-27b"]
+ # ~21.5 GiB usable: Q4 weights (16.7 GiB in-memory) + floor KV +
+ # overhead fits, but no variant reaches 144K-target KV.
+ choice = select_variant(entry, budget(23))
+ assert choice is not None and choice.zero_spill
+ assert choice.reason_key == "best-fits"
+ assert choice.variant.quant == "UD-Q4_K_XL"
+
+
+def test_target_never_degrades_below_floor_choice():
+ """The target preference may only IMPROVE the window, never the
+ floor guarantees: whenever the old floor rule found a zero-spill pick,
+ the new rule also finds one (possibly a smaller quant, never spill)."""
+ for entry in CATALOG:
+ for vram in (8, 12, 16, 24, 32, 48, 96):
+ choice = select_variant(entry, budget(vram, ram_gib=256))
+ if choice is None:
+ continue
+ # Rule 2: whatever was chosen zero-spill must genuinely clear
+ # the floor (the selector's own invariant, re-checked).
+ if choice.zero_spill:
+ assert choice.reason_key in ("best-large-window", "best-fits")
+
+
def test_find_entry_for_model_resolves_split_ids():
hit = find_entry_for_model("DeepSeek-V4-Flash-0731-UD-Q4_K_XL")
assert hit is not None