Repository navigation
Mac prefill: keep the chunk start before a long history's boundary (--tail-checkpoint-tokens) - #287
chadhurley25075-png wants to merge 1 commit into
Conversation
…-tail-checkpoint-tokens) A next turn that edits the end of the last message (an appended line, a re-rendered user message) landed just before the history boundary with no earlier checkpoint and prefilled the whole prompt again. Histories of tail_checkpoint_tokens or more (default 8192, 0: off) now also keep the chunk start before the boundary's own, so such a turn resumes at most one chunk back. 32K GLM-5.3-Flash on an M3 Ultra: 60.9 s -> 0.74 s; appended turns unchanged. Costs one more cached prefix per long turn. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011Vnv1vFxgyK3UZpM2J5dRQ
|
We tested this on an M5 Ultra (256 GB) with Edit-turn A/B (prompt / cached / TTFT; the same in both of two runs, ±0.5 s on cold turns)
Exactness: every reply's Upstream tools, with this PR: Tests: on v0.6.5, Memory, the trade-off you asked about. Each long turn stores one more prefix, about 29 KB a token on this model. At 151K that is 4.4 GiB more per turn: 8.85 GiB instead of 4.45. Wired memory peak did not change (125.9–128.5 GiB in every run). The cost shows up in how many conversations stay cached. We took five 151K conversations, sent turn 1 of each, then appended a new message to each, oldest first. The prompt cache here is 30.6 GiB.
Without spill, the extra entries push out the history-boundary entries that plain follow-up turns need. Re-prefilling the oldest conversation then evicts the next one, so every follow-up re-reads the whole prompt. The tail entry only helps the edit case, so one option is to evict tail entries before boundary entries. Another is to skip the tail entry when keeping it would evict a boundary entry. Either way the appended-turn case would never be worse than before. With spill on, the cost was small in our run. v0.6.5: your commit cherry-picks onto v0.6.5 with no conflicts and no changes. |
|
Thank you, @chadhurley25075-png. Thanks too for correcting the earlier benchmark note yourself. The case you isolated is real. A 32K conversation whose last message is edited went from 60.9 s to 1.06 s to first token. The Python engine is frozen (#286), so this PR won't merge, but the idea carries into the Zig engine. Its server already plans which prompt prefixes to keep and knows each prompt's chunk starts, so the rewrite adds the chunk start before the history boundary for long histories, with the 8,192-token threshold and the off switch you proposed. It takes effect once the Zig engine keeps prompt states across requests, and the commit will carry your credit. |
What this does
On a Mac under the message prefill plan, a turn keeps prompt checkpoints only at chunk starts: the history boundary and the stable prefix. When a client sends a next turn that edits the end of the last message, instead of appending a reply and a new one, the edit lands just before the history boundary. There's no earlier checkpoint, so the whole prompt is prefilled again. Examples of such edits: a line appended to the user message, a re-rendered last message, a client that rewrites a trailing context block.
This PR keeps one more checkpoint for long histories: the chunk start before the history boundary's own. An edit inside the last chunk then resumes at most one chunk (≤ 2,048 tokens) back.
Scheduler(tail_checkpoint_tokens=8192)/ChatApp(tail_checkpoint_tokens=...)/--tail-checkpoint-tokens N. It applies to histories of N tokens or more;0turns it off.choose_checkpointsis unchanged. The extra start is added inPromptFill._start_fill, where the prompt's chunk starts are known, so it always lands on the grid.The trade-off (your call)
It costs one more cached prefix per long turn. We measured about 0.8 GiB for a 32K GLM-5.3-Flash prefix. It lives inside the existing
--prompt-cache-gibbudget and slot count, and it's evicted like any other conversation entry, so a busy server may keep fewer older conversations. The 8192 default reflects our view that below that, a full re-prefill is cheap enough not to spend a slot on. You may want a different threshold, or off by default.Numbers (M3 Ultra 512 GB, GLM-5.3-Flash-MLX-4bit,
--mtp-drafts 1, this branch vs 0.6.2, same box)¹ That run's history boundary fell within 256 tokens of the 2,048 grid, so it merged into the previous start, and the turn re-read about 210 tokens. That's existing behavior, independent of this PR. The prompts differ slightly by seed.
A correction we owe you
Our trial notes earlier today listed "Mac resumed turn at 32K re-prefills (61–79 s)" as a 0.6.2 regression. That was our benchmark's fault. Its "resumed" request appended a line inside the last user message rather than continuing the conversation. Real multi-turn resumes on 0.6.2 are fast, as the table shows. The only real gap is the tail-edit case above, which is what this PR addresses.
Tests
tests/test_tail_checkpoint.py. On a 4-token grid it checks four things: a long history keeps both starts (24 and 28); the setting is off at 0 and below its length; a tail edit resumes at 24 with the setting and at 0 without it; a negative value is refused.Thank you for TensorFold. Shared rounds, exact drafts and the decisions endpoint all held up in our trial today.
🤖 Generated with Claude Code