Views
No views yet

transformers >= 4.48.0.
Below is an example script to run generation in 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-Gen-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)
16inputs = processor(
17 text=["a portrait of young girl. masterpiece, film grained, best quality."],
18 padding=True,
19 return_tensors="pt",
20 return_for_image_generation=True,
21).to(model.device)
22
23image_sizes = inputs.pop("image_sizes")
24HEIGHT, WIDTH = image_sizes[0]
25VISUAL_TOKENS = model.vocabulary_mapping.image_tokens
26
27def prefix_allowed_tokens_fn(batch_id, input_ids):
28 height, width = HEIGHT, WIDTH
29 visual_tokens = VISUAL_TOKENS
30 image_wrapper_token_id = torch.tensor([processor.tokenizer.image_wrapper_token_id], device=model.device)
31 eoi_token_id = torch.tensor([processor.tokenizer.eoi_token_id], device=model.device)
32 eos_token_id = torch.tensor([processor.tokenizer.eos_token_id], device=model.device)
33 pad_token_id = torch.tensor([processor.tokenizer.pad_token_id], device=model.device)
34 eof_token_id = torch.tensor([processor.tokenizer.eof_token_id], device=model.device)
35 eol_token_id = processor.tokenizer.encode("<|extra_200|>", return_tensors="pt")[0]
36
37 position = torch.nonzero(input_ids == image_wrapper_token_id, as_tuple=True)[0][0]
38 offset = input_ids.shape[0] - position
39 if offset % (width + 1) == 0:
40 return (eol_token_id,)
41 elif offset == (width + 1) * height + 1:
42 return (eof_token_id,)
43 elif offset == (width + 1) * height + 2:
44 return (eoi_token_id,)
45 elif offset == (width + 1) * height + 3:
46 return (eos_token_id,)
47 elif offset > (width + 1) * height + 3:
48 return (pad_token_id,)
49 else:
50 return visual_tokens
51
52out = model.generate(
53 **inputs,
54 max_new_tokens=9_000,
55 prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,
56 do_sample=True,
57)
58
59image = model.decode_image_tokens(out.sequences[:, inputs.input_ids.shape[1]: ], height=HEIGHT, width=WIDTH)
60images = processor.postprocess(list(image.float()), return_tensors="PIL.Image.Image")
61for i, image in enumerate(images['pixel_values']):
62 image.save(f"result{i}.png")
63 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},
}