Repository navigation
Conversation
|
Is this the correct behavior? I agree it is represented in the KVCache, but consider this from mlx-lm: if args.system_prompt is not None:
messages.append({"role": "system", "content": args.system_prompt})
messages.append({"role": "user", "content": query})
prompt = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
)
last_response = None
for response in stream_generate(...That is inside the loop and it always injects the system prompt -- my understanding was this is what was expected. Gemini was a little wishy washy on answering :-) |
Good catch. The mlx_lm.chat loop does indeed add the system prompt every turn. Because it reuses the same prompt cache and stream_generate performs no prefix comparison, those tokens are physically appended again; the cache does not deduplicate them. |
9808efe to
ebf2f84
Compare
|
@davidkoski my initial approach here was a bit optimistic 🙂, in fact simplistic. I have now reworked the solution around exact token-prefix reuse rather than omitting static prompt after the first turn. The session still renders the canonical full prompt with instructions and prompt-side tool schemas, then verifies that the static-only render is an exact token prefix of that full prompt. On the first turn we prefill the full prompt and remember that reusable prefix; on later turns, if the prompt configuration is unchanged and the KV cache is safe to reuse, we pass only the suffix tokens that are not already represented in the cache. So the behavior should now match the right semantic model: instructions/tools remain part of every canonical prompt render, but already-cached static prefix tokens are not replayed through the model. This should improve performances when using the library from a coding agent. When eventually #360 will be merged, this can be improved further. |
|
I am still working on some issues, I will update this PR further and add some more comments. |
Render the canonical static+turn prompt fragment and store the proven static token prefix with the KV cache state. Subsequent turns prefill only the suffix of the full render when that prefix still matches, preserving the performance win without relying on role/template assumptions. Treat restored external caches as unknown prompt state and throw on prefix mismatch instead of appending incompatible tokens to an existing KV cache. Add model-prefill tests that prove suffix-only evaluation, avoid standalone dynamic-template regressions, and cover cache mismatch after prompt-affecting changes.
ebf2f84 to
a83868f
Compare
…ped-nax-20261026 Optimize guarded Gemma4 NAX prefill with token-row mapping
…donate Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Proposed changes
Avoid replaying static prompt material in long-lived
ChatSessioninstances.ChatSessionnow renders session instructions and prompt tool schemas only when the KV cache is first built, or afterclear(). Later turns append only incremental messages/tool results, while parser tool schemas remain available for tool-call parsing.Why
For chatbots and agents, static instructions are already represented in the KV cache after the first turn. Re-rendering them on every response wastes context and prefill work. For agents, repeated tool schemas can be especially expensive.
The existing
tools:API remains source-compatible. A groupedToolConfigurationsupports advanced prompt/parser schema separation without expanding every initializer.Notes
clear()resets the session cache, so the next response rebuilds the static prefix. Apps that change instructions mid-session should callclear()or create a new session.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes