test_blip2.py 3.8 KB

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