File size: 87,306 Bytes
bb62af4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
from .modeling_deepseekv2 import DeepseekV2Model, DeepseekV2ForCausalLM
from .configuration_deepseek_v2 import DeepseekV2Config
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
from typing import List, Optional, Tuple, Union
from transformers.cache_utils import Cache
import requests
from PIL import Image, ImageOps, ImageDraw, ImageFont
from io import BytesIO
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import CrossEntropyLoss
from torchvision import transforms
# from torchvision.transforms.functional import InterpolationMode
import os
from .deepencoderv2 import build_sam_vit_b, build_qwen2_decoder_as_encoder, MlpProjector
from addict import Dict
from transformers import TextStreamer
from .conversation import get_conv_template
from abc import ABC
import math
import re
from tqdm import tqdm
import numpy as np
# import time

def scanflow_debug_tensor(name, x):
    if x is None:
        print(f"[scanflow debug] {name}: None", flush=True)
        return

    try:
        t = x.detach()
        finite = torch.isfinite(t) if torch.is_floating_point(t) else torch.ones_like(t, dtype=torch.bool)
        finite_count = int(finite.sum().item())
        total = t.numel()

        msg = (
            f"[scanflow debug] {name}: "
            f"shape={tuple(t.shape)} dtype={t.dtype} device={t.device} "
            f"finite={finite_count}/{total}"
        )

        if torch.is_floating_point(t) and finite_count > 0:
            tf = t[finite]
            msg += (
                f" min={tf.min().item():.6g}"
                f" max={tf.max().item():.6g}"
                f" mean={tf.float().mean().item():.6g}"
            )

        print(msg, flush=True)
    except Exception as exc:
        print(f"[scanflow debug] {name}: failed to summarize: {exc}", flush=True)


def load_image(image_path):

    try:
        image = Image.open(image_path)
        
        corrected_image = ImageOps.exif_transpose(image)
        
        return corrected_image
        
    except Exception as e:
        print(f"error: {e}")
        try:
            return Image.open(image_path)
        except:
            return None


def re_match(text):
    pattern = r'(<\|ref\|>(.*?)<\|/ref\|><\|det\|>(.*?)<\|/det\|>)'
    matches = re.findall(pattern, text, re.DOTALL)

    # pattern1 = r'<\|ref\|>.*?<\|/ref\|>\n'
    # new_text1 = re.sub(pattern1, '', text, flags=re.DOTALL)

    mathes_image = []
    mathes_other = []
    for a_match in matches:
        if '<|ref|>image<|/ref|>' in a_match[0]:
            mathes_image.append(a_match[0])
        else:
            mathes_other.append(a_match[0])
    return matches, mathes_image, mathes_other


def extract_coordinates_and_label(ref_text, image_width, image_height):

    try:
        label_type = ref_text[1]
        cor_list = eval(ref_text[2])
    except Exception as e:
        print(e)
        return None

    return (label_type, cor_list)


def draw_bounding_boxes(image, refs, ouput_path):

    image_width, image_height = image.size
    
    img_draw = image.copy()
    draw = ImageDraw.Draw(img_draw)

    overlay = Image.new('RGBA', img_draw.size, (0, 0, 0, 0))
    draw2 = ImageDraw.Draw(overlay)
    
    # try:
    # except IOError:
    #     try:
    #         font = ImageFont.truetype("DejaVuSans.ttf", 20) 
    #     except IOError:
    font = ImageFont.load_default()

    img_idx = 0
    
    for i, ref in enumerate(refs):
        try:
            result = extract_coordinates_and_label(ref, image_width, image_height)
            if result:
                label_type, points_list = result
                
                color = (np.random.randint(0, 200), np.random.randint(0, 200), np.random.randint(0, 255))

                color_a = color + (20, )
                for points in points_list:
                    x1, y1, x2, y2 = points

                    x1 = int(x1 / 999 * image_width)
                    y1 = int(y1 / 999 * image_height)

                    x2 = int(x2 / 999 * image_width)
                    y2 = int(y2 / 999 * image_height)

                    if label_type == 'image':
                        try:
                            cropped = image.crop((x1, y1, x2, y2))
                            cropped.save(f"{ouput_path}/images/{img_idx}.jpg")
                        except Exception as e:
                            print(e)
                            pass
                        img_idx += 1
                        
                    try:
                        if label_type == 'title':
                            draw.rectangle([x1, y1, x2, y2], outline=color, width=4)
                            draw2.rectangle([x1, y1, x2, y2], fill=color_a, outline=(0, 0, 0, 0), width=1)
                        else:
                            draw.rectangle([x1, y1, x2, y2], outline=color, width=2)
                            draw2.rectangle([x1, y1, x2, y2], fill=color_a, outline=(0, 0, 0, 0), width=1)
                        text_x = x1
                        text_y = max(0, y1 - 15)
                            
                        
                        text_bbox = draw.textbbox((0, 0), label_type, font=font)
                        text_width = text_bbox[2] - text_bbox[0]
                        text_height = text_bbox[3] - text_bbox[1]
                        draw.rectangle([text_x, text_y, text_x + text_width, text_y + text_height], 
                                    fill=(255, 255, 255, 30))
                        
                        draw.text((text_x, text_y), label_type, font=font, fill=color)
                    except:
                        pass
        except:
            continue
    img_draw.paste(overlay, (0, 0), overlay)
    return img_draw


def process_image_with_refs(image, ref_texts, output_path):

    result_image = draw_bounding_boxes(image, ref_texts, output_path)
    
    return result_image





def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
    best_ratio_diff = float('inf')
    best_ratio = (1, 1)
    area = width * height
    for ratio in target_ratios:
        target_aspect_ratio = ratio[0] / ratio[1]
        ratio_diff = abs(aspect_ratio - target_aspect_ratio)
        if ratio_diff < best_ratio_diff:
            best_ratio_diff = ratio_diff
            best_ratio = ratio
        elif ratio_diff == best_ratio_diff:
            if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
                best_ratio = ratio
    # print(f'width: {width}, height: {height}, best_ratio: {best_ratio}')
    return best_ratio


def dynamic_preprocess(image, min_num=2, max_num=6, image_size=768, use_thumbnail=False):
    orig_width, orig_height = image.size
    aspect_ratio = orig_width / orig_height

    # calculate the existing image aspect ratio
    target_ratios = set(
        (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
        i * j <= max_num and i * j >= min_num)
    # print(target_ratios)
    target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])

    # find the closest aspect ratio to the target
    target_aspect_ratio = find_closest_aspect_ratio(
        aspect_ratio, target_ratios, orig_width, orig_height, image_size)

    # print(target_aspect_ratio)
    # calculate the target width and height
    target_width = image_size * target_aspect_ratio[0]
    target_height = image_size * target_aspect_ratio[1]
    blocks = target_aspect_ratio[0] * target_aspect_ratio[1]

    # resize the image
    resized_img = image.resize((target_width, target_height))
    processed_images = []
    for i in range(blocks):
        box = (
            (i % (target_width // image_size)) * image_size,
            (i // (target_width // image_size)) * image_size,
            ((i % (target_width // image_size)) + 1) * image_size,
            ((i // (target_width // image_size)) + 1) * image_size
        )
        # split the image
        split_img = resized_img.crop(box)
        processed_images.append(split_img)
    assert len(processed_images) == blocks
    if use_thumbnail and len(processed_images) != 1:
        thumbnail_img = image.resize((image_size, image_size))
        processed_images.append(thumbnail_img)
    return processed_images, target_aspect_ratio



def normalize_transform(mean, std):
    if mean is None and std is None:
        transform = None
    elif mean is None and std is not None:
        mean = [0.] * len(std)
        transform = transforms.Normalize(mean=mean, std=std)
    elif mean is not None and std is None:
        std = [1.] * len(mean)
        transform = transforms.Normalize(mean=mean, std=std)
    else:
        transform = transforms.Normalize(mean=mean, std=std)

    return transform



def format_messages(
        conversations: List[Dict[str, str]],
        sft_format: str = "deepseek",
        system_prompt: str = "",
):
    """
    Applies the SFT template to conversation.

    Args:
        conversations (List[Dict]): A List of messages.
        sft_format (str, optional): The format of the SFT template to use. Defaults to "deepseek".
        system_prompt (str, optional): The system prompt to use in the SFT template. Defaults to "".

    Returns:
        sft_prompt (str): The formatted text.
    """

    conv = get_conv_template(sft_format)
    conv.set_system_message(system_prompt)
    for message in conversations:
        conv.append_message(message["role"], message["content"].strip())
    sft_prompt = conv.get_prompt().strip()

    return sft_prompt


def text_encode(tokenizer, text: str, bos: bool = True, eos: bool = False):
    t = tokenizer.encode(text, add_special_tokens=False)
    bos_id = 0
    eos_id = 1
    if bos:
        t = [bos_id] + t
    if eos:
        t = t + [eos_id]

    return t

def load_pil_images(conversations: List[Dict[str, str]]) -> List[Image.Image]:
    """

    Args:
        conversations (List[Dict[str, str]]): the conversations with a list of messages. An example is :
            [
                {
                    "role": "User",
                    "content": "<image_placeholder>\nExtract all information from this image and convert them into markdown format.",
                    "images": ["./examples/table_datasets.png"]
                },
                {"role": "Assistant", "content": ""},
            ]

    Returns:
        pil_images (List[PIL.Image.Image]): the list of PIL images.

    """

    pil_images = []

    for message in conversations:
        if "images" not in message:
            continue

        for image_path in message["images"]:
            # print('----------------')
            # print(image_path)
            # print('----------------')
            # exit()
            
            # pil_img = Image.open(image_path)
            pil_img = load_image(image_path)
            pil_img = pil_img.convert("RGB")
            pil_images.append(pil_img)

    return pil_images


class BaseTransform(ABC):

    def set_rng(self, *args, **kwargs):
        pass

    def __call__(self, *args, **kwargs) -> torch.Tensor:
        pass

    @property
    def default_shape(self):
        raise NotImplementedError


class BasicImageTransform(BaseTransform):
    def __init__(
        self, 
        mean: Optional[Tuple[float, float, float]] = (0.5, 0.5, 0.5),
        std: Optional[Tuple[float, float, float]] = (0.5, 0.5, 0.5),
        normalize: bool = True
    ):
        self.mean = mean
        self.std = std
    
        transform_pipelines = [
            transforms.ToTensor()
        ]

        normalize = normalize_transform(mean, std) if normalize else nn.Identity()
        if normalize is not None:
            transform_pipelines.append(normalize)

        self.transform = transforms.Compose(transform_pipelines)
    
    def __call__(self, x):
        x = self.transform(x)
        return x

class NoEOSTextStreamer(TextStreamer):
    def on_finalized_text(self, text: str, stream_end: bool = False):

        eos_text = self.tokenizer.decode([self.tokenizer.eos_token_id], skip_special_tokens=False)
        text = text.replace(eos_text, "\n")
        print(text, flush=True, end="")


class DeepseekOCR2Config(DeepseekV2Config):
    model_type = "DeepseekOCR2"


def scanpath_fixation_loss(
    fixation_logits: torch.Tensor,
    fixation_targets: torch.Tensor,
    fixation_mask: torch.Tensor,
) -> torch.Tensor:
    """
    Computes soft-label cross entropy over visual patch fixation distributions.

    fixation_logits:  [B, T, N]
    fixation_targets: [B, T_max, N]
    fixation_mask:    [B, T_max]
    """
    if fixation_logits.size(-1) != fixation_targets.size(-1):
        raise ValueError(
            f"Fixation token mismatch: logits have {fixation_logits.size(-1)} tokens, "
            f"targets have {fixation_targets.size(-1)} tokens."
        )

    steps = fixation_logits.size(1)
    fixation_targets = fixation_targets[:, :steps, :].to(
        device=fixation_logits.device,
        dtype=torch.float32,
    )
    fixation_mask = fixation_mask[:, :steps].to(
        device=fixation_logits.device,
        dtype=torch.float32,
    )

    log_probs = F.log_softmax(fixation_logits.float(), dim=-1)
    per_step_loss = -(fixation_targets * log_probs).sum(dim=-1)

    return (per_step_loss * fixation_mask).sum() / fixation_mask.sum().clamp_min(1.0)




def scanpath_resize_distribution_to_target_tokens(
    distribution: torch.Tensor,
    target_tokens: int,
    eps: float = 1e-8,
) -> torch.Tensor:
    """
    Resize a flattened spatial distribution to the target token count.

    distribution: [B, T, N]
    target_tokens: usually 256 for the SalChartQA 16x16 global fixation grid.

    If N already matches target_tokens, this returns the normalized input. If both
    N and target_tokens are square grids, it bilinearly resamples the distribution
    spatially and renormalizes. This keeps attention supervision compatible with
    144-token and 256-token visual grids without changing the original model path.
    """
    distribution = distribution.float()
    distribution = distribution / distribution.sum(dim=-1, keepdim=True).clamp_min(eps)

    source_tokens = distribution.size(-1)
    if source_tokens == target_tokens:
        return distribution

    source_side = int(math.sqrt(source_tokens))
    target_side = int(math.sqrt(target_tokens))
    if source_side * source_side != source_tokens or target_side * target_side != target_tokens:
        raise ValueError(
            f"Cannot resize attention distribution from {source_tokens} to {target_tokens} tokens; "
            "both token counts must be square grids."
        )

    batch_size, steps, _ = distribution.shape
    distribution_2d = distribution.reshape(batch_size * steps, 1, source_side, source_side)
    resized = F.interpolate(
        distribution_2d,
        size=(target_side, target_side),
        mode="bilinear",
        align_corners=False,
    )
    resized = resized.reshape(batch_size, steps, target_tokens).clamp_min(0.0)
    return resized / resized.sum(dim=-1, keepdim=True).clamp_min(eps)


def scanpath_attention_alignment_loss(
    visual_attn_maps: torch.Tensor,
    fixation_targets: torch.Tensor,
    fixation_mask: torch.Tensor,
    eps: float = 1e-8,
) -> torch.Tensor:
    """
    Align the four supervised middle recurrent-Qwen layers with human fixations.

    visual_attn_maps: [B, L_sel, T, H, N]
        L_sel = 4 selected middle Qwen layers (10,11,12,13 for a 24-layer model).
        H = all query attention heads (14 in the current Qwen configuration).

    Supervision is intentionally applied in two stages:
      1. Within EACH selected layer, average over ALL attention heads:
             p_l,t = mean_h A_l,t,h
      2. Compute the fixation soft cross-entropy independently for each layer,
         then average the four layer losses.

    This means every selected middle layer must itself exhibit temporally aligned
    visual attention; averaging layers before the loss cannot let one layer hide
    another layer's mismatch.
    """
    if visual_attn_maps is None:
        raise ValueError("visual_attn_maps is None.")
    if visual_attn_maps.dim() != 5:
        raise ValueError(
            f"Expected visual_attn_maps to have shape [B, L_sel, T, H, N], "
            f"got {tuple(visual_attn_maps.shape)}."
        )
    if visual_attn_maps.size(1) != 4:
        raise ValueError(
            "Middle-four attention supervision expects exactly 4 retained layers, "
            f"got {visual_attn_maps.size(1)}."
        )

    # Mean across ALL heads independently within each selected layer.
    # [B,4,T,H,N] -> [B,4,T,N]
    pred_by_layer = visual_attn_maps.float().mean(dim=3)
    pred_by_layer = pred_by_layer / pred_by_layer.sum(
        dim=-1, keepdim=True
    ).clamp_min(eps)

    steps = pred_by_layer.size(2)
    targets = fixation_targets[:, :steps, :].to(
        device=pred_by_layer.device,
        dtype=torch.float32,
    )
    mask = fixation_mask[:, :steps].to(
        device=pred_by_layer.device,
        dtype=torch.float32,
    )
    targets = targets / targets.sum(dim=-1, keepdim=True).clamp_min(eps)

    layer_losses = []
    for layer_slot in range(pred_by_layer.size(1)):
        pred = pred_by_layer[:, layer_slot]  # [B,T,N]

        if pred.size(-1) != targets.size(-1):
            pred = scanpath_resize_distribution_to_target_tokens(
                pred,
                target_tokens=targets.size(-1),
                eps=eps,
            )

        per_step_loss = -(
            targets * pred.clamp_min(eps).log()
        ).sum(dim=-1)
        layer_loss = (
            (per_step_loss * mask).sum()
            / mask.sum().clamp_min(1.0)
        )
        layer_losses.append(layer_loss)

    # Equal supervision strength for each of the four middle layers.
    return torch.stack(layer_losses).mean()


def scanpath_attention_entropy_metrics(
    visual_attn_maps: torch.Tensor,
    fixation_targets: torch.Tensor,
    fixation_mask: torch.Tensor,
    eps: float = 1e-12,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """
    Diagnostics matching the middle-four/all-head attention objective.

    Prediction entropy and excess cross entropy are computed independently for
    each supervised layer after averaging all heads, then averaged across the
    four layers. Target entropy is shared by all layers.
    """
    if visual_attn_maps is None:
        raise ValueError("visual_attn_maps is None.")
    if visual_attn_maps.dim() != 5:
        raise ValueError(
            f"Expected visual_attn_maps to have shape [B, L_sel, T, H, N], "
            f"got {tuple(visual_attn_maps.shape)}."
        )
    if visual_attn_maps.size(1) != 4:
        raise ValueError(
            "Middle-four attention diagnostics expect exactly 4 retained layers, "
            f"got {visual_attn_maps.size(1)}."
        )

    pred_by_layer = visual_attn_maps.float().mean(dim=3)  # [B,4,T,N]
    pred_by_layer = pred_by_layer / pred_by_layer.sum(
        dim=-1, keepdim=True
    ).clamp_min(eps)

    steps = pred_by_layer.size(2)
    targets = fixation_targets[:, :steps, :].to(
        device=pred_by_layer.device,
        dtype=torch.float32,
    )
    mask = fixation_mask[:, :steps].to(
        device=pred_by_layer.device,
        dtype=torch.float32,
    )
    targets = targets / targets.sum(dim=-1, keepdim=True).clamp_min(eps)

    target_entropy_per_step = -(
        targets * targets.clamp_min(eps).log()
    ).sum(dim=-1)
    normalizer = mask.sum().clamp_min(1.0)
    target_entropy = (
        target_entropy_per_step * mask
    ).sum() / normalizer

    layer_prediction_entropies = []
    layer_excess_cross_entropies = []

    for layer_slot in range(pred_by_layer.size(1)):
        pred = pred_by_layer[:, layer_slot]

        if pred.size(-1) != targets.size(-1):
            pred = scanpath_resize_distribution_to_target_tokens(
                pred,
                target_tokens=targets.size(-1),
                eps=eps,
            )

        log_pred = pred.clamp_min(eps).log()
        pred_entropy_per_step = -(pred * log_pred).sum(dim=-1)
        cross_entropy_per_step = -(targets * log_pred).sum(dim=-1)
        excess_ce_per_step = (
            cross_entropy_per_step - target_entropy_per_step
        ).clamp_min(0.0)

        layer_prediction_entropies.append(
            (pred_entropy_per_step * mask).sum() / normalizer
        )
        layer_excess_cross_entropies.append(
            (excess_ce_per_step * mask).sum() / normalizer
        )

    prediction_entropy = torch.stack(layer_prediction_entropies).mean()
    excess_cross_entropy = torch.stack(layer_excess_cross_entropies).mean()
    return target_entropy, prediction_entropy, excess_cross_entropy

def scanpath_fixation_entropy_metrics(
    fixation_logits: torch.Tensor,
    fixation_targets: torch.Tensor,
    fixation_mask: torch.Tensor,
    eps: float = 1e-12,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """
    Computes masked mean entropy diagnostics for ScanFlow fixation prediction.

    Returns:
        target_entropy:
            H(y) = -sum_i y_i log y_i. This is the theoretical minimum
            soft-label cross entropy for the supplied target distributions.

        prediction_entropy:
            H(p) = -sum_i p_i log p_i. Lower values indicate sharper model
            predictions; higher values indicate broader or more uniform maps.

        excess_cross_entropy:
            CE(y, p) - H(y) = KL(y || p). This is the portion of the fixation
            cross entropy caused by prediction-target mismatch rather than the
            irreducible entropy of the Gaussian target itself.

    Shapes:
        fixation_logits:  [B, T, N]
        fixation_targets: [B, T_max, N]
        fixation_mask:    [B, T_max]
    """
    if fixation_logits.size(-1) != fixation_targets.size(-1):
        raise ValueError(
            f"Entropy token mismatch: logits have {fixation_logits.size(-1)} tokens, "
            f"targets have {fixation_targets.size(-1)} tokens."
        )

    steps = fixation_logits.size(1)
    targets = fixation_targets[:, :steps, :].to(
        device=fixation_logits.device,
        dtype=torch.float32,
    )
    mask = fixation_mask[:, :steps].to(
        device=fixation_logits.device,
        dtype=torch.float32,
    )

    # Normalize defensively in case preprocessing introduces small numerical
    # deviations from an exact row sum of one.
    targets = targets / targets.sum(dim=-1, keepdim=True).clamp_min(eps)

    log_probs = F.log_softmax(fixation_logits.float(), dim=-1)
    probs = log_probs.exp()

    per_step_target_entropy = -(
        targets * targets.clamp_min(eps).log()
    ).sum(dim=-1)

    per_step_prediction_entropy = -(
        probs * log_probs
    ).sum(dim=-1)

    per_step_cross_entropy = -(
        targets * log_probs
    ).sum(dim=-1)

    per_step_excess_cross_entropy = (
        per_step_cross_entropy - per_step_target_entropy
    ).clamp_min(0.0)

    normalizer = mask.sum().clamp_min(1.0)

    target_entropy = (
        per_step_target_entropy * mask
    ).sum() / normalizer

    prediction_entropy = (
        per_step_prediction_entropy * mask
    ).sum() / normalizer

    excess_cross_entropy = (
        per_step_excess_cross_entropy * mask
    ).sum() / normalizer

    return target_entropy, prediction_entropy, excess_cross_entropy


def scanpath_diversity_loss(
    fixation_logits: torch.Tensor,
    fixation_targets: torch.Tensor,
    fixation_mask: torch.Tensor,
    eps: float = 1e-8,
) -> torch.Tensor:
    """
    Target-conditioned pairwise diversity loss.

    The loss penalizes predicted fixation distributions for being similar when
    the corresponding ground-truth fixation distributions are different.

    fixation_logits:  [B, T, N]
    fixation_targets: [B, T_max, N]
    fixation_mask:    [B, T_max]

    This is intentionally applied to predicted fixation distributions rather
    than raw hidden states so that diversity must affect the fixation output.
    """
    if fixation_logits.size(-1) != fixation_targets.size(-1):
        raise ValueError(
            f"Diversity token mismatch: logits have {fixation_logits.size(-1)} tokens, "
            f"targets have {fixation_targets.size(-1)} tokens."
        )

    steps = fixation_logits.size(1)
    if steps <= 1:
        return fixation_logits.sum() * 0.0

    predictions = F.softmax(fixation_logits.float(), dim=-1)
    targets = fixation_targets[:, :steps, :].to(
        device=fixation_logits.device,
        dtype=torch.float32,
    )
    mask = fixation_mask[:, :steps].to(
        device=fixation_logits.device,
        dtype=torch.float32,
    )

    pred_norm = F.normalize(predictions, p=2, dim=-1, eps=eps)
    target_norm = F.normalize(targets, p=2, dim=-1, eps=eps)

    pred_similarity = torch.matmul(pred_norm, pred_norm.transpose(1, 2))
    target_similarity = torch.matmul(target_norm, target_norm.transpose(1, 2))

    pair_mask = mask.unsqueeze(2) * mask.unsqueeze(1)
    diagonal = torch.eye(
        steps,
        device=fixation_logits.device,
        dtype=pair_mask.dtype,
    ).unsqueeze(0)
    pair_mask = pair_mask * (1.0 - diagonal)

    # Pairs with similar target maps receive little or no diversity penalty.
    target_difference = (1.0 - target_similarity).clamp_min(0.0).detach()
    weights = pair_mask * target_difference

    weighted_similarity = weights * pred_similarity
    normalizer = weights.sum().clamp_min(eps)
    return weighted_similarity.sum() / normalizer


def scanflow_loss_gradients(
    loss: Optional[torch.Tensor],
    parameters: list[torch.nn.Parameter],
) -> list[Optional[torch.Tensor]]:
    """Return gradients for one loss without modifying parameter.grad."""
    if loss is None or not torch.is_tensor(loss) or not loss.requires_grad:
        return [None] * len(parameters)
    return list(torch.autograd.grad(
        loss,
        parameters,
        retain_graph=True,
        create_graph=False,
        allow_unused=True,
    ))


def scanflow_gradient_norm(
    gradients: list[Optional[torch.Tensor]],
) -> torch.Tensor:
    total = None
    for grad in gradients:
        if grad is None:
            continue
        value = grad.detach().float().pow(2).sum()
        total = value if total is None else total + value
    return torch.tensor(0.0) if total is None else total.sqrt()


def scanflow_gradient_dot(
    gradients_a: list[Optional[torch.Tensor]],
    gradients_b: list[Optional[torch.Tensor]],
) -> torch.Tensor:
    total = None
    for grad_a, grad_b in zip(gradients_a, gradients_b):
        if grad_a is None or grad_b is None:
            continue
        value = (grad_a.detach().float() * grad_b.detach().float()).sum()
        total = value if total is None else total + value
    return torch.tensor(0.0) if total is None else total


def scanflow_gradient_cosine(
    gradients_a: list[Optional[torch.Tensor]],
    gradients_b: list[Optional[torch.Tensor]],
    eps: float = 1e-12,
) -> torch.Tensor:
    norm_a = scanflow_gradient_norm(gradients_a)
    norm_b = scanflow_gradient_norm(gradients_b)
    denominator = norm_a * norm_b
    if float(denominator.detach().cpu()) <= eps:
        return torch.tensor(float("nan"))
    return scanflow_gradient_dot(gradients_a, gradients_b) / denominator


def scanflow_weighted_combined_gradient_norm(
    gradient_groups: list[tuple[float, list[Optional[torch.Tensor]]]],
) -> torch.Tensor:
    if not gradient_groups:
        return torch.tensor(0.0)
    total_sq = None
    for parameter_index in range(len(gradient_groups[0][1])):
        combined = None
        for weight, gradients in gradient_groups:
            grad = gradients[parameter_index]
            if grad is None or weight == 0.0:
                continue
            term = grad.detach().float() * float(weight)
            combined = term if combined is None else combined + term
        if combined is None:
            continue
        value = combined.pow(2).sum()
        total_sq = value if total_sq is None else total_sq + value
    return torch.tensor(0.0) if total_sq is None else total_sq.sqrt()


def scanflow_is_main_process() -> bool:
    return not (
        torch.distributed.is_available()
        and torch.distributed.is_initialized()
        and torch.distributed.get_rank() != 0
    )


def scanflow_diagnostic_patterns() -> tuple[str, ...]:
    raw = os.getenv(
        "DEEPSEEK_OCR2_UPDATE_PARAM_FILTER",
        "qwen2_model.model.model,qwen2_model.model.text_cross_norms,"
        "qwen2_model.model.text_cross_attn_layers,qwen2_model.text_context_proj",
    )
    return tuple(part.strip() for part in raw.split(",") if part.strip())


class DeepseekOCR2Model(DeepseekV2Model):
    """DeepSeek-OCR2 outer model for dynamic ScanFlow Pro V2.

    The visual encoder preserves the recurrent Plan-1 generation of h_1...h_T,
    all against the immutable visual tokens V. After H is complete, V queries H
    once to produce R. A tokenwise reasoning-intensity gate lambda_i(V,Q) then
    produces [V + lambda(V,Q)*R; H]. Human scanpath
    supervision remains attached to the same Qwen self-attention h_t -> V maps
    on the GLOBAL image branch.
    """

    config_class = DeepseekOCR2Config

    def __init__(self, config: DeepseekV2Config):
        super(DeepseekOCR2Model, self).__init__(config)
        self.sam_model = build_sam_vit_b()
        self.qwen2_model = build_qwen2_decoder_as_encoder(max_scanpath_steps=32)
        n_embed = 1280
        self.projector = MlpProjector(
            Dict(projector_type="linear", input_dim=896, n_embed=n_embed)
        )
        embed_std = 1 / torch.sqrt(torch.tensor(n_embed, dtype=torch.float32))
        self.view_seperator = nn.Parameter(torch.randn(n_embed) * embed_std)

        # Compatibility attributes used by older training/evaluation scripts.
        # Recurrence and the V2 global residual are intrinsic to qwen2_model.
        self.enable_scanflow = True
        self.enable_text_query_residual = True
        self.last_visual_attn_maps = None
        self.last_visual_attn_layer_indices = tuple(
            getattr(self.qwen2_model, "attention_supervision_layer_indices", ())
        )

        # Reset is intentionally deferred until the first real forward pass.
        # Hugging Face/MS-Swift constructs the module first and loads checkpoint
        # tensors afterward; resetting inside __init__ would therefore be
        # overwritten by from_pretrained().
        self._scanflow_new_params_reset_done = False
        self._scanflow_v2_new_params_reset_done = False

    def _select_text_context(
        self,
        inputs_embeds: torch.Tensor,
        sample_idx: int,
        attention_mask: Optional[torch.Tensor] = None,
        labels: Optional[torch.Tensor] = None,
        images_seq_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        # The question context conditions the visual/recurrent encoder, but we do
        # not need gradients back into the outer LLM token embeddings through this
        # auxiliary branch.
        token_source = inputs_embeds[sample_idx].detach()
        mask = torch.ones(token_source.shape[0], dtype=torch.bool, device=token_source.device)
        if attention_mask is not None:
            mask = mask & attention_mask[sample_idx].bool()
        if images_seq_mask is not None:
            mask = mask & (~images_seq_mask[sample_idx].bool())
        if labels is not None:
            prompt_mask = mask & labels[sample_idx].eq(-100)
            if prompt_mask.sum() > 0:
                mask = prompt_mask
        if mask.sum() == 0:
            mask = torch.ones_like(mask)
        return token_source[mask].unsqueeze(0)

    def forward(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[List[torch.FloatTensor]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        images: Optional[torch.FloatTensor] = None,
        images_seq_mask: Optional[torch.FloatTensor] = None,
        images_spatial_crop: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        return_dict: Optional[bool] = None,
        scanpath_lengths: Optional[torch.Tensor] = None,
        scanpath_steps: Optional[int] = None,
    ) -> Union[Tuple, BaseModelOutputWithPast]:
        # Original recurrent Plan-1 reset mechanism. Keep this switch off when
        # starting V2 from an already-trained Plan-1 recurrent checkpoint.
        if (
            not self._scanflow_new_params_reset_done
            and os.getenv("SCANFLOW_RESET_NEW_PARAMS", "0").strip().lower()
            in {"1", "true", "yes", "on"}
        ):
            qwen2_model_for_reset = getattr(self, "qwen2_model", None)
            if qwen2_model_for_reset is None or not hasattr(
                qwen2_model_for_reset, "reset_scanflow_parameters"
            ):
                raise RuntimeError(
                    "SCANFLOW_RESET_NEW_PARAMS=1, but qwen2_model does not expose "
                    "reset_scanflow_parameters()."
                )
            qwen2_model_for_reset.reset_scanflow_parameters()
            self._scanflow_new_params_reset_done = True

        # Independent Version-2 reset mechanism. For the first transition from
        # a Plan-1 checkpoint, this initializes only R=CrossAttn(V,H,H) and its
        # question-conditioned tokenwise intensity head, leaving every trained
        # Plan-1 parameter untouched.
        if (
            not self._scanflow_v2_new_params_reset_done
            and os.getenv("SCANFLOW_V2_RESET_NEW_PARAMS", "0").strip().lower()
            in {"1", "true", "yes", "on"}
        ):
            qwen2_model_for_reset = getattr(self, "qwen2_model", None)
            if qwen2_model_for_reset is None or not hasattr(
                qwen2_model_for_reset, "reset_scanflow_v2_parameters"
            ):
                raise RuntimeError(
                    "SCANFLOW_V2_RESET_NEW_PARAMS=1, but qwen2_model does not "
                    "expose reset_scanflow_v2_parameters()."
                )
            qwen2_model_for_reset.reset_scanflow_v2_parameters()
            self._scanflow_v2_new_params_reset_done = True

        if inputs_embeds is None:
            inputs_embeds = self.get_input_embeddings()(input_ids)

        sam_model = getattr(self, "sam_model", None)
        qwen2_model = getattr(self, "qwen2_model", None)
        self.last_visual_attn_maps = None
        self.last_visual_attn_layer_indices = tuple(
            getattr(qwen2_model, "attention_supervision_layer_indices", ())
        )
        batch_visual_attn_maps = []

        if (
            sam_model is not None
            and qwen2_model is not None
            and (input_ids.shape[1] != 1 or self.training)
            and torch.sum(images[0][1]).item() != 0
        ):
            idx = 0
            for image, crop_shape in zip(images, images_spatial_crop):
                del crop_shape
                patches = image[0]
                image_ori = image[1]

                sample_scanpath_lengths = None
                if scanpath_lengths is not None:
                    sample_scanpath_lengths = scanpath_lengths[idx:idx + 1]

                # ScanFlow Pro V2 uses a genuinely dynamic number of causal states.
                # During training use the sample's true length; at inference, None
                # lets qwen2_model fall back to max_scanpath_steps=32.
                if scanpath_steps is not None:
                    sample_scanpath_steps = int(scanpath_steps)
                elif sample_scanpath_lengths is not None:
                    sample_scanpath_steps = int(sample_scanpath_lengths.max().item())
                else:
                    sample_scanpath_steps = None

                sample_text_context = self._select_text_context(
                    inputs_embeds,
                    idx,
                    attention_mask=attention_mask,
                    labels=labels,
                    images_seq_mask=images_seq_mask,
                )

                if torch.sum(patches).item() != 0:
                    with torch.no_grad():
                        local_features_1 = sam_model(patches)
                        global_features_1 = sam_model(image_ori)

                    local_text_context = sample_text_context.expand(
                        local_features_1.shape[0], -1, -1
                    )
                    global_text_context = sample_text_context.expand(
                        global_features_1.shape[0], -1, -1
                    )
                    local_scanpath_lengths = (
                        sample_scanpath_lengths.expand(local_features_1.shape[0])
                        if sample_scanpath_lengths is not None else None
                    )
                    global_scanpath_lengths = (
                        sample_scanpath_lengths.expand(global_features_1.shape[0])
                        if sample_scanpath_lengths is not None else None
                    )

                    # Every view returns [V + lambda(V,Q)*R; H]. The residual and
                    # tokenwise intensity are computed once after H; only the
                    # global branch retains Qwen attention maps for scanpath loss.
                    local_features_2 = qwen2_model(
                        local_features_1,
                        text_context=local_text_context,
                        scanpath_lengths=local_scanpath_lengths,
                        scanpath_steps=sample_scanpath_steps,
                        collect_visual_attention=False,
                    )
                    local_features = self.projector(local_features_2)

                    global_features_2 = qwen2_model(
                        global_features_1,
                        text_context=global_text_context,
                        scanpath_lengths=global_scanpath_lengths,
                        scanpath_steps=sample_scanpath_steps,
                        collect_visual_attention=getattr(
                            qwen2_model, "enable_attention_supervision", False
                        ),
                    )
                    global_visual_attn_maps = getattr(
                        qwen2_model, "last_visual_attn_maps", None
                    )
                    if global_visual_attn_maps is not None:
                        batch_visual_attn_maps.append(global_visual_attn_maps)
                    global_features = self.projector(global_features_2)

                    local_features = local_features.reshape(-1, local_features.size(-1))
                    global_features = global_features.reshape(-1, global_features.size(-1))
                    global_local_features = torch.cat(
                        [local_features, global_features, self.view_seperator[None, :]],
                        dim=0,
                    )
                else:
                    with torch.no_grad():
                        global_features_1 = sam_model(image_ori)
                    global_text_context = sample_text_context.expand(
                        global_features_1.shape[0], -1, -1
                    )
                    global_scanpath_lengths = (
                        sample_scanpath_lengths.expand(global_features_1.shape[0])
                        if sample_scanpath_lengths is not None else None
                    )
                    global_features_2 = qwen2_model(
                        global_features_1,
                        text_context=global_text_context,
                        scanpath_lengths=global_scanpath_lengths,
                        scanpath_steps=sample_scanpath_steps,
                        collect_visual_attention=getattr(
                            qwen2_model, "enable_attention_supervision", False
                        ),
                    )
                    global_visual_attn_maps = getattr(
                        qwen2_model, "last_visual_attn_maps", None
                    )
                    if global_visual_attn_maps is not None:
                        batch_visual_attn_maps.append(global_visual_attn_maps)
                    global_features = self.projector(global_features_2)
                    global_features = global_features.reshape(-1, global_features.size(-1))
                    global_local_features = torch.cat(
                        [global_features, self.view_seperator[None, :]], dim=0
                    )

                image_slot_mask = images_seq_mask[idx].bool().to(inputs_embeds.device)
                num_slots = int(image_slot_mask.sum().item())
                num_features = int(global_local_features.size(0))
                if num_slots != num_features:
                    raise ValueError(
                        "ScanFlow Pro V2 image-token count mismatch for sample "
                        f"{idx}: template reserved {num_slots} <image> positions, "
                        f"but the visual encoder produced {num_features} features. "
                        "The template must reserve (N_visual + T) slots per encoded "
                        "view plus the existing view-separator slot."
                    )

                inputs_embeds[idx].masked_scatter_(
                    image_slot_mask.unsqueeze(-1),
                    global_local_features.to(
                        device=inputs_embeds.device,
                        dtype=inputs_embeds.dtype,
                    ),
                )
                idx += 1

            self.last_visual_attn_maps = (
                torch.cat(batch_visual_attn_maps, dim=0)
                if batch_visual_attn_maps else None
            )

        return super(DeepseekOCR2Model, self).forward(
            input_ids=None,
            attention_mask=attention_mask,
            past_key_values=past_key_values,
            inputs_embeds=inputs_embeds,
            use_cache=use_cache,
            position_ids=position_ids,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )


class DeepseekOCR2ForCausalLM(DeepseekV2ForCausalLM):

    config_class = DeepseekOCR2Config
    # supports_gradient_checkpointing = True

    def __init__(self, config):
        super(DeepseekV2ForCausalLM, self).__init__(config)
        self.model = DeepseekOCR2Model(config)

        self.vocab_size = config.vocab_size

        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)

        # self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)

        # Initialize weights and apply final processing
        self.post_init()

        # Cross-forward snapshots reveal whether an optimizer step changed the
        # selected ScanFlow parameters. During gradient accumulation many
        # forwards will correctly report no change; a change should appear on
        # the first forward after optimizer.step().
        self._scanflow_parameter_snapshot = {}
        self._scanflow_snapshot_forward_step = 0
        self._scanflow_optimizer_membership_logged = False

    def get_model(self):
        return self.model


    def _scanflow_selected_named_parameters(self):
        patterns = scanflow_diagnostic_patterns()
        return [
            (name, parameter)
            for name, parameter in self.named_parameters()
            if parameter.requires_grad
            and any(pattern in name for pattern in patterns)
        ]

    def _scanflow_log_parameter_changes(self) -> None:
        if not self.training or not scanflow_is_main_process():
            return
        if os.getenv("DEEPSEEK_OCR2_LOG_PARAMETER_UPDATES", "1") != "1":
            return

        self._scanflow_snapshot_forward_step += 1
        interval = max(
            1, int(os.getenv("DEEPSEEK_OCR2_UPDATE_LOG_INTERVAL", "10"))
        )
        selected = self._scanflow_selected_named_parameters()

        # Compare current values with values saved at the end of the preceding
        # forward. A nonzero delta means an optimizer update occurred between
        # the two forwards. Zero deltas are expected during accumulation.
        if self._scanflow_parameter_snapshot and (
            self._scanflow_snapshot_forward_step % interval == 0
        ):
            changed_tensors = 0
            total_update_sq = 0.0
            total_parameter_sq = 0.0

            for name, parameter in selected:
                previous = self._scanflow_parameter_snapshot.get(name)
                if previous is None:
                    continue
                current = parameter.detach().float()
                delta = current - previous.to(device=current.device)
                update_norm = delta.norm().item()
                parameter_norm = current.norm().item()
                relative_update = update_norm / (parameter_norm + 1e-12)
                changed = update_norm > 0.0
                changed_tensors += int(changed)
                total_update_sq += update_norm * update_norm
                total_parameter_sq += parameter_norm * parameter_norm

                print(
                    "[scanflow parameter update] "
                    f"forward_step={self._scanflow_snapshot_forward_step} "
                    f"name={name} "
                    f"parameter_norm={parameter_norm:.8g} "
                    f"update_norm={update_norm:.8g} "
                    f"max_update={delta.abs().max().item():.8g} "
                    f"relative_update={relative_update:.8g} "
                    f"changed={changed} "
                    f"finite={bool(torch.isfinite(delta).all().item())}",
                    flush=True,
                )

            total_update_norm = total_update_sq ** 0.5
            total_parameter_norm = total_parameter_sq ** 0.5
            print(
                "[scanflow parameter update summary] "
                f"forward_step={self._scanflow_snapshot_forward_step} "
                f"selected_tensors={len(selected)} "
                f"changed_tensors={changed_tensors} "
                f"total_update_norm={total_update_norm:.8g} "
                f"total_parameter_norm={total_parameter_norm:.8g} "
                f"relative_update={total_update_norm / (total_parameter_norm + 1e-12):.8g}",
                flush=True,
            )

    def _scanflow_save_parameter_snapshot(self) -> None:
        if not self.training:
            return
        if os.getenv("DEEPSEEK_OCR2_LOG_PARAMETER_UPDATES", "1") != "1":
            return
        self._scanflow_parameter_snapshot = {
            name: parameter.detach().float().cpu().clone()
            for name, parameter in self._scanflow_selected_named_parameters()
        }


    def forward(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[List[torch.FloatTensor]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        images: Optional[torch.FloatTensor] = None,
        images_seq_mask: Optional[torch.FloatTensor] = None,
        images_spatial_crop: Optional[torch.FloatTensor] = None,
        scanpath_lengths: Optional[torch.Tensor] = None,
        scanpath_steps: Optional[int] = None,
        fixation_targets: Optional[torch.Tensor] = None,
        fixation_mask: Optional[torch.Tensor] = None,
        fixation_loss_weight: Optional[float] = None,
        diversity_loss_weight: Optional[float] = None,
        attention_loss_weight: Optional[float] = None,
        return_dict: Optional[bool] = None,
        
    ) -> Union[Tuple, CausalLMOutputWithPast]:
        output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
        output_hidden_states = (
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        self._scanflow_log_parameter_changes()

        requested_attention_weight = attention_loss_weight
        if requested_attention_weight is None:
            requested_attention_weight = float(
                os.getenv("DEEPSEEK_OCR2_ATTENTION_LOSS_WEIGHT", "0.0")
            )
        requested_attention_weight = float(requested_attention_weight)

        # Enable recurrent Qwen self-attention collection whenever the attention
        # objective or explicit attention-supervision switch is active.
        collect_attention_maps = (
            requested_attention_weight != 0.0
            or os.getenv("DEEPSEEK_OCR2_ENABLE_ATTENTION_SUPERVISION", "0") == "1"
        )
        qwen_for_attention = getattr(self.model, "qwen2_model", None)
        if collect_attention_maps and qwen_for_attention is not None and hasattr(
            qwen_for_attention, "enable_attention_supervision"
        ):
            qwen_for_attention.enable_attention_supervision = True

        outputs  = self.model(
            input_ids=input_ids,
            past_key_values=past_key_values,
            attention_mask=attention_mask,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            use_cache=use_cache,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            images=images,
            images_seq_mask = images_seq_mask,
            images_spatial_crop = images_spatial_crop,
            labels=labels,
            return_dict=return_dict,
            scanpath_lengths=scanpath_lengths,
            scanpath_steps=scanpath_steps,
            
        )


        
        # print(transformer_outputs)

        hidden_states = outputs[0]

        if not getattr(self, "_scanflow_printed_tensor_stats", False):
            scanflow_debug_tensor("hidden_states_before_lm_head", hidden_states)
            scanflow_debug_tensor("labels", labels)
            if labels is not None:
                valid_labels = labels[..., 1:] != -100
                print(
                    "[scanflow debug] valid shifted labels:",
                    int(valid_labels.sum().item()),
                    "/",
                    valid_labels.numel(),
                    flush=True,
                )

        logits = self.lm_head(hidden_states)
        logits = logits.float()

        if not getattr(self, "_scanflow_printed_tensor_stats", False):
            scanflow_debug_tensor("logits_after_lm_head", logits)

        # logits

        loss = None
        if labels is not None:
            # Shift so that tokens < n predict n
            shift_logits = logits[..., :-1, :].contiguous()
            shift_labels = labels[..., 1:].contiguous()
            # Flatten the tokens
            loss_fct = CrossEntropyLoss()
            shift_logits = shift_logits.view(-1, self.config.vocab_size)
            shift_labels = shift_labels.view(-1)
            # Enable model parallelism
            shift_labels = shift_labels.to(shift_logits.device)
            loss = loss_fct(shift_logits, shift_labels)

        lm_loss = loss
        # Recurrent Plan 1 deliberately supervises ONLY the Qwen self-attention
        # from each generated h_t to visual tokens. The old decoded-fixation and
        # fixation-diversity objectives are disabled by architecture, not merely
        # assigned small weights.
        fixation_loss = None
        diversity_loss = None
        attention_loss = None
        fixation_weight = 0.0
        diversity_weight = 0.0
        attention_weight = requested_attention_weight
        target_entropy = None
        prediction_entropy = None
        excess_fixation_ce = None
        attention_target_entropy = None
        attention_prediction_entropy = None
        attention_excess_ce = None

        # Keep these legacy keyword arguments accepted so existing ms-swift data
        # plumbing does not break, but they have no effect in recurrent Plan 1.
        del fixation_loss_weight, diversity_loss_weight

        if fixation_targets is not None and fixation_mask is not None:
            visual_attn_maps = getattr(self.model, "last_visual_attn_maps", None)
            if visual_attn_maps is None:
                qwen = getattr(self.model, "qwen2_model", None)
                visual_attn_maps = getattr(qwen, "last_visual_attn_maps", None)

            needs_attention_maps = (
                attention_weight != 0.0
                or os.getenv("DEEPSEEK_OCR2_COMPUTE_ATTENTION_DIAGNOSTICS", "0") == "1"
            )
            if visual_attn_maps is None and attention_weight != 0.0:
                raise RuntimeError(
                    "DEEPSEEK_OCR2_ATTENTION_LOSS_WEIGHT is nonzero, but recurrent "
                    "Plan 1 produced no Qwen self-attention maps. Ensure attention "
                    "supervision is enabled before the recurrent encoder forward."
                )

            if not getattr(self, "_scanflow_printed_tensor_stats", False):
                scanflow_debug_tensor("recurrent_plan1_visual_attn_maps", visual_attn_maps)
                scanflow_debug_tensor("fixation_targets", fixation_targets)
                scanflow_debug_tensor("fixation_mask", fixation_mask)
                self._scanflow_printed_tensor_stats = True

            if visual_attn_maps is not None and needs_attention_maps:
                # deepencoderv2 retains only the four selected middle layers.
                # Within each layer the loss averages ALL 14 query heads, aligns
                # that layer-level map to the temporal fixation target, and then
                # averages the four independent layer losses.
                attention_loss = scanpath_attention_alignment_loss(
                    visual_attn_maps=visual_attn_maps,
                    fixation_targets=fixation_targets,
                    fixation_mask=fixation_mask,
                )
                (
                    attention_target_entropy,
                    attention_prediction_entropy,
                    attention_excess_ce,
                ) = scanpath_attention_entropy_metrics(
                    visual_attn_maps=visual_attn_maps,
                    fixation_targets=fixation_targets,
                    fixation_mask=fixation_mask,
                )

            if attention_loss is not None:
                attention_term = attention_weight * attention_loss
                loss = attention_term if lm_loss is None else lm_loss + attention_term

        # Count forward micro-steps before optional diagnostics.
        self._scanflow_forward_step = getattr(self, "_scanflow_forward_step", 0) + 1

        # Per-loss gradient diagnostics. torch.autograd.grad leaves .grad buffers
        # untouched, so the Trainer can still backpropagate the combined loss.
        # This is expensive, so logging is interval-controlled.
        self.last_gradient_metrics = None
        grad_logging_enabled = os.getenv("DEEPSEEK_OCR2_LOG_GRADIENTS", "1") == "1"
        grad_log_interval = max(
            1, int(os.getenv("DEEPSEEK_OCR2_GRAD_LOG_INTERVAL", "10"))
        )
        should_log_gradients = (
            self.training
            and torch.is_grad_enabled()
            and grad_logging_enabled
            and self._scanflow_forward_step % grad_log_interval == 0
        )

        if should_log_gradients:
            filter_text = os.getenv("DEEPSEEK_OCR2_GRAD_PARAM_FILTER", "").strip()
            filters = [item.strip() for item in filter_text.split(",") if item.strip()]
            selected_named_parameters = [
                (name, parameter)
                for name, parameter in self.named_parameters()
                if parameter.requires_grad
                and (not filters or any(token in name for token in filters))
            ]
            selected_parameters = [parameter for _, parameter in selected_named_parameters]

            if selected_parameters:
                lm_gradients = scanflow_loss_gradients(lm_loss, selected_parameters)
                fixation_gradients = scanflow_loss_gradients(
                    fixation_loss, selected_parameters
                )
                diversity_gradients = scanflow_loss_gradients(
                    diversity_loss, selected_parameters
                )
                attention_gradients = scanflow_loss_gradients(
                    attention_loss, selected_parameters
                )

                lm_grad_norm = scanflow_gradient_norm(lm_gradients)
                fixation_grad_norm = scanflow_gradient_norm(fixation_gradients)
                diversity_grad_norm = scanflow_gradient_norm(diversity_gradients)
                attention_grad_norm = scanflow_gradient_norm(attention_gradients)
                weighted_fixation_grad_norm = abs(float(fixation_weight)) * fixation_grad_norm
                weighted_diversity_grad_norm = abs(float(diversity_weight)) * diversity_grad_norm
                weighted_attention_grad_norm = abs(float(attention_weight)) * attention_grad_norm
                combined_grad_norm = scanflow_weighted_combined_gradient_norm([
                    (1.0, lm_gradients),
                    (float(fixation_weight), fixation_gradients),
                    (float(diversity_weight), diversity_gradients),
                    (float(attention_weight), attention_gradients),
                ])

                lm_fixation_cosine = scanflow_gradient_cosine(
                    lm_gradients, fixation_gradients
                )
                lm_diversity_cosine = scanflow_gradient_cosine(
                    lm_gradients, diversity_gradients
                )
                fixation_diversity_cosine = scanflow_gradient_cosine(
                    fixation_gradients, diversity_gradients
                )
                lm_attention_cosine = scanflow_gradient_cosine(
                    lm_gradients, attention_gradients
                )
                fixation_attention_cosine = scanflow_gradient_cosine(
                    fixation_gradients, attention_gradients
                )
                diversity_attention_cosine = scanflow_gradient_cosine(
                    diversity_gradients, attention_gradients
                )

                def _grad_value(value):
                    return float(value.detach().float().cpu().item())

                self.last_gradient_metrics = {
                    "num_trainable_tensors": len(selected_named_parameters),
                    "num_trainable_elements": sum(
                        parameter.numel() for _, parameter in selected_named_parameters
                    ),
                    "lm_grad_norm": _grad_value(lm_grad_norm),
                    "fixation_grad_norm": _grad_value(fixation_grad_norm),
                    "diversity_grad_norm": _grad_value(diversity_grad_norm),
                    "attention_grad_norm": _grad_value(attention_grad_norm),
                    "weighted_fixation_grad_norm": _grad_value(weighted_fixation_grad_norm),
                    "weighted_diversity_grad_norm": _grad_value(weighted_diversity_grad_norm),
                    "weighted_attention_grad_norm": _grad_value(weighted_attention_grad_norm),
                    "combined_grad_norm": _grad_value(combined_grad_norm),
                    "lm_fixation_grad_cosine": _grad_value(lm_fixation_cosine),
                    "lm_diversity_grad_cosine": _grad_value(lm_diversity_cosine),
                    "fixation_diversity_grad_cosine": _grad_value(fixation_diversity_cosine),
                    "lm_attention_grad_cosine": _grad_value(lm_attention_cosine),
                    "fixation_attention_grad_cosine": _grad_value(fixation_attention_cosine),
                    "diversity_attention_grad_cosine": _grad_value(diversity_attention_cosine),
                }

                print(
                    "[scanflow gradients] "
                    f"forward_step={self._scanflow_forward_step} "
                    f"trainable_tensors={self.last_gradient_metrics['num_trainable_tensors']} "
                    f"trainable_elements={self.last_gradient_metrics['num_trainable_elements']} "
                    f"lm_norm={self.last_gradient_metrics['lm_grad_norm']:.6g} "
                    f"fixation_norm={self.last_gradient_metrics['fixation_grad_norm']:.6g} "
                    f"diversity_norm={self.last_gradient_metrics['diversity_grad_norm']:.6g} "
                    f"attention_norm={self.last_gradient_metrics['attention_grad_norm']:.6g} "
                    f"weighted_fixation_norm={self.last_gradient_metrics['weighted_fixation_grad_norm']:.6g} "
                    f"weighted_diversity_norm={self.last_gradient_metrics['weighted_diversity_grad_norm']:.6g} "
                    f"weighted_attention_norm={self.last_gradient_metrics['weighted_attention_grad_norm']:.6g} "
                    f"combined_norm={self.last_gradient_metrics['combined_grad_norm']:.6g} "
                    f"cos_lm_fixation={self.last_gradient_metrics['lm_fixation_grad_cosine']:.6g} "
                    f"cos_lm_diversity={self.last_gradient_metrics['lm_diversity_grad_cosine']:.6g} "
                    f"cos_fixation_diversity={self.last_gradient_metrics['fixation_diversity_grad_cosine']:.6g} "
                    f"cos_lm_attention={self.last_gradient_metrics['lm_attention_grad_cosine']:.6g} "
                    f"cos_fixation_attention={self.last_gradient_metrics['fixation_attention_grad_cosine']:.6g} "
                    f"cos_diversity_attention={self.last_gradient_metrics['diversity_attention_grad_cosine']:.6g}",
                    flush=True,
                )


                if os.getenv("DEEPSEEK_OCR2_LOG_NAMED_GRADIENTS", "1") == "1":
                    named_limit = max(
                        1,
                        int(os.getenv("DEEPSEEK_OCR2_NAMED_GRADIENT_LIMIT", "80")),
                    )
                    for parameter_index, (
                        parameter_name,
                        parameter,
                    ) in enumerate(selected_named_parameters[:named_limit]):
                        lm_grad = lm_gradients[parameter_index]
                        fix_grad = fixation_gradients[parameter_index]
                        div_grad = diversity_gradients[parameter_index]
                        attn_grad = attention_gradients[parameter_index]

                        def _named_norm(grad):
                            if grad is None:
                                return None
                            return grad.detach().float().norm().item()

                        def _named_max(grad):
                            if grad is None:
                                return None
                            return grad.detach().float().abs().max().item()

                        fix_norm = _named_norm(fix_grad)
                        attn_norm = _named_norm(attn_grad)
                        weighted_fix_norm = (
                            None
                            if fix_norm is None
                            else abs(float(fixation_weight)) * fix_norm
                        )
                        weighted_attn_norm = (
                            None
                            if attn_norm is None
                            else abs(float(attention_weight)) * attn_norm
                        )

                        print(
                            "[scanflow named component gradient] "
                            f"forward_step={self._scanflow_forward_step} "
                            f"name={parameter_name} "
                            f"shape={tuple(parameter.shape)} "
                            f"parameter_norm={parameter.detach().float().norm().item():.8g} "
                            f"lm_norm={_named_norm(lm_grad)} "
                            f"fixation_norm={fix_norm} "
                            f"weighted_fixation_norm={weighted_fix_norm} "
                            f"diversity_norm={_named_norm(div_grad)} "
                            f"attention_norm={attn_norm} "
                            f"weighted_attention_norm={weighted_attn_norm} "
                            f"fixation_max={_named_max(fix_grad)} "
                            f"attention_max={_named_max(attn_grad)} "
                            f"fixation_grad_none={fix_grad is None} "
                            f"attention_grad_none={attn_grad is None}",
                            flush=True,
                        )

        self.last_lm_loss = lm_loss.detach() if lm_loss is not None else None
        self.last_fixation_loss = fixation_loss.detach() if fixation_loss is not None else None
        self.last_diversity_loss = diversity_loss.detach() if diversity_loss is not None else None
        self.last_attention_loss = attention_loss.detach() if attention_loss is not None else None
        self.last_target_entropy = target_entropy.detach() if target_entropy is not None else None
        self.last_prediction_entropy = prediction_entropy.detach() if prediction_entropy is not None else None
        self.last_excess_fixation_ce = excess_fixation_ce.detach() if excess_fixation_ce is not None else None
        self.last_attention_target_entropy = attention_target_entropy.detach() if attention_target_entropy is not None else None
        self.last_attention_prediction_entropy = attention_prediction_entropy.detach() if attention_prediction_entropy is not None else None
        self.last_attention_excess_ce = attention_excess_ce.detach() if attention_excess_ce is not None else None

        if os.getenv("DEEPSEEK_OCR2_LOG_LOSSES", "1") == "1":
            def _loss_value(value):
                if value is None:
                    return None
                return float(value.detach().float().item())

            print(
                "[scanflow losses] "
                f"forward_step={self._scanflow_forward_step} "
                f"total={_loss_value(loss)} "
                f"lm={_loss_value(lm_loss)} "
                f"fixation={_loss_value(fixation_loss)} "
                f"target_entropy={_loss_value(target_entropy)} "
                f"prediction_entropy={_loss_value(prediction_entropy)} "
                f"excess_fixation_ce={_loss_value(excess_fixation_ce)} "
                f"diversity={_loss_value(diversity_loss)} "
                f"attention={_loss_value(attention_loss)} "
                f"attention_target_entropy={_loss_value(attention_target_entropy)} "
                f"attention_prediction_entropy={_loss_value(attention_prediction_entropy)} "
                f"attention_excess_ce={_loss_value(attention_excess_ce)} "
                f"fixation_weight={float(fixation_weight):.6g} "
                f"diversity_weight={float(diversity_weight):.6g} "
                f"attention_weight={float(attention_weight):.6g}",
                flush=True,
            )

        self._scanflow_save_parameter_snapshot()

        if not return_dict:
            output = (logits,) + outputs[1:]
            return (loss,) + output if loss is not None else output

        return CausalLMOutputWithPast(
            loss=loss,
            logits=logits,
            past_key_values=outputs.past_key_values,
            hidden_states=outputs.hidden_states,
            attentions=outputs.attentions,
        )


    def prepare_inputs_for_generation(
        self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
    ):
        # Omit tokens covered by past_key_values
        past_length = 0
        if past_key_values is not None:
            if isinstance(past_key_values, Cache):
                cache_length = past_key_values.get_seq_length()
                past_length = past_key_values.seen_tokens
                max_cache_length = past_key_values.get_max_length()
            else:
                cache_length = past_length = past_key_values[0][0].shape[2]
                max_cache_length = None

            # Keep only the unprocessed tokens:
            # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
            # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
            # input)
            if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
                input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
            # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
            # input_ids based on the past_length.
            elif past_length < input_ids.shape[1]:
                input_ids = input_ids[:, past_length:]
            # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.

            # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
            if (
                max_cache_length is not None
                and attention_mask is not None
                and cache_length + input_ids.shape[1] > max_cache_length
            ):
                attention_mask = attention_mask[:, -max_cache_length:]

        position_ids = kwargs.get("position_ids", None)
        if attention_mask is not None and position_ids is None:
            # create position_ids on the fly for batch generation
            position_ids = attention_mask.long().cumsum(-1) - 1
            position_ids.masked_fill_(attention_mask == 0, 1)
            if past_key_values:
                position_ids = position_ids[:, -input_ids.shape[1] :]

        # if self.generation_config.cache_implementation == "static":
        #     # generation with static cache
        #     cache_position = kwargs.get("cache_position", None)
        #     if cache_position is None:
        #         past_length = 0
        #     else:
        #         past_length = cache_position[-1] + 1
        #     input_ids = input_ids[:, past_length:]
        #     position_ids = position_ids[:, past_length:]

        # TODO @gante we should only keep a `cache_position` in generate, and do +=1.
        # same goes for position ids. Could also help with continued generation.
        cache_position = torch.arange(past_length, past_length + position_ids.shape[-1], device=position_ids.device)

        # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
        if inputs_embeds is not None and past_key_values is None:
            model_inputs = {"inputs_embeds": inputs_embeds}
        else:
            model_inputs = {"input_ids": input_ids}

        model_inputs.update(
            {
                "position_ids": position_ids,
                "past_key_values": past_key_values,
                "use_cache": kwargs.get("use_cache"),
                "attention_mask": attention_mask,
                "images": kwargs.get("images", None),
                "images_seq_mask": kwargs.get("images_seq_mask", None),
                "images_spatial_crop": kwargs.get("images_spatial_crop", None),
                "scanpath_lengths": kwargs.get("scanpath_lengths", None),
                "scanpath_steps": kwargs.get("scanpath_steps", None),
            }
        )
        return model_inputs
    

    def disable_torch_init(self):
        """
        Disable the redundant torch default initialization to accelerate model creation.
        """
        import torch
        setattr(torch.nn.Linear, "reset_parameters", lambda self: None)
        setattr(torch.nn.LayerNorm, "reset_parameters", lambda self: None)



    def infer(self, tokenizer, prompt='', image_file='', output_path = '', base_size=1024, image_size=640, crop_mode=True, test_compress=False, save_results=False, eval_mode=False, scanpath_steps=32):
        self.disable_torch_init()

        os.makedirs(output_path, exist_ok=True)
        os.makedirs(f'{output_path}/images', exist_ok=True)

        if prompt and image_file:
            conversation = [
                {
                    "role": "<|User|>",
                    # "content": "<image>\n<|grounding|>Given the layout of the image. ",
                    "content": f'{prompt}',
                    # "content": "君不见黄河之水天上来的下一句是什么?",
                    # "content": "<image>\nFree OCR. ",
                    # "content": "<image>\nParse the figure. ",
                    # "content": "<image>\nExtract the text in the image. ",
                    "images": [f'{image_file}'],
                },
                {"role": "<|Assistant|>", "content": ""},
            ]
        
        elif prompt:
            conversation = [
                {
                    "role": "<|User|>",
                    # "content": "<image>\n<|grounding|>Given the layout of the image. ",
                    "content": f'{prompt}',
                    # "content": "君不见黄河之水天上来的下一句是什么?",
                    # "content": "<image>\nFree OCR. ",
                    # "content": "<image>\nParse the figure. ",
                    # "content": "<image>\nExtract the text in the image. ",
                    # "images": [f'{image_file}'],
                },
                {"role": "<|Assistant|>", "content": ""},
            ]
        else:
            assert False, f'prompt is none!'
        
        prompt = format_messages(conversations=conversation, sft_format='plain', system_prompt='')

        patch_size = 16
        downsample_ratio = 4
        images = load_pil_images(conversation)

        valid_img_tokens = 0
        ratio = 1

        image_draw = images[0].copy()

        w,h = image_draw.size
        # print(w, h)
        ratio = 1 - ((max(w, h) - min(w, h)) / (max(w, h)))
    

        image_transform=BasicImageTransform(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), normalize=True)
        images_seq_mask = []

        image_token = '<image>'
        image_token_id = 128815
        text_splits = prompt.split(image_token)

        images_list, images_crop_list, images_seq_mask = [], [], []
        tokenized_str = []
        images_spatial_crop = []
        for text_sep, image in zip(text_splits, images):

            tokenized_sep = text_encode(tokenizer, text_sep, bos=False, eos=False)
            tokenized_str += tokenized_sep
            images_seq_mask += [False] * len(tokenized_sep)

            if crop_mode:

                if image.size[0] <= 768 and image.size[1] <= 768:
                    crop_ratio = [1, 1]

                else:
                    if crop_mode:
                        # best_width, best_height = select_best_resolution(image.size, self.candidate_resolutions)
                        images_crop_raw, crop_ratio = dynamic_preprocess(image)
                    else:
                        # best_width, best_height = self.image_size, self.image_size
                        crop_ratio = [1, 1]
                
                """process the global view"""
                # image = image.resize((base_size, base_size))
                global_view = ImageOps.pad(image, (base_size, base_size),
                                        color=tuple(int(x * 255) for x in image_transform.mean))
                
                if base_size == 1024:
                    valid_img_tokens += int(256 * ratio)
                elif base_size == 1280:
                    valid_img_tokens += int(400 * ratio)
                # elif base_size == 640:
                #     valid_img_tokens += int(100 * ratio)
                



                
                images_list.append(image_transform(global_view).to(torch.bfloat16))

                # global_view_tensor = image_transform(global_view).to(torch.bfloat16)

                width_crop_num, height_crop_num = crop_ratio

                images_spatial_crop.append([width_crop_num, height_crop_num])
                
                
                if width_crop_num > 1 or height_crop_num > 1:
                    """process the local views"""
                    
                    for i in range(len(images_crop_raw)):
                        images_crop_list.append(image_transform(images_crop_raw[i]).to(torch.bfloat16))
                
                if image_size == 768:
                    valid_img_tokens += len(images_crop_list) * 144

                num_queries = math.ceil((image_size // patch_size) / downsample_ratio)
                num_queries_base = math.ceil((base_size // patch_size) / downsample_ratio)



                """add image tokens"""

                

                # ScanFlow Pro V2 returns [V + lambda(V,Q)*R; H] for the global view
                # and every local crop: N_visual residual-modified visual tokens
                # plus T recurrent reasoning states per view.
                num_local_views = (
                    width_crop_num * height_crop_num
                    if (width_crop_num > 1 or height_crop_num > 1)
                    else 0
                )
                global_v2_tokens = num_queries_base ** 2 + int(scanpath_steps)
                local_v2_tokens = num_queries ** 2 + int(scanpath_steps)
                num_scanflow_v2_image_tokens = (
                    global_v2_tokens
                    + num_local_views * local_v2_tokens
                    + 1
                )  # +1 existing view separator
                tokenized_image = [image_token_id] * num_scanflow_v2_image_tokens
                tokenized_str += tokenized_image
                images_seq_mask += [True] * len(tokenized_image)
                # num_image_tokens.append(len(tokenized_image))

            else:
                # best_width, best_height = self.image_size, self.image_size
                # print(image.size, (best_width, best_height)) # check the select_best_resolutions func

                """process the global view"""
                if image_size <= 768:
                    print('directly resize')
                    image = image.resize((image_size, image_size))
                # else:
                global_view = ImageOps.pad(image, (image_size, image_size),
                                        color=tuple(int(x * 255) for x in image_transform.mean))
                images_list.append(image_transform(global_view).to(torch.bfloat16))

                if base_size == 1024:
                    valid_img_tokens += int(256 * ratio)
                elif base_size == 1280:
                    valid_img_tokens += int(400 * ratio)
                elif base_size == 640:
                    valid_img_tokens += int(100 * 1)
                elif base_size == 512:
                    valid_img_tokens += int(64 * 1)
                elif base_size == 768:
                    valid_img_tokens += int(144 * 1)

                width_crop_num, height_crop_num = 1, 1

                images_spatial_crop.append([width_crop_num, height_crop_num])


                """add image tokens"""
                num_queries = math.ceil((image_size // patch_size) / downsample_ratio)

                # ScanFlow Pro V2: [V + lambda(V,Q)*R; H] for one global view,
                # plus the existing separator.
                tokenized_image = [image_token_id] * (
                    num_queries ** 2 + int(scanpath_steps) + 1
                )
                # tokenized_image += ([self.image_token_id] * (num_queries * width_crop_num) + [self.image_token_id]) * (
                #             num_queries * height_crop_num)
                tokenized_str += tokenized_image
                images_seq_mask += [True] * len(tokenized_image)
                # num_image_tokens.append(len(tokenized_image))
        

        """process the last text split"""
        tokenized_sep = text_encode(tokenizer, text_splits[-1], bos=False, eos=False)
        tokenized_str += tokenized_sep
        images_seq_mask += [False] * len(tokenized_sep)

        """add the bos tokens"""
        bos_id = 0
        tokenized_str = [bos_id] + tokenized_str 
        images_seq_mask = [False] + images_seq_mask



        input_ids = torch.LongTensor(tokenized_str)


        

        images_seq_mask = torch.tensor(images_seq_mask, dtype=torch.bool)


        if len(images_list) == 0:
            images_ori = torch.zeros((1, 3, image_size, image_size))
            images_spatial_crop = torch.zeros((1, 2), dtype=torch.long)
            images_crop = torch.zeros((1, 3, base_size, base_size))

        else:
            images_ori = torch.stack(images_list, dim=0)
            images_spatial_crop = torch.tensor(images_spatial_crop, dtype=torch.long)
            if images_crop_list:
                images_crop = torch.stack(images_crop_list, dim=0)
            else:
                images_crop = torch.zeros((1, 3, base_size, base_size))



        if not eval_mode:
            streamer = NoEOSTextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=False)
            with torch.autocast("cuda", dtype=torch.bfloat16):
                with torch.no_grad():
                    output_ids = self.generate(
                        input_ids.unsqueeze(0).cuda(),
                        images=[(images_crop.cuda(), images_ori.cuda())],
                        images_seq_mask = images_seq_mask.unsqueeze(0).cuda(),
                        images_spatial_crop = images_spatial_crop,
                        scanpath_steps = int(scanpath_steps),
                        # do_sample=False,
                        # num_beams = 1,
                        temperature=0.0,
                        eos_token_id=tokenizer.eos_token_id,
                        streamer=streamer,
                        max_new_tokens=8192,
                        no_repeat_ngram_size = 20,
                        use_cache = True
                        )

        else:
            with torch.autocast("cuda", dtype=torch.bfloat16):
                with torch.no_grad():
                    output_ids = self.generate(
                        input_ids.unsqueeze(0).cuda(),
                        images=[(images_crop.cuda(), images_ori.cuda())],
                        images_seq_mask = images_seq_mask.unsqueeze(0).cuda(),
                        images_spatial_crop = images_spatial_crop,
                        scanpath_steps = int(scanpath_steps),
                        # do_sample=False,
                        # num_beams = 1,
                        temperature=0.0,
                        eos_token_id=tokenizer.eos_token_id,
                        max_new_tokens=8192,
                        no_repeat_ngram_size = 35,
                        use_cache = True
                        )
                

        if '<image>' in conversation[0]['content'] and eval_mode:
                outputs = tokenizer.decode(output_ids[0, input_ids.unsqueeze(0).cuda().shape[1]:])
                stop_str = '<|end▁of▁sentence|>'
                if outputs.endswith(stop_str):
                    outputs = outputs[:-len(stop_str)]
                # re_match
                outputs = outputs.strip()

                return outputs
        
        if '<image>' in conversation[0]['content'] and test_compress:
            outputs = tokenizer.decode(output_ids[0, input_ids.unsqueeze(0).cuda().shape[1]:])
            pure_texts_outputs_token_length = len(text_encode(tokenizer, outputs, bos=False, eos=False))
            print('='*50)
            print('image size: ', (w, h))
            print('valid image tokens: ', int(valid_img_tokens))
            print('output texts tokens (valid): ', pure_texts_outputs_token_length)
            print('compression ratio: ', round(pure_texts_outputs_token_length/valid_img_tokens, 2))
            print('='*50)


        if '<image>' in conversation[0]['content'] and save_results:
            outputs = tokenizer.decode(output_ids[0, input_ids.unsqueeze(0).cuda().shape[1]:])
            stop_str = '<|end▁of▁sentence|>'

            print('='*15 + 'save results:' + '='*15)
            
            # # # # conv.messages[-1][-1] = outputs
            if outputs.endswith(stop_str):
                outputs = outputs[:-len(stop_str)]
            outputs = outputs.strip()

            matches_ref, matches_images, mathes_other = re_match(outputs)
            # print(matches_ref)
            result = process_image_with_refs(image_draw, matches_ref, output_path)


            for idx, a_match_image in enumerate(tqdm(matches_images, desc="image")):
                outputs = outputs.replace(a_match_image, '![](images/' + str(idx) + '.jpg)\n')
            
            for idx, a_match_other in enumerate(tqdm(mathes_other, desc="other")):
                outputs = outputs.replace(a_match_other, '').replace('\\coloneqq', ':=').replace('\\eqqcolon', '=:')


            # if 'structural formula' in conversation[0]['content']:
            #     outputs = '<smiles>' + outputs + '</smiles>'
            with open(f'{output_path}/result.mmd', 'w', encoding = 'utf-8') as afile:
                afile.write(outputs)

            if 'line_type' in outputs:
                import matplotlib.pyplot as plt
                lines = eval(outputs)['Line']['line']

                line_type = eval(outputs)['Line']['line_type']
                # print(lines)

                endpoints = eval(outputs)['Line']['line_endpoint']

                fig, ax = plt.subplots(figsize=(3,3), dpi=200)
                ax.set_xlim(-15, 15)
                ax.set_ylim(-15, 15)

                for idx, line in enumerate(lines):
                    try:
                        p0 = eval(line.split(' -- ')[0])
                        p1 = eval(line.split(' -- ')[-1])

                        if line_type[idx] == '--':
                            ax.plot([p0[0], p1[0]], [p0[1], p1[1]], linewidth=0.8, color='k')
                        else:
                            ax.plot([p0[0], p1[0]], [p0[1], p1[1]], linewidth = 0.8, color = 'k')

                        ax.scatter(p0[0], p0[1], s=5, color = 'k')
                        ax.scatter(p1[0], p1[1], s=5, color = 'k')
                    except:
                        pass

                for endpoint in endpoints:

                    label = endpoint.split(': ')[0]
                    (x, y) = eval(endpoint.split(': ')[1])
                    ax.annotate(label, (x, y), xytext=(1, 1), textcoords='offset points', 
                                fontsize=5, fontweight='light')
                

                plt.savefig(f'{output_path}/geo.jpg')
                plt.close()

            result.save(f"{output_path}/result_with_boxes.jpg")