Skip to content

prompt cache: planned families keep marks on prompts under min_prompt - #669

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jschmied:prompt-cache-planned-short
Oct 12, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jschmied:prompt-cache-planned-short

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

This generalises c79c431, from #584. There you said the min_prompt floor exists for families whose marks end a prompt call early, and that GLM, which keeps only at chunk starts its pass cuts anyway, gained nothing from it, so GLM's store now sets 0. The same holds for every planned family. min_prompt (4,096) exists because a mark can cost an extra prompt call. A planned family's marks fall on chunk starts its pass already makes, so for those families the extra call never happens, yet prompts under 4,096 tokens still keep nothing. This PR skips the threshold when rules.planned is set. Unplanned families keep it.

  • core/prompt_cache.zig: marksRewind returns no marks below min_prompt only when the family is not planned.
  • core/prompt_cache_test.zig: a planned store keeps its mark on a 10-token prompt, and an unplanned one keeps none. The test fails with the old rule.

Receipts on a GB10 (sm_121, CUDA 13.0, main e7c523e). CUDA Nemotron 3.5 Lightning MLX-4bit, tensorfold-native serve --parallel 1, short agent loop of 12 turns growing from 1.2k to 5.2k tokens, two server starts per arm. The 0 arm sets min_prompt = 0 through a local switch, which is what this change does for a planned family:

arm warm TTFT, turns 2-12 (mean) turn 1 replies
main 0.39 s (nothing kept until 4,096) 0.20 s —
min_prompt ignored (planned) 0.24 s 0.21 s equal to main, every turn

Data: results3.jsonl.

On Metal this covers Nemotron, and GLM's explicit min_prompt = 0 becomes redundant (I left it in). I haven't measured Metal.

…t (its marks fall on chunk starts the pass makes)
@ashhart
ashhart merged commit 1c32db6 into ashhart:main Oct 12, 2026
2 checks passed
@ashhart

ashhart commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. The argument from #584 carries over cleanly, and now Metal has numbers too. Your commit 1c51d6195 landed in 1.0.8 under your name, so GitHub shows this PR as merged.

What we measured on Nemotron 3.5 Lightning MLX 4-bit:

  • Same replies. Short chats of five turns (354 to 2,771 tokens; greedy, seeded sampled and thinking on) gave 35 of 35 replies equal to the build before your change and to the same build with the cache off. Every turn equalled its "draft": false reply, on an M5 Max and on a GB10.
  • Time to first token, M5 Max. Before your change these chats resumed nothing. With it, turns 2 to 5 started in 0.19 to 0.31 s instead of 0.66 to 0.85 s. Of 48 two-turn chats, every second turn resumed, in 0.088 s against 0.638 s (medians).
  • Time to first token, CUDA. With your change alone, CUDA kept states on its 2,048-row grid, so turns past 2,048 tokens resumed: on a GB10 they started in 0.12 to 0.16 s instead of 0.39 to 0.41 s. CUDA Nemotron: the prompt path is chunk-invariant, so the cache's 2,048 grid could go #670, also in 1.0.8, removes that grid, so shorter chats resume on CUDA too; its thread has those numbers.
  • First turns. On a Mac, keeping the state costs a first turn about 0.4% (M5 Ultra, A B B A, twelve cold 2,000-token prompts: prefill 0.2556 against 0.2546 s), and decode is unchanged (x1.002 in all four cells). On CUDA with the grid, a state sat where a chunk ends anyway, so cold first turns took as long as before (A B B A on a GB10 over prompts of 330 to 21,500 tokens, within 1%). With CUDA Nemotron: the prompt path is chunk-invariant, so the cache's 2,048 grid could go #670, a first turn under 1,400 tokens keeps two or three states, which costs it 1 to 3 ms on a GB10.
  • Memory. A Metal state is 57 to 61 MiB. At a 1 GiB budget, 48 chats kept at most 18 states (1,023 MiB), evicted 31, and every second turn still resumed and matched. On CUDA a state used to copy the whole window's caches, 544 MiB at a 70,000-token window, so the 16 GiB default had room for 30. 1.0.8 sizes a CUDA state by its prompt instead: 48 MiB of Mamba states plus 7 KiB a token, in whole 2 MiB pages, so 62 MiB at 2,048 tokens, and the 16 GiB default holds 258 states.

One change on our side, in a commit after yours. prompt_cache_test.zig went past our 600-line limit with your test, so the seven learned-state tests moved unchanged to prompt_cache_learn_test.zig. The CHANGELOG names the change in behaviour: short chats on Nemotron now fill the prompt cache up to its budget and evict, as long ones do.

BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
…rn_test.zig (pure move)

prompt_cache_test.zig passed 600 lines with ashhart#669's test; the seven learned-state tests and the two imports only they use move unchanged.
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
A new entry gives the size of a kept state on the 2,048-row grid and the states and chats the 16 GiB
default holds. The ashhart#669 entry said a CUDA state copies the whole window; it now gives the new size.
The RUNBOOK's floor said 4,096 tokens, which ashhart#669 removed; on CUDA a prompt needs more than 2,048.
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
…hhart#670), with no prompt call

A new ashhart#670 entry: states at any row, chunk invariance and the in-chunk keeps checked byte for byte,
later turns' and cold first turns' times, and what a chat keeps now. The sizing entry gives the two
parts a state allocates and the spare Mamba buffers; the ashhart#669 entry's CUDA lines point to both. The
RUNBOOK's paragraph says where a turn resumes and what a state takes.
@ashhart ashhart mentioned this pull request Oct 14, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…rn_test.zig (pure move)

prompt_cache_test.zig passed 600 lines with ashhart#669's test; the seven learned-state tests and the two imports only they use move unchanged.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
ashhart#669 landed above as b90dea7fc (the author's commit, rebased onto this candidate). This keeps the PR's own head commit in the history, with no change to the tree.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
A new entry gives the size of a kept state on the 2,048-row grid and the states and chats the 16 GiB
default holds. The ashhart#669 entry said a CUDA state copies the whole window; it now gives the new size.
The RUNBOOK's floor said 4,096 tokens, which ashhart#669 removed; on CUDA a prompt needs more than 2,048.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…hhart#670), with no prompt call

A new ashhart#670 entry: states at any row, chunk invariance and the in-chunk keeps checked byte for byte,
later turns' and cold first turns' times, and what a chat keeps now. The sizing entry gives the two
parts a state allocates and the spare Mamba buffers; the ashhart#669 entry's CUDA lines point to both. The
RUNBOOK's paragraph says where a turn resumes and what a state takes.
@jschmied
jschmied deleted the prompt-cache-planned-short branch October 23, 2026 21:02
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.

2 participants