[Bugfix][Multimodal] Frame the multi-modal hash digest input (#54283)
Signed-off-by: hotragn <hotragn.pettugani_2024@woxsen.edu.in> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
01c7bf8813
commit
a8d1aa9c99
@@ -268,3 +268,43 @@ def test_hash_media_io_noop_config_preserves_hash():
|
||||
assert hasher.hash_kwargs("blake3", image=loaded) == hasher.hash_kwargs(
|
||||
"blake3", image=plain
|
||||
)
|
||||
|
||||
|
||||
# The digest input is a concatenation of byte chunks, so it has to be uniquely
|
||||
# decodable. Each case below is a pair of distinct processor kwargs that used to
|
||||
# serialize to the same bytes, which made two requests share an mm hash -- and
|
||||
# therefore share both the processor cache entry and the prefix-cache block key.
|
||||
IMAGE = b"\x89PNG\r\n\x1a\n"
|
||||
|
||||
|
||||
def _hash(**mm_processor_kwargs: object) -> str:
|
||||
return MultiModalHasher.hash_kwargs(
|
||||
"blake3", model_id="m", image=IMAGE, **mm_processor_kwargs
|
||||
)
|
||||
|
||||
|
||||
def test_hash_collision_kwargs_key_value_boundary():
|
||||
# Both used to flatten to b"ab" + b"c".
|
||||
assert _hash(**{"ab": "c"}) != _hash(**{"a": "bc"})
|
||||
|
||||
|
||||
def test_hash_collision_nested_vs_flattened_key():
|
||||
nested = _hash(size={"shortest_edge": 224})
|
||||
flattened = _hash(**{"size.shortest_edge": 224})
|
||||
assert nested != flattened
|
||||
|
||||
|
||||
def test_hash_collision_sequence_vs_mapping():
|
||||
assert _hash(fps=[2, 4]) != _hash(fps={"0": 2, "1": 4})
|
||||
|
||||
|
||||
@pytest.mark.parametrize("empty", ["", b"", []])
|
||||
def test_hash_collision_none_vs_empty(empty):
|
||||
assert _hash(video_pruning_rate=None) != _hash(video_pruning_rate=empty)
|
||||
|
||||
|
||||
def test_hash_collision_empty_container_vs_omitted():
|
||||
omitted = _hash()
|
||||
assert _hash(size={}) != omitted
|
||||
assert _hash(size=[]) != omitted
|
||||
assert _hash(size={}) != _hash(size=[])
|
||||
|
||||
@@ -1324,3 +1324,40 @@ def test_processor_inputs_hashes_ignore_unrelated_kwargs():
|
||||
)
|
||||
|
||||
assert inputs.get_mm_hashes("test-model", "blake3") == {"image": ["image-uuid"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("left", "right"),
|
||||
[
|
||||
# Shifting the key/value boundary: both flatten to the dotted key
|
||||
# "mm_processor_kwargs.abc" followed by no value bytes.
|
||||
({"ab": "c"}, {"a": "bc"}),
|
||||
# A nested mapping and a caller-supplied dotted key flatten alike.
|
||||
({"size": {"shortest_edge": 224}}, {"size.shortest_edge": 224}),
|
||||
# A sequence and a mapping keyed by stringified indices flatten alike.
|
||||
({"fps": [2, 4]}, {"fps": {"0": 2, "1": 4}}),
|
||||
# None contributes the key alone, which a zero-byte value also does.
|
||||
({"video_pruning_rate": None}, {"video_pruning_rate": ""}),
|
||||
# An empty container contributes nothing, as does omitting the key.
|
||||
({"size": {}}, {}),
|
||||
],
|
||||
)
|
||||
def test_processor_inputs_hashes_distinguish_kwargs_shapes(left, right):
|
||||
"""Distinct processor kwargs must not share a multi-modal hash.
|
||||
|
||||
``hf_processor_mm_kwargs`` is per-request input, so both the keys and the
|
||||
values here are caller-controlled. The hash is the identity of the
|
||||
processor cache entry and is mixed into the prefix-cache block key, so two
|
||||
requests sharing one is a cross-request cache hit.
|
||||
"""
|
||||
image = random_image(np.random.RandomState(0), min_wh=8, max_wh=9)
|
||||
mm_data_items = MultiModalDataParser().parse_mm_data({"image": [image]})
|
||||
|
||||
def hash_with(hf_processor_mm_kwargs):
|
||||
return ProcessorInputs(
|
||||
prompt=[],
|
||||
mm_data_items=mm_data_items,
|
||||
hf_processor_mm_kwargs=hf_processor_mm_kwargs,
|
||||
).get_mm_hashes("test-model", "blake3")["image"][0]
|
||||
|
||||
assert hash_with(left) != hash_with(right)
|
||||
|
||||
Reference in New Issue
Block a user