test_fuyu.py 4.9 KB

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