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:
parent
0b2cdcf513
commit
8611d92cf4
5 changed files with 131 additions and 6 deletions
|
|
@ -186,7 +186,7 @@ Done.
|
|||
API: http://{host}:{cfg['api_port']}/v1 (OpenAI-compatible)
|
||||
Dashboard: http://{host}:{cfg['web_port']}
|
||||
API key: {paths.KEY_FILE} (show it with: llmhost key)
|
||||
Model: {active} — {"not started yet; run: llmhost start" if a.no_start else ("loading" if hfcache.is_downloaded(first["repo"]) else "downloading, then loading") + "; watch with: llmhost logs -f"}""")
|
||||
Model: {active} — {"not started yet; run: llmhost start" if a.no_start else ("loading" if hfcache.is_downloaded(first["repo"]) else "downloading, then loading") + "; watch progress with: llmhost watch"}""")
|
||||
if not a.no_opencode:
|
||||
setup_opencode(cfg, ask=sys.stdin.isatty() and not a.yes, install_ok=a.opencode)
|
||||
say(f"\nOther machines: run `llmhost opencode-config` here for their opencode setup.")
|
||||
|
|
@ -342,7 +342,7 @@ def cmd_switch(a):
|
|||
cfg["active"] = a.model
|
||||
paths.save_config(cfg)
|
||||
service.restart("model")
|
||||
say(f"Switched to {a.model}. Watch it load with: llmhost logs -f")
|
||||
say(f"Switched to {a.model}. Watch it load with: llmhost watch")
|
||||
|
||||
|
||||
def cmd_ctl(a):
|
||||
|
|
@ -361,6 +361,87 @@ def cmd_logs(a):
|
|||
say(line)
|
||||
|
||||
|
||||
def _served_models(cfg, key):
|
||||
try:
|
||||
if urllib.request.urlopen(f"http://127.0.0.1:{cfg['api_port']}/health", timeout=2).status != 200:
|
||||
return []
|
||||
req = urllib.request.Request(f"http://127.0.0.1:{cfg['api_port']}/v1/models",
|
||||
headers={"Authorization": f"Bearer {key}"})
|
||||
return [m["id"] for m in json.load(urllib.request.urlopen(req, timeout=3))["data"]]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
|
||||
def _fmt_secs(t):
|
||||
t = int(t)
|
||||
return f"{t // 3600}h{t % 3600 // 60:02d}m" if t >= 3600 else (f"{t // 60}m{t % 60:02d}s" if t >= 60 else f"{t}s")
|
||||
|
||||
|
||||
def cmd_watch(a):
|
||||
"""One live status line: download progress, then loading, until the model answers."""
|
||||
cfg = _service_check()
|
||||
key = paths.api_key()
|
||||
name = cfg["active"]
|
||||
m = next(x for x in cfg["models"] if x["name"] == name)
|
||||
tty = sys.stdout.isatty()
|
||||
start = time.time()
|
||||
samples = [] # (time, GiB) over the last ~10 s, for the download rate
|
||||
down_since = None # when the service was first seen stopped (it may just be restarting)
|
||||
last_plain = (None, 0)
|
||||
i = 0
|
||||
|
||||
def show(text, key_phase):
|
||||
nonlocal last_plain
|
||||
if tty:
|
||||
sys.stdout.write("\r\033[K" + text)
|
||||
sys.stdout.flush()
|
||||
elif key_phase != last_plain[0] or time.time() - last_plain[1] >= 15:
|
||||
print(text, flush=True)
|
||||
last_plain = (key_phase, time.time())
|
||||
|
||||
def done(text, rc):
|
||||
if tty:
|
||||
sys.stdout.write("\r\033[K")
|
||||
print(text, flush=True)
|
||||
raise SystemExit(rc)
|
||||
|
||||
try:
|
||||
while True:
|
||||
now = time.time()
|
||||
if name in _served_models(cfg, key):
|
||||
done(f"\u2713 {name} is ready: http://{default_host()}:{cfg['api_port']}/v1 "
|
||||
f"({_fmt_secs(now - start)})", 0)
|
||||
st, _ = service.state("model")
|
||||
if st in ("inactive", "failed"):
|
||||
down_since = down_since or now
|
||||
if now - down_since > 20:
|
||||
lines = [l for l in service.logs("model", 400) if l.strip()][-15:]
|
||||
done(f"The model server is {'not running' if st == 'inactive' else 'failing'}. "
|
||||
f"Last log lines:\n " + "\n ".join(lines) +
|
||||
"\nFull log: llmhost logs" + ("" if st == "failed" else " Start it: llmhost start"), 1)
|
||||
else:
|
||||
down_since = None
|
||||
frame = Spinner.FRAMES[i % len(Spinner.FRAMES)] if tty else "-"
|
||||
i += 1
|
||||
total = m.get("weights_gib") or 0
|
||||
if total and not hfcache.is_downloaded(m["repo"]):
|
||||
got = hfcache.download_gib(m["repo"])
|
||||
samples = [x for x in samples if now - x[0] <= 10] + [(now, got)]
|
||||
rate = (got - samples[0][1]) / (now - samples[0][0]) if now > samples[0][0] else 0
|
||||
frac = min(got / total, 0.999)
|
||||
bar = "\u2588" * int(frac * 24) + "\u2591" * (24 - int(frac * 24))
|
||||
extra = ""
|
||||
if rate > 0.001:
|
||||
extra = f" \u00b7 {rate * 1024 * 2**20 / 1e6:.0f} MB/s \u00b7 ~{_fmt_secs((total - got) / rate)} left"
|
||||
show(f"{frame} Downloading {name} {bar} {got:.1f} / {total:.1f} GiB ({frac:.0%}){extra}",
|
||||
"download")
|
||||
else:
|
||||
show(f"{frame} Loading {name} ({_fmt_secs(now - start)})", "load")
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
done("Stopped watching (the model server keeps going).", 130)
|
||||
|
||||
|
||||
def cmd_key(a):
|
||||
say(paths.api_key())
|
||||
|
||||
|
|
@ -531,6 +612,8 @@ def main(argv=None):
|
|||
s.add_argument("-n", type=int, default=100)
|
||||
s.add_argument("--web", action="store_true", help="dashboard logs instead")
|
||||
s.set_defaults(fn=cmd_logs)
|
||||
sub.add_parser("watch", help="show download and loading progress until the model is ready"
|
||||
).set_defaults(fn=cmd_watch)
|
||||
sub.add_parser("key", help="print the API key").set_defaults(fn=cmd_key)
|
||||
s = sub.add_parser("download", help="download a model ahead of time")
|
||||
s.add_argument("model")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue