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] Initialize Fuyu-8B support #3924

Merged
merged 49 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
89f97ba
support persimmon model
Isotr0py Apr 8, 2024
f51e0a4
optimize persimmon code
Isotr0py Apr 9, 2024
09d126e
make ruff happy
Isotr0py Apr 9, 2024
07592ae
Add Fuyu support
Isotr0py Apr 10, 2024
3a24825
Merge branch 'vllm-project:main' into fuyu
Isotr0py Apr 10, 2024
28d4707
Merge branch 'main' into fuyu
Isotr0py Apr 17, 2024
9e4f994
Fix model loader
Isotr0py Apr 17, 2024
061e812
Fix several typos
Isotr0py Apr 17, 2024
d72810d
fix loader and add example
Isotr0py Apr 17, 2024
222124f
Add fuyu example
Isotr0py Apr 17, 2024
9f190cc
Reformat code
Isotr0py Apr 17, 2024
ca8ddc4
Fix a typo in fuyu example
Isotr0py Apr 17, 2024
bf4def6
rmove fuyu from vision config loading
Isotr0py Apr 17, 2024
ef666cd
Make image_input_shape and image_feature_size
Isotr0py Apr 17, 2024
5815536
format code
Isotr0py Apr 17, 2024
9fce2f0
Fix isort
Isotr0py Apr 17, 2024
65cfc08
expand vision_language_config assertion
Isotr0py Apr 18, 2024
260ab10
Merge branch 'main' into fuyu
Isotr0py Apr 18, 2024
5da3a32
Merge remote-tracking branch 'upstream/main' into fuyu
Isotr0py Jun 25, 2024
dd28d7d
Merge branch 'main' into fuyu
Isotr0py Jun 25, 2024
3e55b0a
revert arg_utils mistake change
Isotr0py Jun 25, 2024
e5a6418
rebase fuyu
Isotr0py Jun 25, 2024
5454031
fix fuyu dynamic shape
Isotr0py Jun 25, 2024
7b64800
add fuyu test
Isotr0py Jun 26, 2024
1f9c405
Merge branch 'vllm-project:main' into fuyu
Isotr0py Jun 26, 2024
de57a56
fix fuyu processor
Isotr0py Jun 26, 2024
cefa770
debug persimmon
Isotr0py Jun 26, 2024
867cb36
fix fuyu test
Isotr0py Jun 27, 2024
b8e8004
Merge branch 'vllm-project:main' into fuyu
Isotr0py Jun 28, 2024
c6e779c
update model flag
Isotr0py Jun 28, 2024
4134084
fix wrong used RowParallelLinear
Isotr0py Jun 28, 2024
dc31551
Merge branch 'vllm-project:main' into fuyu
Isotr0py Jul 5, 2024
018336f
refactor fuyu
Isotr0py Jul 6, 2024
a006ac1
Merge branch 'main' into fuyu
Isotr0py Jul 7, 2024
c788ceb
fix fuyu input processor
Isotr0py Jul 7, 2024
71a38c3
refactor fuyu test
Isotr0py Jul 7, 2024
8a32199
fix fuyu test
Isotr0py Jul 7, 2024
b346bf2
reduce image size_factors
Isotr0py Jul 7, 2024
3b6e411
reduce scale factors
Isotr0py Jul 7, 2024
b7070c4
reduce size_factors
Isotr0py Jul 7, 2024
c55ff87
Merge branch 'vllm-project:main' into fuyu
Isotr0py Jul 8, 2024
71166ee
add monkey-patch for fuyu test
Isotr0py Jul 8, 2024
3a21aab
use shared merge_vision_embeddings and add dim check
Isotr0py Jul 8, 2024
6cff87a
reduce max_model_len
Isotr0py Jul 8, 2024
f806004
reduce size_factors
Isotr0py Jul 8, 2024
50e9c2d
add vllm_to_hf_output
Isotr0py Jul 8, 2024
bf89017
fix vllm_to_hf_output
Isotr0py Jul 8, 2024
004b0c0
Merge remote-tracking branch 'upstream/main' into fuyu
ywang96 Jul 13, 2024
b69ce43
Merge remote-tracking branch 'upstream/main' into fuyu
ywang96 Jul 14, 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
8 changes: 8 additions & 0 deletions docs/source/models/supported_models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Alongside each architecture, we include some popular models that use it.
- Falcon
- :code:`tiiuae/falcon-7b`, :code:`tiiuae/falcon-40b`, :code:`tiiuae/falcon-rw-7b`, etc.
-
* - :code:`FuyuForCausalLM`
DarkLight1337 marked this conversation as resolved.
Show resolved Hide resolved
- Fuyu
- :code:`adept/fuyu-8b` etc.
-
* - :code:`GemmaForCausalLM`
- Gemma
- :code:`google/gemma-2b`, :code:`google/gemma-7b`, etc.
Expand Down Expand Up @@ -139,6 +143,10 @@ Alongside each architecture, we include some popular models that use it.
- Phi-3-Small
- :code:`microsoft/Phi-3-small-8k-instruct`, :code:`microsoft/Phi-3-small-128k-instruct`, etc.
-
* - :code:`PersimmonForCausalLM`
- Persimmon
- :code:`adept/persimmon-8b-base`, :code:`adept/persimmon-8b-chat`, etc.
-
* - :code:`Phi3VForCausalLM`
- Phi-3-Vision
- :code:`microsoft/Phi-3-vision-128k-instruct`, etc.
Expand Down
63 changes: 63 additions & 0 deletions examples/fuyu_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import math
import os
import subprocess

from PIL import Image

from vllm import LLM, SamplingParams
from vllm.multimodal.image import ImagePixelData


def run_fuyu_pixel_values():
llm = LLM(
model="adept/fuyu-8b",
max_model_len=4096,
image_input_type="pixel_values",
image_token_id=71011,
image_input_shape="1,3,1080,1920",
image_feature_size=2304,
)

# load and create image prompt
image = Image.open("images/stop_sign.jpg")
W, H = image.size

nrow = math.ceil(min(H, 1080) / 30)
ncol = math.ceil(min(W, 1920) / 30)

# single-image prompt
prompt = "<image>\nWhat is the content of this image?\n"
prompt = prompt.replace("<image>",
("|SPEAKER|" * ncol + "|NEWLINE|") * nrow)

sampling_params = SamplingParams(temperature=0, max_tokens=64)

outputs = llm.generate(
{
"prompt": prompt,
"multi_modal_data": ImagePixelData(image),
},
sampling_params=sampling_params)
for o in outputs:
generated_text = o.outputs[0].text
print(generated_text)


if __name__ == "__main__":
# Download from s3
s3_bucket_path = "s3://air-example-data-2/vllm_opensource_llava/"
local_directory = "images"

# Make sure the local directory exists or create it
os.makedirs(local_directory, exist_ok=True)

# Use AWS CLI to sync the directory, assume anonymous access
subprocess.check_call([
"aws",
"s3",
"sync",
s3_bucket_path,
local_directory,
"--no-sign-request",
])
run_fuyu_pixel_values()
115 changes: 115 additions & 0 deletions tests/models/test_fuyu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from typing import List, Tuple

import pytest

from vllm.config import VisionLanguageConfig
from vllm.utils import is_cpu

from ..conftest import IMAGE_ASSETS

pytestmark = pytest.mark.vlm

HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({
"stop_sign":
"What color is the stop sign?\n", # noqa: E501
"cherry_blossom":
"What is the season?\n", # noqa: E501
})


def iter_fuyu_configs(model_name: str):
image_hw_to_feature_size = {
(420, 660): 308,
}

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)),
]:
yield (model_name,
VisionLanguageConfig(image_input_type=input_type,
image_feature_size=f,
image_token_id=71011,
image_input_shape=input_shape,
image_processor=model_name,
image_processor_revision=None))


model_and_vl_config = [
*iter_fuyu_configs("adept/fuyu-8b"),
]


def vllm_to_hf_output(vllm_output: Tuple[List[int], str]):
"""Sanitize vllm output to be comparable with hf output.
The function reduces `input_ids` from 1, 32000, 32000, ..., 32000,
x1, x2, x3 ... to 1, 32000, x1, x2, x3 ...
It also reduces `output_str` from "<image><image>bla" to "bla".
"""
input_ids, output_str = vllm_output

hf_input_ids = input_ids[2:]
hf_output_str = output_str

return hf_input_ids, hf_output_str


target_dtype = "half"
if is_cpu():
target_dtype = "bfloat16"


# TODO: Add test for `tensor_parallel_size` [ref: PR #3883]
@pytest.mark.parametrize("model_and_config", model_and_vl_config)
@pytest.mark.parametrize("dtype", [target_dtype])
@pytest.mark.parametrize("max_tokens", [128])
def test_models(hf_runner, vllm_runner, image_assets, 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
_, _, H, W = vlm_config.image_input_shape

# resize images to the model's input shape
hf_images = [asset.for_hf().resize((W, H)) for asset in image_assets]
vllm_images = [asset.for_vllm(vlm_config) for asset in image_assets]
for i in range(len(image_assets)):
vllm_images[i].image = vllm_images[i].image.resize((W, H))

with hf_runner(model_id, dtype=dtype) as hf_model:
hf_outputs = hf_model.generate_greedy(
HF_IMAGE_PROMPTS,
max_tokens,
images=hf_images,
eos_token_id=hf_model.processor.tokenizer.eos_token_id)

ncol, nrow = W // 30, H // 30
image_prompts = ("|SPEAKER|" * ncol + "|NEWLINE|") * nrow
vllm_image_prompts = [
image_prompts + "<s> " + p + "\x04" for p in HF_IMAGE_PROMPTS
]

with vllm_runner(model_id,
max_model_len=1024,
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_outputs[i]
vllm_output_ids, vllm_output_str = vllm_to_hf_output(vllm_outputs[i])
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}")
2 changes: 2 additions & 0 deletions vllm/model_executor/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"DeciLMForCausalLM": ("decilm", "DeciLMForCausalLM"),
"DeepseekForCausalLM": ("deepseek", "DeepseekForCausalLM"),
"FalconForCausalLM": ("falcon", "FalconForCausalLM"),
"FuyuForCausalLM": ("fuyu", "FuyuForCausalLM"),
"GemmaForCausalLM": ("gemma", "GemmaForCausalLM"),
"Gemma2ForCausalLM": ("gemma2", "Gemma2ForCausalLM"),
"GPT2LMHeadModel": ("gpt2", "GPT2LMHeadModel"),
Expand All @@ -48,6 +49,7 @@
"OlmoForCausalLM": ("olmo", "OlmoForCausalLM"),
"OPTForCausalLM": ("opt", "OPTForCausalLM"),
"OrionForCausalLM": ("orion", "OrionForCausalLM"),
"PersimmonForCausalLM": ("persimmon", "PersimmonForCausalLM"),
"PhiForCausalLM": ("phi", "PhiForCausalLM"),
"Phi3ForCausalLM": ("llama", "LlamaForCausalLM"),
"Phi3VForCausalLM": ("phi3v", "Phi3VForCausalLM"),
Expand Down
Loading
Loading