Skip to content

The agent that grows with you

fix(local-runtime): price QSA compute buffers at the launch posture

emozillafbb9453fbb70
diff --git a/hermes_cli/local_runtime/catalog.json b/hermes_cli/local_runtime/catalog.json
index eb5d5d89d87..e823ce0b3c9 100644
--- a/hermes_cli/local_runtime/catalog.json
+++ b/hermes_cli/local_runtime/catalog.json
@@ -70,6 +70,7 @@
"per_layer_f16": 2048,
"moe": true,
"n_vocab": 248320,
+ "architecture": "qwen4exp",
"mtp_draft_depth": 3,
"mmproj": {
"path": "mmproj-BF16.gguf",
diff --git a/hermes_cli/local_runtime/catalog.py b/hermes_cli/local_runtime/catalog.py
index 8e7ba7621a0..0f62023089d 100644
--- a/hermes_cli/local_runtime/catalog.py
+++ b/hermes_cli/local_runtime/catalog.py
@@ -96,6 +96,9 @@ class CatalogEntry:
# Vocab size prices the GPU logits buffers (ubatch x vocab x fp32, doubled under MTP backend
# sampling) — a multi-GiB term at large vocabs that a weights-only fit would miss.
n_vocab: int = 0
+ # GGUF general.architecture; prices architecture-specific buffers (estimator._WINDOW_COMPUTE_BYTES)
+ # before the file is on disk.
+ architecture: str = ""
mmproj: "AssetFile | None" = None # vision projector, downloads with model
draft: "AssetFile | None" = None # spec-decode draft model (e.g. DSpark)
# MTP head shipped as its own file (the model carries none): the engine loads it as the
@@ -132,8 +135,8 @@ class CatalogEntry:
return ModelProfile(
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, kv_scale=1.2 if self.mtp_capable else 1.0,
- lazy_bytes=variant.lazy_bytes)
+ architecture=self.architecture, n_vocab=self.n_vocab,
+ kv_scale=1.2 if self.mtp_capable else 1.0, lazy_bytes=variant.lazy_bytes)
def launch_plan(self, variant: QuantVariant, budget: HardwareBudget) -> LaunchPlan:
# Optional external drafts may use spare memory after download, never reduce this grant.
@@ -285,7 +288,7 @@ _SCALAR_FIELDS = {
"swa_layers": (int, 0), "swa_window": (int, 0),
"moe": (bool, False), "mtp": (bool, False), "mtp_draft_depth": (int, 3),
"n_vocab": (int, 0), "sampling": (dict, {}), "min_engine": (str, ""),
- "quality": (int, 0), "decode_fraction": (float, 1.0),
+ "quality": (int, 0), "decode_fraction": (float, 1.0), "architecture": (str, ""),
}
diff --git a/hermes_cli/local_runtime/context_policy.py b/hermes_cli/local_runtime/context_policy.py
index 61d0438bf78..e910e2f875b 100644
--- a/hermes_cli/local_runtime/context_policy.py
+++ b/hermes_cli/local_runtime/context_policy.py
@@ -32,6 +32,10 @@ TARGET_WINDOW = 144 * 1024
# past this constant). Callers add mmproj bytes on top.
RUNTIME_OVERHEAD_BYTES = int(1.5 * (1 << 30))
+# llama.cpp's default microbatch, and the larger one launch_args passes for faster prefill.
+DEFAULT_UBATCH = 512
+PREFILL_UBATCH = 2048
+
def ladder(native: int) -> list[int]:
"""64K -> 96K -> 128K -> ... -> native (native always the last rung)."""
@@ -114,6 +118,15 @@ class LaunchPlan:
decision: WindowDecision | PhysicsRefusal
mtp_prefill: bool
overhead_bytes: int
+ profile: ModelProfile | None = None # as priced at the chosen posture (posture_profile)
+
+
+def posture_profile(profile: ModelProfile, *, mtp_capable: bool, mtp_prefill: bool) -> ModelProfile:
+ """``profile`` at one launch posture: window-scaled compute buffers grow with the microbatch
+ launch_args passes, and MTP runs a second context beside the target."""
+ ubatch = PREFILL_UBATCH if mtp_prefill or not mtp_capable else DEFAULT_UBATCH
+ contexts = 2 if mtp_capable else 1
+ return replace(profile, window_compute_per_token=profile.window_compute_bytes * ubatch * contexts)
def plan_launch(profile: ModelProfile, budget: HardwareBudget, *, mtp_capable: bool = False,
@@ -130,20 +143,21 @@ def plan_launch(profile: ModelProfile, budget: HardwareBudget, *, mtp_capable: b
initial: dict[bool, WindowDecision | PhysicsRefusal] = {}
def candidate(stacked: bool) -> LaunchPlan:
+ posture = posture_profile(profile, mtp_capable=mtp_capable, mtp_prefill=stacked)
overhead = fixed_overhead + ub_logits_bytes(
profile.n_vocab, mtp_capable=mtp_capable, mtp_prefill=stacked)
- decision = initial_window(profile, budget, overhead_bytes=overhead)
+ decision = initial_window(posture, budget, overhead_bytes=overhead)
initial[stacked] = decision
if isinstance(decision, WindowDecision) and requested_window:
target = min(requested_window, profile.n_ctx_train or requested_window)
if target > decision.window and physics_check(
- profile, budget, target, overhead_bytes=overhead) is None:
- need = footprint_bytes(profile, target, overhead_bytes=overhead)
+ posture, budget, target, overhead_bytes=overhead) is None:
+ need = footprint_bytes(posture, target, overhead_bytes=overhead)
decision = WindowDecision(
window=target, spill_bytes=max(0, need - budget.usable_vram_bytes),
- kv_on_gpu=ctx_bytes(profile, target) + overhead <= budget.usable_vram_bytes,
+ kv_on_gpu=ctx_bytes(posture, target) + overhead <= budget.usable_vram_bytes,
reasons=[f"grown window restored ({target // 1024}K)"])
- return LaunchPlan(decision, stacked, overhead)
+ return LaunchPlan(decision, stacked, overhead, posture)
lean = candidate(False)
if not mtp_capable:
@@ -186,13 +200,13 @@ def fit_to_free_memory(plan: LaunchPlan, profile: ModelProfile, live: HardwareBu
return plan
return LaunchPlan(replace(now.decision, reasons=[
f"{now.decision.window // 1024}K fits beside other programs' GPU memory "
- f"({decision.window // 1024}K would not)"]), now.mtp_prefill, now.overhead_bytes)
+ f"({decision.window // 1024}K would not)"]), now.mtp_prefill, now.overhead_bytes, now.profile)
floor = min(FLOOR, profile.n_ctx_train or FLOOR)
if decision.window <= floor:
return plan
return LaunchPlan(WindowDecision(window=floor, spill_bytes=0, kv_on_gpu=True, reasons=[
f"floor held at {floor // 1024}K; other programs hold most of the GPU memory"]),
- plan.mtp_prefill, plan.overhead_bytes)
+ plan.mtp_prefill, plan.overhead_bytes, plan.profile)
@dataclass
@@ -287,9 +301,9 @@ def launch_args(profile: ModelProfile, decision: WindowDecision, *, flash_attent
args += ["--spec-type", "draft-mtp", "--spec-draft-n-max", str(mtp_draft_depth),
"--backend-sampling", "--spec-draft-backend-sampling"]
if mtp_prefill:
- args += ["-b", "4096", "-ub", "2048"]
+ args += ["-b", "4096", "-ub", str(PREFILL_UBATCH)]
else:
- args += ["-b", "2048", "-ub", "2048"]
+ args += ["-b", str(PREFILL_UBATCH), "-ub", str(PREFILL_UBATCH)]
if flash_attention:
args += ["-ctk", "q8_0", "-ctv", "q8_0", "-fa", "on"]
if decision.spilled and not uma:
diff --git a/hermes_cli/local_runtime/estimator.py b/hermes_cli/local_runtime/estimator.py
index b7653675015..13f88e39c4d 100644
--- a/hermes_cli/local_runtime/estimator.py
+++ b/hermes_cli/local_runtime/estimator.py
@@ -42,6 +42,13 @@ def _expand_swa_period(period: int, n_layer: int, dense_first: bool) -> list[int
# right order; unknown SSM shapes must never underestimate.
_RECURRENT_STATE_PER_LAYER = 4 << 20
+# Compute-buffer bytes per microbatch token per window token, per llama.cpp context, for
+# architectures whose attention scores the whole window each microbatch. qwen4exp's QSA indexer:
+# measured on b11370 with the router's unified KV (every slot sees the full window), device plus
+# host buffers, target and MTP head contexts alike (39.5 + 6.1 B; 5.9 GiB per context at 256K and
+# -ub 512, ~23 GiB at -ub 2048).
+_WINDOW_COMPUTE_BYTES = {"qwen4exp": 46}
+
class LayerKind(Enum):
FULL = "full"
@@ -75,6 +82,13 @@ class ModelProfile:
# Bytes of architecture-marked tensors (gguf._LAZY_READ_TENSORS) the engine can read from disk
# on demand instead of loading.
lazy_bytes: int = 0
+ # Compute-buffer bytes per window token at the launch posture (plan_launch sets it from
+ # window_compute_bytes, the microbatch and the context count); zero prices none.
+ window_compute_per_token: int = 0
+
+ @property
+ def window_compute_bytes(self) -> int:
+ return _WINDOW_COMPUTE_BYTES.get(self.architecture, 0)
@property
def per_token_kv_f16(self) -> int:
@@ -166,8 +180,9 @@ 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. Scaled by profile.kv_scale (MTP draft context)."""
+ """Memory that grows with the window: full layers linear in T, SWA layers capped at the sliding
+ window, recurrent layers constant, KV scaled by profile.kv_scale (MTP draft context), plus the
+ posture's window-scaled compute buffers."""
factor = kv_dtype_factor(flash_attention)
total = 0.0
for kind, per_token_f16 in profile.layers:
@@ -177,7 +192,7 @@ def ctx_bytes(profile: ModelProfile, window: int, *, flash_attention: bool = Tru
total += per_token_f16 * factor * min(window, profile.swa_window)
else:
total += per_token_f16 * factor * window
- return int(total * profile.kv_scale)
+ return int(total * profile.kv_scale) + profile.window_compute_per_token * window
@dataclass
diff --git a/hermes_cli/local_runtime/presets.py b/hermes_cli/local_runtime/presets.py
index 14c85eba254..674c0dfc834 100644
--- a/hermes_cli/local_runtime/presets.py
+++ b/hermes_cli/local_runtime/presets.py
@@ -6,7 +6,7 @@ from __future__ import annotations
import json
import logging
-from dataclasses import dataclass, replace
+from dataclasses import dataclass
from pathlib import Path
from hermes_cli.local_runtime.context_policy import (
@@ -161,7 +161,8 @@ def preset_for_model(gguf: Path, budget: HardwareBudget,
# The model carries no MTP layers: draft-mtp drafts from the shipped head instead.
keys["model-draft"] = str(companions.mtp_head)
draft_path = _asset_path(entry.draft) if decision.spilled else None
- if draft_path is not None and _draft_fits(draft_path, profile, budget, decision.window, plan.overhead_bytes):
+ if draft_path is not None and _draft_fits(draft_path, plan.profile, budget, decision.window,
+ plan.overhead_bytes):
keys["model-draft"] = str(draft_path)
keys["spec-type"] = "draft-dspark"
# Unsloth's measured cliff: acceptance 83% at 2-3 drafts, collapses at 4.
@@ -186,9 +187,7 @@ def resident_footprint(gguf: Path, budget: HardwareBudget, window: int) -> int |
plan = plan_launch(profile, budget, mtp_capable=is_mtp,
fixed_overhead=RUNTIME_OVERHEAD_BYTES + companions.nbytes,
requested_window=window)
- if is_mtp and profile.kv_scale == 1.0:
- profile = replace(profile, kv_scale=1.2)
- return footprint_bytes(profile, window, overhead_bytes=plan.overhead_bytes)
+ return footprint_bytes(plan.profile, window, overhead_bytes=plan.overhead_bytes)
def _launch_footprint(gguf: Path, budget: HardwareBudget) -> int | None:
@@ -213,7 +212,7 @@ def _launch_footprint(gguf: Path, budget: HardwareBudget) -> int | None:
return None
# Priced whole even when the plan spills: a spilled model still holds part of its weights on
# the device, and over-counting errs toward the side that cannot thrash.
- return footprint_bytes(profile, plan.decision.window, overhead_bytes=plan.overhead_bytes)
+ return footprint_bytes(plan.profile, plan.decision.window, overhead_bytes=plan.overhead_bytes)
def admitted_residency_count(models_dir: Path, budget: HardwareBudget, configured: int) -> int:
diff --git a/tests/hermes_cli/test_catalog_variants.py b/tests/hermes_cli/test_catalog_variants.py
index 7316afb33e3..28a6c44fe29 100644
--- a/tests/hermes_cli/test_catalog_variants.py
+++ b/tests/hermes_cli/test_catalog_variants.py
@@ -143,7 +143,8 @@ def test_catalog_and_preset_agree_on_identical_model_facts(tmp_path, monkeypatch
from types import SimpleNamespace
from hermes_cli.local_runtime import bootstrap, catalog, presets
- from hermes_cli.local_runtime.context_policy import RUNTIME_OVERHEAD_BYTES, ub_logits_bytes
+ from hermes_cli.local_runtime.context_policy import (
+ RUNTIME_OVERHEAD_BYTES, posture_profile, ub_logits_bytes)
from hermes_cli.local_runtime.estimator import ctx_bytes
from hermes_cli.web_routers.local_models import _catalog_row
@@ -177,7 +178,9 @@ def test_catalog_and_preset_agree_on_identical_model_facts(tmp_path, monkeypatch
overhead = (RUNTIME_OVERHEAD_BYTES + entry.companion_bytes
+ ub_logits_bytes(profile.n_vocab, mtp_capable=mtp,
mtp_prefill=preset.keys.get("ubatch-size") == "2048" and mtp))
- need = profile.weights_bytes + ctx_bytes(profile, preset.window) + overhead
+ posture = posture_profile(profile, mtp_capable=mtp,
+ mtp_prefill=preset.keys.get("ubatch-size") == "2048" and mtp)
+ need = profile.weights_bytes + ctx_bytes(posture, preset.window) + overhead
assert preset.spilled == (need > machine.usable_vram_bytes)
assert need <= machine.usable_vram_bytes + machine.ram_available_bytes
diff --git a/tests/hermes_cli/test_local_recommendation.py b/tests/hermes_cli/test_local_recommendation.py
index 3626b6e13d9..6dcf03ef221 100644
--- a/tests/hermes_cli/test_local_recommendation.py
+++ b/tests/hermes_cli/test_local_recommendation.py
@@ -71,7 +71,8 @@ def _unified(size_gb: int) -> HardwareBudget:
# - 96 GB and up, discrete or unified: Flash Next fits resident. Its
# IQ4_XS build loads ~60 GiB — the engine reads the 26.8 GiB per-layer
# embedding table from disk on demand — so with its MTP head and
-# projector it needs ~74 GiB even at the native 256K window. It is the
+# projector it holds the native 256K window in ~83 GiB on 128 GB, and
+# 144K on 96 GB. It is the
# pick: highest quality, and its sparse decode clears the floor even at
# UMA bandwidth (~28 tok/s predicted at 210 GB/s).
# - Unified 32-48 GB: the dense 27B predicts ~13 tok/s at UMA bandwidth
diff --git a/tests/hermes_cli/test_local_window_compute.py b/tests/hermes_cli/test_local_window_compute.py
new file mode 100644
index 00000000000..729b039f5e3
--- /dev/null
+++ b/tests/hermes_cli/test_local_window_compute.py
@@ -0,0 +1,54 @@
+"""Compute buffers that grow with the window are priced at the launch posture.
+
+Qwen4Exp's QSA attention scores the whole window each microbatch. Under the router's unified KV
+(``--parallel`` auto: every slot sees the full window) llama.cpp b11370 reserved ~46 bytes per
+microbatch token per window token for each context, device plus host, and an MTP head is a second
+context. At 256K that is 5.9 GiB per context at ``-ub 512`` and ~23 GiB at ``-ub 2048``; priced as
+logits alone, the prefill posture loaded the target and then ran out of memory creating the MTP
+context on a 128 GB DGX Spark.
+"""
+
+from __future__ import annotations
+
+from hermes_cli.local_runtime.catalog import catalog_by_id
+from hermes_cli.local_runtime.context_policy import launch_args, posture_profile
+from hermes_cli.local_runtime.estimator import HardwareBudget, footprint_bytes
+
+GIB = 1 << 30
+MIB = 1 << 20
+# A 128 GB GB10 (DGX Spark) as the probe budgets it: the CUDA pool, 20% headroom, lazy reads.
+SPARK = HardwareBudget(usable_vram_bytes=int(121.7 * GIB * 0.8), total_device_bytes=int(121.7 * GIB),
+ ram_available_bytes=0, uma=True)
+# What b11370 allocated for Flash Next UD-IQ4_XS + ggml-org's Q8_0 head + the BF16 projector at
+# -c 262144, -ub 512, unified KV, q8_0 cache, --lazy-mode on (MiB, from its own buffer logs).
+MEASURED_256K_LEAN = (61222.13 + 644.14 # target weights, device + host
+ + 3291.18 + 644.14 # MTP head weights
+ + 865.5 + 248.1 # projector + its compute buffer
+ + 3264 + 816 + 512 + 128 # target and head KV
+ + 5111.55 + 798.45 # target compute, device + host
+ + 5053.94 + 808.15) * MIB # head compute, device + host
+
+
+def test_flash_next_on_a_128_gb_spark_runs_256k_at_the_lean_microbatch_and_is_priced_as_measured():
+ entry = catalog_by_id()["qwen3.8-flash-next"]
+ variant = entry.variants[0]
+
+ plan = entry.launch_plan(variant, SPARK)
+ args = launch_args(plan.profile, plan.decision, mtp_capable=True, uma=True,
+ mtp_prefill=plan.mtp_prefill)
+ priced = footprint_bytes(plan.profile, plan.decision.window, overhead_bytes=plan.overhead_bytes)
+
+ assert plan.decision.window == 262144 and not plan.decision.spilled
+ assert "-ub" not in args
+ assert MEASURED_256K_LEAN <= priced <= MEASURED_256K_LEAN * 1.1
+
+
+def test_the_prefill_microbatch_pays_four_times_the_window_compute_of_the_lean_one():
+ entry = catalog_by_id()["qwen3.8-flash-next"]
+ profile = entry.profile(entry.variants[0])
+
+ lean = posture_profile(profile, mtp_capable=True, mtp_prefill=False)
+ stacked = posture_profile(profile, mtp_capable=True, mtp_prefill=True)
+
+ assert stacked.window_compute_per_token == 4 * lean.window_compute_per_token
+ assert lean.window_compute_per_token == 46 * 512 * 2