2024-07-20 11:39:07 -05:00
|
|
|
import os
|
2024-07-03 11:34:00 +08:00
|
|
|
import re
|
2024-10-16 18:49:37 +08:00
|
|
|
from typing import List, Optional, Tuple, Type
|
2024-06-18 10:34:33 +08:00
|
|
|
|
|
|
|
import pytest
|
2024-10-30 10:32:17 -06:00
|
|
|
from transformers import AutoTokenizer
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-12-26 12:23:20 +08:00
|
|
|
from vllm.multimodal.image import rescale_image_size
|
2024-10-22 15:50:43 +08:00
|
|
|
from vllm.platforms import current_platform
|
2024-07-03 11:34:00 +08:00
|
|
|
from vllm.sequence import SampleLogprobs
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-10-30 10:32:17 -06:00
|
|
|
from ....conftest import IMAGE_ASSETS, HfRunner, PromptImageInput, VllmRunner
|
|
|
|
from ...utils import check_logprobs_close
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-06-26 16:02:34 +08:00
|
|
|
HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({
|
|
|
|
"stop_sign":
|
2024-06-18 10:34:33 +08:00
|
|
|
"<|user|>\n<|image_1|>\nWhat's the content of the image?<|end|>\n<|assistant|>\n", # noqa: E501
|
2024-06-26 16:02:34 +08:00
|
|
|
"cherry_blossom":
|
2024-07-03 11:34:00 +08:00
|
|
|
"<|user|>\n<|image_1|>\nWhat is the season?<|end|>\n<|assistant|>\n",
|
2024-06-26 16:02:34 +08:00
|
|
|
})
|
2024-08-25 19:51:20 +08:00
|
|
|
HF_MULTIIMAGE_IMAGE_PROMPT = "<|user|>\n<|image_1|>\n<|image_2|>\nDescribe these images.<|end|>\n<|assistant|>\n" # noqa: E501
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-08-21 18:36:24 -07:00
|
|
|
models = ["microsoft/Phi-3.5-vision-instruct"]
|
2024-06-18 10:34:33 +08:00
|
|
|
|
|
|
|
|
2024-07-03 11:34:00 +08:00
|
|
|
def vllm_to_hf_output(vllm_output: Tuple[List[int], str,
|
|
|
|
Optional[SampleLogprobs]],
|
2024-07-03 15:14:16 -07:00
|
|
|
model: str):
|
|
|
|
"""Sanitize vllm output to be comparable with hf output."""
|
|
|
|
_, output_str, out_logprobs = vllm_output
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-07-03 11:34:00 +08:00
|
|
|
output_str_without_image = re.sub(r"(<\|image_\d+\|>)+", "", output_str)
|
|
|
|
assert output_str_without_image[0] == " "
|
|
|
|
output_str_without_image = output_str_without_image[1:]
|
|
|
|
|
2024-07-04 09:58:18 +08:00
|
|
|
hf_output_str = output_str_without_image + "<|end|><|endoftext|>"
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-07-03 15:14:16 -07:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model)
|
2024-07-03 11:34:00 +08:00
|
|
|
hf_output_ids = tokenizer.encode(output_str_without_image)
|
|
|
|
assert hf_output_ids[0] == 1
|
|
|
|
hf_output_ids = hf_output_ids[1:]
|
|
|
|
|
|
|
|
return hf_output_ids, hf_output_str, out_logprobs
|
2024-06-18 10:34:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
target_dtype = "half"
|
|
|
|
|
2024-07-20 11:39:07 -05:00
|
|
|
# ROCm Triton FA can run into shared memory issues with these models,
|
|
|
|
# use other backends in the meantime
|
|
|
|
# FIXME (mattwong, gshtrasb, hongxiayan)
|
2024-10-28 12:07:00 +08:00
|
|
|
if current_platform.is_rocm():
|
2024-07-20 11:39:07 -05:00
|
|
|
os.environ["VLLM_USE_TRITON_FLASH_ATTN"] = "0"
|
|
|
|
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-06-29 23:45:54 +08:00
|
|
|
def run_test(
|
|
|
|
hf_runner: Type[HfRunner],
|
|
|
|
vllm_runner: Type[VllmRunner],
|
2024-09-07 16:38:23 +08:00
|
|
|
inputs: List[Tuple[List[str], PromptImageInput]],
|
2024-07-03 15:14:16 -07:00
|
|
|
model: str,
|
2024-06-29 23:45:54 +08:00
|
|
|
*,
|
|
|
|
dtype: str,
|
|
|
|
max_tokens: int,
|
2024-07-03 11:34:00 +08:00
|
|
|
num_logprobs: int,
|
2024-08-27 23:28:30 +08:00
|
|
|
mm_limit: int,
|
2024-06-29 23:45:54 +08:00
|
|
|
tensor_parallel_size: int,
|
|
|
|
distributed_executor_backend: Optional[str] = None,
|
|
|
|
):
|
2024-06-18 10:34:33 +08:00
|
|
|
"""Inference result should be the same between hf and vllm.
|
|
|
|
|
2024-09-14 01:20:06 +08:00
|
|
|
All the image fixtures for the test are from IMAGE_ASSETS.
|
2024-06-18 10:34:33 +08:00
|
|
|
For huggingface runner, we provide the PIL images as input.
|
2024-09-24 01:36:46 -06:00
|
|
|
For vllm runner, we provide MultiModalDataDict objects
|
2024-08-15 01:55:42 +08:00
|
|
|
and corresponding MultiModalConfig as input.
|
2024-06-18 10:34:33 +08:00
|
|
|
Note, the text input is also adjusted to abide by vllm contract.
|
|
|
|
The text output is sanitized to be able to compare with hf.
|
|
|
|
"""
|
2024-10-30 10:32:17 -06:00
|
|
|
# HACK - this is an attempted workaround for the following bug
|
|
|
|
# https://github.com/huggingface/transformers/issues/34307
|
|
|
|
from transformers import AutoImageProcessor # noqa: F401
|
|
|
|
from transformers import AutoProcessor # noqa: F401
|
2024-06-18 10:34:33 +08:00
|
|
|
|
2024-06-30 01:06:13 -07:00
|
|
|
# NOTE: take care of the order. run vLLM first, and then run HF.
|
|
|
|
# vLLM needs a fresh new process without cuda initialization.
|
|
|
|
# if we run HF first, the cuda initialization will be done and it
|
|
|
|
# will hurt multiprocessing backend with fork method (the default method).
|
2024-07-03 11:34:00 +08:00
|
|
|
# max_model_len should be greater than image_feature_size
|
2024-07-03 15:14:16 -07:00
|
|
|
with vllm_runner(model,
|
2024-10-19 02:31:58 +08:00
|
|
|
task="generate",
|
2024-07-03 11:34:00 +08:00
|
|
|
max_model_len=4096,
|
2024-09-29 00:54:35 +08:00
|
|
|
max_num_seqs=2,
|
2024-06-18 10:34:33 +08:00
|
|
|
dtype=dtype,
|
2024-08-27 23:28:30 +08:00
|
|
|
limit_mm_per_prompt={"image": mm_limit},
|
2024-06-29 23:45:54 +08:00
|
|
|
tensor_parallel_size=tensor_parallel_size,
|
|
|
|
distributed_executor_backend=distributed_executor_backend,
|
2024-07-03 15:14:16 -07:00
|
|
|
enforce_eager=True) as vllm_model:
|
2024-08-27 23:28:30 +08:00
|
|
|
vllm_outputs_per_case = [
|
2024-07-03 11:34:00 +08:00
|
|
|
vllm_model.generate_greedy_logprobs(prompts,
|
|
|
|
max_tokens,
|
|
|
|
num_logprobs=num_logprobs,
|
2024-07-27 19:53:07 +08:00
|
|
|
images=images)
|
2024-08-27 23:28:30 +08:00
|
|
|
for prompts, images in inputs
|
2024-06-30 01:06:13 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
# use eager mode for hf runner, since phi3_v didn't work with flash_attn
|
|
|
|
hf_model_kwargs = {"_attn_implementation": "eager"}
|
2024-07-03 15:14:16 -07:00
|
|
|
with hf_runner(model, dtype=dtype,
|
2024-06-30 01:06:13 -07:00
|
|
|
model_kwargs=hf_model_kwargs) as hf_model:
|
2024-07-03 11:34:00 +08:00
|
|
|
eos_token_id = hf_model.processor.tokenizer.eos_token_id
|
2024-08-27 23:28:30 +08:00
|
|
|
hf_outputs_per_case = [
|
2024-07-03 11:34:00 +08:00
|
|
|
hf_model.generate_greedy_logprobs_limit(prompts,
|
|
|
|
max_tokens,
|
|
|
|
num_logprobs=num_logprobs,
|
2024-07-27 19:53:07 +08:00
|
|
|
images=images,
|
2024-07-03 11:34:00 +08:00
|
|
|
eos_token_id=eos_token_id)
|
2024-08-27 23:28:30 +08:00
|
|
|
for prompts, images in inputs
|
2024-07-03 11:34:00 +08:00
|
|
|
]
|
2024-06-29 23:45:54 +08:00
|
|
|
|
2024-08-27 23:28:30 +08:00
|
|
|
for hf_outputs, vllm_outputs in zip(hf_outputs_per_case,
|
|
|
|
vllm_outputs_per_case):
|
2024-07-03 11:34:00 +08:00
|
|
|
check_logprobs_close(
|
|
|
|
outputs_0_lst=hf_outputs,
|
|
|
|
outputs_1_lst=[
|
2024-07-03 15:14:16 -07:00
|
|
|
vllm_to_hf_output(vllm_output, model)
|
2024-07-03 11:34:00 +08:00
|
|
|
for vllm_output in vllm_outputs
|
|
|
|
],
|
|
|
|
name_0="hf",
|
|
|
|
name_1="vllm",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Since we use _attn_implementation="eager" for hf_runner, there is more
|
|
|
|
# significant numerical difference. The basic `logprobs=5` fails to pass.
|
2024-07-03 15:14:16 -07:00
|
|
|
@pytest.mark.parametrize("model", models)
|
2024-07-03 11:34:00 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"size_factors",
|
|
|
|
[
|
|
|
|
# No image
|
|
|
|
[],
|
|
|
|
# Single-scale
|
|
|
|
[1.0],
|
|
|
|
# Single-scale, batched
|
|
|
|
[1.0, 1.0, 1.0],
|
|
|
|
# Multi-scale
|
|
|
|
[0.25, 0.5, 1.0],
|
|
|
|
],
|
|
|
|
)
|
2024-06-29 23:45:54 +08:00
|
|
|
@pytest.mark.parametrize("dtype", [target_dtype])
|
|
|
|
@pytest.mark.parametrize("max_tokens", [128])
|
2024-07-03 11:34:00 +08:00
|
|
|
@pytest.mark.parametrize("num_logprobs", [10])
|
2024-07-03 15:14:16 -07:00
|
|
|
def test_models(hf_runner, vllm_runner, image_assets, model, size_factors,
|
|
|
|
dtype: str, max_tokens: int, num_logprobs: int) -> None:
|
2024-08-27 23:28:30 +08:00
|
|
|
images = [asset.pil_image for asset in image_assets]
|
|
|
|
|
|
|
|
inputs_per_image = [(
|
|
|
|
[prompt for _ in size_factors],
|
|
|
|
[rescale_image_size(image, factor) for factor in size_factors],
|
|
|
|
) for image, prompt in zip(images, HF_IMAGE_PROMPTS)]
|
|
|
|
|
2024-06-29 23:45:54 +08:00
|
|
|
run_test(
|
|
|
|
hf_runner,
|
|
|
|
vllm_runner,
|
2024-08-27 23:28:30 +08:00
|
|
|
inputs_per_image,
|
2024-07-03 15:14:16 -07:00
|
|
|
model,
|
2024-06-29 23:45:54 +08:00
|
|
|
dtype=dtype,
|
|
|
|
max_tokens=max_tokens,
|
2024-07-03 11:34:00 +08:00
|
|
|
num_logprobs=num_logprobs,
|
2024-08-27 23:28:30 +08:00
|
|
|
mm_limit=1,
|
2024-06-29 23:45:54 +08:00
|
|
|
tensor_parallel_size=1,
|
|
|
|
)
|
2024-08-24 18:16:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("model", models)
|
|
|
|
@pytest.mark.parametrize("dtype", [target_dtype])
|
|
|
|
def test_regression_7840(hf_runner, vllm_runner, image_assets, model,
|
|
|
|
dtype) -> None:
|
2024-08-27 23:28:30 +08:00
|
|
|
images = [asset.pil_image for asset in image_assets]
|
|
|
|
|
|
|
|
inputs_regresion_7840 = [
|
|
|
|
([prompt], [image]) for image, prompt in zip(images, HF_IMAGE_PROMPTS)
|
|
|
|
]
|
|
|
|
|
2024-08-24 18:16:24 -07:00
|
|
|
# Regression test for #7840.
|
|
|
|
run_test(
|
|
|
|
hf_runner,
|
|
|
|
vllm_runner,
|
2024-08-27 23:28:30 +08:00
|
|
|
inputs_regresion_7840,
|
2024-08-24 18:16:24 -07:00
|
|
|
model,
|
|
|
|
dtype=dtype,
|
|
|
|
max_tokens=128,
|
|
|
|
num_logprobs=10,
|
2024-08-27 23:28:30 +08:00
|
|
|
mm_limit=1,
|
2024-08-24 18:16:24 -07:00
|
|
|
tensor_parallel_size=1,
|
|
|
|
)
|
2024-08-25 19:51:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("model", models)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"size_factors",
|
|
|
|
[
|
|
|
|
# No image
|
|
|
|
[],
|
|
|
|
# Single-scale
|
|
|
|
[1.0],
|
|
|
|
# Single-scale, batched
|
|
|
|
[1.0, 1.0, 1.0],
|
|
|
|
# Multi-scale
|
|
|
|
[0.25, 0.5, 1.0],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize("dtype", [target_dtype])
|
|
|
|
@pytest.mark.parametrize("max_tokens", [128])
|
2024-08-27 23:28:30 +08:00
|
|
|
@pytest.mark.parametrize("num_logprobs", [10])
|
2024-08-25 19:51:20 +08:00
|
|
|
def test_multi_images_models(hf_runner, vllm_runner, image_assets, model,
|
|
|
|
size_factors, dtype: str, max_tokens: int,
|
|
|
|
num_logprobs: int) -> None:
|
2024-08-27 23:28:30 +08:00
|
|
|
images = [asset.pil_image for asset in image_assets]
|
|
|
|
|
|
|
|
inputs_per_case = [
|
|
|
|
([HF_MULTIIMAGE_IMAGE_PROMPT for _ in size_factors],
|
|
|
|
[[rescale_image_size(image, factor) for image in images]
|
|
|
|
for factor in size_factors])
|
|
|
|
]
|
|
|
|
|
|
|
|
run_test(
|
2024-08-25 19:51:20 +08:00
|
|
|
hf_runner,
|
|
|
|
vllm_runner,
|
2024-08-27 23:28:30 +08:00
|
|
|
inputs_per_case,
|
2024-08-25 19:51:20 +08:00
|
|
|
model,
|
|
|
|
dtype=dtype,
|
|
|
|
max_tokens=max_tokens,
|
|
|
|
num_logprobs=num_logprobs,
|
2024-08-27 23:28:30 +08:00
|
|
|
mm_limit=2,
|
2024-08-25 19:51:20 +08:00
|
|
|
tensor_parallel_size=1,
|
|
|
|
)
|