From e6c07ea5763bada5da789ed437851ee1526fd5a8 Mon Sep 17 00:00:00 2001 From: Jellow Date: Fri, 25 Sep 2026 06:38:03 +0800 Subject: [PATCH] [Bugfix][KV Cache] Fix incremental multimodal block hashing (#51694) Signed-off-by: Jellow <49915976+CZT0@users.noreply.github.com> Signed-off-by: Jellow --- tests/v1/core/test_kv_cache_utils.py | 31 ++++++++++++++++++++++++++++ vllm/v1/core/kv_cache_utils.py | 16 ++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/tests/v1/core/test_kv_cache_utils.py b/tests/v1/core/test_kv_cache_utils.py index bc2a761d86..24be16e4a5 100644 --- a/tests/v1/core/test_kv_cache_utils.py +++ b/tests/v1/core/test_kv_cache_utils.py @@ -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( diff --git a/vllm/v1/core/kv_cache_utils.py b/vllm/v1/core/kv_cache_utils.py index af1112c54a..8d60b09fa3 100644 --- a/vllm/v1/core/kv_cache_utils.py +++ b/vllm/v1/core/kv_cache_utils.py @@ -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