test_blip2.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from typing import List, Optional, Tuple
  2. import pytest
  3. from transformers import AutoModelForVision2Seq, AutoTokenizer
  4. from aphrodite.common.sequence import SampleLogprobs
  5. from aphrodite.multimodal.utils import rescale_image_size
  6. from ....conftest import IMAGE_ASSETS
  7. from ...utils import check_logprobs_close
  8. HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({
  9. "stop_sign":
  10. "Question: What's the content of the image? Answer:",
  11. "cherry_blossom":
  12. "Question: What is the season? Answer:",
  13. })
  14. def aphrodite_to_hf_output(aphrodite_output: Tuple[List[int], str,
  15. Optional[SampleLogprobs]],
  16. model: str):
  17. """Sanitize aphrodite output to be comparable with hf output."""
  18. _, output_str, out_logprobs = aphrodite_output
  19. hf_output_str = output_str + "\n"
  20. tokenizer = AutoTokenizer.from_pretrained(model)
  21. hf_output_ids = tokenizer.encode(hf_output_str)
  22. assert hf_output_ids[0] == tokenizer.bos_token_id
  23. hf_output_ids = hf_output_ids[1:]
  24. return hf_output_ids, hf_output_str, out_logprobs
  25. @pytest.mark.parametrize("model", ["Salesforce/blip2-opt-2.7b"])
  26. @pytest.mark.parametrize(
  27. "size_factors",
  28. [
  29. # No image
  30. [],
  31. # Single-scale
  32. [1.0],
  33. # Single-scale, batched
  34. [1.0, 1.0, 1.0],
  35. # Multi-scale
  36. [0.25, 0.5, 1.0],
  37. ],
  38. )
  39. @pytest.mark.parametrize("dtype", ["half"])
  40. @pytest.mark.parametrize("max_tokens", [128])
  41. @pytest.mark.parametrize("num_logprobs", [5])
  42. def test_models(hf_runner, aphrodite_runner, image_assets, model, size_factors,
  43. dtype: str, max_tokens: int, num_logprobs: int) -> None:
  44. """Inference result should be the same between hf and aphrodite.
  45. All the image fixtures for the test are from IMAGE_ASSETS.
  46. For huggingface runner, we provide the PIL images as input.
  47. For aphrodite runner, we provide MultiModalData objects and corresponding
  48. MultiModalConfig as input.
  49. Note, the text input is also adjusted to abide by aphrodite contract.
  50. The text output is sanitized to be able to compare with hf.
  51. """
  52. images = [asset.pil_image for asset in image_assets]
  53. inputs_per_image = [(
  54. [prompt for _ in size_factors],
  55. [rescale_image_size(image, factor) for factor in size_factors],
  56. ) for image, prompt in zip(images, HF_IMAGE_PROMPTS)]
  57. # max_model_len should be greater than image_feature_size
  58. with aphrodite_runner(
  59. model,
  60. dtype=dtype,
  61. enforce_eager=True,
  62. ) as aphrodite_model:
  63. aphrodite_outputs_per_image = [
  64. aphrodite_model.generate_greedy_logprobs(prompts,
  65. max_tokens,
  66. num_logprobs=num_logprobs,
  67. images=images)
  68. for prompts, images in inputs_per_image
  69. ]
  70. with hf_runner(model, dtype=dtype,
  71. auto_cls=AutoModelForVision2Seq) as hf_model:
  72. hf_outputs_per_image = [
  73. hf_model.generate_greedy_logprobs_limit(prompts,
  74. max_tokens,
  75. num_logprobs=num_logprobs,
  76. images=images)
  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, model)
  85. for aphrodite_output in aphrodite_outputs
  86. ],
  87. name_0="hf",
  88. name_1="aphrodite",
  89. )