Skip to content

Mac prefill: keep the chunk start before a long history's boundary (--tail-checkpoint-tokens) - #287

Closed
chadhurley25075-png wants to merge 1 commit into
ashhart:mainfrom
chadhurley25075-png:tail-checkpoint
Closed

chadhurley25075-png wants to merge 1 commit into
ashhart:mainfrom
chadhurley25075-png:tail-checkpoint

Conversation

@chadhurley25075-png

Copy link
Copy Markdown
Contributor

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.

  • New setting Scheduler(tail_checkpoint_tokens=8192) / ChatApp(tail_checkpoint_tokens=...) / --tail-checkpoint-tokens N. It applies to histories of N tokens or more; 0 turns it off.
  • choose_checkpoints is unchanged. The extra start is added in PromptFill._start_fill, where the prompt's chunk starts are known, so it always lands on the grid.
  • README options table updated.

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-gib budget 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)

32K-token conversation 0.6.2 this branch
Next turn appends reply + new message, thinking off TTFT 0.20 / 0.26 s (35,104 cached) 0.19 / 0.27 s (35,190 cached)
Same, thinking on 0.19 / 0.27 s (35,162 cached) 0.70 / 0.74 s (34,816 cached)¹
Next turn edits the end of the last message, thinking off 60.9 s (cached 0) 1.06 s (cached 34,816)
Same, thinking on — 0.69 s (cached 34,816)

¹ 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

  • New 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.
  • Non-CUDA suite run on an M3 Ultra (MLX 0.32.3): 3,762 passed, 875 skipped, 0 failed (6 min 21 s).

Thank you for TensorFold. Shared rounds, exact drafts and the decisions endpoint all held up in our trial today.

🤖 Generated with Claude Code

…-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
@ashhart ashhart mentioned this pull request Oct 3, 2026
@gilby

gilby commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

We tested this on an M5 Ultra (256 GB) with Jundot/Qwen3.8-Flash-Next-oQ4e-mtp, --parallel 4 --context 262144 --reasoning-effort medium. We ran one server at a time, this branch against the same tree without it, with greedy decoding and max_tokens 64. On this model the plan cuts prompt chunks of up to 8,192 tokens, not 2,048, so an edited turn can re-read up to 8K tokens. In our conversations it re-read about 1,800, because the chunk start at the last reply was closer.

Edit-turn A/B (prompt / cached / TTFT; the same in both of two runs, ±0.5 s on cold turns)

history next turn without with this PR
17.6K a line appended to the last user message 17,641 / 0 / 5.8 s 17,641 / 15,817 / 0.69 s
17.6K trailing <context> block rewritten 17,625 / 0 / 5.8 s 17,625 / 15,817 / 0.69 s
17.6K a new user message appended 17,659 / 17,625 / 0.10 s same
66K line appended 66,280 / 0 / 23.2 s 66,280 / 64,454 / 0.84 s
66K block rewritten 66,264 / 0 / 23.3 s 66,264 / 64,454 / 0.84 s
66K new message 66,298 / 66,264 / 0.21 s same (0.22 s)
151K line appended 151,359 / 0 / 57.3 s 151,359 / 149,544 / 1.12 s
151K block rewritten 151,344 / 0 / 57.1 s 151,344 / 149,544 / 1.09 s
151K new message 151,377 / 151,343 / 0.42 s same (0.44 s)

Exactness: every reply's token_sha was the same with and without the PR. That covers the resumed edited turns compared with the same prompts prefilled cold, both on a fresh server with this PR and on the server without it.

Upstream tools, with this PR: tools/bench_concurrent.py --levels 4 --alone --serial, two runs: 0 failed, alone 12/12 equal and serial equal at every level. Aggregate was 286–314 tok/s on code and 228–272 on chat. tools/bench_openai.py, greedy, median of 3: fibonacci-raw 168.0 / 168.5 tok/s with the PR against 162.9 / 168.8 without it, and gpu-chat-no-think 149.1 / 147.9 against 149.4 / 149.3. Decode is unchanged.

Tests: on v0.6.5, tests/test_tail_checkpoint.py fails without the PR (5 tests) and passes with it. The rest of the non-CUDA suite gave the same result on both (5,005 passed, 596 skipped). Its one failure, test_glm5_prompt_kernels.py::test_fused_index_scores_are_the_three_ops, fails on plain v0.6.5 on this machine as well.

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 with, --spill-gib 64 with, --spill-gib 0 (the default)
entries after turn 1 of all 5 5 (22.3 GiB), 0 evicted 6 (26.6 GiB), conversations spilled 6 (26.6 GiB)
appended turns, cached 5 of 5 5 of 5 (3 read back from disk) 0 of 5
appended turns, TTFT 0.42–0.43 s 0.41–0.91 s 58–59 s each

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. prompt_fill.py and scheduler.py are the same there as on your base. The rebased commit, with your authorship kept, is on our fork as branch pr287-on-v065 (71c67d2), if that saves you a step.

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants