test_fuyu.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from typing import List, Optional, Tuple, Type
  2. import pytest
  3. from aphrodite.common.sequence import SampleLogprobs
  4. from aphrodite.common.utils import is_cpu
  5. from aphrodite.multimodal.utils import rescale_image_size
  6. from ..conftest import IMAGE_ASSETS, AphroditeRunner, HfRunner, _ImageAssets
  7. from .utils import check_logprobs_close
  8. pytestmark = pytest.mark.vlm
  9. HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({
  10. "stop_sign":
  11. "What's the content of the image?\n",
  12. "cherry_blossom":
  13. "What is the season?\n",
  14. })
  15. models = ["adept/fuyu-8b"]
  16. def aphrodite_to_hf_output(aphrodite_output: Tuple[List[int], str,
  17. Optional[SampleLogprobs]]):
  18. """Sanitize aphrodite output to be comparable with hf output."""
  19. output_ids, output_str, out_logprobs = aphrodite_output
  20. hf_output_str = output_str.lstrip() + "|ENDOFTEXT|"
  21. return output_ids, hf_output_str, out_logprobs
  22. def run_test(
  23. hf_runner: Type[HfRunner],
  24. aphrodite_runner: Type[AphroditeRunner],
  25. image_assets: _ImageAssets,
  26. model: str,
  27. *,
  28. size_factors: List[float],
  29. dtype: str,
  30. max_tokens: int,
  31. num_logprobs: int,
  32. tensor_parallel_size: int,
  33. distributed_executor_backend: Optional[str] = None,
  34. ):
  35. """Inference result should be the same between hf and aphrodite.
  36. All the image fixtures for the test is under tests/images.
  37. For huggingface runner, we provide the PIL images as input.
  38. For aphrodite runner, we provide MultiModalDataDict objects
  39. and corresponding MultiModalConfig as input.
  40. Note, the text input is also adjusted to abide by aphrodite contract.
  41. The text output is sanitized to be able to compare with hf.
  42. """
  43. images = [asset.pil_image for asset in image_assets]
  44. inputs_per_image = [(
  45. [prompt for _ in size_factors],
  46. [rescale_image_size(image, factor) for factor in size_factors],
  47. ) for image, prompt in zip(images, HF_IMAGE_PROMPTS)]
  48. # NOTE: take care of the order. run Aphrodite first, and then run HF.
  49. # Aphrodite needs a fresh new process without cuda initialization.
  50. # if we run HF first, the cuda initialization will be done and it
  51. # will hurt multiprocessing backend with fork method (the default method).
  52. # max_model_len should be greater than image_feature_size
  53. with aphrodite_runner(model,
  54. max_model_len=2560,
  55. max_num_seqs=1,
  56. dtype=dtype,
  57. tensor_parallel_size=tensor_parallel_size,
  58. distributed_executor_backend=distributed_executor_backend,
  59. enforce_eager=True) as aphrodite_model:
  60. aphrodite_outputs_per_image = [
  61. aphrodite_model.generate_greedy_logprobs(prompts,
  62. max_tokens,
  63. num_logprobs=num_logprobs,
  64. images=images)
  65. for prompts, images in inputs_per_image
  66. ]
  67. with hf_runner(model, dtype=dtype) as hf_model:
  68. hf_model.model.get_output_embeddings = lambda: \
  69. hf_model.model.language_model.get_output_embeddings()
  70. eos_token_id = hf_model.processor.tokenizer.eos_token_id
  71. hf_outputs_per_image = [
  72. hf_model.generate_greedy_logprobs_limit(prompts,
  73. max_tokens,
  74. num_logprobs=num_logprobs,
  75. images=images,
  76. eos_token_id=eos_token_id)
  77. for prompts, images in inputs_per_image
  78. ]
  79. for hf_outputs, aphrodite_outputs in zip(hf_outputs_per_image,
  80. aphrodite_outputs_per_image):
  81. check_logprobs_close(
  82. outputs_0_lst=hf_outputs,
  83. outputs_1_lst=[
  84. aphrodite_to_hf_output(aphrodite_output)
  85. for aphrodite_output in aphrodite_outputs
  86. ],
  87. name_0="hf",
  88. name_1="aphrodite",
  89. )
  90. target_dtype = "half"
  91. if is_cpu():
  92. target_dtype = "bfloat16"
  93. @pytest.mark.parametrize("model", models)
  94. @pytest.mark.parametrize(
  95. "size_factors",
  96. [
  97. # No image
  98. [],
  99. # Single-scale
  100. [0.25],
  101. # Single-scale, batched
  102. [0.25, 0.25, 0.25],
  103. # Multi-scale
  104. [0.25, 0.2, 0.15],
  105. ],
  106. )
  107. @pytest.mark.parametrize("dtype", [target_dtype])
  108. @pytest.mark.parametrize("max_tokens", [128])
  109. @pytest.mark.parametrize("num_logprobs", [10])
  110. def test_models(hf_runner, aphrodite_runner, image_assets, model, size_factors,
  111. dtype: str, max_tokens: int, num_logprobs: int) -> None:
  112. run_test(
  113. hf_runner,
  114. aphrodite_runner,
  115. image_assets,
  116. model,
  117. size_factors=size_factors,
  118. dtype=dtype,
  119. max_tokens=max_tokens,
  120. num_logprobs=num_logprobs,
  121. tensor_parallel_size=1,
  122. )