Skip to content

perf(cuda): past --checkpoint-slots, a kept state its conversation has moved past goes first - #190

Closed
nood-co1 wants to merge 1 commit into
ashhart:mainfrom
blockbrain-ai:feat/retention-moved-past-pr
Closed

nood-co1 wants to merge 1 commit into
ashhart:mainfrom
blockbrain-ai:feat/retention-moved-past-pr

Conversation

@nood-co1

@nood-co1 nood-co1 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Past keep, PrefixCache drops the oldest kept prompt state that was never resumed from, else the oldest (cuda/streams.py, PrefixCache._drop). When conversations alternate on one server, that rule drops the state a
conversation is about to resume from: its newest state has not been resumed from yet, while its older states, which the
newest one extends, have been. The next turn then resumes from an older state and prefills the difference again.

This PR adds one rule ahead of the existing one: a kept state whose every longer kept state extends it along one line
(its own conversation went on, and resumes from the longest) goes first. A state that diverging prompts both extend,
such as a system block two conversations share, is never dropped by the new rule. Without such a state the existing
rule applies unchanged. Which states exist changes; their bits do not: a resume still takes the longest kept prefix,
and every kept state still has a fresh prefill's bits. The rule applies wherever PrefixCache drops a state: past
keep in add, and for memory in evict (KVRoom), so both CUDA families that use it (qwen3_5, qwen3_5_moe) get
it, on one stream and with --parallel.

The branch is now this PR's one commit on 0.6.1 (17c73e1); the change is the same.

Why

A coding agent with a sub-agent sends two conversations whose calls alternate. Each call adds one or two states (a
message start, the prompt end one token early). With the 27B's three kept states (KEEP), the sequence below repeats
in our logs:

  1. the main conversation's turn n resumes from its state at turn n-1 (that state is now "resumed from") and keeps a
    state at turn n (never resumed from yet);
  2. the sub-agent's call keeps its own state; past keep, the never-resumed state goes: the main conversation's turn n;
  3. the main conversation's turn n+1 finds turn n-1's state and prefills turn n's tokens again.

tests/test_cuda_prefix_cache_moved_past.py reproduces this sequence on the old rule and on the new one.

We measured it on our own agent traffic, not a public benchmark. A DeepSeek Harness coding agent ran three sessions
over public repositories (attrs, itsdangerous, requests), each with a sub-agent: 55 requests (52 main calls, 3 title
calls), 997,402 prompt tokens. We replayed that recorded log at its recorded arrival times, each body capped at its
recorded reply length, on two DGX Sparks (GB10) serving the 27B with DFlash2 and --parallel 2, each Spark running
both builds, in opposite orders:

Spark Build, kept states Prefilled tokens Engine prefill s TTFT median TTFT p90 Whole request median Whole request p90
A 0.6.1, 3 (the default) 229,274 155.4 3.53 s 11.78 s 11.18 s 49.09 s
A this PR, 3 180,951 (0.789x) 100.5 4.13 s 7.23 s 11.11 s 38.74 s
B this PR, 3 180,951 99.9 3.93 s 7.06 s 11.19 s 38.87 s
B 0.6.1, 3 (the default) 229,274 157.7 3.42 s 11.87 s 11.28 s 49.13 s

Every one of the 55 replies is identical by token_sha across the four runs. With the default three kept states this
PR prefills 21.1% fewer tokens than 0.6.1 and spends 35-37% fewer engine seconds in prefill; the p90 time to first
token falls by 39-41% and the p90 whole request by 21%. The TTFT median rises on both Sparks (by 0.60 and 0.51 s).
Paired by request, the median change is 0.00 and -0.03 s: the 49 requests whose cached length is the same are
unchanged, the 5 that now resume from a longer state start 7.4-7.7 s sooner, and the one fork back (see the trade-off
below) starts 4.3-4.4 s later.

On 0.6.0 (c464617, this PR's first base), the same replay prefilled 255,505 and 249,342 tokens on two Sparks against
180,951 with this rule (217,306 with six kept states), every reply identical across those five runs. 0.6.1 as released
prefills fewer tokens than 0.6.0 on this log; with this rule both prefill exactly 180,951, which a replay model of the
store (0.6.0's own PrefixCache, resume_points, MIN_GAP and entry_end with this rule) predicted before any GPU
run.

One trade-off: a later request that forks from an older point of a conversation that has moved on, rather than
continuing it, finds that point only if its state is still kept. The old rule kept resumed-from states longer; this one
keeps a conversation's newest state. On our traffic continuations far outnumber such forks.

Change

  • cuda/streams.py: PrefixCache._moved_past(entry): every longer entry that extends entry lies on one line (sorted
    by length, each extends the one before). _drop takes the first such entry among the candidates, then the existing
    rule. The cost is a prefix comparison per pair of kept states at a drop (at most keep squared, as longest already
    compares each entry once per request).
  • tests/test_cuda_prefix_cache_moved_past.py (host): two conversations sharing a system block resume from their newest
    states; the old rule's drop of the newest state; a block that diverging conversations extend stays; without
    extensions the old rule holds; evict and KVRoom take a moved-past state among those offered.

Tests

Host: macOS, Python 3.13, PyTorch 2.11.0 CPU.

  • tests/test_cuda_prefix_cache_moved_past.py, 7 cases. On 0.6.1 (17c73e1) 4 fail: the two conversations sharing a
    system block, the block that diverging conversations extend, and the two eviction cases (evict, KVRoom). The 3
    that pass on 0.6.1 check what this PR leaves as it was: the old rule's own behaviour, entries with no extension, and a
    state resumed from after its extension was kept.
  • tests/cuda/test_qwen27_multi.py::test_accept_and_prefix_cache keeps passing: there [1, 2] is resumed from after
    [1, 2, 3] was kept, so it is not moved past and the least recently used state goes, as before.
Run Result
Whole host suite (tests without tests/cuda and tests/test_qwen4_exp_prefill.py, whose indexer-kernel tests abort the interpreter on this Mac on main too), main (17c73e1) 33 failed, 4,309 passed, 364 skipped
Same, this branch 32 failed, 4,317 passed, 364 skipped

The same 32 ids fail on both, none in a file this PR changes. The 33rd on main,
tests/test_idle_release.py::test_a_stream_finishing_beside_another_leaves_the_cache_until_the_engine_is_idle, failed
a timing assertion while both suites ran at once; alone it passes 5 of 5 on main and 5 of 5 on this branch.
ruff check on the changed files passes.

GPU status

Two DGX Sparks (GB10), the tensorfold-bench image (PyTorch 2.13.0a0, CUDA 13.3), TORCH_CUDA_ARCH_LIST=12.1,
Qwen3.8-27B MLX 4-bit with the DFlash2 drafter, this PR's source on 0.6.1:

  • tests/cuda/test_qwen27_prompt_end_cache.py, test_qwen27_multi.py, test_qwen27_kv_room.py,
    test_qwen27_shared_kv.py, test_qwen27_checkpoint_slots.py: 121 passed on each Spark (102.8 s, 102.4 s); then the
    27B served with --parallel 2 and settled.
  • The replays above.

Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).

…s moved past goes first

A conversation that goes on keeps a state at each turn. Under the old rule the newest, not resumed from yet, went first
when another conversation kept its own, and the next turn resumed from an older state and prefilled the difference
again. Now a state goes first when every longer kept state extends it along one line and was kept after its last use;
a block that diverging conversations both extend (a shared system prompt), or a state resumed from after its extension
was kept (a fork back to it), stays, and otherwise the old rule applies. Which states exist changes, never their bits.
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @nood-co1. Replaying traffic from your own coding tools made this easy to judge. With the default three kept states the rule prefilled 21% fewer tokens, the p90 time to first token fell by about 40%, and every reply stayed identical by token_sha.

The Python engine is frozen (#286) and new work goes into the Zig engine, so this PR won't merge. The Qwen families aren't on the Zig engine yet, Zig CUDA doesn't serve yet, and no Zig engine keeps prompt states across requests, so there is no eviction rule there to fix today. When the Zig prompt cache is built, your rule of dropping first a state its own conversation has moved past is where its eviction starts, and that work carries your credit.

@ashhart ashhart closed this Oct 6, 2026
grearjake-star added a commit to grearjake-star/TensorFold that referenced this pull request Oct 8, 2026
…ved past goes first

Past keep, PrefixCache dropped the oldest kept prompt state never resumed from, else the oldest. When
conversations alternate, that dropped the state a conversation was about to resume from: its newest
state had not been resumed yet, while its older states (which the newest extends) had. One rule now
comes first: a kept state whose every longer kept state extends it along one line (its own
conversation went on and resumes from the longest) goes first. A state that diverging prompts both
extend, such as a shared system block, is never dropped by it, and without such a state the old rule
applies unchanged. Which states exist changes, never their bits. PrefixCache serves the Qwen3.8-27B and
Qwen3.6 engines; Flash Next keeps its states in its own slot logic and is unaffected.

Upstream: ashhart#190 (replayed coding-tool traffic: 21% fewer prefilled tokens, p90 TTFT
about -40%, replies identical by token_sha), closed unmerged with the Python engine frozen.

Co-authored-by: nood-co1 <[email protected]>
@ashhart

ashhart commented Oct 14, 2026

Copy link
Copy Markdown
Owner

@nood-co1, a correction. When we closed this we said it wouldn't merge. It had already shipped in 0.6.3 as your commit. Since then the native prompt cache has taken your rule: a kept state its own conversation has moved past goes first, and a shared system block or a state a conversation forks back to stays (zig/src/core/prompt_cache.zig).

As of f930fbb you are a co-author on main for it and for your other server work, and the credits page names #190 and #65. Sorry for the mixed message, and thank you for the replayed traffic that made the rule easy to trust.

plotarmordev added a commit to plotarmordev/TensorFold that referenced this pull request Nov 22, 2026
…n-190-follow-up-keep-u

Merge upstream main (ashhart#190 follow-up): keep upstream's H3 server idle ticker and fail-closed startup beside my draft/routed options
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