rec_unimernet_head.py
93.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
import copy
import math
import re
import numpy as np
import inspect
import warnings
from collections import OrderedDict
from typing import Optional, Tuple, Union, List, Dict, Any
from dataclasses import dataclass, fields, is_dataclass
import torch
import torch.nn as nn
from torch import Tensor
import torch.nn.functional as F
from torch.nn import CrossEntropyLoss
from mineru.utils.config_reader import get_device
class ModelOutput(OrderedDict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __post_init__(self):
class_fields = fields(self)
if not len(class_fields):
raise ValueError(f"{self.__class__.__name__} has no fields.")
if not all(field.default is None for field in class_fields[1:]):
raise ValueError(
f"{self.__class__.__name__} should not have more than one required field."
)
first_field = getattr(self, class_fields[0].name)
other_fields_are_none = all(
getattr(self, field.name) is None for field in class_fields[1:]
)
if other_fields_are_none:
if isinstance(first_field, dict):
iterator = first_field.items()
first_field_iterator = True
else:
try:
iterator = iter(first_field)
first_field_iterator = True
except TypeError:
first_field_iterator = False
if first_field_iterator:
for idx, element in enumerate(iterator):
if (
not isinstance(element, (list, tuple))
or not len(element) == 2
or not isinstance(element[0], str)
):
if idx == 0:
self[class_fields[0].name] = first_field
else:
raise ValueError(
f"Cannot set key/value for {element}. It needs to be a tuple (key, value)."
)
break
setattr(self, element[0], element[1])
if element[1] is not None:
self[element[0]] = element[1]
elif first_field is not None:
self[class_fields[0].name] = first_field
else:
for field in class_fields:
v = getattr(self, field.name)
if v is not None:
self[field.name] = v
def __delitem__(self, *args, **kwargs):
raise Exception(
f"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."
)
def setdefault(self, *args, **kwargs):
raise Exception(
f"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."
)
def pop(self, *args, **kwargs):
raise Exception(
f"You cannot use ``pop`` on a {self.__class__.__name__} instance."
)
def update(self, *args, **kwargs):
raise Exception(
f"You cannot use ``update`` on a {self.__class__.__name__} instance."
)
def __getitem__(self, k):
if isinstance(k, str):
inner_dict = dict(self.items())
return inner_dict[k]
else:
return self.to_tuple()[k]
def __setattr__(self, name, value):
if name in self.keys() and value is not None:
super().__setitem__(name, value)
super().__setattr__(name, value)
def __setitem__(self, key, value):
super().__setitem__(key, value)
super().__setattr__(key, value)
def __reduce__(self):
if not is_dataclass(self):
return super().__reduce__()
callable, _args, *remaining = super().__reduce__()
args = tuple(getattr(self, field.name) for field in fields(self))
return callable, args, *remaining
def to_tuple(self):
return tuple(self[k] for k in self.keys())
@dataclass
class BaseModelOutputWithPastAndCrossAttentions(ModelOutput):
last_hidden_state = None
past_key_values = None
hidden_states = None
attentions = None
cross_attentions = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@dataclass
class Seq2SeqLMOutput(ModelOutput):
loss = None
logits = None
past_key_values = None
decoder_hidden_states = None
decoder_attentions = None
cross_attentions = None
encoder_last_hidden_state = None
encoder_hidden_states = None
encoder_attentions = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class MBartConfig(object):
model_type = "mbart"
keys_to_ignore_at_inference = ["past_key_values"]
attribute_map = {
"num_attention_heads": "encoder_attention_heads",
"hidden_size": "d_model",
}
def __init__(
self,
vocab_size=50265,
max_position_embeddings=1024,
encoder_layers=12,
encoder_ffn_dim=4096,
encoder_attention_heads=16,
decoder_layers=12,
decoder_ffn_dim=4096,
decoder_attention_heads=16,
encoder_layerdrop=0.0,
decoder_layerdrop=0.0,
use_cache=True,
is_encoder_decoder=True,
activation_function="gelu",
d_model=1024,
dropout=0.1,
output_hidden_states=False,
use_return_dict=True,
attention_dropout=0.0,
activation_dropout=0.0,
init_std=0.02,
classifier_dropout=0.0,
scale_embedding=False,
pad_token_id=1,
bos_token_id=0,
eos_token_id=2,
forced_eos_token_id=2,
_attn_implementation="eager",
hidden_size=1024,
use_parallel=False,
parallel_step=2,
is_export=False,
**kwargs,
):
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.max_position_embeddings = max_position_embeddings
self.d_model = d_model
self.encoder_ffn_dim = encoder_ffn_dim
self.encoder_layers = encoder_layers
self.encoder_attention_heads = encoder_attention_heads
self.decoder_ffn_dim = decoder_ffn_dim
self.decoder_layers = decoder_layers
self.decoder_attention_heads = decoder_attention_heads
self.dropout = dropout
self.output_hidden_states = output_hidden_states
self.use_return_dict = use_return_dict
self.attention_dropout = attention_dropout
self.activation_dropout = activation_dropout
self.activation_function = activation_function
self.init_std = init_std
self.encoder_layerdrop = encoder_layerdrop
self.decoder_layerdrop = decoder_layerdrop
self.classifier_dropout = classifier_dropout
self.use_cache = use_cache
self.num_hidden_layers = encoder_layers
self.scale_embedding = (
scale_embedding # scale factor will be sqrt(d_model) if True
)
self.pad_token_id = pad_token_id
self.bos_token_id = bos_token_id
self.eos_token_id = eos_token_id
self.is_encoder_decoder = is_encoder_decoder
self.forced_eos_token_id = forced_eos_token_id
self._attn_implementation = _attn_implementation
self.use_parallel = use_parallel
self.parallel_step = parallel_step
self.is_export = is_export
super().__init__()
@dataclass
class AttentionMaskConverter:
"""
A utility class for converting attention masks used in transformer models.
This class handles the conversion of attention masks based on whether the
attention mechanism is causal (i.e., preventing information flow from future
tokens to past tokens) and whether a sliding window approach is used.
Attributes:
is_causal (bool): Indicates if the attention mechanism is causal.
sliding_window (Optional[int]): Specifies the size of the sliding window
for local attention, if applicable.
Args:
is_causal (bool): Determines if the attention mask should enforce causality.
sliding_window (Optional[int], optional): The size of the sliding window
for local attention. Default is None.
"""
is_causal: bool
sliding_window: int
def __init__(self, is_causal: bool, sliding_window=None):
self.is_causal = is_causal
self.sliding_window = sliding_window
if self.sliding_window is not None and self.sliding_window <= 0:
raise ValueError(
f"Make sure that when passing `sliding_window` that its value is a strictly positive integer, not `{self.sliding_window}`"
)
@staticmethod
def _make_causal_mask(
input_ids_shape,
dtype,
past_key_values_length=0,
sliding_window=None,
is_export=False,
):
bsz, tgt_len = input_ids_shape
if is_export:
mask = torch.full(
[tgt_len, tgt_len], fill_value=torch.finfo(dtype).min, dtype=torch.float64
)
else:
mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min)
mask_cond = torch.arange(mask.shape[-1])
mask = mask.masked_fill_(
mask_cond < (mask_cond + 1).reshape([mask.shape[-1], 1]), 0
)
return mask[None, None, :, :].expand(
[bsz, 1, tgt_len, tgt_len + past_key_values_length]
)
def to_4d_export(
self,
attention_mask_2d,
query_length,
dtype,
key_value_length,
is_export=False,
):
input_shape = (attention_mask_2d.shape[0], query_length)
expanded_attn_mask = self._expand_mask(
attention_mask_2d, dtype, tgt_len=input_shape[-1]
)
expanded_4d_mask = expanded_attn_mask
return expanded_4d_mask
def to_4d(
self,
attention_mask_2d,
query_length,
dtype,
key_value_length,
is_export=False,
):
input_shape = (attention_mask_2d.shape[0], query_length)
causal_4d_mask = None
if (input_shape[-1] > 1 or self.sliding_window is not None) and self.is_causal:
if key_value_length is None:
raise ValueError(
"This attention mask converter is causal. Make sure to pass `key_value_length` to correctly create a causal mask."
)
past_key_values_length = key_value_length - query_length
causal_4d_mask = self._make_causal_mask(
input_shape,
dtype,
past_key_values_length=past_key_values_length,
sliding_window=self.sliding_window,
is_export=is_export,
)
elif self.sliding_window is not None:
raise NotImplementedError(
"Sliding window is currently only implemented for causal masking"
)
expanded_attn_mask = self._expand_mask(
attention_mask_2d, dtype, tgt_len=input_shape[-1]
)
if causal_4d_mask is not None:
if is_export:
expanded_attn_mask = causal_4d_mask
return expanded_attn_mask
else:
expanded_attn_mask = causal_4d_mask.masked_fill_(
expanded_attn_mask.to(torch.bool), torch.finfo(dtype).min
)
expanded_4d_mask = expanded_attn_mask
return expanded_4d_mask
def _expand_mask(self, mask, dtype, tgt_len=None):
bsz, src_len = mask.shape
tgt_len = tgt_len if tgt_len is not None else src_len
expanded_mask = (
mask[:, None, None, :].expand([bsz, 1, tgt_len, src_len]).to(dtype)
)
inverted_mask = 1.0 - expanded_mask
return inverted_mask.masked_fill_(
inverted_mask.to(torch.bool), torch.finfo(dtype).min
)
def _prepare_4d_attention_mask(mask, dtype, tgt_len=None):
return AttentionMaskConverter._expand_mask(mask=mask, dtype=dtype, tgt_len=tgt_len)
def _prepare_4d_causal_attention_mask_export(
attention_mask,
input_shape,
inputs_embeds,
past_key_values_length,
sliding_window=None,
is_export=False,
):
attn_mask_converter = AttentionMaskConverter(
is_causal=True, sliding_window=sliding_window
)
key_value_length = input_shape[-1] + past_key_values_length
shape = attention_mask.shape
len_shape = len(shape)
attention_mask = attn_mask_converter.to_4d_export(
attention_mask,
input_shape[-1],
key_value_length=key_value_length,
dtype=inputs_embeds.dtype,
is_export=is_export,
)
return attention_mask
def _prepare_4d_causal_attention_mask(
attention_mask,
input_shape,
inputs_embeds,
past_key_values_length,
sliding_window=None,
is_export=False,
):
attn_mask_converter = AttentionMaskConverter(
is_causal=True, sliding_window=sliding_window
)
key_value_length = input_shape[-1] + past_key_values_length
shape = attention_mask.shape
len_shape = len(shape)
if (attention_mask is not None) and (len_shape == 2):
attention_mask = attn_mask_converter.to_4d(
attention_mask,
input_shape[-1],
key_value_length=key_value_length,
dtype=inputs_embeds.dtype,
is_export=is_export,
)
return attention_mask
elif attention_mask is not None and len(attention_mask.shape) == 4:
expected_shape = (input_shape[0], 1, input_shape[1], key_value_length)
if tuple(attention_mask.shape) != expected_shape:
raise ValueError(
f"Incorrect 4D attention_mask shape: {tuple(attention_mask.shape)}; expected: {expected_shape}."
)
else:
inverted_mask = 1.0 - attention_mask
attention_mask = inverted_mask.masked_fill_(
inverted_mask.to(torch.bool), torch.finfo(inputs_embeds.dtype).min
)
else:
attention_mask = attn_mask_converter.to_causal_4d(
input_shape[0],
input_shape[-1],
key_value_length,
dtype=inputs_embeds.dtype,
)
return attention_mask
class MBartLearnedPositionalEmbedding(nn.Embedding):
"""
This module learns positional embeddings up to a fixed maximum size.
"""
def __init__(self, num_embeddings, embedding_dim):
self.offset = 2
super().__init__(num_embeddings + self.offset, embedding_dim)
self.device = torch.device(get_device())
def forward(self, input_ids, past_key_values_length=0):
"""`input_ids' shape is expected to be [bsz x seqlen]."""
bsz, seq_len = input_ids.shape[:2]
positions = torch.arange(
past_key_values_length, past_key_values_length + seq_len, dtype=torch.int64
).expand([bsz, -1]).to(self.device)
return nn.Embedding.forward(self, positions + self.offset)
class MBartPreTrainedModel(nn.Module):
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["MBartDecoderLayer", "MBartAttention"]
_supports_flash_attn_2 = True
def __init__(self, config):
super().__init__()
self.config = config
def _initialize_weights(self, module):
"""
Initialize the weights if they are not already initialized.
"""
if getattr(module, "_is_hf_initialized", False):
return
self._init_weights(module)
def post_init(self):
self.apply(self._initialize_weights)
def _init_weights(self, module):
std = self.config.init_std
if isinstance(module, nn.Linear):
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
torch.nn.init.constant_(module.bias, val=0.0)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
if module.padding_idx is not None:
torch.nn.init.constant_(module.weight[module.padding_idx], val=0.0)
@property
def dummy_inputs(self):
pad_token = self.config.pad_token_id
input_ids = torch.tensor([[0, 6, 10, 4, 2], [0, 8, 12, 2, pad_token]])
dummy_inputs = {
"attention_mask": input_ids.ne(pad_token),
"input_ids": input_ids,
}
return dummy_inputs
class MBartAttention(nn.Module):
"""Multi-headed attention from 'Attention Is All You Need' paper"""
def __init__(
self,
embed_dim,
num_heads,
dropout: float = 0.0,
is_decoder: bool = False,
bias: bool = True,
is_causal: bool = False,
config=None,
):
super().__init__()
self.embed_dim = embed_dim
self.num_heads = num_heads
self.dropout = dropout
self.head_dim = embed_dim // num_heads
self.config = config
if (self.head_dim * num_heads) != self.embed_dim:
raise ValueError(
f"embed_dim must be divisible by num_heads (got `embed_dim`: {self.embed_dim}"
f" and `num_heads`: {num_heads})."
)
self.scaling = self.head_dim ** -0.5
self.is_decoder = is_decoder
self.is_causal = is_causal
self.k_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
self.v_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
def _shape(self, tensor, seq_len, bsz):
return tensor.reshape([bsz, seq_len, self.num_heads, self.head_dim]).permute(
0, 2, 1, 3
)
def forward(
self,
hidden_states,
key_value_states=None,
past_key_value=None,
attention_mask=None,
layer_head_mask=None,
output_attentions=False,
):
is_cross_attention = key_value_states is not None
bsz, tgt_len, _ = hidden_states.shape
query_states = self.q_proj(hidden_states) * self.scaling
if (
is_cross_attention
and past_key_value is not None
and past_key_value[0].shape[2] == key_value_states.shape[1]
):
key_states = past_key_value[0]
value_states = past_key_value[1]
elif is_cross_attention:
key_states = self._shape(self.k_proj(key_value_states), -1, bsz)
value_states = self._shape(self.v_proj(key_value_states), -1, bsz)
elif past_key_value is not None:
key_states = self._shape(self.k_proj(hidden_states), -1, bsz)
value_states = self._shape(self.v_proj(hidden_states), -1, bsz)
key_states = torch.concat([past_key_value[0], key_states], dim=2)
value_states = torch.concat([past_key_value[1], value_states], dim=2)
else:
key_states = self._shape(self.k_proj(hidden_states), -1, bsz)
value_states = self._shape(self.v_proj(hidden_states), -1, bsz)
if self.is_decoder:
past_key_value = (key_states, value_states)
proj_shape = (bsz * self.num_heads, -1, self.head_dim)
query_states = self._shape(query_states, tgt_len, bsz).reshape(proj_shape)
key_states = key_states.reshape(proj_shape)
value_states = value_states.reshape(proj_shape)
src_len = key_states.shape[1]
attn_weights = torch.bmm(query_states, key_states.permute([0, 2, 1]))
if attention_mask is not None:
attn_weights = (
attn_weights.reshape([bsz, self.num_heads, tgt_len, src_len])
+ attention_mask
)
attn_weights = attn_weights.reshape(
[bsz * self.num_heads, tgt_len, src_len]
)
attn_weights = nn.functional.softmax(attn_weights, dim=-1)
if layer_head_mask is not None:
if tuple(layer_head_mask.shape) != (self.num_heads,):
raise ValueError(
f"Head mask for a single layer should be of shape {(self.num_heads,)}, but is"
f" {layer_head_mask.shape}"
)
attn_weights = layer_head_mask.reshape(
[1, -1, 1, 1]
) * attn_weights.reshape([bsz, self.num_heads, tgt_len, src_len])
attn_weights = attn_weights.reshape(
[bsz * self.num_heads, tgt_len, src_len]
)
if output_attentions:
attn_weights_reshaped = attn_weights.reshape(
[bsz, self.num_heads, tgt_len, src_len]
)
attn_weights = attn_weights_reshaped.reshape(
[bsz * self.num_heads, tgt_len, src_len]
)
else:
attn_weights_reshaped = None
attn_probs = nn.functional.dropout(
attn_weights, p=self.dropout, training=self.training
)
attn_output = torch.bmm(attn_probs, value_states)
attn_output = attn_output.reshape([bsz, self.num_heads, tgt_len, self.head_dim])
attn_output = attn_output.permute([0, 2, 1, 3])
attn_output = attn_output.reshape([bsz, tgt_len, self.embed_dim])
attn_output = self.out_proj(attn_output)
return attn_output, attn_weights_reshaped, past_key_value
MBART_ATTENTION_CLASSES = {
"eager": MBartAttention,
}
class MBartDecoderLayer(nn.Module):
def __init__(self, config):
super().__init__()
self.embed_dim = config.d_model
self.self_attn = MBART_ATTENTION_CLASSES[config._attn_implementation](
embed_dim=self.embed_dim,
num_heads=config.decoder_attention_heads,
dropout=config.attention_dropout,
is_decoder=True,
is_causal=True,
config=config,
)
self.is_export = config.is_export
self.dropout = config.dropout
self.activation_fn = F.gelu
self.activation_dropout = config.activation_dropout
self.self_attn_layer_norm = nn.LayerNorm(self.embed_dim)
self.encoder_attn = MBART_ATTENTION_CLASSES[config._attn_implementation](
self.embed_dim,
config.decoder_attention_heads,
dropout=config.attention_dropout,
is_decoder=True,
config=config,
)
self.encoder_attn_layer_norm = nn.LayerNorm(self.embed_dim)
self.fc1 = nn.Linear(self.embed_dim, config.decoder_ffn_dim)
self.fc2 = nn.Linear(config.decoder_ffn_dim, self.embed_dim)
self.final_layer_norm = nn.LayerNorm(self.embed_dim)
self.device = torch.device(get_device())
def forward(
self,
hidden_states,
attention_mask=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
layer_head_mask=None,
cross_attn_layer_head_mask=None,
past_key_value: Optional[Tuple[torch.Tensor]] = None,
output_attentions: Optional[bool] = False,
use_cache: Optional[bool] = True,
) -> torch.Tensor:
residual = hidden_states
hidden_states = self.self_attn_layer_norm(hidden_states)
self_attn_past_key_value = None
if past_key_value is not None:
self_attn_past_key_value = tuple(
t.to(self.device) if isinstance(t, torch.Tensor) else t for t in past_key_value[:2]
)
hidden_states, self_attn_weights, present_key_value = self.self_attn(
hidden_states=hidden_states,
past_key_value=self_attn_past_key_value,
attention_mask=attention_mask,
layer_head_mask=layer_head_mask,
output_attentions=output_attentions,
)
hidden_states = nn.functional.dropout(
hidden_states, p=self.dropout, training=self.training
)
hidden_states = residual + hidden_states
cross_attn_present_key_value = None
cross_attn_weights = None
if encoder_hidden_states is not None:
residual = hidden_states
hidden_states = self.encoder_attn_layer_norm(hidden_states)
cross_attn_past_key_value = (
past_key_value[-2:] if past_key_value is not None else None
)
hidden_states, cross_attn_weights, cross_attn_present_key_value = (
self.encoder_attn(
hidden_states=hidden_states,
key_value_states=encoder_hidden_states,
attention_mask=encoder_attention_mask,
layer_head_mask=cross_attn_layer_head_mask,
past_key_value=cross_attn_past_key_value,
output_attentions=output_attentions,
)
)
hidden_states = nn.functional.dropout(
hidden_states, p=self.dropout, training=self.training
)
hidden_states = residual + hidden_states
present_key_value = present_key_value + cross_attn_present_key_value
residual = hidden_states
hidden_states = self.final_layer_norm(hidden_states)
hidden_states = self.activation_fn(self.fc1(hidden_states))
hidden_states = nn.functional.dropout(
hidden_states, p=self.activation_dropout, training=self.training
)
hidden_states = self.fc2(hidden_states)
hidden_states = nn.functional.dropout(
hidden_states, p=self.dropout, training=self.training
)
hidden_states = residual + hidden_states
outputs = (hidden_states,)
if output_attentions:
outputs += (self_attn_weights, cross_attn_weights)
if self.is_export:
outputs += (present_key_value,)
else:
if use_cache:
outputs += (present_key_value,)
return outputs
class MBartForCausalLM(MBartPreTrainedModel):
_tied_weights_keys = ["lm_head.weight"]
def __init__(self, config):
config = copy.deepcopy(config)
config.is_decoder = True
config.is_encoder_decoder = False
super().__init__(config)
self.model = MBartDecoderWrapper(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
self.post_init()
def get_input_embeddings(self):
return self.model.decoder.embed_tokens
def set_input_embeddings(self, value):
self.model.decoder.embed_tokens = value
def get_output_embeddings(self):
return self.lm_head
def set_output_embeddings(self, new_embeddings):
self.lm_head = new_embeddings
def set_decoder(self, decoder):
self.model.decoder = decoder
def get_decoder(self):
return self.model.decoder
def forward(
self,
input_ids=None,
attention_mask=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
head_mask=None,
cross_attn_head_mask=None,
past_key_values=None,
inputs_embeds=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
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
)
outputs = self.model.decoder(
input_ids=input_ids,
attention_mask=attention_mask,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
head_mask=head_mask,
cross_attn_head_mask=cross_attn_head_mask,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
logits = self.lm_head(outputs[0])
loss = None
if labels is not None:
labels = labels
loss_fct = CrossEntropyLoss()
loss = loss_fct(
logits.reshape([-1, self.config.vocab_size]), labels.reshape([-1])
)
if not return_dict:
output = (logits,) + outputs[1:]
return (loss,) + output if loss is not None else output
return CausalLMOutputWithCrossAttentions(
loss=loss,
logits=logits,
past_key_values=outputs.past_key_values,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
cross_attentions=outputs.cross_attentions,
)
def prepare_inputs_for_generation(
self,
input_ids,
past_key_values=None,
attention_mask=None,
use_cache=None,
**kwargs,
):
if attention_mask is None:
attention_mask = input_ids.new_ones(input_ids.shape)
if past_key_values:
past_length = past_key_values[0][0].shape[2]
if input_ids.shape[1] > past_length:
remove_prefix_length = past_length
else:
remove_prefix_length = input_ids.shape[1] - 1
input_ids = input_ids[:, remove_prefix_length:]
return {
"input_ids": input_ids,
"attention_mask": attention_mask,
"past_key_values": past_key_values,
"use_cache": use_cache,
}
@staticmethod
def _reorder_cache(past_key_values, beam_idx):
reordered_past = ()
for layer_past in past_key_values:
reordered_past += (
tuple(
past_state.index_select(0, beam_idx) for past_state in layer_past
),
)
return reordered_past
class myLayerNorm(nn.LayerNorm):
"""
Custom implementation of Layer Normalization, with additional options.
This class extends the standard LayerNorm to include optional features,
such as drop block regularization, which might be used for improving
model generalization.
Args:
num_channels (int): The number of features or channels in the input.
eps (float, optional): A small value added to the denominator for numerical stability. Default is 1e-5.
affine (bool, optional): If True, this module has learnable affine parameters (gamma and beta). Default is True.
drop_block (optional): Additional regularization technique that might be applied. Default is None.
"""
def __init__(
self,
num_channels,
eps=1e-5,
affine=True,
drop_block=None,
):
super(nn.LayerNorm, self).__init__()
self._epsilon = eps
self.num_channels = num_channels
if affine:
self.weight = torch.nn.Parameter(torch.randn([num_channels]) * 0.01)
self.bias = torch.nn.Parameter(torch.randn([num_channels]) * 0.01)
torch.nn.init.ones_(self.weight)
torch.nn.init.zeros_(self.bias)
def forward(self, x):
x = F.layer_norm(
x,
[self.num_channels],
weight=self.weight,
bias=self.bias,
eps=self._epsilon,
)
return x
class MBartDecoder(MBartPreTrainedModel):
"""
Transformer decoder consisting of *config.decoder_layers* layers. Each layer is a [`MBartDecoderLayer`]
Args:
config
embed_tokens (nn.Embedding): output embedding
"""
def __init__(self, config, embed_tokens=None):
super().__init__(config)
self.dropout = config.dropout
self.layerdrop = config.decoder_layerdrop
self.padding_idx = config.pad_token_id
self.max_target_positions = config.max_position_embeddings
self.embed_scale = math.sqrt(config.d_model) if config.scale_embedding else 1.0
self.embed_tokens = nn.Embedding(
config.vocab_size, config.d_model, self.padding_idx
)
if embed_tokens is not None:
self.embed_tokens.weight = embed_tokens.weight
self.embed_positions = MBartLearnedPositionalEmbedding(
config.max_position_embeddings,
config.d_model,
)
self.layers = nn.ModuleList(
[MBartDecoderLayer(config) for _ in range(config.decoder_layers)]
)
self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
self.layernorm_embedding = myLayerNorm(config.d_model, affine=True)
self.layer_norm = nn.LayerNorm(config.d_model)
self.gradient_checkpointing = False
# Initialize weights and apply final processing
self.post_init()
self.is_export = config.is_export
def get_input_embeddings(self):
return self.embed_tokens
def set_input_embeddings(self, value):
self.embed_tokens = value
def forward(
self,
input_ids=None,
attention_mask=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
head_mask=None,
cross_attn_head_mask=None,
past_key_values=None,
inputs_embeds=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
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
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
if input_ids is not None and inputs_embeds is not None:
raise ValueError(
"You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time"
)
elif input_ids is not None:
input = input_ids
input_shape = input.shape
input_ids = input_ids.reshape([-1, input_shape[-1]])
elif inputs_embeds is not None:
input_shape = inputs_embeds.shape[:-1]
input = inputs_embeds[:, :, -1]
else:
raise ValueError(
"You have to specify either decoder_input_ids or decoder_inputs_embeds"
)
past_key_values_length = (
past_key_values[0][0].shape[2] if past_key_values is not None else 0
)
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
if self._use_flash_attention_2:
attention_mask = (
attention_mask
if (attention_mask is not None and 0 in attention_mask)
else None
)
else:
attention_mask = _prepare_4d_causal_attention_mask(
attention_mask,
input_shape,
inputs_embeds,
past_key_values_length,
is_export=self.is_export,
)
if encoder_hidden_states is not None and encoder_attention_mask is not None:
if self._use_flash_attention_2:
encoder_attention_mask = (
encoder_attention_mask if 0 in encoder_attention_mask else None
)
else:
encoder_attention_mask = _prepare_4d_attention_mask(
encoder_attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]
)
# embed positions
positions = self.embed_positions(input, past_key_values_length)
hidden_states = inputs_embeds + positions
hidden_states = self.layernorm_embedding(hidden_states)
hidden_states = nn.functional.dropout(
hidden_states, p=self.dropout, training=self.training
)
if self.gradient_checkpointing and self.training:
if use_cache:
print(
"`use_cache=True` is incompatible with gradient checkpointing`. Setting `use_cache=False`..."
)
use_cache = False
all_hidden_states = () if output_hidden_states else None
all_self_attns = () if output_attentions else None
all_cross_attentions = (
() if (output_attentions and encoder_hidden_states is not None) else None
)
next_decoder_cache = () if use_cache else None
for attn_mask, mask_name in zip(
[head_mask, cross_attn_head_mask], ["head_mask", "cross_attn_head_mask"]
):
if attn_mask is not None:
if attn_mask.shape[0] != len(self.layers):
raise ValueError(
f"The `{mask_name}` should be specified for {len(self.layers)} layers, but it is for"
f" {attn_mask.shape[0]}."
)
for idx, decoder_layer in enumerate(self.layers):
if output_hidden_states:
all_hidden_states += (hidden_states,)
if self.training:
dropout_probability = torch.rand([])
if dropout_probability < self.layerdrop:
continue
past_key_value = (
past_key_values[idx] if past_key_values is not None else None
)
if self.gradient_checkpointing and self.training:
layer_outputs = self._gradient_checkpointing_func(
decoder_layer.__call__,
hidden_states,
attention_mask,
encoder_hidden_states,
encoder_attention_mask,
head_mask[idx] if head_mask is not None else None,
(
cross_attn_head_mask[idx]
if cross_attn_head_mask is not None
else None
),
None,
output_attentions,
use_cache,
)
else:
layer_outputs = decoder_layer(
hidden_states,
attention_mask=attention_mask,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
layer_head_mask=(head_mask[idx] if head_mask is not None else None),
cross_attn_layer_head_mask=(
cross_attn_head_mask[idx]
if cross_attn_head_mask is not None
else None
),
past_key_value=past_key_value,
output_attentions=output_attentions,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
if use_cache:
next_decoder_cache += (layer_outputs[3 if output_attentions else 1],)
if output_attentions:
all_self_attns += (layer_outputs[1],)
if encoder_hidden_states is not None:
all_cross_attentions += (layer_outputs[2],)
hidden_states = self.layer_norm(hidden_states)
if output_hidden_states:
all_hidden_states += (hidden_states,)
next_cache = next_decoder_cache if use_cache else None
if not return_dict:
return tuple(
v
for v in [
hidden_states,
next_cache,
all_hidden_states,
all_self_attns,
all_cross_attentions,
]
if v is not None
)
return BaseModelOutputWithPastAndCrossAttentions(
last_hidden_state=hidden_states,
past_key_values=next_cache,
hidden_states=all_hidden_states,
attentions=all_self_attns,
cross_attentions=all_cross_attentions,
)
class MBartDecoderWrapper(MBartPreTrainedModel):
"""
This wrapper class is a helper class to correctly load pretrained checkpoints when the causal language model is
used in combination with the [`EncoderDecoderModel`] framework.
"""
def __init__(self, config):
super().__init__(config)
self.decoder = MBartDecoder(config)
def forward(self, *args, **kwargs):
return self.decoder(*args, **kwargs)
def _in_projection(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
w_q: torch.Tensor,
w_k: torch.Tensor,
w_v: torch.Tensor,
b_q: Optional[torch.Tensor] = None,
b_k: Optional[torch.Tensor] = None,
b_v: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
Eq, Ek, Ev = q.shape[-1], k.shape[-1], v.shape[-1]
assert w_q.shape == (
Eq,
Eq,
), f"expecting query weights shape of {(Eq, Eq)}, but got {w_q.shape}"
assert w_k.shape == (
Eq,
Ek,
), f"expecting key weights shape of {(Eq, Ek)}, but got {w_k.shape}"
assert w_v.shape == (
Eq,
Ev,
), f"expecting value weights shape of {(Eq, Ev)}, but got {w_v.shape}"
assert b_q is None or b_q.shape == (
Eq,
), f"expecting query bias shape of {(Eq,)}, but got {b_q.shape}"
assert b_k is None or b_k.shape == (
Eq,
), f"expecting key bias shape of {(Eq,)}, but got {b_k.shape}"
assert b_v is None or b_v.shape == (
Eq,
), f"expecting value bias shape of {(Eq,)}, but got {b_v.shape}"
return linear(q, w_q.T, b_q), linear(k, w_k.T, b_k), linear(v, w_v.T, b_v)
def _scaled_dot_product_attention(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
attn_mask: Optional[torch.Tensor] = None,
dropout_p: float = 0.0,
) -> Tuple[torch.Tensor, torch.Tensor]:
B, Nt, E = q.shape
q = q / math.sqrt(E)
attn = torch.bmm(q, k.permute([0, 2, 1]))
if attn_mask is not None:
attn += attn_mask
attn = F.softmax(attn, dim=-1)
if dropout_p > 0.0:
attn = F.dropout(attn, p=dropout_p)
output = torch.bmm(attn, v)
return output, attn
def linear(x, w, b, is_transpose):
if is_transpose:
w = w.T
if b is not None:
return torch.matmul(x, w) + b
else:
return torch.matmul(x, w)
def _in_projection_packed(
q: Tensor,
k: Tensor,
v: Tensor,
w: Tensor,
b: Optional[Tensor] = None,
is_export=False,
) -> List[Tensor]:
E = q.shape[-1]
if k is v:
if q is k:
proj = linear(q, w, b, is_transpose=True)
if is_export:
B, D, L = proj.shape
proj = proj.reshape([B, D, 3, E])
proj = (
proj.unsqueeze(0)
.permute([3, 1, 2, 0, 4])
.squeeze(-2)
.contiguous()
)
else:
proj = (
proj.unflatten(-1, (3, E))
.unsqueeze(0)
.permute([3, 1, 2, 0, 4])
.squeeze(-2)
.contiguous()
)
return proj[0], proj[1], proj[2]
else:
w_q, w_k, w_v = w.chunk(3)
if b is None:
b_q = b_k = b_v = None
else:
b_q, b_k, b_v = b.chunk(3)
return linear(q, w_q, b_q), linear(k, w_k, b_k), linear(v, w_v, b_v)
def multi_head_attention_forward(
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
embed_dim_to_check: int,
num_heads: int,
in_proj_weight: torch.Tensor,
in_proj_bias: Optional[torch.Tensor],
bias_k: Optional[torch.Tensor],
bias_v: Optional[torch.Tensor],
add_zero_attn: bool,
dropout_p: float,
out_proj_weight: torch.Tensor,
out_proj_bias: Optional[torch.Tensor],
training: bool = True,
key_padding_mask: Optional[torch.Tensor] = None,
need_weights: bool = True,
attn_mask: Optional[torch.Tensor] = None,
use_separate_proj_weight: bool = False,
q_proj_weight: Optional[torch.Tensor] = None,
k_proj_weight: Optional[torch.Tensor] = None,
v_proj_weight: Optional[torch.Tensor] = None,
static_k: Optional[torch.Tensor] = None,
static_v: Optional[torch.Tensor] = None,
is_export=False,
):
tgt_len, bsz, embed_dim = query.shape
src_len, _, _ = key.shape
if isinstance(embed_dim, torch.Tensor):
head_dim = embed_dim.div(num_heads, rounding_mode="trunc")
else:
head_dim = embed_dim // num_heads
q, k, v = _in_projection_packed(
query, key, value, in_proj_weight, in_proj_bias, is_export
)
if key_padding_mask is not None and key_padding_mask.dtype == torch.uint8:
warnings.warn(
"Byte tensor for key_padding_mask in nn.MultiheadAttention is deprecated. Use bool tensor instead."
)
key_padding_mask = key_padding_mask.to(torch.bool)
if bias_k is not None and bias_v is not None: # False
assert static_k is None, "bias cannot be added to static key."
assert static_v is None, "bias cannot be added to static value."
k = torch.concat([k, bias_k.repeat(1, bsz, 1)])
v = torch.concat([v, bias_v.repeat(1, bsz, 1)])
else:
assert bias_k is None
assert bias_v is None
q = q.reshape([tgt_len, bsz * num_heads, head_dim]).permute([1, 0, 2])
if static_k is None: # True
k = k.reshape([k.shape[0], bsz * num_heads, head_dim]).permute([1, 0, 2])
else:
assert (
static_k.shape[0] == bsz * num_heads
), f"expecting static_k.size(0) of {bsz * num_heads}, but got {static_k.shape[0]}"
assert (
static_k.shape[2] == head_dim
), f"expecting static_k.size(2) of {head_dim}, but got {static_k.shape[2]}"
k = static_k
if static_v is None: # True
v = v.reshape([v.shape[0], bsz * num_heads, head_dim]).transpose([1, 0, 2])
else:
assert (
static_v.shape[0] == bsz * num_heads
), f"expecting static_v.size(0) of {bsz * num_heads}, but got {static_v.shape[0]}"
assert (
static_v.shape[2] == head_dim
), f"expecting static_v.size(2) of {head_dim}, but got {static_v.shape[2]}"
v = static_v
src_len = k.shape[1]
if not training:
dropout_p = 0.0
attn_output, attn_output_weights = _scaled_dot_product_attention(
q, k, v, attn_mask, dropout_p
)
attn_output = attn_output.permute([1, 0, 2]).reshape([tgt_len, bsz, embed_dim])
attn_output = linear(
attn_output, out_proj_weight, out_proj_bias, is_transpose=False
)
if need_weights:
attn_output_weights = attn_output_weights.reshape(
[bsz, num_heads, tgt_len, src_len]
)
return attn_output, attn_output_weights.sum(dim=1) / num_heads
else:
return attn_output, None
class MyMultiheadAttention(nn.Module):
"""
Custom implementation of a multi-head attention layer.
Attributes:
__constants__ (list): List of constant attributes.
bias_k (Optional[paddle.Tensor]): Optional tensor for key bias.
bias_v (Optional[paddle.Tensor]): Optional tensor for value bias.
Args:
embed_dim (int): Total dimension of the model. This is the size of the input feature vectors.
num_heads (int): Number of parallel attention heads. The input dimension must be divisible by the number of heads.
dropout (float, optional): Dropout probability on the attention weights. Default is 0.0.
bias (bool, optional): If True, adds a learnable bias to the output. Default is True.
add_bias_kv (bool, optional): If True, adds bias to the key and value sequences. Default is False.
add_zero_attn (bool, optional): If True, adds a zero attention head. Default is False.
kdim (int, optional): Total number of features for keys. If None, defaults to embed_dim.
vdim (int, optional): Total number of features for values. If None, defaults to embed_dim.
batch_first (bool, optional): If True, the input and output tensors are provided as (batch, seq, feature). Default is False.
device (optional): The device on which the layer's parameters should be initialized. Default is None.
dtype (optional): The data type for the parameters. Default is None.
is_export (bool, optional): If True, the layer is set up for export, potentially changing behavior for compatibility. Default is False.
"""
__constants__ = ["batch_first"]
bias_k: Optional[torch.Tensor]
bias_v: Optional[torch.Tensor]
def __init__(
self,
embed_dim,
num_heads,
dropout=0.0,
bias=True,
add_bias_kv=False,
add_zero_attn=False,
kdim=None,
vdim=None,
batch_first=False,
device=None,
dtype=None,
is_export=False,
) -> None:
super(MyMultiheadAttention, self).__init__()
self.embed_dim = embed_dim
self.kdim = kdim if kdim is not None else embed_dim
self.vdim = vdim if vdim is not None else embed_dim
self._qkv_same_embed_dim = self.kdim == embed_dim and self.vdim == embed_dim
self.num_heads = num_heads
self.dropout = dropout
self.batch_first = batch_first
self.head_dim = embed_dim // num_heads
self.is_export = is_export
assert (
self.head_dim * num_heads == self.embed_dim
), "embed_dim must be divisible by num_heads"
if self._qkv_same_embed_dim is False:
pass
else:
if dtype is None:
dtype = torch.float32
self.in_proj_weight = torch.nn.Parameter(torch.randn(3 * embed_dim, embed_dim) * 0.01)
self.q_proj_weight = None
self.k_proj_weight = None
self.v_proj_weight = None
if bias:
self.in_proj_bias = torch.nn.Parameter(torch.randn(3 * embed_dim, ) * 0.01)
torch.nn.init.zeros_(self.in_proj_bias)
else:
self.in_proj_bias = None
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
if add_bias_kv:
pass
else:
self.bias_k = self.bias_v = None
self.add_zero_attn = add_zero_attn
self._reset_parameters()
def _reset_parameters(self):
if self._qkv_same_embed_dim:
torch.nn.init.xavier_normal_(self.in_proj_weight)
else:
torch.nn.init.xavier_normal_(self.q_proj_weight)
torch.nn.init.xavier_normal_(self.k_proj_weight)
torch.nn.init.xavier_normal_(self.v_proj_weight)
if self.in_proj_bias is not None:
torch.nn.init.zeros_(self.in_proj_bias)
torch.nn.init.zeros_(self.out_proj.bias)
if self.bias_k is not None:
torch.nn.init.xavier_normal_(self.bias_k)
if self.bias_v is not None:
torch.nn.init.xavier_normal_(self.bias_v)
def forward(
self,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
key_padding_mask: Optional[torch.Tensor] = None,
need_weights: bool = True,
attn_mask: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
attn_output, attn_output_weights = multi_head_attention_forward(
query,
key,
value,
self.embed_dim,
self.num_heads,
self.in_proj_weight,
self.in_proj_bias,
self.bias_k,
self.bias_v,
self.add_zero_attn,
self.dropout,
self.out_proj.weight,
self.out_proj.bias,
training=self.training,
key_padding_mask=key_padding_mask,
need_weights=need_weights,
attn_mask=attn_mask,
is_export=self.is_export,
)
return attn_output, attn_output_weights
class LogitsProcessorList(list):
"""
A list of logits processors that can be applied sequentially.
Methods:
__call__(input_ids, scores, **kwargs): Apply all processors to the given inputs.
"""
def __call__(self, input_ids, scores, **kwargs):
for processor in self:
function_args = inspect.signature(processor.__call__).parameters
if len(function_args) > 2:
if not all(arg in kwargs for arg in list(function_args.keys())[2:]):
raise ValueError(
f"Make sure that all the required parameters: {list(function_args.keys())} for "
f"{processor.__class__} are passed to the logits processor."
)
scores = processor(input_ids, scores, **kwargs)
else:
scores = processor(input_ids, scores)
return scores
class ForcedEOSTokenLogitsProcessor(object):
"""
A processor that forces the generation of an end-of-sequence (EOS) token
at a specified position in the sequence.
This is typically used in language generation tasks to ensure that the
generated sequence ends properly when it reaches a certain length.
Args:
max_length (int): The maximum length of the sequence. Forces EOS when this length is reached.
eos_token_id (Union[int, List[int]]): The ID(s) of the EOS token(s) to be forced in the sequence.
"""
def __init__(self, max_length: int, eos_token_id: Union[int, List[int]]):
self.max_length = max_length
if isinstance(eos_token_id, int):
eos_token_id = [eos_token_id]
self.eos_token_id = eos_token_id
def __call__(self, input_ids, scores):
cur_len = input_ids.shape[-1]
scores_processed = scores
if cur_len == self.max_length - 1:
scores_processed = torch.full_like(scores, -math.inf)
scores_processed[:, self.eos_token_id] = 0
return scores_processed
@dataclass
class CausalLMOutputWithCrossAttentions(ModelOutput):
loss = None
logits = None
past_key_values = None
hidden_states = None
attentions = None
cross_attentions = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@dataclass
class CausalLMOutputWithCrossAttentionsAndCounting(ModelOutput):
"""
Base class for causal language model (or autoregressive) outputs.
"""
logits = None
counting = None
past_key_values = None
hidden_states = None
attentions = None
cross_attentions = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class CustomMBartDecoder(MBartDecoder):
"""
A custom MBartDecoder that includes additional processing layers.
This class extends the MBartDecoder by adding a customizable neural network
component called `counting_context_weight`, which applies a series of linear
transformations followed by ReLU activations. This can be used to modify or
enhance the decoder's behavior for specific tasks.
Args:
config: The configuration object containing model parameters.
"""
def __init__(self, config):
super().__init__(config)
hidden_size = config.d_model
self.is_export = config.is_export
self.counting_context_weight = nn.Sequential(
nn.Linear(config.vocab_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, config.d_model),
)
def forward(
self,
input_ids=None,
attention_mask=None,
count_pred=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
head_mask=None,
cross_attn_head_mask=None,
past_key_values=None,
inputs_embeds=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
self.is_export = False if self.training else True
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
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
if input_ids is not None and inputs_embeds is not None:
raise ValueError(
"You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time"
)
elif input_ids is not None:
input = input_ids
input_shape = input.shape
input_ids = input_ids.reshape([-1, input_shape[-1]])
elif inputs_embeds is not None:
input_shape = inputs_embeds.shape[:-1]
input = inputs_embeds[:, :, -1]
else:
raise ValueError(
"You have to specify either decoder_input_ids or decoder_inputs_embeds"
)
past_key_values_length = (
past_key_values[0][0].shape[2] if past_key_values is not None else 0
)
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
if self._use_flash_attention_2:
attention_mask = (
attention_mask
if (attention_mask is not None and 0 in attention_mask)
else None
)
else:
if self.is_export:
attention_mask = _prepare_4d_causal_attention_mask_export(
attention_mask,
input_shape,
inputs_embeds,
past_key_values_length,
is_export=self.is_export,
).to(torch.float32)
else:
attention_mask = _prepare_4d_causal_attention_mask(
attention_mask,
input_shape,
inputs_embeds,
past_key_values_length,
is_export=self.is_export,
)
if encoder_hidden_states is not None and encoder_attention_mask is not None:
if self._use_flash_attention_2:
encoder_attention_mask = (
encoder_attention_mask if 0 in encoder_attention_mask else None
)
else:
encoder_attention_mask = _prepare_4d_attention_mask(
encoder_attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]
)
# embed positions
positions = self.embed_positions(input, past_key_values_length)
hidden_states = inputs_embeds + positions
# TODO: add counting context weight to hidden_states
if count_pred is not None:
count_context_weight = self.counting_context_weight(count_pred)
hidden_states = hidden_states + 0.5 * count_context_weight.unsqueeze(1)
hidden_states = self.layernorm_embedding(hidden_states)
hidden_states = nn.functional.dropout(
hidden_states, p=self.dropout, training=self.training
)
if self.gradient_checkpointing and self.training:
if use_cache:
print(
"`use_cache=True` is incompatible with gradient checkpointing`. Setting `use_cache=False`..."
)
use_cache = False
# decoder layers
all_hidden_states = () if output_hidden_states else None
all_self_attns = () if output_attentions else None
all_cross_attentions = (
() if (output_attentions and encoder_hidden_states is not None) else None
)
next_decoder_cache = () if use_cache else None
# check if head_mask/cross_attn_head_mask has a correct number of layers specified if desired
for attn_mask, mask_name in zip(
[head_mask, cross_attn_head_mask], ["head_mask", "cross_attn_head_mask"]
):
if attn_mask is not None:
if attn_mask.size()[0] != len(self.layers):
raise ValueError(
f"The `{mask_name}` should be specified for {len(self.layers)} layers, but it is for"
f" {attn_mask.size()[0]}."
)
for idx, decoder_layer in enumerate(self.layers):
if output_hidden_states:
all_hidden_states += (hidden_states,)
if self.training:
dropout_probability = torch.rand()
if dropout_probability < self.layerdrop:
continue
past_key_value = (
past_key_values[idx] if past_key_values is not None else None
)
if self.gradient_checkpointing and self.training:
layer_outputs = self._gradient_checkpointing_func(
decoder_layer.__call__,
hidden_states,
attention_mask,
encoder_hidden_states,
encoder_attention_mask,
head_mask[idx] if head_mask is not None else None,
(
cross_attn_head_mask[idx]
if cross_attn_head_mask is not None
else None
),
None,
output_attentions,
use_cache,
)
else:
layer_outputs = decoder_layer(
hidden_states,
attention_mask=attention_mask,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
layer_head_mask=(head_mask[idx] if head_mask is not None else None),
cross_attn_layer_head_mask=(
cross_attn_head_mask[idx]
if cross_attn_head_mask is not None
else None
),
past_key_value=past_key_value,
output_attentions=output_attentions,
use_cache=use_cache,
)
hidden_states = layer_outputs[0]
if self.is_export:
next_decoder_cache += (layer_outputs[3 if output_attentions else 1],)
else:
if use_cache:
next_decoder_cache += (
layer_outputs[3 if output_attentions else 1],
)
if output_attentions:
all_self_attns += (layer_outputs[1],)
if encoder_hidden_states is not None:
all_cross_attentions += (layer_outputs[2],)
hidden_states = self.layer_norm(hidden_states)
if output_hidden_states:
all_hidden_states += (hidden_states,)
if self.is_export:
next_cache = next_decoder_cache
else:
next_cache = next_decoder_cache if use_cache else None
if not self.is_export:
if not return_dict:
return tuple(
v
for v in [
hidden_states,
next_cache,
all_hidden_states,
all_self_attns,
all_cross_attentions,
]
if v is not None
)
return BaseModelOutputWithPastAndCrossAttentions(
last_hidden_state=hidden_states,
past_key_values=next_cache,
hidden_states=all_hidden_states,
attentions=all_self_attns,
cross_attentions=all_cross_attentions,
)
class SelfAttentionBlock(nn.Module):
"""
A self-attention block that implements multi-head self-attention
followed by a feed-forward network, typically used in transformer architectures.
Args:
embed_size (int): The size of the embedding vector.
num_heads (int): The number of attention heads.
is_export (bool): Flag indicating whether to configure the layer for export.
"""
def __init__(self, embed_size, num_heads, is_export):
super(SelfAttentionBlock, self).__init__()
self.self_attention = MyMultiheadAttention(
embed_dim=embed_size, num_heads=num_heads, is_export=is_export
)
self.norm = nn.LayerNorm(embed_size)
def forward(self, x):
attn_output, _ = self.self_attention(x, x, x)
x = self.norm(attn_output + x)
return x
class SeqCountingDecoder(nn.Module):
"""
A custom sequence counting decoder that incorporates multi-head attention layers
and feed-forward networks to process sequences, potentially for latex code counting .
Args:
in_features (int): The number of input features.
out_features (int): The number of output features.
num_heads (int): The number of attention heads. Defaults to 8.
num_layers (int): The number of attention layers. Defaults to 4.
is_export (bool): Flag indicating whether to configure the layer for export.
"""
def __init__(
self, in_features, out_features, num_heads=8, num_layers=4, is_export=False
):
super(SeqCountingDecoder, self).__init__()
self.attention_blocks = nn.ModuleList(
[
SelfAttentionBlock(
embed_size=in_features, num_heads=num_heads, is_export=is_export
)
for i in range(num_layers)
]
)
self.fc1 = nn.Linear(in_features, in_features // 2)
self.relu = nn.ReLU()
self.global_avg_pool = nn.AdaptiveAvgPool1d(1)
self.fc2 = nn.Linear(in_features // 2, out_features)
def forward(self, x):
for block in self.attention_blocks:
x = block(x)
x = self.fc1(x)
x = self.relu(x)
x = x.transpose([0, 2, 1])
x = self.global_avg_pool(x)
x = x.squeeze(-1)
x = self.fc2(x)
return x
class CustomMBartForCausalLM(MBartForCausalLM):
"""
Custom MBart model for causal language modeling with a custom decoder.
This class extends the MBartForCausalLM by replacing its decoder with a
custom decoder, allowing for additional flexibility and features in the
decoding process.
Args:
config: The configuration object containing model parameters.
length_aware (bool): A flag to enable or configure length-aware mechanisms.
"""
def __init__(self, config, length_aware=True):
super().__init__(config)
self.model.decoder = CustomMBartDecoder(config)
self.counting_decoder = SeqCountingDecoder(
config.d_model, config.vocab_size, is_export=config.is_export
)
self.length_aware = length_aware
def forward(
self,
input_ids=None,
attention_mask=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
head_mask=None,
cross_attn_head_mask=None,
past_key_values=None,
inputs_embeds=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
count_gt=None,
):
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
)
if self.length_aware:
count_pred = self.counting_decoder(encoder_hidden_states)
else:
count_pred = None
outputs = self.model.decoder(
input_ids=input_ids,
attention_mask=attention_mask,
count_pred=count_pred,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
head_mask=head_mask,
cross_attn_head_mask=cross_attn_head_mask,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
logits = self.lm_head(outputs[0])
return CausalLMOutputWithCrossAttentionsAndCounting(
logits=logits,
counting=count_pred,
past_key_values=outputs.past_key_values,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
cross_attentions=outputs.cross_attentions,
)
class UniMERNetHead(nn.Module):
"""Implementation of UniMERNetHead decoder.
Args:
max_new_tokens (int): Maximum number of new tokens to generate.
decoder_start_token_id (int): ID of the token that starts the decoding.
temperature (float): Sampling temperature for generation.
do_sample (bool): Whether to use sampling; if False, uses greedy decoding.
top_p (float): Top-p (nucleus) sampling parameter.
in_channels (int): Number of input channels/features.
encoder_hidden_size (int): Hidden size of the encoder.
decoder_hidden_size (int): Hidden size of the decoder.
decoder_ffn_dim (int): Dimension of the decoder's feed-forward network.
decoder_layers (int): Number of layers in the decoder.
is_export (bool): Flag indicating if the model is being prepared for export.
length_aware (bool): Flag to enable length-aware mechanisms.
"""
def __init__(
self,
max_new_tokens=1536,
decoder_start_token_id=0,
temperature=0.2,
do_sample=False,
top_p=0.95,
in_channels=1024,
encoder_hidden_size=1024,
decoder_hidden_size=1024,
decoder_ffn_dim=4096,
decoder_layers=8,
is_export=False,
length_aware=True,
):
super().__init__()
mbart_config_dict = {
"activation_dropout": 0.0,
"activation_function": "gelu",
"add_cross_attention": True,
"add_final_layer_norm": True,
"attention_dropout": 0.0,
"bos_token_id": 0,
"classifier_dropout": 0.0,
"d_model": decoder_hidden_size,
"decoder_attention_heads": 16,
"decoder_ffn_dim": decoder_ffn_dim,
"decoder_layerdrop": 0.0,
"decoder_layers": decoder_layers,
"dropout": 0.1,
"encoder_attention_heads": 16,
"encoder_ffn_dim": 4096,
"encoder_layerdrop": 0.0,
"encoder_layers": 12,
"eos_token_id": 2,
"forced_eos_token_id": 2,
"init_std": 0.02,
"is_decoder": True,
"is_encoder_decoder": False,
"output_hidden_states": False,
"max_position_embeddings": max_new_tokens,
"model_type": "mbart",
"num_hidden_layers": 12,
"pad_token_id": 1,
"scale_embedding": True,
"tie_word_embeddings": False,
"transformers_version": "4.40.0",
"use_cache": True,
"use_return_dict": True,
"vocab_size": 50000,
"_attn_implementation": "eager",
"hidden_size": decoder_hidden_size,
"is_export": is_export,
}
self.max_new_tokens = max_new_tokens
self.decoder_start_token_id = decoder_start_token_id
self.temperature = temperature
self.do_sample = do_sample
self.top_p = top_p
self.max_seq_len = max_new_tokens
self.config_decoder = MBartConfig(**mbart_config_dict)
self.encoder_hidden_size = encoder_hidden_size
self.is_export = self.config_decoder.is_export
self.decoder = CustomMBartForCausalLM(
self.config_decoder, length_aware=length_aware
)
if self.config_decoder.hidden_size != self.encoder_hidden_size:
self.enc_to_dec_proj = nn.Linear(
self.encoder_hidden_size, self.config_decoder.hidden_size
)
generation_config = {
"max_length": 1537,
"forced_eos_token_id": 2,
}
self.eos_token_id = generation_config["forced_eos_token_id"]
self.pad_token_id = self.config_decoder.pad_token_id
self.logits_processor = LogitsProcessorList()
self.logits_processor.append(
ForcedEOSTokenLogitsProcessor(
generation_config["max_length"],
generation_config["forced_eos_token_id"],
)
)
def _get_decoder_start_token_id(
self, decoder_start_token_id=None, bos_token_id=None
) -> int:
decoder_start_token_id = (
decoder_start_token_id
if decoder_start_token_id is not None
else self.generation_config.decoder_start_token_id
)
bos_token_id = (
bos_token_id
if bos_token_id is not None
else self.generation_config.bos_token_id
)
if decoder_start_token_id is not None:
return decoder_start_token_id
elif bos_token_id is not None:
return bos_token_id
raise ValueError(
"`decoder_start_token_id` or `bos_token_id` has to be defined for encoder-decoder generation."
)
def _prepare_decoder_input_ids_for_generation(
self,
batch_size,
model_kwargs,
decoder_start_token_id=None,
bos_token_id=None,
):
if model_kwargs is not None and "decoder_input_ids" in model_kwargs:
decoder_input_ids = model_kwargs.pop("decoder_input_ids")
elif "input_ids" in model_kwargs:
decoder_input_ids = model_kwargs.pop("input_ids")
else:
decoder_input_ids = None
decoder_start_token_id = self._get_decoder_start_token_id(
decoder_start_token_id, bos_token_id
)
if isinstance(decoder_start_token_id, list):
if len(decoder_start_token_id) != batch_size:
raise ValueError(
f"`decoder_start_token_id` expected to have length {batch_size} but got {len(decoder_start_token_id)}"
)
decoder_input_ids_start = torch.LongTensor(decoder_start_token_id)
decoder_input_ids_start = decoder_input_ids_start.view(-1, 1)
else:
decoder_input_ids_start = (
torch.ones(
(batch_size, 1),
dtype=torch.int64,
)
* decoder_start_token_id
)
if decoder_input_ids is None:
decoder_input_ids = decoder_input_ids_start
elif (
self.config.model_type == "vision-encoder-decoder"
and "donut" in self.name_or_path.lower()
):
pass
elif self.config.model_type in ["whisper"]:
pass
elif (
isinstance(decoder_start_token_id, int)
and (decoder_input_ids[:, 0] != decoder_start_token_id).all().item()
) or (
isinstance(decoder_start_token_id, torch.Tensor)
and (decoder_input_ids[:, 0] != decoder_start_token_id[:, 0]).all().item()
):
decoder_input_ids = torch.concat(
[decoder_input_ids_start, decoder_input_ids], dim=-1
)
if "decoder_attention_mask" in model_kwargs:
decoder_attention_mask = model_kwargs["decoder_attention_mask"]
decoder_attention_mask = torch.cat(
(
torch.ones_like(decoder_attention_mask)[:, :1],
decoder_attention_mask,
),
dim=-1,
)
model_kwargs["decoder_attention_mask"] = decoder_attention_mask
return decoder_input_ids, model_kwargs
def prepare_inputs_for_generation_mbart(
self,
input_ids,
past_key_values=None,
attention_mask=None,
use_cache=None,
**kwargs,
):
if attention_mask is None:
attention_mask = torch.ones(input_ids.shape)
if past_key_values:
past_length = past_key_values[0][0].shape[2]
if input_ids.shape[1] > past_length:
remove_prefix_length = past_length
else:
remove_prefix_length = input_ids.shape[1] - 1
input_ids = input_ids[:, remove_prefix_length:]
return {
"input_ids": input_ids,
"attention_mask": attention_mask,
"past_key_values": past_key_values,
"use_cache": use_cache,
}
def prepare_inputs_for_generation(
self,
input_ids,
past_key_values=None,
attention_mask=None,
use_cache=None,
encoder_outputs=None,
**kwargs,
):
decoder_inputs = self.prepare_inputs_for_generation_mbart(
input_ids, past_key_values=past_key_values
)
decoder_attention_mask = (
decoder_inputs["attention_mask"]
if "attention_mask" in decoder_inputs
else None
)
input_dict = {
"attention_mask": attention_mask,
"decoder_attention_mask": decoder_attention_mask,
"decoder_input_ids": decoder_inputs["input_ids"],
"encoder_outputs": encoder_outputs,
"past_key_values": decoder_inputs["past_key_values"],
"use_cache": use_cache,
}
return input_dict
def prepare_inputs_for_generation_export(
self,
past_key_values=None,
attention_mask=None,
use_cache=None,
encoder_outputs=None,
**kwargs,
):
input_dict = {
"decoder_attention_mask": None,
"use_cache": use_cache,
}
return input_dict
def _extract_past_from_model_output(
self, outputs: ModelOutput, standardize_cache_format: bool = False
):
past_key_values = None
if "past_key_values" in outputs:
past_key_values = outputs.past_key_values
elif "mems" in outputs:
past_key_values = outputs.mems
elif "past_buckets_states" in outputs:
past_key_values = outputs.past_buckets_states
return past_key_values
def _update_model_kwargs_for_generation(
self,
outputs: ModelOutput,
model_kwargs: Dict[str, Any],
is_encoder_decoder: bool = False,
standardize_cache_format: bool = False,
) -> Dict[str, Any]:
model_kwargs["past_key_values"] = self._extract_past_from_model_output(
outputs, standardize_cache_format=standardize_cache_format
)
if getattr(outputs, "state", None) is not None:
model_kwargs["state"] = outputs.state
if "token_type_ids" in model_kwargs:
token_type_ids = model_kwargs["token_type_ids"]
model_kwargs["token_type_ids"] = torch.concat(
[token_type_ids, token_type_ids[:, -1].unsqueeze(-1)], dim=-1
)
if not is_encoder_decoder:
if "attention_mask" in model_kwargs:
attention_mask = model_kwargs["attention_mask"]
model_kwargs["attention_mask"] = torch.concat(
[
attention_mask,
attention_mask.new_ones((attention_mask.shape[0], 1)),
],
dim=-1,
)
else:
if "decoder_attention_mask" in model_kwargs:
decoder_attention_mask = model_kwargs["decoder_attention_mask"]
model_kwargs["decoder_attention_mask"] = torch.concat(
[
decoder_attention_mask,
decoder_attention_mask.new_ones(
(decoder_attention_mask.shape[0], 1)
),
],
dim=-1,
)
if (
"cache_position" in model_kwargs
and model_kwargs["cache_position"] is not None
):
model_kwargs["cache_position"] = model_kwargs["cache_position"][-1:] + 1
return model_kwargs
def stopping_criteria(self, input_ids):
if self.is_export:
return input_ids[:, -1] == torch.Tensor([self.eos_token_id])
is_done = torch.isin(input_ids[:, -1], torch.Tensor([self.eos_token_id]))
return is_done
def generate_single_iter(
self,
decoder_input_ids=None,
decoder_attention_mask=None,
encoder_outputs=None,
past_key_values=None,
decoder_inputs_embeds=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
**kwargs,
):
encoder_hidden_states = encoder_outputs[0]
if self.config_decoder.hidden_size != self.encoder_hidden_size:
encoder_hidden_states = self.enc_to_dec_proj(encoder_hidden_states)
kwargs_decoder = {}
decoder_outputs = self.decoder(
input_ids=decoder_input_ids,
attention_mask=decoder_attention_mask,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=None,
inputs_embeds=None,
output_attentions=False,
output_hidden_states=output_hidden_states,
use_cache=use_cache,
past_key_values=past_key_values,
return_dict=return_dict,
**kwargs_decoder,
)
return Seq2SeqLMOutput(
loss=None,
logits=decoder_outputs.logits,
past_key_values=decoder_outputs.past_key_values,
decoder_hidden_states=decoder_outputs.hidden_states,
decoder_attentions=decoder_outputs.attentions,
cross_attentions=decoder_outputs.cross_attentions,
encoder_last_hidden_state=encoder_outputs.last_hidden_state,
encoder_hidden_states=encoder_outputs.hidden_states,
encoder_attentions=encoder_outputs.attentions,
)
@torch.no_grad()
def generate(
self,
model_kwargs,
):
"""
Generate sequences using the UniMERNetHead for inference tasks.
Args:
model_kwargs (dict): A dictionary of model configurations and inputs, which typically include:
- encoder_outputs: Outputs from the encoder.
- use_cache: Boolean flag to indicate if caching should be used.
- output_attentions: Boolean flag for outputting attention scores.
- output_hidden_states: Boolean flag for outputting hidden states.
Returns:
A tensor containing the generated sequences.
"""
batch_size = model_kwargs["encoder_outputs"]["last_hidden_state"].shape[0]
generation_config = {
"decoder_start_token_id": 0,
"bos_token_id": 0,
}
input_ids, model_kwargs = self._prepare_decoder_input_ids_for_generation(
batch_size=batch_size,
model_kwargs=model_kwargs,
decoder_start_token_id=generation_config["decoder_start_token_id"],
bos_token_id=generation_config["bos_token_id"],
)
model_kwargs["key use_cache"] = True
batch_size, cur_len = input_ids.shape
if "inputs_embeds" in model_kwargs:
cur_len = model_kwargs["inputs_embeds"].shape[1]
model_kwargs["cache_position"] = torch.arange(cur_len)
pad_token_id = self.pad_token_id
eos_token_id = [self.eos_token_id]
eos_token = self.eos_token_id
unfinished_sequences = torch.ones(batch_size, dtype=torch.int64)
for idx in range(self.max_seq_len):
model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
outputs = self.generate_single_iter(
**model_inputs,
return_dict=True,
output_attentions=False,
output_hidden_states=False,
)
next_token_logits = outputs.logits[:, -1, :]
next_tokens_scores = self.logits_processor(input_ids, next_token_logits)
next_tokens = torch.argmax(next_tokens_scores, dim=-1)
if eos_token_id is not None:
if pad_token_id is None:
raise ValueError(
"If `eos_token_id` is defined, make sure that `pad_token_id` is defined."
)
next_tokens = next_tokens * unfinished_sequences + pad_token_id * (
1 - unfinished_sequences
)
input_ids = torch.concat([input_ids, next_tokens[:, None]], dim=-1)
model_kwargs = self._update_model_kwargs_for_generation(
outputs,
model_kwargs,
is_encoder_decoder=self.config_decoder.is_encoder_decoder,
)
unfinished_sequences = unfinished_sequences & ~self.stopping_criteria(
input_ids
).to(torch.int64)
if (
eos_token is not None
and (
torch.cumsum((input_ids == eos_token).to(torch.int64), 1)[:, -1]
>= 1
).all()
):
break
return input_ids
@torch.no_grad()
def generate_export(
self,
encoder_outputs,
model_kwargs,
):
batch_size = encoder_outputs["last_hidden_state"].shape[0]
generation_config = {
"decoder_start_token_id": 0,
"bos_token_id": 0,
}
input_ids, model_kwargs = self._prepare_decoder_input_ids_for_generation(
batch_size=batch_size,
model_kwargs=model_kwargs,
decoder_start_token_id=generation_config["decoder_start_token_id"],
bos_token_id=generation_config["bos_token_id"],
)
input_ids = input_ids.reshape([-1, 1])
decoder_input_ids = input_ids
model_kwargs["key use_cache"] = True
batch_size, cur_len = input_ids.shape
if "inputs_embeds" in model_kwargs:
cur_len = model_kwargs["inputs_embeds"].shape[1]
cache_position = torch.arange(cur_len)
pad_token_id = self.pad_token_id
eos_token_id = [self.eos_token_id]
eos_token = self.eos_token_id
unfinished_sequences = torch.ones([batch_size], dtype=torch.int64)
i_idx = torch.full([], 0)
past_key_values = []
for i in range(8):
init_arr = torch.zeros([batch_size, 16, 0, 64])
cache = (init_arr, init_arr, init_arr, init_arr)
past_key_values.append(cache)
idx = 0
while i_idx < torch.Tensor(self.max_seq_len):
model_inputs = self.prepare_inputs_for_generation_export(
past_key_values=past_key_values, **model_kwargs
)
decoder_attention_mask = model_inputs["decoder_attention_mask"]
decoder_attention_mask = torch.ones(input_ids.shape)
outputs = self.generate_single_iter(
decoder_input_ids=decoder_input_ids,
decoder_attention_mask=decoder_attention_mask,
encoder_outputs=encoder_outputs,
past_key_values=past_key_values,
return_dict=True,
output_attentions=False,
output_hidden_states=False,
)
next_token_logits = outputs.logits[:, -1, :]
next_tokens_scores = self.logits_processor(input_ids, next_token_logits)
next_tokens = torch.argmax(next_tokens_scores, dim=-1)
if eos_token_id is not None:
next_tokens = next_tokens * unfinished_sequences + pad_token_id * (
1 - unfinished_sequences
)
input_ids = torch.concat([input_ids, next_tokens.unsqueeze(1)], dim=-1)
past_length = past_key_values[0][0].shape[2]
decoder_input_ids = next_tokens.unsqueeze(1)
past_key_values = outputs.past_key_values
cache_position = cache_position[-1:] + 1
unfinished_sequences = unfinished_sequences & ~self.stopping_criteria(
input_ids
).to(torch.int64)
if (
eos_token is not None
and (
torch.cumsum((input_ids == eos_token).to(torch.int64), 1)[:, -1]
>= 1
).all()
):
break
i_idx += 1
return input_ids
def forwad_train(
self,
encoder_outputs,
decoder_input_ids,
decoder_attention_mask,
past_key_values=None,
decoder_inputs_embeds=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
**kwargs,
):
"""
Training for the UniMERNetHead.
Args:
encoder_outputs: Outputs from the encoder, used as input to the decoder.
decoder_input_ids: Input IDs for the decoder.
decoder_attention_mask: Attention mask for the decoder inputs.
past_key_values: Cached key/values for faster decoding.
decoder_inputs_embeds: Optional embeddings for the decoder inputs.
labels: Target labels for calculating loss.
use_cache: Whether to use cache during decoding.
output_attentions: Whether to return attention scores.
output_hidden_states: Whether to return hidden states.
return_dict: Whether to return a dictionary of outputs.
**kwargs: Additional keyword arguments.
Returns:
logits: The raw, unnormalized predictions from the model.
count_pred: Optional prediction related to sequence length or other counts.
masked_labels: The labels used during training, possibly masked.
"""
labels = decoder_input_ids * 1
labels = labels.masked_fill_(labels == self.pad_token_id, -100)
input_decoder_input_ids = decoder_input_ids[:, :-1]
input_decoder_attention_mask = decoder_attention_mask[:, :-1]
encoder_hidden_states = encoder_outputs[0]
if self.config_decoder.hidden_size != self.encoder_hidden_size:
encoder_hidden_states = self.enc_to_dec_proj(encoder_hidden_states)
kwargs_decoder = {}
decoder_outputs = self.decoder(
input_ids=input_decoder_input_ids,
attention_mask=input_decoder_attention_mask,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=None,
inputs_embeds=None,
output_attentions=False,
output_hidden_states=output_hidden_states,
use_cache=use_cache,
past_key_values=past_key_values,
return_dict=return_dict,
**kwargs_decoder,
)
logits = decoder_outputs.logits
count_pred = decoder_outputs.counting
return logits, count_pred, labels
def forward(self, inputs, targets=None):
"""
Forward pass for the UniMERNetHead, handling both training and inference.
Args:
inputs: The input data, which can vary based on training or inference.
targets: The target labels, used only during training.
Returns:
During inference: Returns predicted latex code.
During training: Returns logits, predicted counts, and masked labels.
"""
self.is_export = False if self.training else True
if not self.training:
encoder_outputs = inputs
if self.is_export:
model_kwargs = {
"output_attentions": False,
"output_hidden_states": False,
"use_cache": True,
}
word_pred = self.generate_export(encoder_outputs, model_kwargs)
else:
model_kwargs = {
"output_attentions": False,
"output_hidden_states": False,
"use_cache": True,
"encoder_outputs": encoder_outputs,
}
word_pred = self.generate(model_kwargs)
return word_pred
encoder_outputs, tgt_seq, mask = inputs
logits, count_pred, masked_labels = self.forwad_train(
encoder_outputs, tgt_seq, mask
)
return logits, count_pred, masked_labels