test_chameleon.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from typing import List, Optional, Type
  2. import pytest
  3. from transformers import AutoModelForVision2Seq, BatchEncoding
  4. from aphrodite.common.utils import STR_DTYPE_TO_TORCH_DTYPE
  5. from aphrodite.multimodal.utils import rescale_image_size
  6. from ....conftest import IMAGE_ASSETS, AphroditeRunner, HfRunner, _ImageAssets
  7. from ...utils import check_outputs_equal
  8. HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({
  9. "stop_sign":
  10. "USER: <image>\nWhat's the content of the image?\nASSISTANT:",
  11. "cherry_blossom":
  12. "USER: <image>\nWhat is the season?\nASSISTANT:",
  13. })
  14. models = ["facebook/chameleon-7b"]
  15. def run_test(
  16. hf_runner: Type[HfRunner],
  17. aphrodite_runner: Type[AphroditeRunner],
  18. image_assets: _ImageAssets,
  19. model: str,
  20. *,
  21. size_factors: List[float],
  22. dtype: str,
  23. max_tokens: int,
  24. num_logprobs: int,
  25. tensor_parallel_size: int,
  26. distributed_executor_backend: Optional[str] = None,
  27. ):
  28. """Inference result should be the same between hf and aphrodite.
  29. All the image fixtures for the test are from IMAGE_ASSETS.
  30. For huggingface runner, we provide the PIL images as input.
  31. For aphrodite runner, we provide MultiModalDataDict objects
  32. and corresponding vision language config as input.
  33. Note, the text input is also adjusted to abide by aphrodite contract.
  34. The text output is sanitized to be able to compare with hf.
  35. """
  36. torch_dtype = STR_DTYPE_TO_TORCH_DTYPE[dtype]
  37. images = [asset.pil_image for asset in image_assets]
  38. inputs_per_image = [(
  39. [prompt for _ in size_factors],
  40. [rescale_image_size(image, factor) for factor in size_factors],
  41. ) for image, prompt in zip(images, HF_IMAGE_PROMPTS)]
  42. with aphrodite_runner(model,
  43. max_model_len=4096,
  44. dtype=dtype,
  45. tensor_parallel_size=tensor_parallel_size,
  46. distributed_executor_backend=distributed_executor_backend,
  47. enforce_eager=True) as aphrodite_model:
  48. aphrodite_outputs_per_image = [
  49. aphrodite_model.generate_greedy_logprobs(prompts,
  50. max_tokens,
  51. num_logprobs=num_logprobs,
  52. images=images)
  53. for prompts, images in inputs_per_image
  54. ]
  55. def process(hf_inputs: BatchEncoding):
  56. hf_inputs["pixel_values"] = hf_inputs["pixel_values"] \
  57. .to(torch_dtype) # type: ignore
  58. return hf_inputs
  59. with hf_runner(model,
  60. dtype=dtype,
  61. postprocess_inputs=process,
  62. auto_cls=AutoModelForVision2Seq) as hf_model:
  63. hf_outputs_per_image = [
  64. hf_model.generate_greedy_logprobs_limit(prompts,
  65. max_tokens,
  66. num_logprobs=num_logprobs,
  67. images=images)
  68. for prompts, images in inputs_per_image
  69. ]
  70. for hf_outputs, aphrodite_outputs in zip(hf_outputs_per_image,
  71. aphrodite_outputs_per_image):
  72. # HF Logprobs include image tokens, unlike Aphrodite, so we don't
  73. # directly compare them
  74. check_outputs_equal(
  75. outputs_0_lst=[outputs[:2] for outputs in hf_outputs],
  76. outputs_1_lst=[outputs[:2] for outputs in aphrodite_outputs],
  77. name_0="hf",
  78. name_1="aphrodite",
  79. )
  80. @pytest.mark.parametrize("model", models)
  81. @pytest.mark.parametrize(
  82. "size_factors",
  83. [
  84. # No image
  85. [],
  86. # Single-scale
  87. [1.0],
  88. # Single-scale, batched
  89. [1.0, 1.0, 1.0],
  90. # Multi-scale
  91. [0.25, 0.5, 1.0],
  92. ],
  93. )
  94. @pytest.mark.parametrize("dtype", ["bfloat16"])
  95. @pytest.mark.parametrize("max_tokens", [8])
  96. @pytest.mark.parametrize("num_logprobs", [5])
  97. def test_models(hf_runner, aphrodite_runner, image_assets, model, size_factors,
  98. dtype, max_tokens, num_logprobs) -> None:
  99. run_test(
  100. hf_runner,
  101. aphrodite_runner,
  102. image_assets,
  103. model,
  104. size_factors=size_factors,
  105. dtype=dtype,
  106. max_tokens=max_tokens,
  107. num_logprobs=num_logprobs,
  108. tensor_parallel_size=1,
  109. )