Views
No views yet
| Unsloth supports | Free Notebooks | Performance | Memory use |
|---|---|---|---|
| Llama-3.2 (3B) | ▶️ Start on Colab | 2.4x faster | 58% less |
| Llama-3.2 (11B vision) | ▶️ Start on Colab | 2x faster | 40% less |
| Qwen2 VL (7B) | ▶️ Start on Colab | 1.8x faster | 40% less |
| Qwen2.5 (7B) | ▶️ Start on Colab | 2x faster | 60% less |
| Llama-3.1 (8B) | ▶️ Start on Colab | 2.4x faster | 58% less |
| Phi-3.5 (mini) | ▶️ Start on Colab | 2x faster | 50% less |
| Gemma 2 (9B) | ▶️ Start on Colab | 2.4x faster | 58% less |
| Mistral (7B) | ▶️ Start on Colab | 2.2x faster | 62% less |
| DPO - Zephyr | ▶️ Start on Colab | 1.9x faster | 19% less |
transformers >= 4.35.3.
The model supports multi-image and multi-prompt generation. Meaning that you can pass multiple images in your prompt. Make sure also to follow the correct prompt template (USER: xxx\nASSISTANT:) and add the token <image> to the location where you want to query images:pipeline:"llava-hf/llava-1.5-7b-hf" checkpoint.1from transformers import pipeline, AutoProcessor
2from PIL import Image
3import requests
4
5model_id = "llava-hf/llava-1.5-7b-hf"
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 history 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 "role": "user",
15 "content": [
16 {"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
17 {"type": "image"},
18 ],
19 },
20]
21processor = AutoProcessor.from_pretrained(model_id)
22
23prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
24
25outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
26print(outputs)
27>>> {"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 = "llava-hf/llava-1.5-7b-hf"
8model = LlavaForConditionalGeneration.from_pretrained(
9 model_id,
10 torch_dtype=torch.float16,
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.float16,
4 low_cpu_mem_usage=True,
5+ use_flash_attention_2=True
6).to(0)