Views
No views yet
LlavaLlamaForCausalLM) incorporating a surrogate-trained vision encoder, and can be used directly with the transformers library.1from transformers import AutoProcessor, AutoModelForCausalLM
2import torch
3from PIL import Image
4import requests
5from io import BytesIO
6
7# Load model and processor
8# Example model ID for a surrogate-trained 8B Llama-3.1 encoder
9model_id = "tomg-group-umd/llama3.1-8b_surrogate-trained-encoder"
10model = AutoModelForCausalLM.from_pretrained(
11 model_id,
12 torch_dtype=torch.bfloat16,
13 device_map="auto",
14 trust_remote_code=True # Required as it uses a custom LlavaLlamaForCausalLM architecture
15)
16processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
17
18# Prepare inputs
19image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/bird_sized.jpg"
20image = Image.open(BytesIO(requests.get(image_url).content))
21
22messages = [
23 {"role": "user", "content": "What is in this image?"},
24]
25
26# Apply chat template and process inputs
27prompt = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
28inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
29
30# Generate response
31output_ids = model.generate(**inputs, max_new_tokens=50)
32output = processor.decode(output_ids[0], skip_special_tokens=True)
33
34print(output)
35# Expected output (may vary slightly):
36# "A bird with a blue head and green body is perched on a branch. The bird has a long tail and is facing to the right."1@inproceedings{yue2025zero,
2 title = {Zero-Shot Vision Encoder Grafting via LLM Surrogates},
3 author = {Yue, Kaiyu and Singla, Vasu and Jia, Menglin and Kirchenbauer, John and Qadri, Rifaa and Cai, Zikui and Bhatele, Abhinav and Huang, Furong and Goldstein, Tom},
4 booktitle = {International Conference on Computer Vision (ICCV)},
5 year = {2025}
6}