-
-
Notifications
You must be signed in to change notification settings - Fork 20.2k
Add Automatic Prefix Caching #2762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 61 commits
Commits
Show all changes
88 commits
Select commit
Hold shift + click to select a range
d1a91aa
init
SageMoore f27bfc8
Merge branch 'upstream-main' into prefix-caching
SageMoore ec21130
Move evictor and eviction policy to a separate class
ElizaWszola 73ab52c
format, replace match with if-else
ElizaWszola 76b5290
shore up some of the eviction logic
SageMoore fb9132b
autoformat
SageMoore c84bbda
Test block hashing
ElizaWszola be146c0
Format
ElizaWszola 063d2fb
added block allocator tests
SageMoore 15099d2
added timestamps to the PhysicalTokenBlock and updated the eviction l…
SageMoore 9411e06
Delete the free hash table from the evictor class
SageMoore 359b829
Remove the evictor class in favor of eviction free functions
SageMoore c9b0be6
debugging in progress
ElizaWszola cc80d1b
Merge branch 'prefix-caching' of https://github.com/neuralmagic/neura…
ElizaWszola 6218d1a
partial block support
ElizaWszola b35819d
Move PhysicalTokenBlock.last_accessed updates to the block_manager/sc…
SageMoore 38c1fc6
Remove overly aggressive assert
SageMoore b3e73f5
minor refactoring
SageMoore 48624d9
Add prefix len to eviction strategy
ElizaWszola 9780ccd
Merge branch 'prefix-caching' of https://github.com/neuralmagic/neura…
ElizaWszola bb471f2
fixed a few bugs in the partial block management code
SageMoore 5d5db12
auto format
SageMoore ffbddd9
fix fork/cow mechanisms so that they work with partial blocks
SageMoore 1f7fe42
replace the partial block table with a simpler promotion mechanism
SageMoore 7ab75d7
clean up the BlockSpaceManager a bit
SageMoore ca3e288
fix minor typos
SageMoore ecf389d
minor name change
SageMoore 427566a
update assert
SageMoore a3431bb
fix swap_in and swap_out
ElizaWszola dedc9c0
remove dead code in BlockSpaceManager
SageMoore 86299a4
refactor swap_in/swap_out in BlockSpaceManager
SageMoore 614a197
Update the partial block promotion logic to account for the full vers…
SageMoore 0f85474
remove min from sequence hash
ElizaWszola 9672b20
Remove prefix.py
SageMoore 6044c2b
misc formatting
SageMoore 9f7ae9f
bring back free table
ElizaWszola 6655130
format
ElizaWszola 1d6f0a0
update get_num_free_blocks to account for blocks in free table
SageMoore 0ca5c43
add some more asserts to BlockAllocator
SageMoore 7d6444d
contains_block() now looks at both table and free_table + a couple as…
ElizaWszola 4775423
updated semantics of prefix length in block
SageMoore 5cfee5f
bring back prefix block tables
ElizaWszola c64dd0d
Merge branch 'prefix-caching' of https://github.com/neuralmagic/neura…
ElizaWszola 4925071
Nits (style)
ElizaWszola 4fba5f9
delete comment
ElizaWszola 46c62e4
Added computed_block_nums
SageMoore 38b34d8
pythonize get_all_computed_block_ids
ElizaWszola ba97f80
account for prefix_len=0 in _prepare_prompt
SageMoore fe37722
attempt to fix build
SageMoore 28f4ad2
attempt to fix build
SageMoore bff30a7
cap computed blocks to prefix length
SageMoore e829c34
misc fixes
SageMoore 7f78ad4
typo
SageMoore 18da5e6
account for none
SageMoore 49357be
block manager refactoring
SageMoore ea4ec9d
clamp prefix length down to a multiple of block size
SageMoore f5fa2de
minor prefix length fix
SageMoore 704aa47
replace 16 with block size
SageMoore 8771b3f
First round of feedback changes
ElizaWszola b2c5992
Merge branch 'upstream-main' into prefix-caching
ElizaWszola 2dba195
added a flag to disable automatic prefix caching
SageMoore ba01fa8
Update vllm/engine/llm_engine.py
mgoin 16f9e80
Merge branch 'upstream-main' into prefix-caching
ElizaWszola 2914b5a
remove explicit prefix pos
ElizaWszola bd235fd
remove assert for sliding window, check what will happen
ElizaWszola ba382d9
Try the other way around
ElizaWszola 660007f
Delete redundant prefix caching test
ElizaWszola f74f67d
Don't add last block to
ElizaWszola 093cb1c
Format
ElizaWszola d459d15
refactored the eviction logic into a separate class
SageMoore fea6789
minor fixes
SageMoore 052c294
format evictor file
SageMoore e26cd8e
added documentation to the evictor class
SageMoore 2335360
delete newline
SageMoore d66154c
format
SageMoore 6a38439
Fix timestamp in eviction policy
ElizaWszola 47e94ba
Merge branch 'upstream-main' into prefix-caching
ElizaWszola a449eb6
addressing review comments
SageMoore 30708b8
minor evictor fix
SageMoore 4e99660
format
SageMoore 5b4413b
More protection against sliding window
ElizaWszola 63a1985
Merge branch 'prefix-caching' of https://github.com/neuralmagic/neura…
ElizaWszola 7d17304
Change automatic prefix caching arg to enable in arg utils
ElizaWszola 6358bf0
fix minor BlockAllocator update_hash bug
SageMoore b9fbb66
fix test_prefix_caching test
SageMoore 4ce8ceb
fix minor perf regression
SageMoore 11126ab
Only mark last prefix block as computed, assume no computed blocks wi…
ElizaWszola e252bb6
Merge branch 'main' into prefix-caching
SageMoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Test hashing of cache blocks. | ||
|
|
||
| Run `pytest tests/test_cache_block_hashing.py`. | ||
| """ | ||
| import pytest | ||
|
|
||
| from vllm.transformers_utils.tokenizer import TokenizerGroup | ||
| from vllm.sequence import Sequence | ||
|
|
||
| # Make two prefixes with different first blocks. | ||
| prefix_start = [("You are an expert"), ("You are a")] | ||
| prefix_common = ( | ||
| " school principal, skilled in effectively managing " | ||
| "faculty and staff. Draft 10-15 questions for a potential first grade " | ||
| "Head Teacher for my K-12, all-girls', independent school that emphasizes " | ||
| "community, joyful discovery, and life-long learning. The candidate is " | ||
| "coming in for a first-round panel interview for a 8th grade Math " | ||
| "teaching role. They have 5 years of previous teaching experience " | ||
| "as an assistant teacher at a co-ed, public school with experience " | ||
| "in middle school math teaching. Based on this, fulfill " | ||
| "the following: ") | ||
| prefixes = [start + prefix_common for start in prefix_start] | ||
|
|
||
| # Sample prompts. | ||
| sample_prompts = [ | ||
| "Hello, my name is", "The president of the United States is", | ||
| "The capital of France is", "The future of AI is" | ||
| ] | ||
|
|
||
|
|
||
| # Helper function. | ||
| def flatten_2d(li): | ||
| return [lss for ls in li for lss in ls] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", ["facebook/opt-125m"]) | ||
| @pytest.mark.parametrize("block_size", [16]) | ||
| @pytest.mark.parametrize("max_num_seqs", [256]) | ||
| def test_auto_prefix_caching(model: str, block_size: int, max_num_seqs: int): | ||
|
|
||
| tokenizer = TokenizerGroup( | ||
| tokenizer_id="facebook/opt-125m", | ||
| enable_lora=False, | ||
| max_num_seqs=max_num_seqs, | ||
| max_input_length=None, | ||
| ) | ||
|
|
||
| hashes = [] | ||
|
|
||
| for prefix in prefixes: | ||
| hashes.append([]) | ||
| prompts = [prefix + prompt for prompt in sample_prompts] | ||
| seq_id = 0 | ||
| for prompt in prompts: | ||
| hashes[-1].append([]) | ||
| prompt_token_ids = tokenizer.encode(prompt) | ||
| seq = Sequence(seq_id, prompt, prompt_token_ids, block_size) | ||
|
|
||
| num_blocks = len(prompt_token_ids) // block_size | ||
| for idx in range(num_blocks): | ||
| hashes[-1][-1].append(seq.hash(idx)) | ||
|
|
||
| seq_id += 1 | ||
|
|
||
| # Check that hashes made with two prefixes with different first blocks are | ||
| # different everywhere. | ||
| for hash0, hash1 in zip(flatten_2d(hashes[0]), flatten_2d(hashes[1])): | ||
| assert (hash0 != hash1) | ||
|
|
||
| # Check that hashes of different prompts made with the same prefix are the | ||
| # same until the hashes that contain the prompt. | ||
| for hash_pref in hashes: | ||
| same_hashes = [tuple(h[:-1]) for h in hash_pref] | ||
| different_hashes = [h[-1] for h in hash_pref] | ||
| assert (len(set(same_hashes)) == 1) | ||
| assert (len(set(different_hashes)) == len(different_hashes)) |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.