MLX: cap KV caches and recover from crashes; add llmhost watch

- mlx_lm.server kept up to 10 conversations' KV caches with no byte
  limit and prefilled 8 prompts at once, which ran a 16 GB Mac out of
  GPU memory. The plan now gives the live request half the free memory
  (at least 32k tokens), caps --prompt-cache-bytes to the rest, and on
  small machines prefills one prompt at a time.
- When its generation thread dies, mlx_lm.server keeps running but fails
  every request. The proxy now restarts it.
- llmhost watch: one live line with download progress (bytes, speed,
  time left), then loading, until the model answers. Setup and switch
  point at it instead of llmhost logs -f.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QaE1hpvyZTJixEbUUMj9Ca
This commit is contained in:
neil 2026-11-02 07:45:12 -08:00
parent 0b2cdcf513
commit 8611d92cf4
5 changed files with 131 additions and 6 deletions

View file

@ -71,8 +71,17 @@ def plan(hw, catalog, prefer="balanced"):
free = total * util - w - _overhead_gib(w, tp)
ctx = int(free * 2**20 / kv_kib) if free > 0 else 0
if ctx >= MIN_CONTEXT:
est = min(ctx, m["context"])
choice = {"format": fmt, "repo": v["repo"], "weights_gib": round(w, 1),
"util": util, "est_context": min(ctx, m["context"]), "mtp": bool(v.get("mtp"))}
"util": util, "est_context": est, "mtp": bool(v.get("mtp"))}
if kind == "apple":
# mlx_lm.server also keeps finished conversations' KV caches for reuse.
# Give the live request half the free memory (at least MIN_CONTEXT) and
# cap the reuse cache to the rest, so the two can't exhaust GPU memory.
est = min(max(MIN_CONTEXT, ctx // 2), m["context"])
cache = free - est * kv_kib / 2**20
choice.update(est_context=est, free_gib=round(free, 2),
prompt_cache_gib=round(max(cache, 0.25), 2))
break
if choice:
break
@ -143,5 +152,11 @@ def mlx_args(entry, choice, port):
"--max-tokens", "32768"] # mlx_lm defaults to 512, which truncates coding answers
if x.get("trust_remote_code"):
args += ["--trust-remote-code"]
if choice.get("prompt_cache_gib"):
# Unset, mlx_lm keeps up to 10 conversations' caches with no byte limit (OOM on small Macs).
args += ["--prompt-cache-bytes", str(int(choice["prompt_cache_gib"] * 2**30))]
if choice.get("free_gib", 99) < 8:
# Little room beyond the weights: prefill one prompt at a time (default 8 in parallel).
args += ["--prompt-concurrency", "1", "--decode-concurrency", "4"]
args += x.get("extra_args", [])
return args