Sliding window 30+20 + GEN 0 cache pre-warm (pre-warm-on-change)

Window: MAIN_AREA 30 + BUFFER 20 (same 50 cap) halves rotation frequency. context: shared compose_prefix (byte-exact prefix of compose) + notepad_fingerprint (rendered header body, excludes updated_at). inkling: build_prefix_ids renders without finish_prompt. service: prewarm() sends GEN 0 directly (no PerfMeter/DB). sessions: schedule_prewarm at turn end on pending rotation OR notepad-dirty (ws=0 normalized); session_chat loads only the window subset.

examples/prewarm_bench.rs: joint GEN 0 -> RESUME bench, PASS on HIP (cached==L==385). Verified live all-agent: notepad write -> pre-warm 1468 -> next turn cached_tokens==1468. Tests: prefix property, opener-skip, GEN 0 primitive, notepad fingerprint. Suite 162 passed + 3 ignored.
This commit is contained in:
adm committed 2026-11-26 19:12:59 +01:00
1 parent 0ce84581a4
commit a52bec97d4
12 files changed
+650 -59

No files matched your search

+19
View File
@@ -153,3 +153,22 @@ async fn request_stop_signals_the_owner_without_taking_the_lock() {
assert!(engine.is_idle());
engine.shutdown().await.unwrap();
}
#[tokio::test]
async fn gen_zero_prefills_without_generating() {
// The cache pre-warm primitive: GEN 0 prefills the ids and emits one DONE
// with zero generated tokens (no T lines), prompt_tokens = the prefix length.
let engine = start_mock().await;
let out = engine
.generate(0, &Sampling::default(), &[1, 2, 3, 4], |_| {})
.await
.unwrap();
assert!(out.tokens.is_empty(), "GEN 0 generates no tokens");
let done = out.done.unwrap();
assert_eq!(done.generated, 0);
assert_eq!(done.prompt_tokens, 4);
assert_eq!(done.finish, "length");
// the wires are free again: a real turn queues/runs normally after the warm
assert!(engine.is_idle());
engine.shutdown().await.unwrap();
}
+47
View File
@@ -268,3 +268,50 @@ fn config_sampling_defaults_are_applied_and_overridable() {
assert!(s2.keys().contains("top_k=64"));
let _ = std::fs::remove_file(&path);
}
#[test]
fn build_prefix_ids_skips_opener_and_is_token_prefix() {
let (tok, path) = fixture_tokenizer();
// A template emitting the effort-none line, so finish_prompt WOULD append the
// generation opener on build_prompt; build_prefix_ids must skip it.
let tmpl = ChatTemplate::new(
"{%- for m in messages -%}{%- if loop.first -%}<|content_text|>Thinking effort level: 0<|end_message|>{%- endif -%}<|content_text|>{{ m.content }}<|end_message|>{%- endfor -%}",
)
.unwrap();
let fmt = InklingFormatter::new(
tok.clone(),
tmpl,
"m".into(),
Some("none".into()),
Sampling::default(),
);
let prefix_req: ChatRequest = serde_json::from_value(json!({
"model": "m",
"messages": [
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "answer"}
]
}))
.unwrap();
let full_req: ChatRequest = serde_json::from_value(json!({
"model": "m",
"messages": [
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "answer"},
{"role": "user", "content": "hello"}
]
}))
.unwrap();
let prefix_ids = fmt.build_prefix_ids(&prefix_req).unwrap();
let full_ids = fmt.build_prompt(&full_req).unwrap().ids;
// The pre-warm ids are a byte-exact token prefix of the real turn's ids (the
// message boundary is a special token, so the BPE/WordLevel span never merges
// across it).
assert!(full_ids.len() > prefix_ids.len());
assert_eq!(&full_ids[..prefix_ids.len()], &prefix_ids[..]);
// build_prompt on the SAME prefix appends the opener -> longer than the
// prefix-only build (this is exactly what the pre-warm must NOT include).
let prefix_with_opener = fmt.build_prompt(&prefix_req).unwrap().ids;
assert!(prefix_with_opener.len() > prefix_ids.len());
let _ = std::fs::remove_file(&path);
}
+9 -1
View File
@@ -94,8 +94,16 @@ while True:
out(f"DONE {n} {len(ids)} 1.0 2.0 {'abort' if stopped else 'length'}")
continue
# GEN 0 (max_new = 0): prefill only, no generation — the cache pre-warm
# primitive. One DONE, zero tokens, prompt_tokens = len(ids), finish "length".
max_new = int(parts[1]) if parts[1].lstrip("-").isdigit() else 0
if max_new == 0:
out("RESUME 0")
out(f"DONE 0 {len(ids)} 5.0 0.0 length 0 0 0")
continue
out("RESUME 1")
out("PP 2 4 0.9 2222.2")
for t in (101, 102, 103, 104, 105):
out(f"T {t}")
out("DONE 5 4 12.5 34.0 stop 3 4 1")
out("DONE 5 4 12.5 34.0 stop 3 4 1")