File size: 55,348 Bytes
1efcb3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
import argparse
import traceback
import random
import re
import copy
import torch
import torch.distributed as dist
import os
import json, jsonlines
from tqdm import tqdm
import pdb
import numpy as np
import socket
from vllm import LLM, SamplingParams
from transformers import Qwen2_5_VLProcessor, Qwen2_5_VLForConditionalGeneration
from transformers import AutoProcessor, AutoTokenizer
from qwen_vl_utils import process_vision_info, fetch_image
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from datasets import load_dataset
import sys
sys.path.append('/mnt/beegfs/dzhu6/ViLaSR')
from utils.edit_image import merge_bbox_movement, parse_bbox_and_movement, plot_movement, plot_bounding_boxes
from typing import List, Dict, Any
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
import time, csv
csv.field_size_limit(sys.maxsize)


SYSTEM_PROMPT = """### Guidance:
You are a spatial reasoning assistant with access to two powerful visualization tools.
Your task is to break down complex spatial problems and iteratively refine your solution through visualization feedback.

### Available tools:
You can use the following two tools to visualize. After each tool usage, you must wait for and analyze the visualization feedback before proceeding.

1. **Object Mapper**
- Purpose: Identifies and maps key items in the space
- Input format: JSON
```json
[{{
    "index": i, # Image index
    "bbox_2d": [x1, y1, x2, y2],
    "label": "object name/description"
}}]
```
- Output: Generates bounding boxes for visual inspection of the i-th image

2. **Path Tracer**
- Purpose: Plots movement or connections between points
- Input format: JSON
```json
[{{
    "index": i, # Image index
    "start_point_2d": [x1, y1],
    "end_point_2d": [x2, y2],
    "label": "trace_description"
}}]
```
- Output: Generates visual paths for verification of the i-th image

### Required Output Format:
For each reasoning step, you must structure your response as follows:
<think> [Your detailed reasoning process] </think> Action: [Object Mapper/Path Tracer]
```json
[JSON format coordinates]
```

After your reasoning and iteratively refine your solution through visualization feedback, you should arrive at a final answer and structure your response as follows:
<think> [Your detailed reasoning process] </think> Action: Answer
<answer> [Your final answer] </answer>

### Please NOTE the following reasoning techniques:
1. Initial Analysis
   - Break down the spatial problem
   - Plan your approach

2. Iterative Reasoning for Each Step
   - Choose appropriate tool
   - Provide absolute coordinates in JSON format (The top-left corner of the image is (0, 0) and the bottom-right corner is ({width}, {height}))
   - Observe the visualization output
   - Reflect on the visualization:
     * Is the placement/path accurate?
     * Does it align with your reasoning?
     * What adjustments are needed?
   - Backtrack and Adjust:
     * If errors found, backtrack to previous step to modify actions or decisions as needed"""
#  (You can only edit ONE image at each step)
PROMPT_TEMPLATE = """
### Question:
{question}

Begin your reasoning. After each tool use, critically evaluate the visualization and adjust if needed:
"""

BSZ=1                      # 50 reduce it if GPU OOM
MAX_IMAGES=45
SUBIMAGE_PATTERN = r".*\#\#\#\[([\d\.]+),\s*([\d\.]+),\s*([\d\.]+),\s*([\d\.]+)\]"
TYPE_TEMPLATE = {
    # ----------- same as cold start -----------
    "multiple choice":  '\nAnswer with the option\'s letter from the given choices directly.',  #  " Please provide only the single option letter (e.g., A, B, C, D, etc.) within the <answer> </answer> tags.",
    "free-form": '', # " Please provide your text answer within the <answer> </answer> tags.",
    "regression":  '\nPlease answer the question using a single word or phrase (e.g., 42 or 3.14).', # " Please provide the numerical value (e.g., 42 or 3.14) within the <answer> </answer> tags."
    "numerical": '\nPlease answer the question using a single word or phrase (e.g., 42 or 3.14).',      # same as regression  
    "vci": "",                                                                                          # for spar-bench
}

from dataclasses import dataclass
@dataclass
class ProcessData:
    index: int
    response: str
    mm_data: Dict
    bbox_list_origin: Dict
    movement_list_origin: Dict
    finish_reason: str
    is_finished: bool
    grid_size: int

def calculate_grid_centers(image_size=616, grid_size=5):
    """for maze data"""

    # matplotlib default margins
    margin_left = int(image_size * 0.125)    
    margin_right = int(image_size * 0.1)     
    margin_bottom = int(image_size * 0.11)   
    margin_top = int(image_size * 0.12)      
    
    usable_width = image_size - (margin_left + margin_right)
    usable_height = image_size - (margin_top + margin_bottom)
    
    cell_width = usable_width / grid_size
    cell_height = usable_height / grid_size
    
    centers = []
    for i in range(grid_size):
        for j in range(grid_size):
            # 计算中心坐标,考虑不同的边距
            center_x = margin_left + cell_width/2 + j * cell_width
            center_y = margin_top + cell_height/2 + i * cell_height
            centers.append((center_x, center_y))
    # print("calculate_grid_centers:", image_size, margin_left, margin_right, cell_width, cell_height)
    return centers, (cell_width+cell_height)/2  # 返回x和y方向的格子大小

def check_path_tracer(movement_list, centers, cell_size):
    for movement in movement_list:
        for key in ['start_point_2d', 'end_point_2d']:
            x, y = int(movement[key][0]), int(movement[key][1])
            min_distance = min([np.sqrt((x-c[0])**2 + (y-c[1])**2) for c in centers])
            if min_distance > cell_size/2:
                # print(key, movement[key], min_distance, cell_size/2, centers)
                return False
    return True

def check_repetition(allindex, bbox_list_origin, movement_list_origin):
    for cnt, tmp_index in enumerate(allindex):
        for bbox_list in list(bbox_list_origin.values()):
            for bbox in bbox_list:
                if bbox in allindex[tmp_index]["bbox_list"]:
                    return True
        for movement_list in list(movement_list_origin.values()):
            for movement in movement_list:
                if movement in allindex[tmp_index]["movement_list"]:
                    return True
    return False

def process_single_response(data: ProcessData):
    """处理单个响应的函数"""
    if data.is_finished is True:
        return {
            'index': data.index,
            'response': data.response,
            'finish_reason': data.finish_reason,
            'is_finished': data.is_finished,
            'processed_image_idx': [None],
        }
    try:
        # 解析和绘图
        bbox_list_new, movement_list_new = parse_bbox_and_movement(data.response)
        current_image_index = len(data.mm_data['image'])
        image_index_list, image_list = [], []
        bbox_list, movement_list = data.bbox_list_origin, data.movement_list_origin
        finish_reason = None

        try:
            allindex = {}
            for tmp_bbox_list in bbox_list_new:
                tmp_bbox_list = copy.deepcopy(tmp_bbox_list)
                if tmp_bbox_list["index"] in allindex:
                    if "bbox_list" in allindex[tmp_bbox_list["index"]]:
                        allindex[tmp_bbox_list["index"]]["bbox_list"].append(tmp_bbox_list)
                    else:
                        allindex[tmp_bbox_list["index"]]["bbox_list"] = [tmp_bbox_list]
                else:
                    allindex[tmp_bbox_list["index"]] = {'bbox_list': [tmp_bbox_list], 'movement_list': []}
            for tmp_movement_list in movement_list_new:
                tmp_movement_list = copy.deepcopy(tmp_movement_list)
                if tmp_movement_list["index"] in allindex:
                    if "movement_list" in allindex[tmp_movement_list["index"]]:
                        allindex[tmp_movement_list["index"]]["movement_list"].append(tmp_movement_list)
                    else:
                        allindex[tmp_movement_list["index"]]["movement_list"] = [tmp_movement_list]
                else:
                    allindex[tmp_movement_list["index"]] = {'bbox_list': [], 'movement_list': [tmp_movement_list]}
        except Exception as e:
            traceback.print_exc()
            print("bbox_list_new, movement_list_new: ", bbox_list_new, movement_list_new)
            finish_reason = "ToolGenError"

        if len(allindex) == 0:
            finish_reason = "ToolError"
        elif len(data.mm_data['image']) >= MAX_IMAGES+1:
            finish_reason = "TooManyImages"


        if finish_reason is not None:
            return {
                'index': data.index,
                'processed_image_idx': [None],
                'image': [data.mm_data['image'][0].copy()],
                'response': data.response,
                'finish_reason': finish_reason,
                'bbox_list': bbox_list,
                'movement_list': movement_list,
                'is_finished': True,
            }
        for cnt, tmp_index in enumerate(allindex):
            bbox_list_new, movement_list_new = allindex[tmp_index]["bbox_list"], allindex[tmp_index]["movement_list"]
            image_index_new = current_image_index + cnt
            image_index, bbox_list, movement_list = merge_bbox_movement(
                bbox_list_origin=data.bbox_list_origin,
                movement_list_origin=data.movement_list_origin,
                bbox_list_new=bbox_list_new,
                movement_list_new=movement_list_new,
                image_index_new=image_index_new,
            )
            image_index_list.append(image_index)
            if image_index == -1:
                return {
                    'index': data.index,
                    'processed_image_idx': [None],
                    'image': [data.mm_data['image'][0].copy()],
                    'response': data.response,
                    'finish_reason': "ToolError",
                    'bbox_list': bbox_list,
                    'movement_list': movement_list,
                    'is_finished': True,
                }
            image = data.mm_data['image'][image_index].copy()
            assert isinstance(image, Image.Image)
            input_width, input_height = image.size

            # draw bbox and lines
            plot_bounding_boxes(image, bbox_list[image_index_new], input_height=input_height, input_width=input_width)
            plot_movement(image, movement_list[image_index_new], input_height=input_height, input_width=input_width)
            
            image_list.append(image)

        return {
            'index': data.index,
            'processed_image_idx': image_index_list,
            'image': image_list,
            'response': data.response,
            'finish_reason': data.finish_reason,
            'bbox_list': bbox_list,
            'movement_list': movement_list,
            'is_finished': data.is_finished
        }
    except Exception as e:
        print(f"Error processing response {data.index}: {str(e)}")
        traceback.print_exc()
        return None


def save_samples_info(samples_info, save_dir):

    def get_unique_dir(base_path, prefix='generation'):
        """generate unique dirctory"""
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        counter = 0
        while True:
            if counter == 0:
                dir_name = f"{prefix}_{timestamp}"
            else:
                dir_name = f"{prefix}_{timestamp}_{counter}"

            full_path = os.path.join(base_path, dir_name)
            if not os.path.exists(full_path):
                return full_path
            counter += 1

    all_sample_dir = []
    for idx, sample in enumerate(samples_info):
        if 'qid' in sample:
            sample_dir = os.path.join(save_dir, sample['qid'])
        elif 'id' in sample:
            sample_dir = os.path.join(save_dir, sample['id'])
        elif 'index' in sample:
            sample_dir = os.path.join(save_dir, sample['index'])
        else:
            sample_dir = get_unique_dir(save_dir, f'sample')
        os.makedirs(sample_dir, exist_ok=True)
        all_sample_dir.append(sample_dir)

        text_data = {
            'prompt': sample['prompt'],
            'sequence': sample['sequence'],
            'response': sample['response'],
            'finish_reason': sample['finish_reason'],
            'execution_pass': sample['execution_pass']
        }
        
        with open(os.path.join(sample_dir, 'text_data.json'), 'w', encoding='utf-8') as f:
            json.dump(text_data, f, indent=2, ensure_ascii=False)
        
        # save images
        if 'multi_modal_data' in sample and 'image' in sample['multi_modal_data']:
            images_dir = os.path.join(sample_dir, 'images')
            os.makedirs(images_dir, exist_ok=True)
            
            for img_idx, img in enumerate(sample['multi_modal_data']['image']):
                if isinstance(img, Image.Image):
                    img_path = os.path.join(images_dir, f'image_{img_idx}.png')
                    img.save(img_path)
    return all_sample_dir


def get_qwen_chat(model, processor, texts, images, sampling_params={}):
    responses = []
    for text, image in zip(texts, images):
        inputs = processor(text=[text], images=[image], padding=True, return_tensors='pt').to(model.device)

        with torch.inference_mode():
            generated_ids = model.generate(
                **inputs,
                pad_token_id=processor.tokenizer.eos_token_id,
                temperature=sampling_params.temperature,
                top_p=sampling_params.top_p,
                max_new_tokens=sampling_params.max_tokens,
            )
        
        generated_ids_trimmed = [
            out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
        ]
        response = processor.batch_decode(
            generated_ids_trimmed, 
            skip_special_tokens=True, 
            clean_up_tokenization_spaces=False,
        )[0]
        responses.append(response)
    return responses

def multi_turn_generate(inference_engine, processor, tokenizer, vllm_inputs=None, sampling_params=None, prompt_token_ids=None,  use_tqdm=False, save_dir=None, max_num_steps=10):
    def _get_prompts_and_indices(samples_info):
        prompts, multi_modal_data, indices=[], [], []
        for index, info in enumerate(samples_info):
            if not info['stop'] and len(info['multi_modal_data']['image']) <= MAX_IMAGES:
                prompts.append(info['sequence'])
                multi_modal_data.append(info['multi_modal_data'])
                indices.append(info['index'])
        return prompts, multi_modal_data, indices

    sampling_params=copy.deepcopy(sampling_params)
    new_vllm_inputs = []
    for single_vllm_input in vllm_inputs:
        prompt = tokenizer.decode(single_vllm_input['prompt_token_ids'], skip_special_tokens=False)
        new_vllm_inputs.extend([{
            "id": single_vllm_input['id'],
            "prompt": prompt,
            "multi_modal_data": single_vllm_input['multi_modal_data'],
            "grid_size": single_vllm_input['grid_size'],
        }   for _ in range(sampling_params.n)])
        
    sampling_params.n=1
    sampling_params.detokenize=True             # True convert ids to text
    samples_info = []
    for index, item in enumerate(new_vllm_inputs):
        
        processed_image = [fetch_image({'image': origin_image}) for origin_image in item['multi_modal_data']['image']]
        sample_info = {
            "id": item["id"],
            "prompt": item["prompt"],
            "sequence": item["prompt"],
            "multi_modal_data": {"image": processed_image},
            "response": "",
            "stop": False,
            "finish_reason": None,
            "processed_image_idx": [],
            "index": index,
            "mask_info": [],
            "execution_pass": 0,
            "bbox_list": {img_idx: [] for img_idx in range(len(processed_image))},        # save bbox_list for each image index 
            "movement_list": {img_idx: [] for img_idx in range(len(processed_image))},     # save movement_list for each image index
            "grid_size": item['grid_size'],
        }
        samples_info.append(sample_info)
    intermediate_prompt = 'The index of the given image is {current_image_idx} (width: {width}, height: {height}). Continue your reasoning. After each tool use, critically evaluate the visualization and adjust if needed:'
    final_prompt = 'The index of the given image is {current_image_idx} (width: {width}, height: {height}). Then, you can not invoke the Object Mapper or Path Tracer tool. Please answer the initial question and structure your response as required:'
    intermediate_template = """<|im_end|>
<|im_start|>user
{pad}
{prompt}
<|im_end|>
<|im_start|>assistant
"""                 # connect multi-turn conversations
    num_llm_calls_available = max_num_steps - 1
    while num_llm_calls_available >= 0:
        num_llm_calls_available-=1
        input_prompts, multi_modal_data, indices=_get_prompts_and_indices(samples_info)  # _get_prompts_and_indices
        # print("input_prompts:", len(input_prompts), input_prompts[0])
        # print("multi_modal_data:", len(multi_modal_data), multi_modal_data[0])
        # print("indices:", len(indices), indices[0])
        # print("num_llm_calls_available:", num_llm_calls_available)
        if type(inference_engine) == LLM:
            input_prompts = [{
                'prompt_token_ids': tokenizer.encode(prompt, add_special_tokens=False)[:],
                'multi_modal_data': mm_data                             # {'image', list()}
                } for prompt, mm_data in zip(input_prompts, multi_modal_data)]
            outputs = inference_engine.generate(prompts=input_prompts, sampling_params=sampling_params, use_tqdm=use_tqdm)
        else:
            responses = get_qwen_chat(inference_engine, processor, input_prompts, [m['image'] for m in multi_modal_data], sampling_params)
            class A:
                pass
            outputs = []
            for request_id, response in enumerate(responses):
                a = A()
                a.outputs = [A()]
                a.outputs[0].text = response
                a.outputs[0].finish_reason = "stop"
                a.outputs[0].stop_reason = None
                a.request_id = request_id
                outputs.append(a)

        sorted_outputs = sorted(outputs, key=lambda output: int(output.request_id))
        responses=[x.outputs[0].text for x in sorted_outputs]
        finish_reason=[x.outputs[0].finish_reason for x in sorted_outputs]  # "stop", "length"
        stop_reason=[x.outputs[0].stop_reason for x in sorted_outputs]      # None: have EOS
        if num_llm_calls_available==-1:
            for i ,index in enumerate(indices):
                samples_info[index]['response']+=responses[i]
                samples_info[index]['sequence']+=responses[i]
                samples_info[index]['stop']=True
                samples_info[index]['finish_reason']=finish_reason[i]
            break

        def _is_finished(finish_reason, stop_reason, response):
            if finish_reason=='stop' and stop_reason==None and "<answer>" in response and "</answer>" in response: 
                return True
            if finish_reason=='length':
                return True
            if finish_reason=='rule':
                return True
            return False
        
        # breakpoint()
        is_finished=[_is_finished(finish_reason[i], stop_reason[i], responses[i]) for i in range(len(finish_reason))]
        # check if all samples are finished
        if all([x for x in is_finished]): 
            for i ,index in enumerate(indices):
                samples_info[index]['response']+=responses[i]
                samples_info[index]['sequence']+=responses[i]
                samples_info[index]['stop']=True
                samples_info[index]['finish_reason']=finish_reason[i]
            break
        
        # ----------- Parallel Process -----------
        # Prepare Data
        process_data_list = [
            ProcessData(
                index=index,
                response=responses[i],
                mm_data=samples_info[index]['multi_modal_data'],
                bbox_list_origin=samples_info[index]["bbox_list"],
                movement_list_origin=samples_info[index]["movement_list"],
                finish_reason=finish_reason[i],
                is_finished=is_finished[i],                      # if is_finished == True, stop reasoning
                grid_size=samples_info[index]['grid_size'],
            )  for i, index in enumerate(indices)] 
        with ThreadPoolExecutor(max_workers=max(min(len(indices), os.cpu_count()//2, 64), 1) ) as executor:
            results = list(executor.map(process_single_response, process_data_list))

        # update samples_info
        for result in results:
            if result is not None:
                index = result['index']
                samples_info[index]['response'] += result['response']
                samples_info[index]['stop'] = result['is_finished']
                samples_info[index]['finish_reason'] = result['finish_reason']
                samples_info[index]['processed_image_idx'].extend(result['processed_image_idx'])
                if result['is_finished'] is False:
                    current_image_count = len(samples_info[index]['multi_modal_data']['image'])  
                    if len(result["image"]) > 1:        # 处理多图片情况
                        current_image_idx = current_image_count + 1
                        pad_prompt = ""
                        for tmp_image_idx, tmp_image in enumerate(result["image"]):
                            width, height = fetch_image({"image": tmp_image}).size
                            if current_image_count + tmp_image_idx + 1>=MAX_IMAGES:
                                pad_prompt += f"<|vision_start|><|image_pad|><|vision_end|>" + final_prompt.format(
                                    current_image_idx=current_image_idx + tmp_image_idx,
                                    width=width,
                                    height=height,
                                )
                                samples_info[index]['multi_modal_data']['image'].append(tmp_image)
                                break
                            else:
                                if tmp_image_idx <= len(result["image"]) - 2:
                                    pad_prompt += f"<|vision_start|><|image_pad|><|vision_end|>The index of the given image is {current_image_idx+tmp_image_idx} (width: {width}, height: {height}).\n"
                                else:
                                    if num_llm_calls_available > 0:
                                        pad_prompt += f"<|vision_start|><|image_pad|><|vision_end|>" + intermediate_prompt.format(
                                            current_image_idx=current_image_idx + tmp_image_idx,
                                            width=width,
                                            height=height,
                                        )
                                    else:
                                        pad_prompt += f"<|vision_start|><|image_pad|><|vision_end|>" + final_prompt.format(
                                            current_image_idx=current_image_idx + tmp_image_idx,
                                            width=width,
                                            height=height,
                                        )
                                samples_info[index]['multi_modal_data']['image'].append(tmp_image)
                        
                        samples_info[index]['sequence'] += result['response'] + intermediate_template.format(prompt="", pad=pad_prompt)  
                    else:
                        current_image_idx = current_image_count + 1
                        width, height = fetch_image({"image": result["image"][0]}).size
                        if current_image_count + 1>= MAX_IMAGES:
                            # Maximum limit reached, switching to final_prompt
                            prompt = final_prompt.format(
                                current_image_idx=current_image_idx,
                                width=width,
                                height=height,
                            )
                        else:
                            
                            prompt = (intermediate_prompt if num_llm_calls_available > 0 else final_prompt).format(
                                current_image_idx=current_image_idx,
                                width=width,
                                height=height,
                            )
                        
                        samples_info[index]['sequence'] += result['response'] + intermediate_template.format(
                            prompt=prompt,
                            pad="<|vision_start|><|image_pad|><|vision_end|>"
                        )
                        samples_info[index]['multi_modal_data']['image'].append(result['image'][0])
                    
                    # 更新其他信息
                    samples_info[index]['bbox_list'] = result['bbox_list']
                    samples_info[index]["movement_list"] = result['movement_list']
                else:
                    samples_info[index]['sequence'] += result['response']

    for i, line in enumerate(samples_info):
        if samples_info[i]['finish_reason']!='length': 
            samples_info[i]['sequence']+=tokenizer.eos_token                # add end of sentence

    batch_sequences = [sample['sequence'] for sample in samples_info]
    if save_dir:
        all_sample_dir = save_samples_info(samples_info, save_dir)
        return batch_sequences, all_sample_dir
    return batch_sequences


def parse_dialog(serialized_content):
    # segement dialogue
    segments = re.split(r'<\|im_start\|>|<\|im_end\|>', serialized_content)
    segments = [s for s in segments if s]  
    
    conversations = []
    current_role = None
    current_content = []
    
    system_content = None
    if segments[0].startswith('system'):
        system_content = segments[0].replace('system\n\n', '', 1)  # only replace the first time
        segments = segments[1:]
    
    if system_content:
        conversations.append({
            "role": "system",
            "content": system_content
        })

    for segment in segments:
        if segment.startswith('user'):
            has_vision = '<|vision_start|><|image_pad|><|vision_end|>' in segment
            text = segment.replace('user\n', '', 1)  # only replace the first time
            # text = text.replace('<|vision_start|><|image_pad|><|vision_end|>\n', '', 1)               # keep <|vision_start|><|image_pad|><|vision_end|>
            
            content = []
            if has_vision:
                content.append({
                    "type": "image",
                    "image": "image_path",
                    "nframes": "args.max_frames",
                    "max_pixels": args.max_pixels
                })
            content.append({
                "type": "text",
                "text": text
            })
            
            conversations.append({
                "role": "user",
                "content": content
            })
        elif segment.startswith('assistant'):
            text = segment.replace('assistant\n', '', 1)  # only replace the first time
            conversations.append({
                "role": "assistant",
                "content": text
            })
    
    return conversations


def setup_distributed():
    """Setup distributed training environment for SLURM"""
    if "SLURM_PROCID" in os.environ:
        rank = int(os.environ["SLURM_PROCID"])
        world_size = int(os.environ["SLURM_NTASKS"])
        local_rank = int(os.environ["SLURM_LOCALID"])
        master_addr = os.environ.get("MASTER_ADDR", "127.0.0.1")
        master_port = os.environ.get("MASTER_PORT", "29500")
    else:
        rank = int(os.environ.get("RANK", 0))
        world_size = int(os.environ.get("WORLD_SIZE", 1))
        local_rank = int(os.environ.get("LOCAL_RANK", 0))
        master_addr = os.environ.get("MASTER_ADDR", "127.0.0.1")
        master_port = os.environ.get("MASTER_PORT", "29500")

    os.environ["MASTER_ADDR"] = master_addr
    os.environ["MASTER_PORT"] = master_port
    
    dist.init_process_group("nccl", rank=rank, world_size=world_size)
    torch.cuda.set_device(local_rank)
    return rank, world_size, local_rank


def eval_model(args):
    # rank, world_size, local_rank = setup_distributed()
    
    # print(f"RANK and WORLD_SIZE: {rank}/{world_size}, local rank: {local_rank}")
    # hostname = socket.gethostname()
    # print(f"Process {rank} is running on node {hostname}")
    # Model
    model_path = args.model_path
    model_name = args.model_name

    print(f"prune: {args.prune}")
    print(f"resize: {args.resize}")
    print(f"Loading from {model_path}")
    print("torch.cuda.device_count():", torch.cuda.device_count())
    if args.prune and 'vscan' in args.prune.lower():
        from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import (
            Qwen2_5_VisionTransformerPretrainedModel,
            Qwen2_5_VLVisionBlock,
            Qwen2_5_VLVisionSdpaAttention,
            Qwen2_5_VLVisionFlashAttention2,
            Qwen2_5_VisionPatchEmbed,
            Qwen2_5_VLModel
        )
        sys.path.append('/mnt/beegfs/dzhu6/SelfEvolvingAgent/VScan/')
        from qwen.model.qwen2_5_vl_custom import (
            Qwen2_5_VLForConditionalGeneration_X,
            Qwen2_5_VisionTransformerPretrainedModel_X,
            Qwen2_5_VLVisionBlock_X,
            Qwen2_5_VLVisionSdpaAttention_X,
            Qwen2_5_VLVisionFlashAttention2_X,
            Qwen2_5_VisionPatchEmbed_X,
            Qwen2_5_VLModel_X
        )
        Qwen2_5_VLForConditionalGeneration.forward = Qwen2_5_VLForConditionalGeneration_X.forward
        Qwen2_5_VisionTransformerPretrainedModel.forward = Qwen2_5_VisionTransformerPretrainedModel_X.forward
        Qwen2_5_VLVisionBlock.forward = Qwen2_5_VLVisionBlock_X.forward
        Qwen2_5_VLVisionSdpaAttention.forward = Qwen2_5_VLVisionSdpaAttention_X.forward
        Qwen2_5_VLVisionFlashAttention2.forward = Qwen2_5_VLVisionFlashAttention2_X.forward
        Qwen2_5_VisionPatchEmbed.forward = Qwen2_5_VisionPatchEmbed_X.forward
        Qwen2_5_VLModel.forward = Qwen2_5_VLModel_X.forward
        Qwen2_5_VLModel.layer_prune = Qwen2_5_VLModel_X.layer_prune
        print("Qwen2_5_VLForConditionalGeneration.forward:", Qwen2_5_VLForConditionalGeneration.forward)
        llm = Qwen2_5_VLForConditionalGeneration.from_pretrained(
            args.model_path,
            torch_dtype=torch.bfloat16,
            device_map=f"auto",
            attn_implementation="flash_attention_2",
            trust_remote_code=True
        ).eval()
        llm.model.layer_list = [14]
        llm.model.image_token_ratio_list = [0.333]
        llm.image_token_ratio = 0.167
        min_pixels = 4*28*28
        max_pixels = 1280*28*28
    else:
        # llm = Qwen2_5_VLForConditionalGeneration.from_pretrained(
        #     args.model_path,
        #     torch_dtype=torch.bfloat16,
        #     device_map=f"auto",
        #     attn_implementation="flash_attention_2",
        #     trust_remote_code=True
        # ).eval()
        tensor_parallel_size = int(os.environ.get("SLURM_NTASKS", "1"))
        pipeline_parallel_size = int(os.environ.get("VLLM_PIPELINE_PARALLEL_SIZE", "1"))

        print(f"Loading from {model_path}, tensor_parallel_size: {tensor_parallel_size}, pipeline_parallel_size: {pipeline_parallel_size}")
        llm = LLM(
            trust_remote_code=True,
            model=model_path,
            dtype="bfloat16",
            tensor_parallel_size=tensor_parallel_size,
            pipeline_parallel_size=pipeline_parallel_size,
            limit_mm_per_prompt={"image": 62, "video": 10},
            gpu_memory_utilization=0.85,                   # default 0.9
            enable_prefix_caching=True                     # cache
        )

    processor = AutoProcessor.from_pretrained(model_path)
    sampling_params = SamplingParams(
        temperature=args.temperature,
        top_p=args.top_p,
        max_tokens=16384,
        stop_token_ids=[],
    )
    processor = AutoProcessor.from_pretrained(model_path)
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    tokenizer.padding_side = "left"
    processor.tokenizer = tokenizer
    '''
    file_path = args.input_file
    if file_path.endswith('.jsonl'):
        with open(file_path, 'r', encoding='utf-8') as f:
            data = [json.loads(line) for line in f]
    else:
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
    '''
    if 'blink' in args.dataset.lower():
        sys.path.append('/mnt/beegfs/dzhu6/VisualSketchpad/')
        from infer2 import get_blink_dataset
        data = get_blink_dataset()
    elif 'spatialeval' in args.input_file.lower():
        data = load_dataset("MilaWang/SpatialEval", "vqa", split="test")
    elif '3dsr' in args.dataset.lower():
        dp = "/mnt/beegfs/dzhu6/correlation/3dsrbench_v1_vlmevalkit_circular.tsv"
        with open(dp, 'r') as f:
            reader = csv.DictReader(f, delimiter='\t')
            data = list(reader)

    st, ed = (len(data)*args.split)//args.all, (len(data)*(args.split+1))//args.all
    print(f"{len(data)} lines found, generating from {st} to {ed}")
    print("Data: ", len(data))
    if type(data) is list:
        data = data[st:ed]
    else:
        data = data.select(range(st, ed))
    print("Data: ", len(data))
    batches = []
    messages = []
    ids = []
    def get_key(x):
        return x['id'] if 'id' in x else x['qid'] if 'qid' in x else x['index']

    start_idx = 0
    old = set()
    if args.all > 1:
        # 分split
        output_dir = os.path.join(args.output_dir, f"split_{args.split}_all_{args.all}")     # args.output_dir 
    else:
        output_dir = args.output_dir 
    save_dir = output_dir
    if args.over_write:
        os.system(f"rm -rf {output_dir} && mkdir {output_dir}")
    else:
        if not os.path.exists(output_dir):
            os.system(f"mkdir {output_dir}")

    print("Output Dir: ", output_dir)
    output_file_path = f"{output_dir}/results.jsonl"
    if os.path.exists(output_file_path):
        mode = "a"
        with jsonlines.open(output_file_path) as fin:
            for line in fin:
                old.add(get_key(line))
            #    start_idx += 1
        # start_idx = len(set([x['id'] if 'id' in x else x['qid'] for x in jsonlines.open(output_file_path)]))
    else:
        mode = "w"
    print("loaded ", len(old), " lines from ", output_file_path)

    for xidx, x in enumerate(data):
        if get_key(x) in old:
            continue
        # if 'spatialeval' in args.dataset.lower() and 'real' not in get_key(x):
        #     continue
        ids.append(get_key(x))
        batches.append(x.copy())
        batches[-1].pop("image", None)
        # if xidx == 0:
        #     print(batches[-1])
        '''
        {
            "dataset": "SpatialEval",
            "task": "spatialreal",
            "split": "test",
            "question_id": "spatialreal.vqa.sa_1543979.0",
            "question": "Please answer the following question based on the image. How many individual cartons of strawberries are there in the wooden basket? Available options:\nA. eight\nB. ten\nC. four\nD. six\n",
            "answer": "D",
            "image_path": [
                "SpatialEval/spatialreal.vqa.sa_1543979.0.png"
            ],
            "data_type": "image",
            "problem_type": "multiple choice"
        },
        '''
        if args.dataset in ["vsi_bench"]:
            prompt = f"These are frames from a video, numbered from 1 to {args.max_frames} in sequence. That is, the index of each image is 1, 2, 3, ..., {args.max_frames}.\n\nAnswer the quesntion with appropriate tools:\n" + x['question'] # + '\n\nThe final answer should be a single word or phrase.'
            if x['problem_type'] == 'multiple choice' and 'options' in x:
                prompt = prompt + '\n' +  '\n'.join(x['options'])
            prompt = prompt + TYPE_TEMPLATE[x['problem_type'].lower()]
            width, height = fetch_image({"image": os.path.join(args.image_folder, x["image_path"][0]), "max_pixels": args.max_pixels}).size
            image_messages = []
            for image_idx, image_path in enumerate(x["image_path"]):
                image_messages.extend([
                    {
                        "type": "image",
                        "image": os.path.join(args.image_folder, image_path),
                        "nframes": args.max_frames,
                        "max_pixels": args.max_pixels
                    },
                    {
                        "type": "text",
                        "text": f"The index of the given image is {image_idx+1} (width: {width}, height: {height}).\n",
                    }
                ])
            image_messages.append({
                "type": "text",
                "text": PROMPT_TEMPLATE.format(question=prompt)
            })
            msg = [
                {
                    "role": "system",
                    "content": SYSTEM_PROMPT.format(width=width, height=height)
                },
                {
                    "role": "user",
                    "content": image_messages,
                }
            ]
        elif "3dsrbench" in args.dataset.lower():
            x['index'] = x['qid']
            question = x['question']
            question += f"\nA. {x['A']}"
            question += f"\nB. {x['B']}"
            if "C" in x and x['C'] is not None and x['C'].strip() != "":
                question += f"\nC. {x['C']}"
            if "D" in x and x['D'] is not None and x['D'].strip() != "":
                question += f"\nD. {x['D']}"
            question += "\n"
            prompt = question

            if "flip" in x['index'].lower():
                if "flip" in x['index'].lower():
                    ext = x['image_url'].split('.')[-1]
                    flip = x['image_url'].replace("http://images.cocodataset.org/", "/mnt/beegfs/dzhu6/coco_images/").replace("." + ext, "_flip." + ext)
                    if not os.path.exists(flip):
                        image = Image.open(x['image_url'].replace("http://images.cocodataset.org/", "/mnt/beegfs/dzhu6/coco_images/")).convert("RGB")
                        image = image.transpose(method=Image.FLIP_LEFT_RIGHT)
                        image.save(flip)
                    x['image_url'] = x['image_url'].replace('.' + ext, "_flip." + ext)

            x["problem_type"] = "multiple choice"
            x['image_path'] = [x['image_url'].replace("http://images.cocodataset.org/", "")]
            prompt = prompt + '\nThe index of the given image is 1.' + TYPE_TEMPLATE[x['problem_type'].lower()]

            width, height = fetch_image({"image": os.path.join(args.image_folder, x["image_path"][0]), "max_pixels": args.max_pixels}).size
            if args.resize:
                width, height = width//2, height//2
            msg = [
                {
                    "role": "system",
                    "content": SYSTEM_PROMPT.format(width=width, height=height)
                },
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "image",
                            "image": os.path.join(args.image_folder, x["image_path"][0]),
                            "nframes": args.max_frames,
                            "grid_size": x["grid_size"] if "grid_size" in x else None,
                            "max_pixels": args.max_pixels
                        },
                        {
                            "type": "text",
                            "text": PROMPT_TEMPLATE.format(question=prompt)
                        }
                ]
            }]
        elif "blink" in args.dataset.lower():
            image_inputs, _ = process_vision_info(x['messages'])
            if image_inputs:
                width, height = image_inputs[0].size
                if args.resize:
                    width, height = width//2, height//2
                msg = [
                {
                        "role": "system",
                        "content": SYSTEM_PROMPT.format(width=width, height=height)
                    },
                ]
            else:
                msg = [
                {
                        "role": "system",
                        "content": SYSTEM_PROMPT.format(width=0, height=0)
                    },
                ]
            msg += x['messages']
        elif 'spatialeval' in args.dataset.lower():  # 3dsrbench_v1
            x["problem_type"] = "multiple choice"
            x['image_path'] = [x['id'] + '.png']
            prompt =  x["text"]
            prompt = prompt + '\nThe index of the given image is 1.' + TYPE_TEMPLATE[x['problem_type'].lower()]
            width, height = fetch_image({"image": os.path.join(args.image_folder, x["image_path"][0]), "max_pixels": args.max_pixels}).size
            if args.resize:
                width, height = width//2, height//2
            msg = [
                {
                    "role": "system",
                    "content": SYSTEM_PROMPT.format(width=width, height=height)
                },
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "image",
                            "image": os.path.join(args.image_folder, x["image_path"][0]),
                            "nframes": args.max_frames,
                            "grid_size": x["grid_size"] if "grid_size" in x else None,
                            "max_pixels": args.max_pixels
                        },
                        {
                            "type": "text",
                            "text": PROMPT_TEMPLATE.format(question=prompt)
                        }
                ]
            }]
        elif args.dataset in ["maze", "SpatialEval_spatialreal",]:
            prompt =  x["question"]
            if x['problem_type'] == 'multiple choice' and 'options' in x:
                prompt = prompt + '\n' +  '\n'.join(x['options'])
            prompt =  prompt + '\nThe index of the given image is 1.' + TYPE_TEMPLATE[x['problem_type'].lower()]
            width, height = fetch_image({"image": os.path.join(args.image_folder, x["image_path"][0]), "max_pixels": args.max_pixels}).size
            msg = [
                {
                    "role": "system",
                    "content": SYSTEM_PROMPT.format(width=width, height=height)
                },
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "image",
                            "image": os.path.join(args.image_folder, x["image_path"][0]),
                            "nframes": args.max_frames,
                            "grid_size": x["grid_size"] if "grid_size" in x else None,
                            "max_pixels": args.max_pixels
                        },
                        {
                            "type": "text",
                            "text": PROMPT_TEMPLATE.format(question=prompt)
                        }
                ]
            }]
        elif args.dataset in ["spar_bench", "spar_bench_tiny", "mmsi_bench"]:
            prompt = x["question"]
            if x['problem_type'] == 'multiple choice' and x.get('options', None) is not None:
                prompt = prompt + '\n' + '\n'.join(x['options'])
            prompt = prompt.replace("Your answer can only include one of options A, B, C or D.", "")
            prompt = prompt.replace("Answer using a single number and nothing else.", "")
            
            post_prompt = ""
            if x.get('original_question_type', None) in ['position_matching', "camera_motion_infer"]:
                post_prompt = "The values represent the bounding box coordinates normalized to a 0-1000 scale, with the top-left corner as the origin of the image."
            prompt = prompt + "\n" + post_prompt 

            if x['data_type'] == 'single_view':
                prompt =  prompt + '\nThe index of the given image is 1.' + TYPE_TEMPLATE[x['problem_type'].lower()]
                width, height = fetch_image({"image": os.path.join(args.image_folder, x["image_path"][0]), "max_pixels": args.max_pixels}).size
                msg = [
                    {
                        "role": "system",
                        "content": SYSTEM_PROMPT.format(width=width, height=height)
                    },
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "image",
                                "image": os.path.join(args.image_folder, x["image_path"][0]),
                                "max_pixels": args.max_pixels
                            },
                            {
                                "type": "text",
                                "text": PROMPT_TEMPLATE.format(question=prompt) 
                            }
                        ]
                    }
                ]
            elif x['data_type'] == 'multi_view':  # multi_view
                n_frames = len(x["image_path"])  
                width, height = fetch_image({"image": os.path.join(args.image_folder, x["image_path"][0]), "max_pixels": args.max_pixels}).size
                image_messages = []
                for image_idx, image_path in enumerate(x["image_path"]):
                    image_messages.extend([
                        {
                            "type": "image",
                            "image": os.path.join(args.image_folder, image_path),
                            "max_pixels": args.max_pixels
                        },
                        {
                            "type": "text",
                            "text": f"The index of the given image is {image_idx+1} (width: {width}, height: {height}).\n"
                        }
                    ])
                prompt = prompt + TYPE_TEMPLATE[x['problem_type'].lower()]
                image_messages.append({
                    "type": "text",
                    "text": PROMPT_TEMPLATE.format(question=prompt)
                })
                
                msg = [
                    {
                        "role": "system",
                        "content": SYSTEM_PROMPT.format(width=width, height=height)
                    },
                    {
                        "role": "user",
                        "content": image_messages
                    }
                ]
        else:
            raise Exception(f"UNKNON args.dataset: {args.dataset}")
        messages.append(msg)
    print("messages: ", len(messages))
    print("batches: ", len(batches))


    with open(output_file_path, mode, encoding="utf-8") as fout:
        print("Message Example:", messages[0])
        print(f"Start from the {start_idx} example")
        for i in tqdm(range(start_idx, len(messages), BSZ), desc="Processing batches"):
            batch_messages = messages[i:i + BSZ]
            prompts = [processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True) for msg in batch_messages]
            image_num = []
            for msg in batch_messages:
                current_image_num = 0
                for turn in msg:
                    if isinstance(turn["content"], list):
                        for turn_content in turn["content"]:
                            if turn_content["type"] == "image":
                                current_image_num += 1
                if args.dataset in ["vsi_bench"]:
                    assert current_image_num == args.max_frames, f"wrong image number: {current_image_num} != {args.max_frames}"
                elif args.dataset in ["maze", "SpatialEval_spatialreal", "SpatialEval"]:
                    assert current_image_num == 1, f"wrong image number: {current_image_num}"
                image_num.append(current_image_num)
            image_inputs, video_inputs, video_kwargs = process_vision_info(batch_messages, return_video_kwargs=True)
            image_idx = 0
            video_idx = 0
            llm_inputs = []
            for idx, (prompt, msg) in enumerate(zip(prompts, batch_messages)):
                mm_type = batch_messages[idx][1]['content'][0]['type']
                sample_mm_data = {}
                sample_video_kw = {}
                if mm_type == 'image':
                    sample_mm_data["image"] = []
                    for current_idx in range(image_num[idx]):
                        width, height = image_inputs[image_idx].size
                        image = image_inputs[image_idx]
                        if args.resize:
                            image = image.resize((width//2, height//2), resample=Image.Resampling.LANCZOS)
                        if args.dataset in ["video", "vsi_bench"]:
                            sample_mm_data["image"].append(image)
                        else:
                            sample_mm_data["image"].append(image)
                        image_idx += 1
                elif mm_type == 'video':
                    sample_mm_data["video"] = [video_inputs[video_idx]]
                    for key, value in video_kwargs.items():
                        sample_video_kw[key] = value[video_idx]
                    video_idx += 1
                llm_inputs.append({
                    'id': ids[i + idx],
                    "prompt": prompt,
                    "prompt_token_ids": tokenizer.encode(prompt, add_special_tokens=False),
                    "multi_modal_data": sample_mm_data,
                    "mm_processor_kwargs": sample_video_kw,
                    "grid_size": msg[1]["content"][0]["grid_size"] if args.dataset == 'maze' else None
                })
            if image_inputs is not None:
                assert image_idx == len(image_inputs), f"Image index mismatch: {image_idx} != {len(image_inputs)}"
            if video_inputs is not None:
                assert video_idx == len(video_inputs), f"Video index mismatch: {video_idx} != {len(video_inputs)}"

            try:
                if i < 1e9:
                    batch_sequences = multi_turn_generate(llm, processor, tokenizer, vllm_inputs=llm_inputs, sampling_params=sampling_params, save_dir=save_dir,
                                max_num_steps=20 if args.dataset=="maze" else 10)
                    batch_sequences, all_sample_dir = batch_sequences
                else:
                    batch_sequences = multi_turn_generate(llm, processor, tokenizer, vllm_inputs=llm_inputs, sampling_params=sampling_params, save_dir=None,
                                max_num_steps=20 if args.dataset=="maze" else 10)
                    all_sample_dir = [None] * len(batch_sequences)
                batch_conversations = [parse_dialog(sequence) for sequence in batch_sequences]
                print(f"Processed batch {(i)//BSZ + 1}/{(len(messages) + BSZ - 1)//BSZ}. ")
            except Exception as e:
                print(f"Error processing batch starting at index {i}: {e}")
                continue

            for input_example, model_output, sample_dir in zip(batches[i:i + BSZ], batch_conversations, all_sample_dir):
                result = input_example.copy()
                result['conversations'] = model_output
                result['model_output'] = model_output[-1]['content']
                result['model_id'] = model_name
                result['sample_dir'] = sample_dir

                fout.write(
                    json.dumps(result)
                    + "\n"
                )
                fout.flush()
                if 'spatialeval' in args.dataset.lower():
                    with jsonlines.open(f"/mnt/beegfs/dzhu6/SpatialEval/outputs/vqa/spatialreal/m-{model_name}_bare_prune_{args.prune}{'_resize_True' if args.resize else ''}_split_{args.split}_all_{args.all}.jsonl", "a") as writer:
                        result = input_example.copy()
                        result['answer'] = model_output[-1]['content']
                        writer.write(result)
                elif '3dsr' in args.dataset.lower():
                    with jsonlines.open(f"/mnt/beegfs/dzhu6/correlation/results/{model_name}_prune_{args.prune}{'_resize_True' if args.resize else ''}_split_{args.split}_all_{args.all}.jsonl", "a") as writer:
                        result = input_example.copy()
                        result['response'] = model_output[-1]['content']
                        writer.write(result)
                elif 'blink' in args.dataset.lower():
                    with jsonlines.open(f"/mnt/beegfs/dzhu6/VisualSketchpad/{model_name}_traj_False_prune_{args.prune}{'_resize_True' if args.resize else ''}_maxnewtokens_16384_split_{args.split}_all_{args.all}.jsonl", "a") as writer:
                        result = input_example.copy()
                        result.pop('messages', None)
                        result['response'] = model_output[-1]['content']
                        writer.write(result)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model-path", type=str, required=True)
    parser.add_argument("--model-name", type=str, required=True)
    parser.add_argument("--model-base", type=str, default=None)
    parser.add_argument("--dataset", type=str, required=True, help="")
    parser.add_argument("--image-folder", type=str, default="")
    parser.add_argument("--input-file", type=str, required=True, help="Path to the question file")
    parser.add_argument("--output-dir", type=str, default="./result")
    parser.add_argument("--temperature", type=float, default=0.75)
    parser.add_argument("--top_p", type=float, default=0.9)
    parser.add_argument("--num_beams", type=int, default=1)
    parser.add_argument("--max-frames", type=int, default=32)
    parser.add_argument("--max-pixels", type=int, default=256*28*28)
    parser.add_argument("--over_write", type=int, default=0, help="Whether to overwrite the output directory")
    parser.add_argument("--split", type=int, default=1)
    parser.add_argument("--all", type=int, default=1)
    parser.add_argument("--prune", type=str, default=None)
    parser.add_argument("--resize", action="store_true", default=False)
    args = parser.parse_args()
    if args.image_folder == "None":
        args.image_folder = ""
    eval_model(args)