Views
No views yet
long-llava-qwen2-7b is a open source large-Context Multimodal LLM and can perform language, image, and video understanding. In stead of proposing a new model archiecture, we extended llava to support make it support long context in a multimodal setting (i.e., multiple images, short and long videos). long-llava-qwen2-7b was fine-tuned from Qwen2-7B-Instruct.
long-llava-qwen2-7b scored averagely ~88.0% on this NIAH benchmark across different numbers of frame depths and frames shown in this plot.long-llava-qwen2-7b achieves SOTAs on both perception and cognition evaluation.| Models | mme_cognition_score | mme_percetion_score |
|---|---|---|
| long_llava_qwen2_7b | 350 | 1494.64386 |
| llava_1.5_7b_hf | 326.42857 | 1492.13225 |
long-llava-qwen2-7b scored a overall 57.1% with subtitles and 52.9% with as shown in this table (adapted from the VideoMME Leaderboard), which makes it the SOTA for 7B models.| Models | LLM Params | Overall (%) - w/o subs | Overall (%) - w subs |
|---|---|---|---|
| long_llava_qwen2_7b | 7B | 52.9 | 57.1 |
| LongVA | 7B | 52.6 | 54.3 |
| VideoLLaMA 2 | 7B | 47.9 | 50.3 |
| ShareGemini | 7B | 43.2 | 47.9 |
| Chat-UniVi-v1.5 | 7B | 40.6 | 45.9 |
| VideoChat2-Mistral | 7B | 39.5 | 43.8 |
| ST-LLM | 7B | 37.9 | 42.3 |
| Qwen-VL-Chat | 7B | 41.1 | 41.9 |
| Video-LLaVA | 7B | 39.9 | 41.6 |
long_llava_qwen2_7b's long context capability by understanding both images and videos. This can be useful for onboarding new developers.
git clone https://github.com/awslabs/extending-the-context-length-of-open-source-llms.git
cd extending-the-context-length-of-open-source-llms/long-llava-qwen2-7b
conda create -n long-llava python=3.12 -y
conda activate long-llava
pip install -r local_demo/requirements.txt
python local_demo/multimodal_chat.pyhttp://localhost:6006 or https://xxxxxxxxxxxx.gradio.live if share=Ture is enabled in local_demo/multimodal_chat.py.transformers >= 4.42.0.
The model supports multi-image and multi-prompt generation. Meaning that you can pass multiple images in your prompt.pipeline:"aws-prototyping/long-llava-qwen2-7b" checkpoint.1from transformers import pipeline
2from PIL import Image
3import requests
4
5model_id = "aws-prototyping/long-llava-qwen2-7b"
6pipe = pipeline("image-to-text", model=model_id)
7url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
8image = Image.open(requests.get(url, stream=True).raw)
9
10# Define a chat histiry and use `apply_chat_template` to get correctly formatted prompt
11# Each value in "content" has to be a list of dicts with types ("text", "image")
12conversation = [
13 {
14
15 "role": "user",
16 "content": [
17 {"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
18 {"type": "image"},
19 ],
20 },
21]
22prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
23
24outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
25print(outputs)
26>>> {"generated_text": "\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT: Lava"}transformers:float16 precision on a GPU device:1import requests
2from PIL import Image
3
4import torch
5from transformers import AutoProcessor, LlavaForConditionalGeneration
6
7model_id = "aws-prototyping/long-llava-qwen2-7b"
8model = LlavaForConditionalGeneration.from_pretrained(
9 model_id,
10 torch_dtype=torch.bfloat16,
11 low_cpu_mem_usage=True,
12).to(0)
13
14processor = AutoProcessor.from_pretrained(model_id)
15
16# Define a chat histiry and use `apply_chat_template` to get correctly formatted prompt
17# Each value in "content" has to be a list of dicts with types ("text", "image")
18conversation = [
19 {
20
21 "role": "user",
22 "content": [
23 {"type": "text", "text": "What are these?"},
24 {"type": "image"},
25 ],
26 },
27]
28prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
29
30image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
31raw_image = Image.open(requests.get(image_file, stream=True).raw)
32inputs = processor(images=raw_image, text=prompt, return_tensors='pt').to(0, torch.float16)
33
34output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
35print(processor.decode(output[0][2:], skip_special_tokens=True))bitsandbytes librarybitsandbytes, pip install bitsandbytes and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with:1model = LlavaForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.float16,
4 low_cpu_mem_usage=True,
5+ load_in_4bit=True
6)flash-attn. Refer to the original repository of Flash Attention regarding that package installation. Simply change the snippet above with:1model = LlavaForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.bfloat16,
4 low_cpu_mem_usage=True,
5+ use_flash_attention_2=True
6).to(0)g5.4xlarge or larger instance, install vLLM as per vLLM docs.pip install vllm==0.5.51vllm serve aws-prototyping/long-llava-qwen2-7b \
2 —max_model_len 81921import base64
2
3import requests
4from openai import OpenAI
5
6# Modify OpenAI's API key and API base to use vLLM's API server.
7openai_api_key = "EMPTY"
8openai_api_base = "http://localhost:8000/v1"
9
10client = OpenAI(
11 # defaults to os.environ.get("OPENAI_API_KEY")
12 api_key=openai_api_key,
13 base_url=openai_api_base,
14)
15
16models = client.models.list()
17model = models.data[0].id
18
19image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
20
21# Use image url in the payload
22chat_completion_from_url = client.chat.completions.create(
23 messages=[{
24 "role":
25 "user",
26 "content": [
27 {
28 "type": "text",
29 "text": "What’s in this image?"
30 },
31 {
32 "type": "image_url",
33 "image_url": {
34 "url": image_url
35 },
36 },
37 ],
38 }],
39 model=model,
40 max_tokens=64,
41)
42
43result = chat_completion_from_url.choices[0].message.content
44print(f"Chat completion output:{result}")
45
46
47# Use base64 encoded image in the payload
48def encode_image_base64_from_url(image_url: str) -> str:
49 """Encode an image retrieved from a remote url to base64 format."""
50
51 with requests.get(image_url) as response:
52 response.raise_for_status()
53 result = base64.b64encode(response.content).decode('utf-8')
54
55 return result
56
57
58image_base64 = encode_image_base64_from_url(image_url=image_url)
59chat_completion_from_base64 = client.chat.completions.create(
60 messages=[{
61 "role":
62 "user",
63 "content": [
64 {
65 "type": "text",
66 "text": "What’s in this image?"
67 },
68 {
69 "type": "image_url",
70 "image_url": {
71 "url": f"data:image/jpeg;base64,{image_base64}"
72 },
73 },
74 ],
75 }],
76 model=model,
77 max_tokens=64,
78)
79
80result = chat_completion_from_base64.choices[0].message.content
81print(f"Chat completion output:{result}")long-llava-qwen2-7b model, it is important to perform your own independent assessment, and take measures to ensure that your use would comply with your own specific quality control practices and standards, and that your use would comply with the local rules, laws, regulations, licenses and terms that apply to you, and your content.@misc{long-llava-qwen2-7b-2024,
author = { {Yin Song and Chen Wu and Eden Duthie} },
title = { {aws-prototyping/long-llava-qwen2-7b} },
year = 2024,
url = { https://huggingface.co/aws-prototyping/long-llava-qwen2-7b },
publisher = { Hugging Face }
}