diff --git a/llmhost/backends/mlx.py b/llmhost/backends/mlx.py index 1bda3e8..06de6e6 100644 --- a/llmhost/backends/mlx.py +++ b/llmhost/backends/mlx.py @@ -97,12 +97,15 @@ class Stats: self.gen_total = 0 self.prompt_total = 0 self.requests_total = 0 + self.ttft_sum = 0.0 # seconds from request received to first generated token, + self.ttft_count = 0 # streamed requests only (non-streamed ones arrive all at once) self.ready = False # set once the model has actually answered (see _readiness_probe) def snapshot(self): with self.lock: return {"running": self.running, "gen_total": self.gen_total, - "prompt_total": self.prompt_total, "requests_total": self.requests_total} + "prompt_total": self.prompt_total, "requests_total": self.requests_total, + "ttft_sum": self.ttft_sum, "ttft_count": self.ttft_count} def make_handler(inner_port, served_name, context, key, stats): @@ -152,6 +155,7 @@ def make_handler(inner_port, served_name, context, key, stats): def do_POST(self): if not self._authed(): return + self._t0 = time.monotonic() n = int(self.headers.get("Content-Length") or 0) raw = self.rfile.read(n) if n else b"" strip_usage = False @@ -196,6 +200,7 @@ def make_handler(inner_port, served_name, context, key, stats): self.end_headers() self.close_connection = True buf = b"" + self._first = False while True: chunk = resp.read1(65536) if hasattr(resp, "read1") else resp.read(4096) if not chunk: @@ -239,6 +244,7 @@ def make_handler(inner_port, served_name, context, key, stats): except ValueError: return False u = d.get("usage") + first_token = False with stats.lock: if u: # final usage chunk (stream_options.include_usage) is exact; prefer it stats.prompt_total += int(u.get("prompt_tokens") or 0) @@ -246,6 +252,11 @@ def make_handler(inner_port, served_name, context, key, stats): delta = c.get("delta") or {} if delta.get("content") or delta.get("reasoning") or delta.get("reasoning_content") or delta.get("tool_calls"): stats.gen_total += 1 # mlx streams one token per chunk + first_token = True + if first_token and not self._first and getattr(self, "_t0", None) is not None: + self._first = True + stats.ttft_sum += time.monotonic() - self._t0 + stats.ttft_count += 1 return bool(u) and not d.get("choices") return Proxy diff --git a/llmhost/web/index.html b/llmhost/web/index.html index 781340e..70f5bf5 100644 --- a/llmhost/web/index.html +++ b/llmhost/web/index.html @@ -11,7 +11,7 @@ --accent: #3b6ef5; --accent-weak: #e6edfe; --ok: #1f9d55; --ok-weak: #e3f5ea; --warn: #b7791f; --warn-weak: #fbf1dc; --bad: #d64545; --bad-weak: #fbe5e5; - --c1: #3b6ef5; --c2: #0f9d8a; --c3: #c2620f; --c4: #d13d5e; --c5: #8a5cf0; --c6: #2f9e44; + --c1: #3b6ef5; --c2: #0f9d8a; --c3: #c2620f; --c4: #d13d5e; --c5: #8a5cf0; --c6: #2f9e44; --c7: #b5179e; --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color-scheme: light; } @@ -22,7 +22,7 @@ --accent: #6d93ff; --accent-weak: #1c2640; --ok: #3ccf7f; --ok-weak: #143022; --warn: #e3a94a; --warn-weak: #33280f; --bad: #f07272; --bad-weak: #3a1818; - --c1: #6d93ff; --c2: #3fc9b4; --c3: #f0954a; --c4: #f2708c; --c5: #a98bff; --c6: #5fd27a; + --c1: #6d93ff; --c2: #3fc9b4; --c3: #f0954a; --c4: #f2708c; --c5: #a98bff; --c6: #5fd27a; --c7: #e67ad1; color-scheme: dark; } } @@ -96,7 +96,9 @@ button.danger { color: var(--bad); } .bar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .4s; } .charts { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 14px; } -@media (max-width: 820px) { .charts { grid-template-columns: minmax(0, 1fr); } } +.charts.four { grid-template-columns: repeat(4, minmax(0, 1fr)); } +@media (max-width: 1100px) { .charts.four { grid-template-columns: repeat(2, minmax(0, 1fr)); } } +@media (max-width: 820px) { .charts, .charts.four { grid-template-columns: minmax(0, 1fr); } } .chart .head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; } .chart .head span { font-size: 12px; color: var(--muted); } .chart .head b { font-size: 13px; font-variant-numeric: tabular-nums; } @@ -171,6 +173,7 @@ pre {
Generation
—
Prompt processing
—
+
Time to first token
—
Requests
—
KV cache
—
GPU
—
@@ -179,9 +182,10 @@ pre {
Prefix cache hits
—
prompt tokens reused
-
+
Generation tok/s
10 min agonow
Prompt processing tok/s
10 min agonow
+
Time to first token
10 min agonow
KV cache %
10 min agonow
@@ -246,6 +250,7 @@ const fmt = { big: (v) => v == null ? "—" : v >= 1e9 ? (v / 1e9).toFixed(2) + "B" : v >= 1e6 ? (v / 1e6).toFixed(1) + "M" : v >= 1e3 ? (v / 1e3).toFixed(1) + "k" : String(Math.round(v)), dur: (s) => { if (s == null) return ""; s = Math.floor(s); const d = Math.floor(s / 86400), h = Math.floor(s % 86400 / 3600), m = Math.floor(s % 3600 / 60); return d ? `${d}d ${h}h` : h ? `${h}h ${m}m` : m ? `${m}m ${s % 60}s` : `${s}s`; }, + secs: (v) => v == null ? "—" : v < 1 ? Math.round(v * 1000) + " ms" : v < 10 ? v.toFixed(2) + " s" : v.toFixed(1) + " s", ctx: (n) => n == null ? "" : n >= 1024 ? Math.round(n / 1024) + "k ctx" : n + " ctx", }; @@ -324,6 +329,12 @@ function render(s) { $("t-gen-s").textContent = live && m.gen_total != null ? fmt.big(m.gen_total) + " generated" : ""; setV("t-prompt", live ? fmt.n(m.prompt_tps, 0) : "—", "tok/s"); $("t-prompt-s").textContent = live && m.prompt_total != null ? fmt.big(m.prompt_total) + " processed" : ""; + // Latest measured value, kept while idle (a request has to start for a new one). + const tt = m.ttft_last != null ? fmt.secs(m.ttft_last).split(" ") : null; + $("t-ttft").innerHTML = live && tt ? `${tt[0]}${tt[1]}` : "—"; + $("t-ttft-s").textContent = live && m.ttft_avg != null + ? `avg ${fmt.secs(m.ttft_avg)} · ${fmt.big(m.ttft_count)} req` + (s.backend === "mlx" ? " (streamed)" : "") + : live ? "no requests yet" : ""; $("t-req").innerHTML = live ? `${fmt.n(m.running)}running` : "—"; $("t-req-s").textContent = live ? `${fmt.n(m.waiting)} waiting` + (m.preemptions ? ` · ${fmt.n(m.preemptions)} preempted` : "") : ""; setV("t-kv", live ? fmt.n(m.kv_pct, 1) : "—", "%"); @@ -339,6 +350,7 @@ function render(s) { chartTimes = h.map((p) => p.t); chart("c-gen", h.map((p) => p.gen), "var(--c1)", null, (v) => fmt.n(v, 0) + " tok/s"); chart("c-prompt", h.map((p) => p.prompt), "var(--c6)", null, (v) => fmt.n(v, 0) + " tok/s"); + chart("c-ttft", h.map((p) => p.ttft), "var(--c7)", null, (v) => fmt.secs(v)); chart("c-util", h.map((p) => p.util), "var(--c2)", 100, (v) => fmt.pct(v)); chart("c-kv", h.map((p) => p.kv), "var(--c3)", null, (v) => fmt.n(v, 1) + "%"); // Fixed scales so the shape means something: 0–100 °C, and 0 to the board power limit. @@ -368,10 +380,15 @@ function chart(id, vals, color, fixedMax, f) { else { d += `L${x(i)},${y(v)}`; area += `L${x(i)},${y(v)}`; } }); if (segStart != null) area += `L${x(pts.length - 1)},${H}Z`; + let dots = ""; + pts.forEach((v, i) => { + if (v != null && pts[i - 1] == null && pts[i + 1] == null) dots += `M${x(i)},${y(v)}l0,0`; + }); svg.innerHTML = `` + `` + - ``; + `` + + (dots ? `` : ""); svg._data = { pts, times, color, f, x, y }; drawHover(svg); } diff --git a/llmhost/web/server.py b/llmhost/web/server.py index 76ff3a3..db20b16 100644 --- a/llmhost/web/server.py +++ b/llmhost/web/server.py @@ -62,6 +62,8 @@ def parse_vllm_metrics(text): "spec_accepted": vals.get("vllm:spec_decode_num_accepted_tokens_total"), "spec_drafted": vals.get("vllm:spec_decode_num_draft_tokens_total"), "preemptions": vals.get("vllm:num_preemptions_total"), + "ttft_sum": vals.get("vllm:time_to_first_token_seconds_sum"), + "ttft_count": vals.get("vllm:time_to_first_token_seconds_count"), } @@ -75,6 +77,7 @@ class Sampler: self.state = {} self.action = None self._dl_cache = (0, {}) + self.last_ttft = None # (time, seconds) of the latest sample with new first tokens def reload_cfg(self): self.cfg = paths.load_config() or self.cfg @@ -128,19 +131,26 @@ class Sampler: else: phase = "loading" - gen_rate = prompt_rate = None + gen_rate = prompt_rate = ttft = None if metrics.get("gen_total") is not None and self.prev: pt, pm = self.prev dt = now - pt if dt > 0 and pm.get("gen_total") is not None: gen_rate = max(metrics["gen_total"] - pm["gen_total"], 0) / dt prompt_rate = max((metrics.get("prompt_total") or 0) - (pm.get("prompt_total") or 0), 0) / dt + # Average time to first token of the requests that got their first token since the + # last sample (none: no point, so the chart shows a gap rather than a stale value). + dn = (metrics.get("ttft_count") or 0) - (pm.get("ttft_count") or 0) + if dn > 0: + ttft = max((metrics.get("ttft_sum") or 0) - (pm.get("ttft_sum") or 0), 0) / dn + if ttft is not None: + self.last_ttft = (now, ttft) self.prev = (now, metrics) if metrics else None gpu = hw.live_stats(cfg["kind"]) kv = metrics.get("kv") point = { - "t": now, "gen": gen_rate, "prompt": prompt_rate, + "t": now, "gen": gen_rate, "prompt": prompt_rate, "ttft": ttft, "kv": kv * 100 if kv is not None else None, "util": gpu["util"] if gpu else None, "temp": gpu["temp"] if gpu else None, @@ -176,6 +186,10 @@ class Sampler: "prefix_hit": ratio("prefix_hits", "prefix_queries"), "spec_accept": ratio("spec_accepted", "spec_drafted"), "preemptions": metrics.get("preemptions"), + "ttft_last": self.last_ttft[1] if self.last_ttft else None, + "ttft_last_t": self.last_ttft[0] if self.last_ttft else None, + "ttft_avg": ratio("ttft_sum", "ttft_count"), + "ttft_count": metrics.get("ttft_count"), }, }