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

@ -41,9 +41,13 @@ QUIET_PATHS = {"/health", "/v1/models", "/llmhost/metrics"}
NOISE = (b'"GET /health HTTP', b"- INFO - HTTP Request: ", b"sending unauthenticated requests to the HF Hub")
def _pump_output(stream):
FATAL = b"generation thread died"
def _pump_output(stream, on_fatal=None):
"""Copy the child's output to ours, dropping noise. Splits on \r too so download
progress bars still update instead of being held back until a newline."""
progress bars still update instead of being held back until a newline. Calls on_fatal
when mlx_lm.server reports its generator died (it keeps running but can't answer)."""
buf = b""
out = sys.stdout.buffer
while True:
@ -59,6 +63,9 @@ def _pump_output(stream):
if not any(n in seg for n in NOISE):
out.write(seg)
out.flush()
if on_fatal and FATAL in seg:
on_fatal()
on_fatal = None
if buf:
out.write(buf)
out.flush()
@ -278,7 +285,12 @@ def run_server(cfg, entry, choice, args):
exe = str(VENV / "bin/mlx_lm.server")
child = subprocess.Popen([exe, *args], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
threading.Thread(target=_pump_output, args=(child.stdout,), daemon=True).start()
def generator_died():
# Every request fails from here on, so exit and let the service manager restart us.
print("llm-host: mlx_lm.server's generator crashed (often: out of GPU memory); restarting it",
flush=True)
child.terminate()
threading.Thread(target=_pump_output, args=(child.stdout, generator_died), daemon=True).start()
threading.Thread(target=_readiness_probe, args=(inner, stats), daemon=True).start()
def stop(*_):