From 51fce4c83e225284dc40a88b1240fb1dadab7a8b Mon Sep 17 00:00:00 2001
From: code-martin <44068401+code-martin@users.noreply.github.com>
Date: Mon, 28 Sep 2026 18:59:58 +0300
Subject: [PATCH] telemetry: per-request decode expert cache hit rate in DONE
and web monitor
- Reset cache hits/lookups baseline at start of autoregressive decode to exclude prefill distortion
- Append req_hits and req_look to engine DONE line and log decode hit rate to stderr
- Parse decode hit metrics in server.py and record hit_rate per request in history
- Add Hit rate column to Recent requests table in web monitor
---
serve/server.py | 9 +++++++--
serve/web/app.js | 5 +++--
serve/web/index.html | 4 ++--
src/program/generate.cpp | 16 +++++++++++++---
4 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/serve/server.py b/serve/server.py
index b0a2f312..d46cd870 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -223,6 +223,8 @@ class StrataEngine:
"decode_ms": float(f[4]), "finish": f[5]}
if len(f) >= 9: # the conversation cache's fields (engine 0.1.3+)
self.last.update(drafts_accepted=int(f[6]), drafts_offered=int(f[7]), reused=int(f[8]))
+ if len(f) >= 11: # decode hit rate fields
+ self.last.update(hits=int(f[9]), lookups=int(f[10]))
@staticmethod
def sampling_keys(sampling: dict) -> str:
@@ -743,6 +745,7 @@ class Service:
last = dict(getattr(self.engine, "last", {}) or {})
started = self.status.get("started", time.time())
loaded = str((getattr(self.engine, "info", {}) or {}).get("cvec", 0)) not in ("0", "", "None")
+ hit_rate = round(last["hits"] / last["lookups"], 3) if last.get("lookups") else None
self.history.append({
"projection": (sampling or {}).get("experimental_speed_projection") is not False
if loaded else None,
@@ -750,7 +753,8 @@ class Service:
"prompt_tokens": len(ids), "reused": last.get("reused"), "output_tokens": n,
"prompt_ms": last.get("prompt_ms"), "decode_ms": last.get("decode_ms"),
"decode_tok_s": round(last["generated"] / (last["decode_ms"] / 1000), 1)
- if n and last.get("generated") and last.get("decode_ms") else None})
+ if n and last.get("generated") and last.get("decode_ms") else None,
+ "hit_rate": hit_rate})
t = self.totals
t["requests"] += 1
t["prompt_tokens"] += len(ids)
@@ -762,8 +766,9 @@ class Service:
el = now - self.status.get("started", now)
ft = self.status.get("first_token")
rate = n / max(1e-6, now - ft) if ft else 0.0
+ hit_msg = f", expert cache {hit_rate*100:.1f}% hit" if hit_rate is not None else ""
print(f"[strata] done: {n} tokens in {el:.0f} s ({rate:.1f} tok/s) "
- f"({finish}, cancel={cancel.is_set()})", flush=True)
+ f"({finish}, cancel={cancel.is_set()}){hit_msg}", flush=True)
if os.environ.get("STRATA_DEBUG") and raw_ids:
print(f"[strata] raw: {self.tok.decode(raw_ids)!r}", flush=True)
self.status["busy"] = False
diff --git a/serve/web/app.js b/serve/web/app.js
index 43757b9a..6892f965 100644
--- a/serve/web/app.js
+++ b/serve/web/app.js
@@ -274,7 +274,7 @@ function renderMonitor(live, hw, st, eng, h, last, requests, totals, kept) {
// recent requests
const body = $("req-body");
if (!requests.length) {
- body.innerHTML = `
| No requests yet |
`;
+ body.innerHTML = `| No requests yet |
`;
} else {
const badge = {stop: ["", "Done"], length: ["", "Max tokens"], cancel: ["st-badge--queued", "Stopped"],
disconnect: ["st-badge--queued", "Closed"], error: ["st-badge--error", "Error"]};
@@ -282,9 +282,10 @@ function renderMonitor(live, hw, st, eng, h, last, requests, totals, kept) {
const [cls, text] = badge[r.finish] || ["", r.finish || "–"];
const t = new Date(r.time * 1000).toLocaleTimeString([], {hour: "2-digit", minute: "2-digit", second: "2-digit"});
const proj = r.projection == null ? "" : ` ${r.projection ? "ESP" : "stock"}`;
+ const hit = r.hit_rate == null ? "–" : `${(r.hit_rate * 100).toFixed(1)}%`;
return `| ${esc(t)} | ${esc(text)}${proj} | ${fmt(r.prompt_tokens)} |
${fmt(r.reused)} | ${fmt(r.output_tokens)} | ${fmt(r.decode_tok_s, 1)} |
- ${fmt(r.duration_s, 1)} s |
`;
+ ${hit} | ${fmt(r.duration_s, 1)} s | `;
}).join("");
}
const all = $("req-all");
diff --git a/serve/web/index.html b/serve/web/index.html
index bdf2b86e..4a9dcc90 100644
--- a/serve/web/index.html
+++ b/serve/web/index.html
@@ -117,8 +117,8 @@
| Time | Status | Prompt | Reused | Output |
- Tok/s | Duration |
- | No requests yet |
+ Tok/s | Hit rate | Duration |
+ | No requests yet |
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index d84d8647..1aa8d19e 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -3011,6 +3011,8 @@ int main(int argc, char** argv) {
for (int64_t i = 0; i < n - 1; ++i) consumed.push_back((int32_t) ids[(size_t) i]);
const char* finish = "length";
const Clock::time_point d0 = Clock::now();
+ const int64_t decode_hits0 = drive.d.cache_hits;
+ const int64_t decode_look0 = drive.d.cache_hits + drive.d.cache_admitted + drive.d.cache_refused;
if (cancelled) finish = "cancel";
while (!cancelled && produced_n < max_new) {
int T = S_mtp;
@@ -3190,9 +3192,12 @@ int main(int argc, char** argv) {
(unsigned long long) h_pool, (unsigned long long) h_kv, (unsigned long long) h_mtp,
(unsigned long long) h_stale, ss.ple_prev[0], ss.ple_prev[1]);
}
- // DONE
- std::printf("DONE %lld %lld %.1f %.1f %s %lld %lld %lld\n", (long long) produced_n, (long long) n, prompt_ms,
- decode_ms, finish, (long long) draft_accepted, (long long) draft_offered, (long long) resume);
+ const int64_t req_hits = drive.d.cache_hits - decode_hits0;
+ const int64_t req_look = (drive.d.cache_hits + drive.d.cache_admitted + drive.d.cache_refused) - decode_look0;
+ // DONE [hits] [lookups]
+ std::printf("DONE %lld %lld %.1f %.1f %s %lld %lld %lld %lld %lld\n", (long long) produced_n, (long long) n, prompt_ms,
+ decode_ms, finish, (long long) draft_accepted, (long long) draft_offered, (long long) resume,
+ (long long) req_hits, (long long) req_look);
std::fflush(stdout);
const int64_t fresh = n - resume;
std::fprintf(stderr, "strata serve: prompt %lld tokens = %lld reused + %lld read in %.0f ms (%.1f tok/s), "
@@ -3201,6 +3206,11 @@ int main(int argc, char** argv) {
prompt_ms > 0 ? 1000.0 * fresh / prompt_ms : 0.0, (long long) produced_n, decode_ms,
decode_ms > 0 ? 1000.0 * produced_n / decode_ms : 0.0, (long long) draft_accepted,
(long long) draft_offered, checks.size(), cancelled ? " (cancelled)" : "");
+ if (req_look > 0) {
+ std::fprintf(stderr, "strata serve: decode expert cache hit rate: %.1f%% (%lld / %lld hits)\n",
+ 100.0 * (double) req_hits / (double) req_look,
+ (long long) req_hits, (long long) req_look);
+ }
if (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1) {
// KV streaming, cumulative over the process: blocks the selections named vs blocks read from RAM
uint64_t miss = 0, look = 0;