Add cache resume optimization for identical prompts#569
Closed
blightbow wants to merge 2 commits into
Closed
Conversation
Add support for skipping prompt prefill when the KV cache already contains the full prompt from a previous identical request. This provides 2.7-15x speedup for retry scenarios (e.g., chat "swiping"). Implementation: - Add can_resume_from_cache() to validate cache state - Add resume_from_cache parameter to generate_step() - Skip prefill loop when cache.offset >= prompt_length - Maintain all integrity constraints (RoPE, masks, slicing) The optimization is backwards compatible (defaults to False) and does not manipulate cache.offset directly, avoiding potential integrity issues with positional embeddings and attention masks. Tested with Qwen3-235B model: 2.048s → 0.749s (2.73x speedup) on 7-token prompt. Expected 6-15x on longer prompts (100-500 tokens).
Add comprehensive unit tests for the cache resume functionality: - test_can_resume_from_cache(): Tests the validation function with various cache states (empty, sufficient, insufficient tokens) - test_resume_from_cache_with_generate(): Tests end-to-end behavior with actual generation, verifying identical token outputs The tests verify that: 1. can_resume_from_cache() correctly identifies when cache is valid 2. Cache resume produces identical generation outputs 3. Cache offset increases correctly during resumed generation 4. The optimization works with both KVCache and RotatingKVCache All 18 prompt_cache tests pass successfully.
Member
|
Closing as we have more sophisticated cache reuse in |
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.
Add support for skipping prompt prefill when the KV cache already
contains the full prompt from a previous identical request. This
provides 2.7-15x speedup for retry scenarios (e.g., chat "swiping").
Implementation:
The optimization is backwards compatible (defaults to False) and
does not manipulate cache.offset directly, avoiding potential
integrity issues with positional embeddings and attention masks.
Add comprehensive unit tests for the cache resume functionality:
various cache states (empty, sufficient, insufficient tokens)
with actual generation, verifying identical token outputs
The tests verify that:
Tested with Qwen3-235B model: 2.048s → 0.749s (2.73x speedup)
on 7-token prompt. Expected 6-15x on longer prompts (100-500 tokens).