Skip to content

The agent that grows with you

fix(local-runtime): MTP and the large microbatch must not stack — choose per model, price by vocab

emozillab92433993fa0
diff --git a/hermes_cli/local_runtime/catalog.py b/hermes_cli/local_runtime/catalog.py
index 8f25ecdb85f..953abc2be6b 100644
--- a/hermes_cli/local_runtime/catalog.py
+++ b/hermes_cli/local_runtime/catalog.py
@@ -52,6 +52,7 @@ from hermes_cli.local_runtime.context_policy import (
FLOOR,
RUNTIME_OVERHEAD_BYTES,
TARGET_WINDOW,
+ ub_logits_bytes,
)
from hermes_cli.local_runtime.estimator import (
HardwareBudget,
@@ -131,6 +132,10 @@ class CatalogEntry:
# 243.8 tok/s, 59% at 3 -> 221.6; Nemotron holds at 3). NVIDIA's
# per-SKU recipes seeded these; our receipts confirmed them.
mtp_draft_depth: int = 3
+ # Vocab size prices the GPU logits buffers (ubatch x vocab x fp32,
+ # doubled under MTP backend sampling) — a 248K-vocab model once packed
+ # a card 3.9 GiB past a fit that ignored this.
+ n_vocab: int = 0
mmproj: "AssetFile | None" = None # vision projector, downloads with model
draft: "AssetFile | None" = None # spec-decode draft model (e.g. DSpark)
sampling: dict = field(default_factory=dict) # INI long-form launch defaults
@@ -143,7 +148,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)
+ layers=layers, swa_window=self.swa_window, moe=self.moe,
+ n_vocab=self.n_vocab)
def download_files(self, variant: QuantVariant) -> tuple:
"""Everything a download job fetches for this variant, in order."""
@@ -181,7 +187,9 @@ def select_variant(entry: CatalogEntry, budget: HardwareBudget) -> VariantChoice
between adjacent rungs are small; 64K vs 216K changes what a session
can do), but nothing outranks full residency.
"""
- overhead = RUNTIME_OVERHEAD_BYTES + (entry.mmproj.size_bytes if entry.mmproj else 0)
+ overhead = (RUNTIME_OVERHEAD_BYTES
+ + (entry.mmproj.size_bytes if entry.mmproj else 0)
+ + ub_logits_bytes(entry.n_vocab, mtp_capable=entry.mtp))
native = entry.n_ctx_train or FLOOR
floor_kv = None
target_kv = None
@@ -245,6 +253,7 @@ CATALOG: tuple[CatalogEntry, ...] = (
# 256 head_dim = 4 KiB/token per full layer). The GGUF header is
# the authority after download.
full_layers=16, recurrent_layers=48, per_layer_f16=4096,
+ n_vocab=248320,
mmproj=AssetFile("mmproj-BF16.gguf", 931146432,
"83ee4f4f205fa514161778c41df1ea14144faa0f713510893b63c2395f5c2d53",
local="mmproj-Qwen3.8-27B-BF16.gguf"),
@@ -278,7 +287,7 @@ CATALOG: tuple[CatalogEntry, ...] = (
# Config-derived (Qwen/Qwen3.6-35B-A3B): 40 layers, 10 full-attn
# (interval 4) + 30 linear; KV heads 2 x head_dim 256.
full_layers=10, recurrent_layers=30, per_layer_f16=2048,
- moe=True, mtp=True, mtp_draft_depth=2,
+ moe=True, mtp=True, mtp_draft_depth=2, n_vocab=248320,
mmproj=AssetFile("mmproj-BF16.gguf", 902822528,
"da63cb47a76763c712393f8a017070188a304fa39f8aeea6edc629ed7b975cfa",
local="mmproj-Qwen3.6-35B-A3B-BF16.gguf"),
@@ -311,7 +320,7 @@ CATALOG: tuple[CatalogEntry, ...] = (
# KV on the predecessor). GGUF header is
# the authority after download.
full_layers=6, recurrent_layers=46, per_layer_f16=1024,
- moe=True, mtp=True,
+ moe=True, mtp=True, n_vocab=131072,
sampling={"temp": "0.6", "top-p": "0.95", "min-p": "0.01"},
tags=("day-0", "long-context", "hybrid", "moe", "mtp"),
),
@@ -340,6 +349,7 @@ CATALOG: tuple[CatalogEntry, ...] = (
# overestimating here keeps the zero-spill promise safe until the
# GGUF header corrects it.
full_layers=60, recurrent_layers=0, per_layer_f16=4096,
+ n_vocab=202048,
mmproj=AssetFile("mmproj-Muse-Glimmer-30B-BF16.gguf", 3849173728,
"d08cdcfa0b41d8e20554b52df404ba4f7b440d0bc502a90038508b6407df8ee1"),
sampling={"temp": "1.0", "top-p": "0.95", "top-k": "64"},
@@ -382,7 +392,7 @@ CATALOG: tuple[CatalogEntry, ...] = (
# Config shows sliding_window=128 with no per-layer map — priced
# all-full (conservative); GGUF header decides after download.
full_layers=43, recurrent_layers=0, per_layer_f16=1152,
- moe=True,
+ moe=True, n_vocab=163840,
draft=AssetFile("dspark-DeepSeek-V4-Flash-0731-Q8_0.gguf", 10896057440,
"2c7ac54b0b64a99df1f139a9f1371a00198265e1d6a614b77597d20a655a4249"),
sampling={"temp": "1.0", "top-p": "0.95", "min-p": "0.01"},
diff --git a/hermes_cli/local_runtime/context_policy.py b/hermes_cli/local_runtime/context_policy.py
index 4d23e19b88f..bdc8d171f0b 100644
--- a/hermes_cli/local_runtime/context_policy.py
+++ b/hermes_cli/local_runtime/context_policy.py
@@ -59,15 +59,14 @@ _EARLY_COST_CTX_FRACTION = 0.15 # bounded early cost when weights spill
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
-# shaved a layer to CPU — ~1.5 GiB of runtime overhead plus the vision
-# projector when present. The -ub 2048 microbatch adds ~0.4 GiB of compute
-# buffer over the 512 default (measured: +367 MiB on the 35B) — the prefill
-# hint and this constant move together or the fit lies. A fit that ignores
-# this passes on paper and spills in practice. Callers add mmproj bytes on
+# buffers at the DEFAULT microbatch (-ub 512, no MTP). 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. Microbatch/MTP logits buffers are priced separately per model
+# (ub_logits_bytes — they scale with the model's vocab and doubled once
+# packed a card 3.9 GiB past this constant). Callers add mmproj bytes on
# top.
-RUNTIME_OVERHEAD_BYTES = int(1.9 * (1 << 30))
+RUNTIME_OVERHEAD_BYTES = int(1.5 * (1 << 30))
def ladder(native: int) -> list[int]:
@@ -229,16 +228,33 @@ def launch_args(profile: ModelProfile, decision: WindowDecision, *,
mtp_draft_depth: int = 3) -> list[str]:
"""Per-model launch flags from a window decision. Explicit -c puts fit
into spill-weights-and-hold-ctx; q8 KV cache wherever flash attention
- exists; -ot placement on spilled configs; MTP spec decode wherever the
- model ships it (resident decode measured +16% at depth 2 on the 35B —
- the old spilled-only gate left that on the table); large microbatch
- for prefill (+27% measured at -ub 2048, priced in RUNTIME_OVERHEAD)."""
- args = ["-c", str(decision.window), "-b", "2048", "-ub", "2048"]
+ exists; -ot placement on spilled configs.
+
+ MTP and the large prefill microbatch are BOTH wins but must not stack:
+ backend sampling keeps a ubatch x vocab x fp32 logits buffer on the
+ GPU, and MTP's draft context doubles it — measured 5.8 GiB of overhead
+ on a 248K-vocab model at -ub 2048 vs 1.9 priced, which silently packed
+ a 32 GiB card. So MTP models run decode-optimized (-ub 512, measured
+ 247 tok/s) and non-MTP models run prefill-optimized (-ub 2048,
+ measured +27%); ub_logits_bytes() prices whichever was chosen."""
+ args = ["-c", str(decision.window)]
+ if mtp_capable:
+ args += ["--spec-type", "draft-mtp",
+ "--spec-draft-n-max", str(mtp_draft_depth),
+ "--backend-sampling"]
+ else:
+ args += ["-b", "2048", "-ub", "2048"]
if flash_attention:
args += ["-ctk", "q8_0", "-ctv", "q8_0", "-fa", "on"]
if decision.spilled:
args += spill_overrides(profile)
- if mtp_capable:
- args += ["--spec-type", "draft-mtp",
- "--spec-draft-n-max", str(mtp_draft_depth)]
return args
+
+
+def ub_logits_bytes(n_vocab: int, *, mtp_capable: bool) -> 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 = 512 if mtp_capable else 2048
+ return ubatch * max(0, int(n_vocab)) * 4 * (2 if mtp_capable else 1)
diff --git a/hermes_cli/local_runtime/estimator.py b/hermes_cli/local_runtime/estimator.py
index 72c01070bbf..8df6dcd3136 100644
--- a/hermes_cli/local_runtime/estimator.py
+++ b/hermes_cli/local_runtime/estimator.py
@@ -60,6 +60,7 @@ class ModelProfile:
swa_window: int = 0
moe: bool = False
architecture: str = ""
+ n_vocab: int = 0 # prices logits buffers (ubatch x vocab)
@property
def per_token_kv_f16(self) -> int:
@@ -118,6 +119,7 @@ def profile_from_gguf(header: GGUFHeader) -> ModelProfile:
swa_window=header.sliding_window,
moe=header.expert_count > 0,
architecture=header.architecture,
+ n_vocab=header.n_vocab,
)
diff --git a/hermes_cli/local_runtime/gguf.py b/hermes_cli/local_runtime/gguf.py
index fa3e172e9da..c716514f8d5 100644
--- a/hermes_cli/local_runtime/gguf.py
+++ b/hermes_cli/local_runtime/gguf.py
@@ -63,6 +63,17 @@ class GGUFHeader:
def n_layer(self) -> int:
return int(self._arch_key("block_count") or 0)
+ @property
+ def n_vocab(self) -> int:
+ """Vocabulary size: prices the GPU logits buffers (they scale
+ ubatch x vocab). vocab_size metadata when present, else the
+ tokenizer list length."""
+ v = self._arch_key("vocab_size")
+ if v:
+ return int(v)
+ toks = self.metadata.get("tokenizer.ggml.tokens")
+ return len(toks) if isinstance(toks, list) else 0
+
@property
def n_ctx_train(self) -> int:
return int(self._arch_key("context_length") or 0)
diff --git a/hermes_cli/local_runtime/presets.py b/hermes_cli/local_runtime/presets.py
index 49ab4f92f55..977a14b4e96 100644
--- a/hermes_cli/local_runtime/presets.py
+++ b/hermes_cli/local_runtime/presets.py
@@ -17,6 +17,7 @@ from hermes_cli.local_runtime.context_policy import (
WindowDecision,
initial_window,
launch_args,
+ ub_logits_bytes,
)
from hermes_cli.local_runtime.estimator import (
HardwareBudget,
@@ -90,18 +91,23 @@ def generate_presets(models_dir: Path, budget: HardwareBudget,
except (ValueError, OSError) as exc:
logger.warning("preset skip %s: %s", gguf.name, exc)
continue
- # Overhead beyond weights+KV: runtime buffers, plus the vision
- # projector when this model ships one (it loads beside the weights).
+ # Overhead beyond weights+KV: runtime buffers, the vision projector
+ # when this model ships one, and the logits buffers of whichever
+ # microbatch/MTP posture launch_args will choose — flag and price
+ # decided together, from the same facts.
hit = find_entry_for_model(model_id)
entry = hit[0] if hit is not None else None
+ is_mtp = (entry.mtp if entry is not None
+ else model_id in (mtp_capable or set()))
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
+ logits_bytes = ub_logits_bytes(profile.n_vocab, mtp_capable=is_mtp)
decision = initial_window(
profile, budget,
- overhead_bytes=RUNTIME_OVERHEAD_BYTES + mmproj_bytes)
+ overhead_bytes=RUNTIME_OVERHEAD_BYTES + mmproj_bytes + logits_bytes)
if isinstance(decision, PhysicsRefusal):
entries.append(PresetEntry(model_id=model_id, window=0,
spilled=False, refusal=decision.message))
@@ -121,7 +127,7 @@ def generate_presets(models_dir: Path, budget: HardwareBudget,
target = min(int(override), native)
kv = ctx_bytes(profile, target)
need = (profile.weights_bytes + kv
- + RUNTIME_OVERHEAD_BYTES + mmproj_bytes)
+ + RUNTIME_OVERHEAD_BYTES + mmproj_bytes + logits_bytes)
if need <= budget.usable_vram_bytes + budget.ram_available_bytes:
spill = max(0, need - budget.usable_vram_bytes)
decision = WindowDecision(
@@ -131,13 +137,8 @@ def generate_presets(models_dir: Path, budget: HardwareBudget,
except Exception as exc: # noqa: BLE001 — overrides are advisory
logger.debug("window override skipped for %s: %s", model_id, exc)
- hit = find_entry_for_model(model_id)
- entry = hit[0] if hit is not None else None
-
- # MTP: catalog knowledge wins (per-model measured draft depth);
- # the caller's mtp_capable set covers non-catalog GGUFs.
- is_mtp = (entry.mtp if entry is not None
- else model_id in (mtp_capable or set()))
+ # (entry and is_mtp resolved above, where the overhead was priced —
+ # the launch flags below MUST match that pricing.)
args = launch_args(profile, decision, mtp_capable=is_mtp,
mtp_draft_depth=(entry.mtp_draft_depth
if entry is not None else 3))
diff --git a/hermes_cli/web_routers/local_models.py b/hermes_cli/web_routers/local_models.py
index 8f3de0fbea6..e54a391b855 100644
--- a/hermes_cli/web_routers/local_models.py
+++ b/hermes_cli/web_routers/local_models.py
@@ -450,6 +450,7 @@ async def local_models_catalog():
from hermes_cli.local_runtime.context_policy import (
RUNTIME_OVERHEAD_BYTES,
initial_window,
+ ub_logits_bytes,
)
from hermes_cli.local_runtime.estimator import PhysicsRefusal
from hermes_cli.local_runtime.hardware import probe_budget
@@ -496,11 +497,12 @@ async def local_models_catalog():
variant = choice.variant
profile = entry.profile(variant)
# Same overhead the launch decision prices (runtime buffers +
- # vision projector): the row must advertise the window the model
- # will actually get, not a paper number the server's own fit then
- # shaves down.
- overhead = RUNTIME_OVERHEAD_BYTES + (
- entry.mmproj.size_bytes if entry.mmproj else 0)
+ # vision projector + the microbatch/MTP logits buffers): the row
+ # must advertise the window the model will actually get, not a
+ # paper number the server's own fit then shaves down.
+ overhead = (RUNTIME_OVERHEAD_BYTES
+ + (entry.mmproj.size_bytes if entry.mmproj else 0)
+ + ub_logits_bytes(entry.n_vocab, mtp_capable=entry.mtp))
decision = initial_window(profile, budget, overhead_bytes=overhead)
download_total = entry.download_bytes(variant)
row.update({
diff --git a/tests/hermes_cli/test_catalog_variants.py b/tests/hermes_cli/test_catalog_variants.py
index c791af325db..ed7e8beb068 100644
--- a/tests/hermes_cli/test_catalog_variants.py
+++ b/tests/hermes_cli/test_catalog_variants.py
@@ -129,9 +129,10 @@ def test_floor_fallback_when_no_variant_reaches_target():
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))
+ # ~24.5 GiB usable: Q4 weights (16.7 GiB in-memory) + floor KV (2.2)
+ # + overhead (1.5 + 0.9 mmproj + 1.9 vocab logits) fits, but no
+ # variant reaches 144K-target KV (+2.7 more for Q4, more for Q5+).
+ choice = select_variant(entry, budget(24.5))
assert choice is not None and choice.zero_spill
assert choice.reason_key == "best-fits"
assert choice.variant.quant == "UD-Q4_K_XL"
diff --git a/tests/hermes_cli/test_context_policy.py b/tests/hermes_cli/test_context_policy.py
index cb330a2245b..f5ce333c254 100644
--- a/tests/hermes_cli/test_context_policy.py
+++ b/tests/hermes_cli/test_context_policy.py
@@ -20,6 +20,7 @@ from hermes_cli.local_runtime.context_policy import (
ladder,
launch_args,
spill_overrides,
+ ub_logits_bytes,
)
from hermes_cli.local_runtime.estimator import (
HardwareBudget,
@@ -312,22 +313,41 @@ def test_launch_args_contract():
a = launch_args(p, spilled, mtp_capable=True)
assert a[:2] == ["-c", str(FLOOR)] # explicit window, always
- assert "-ub" in a and a[a.index("-ub") + 1] == "2048" # prefill hint
assert "q8_0" in a # q8 KV under flash attn
assert "-ot" in a # spill placement
assert "--spec-type" in a # MTP on spilled
- # MTP is no longer gated on spill: resident decode measured +16% at
- # depth 2 — spec decode runs wherever the model ships MTP heads.
+ # MTP is not gated on spill: resident decode measured +16% at depth 2.
b = launch_args(p, resident, mtp_capable=True, mtp_draft_depth=2)
assert "-ot" not in b, "placement is spill-only"
assert "--spec-type" in b, "MTP must run on resident configs too"
assert b[b.index("--spec-draft-n-max") + 1] == "2"
+ assert "--backend-sampling" in b
- c = launch_args(p, spilled, flash_attention=False, mtp_capable=False)
- assert "q8_0" not in c # f16 on non-FA fallback
+ # MTP and the large microbatch are mutually exclusive: backend
+ # sampling keeps ubatch x vocab fp32 logits on the GPU and MTP
+ # doubles it — stacked, they packed a 32 GiB card 3.9 GiB past the
+ # priced overhead. MTP configs stay at the default ubatch.
+ assert "-ub" not in b, "MTP configs must not carry the large microbatch"
+
+ c = launch_args(p, resident, mtp_capable=False)
+ assert "-ub" in c and c[c.index("-ub") + 1] == "2048" # prefill hint
assert "--spec-type" not in c
+ d = launch_args(p, spilled, flash_attention=False, mtp_capable=False)
+ assert "q8_0" not in d # f16 on non-FA fallback
+
+
+def test_ub_logits_bytes_prices_the_flag_choice():
+ """The logits-buffer price must match the microbatch launch_args
+ chooses: 2048 x vocab x 4 for non-MTP, 512 x vocab x 4 x 2 for MTP
+ (draft context doubles it). 248320-vocab receipts: ~1.9 GiB at
+ ub2048, ~0.95 GiB under MTP."""
+ v = 248320
+ assert ub_logits_bytes(v, mtp_capable=False) == 2048 * v * 4
+ assert ub_logits_bytes(v, mtp_capable=True) == 512 * v * 4 * 2
+ assert ub_logits_bytes(0, mtp_capable=True) == 0 # unknown vocab: no charge
+
def test_no_refusal_branch_past_physics():
"""Design invariant: anything past the physics check is servable —