-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Roger Wang <[email protected]>
- Loading branch information
Showing
4 changed files
with
78 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import pickle | ||
from blake3 import blake3 | ||
import torch | ||
import numpy as np | ||
from PIL import Image | ||
from typing import Iterable | ||
|
||
from vllm.logger import init_logger | ||
|
||
logger = init_logger(__name__) | ||
|
||
class MultiModalHasher: | ||
|
||
@classmethod | ||
def serialize_item(cls, obj: object) -> bytes: | ||
# Simple cases | ||
if isinstance(obj, str): | ||
return obj.encode("utf-8") | ||
if isinstance(obj, bytes): | ||
return obj | ||
if isinstance(obj, Image.Image): | ||
return obj.tobytes() | ||
|
||
# Convertible to NumPy arrays | ||
if isinstance(obj, torch.Tensor): | ||
obj = obj.numpy() | ||
if isinstance(obj, (int, float)): | ||
obj = np.array(obj) | ||
if isinstance(obj, np.ndarray): | ||
return obj.tobytes() | ||
|
||
logger.warning( | ||
"No serialization method found for %s. " | ||
"Falling back to pickle.", type(obj)) | ||
|
||
return pickle.dumps(obj) | ||
|
||
@classmethod | ||
def item_to_bytes(cls, | ||
key: str, | ||
obj: object, | ||
) -> Iterable[tuple[bytes, bytes]]: | ||
# Recursive cases | ||
if isinstance(obj, (list, tuple)): | ||
for i, elem in enumerate(obj): | ||
yield from cls.item_to_bytes(f"{key}.{i}", elem) | ||
elif isinstance(obj, dict): | ||
for k, v in obj.items(): | ||
yield from cls.item_to_bytes(f"{key}.{k}", v) | ||
else: | ||
key_bytes = cls.serialize_item(key) | ||
value_bytes = cls.serialize_item(obj) | ||
yield key_bytes, value_bytes | ||
|
||
@classmethod | ||
def hash_kwargs(cls, **kwargs: object) -> str: | ||
hasher = blake3() | ||
|
||
for k, v in kwargs.items(): | ||
for k_bytes, v_bytes in cls.item_to_bytes(k, v): | ||
hasher.update(k_bytes) | ||
hasher.update(v_bytes) | ||
|
||
return hasher.hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters