[GW] Prefix cache: save prompt-boundary checkpoint + emit RESUME (fix multi-turn reuse in the GUI)

Root cause (traced across strata-ctl + strata): strata-ctl build_prompt re-renders
the chat template from conversation TEXT and re-encodes every turn; the assistant
reply round-trips through detokenize/re-encode (special tokens stripped), so it is
NOT token-identical to the generated ids. The first port saved a checkpoint only at
the post-decode pos (prompt+generated), exactly the part that diverges -> no match
on turn 2 -> full cold re-prefill (Federico measured 734s vs 783s, no speedup).

Fix mirrors strata ink_serve.h:949 (prompt-end checkpoint, longest-prefix match):
also save a checkpoint at the PROMPT boundary, the stable system+history prefix
that re-renders identically across turns. best_match picks the longest valid
prefix, so turn 2 restores the prompt and prefills only the new dialogue suffix.
Emit RESUME <start> via on_prefill_complete (was hardcoded 0) so strata-ctl reports
reused/cached tokens. VETTA_DBG_PREFIX logs match/save for live diagnosis.

Verified (gfx1150, Inkling-2-Mini, FP16 KV, greedy): realistic turn-2 with a
DIFFERENT assistant tail restores the 300-token prompt boundary (debug restored=300),
parity EXACT vs cold, prefill 6.02s vs 72.42s (12x). CPU 91 + gw GPU 5 green, Lean 0
sorryAx. Absolute first-turn prefill speed still needs batched prefill (next brick).
This commit is contained in:
adm committed 2026-12-01 21:35:38 +01:00
1 parent 442d26c90c
commit 20e152640e
1 file changed
+25 -7
+25 -7
View File
@@ -273,6 +273,9 @@ pub fn serve_gen(
}
}
}
if std::env::var("VETTA_DBG_PREFIX").is_ok() {
eprintln!("[prefix] prompt_len={n_prompt} restored={start}");
}
// Resident weight cache for this request: weights upload once and stay on
// the GPU across prefill + decode (no per-token NVMe expert re-reads).
@@ -309,7 +312,9 @@ pub fn serve_gen(
}
let prompt_ms = t0.elapsed().as_secs_f64() * 1000.0;
// GEN 0 pre-warm: on_prefill_complete emits DONE directly (never decodes).
match sess.on_prefill_complete(0, prompt_ms) {
// `start` is the restored prefix length: >0 emits RESUME <start> (wire-compatible
// with strata, so strata-ctl reports the reused/cached prompt tokens).
match sess.on_prefill_complete(start as u32, prompt_ms) {
Ok(lines) => emit_all(emit, lines),
Err(e) => {
emit_all(emit, sess.on_error(&e));
@@ -317,11 +322,20 @@ pub fn serve_gen(
return;
}
}
if g.max_new == 0 {
// Pre-warm folded the full prompt into `states`: checkpoint it for reuse.
// Checkpoint the PROMPT boundary. This is the prefix that stays token-identical
// across turns: strata-ctl re-renders the same system+history text each turn,
// while the assistant reply round-trips through detokenize/re-encode (special
// tokens stripped) and is NOT token-identical to the generated ids. Saving here
// — not only at the post-decode `pos` — is what lets the next turn restore the
// stable prefix and prefill only the new dialogue suffix. Mirrors strata
// --auto-ckpt's prompt-boundary save (proofs/prefix_cache.lean restore_equiv).
if std::env::var("VETTA_NO_PREFIX_CACHE").is_err() && pos > 0 {
let _ = eng.prefix.borrow_mut().save(&history[..pos as usize], &states, &eng.d);
if std::env::var("VETTA_DBG_PREFIX").is_ok() {
eprintln!("[prefix] prompt-boundary save seq={pos}");
}
}
if g.max_new == 0 {
stop.store(false, Ordering::Relaxed);
return;
}
@@ -397,12 +411,16 @@ pub fn serve_gen(
pos += 1;
}
let decode_ms = t1.elapsed().as_secs_f64() * 1000.0;
// Checkpoint the full processed prefix (prompt + generated tokens) so the next
// turn restores it and prefills only its new suffix. `pos` counts the tokens
// actually folded into `states` (history[..pos]); a sampled token that was not
// forwarded (EOS / last length step) is excluded, keeping the checkpoint valid.
// Also checkpoint the full processed sequence (prompt + generated tokens). This
// is a bonus reuse when the next prompt IS token-identical (e.g. a retry); the
// prompt-boundary checkpoint above is the robust one for normal chat. `pos`
// counts tokens actually folded into `states` (history[..pos]); a sampled token
// not forwarded (EOS / last length step) is excluded, keeping the ckpt valid.
if std::env::var("VETTA_NO_PREFIX_CACHE").is_err() && pos > 0 {
let _ = eng.prefix.borrow_mut().save(&history[..pos as usize], &states, &eng.d);
if std::env::var("VETTA_DBG_PREFIX").is_ok() {
eprintln!("[prefix] full-sequence save seq={pos}");
}
}
emit_all(emit, sess.on_finish(finish, decode_ms).unwrap_or_default());
stop.store(false, Ordering::Relaxed);