Repository navigation
Conversation
DelegatingSession reused the key/value cache only when everything it
retained was an exact prefix of the new request; any divergence reset and
re-encoded the whole conversation. A reasoning model (Qwen3, Gemma 4)
always diverges, because its cache holds the <think> block that the chat
template drops from history, so every turn after the first re-encoded
everything.
A family whose state is purely positional now keeps the longest common
encoded prefix. Doing that correctly needed the seed placement worked out,
and measuring it on exact CPU logits found that the existing continuation
was wrong on its own:
- The loops disagree on what a continuation (startPosition > 0) feeds.
Llama, Mistral, Phi3, Granite and every GPU prefill/decode loop feed
the seed at S and the prompt from S+1. The Qwen 3 loops (Qwen2/3,
Gemma 4, DeepSeek2, LFM2, gpt-oss) never feed the seed after position 0.
Each loop now records its convention on the State (ContinuationFeed).
- On the seed-first loops the session continued at its position with the
seed the loop left behind, which is the last sampled token. After a
stop that token was fed twice; after a budget cut, slot P-1 was never
written. The first answer logits of the next turn moved by up to 1.1.
The prompt-only loops had the same unwritten slot after a budget cut.
- dropReasoningFromHistory rewound position but kept that stale seed, so
a seed-first loop wrote the last reasoning token ahead of the answer.
The session now always rewinds to R = common - 1 and writes full[R] into
slot R again. On a seed-first loop full[R] is the reseeded token; on a
prompt-only loop it is the prompt's first token. Rewriting R recomputes
an identical entry when the slot was already written, and supplies the
missing write when it was not. Recurrent families, and states with no
recorded convention, keep the exact-prefix-or-reset behaviour.
Also fixes the host batched prefill, which with firstIndex()==1 fed the
last prompt token twice. That is the same defect generateWithPrefill
already fixed on the GPU. On Llama-3.2-1B Q8_0 at batch 4 the first
logits moved by 9.2 and the greedy token changed.
Qwen3-0.6B Q8_0 on CPU, a 6-turn chat at greedy with answers replayed
without reasoning, gave identical answers before and after:
prompt tokens fed per turn before 15/44/98/156/218/332 (863)
after 15/29/54/58/62/114 (332)
turn-6 prefill 7.04 s -> 2.71 s
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A chat session with a reasoning model (Qwen3, Gemma 4) re-encodes the whole conversation on every turn, so each turn's prefill grows with the length of the chat.
DelegatingSessionreuses its key/value cache only when everything it holds is an exact prefix of the new request; any difference resets the cache and encodes everything again. A reasoning model's cache holds the<think>block it generated, but the chat template drops reasoning from the history a client sends back. So the second request always differs from the cache at the previous answer, and every turn after the first starts from zero.Working out where to rewind also showed that the existing continuation was wrong in two cases:
The two loop families disagree on what a continuation feeds. Llama, Mistral, Phi-3, Granite and every GPU prefill/decode loop feed the seed token at the start position and the prompt after it. The Qwen 3-style loops (Qwen2/3, Gemma 4, DeepSeek2, LFM2, gpt-oss) never feed the seed after position 0. On the seed-first loops, a session continued with whatever seed the loop left behind:
Either way, the next turn's first logits moved by up to 1.1. The prompt-only loops had the same unwritten slot after a budget cut.
dropReasoningFromHistoryrewound the position but kept that stale seed, so a seed-first loop wrote the last reasoning token ahead of the answer.Also, the host batched prefill fed the last prompt token twice when
firstIndex() == 1; the GPU path had already been fixed for this. On Llama-3.2-1B Q8_0 at batch 4, the first logits moved by 9.2 and the greedy token changed.To reproduce: run a multi-turn chat on one session, sending each answer back without its reasoning as a chat client does, and compare the prompt tokens each turn feeds (
GenerationResult.promptTokens()) with a fresh session's. Onmain, Qwen3-0.6B Q8_0 at greedy:Fix
ContinuationFeed(new): each generation loop records on theStatewhich convention it follows,SEED_THEN_PROMPTorPROMPT_ONLY.DelegatingSession: for a family whose state is purely positional and has a recorded convention, the session keeps the longest common encoded prefixcommonof the cache and the request. It always rewinds toR = common - 1and writesfull[R]into slotRagain:full[R]is fed as the seed;Rewriting
Rrecomputes the identical entry when that slot was already written, and supplies the missing write when it wasn't, which fixes the stop-token and budget-cut cases above. Recurrent families, and states without a recorded convention, keep the exact-prefix-or-reset behaviour.dropReasoningFromHistoryreseeds with the right token.TokenGenerationLoop:ContinuationFeed.EncodedPrefix.commonPrefixLength(full): the length of the prefix the cache and the request share. A turn'spromptTokens()is what it actually fed, which the measurements below use.Formatting of the touched files, with the project's Spotless settings.
Testing
Unit tests:
./mvnw -Pjdk22plus testgives 980 tests and 0 failures.New accel tests (
-Pjdk22plus,accel-tests, Apple M4 Pro, all 10 pass). They compare exact CPU logits and tokens.ContinuationPlacementAccelTest:llamaFeedsTheSeedFirstAndTheRewoundShapeIsExactandqwen3FeedsThePromptOnlyAndTheRewoundShapeIsExact: a rewound continuation produces the same logits as encoding the whole prompt, for each convention.theHostBatchedPrefillFeedsTheLastPromptTokenOnce.ConversationAccelTest:End to end with the reproduction above, using
main's and this branch's jars, at greedy with a fixed seed:main: prompt tokens fed over 4 turnsNot covered