Views
No views yet

transformers >= 4.48.0.
Make sure also to follow the correct prompt template (USER: xxxASSISTANT:) and add the token <image> to the location where you want to query images:pipeline:1from transformers import pipeline
2
3pipe = pipeline("image-text-to-text", model="BAAI/Emu3-Chat-hf")
4messages = [
5 {
6 "role": "user",
7 "content": [
8 {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"},
9 {"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
10 ],
11 },
12]
13
14out = pipe(text=messages, max_new_tokens=20)
15print(out)
16>>> [{'input_text': [{'role': 'user', 'content': [{'type': 'image', 'url': 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'}, {'type': 'text', 'text': 'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'}]}], 'generated_text': 'Lava'}]transformers:float16 precision on a GPU device:1import requests
2from PIL import Image
3
4import torch
5from transformers import AutoProcessor, Emu3ForConditionalGeneration
6
7model_id = "BAAI/Emu3-Chat-hf"
8model = Emu3ForConditionalGeneration.from_pretrained(
9 model_id,
10 torch_dtype=torch.float16,
11 low_cpu_mem_usage=True,
12 device_map="cuda:0",
13)
14
15processor = AutoProcessor.from_pretrained(model_id)
16
17# Define a chat history and use `apply_chat_template` to get correctly formatted prompt
18# Each value in "content" has to be a list of dicts with types ("text", "image")
19conversation = [
20 {
21
22 "role": "user",
23 "content": [
24 {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
25 {"type": "text", "text": "What are these?"},
26 ],
27 },
28]
29inputs_dict = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=True, return_dict=True)
30inputs_dict = inputs_dict.to(0, torch.float16)
31
32output = model.generate(**inputs_dict, max_new_tokens=50, do_sample=False)
33print(processor.decode(output[0][2:], skip_special_tokens=True))flash-attn. Refer to the original repository of Flash Attention regarding that package installation. Simply change the snippet above with:1model = Emu3ForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.float16,
4 low_cpu_mem_usage=True,
5+ attn_implementation="flash_attention_2",
6 device_map="cuda:0",
7)@misc{wang2024emu3nexttokenpredictionneed,
title={Emu3: Next-Token Prediction is All You Need},
author={Xinlong Wang and Xiaosong Zhang and Zhengxiong Luo and Quan Sun and Yufeng Cui and Jinsheng Wang and Fan Zhang and Yueze Wang and Zhen Li and Qiying Yu and Yingli Zhao and Yulong Ao and Xuebin Min and Tao Li and Boya Wu and Bo Zhao and Bowen Zhang and Liangdong Wang and Guang Liu and Zheqi He and Xi Yang and Jingjing Liu and Yonghua Lin and Tiejun Huang and Zhongyuan Wang},
year={2024},
eprint={2409.18869},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2409.18869},
}