fix: ensure prompt cache is invalidated whenever KV cache is cleared - #542
Open
thunderbug1 wants to merge 1 commit into
Open
fix: ensure prompt cache is invalidated whenever KV cache is cleared#542thunderbug1 wants to merge 1 commit into
thunderbug1 wants to merge 1 commit into
Conversation
…OCm#486) The prompt prefix cache in /v1/chat/completions was broken in several ways: 1. Non-streaming path unconditionally cleared context and reset cache, making prefix caching impossible for non-streaming requests. 2. update_checksum() included tool messages in its checksum calculation while can_use_cache() skipped them, causing permanent cache misses whenever tools were present in the conversation. 3. Error paths (max length, exceptions) in handle_openai_chat_completion cleared the KV cache via clear_context() but did not call prompt_cache.reset(), so the next request would stale cache-hit against an empty KV cache and only insert the last message. 4. Other handlers (/api/chat, /api/generate, /v1/completions) all call clear_context() on every request but never invalidated the prompt cache, causing the same stale-hit problem for any interleaved requests. Fix all cases by introducing a reset_context() helper on RestHandler that couples clear_context() with prompt_cache.reset(), ensuring the prompt cache can never outlive the KV state it refers to. The only bare clear_context() calls that remain are the two cache-miss paths in handle_openai_chat_completion where the cache is immediately re-initialized with update_checksum().
Contributor
Thank you! Currently, prefix cache is not activated for non-stream mode. Any specific app in your mind? |
Author
|
I was playing around with using claude code with flm and found some issues with kv caching. The main issue in the end was actually that claude code changes the system prompt with every message but I fixed the other issues on the way. |
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.
Summary
Fixes #486
The prompt prefix cache in
/v1/chat/completionswas broken in multiple ways, preventing multi-turn conversations from reusing the cached KV state.clear_context()andprompt_cache.reset(), making prefix caching impossible for non-streaming requestsupdate_checksum()included tool messages whilecan_use_cache()skipped them, causing permanent cache misses when tools were present/api/chat,/api/generate,/v1/completionsall clear the KV cache without invalidating the prompt cacheChanges
clear_context()andprompt_cache.reset()from the non-streaming path inhandle_openai_chat_completionupdate_checksum()to skip tool messages consistently withcan_use_cache()reset_context()helper onRestHandlerthat couplesclear_context()withprompt_cache.reset()clear_context()calls across all handlers withreset_context(), except the two cache-miss paths where the cache is immediately re-initializedTest plan
flm servewith a model/v1/chat/completionsand verify "Use cached prompt!" appears in logs for subsequent turns/api/chator/api/generatedon't cause stale cache hits on the next/v1/chat/completionsrequest