[Bugfix][KV Cache] Fix incremental multimodal block hashing (#51694)

Signed-off-by: Jellow <49915976+CZT0@users.noreply.github.com>
Signed-off-by: Jellow <dvdx@foxmail.com>
This commit is contained in:
Jellow
2026-09-24 15:38:03 -07:00
committed by GitHub
parent 36f94d5fe8
commit e6c07ea576
2 changed files with 41 additions and 6 deletions
+31
View File
@@ -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(
+10 -6
View File
@@ -15,6 +15,7 @@ from typing import TYPE_CHECKING, Any, NamedTuple, NewType, TypeAlias, cast, ove
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
@@ -845,12 +846,15 @@ def get_request_block_hasher(
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