Repository navigation
Conversation
…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.
9677bc9 to
8a33d69
Compare
|
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 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. |
…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]>
|
@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 ( 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. |
…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
Summary
Past
keep,PrefixCachedrops 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 aconversation 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
PrefixCachedrops a state: pastkeepinadd, and for memory inevict(KVRoom), so both CUDA families that use it (qwen3_5,qwen3_5_moe) getit, 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 repeatsin our logs:
state at turn n (never resumed from yet);
keep, the never-resumed state goes: the main conversation's turn n;tests/test_cuda_prefix_cache_moved_past.pyreproduces 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 runningboth builds, in opposite orders:
Every one of the 55 replies is identical by
token_shaacross the four runs. With the default three kept states thisPR 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 against180,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_GAPandentry_endwith this rule) predicted before any GPUrun.
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 extendsentrylies on one line (sortedby length, each extends the one before).
_droptakes the first such entry among the candidates, then the existingrule. The cost is a prefix comparison per pair of kept states at a drop (at most
keepsquared, aslongestalreadycompares each entry once per request).
tests/test_cuda_prefix_cache_moved_past.py(host): two conversations sharing a system block resume from their neweststates; the old rule's drop of the newest state; a block that diverging conversations extend stays; without
extensions the old rule holds;
evictandKVRoomtake 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 asystem block, the block that diverging conversations extend, and the two eviction cases (
evict,KVRoom). The 3that 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_cachekeeps 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.testswithouttests/cudaandtests/test_qwen4_exp_prefill.py, whose indexer-kernel tests abort the interpreter on this Mac onmaintoo),main(17c73e1)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, faileda timing assertion while both suites ran at once; alone it passes 5 of 5 on
mainand 5 of 5 on this branch.ruff checkon the changed files passes.GPU status
Two DGX Sparks (GB10), the
tensorfold-benchimage (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 the27B served with
--parallel 2and settled.Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).