Signed-off-by: amasen02 <amasen02@users.noreply.github.com> Co-authored-by: amasen02 <amasen02@users.noreply.github.com>
This commit is contained in:
co-authored by
amasen02
parent
b68408043d
commit
9ca6dbba71
@@ -1,6 +1,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import contextlib
|
||||
import hashlib
|
||||
import struct
|
||||
import uuid
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
@@ -8,7 +10,7 @@ from pathlib import Path
|
||||
import numpy as np
|
||||
import pytest
|
||||
import torch
|
||||
from PIL import Image, ImageDraw
|
||||
from PIL import Image, ImageDraw, ImageOps
|
||||
|
||||
from vllm.config.multimodal import MMHasherAlgorithm
|
||||
from vllm.multimodal.hasher import MultiModalHasher
|
||||
@@ -199,6 +201,33 @@ def test_hash_image_exif_id():
|
||||
)
|
||||
|
||||
|
||||
def test_hash_image_malformed_exif():
|
||||
# Test that images with malformed EXIF headers (e.g. invalid TIFF header)
|
||||
# do not raise an unhandled exception during hashing and fall back to image data.
|
||||
buf = BytesIO()
|
||||
Image.new("RGB", (64, 48)).save(buf, "JPEG")
|
||||
jpg = buf.getvalue()
|
||||
rest = jpg[2:]
|
||||
rest = rest[2 + struct.unpack(">H", rest[2:4])[0] :]
|
||||
payload = b"Exif\x00\x00XXXX\x00\x00\x00\x08" + bytes(32)
|
||||
data = b"\xff\xd8\xff\xe1" + struct.pack(">H", len(payload) + 2) + payload + rest
|
||||
|
||||
image = Image.open(BytesIO(data))
|
||||
with contextlib.suppress(Exception):
|
||||
image = ImageOps.exif_transpose(image)
|
||||
image.load()
|
||||
|
||||
hasher = MultiModalHasher
|
||||
# Should hash without raising SyntaxError or any other exception
|
||||
hash_val = hasher.hash_kwargs("blake3", image=image)
|
||||
assert isinstance(hash_val, str) and len(hash_val) > 0
|
||||
|
||||
# Also verify MediaWithBytes wrapping the image with malformed EXIF
|
||||
media_item = MediaWithBytes(image, data)
|
||||
hash_media = hasher.hash_kwargs("blake3", image=media_item)
|
||||
assert isinstance(hash_media, str) and len(hash_media) > 0
|
||||
|
||||
|
||||
def _rgba_png_bytes() -> bytes:
|
||||
image = Image.new("RGBA", (8, 8), (255, 0, 0, 128))
|
||||
buf = BytesIO()
|
||||
|
||||
+19
-10
@@ -48,6 +48,19 @@ def _get_hasher_factory(
|
||||
raise ValueError(f"Unsupported hash algorithm: {algorithm}")
|
||||
|
||||
|
||||
def _get_image_id_bytes(image: Image.Image) -> bytes | None:
|
||||
try:
|
||||
exif = image.getexif()
|
||||
image_id = exif.get(Image.ExifTags.Base.ImageID)
|
||||
if isinstance(image_id, uuid.UUID):
|
||||
return image_id.bytes
|
||||
except Exception:
|
||||
# Tolerate malformed EXIF metadata (e.g. invalid TIFF header)
|
||||
# and fall back to serializing raw image data or bytes.
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
class MultiModalHasher:
|
||||
@classmethod
|
||||
def serialize_item(cls, obj: object) -> Iterable[bytes | memoryview]:
|
||||
@@ -60,11 +73,9 @@ class MultiModalHasher:
|
||||
return (np.array(obj).tobytes(),)
|
||||
|
||||
if isinstance(obj, Image.Image):
|
||||
exif = obj.getexif()
|
||||
if Image.ExifTags.Base.ImageID in exif and isinstance(
|
||||
exif[Image.ExifTags.Base.ImageID], uuid.UUID
|
||||
):
|
||||
return (exif[Image.ExifTags.Base.ImageID].bytes,)
|
||||
image_id = _get_image_id_bytes(obj)
|
||||
if image_id is not None:
|
||||
return (image_id,)
|
||||
|
||||
data = {"mode": obj.mode, "data": np.asarray(obj)}
|
||||
palette = obj.palette
|
||||
@@ -76,11 +87,9 @@ class MultiModalHasher:
|
||||
return cls.iter_item_to_bytes("image", data)
|
||||
|
||||
if isinstance(obj, MediaWithBytes) and isinstance(obj.media, Image.Image):
|
||||
exif = obj.media.getexif()
|
||||
if Image.ExifTags.Base.ImageID in exif and isinstance(
|
||||
exif[Image.ExifTags.Base.ImageID], uuid.UUID
|
||||
):
|
||||
return (exif[Image.ExifTags.Base.ImageID].bytes,)
|
||||
image_id = _get_image_id_bytes(obj.media)
|
||||
if image_id is not None:
|
||||
return (image_id,)
|
||||
|
||||
if obj.io_config:
|
||||
return cls.iter_item_to_bytes(
|
||||
|
||||
Reference in New Issue
Block a user