Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Model] Add PaliGemma #5189

Merged
merged 41 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e6352e5
initial
ywang96 Jun 2, 2024
7dfbe44
remove lm head
ywang96 Jun 2, 2024
3fd77fe
Merge branch 'main' into paligemma
ywang96 Jun 7, 2024
ccb0f25
Merge branch 'main' into paligemma
ywang96 Jun 8, 2024
9b5269d
update tests
ywang96 Jun 9, 2024
af11afa
fix test
ywang96 Jun 9, 2024
a465e85
format
ywang96 Jun 9, 2024
3e9a12b
fix model loading
ywang96 Jun 9, 2024
c734a17
fix input args
ywang96 Jun 9, 2024
2d7de4d
fix model loading
ywang96 Jun 9, 2024
2f65bf7
add embedding method to gemma
ywang96 Jun 9, 2024
04e4ace
fix linear output
ywang96 Jun 9, 2024
4a9551d
update gemma forward
ywang96 Jun 9, 2024
6fd10f1
update
ywang96 Jun 9, 2024
d08db94
fix test
ywang96 Jun 9, 2024
e325630
remove extra bos
ywang96 Jun 10, 2024
cbb7c49
format
ywang96 Jun 10, 2024
7ea7265
add gemma to model test
ywang96 Jun 10, 2024
9a8cd85
try normal caption
ywang96 Jun 11, 2024
9069831
Merge branch 'main' into paligemma
ywang96 Jun 12, 2024
7cb1cbb
Merge branch 'main' into paligemma
ywang96 Jun 25, 2024
7db6122
[Model] Add Gemma 2
WoosukKwon Jun 27, 2024
df2c007
Remove supports_lora=True
WoosukKwon Jun 27, 2024
9ba7aac
[Bugfix] Fix precision issues in Gemma 1
WoosukKwon Jun 27, 2024
6b32a1e
Minor
WoosukKwon Jun 27, 2024
6bfba0a
Merge branch 'main' into woosuk-gemma1
WoosukKwon Jun 27, 2024
bdf9334
Merge branch 'woosuk-gemma1' of https://github.com/vllm-project/vllm …
WoosukKwon Jun 27, 2024
524db49
Merge branch 'main' into woosuk-gemma1
WoosukKwon Jun 28, 2024
7e6f0fd
Merge branch 'main' into paligemma
ywang96 Jun 28, 2024
50ae420
Merge remote-tracking branch 'upstream/woosuk-gemma1' into paligemma
ywang96 Jun 28, 2024
e0828b0
Merge branch 'main' into paligemma
ywang96 Jul 5, 2024
c4fa37f
update paligemma
ywang96 Jul 6, 2024
b09066e
update test
ywang96 Jul 6, 2024
bf4bb58
update
ywang96 Jul 6, 2024
c1b9ebf
update
ywang96 Jul 6, 2024
5c0d2ec
add model to doc
ywang96 Jul 6, 2024
0b76ac1
address comments
ywang96 Jul 6, 2024
4823852
fix eos
ywang96 Jul 6, 2024
02b7c21
Merge branch 'main' into paligemma
ywang96 Jul 6, 2024
1651b15
move doc
ywang96 Jul 6, 2024
2f94007
Update docs/source/models/supported_models.rst
ywang96 Jul 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tests
  • Loading branch information
ywang96 committed Jun 9, 2024
commit 9b5269d67456516f197e4cd3fb1c9a8ece9550d8
6 changes: 3 additions & 3 deletions .buildkite/test-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ steps:
- label: Models Test
#mirror_hardwares: [amd]
commands:
- pytest -v -s models -m \"not llava\"
- pytest -v -s models -m \"not vlm\"

- label: Llava Test
- label: Vision Model Test
mirror_hardwares: [amd]
commands:
- bash ../.buildkite/download-images.sh
- pytest -v -s models -m llava
- pytest -v -s models -m vlm

- label: Prefix Caching Test
mirror_hardwares: [amd]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ markers = [
"skip_global_cleanup",
"llm: run tests for vLLM API only",
"openai: run tests for OpenAI API only",
"llava: run tests for LLaVA models only",
"vlm: run tests for vision language models only",
]
2 changes: 1 addition & 1 deletion tests/models/test_llava.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ..conftest import IMAGE_FILES

pytestmark = pytest.mark.llava
pytestmark = pytest.mark.vlm

# The image token is placed before "user" on purpose so that the test can pass
HF_IMAGE_PROMPTS = [
Expand Down
111 changes: 111 additions & 0 deletions tests/models/text_paligemma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from typing import List, Tuple

import pytest
from transformers import AutoTokenizer

from vllm.config import VisionLanguageConfig

from ..conftest import IMAGE_FILES

pytestmark = pytest.mark.vlm

# The image token is placed before "user" on purpose so that the test can pass
HF_IMAGE_PROMPTS = [
"<image>\nUSER: What's the content of the image?\nASSISTANT:",
"<image>\nUSER: What is the season?\nASSISTANT:",
]

assert len(HF_IMAGE_PROMPTS) == len(IMAGE_FILES)


def iter_paligemma_configs(model_name: str):
image_hw_to_feature_size = {
(224, 224): 256,
}

for (h, w), f in image_hw_to_feature_size.items():
for input_type, input_shape in [
(VisionLanguageConfig.ImageInputType.PIXEL_VALUES, (1, 3, h, w)),
(VisionLanguageConfig.ImageInputType.IMAGE_FEATURES, (1, f, 1152)),
]:
yield (model_name,
VisionLanguageConfig(image_input_type=input_type,
image_feature_size=f,
image_token_id=257152,
image_input_shape=input_shape,
image_processor=model_name,
image_processor_revision=None))


model_and_vl_config = [
*iter_paligemma_configs("google/paligemma-3b-pt-224"),
]


def vllm_to_hf_output(vllm_output: Tuple[List[int], str],
vlm_config: VisionLanguageConfig, model_id: str):
"""Sanitize vllm output to be comparable with hf output.
"""
input_ids, output_str = vllm_output
image_token_id = vlm_config.image_token_id

tokenizer = AutoTokenizer.from_pretrained(model_id)
image_token_str = tokenizer.decode(image_token_id)

# remove image token, bos token and the last newline token
hf_input_ids = [
input_id for input_id in input_ids
if input_id != image_token_id and input_id != 2
]
if hf_input_ids[-1] == 108:
hf_input_ids = hf_input_ids[:-1]

# remove image token from the output string
hf_output_str = output_str \
.replace(image_token_str * vlm_config.image_feature_size, "")

return hf_input_ids, hf_output_str


@pytest.mark.parametrize("model_and_config", model_and_vl_config)
@pytest.mark.parametrize("dtype", ["half"])
@pytest.mark.parametrize("max_tokens", [128])
def test_models(hf_runner, vllm_runner, hf_images, vllm_images,
model_and_config, dtype: str, max_tokens: int) -> None:
"""Inference result should be the same between hf and vllm.

All the image fixtures for the test is under tests/images.
For huggingface runner, we provide the PIL images as input.
For vllm runner, we provide MultiModalData objects and corresponding
vision language config as input.
Note, the text input is also adjusted to abide by vllm contract.
The text output is sanitized to be able to compare with hf.
"""
model_id, vlm_config = model_and_config

with hf_runner(model_id, dtype=dtype, is_vision_model=True) as hf_model:
hf_outputs = hf_model.generate_greedy(HF_IMAGE_PROMPTS,
max_tokens,
images=hf_images)

vllm_image_prompts = [
'<image>' * vlm_config.image_feature_size + '<bos>' + p + '\n'
for p in HF_IMAGE_PROMPTS
]

with vllm_runner(model_id,
dtype=dtype,
enforce_eager=True,
**vlm_config.as_cli_args_dict()) as vllm_model:
vllm_outputs = vllm_model.generate_greedy(vllm_image_prompts,
max_tokens,
images=vllm_images)

for i in range(len(HF_IMAGE_PROMPTS)):
hf_output_ids, hf_output_str = hf_outputs[i]
vllm_output_ids, vllm_output_str = vllm_to_hf_output(
vllm_outputs[i], vlm_config, model_id)
assert hf_output_str == vllm_output_str, (
f"Test{i}:\nHF: {hf_output_str!r}\nvLLM: {vllm_output_str!r}")
assert hf_output_ids == vllm_output_ids, (
f"Test{i}:\nHF: {hf_output_ids}\nvLLM: {vllm_output_ids}")
12 changes: 11 additions & 1 deletion vllm/model_executor/models/paligemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import torch
from torch import nn
from transformers import SiglipVisionModel, PaliGemmaConfig
from transformers import PaliGemmaConfig, SiglipVisionModel

from vllm.attention import AttentionMetadata
from vllm.config import CacheConfig, VisionLanguageConfig
Expand All @@ -14,6 +14,8 @@
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
from vllm.model_executor.models.gemma import GemmaModel
from vllm.model_executor.sampling_metadata import SamplingMetadata
from vllm.multimodal import MULTIMODAL_REGISTRY
from vllm.multimodal.image import get_dummy_image_data
from vllm.sequence import SamplerOutput

from .vlm_base import VisionLanguageModelBase
Expand Down Expand Up @@ -53,6 +55,9 @@ class PaliGemmaImageFeatureInputs(TypedDict):
PaliGemmaImageFeatureInputs]


@MULTIMODAL_REGISTRY.register_image_feature_input()
@MULTIMODAL_REGISTRY.register_image_pixel_input()
@MULTIMODAL_REGISTRY.register_dummy_data(get_dummy_image_data)
class PaliGemmaForConditionalGeneration(VisionLanguageModelBase):

def __init__(self,
Expand Down Expand Up @@ -182,7 +187,12 @@ def forward(self,
kv_caches: List[torch.Tensor],
attn_metadata: AttentionMetadata,
image_input: Optional[torch.Tensor] = None) -> SamplerOutput:
"""
The correct prompt format needs to be:
'<image>' * image_feature_size + '<bos>' + prompt + '\n'

See https://github.com/huggingface/transformers/blob/25245ec26dc29bcf6102e1b4ddd0dfd02e720cf5/src/transformers/models/paligemma/processing_paligemma.py#L55
""" # noqa
parsed_image_input = self._parse_and_validate_image_input(image_input)

if parsed_image_input is not None:
Expand Down
2 changes: 2 additions & 0 deletions vllm/multimodal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def get_full_image_text_prompt(image_prompt: str, text_prompt: str,

if config.hf_config.model_type == "llava":
full_prompt = f"{image_prompt}\n{text_prompt}"
elif config.hf_config.model_type == "paligemma":
full_prompt = f"{image_prompt}<bos>{text_prompt}\n"
else:
raise ValueError(
f"Unsupported model type: {config.hf_config.model_type}")
Expand Down
Loading