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

[VLM][Model] Support image input for Chameleon #6633

Merged
merged 14 commits into from
Jul 23, 2024

Conversation

ywang96
Copy link
Member

@ywang96 ywang96 commented Jul 22, 2024

This PR is a follow-up to #5770 that adds image + text -> text support for Chameleon. Current implementation only allows one image per request.

FIXES #5721


PR Checklist (Click to Expand)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!

Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which consists a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of default ones by unblocking the steps in your fast-check build on Buildkite UI.

Once the PR is approved and ready to go, please make sure to run full CI as it is required to merge (or just use auto-merge).

To run full CI, you can do one of these:

  • Comment /ready on the PR
  • Add ready label to the PR
  • Enable auto-merge.

🚀

@ywang96
Copy link
Member Author

ywang96 commented Jul 22, 2024

This PR is ready to be reviewed and tested. Below is an example modified from the Chameleon PR on transformers.

from vllm import LLM, SamplingParams
import torch
from PIL import Image
import requests

model_path = "facebook/chameleon-30b"
llm = LLM(model=model_path, dtype=torch.bfloat16, max_num_seqs=10)

image = Image.open(requests.get("https://uploads4.wikiart.org/images/paul-klee/death-for-the-idea-1915.jpg!Large.jpg", stream=True).raw)
greedy_params = SamplingParams(temperature=0.0, max_tokens=40)
prompt = "I'm very intrigued by this work of art:<image>Please tell me about the artist."
output = llm.generate(
    {"prompt": prompt,
     "multi_modal_data": {"image": image}
    },
    greedy_params)

print(output[0].outputs[0].text)

Output

The image you provided is a"Drawing for "The Persistence of Memory"" by Salvador Dalí.

Salvador Dalí was a Spanish surrealist artist known for his flam

On main branch oftransformers

import torch
from PIL import Image
import requests

from transformers import (
    ChameleonConfig,
    ChameleonForConditionalGeneration,
    ChameleonImageProcessor,
    ChameleonProcessor,
)

model_path = "facebook/chameleon-30b"
model = ChameleonForConditionalGeneration.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map="auto")
processor = ChameleonProcessor.from_pretrained(model_path)

prompt = "I'm very intrigued by this work of art:<image>Please tell me about the artist."
image = Image.open(requests.get("https://uploads4.wikiart.org/images/paul-klee/death-for-the-idea-1915.jpg!Large.jpg", stream=True).raw)

inputs = processor(prompt, images=[image], return_tensors="pt").to(model.device, dtype=torch.bfloat16)
length = inputs.input_ids.shape[1]
out = model.generate(**inputs, max_new_tokens=40, do_sample=False)
generated_text = processor.batch_decode(out[:, length:], skip_special_tokens=False)[0]
print(generated_text)

Output

The image you provided is a"Drawing for "The Persistence of Memory"" by Salvador Dalí, created in 1931. This drawing is a preliminary sketch for one of

@ywang96 ywang96 marked this pull request as ready for review July 22, 2024 08:58
@DarkLight1337
Copy link
Member

It would be great if you could add a test case for inputting images into Chameleon!

@xwjiang2010
Copy link
Contributor

Running into an OOM error under default setting. A100 - 80G, 7B Chameleon model.

Code

from PIL import Image


from vllm import LLM

def run():
    llm = LLM(model="/mnt/local_storage/data/cache/huggingface/hub/models--facebook--chameleon-7b/snapshots/bbd94872b11573e40a061783cd1766408614b6e4/") # , max_num_seqs=10)
    # llm = LLM(model="/home/ray/.cache/huggingface/hub/models--facebook--chameleon-30b/snapshots/1c95500025a2a51183c7bb8f410ecd3d84e4cb2c/")

    prompt = "Write me a poetry for kids based on this image."


    image = Image.open("/home/ray/default/0.JPG")
    outputs = llm.generate({
        "prompt": prompt,
        "multi_modal_data": {
            "image": image
        },
    })

    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)

run()

Stacktrace

rank0]: Traceback (most recent call last):
[rank0]:   File "/home/ray/default/vllm/test_chameleon.py", line 28, in <module>
[rank0]:     run_llava()
[rank0]:   File "/home/ray/default/vllm/test_chameleon.py", line 10, in run_llava
[rank0]:     llm = LLM(model="/mnt/local_storage/data/cache/huggingface/hub/models--facebook--chameleon-7b/snapshots/bbd94872b11573e40a061783cd1766408614b6e4/")
[rank0]:   File "/home/ray/default/vllm/vllm/entrypoints/llm.py", line 156, in __init__
[rank0]:     self.llm_engine = LLMEngine.from_engine_args(
[rank0]:   File "/home/ray/default/vllm/vllm/engine/llm_engine.py", line 440, in from_engine_args
[rank0]:     engine = cls(
[rank0]:   File "/home/ray/default/vllm/vllm/engine/llm_engine.py", line 264, in __init__
[rank0]:     self._initialize_kv_caches()
[rank0]:   File "/home/ray/default/vllm/vllm/engine/llm_engine.py", line 363, in _initialize_kv_caches
[rank0]:     self.model_executor.determine_num_available_blocks())
[rank0]:   File "/home/ray/default/vllm/vllm/executor/gpu_executor.py", line 94, in determine_num_available_blocks
[rank0]:     return self.driver_worker.determine_num_available_blocks()
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/worker/worker.py", line 179, in determine_num_available_blocks
[rank0]:     self.model_runner.profile_run()
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/worker/model_runner.py", line 767, in profile_run
[rank0]:     self.execute_model(model_input, kv_caches, intermediate_tensors)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/worker/model_runner.py", line 1185, in execute_model
[rank0]:     hidden_or_intermediate_states = model_executable(
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 946, in forward
[rank0]:     image_tokens = self.model.get_image_tokens(image_input["data"].to(
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 843, in get_image_tokens
[rank0]:     _, _, image_toks = self.vqmodel.encode(pixel_values)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 741, in encode
[rank0]:     hidden_states = self.encoder(pixel_values)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 709, in forward
[rank0]:     hidden_states.append(self.down[i_level].downsample(
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 487, in forward
[rank0]:     hidden_states = self.conv(hidden_states)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py", line 460, in forward
[rank0]:     return self._conv_forward(input, self.weight, self.bias)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py", line 456, in _conv_forward
[rank0]:     return F.conv2d(input, weight, bias, self.stride,
[rank0]: torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 4.00 GiB. GPU 

I think there is something wrong with the profiling logic. Especially considering the hardware is pretty generous for the size of the model run.

@ywang96
Copy link
Member Author

ywang96 commented Jul 22, 2024

Running into an OOM error under default setting. A100 - 80G, 7B Chameleon model.

Code

from PIL import Image


from vllm import LLM

def run():
    llm = LLM(model="/mnt/local_storage/data/cache/huggingface/hub/models--facebook--chameleon-7b/snapshots/bbd94872b11573e40a061783cd1766408614b6e4/") # , max_num_seqs=10)
    # llm = LLM(model="/home/ray/.cache/huggingface/hub/models--facebook--chameleon-30b/snapshots/1c95500025a2a51183c7bb8f410ecd3d84e4cb2c/")

    prompt = "Write me a poetry for kids based on this image."


    image = Image.open("/home/ray/default/0.JPG")
    outputs = llm.generate({
        "prompt": prompt,
        "multi_modal_data": {
            "image": image
        },
    })

    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)

run()

Stacktrace

rank0]: Traceback (most recent call last):
[rank0]:   File "/home/ray/default/vllm/test_chameleon.py", line 28, in <module>
[rank0]:     run_llava()
[rank0]:   File "/home/ray/default/vllm/test_chameleon.py", line 10, in run_llava
[rank0]:     llm = LLM(model="/mnt/local_storage/data/cache/huggingface/hub/models--facebook--chameleon-7b/snapshots/bbd94872b11573e40a061783cd1766408614b6e4/")
[rank0]:   File "/home/ray/default/vllm/vllm/entrypoints/llm.py", line 156, in __init__
[rank0]:     self.llm_engine = LLMEngine.from_engine_args(
[rank0]:   File "/home/ray/default/vllm/vllm/engine/llm_engine.py", line 440, in from_engine_args
[rank0]:     engine = cls(
[rank0]:   File "/home/ray/default/vllm/vllm/engine/llm_engine.py", line 264, in __init__
[rank0]:     self._initialize_kv_caches()
[rank0]:   File "/home/ray/default/vllm/vllm/engine/llm_engine.py", line 363, in _initialize_kv_caches
[rank0]:     self.model_executor.determine_num_available_blocks())
[rank0]:   File "/home/ray/default/vllm/vllm/executor/gpu_executor.py", line 94, in determine_num_available_blocks
[rank0]:     return self.driver_worker.determine_num_available_blocks()
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/worker/worker.py", line 179, in determine_num_available_blocks
[rank0]:     self.model_runner.profile_run()
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/worker/model_runner.py", line 767, in profile_run
[rank0]:     self.execute_model(model_input, kv_caches, intermediate_tensors)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/worker/model_runner.py", line 1185, in execute_model
[rank0]:     hidden_or_intermediate_states = model_executable(
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 946, in forward
[rank0]:     image_tokens = self.model.get_image_tokens(image_input["data"].to(
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 843, in get_image_tokens
[rank0]:     _, _, image_toks = self.vqmodel.encode(pixel_values)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 741, in encode
[rank0]:     hidden_states = self.encoder(pixel_values)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 709, in forward
[rank0]:     hidden_states.append(self.down[i_level].downsample(
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/default/vllm/vllm/model_executor/models/chameleon.py", line 487, in forward
[rank0]:     hidden_states = self.conv(hidden_states)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py", line 460, in forward
[rank0]:     return self._conv_forward(input, self.weight, self.bias)
[rank0]:   File "/home/ray/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py", line 456, in _conv_forward
[rank0]:     return F.conv2d(input, weight, bias, self.stride,
[rank0]: torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 4.00 GiB. GPU 

I think there is something wrong with the profiling logic. Especially considering the hardware is pretty generous for the size of the model run.

Thank you for catching this - this has been fixed!

@xwjiang2010
Copy link
Contributor

I am done with manual testing.
Understand that we cannot add correctness test now. But can we have one test that exercises the code? Preferably with multiple images in a batch.

@ywang96
Copy link
Member Author

ywang96 commented Jul 23, 2024

I have addressed all comments and added a batched test! cc @xwjiang2010 @DarkLight1337

Copy link
Member

@DarkLight1337 DarkLight1337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing this!

@ywang96 ywang96 added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 23, 2024
@simon-mo simon-mo merged commit 22fa2e3 into vllm-project:main Jul 23, 2024
18 of 22 checks passed
xjpang pushed a commit to xjpang/vllm that referenced this pull request Jul 24, 2024
cduk pushed a commit to cduk/vllm-pascal that referenced this pull request Aug 6, 2024
kylesayrs pushed a commit to neuralmagic/vllm that referenced this pull request Aug 17, 2024
@DarkLight1337 DarkLight1337 changed the title [VLM][Model] Support image input for Chameleon [VLM][Model] Support image input for Chameleon Aug 22, 2024
Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[New Model]: Chameleon support
4 participants