-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_infer_imagegen.py
More file actions
203 lines (163 loc) · 6.46 KB
/
test_infer_imagegen.py
File metadata and controls
203 lines (163 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import torch
import time
import numpy as np
from bisect import bisect_left
from tqdm import tqdm
from transformers import (
AutoProcessor,
)
from modeling_bailingmm2 import BailingMM2NativeForConditionalGeneration
import warnings
warnings.filterwarnings("ignore")
from IPython import embed
import json
from PIL import Image
# def split_model():
# device_map = {}
# world_size = torch.cuda.device_count()
# num_layers = 32
# layer_per_gpu = num_layers // world_size
# layer_per_gpu = [i * layer_per_gpu - 1 for i in range(1, world_size + 1)]
# for i in range(num_layers):
# device_map[f'model.model.layers.{i}'] = bisect_left(layer_per_gpu, i)
# device_map['vision'] = 0
# device_map['audio'] = 0
# device_map['linear_proj'] = 0
# device_map['linear_proj_audio'] = 0
# device_map['model.model.word_embeddings.weight'] = 0
# device_map['model.model.norm.weight'] = 0
# device_map['model.lm_head.weight'] = 0
# device_map['model.model.norm'] = 0
# device_map[f'model.model.layers.{num_layers - 1}'] = 0
# return device_map
def split_model():
device_map = {}
world_size = torch.cuda.device_count() - 1
print(world_size)
num_layers = 32
layer_per_gpu = num_layers // world_size
layer_per_gpu = [i * layer_per_gpu - 1 for i in range(1, world_size + 1)]
for i in range(num_layers):
device_id = bisect_left(layer_per_gpu, i) + 1
#print(device_id)
if device_id > world_size:
device_id = i % world_size + 1
print(device_id)
device_map[f'model.model.layers.{i}'] = device_id
device_map['vision'] = 0
device_map['audio'] = 0
device_map['linear_proj'] = 0
device_map['linear_proj_audio'] = 0
device_map['model.model.word_embeddings.weight'] = 0
device_map['model.model.norm.weight'] = 0
device_map['model.lm_head.weight'] = 0
device_map['model.model.norm'] = 0
device_map[f'model.model.layers.{num_layers - 1}'] = 0
return device_map
if __name__ == '__main__':
model_name_or_path = "/nativemm/share/cpfs/yuxuzheng.yxz/release/bailing_native_moe_ming_flash_v2.0_xpo_final_20260205_hf_metax_ais16863699"
#"/nativemm/share/cpfs/weilong.cwl/checkpoints/megatron_flashv2.0_sft1_hf_metax/" #"."
code_path = "."
processor = AutoProcessor.from_pretrained(code_path, trust_remote_code=True)
save_dir = "./generated_imgs"
if not os.path.exists(save_dir):
os.mkdir(save_dir)
model = BailingMM2NativeForConditionalGeneration.from_pretrained(
model_name_or_path,
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
device_map=split_model(),
load_image_gen=True,
).to(dtype=torch.bfloat16)
prompt = "Draw a beautiful girl with short black hair and red dress."
messages = [
{
"role": "HUMAN",
"content": [
{"type": "text", "text": prompt},
],
}
]
text = processor.apply_chat_template(messages, add_generation_prompt=True)
image_inputs, _, _ = processor.process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
return_tensors="pt",
).to(model.device)
for k in inputs.keys():
if k in ["pixel_values", "pixel_values_videos", "audio_feats", "pixel_values_reference"]:
inputs[k] = inputs[k].to(dtype=torch.bfloat16)
print(f"Instruction: {prompt}")
# set `image_gen=True` to enable image generation
image = model.generate(
**inputs,
image_gen=True,
image_gen_seed=42,
)
save_path = os.path.join(save_dir, "./t2i_girl.jpg")
image.save(save_path)
print(f"saved to {save_path}")
prompt = "背景换成沙滩, 动作是拿手机自拍."
messages = [
{
"role": "HUMAN",
"content": [
{"type": "image", "image": save_path},
{"type": "text", "text": prompt},
],
}
]
text = processor.apply_chat_template(messages, add_generation_prompt=True)
image_inputs, _, _ = processor.process_vision_info(messages)
ref_image_inputs = processor.process_reference_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
return_tensors="pt",
image_gen_ref_images=ref_image_inputs,
)
inputs = inputs.to(model.device)
for k in inputs.keys():
if k in ["pixel_values", "pixel_values_videos", "audio_feats", "pixel_values_reference"]:
inputs[k] = inputs[k].to(dtype=torch.bfloat16)
print(f"Instruction: {prompt}; Input image: {save_path}")
# set `image_gen=True` to enable image generation
image = model.generate(
**inputs,
image_gen=True,
image_gen_seed=43,
)
save_path = os.path.join(save_dir, "./edit_girl.jpg")
image.save(save_path)
print(f"saved to {save_path}")
prompt = "A whimsical comic-style illustration of a cozy bookstore entrance on a sunny afternoon. The storefront features warm brick walls and large glass windows filled with stacked books and potted ferns. Above the wooden door hangs a hand-painted signboard with bold, stylized Chinese characters reading “理解与生成统一” accented with curling vines and tiny stars. Sunlight casts playful shadows on the cobblestone path leading to the door, where a vintage lantern in a sunbeam add charm. The linework is clean, colors vibrant yet soft, evoking a friendly, storybook atmosphere. No people or vehicles are present, emphasizing quiet serenity."
messages = [
{
"role": "HUMAN",
"content": [
{"type": "text", "text": prompt},
],
}
]
text = processor.apply_chat_template(messages, add_generation_prompt=True)
image_inputs, _, _ = processor.process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
return_tensors="pt",
).to(model.device)
for k in inputs.keys():
if k in ["pixel_values", "pixel_values_videos", "audio_feats", "pixel_values_reference"]:
inputs[k] = inputs[k].to(dtype=torch.bfloat16)
print(f"Instruction: {prompt}")
# set `image_gen=True` to enable image generation
image = model.generate(
**inputs,
image_gen=True,
image_gen_seed=42,
)
save_path = os.path.join(save_dir, "./t2i_text.jpg")
image.save(save_path)
print(f"saved to {save_path}")