-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
387 lines (329 loc) · 14.9 KB
/
inference.py
File metadata and controls
387 lines (329 loc) · 14.9 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
""" Utility classes and functions related to FRES (ACL 2025).
Copyright (c) 2025 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
butWITHOUT ANYWARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import argparse
import base64
import json
import torch
from prompts import PROMPT_NT, PROMPT_NT_IMG
from utils import reformat_table, load_image
from PIL import Image
from tqdm import tqdm
from mistral_inference.transformer import Transformer
from mistral_inference.generate import generate
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage, TextChunk, ImageURLChunk
from mistral_common.protocol.instruct.request import ChatCompletionRequest
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
from transformers import AutoProcessor, AutoTokenizer, AutoModelForCausalLM, AutoModel
from vllm import LLM, SamplingParams
from qwen_vl_utils import process_vision_info
MIN_PIXELS = 20*28*28
MAX_PIXELS = 35000*28*28
def prepare_prompts(instance, add_table_text, no_image, style, no_question):
white_place_holder_url = base64.b64encode(
open("../data/images/white_place_holder.jpg", 'rb').read()).decode('ascii')
question = instance["question"] if not no_question else ""
dataset = instance["dataset"] if "dataset" in list(
instance.keys()) else ""
id = instance["table_id"] if "table_id" in list(
instance.keys()) else instance['id']
id = str(id) if not isinstance(id, str) else id
table_caption = instance["table_caption"] if "table_caption" in list(
instance.keys()) else None
table_string = instance["table_string"] if "table_string" in list(
instance.keys()) else reformat_table(instance["table"], table_caption, style=style)
image_path = f"../data/images/{dataset}_{id}.jpg"
try:
image_url = base64.b64encode(
open(image_path, 'rb').read()).decode('ascii')
except:
print("Can not find image path:", image_path)
image_url = None
if add_table_text:
text_prompt = PROMPT_NT[style].format(
question=question, input_seg=table_string)
else:
text_prompt = PROMPT_NT_IMG[style].format(
question=question)
if not no_image:
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": f"data:image;base64,{image_url}"},
{"type": "text", "text": text_prompt}
],
}
]
else:
messages = [
{
"role": "user",
"content": [
{"type": "image",
"image": f"data:image;base64,{white_place_holder_url}"},
{"type": "text", "text": text_prompt}
],
}
]
return messages
def run_glm_hf(model_path):
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True
).to("cuda").eval()
return model, tokenizer
def glm_inference(model, tokenizer, image, text, max_length=8000):
image = image.convert('RGB')
inputs = tokenizer.apply_chat_template([{"role": "user", "image": image, "content": text}],
add_generation_prompt=True, tokenize=True, return_tensors="pt",
return_dict=True) # chat mode
# print(len(inputs)) # input_ids attention_mask position_ids images
if inputs["input_ids"].shape[-1] > max_length:
inputs["input_ids"] = inputs["input_ids"][:, :max_length]
inputs["attention_mask"] = inputs["attention_mask"][:, :max_length]
inputs["position_ids"] = inputs["position_ids"][:, :max_length]
inputs = inputs.to("cuda")
gen_kwargs = {"max_new_tokens": 256, "do_sample": False}
with torch.no_grad():
outputs = model.generate(**inputs, **gen_kwargs)
outputs = outputs[:, inputs['input_ids'].shape[1]:]
generated_texts = tokenizer.decode(outputs[0])
return generated_texts
def run_phi_hf(model_path):
# Note: set _attn_implementation='eager' if you don't have flash_attn installed
model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="cuda",
trust_remote_code=True,
torch_dtype="auto",
_attn_implementation='eager'
)
# for best performance, use num_crops=4 for multi-frame, num_crops=16 for single-frame.
processor = AutoProcessor.from_pretrained(model_path,
trust_remote_code=True,
num_crops=16
)
return model, processor
def phi_inference(model, tokenizer, image, text, max_length=8000):
images = [image]
placeholder = "<|image_1|>\n"
messages = [
{"role": "user", "content": placeholder+text},
]
prompt = tokenizer.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
inputs = tokenizer(prompt, images, max_length=max_length,
return_tensors="pt") # input_ids, attention_mask, pixel_value, image_sizes
if inputs["input_ids"].shape[-1] > max_length:
inputs["input_ids"] = inputs["input_ids"][:, :max_length]
inputs["attention_mask"] = inputs["attention_mask"][:, :max_length]
inputs = inputs.to("cuda")
generation_args = {
"max_new_tokens": 256,
"temperature": 0.0,
"do_sample": False,
}
generate_ids = model.generate(**inputs,
eos_token_id=tokenizer.tokenizer.eos_token_id,
**generation_args
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
response = tokenizer.batch_decode(generate_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False)[0]
return response
def run_pixtral_hf(model_path):
tokenizer = MistralTokenizer.from_file(f"{model_path}/tekken.json")
model = Transformer.from_folder(model_path)
return model, tokenizer
def run_llava_next(model_path):
processor = LlavaNextProcessor.from_pretrained(model_path)
llm = LlavaNextForConditionalGeneration.from_pretrained(
model_path, torch_dtype=torch.float16, low_cpu_mem_usage=True)
llm.to("cuda:0")
return llm, processor
def llava_inference(model, processor, image, text):
# Define a chat history and use `apply_chat_template` to get correctly formatted prompt
# Each value in "content" has to be a list of dicts with types ("text", "image")
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": text},
{"type": "image"},
],
},
]
prompt = processor.apply_chat_template(
conversation, add_generation_prompt=True)
inputs = processor(images=image, text=prompt,
return_tensors="pt").to("cuda:0")
# autoregressively complete prompt
output = model.generate(**inputs, max_new_tokens=512, do_sample=False)
return processor.decode(output[0], skip_special_tokens=True)
def run_internvl(model_path):
# model_name = "OpenGVLab/InternVL2-2B"
llm = AutoModel.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True).eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True, use_fast=False)
return llm, tokenizer
def internvl_inference(model, tokenizer, image_path, text, max_length=8000):
# max length 8192
# set the max number of tiles in `max_num`
pixel_values = load_image(image_path,
max_num=1).to(torch.bfloat16).cuda()
generation_config = dict(max_new_tokens=256, do_sample=False)
question = f'<image>\n{text}'
model_inputs_len = len(tokenizer.encode(question))
if model_inputs_len > max_length:
question = tokenizer.decode(tokenizer.encode(question)[
:max_length])
response = model.chat(tokenizer, pixel_values, question, generation_config)
return response
def loading_model(model_path):
llm = LLM(
model=model_path,
limit_mm_per_prompt={"image": 2},
# dtype="float16"
)
processor = AutoProcessor.from_pretrained(
model_path, min_pixels=MIN_PIXELS, max_pixels=MAX_PIXELS)
return llm, processor
def mistral_inference(model, tokenizer, image_url, text):
completion_request = ChatCompletionRequest(messages=[UserMessage(
content=[ImageURLChunk(image_url=image_url), TextChunk(text=text)])])
encoded = tokenizer.encode_chat_completion(completion_request)
images = encoded.images
tokens = encoded.tokens
out_tokens, _ = generate([tokens], model, images=[images], max_tokens=1024,
temperature=0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
result = tokenizer.decode(out_tokens[0])
if "```json" in result:
result = "\n".join(result.split("\n")[1:-1])
return result
def load_dataset(dataset_path):
with open(dataset_path, "r") as f:
data = [json.loads(item) for item in f]
return data
def main(args):
if "qwen" in args.model_name:
llm, processor = loading_model(args.model_path)
elif "intern" in args.model_name:
# processor is the tokenizer
llm, tokenizer = run_internvl(args.model_path)
elif "mistral" in args.model_name:
llm, tokenizer = run_pixtral_hf(args.model_path)
elif "llava" in args.model_name:
llm, tokenizer = run_llava_next(args.model_path)
elif "phi" in args.model_name:
llm, tokenizer = run_phi_hf(args.model_path)
elif "glm" in args.model_name:
llm, tokenizer = run_glm_hf(args.model_path)
dataset = load_dataset(args.dataset_path)
for i, item in enumerate(dataset):
item["instance_index"] = i
messages = [prepare_prompts(item, add_table_text=args.add_table_text,
no_image=args.no_image, style=args.style, no_question=args.no_question) for item in dataset]
sampling_params = SamplingParams(
temperature=0,
max_tokens=256,
seed=42,
n=1
)
with open(args.output_dir+f'/{args.model_name}_add_table_text_{args.add_table_text}_no_image_{args.no_image}_style_{args.style}_no_question_{args.no_question}_output.json', "w") as f:
for msg, ori in tqdm(zip(messages, dataset)):
dataset = ori["dataset"] if "dataset" in list(ori.keys()) else ""
id = ori["table_id"] if "table_id" in list(ori.keys()) else ""
if not args.no_image:
try:
image_path = f"images/{dataset}_{id}.jpg"
img = Image.open(image_path)
except:
img = None
else:
image_path = "images/white_place_holder.jpg"
img = Image.open(image_path)
encoded_image_url = msg[0]["content"][0]["image"]
text = msg[0]["content"][1]["text"]
if "qwen" in args.model_name:
prompt = processor.apply_chat_template(
msg, tokenize=False, add_generation_prompt=True)
# inputs = processor(
# text=[prompt], images=[img], padding=True, return_tensors="pt"
# )
# input_token_length = inputs["input_ids"].shape[-1]
mm_data = {"image": process_vision_info(
msg)[0]} # image_input
llm_inputs = {"prompt": prompt, "multi_modal_data": mm_data}
outputs = llm.generate(
[llm_inputs], sampling_params=sampling_params)
generated_texts = outputs[0].outputs[0].text
elif "mistral" in args.model_name:
generated_texts = mistral_inference(
llm, tokenizer, encoded_image_url, text)
elif "llava" in args.model_name:
generated_texts = llava_inference(llm, tokenizer, img, text)
elif "intern" in args.model_name:
generated_texts = internvl_inference(
llm, tokenizer, image_path, text)
elif "phi" in args.model_name:
generated_texts = phi_inference(llm, tokenizer, img, text)
elif "glm" in args.model_name:
generated_texts = glm_inference(llm, tokenizer, img, text)
idx = ori["id"] if "id" in list(ori.keys()) else ""
table_idx = ori["table_id"] if "table_id" in list(
ori.keys()) else ""
question = ori["question"]
answer = ori["answer"]
quetsion_complexity = ori["question_complexity"] if "question_complexity" in list(
ori.keys()) else ""
size = ori["size"] if "size" in list(ori.keys()) else ""
table_res = ori["table_res"] if "table_res" in list(
ori.keys()) else ""
dataset = ori["dataset"] if "dataset" in list(
ori.keys()) else ""
new_instance = {"question": question,
"answer": answer, "pred_answer": generated_texts, "id": idx, "table_id": table_idx, "question_complexity": quetsion_complexity, "size": size, "table_res": table_res, "dataset": dataset}
f.write(json.dumps(new_instance)+"\n")
f.flush()
return
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str,
default="")
parser.add_argument('--dataset_path', type=str,
default="controlled_data.json")
parser.add_argument('--model_name', type=str,
default="qwen7b")
parser.add_argument('--output_dir', type=str,
default='.')
parser.add_argument('--no_question', action='store_true')
parser.add_argument('--add_table_text', action='store_true')
parser.add_argument('--no_image', action='store_true')
parser.add_argument('--bz', type=int, default=10)
parser.add_argument('--style', type=int, default=0)
args = parser.parse_args()
main(args)