Repository navigation
Conversation
…rompts fork there Prompts which share everything but their last user turn, like the same few-shot examples followed by different inputs or the same document followed by different questions, reused only the system prompt + tools snapshot: the end snapshot of one prompt does not cover the next one, and the boundary snapshot ends before the first user turn. Every such prompt read the whole shared history again. _prefix_insert now finds the last user-turn marker as well as the first. When the prompt before this one shared everything up to it and went on differently (PrefixSlotCache::forked, against the prompt set_last_prompt remembered), it prefills up to the marker in a pass of its own and saves a boundary slot there, as it does for the system prompt + tools. As a boundary slot it is evicted after the end slots, and end_kv_from prefers it, so the end slots saved after it hold only the rows past it and keep it as their head. It is saved only when it is at least min_boundary tokens into the prompt and adds at least --prefix-min-gain tokens over the system prompt + tools slot and over what the prompt resumed from. forget_all clears the remembered prompt. A chat turn only continues the prompt before it, so it is no fork and saves nothing more: the extra prefill pass has a cost of its own whatever its length, and saving the history on every request made chat turns 2-4 on qwen3.6-moe about 3 s slower each. The first two prompts which share a history therefore read all of it, the later ones resume there. qwen3.8-mtp and qwen3.8-dflash2 save only boundary slots, so they save this one too. Test: test_shared_history and test_chat_saves_no_history in the prefix_slot_cache test; its prefill helper follows the new _prefix_insert. Docs: prefix_cache.md (how it works, the Qwen3.8 limit) and the Qwen3.8 model card.
|
Nice change: the numbers show a real win for few-shot / shared-document workloads, and gating the save on a detected fork so chat turns pay nothing is the right call. I read the diff statically (no build or run on my side); a few points before merge: Worth checking
Smaller points |
… head pass for a slot not kept An end slot keeps the rows of a head with pictures in it itself. resume() puts the rows before kv_from back from the engine's live context or from a self-contained slot with the same tokens, and matches both by tokens only, so the same tokens with another picture of the same size would put that picture's rows under the end slot. A history slot holds pictures when they were inside the part a prompt resumed from, like a chat with a picture whose later message is edited. A head is prefilled in a pass of its own only when its slot is kept (fits()). save() makes room for a self-contained slot by evicting any other one, so it refuses such a slot only when the cache is off or the slot alone is bigger than --prefix-cache-mb, which fits() checks and warns about once. The marker scan and the guards are in PrefixSlotCache (find_user_turns, plan_heads, end_kv_from over both heads), and _prefix_insert and the test's prefill helper both call them. forked() notes that it never selects rows, and that an earlier turn sent again is a fork. Test: test_find_user_turns, test_forked, test_plan_heads, test_head_not_kept_not_split and test_history_with_pictures in the prefix_slot_cache test. Docs: a prompt is compared only with the prompt just before it; a history is kept over every reply, so many different histories leave fewer slots for replies.
|
Thanks for the careful read. The new commit addresses 1, 2, 6 and 7, and 3 with a note in the docs; for 4 and 5 I explain below why I would keep them as they are.
|
Related to #737: this covers requests which share their earlier messages (few-shot example pairs, a document in an earlier message) and differ in the last one. A shared part and a payload inside one user message are not covered.
Why
Prompts which share everything but their last user turn, like the same few-shot examples followed by different inputs, or the same document followed by different questions, reuse only the system prompt + tools snapshot today. The end snapshot of one prompt does not cover the next one, and the boundary snapshot ends before the first user turn, so every such prompt reads the whole shared history again. With about 6.5K tokens of few-shot examples that is 26-31 s of prefill per request on
qwen3.6-moe:35b-a3band 63 s onqwen3.8-mtp:27b.What
_prefix_insertfinds the last user-turn marker as well as the first. In the Qwen templates tool results are a user turn too.PrefixSlotCache::forked(), against the promptset_last_prompt()remembers), it prefills up to the marker in a pass of its own and saves a boundary slot there, as it does for the system prompt + tools.end_kv_fromprefers it, so the end slots saved after it hold only the rows past it and keep it as their head. A head with pictures in it is never shared this way:resume()matches the rows beforekv_fromby tokens only, so such an end slot keeps its rows from the system prompt slot on, or all of them.min_boundary(512) tokens into the prompt, adds at least--prefix-min-gaintokens over the system prompt + tools slot and over the slot the prompt resumed from, and its slot is kept (fits(): the cache is on and the slot alone is within--prefix-cache-mb). The system prompt pass runs under the samefits()check.PrefixSlotCache(find_user_turns(),plan_heads(),end_kv_from()over both heads), called by_prefix_insertand by the test.forget_all()clears the remembered prompt.qwen3.8-mtpandqwen3.8-dflash2(boundary_only) save this slot too. They keep no end slots, so for them it is the only way to reuse a shared history.prefix_cache.md(how it works, that a prompt is compared only with the prompt just before it, that many different histories leave fewer slots for replies, the Qwen3.8 limit) and the Qwen3.8 model card.Why only when prompts fork
A chat never resumes from the start of its last user turn: its next turn continues from the end slot. Saving the history there would cost every chat turn a prefill pass of its own up to the marker, and a boundary slot per turn, which is evicted last and so would push out the end slots of other conversations. Waiting for the second prompt which forks at the same place costs one more full read for few-shot workloads and nothing for chats.
Testing
test_prefix_slot_cache(ascpu-tests.ymlruns it): newtest_shared_history(the history slot is saved from the second prompt on, the later prompts resume there, and it outlives the evicted end slots),test_chat_saves_no_history(a chat turn saves nothing before its last user turn, a fork from the same history does),test_find_user_turns,test_forked,test_plan_heads,test_head_not_kept_not_splitandtest_history_with_pictures(the same tokens with another picture never resume on the first picture's rows). Theprefillhelper calls the samePrefixSlotCachemethods as_prefix_insert. All pass, also merged with current main..debon Ubuntu 26.04 (GCC 15), no warnings from the changed files.qwen3.5:9bon the NPU: a red picture's history slot, then the same text with a blue picture of the same size, an unrelated request, and the question which colour the picture is. Before the fix the blue picture's end slot was saved on the red picture's history slot and the answer was "Red."; with it the end slot is self-contained and the answer is "Blue.", as from a cold run.qwen3.6-moe:35b-a3b, v1.0.13 against this branch,flm serve --ctx-len 16384. A 266-character system prompt (below 512 tokens, so no system prompt slot in either build) and 9 few-shot example pairs, followed by one input as the last user message, greedy, 32 tokens per answer. Prefill time:Request 1 differs by 4.5 s between the builds with the same prompt (other work loaded the CPU during the runs), so the cost of the extra pass in request 2 is within the noise of this run.
The same examples with a 676-token system prompt save both heads, the second request resuming at the system prompt and saving the history, the third resuming at the history:
qwen3.8-mtp:27b, v1.0.13 with this change applied (the same code),--ctx-len 16377, a sustained run of 19 requests with the same examples (about 6.6K tokens), each with ajson_schemaresponse format: the first two requests read everything in 63.4 and 63.3 s, the second one saving the history (6591 tokens). The other 17 resumed there and read the rest in 4.0-6.0 s:Notes
_prefix_insert, so it does not update the remembered prompt. At worst one more prompt reads everything.qwen3.6-moe:35b-a3bandqwen3.8-mtp:27bonly. The change applies to every model which uses_prefix_insert, each with its own user-turn marker. Models with a sliding window or a convolution state resume a slot only when the prompt starts with all of it, which is the case for the history slot.