-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_infer.py
More file actions
222 lines (191 loc) · 6.63 KB
/
test_infer.py
File metadata and controls
222 lines (191 loc) · 6.63 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import torch
import time
import numpy as np
from bisect import bisect_left
from transformers import (
AutoProcessor,
)
from modeling_bailingmm2 import BailingMM2NativeForConditionalGeneration
import warnings
warnings.filterwarnings("ignore")
def generate(messages, processor, model, sys_prompt_exp=None, use_cot_system_prompt=False, max_new_tokens=512):
text = processor.apply_chat_template(
messages,
sys_prompt_exp=sys_prompt_exp,
use_cot_system_prompt=use_cot_system_prompt
)
image_inputs, video_inputs, audio_inputs = processor.process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
audios=audio_inputs,
return_tensors="pt",
audio_kwargs={"use_whisper_encoder": True},
).to(model.device)
for k in inputs.keys():
if k == "pixel_values" or k == "pixel_values_videos" or k == "audio_feats":
inputs[k] = inputs[k].to(dtype=torch.bfloat16)
srt_time = time.time()
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
use_cache=True,
eos_token_id=processor.gen_terminator,
num_logits_to_keep=1,
)
end_time = time.time()
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
# tps = generated_ids.shape[1] / (end_time - srt_time)
# print(f"generated {generated_ids.shape[1]} tokens in {end_time - srt_time:.2f} seconds, tokens per second: {tps:.2f} tokens/s")
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
return output_text
if __name__ == '__main__':
model_name_or_path = "."
code_path = "."
model = BailingMM2NativeForConditionalGeneration.from_pretrained(
model_name_or_path,
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
device_map="auto",
load_image_gen=False,
).to(dtype=torch.bfloat16)
processor = AutoProcessor.from_pretrained(code_path, trust_remote_code=True)
vision_path = "/input/sunyunxiao.syx/assets/"
messages = [
{
"role": "HUMAN",
"content": [
{"type": "image", "image": os.path.join(vision_path, "flowers.jpg")},
{"type": "text", "text": "What kind of flower is this?"},
],
}
]
srt_time = time.time()
output_text = generate(messages, processor=processor, model=model, max_new_tokens=512)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")
messages = [
{
"role": "HUMAN",
"content": [
{"type": "text", "text": "请介绍下你自己"}
],
}
]
srt_time = time.time()
output_text = generate(messages, processor=processor, model=model, max_new_tokens=512)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")
messages = [
{
"role": "HUMAN",
"content": [
{"type": "video", "video": os.path.join(vision_path, "yoga.mp4")},
{"type": "text", "text": "What is the woman doing?"},
],
}
]
srt_time = time.time()
output_text = generate(messages, processor=processor, model=model, max_new_tokens=512)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")
messages = [
{
"role": "HUMAN",
"content": [
{"type": "text", "text": "中国的首都是哪里?"},
],
},
{
"role": "ASSISTANT",
"content": [
{"type": "text", "text": "北京"},
],
},
{
"role": "HUMAN",
"content": [
{"type": "text", "text": "它的占地面积是多少?有多少常住人口?"},
],
},
]
srt_time = time.time()
output_text = generate(messages, processor=processor, model=model, max_new_tokens=512)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")
messages = [
{
"role": "HUMAN",
"content": [
{"type": "text", "text": "请详细介绍鹦鹉的生活习性。"}
],
}
]
srt_time = time.time()
output_text = generate(messages, processor=processor, model=model, max_new_tokens=8192, use_cot_system_prompt=True)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")
messages = [
{
"role": "HUMAN",
"content": [
{"type": "video", "video": os.path.join(vision_path, "yoga.mp4"), "max_frames": 40, "sample": "uniform"},
{"type": "image", "image": os.path.join(vision_path, "flowers.jpg")},
{"type": "text", "text": "What is the woman doing in the video and what kind of flower is in the image?"},
],
}
]
srt_time = time.time()
output_text = generate(
messages, processor=processor, model=model, max_new_tokens=512
)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")
messages = [
{
"role": "HUMAN",
"content": [
{
"type": "video",
"video": os.path.join(vision_path, "video_demo294_0.mp4"),
},
{
"type": "text",
"text": "我们好像快到了吧?前面那个写着沃尔玛的就是我们要去的地方吗?",
},
],
},
{
"role": "ASSISTANT",
"content": [
{
"type": "text",
"text": "是的,根据路边的指示牌,我们正在接近沃尔玛超市的停车场。",
},
],
},
{
"role": "HUMAN",
"content": [
{
"type": "video",
"video": os.path.join(vision_path, "video_demo294_1.mp4"),
},
{
"type": "audio",
"audio": os.path.join(vision_path, "video_demo_query.wav"),
},
],
},
]
srt_time = time.time()
output_text = generate(messages, processor=processor, model=model, max_new_tokens=512)
print(output_text)
print(f"Generate time: {(time.time() - srt_time):.2f}s")