-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_decoding_final.py
More file actions
401 lines (352 loc) · 16.1 KB
/
control_decoding_final.py
File metadata and controls
401 lines (352 loc) · 16.1 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration, GenerationConfig
# from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration
# from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
# from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
import torch
from PIL import Image
import requests
import json
from vlm_value_models import ValueModel
import math
from tqdm import tqdm
import argparse
import numpy as np
import pandas as pd
import os
from typing import List, Dict
import gc
def split_list(lst, n):
"""Split a list into n (roughly) equal-sized chunks"""
chunk_size = math.ceil(len(lst) / n) # integer division
return [lst[i:i+chunk_size] for i in range(0, len(lst), chunk_size)]
def get_chunk(lst, n, k):
chunks = split_list(lst, n)
return chunks[k]
def load_data(data_path):
datas = []
with open(data_path, 'r') as files:
for line in files:
datas.append(json.loads(line))
return datas
def dump_to_jsonl(obj: list[dict], path: str):
with open(path, 'w') as file:
file.writelines([json.dumps(x) + '\n' for x in obj])
# def get_dataset(data_pth, llava_image_pth):
# with open(data_pth, "r", encoding="utf-8") as file:
# llava_datas = json.load(file)
# datas = []
# for data in llava_datas:
# prompt = data['prompt']
# if prompt.startswith("<image>\n"):
# prompt = prompt[len("<image>\n"):]
# if prompt.endswith("\n<image>"):
# prompt = prompt[:-len("\n<image>")]
# datas.append({
# 'text': prompt,
# 'image': f'{llava_image_pth}//{data["image"]}',
# 'image_path': f'{llava_image_pth}//{data["image"]}'
# })
# return datas
def get_dataset(data_pth: str, llava_image_pth: str) -> List[Dict[str, str]]:
dataset = []
_, ext = os.path.splitext(data_pth)
ext = ext.lower()
if ext in {".parquet", ".pq"}:
df = pd.read_parquet(data_pth)
for idx, row in df.iterrows():
question = row.get('question') or row.get('text') or ""
image_field = row.get('image_path') or row.get('image') or f"{idx}.jpg"
image_full_path = os.path.join(llava_image_pth, os.path.basename(image_field))
dataset.append({
'text': question,
'image': image_full_path,
'image_path': image_full_path
})
else:
with open(data_pth, "r", encoding="utf-8") as file:
records = json.load(file)
for rec in records:
prompt = rec.get('prompt') or rec.get('question') or ""
if prompt.startswith("<image>\n"):
prompt = prompt[len("<image>\n"):]
if prompt.endswith("\n<image>"):
prompt = prompt[:-len("\n<image>")]
image_name = rec.get('image') or rec.get('image_path') or ""
image_full_path = os.path.join(llava_image_pth, os.path.basename(image_name))
dataset.append({
'text': prompt,
'image': image_full_path,
'image_path': image_full_path
})
return dataset
def main(args):
device = "cuda" if torch.cuda.is_available() else "cpu"
############# Load data ##############
# datas = load_data(args.data_pth)
# data_chunk = get_chunk(datas, args.num_chunks, args.chunk_idx)
dataset = get_dataset(data_pth=args.data_pth, llava_image_pth=args.image_folder)
data_chunk = get_chunk(dataset, args.num_chunks, args.chunk_idx)
############# Load VLM ##############
processor = LlavaNextProcessor.from_pretrained(args.model_id)
model_dtype = torch.float16 if device == "cuda" else torch.float32
model = LlavaNextForConditionalGeneration.from_pretrained(
args.model_id,
torch_dtype=model_dtype,
low_cpu_mem_usage=True,
local_files_only=True,
device_map=device if device == "cuda" else None)
model = model.bfloat16()
model.to(device)
print("Model Loading....")
############# Load Value net ##############
value_net = ValueModel(args.model_id)
value_net.from_pretrained(args.value_net_pth)
torch.cuda.empty_cache()
value_net_dtype = torch.float16 if device == "cuda" else torch.float32
value_net.to(device)
value_net = value_net.bfloat16()
print("value_net Model Loaded....")
tokenizer_max_len = getattr(value_net.processor.tokenizer, "model_max_length", args.value_net_max_length)
value_net_max_length = min(args.value_net_max_length, tokenizer_max_len)
if value_net_max_length <= 0:
raise ValueError("value_net_max_length must be positive.")
import time
decoding_results = []
for data in tqdm(data_chunk, desc="Decoding Progress"):
start_time = time.perf_counter()
try:
images = [Image.open(data['image_path'])]
conversation = [{
"role": "user",
"content": [
{"type": "text", "text": data['text']},
{"type": "image"},
],
}]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
inputs = processor(images=images, text=[prompt], return_tensors="pt").to(device)
question_input_length = inputs['input_ids'].shape[1]
temp_generation_config_list = [
GenerationConfig(
temperature=0.1,
do_sample=True,
top_p=0.9,
),
GenerationConfig(
temperature=0.3,
do_sample=True,
top_p=0.9,
),
GenerationConfig(
temperature=0.5,
do_sample=True,
top_p=0.9,
),
GenerationConfig(
temperature=0.7,
do_sample=True,
top_p=0.9,
),
GenerationConfig(
temperature=0.9,
do_sample=True,
top_p=0.9,
),
GenerationConfig(
do_sample=False,
)
]
candidate_replies = []
candidate_states = []
for temp_generation_config in temp_generation_config_list:
with torch.no_grad():
outputs = model.generate(
**inputs,
generation_config=temp_generation_config,
max_length=4096,
tokenizer=processor.tokenizer,
# stop_strings=['.'],
num_beams=args.num_beams,
num_return_sequences=args.num_return_sequences
)
for output in outputs:
new_generated_reply = processor.decode(output[question_input_length:], skip_special_tokens=True)
candidate_replies.append(new_generated_reply)
state = value_net.base_model.processor.apply_chat_template([
{
"role": "user",
"content": [
{"type": "text", "text": new_generated_reply},
{"type": "image"}
]
}
], tokenize=False)
candidate_states.append(state)
batch = value_net.base_model.processor(
text=candidate_states,
images=len(candidate_states)*images,
padding='max_length',
max_length=value_net_max_length,
truncation=True,
return_tensors="pt"
).to(device)
current_inputs = {
'input_ids': batch['input_ids'],
'attention_mask': batch['attention_mask'],
'pixel_values': batch['pixel_values'],
'image_sizes': batch['image_sizes'],
}
with torch.no_grad():
candidate_values = value_net.base_model(current_inputs)
max_index = torch.argmax(candidate_values).item()
chosen_response = candidate_replies[max_index]
## Find the low-scoring segments
sentences = [s.strip() for s in chosen_response.split('.') if s.strip()]
if not sentences: # nothing to filter
final_caption = chosen_response
else:
sentence_states = [
value_net.base_model.processor.apply_chat_template(
[
{
"role": "user",
"content": [
{"type": "text", "text": s},
{"type": "image"}
]
}
],
tokenize=False
)
for s in sentences
]
batch = value_net.base_model.processor(
text=sentence_states,
images=len(sentence_states) * images, # replicate image per sentence
padding='max_length',
max_length=value_net_max_length,
truncation=True,
return_tensors="pt"
).to(device)
current_inputs = {
"input_ids": batch["input_ids"],
"attention_mask":batch["attention_mask"],
"pixel_values": batch["pixel_values"],
"image_sizes": batch["image_sizes"],
}
with torch.no_grad():
values = value_net.base_model(current_inputs)
values = values.squeeze(-1)
# ── 5. filter sentences whose score ≥ 2.0 ──────────────────────────────────
keep_mask = values >= 2.14 # Bool tensor
filtered_sentences = [s for s, keep in zip(sentences, keep_mask) if keep]
final_caption = '. '.join(filtered_sentences)
if final_caption: # add trailing period
final_caption += '.'
new_generated_reply = final_caption
del inputs
assistant_reply = None
# max_iterations = 5 # Prevent infinite loops and iteration < max_iterations
iteration = 0
while assistant_reply != new_generated_reply:
assistant_reply = new_generated_reply
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": data['text']},
{"type": "image"},
],
},
{
"role": "assistant",
"content": [
{"type": "text", "text": '{TEXT}'}, ],
}]
conversation[-1]['content'][0]['text'] = chosen_response
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
prompt = prompt[:-5] # remove trailing token (e.g., '</s>')
inputs = processor(images=images, text=[prompt], return_tensors="pt").to(device)
reply_input_length = inputs['input_ids'].shape[1]
candidate_replies = []
candidate_new_replies = []
candidate_states = []
for temp_generation_config in temp_generation_config_list:
with torch.no_grad():
outputs = model.generate(
**inputs,
generation_config=temp_generation_config,
max_length=4096,
# max_length=512,
tokenizer=processor.tokenizer,
stop_strings=['.'],
num_beams=args.num_beams,
num_return_sequences=args.num_return_sequences
)
for output in outputs:
reply_candidate = processor.decode(output[question_input_length:], skip_special_tokens=True)
new_generated_candidate = processor.decode(output[reply_input_length:], skip_special_tokens=True)
candidate_replies.append(reply_candidate)
candidate_new_replies.append(new_generated_candidate)
state = value_net.base_model.processor.apply_chat_template([
{
"role": "user",
"content": [
{"type": "text", "text": new_generated_candidate},
{"type": "image"}
]
}
], tokenize=False)
candidate_states.append(state)
batch = value_net.base_model.processor(
text=candidate_states,
images=len(candidate_states)*images,
padding='max_length',
max_length=value_net_max_length,
truncation=True,
return_tensors="pt"
).to(device)
current_inputs = {
'input_ids': batch['input_ids'],
'attention_mask': batch['attention_mask'],
'pixel_values': batch['pixel_values'],
'image_sizes': batch['image_sizes']
}
with torch.no_grad():
candidate_values = value_net.base_model(current_inputs)
best_index = torch.argmax(candidate_values).item()
chosen_response = candidate_replies[best_index]
new_generated_reply = candidate_new_replies[best_index]
del inputs, batch # Free memory for next iteration
iteration += 1
torch.cuda.empty_cache()
gc.collect()
print(chosen_response)
decoding_results.append({
'text': data['text'],
'image': data['image'],
'image_path': data['image_path'],
'decoding_result': chosen_response,
})
except Exception as e:
print(f"An unexpected error occurred: {e}")
elapsed = time.perf_counter() - start_time
print(f"\n[control_decoding_final.py] elapsed time: {elapsed:.2f} s")
dump_to_jsonl(decoding_results, args.output_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_id", type=str, default="llava-hf/llava-v1.6-mistral-7b-hf")
parser.add_argument("--data_pth", type=str, default=None)
parser.add_argument("--image_folder", type=str, default=None)
parser.add_argument("--value_net_pth", type=str, default=None)
parser.add_argument("--value_net_max_length", type=int, default=2048)
parser.add_argument("--step_size", type=int, default=1)
parser.add_argument("--num_return_sequences", type=int, default=2)
parser.add_argument("--num_beams", type=int, default=4)
parser.add_argument("--num-chunks", type=int, default=1)
parser.add_argument("--chunk-idx", type=int, default=0)
parser.add_argument("--per_gpu_batch_size", type=int, default=2)
parser.add_argument("--gpu-id", type=int, default=0)
parser.add_argument("--output_file", type=str, default="answer.jsonl")
args = parser.parse_args()
main(args)