-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Llava Onevision: add model #32673
Merged
Merged
Llava Onevision: add model #32673
Changes from 10 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
04ea11d
working version
zucchini-nlp 380e99a
fix copies
zucchini-nlp 2c82735
update
zucchini-nlp 1f854cc
tests
zucchini-nlp 4c41164
update docs
zucchini-nlp 7926afd
codestyle
zucchini-nlp ed4c3ad
Merge branch 'huggingface:main' into llava-onevision
zucchini-nlp e6ca32c
add more tests
zucchini-nlp b734893
add returns for docs
zucchini-nlp c660105
clean up
zucchini-nlp f85aecf
Update src/transformers/models/llava_onevision/processing_llava_onevi…
zucchini-nlp 4829831
updates
zucchini-nlp 1254c13
codestyle
zucchini-nlp fdbd460
Merge branch 'main' into llava-onevision
zucchini-nlp 3ecaa0d
style
zucchini-nlp 6ac443e
shouldn't be reversed
zucchini-nlp 6025390
[run-slow] llava_onevision
zucchini-nlp d7789f1
Merge remote-tracking branch 'upstream/main' into llava-onevision
zucchini-nlp 9c44c23
[run-slow] llava_onevision
zucchini-nlp 3dc34bf
Merge branch 'huggingface:main' into llava-onevision
zucchini-nlp 90ff94d
add pooling in videos
zucchini-nlp 1b99e48
Merge remote-tracking branch 'upstream/main' into llava-onevision
zucchini-nlp c5ccad1
[run-slow] llava_onevision
zucchini-nlp 73f100e
num-logits-to-keep
zucchini-nlp 44352f9
[run-slow] llava_onevision
zucchini-nlp ae18fc8
[run-slow] llava_onevision
zucchini-nlp 2a69a9a
Update tests/test_modeling_common.py
zucchini-nlp 5322d6f
video matched orig impl
zucchini-nlp b27c4f3
Merge remote-tracking branch 'upstream/main' into llava-onevision
zucchini-nlp ecd6743
fix tests
zucchini-nlp 278eb86
chat template was modified
zucchini-nlp 4ce02ed
Update docs/source/en/model_doc/llava_onevision.md
zucchini-nlp 7c5ae0d
add morer info in the doc page
zucchini-nlp 1fcb179
Merge branch 'main' into llava-onevision
zucchini-nlp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,312 @@ | ||
<!--Copyright 2024 The HuggingFace Team. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
|
||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
rendered properly in your Markdown viewer. | ||
|
||
--> | ||
|
||
# LLaVA-Onevision | ||
|
||
## Overview | ||
|
||
The LLaVA-Onevision model was proposed in [LLaVA-OneVision: Easy Visual Task Transfer](https://arxiv.org/abs/2408.03326) by <Bo Li, Yuanhan Zhang, Dong Guo, Renrui Zhang, Feng Li, Hao Zhang, Kaichen Zhang, Yanwei Li, Ziwei Liu, Chunyuan Li | ||
|
||
The abstract from the paper is the following: | ||
|
||
*We present LLaVA-OneVision, a family of open large multimodal models (LMMs) | ||
developed by consolidating our insights into data, models, and visual representations in the LLaVA-NeXT blog series. Our experimental results demonstrate that | ||
LLaVA-OneVision is the first single model that can simultaneously push the performance boundaries of open LMMs in three important computer vision scenarios: | ||
single-image, multi-image, and video scenarios. Importantly, the design of LLaVAOneVision allows strong transfer learning across different modalities/scenarios, | ||
yielding new emerging capabilities. In particular, strong video understanding and | ||
cross-scenario capabilities are demonstrated through task transfer from images to | ||
videos.* | ||
|
||
Tips: | ||
|
||
zucchini-nlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- We advise users to use `padding_side="left"` when computing batched generation as it leads to more accurate results. Simply make sure to call `processor.tokenizer.padding_side = "left"` before generating. | ||
|
||
<Tip warning={true}> | ||
|
||
- Llava-Next uses different number of patches for images and thus has to pad the inputs inside modeling code, aside from the padding done when processing the inputs. The default setting is "left-padding" if model is in `eval()` mode, otherwise "right-padding". | ||
zucchini-nlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</Tip> | ||
|
||
- Note that the model should use a specific prompt format, on which the large language model (LLM) was trained. You can use the processor's `apply_chat_template` to format your prompts correctly. For that you have to construct a conversation history, passing a plain string will not format your prompt. Each message in the conversation history for chat templates is a dictionary with keys "role" and "content". The "content" should be a list of dictionaries, for "text" and "image" modalities. | ||
|
||
We will use [llava-onevision-qwen2-7b-si-hf](https://huggingface.co/llava-hf/llava-onevision-qwen2-7b-si-hf) and a conversation history of text and image. Each content field has to be a list of dicts, as follows: | ||
|
||
```python | ||
from transformers import AutoProcessor | ||
|
||
processor = AutoProcessor.from_pretrained("llava-hf/llava-onevision-qwen2-7b-si-hf") | ||
|
||
conversation = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image"}, | ||
{"type": "text", "text": "What’s shown in this image?"}, | ||
], | ||
}, | ||
{ | ||
"role": "assistant", | ||
"content": [{"type": "text", "text": "This image shows a red stop sign."},] | ||
}, | ||
{ | ||
|
||
"role": "user", | ||
"content": [ | ||
{"type": "text", "text": "Describe the image in more details."}, | ||
], | ||
}, | ||
] | ||
|
||
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True) | ||
|
||
# Note that the template simply formats your prompt, you still have to tokenize it and obtain pixel values for your images | ||
print(text_prompt) | ||
>>> "<|im_start|>user\n<image>What is shown in this image?<|im_end|>\n<|im_start|>assistant\nPage showing the list of options.<|im_end|>" | ||
``` | ||
|
||
This model was contributed by [RaushanTurganbay](https://huggingface.co/RaushanTurganbay). | ||
The original code can be found [here](https://github.com/LLaVA-VL/LLaVA-NeXT/tree/main). | ||
|
||
|
||
## Usage example | ||
|
||
### Single image inference | ||
|
||
Here's how to load the model and perform inference in half-precision (`torch.float16`): | ||
|
||
```python | ||
from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration | ||
import torch | ||
from PIL import Image | ||
import requests | ||
|
||
processor = AutoProcessor.from_pretrained("llava-hf/llava-onevision-qwen2-7b-ov-hf") | ||
model = LlavaOnevisionForConditionalGeneration.from_pretrained("llava-hf/llava-onevision-qwen2-7b-ov-hf", torch_dtype=torch.float16, low_cpu_mem_usage=True) | ||
model.to("cuda:0") | ||
|
||
# prepare image and text prompt, using the appropriate prompt template | ||
url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true" | ||
image = Image.open(requests.get(url, stream=True).raw) | ||
|
||
conversation = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image"}, | ||
{"type": "text", "text": "What is shown in this image?"}, | ||
], | ||
}, | ||
] | ||
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True) | ||
inputs = processor(images=image, text=prompt, return_tensors="pt").to("cuda:0", torch.float16) | ||
|
||
# autoregressively complete prompt | ||
output = model.generate(**inputs, max_new_tokens=100) | ||
print(processor.decode(output[0], skip_special_tokens=True)) | ||
'user\n\nWhat is shown in this image?\nassistant\nThe image shows a radar chart, also known as a spider chart or a star chart, which is used to compare multiple quantitative variables. Each axis represents a different variable, and the chart is filled with' | ||
``` | ||
|
||
### Multi image inference | ||
|
||
LLaVa-Onevision can perform inference with multiple images as input, where images either belong to the same prompt or different prompts (in batched inference). For that you have to use checkpoints with an "ov" suffix. Here is how you can do it: | ||
|
||
```python | ||
import requests | ||
from PIL import Image | ||
import torch | ||
from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration | ||
|
||
# Load the model in half-precision | ||
model = LlavaOnevisionForConditionalGeneration.from_pretrained("llava-hf/llava-onevision-qwen2-7b-ov-hf", torch_dtype=torch.float16, device_map="auto") | ||
processor = AutoProcessor.from_pretrained("llava-hf/llava-onevision-qwen2-7b-ov-hf") | ||
|
||
# Get three different images | ||
url = "https://www.ilankelman.org/stopsigns/australia.jpg" | ||
image_stop = Image.open(requests.get(url, stream=True).raw) | ||
|
||
url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
image_cats = Image.open(requests.get(url, stream=True).raw) | ||
|
||
url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg" | ||
image_snowman = Image.open(requests.get(url, stream=True).raw) | ||
|
||
# Prepare a batch of two prompts, where the first one is a multi-turn conversation and the second is not | ||
conversation_1 = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image"}, | ||
{"type": "text", "text": "What is shown in this image?"}, | ||
], | ||
}, | ||
{ | ||
"role": "assistant", | ||
"content": [ | ||
{"type": "text", "text": "There is a red stop sign in the image."}, | ||
], | ||
}, | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image"}, | ||
{"type": "text", "text": "What about this image? How many cats do you see?"}, | ||
], | ||
}, | ||
] | ||
|
||
conversation_2 = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image"}, | ||
{"type": "text", "text": "What is shown in this image?"}, | ||
], | ||
}, | ||
] | ||
|
||
prompt_1 = processor.apply_chat_template(conversation_1, add_generation_prompt=True) | ||
prompt_2 = processor.apply_chat_template(conversation_2, add_generation_prompt=True) | ||
prompts = [prompt_1, prompt_2] | ||
|
||
# We can simply feed images in the order they have to be used in the text prompt | ||
inputs = processor(images=[image_stop, image_cats, image_snowman], text=prompts, padding=True, return_tensors="pt").to(model.device, torch.float16) | ||
|
||
# Generate | ||
generate_ids = model.generate(**inputs, max_new_tokens=30) | ||
processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) | ||
['user\n\nWhat is shown in this image?\nassistant\nThere is a red stop sign in the image.\nuser\n\nWhat about this image? How many cats do you see?\nassistant\ntwo', 'user\n\nWhat is shown in this image?\nassistant\n'] | ||
``` | ||
|
||
### Video inference | ||
|
||
LLaVa-Onevision can perform inference with multiple images as input, where images either belong to the same prompt or different prompts (in batched inference). Here is how you can do it: | ||
zucchini-nlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
import av | ||
import numpy as np | ||
from huggingface_hub import hf_hub_download | ||
|
||
import torch | ||
from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration | ||
|
||
# Load the model in half-precision | ||
model = LlavaOnevisionForConditionalGeneration.from_pretrained("llava-hf/llava-onevision-qwen2-7b-ov-hf", torch_dtype=torch.float16, device_map="auto") | ||
processor = AutoProcessor.from_pretrained("llava-hf/llava-onevision-qwen2-7b-ov-hf") | ||
|
||
|
||
def read_video_pyav(container, indices): | ||
''' | ||
Decode the video with PyAV decoder. | ||
Args: | ||
container (`av.container.input.InputContainer`): PyAV container. | ||
indices (`List[int]`): List of frame indices to decode. | ||
Returns: | ||
result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). | ||
''' | ||
frames = [] | ||
container.seek(0) | ||
start_index = indices[0] | ||
end_index = indices[-1] | ||
for i, frame in enumerate(container.decode(video=0)): | ||
if i > end_index: | ||
break | ||
if i >= start_index and i in indices: | ||
frames.append(frame) | ||
return np.stack([x.to_ndarray(format="rgb24") for x in frames]) | ||
|
||
# Load the video as an np.array, sampling uniformly 8 frames (can sample more for longer videos, up to 32 frames) | ||
video_path = hf_hub_download(repo_id="raushan-testing-hf/videos-test", filename="sample_demo_1.mp4", repo_type="dataset") | ||
container = av.open(video_path) | ||
total_frames = container.streams.video[0].frames | ||
indices = np.arange(0, total_frames, total_frames / 8).astype(int) | ||
video = read_video_pyav(container, indices) | ||
|
||
# For videos we have to feed a "video" type instead of "image" | ||
conversation = [ | ||
{ | ||
|
||
"role": "user", | ||
"content": [ | ||
{"type": "video"}, | ||
{"type": "text", "text": "Why is this video funny?"}, | ||
], | ||
}, | ||
] | ||
|
||
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True) | ||
inputs = processor(videos=list(video), text=prompt, return_tensors="pt").to("cuda:0", torch.float16) | ||
|
||
out = model.generate(**inputs, max_new_tokens=60) | ||
processor.batch_decode(out, skip_special_tokens=True, clean_up_tokenization_spaces=True) | ||
["user\n\nWhy is this video funny?\nassistant\nThe video appears to be humorous because it shows a young child, who is wearing glasses and holding a book, seemingly reading with a serious and focused expression. The child's glasses are a bit oversized for their face, which adds a comical touch, as it's a common trope to see children wearing"] | ||
``` | ||
|
||
## Model optimization | ||
|
||
### Quantization using Bitsandbytes | ||
|
||
The model can be loaded in 8 or 4 bits, greatly reducing the memory requirements while maintaining the performance of the original model. First make sure to install bitsandbytes, `pip install bitsandbytes` and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with: | ||
|
||
```python | ||
from transformers import LlavaOnevisionForConditionalGeneration, BitsAndBytesConfig | ||
|
||
# specify how to quantize the model | ||
quantization_config = BitsAndBytesConfig( | ||
load_in_4bit=True, | ||
bnb_4bit_quant_type="nf4", | ||
bnb_4bit_compute_dtype=torch.float16, | ||
) | ||
|
||
model = LlavaOnevisionForConditionalGeneration.from_pretrained(model_id, quantization_config=quantization_config, device_map="auto") | ||
``` | ||
|
||
### Use Flash-Attention 2 to further speed-up generation | ||
|
||
First make sure to install flash-attn. Refer to the [original repository of Flash Attention](https://github.com/Dao-AILab/flash-attention) regarding that package installation. Simply change the snippet above with: | ||
|
||
```python | ||
from transformers import LlavaOnevisionForConditionalGeneration | ||
|
||
model = LlavaOnevisionForConditionalGeneration.from_pretrained( | ||
model_id, | ||
torch_dtype=torch.float16, | ||
low_cpu_mem_usage=True, | ||
use_flash_attention_2=True | ||
).to(0) | ||
``` | ||
|
||
|
||
## LlavaOnevisionConfig | ||
|
||
[[autodoc]] LlavaOnevisionConfig | ||
|
||
## LlavaOnevisionProcessor | ||
|
||
[[autodoc]] LlavaOnevisionProcessor | ||
|
||
## LlavaOnevisionImageProcessor | ||
|
||
[[autodoc]] LlavaOnevisionImageProcessor | ||
|
||
## LlavaOnevisionVideoProcessor | ||
|
||
[[autodoc]] LlavaOnevisionVideoProcessor | ||
|
||
## LlavaOnevisionForConditionalGeneration | ||
|
||
[[autodoc]] LlavaOnevisionForConditionalGeneration | ||
- forward |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a short description of the model here? This is what will be used in the release notes. A good example is the music gen page
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done