Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/v1/core/test_kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,37 @@ def test_request_block_hasher(hash_fn):
assert block_hashes[1] == hash_fn((block_hashes[0], (3, 4, 5), (("hash2", 0),)))


@pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor])
def test_request_block_hasher_incremental_append_with_multiple_mm_features(hash_fn):
mm_positions = [
PlaceholderRange(offset=4, length=2),
PlaceholderRange(offset=6, length=1),
]
incremental = make_request(
request_id="incremental",
prompt_token_ids=list(range(7)),
block_size=4,
hash_fn=hash_fn,
mm_positions=mm_positions,
mm_hashes=["A", "B"],
)
incremental.append_output_token_ids(7)
fresh = make_request(
request_id="fresh",
prompt_token_ids=list(range(8)),
block_size=4,
hash_fn=hash_fn,
mm_positions=mm_positions,
mm_hashes=["A", "B"],
)

expected_second_hash = hash_fn(
(incremental.block_hashes[0], (4, 5, 6, 7), (("A", 0), ("B", 2)))
)
assert incremental.block_hashes[1] == expected_second_hash
assert incremental.block_hashes == fresh.block_hashes


@pytest.mark.parametrize("hash_fn", [sha256, sha256_cbor])
def test_hash_tokens_different_mm_input(hash_fn):
request1 = make_request(
Expand Down
16 changes: 10 additions & 6 deletions vllm/v1/core/kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from vllm import envs
from vllm.config import VllmConfig
from vllm.logger import init_logger
from vllm.multimodal.utils import get_mm_features_in_window
from vllm.utils.hashing import xxhash, xxhash_cbor
from vllm.utils.math_utils import cdiv, round_up
from vllm.utils.mem_utils import format_gib
Expand Down Expand Up @@ -845,12 +846,15 @@ def request_block_hasher(request: Request) -> list[BlockHash]:
return []

curr_mm_idx = 0
if start_token_idx > 0:
# Set curr_mm_idx = -1 to indicate the last mm input.
# Note that since we reach to this branch only when the block is
# completed with generated tokens, we only need to consider the
# last mm input.
curr_mm_idx = -1
mm_features = request.mm_features
if start_token_idx > 0 and mm_features:
last_mm_pos = mm_features[-1].mm_position
if last_mm_pos.offset + last_mm_pos.length > start_token_idx:
curr_mm_idx, _ = get_mm_features_in_window(
mm_features,
start_token_idx,
start_token_idx + hash_block_size,
)

prev_block_hash_value = (
request.block_hashes[-1] if request.block_hashes else None
Expand Down
Loading