Views
No views yet


1import os
2import torch
3from torchvision.utils import save_image
4from transformers import AutoModelForCausalLM, AutoProcessor
5os.environ["CUDA_VISIBLE_DEVICES"] = "7"
6os.environ["HF_HUB_OFFLINE"] = "1"
7os.environ["TRANSFORMERS_OFFLINE"] = "1"
8
9ckpt = "ckpt_path"
10device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
12processor = AutoProcessor.from_pretrained(ckpt, trust_remote_code=True)
13model = AutoModelForCausalLM.from_pretrained(ckpt, trust_remote_code=True)
14model.to(device)
15model = model.to(torch.bfloat16)
16model.eval()
17
18content = """
19In the center of a bustling intersection, a large tree with a thick trunk and sprawling branches stands out amidst the concrete.
20Its green leaves contrast sharply with the grey asphalt roads that converge around it. Traffic lights and street signs are positioned awkwardly around the tree's base, creating an unusual juxtaposition of nature and urban infrastructure.
21"""
22images_batch = [None]
23
24messages_batch = [
25 [{"role": "user", "content": content}],
26 ]
27
28texts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in messages_batch]
29inputs = processor(text=texts, images=images_batch, return_tensors="pt", add_im_start_id=True)
30inputs = {k: (v.to(device=device) if isinstance(v, torch.Tensor) else v) for k, v in inputs.items()}
31
32gen_config = {
33 "max_length": 300,
34 "cfg_scale": 9.5,
35 "temperature": 0.0,
36 "num_inference_steps": 80,
37 "alpha": 0.5,
38 "edit_image": False,
39}
40
41inputs.update(gen_config)
42generated = model.generate(**inputs)
43input_ids = generated["input_ids"]
44images = generated["images"][0]
45
46current_img = images[0]
47current_img = current_img.clamp(0.0, 1.0)
48save_image(current_img, f"outputs/case_.png")
49
50print(f"Save image: outputs/case_.png")1import os
2import torch
3from transformers import AutoModelForCausalLM, AutoProcessor
4from PIL import Image
5os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
6os.environ["HF_HUB_OFFLINE"] = "1"
7os.environ["TRANSFORMERS_OFFLINE"] = "1"
8
9ckpt = "ckpt_path"
10device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
12processor = AutoProcessor.from_pretrained(ckpt, trust_remote_code=True)
13model = AutoModelForCausalLM.from_pretrained(ckpt, trust_remote_code=True)
14model = model.to(torch.bfloat16)
15model.to(device)
16model.eval()
17
18content = "<im_start><image><im_end>\n Discribe this image."
19
20img = Image.open("fig/logo.png")
21images_batch = [img,]
22
23messages_batch = [
24 [{"role": "user", "content": content}],
25 ]
26
27texts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in messages_batch]
28inputs = processor(text=texts, images=images_batch, return_tensors="pt", add_im_start_id=False)
29inputs = {k: (v.to(device=device) if isinstance(v, torch.Tensor) else v) for k, v in inputs.items()}
30
31gen_config = {
32 "max_length": 150,
33 "temperature": 0.3,
34}
35
36inputs.update(gen_config)
37generated = model.generate(**inputs)
38input_ids = generated["input_ids"]
39
40print(processor.tokenizer.batch_decode(input_ids, skip_special_tokens=True))1@article{zhang2026cheers,
2 title={CHEERS: DECOUPLING PATCH DETAILS FROM SEMANTIC REPRESENTATIONS ENABLES UNIFIED MULTIMODAL COMPREHENSION AND GENERATION},
3 author={Zhang, Yichen and Peng, Da and Guo, Zonghao and Zhang, Zijian and Yang, Xuesong and Sun, Tong and Sun, Shichu and Zhang, Yidan and Li, Yanghao and Zhao, Haiyan and others},
4 journal={arXiv preprint arXiv:2603.12793},
5 year={2026}
6}