Skip to content

Prompt cache: a state a conversation resumes again is a branch point, not superseded - #735

Closed
BadAd84 wants to merge 1 commit into
ashhart:mainfrom
BadAd84:prompt-cache-branch-point
Closed

BadAd84 wants to merge 1 commit into
ashhart:mainfrom
BadAd84:prompt-cache-branch-point

Conversation

@BadAd84

@BadAd84 BadAd84 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

What this changes

When the prompt cache needs room it frees first a state that a longer state, kept by a later prompt, extends (victimBut in zig/src/core/prompt_cache.zig; pick in zig/src/core/prompt_radix.zig). That's right for a conversation that has grown past its old turns.

It's wrong for the state several continuations branch from: a re-sent turn, a regenerated or edited reply, or an agent that starts each step from the same history. Once one of them has kept its own longer state, the state the next one needs counts as superseded, and the next prompt that keeps anything (a long prompt keeps its history) evicts it first. Every later continuation then prefills the whole history again and keeps nothing at that history either, so the conversation never recovers.

A kept state that is resumed again after a later prompt kept a longer state extending it is now a branch point: the superseded rule skips it, and it only goes as the least recently used, like a shared system cut does. A state resumed once and then extended is still superseded first, as before, so a conversation that grows turn by turn frees its old turns first.

Both stores get the rule (the paged store has no backend yet; the rule is the same there).

Found this on a Qwen3.8 Flash Next serve built on an earlier TensorFold whose store has this same rule. One GB10, a ~100k-token conversation, three other streams decoding, and other 1k-8k token prompts in between its turns:

  • without the rule, 18 of 20 turns lost the conversation's state and prefilled its ~100k tokens again (about 35 s, against about 1.2 s resumed);
  • with the branch-point rule (and a second rule for short whole-prompt states, which main doesn't have), 0 of 20 were lost, and 0 of 383 over a 20-minute run.

On main the same sequence fails in a host test, below.

The cost: a branch point that's never resumed again sticks around until it's the least recently used state, where it used to go first.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: TensorFold 0efbd447 (main), Zig 0.17.0, Linux aarch64 on a GB10 host, host tests only (CPU). No checkpoint.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: not run. The change is eviction order only: no row arithmetic, kernel or reply path changes. In every new test a resumed prompt pass equals a fresh one (the test family's running sum).
  • Decode speed before and after, from tools/bench_openai.py: not run (no decode path changed).
  • Prompt speed before and after, from tools/prefill_cold.py: not run. A resume now scans the kept states once for a longer one that extends it, the same scan eviction already makes for each state.
  • Tests run, with pass, fail and skip counts:
    • zig build test: main 616/621 (5 skipped); this change 623/628 (5 skipped).
    • zig/tests/server/zig_test.sh: main 155/155 and 96/96; this change 155/155 and 100/100.
    • Before the fix (main plus only the new tests): the flat store's test fails (expected 100117, found 0: the conversation's next turn prefills from the start after a 6k prompt made room) and so does the paged store's (expected 20, found 0); the other two new tests pass on main as they should (a state resumed once and then extended is still superseded first; a turn's rewind state is not superseded by the history state its own prompt kept).
    • Mutants, each a single replacement in a copy of this change, judged by the suites' exit code with the named test failing: 7 of 7 killed. Never marking a branch point, ignoring the mark in victimBut, marking every resumed state, dropping the "kept by a longer prompt" condition from the extension check (killed by the rewind test, which I added for it), and the same three for the paged store.
    • python3 tools/zig/lean_check.py reports no problem this change adds; zig fmt --check passes on the changed files.
  • Not run, and why: no GPU or Metal run. The engines that use this store (CUDA Nemotron and GLM, and on a Mac Flash Next and Qwen3.8-27B) were not run on this change; I have no Mac, and the GB10 measurement above is from the earlier TensorFold.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Resumed equals fresh in the host tests; no engine run.)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (Not measured on a GPU.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The two tests for the bug fail on main; the other two pin behaviour this change keeps and pass on both.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
…when the CUDA server uses its own kernels

--kernels native is the CUDA CLI's flag; the server runs its own kernels whenever it has no captured Triton set.
@ashhart

ashhart commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Thank you, @BadAd84. This is a real gap and you found its exact shape: the superseded rule is right for a conversation that only grows and wrong for a turn that several continuations share. Your fix keeps the first case as it was. It landed in 1.0.9 as e17e2776c, under your name.

One thing differs from how we usually land a PR. The commit here carries a personal email address, and we keep personal addresses out of the public history, so the landed commit is authored with your GitHub noreply address, [email protected]. It still counts as yours on GitHub. Because this PR's own head commit isn't in main, GitHub won't mark it merged, so I'm closing it with this link to the landed commit.

We ran it served, with Nemotron 3.5 Lightning MLX 4-bit and a small cache budget, before and after the change on the same machines: a ~4,000-token history, two forks from it, each with a ~450-token assistant answer and a follow-up so each keeps its own longer state, then unrelated prompts until the cache has to free a state, then a third fork.

  • M3 Ultra, 0.3 GiB. At the first eviction the build without your change freed the shared history, and the third fork read all 3,956 tokens of it again. With your change the cache freed the first fork's state instead, and the third fork resumed all 3,956.
  • GB10, 1 GiB. The same: without your change the third fork resumed only the 7 tokens that open a user message; with it, 3,956. Here the history also outlived a growing conversation, so a fourth fork resumed it too.
  • A conversation that only grows (~450 tokens a turn) resumed the previous turn's prompt every turn on both builds, with the cache freeing old turns as it went.
  • Same replies. Every reply in these runs equals what a server with the cache off gives, on both builds and both machines.

Two notes from our review, for later and not blocking. A branch point keeps its mark after the longer state that set it is gone, so it leaves only as the least recently used, the cost you describe. And the GB10 numbers in the description came from an earlier tree that also had a rule for short whole-prompt states, which this commit doesn't add. If that second rule matters on main, a PR with a measurement on main would show it.

@ashhart ashhart closed this Oct 13, 2026
@ashhart ashhart mentioned this pull request Oct 14, 2026
outcastofmusic added a commit to outcastofmusic/TensorFold that referenced this pull request Oct 17, 2026
…oint, freed only as least recently used

A resumed state kept its place in the cache's use order, and once the
continuation kept its own longer state, _superseded counted the shared
history as superseded and freed it first under memory pressure. Every next
continuation (a re-sent, edited or forked turn, an agent that restarts each
step from one history) then prefilled the whole history again. A resume now
moves the state to the newest end and marks it a branch point when a later
turn already extends it; _superseded skips branch points. A conversation that
only grows still frees its old turns first. Ports upstream ashhart#735.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
outcastofmusic added a commit to outcastofmusic/TensorFold that referenced this pull request Oct 17, 2026
…nder the entry cap

Upstream ashhart#735 fixed a cache that freed such a history first. Flash Next's
fork already copies the history to a spare slot, where it is the slot's
shortest entry, which _superseded never gives up; this test holds that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
…when the CUDA server uses its own kernels

--kernels native is the CUDA CLI's flag; the server runs its own kernels whenever it has no captured Triton set.
@BadAd84
BadAd84 deleted the prompt-cache-branch-point branch November 5, 2026 14:55
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