2024-06-03 13:56:41 +08:00
|
|
|
import numpy as np
|
|
|
|
import pytest
|
2024-06-10 20:47:15 +08:00
|
|
|
from transformers import CLIPImageProcessor, LlavaNextImageProcessor
|
2024-06-03 13:56:41 +08:00
|
|
|
|
2024-07-02 00:57:09 -07:00
|
|
|
from vllm.config import ModelConfig
|
2024-06-03 13:56:41 +08:00
|
|
|
from vllm.multimodal import MULTIMODAL_REGISTRY
|
|
|
|
|
2024-06-04 12:01:46 +08:00
|
|
|
from ..conftest import _STR_DTYPE_TO_TORCH_DTYPE
|
2024-06-03 13:56:41 +08:00
|
|
|
|
2024-06-04 12:01:46 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", ["half", "float"])
|
2024-06-26 16:02:34 +08:00
|
|
|
def test_clip_image_processor(image_assets, dtype):
|
2024-06-03 13:56:41 +08:00
|
|
|
MODEL_NAME = "llava-hf/llava-1.5-7b-hf"
|
|
|
|
|
|
|
|
hf_processor = CLIPImageProcessor.from_pretrained(MODEL_NAME)
|
|
|
|
assert isinstance(hf_processor, CLIPImageProcessor)
|
|
|
|
|
|
|
|
model_config = ModelConfig(
|
|
|
|
model=MODEL_NAME,
|
|
|
|
tokenizer=MODEL_NAME,
|
|
|
|
tokenizer_mode="auto",
|
|
|
|
trust_remote_code=False,
|
|
|
|
seed=0,
|
|
|
|
dtype=dtype,
|
|
|
|
revision=None,
|
|
|
|
)
|
|
|
|
|
2024-06-26 16:02:34 +08:00
|
|
|
for asset in image_assets:
|
2024-06-03 13:56:41 +08:00
|
|
|
hf_result = hf_processor.preprocess(
|
2024-06-26 16:02:34 +08:00
|
|
|
asset.pil_image,
|
2024-06-04 12:01:46 +08:00
|
|
|
return_tensors="pt",
|
|
|
|
).to(dtype=_STR_DTYPE_TO_TORCH_DTYPE[dtype])
|
2024-06-28 20:09:56 +08:00
|
|
|
vllm_result = MULTIMODAL_REGISTRY.map_input(
|
|
|
|
model_config,
|
2024-07-02 00:57:09 -07:00
|
|
|
{"image": asset.pil_image},
|
2024-06-03 13:56:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert hf_result.keys() == vllm_result.keys()
|
2024-06-04 12:01:46 +08:00
|
|
|
for key, hf_tensor in hf_result.items():
|
|
|
|
hf_arr: np.ndarray = hf_tensor.numpy()
|
2024-06-03 13:56:41 +08:00
|
|
|
vllm_arr: np.ndarray = vllm_result[key].numpy()
|
|
|
|
|
|
|
|
assert hf_arr.shape == vllm_arr.shape, f"Failed for key={key}"
|
|
|
|
assert np.allclose(hf_arr, vllm_arr), f"Failed for key={key}"
|
|
|
|
|
|
|
|
|
2024-06-10 20:47:15 +08:00
|
|
|
@pytest.mark.xfail(
|
|
|
|
reason="Inconsistent image processor being used due to lack "
|
|
|
|
"of support for dynamic image token replacement")
|
|
|
|
@pytest.mark.parametrize("dtype", ["half", "float"])
|
2024-06-26 16:02:34 +08:00
|
|
|
def test_llava_next_image_processor(image_assets, dtype):
|
2024-06-10 20:47:15 +08:00
|
|
|
MODEL_NAME = "llava-hf/llava-v1.6-34b-hf"
|
|
|
|
|
|
|
|
hf_processor = LlavaNextImageProcessor.from_pretrained(MODEL_NAME)
|
|
|
|
assert isinstance(hf_processor, LlavaNextImageProcessor)
|
|
|
|
|
|
|
|
model_config = ModelConfig(
|
|
|
|
model=MODEL_NAME,
|
|
|
|
tokenizer=MODEL_NAME,
|
|
|
|
tokenizer_mode="auto",
|
|
|
|
trust_remote_code=False,
|
|
|
|
seed=0,
|
|
|
|
dtype=dtype,
|
|
|
|
revision=None,
|
|
|
|
)
|
|
|
|
|
2024-06-26 16:02:34 +08:00
|
|
|
for asset in image_assets:
|
2024-06-10 20:47:15 +08:00
|
|
|
hf_result = hf_processor.preprocess(
|
2024-06-26 16:02:34 +08:00
|
|
|
asset.pil_image,
|
2024-06-10 20:47:15 +08:00
|
|
|
return_tensors="pt",
|
|
|
|
).to(dtype=_STR_DTYPE_TO_TORCH_DTYPE[dtype])
|
2024-06-28 20:09:56 +08:00
|
|
|
vllm_result = MULTIMODAL_REGISTRY.map_input(
|
|
|
|
model_config,
|
2024-07-02 00:57:09 -07:00
|
|
|
{"image": asset.pil_image},
|
2024-06-10 20:47:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert hf_result.keys() == vllm_result.keys()
|
|
|
|
for key, hf_tensor in hf_result.items():
|
|
|
|
hf_arr: np.ndarray = hf_tensor.numpy()
|
|
|
|
vllm_arr: np.ndarray = vllm_result[key].numpy()
|
|
|
|
|
|
|
|
assert hf_arr.shape == vllm_arr.shape, f"Failed for key={key}"
|
|
|
|
assert np.allclose(hf_arr, vllm_arr), f"Failed for key={key}"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail(
|
|
|
|
reason="Example image pixels were not processed using HuggingFace")
|
2024-06-03 13:56:41 +08:00
|
|
|
@pytest.mark.parametrize("dtype", ["float"])
|
2024-06-26 16:02:34 +08:00
|
|
|
def test_image_pixel_types(image_assets, dtype):
|
2024-06-03 13:56:41 +08:00
|
|
|
MODEL_NAME = "llava-hf/llava-1.5-7b-hf"
|
|
|
|
|
|
|
|
model_config = ModelConfig(
|
|
|
|
model=MODEL_NAME,
|
|
|
|
tokenizer=MODEL_NAME,
|
|
|
|
tokenizer_mode="auto",
|
|
|
|
trust_remote_code=False,
|
|
|
|
seed=0,
|
|
|
|
dtype=dtype,
|
|
|
|
revision=None,
|
2024-07-02 00:57:09 -07:00
|
|
|
)
|
2024-06-26 16:02:34 +08:00
|
|
|
for asset in image_assets:
|
2024-06-28 20:09:56 +08:00
|
|
|
image_result = MULTIMODAL_REGISTRY.map_input(
|
|
|
|
model_config,
|
2024-07-02 00:57:09 -07:00
|
|
|
{"image": asset.pil_image},
|
2024-06-03 13:56:41 +08:00
|
|
|
)
|
2024-06-28 20:09:56 +08:00
|
|
|
tensor_result = MULTIMODAL_REGISTRY.map_input(
|
|
|
|
model_config,
|
2024-07-02 00:57:09 -07:00
|
|
|
{"image": asset.pil_image},
|
2024-06-03 13:56:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert image_result.keys() == tensor_result.keys()
|
|
|
|
for key, image_arr in image_result.items():
|
|
|
|
tensor_arr: np.ndarray = tensor_result[key].numpy()
|
|
|
|
|
|
|
|
assert image_arr.shape == tensor_arr.shape, f"Failed for key={key}"
|
2024-06-10 20:47:15 +08:00
|
|
|
assert np.allclose(image_arr, tensor_arr), f"Failed for key={key}"
|