Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

third_party_ggml.py 269 KB

You have to be logged in to leave a comment. Sign In
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
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
  1. """This module is the core of the ggml-python library, it exposes a low-level [ctypes](https://docs.python.org/3/library/ctypes.html)-based interface for ggml.
  2. Structures and functions in the `ggml.ggml` module map directly to the original ggml C library and
  3. they operate at a fairly low level.
  4. No additional runtime checks checks are performed nor is memory management handled automatically.
  5. You've been warned :).
  6. With that in mind here are some useful things to keep in mind
  7. - Functions accept both ctypes types (c_int, c_bool, c_float, etc.) and Python types (int, bool, float, etc.) as parameters.
  8. - Functions return Python types for simple values (int, bool, float, etc.) and ctypes types for complex values ([ggml_context_p][ggml.ggml_context_p], [ggml_tensor_p][ggml.ggml_tensor_p], etc.).
  9. - Memory management is the responsibility of the user. The user must call [ggml.ggml_free][] on the context after calling [ggml.ggml_init][].
  10. Example
  11. ```python
  12. import ggml
  13. import ctypes
  14. # Allocate a new context with 16 MB of memory
  15. params = ggml.ggml_init_params(mem_size=16 * 1024 * 1024, mem_buffer=None)
  16. ctx = ggml.ggml_init(params=params)
  17. # Instantiate tensors
  18. x = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  19. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  20. b = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  21. # Use ggml operations to build a computational graph
  22. x2 = ggml.ggml_mul(ctx, x, x)
  23. f = ggml.ggml_add(ctx, ggml.ggml_mul(ctx, a, x2), b)
  24. gf = ggml.ggml_new_graph(ctx)
  25. ggml.ggml_build_forward_expand(gf, f)
  26. # Set the input values
  27. ggml.ggml_set_f32(x, 2.0)
  28. ggml.ggml_set_f32(a, 3.0)
  29. ggml.ggml_set_f32(b, 4.0)
  30. # Compute the graph
  31. ggml.ggml_graph_compute_with_ctx(ctx, gf, 1)
  32. # Get the output value
  33. output = ggml.ggml_get_f32_1d(f, 0)
  34. assert output == 16.0
  35. # Free the context
  36. ggml.ggml_free(ctx)
  37. ```
  38. """
  39. import os
  40. import sys
  41. import ctypes
  42. import pathlib
  43. import importlib.resources
  44. from pathlib import Path
  45. from typing import List, Optional, Sequence, Union
  46. from typing_extensions import TypeAlias
  47. # Load the library
  48. def load_shared_library(base_path: Path, lib_base_name: str):
  49. # Construct the paths to the possible shared library names
  50. # Searching for the library in the current directory under the name "libggml" (default name
  51. # for ggml) and "ggml" (default name for this repo)
  52. lib_names: List[str] = [
  53. f"lib{lib_base_name}.so",
  54. f"lib{lib_base_name}.dylib",
  55. f"{lib_base_name}.dll",
  56. ]
  57. cdll_args = dict() # type: ignore
  58. # Add the library directory to the DLL search path on Windows (if needed)
  59. if sys.platform == "win32" and sys.version_info >= (3, 8):
  60. os.add_dll_directory(str(base_path))
  61. cdll_args["winmode"] = 0
  62. for lib_name in lib_names:
  63. # Try to load the shared library, handling potential errors
  64. path = base_path / lib_name
  65. if not path.exists():
  66. continue
  67. try:
  68. return ctypes.CDLL(str(path), **cdll_args)
  69. except Exception as e:
  70. raise RuntimeError(f"Failed to load shared library '{path}': {e}")
  71. raise FileNotFoundError(
  72. f"Shared library with base name '{lib_base_name}' not found in {base_path}"
  73. )
  74. base_path = pathlib.Path(__file__).parent.resolve() / "build/examples/unity"
  75. lib_base_name = "fairseq2_cpp"
  76. lib = load_shared_library(base_path, lib_base_name)
  77. #####################################################
  78. # GGML Utility Types
  79. #####################################################
  80. CFloatArray: TypeAlias = "ctypes.Array[ctypes.c_float]"
  81. CInt64Array: TypeAlias = "ctypes.Array[ctypes.c_int64]"
  82. CIntPointer: TypeAlias = "ctypes._Pointer[ctypes.c_int]" # type: ignore
  83. CCharPointer: TypeAlias = "ctypes._Pointer[ctypes.c_char]" # type: ignore
  84. #####################################################
  85. # GGML API
  86. # source: ggml.h
  87. #####################################################
  88. # define GGML_FILE_MAGIC 0x67676d6c // "ggml"
  89. # define GGML_FILE_VERSION 1
  90. GGML_FILE_MAGIC = 0x67676D6C
  91. GGML_FILE_VERSION = 1
  92. # define GGML_QNT_VERSION 2 // bump this on quantization format changes
  93. # define GGML_QNT_VERSION_FACTOR 1000 // do not change this
  94. GGML_QNT_VERSION = 2
  95. GGML_QNT_VERSION_FACTOR = 1000
  96. # define GGML_MAX_DIMS 4
  97. # define GGML_MAX_PARAMS 2048
  98. # define GGML_MAX_CONTEXTS 64
  99. # define GGML_MAX_SRC 10
  100. # define GGML_MAX_NAME 64
  101. # define GGML_MAX_OP_PARAMS 64
  102. # define GGML_DEFAULT_N_THREADS 4
  103. # define GGML_DEFAULT_GRAPH_SIZE 2048
  104. GGML_MAX_DIMS = 4
  105. GGML_MAX_PARAMS = 2048
  106. GGML_MAX_CONTEXTS = 64
  107. GGML_MAX_SRC = 10
  108. GGML_MAX_NAME = 64
  109. GGML_MAX_OP_PARAMS = 64
  110. GGML_DEFAULT_N_THREADS = 4
  111. GGML_DEFAULT_GRAPH_SIZE = 2048
  112. # #if UINTPTR_MAX == 0XFFFFFFFF
  113. # #define GGML_MEMALIGN 4
  114. # #else
  115. # # define GGML_MEMALIGN 16
  116. # #endif
  117. GGML_MEMALIGN = (
  118. 16 if ctypes.sizeof(ctypes.c_void_p) == 4 else 32
  119. ) # FIXME: Check if this is correct
  120. # #define GGML_EXIT_SUCCESS 0
  121. GGML_EXIT_SUCCESS = 0
  122. # #define GGML_EXIT_ABORTED 1
  123. GGML_EXIT_ABORTED = 1
  124. # define GGUF_MAGIC "GGUF"
  125. GGUF_MAGIC = "GGUF"
  126. # define GGUF_VERSION 3
  127. GGUF_VERSION = 3
  128. # #define GGUF_DEFAULT_ALIGNMENT 32
  129. GGUF_DEFAULT_ALIGNMENT = 32
  130. # TODO: Check if this is correct
  131. # typedef uint16_t ggml_fp16_t;
  132. ggml_fp16_t = ctypes.c_uint16
  133. CFP16Array: TypeAlias = "ctypes.Array[ggml_fp16_t]"
  134. # GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x);
  135. def ggml_fp16_to_fp32(x: ggml_fp16_t) -> float:
  136. return lib.ggml_fp16_to_fp32(x)
  137. lib.ggml_fp16_to_fp32.argtypes = [ggml_fp16_t]
  138. lib.ggml_fp16_to_fp32.restype = ctypes.c_float
  139. # GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x);
  140. def ggml_fp32_to_fp16(x: ctypes.c_float) -> int:
  141. return lib.ggml_fp32_to_fp16(x)
  142. lib.ggml_fp32_to_fp16.argtypes = [ctypes.c_float]
  143. lib.ggml_fp32_to_fp16.restype = ggml_fp16_t
  144. # GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, size_t n);
  145. def ggml_fp16_to_fp32_row(
  146. x: CFP16Array,
  147. y: CFloatArray,
  148. n: Union[ctypes.c_int, int],
  149. ) -> None:
  150. return lib.ggml_fp16_to_fp32_row(x, y, n)
  151. lib.ggml_fp16_to_fp32_row.argtypes = [
  152. ctypes.POINTER(ggml_fp16_t),
  153. ctypes.POINTER(ctypes.c_float),
  154. ctypes.c_int,
  155. ]
  156. lib.ggml_fp16_to_fp32_row.restype = None
  157. # GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, size_t n);
  158. def ggml_fp32_to_fp16_row(
  159. x: CFloatArray,
  160. y: CFP16Array,
  161. n: Union[ctypes.c_int, int],
  162. ) -> None:
  163. return lib.ggml_fp32_to_fp16_row(x, y, n)
  164. lib.ggml_fp32_to_fp16_row.argtypes = [
  165. ctypes.POINTER(ctypes.c_float),
  166. ctypes.POINTER(ggml_fp16_t),
  167. ctypes.c_int,
  168. ]
  169. lib.ggml_fp32_to_fp16_row.restype = None
  170. # struct ggml_context;
  171. ggml_context_p = ctypes.c_void_p
  172. """Opaque pointer to a ggml_context.
  173. ggml_context structs are not accessed directly instead they must be created using [ggml_init](ggml.ggml_init) and freed using [ggml_free](ggml.ggml_free)."""
  174. # enum ggml_type {
  175. # GGML_TYPE_F32 = 0,
  176. # GGML_TYPE_F16 = 1,
  177. # GGML_TYPE_Q4_0 = 2,
  178. # GGML_TYPE_Q4_1 = 3,
  179. # // GGML_TYPE_Q4_2 = 4, support has been removed
  180. # // GGML_TYPE_Q4_3 (5) support has been removed
  181. # GGML_TYPE_Q5_0 = 6,
  182. # GGML_TYPE_Q5_1 = 7,
  183. # GGML_TYPE_Q8_0 = 8,
  184. # GGML_TYPE_Q8_1 = 9,
  185. # GGML_TYPE_Q2_K = 10,
  186. # GGML_TYPE_Q3_K = 11,
  187. # GGML_TYPE_Q4_K = 12,
  188. # GGML_TYPE_Q5_K = 13,
  189. # GGML_TYPE_Q6_K = 14,
  190. # GGML_TYPE_Q8_K = 15,
  191. # GGML_TYPE_I8,
  192. # GGML_TYPE_I16,
  193. # GGML_TYPE_I32,
  194. # GGML_TYPE_COUNT,
  195. # };
  196. GGML_TYPE_F32 = 0
  197. GGML_TYPE_F16 = 1
  198. GGML_TYPE_Q4_0 = 2
  199. GGML_TYPE_Q4_1 = 3
  200. GGML_TYPE_Q5_0 = 6
  201. GGML_TYPE_Q5_1 = 7
  202. GGML_TYPE_Q8_0 = 8
  203. GGML_TYPE_Q8_1 = 9
  204. GGML_TYPE_Q2_K = 10
  205. GGML_TYPE_Q3_K = 11
  206. GGML_TYPE_Q4_K = 12
  207. GGML_TYPE_Q5_K = 13
  208. GGML_TYPE_Q6_K = 14
  209. GGML_TYPE_Q8_K = 15
  210. GGML_TYPE_I8 = 16
  211. GGML_TYPE_I16 = 17
  212. GGML_TYPE_I32 = 18
  213. GGML_TYPE_COUNT = 19
  214. # enum ggml_backend_type {
  215. # GGML_BACKEND_CPU = 0,
  216. # GGML_BACKEND_GPU = 10,
  217. # GGML_BACKEND_GPU_SPLIT = 20,
  218. # };
  219. GGML_BACKEND_CPU = 0
  220. GGML_BACKEND_GPU = 10
  221. GGML_BACKEND_GPU_SPLIT = 20
  222. # // model file types
  223. # enum ggml_ftype {
  224. # GGML_FTYPE_UNKNOWN = -1,
  225. # GGML_FTYPE_ALL_F32 = 0,
  226. # GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
  227. # GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
  228. # GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
  229. # GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
  230. # GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
  231. # GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
  232. # GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
  233. # GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
  234. # GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
  235. # GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
  236. # GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
  237. # GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
  238. # };
  239. GGML_FTYPE_UNKNOWN = -1
  240. GGML_FTYPE_ALL_F32 = 0
  241. GGML_FTYPE_MOSTLY_F16 = 1
  242. GGML_FTYPE_MOSTLY_Q4_0 = 2
  243. GGML_FTYPE_MOSTLY_Q4_1 = 3
  244. GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4
  245. GGML_FTYPE_MOSTLY_Q8_0 = 7
  246. GGML_FTYPE_MOSTLY_Q5_0 = 8
  247. GGML_FTYPE_MOSTLY_Q5_1 = 9
  248. GGML_FTYPE_MOSTLY_Q2_K = 10
  249. GGML_FTYPE_MOSTLY_Q3_K = 11
  250. GGML_FTYPE_MOSTLY_Q4_K = 12
  251. GGML_FTYPE_MOSTLY_Q5_K = 13
  252. GGML_FTYPE_MOSTLY_Q6_K = 14
  253. # // available tensor operations:
  254. # enum ggml_op {
  255. # GGML_OP_NONE = 0,
  256. # GGML_OP_DUP,
  257. # GGML_OP_ADD,
  258. # GGML_OP_ADD1,
  259. # GGML_OP_ACC,
  260. # GGML_OP_SUB,
  261. # GGML_OP_MUL,
  262. # GGML_OP_DIV,
  263. # GGML_OP_SQR,
  264. # GGML_OP_SQRT,
  265. # GGML_OP_LOG,
  266. # GGML_OP_SUM,
  267. # GGML_OP_SUM_ROWS,
  268. # GGML_OP_MEAN,
  269. # GGML_OP_ARGMAX,
  270. # GGML_OP_REPEAT,
  271. # GGML_OP_REPEAT_BACK,
  272. # GGML_OP_CONCAT,
  273. # GGML_OP_SILU_BACK,
  274. # GGML_OP_NORM, // normalize
  275. # GGML_OP_RMS_NORM,
  276. # GGML_OP_RMS_NORM_BACK,
  277. # GGML_OP_GROUP_NORM,
  278. # GGML_OP_MUL_MAT,
  279. # GGML_OP_MUL_MAT_ID,
  280. # GGML_OP_OUT_PROD,
  281. # GGML_OP_SCALE,
  282. # GGML_OP_SET,
  283. # GGML_OP_CPY,
  284. # GGML_OP_CONT,
  285. # GGML_OP_RESHAPE,
  286. # GGML_OP_VIEW,
  287. # GGML_OP_PERMUTE,
  288. # GGML_OP_TRANSPOSE,
  289. # GGML_OP_GET_ROWS,
  290. # GGML_OP_GET_ROWS_BACK,
  291. # GGML_OP_DIAG,
  292. # GGML_OP_DIAG_MASK_INF,
  293. # GGML_OP_DIAG_MASK_ZERO,
  294. # GGML_OP_SOFT_MAX,
  295. # GGML_OP_SOFT_MAX_BACK,
  296. # GGML_OP_ROPE,
  297. # GGML_OP_ROPE_BACK,
  298. # GGML_OP_ALIBI,
  299. # GGML_OP_CLAMP,
  300. # GGML_OP_CONV_TRANSPOSE_1D,
  301. # GGML_OP_IM2COL,
  302. # GGML_OP_CONV_TRANSPOSE_2D,
  303. # GGML_OP_POOL_1D,
  304. # GGML_OP_POOL_2D,
  305. # GGML_OP_UPSCALE, // nearest interpolate
  306. # GGML_OP_PAD,
  307. # GGML_OP_ARGSORT,
  308. # GGML_OP_LEAKY_RELU,
  309. # GGML_OP_FLASH_ATTN,
  310. # GGML_OP_FLASH_FF,
  311. # GGML_OP_FLASH_ATTN_BACK,
  312. # GGML_OP_WIN_PART,
  313. # GGML_OP_WIN_UNPART,
  314. # GGML_OP_GET_REL_POS,
  315. # GGML_OP_ADD_REL_POS,
  316. # GGML_OP_UNARY,
  317. # GGML_OP_MAP_UNARY,
  318. # GGML_OP_MAP_BINARY,
  319. # GGML_OP_MAP_CUSTOM1_F32,
  320. # GGML_OP_MAP_CUSTOM2_F32,
  321. # GGML_OP_MAP_CUSTOM3_F32,
  322. # GGML_OP_MAP_CUSTOM1,
  323. # GGML_OP_MAP_CUSTOM2,
  324. # GGML_OP_MAP_CUSTOM3,
  325. # GGML_OP_CROSS_ENTROPY_LOSS,
  326. # GGML_OP_CROSS_ENTROPY_LOSS_BACK,
  327. # GGML_OP_COUNT,
  328. # };
  329. GGML_OP_NONE = 0
  330. GGML_OP_DUP = 1
  331. GGML_OP_ADD = 2
  332. GGML_OP_ADD1 = 3
  333. GGML_OP_ACC = 4
  334. GGML_OP_SUB = 5
  335. GGML_OP_MUL = 6
  336. GGML_OP_DIV = 7
  337. GGML_OP_SQR = 8
  338. GGML_OP_SQRT = 9
  339. GGML_OP_LOG = 10
  340. GGML_OP_SUM = 11
  341. GGML_OP_SUM_ROWS = 12
  342. GGML_OP_MEAN = 13
  343. GGML_OP_ARGMAX = 14
  344. GGML_OP_REPEAT = 15
  345. GGML_OP_REPEAT_BACK = 16
  346. GGML_OP_CONCAT = 17
  347. GGML_OP_SILU_BACK = 18
  348. GGML_OP_NORM = 19
  349. GGML_OP_RMS_NORM = 20
  350. GGML_OP_RMS_NORM_BACK = 21
  351. GGML_OP_GROUP_NORM = 22
  352. GGML_OP_MUL_MAT = 23
  353. GGML_OP_MUL_MAT_ID = 24
  354. GGML_OP_OUT_PROD = 25
  355. GGML_OP_SCALE = 26
  356. GGML_OP_SET = 27
  357. GGML_OP_CPY = 28
  358. GGML_OP_CONT = 29
  359. GGML_OP_RESHAPE = 30
  360. GGML_OP_VIEW = 31
  361. GGML_OP_PERMUTE = 32
  362. GGML_OP_TRANSPOSE = 33
  363. GGML_OP_GET_ROWS = 34
  364. GGML_OP_GET_ROWS_BACK = 35
  365. GGML_OP_DIAG = 36
  366. GGML_OP_DIAG_MASK_INF = 37
  367. GGML_OP_DIAG_MASK_ZERO = 38
  368. GGML_OP_SOFT_MAX = 39
  369. GGML_OP_SOFT_MAX_BACK = 40
  370. GGML_OP_ROPE = 41
  371. GGML_OP_ROPE_BACK = 42
  372. GGML_OP_ALIBI = 43
  373. GGML_OP_CLAMP = 44
  374. GGML_OP_CONV_TRANSPOSE_1D = 45
  375. GGML_OP_IM2COL = 46
  376. GGML_OP_CONV_TRANSPOSE_2D = 47
  377. GGML_OP_POOL_1D = 48
  378. GGML_OP_POOL_2D = 49
  379. GGML_OP_UPSCALE = 50
  380. GGML_OP_PAD = 51
  381. GGML_OP_ARGSORT = 52
  382. GGML_OP_LEAKY_RELU = 53
  383. GGML_OP_FLASH_ATTN = 54
  384. GGML_OP_FLASH_FF = 55
  385. GGML_OP_FLASH_ATTN_BACK = 56
  386. GGML_OP_WIN_PART = 57
  387. GGML_OP_WIN_UNPART = 58
  388. GGML_OP_GET_REL_POS = 59
  389. GGML_OP_ADD_REL_POS = 60
  390. GGML_OP_UNARY = 61
  391. GGML_OP_MAP_UNARY = 62
  392. GGML_OP_MAP_BINARY = 63
  393. GGML_OP_MAP_CUSTOM1_F32 = 64
  394. GGML_OP_MAP_CUSTOM2_F32 = 65
  395. GGML_OP_MAP_CUSTOM3_F32 = 66
  396. GGML_OP_MAP_CUSTOM1 = 67
  397. GGML_OP_MAP_CUSTOM2 = 68
  398. GGML_OP_MAP_CUSTOM3 = 69
  399. GGML_OP_CROSS_ENTROPY_LOSS = 70
  400. GGML_OP_CROSS_ENTROPY_LOSS_BACK = 71
  401. GGML_OP_COUNT = 72
  402. # enum ggml_unary_op {
  403. # GGML_UNARY_OP_ABS,
  404. # GGML_UNARY_OP_SGN,
  405. # GGML_UNARY_OP_NEG,
  406. # GGML_UNARY_OP_STEP,
  407. # GGML_UNARY_OP_TANH,
  408. # GGML_UNARY_OP_ELU,
  409. # GGML_UNARY_OP_RELU,
  410. # GGML_UNARY_OP_GELU,
  411. # GGML_UNARY_OP_GELU_QUICK,
  412. # GGML_UNARY_OP_SILU,
  413. # GGML_UNARY_OP_LEAKY
  414. # GGML_UNARY_OP_COUNT,
  415. # };
  416. GGML_UNARY_OP_ABS = 0
  417. GGML_UNARY_OP_SGN = 1
  418. GGML_UNARY_OP_NEG = 2
  419. GGML_UNARY_OP_STEP = 3
  420. GGML_UNARY_OP_TANH = 4
  421. GGML_UNARY_OP_ELU = 5
  422. GGML_UNARY_OP_RELU = 6
  423. GGML_UNARY_OP_GELU = 7
  424. GGML_UNARY_OP_GELU_QUICK = 8
  425. GGML_UNARY_OP_SILU = 9
  426. GGML_UNARY_OP_LEAKY = 10
  427. GGML_UNARY_OP_COUNT = 11
  428. # enum ggml_object_type {
  429. # GGML_OBJECT_TENSOR,
  430. # GGML_OBJECT_GRAPH,
  431. # GGML_OBJECT_WORK_BUFFER
  432. # };
  433. GGML_OBJECT_TENSOR = 0
  434. GGML_OBJECT_GRAPH = 1
  435. GGML_OBJECT_WORK_BUFFER = 2
  436. # enum ggml_log_level {
  437. # GGML_LOG_LEVEL_ERROR = 2,
  438. # GGML_LOG_LEVEL_WARN = 3,
  439. # GGML_LOG_LEVEL_INFO = 4
  440. # };
  441. GGML_LOG_LEVEL_ERROR = 2
  442. GGML_LOG_LEVEL_WARN = 3
  443. GGML_LOG_LEVEL_INFO = 4
  444. # // ggml object
  445. # struct ggml_object {
  446. # size_t offs;
  447. # size_t size;
  448. # struct ggml_object * next;
  449. # enum ggml_object_type type;
  450. # char padding[4];
  451. # };
  452. class ggml_object(ctypes.Structure):
  453. pass
  454. ggml_object._fields_ = [
  455. ("offs", ctypes.c_size_t),
  456. ("size", ctypes.c_size_t),
  457. ("next", ctypes.POINTER(ggml_object)),
  458. ("type", ctypes.c_int),
  459. ("padding", ctypes.c_char * 4),
  460. ]
  461. ggml_object_p: TypeAlias = "ctypes._Pointer[ggml_object]" # type: ignore
  462. GGML_OBJECT_SIZE = ctypes.sizeof(ggml_object)
  463. # // n-dimensional tensor
  464. # struct ggml_tensor {
  465. # enum ggml_type type;
  466. # enum ggml_backend_type backend;
  467. # struct ggml_backend_buffer * buffer;
  468. # int n_dims;
  469. # int64_t ne[GGML_MAX_DIMS]; // number of elements
  470. # size_t nb[GGML_MAX_DIMS]; // stride in bytes:
  471. # // nb[0] = ggml_type_size(type)
  472. # // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding
  473. # // nb[i] = nb[i-1] * ne[i-1]
  474. # // compute data
  475. # enum ggml_op op;
  476. # // op params - allocated as int32_t for alignment
  477. # int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
  478. # bool is_param;
  479. # struct ggml_tensor * grad;
  480. # struct ggml_tensor * src[GGML_MAX_SRC];
  481. # // performance
  482. # int perf_runs;
  483. # int64_t perf_cycles;
  484. # int64_t perf_time_us;
  485. # struct ggml_tensor * view_src;
  486. # size_t view_offs;
  487. # void * data;
  488. # char name[GGML_MAX_NAME];
  489. # void * extra; // extra things e.g. for ggml-cuda.cu
  490. # char padding[12];
  491. # };
  492. class ggml_tensor(ctypes.Structure):
  493. """n-dimensional tensor
  494. Attributes:
  495. type (int): ggml_type
  496. backend (int): ggml_backend
  497. buffer (ctypes.pointer[ggml_backend_buffer]): pointer to backend buffer
  498. n_dims (int): number of dimensions
  499. ne (ctypes.Array[ctypes.c_int64]): number of elements in each dimension
  500. nb (ctypes.Array[ctypes.c_size_t]): stride in bytes for each dimension
  501. op (int): ggml operation
  502. op_params (ctypes.Array[ctypes.c_int32]): `GGML_MAX_OP_PARAMS`-length array of operation parameters
  503. is_param (bool): is this a parameter tensor
  504. grad (ggml_tensor_p): reference to gradient tensor
  505. src (ctypes.Array[ggml_tensor_p]): `GGML_MAX_SRC`-length array of source tensors
  506. perf_runs (int): number of performance runs
  507. perf_cycles (int): number of cycles
  508. perf_time_us (int): time in microseconds
  509. view_src (ggml_tensor_p): pointer to tensor if this tensor is a view, None if the tensor is not a view
  510. view_offs (ctypes.c_size_t): offset into the data pointer of the view tensor
  511. data (ctypes.c_void_p): reference to raw tensor data
  512. name (bytes): name of tensor
  513. extra (ctypes.c_void_p): extra data (e.g. for CUDA)
  514. """
  515. pass
  516. ggml_tensor._fields_ = [
  517. ("type", ctypes.c_int),
  518. ("backend", ctypes.c_int),
  519. ("buffer", ctypes.c_void_p),
  520. ("n_dims", ctypes.c_int),
  521. ("ne", ctypes.c_int64 * GGML_MAX_DIMS),
  522. ("nb", ctypes.c_size_t * GGML_MAX_DIMS),
  523. ("op", ctypes.c_int),
  524. (
  525. "op_params",
  526. ctypes.c_int32 * (GGML_MAX_OP_PARAMS // ctypes.sizeof(ctypes.c_int32)),
  527. ),
  528. ("is_param", ctypes.c_bool),
  529. ("grad", ctypes.POINTER(ggml_tensor)),
  530. ("src", ctypes.POINTER(ggml_tensor) * GGML_MAX_SRC),
  531. ("perf_runs", ctypes.c_int),
  532. ("perf_cycles", ctypes.c_int64),
  533. ("perf_time_us", ctypes.c_int64),
  534. ("view_src", ctypes.POINTER(ggml_tensor)),
  535. ("view_offs", ctypes.c_size_t),
  536. ("data", ctypes.c_void_p),
  537. ("name", ctypes.c_char * GGML_MAX_NAME),
  538. ("extra", ctypes.c_void_p),
  539. ("padding", ctypes.c_char * 12),
  540. ]
  541. GGML_TENSOR_SIZE = ctypes.sizeof(ggml_tensor)
  542. ggml_tensor_p: TypeAlias = "ctypes._Pointer[ggml_tensor]" # type: ignore
  543. """ctypes pointer to a [ggml_tensor][ggml.ggml_tensor]
  544. Can be dereferenced to a [ggml_tensor][ggml.ggml_tensor] object using
  545. the `.contents` attribute."""
  546. abort_callback_t = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p)
  547. # // the compute plan that needs to be prepared for ggml_graph_compute()
  548. # // since https://github.com/ggerganov/ggml/issues/287
  549. # struct ggml_cplan {
  550. # size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
  551. # uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
  552. # int n_threads;
  553. # // abort ggml_graph_compute when true
  554. # bool (*abort_callback)(void * data);
  555. # void * abort_callback_data;
  556. # };
  557. class ggml_cplan(ctypes.Structure):
  558. """Compute plan for a ggml computation graph
  559. Attributes:
  560. work_size (int): size of work buffer
  561. work_data (ctypes.pointer[ctypes.c_uint8]): work buffer
  562. n_threads (int): number of threads
  563. abort_callback (abort_callback_t): abort callback
  564. abort_callback_data (ctypes.c_void_p): abort callback data
  565. """
  566. _fields_ = [
  567. ("work_size", ctypes.c_size_t),
  568. ("work_data", ctypes.POINTER(ctypes.c_uint8)),
  569. ("n_threads", ctypes.c_int),
  570. (
  571. "abort_callback",
  572. abort_callback_t,
  573. ),
  574. ("abort_callback_data", ctypes.c_void_p),
  575. ]
  576. GGML_CPLAN_SIZE = ctypes.sizeof(ggml_cplan)
  577. ggml_cplan_p: TypeAlias = "ctypes._Pointer[ggml_cplan]" # type: ignore
  578. """ctypes pointer to a [ggml_cplan][ggml.ggml_cplan]
  579. Can be dereferenced to a [ggml_cplan][ggml.ggml_cplan] object using
  580. the `.contents` attribute."""
  581. # enum ggml_cgraph_eval_order {
  582. # GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
  583. # GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
  584. # GGML_CGRAPH_EVAL_ORDER_COUNT
  585. # };
  586. GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0
  587. GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT = 1
  588. GGML_CGRAPH_EVAL_ORDER_COUNT = 2
  589. # struct ggml_hash_set {
  590. # size_t size;
  591. # struct ggml_tensor ** keys;
  592. # };
  593. class ggml_hash_set(ctypes.Structure):
  594. _fields_ = [
  595. ("size", ctypes.c_size_t),
  596. ("keys", ctypes.POINTER(ctypes.POINTER(ggml_tensor))),
  597. ]
  598. # // computation graph
  599. # struct ggml_cgraph {
  600. # int size;
  601. # int n_nodes;
  602. # int n_leafs;
  603. # struct ggml_tensor ** nodes;
  604. # struct ggml_tensor ** grads;
  605. # struct ggml_tensor ** leafs;
  606. # struct ggml_hash_set visited_hash_table;
  607. # enum ggml_cgraph_eval_order order;
  608. # // performance
  609. # int perf_runs;
  610. # int64_t perf_cycles;
  611. # int64_t perf_time_us;
  612. # };
  613. class ggml_cgraph(ctypes.Structure):
  614. """ggml computation graph
  615. Attributes:
  616. n_nodes (int): number of nodes
  617. n_leafs (int): number of leafs
  618. nodes (ctypes.Array[ggml_tensor_p]): `n_nodes`-length array of compute tensors
  619. grads (ctypes.Array[ggml_tensor_p]): `n_nodes`-length array of gradient tensors
  620. leafs (ctypes.Array[ggml_tensor_p]): `n_leafs`-length array of parameter tensors
  621. visited_hash_table (ctypes.Array[ctypes.POINTER(ggml_tensor)]): hash table of visited tensors
  622. order (int): evaluation order
  623. perf_runs (int): number of runs
  624. perf_cycles (int): number of cycles
  625. perf_time_us (int): computation time in microseconds"""
  626. _fields_ = [
  627. ("size", ctypes.c_int),
  628. ("n_nodes", ctypes.c_int),
  629. ("n_leafs", ctypes.c_int),
  630. ("nodes", ctypes.POINTER(ctypes.POINTER(ggml_tensor))),
  631. ("grads", ctypes.POINTER(ctypes.POINTER(ggml_tensor))),
  632. ("leafs", ctypes.POINTER(ctypes.POINTER(ggml_tensor))),
  633. ("visited_hash_table", ggml_hash_set),
  634. ("order", ctypes.c_int),
  635. ("perf_runs", ctypes.c_int),
  636. ("perf_cycles", ctypes.c_int64),
  637. ("perf_time_us", ctypes.c_int64),
  638. ]
  639. ggml_cgraph_p: TypeAlias = "ctypes._Pointer[ggml_cgraph]" # type: ignore
  640. """ctypes pointer to a [ggml_cgraph][ggml.ggml_cgraph]
  641. Can be dereferenced to a [ggml_cgraph][ggml.ggml_cgraph] object using
  642. the `.contents` attribute."""
  643. # struct ggml_scratch {
  644. # size_t offs;
  645. # size_t size;
  646. # void * data;
  647. # };
  648. class ggml_scratch(ctypes.Structure):
  649. _fields_ = [
  650. ("offs", ctypes.c_size_t),
  651. ("size", ctypes.c_size_t),
  652. ("data", ctypes.c_void_p),
  653. ]
  654. # struct ggml_init_params {
  655. # // memory pool
  656. # size_t mem_size; // bytes
  657. # void * mem_buffer; // if NULL, memory will be allocated internally
  658. # bool no_alloc; // don't allocate memory for the tensor data
  659. # };
  660. class ggml_init_params(ctypes.Structure):
  661. """Initialization parameters for a ggml context
  662. **NOTE**: Reference counting does not cross into ggml, if you allocate a memory buffer
  663. in python using ctypes Arrays or a numpy array, you must keep a reference to it until
  664. you free the ggml context otherwise you will encounter a segmentation fault.
  665. Attributes:
  666. mem_size (int): size of memory pool in bytes
  667. mem_buffer (ctypes.c_void_p): pointer to memory pool, if None, memory will be allocated internally
  668. no_alloc (bool): don't allocate memory for tensor data
  669. """
  670. _fields_ = [
  671. ("mem_size", ctypes.c_size_t),
  672. ("mem_buffer", ctypes.c_void_p),
  673. ("no_alloc", ctypes.c_bool),
  674. ]
  675. # // compute types
  676. # // NOTE: the INIT or FINALIZE pass is not scheduled unless explicitly enabled.
  677. # // This behavior was changed since https://github.com/ggerganov/llama.cpp/pull/1995.
  678. # enum ggml_task_type {
  679. # GGML_TASK_INIT = 0,
  680. # GGML_TASK_COMPUTE,
  681. # GGML_TASK_FINALIZE,
  682. # };
  683. GGML_TASK_INIT = 0
  684. GGML_TASK_COMPUTE = 1
  685. GGML_TASK_FINALIZE = 2
  686. # struct ggml_compute_params {
  687. # enum ggml_task_type type;
  688. # // ith = thread index, nth = number of threads
  689. # int ith, nth;
  690. # // work buffer for all threads
  691. # size_t wsize;
  692. # void * wdata;
  693. # };
  694. class ggml_compute_params(ctypes.Structure):
  695. _fields_ = [
  696. ("type", ctypes.c_int),
  697. ("ith", ctypes.c_int),
  698. ("nth", ctypes.c_int),
  699. ("wsize", ctypes.c_size_t),
  700. ("wdata", ctypes.c_void_p),
  701. ]
  702. ggml_compute_params_p: TypeAlias = "ctypes._Pointer[ggml_compute_params]" # type: ignore
  703. # // misc
  704. # GGML_API void ggml_time_init(void); // call this once at the beginning of the program
  705. def ggml_time_init():
  706. return lib.ggml_time_init()
  707. lib.ggml_time_init.argtypes = []
  708. lib.ggml_time_init.restype = None
  709. # GGML_API int64_t ggml_time_ms(void);
  710. def ggml_time_ms() -> int:
  711. return lib.ggml_time_ms()
  712. lib.ggml_time_ms.argtypes = []
  713. lib.ggml_time_ms.restype = ctypes.c_int64
  714. # GGML_API int64_t ggml_time_us(void);
  715. def ggml_time_us() -> int:
  716. return lib.ggml_time_us()
  717. lib.ggml_time_us.argtypes = []
  718. lib.ggml_time_us.restype = ctypes.c_int64
  719. # GGML_API int64_t ggml_cycles(void);
  720. def ggml_cycles() -> int:
  721. return lib.ggml_cycles()
  722. lib.ggml_cycles.argtypes = []
  723. lib.ggml_cycles.restype = ctypes.c_int64
  724. # GGML_API int64_t ggml_cycles_per_ms(void);
  725. def ggml_cycles_per_ms() -> int:
  726. return lib.ggml_cycles_per_ms()
  727. lib.ggml_cycles_per_ms.argtypes = []
  728. lib.ggml_cycles_per_ms.restype = ctypes.c_int64
  729. # GGML_API void ggml_print_backtrace(void);
  730. def ggml_print_backtrace():
  731. return lib.ggml_print_backtrace()
  732. lib.ggml_print_backtrace.argtypes = []
  733. lib.ggml_print_backtrace.restype = None
  734. # GGML_API void ggml_numa_init(void); // call once for better performance on NUMA systems
  735. def ggml_numa_init():
  736. return lib.ggml_numa_init()
  737. lib.ggml_numa_init.argtypes = []
  738. lib.ggml_numa_init.restype = None
  739. # GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
  740. def ggml_is_numa() -> bool:
  741. return lib.ggml_is_numa()
  742. lib.ggml_is_numa.argtypes = []
  743. lib.ggml_is_numa.restype = ctypes.c_bool
  744. # GGML_API void ggml_print_object (const struct ggml_object * obj);
  745. def ggml_print_object(obj: ggml_object_p):
  746. return lib.ggml_print_object(obj)
  747. lib.ggml_print_object.argtypes = [ctypes.POINTER(ggml_object)]
  748. lib.ggml_print_object.restype = None
  749. # GGML_API void ggml_print_objects(const struct ggml_context * ctx);
  750. def ggml_print_objects(ctx: ggml_context_p):
  751. return lib.ggml_print_objects(ctx)
  752. lib.ggml_print_objects.argtypes = [ggml_context_p]
  753. lib.ggml_print_objects.restype = None
  754. # GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
  755. def ggml_nelements(
  756. tensor: ggml_tensor_p,
  757. ) -> int:
  758. """Get the number of elements in a tensor
  759. Parameters:
  760. tensor: tensor
  761. Returns:
  762. number of elements"""
  763. return lib.ggml_nelements(tensor)
  764. lib.ggml_nelements.argtypes = [ctypes.POINTER(ggml_tensor)]
  765. lib.ggml_nelements.restype = ctypes.c_int64
  766. # GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
  767. def ggml_nrows(
  768. tensor: ggml_tensor_p,
  769. ) -> int:
  770. """Get the number of rows in a tensor
  771. Parameters:
  772. tensor: tensor
  773. Returns:
  774. number of rows"""
  775. return lib.ggml_nrows(tensor)
  776. lib.ggml_nrows.argtypes = [ctypes.POINTER(ggml_tensor)]
  777. lib.ggml_nrows.restype = ctypes.c_int64
  778. # GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
  779. def ggml_nbytes(
  780. tensor: ggml_tensor_p,
  781. ) -> int:
  782. """Get the number of bytes required to store tensor data
  783. Parameters:
  784. tensor: tensor
  785. Returns:
  786. number of bytes"""
  787. return lib.ggml_nbytes(tensor)
  788. lib.ggml_nbytes.argtypes = [ctypes.POINTER(ggml_tensor)]
  789. lib.ggml_nbytes.restype = ctypes.c_size_t
  790. # GGML_API size_t ggml_nbytes_pad (const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
  791. def ggml_nbytes_pad(
  792. tensor: ggml_tensor_p,
  793. ) -> int:
  794. """Get the number of bytes required to store tensor data, padded to GGML_MEM_ALIGN
  795. Parameters:
  796. tensor: tensor
  797. Returns:
  798. number of bytes"""
  799. return lib.ggml_nbytes_pad(tensor)
  800. lib.ggml_nbytes_pad.argtypes = [ctypes.POINTER(ggml_tensor)]
  801. lib.ggml_nbytes_pad.restype = ctypes.c_size_t
  802. # GGML_API size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split);
  803. def ggml_nbytes_split(
  804. tensor: ggml_tensor_p,
  805. nrows_split: Union[ctypes.c_int, int],
  806. ) -> int:
  807. return lib.ggml_nbytes_split(tensor, nrows_split)
  808. lib.ggml_nbytes_split.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int]
  809. lib.ggml_nbytes_split.restype = ctypes.c_size_t
  810. # GGML_API int ggml_blck_size (enum ggml_type type);
  811. def ggml_blck_size(type: Union[ctypes.c_int, int]) -> int:
  812. return lib.ggml_blck_size(type)
  813. lib.ggml_blck_size.argtypes = [ctypes.c_int]
  814. lib.ggml_blck_size.restype = ctypes.c_int
  815. # GGML_API size_t ggml_type_size (enum ggml_type type); // size in bytes for all elements in a block
  816. def ggml_type_size(type: Union[ctypes.c_int, int]) -> int:
  817. return lib.ggml_type_size(type)
  818. lib.ggml_type_size.argtypes = [ctypes.c_int]
  819. lib.ggml_type_size.restype = ctypes.c_size_t
  820. # GGML_API float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
  821. def ggml_type_sizef(type: Union[ctypes.c_int, int]) -> float:
  822. return lib.ggml_type_sizef(type)
  823. lib.ggml_type_sizef.argtypes = [ctypes.c_int]
  824. lib.ggml_type_sizef.restype = ctypes.c_float
  825. # GGML_API const char * ggml_type_name(enum ggml_type type);
  826. def ggml_type_name(type: Union[ctypes.c_int, int]) -> bytes:
  827. return lib.ggml_type_name(type)
  828. lib.ggml_type_name.argtypes = [ctypes.c_int]
  829. lib.ggml_type_name.restype = ctypes.c_char_p
  830. # GGML_API const char * ggml_op_name (enum ggml_op op);
  831. def ggml_op_name(op: Union[ctypes.c_int, int]) -> bytes:
  832. return lib.ggml_op_name(op)
  833. lib.ggml_op_name.argtypes = [ctypes.c_int]
  834. lib.ggml_op_name.restype = ctypes.c_char_p
  835. # GGML_API const char * ggml_op_symbol(enum ggml_op op);
  836. def ggml_op_symbol(op: Union[ctypes.c_int, int]) -> bytes:
  837. return lib.ggml_op_symbol(op)
  838. lib.ggml_op_symbol.argtypes = [ctypes.c_int]
  839. lib.ggml_op_symbol.restype = ctypes.c_char_p
  840. # GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op);
  841. def ggml_unary_op_name(op: Union[ctypes.c_int, int]) -> bytes:
  842. return lib.ggml_unary_op_name(op)
  843. lib.ggml_unary_op_name.argtypes = [ctypes.c_int]
  844. lib.ggml_unary_op_name.restype = ctypes.c_char_p
  845. # GGML_API const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name
  846. def ggml_op_desc(
  847. t: ggml_tensor_p,
  848. ) -> bytes:
  849. return lib.ggml_op_desc(t)
  850. lib.ggml_op_desc.argtypes = [ctypes.POINTER(ggml_tensor)]
  851. lib.ggml_op_desc.restype = ctypes.c_char_p
  852. # GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
  853. def ggml_element_size(
  854. tensor: ggml_tensor_p,
  855. ) -> int:
  856. return lib.ggml_element_size(tensor)
  857. lib.ggml_element_size.argtypes = [ctypes.POINTER(ggml_tensor)]
  858. lib.ggml_element_size.restype = ctypes.c_size_t
  859. # GGML_API bool ggml_is_quantized(enum ggml_type type);
  860. def ggml_is_quantized(type: Union[ctypes.c_int, int]) -> bool:
  861. return lib.ggml_is_quantized(type)
  862. lib.ggml_is_quantized.argtypes = [ctypes.c_int]
  863. lib.ggml_is_quantized.restype = ctypes.c_bool
  864. # // TODO: temporary until model loading of ggml examples is refactored
  865. # GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
  866. def ggml_ftype_to_ggml_type(ftype: Union[ctypes.c_int, int]) -> int:
  867. return lib.ggml_ftype_to_ggml_type(ftype)
  868. lib.ggml_ftype_to_ggml_type.argtypes = [ctypes.c_int]
  869. lib.ggml_ftype_to_ggml_type.restype = ctypes.c_int
  870. # GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
  871. def ggml_is_transposed(
  872. tensor: ggml_tensor_p,
  873. ) -> bool:
  874. """Check if a tensor is transposed
  875. Parameters:
  876. tensor: tensor
  877. Returns:
  878. True if tensor is transposed else False"""
  879. return lib.ggml_is_transposed(tensor)
  880. lib.ggml_is_transposed.argtypes = [ctypes.POINTER(ggml_tensor)]
  881. lib.ggml_is_transposed.restype = ctypes.c_bool
  882. # GGML_API bool ggml_is_contiguous(const struct ggml_tensor * tensor);
  883. def ggml_is_contiguous(
  884. tensor: ggml_tensor_p,
  885. ) -> bool:
  886. """Check if a tensor is contiguous
  887. Parameters:
  888. tensor: tensor
  889. Returns:
  890. True if tensor is contiguous else False"""
  891. return lib.ggml_is_contiguous(tensor)
  892. lib.ggml_is_contiguous.argtypes = [ctypes.POINTER(ggml_tensor)]
  893. lib.ggml_is_contiguous.restype = ctypes.c_bool
  894. # GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
  895. def ggml_is_permuted(
  896. tensor: ggml_tensor_p,
  897. ) -> bool:
  898. """Check if a tensor is permuted
  899. Parameters:
  900. tensor: tensor
  901. Returns:
  902. True if tensor is permuted else False"""
  903. return lib.ggml_is_permuted(tensor)
  904. lib.ggml_is_permuted.argtypes = [ctypes.POINTER(ggml_tensor)]
  905. lib.ggml_is_permuted.restype = ctypes.c_bool
  906. # GGML_API bool ggml_are_same_shape(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
  907. def ggml_are_same_shape(
  908. t0: ggml_tensor_p,
  909. t1: ggml_tensor_p,
  910. ) -> bool:
  911. """Check if two tensors have the same shape
  912. Parameters:
  913. t0: tensor 0
  914. t1: tensor 1
  915. Returns:
  916. True if tensors have the same shape else False"""
  917. return lib.ggml_are_same_shape(t0, t1)
  918. lib.ggml_are_same_shape.argtypes = [
  919. ctypes.POINTER(ggml_tensor),
  920. ctypes.POINTER(ggml_tensor),
  921. ]
  922. lib.ggml_are_same_shape.restype = ctypes.c_bool
  923. # // use this to compute the memory overhead of a tensor
  924. # GGML_API size_t ggml_tensor_overhead(void);
  925. def ggml_tensor_overhead() -> int:
  926. """Overhead required for a tensor struct in bytes
  927. Returns:
  928. size of tensor struct in bytes"""
  929. return lib.ggml_tensor_overhead()
  930. lib.ggml_tensor_overhead.argtypes = []
  931. lib.ggml_tensor_overhead.restype = ctypes.c_size_t
  932. # // main
  933. # GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
  934. def ggml_init(
  935. params: ggml_init_params,
  936. ) -> ggml_context_p:
  937. """Instantiate a new ggml context with params.
  938. You must call `ggml_free()` to free the context.
  939. Parameters:
  940. params: ggml init params
  941. Returns:
  942. Pointer to ggml_context"""
  943. return lib.ggml_init(params)
  944. lib.ggml_init.argtypes = [ggml_init_params]
  945. lib.ggml_init.restype = ggml_context_p
  946. # GGML_API void ggml_free(struct ggml_context * ctx);
  947. def ggml_free(ctx: ggml_context_p):
  948. """Free the ggml context.
  949. Parameters:
  950. ctx: ggml context"""
  951. return lib.ggml_free(ctx)
  952. lib.ggml_free.argtypes = [ggml_context_p]
  953. lib.ggml_free.restype = None
  954. # GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
  955. def ggml_used_mem(ctx: ggml_context_p) -> int:
  956. """Return the amount of memory used by the ggml context in bytes.
  957. Parameters:
  958. ctx: ggml context
  959. Returns:
  960. amount of memory used in bytes"""
  961. return lib.ggml_used_mem(ctx)
  962. lib.ggml_used_mem.argtypes = [ggml_context_p]
  963. lib.ggml_used_mem.restype = ctypes.c_size_t
  964. # GGML_API size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch);
  965. def ggml_set_scratch(ctx: ggml_context_p, scratch: ggml_scratch) -> int:
  966. """Set the scratch buffer for the ggml context."""
  967. return lib.ggml_set_scratch(ctx, scratch)
  968. lib.ggml_set_scratch.argtypes = [ggml_context_p, ggml_scratch]
  969. lib.ggml_set_scratch.restype = ctypes.c_size_t
  970. # GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
  971. def ggml_get_no_alloc(ctx: ggml_context_p) -> bool:
  972. """Return the no_alloc flag for the ggml context."""
  973. return lib.ggml_get_no_alloc(ctx)
  974. lib.ggml_get_no_alloc.argtypes = [ggml_context_p]
  975. lib.ggml_get_no_alloc.restype = ctypes.c_bool
  976. # GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
  977. def ggml_set_no_alloc(ctx: ggml_context_p, no_alloc: Union[ctypes.c_bool, bool]):
  978. """Set the no_alloc flag for the ggml context."""
  979. return lib.ggml_set_no_alloc(ctx, no_alloc)
  980. lib.ggml_set_no_alloc.argtypes = [ggml_context_p, ctypes.c_bool]
  981. lib.ggml_set_no_alloc.restype = None
  982. # GGML_API void * ggml_get_mem_buffer (struct ggml_context * ctx);
  983. def ggml_get_mem_buffer(ctx: ggml_context_p) -> Optional[ctypes.c_void_p]:
  984. """Return the memory buffer for the ggml context."""
  985. return lib.ggml_get_mem_buffer(ctx)
  986. lib.ggml_get_mem_buffer.argtypes = [ggml_context_p]
  987. lib.ggml_get_mem_buffer.restype = ctypes.c_void_p
  988. # GGML_API size_t ggml_get_mem_size (struct ggml_context * ctx);
  989. def ggml_get_mem_size(ctx: ggml_context_p) -> int:
  990. """Return the size of the memory buffer for the ggml context in bytes."""
  991. return lib.ggml_get_mem_size(ctx)
  992. lib.ggml_get_mem_size.argtypes = [ggml_context_p]
  993. lib.ggml_get_mem_size.restype = ctypes.c_size_t
  994. # GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
  995. def ggml_get_max_tensor_size(ctx: ggml_context_p) -> int:
  996. """Return the maximum size of a tensor in bytes."""
  997. return lib.ggml_get_max_tensor_size(ctx)
  998. lib.ggml_get_max_tensor_size.argtypes = [ggml_context_p]
  999. lib.ggml_get_max_tensor_size.restype = ctypes.c_size_t
  1000. # GGML_API struct ggml_tensor * ggml_new_tensor(
  1001. # struct ggml_context * ctx,
  1002. # enum ggml_type type,
  1003. # int n_dims,
  1004. # const int64_t *ne);
  1005. def ggml_new_tensor(
  1006. ctx: ggml_context_p,
  1007. type: Union[ctypes.c_int, int],
  1008. n_dims: Union[ctypes.c_int, int],
  1009. ne: CInt64Array,
  1010. ) -> ggml_tensor_p:
  1011. """Create a new tensor with the given type, number of dimensions, and number of elements in each dimension.
  1012. Parameters:
  1013. ctx: ggml context
  1014. type: ggml type
  1015. n_dims: number of dimensions
  1016. ne (ctypes.Array[ctypes.c_int64]): number of elements in each dimension (array of length n_dims)
  1017. Returns:
  1018. Pointer to ggml_tensor"""
  1019. return lib.ggml_new_tensor(ctx, type, n_dims, ne)
  1020. lib.ggml_new_tensor.argtypes = [
  1021. ggml_context_p,
  1022. ctypes.c_int,
  1023. ctypes.c_int,
  1024. ctypes.POINTER(ctypes.c_int64),
  1025. ]
  1026. lib.ggml_new_tensor.restype = ctypes.POINTER(ggml_tensor)
  1027. # GGML_API struct ggml_tensor * ggml_new_tensor_1d(
  1028. # struct ggml_context * ctx,
  1029. # enum ggml_type type,
  1030. # int64_t ne0);
  1031. def ggml_new_tensor_1d(
  1032. ctx: ggml_context_p, type: Union[ctypes.c_int, int], ne0: Union[ctypes.c_int64, int]
  1033. ) -> ggml_tensor_p:
  1034. """Create a new 1-dimensional tensor with the given type and number of elements.
  1035. Parameters:
  1036. ctx: ggml context
  1037. type: ggml type
  1038. ne0: number of elements in dimension 0
  1039. Returns:
  1040. Pointer to ggml_tensor"""
  1041. return lib.ggml_new_tensor_1d(ctx, type, ne0)
  1042. lib.ggml_new_tensor_1d.argtypes = [ggml_context_p, ctypes.c_int, ctypes.c_int64]
  1043. lib.ggml_new_tensor_1d.restype = ctypes.POINTER(ggml_tensor)
  1044. # GGML_API struct ggml_tensor * ggml_new_tensor_2d(
  1045. # struct ggml_context * ctx,
  1046. # enum ggml_type type,
  1047. # int64_t ne0,
  1048. # int64_t ne1);
  1049. def ggml_new_tensor_2d(
  1050. ctx: ggml_context_p,
  1051. type: Union[ctypes.c_int, int],
  1052. ne0: Union[ctypes.c_int64, int],
  1053. ne1: Union[ctypes.c_int64, int],
  1054. ) -> ggml_tensor_p:
  1055. """Create a new 2-dimensional tensor with the given type and number of elements in each dimension.
  1056. Parameters:
  1057. ctx: ggml context
  1058. type: ggml type
  1059. ne0: number of elements in dimension 0
  1060. ne1: number of elements in dimension 1
  1061. Returns:
  1062. Pointer to ggml_tensor"""
  1063. return lib.ggml_new_tensor_2d(ctx, type, ne0, ne1)
  1064. lib.ggml_new_tensor_2d.argtypes = [
  1065. ggml_context_p,
  1066. ctypes.c_int,
  1067. ctypes.c_int64,
  1068. ctypes.c_int64,
  1069. ]
  1070. lib.ggml_new_tensor_2d.restype = ctypes.POINTER(ggml_tensor)
  1071. # GGML_API struct ggml_tensor * ggml_new_tensor_3d(
  1072. # struct ggml_context * ctx,
  1073. # enum ggml_type type,
  1074. # int64_t ne0,
  1075. # int64_t ne1,
  1076. # int64_t ne2);
  1077. def ggml_new_tensor_3d(
  1078. ctx: ggml_context_p,
  1079. type: Union[ctypes.c_int, int],
  1080. ne0: Union[ctypes.c_int64, int],
  1081. ne1: Union[ctypes.c_int64, int],
  1082. ne2: Union[ctypes.c_int64, int],
  1083. ) -> ggml_tensor_p:
  1084. """Create a new 3-dimensional tensor with the given type and number of elements in each dimension.
  1085. Parameters:
  1086. ctx: ggml context
  1087. type: ggml type
  1088. ne0: number of elements in dimension 0
  1089. ne1: number of elements in dimension 1
  1090. ne2: number of elements in dimension 2
  1091. Returns:
  1092. Pointer to ggml_tensor"""
  1093. return lib.ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2)
  1094. lib.ggml_new_tensor_3d.argtypes = [
  1095. ggml_context_p,
  1096. ctypes.c_int,
  1097. ctypes.c_int64,
  1098. ctypes.c_int64,
  1099. ctypes.c_int64,
  1100. ]
  1101. lib.ggml_new_tensor_3d.restype = ctypes.POINTER(ggml_tensor)
  1102. # GGML_API struct ggml_tensor * ggml_new_tensor_4d(
  1103. # struct ggml_context * ctx,
  1104. # enum ggml_type type,
  1105. # int64_t ne0,
  1106. # int64_t ne1,
  1107. # int64_t ne2,
  1108. # int64_t ne3);
  1109. def ggml_new_tensor_4d(
  1110. ctx: ggml_context_p,
  1111. type: Union[ctypes.c_int, int],
  1112. ne0: Union[ctypes.c_int64, int],
  1113. ne1: Union[ctypes.c_int64, int],
  1114. ne2: Union[ctypes.c_int64, int],
  1115. ne3: Union[ctypes.c_int64, int],
  1116. ) -> ggml_tensor_p:
  1117. """Create a new 4-dimensional tensor with the given type and number of elements in each dimension.
  1118. Parameters:
  1119. ctx: ggml context
  1120. type: ggml type
  1121. ne0: number of elements in dimension 0
  1122. ne1: number of elements in dimension 1
  1123. ne2: number of elements in dimension 2
  1124. Returns:
  1125. Pointer to ggml_tensor"""
  1126. return lib.ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3)
  1127. lib.ggml_new_tensor_4d.argtypes = [
  1128. ggml_context_p,
  1129. ctypes.c_int,
  1130. ctypes.c_int64,
  1131. ctypes.c_int64,
  1132. ctypes.c_int64,
  1133. ctypes.c_int64,
  1134. ]
  1135. lib.ggml_new_tensor_4d.restype = ctypes.POINTER(ggml_tensor)
  1136. # GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
  1137. def ggml_new_i32(
  1138. ctx: ggml_context_p, value: Union[ctypes.c_int32, int]
  1139. ) -> ggml_tensor_p:
  1140. """Create a 1 element tensor with the given integer value.
  1141. Parameters:
  1142. ctx: ggml context
  1143. value: integer value
  1144. Returns:
  1145. Pointer to ggml_tensor"""
  1146. return lib.ggml_new_i32(ctx, value)
  1147. lib.ggml_new_i32.argtypes = [ggml_context_p, ctypes.c_int32]
  1148. lib.ggml_new_i32.restype = ctypes.POINTER(ggml_tensor)
  1149. # GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
  1150. def ggml_new_f32(
  1151. ctx: ggml_context_p,
  1152. value: Union[ctypes.c_float, float],
  1153. ) -> ggml_tensor_p:
  1154. """Create a 1 element tensor with the given float value.
  1155. Parameters:
  1156. ctx: ggml context
  1157. value: float value
  1158. Returns:
  1159. Pointer to ggml_tensor"""
  1160. return lib.ggml_new_f32(ctx, value)
  1161. lib.ggml_new_f32.argtypes = [ggml_context_p, ctypes.c_float]
  1162. lib.ggml_new_f32.restype = ctypes.POINTER(ggml_tensor)
  1163. # GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
  1164. def ggml_dup_tensor(ctx: ggml_context_p, src: ggml_tensor_p) -> ggml_tensor_p:
  1165. """Create a new tensor with the same type and dimensions as the source tensor.
  1166. Parameters:
  1167. ctx: ggml context
  1168. src: source tensor
  1169. Returns:
  1170. Pointer to ggml_tensor"""
  1171. return lib.ggml_dup_tensor(ctx, src)
  1172. lib.ggml_dup_tensor.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1173. lib.ggml_dup_tensor.restype = ctypes.POINTER(ggml_tensor)
  1174. # GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
  1175. def ggml_view_tensor(ctx: ggml_context_p, src: ggml_tensor_p) -> ggml_tensor_p:
  1176. """Create a new tensor with the same type, dimensions and data as the source tensor.
  1177. Parameters:
  1178. ctx: ggml context
  1179. src: source tensor
  1180. Returns:
  1181. Pointer to ggml_tensor"""
  1182. return lib.ggml_view_tensor(ctx, src)
  1183. lib.ggml_view_tensor.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1184. lib.ggml_view_tensor.restype = ctypes.POINTER(ggml_tensor)
  1185. # // Context tensor enumeration and lookup
  1186. # GGML_API struct ggml_tensor * ggml_get_first_tensor(struct ggml_context * ctx);
  1187. def ggml_get_first_tensor(ctx: ggml_context_p) -> ggml_tensor_p:
  1188. """Get the first tensor from the ggml context.
  1189. Parameters:
  1190. ctx: ggml context
  1191. Returns:
  1192. Pointer to ggml_tensor"""
  1193. return lib.ggml_get_first_tensor(ctx)
  1194. lib.ggml_get_first_tensor.argtypes = [ggml_context_p]
  1195. lib.ggml_get_first_tensor.restype = ctypes.POINTER(ggml_tensor)
  1196. # GGML_API struct ggml_tensor * ggml_get_next_tensor (struct ggml_context * ctx, struct ggml_tensor * tensor);
  1197. def ggml_get_next_tensor(ctx: ggml_context_p, tensor: ggml_tensor_p) -> ggml_tensor_p:
  1198. """Get the next tensor from the ggml context.
  1199. Parameters:
  1200. ctx: ggml context
  1201. tensor: tensor
  1202. Returns:
  1203. Pointer to ggml_tensor"""
  1204. return lib.ggml_get_next_tensor(ctx, tensor)
  1205. lib.ggml_get_next_tensor.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1206. lib.ggml_get_next_tensor.restype = ctypes.POINTER(ggml_tensor)
  1207. # GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
  1208. def ggml_get_tensor(ctx: ggml_context_p, name: bytes) -> ggml_tensor_p:
  1209. """Get a tensor from the ggml context by name.
  1210. Parameters:
  1211. ctx: ggml context
  1212. name: name of tensor
  1213. Returns:
  1214. Pointer to ggml_tensor"""
  1215. return lib.ggml_get_tensor(ctx, name)
  1216. lib.ggml_get_tensor.argtypes = [ggml_context_p, ctypes.c_char_p]
  1217. lib.ggml_get_tensor.restype = ctypes.POINTER(ggml_tensor)
  1218. # GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
  1219. def ggml_set_zero(
  1220. tensor: ggml_tensor_p,
  1221. ) -> ggml_tensor_p:
  1222. """Zero all elements in a tensor.
  1223. Parameters:
  1224. tensor: tensor
  1225. Returns:
  1226. Pointer to ggml_tensor"""
  1227. return lib.ggml_set_zero(tensor)
  1228. lib.ggml_set_zero.argtypes = [ctypes.POINTER(ggml_tensor)]
  1229. lib.ggml_set_zero.restype = ctypes.POINTER(ggml_tensor)
  1230. # GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
  1231. def ggml_set_i32(
  1232. tensor: ggml_tensor_p,
  1233. value: Union[ctypes.c_int32, int],
  1234. ) -> ggml_tensor_p:
  1235. """Set all elements in a tensor to the given integer value.
  1236. Parameters:
  1237. tensor: tensor
  1238. value: integer value
  1239. Returns:
  1240. Pointer to ggml_tensor"""
  1241. return lib.ggml_set_i32(tensor, value)
  1242. lib.ggml_set_i32.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int32]
  1243. lib.ggml_set_i32.restype = ctypes.POINTER(ggml_tensor)
  1244. # GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
  1245. def ggml_set_f32(
  1246. tensor: ggml_tensor_p,
  1247. value: Union[ctypes.c_float, float],
  1248. ) -> ggml_tensor_p:
  1249. """Set all elements in a tensor to the given float value.
  1250. Parameters:
  1251. tensor: tensor
  1252. value: float value
  1253. Returns:
  1254. Pointer to ggml_tensor"""
  1255. return lib.ggml_set_f32(tensor, value)
  1256. lib.ggml_set_f32.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_float]
  1257. lib.ggml_set_f32.restype = ctypes.POINTER(ggml_tensor)
  1258. # // Converts a flat index into coordinates
  1259. # GGML_API void ggml_unravel_index(const struct ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
  1260. def ggml_unravel_index(
  1261. tensor: ggml_tensor_p,
  1262. i: Union[ctypes.c_int64, int],
  1263. i0, # type: "ctypes._Pointer(ctypes.c_int64)" # type: ignore
  1264. i1, # type: "ctypes._Pointer(ctypes.c_int64)" # type: ignore
  1265. i2, # type: "ctypes._Pointer(ctypes.c_int64)" # type: ignore
  1266. i3, # type: "ctypes._Pointer(ctypes.c_int64)" # type: ignore
  1267. ):
  1268. """Convert a flat index into coordinates.
  1269. Parameters:
  1270. tensor: tensor
  1271. i: flat index
  1272. i0: pointer to index 0
  1273. i1: pointer to index 1
  1274. i2: pointer to index 2
  1275. i3: pointer to index 3"""
  1276. return lib.ggml_unravel_index(tensor, i, i0, i1, i2, i3)
  1277. lib.ggml_unravel_index.argtypes = [
  1278. ctypes.POINTER(ggml_tensor),
  1279. ctypes.c_int64,
  1280. ctypes.POINTER(ctypes.c_int64),
  1281. ctypes.POINTER(ctypes.c_int64),
  1282. ctypes.POINTER(ctypes.c_int64),
  1283. ctypes.POINTER(ctypes.c_int64),
  1284. ]
  1285. lib.ggml_unravel_index.restype = None
  1286. # GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
  1287. def ggml_get_i32_1d(
  1288. tensor: ggml_tensor_p,
  1289. i: Union[ctypes.c_int, int],
  1290. ) -> int:
  1291. """Get the integer value of the i-th element in a 1-dimensional tensor.
  1292. Parameters:
  1293. tensor: tensor
  1294. i: index of element
  1295. Returns:
  1296. integer value of element at index i"""
  1297. return lib.ggml_get_i32_1d(tensor, i)
  1298. lib.ggml_get_i32_1d.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int]
  1299. lib.ggml_get_i32_1d.restype = ctypes.c_int32
  1300. # GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
  1301. def ggml_set_i32_1d(
  1302. tensor: ggml_tensor_p,
  1303. i: Union[ctypes.c_int, int],
  1304. value: Union[ctypes.c_int32, int],
  1305. ):
  1306. """Set the integer value of the i-th element in a 1-dimensional tensor.
  1307. Parameters:
  1308. tensor: tensor
  1309. i: index of element
  1310. value: integer value to set element to"""
  1311. return lib.ggml_set_i32_1d(tensor, i, value)
  1312. lib.ggml_set_i32_1d.argtypes = [
  1313. ctypes.POINTER(ggml_tensor),
  1314. ctypes.c_int,
  1315. ctypes.c_int32,
  1316. ]
  1317. lib.ggml_set_i32_1d.restype = None
  1318. # GGML_API int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
  1319. def ggml_get_i32_nd(
  1320. tensor: ggml_tensor_p,
  1321. i0: Union[ctypes.c_int, int],
  1322. i1: Union[ctypes.c_int, int],
  1323. i2: Union[ctypes.c_int, int],
  1324. i3: Union[ctypes.c_int, int],
  1325. ) -> int:
  1326. """Get the integer value of the element at the given coordinates in a 4-dimensional tensor.
  1327. Parameters:
  1328. tensor: tensor
  1329. i0: index of element in dimension 0
  1330. i1: index of element in dimension 1
  1331. i2: index of element in dimension 2
  1332. i3: index of element in dimension 3
  1333. Returns:
  1334. integer value of element at coordinates"""
  1335. return lib.ggml_get_i32_nd(tensor, i0, i1, i2, i3)
  1336. lib.ggml_get_i32_nd.argtypes = [
  1337. ctypes.POINTER(ggml_tensor),
  1338. ctypes.c_int,
  1339. ctypes.c_int,
  1340. ctypes.c_int,
  1341. ctypes.c_int,
  1342. ]
  1343. lib.ggml_get_i32_nd.restype = ctypes.c_int32
  1344. # GGML_API void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value);
  1345. def ggml_set_i32_nd(
  1346. tensor: ggml_tensor_p,
  1347. i0: Union[ctypes.c_int, int],
  1348. i1: Union[ctypes.c_int, int],
  1349. i2: Union[ctypes.c_int, int],
  1350. i3: Union[ctypes.c_int, int],
  1351. value: Union[ctypes.c_int32, int],
  1352. ):
  1353. """Set the integer value of the element at the given coordinates in a 4-dimensional tensor.
  1354. Parameters:
  1355. tensor: tensor
  1356. i0: index of element in dimension 0
  1357. i1: index of element in dimension 1
  1358. i2: index of element in dimension 2
  1359. i3: index of element in dimension 3
  1360. value: integer value to set element to"""
  1361. return lib.ggml_set_i32_nd(tensor, i0, i1, i2, i3, value)
  1362. lib.ggml_set_i32_nd.argtypes = [
  1363. ctypes.POINTER(ggml_tensor),
  1364. ctypes.c_int,
  1365. ctypes.c_int,
  1366. ctypes.c_int,
  1367. ctypes.c_int,
  1368. ctypes.c_int32,
  1369. ]
  1370. lib.ggml_set_i32_nd.restype = None
  1371. # GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
  1372. def ggml_get_f32_1d(
  1373. tensor: ggml_tensor_p,
  1374. i: Union[ctypes.c_int, int],
  1375. ) -> float:
  1376. """Get the float value of the i-th element in a 1-dimensional tensor.
  1377. Parameters:
  1378. tensor: tensor
  1379. Returns:
  1380. float value of element at index i"""
  1381. return lib.ggml_get_f32_1d(tensor, i)
  1382. lib.ggml_get_f32_1d.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int]
  1383. lib.ggml_get_f32_1d.restype = ctypes.c_float
  1384. # GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
  1385. def ggml_set_f32_1d(
  1386. tensor: ggml_tensor_p,
  1387. i: Union[ctypes.c_int, int],
  1388. value: Union[ctypes.c_float, float],
  1389. ):
  1390. """Set the float value of the i-th element in a 1-dimensional tensor.
  1391. Parameters:
  1392. tensor: tensor
  1393. i: index of element
  1394. value: float value to set element to"""
  1395. return lib.ggml_set_f32_1d(tensor, i, value)
  1396. lib.ggml_set_f32_1d.argtypes = [
  1397. ctypes.POINTER(ggml_tensor),
  1398. ctypes.c_int,
  1399. ctypes.c_float,
  1400. ]
  1401. lib.ggml_set_f32_1d.restype = None
  1402. # GGML_API float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
  1403. def ggml_get_f32_nd(
  1404. tensor: ggml_tensor_p,
  1405. i0: Union[ctypes.c_int, int],
  1406. i1: Union[ctypes.c_int, int],
  1407. i2: Union[ctypes.c_int, int],
  1408. i3: Union[ctypes.c_int, int],
  1409. ) -> float:
  1410. """Get the float value of the element at the given coordinates in a 4-dimensional tensor.
  1411. Parameters:
  1412. tensor: tensor
  1413. i0: index of element in dimension 0
  1414. i1: index of element in dimension 1
  1415. i2: index of element in dimension 2
  1416. i3: index of element in dimension 3
  1417. Returns:
  1418. float value of element at coordinates"""
  1419. return lib.ggml_get_f32_nd(tensor, i0, i1, i2, i3)
  1420. lib.ggml_get_f32_nd.argtypes = [
  1421. ctypes.POINTER(ggml_tensor),
  1422. ctypes.c_int,
  1423. ctypes.c_int,
  1424. ctypes.c_int,
  1425. ctypes.c_int,
  1426. ]
  1427. lib.ggml_get_f32_nd.restype = ctypes.c_float
  1428. # GGML_API void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value);
  1429. def ggml_set_f32_nd(
  1430. tensor: ggml_tensor_p,
  1431. i0: Union[ctypes.c_int, int],
  1432. i1: Union[ctypes.c_int, int],
  1433. i2: Union[ctypes.c_int, int],
  1434. i3: Union[ctypes.c_int, int],
  1435. value: Union[ctypes.c_float, float],
  1436. ):
  1437. """Set the float value of the element at the given coordinates in a 4-dimensional tensor.
  1438. Parameters:
  1439. tensor: tensor
  1440. i0: index of element in dimension 0
  1441. i1: index of element in dimension 1
  1442. i2: index of element in dimension 2
  1443. i3: index of element in dimension 3
  1444. value: float value to set element to"""
  1445. return lib.ggml_set_f32_nd(tensor, i0, i1, i2, i3, value)
  1446. lib.ggml_set_f32_nd.argtypes = [
  1447. ctypes.POINTER(ggml_tensor),
  1448. ctypes.c_int,
  1449. ctypes.c_int,
  1450. ctypes.c_int,
  1451. ctypes.c_int,
  1452. ctypes.c_float,
  1453. ]
  1454. lib.ggml_set_f32_nd.restype = None
  1455. # GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
  1456. def ggml_get_data(
  1457. tensor: ggml_tensor_p,
  1458. ) -> Optional[ctypes.c_void_p]:
  1459. """Get the data pointer of a tensor.
  1460. Parameters:
  1461. tensor: tensor
  1462. Returns:
  1463. Pointer to data, or None if tensor has no data"""
  1464. return lib.ggml_get_data(tensor)
  1465. lib.ggml_get_data.argtypes = [ctypes.POINTER(ggml_tensor)]
  1466. lib.ggml_get_data.restype = ctypes.c_void_p
  1467. # GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
  1468. def ggml_get_data_f32(
  1469. tensor: ggml_tensor_p,
  1470. ) -> Optional[CFloatArray]:
  1471. """Get the data pointer of a tensor as a float array.
  1472. Parameters:
  1473. tensor: tensor
  1474. Returns:
  1475. (Optional[ctypes.Array[ctypes.c_float]]): array of float to data, or None if tensor has no data
  1476. """
  1477. return lib.ggml_get_data_f32(tensor)
  1478. lib.ggml_get_data_f32.argtypes = [ctypes.POINTER(ggml_tensor)]
  1479. lib.ggml_get_data_f32.restype = ctypes.POINTER(ctypes.c_float)
  1480. # GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
  1481. def ggml_get_unary_op(
  1482. tensor: ggml_tensor_p,
  1483. ) -> int:
  1484. """Get the unary operation of a tensor.
  1485. Parameters:
  1486. tensor: tensor
  1487. Returns:
  1488. unary operation"""
  1489. return lib.ggml_get_unary_op(tensor)
  1490. lib.ggml_get_unary_op.argtypes = [ctypes.POINTER(ggml_tensor)]
  1491. lib.ggml_get_unary_op.restype = ctypes.c_int
  1492. # GGML_API const char * ggml_get_name(const struct ggml_tensor * tensor);
  1493. def ggml_get_name(
  1494. tensor: ggml_tensor_p,
  1495. ) -> bytes:
  1496. """Get the name of a tensor.
  1497. Parameters:
  1498. tensor: tensor
  1499. Returns:
  1500. name of tensor"""
  1501. return lib.ggml_get_name(tensor)
  1502. lib.ggml_get_name.argtypes = [ctypes.POINTER(ggml_tensor)]
  1503. lib.ggml_get_name.restype = ctypes.c_char_p
  1504. # GGML_API struct ggml_tensor * ggml_set_name(struct ggml_tensor * tensor, const char * name);
  1505. def ggml_set_name(
  1506. tensor: ggml_tensor_p,
  1507. name: bytes,
  1508. ) -> ggml_tensor_p:
  1509. """Set the name of a tensor.
  1510. Parameters:
  1511. tensor: tensor
  1512. name: name to set tensor to
  1513. Returns:
  1514. Pointer to ggml_tensor"""
  1515. return lib.ggml_set_name(tensor, name)
  1516. lib.ggml_set_name.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_char_p]
  1517. lib.ggml_set_name.restype = ctypes.POINTER(ggml_tensor)
  1518. # GGML_API struct ggml_tensor * ggml_format_name(struct ggml_tensor * tensor, const char * fmt, ...);
  1519. def ggml_format_name(
  1520. tensor: ggml_tensor_p,
  1521. fmt: bytes,
  1522. *args: Sequence[Union[bool, int, float, str]],
  1523. ) -> ggml_tensor_p:
  1524. """Format the name of a tensor using the given format c string and arguments.
  1525. Parameters:
  1526. tensor: tensor
  1527. fmt: format c string
  1528. args: arguments to format string
  1529. Returns:
  1530. Pointer to ggml_tensor"""
  1531. return lib.ggml_format_name(tensor, fmt, *args)
  1532. lib.ggml_format_name.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_char_p]
  1533. lib.ggml_format_name.restype = ctypes.POINTER(ggml_tensor)
  1534. # //
  1535. # // operations on tensors with backpropagation
  1536. # //
  1537. # GGML_API struct ggml_tensor * ggml_dup(
  1538. # struct ggml_context * ctx,
  1539. # struct ggml_tensor * a);
  1540. def ggml_dup(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1541. return lib.ggml_dup(ctx, a)
  1542. lib.ggml_dup.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1543. lib.ggml_dup.restype = ctypes.POINTER(ggml_tensor)
  1544. # // in-place, returns view(a)
  1545. # GGML_API struct ggml_tensor * ggml_dup_inplace(
  1546. # struct ggml_context * ctx,
  1547. # struct ggml_tensor * a);
  1548. def ggml_dup_inplace(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1549. return lib.ggml_dup_inplace(ctx, a)
  1550. lib.ggml_dup_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1551. lib.ggml_dup_inplace.restype = ctypes.POINTER(ggml_tensor)
  1552. # GGML_API struct ggml_tensor * ggml_add(
  1553. # struct ggml_context * ctx,
  1554. # struct ggml_tensor * a,
  1555. # struct ggml_tensor * b);
  1556. def ggml_add(
  1557. ctx: ggml_context_p,
  1558. a: ggml_tensor_p,
  1559. b: ggml_tensor_p,
  1560. ) -> ggml_tensor_p:
  1561. """Add two tensors together and return the result.
  1562. Parameters:
  1563. ctx: ggml context
  1564. a: first tensor
  1565. b: second tensor
  1566. Returns:
  1567. Pointer to ggml_tensor"""
  1568. return lib.ggml_add(ctx, a, b)
  1569. lib.ggml_add.argtypes = [
  1570. ggml_context_p,
  1571. ctypes.POINTER(ggml_tensor),
  1572. ctypes.POINTER(ggml_tensor),
  1573. ]
  1574. lib.ggml_add.restype = ctypes.POINTER(ggml_tensor)
  1575. # GGML_API struct ggml_tensor * ggml_add_inplace(
  1576. # struct ggml_context * ctx,
  1577. # struct ggml_tensor * a,
  1578. # struct ggml_tensor * b);
  1579. def ggml_add_inplace(
  1580. ctx: ggml_context_p,
  1581. a: ggml_tensor_p,
  1582. b: ggml_tensor_p,
  1583. ) -> ggml_tensor_p:
  1584. """Add two tensors together and store the result in the first tensor.
  1585. Parameters:
  1586. ctx: ggml context
  1587. a: first tensor
  1588. b: second tensor
  1589. Returns:
  1590. Pointer to ggml_tensor"""
  1591. return lib.ggml_add_inplace(ctx, a, b)
  1592. lib.ggml_add_inplace.argtypes = [
  1593. ggml_context_p,
  1594. ctypes.POINTER(ggml_tensor),
  1595. ctypes.POINTER(ggml_tensor),
  1596. ]
  1597. lib.ggml_add_inplace.restype = ctypes.POINTER(ggml_tensor)
  1598. # GGML_API struct ggml_tensor * ggml_add_cast(
  1599. # struct ggml_context * ctx,
  1600. # struct ggml_tensor * a,
  1601. # struct ggml_tensor * b,
  1602. # enum ggml_type type);
  1603. def ggml_add_cast(
  1604. ctx: ggml_context_p,
  1605. a: ggml_tensor_p,
  1606. b: ggml_tensor_p,
  1607. type: Union[ctypes.c_int, int],
  1608. ) -> ggml_tensor_p:
  1609. """Add two tensors together and cast the result to the given type.
  1610. Parameters:
  1611. ctx: ggml context
  1612. a: first tensor
  1613. b: second tensor
  1614. type: type to cast result to
  1615. Returns:
  1616. Pointer to ggml_tensor"""
  1617. return lib.ggml_add_cast(ctx, a, b, type)
  1618. lib.ggml_add_cast.argtypes = [
  1619. ggml_context_p,
  1620. ctypes.POINTER(ggml_tensor),
  1621. ctypes.POINTER(ggml_tensor),
  1622. ctypes.c_int,
  1623. ]
  1624. lib.ggml_add_cast.restype = ctypes.POINTER(ggml_tensor)
  1625. # GGML_API struct ggml_tensor * ggml_add1(
  1626. # struct ggml_context * ctx,
  1627. # struct ggml_tensor * a,
  1628. # struct ggml_tensor * b);
  1629. def ggml_add1(
  1630. ctx: ggml_context_p,
  1631. a: ggml_tensor_p,
  1632. b: ggml_tensor_p,
  1633. ) -> ggml_tensor_p:
  1634. return lib.ggml_add1(ctx, a, b)
  1635. lib.ggml_add1.argtypes = [
  1636. ggml_context_p,
  1637. ctypes.POINTER(ggml_tensor),
  1638. ctypes.POINTER(ggml_tensor),
  1639. ]
  1640. lib.ggml_add1.restype = ctypes.POINTER(ggml_tensor)
  1641. # GGML_API struct ggml_tensor * ggml_add1_inplace(
  1642. # struct ggml_context * ctx,
  1643. # struct ggml_tensor * a,
  1644. # struct ggml_tensor * b);
  1645. def ggml_add1_inplace(
  1646. ctx: ggml_context_p,
  1647. a: ggml_tensor_p,
  1648. b: ggml_tensor_p,
  1649. ) -> ggml_tensor_p:
  1650. return lib.ggml_add1_inplace(ctx, a, b)
  1651. lib.ggml_add1_inplace.argtypes = [
  1652. ggml_context_p,
  1653. ctypes.POINTER(ggml_tensor),
  1654. ctypes.POINTER(ggml_tensor),
  1655. ]
  1656. lib.ggml_add1_inplace.restype = ctypes.POINTER(ggml_tensor)
  1657. # // dst = a
  1658. # // view(dst, nb1, nb2, nb3, offset) += b
  1659. # // return dst
  1660. # GGML_API struct ggml_tensor * ggml_acc(
  1661. # struct ggml_context * ctx,
  1662. # struct ggml_tensor * a,
  1663. # struct ggml_tensor * b,
  1664. # size_t nb1,
  1665. # size_t nb2,
  1666. # size_t nb3,
  1667. # size_t offset);
  1668. def ggml_acc(
  1669. ctx: ggml_context_p,
  1670. a: ggml_tensor_p,
  1671. b: ggml_tensor_p,
  1672. nb1: Union[ctypes.c_size_t, int],
  1673. nb2: Union[ctypes.c_size_t, int],
  1674. nb3: Union[ctypes.c_size_t, int],
  1675. offset: Union[ctypes.c_size_t, int],
  1676. ) -> ggml_tensor_p:
  1677. return lib.ggml_acc(ctx, a, b, nb1, nb2, nb3, offset)
  1678. lib.ggml_acc.argtypes = [
  1679. ggml_context_p,
  1680. ctypes.POINTER(ggml_tensor),
  1681. ctypes.POINTER(ggml_tensor),
  1682. ctypes.c_size_t,
  1683. ctypes.c_size_t,
  1684. ctypes.c_size_t,
  1685. ctypes.c_size_t,
  1686. ]
  1687. lib.ggml_acc.restype = ctypes.POINTER(ggml_tensor)
  1688. # GGML_API struct ggml_tensor * ggml_acc_inplace(
  1689. # struct ggml_context * ctx,
  1690. # struct ggml_tensor * a,
  1691. # struct ggml_tensor * b,
  1692. # size_t nb1,
  1693. # size_t nb2,
  1694. # size_t nb3,
  1695. # size_t offset);
  1696. def ggml_acc_inplace(
  1697. ctx: ggml_context_p,
  1698. a: ggml_tensor_p,
  1699. b: ggml_tensor_p,
  1700. nb1: Union[ctypes.c_size_t, int],
  1701. nb2: Union[ctypes.c_size_t, int],
  1702. nb3: Union[ctypes.c_size_t, int],
  1703. offset: Union[ctypes.c_size_t, int],
  1704. ) -> ggml_tensor_p:
  1705. return lib.ggml_acc_inplace(ctx, a, b, nb1, nb2, nb3, offset)
  1706. lib.ggml_acc_inplace.argtypes = [
  1707. ggml_context_p,
  1708. ctypes.POINTER(ggml_tensor),
  1709. ctypes.POINTER(ggml_tensor),
  1710. ctypes.c_size_t,
  1711. ctypes.c_size_t,
  1712. ctypes.c_size_t,
  1713. ctypes.c_size_t,
  1714. ]
  1715. lib.ggml_acc_inplace.restype = ctypes.POINTER(ggml_tensor)
  1716. # GGML_API struct ggml_tensor * ggml_sub(
  1717. # struct ggml_context * ctx,
  1718. # struct ggml_tensor * a,
  1719. # struct ggml_tensor * b);
  1720. def ggml_sub(
  1721. ctx: ggml_context_p,
  1722. a: ggml_tensor_p,
  1723. b: ggml_tensor_p,
  1724. ) -> ggml_tensor_p:
  1725. """Subtract two tensors and return the result.
  1726. Parameters:
  1727. ctx: ggml context
  1728. a: first tensor
  1729. b: second tensor
  1730. Returns:
  1731. Pointer to ggml_tensor"""
  1732. return lib.ggml_sub(ctx, a, b)
  1733. lib.ggml_sub.argtypes = [
  1734. ggml_context_p,
  1735. ctypes.POINTER(ggml_tensor),
  1736. ctypes.POINTER(ggml_tensor),
  1737. ]
  1738. lib.ggml_sub.restype = ctypes.POINTER(ggml_tensor)
  1739. # GGML_API struct ggml_tensor * ggml_sub_inplace(
  1740. # struct ggml_context * ctx,
  1741. # struct ggml_tensor * a,
  1742. # struct ggml_tensor * b);
  1743. def ggml_sub_inplace(
  1744. ctx: ggml_context_p,
  1745. a: ggml_tensor_p,
  1746. b: ggml_tensor_p,
  1747. ) -> ggml_tensor_p:
  1748. """Subtract two tensors and store the result in the first tensor.
  1749. Parameters:
  1750. ctx: ggml context
  1751. a: first tensor
  1752. b: second tensor
  1753. Returns:
  1754. Pointer to ggml_tensor"""
  1755. return lib.ggml_sub_inplace(ctx, a, b)
  1756. lib.ggml_sub_inplace.argtypes = [
  1757. ggml_context_p,
  1758. ctypes.POINTER(ggml_tensor),
  1759. ctypes.POINTER(ggml_tensor),
  1760. ]
  1761. lib.ggml_sub_inplace.restype = ctypes.POINTER(ggml_tensor)
  1762. # GGML_API struct ggml_tensor * ggml_mul(
  1763. # struct ggml_context * ctx,
  1764. # struct ggml_tensor * a,
  1765. # struct ggml_tensor * b);
  1766. def ggml_mul(
  1767. ctx: ggml_context_p,
  1768. a: ggml_tensor_p,
  1769. b: ggml_tensor_p,
  1770. ) -> ggml_tensor_p:
  1771. """Element-wise multiply two tensors and return the result.
  1772. Parameters:
  1773. ctx: ggml context
  1774. a: first tensor
  1775. b: second tensor
  1776. Returns:
  1777. Pointer to ggml_tensor"""
  1778. return lib.ggml_mul(ctx, a, b)
  1779. lib.ggml_mul.argtypes = [
  1780. ggml_context_p,
  1781. ctypes.POINTER(ggml_tensor),
  1782. ctypes.POINTER(ggml_tensor),
  1783. ]
  1784. lib.ggml_mul.restype = ctypes.POINTER(ggml_tensor)
  1785. # GGML_API struct ggml_tensor * ggml_mul_inplace(
  1786. # struct ggml_context * ctx,
  1787. # struct ggml_tensor * a,
  1788. # struct ggml_tensor * b);
  1789. def ggml_mul_inplace(
  1790. ctx: ggml_context_p,
  1791. a: ggml_tensor_p,
  1792. b: ggml_tensor_p,
  1793. ) -> ggml_tensor_p:
  1794. """Element-wise multiply two tensors and store the result in the first tensor.
  1795. Parameters:
  1796. ctx: ggml context
  1797. a: first tensor
  1798. b: second tensor
  1799. Returns:
  1800. Pointer to ggml_tensor"""
  1801. return lib.ggml_mul_inplace(ctx, a, b)
  1802. lib.ggml_mul_inplace.argtypes = [
  1803. ggml_context_p,
  1804. ctypes.POINTER(ggml_tensor),
  1805. ctypes.POINTER(ggml_tensor),
  1806. ]
  1807. lib.ggml_mul_inplace.restype = ctypes.POINTER(ggml_tensor)
  1808. # GGML_API struct ggml_tensor * ggml_div(
  1809. # struct ggml_context * ctx,
  1810. # struct ggml_tensor * a,
  1811. # struct ggml_tensor * b);
  1812. def ggml_div(
  1813. ctx: ggml_context_p,
  1814. a: ggml_tensor_p,
  1815. b: ggml_tensor_p,
  1816. ) -> ggml_tensor_p:
  1817. """Element-wise divide two tensors and return the result.
  1818. Parameters:
  1819. ctx: ggml context
  1820. a: first tensor
  1821. b: second tensor
  1822. Returns:
  1823. Pointer to ggml_tensor"""
  1824. return lib.ggml_div(ctx, a, b)
  1825. lib.ggml_div.argtypes = [
  1826. ggml_context_p,
  1827. ctypes.POINTER(ggml_tensor),
  1828. ctypes.POINTER(ggml_tensor),
  1829. ]
  1830. lib.ggml_div.restype = ctypes.POINTER(ggml_tensor)
  1831. # GGML_API struct ggml_tensor * ggml_div_inplace(
  1832. # struct ggml_context * ctx,
  1833. # struct ggml_tensor * a,
  1834. # struct ggml_tensor * b);
  1835. def ggml_div_inplace(
  1836. ctx: ggml_context_p,
  1837. a: ggml_tensor_p,
  1838. b: ggml_tensor_p,
  1839. ) -> ggml_tensor_p:
  1840. """Element-wise divide two tensors and store the result in the first tensor.
  1841. Parameters:
  1842. ctx: ggml context
  1843. a: first tensor
  1844. b: second tensor
  1845. Returns:
  1846. Pointer to ggml_tensor"""
  1847. return lib.ggml_div_inplace(ctx, a, b)
  1848. lib.ggml_div_inplace.argtypes = [
  1849. ggml_context_p,
  1850. ctypes.POINTER(ggml_tensor),
  1851. ctypes.POINTER(ggml_tensor),
  1852. ]
  1853. lib.ggml_div_inplace.restype = ctypes.POINTER(ggml_tensor)
  1854. # GGML_API struct ggml_tensor * ggml_sqr(
  1855. # struct ggml_context * ctx,
  1856. # struct ggml_tensor * a);
  1857. def ggml_sqr(
  1858. ctx: ggml_context_p,
  1859. a: ggml_tensor_p,
  1860. ) -> ggml_tensor_p:
  1861. """Square all elements in a tensor and return the result.
  1862. Parameters:
  1863. ctx: ggml context
  1864. a: tensor
  1865. Returns:
  1866. Pointer to ggml_tensor"""
  1867. return lib.ggml_sqr(ctx, a)
  1868. lib.ggml_sqr.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1869. lib.ggml_sqr.restype = ctypes.POINTER(ggml_tensor)
  1870. # GGML_API struct ggml_tensor * ggml_sqr_inplace(
  1871. # struct ggml_context * ctx,
  1872. # struct ggml_tensor * a);
  1873. def ggml_sqr_inplace(
  1874. ctx: ggml_context_p,
  1875. a: ggml_tensor_p,
  1876. ) -> ggml_tensor_p:
  1877. """Square all elements in a tensor and store the result in the first tensor.
  1878. Parameters:
  1879. ctx: ggml context
  1880. a: tensor
  1881. Returns:
  1882. Pointer to ggml_tensor"""
  1883. return lib.ggml_sqr_inplace(ctx, a)
  1884. lib.ggml_sqr_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1885. lib.ggml_sqr_inplace.restype = ctypes.POINTER(ggml_tensor)
  1886. # GGML_API struct ggml_tensor * ggml_sqrt(
  1887. # struct ggml_context * ctx,
  1888. # struct ggml_tensor * a);
  1889. def ggml_sqrt(
  1890. ctx: ggml_context_p,
  1891. a: ggml_tensor_p,
  1892. ) -> ggml_tensor_p:
  1893. """Square root all elements in a tensor and return the result.
  1894. Parameters:
  1895. ctx: ggml context
  1896. a: tensor
  1897. Returns:
  1898. Pointer to ggml_tensor"""
  1899. return lib.ggml_sqrt(ctx, a)
  1900. lib.ggml_sqrt.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1901. lib.ggml_sqrt.restype = ctypes.POINTER(ggml_tensor)
  1902. # GGML_API struct ggml_tensor * ggml_sqrt_inplace(
  1903. # struct ggml_context * ctx,
  1904. # struct ggml_tensor * a);
  1905. def ggml_sqrt_inplace(
  1906. ctx: ggml_context_p,
  1907. a: ggml_tensor_p,
  1908. ) -> ggml_tensor_p:
  1909. """Square root all elements in a tensor and store the result in the first tensor.
  1910. Parameters:
  1911. ctx: ggml context
  1912. Returns:
  1913. Pointer to ggml_tensor"""
  1914. return lib.ggml_sqrt_inplace(ctx, a)
  1915. lib.ggml_sqrt_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1916. lib.ggml_sqrt_inplace.restype = ctypes.POINTER(ggml_tensor)
  1917. # GGML_API struct ggml_tensor * ggml_log(
  1918. # struct ggml_context * ctx,
  1919. # struct ggml_tensor * a);
  1920. def ggml_log(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1921. """Take the natural logarithm of all elements in a tensor and return the result.
  1922. Parameters:
  1923. ctx: ggml context
  1924. a: tensor
  1925. Returns:
  1926. Pointer to ggml_tensor"""
  1927. return lib.ggml_log(ctx, a)
  1928. lib.ggml_log.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1929. lib.ggml_log.restype = ctypes.POINTER(ggml_tensor)
  1930. # GGML_API struct ggml_tensor * ggml_log_inplace(
  1931. # struct ggml_context * ctx,
  1932. # struct ggml_tensor * a);
  1933. def ggml_log_inplace(
  1934. ctx: ggml_context_p,
  1935. a: ggml_tensor_p,
  1936. ) -> ggml_tensor_p:
  1937. """Take the natural logarithm of all elements in a tensor and store the result in the first tensor.
  1938. Parameters:
  1939. ctx: ggml context
  1940. a: tensor
  1941. Returns:
  1942. Pointer to ggml_tensor"""
  1943. return lib.ggml_log_inplace(ctx, a)
  1944. lib.ggml_log_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1945. lib.ggml_log_inplace.restype = ctypes.POINTER(ggml_tensor)
  1946. # // return scalar
  1947. # GGML_API struct ggml_tensor * ggml_sum(
  1948. # struct ggml_context * ctx,
  1949. # struct ggml_tensor * a);
  1950. def ggml_sum(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1951. """Sum all elements in a tensor and return the result.
  1952. Parameters:
  1953. ctx: ggml context
  1954. a: tensor
  1955. Returns:
  1956. Pointer to ggml_tensor"""
  1957. return lib.ggml_sum(ctx, a)
  1958. lib.ggml_sum.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1959. lib.ggml_sum.restype = ctypes.POINTER(ggml_tensor)
  1960. # // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
  1961. # GGML_API struct ggml_tensor * ggml_sum_rows(
  1962. # struct ggml_context * ctx,
  1963. # struct ggml_tensor * a);
  1964. def ggml_sum_rows(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1965. """Sum all elements in a tensor along the first axis and return the result.
  1966. sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
  1967. Parameters:
  1968. ctx: ggml context
  1969. a: tensor
  1970. Returns:
  1971. Pointer to ggml_tensor"""
  1972. return lib.ggml_sum_rows(ctx, a)
  1973. lib.ggml_sum_rows.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1974. lib.ggml_sum_rows.restype = ctypes.POINTER(ggml_tensor)
  1975. # // mean along rows
  1976. # GGML_API struct ggml_tensor * ggml_mean(
  1977. # struct ggml_context * ctx,
  1978. # struct ggml_tensor * a);
  1979. def ggml_mean(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1980. """Take the mean of all elements in a tensor and return the result.
  1981. Parameters:
  1982. ctx: ggml context
  1983. a: tensor
  1984. Returns:
  1985. Pointer to ggml_tensor"""
  1986. return lib.ggml_mean(ctx, a)
  1987. lib.ggml_mean.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  1988. lib.ggml_mean.restype = ctypes.POINTER(ggml_tensor)
  1989. # // argmax along rows
  1990. # GGML_API struct ggml_tensor * ggml_argmax(
  1991. # struct ggml_context * ctx,
  1992. # struct ggml_tensor * a);
  1993. def ggml_argmax(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  1994. """Take the argmax of all elements in a tensor and return the result.
  1995. argmax along rows
  1996. Parameters:
  1997. ctx: ggml context
  1998. a: tensor
  1999. Returns:
  2000. Pointer to ggml_tensor"""
  2001. return lib.ggml_argmax(ctx, a)
  2002. lib.ggml_argmax.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2003. lib.ggml_argmax.restype = ctypes.POINTER(ggml_tensor)
  2004. # // if a is the same shape as b, and a is not parameter, return a
  2005. # // otherwise, return a new tensor: repeat(a) to fit in b
  2006. # GGML_API struct ggml_tensor * ggml_repeat(
  2007. # struct ggml_context * ctx,
  2008. # struct ggml_tensor * a,
  2009. # struct ggml_tensor * b);
  2010. def ggml_repeat(
  2011. ctx: ggml_context_p,
  2012. a: ggml_tensor_p,
  2013. b: ggml_tensor_p,
  2014. ) -> ggml_tensor_p:
  2015. """Repeat a tensor to fit the shape of another tensor.
  2016. If a is the same shape as b, and a is not parameter, return a
  2017. Parameters:
  2018. ctx: ggml context
  2019. a: tensor to repeat
  2020. b: tensor to fit
  2021. Returns:
  2022. Pointer to ggml_tensor"""
  2023. return lib.ggml_repeat(ctx, a, b)
  2024. lib.ggml_repeat.argtypes = [
  2025. ggml_context_p,
  2026. ctypes.POINTER(ggml_tensor),
  2027. ctypes.POINTER(ggml_tensor),
  2028. ]
  2029. lib.ggml_repeat.restype = ctypes.POINTER(ggml_tensor)
  2030. # // sums repetitions in a into shape of b
  2031. # GGML_API struct ggml_tensor * ggml_repeat_back(
  2032. # struct ggml_context * ctx,
  2033. # struct ggml_tensor * a,
  2034. # struct ggml_tensor * b);
  2035. def ggml_repeat_back(
  2036. ctx: ggml_context_p,
  2037. a: ggml_tensor_p,
  2038. b: ggml_tensor_p,
  2039. ) -> ggml_tensor_p:
  2040. return lib.ggml_repeat_back(ctx, a, b)
  2041. lib.ggml_repeat_back.argtypes = [
  2042. ggml_context_p,
  2043. ctypes.POINTER(ggml_tensor),
  2044. ctypes.POINTER(ggml_tensor),
  2045. ]
  2046. lib.ggml_repeat_back.restype = ctypes.POINTER(ggml_tensor)
  2047. # // concat a and b on dim 2
  2048. # // used in stable-diffusion
  2049. # GGML_API struct ggml_tensor * ggml_concat(
  2050. # struct ggml_context * ctx,
  2051. # struct ggml_tensor * a,
  2052. # struct ggml_tensor * b);
  2053. def ggml_concat(
  2054. ctx: ggml_context_p,
  2055. a: ggml_tensor_p,
  2056. b: ggml_tensor_p,
  2057. ) -> ggml_tensor_p:
  2058. """Concatenate two tensors along the second axis and return the result.
  2059. Parameters:
  2060. ctx: ggml context
  2061. a: first tensor
  2062. b: second tensor
  2063. Returns:
  2064. Pointer to ggml_tensor"""
  2065. return lib.ggml_concat(ctx, a, b)
  2066. lib.ggml_concat.argtypes = [
  2067. ggml_context_p,
  2068. ctypes.POINTER(ggml_tensor),
  2069. ctypes.POINTER(ggml_tensor),
  2070. ]
  2071. lib.ggml_concat.restype = ctypes.POINTER(ggml_tensor)
  2072. # GGML_API struct ggml_tensor * ggml_abs(
  2073. # struct ggml_context * ctx,
  2074. # struct ggml_tensor * a);
  2075. def ggml_abs(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2076. """Take the absolute value of all elements in a tensor and return the result.
  2077. Parameters:
  2078. ctx: ggml context
  2079. a: tensor
  2080. Returns:
  2081. Pointer to ggml_tensor"""
  2082. return lib.ggml_abs(ctx, a)
  2083. lib.ggml_abs.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2084. lib.ggml_abs.restype = ctypes.POINTER(ggml_tensor)
  2085. # GGML_API struct ggml_tensor * ggml_abs_inplace(
  2086. # struct ggml_context * ctx,
  2087. # struct ggml_tensor * a);
  2088. def ggml_abs_inplace(
  2089. ctx: ggml_context_p,
  2090. a: ggml_tensor_p,
  2091. ) -> ggml_tensor_p:
  2092. """Take the absolute value of all elements in a tensor and store the result in the first tensor.
  2093. Parameters:
  2094. ctx: ggml context
  2095. a: tensor
  2096. Returns:
  2097. Pointer to ggml_tensor"""
  2098. return lib.ggml_abs_inplace(ctx, a)
  2099. lib.ggml_abs_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2100. lib.ggml_abs_inplace.restype = ctypes.POINTER(ggml_tensor)
  2101. # GGML_API struct ggml_tensor * ggml_sgn(
  2102. # struct ggml_context * ctx,
  2103. # struct ggml_tensor * a);
  2104. def ggml_sgn(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2105. """Get the sign of all elements in a tensor and return the result.
  2106. Parameters:
  2107. ctx: ggml context
  2108. a: tensor
  2109. Returns:
  2110. Pointer to ggml_tensor"""
  2111. return lib.ggml_sgn(ctx, a)
  2112. lib.ggml_sgn.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2113. lib.ggml_sgn.restype = ctypes.POINTER(ggml_tensor)
  2114. # GGML_API struct ggml_tensor * ggml_sgn_inplace(
  2115. # struct ggml_context * ctx,
  2116. # struct ggml_tensor * a);
  2117. def ggml_sgn_inplace(
  2118. ctx: ggml_context_p,
  2119. a: ggml_tensor_p,
  2120. ) -> ggml_tensor_p:
  2121. """Get the sign of all elements in a tensor and store the result in the first tensor.
  2122. Parameters:
  2123. ctx: ggml context
  2124. a: tensor
  2125. Returns:
  2126. Pointer to ggml_tensor"""
  2127. return lib.ggml_sgn_inplace(ctx, a)
  2128. lib.ggml_sgn_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2129. lib.ggml_sgn_inplace.restype = ctypes.POINTER(ggml_tensor)
  2130. # GGML_API struct ggml_tensor * ggml_neg(
  2131. # struct ggml_context * ctx,
  2132. # struct ggml_tensor * a);
  2133. def ggml_neg(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2134. """Negate all elements in a tensor and return the result.
  2135. Parameters:
  2136. ctx: ggml context
  2137. a: tensor
  2138. Returns:
  2139. Pointer to ggml_tensor"""
  2140. return lib.ggml_neg(ctx, a)
  2141. lib.ggml_neg.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2142. lib.ggml_neg.restype = ctypes.POINTER(ggml_tensor)
  2143. # GGML_API struct ggml_tensor * ggml_neg_inplace(
  2144. # struct ggml_context * ctx,
  2145. # struct ggml_tensor * a);
  2146. def ggml_neg_inplace(
  2147. ctx: ggml_context_p,
  2148. a: ggml_tensor_p,
  2149. ) -> ggml_tensor_p:
  2150. """Negate all elements in a tensor and store the result in the first tensor.
  2151. Parameters:
  2152. ctx: ggml context
  2153. a: tensor
  2154. Returns:
  2155. Pointer to ggml_tensor"""
  2156. return lib.ggml_neg_inplace(ctx, a)
  2157. lib.ggml_neg_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2158. lib.ggml_neg_inplace.restype = ctypes.POINTER(ggml_tensor)
  2159. # GGML_API struct ggml_tensor * ggml_step(
  2160. # struct ggml_context * ctx,
  2161. # struct ggml_tensor * a);
  2162. def ggml_step(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2163. return lib.ggml_step(ctx, a)
  2164. lib.ggml_step.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2165. lib.ggml_step.restype = ctypes.POINTER(ggml_tensor)
  2166. # GGML_API struct ggml_tensor * ggml_tanh(
  2167. # struct ggml_context * ctx,
  2168. # struct ggml_tensor * a);
  2169. def ggml_tanh(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2170. """Apply the tanh activation function to all elements in a tensor and return the result.
  2171. Parameters:
  2172. ctx: ggml context
  2173. a: tensor
  2174. Returns:
  2175. Pointer to ggml_tensor"""
  2176. return lib.ggml_tanh(ctx, a)
  2177. lib.ggml_tanh.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2178. lib.ggml_tanh.restype = ctypes.POINTER(ggml_tensor)
  2179. # GGML_API struct ggml_tensor * ggml_tanh_inplace(
  2180. # struct ggml_context * ctx,
  2181. # struct ggml_tensor * a);
  2182. def ggml_tanh_inplace(
  2183. ctx: ggml_context_p,
  2184. a: ggml_tensor_p,
  2185. ) -> ggml_tensor_p:
  2186. """Apply the tanh activation function to all elements in a tensor and store the result in the first tensor.
  2187. Parameters:
  2188. ctx: ggml context
  2189. a: tensor
  2190. Returns:
  2191. Pointer to ggml_tensor"""
  2192. return lib.ggml_tanh_inplace(ctx, a)
  2193. lib.ggml_tanh_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2194. lib.ggml_tanh_inplace.restype = ctypes.POINTER(ggml_tensor)
  2195. # GGML_API struct ggml_tensor * ggml_elu(
  2196. # struct ggml_context * ctx,
  2197. # struct ggml_tensor * a);
  2198. def ggml_elu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2199. """Apply the ELU activation function to all elements in a tensor and return the result.
  2200. Parameters:
  2201. ctx: ggml context
  2202. a: tensor
  2203. Returns:
  2204. Pointer to ggml_tensor"""
  2205. return lib.ggml_elu(ctx, a)
  2206. lib.ggml_elu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2207. lib.ggml_elu.restype = ctypes.POINTER(ggml_tensor)
  2208. # GGML_API struct ggml_tensor * ggml_elu_inplace(
  2209. # struct ggml_context * ctx,
  2210. # struct ggml_tensor * a);
  2211. def ggml_elu_inplace(
  2212. ctx: ggml_context_p,
  2213. a: ggml_tensor_p,
  2214. ) -> ggml_tensor_p:
  2215. """Apply the ELU activation function to all elements in a tensor and store the result in the first tensor.
  2216. Parameters:
  2217. ctx: ggml context
  2218. a: tensor
  2219. Returns:
  2220. Pointer to ggml_tensor"""
  2221. return lib.ggml_elu_inplace(ctx, a)
  2222. lib.ggml_elu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2223. lib.ggml_elu_inplace.restype = ctypes.POINTER(ggml_tensor)
  2224. # GGML_API struct ggml_tensor * ggml_relu(
  2225. # struct ggml_context * ctx,
  2226. # struct ggml_tensor * a);
  2227. def ggml_relu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2228. """Apply the ReLU activation function to all elements in a tensor and return the result.
  2229. Parameters:
  2230. ctx: ggml context
  2231. a: tensor
  2232. Returns:
  2233. Pointer to ggml_tensor"""
  2234. return lib.ggml_relu(ctx, a)
  2235. lib.ggml_relu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2236. lib.ggml_relu.restype = ctypes.POINTER(ggml_tensor)
  2237. # GGML_API struct ggml_tensor * ggml_leaky_relu(
  2238. # struct ggml_context * ctx,
  2239. # struct ggml_tensor * a, float negative_slope, bool inplace);
  2240. def ggml_leaky_relu(
  2241. ctx: ggml_context_p, a: ggml_tensor_p, negative_slope: float, inplace: bool
  2242. ) -> ggml_tensor_p:
  2243. """Apply the Leaky ReLU activation function to all elements in a tensor and return the result.
  2244. Parameters:
  2245. ctx: ggml context
  2246. a: tensor
  2247. negative_slope: negative slope
  2248. inplace: whether to store the result in the first tensor
  2249. Returns:
  2250. Pointer to ggml_tensor"""
  2251. return lib.ggml_leaky_relu(ctx, a, negative_slope, inplace)
  2252. lib.ggml_leaky_relu.argtypes = [
  2253. ggml_context_p,
  2254. ctypes.POINTER(ggml_tensor),
  2255. ctypes.c_float,
  2256. ctypes.c_bool,
  2257. ]
  2258. lib.ggml_leaky_relu.restype = ctypes.POINTER(ggml_tensor)
  2259. # GGML_API struct ggml_tensor * ggml_relu_inplace(
  2260. # struct ggml_context * ctx,
  2261. # struct ggml_tensor * a);
  2262. def ggml_relu_inplace(
  2263. ctx: ggml_context_p,
  2264. a: ggml_tensor_p,
  2265. ) -> ggml_tensor_p:
  2266. """Apply the ReLU activation function to all elements in a tensor and store the result in the first tensor.
  2267. Parameters:
  2268. ctx: ggml context
  2269. a: tensor
  2270. Returns:
  2271. Pointer to ggml_tensor"""
  2272. return lib.ggml_relu_inplace(ctx, a)
  2273. lib.ggml_relu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2274. lib.ggml_relu_inplace.restype = ctypes.POINTER(ggml_tensor)
  2275. # GGML_API struct ggml_tensor * ggml_gelu(
  2276. # struct ggml_context * ctx,
  2277. # struct ggml_tensor * a);
  2278. def ggml_gelu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2279. """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and return the result.
  2280. Parameters:
  2281. ctx: ggml context
  2282. a: tensor
  2283. Returns:
  2284. Pointer to ggml_tensor"""
  2285. return lib.ggml_gelu(ctx, a)
  2286. lib.ggml_gelu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2287. lib.ggml_gelu.restype = ctypes.POINTER(ggml_tensor)
  2288. # GGML_API struct ggml_tensor * ggml_gelu_inplace(
  2289. # struct ggml_context * ctx,
  2290. # struct ggml_tensor * a);
  2291. def ggml_gelu_inplace(
  2292. ctx: ggml_context_p,
  2293. a: ggml_tensor_p,
  2294. ) -> ggml_tensor_p:
  2295. """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and store the result in the first tensor.
  2296. Parameters:
  2297. ctx: ggml context
  2298. a: tensor
  2299. Returns:
  2300. Pointer to ggml_tensor"""
  2301. return lib.ggml_gelu_inplace(ctx, a)
  2302. lib.ggml_gelu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2303. lib.ggml_gelu_inplace.restype = ctypes.POINTER(ggml_tensor)
  2304. # GGML_API struct ggml_tensor * ggml_gelu_quick(
  2305. # struct ggml_context * ctx,
  2306. # struct ggml_tensor * a);
  2307. def ggml_gelu_quick(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2308. """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and return the result.
  2309. Parameters:
  2310. ctx: ggml context
  2311. a: tensor
  2312. Returns:
  2313. Pointer to ggml_tensor"""
  2314. return lib.ggml_gelu_quick(ctx, a)
  2315. lib.ggml_gelu_quick.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2316. lib.ggml_gelu_quick.restype = ctypes.POINTER(ggml_tensor)
  2317. # GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
  2318. # struct ggml_context * ctx,
  2319. # struct ggml_tensor * a);
  2320. def ggml_gelu_quick_inplace(
  2321. ctx: ggml_context_p,
  2322. a: ggml_tensor_p,
  2323. ) -> ggml_tensor_p:
  2324. """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and store the result in the first tensor.
  2325. Parameters:
  2326. ctx: ggml context
  2327. a: tensor
  2328. Returns:
  2329. Pointer to ggml_tensor"""
  2330. return lib.ggml_gelu_quick_inplace(ctx, a)
  2331. lib.ggml_gelu_quick_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2332. lib.ggml_gelu_quick_inplace.restype = ctypes.POINTER(ggml_tensor)
  2333. # GGML_API struct ggml_tensor * ggml_silu(
  2334. # struct ggml_context * ctx,
  2335. # struct ggml_tensor * a);
  2336. def ggml_silu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2337. """Apply the Sigmoid Linear Unit activation function to all elements in a tensor and return the result.
  2338. Parameters:
  2339. ctx: ggml context
  2340. a: tensor
  2341. Returns:
  2342. Pointer to ggml_tensor"""
  2343. return lib.ggml_silu(ctx, a)
  2344. lib.ggml_silu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2345. lib.ggml_silu.restype = ctypes.POINTER(ggml_tensor)
  2346. # GGML_API struct ggml_tensor * ggml_silu_inplace(
  2347. # struct ggml_context * ctx,
  2348. # struct ggml_tensor * a);
  2349. def ggml_silu_inplace(
  2350. ctx: ggml_context_p,
  2351. a: ggml_tensor_p,
  2352. ) -> ggml_tensor_p:
  2353. """Apply the Sigmoid Linear Unit activation function to all elements in a tensor and store the result in the first tensor.
  2354. Parameters:
  2355. ctx: ggml context
  2356. a: tensor
  2357. Returns:
  2358. Pointer to ggml_tensor"""
  2359. return lib.ggml_silu_inplace(ctx, a)
  2360. lib.ggml_silu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2361. lib.ggml_silu_inplace.restype = ctypes.POINTER(ggml_tensor)
  2362. # // a - x
  2363. # // b - dy
  2364. # GGML_API struct ggml_tensor * ggml_silu_back(
  2365. # struct ggml_context * ctx,
  2366. # struct ggml_tensor * a,
  2367. # struct ggml_tensor * b);
  2368. def ggml_silu_back(
  2369. ctx: ggml_context_p,
  2370. a: ggml_tensor_p,
  2371. b: ggml_tensor_p,
  2372. ) -> ggml_tensor_p:
  2373. return lib.ggml_silu_back(ctx, a, b)
  2374. lib.ggml_silu_back.argtypes = [
  2375. ggml_context_p,
  2376. ctypes.POINTER(ggml_tensor),
  2377. ctypes.POINTER(ggml_tensor),
  2378. ]
  2379. lib.ggml_silu_back.restype = ctypes.POINTER(ggml_tensor)
  2380. # // normalize along rows
  2381. # GGML_API struct ggml_tensor * ggml_norm(
  2382. # struct ggml_context * ctx,
  2383. # struct ggml_tensor * a
  2384. # float eps);
  2385. def ggml_norm(
  2386. ctx: ggml_context_p,
  2387. a: ggml_tensor_p,
  2388. eps: Union[ctypes.c_float, float],
  2389. ) -> ggml_tensor_p:
  2390. """Normalize all elements in a tensor along the first axis and return the result.
  2391. normalize along rows.
  2392. Parameters:
  2393. ctx: ggml context
  2394. a: tensor
  2395. eps: minimum value to avoid division by zero
  2396. Returns:
  2397. Pointer to ggml_tensor"""
  2398. return lib.ggml_norm(ctx, a, eps)
  2399. lib.ggml_norm.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor), ctypes.c_float]
  2400. lib.ggml_norm.restype = ctypes.POINTER(ggml_tensor)
  2401. # GGML_API struct ggml_tensor * ggml_norm_inplace(
  2402. # struct ggml_context * ctx,
  2403. # struct ggml_tensor * a
  2404. # float eps);
  2405. def ggml_norm_inplace(
  2406. ctx: ggml_context_p,
  2407. a: ggml_tensor_p,
  2408. eps: Union[ctypes.c_float, float],
  2409. ) -> ggml_tensor_p:
  2410. """Normalize all elements in a tensor along the first axis and store the result in the first tensor.
  2411. normalize along rows.
  2412. Parameters:
  2413. ctx: ggml context
  2414. a: tensor
  2415. eps: minimum value to avoid division by zero
  2416. Returns:
  2417. Pointer to ggml_tensor"""
  2418. return lib.ggml_norm_inplace(ctx, a, eps)
  2419. lib.ggml_norm_inplace.argtypes = [
  2420. ggml_context_p,
  2421. ctypes.POINTER(ggml_tensor),
  2422. ctypes.c_float,
  2423. ]
  2424. lib.ggml_norm_inplace.restype = ctypes.POINTER(ggml_tensor)
  2425. # GGML_API struct ggml_tensor * ggml_rms_norm(
  2426. # struct ggml_context * ctx,
  2427. # struct ggml_tensor * a,
  2428. # float eps);
  2429. def ggml_rms_norm(
  2430. ctx: ggml_context_p,
  2431. a: ggml_tensor_p,
  2432. eps: Union[ctypes.c_float, float],
  2433. ) -> ggml_tensor_p:
  2434. """Compute the RMS norm of a tensor and return the result.
  2435. Parameters:
  2436. ctx: ggml context
  2437. a: tensor
  2438. eps: float
  2439. Returns:
  2440. Pointer to ggml_tensor"""
  2441. return lib.ggml_rms_norm(ctx, a, eps)
  2442. lib.ggml_rms_norm.argtypes = [
  2443. ggml_context_p,
  2444. ctypes.POINTER(ggml_tensor),
  2445. ctypes.c_float,
  2446. ]
  2447. lib.ggml_rms_norm.restype = ctypes.POINTER(ggml_tensor)
  2448. # GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
  2449. # struct ggml_context * ctx,
  2450. # struct ggml_tensor * a,
  2451. # float eps);
  2452. def ggml_rms_norm_inplace(
  2453. ctx: ggml_context_p,
  2454. a: ggml_tensor_p,
  2455. eps: Union[ctypes.c_float, float],
  2456. ) -> ggml_tensor_p:
  2457. return lib.ggml_rms_norm_inplace(ctx, a, eps)
  2458. lib.ggml_rms_norm_inplace.argtypes = [
  2459. ggml_context_p,
  2460. ctypes.POINTER(ggml_tensor),
  2461. ctypes.c_float,
  2462. ]
  2463. lib.ggml_rms_norm_inplace.restype = ctypes.POINTER(ggml_tensor)
  2464. # // group normalize along ne0*ne1*n_groups
  2465. # // used in stable-diffusion
  2466. # // TODO: eps is hardcoded to 1e-6 for now
  2467. # GGML_API struct ggml_tensor * ggml_group_norm(
  2468. # struct ggml_context * ctx,
  2469. # struct ggml_tensor * a,
  2470. # int n_groups);
  2471. def ggml_group_norm(
  2472. ctx: ggml_context_p,
  2473. a: ggml_tensor_p,
  2474. n_groups: int,
  2475. ) -> ggml_tensor_p:
  2476. """Group normalize a tensor and return the result.
  2477. Parameters:
  2478. ctx: ggml context
  2479. a: tensor
  2480. n_groups: int
  2481. Returns:
  2482. Pointer to ggml_tensor"""
  2483. return lib.ggml_group_norm(ctx, a, n_groups)
  2484. lib.ggml_group_norm.argtypes = [
  2485. ggml_context_p,
  2486. ctypes.POINTER(ggml_tensor),
  2487. ctypes.c_int,
  2488. ]
  2489. lib.ggml_group_norm.restype = ctypes.POINTER(ggml_tensor)
  2490. # GGML_API struct ggml_tensor * ggml_group_norm_inplace(
  2491. # struct ggml_context * ctx,
  2492. # struct ggml_tensor * a,
  2493. # int n_groups);
  2494. def ggml_group_norm_inplace(
  2495. ctx: ggml_context_p,
  2496. a: ggml_tensor_p,
  2497. n_groups: int,
  2498. ) -> ggml_tensor_p:
  2499. """Group normalize a tensor and store the result in the first tensor.
  2500. Parameters:
  2501. ctx: ggml context
  2502. a: tensor
  2503. n_groups: int
  2504. Returns:
  2505. Pointer to ggml_tensor"""
  2506. return lib.ggml_group_norm_inplace(ctx, a, n_groups)
  2507. lib.ggml_group_norm_inplace.argtypes = [
  2508. ggml_context_p,
  2509. ctypes.POINTER(ggml_tensor),
  2510. ctypes.c_int,
  2511. ]
  2512. lib.ggml_group_norm_inplace.restype = ctypes.POINTER(ggml_tensor)
  2513. # // a - x
  2514. # // b - dy
  2515. # GGML_API struct ggml_tensor * ggml_rms_norm_back(
  2516. # struct ggml_context * ctx,
  2517. # struct ggml_tensor * a,
  2518. # struct ggml_tensor * b
  2519. # float eps);
  2520. def ggml_rms_norm_back(
  2521. ctx: ggml_context_p,
  2522. a: ggml_tensor_p,
  2523. b: ggml_tensor_p,
  2524. eps: Union[ctypes.c_float, float],
  2525. ) -> ggml_tensor_p:
  2526. return lib.ggml_rms_norm_back(ctx, a, b, eps)
  2527. lib.ggml_rms_norm_back.argtypes = [
  2528. ggml_context_p,
  2529. ctypes.POINTER(ggml_tensor),
  2530. ctypes.POINTER(ggml_tensor),
  2531. ctypes.c_float,
  2532. ]
  2533. lib.ggml_rms_norm_back.restype = ctypes.POINTER(ggml_tensor)
  2534. # // A: k columns, n rows => [ne03, ne02, n, k]
  2535. # // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
  2536. # // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
  2537. # GGML_API struct ggml_tensor * ggml_mul_mat(
  2538. # struct ggml_context * ctx,
  2539. # struct ggml_tensor * a,
  2540. # struct ggml_tensor * b);
  2541. def ggml_mul_mat(
  2542. ctx: ggml_context_p,
  2543. a: ggml_tensor_p,
  2544. b: ggml_tensor_p,
  2545. ) -> ggml_tensor_p:
  2546. """Multiply two matrices and return the result.
  2547. A: k columns, n rows => [ne03, ne02, n, k]
  2548. B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
  2549. result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
  2550. Parameters:
  2551. ctx: ggml context
  2552. a: tensor
  2553. b: tensor
  2554. Returns:
  2555. Pointer to ggml_tensor"""
  2556. return lib.ggml_mul_mat(ctx, a, b)
  2557. lib.ggml_mul_mat.argtypes = [
  2558. ggml_context_p,
  2559. ctypes.POINTER(ggml_tensor),
  2560. ctypes.POINTER(ggml_tensor),
  2561. ]
  2562. lib.ggml_mul_mat.restype = ctypes.POINTER(ggml_tensor)
  2563. # // indirect matrix multiplication
  2564. # // ggml_mul_mat_id(ctx, as, ids, id, b) ~= ggml_mul_mat(as[ids[id]], b)
  2565. # GGML_API struct ggml_tensor * ggml_mul_mat_id(
  2566. # struct ggml_context * ctx,
  2567. # struct ggml_tensor * const as[],
  2568. # int n_as,
  2569. # struct ggml_tensor * ids,
  2570. # int id,
  2571. # struct ggml_tensor * b);
  2572. def ggml_mul_mat_id(
  2573. ctx: ggml_context_p,
  2574. as_, # type: ctypes.POINTER(ctypes.POINTER(ggml_tensor)) # type: ignore
  2575. n_as: int,
  2576. ids: ggml_tensor_p,
  2577. id_: int,
  2578. b: ggml_tensor_p,
  2579. ) -> ggml_tensor_p:
  2580. """Multiply two matrices and return the result.
  2581. indirect matrix multiplication
  2582. ggml_mul_mat_id(ctx, as, ids, id, b) ~= ggml_mul_mat(as[ids[id]], b)
  2583. Parameters:
  2584. ctx: ggml context
  2585. as_: array of tensor pointers
  2586. n_as: int
  2587. ids: tensor
  2588. id_: int
  2589. b: tensor
  2590. Returns:
  2591. Pointer to ggml_tensor"""
  2592. return lib.ggml_mul_mat_id(ctx, as_, n_as, ids, id_, b)
  2593. lib.ggml_mul_mat_id.argtypes = [
  2594. ggml_context_p,
  2595. ctypes.POINTER(ctypes.POINTER(ggml_tensor)),
  2596. ctypes.c_int,
  2597. ctypes.POINTER(ggml_tensor),
  2598. ctypes.c_int,
  2599. ctypes.POINTER(ggml_tensor),
  2600. ]
  2601. lib.ggml_mul_mat_id.restype = ctypes.POINTER(ggml_tensor)
  2602. # // A: m columns, n rows,
  2603. # // B: p columns, n rows,
  2604. # // result is m columns, p rows
  2605. # GGML_API struct ggml_tensor * ggml_out_prod(
  2606. # struct ggml_context * ctx,
  2607. # struct ggml_tensor * a,
  2608. # struct ggml_tensor * b);
  2609. def ggml_out_prod(
  2610. ctx: ggml_context_p,
  2611. a: ggml_tensor_p,
  2612. b: ggml_tensor_p,
  2613. ) -> ggml_tensor_p:
  2614. """Compute the outer product of two matrices and return the result.
  2615. A: m columns, n rows,
  2616. B: p columns, n rows,
  2617. result is m columns, p rows
  2618. Parameters:
  2619. ctx: ggml context
  2620. a: tensor
  2621. b: tensor
  2622. Returns:
  2623. Pointer to ggml_tensor"""
  2624. return lib.ggml_out_prod(ctx, a, b)
  2625. lib.ggml_out_prod.argtypes = [
  2626. ggml_context_p,
  2627. ctypes.POINTER(ggml_tensor),
  2628. ctypes.POINTER(ggml_tensor),
  2629. ]
  2630. lib.ggml_out_prod.restype = ctypes.POINTER(ggml_tensor)
  2631. # //
  2632. # // operations on tensors without backpropagation
  2633. # //
  2634. # GGML_API struct ggml_tensor * ggml_scale(
  2635. # struct ggml_context * ctx,
  2636. # struct ggml_tensor * a,
  2637. # struct ggml_tensor * b);
  2638. def ggml_scale(
  2639. ctx: ggml_context_p,
  2640. a: ggml_tensor_p,
  2641. b: ggml_tensor_p,
  2642. ) -> ggml_tensor_p:
  2643. """Scale a tensor by another tensor and return the result.
  2644. Parameters:
  2645. ctx: ggml context
  2646. a: tensor
  2647. b: tensor
  2648. Returns:
  2649. Pointer to ggml_tensor"""
  2650. return lib.ggml_scale(ctx, a, b)
  2651. lib.ggml_scale.argtypes = [
  2652. ggml_context_p,
  2653. ctypes.POINTER(ggml_tensor),
  2654. ctypes.POINTER(ggml_tensor),
  2655. ]
  2656. lib.ggml_scale.restype = ctypes.POINTER(ggml_tensor)
  2657. # // in-place, returns view(a)
  2658. # GGML_API struct ggml_tensor * ggml_scale_inplace(
  2659. # struct ggml_context * ctx,
  2660. # struct ggml_tensor * a,
  2661. # struct ggml_tensor * b);
  2662. def ggml_scale_inplace(
  2663. ctx: ggml_context_p,
  2664. a: ggml_tensor_p,
  2665. b: ggml_tensor_p,
  2666. ) -> ggml_tensor_p:
  2667. """Scale a tensor by another tensor and store the result in the first tensor.
  2668. Parameters:
  2669. ctx: ggml context
  2670. a: tensor
  2671. Returns:
  2672. Pointer to ggml_tensor"""
  2673. return lib.ggml_scale_inplace(ctx, a, b)
  2674. lib.ggml_scale_inplace.argtypes = [
  2675. ggml_context_p,
  2676. ctypes.POINTER(ggml_tensor),
  2677. ctypes.POINTER(ggml_tensor),
  2678. ]
  2679. lib.ggml_scale_inplace.restype = ctypes.POINTER(ggml_tensor)
  2680. # // b -> view(a,offset,nb1,nb2,3), return modified a
  2681. # GGML_API struct ggml_tensor * ggml_set(
  2682. # struct ggml_context * ctx,
  2683. # struct ggml_tensor * a,
  2684. # struct ggml_tensor * b,
  2685. # size_t nb1,
  2686. # size_t nb2,
  2687. # size_t nb3,
  2688. # size_t offset);
  2689. def ggml_set(
  2690. ctx: ggml_context_p,
  2691. a: ggml_tensor_p,
  2692. b: ggml_tensor_p,
  2693. nb1: Union[ctypes.c_size_t, int],
  2694. nb2: Union[ctypes.c_size_t, int],
  2695. nb3: Union[ctypes.c_size_t, int],
  2696. offset: Union[ctypes.c_size_t, int],
  2697. ) -> ggml_tensor_p:
  2698. return lib.ggml_set(ctx, a, b, nb1, nb2, nb3, offset)
  2699. lib.ggml_set.argtypes = [
  2700. ggml_context_p,
  2701. ctypes.POINTER(ggml_tensor),
  2702. ctypes.POINTER(ggml_tensor),
  2703. ctypes.c_size_t,
  2704. ctypes.c_size_t,
  2705. ctypes.c_size_t,
  2706. ctypes.c_size_t,
  2707. ]
  2708. lib.ggml_set.restype = ctypes.POINTER(ggml_tensor)
  2709. # // b -> view(a,offset,nb1,nb2,3), return view(a)
  2710. # GGML_API struct ggml_tensor * ggml_set_inplace(
  2711. # struct ggml_context * ctx,
  2712. # struct ggml_tensor * a,
  2713. # struct ggml_tensor * b,
  2714. # size_t nb1,
  2715. # size_t nb2,
  2716. # size_t nb3,
  2717. # size_t offset);
  2718. def ggml_set_inplace(
  2719. ctx: ggml_context_p,
  2720. a: ggml_tensor_p,
  2721. b: ggml_tensor_p,
  2722. nb1: Union[ctypes.c_size_t, int],
  2723. nb2: Union[ctypes.c_size_t, int],
  2724. nb3: Union[ctypes.c_size_t, int],
  2725. offset: Union[ctypes.c_size_t, int],
  2726. ) -> ggml_tensor_p:
  2727. return lib.ggml_set_inplace(ctx, a, b, nb1, nb2, nb3, offset)
  2728. lib.ggml_set_inplace.argtypes = [
  2729. ggml_context_p,
  2730. ctypes.POINTER(ggml_tensor),
  2731. ctypes.POINTER(ggml_tensor),
  2732. ctypes.c_size_t,
  2733. ctypes.c_size_t,
  2734. ctypes.c_size_t,
  2735. ctypes.c_size_t,
  2736. ]
  2737. lib.ggml_set_inplace.restype = ctypes.POINTER(ggml_tensor)
  2738. # GGML_API struct ggml_tensor * ggml_set_1d(
  2739. # struct ggml_context * ctx,
  2740. # struct ggml_tensor * a,
  2741. # struct ggml_tensor * b,
  2742. # size_t offset);
  2743. def ggml_set_1d(
  2744. ctx: ggml_context_p,
  2745. a: ggml_tensor_p,
  2746. b: ggml_tensor_p,
  2747. offset: Union[ctypes.c_size_t, int],
  2748. ) -> ggml_tensor_p:
  2749. return lib.ggml_set_1d(ctx, a, b, offset)
  2750. lib.ggml_set_1d.argtypes = [
  2751. ggml_context_p,
  2752. ctypes.POINTER(ggml_tensor),
  2753. ctypes.POINTER(ggml_tensor),
  2754. ctypes.c_size_t,
  2755. ]
  2756. lib.ggml_set_1d.restype = ctypes.POINTER(ggml_tensor)
  2757. # GGML_API struct ggml_tensor * ggml_set_1d_inplace(
  2758. # struct ggml_context * ctx,
  2759. # struct ggml_tensor * a,
  2760. # struct ggml_tensor * b,
  2761. # size_t offset);
  2762. def ggml_set_1d_inplace(
  2763. ctx: ggml_context_p,
  2764. a: ggml_tensor_p,
  2765. b: ggml_tensor_p,
  2766. offset: Union[ctypes.c_size_t, int],
  2767. ) -> ggml_tensor_p:
  2768. return lib.ggml_set_1d_inplace(ctx, a, b, offset)
  2769. lib.ggml_set_1d_inplace.argtypes = [
  2770. ggml_context_p,
  2771. ctypes.POINTER(ggml_tensor),
  2772. ctypes.POINTER(ggml_tensor),
  2773. ctypes.c_size_t,
  2774. ]
  2775. lib.ggml_set_1d_inplace.restype = ctypes.POINTER(ggml_tensor)
  2776. # // b -> view(a,offset,nb1,nb2,3), return modified a
  2777. # GGML_API struct ggml_tensor * ggml_set_2d(
  2778. # struct ggml_context * ctx,
  2779. # struct ggml_tensor * a,
  2780. # struct ggml_tensor * b,
  2781. # size_t nb1,
  2782. # size_t offset);
  2783. def ggml_set_2d(
  2784. ctx: ggml_context_p,
  2785. a: ggml_tensor_p,
  2786. b: ggml_tensor_p,
  2787. nb1: Union[ctypes.c_size_t, int],
  2788. offset: Union[ctypes.c_size_t, int],
  2789. ) -> ggml_tensor_p:
  2790. return lib.ggml_set_2d(ctx, a, b, nb1, offset)
  2791. lib.ggml_set_2d.argtypes = [
  2792. ggml_context_p,
  2793. ctypes.POINTER(ggml_tensor),
  2794. ctypes.POINTER(ggml_tensor),
  2795. ctypes.c_size_t,
  2796. ctypes.c_size_t,
  2797. ]
  2798. lib.ggml_set_2d.restype = ctypes.POINTER(ggml_tensor)
  2799. # // b -> view(a,offset,nb1,nb2,3), return view(a)
  2800. # GGML_API struct ggml_tensor * ggml_set_2d_inplace(
  2801. # struct ggml_context * ctx,
  2802. # struct ggml_tensor * a,
  2803. # struct ggml_tensor * b,
  2804. # size_t nb1,
  2805. # size_t offset);
  2806. def ggml_set_2d_inplace(
  2807. ctx: ggml_context_p,
  2808. a: ggml_tensor_p,
  2809. b: ggml_tensor_p,
  2810. nb1: Union[ctypes.c_size_t, int],
  2811. offset: Union[ctypes.c_size_t, int],
  2812. ) -> ggml_tensor_p:
  2813. return lib.ggml_set_2d_inplace(ctx, a, b, nb1, offset)
  2814. lib.ggml_set_2d_inplace.argtypes = [
  2815. ggml_context_p,
  2816. ctypes.POINTER(ggml_tensor),
  2817. ctypes.POINTER(ggml_tensor),
  2818. ctypes.c_size_t,
  2819. ctypes.c_size_t,
  2820. ]
  2821. lib.ggml_set_2d_inplace.restype = ctypes.POINTER(ggml_tensor)
  2822. # // a -> b, return view(b)
  2823. # GGML_API struct ggml_tensor * ggml_cpy(
  2824. # struct ggml_context * ctx,
  2825. # struct ggml_tensor * a,
  2826. # struct ggml_tensor * b);
  2827. def ggml_cpy(
  2828. ctx: ggml_context_p,
  2829. a: ggml_tensor_p,
  2830. b: ggml_tensor_p,
  2831. ) -> ggml_tensor_p:
  2832. return lib.ggml_cpy(ctx, a, b)
  2833. lib.ggml_cpy.argtypes = [
  2834. ggml_context_p,
  2835. ctypes.POINTER(ggml_tensor),
  2836. ctypes.POINTER(ggml_tensor),
  2837. ]
  2838. lib.ggml_cpy.restype = ctypes.POINTER(ggml_tensor)
  2839. # // a -> b, in-place, return view(b)
  2840. # GGML_API struct ggml_tensor * ggml_cpy_inplace(
  2841. # struct ggml_context * ctx,
  2842. # struct ggml_tensor * a,
  2843. # struct ggml_tensor * b);
  2844. def ggml_cpy_inplace(
  2845. ctx: ggml_context_p,
  2846. a: ggml_tensor_p,
  2847. b: ggml_tensor_p,
  2848. ) -> ggml_tensor_p:
  2849. return lib.ggml_cpy_inplace(ctx, a, b)
  2850. lib.ggml_cpy_inplace.argtypes = [
  2851. ggml_context_p,
  2852. ctypes.POINTER(ggml_tensor),
  2853. ctypes.POINTER(ggml_tensor),
  2854. ]
  2855. lib.ggml_cpy_inplace.restype = ctypes.POINTER(ggml_tensor)
  2856. # // make contiguous
  2857. # GGML_API struct ggml_tensor * ggml_cont(
  2858. # struct ggml_context * ctx,
  2859. # struct ggml_tensor * a);
  2860. def ggml_cont(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  2861. """Make a tensor contiguous and return the result.
  2862. Parameters:
  2863. ctx: ggml context
  2864. a: tensor
  2865. Returns:
  2866. Pointer to ggml_tensor"""
  2867. return lib.ggml_cont(ctx, a)
  2868. lib.ggml_cont.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2869. lib.ggml_cont.restype = ctypes.POINTER(ggml_tensor)
  2870. # // make contiguous, in-place
  2871. # GGML_API struct ggml_tensor * ggml_cont_inplace(
  2872. # struct ggml_context * ctx,
  2873. # struct ggml_tensor * a);
  2874. def ggml_cont_inplace(
  2875. ctx: ggml_context_p,
  2876. a: ggml_tensor_p,
  2877. ) -> ggml_tensor_p:
  2878. """Make a tensor contiguous and store the result in the first tensor.
  2879. Parameters:
  2880. ctx: ggml context
  2881. a: tensor
  2882. Returns:
  2883. Pointer to ggml_tensor"""
  2884. return lib.ggml_cont_inplace(ctx, a)
  2885. lib.ggml_cont_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  2886. lib.ggml_cont_inplace.restype = ctypes.POINTER(ggml_tensor)
  2887. # // make contiguous, with new shape
  2888. # GGML_API struct ggml_tensor * ggml_cont_1d(
  2889. # struct ggml_context * ctx,
  2890. # struct ggml_tensor * a,
  2891. # int64_t ne0);
  2892. def ggml_cont_1d(
  2893. ctx: ggml_context_p,
  2894. a: ggml_tensor_p,
  2895. ne0: Union[ctypes.c_int64, int],
  2896. ) -> ggml_tensor_p:
  2897. return lib.ggml_cont_1d(ctx, a, ne0)
  2898. lib.ggml_cont_1d.argtypes = [
  2899. ggml_context_p,
  2900. ctypes.POINTER(ggml_tensor),
  2901. ctypes.c_int64,
  2902. ]
  2903. lib.ggml_cont_1d.restype = ctypes.POINTER(ggml_tensor)
  2904. # GGML_API struct ggml_tensor * ggml_cont_2d(
  2905. # struct ggml_context * ctx,
  2906. # struct ggml_tensor * a,
  2907. # int64_t ne0,
  2908. # int64_t ne1);
  2909. def ggml_cont_2d(
  2910. ctx: ggml_context_p,
  2911. a: ggml_tensor_p,
  2912. ne0: Union[ctypes.c_int64, int],
  2913. ne1: Union[ctypes.c_int64, int],
  2914. ) -> ggml_tensor_p:
  2915. return lib.ggml_cont_2d(ctx, a, ne0, ne1)
  2916. lib.ggml_cont_2d.argtypes = [
  2917. ggml_context_p,
  2918. ctypes.POINTER(ggml_tensor),
  2919. ctypes.c_int64,
  2920. ctypes.c_int64,
  2921. ]
  2922. lib.ggml_cont_2d.restype = ctypes.POINTER(ggml_tensor)
  2923. # GGML_API struct ggml_tensor * ggml_cont_3d(
  2924. # struct ggml_context * ctx,
  2925. # struct ggml_tensor * a,
  2926. # int64_t ne0,
  2927. # int64_t ne1,
  2928. # int64_t ne2);
  2929. def ggml_cont_3d(
  2930. ctx: ggml_context_p,
  2931. a: ggml_tensor_p,
  2932. ne0: Union[ctypes.c_int64, int],
  2933. ne1: Union[ctypes.c_int64, int],
  2934. ne2: Union[ctypes.c_int64, int],
  2935. ) -> ggml_tensor_p:
  2936. return lib.ggml_cont_3d(ctx, a, ne0, ne1, ne2)
  2937. lib.ggml_cont_3d.argtypes = [
  2938. ggml_context_p,
  2939. ctypes.POINTER(ggml_tensor),
  2940. ctypes.c_int64,
  2941. ctypes.c_int64,
  2942. ctypes.c_int64,
  2943. ]
  2944. lib.ggml_cont_3d.restype = ctypes.POINTER(ggml_tensor)
  2945. # GGML_API struct ggml_tensor * ggml_cont_4d(
  2946. # struct ggml_context * ctx,
  2947. # struct ggml_tensor * a,
  2948. # int64_t ne0,
  2949. # int64_t ne1,
  2950. # int64_t ne2,
  2951. # int64_t ne3);
  2952. def ggml_cont_4d(
  2953. ctx: ggml_context_p,
  2954. a: ggml_tensor_p,
  2955. ne0: Union[ctypes.c_int64, int],
  2956. ne1: Union[ctypes.c_int64, int],
  2957. ne2: Union[ctypes.c_int64, int],
  2958. ne3: Union[ctypes.c_int64, int],
  2959. ) -> ggml_tensor_p:
  2960. return lib.ggml_cont_4d(ctx, a, ne0, ne1, ne2, ne3)
  2961. lib.ggml_cont_4d.argtypes = [
  2962. ggml_context_p,
  2963. ctypes.POINTER(ggml_tensor),
  2964. ctypes.c_int64,
  2965. ctypes.c_int64,
  2966. ctypes.c_int64,
  2967. ctypes.c_int64,
  2968. ]
  2969. lib.ggml_cont_4d.restype = ctypes.POINTER(ggml_tensor)
  2970. # // return view(a), b specifies the new shape
  2971. # // TODO: when we start computing gradient, make a copy instead of view
  2972. # GGML_API struct ggml_tensor * ggml_reshape(
  2973. # struct ggml_context * ctx,
  2974. # struct ggml_tensor * a,
  2975. # struct ggml_tensor * b);
  2976. def ggml_reshape(
  2977. ctx: ggml_context_p,
  2978. a: ggml_tensor_p,
  2979. b: ggml_tensor_p,
  2980. ) -> ggml_tensor_p:
  2981. return lib.ggml_reshape(ctx, a, b)
  2982. lib.ggml_reshape.argtypes = [
  2983. ggml_context_p,
  2984. ctypes.POINTER(ggml_tensor),
  2985. ctypes.POINTER(ggml_tensor),
  2986. ]
  2987. lib.ggml_reshape.restype = ctypes.POINTER(ggml_tensor)
  2988. # // return view(a)
  2989. # // TODO: when we start computing gradient, make a copy instead of view
  2990. # GGML_API struct ggml_tensor * ggml_reshape_1d(
  2991. # struct ggml_context * ctx,
  2992. # struct ggml_tensor * a,
  2993. # int64_t ne0);
  2994. def ggml_reshape_1d(
  2995. ctx: ggml_context_p,
  2996. a: ggml_tensor_p,
  2997. ne0: Union[ctypes.c_int64, int],
  2998. ) -> ggml_tensor_p:
  2999. return lib.ggml_reshape_1d(ctx, a, ne0)
  3000. lib.ggml_reshape_1d.argtypes = [
  3001. ggml_context_p,
  3002. ctypes.POINTER(ggml_tensor),
  3003. ctypes.c_int64,
  3004. ]
  3005. lib.ggml_reshape_1d.restype = ctypes.POINTER(ggml_tensor)
  3006. # GGML_API struct ggml_tensor * ggml_reshape_2d(
  3007. # struct ggml_context * ctx,
  3008. # struct ggml_tensor * a,
  3009. # int64_t ne0,
  3010. # int64_t ne1);
  3011. def ggml_reshape_2d(
  3012. ctx: ggml_context_p,
  3013. a: ggml_tensor_p,
  3014. ne0: Union[ctypes.c_int64, int],
  3015. ne1: Union[ctypes.c_int64, int],
  3016. ) -> ggml_tensor_p:
  3017. return lib.ggml_reshape_2d(ctx, a, ne0, ne1)
  3018. lib.ggml_reshape_2d.argtypes = [
  3019. ggml_context_p,
  3020. ctypes.POINTER(ggml_tensor),
  3021. ctypes.c_int64,
  3022. ctypes.c_int64,
  3023. ]
  3024. lib.ggml_reshape_2d.restype = ctypes.POINTER(ggml_tensor)
  3025. # // return view(a)
  3026. # // TODO: when we start computing gradient, make a copy instead of view
  3027. # GGML_API struct ggml_tensor * ggml_reshape_3d(
  3028. # struct ggml_context * ctx,
  3029. # struct ggml_tensor * a,
  3030. # int64_t ne0,
  3031. # int64_t ne1,
  3032. # int64_t ne2);
  3033. def ggml_reshape_3d(
  3034. ctx: ggml_context_p,
  3035. a: ggml_tensor_p,
  3036. ne0: Union[ctypes.c_int64, int],
  3037. ne1: Union[ctypes.c_int64, int],
  3038. ne2: Union[ctypes.c_int64, int],
  3039. ) -> ggml_tensor_p:
  3040. return lib.ggml_reshape_3d(ctx, a, ne0, ne1, ne2)
  3041. lib.ggml_reshape_3d.argtypes = [
  3042. ggml_context_p,
  3043. ctypes.POINTER(ggml_tensor),
  3044. ctypes.c_int64,
  3045. ctypes.c_int64,
  3046. ctypes.c_int64,
  3047. ]
  3048. lib.ggml_reshape_3d.restype = ctypes.POINTER(ggml_tensor)
  3049. # GGML_API struct ggml_tensor * ggml_reshape_4d(
  3050. # struct ggml_context * ctx,
  3051. # struct ggml_tensor * a,
  3052. # int64_t ne0,
  3053. # int64_t ne1,
  3054. # int64_t ne2,
  3055. # int64_t ne3);
  3056. def ggml_reshape_4d(
  3057. ctx: ggml_context_p,
  3058. a: ggml_tensor_p,
  3059. ne0: Union[ctypes.c_int64, int],
  3060. ne1: Union[ctypes.c_int64, int],
  3061. ne2: Union[ctypes.c_int64, int],
  3062. ne3: Union[ctypes.c_int64, int],
  3063. ) -> ggml_tensor_p:
  3064. return lib.ggml_reshape_4d(ctx, a, ne0, ne1, ne2, ne3)
  3065. lib.ggml_reshape_4d.argtypes = [
  3066. ggml_context_p,
  3067. ctypes.POINTER(ggml_tensor),
  3068. ctypes.c_int64,
  3069. ctypes.c_int64,
  3070. ctypes.c_int64,
  3071. ctypes.c_int64,
  3072. ]
  3073. lib.ggml_reshape_4d.restype = ctypes.POINTER(ggml_tensor)
  3074. # // offset in bytes
  3075. # GGML_API struct ggml_tensor * ggml_view_1d(
  3076. # struct ggml_context * ctx,
  3077. # struct ggml_tensor * a,
  3078. # int64_t ne0,
  3079. # size_t offset);
  3080. def ggml_view_1d(
  3081. ctx: ggml_context_p,
  3082. a: ggml_tensor_p,
  3083. ne0: Union[ctypes.c_int64, int],
  3084. offset: Union[ctypes.c_size_t, int],
  3085. ) -> ggml_tensor_p:
  3086. return lib.ggml_view_1d(ctx, a, ne0, offset)
  3087. lib.ggml_view_1d.argtypes = [
  3088. ggml_context_p,
  3089. ctypes.POINTER(ggml_tensor),
  3090. ctypes.c_int64,
  3091. ctypes.c_size_t,
  3092. ]
  3093. lib.ggml_view_1d.restype = ctypes.POINTER(ggml_tensor)
  3094. # GGML_API struct ggml_tensor * ggml_view_2d(
  3095. # struct ggml_context * ctx,
  3096. # struct ggml_tensor * a,
  3097. # int64_t ne0,
  3098. # int64_t ne1,
  3099. # size_t nb1, // row stride in bytes
  3100. # size_t offset);
  3101. def ggml_view_2d(
  3102. ctx: ggml_context_p,
  3103. a: ggml_tensor_p,
  3104. ne0: Union[ctypes.c_int64, int],
  3105. ne1: Union[ctypes.c_int64, int],
  3106. nb1: Union[ctypes.c_size_t, int],
  3107. offset: Union[ctypes.c_size_t, int],
  3108. ) -> ggml_tensor_p:
  3109. return lib.ggml_view_2d(ctx, a, ne0, ne1, nb1, offset)
  3110. lib.ggml_view_2d.argtypes = [
  3111. ggml_context_p,
  3112. ctypes.POINTER(ggml_tensor),
  3113. ctypes.c_int64,
  3114. ctypes.c_int64,
  3115. ctypes.c_size_t,
  3116. ctypes.c_size_t,
  3117. ]
  3118. lib.ggml_view_2d.restype = ctypes.POINTER(ggml_tensor)
  3119. # GGML_API struct ggml_tensor * ggml_view_3d(
  3120. # struct ggml_context * ctx,
  3121. # struct ggml_tensor * a,
  3122. # int64_t ne0,
  3123. # int64_t ne1,
  3124. # int64_t ne2,
  3125. # size_t nb1, // row stride in bytes
  3126. # size_t nb2, // slice stride in bytes
  3127. # size_t offset);
  3128. def ggml_view_3d(
  3129. ctx: ggml_context_p,
  3130. a: ggml_tensor_p,
  3131. ne0: Union[ctypes.c_int64, int],
  3132. ne1: Union[ctypes.c_int64, int],
  3133. ne2: Union[ctypes.c_int64, int],
  3134. nb1: Union[ctypes.c_size_t, int],
  3135. nb2: Union[ctypes.c_size_t, int],
  3136. offset: Union[ctypes.c_size_t, int],
  3137. ) -> ggml_tensor_p:
  3138. return lib.ggml_view_3d(ctx, a, ne0, ne1, ne2, nb1, nb2, offset)
  3139. lib.ggml_view_3d.argtypes = [
  3140. ggml_context_p,
  3141. ctypes.POINTER(ggml_tensor),
  3142. ctypes.c_int64,
  3143. ctypes.c_int64,
  3144. ctypes.c_int64,
  3145. ctypes.c_size_t,
  3146. ctypes.c_size_t,
  3147. ctypes.c_size_t,
  3148. ]
  3149. lib.ggml_view_3d.restype = ctypes.POINTER(ggml_tensor)
  3150. # GGML_API struct ggml_tensor * ggml_view_4d(
  3151. # struct ggml_context * ctx,
  3152. # struct ggml_tensor * a,
  3153. # int64_t ne0,
  3154. # int64_t ne1,
  3155. # int64_t ne2,
  3156. # int64_t ne3,
  3157. # size_t nb1, // row stride in bytes
  3158. # size_t nb2, // slice stride in bytes
  3159. # size_t nb3,
  3160. # size_t offset);
  3161. def ggml_view_4d(
  3162. ctx: ggml_context_p,
  3163. a: ggml_tensor_p,
  3164. ne0: Union[ctypes.c_int64, int],
  3165. ne1: Union[ctypes.c_int64, int],
  3166. ne2: Union[ctypes.c_int64, int],
  3167. ne3: Union[ctypes.c_int64, int],
  3168. nb1: Union[ctypes.c_size_t, int],
  3169. nb2: Union[ctypes.c_size_t, int],
  3170. nb3: Union[ctypes.c_size_t, int],
  3171. offset: Union[ctypes.c_size_t, int],
  3172. ) -> ggml_tensor_p:
  3173. return lib.ggml_view_4d(ctx, a, ne0, ne1, ne2, ne3, nb1, nb2, nb3, offset)
  3174. lib.ggml_view_4d.argtypes = [
  3175. ggml_context_p,
  3176. ctypes.POINTER(ggml_tensor),
  3177. ctypes.c_int64,
  3178. ctypes.c_int64,
  3179. ctypes.c_int64,
  3180. ctypes.c_int64,
  3181. ctypes.c_size_t,
  3182. ctypes.c_size_t,
  3183. ctypes.c_size_t,
  3184. ctypes.c_size_t,
  3185. ]
  3186. lib.ggml_view_4d.restype = ctypes.POINTER(ggml_tensor)
  3187. # GGML_API struct ggml_tensor * ggml_permute(
  3188. # struct ggml_context * ctx,
  3189. # struct ggml_tensor * a,
  3190. # int axis0,
  3191. # int axis1,
  3192. # int axis2,
  3193. # int axis3);
  3194. def ggml_permute(
  3195. ctx: ggml_context_p,
  3196. a: ggml_tensor_p,
  3197. axis0: Union[ctypes.c_int, int],
  3198. axis1: Union[ctypes.c_int, int],
  3199. axis2: Union[ctypes.c_int, int],
  3200. axis3: Union[ctypes.c_int, int],
  3201. ) -> ggml_tensor_p:
  3202. return lib.ggml_permute(ctx, a, axis0, axis1, axis2, axis3)
  3203. lib.ggml_permute.argtypes = [
  3204. ggml_context_p,
  3205. ctypes.POINTER(ggml_tensor),
  3206. ctypes.c_int,
  3207. ctypes.c_int,
  3208. ctypes.c_int,
  3209. ctypes.c_int,
  3210. ]
  3211. lib.ggml_permute.restype = ctypes.POINTER(ggml_tensor)
  3212. # // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
  3213. # GGML_API struct ggml_tensor * ggml_transpose(
  3214. # struct ggml_context * ctx,
  3215. # struct ggml_tensor * a);
  3216. def ggml_transpose(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  3217. """Transpose *the first two dimensions* of a tensor and return the result.
  3218. alias for `ggml_permute(ctx, a, 1, 0, 2, 3)`
  3219. Parameters:
  3220. ctx: ggml context
  3221. a: tensor
  3222. Returns:
  3223. Pointer to ggml_tensor"""
  3224. return lib.ggml_transpose(ctx, a)
  3225. lib.ggml_transpose.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  3226. lib.ggml_transpose.restype = ctypes.POINTER(ggml_tensor)
  3227. # // supports 3D: a->ne[2] == b->ne[1]
  3228. # GGML_API struct ggml_tensor * ggml_get_rows(
  3229. # struct ggml_context * ctx,
  3230. # struct ggml_tensor * a,
  3231. # struct ggml_tensor * b);
  3232. def ggml_get_rows(
  3233. ctx: ggml_context_p,
  3234. a: ggml_tensor_p,
  3235. b: ggml_tensor_p,
  3236. ) -> ggml_tensor_p:
  3237. return lib.ggml_get_rows(ctx, a, b)
  3238. lib.ggml_get_rows.argtypes = [
  3239. ggml_context_p,
  3240. ctypes.POINTER(ggml_tensor),
  3241. ctypes.POINTER(ggml_tensor),
  3242. ]
  3243. lib.ggml_get_rows.restype = ctypes.POINTER(ggml_tensor)
  3244. # GGML_API struct ggml_tensor * ggml_get_rows_back(
  3245. # struct ggml_context * ctx,
  3246. # struct ggml_tensor * a,
  3247. # struct ggml_tensor * b,
  3248. # struct ggml_tensor * c);
  3249. def ggml_get_rows_back(
  3250. ctx: ggml_context_p,
  3251. a: ggml_tensor_p,
  3252. b: ggml_tensor_p,
  3253. c: ggml_tensor_p,
  3254. ) -> ggml_tensor_p:
  3255. return lib.ggml_get_rows_back(ctx, a, b, c)
  3256. lib.ggml_get_rows_back.argtypes = [
  3257. ggml_context_p,
  3258. ctypes.POINTER(ggml_tensor),
  3259. ctypes.POINTER(ggml_tensor),
  3260. ctypes.POINTER(ggml_tensor),
  3261. ]
  3262. lib.ggml_get_rows_back.restype = ctypes.POINTER(ggml_tensor)
  3263. # GGML_API struct ggml_tensor * ggml_diag(
  3264. # struct ggml_context * ctx,
  3265. # struct ggml_tensor * a);
  3266. def ggml_diag(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  3267. return lib.ggml_diag(ctx, a)
  3268. lib.ggml_diag.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  3269. lib.ggml_diag.restype = ctypes.POINTER(ggml_tensor)
  3270. # // set elements above the diagonal to -INF
  3271. # GGML_API struct ggml_tensor * ggml_diag_mask_inf(
  3272. # struct ggml_context * ctx,
  3273. # struct ggml_tensor * a,
  3274. # int n_past);
  3275. def ggml_diag_mask_inf(
  3276. ctx: ggml_context_p,
  3277. a: ggml_tensor_p,
  3278. n_past: Union[ctypes.c_int, int],
  3279. ) -> ggml_tensor_p:
  3280. return lib.ggml_diag_mask_inf(ctx, a, n_past)
  3281. lib.ggml_diag_mask_inf.argtypes = [
  3282. ggml_context_p,
  3283. ctypes.POINTER(ggml_tensor),
  3284. ctypes.c_int,
  3285. ]
  3286. lib.ggml_diag_mask_inf.restype = ctypes.POINTER(ggml_tensor)
  3287. # // in-place, returns view(a)
  3288. # GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
  3289. # struct ggml_context * ctx,
  3290. # struct ggml_tensor * a,
  3291. # int n_past);
  3292. def ggml_diag_mask_inf_inplace(
  3293. ctx: ggml_context_p,
  3294. a: ggml_tensor_p,
  3295. n_past: Union[ctypes.c_int, int],
  3296. ) -> ggml_tensor_p:
  3297. return lib.ggml_diag_mask_inf_inplace(ctx, a, n_past)
  3298. lib.ggml_diag_mask_inf_inplace.argtypes = [
  3299. ggml_context_p,
  3300. ctypes.POINTER(ggml_tensor),
  3301. ctypes.c_int,
  3302. ]
  3303. lib.ggml_diag_mask_inf_inplace.restype = ctypes.POINTER(ggml_tensor)
  3304. # // set elements above the diagonal to 0
  3305. # GGML_API struct ggml_tensor * ggml_diag_mask_zero(
  3306. # struct ggml_context * ctx,
  3307. # struct ggml_tensor * a,
  3308. # int n_past);
  3309. def ggml_diag_mask_zero(
  3310. ctx: ggml_context_p,
  3311. a: ggml_tensor_p,
  3312. n_past: Union[ctypes.c_int, int],
  3313. ) -> ggml_tensor_p:
  3314. return lib.ggml_diag_mask_zero(ctx, a, n_past)
  3315. lib.ggml_diag_mask_zero.argtypes = [
  3316. ggml_context_p,
  3317. ctypes.POINTER(ggml_tensor),
  3318. ctypes.c_int,
  3319. ]
  3320. lib.ggml_diag_mask_zero.restype = ctypes.POINTER(ggml_tensor)
  3321. # // in-place, returns view(a)
  3322. # GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
  3323. # struct ggml_context * ctx,
  3324. # struct ggml_tensor * a,
  3325. # int n_past);
  3326. def ggml_diag_mask_zero_inplace(
  3327. ctx: ggml_context_p,
  3328. a: ggml_tensor_p,
  3329. n_past: Union[ctypes.c_int, int],
  3330. ) -> ggml_tensor_p:
  3331. return lib.ggml_diag_mask_zero_inplace(ctx, a, n_past)
  3332. lib.ggml_diag_mask_zero_inplace.argtypes = [
  3333. ggml_context_p,
  3334. ctypes.POINTER(ggml_tensor),
  3335. ctypes.c_int,
  3336. ]
  3337. lib.ggml_diag_mask_zero_inplace.restype = ctypes.POINTER(ggml_tensor)
  3338. # GGML_API struct ggml_tensor * ggml_soft_max(
  3339. # struct ggml_context * ctx,
  3340. # struct ggml_tensor * a);
  3341. def ggml_soft_max(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  3342. return lib.ggml_soft_max(ctx, a)
  3343. lib.ggml_soft_max.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  3344. lib.ggml_soft_max.restype = ctypes.POINTER(ggml_tensor)
  3345. # // in-place, returns view(a)
  3346. # GGML_API struct ggml_tensor * ggml_soft_max_inplace(
  3347. # struct ggml_context * ctx,
  3348. # struct ggml_tensor * a);
  3349. def ggml_soft_max_inplace(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
  3350. return lib.ggml_soft_max_inplace(ctx, a)
  3351. lib.ggml_soft_max_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  3352. lib.ggml_soft_max_inplace.restype = ctypes.POINTER(ggml_tensor)
  3353. # // fused soft_max(a*scale + mask)
  3354. # // mask is optional
  3355. # GGML_API struct ggml_tensor * ggml_soft_max_ext(
  3356. # struct ggml_context * ctx,
  3357. # struct ggml_tensor * a,
  3358. # struct ggml_tensor * mask,
  3359. # float scale);
  3360. def ggml_soft_max_ext(
  3361. ctx: ggml_context_p,
  3362. a: ggml_tensor_p,
  3363. mask: ggml_tensor_p,
  3364. scale: Union[ctypes.c_float, float],
  3365. ) -> ggml_tensor_p:
  3366. return lib.ggml_soft_max_ext(ctx, a, mask, scale)
  3367. lib.ggml_soft_max_ext.argtypes = [
  3368. ggml_context_p,
  3369. ctypes.POINTER(ggml_tensor),
  3370. ctypes.POINTER(ggml_tensor),
  3371. ctypes.c_float,
  3372. ]
  3373. lib.ggml_soft_max_ext.restype = ctypes.POINTER(ggml_tensor)
  3374. # GGML_API struct ggml_tensor * ggml_soft_max_back(
  3375. # struct ggml_context * ctx,
  3376. # struct ggml_tensor * a,
  3377. # struct ggml_tensor * b);
  3378. def ggml_soft_max_back(
  3379. ctx: ggml_context_p,
  3380. a: ggml_tensor_p,
  3381. b: ggml_tensor_p,
  3382. ) -> ggml_tensor_p:
  3383. return lib.ggml_soft_max_back(ctx, a, b)
  3384. lib.ggml_soft_max_back.argtypes = [
  3385. ggml_context_p,
  3386. ctypes.POINTER(ggml_tensor),
  3387. ctypes.POINTER(ggml_tensor),
  3388. ]
  3389. lib.ggml_soft_max_back.restype = ctypes.POINTER(ggml_tensor)
  3390. # // in-place, returns view(a)
  3391. # GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
  3392. # struct ggml_context * ctx,
  3393. # struct ggml_tensor * a,
  3394. # struct ggml_tensor * b);
  3395. def ggml_soft_max_back_inplace(
  3396. ctx: ggml_context_p,
  3397. a: ggml_tensor_p,
  3398. b: ggml_tensor_p,
  3399. ) -> ggml_tensor_p:
  3400. return lib.ggml_soft_max_back_inplace(ctx, a, b)
  3401. lib.ggml_soft_max_back_inplace.argtypes = [
  3402. ggml_context_p,
  3403. ctypes.POINTER(ggml_tensor),
  3404. ctypes.POINTER(ggml_tensor),
  3405. ]
  3406. lib.ggml_soft_max_back_inplace.restype = ctypes.POINTER(ggml_tensor)
  3407. # // rotary position embedding
  3408. # // if mode & 1 == 1, skip n_past elements (DEPRECATED)
  3409. # // if mode & 2 == 1, GPT-NeoX style
  3410. # // if mode & 4 == 1, ChatGLM style
  3411. # //
  3412. # // b is an int32 vector with size a->ne[2], it contains the positions
  3413. # GGML_API struct ggml_tensor * ggml_rope(
  3414. # struct ggml_context * ctx,
  3415. # struct ggml_tensor * a,
  3416. # struct ggml_tensor * b,
  3417. # int n_dims,
  3418. # int mode,
  3419. # int n_ctx);
  3420. def ggml_rope(
  3421. ctx: ggml_context_p,
  3422. a: ggml_tensor_p,
  3423. b: ggml_tensor_p,
  3424. n_dims: Union[ctypes.c_int, int],
  3425. mode: Union[ctypes.c_int, int],
  3426. n_ctx: Union[ctypes.c_int, int],
  3427. ) -> ggml_tensor_p:
  3428. """Rotary position embedding
  3429. Parameters:
  3430. ctx: ggml context
  3431. a: tensor
  3432. b: int32 vector with size a->ne[2], it contains the positions
  3433. n_dims: number of dimensions
  3434. mode: if mode & 1 == 1, skip n_past elements (DEPRECATED)
  3435. if mode & 2 == 1, GPT-NeoX style
  3436. if mode & 4 == 1, ChatGLM style
  3437. n_ctx: context size
  3438. Returns:
  3439. Pointer to ggml_tensor"""
  3440. return lib.ggml_rope(ctx, a, b, n_dims, mode, n_ctx)
  3441. lib.ggml_rope.argtypes = [
  3442. ggml_context_p,
  3443. ctypes.POINTER(ggml_tensor),
  3444. ctypes.POINTER(ggml_tensor),
  3445. ctypes.c_int,
  3446. ctypes.c_int,
  3447. ctypes.c_int,
  3448. ]
  3449. lib.ggml_rope.restype = ctypes.POINTER(ggml_tensor)
  3450. # // in-place, returns view(a)
  3451. # GGML_API struct ggml_tensor * ggml_rope_inplace(
  3452. # struct ggml_context * ctx,
  3453. # struct ggml_tensor * a,
  3454. # struct ggml_tensor * b,
  3455. # int n_dims,
  3456. # int mode,
  3457. # int n_ctx);
  3458. def ggml_rope_inplace(
  3459. ctx: ggml_context_p,
  3460. a: ggml_tensor_p,
  3461. b: ggml_tensor_p,
  3462. n_dims: Union[ctypes.c_int, int],
  3463. mode: Union[ctypes.c_int, int],
  3464. n_ctx: Union[ctypes.c_int, int],
  3465. ) -> ggml_tensor_p:
  3466. """Rotary position embedding inplace
  3467. Parameters:
  3468. ctx: ggml context
  3469. a: tensor
  3470. b: int32 vector with size a->ne[2], it contains the positions
  3471. n_dims: number of dimensions
  3472. mode: if mode & 1 == 1, skip n_past elements (DEPRECATED)
  3473. if mode & 2 == 1, GPT-NeoX style
  3474. if mode & 4 == 1, ChatGLM style
  3475. n_ctx: context size
  3476. Returns:
  3477. Pointer to ggml_tensor"""
  3478. return lib.ggml_rope_inplace(ctx, a, b, n_dims, mode, n_ctx)
  3479. lib.ggml_rope_inplace.argtypes = [
  3480. ggml_context_p,
  3481. ctypes.POINTER(ggml_tensor),
  3482. ctypes.POINTER(ggml_tensor),
  3483. ctypes.c_int,
  3484. ctypes.c_int,
  3485. ctypes.c_int,
  3486. ]
  3487. lib.ggml_rope_inplace.restype = ctypes.POINTER(ggml_tensor)
  3488. # // custom RoPE
  3489. # GGML_API struct ggml_tensor * ggml_rope_custom(
  3490. # struct ggml_context * ctx,
  3491. # struct ggml_tensor * a,
  3492. # struct ggml_tensor * b,
  3493. # int n_dims,
  3494. # int mode,
  3495. # int n_ctx,
  3496. # int n_orig_ctx,
  3497. # float freq_base,
  3498. # float freq_scale,
  3499. # float ext_factor,
  3500. # float attn_factor,
  3501. # float beta_fast,
  3502. # float beta_slow);
  3503. def ggml_rope_custom(
  3504. ctx: ggml_context_p,
  3505. a: ggml_tensor_p,
  3506. b: ggml_tensor_p,
  3507. n_dims: Union[ctypes.c_int, int],
  3508. mode: Union[ctypes.c_int, int],
  3509. n_ctx: Union[ctypes.c_int, int],
  3510. n_orig_ctx: Union[ctypes.c_int, int],
  3511. freq_base: Union[ctypes.c_float, float],
  3512. freq_scale: Union[ctypes.c_float, float],
  3513. ext_factor: Union[ctypes.c_float, float],
  3514. attn_factor: Union[ctypes.c_float, float],
  3515. beta_fast: Union[ctypes.c_float, float],
  3516. beta_slow: Union[ctypes.c_float, float],
  3517. ) -> ggml_tensor_p:
  3518. """Custom rotary position embedding"""
  3519. return lib.ggml_rope_custom(
  3520. ctx,
  3521. a,
  3522. b,
  3523. n_dims,
  3524. mode,
  3525. n_ctx,
  3526. n_orig_ctx,
  3527. freq_base,
  3528. freq_scale,
  3529. ext_factor,
  3530. attn_factor,
  3531. beta_fast,
  3532. beta_slow,
  3533. )
  3534. lib.ggml_rope_custom.argtypes = [
  3535. ggml_context_p,
  3536. ctypes.POINTER(ggml_tensor),
  3537. ctypes.POINTER(ggml_tensor),
  3538. ctypes.c_int,
  3539. ctypes.c_int,
  3540. ctypes.c_int,
  3541. ctypes.c_int,
  3542. ctypes.c_float,
  3543. ctypes.c_float,
  3544. ctypes.c_float,
  3545. ctypes.c_float,
  3546. ctypes.c_float,
  3547. ctypes.c_float,
  3548. ]
  3549. lib.ggml_rope_custom.restype = ctypes.POINTER(ggml_tensor)
  3550. # // in-place, returns view(a)
  3551. # GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
  3552. # struct ggml_context * ctx,
  3553. # struct ggml_tensor * a,
  3554. # struct ggml_tensor * b,
  3555. # int n_dims,
  3556. # int mode,
  3557. # int n_ctx,
  3558. # int n_orig_ctx,
  3559. # float freq_base,
  3560. # float freq_scale,
  3561. # float ext_factor,
  3562. # float attn_factor,
  3563. # float beta_fast,
  3564. # float beta_slow);
  3565. def ggml_rope_custom_inplace(
  3566. ctx: ggml_context_p,
  3567. a: ggml_tensor_p,
  3568. b: ggml_tensor_p,
  3569. n_dims: Union[ctypes.c_int, int],
  3570. mode: Union[ctypes.c_int, int],
  3571. n_ctx: Union[ctypes.c_int, int],
  3572. n_orig_ctx: Union[ctypes.c_int, int],
  3573. freq_base: Union[ctypes.c_float, float],
  3574. freq_scale: Union[ctypes.c_float, float],
  3575. ext_factor: Union[ctypes.c_float, float],
  3576. attn_factor: Union[ctypes.c_float, float],
  3577. beta_fast: Union[ctypes.c_float, float],
  3578. beta_slow: Union[ctypes.c_float, float],
  3579. ) -> ggml_tensor_p:
  3580. """Custom rotary position embedding inplace"""
  3581. return lib.ggml_rope_custom_inplace(
  3582. ctx,
  3583. a,
  3584. b,
  3585. n_dims,
  3586. mode,
  3587. n_ctx,
  3588. n_orig_ctx,
  3589. freq_base,
  3590. freq_scale,
  3591. ext_factor,
  3592. attn_factor,
  3593. beta_fast,
  3594. beta_slow,
  3595. )
  3596. lib.ggml_rope_custom_inplace.argtypes = [
  3597. ggml_context_p,
  3598. ctypes.POINTER(ggml_tensor),
  3599. ctypes.POINTER(ggml_tensor),
  3600. ctypes.c_int,
  3601. ctypes.c_int,
  3602. ctypes.c_int,
  3603. ctypes.c_int,
  3604. ctypes.c_float,
  3605. ctypes.c_float,
  3606. ctypes.c_float,
  3607. ctypes.c_float,
  3608. ctypes.c_float,
  3609. ctypes.c_float,
  3610. ]
  3611. lib.ggml_rope_custom_inplace.restype = ctypes.POINTER(ggml_tensor)
  3612. # // compute correction dims for YaRN RoPE scaling
  3613. # void ggml_rope_yarn_corr_dims(
  3614. # int n_dims, int n_orig_ctx, float freq_base, float beta_fast, float beta_slow, float dims[2]);
  3615. def ggml_rope_yarn_corr_dims(
  3616. n_dims: Union[ctypes.c_int, int],
  3617. n_orig_ctx: Union[ctypes.c_int, int],
  3618. freq_base: Union[ctypes.c_float, float],
  3619. beta_fast: Union[ctypes.c_float, float],
  3620. beta_slow: Union[ctypes.c_float, float],
  3621. dims: CFloatArray,
  3622. ) -> None:
  3623. """Compute correction dims for YaRN RoPE scaling"""
  3624. return lib.ggml_rope_yarn_corr_dims(
  3625. n_dims,
  3626. n_orig_ctx,
  3627. freq_base,
  3628. beta_fast,
  3629. beta_slow,
  3630. dims,
  3631. )
  3632. lib.ggml_rope_yarn_corr_dims.argtypes = [
  3633. ctypes.c_int,
  3634. ctypes.c_int,
  3635. ctypes.c_float,
  3636. ctypes.c_float,
  3637. ctypes.c_float,
  3638. ctypes.POINTER(ctypes.c_float),
  3639. ]
  3640. lib.ggml_rope_yarn_corr_dims.restype = None
  3641. # // xPos RoPE, in-place, returns view(a)
  3642. # GGML_API struct ggml_tensor * ggml_rope_xpos_inplace(
  3643. # struct ggml_context * ctx,
  3644. # struct ggml_tensor * a,
  3645. # struct ggml_tensor * b,
  3646. # int n_dims,
  3647. # float base,
  3648. # bool down);
  3649. def ggml_rope_xpos_inplace(
  3650. ctx: ggml_context_p,
  3651. a: ggml_tensor_p,
  3652. b: ggml_tensor_p,
  3653. n_dims: Union[ctypes.c_int, int],
  3654. base: Union[ctypes.c_float, float],
  3655. down: Union[ctypes.c_bool, bool],
  3656. ) -> ggml_tensor_p:
  3657. """xPos RoPE, in-place, returns view(a)"""
  3658. return lib.ggml_rope_xpos_inplace(ctx, a, b, n_dims, base, down)
  3659. lib.ggml_rope_xpos_inplace.argtypes = [
  3660. ggml_context_p,
  3661. ctypes.POINTER(ggml_tensor),
  3662. ctypes.POINTER(ggml_tensor),
  3663. ctypes.c_int,
  3664. ctypes.c_float,
  3665. ctypes.c_bool,
  3666. ]
  3667. lib.ggml_rope_xpos_inplace.restype = ctypes.POINTER(ggml_tensor)
  3668. # // rotary position embedding backward, i.e compute dx from dy
  3669. # // a - dy
  3670. # GGML_API struct ggml_tensor * ggml_rope_back(
  3671. # struct ggml_context * ctx,
  3672. # struct ggml_tensor * a,
  3673. # struct ggml_tensor * b,
  3674. # int n_dims,
  3675. # int mode,
  3676. # int n_ctx,
  3677. # int n_orig_ctx,
  3678. # float freq_base,
  3679. # float freq_scale,
  3680. # float ext_factor,
  3681. # float attn_factor,
  3682. # float beta_fast,
  3683. # float beta_slow,
  3684. # float xpos_base,
  3685. # bool xpos_down);
  3686. def ggml_rope_back(
  3687. ctx: ggml_context_p,
  3688. a: ggml_tensor_p,
  3689. b: ggml_tensor_p,
  3690. n_dims: Union[ctypes.c_int, int],
  3691. mode: Union[ctypes.c_int, int],
  3692. n_ctx: Union[ctypes.c_int, int],
  3693. n_orig_ctx: Union[ctypes.c_int, int],
  3694. freq_base: Union[ctypes.c_float, float],
  3695. freq_scale: Union[ctypes.c_float, float],
  3696. ext_factor: Union[ctypes.c_float, float],
  3697. attn_factor: Union[ctypes.c_float, float],
  3698. beta_fast: Union[ctypes.c_float, float],
  3699. beta_slow: Union[ctypes.c_float, float],
  3700. xpos_base: Union[ctypes.c_float, float],
  3701. xpos_down: Union[ctypes.c_bool, bool],
  3702. ) -> ggml_tensor_p:
  3703. """Rotary position embedding backward pass"""
  3704. return lib.ggml_rope_back(
  3705. ctx,
  3706. a,
  3707. b,
  3708. n_dims,
  3709. mode,
  3710. n_ctx,
  3711. n_orig_ctx,
  3712. freq_base,
  3713. freq_scale,
  3714. ext_factor,
  3715. attn_factor,
  3716. beta_fast,
  3717. beta_slow,
  3718. xpos_base,
  3719. xpos_down,
  3720. )
  3721. lib.ggml_rope_back.argtypes = [
  3722. ggml_context_p,
  3723. ctypes.POINTER(ggml_tensor),
  3724. ctypes.POINTER(ggml_tensor),
  3725. ctypes.c_int,
  3726. ctypes.c_int,
  3727. ctypes.c_int,
  3728. ctypes.c_int,
  3729. ctypes.c_float,
  3730. ctypes.c_float,
  3731. ctypes.c_float,
  3732. ctypes.c_float,
  3733. ctypes.c_float,
  3734. ctypes.c_float,
  3735. ctypes.c_float,
  3736. ctypes.c_bool,
  3737. ]
  3738. lib.ggml_rope_back.restype = ctypes.POINTER(ggml_tensor)
  3739. # // alibi position embedding
  3740. # // in-place, returns view(a)
  3741. # GGML_API struct ggml_tensor * ggml_alibi(
  3742. # struct ggml_context * ctx,
  3743. # struct ggml_tensor * a,
  3744. # int n_past,
  3745. # int n_head,
  3746. # float bias_max);
  3747. def ggml_alibi(
  3748. ctx: ggml_context_p,
  3749. a: ggml_tensor_p,
  3750. n_past: Union[ctypes.c_int, int],
  3751. n_head: Union[ctypes.c_int, int],
  3752. bias_max: Union[ctypes.c_float, float],
  3753. ) -> ggml_tensor_p:
  3754. return lib.ggml_alibi(ctx, a, n_past, n_head, bias_max)
  3755. lib.ggml_alibi.argtypes = [
  3756. ggml_context_p,
  3757. ctypes.POINTER(ggml_tensor),
  3758. ctypes.c_int,
  3759. ctypes.c_int,
  3760. ctypes.c_float,
  3761. ]
  3762. lib.ggml_alibi.restype = ctypes.POINTER(ggml_tensor)
  3763. # // clamp
  3764. # // in-place, returns view(a)
  3765. # GGML_API struct ggml_tensor * ggml_clamp(
  3766. # struct ggml_context * ctx,
  3767. # struct ggml_tensor * a,
  3768. # float min,
  3769. # float max);
  3770. def ggml_clamp(
  3771. ctx: ggml_context_p,
  3772. a: ggml_tensor_p,
  3773. min: Union[ctypes.c_float, float],
  3774. max: Union[ctypes.c_float, float],
  3775. ) -> ggml_tensor_p:
  3776. """Clamp tensor values between min and max
  3777. Parameters:
  3778. ctx: ggml context
  3779. a: tensor
  3780. min: minimum value
  3781. max: maximum value
  3782. Returns:
  3783. Pointer to ggml_tensor"""
  3784. return lib.ggml_clamp(ctx, a, min, max)
  3785. lib.ggml_clamp.argtypes = [
  3786. ggml_context_p,
  3787. ctypes.POINTER(ggml_tensor),
  3788. ctypes.c_float,
  3789. ctypes.c_float,
  3790. ]
  3791. lib.ggml_clamp.restype = ctypes.POINTER(ggml_tensor)
  3792. # GGML_API struct ggml_tensor * ggml_im2col(
  3793. # struct ggml_context * ctx,
  3794. # struct ggml_tensor * a,
  3795. # struct ggml_tensor * b,
  3796. # int s0,
  3797. # int s1,
  3798. # int p0,
  3799. # int p1,
  3800. # int d0,
  3801. # int d1,
  3802. # bool is_2D);
  3803. def ggml_im2col(
  3804. ctx: ggml_context_p,
  3805. a: ggml_tensor_p,
  3806. b: ggml_tensor_p,
  3807. s0: Union[ctypes.c_int, int],
  3808. s1: Union[ctypes.c_int, int],
  3809. p0: Union[ctypes.c_int, int],
  3810. p1: Union[ctypes.c_int, int],
  3811. d0: Union[ctypes.c_int, int],
  3812. d1: Union[ctypes.c_int, int],
  3813. is_2D: Union[ctypes.c_bool, bool],
  3814. ) -> ggml_tensor_p:
  3815. return lib.ggml_im2col(ctx, a, b, s0, s1, p0, p1, d0, d1, is_2D)
  3816. lib.ggml_im2col.argtypes = [
  3817. ggml_context_p,
  3818. ctypes.POINTER(ggml_tensor),
  3819. ctypes.POINTER(ggml_tensor),
  3820. ctypes.c_int,
  3821. ctypes.c_int,
  3822. ctypes.c_int,
  3823. ctypes.c_int,
  3824. ctypes.c_int,
  3825. ctypes.c_int,
  3826. ctypes.c_bool,
  3827. ]
  3828. lib.ggml_im2col.restype = ctypes.POINTER(ggml_tensor)
  3829. # GGML_API struct ggml_tensor * ggml_conv_1d(
  3830. # struct ggml_context * ctx,
  3831. # struct ggml_tensor * a,
  3832. # struct ggml_tensor * b,
  3833. # int s0, // stride
  3834. # int p0, // padding
  3835. # int d0); // dilation
  3836. def ggml_conv_1d(
  3837. ctx: ggml_context_p,
  3838. a: ggml_tensor_p,
  3839. b: ggml_tensor_p,
  3840. s0: Union[ctypes.c_int, int],
  3841. p0: Union[ctypes.c_int, int],
  3842. d0: Union[ctypes.c_int, int],
  3843. ) -> ggml_tensor_p:
  3844. """Convolution 1D
  3845. Parameters:
  3846. a: input tensor
  3847. b: filter tensor
  3848. s0: stride
  3849. p0: padding
  3850. d0: dilation
  3851. Returns:
  3852. output tensor"""
  3853. return lib.ggml_conv_1d(ctx, a, b, s0, p0, d0)
  3854. lib.ggml_conv_1d.argtypes = [
  3855. ggml_context_p,
  3856. ctypes.POINTER(ggml_tensor),
  3857. ctypes.POINTER(ggml_tensor),
  3858. ctypes.c_int,
  3859. ctypes.c_int,
  3860. ctypes.c_int,
  3861. ]
  3862. lib.ggml_conv_1d.restype = ctypes.POINTER(ggml_tensor)
  3863. # // conv_1d with padding = half
  3864. # // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
  3865. # GGML_API struct ggml_tensor* ggml_conv_1d_ph(
  3866. # struct ggml_context * ctx,
  3867. # struct ggml_tensor * a,
  3868. # struct ggml_tensor * b,
  3869. # int s,
  3870. # int d);
  3871. def ggml_conv_1d_ph(
  3872. ctx: ggml_context_p,
  3873. a: ggml_tensor_p,
  3874. b: ggml_tensor_p,
  3875. s: Union[ctypes.c_int, int],
  3876. d: Union[ctypes.c_int, int],
  3877. ) -> ggml_tensor_p:
  3878. """Convolution 1D with padding = half
  3879. Parameters:
  3880. a: input tensor
  3881. b: filter tensor
  3882. s: stride
  3883. d: dilation
  3884. Returns:
  3885. output tensor"""
  3886. return lib.ggml_conv_1d_ph(ctx, a, b, s, d)
  3887. lib.ggml_conv_1d_ph.argtypes = [
  3888. ggml_context_p,
  3889. ctypes.POINTER(ggml_tensor),
  3890. ctypes.POINTER(ggml_tensor),
  3891. ctypes.c_int,
  3892. ctypes.c_int,
  3893. ]
  3894. lib.ggml_conv_1d_ph.restype = ctypes.POINTER(ggml_tensor)
  3895. # GGML_API struct ggml_tensor * ggml_conv_transpose_1d(
  3896. # struct ggml_context * ctx,
  3897. # struct ggml_tensor * a,
  3898. # struct ggml_tensor * b,
  3899. # int s0,
  3900. # int p0,
  3901. # int d0);
  3902. def ggml_conv_transpose_1d(
  3903. ctx: ggml_context_p,
  3904. a: ggml_tensor_p,
  3905. b: ggml_tensor_p,
  3906. s0: Union[ctypes.c_int, int],
  3907. p0: Union[ctypes.c_int, int],
  3908. d0: Union[ctypes.c_int, int],
  3909. ) -> ggml_tensor_p:
  3910. """Convolution transpose 1D
  3911. Parameters:
  3912. a: input tensor
  3913. b: filter tensor
  3914. s0: stride
  3915. p0: padding
  3916. d0: dilation
  3917. Returns:
  3918. output tensor"""
  3919. return lib.ggml_conv_transpose_1d(ctx, a, b, s0, p0, d0)
  3920. lib.ggml_conv_transpose_1d.argtypes = [
  3921. ggml_context_p,
  3922. ctypes.POINTER(ggml_tensor),
  3923. ctypes.POINTER(ggml_tensor),
  3924. ctypes.c_int,
  3925. ctypes.c_int,
  3926. ctypes.c_int,
  3927. ]
  3928. lib.ggml_conv_transpose_1d.restype = ctypes.POINTER(ggml_tensor)
  3929. # GGML_API struct ggml_tensor * ggml_conv_2d(
  3930. # struct ggml_context * ctx,
  3931. # struct ggml_tensor * a,
  3932. # struct ggml_tensor * b,
  3933. # int s0,
  3934. # int s1,
  3935. # int p0,
  3936. # int p1,
  3937. # int d0,
  3938. # int d1);
  3939. def ggml_conv_2d(
  3940. ctx: ggml_context_p,
  3941. a: ggml_tensor_p,
  3942. b: ggml_tensor_p,
  3943. s0: Union[ctypes.c_int, int],
  3944. s1: Union[ctypes.c_int, int],
  3945. p0: Union[ctypes.c_int, int],
  3946. p1: Union[ctypes.c_int, int],
  3947. d0: Union[ctypes.c_int, int],
  3948. d1: Union[ctypes.c_int, int],
  3949. ) -> ggml_tensor_p:
  3950. """Convolution 2D
  3951. Parameters:
  3952. a: input tensor
  3953. b: filter tensor
  3954. s0: stride
  3955. s1: stride
  3956. p0: padding
  3957. p1: padding
  3958. d0: dilation
  3959. d1: dilation
  3960. Returns:
  3961. output tensor"""
  3962. return lib.ggml_conv_2d(ctx, a, b, s0, s1, p0, p1, d0, d1)
  3963. lib.ggml_conv_2d.argtypes = [
  3964. ggml_context_p,
  3965. ctypes.POINTER(ggml_tensor),
  3966. ctypes.POINTER(ggml_tensor),
  3967. ctypes.c_int,
  3968. ctypes.c_int,
  3969. ctypes.c_int,
  3970. ctypes.c_int,
  3971. ctypes.c_int,
  3972. ctypes.c_int,
  3973. ]
  3974. lib.ggml_conv_2d.restype = ctypes.POINTER(ggml_tensor)
  3975. # // kernel size is a->ne[0] x a->ne[1]
  3976. # // stride is equal to kernel size
  3977. # // padding is zero
  3978. # // example:
  3979. # // a: 16 16 3 768
  3980. # // b: 1024 1024 3 1
  3981. # // res: 64 64 768 1
  3982. # // used in sam
  3983. # GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
  3984. # struct ggml_context * ctx,
  3985. # struct ggml_tensor * a,
  3986. # struct ggml_tensor * b);
  3987. def ggml_conv_2d_sk_p0(
  3988. ctx: ggml_context_p,
  3989. a: ggml_tensor_p,
  3990. b: ggml_tensor_p,
  3991. ) -> ggml_tensor_p:
  3992. """Convolution 2D
  3993. Parameters:
  3994. a: input tensor
  3995. b: filter tensor
  3996. Returns:
  3997. output tensor"""
  3998. return lib.ggml_conv_2d_sk_p0(ctx, a, b)
  3999. lib.ggml_conv_2d_sk_p0.argtypes = [
  4000. ggml_context_p,
  4001. ctypes.POINTER(ggml_tensor),
  4002. ctypes.POINTER(ggml_tensor),
  4003. ]
  4004. lib.ggml_conv_2d_sk_p0.restype = ctypes.POINTER(ggml_tensor)
  4005. # // kernel size is a->ne[0] x a->ne[1]
  4006. # // stride is 1
  4007. # // padding is half
  4008. # // example:
  4009. # // a: 3 3 256 256
  4010. # // b: 64 64 256 1
  4011. # // res: 64 64 256 1
  4012. # // used in sam
  4013. # GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
  4014. # struct ggml_context * ctx,
  4015. # struct ggml_tensor * a,
  4016. # struct ggml_tensor * b);
  4017. def ggml_conv_2d_s1_ph(
  4018. ctx: ggml_context_p,
  4019. a: ggml_tensor_p,
  4020. b: ggml_tensor_p,
  4021. ) -> ggml_tensor_p:
  4022. """Convolution 2D with stride = 1 and padding = half
  4023. Parameters:
  4024. a: input tensor
  4025. b: filter tensor
  4026. Returns:
  4027. output tensor"""
  4028. return lib.ggml_conv_2d_s1_ph(ctx, a, b)
  4029. lib.ggml_conv_2d_s1_ph.argtypes = [
  4030. ggml_context_p,
  4031. ctypes.POINTER(ggml_tensor),
  4032. ctypes.POINTER(ggml_tensor),
  4033. ]
  4034. lib.ggml_conv_2d_s1_ph.restype = ctypes.POINTER(ggml_tensor)
  4035. # GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
  4036. # struct ggml_context * ctx,
  4037. # struct ggml_tensor * a,
  4038. # struct ggml_tensor * b,
  4039. # int stride);
  4040. def ggml_conv_transpose_2d_p0(
  4041. ctx: ggml_context_p,
  4042. a: ggml_tensor_p,
  4043. b: ggml_tensor_p,
  4044. stride: Union[ctypes.c_int, int],
  4045. ) -> ggml_tensor_p:
  4046. """Convolution Transpose 2D with padding = zero
  4047. Parameters:
  4048. a: input tensor
  4049. b: filter tensor
  4050. stride: stride
  4051. Returns:
  4052. output tensor"""
  4053. return lib.ggml_conv_transpose_2d_p0(ctx, a, b, stride)
  4054. lib.ggml_conv_transpose_2d_p0.argtypes = [
  4055. ggml_context_p,
  4056. ctypes.POINTER(ggml_tensor),
  4057. ctypes.POINTER(ggml_tensor),
  4058. ctypes.c_int,
  4059. ]
  4060. lib.ggml_conv_transpose_2d_p0.restype = ctypes.POINTER(ggml_tensor)
  4061. # enum ggml_op_pool {
  4062. # GGML_OP_POOL_MAX,
  4063. # GGML_OP_POOL_AVG,
  4064. # GGML_OP_POOL_COUNT,
  4065. # };
  4066. GGML_OP_POOL_MAX = 0
  4067. GGML_OP_POOL_AVG = 1
  4068. GGML_OP_POOL_COUNT = 2
  4069. # GGML_API struct ggml_tensor * ggml_pool_1d(
  4070. # struct ggml_context * ctx,
  4071. # struct ggml_tensor * a,
  4072. # enum ggml_op_pool op,
  4073. # int k0, // kernel size
  4074. # int s0, // stride
  4075. # int p0); // padding
  4076. def ggml_pool_1d(
  4077. ctx: ggml_context_p,
  4078. a: ggml_tensor_p,
  4079. op: Union[ctypes.c_int, int],
  4080. k0: Union[ctypes.c_int, int],
  4081. s0: Union[ctypes.c_int, int],
  4082. p0: Union[ctypes.c_int, int],
  4083. ) -> ggml_tensor_p:
  4084. """1D Pooling
  4085. Parameters:
  4086. a: input tensor
  4087. op: pooling operation
  4088. k0: kernel size
  4089. s0: stride
  4090. p0: padding
  4091. Returns:
  4092. output tensor"""
  4093. return lib.ggml_pool_1d(ctx, a, op, k0, s0, p0)
  4094. lib.ggml_pool_1d.argtypes = [
  4095. ggml_context_p,
  4096. ctypes.POINTER(ggml_tensor),
  4097. ctypes.c_int,
  4098. ctypes.c_int,
  4099. ctypes.c_int,
  4100. ctypes.c_int,
  4101. ]
  4102. lib.ggml_pool_1d.restype = ctypes.POINTER(ggml_tensor)
  4103. # // the result will have 2*p0 padding for the first dimension
  4104. # // and 2*p1 padding for the second dimension
  4105. # GGML_API struct ggml_tensor * ggml_pool_2d(
  4106. # struct ggml_context * ctx,
  4107. # struct ggml_tensor * a,
  4108. # enum ggml_op_pool op,
  4109. # int k0,
  4110. # int k1,
  4111. # int s0,
  4112. # int s1,
  4113. # float p0,
  4114. # float p1);
  4115. def ggml_pool_2d(
  4116. ctx: ggml_context_p,
  4117. a: ggml_tensor_p,
  4118. op: Union[ctypes.c_int, int],
  4119. k0: Union[ctypes.c_int, int],
  4120. k1: Union[ctypes.c_int, int],
  4121. s0: Union[ctypes.c_int, int],
  4122. s1: Union[ctypes.c_int, int],
  4123. p0: Union[ctypes.c_float, float],
  4124. p1: Union[ctypes.c_float, float],
  4125. ) -> ggml_tensor_p:
  4126. """2D Pooling
  4127. Parameters:
  4128. a: input tensor
  4129. op: pooling operation
  4130. k0: kernel size
  4131. k1: kernel size
  4132. s0: stride
  4133. s1: stride
  4134. p0: padding
  4135. p1: padding
  4136. Returns:
  4137. output tensor"""
  4138. return lib.ggml_pool_2d(ctx, a, op, k0, k1, s0, s1, p0, p1)
  4139. lib.ggml_pool_2d.argtypes = [
  4140. ggml_context_p,
  4141. ctypes.POINTER(ggml_tensor),
  4142. ctypes.c_int,
  4143. ctypes.c_int,
  4144. ctypes.c_int,
  4145. ctypes.c_int,
  4146. ctypes.c_float,
  4147. ctypes.c_float,
  4148. ]
  4149. lib.ggml_pool_2d.restype = ctypes.POINTER(ggml_tensor)
  4150. # // nearest interpolate
  4151. # // used in stable-diffusion
  4152. # GGML_API struct ggml_tensor * ggml_upscale(
  4153. # struct ggml_context * ctx,
  4154. # struct ggml_tensor * a,
  4155. # int scale_factor);
  4156. def ggml_upscale(
  4157. ctx: ggml_context_p,
  4158. a: ggml_tensor_p,
  4159. scale_factor: Union[ctypes.c_int, int],
  4160. ) -> ggml_tensor_p:
  4161. """Upscale
  4162. Parameters:
  4163. a: input tensor
  4164. scale_factor: scale factor
  4165. Returns:
  4166. output tensor"""
  4167. return lib.ggml_upscale(ctx, a, scale_factor)
  4168. lib.ggml_upscale.argtypes = [
  4169. ggml_context_p,
  4170. ctypes.POINTER(ggml_tensor),
  4171. ctypes.c_int,
  4172. ]
  4173. lib.ggml_upscale.restype = ctypes.POINTER(ggml_tensor)
  4174. # // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
  4175. # GGML_API struct ggml_tensor * ggml_pad(
  4176. # struct ggml_context * ctx,
  4177. # struct ggml_tensor * a,
  4178. # int p0,
  4179. # int p1,
  4180. # int p2,
  4181. # int p3);
  4182. def ggml_pad(
  4183. ctx: ggml_context_p,
  4184. a: ggml_tensor_p,
  4185. p0: Union[ctypes.c_int, int],
  4186. p1: Union[ctypes.c_int, int],
  4187. p2: Union[ctypes.c_int, int],
  4188. p3: Union[ctypes.c_int, int],
  4189. ) -> ggml_tensor_p:
  4190. """Pad tensor with zeros
  4191. Parameters:
  4192. a: input tensor
  4193. p0: padding
  4194. p1: padding
  4195. p2: padding
  4196. p3: padding
  4197. Returns:
  4198. output tensor"""
  4199. return lib.ggml_pad(ctx, a, p0, p1, p2, p3)
  4200. lib.ggml_pad.argtypes = [
  4201. ggml_context_p,
  4202. ctypes.POINTER(ggml_tensor),
  4203. ctypes.c_int,
  4204. ctypes.c_int,
  4205. ctypes.c_int,
  4206. ctypes.c_int,
  4207. ]
  4208. lib.ggml_pad.restype = ctypes.POINTER(ggml_tensor)
  4209. # // sort rows
  4210. # enum ggml_sort_order {
  4211. # GGML_SORT_ASC,
  4212. # GGML_SORT_DESC,
  4213. # };
  4214. GGML_SORT_ASC = 0
  4215. GGML_SORT_DESC = 1
  4216. # GGML_API struct ggml_tensor * ggml_argsort(
  4217. # struct ggml_context * ctx,
  4218. # struct ggml_tensor * a,
  4219. # enum ggml_sort_order order);
  4220. def ggml_argsort(
  4221. ctx: ggml_context_p,
  4222. a: ggml_tensor_p,
  4223. order: Union[ctypes.c_int, int],
  4224. ) -> ggml_tensor_p:
  4225. """Argsort
  4226. Parameters:
  4227. a: input tensor
  4228. order: sort order
  4229. Returns:
  4230. output tensor"""
  4231. return lib.ggml_argsort(ctx, a, order)
  4232. lib.ggml_argsort.argtypes = [
  4233. ggml_context_p,
  4234. ctypes.POINTER(ggml_tensor),
  4235. ctypes.c_int,
  4236. ]
  4237. lib.ggml_argsort.restype = ctypes.POINTER(ggml_tensor)
  4238. # // top k elements per row
  4239. # GGML_API struct ggml_tensor * ggml_top_k(
  4240. # struct ggml_context * ctx,
  4241. # struct ggml_tensor * a,
  4242. # int k);
  4243. def ggml_top_k(
  4244. ctx: ggml_context_p,
  4245. a: ggml_tensor_p,
  4246. k: Union[ctypes.c_int, int],
  4247. ) -> ggml_tensor_p:
  4248. """Top k elements per row
  4249. Parameters:
  4250. a: input tensor
  4251. k: number of elements
  4252. Returns:
  4253. output tensor"""
  4254. return lib.ggml_top_k(ctx, a, k)
  4255. # GGML_API struct ggml_tensor * ggml_flash_attn(
  4256. # struct ggml_context * ctx,
  4257. # struct ggml_tensor * q,
  4258. # struct ggml_tensor * k,
  4259. # struct ggml_tensor * v,
  4260. # bool masked);
  4261. def ggml_flash_attn(
  4262. ctx: ggml_context_p,
  4263. q: ggml_tensor_p,
  4264. k: ggml_tensor_p,
  4265. v: ggml_tensor_p,
  4266. masked: Union[ctypes.c_bool, bool],
  4267. ) -> ggml_tensor_p:
  4268. return lib.ggml_flash_attn(ctx, q, k, v, masked)
  4269. lib.ggml_flash_attn.argtypes = [
  4270. ggml_context_p,
  4271. ctypes.POINTER(ggml_tensor),
  4272. ctypes.POINTER(ggml_tensor),
  4273. ctypes.POINTER(ggml_tensor),
  4274. ctypes.c_bool,
  4275. ]
  4276. lib.ggml_flash_attn.restype = ctypes.POINTER(ggml_tensor)
  4277. # GGML_API struct ggml_tensor * ggml_flash_attn_back(
  4278. # struct ggml_context * ctx,
  4279. # struct ggml_tensor * q,
  4280. # struct ggml_tensor * k,
  4281. # struct ggml_tensor * v,
  4282. # struct ggml_tensor * d,
  4283. # bool masked);
  4284. def ggml_flash_attn_back(
  4285. ctx: ggml_context_p,
  4286. q: ggml_tensor_p,
  4287. k: ggml_tensor_p,
  4288. v: ggml_tensor_p,
  4289. d: ggml_tensor_p,
  4290. masked: Union[ctypes.c_bool, bool],
  4291. ) -> ggml_tensor_p:
  4292. return lib.ggml_flash_attn_back(ctx, q, k, v, d, masked)
  4293. lib.ggml_flash_attn_back.argtypes = [
  4294. ggml_context_p,
  4295. ctypes.POINTER(ggml_tensor),
  4296. ctypes.POINTER(ggml_tensor),
  4297. ctypes.POINTER(ggml_tensor),
  4298. ctypes.POINTER(ggml_tensor),
  4299. ctypes.c_bool,
  4300. ]
  4301. lib.ggml_flash_attn_back.restype = ctypes.POINTER(ggml_tensor)
  4302. # GGML_API struct ggml_tensor * ggml_flash_ff(
  4303. # struct ggml_context * ctx,
  4304. # struct ggml_tensor * a,
  4305. # struct ggml_tensor * b0,
  4306. # struct ggml_tensor * b1,
  4307. # struct ggml_tensor * c0,
  4308. # struct ggml_tensor * c1);
  4309. def ggml_flash_ff(
  4310. ctx: ggml_context_p,
  4311. a: ggml_tensor_p,
  4312. b0: ggml_tensor_p,
  4313. b1: ggml_tensor_p,
  4314. c0: ggml_tensor_p,
  4315. c1: ggml_tensor_p,
  4316. ) -> ggml_tensor_p:
  4317. return lib.ggml_flash_ff(ctx, a, b0, b1, c0, c1)
  4318. lib.ggml_flash_ff.argtypes = [
  4319. ggml_context_p,
  4320. ctypes.POINTER(ggml_tensor),
  4321. ctypes.POINTER(ggml_tensor),
  4322. ctypes.POINTER(ggml_tensor),
  4323. ctypes.POINTER(ggml_tensor),
  4324. ctypes.POINTER(ggml_tensor),
  4325. ]
  4326. lib.ggml_flash_ff.restype = ctypes.POINTER(ggml_tensor)
  4327. # // partition into non-overlapping windows with padding if needed
  4328. # // example:
  4329. # // a: 768 64 64 1
  4330. # // w: 14
  4331. # // res: 768 14 14 25
  4332. # // used in sam
  4333. # GGML_API struct ggml_tensor * ggml_win_part(
  4334. # struct ggml_context * ctx,
  4335. # struct ggml_tensor * a,
  4336. # int w);
  4337. def ggml_win_part(
  4338. ctx: ggml_context_p,
  4339. a: ggml_tensor_p,
  4340. w: Union[ctypes.c_int, int],
  4341. ) -> ggml_tensor_p:
  4342. return lib.ggml_win_part(ctx, a, w)
  4343. lib.ggml_win_part.argtypes = [
  4344. ggml_context_p,
  4345. ctypes.POINTER(ggml_tensor),
  4346. ctypes.c_int,
  4347. ]
  4348. lib.ggml_win_part.restype = ctypes.POINTER(ggml_tensor)
  4349. # // reverse of ggml_win_part
  4350. # // used in sam
  4351. # GGML_API struct ggml_tensor * ggml_win_unpart(
  4352. # struct ggml_context * ctx,
  4353. # struct ggml_tensor * a,
  4354. # int w0,
  4355. # int h0,
  4356. # int w);
  4357. def ggml_win_unpart(
  4358. ctx: ggml_context_p,
  4359. a: ggml_tensor_p,
  4360. w0: Union[ctypes.c_int, int],
  4361. h0: Union[ctypes.c_int, int],
  4362. w: Union[ctypes.c_int, int],
  4363. ) -> ggml_tensor_p:
  4364. return lib.ggml_win_unpart(ctx, a, w0, h0, w)
  4365. lib.ggml_win_unpart.argtypes = [
  4366. ggml_context_p,
  4367. ctypes.POINTER(ggml_tensor),
  4368. ctypes.c_int,
  4369. ctypes.c_int,
  4370. ctypes.c_int,
  4371. ]
  4372. lib.ggml_win_unpart.restype = ctypes.POINTER(ggml_tensor)
  4373. # GGML_API struct ggml_tensor * ggml_unary(
  4374. # struct ggml_context * ctx,
  4375. # struct ggml_tensor * a,
  4376. # enum ggml_unary_op op);
  4377. def ggml_unary(
  4378. ctx: ggml_context_p,
  4379. a: ggml_tensor_p,
  4380. op: Union[ctypes.c_int, int],
  4381. ) -> ggml_tensor_p:
  4382. return lib.ggml_unary(ctx, a, op)
  4383. lib.ggml_unary.argtypes = [
  4384. ggml_context_p,
  4385. ctypes.POINTER(ggml_tensor),
  4386. ctypes.c_int,
  4387. ]
  4388. lib.ggml_unary.restype = ctypes.POINTER(ggml_tensor)
  4389. # GGML_API struct ggml_tensor * ggml_unary_inplace(
  4390. # struct ggml_context * ctx,
  4391. # struct ggml_tensor * a,
  4392. # enum ggml_unary_op op);
  4393. def ggml_unary_inplace(
  4394. ctx: ggml_context_p,
  4395. a: ggml_tensor_p,
  4396. op: Union[ctypes.c_int, int],
  4397. ) -> ggml_tensor_p:
  4398. return lib.ggml_unary_inplace(ctx, a, op)
  4399. lib.ggml_unary_inplace.argtypes = [
  4400. ggml_context_p,
  4401. ctypes.POINTER(ggml_tensor),
  4402. ctypes.c_int,
  4403. ]
  4404. lib.ggml_unary_inplace.restype = ctypes.POINTER(ggml_tensor)
  4405. # // used in sam
  4406. # GGML_API struct ggml_tensor * ggml_get_rel_pos(
  4407. # struct ggml_context * ctx,
  4408. # struct ggml_tensor * a,
  4409. # int qh,
  4410. # int kh);
  4411. def ggml_get_rel_pos(
  4412. ctx: ggml_context_p,
  4413. a: ggml_tensor_p,
  4414. qh: Union[ctypes.c_int, int],
  4415. kh: Union[ctypes.c_int, int],
  4416. ) -> ggml_tensor_p:
  4417. return lib.ggml_get_rel_pos(ctx, a, qh, kh)
  4418. lib.ggml_get_rel_pos.argtypes = [
  4419. ggml_context_p,
  4420. ctypes.POINTER(ggml_tensor),
  4421. ctypes.c_int,
  4422. ctypes.c_int,
  4423. ]
  4424. lib.ggml_get_rel_pos.restype = ctypes.POINTER(ggml_tensor)
  4425. # // used in sam
  4426. # GGML_API struct ggml_tensor * ggml_add_rel_pos(
  4427. # struct ggml_context * ctx,
  4428. # struct ggml_tensor * a,
  4429. # struct ggml_tensor * pw,
  4430. # struct ggml_tensor * ph);
  4431. def ggml_add_rel_pos(
  4432. ctx: ggml_context_p,
  4433. a: ggml_tensor_p,
  4434. pw: ggml_tensor_p,
  4435. ph: ggml_tensor_p,
  4436. ) -> ggml_tensor_p:
  4437. return lib.ggml_add_rel_pos(ctx, a, pw, ph)
  4438. lib.ggml_add_rel_pos.argtypes = [
  4439. ggml_context_p,
  4440. ctypes.POINTER(ggml_tensor),
  4441. ctypes.POINTER(ggml_tensor),
  4442. ctypes.POINTER(ggml_tensor),
  4443. ]
  4444. lib.ggml_add_rel_pos.restype = ctypes.POINTER(ggml_tensor)
  4445. # GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
  4446. # struct ggml_context * ctx,
  4447. # struct ggml_tensor * a,
  4448. # struct ggml_tensor * pw,
  4449. # struct ggml_tensor * ph);
  4450. def ggml_add_rel_pos_inplace(
  4451. ctx: ggml_context_p,
  4452. a: ggml_tensor_p,
  4453. pw: ggml_tensor_p,
  4454. ph: ggml_tensor_p,
  4455. ) -> ggml_tensor_p:
  4456. return lib.ggml_add_rel_pos_inplace(ctx, a, pw, ph)
  4457. lib.ggml_add_rel_pos_inplace.argtypes = [
  4458. ggml_context_p,
  4459. ctypes.POINTER(ggml_tensor),
  4460. ctypes.POINTER(ggml_tensor),
  4461. ctypes.POINTER(ggml_tensor),
  4462. ]
  4463. lib.ggml_add_rel_pos_inplace.restype = ctypes.POINTER(ggml_tensor)
  4464. # // custom operators (DEPRECATED)
  4465. # typedef void (*ggml_unary_op_f32_t)(const int, float *, const float *);
  4466. ggml_unary_op_f32_t = ctypes.CFUNCTYPE(
  4467. None, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float)
  4468. )
  4469. # typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
  4470. ggml_binary_op_f32_t = ctypes.CFUNCTYPE(
  4471. None,
  4472. ctypes.c_int,
  4473. ctypes.POINTER(ctypes.c_float),
  4474. ctypes.POINTER(ctypes.c_float),
  4475. ctypes.POINTER(ctypes.c_float),
  4476. )
  4477. # typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
  4478. ggml_custom1_op_f32_t = ctypes.CFUNCTYPE(
  4479. None, ctypes.POINTER(ggml_tensor), ctypes.POINTER(ggml_tensor)
  4480. )
  4481. """Unary operator function type"""
  4482. # typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  4483. ggml_custom2_op_f32_t = ctypes.CFUNCTYPE(
  4484. None,
  4485. ctypes.POINTER(ggml_tensor),
  4486. ctypes.POINTER(ggml_tensor),
  4487. ctypes.POINTER(ggml_tensor),
  4488. )
  4489. """Binary operator function type"""
  4490. # typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  4491. ggml_custom3_op_f32_t = ctypes.CFUNCTYPE(
  4492. None,
  4493. ctypes.POINTER(ggml_tensor),
  4494. ctypes.POINTER(ggml_tensor),
  4495. ctypes.POINTER(ggml_tensor),
  4496. ctypes.POINTER(ggml_tensor),
  4497. )
  4498. """Ternary operator function type"""
  4499. # GGML_API struct ggml_tensor * ggml_map_unary_f32(
  4500. # struct ggml_context * ctx,
  4501. # struct ggml_tensor * a,
  4502. # ggml_unary_op_f32_t fun);
  4503. def ggml_map_unary_f32(
  4504. ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._FuncPointer" # type: ignore
  4505. ) -> ggml_tensor_p:
  4506. return lib.ggml_map_unary_f32(ctx, a, fun)
  4507. lib.ggml_map_unary_f32.argtypes = [
  4508. ggml_context_p,
  4509. ctypes.POINTER(ggml_tensor),
  4510. ggml_unary_op_f32_t,
  4511. ]
  4512. lib.ggml_map_unary_f32.restype = ctypes.POINTER(ggml_tensor)
  4513. # GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
  4514. # struct ggml_context * ctx,
  4515. # struct ggml_tensor * a,
  4516. # ggml_unary_op_f32_t fun);
  4517. def ggml_map_unary_inplace_f32(
  4518. ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._FuncPointer" # type: ignore
  4519. ) -> ggml_tensor_p:
  4520. return lib.ggml_map_unary_inplace_f32(ctx, a, fun)
  4521. lib.ggml_map_unary_inplace_f32.argtypes = [
  4522. ggml_context_p,
  4523. ctypes.POINTER(ggml_tensor),
  4524. ggml_unary_op_f32_t,
  4525. ]
  4526. lib.ggml_map_unary_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
  4527. # GGML_API struct ggml_tensor * ggml_map_binary_f32(
  4528. # struct ggml_context * ctx,
  4529. # struct ggml_tensor * a,
  4530. # struct ggml_tensor * b,
  4531. # ggml_binary_op_f32_t fun);
  4532. def ggml_map_binary_f32(
  4533. ctx: ggml_context_p,
  4534. a: ggml_tensor_p,
  4535. b: ggml_tensor_p,
  4536. fun: "ctypes._FuncPointer", # type: ignore
  4537. ) -> ggml_tensor_p:
  4538. return lib.ggml_map_binary_f32(ctx, a, b, fun)
  4539. lib.ggml_map_binary_f32.argtypes = [
  4540. ggml_context_p,
  4541. ctypes.POINTER(ggml_tensor),
  4542. ctypes.POINTER(ggml_tensor),
  4543. ggml_binary_op_f32_t,
  4544. ]
  4545. lib.ggml_map_binary_f32.restype = ctypes.POINTER(ggml_tensor)
  4546. # GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
  4547. # struct ggml_context * ctx,
  4548. # struct ggml_tensor * a,
  4549. # struct ggml_tensor * b,
  4550. # ggml_binary_op_f32_t fun);
  4551. def ggml_map_binary_inplace_f32(
  4552. ctx: ggml_context_p,
  4553. a: ggml_tensor_p,
  4554. b: ggml_tensor_p,
  4555. fun: "ctypes._FuncPointer", # type: ignore
  4556. ) -> ggml_tensor_p:
  4557. return lib.ggml_map_binary_inplace_f32(ctx, a, b, fun)
  4558. lib.ggml_map_binary_inplace_f32.argtypes = [
  4559. ggml_context_p,
  4560. ctypes.POINTER(ggml_tensor),
  4561. ctypes.POINTER(ggml_tensor),
  4562. ggml_binary_op_f32_t,
  4563. ]
  4564. lib.ggml_map_binary_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
  4565. # GGML_API struct ggml_tensor * ggml_map_custom1_f32(
  4566. # struct ggml_context * ctx,
  4567. # struct ggml_tensor * a,
  4568. # ggml_custom1_op_f32_t fun);
  4569. def ggml_map_custom1_f32(
  4570. ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._FuncPointer" # type: ignore
  4571. ) -> ggml_tensor_p:
  4572. """Custom unary operator on a tensor.
  4573. Example:
  4574. ```python
  4575. import ggml
  4576. @ggml.ggml_custom1_op_f32_t
  4577. def custom_op(b: ggml.tensor_p, a: ggml.tensor_p):
  4578. # do something with a and copy to b
  4579. return
  4580. ...
  4581. b = ggml.ggml_map_custom1_f32(ctx, a, custom_op)
  4582. ```
  4583. Parameters:
  4584. a: input tensor
  4585. fun (ggml.ggml_custom1_op_f32_t): function to apply to each element
  4586. Returns:
  4587. output tensor"""
  4588. return lib.ggml_map_custom1_f32(ctx, a, fun)
  4589. lib.ggml_map_custom1_f32.argtypes = [
  4590. ggml_context_p,
  4591. ctypes.POINTER(ggml_tensor),
  4592. ggml_custom1_op_f32_t,
  4593. ]
  4594. lib.ggml_map_custom1_f32.restype = ctypes.POINTER(ggml_tensor)
  4595. # GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
  4596. # struct ggml_context * ctx,
  4597. # struct ggml_tensor * a,
  4598. # ggml_custom1_op_f32_t fun);
  4599. def ggml_map_custom1_inplace_f32(
  4600. ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._CFuncPtr" # type: ignore
  4601. ) -> ggml_tensor_p:
  4602. """Custom unary operator on a tensor inplace.
  4603. Parameters:
  4604. a: input tensor
  4605. fun (ggml.ggml_custom1_op_f32_t): function to apply to each element
  4606. Returns:
  4607. output tensor"""
  4608. return lib.ggml_map_custom1_inplace_f32(ctx, a, fun)
  4609. lib.ggml_map_custom1_inplace_f32.argtypes = [
  4610. ggml_context_p,
  4611. ctypes.POINTER(ggml_tensor),
  4612. ggml_custom1_op_f32_t,
  4613. ]
  4614. lib.ggml_map_custom1_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
  4615. # GGML_API struct ggml_tensor * ggml_map_custom2_f32(
  4616. # struct ggml_context * ctx,
  4617. # struct ggml_tensor * a,
  4618. # struct ggml_tensor * b,
  4619. # ggml_custom2_op_f32_t fun);
  4620. def ggml_map_custom2_f32(
  4621. ctx: ggml_context_p,
  4622. a: ggml_tensor_p,
  4623. b: ggml_tensor_p,
  4624. fun: "ctypes._FuncPointer", # type: ignore
  4625. ) -> ggml_tensor_p:
  4626. """Custom binary operator on two tensors.
  4627. Parameters:
  4628. a: input tensor
  4629. b: input tensor
  4630. fun (ggml.ggml_custom2_op_f32_t): function to apply to each element
  4631. Returns:
  4632. output tensor"""
  4633. return lib.ggml_map_custom2_f32(ctx, a, b, fun)
  4634. lib.ggml_map_custom2_f32.argtypes = [
  4635. ggml_context_p,
  4636. ctypes.POINTER(ggml_tensor),
  4637. ctypes.POINTER(ggml_tensor),
  4638. ggml_custom2_op_f32_t,
  4639. ]
  4640. lib.ggml_map_custom2_f32.restype = ctypes.POINTER(ggml_tensor)
  4641. # GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
  4642. # struct ggml_context * ctx,
  4643. # struct ggml_tensor * a,
  4644. # struct ggml_tensor * b,
  4645. # ggml_custom2_op_f32_t fun);
  4646. def ggml_map_custom2_inplace_f32(
  4647. ctx: ggml_context_p,
  4648. a: ggml_tensor_p,
  4649. b: ggml_tensor_p,
  4650. fun: "ctypes._FuncPointer", # type: ignore
  4651. ) -> ggml_tensor_p:
  4652. """Custom binary operator on two tensors inplace.
  4653. Parameters:
  4654. a: input tensor
  4655. b: input tensor
  4656. fun (ggml.ggml_custom2_op_f32_t): function to apply to each element
  4657. Returns:
  4658. output tensor"""
  4659. return lib.ggml_map_custom2_inplace_f32(ctx, a, b, fun)
  4660. lib.ggml_map_custom2_inplace_f32.argtypes = [
  4661. ggml_context_p,
  4662. ctypes.POINTER(ggml_tensor),
  4663. ctypes.POINTER(ggml_tensor),
  4664. ggml_custom2_op_f32_t,
  4665. ]
  4666. lib.ggml_map_custom2_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
  4667. # GGML_API struct ggml_tensor * ggml_map_custom3_f32(
  4668. # struct ggml_context * ctx,
  4669. # struct ggml_tensor * a,
  4670. # struct ggml_tensor * b,
  4671. # struct ggml_tensor * c,
  4672. # ggml_custom3_op_f32_t fun);
  4673. def ggml_map_custom3_f32(
  4674. ctx: ggml_context_p,
  4675. a: ggml_tensor_p,
  4676. b: ggml_tensor_p,
  4677. c: ggml_tensor_p,
  4678. fun: "ctypes._FuncPointer", # type: ignore
  4679. ) -> ggml_tensor_p:
  4680. """Custom ternary operator on three tensors.
  4681. Parameters:
  4682. a: input tensor
  4683. b: input tensor
  4684. c: input tensor
  4685. fun (ggml.ggml_custom3_op_f32_t): function to apply to each element
  4686. Returns:
  4687. output tensor"""
  4688. return lib.ggml_map_custom3_f32(ctx, a, b, c, fun)
  4689. lib.ggml_map_custom3_f32.argtypes = [
  4690. ggml_context_p,
  4691. ctypes.POINTER(ggml_tensor),
  4692. ctypes.POINTER(ggml_tensor),
  4693. ctypes.POINTER(ggml_tensor),
  4694. ggml_custom3_op_f32_t,
  4695. ]
  4696. lib.ggml_map_custom3_f32.restype = ctypes.POINTER(ggml_tensor)
  4697. # GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
  4698. # struct ggml_context * ctx,
  4699. # struct ggml_tensor * a,
  4700. # struct ggml_tensor * b,
  4701. # struct ggml_tensor * c,
  4702. # ggml_custom3_op_f32_t fun);
  4703. def ggml_map_custom3_inplace_f32(
  4704. ctx: ggml_context_p,
  4705. a: ggml_tensor_p,
  4706. b: ggml_tensor_p,
  4707. c: ggml_tensor_p,
  4708. fun: "ctypes._FuncPointer", # type: ignore
  4709. ) -> ggml_tensor_p:
  4710. """Custom ternary operator on three tensors inplace.
  4711. Parameters:
  4712. a: input tensor
  4713. b: input tensor
  4714. c: input tensor
  4715. fun (ggml.ggml_custom3_op_f32_t): function to apply to each element
  4716. Returns:
  4717. output tensor"""
  4718. return lib.ggml_map_custom3_inplace_f32(ctx, a, b, c, fun)
  4719. lib.ggml_map_custom3_inplace_f32.argtypes = [
  4720. ggml_context_p,
  4721. ctypes.POINTER(ggml_tensor),
  4722. ctypes.POINTER(ggml_tensor),
  4723. ctypes.POINTER(ggml_tensor),
  4724. ggml_custom3_op_f32_t,
  4725. ]
  4726. lib.ggml_map_custom3_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
  4727. # // custom operators v2
  4728. # typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
  4729. ggml_custom1_op_t = ctypes.CFUNCTYPE(
  4730. None,
  4731. ctypes.POINTER(ggml_tensor),
  4732. ctypes.POINTER(ggml_tensor),
  4733. ctypes.c_int,
  4734. ctypes.c_int,
  4735. ctypes.c_void_p,
  4736. )
  4737. """Custom unary operator on a tensor."""
  4738. # typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
  4739. ggml_custom2_op_t = ctypes.CFUNCTYPE(
  4740. None,
  4741. ctypes.POINTER(ggml_tensor),
  4742. ctypes.POINTER(ggml_tensor),
  4743. ctypes.POINTER(ggml_tensor),
  4744. ctypes.c_int,
  4745. ctypes.c_int,
  4746. ctypes.c_void_p,
  4747. )
  4748. """Custom binary operator on two tensors."""
  4749. # typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
  4750. ggml_custom3_op_t = ctypes.CFUNCTYPE(
  4751. None,
  4752. ctypes.POINTER(ggml_tensor),
  4753. ctypes.POINTER(ggml_tensor),
  4754. ctypes.POINTER(ggml_tensor),
  4755. ctypes.POINTER(ggml_tensor),
  4756. ctypes.c_int,
  4757. ctypes.c_int,
  4758. ctypes.c_void_p,
  4759. )
  4760. """Custom ternary operator on three tensors."""
  4761. # #define GGML_N_TASKS_MAX -1
  4762. GGML_N_TASKS_MAX = -1
  4763. # GGML_API struct ggml_tensor * ggml_map_custom1(
  4764. # struct ggml_context * ctx,
  4765. # struct ggml_tensor * a,
  4766. # ggml_custom1_op_t fun,
  4767. # int n_tasks,
  4768. # void * userdata);
  4769. def ggml_map_custom1(
  4770. ctx: ggml_context_p,
  4771. a: ggml_tensor_p,
  4772. fun: "ctypes._FuncPointer", # type: ignore
  4773. n_tasks: Union[ctypes.c_int, int],
  4774. userdata: Optional[ctypes.c_void_p],
  4775. ) -> ggml_tensor_p:
  4776. return lib.ggml_map_custom1(ctx, a, fun, n_tasks, userdata)
  4777. lib.ggml_map_custom1.argtypes = [
  4778. ggml_context_p,
  4779. ctypes.POINTER(ggml_tensor),
  4780. ggml_custom1_op_t,
  4781. ctypes.c_int,
  4782. ctypes.c_void_p,
  4783. ]
  4784. lib.ggml_map_custom1.restype = ctypes.POINTER(ggml_tensor)
  4785. # GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
  4786. # struct ggml_context * ctx,
  4787. # struct ggml_tensor * a,
  4788. # ggml_custom1_op_t fun,
  4789. # int n_tasks,
  4790. # void * userdata);
  4791. def ggml_map_custom1_inplace(
  4792. ctx: ggml_context_p,
  4793. a: ggml_tensor_p,
  4794. fun: "ctypes._FuncPointer", # type: ignore
  4795. n_tasks: Union[ctypes.c_int, int],
  4796. userdata: Optional[ctypes.c_void_p],
  4797. ) -> ggml_tensor_p:
  4798. return lib.ggml_map_custom1_inplace(ctx, a, fun, n_tasks, userdata)
  4799. lib.ggml_map_custom1_inplace.argtypes = [
  4800. ggml_context_p,
  4801. ctypes.POINTER(ggml_tensor),
  4802. ggml_custom1_op_t,
  4803. ctypes.c_int,
  4804. ctypes.c_void_p,
  4805. ]
  4806. lib.ggml_map_custom1_inplace.restype = ctypes.POINTER(ggml_tensor)
  4807. # GGML_API struct ggml_tensor * ggml_map_custom2(
  4808. # struct ggml_context * ctx,
  4809. # struct ggml_tensor * a,
  4810. # struct ggml_tensor * b,
  4811. # ggml_custom2_op_t fun,
  4812. # int n_tasks,
  4813. # void * userdata);
  4814. def ggml_map_custom2(
  4815. ctx: ggml_context_p,
  4816. a: ggml_tensor_p,
  4817. b: ggml_tensor_p,
  4818. fun: "ctypes._FuncPointer", # type: ignore
  4819. n_tasks: Union[ctypes.c_int, int],
  4820. userdata: Optional[ctypes.c_void_p],
  4821. ) -> ggml_tensor_p:
  4822. return lib.ggml_map_custom2(ctx, a, b, fun, n_tasks, userdata)
  4823. lib.ggml_map_custom2.argtypes = [
  4824. ggml_context_p,
  4825. ctypes.POINTER(ggml_tensor),
  4826. ctypes.POINTER(ggml_tensor),
  4827. ggml_custom2_op_t,
  4828. ctypes.c_int,
  4829. ctypes.c_void_p,
  4830. ]
  4831. lib.ggml_map_custom2.restype = ctypes.POINTER(ggml_tensor)
  4832. # GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
  4833. # struct ggml_context * ctx,
  4834. # struct ggml_tensor * a,
  4835. # struct ggml_tensor * b,
  4836. # ggml_custom2_op_t fun,
  4837. # int n_tasks,
  4838. # void * userdata);
  4839. def ggml_map_custom2_inplace(
  4840. ctx: ggml_context_p,
  4841. a: ggml_tensor_p,
  4842. b: ggml_tensor_p,
  4843. fun: "ctypes._FuncPointer", # type: ignore
  4844. n_tasks: Union[ctypes.c_int, int],
  4845. userdata: Optional[ctypes.c_void_p],
  4846. ) -> ggml_tensor_p:
  4847. return lib.ggml_map_custom2_inplace(ctx, a, b, fun, n_tasks, userdata)
  4848. lib.ggml_map_custom2_inplace.argtypes = [
  4849. ggml_context_p,
  4850. ctypes.POINTER(ggml_tensor),
  4851. ctypes.POINTER(ggml_tensor),
  4852. ggml_custom2_op_t,
  4853. ctypes.c_int,
  4854. ctypes.c_void_p,
  4855. ]
  4856. lib.ggml_map_custom2_inplace.restype = ctypes.POINTER(ggml_tensor)
  4857. # GGML_API struct ggml_tensor * ggml_map_custom3(
  4858. # struct ggml_context * ctx,
  4859. # struct ggml_tensor * a,
  4860. # struct ggml_tensor * b,
  4861. # struct ggml_tensor * c,
  4862. # ggml_custom3_op_t fun,
  4863. # int n_tasks,
  4864. # void * userdata);
  4865. def ggml_map_custom3(
  4866. ctx: ggml_context_p,
  4867. a: ggml_tensor_p,
  4868. b: ggml_tensor_p,
  4869. c: ggml_tensor_p,
  4870. fun: "ctypes._FuncPointer", # type: ignore
  4871. n_tasks: Union[ctypes.c_int, int],
  4872. userdata: Optional[ctypes.c_void_p],
  4873. ) -> ggml_tensor_p:
  4874. return lib.ggml_map_custom3(ctx, a, b, c, fun, n_tasks, userdata)
  4875. lib.ggml_map_custom3.argtypes = [
  4876. ggml_context_p,
  4877. ctypes.POINTER(ggml_tensor),
  4878. ctypes.POINTER(ggml_tensor),
  4879. ctypes.POINTER(ggml_tensor),
  4880. ggml_custom3_op_t,
  4881. ctypes.c_int,
  4882. ctypes.c_void_p,
  4883. ]
  4884. lib.ggml_map_custom3.restype = ctypes.POINTER(ggml_tensor)
  4885. # GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
  4886. # struct ggml_context * ctx,
  4887. # struct ggml_tensor * a,
  4888. # struct ggml_tensor * b,
  4889. # struct ggml_tensor * c,
  4890. # ggml_custom3_op_t fun,
  4891. # int n_tasks,
  4892. # void * userdata);
  4893. def ggml_map_custom3_inplace(
  4894. ctx: ggml_context_p,
  4895. a: ggml_tensor_p,
  4896. b: ggml_tensor_p,
  4897. c: ggml_tensor_p,
  4898. fun: "ctypes._FuncPointer", # type: ignore
  4899. n_tasks: Union[ctypes.c_int, int],
  4900. userdata: Optional[ctypes.c_void_p],
  4901. ) -> ggml_tensor_p:
  4902. return lib.ggml_map_custom3_inplace(ctx, a, b, c, fun, n_tasks, userdata)
  4903. lib.ggml_map_custom3_inplace.argtypes = [
  4904. ggml_context_p,
  4905. ctypes.POINTER(ggml_tensor),
  4906. ctypes.POINTER(ggml_tensor),
  4907. ctypes.POINTER(ggml_tensor),
  4908. ggml_custom3_op_t,
  4909. ctypes.c_int,
  4910. ctypes.c_void_p,
  4911. ]
  4912. lib.ggml_map_custom3_inplace.restype = ctypes.POINTER(ggml_tensor)
  4913. # // loss function
  4914. # GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
  4915. # struct ggml_context * ctx,
  4916. # struct ggml_tensor * a,
  4917. # struct ggml_tensor * b);
  4918. def ggml_cross_entropy_loss(
  4919. ctx: ggml_context_p,
  4920. a: ggml_tensor_p,
  4921. b: ggml_tensor_p,
  4922. ) -> ggml_tensor_p:
  4923. return lib.ggml_cross_entropy_loss(ctx, a, b)
  4924. lib.ggml_cross_entropy_loss.argtypes = [
  4925. ggml_context_p,
  4926. ctypes.POINTER(ggml_tensor),
  4927. ctypes.POINTER(ggml_tensor),
  4928. ]
  4929. lib.ggml_cross_entropy_loss.restype = ctypes.POINTER(ggml_tensor)
  4930. # GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
  4931. # struct ggml_context * ctx,
  4932. # struct ggml_tensor * a,
  4933. # struct ggml_tensor * b,
  4934. # struct ggml_tensor * c);
  4935. def ggml_cross_entropy_loss_back(
  4936. ctx: ggml_context_p,
  4937. a: ggml_tensor_p,
  4938. b: ggml_tensor_p,
  4939. c: ggml_tensor_p,
  4940. ) -> ggml_tensor_p:
  4941. return lib.ggml_cross_entropy_loss_back(ctx, a, b, c)
  4942. lib.ggml_cross_entropy_loss_back.argtypes = [
  4943. ggml_context_p,
  4944. ctypes.POINTER(ggml_tensor),
  4945. ctypes.POINTER(ggml_tensor),
  4946. ctypes.POINTER(ggml_tensor),
  4947. ]
  4948. lib.ggml_cross_entropy_loss_back.restype = ctypes.POINTER(ggml_tensor)
  4949. # //
  4950. # // automatic differentiation
  4951. # //
  4952. # GGML_API void ggml_set_param(
  4953. # struct ggml_context * ctx,
  4954. # struct ggml_tensor * tensor);
  4955. def ggml_set_param(ctx: ggml_context_p, tensor: ggml_tensor_p):
  4956. return lib.ggml_set_param(ctx, tensor)
  4957. lib.ggml_set_param.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
  4958. lib.ggml_set_param.restype = None
  4959. # GGML_API void ggml_build_forward_expand (struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
  4960. def ggml_build_forward_expand(
  4961. cgraph: ggml_cgraph_p,
  4962. tensor: ggml_tensor_p,
  4963. ):
  4964. """Add a tensor to the forward computation graph. This is used to
  4965. compute and save the value of the tensor.
  4966. Parameters:
  4967. cgraph: The graph.
  4968. tensor: The tensor."""
  4969. return lib.ggml_build_forward_expand(cgraph, tensor)
  4970. lib.ggml_build_forward_expand.argtypes = [
  4971. ctypes.POINTER(ggml_cgraph),
  4972. ctypes.POINTER(ggml_tensor),
  4973. ]
  4974. lib.ggml_build_forward_expand.restype = None
  4975. # GGML_API void ggml_build_backward_expand(struct ggml_context * ctx, struct ggml_cgraph * gf, struct ggml_cgraph * gb, bool keep);
  4976. def ggml_build_backward_expand(
  4977. ctx: ggml_context_p,
  4978. gf: ggml_cgraph_p,
  4979. gb: ggml_cgraph_p,
  4980. keep: Union[ctypes.c_bool, bool],
  4981. ):
  4982. """Add a tensor to the backward computation graph. This is used to
  4983. compute the gradient of the tensor.
  4984. Parameters:
  4985. ctx: The context.
  4986. gf: The forward graph.
  4987. gb: The backward graph.
  4988. keep: Whether to keep the tensor."""
  4989. return lib.ggml_build_backward_expand(ctx, gf, gb, keep)
  4990. lib.ggml_build_backward_expand.argtypes = [
  4991. ggml_context_p,
  4992. ctypes.POINTER(ggml_cgraph),
  4993. ctypes.POINTER(ggml_cgraph),
  4994. ctypes.c_bool,
  4995. ]
  4996. lib.ggml_build_backward_expand.restype = None
  4997. # // graph allocation in a context
  4998. # GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
  4999. def ggml_new_graph(ctx: ggml_context_p) -> ggml_cgraph_p:
  5000. """Create a new graph.
  5001. Parameters:
  5002. ctx: The context.
  5003. Returns:
  5004. The graph."""
  5005. return lib.ggml_new_graph(ctx)
  5006. lib.ggml_new_graph.argtypes = [ggml_context_p]
  5007. lib.ggml_new_graph.restype = ctypes.POINTER(ggml_cgraph)
  5008. # GGML_API struct ggml_cgraph * ggml_new_graph_custom (struct ggml_context * ctx, size_t size, bool grads);
  5009. def ggml_new_graph_custom(
  5010. ctx: ggml_context_p,
  5011. size: Union[ctypes.c_size_t, int],
  5012. grads: Union[ctypes.c_bool, bool],
  5013. ) -> ggml_cgraph_p:
  5014. """Create a new graph with custom size and grads.
  5015. Parameters:
  5016. ctx: The context.
  5017. size: The size of the graph.
  5018. grads: Whether to keep the gradients.
  5019. Returns:
  5020. The graph."""
  5021. return lib.ggml_new_graph_custom(ctx, size, grads)
  5022. lib.ggml_new_graph_custom.argtypes = [ggml_context_p, ctypes.c_size_t, ctypes.c_bool]
  5023. lib.ggml_new_graph_custom.restype = ctypes.POINTER(ggml_cgraph)
  5024. # GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph);
  5025. def ggml_graph_dup(
  5026. ctx: ggml_context_p,
  5027. cgraph: ggml_cgraph_p,
  5028. ) -> ggml_cgraph_p:
  5029. """Duplicate a graph.
  5030. Parameters:
  5031. ctx: The context.
  5032. cgraph: The graph.
  5033. Returns:
  5034. The graph."""
  5035. return lib.ggml_graph_dup(ctx, cgraph)
  5036. lib.ggml_graph_dup.argtypes = [ggml_context_p, ctypes.POINTER(ggml_cgraph)]
  5037. lib.ggml_graph_dup.restype = ctypes.POINTER(ggml_cgraph)
  5038. # GGML_API struct ggml_cgraph ggml_graph_view (struct ggml_cgraph * cgraph, int i0, int i1);
  5039. def ggml_graph_view(
  5040. cgraph: ggml_cgraph_p,
  5041. i0: Union[ctypes.c_int, int],
  5042. i1: Union[ctypes.c_int, int],
  5043. ) -> ggml_cgraph:
  5044. """View a graph.
  5045. Parameters:
  5046. cgraph: The graph.
  5047. i0: The start index.
  5048. i1: The end index.
  5049. Returns:
  5050. The graph."""
  5051. return lib.ggml_graph_view(cgraph, i0, i1)
  5052. lib.ggml_graph_view.argtypes = [ctypes.POINTER(ggml_cgraph), ctypes.c_int, ctypes.c_int]
  5053. lib.ggml_graph_view.restype = ggml_cgraph
  5054. # GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
  5055. def ggml_graph_cpy(
  5056. src: ggml_cgraph_p,
  5057. dst: ggml_cgraph_p,
  5058. ):
  5059. """Copy a graph.
  5060. Parameters:
  5061. src: The source graph.
  5062. dst: The destination graph."""
  5063. return lib.ggml_graph_cpy(src, dst)
  5064. lib.ggml_graph_cpy.argtypes = [ctypes.POINTER(ggml_cgraph), ctypes.POINTER(ggml_cgraph)]
  5065. lib.ggml_graph_cpy.restype = None
  5066. # GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // zero grads
  5067. def ggml_graph_reset(
  5068. cgraph: ggml_cgraph_p,
  5069. ):
  5070. """Reset a graph.
  5071. Parameters:
  5072. cgraph: The graph."""
  5073. return lib.ggml_graph_reset(cgraph)
  5074. lib.ggml_graph_reset.argtypes = [ctypes.POINTER(ggml_cgraph)]
  5075. lib.ggml_graph_reset.restype = None
  5076. # GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
  5077. def ggml_graph_clear(
  5078. cgraph: ggml_cgraph_p,
  5079. ):
  5080. """Clear a graph.
  5081. Parameters:
  5082. cgraph: The graph."""
  5083. return lib.ggml_graph_clear(cgraph)
  5084. lib.ggml_graph_clear.argtypes = [ctypes.POINTER(ggml_cgraph)]
  5085. lib.ggml_graph_clear.restype = None
  5086. # GGML_API size_t ggml_graph_overhead(void);
  5087. def ggml_graph_overhead() -> int:
  5088. """Get the overhead of the graph."""
  5089. return lib.ggml_graph_overhead()
  5090. lib.ggml_graph_overhead.argtypes = []
  5091. lib.ggml_graph_overhead.restype = ctypes.c_size_t
  5092. # GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads);
  5093. def ggml_graph_overhead_custom(
  5094. size: Union[ctypes.c_size_t, int],
  5095. grads: Union[ctypes.c_bool, bool],
  5096. ) -> int:
  5097. return lib.ggml_graph_overhead_custom(size, grads)
  5098. lib.ggml_graph_overhead_custom.argtypes = [ctypes.c_size_t, ctypes.c_bool]
  5099. lib.ggml_graph_overhead_custom.restype = ctypes.c_size_t
  5100. # // ggml_graph_plan() has to be called before ggml_graph_compute()
  5101. # // when plan.work_size > 0, caller must allocate memory for plan.work_data
  5102. # GGML_API struct ggml_cplan ggml_graph_plan (struct ggml_cgraph * cgraph, int n_threads /*= GGML_DEFAULT_N_THREADS*/);
  5103. def ggml_graph_plan(
  5104. cgraph: ggml_cgraph_p,
  5105. n_threads: Union[ctypes.c_int, int] = GGML_DEFAULT_N_THREADS,
  5106. ) -> ggml_cplan:
  5107. """Plan the computation graph.
  5108. Parameters:
  5109. cgraph: The graph.
  5110. n_threads: The number of threads to use.
  5111. Returns:
  5112. The plan."""
  5113. return lib.ggml_graph_plan(cgraph, n_threads)
  5114. lib.ggml_graph_plan.argtypes = [
  5115. ctypes.POINTER(ggml_cgraph),
  5116. ctypes.c_int,
  5117. ]
  5118. lib.ggml_graph_plan.restype = ggml_cplan
  5119. # GGML_API int ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
  5120. def ggml_graph_compute(
  5121. cgraph: ggml_cgraph_p,
  5122. cplan: ggml_cplan_p,
  5123. ) -> int:
  5124. return lib.ggml_graph_compute(cgraph, cplan)
  5125. lib.ggml_graph_compute.argtypes = [
  5126. ctypes.POINTER(ggml_cgraph),
  5127. ctypes.POINTER(ggml_cplan),
  5128. ]
  5129. lib.ggml_graph_compute.restype = ctypes.c_int
  5130. # // same as ggml_graph_compute() but the work data is allocated as a part of the context
  5131. # // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
  5132. # GGML_API void ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
  5133. def ggml_graph_compute_with_ctx(
  5134. ctx: ggml_context_p,
  5135. cgraph: ggml_cgraph_p,
  5136. n_threads: Union[ctypes.c_int, int],
  5137. ):
  5138. """Compute the graph with a context.
  5139. Parameters:
  5140. ctx: The context.
  5141. cgraph: The graph.
  5142. n_threads: The number of threads to use."""
  5143. return lib.ggml_graph_compute_with_ctx(ctx, cgraph, n_threads)
  5144. lib.ggml_graph_compute_with_ctx.argtypes = [
  5145. ggml_context_p,
  5146. ctypes.POINTER(ggml_cgraph),
  5147. ctypes.c_int,
  5148. ]
  5149. lib.ggml_graph_compute_with_ctx.restype = None
  5150. # GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
  5151. def ggml_graph_get_tensor(
  5152. cgraph: ggml_cgraph_p,
  5153. name: bytes,
  5154. ) -> ggml_tensor_p:
  5155. """Get a tensor from the graph by name.
  5156. Parameters:
  5157. cgraph: The graph.
  5158. name: The name of the tensor.
  5159. Returns:
  5160. The tensor."""
  5161. return lib.ggml_graph_get_tensor(cgraph, name)
  5162. lib.ggml_graph_get_tensor.argtypes = [
  5163. ctypes.POINTER(ggml_cgraph),
  5164. ctypes.c_char_p,
  5165. ]
  5166. lib.ggml_graph_get_tensor.restype = ctypes.POINTER(ggml_tensor)
  5167. # GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
  5168. def ggml_graph_export(
  5169. cgraph: ggml_cgraph_p,
  5170. fname: bytes,
  5171. ):
  5172. return lib.ggml_graph_export(cgraph, fname)
  5173. lib.ggml_graph_export.argtypes = [
  5174. ctypes.POINTER(ggml_cgraph),
  5175. ctypes.c_char_p,
  5176. ]
  5177. lib.ggml_graph_export.restype = None
  5178. # GGML_API struct ggml_cgraph * ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
  5179. def ggml_graph_import(
  5180. fname: bytes,
  5181. ctx_data: "ctypes._Pointer[ggml_context_p]", # type: ignore
  5182. ctx_eval: "ctypes._Pointer[ggml_context_p]", # type: ignore
  5183. ) -> ggml_cgraph_p:
  5184. return lib.ggml_graph_import(fname, ctx_data, ctx_eval)
  5185. lib.ggml_graph_import.argtypes = [
  5186. ctypes.c_char_p,
  5187. ctypes.POINTER(ggml_context_p),
  5188. ctypes.POINTER(ggml_context_p),
  5189. ]
  5190. lib.ggml_graph_import.restype = ctypes.POINTER(ggml_cgraph)
  5191. # // print info and performance information for the graph
  5192. # GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
  5193. def ggml_graph_print(
  5194. cgraph: ggml_cgraph_p,
  5195. ):
  5196. return lib.ggml_graph_print(cgraph)
  5197. lib.ggml_graph_print.argtypes = [ctypes.POINTER(ggml_cgraph)]
  5198. lib.ggml_graph_print.restype = None
  5199. # // dump the graph into a file using the dot format
  5200. # GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
  5201. def ggml_graph_dump_dot(
  5202. gb: ggml_cgraph_p,
  5203. gf: ggml_cgraph_p,
  5204. filename: bytes,
  5205. ):
  5206. return lib.ggml_graph_dump_dot(gb, gf, filename)
  5207. lib.ggml_graph_dump_dot.argtypes = [
  5208. ctypes.POINTER(ggml_cgraph),
  5209. ctypes.POINTER(ggml_cgraph),
  5210. ctypes.c_char_p,
  5211. ]
  5212. lib.ggml_graph_dump_dot.restype = None
  5213. # // build gradient checkpointing backward graph gb for gf using provided checkpoints
  5214. # // gb_tmp will contain original backward graph with rewritten backward process nodes,
  5215. # // but without the second forward pass nodes.
  5216. # GGML_API void ggml_build_backward_gradient_checkpointing(
  5217. # struct ggml_context * ctx,
  5218. # struct ggml_cgraph * gf,
  5219. # struct ggml_cgraph * gb,
  5220. # struct ggml_cgraph * gb_tmp,
  5221. # struct ggml_tensor * * checkpoints,
  5222. # int n_checkpoints);
  5223. def ggml_build_backward_gradient_checkpointing(
  5224. ctx: ggml_context_p,
  5225. gf: ggml_cgraph_p,
  5226. gb: ggml_cgraph_p,
  5227. gb_tmp: ggml_cgraph_p,
  5228. checkpoints: "ctypes._Pointer[ggml_tensor_p]", # type: ignore
  5229. n_checkpoints: Union[ctypes.c_int, int],
  5230. ):
  5231. return lib.ggml_build_backward_gradient_checkpointing(
  5232. ctx, gf, gb, gb_tmp, checkpoints, n_checkpoints
  5233. )
  5234. lib.ggml_build_backward_gradient_checkpointing.argtypes = [
  5235. ggml_context_p,
  5236. ctypes.POINTER(ggml_cgraph),
  5237. ctypes.POINTER(ggml_cgraph),
  5238. ctypes.POINTER(ggml_cgraph),
  5239. ctypes.POINTER(ctypes.POINTER(ggml_tensor)),
  5240. ctypes.c_int,
  5241. ]
  5242. lib.ggml_build_backward_gradient_checkpointing.restype = None
  5243. # //
  5244. # // optimization
  5245. # //
  5246. # // optimization methods
  5247. # enum ggml_opt_type {
  5248. # GGML_OPT_ADAM,
  5249. # GGML_OPT_LBFGS,
  5250. # };
  5251. GGML_OPT_ADAM = 0
  5252. GGML_OPT_LBFGS = 1
  5253. # // linesearch methods
  5254. # enum ggml_linesearch {
  5255. # GGML_LINESEARCH_DEFAULT = 1,
  5256. # GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
  5257. # GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
  5258. # GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
  5259. # };
  5260. GGML_LINESEARCH_DEFAULT = 1
  5261. GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0
  5262. GGML_LINESEARCH_BACKTRACKING_WOLFE = 1
  5263. GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2
  5264. # // optimization return values
  5265. # enum ggml_opt_result {
  5266. # GGML_OPT_OK = 0,
  5267. # GGML_OPT_DID_NOT_CONVERGE,
  5268. # GGML_OPT_NO_CONTEXT,
  5269. # GGML_OPT_INVALID_WOLFE,
  5270. # GGML_OPT_FAIL,
  5271. # GGML_OPT_CANCEL,
  5272. # GGML_LINESEARCH_FAIL = -128,
  5273. # GGML_LINESEARCH_MINIMUM_STEP,
  5274. # GGML_LINESEARCH_MAXIMUM_STEP,
  5275. # GGML_LINESEARCH_MAXIMUM_ITERATIONS,
  5276. # GGML_LINESEARCH_INVALID_PARAMETERS,
  5277. # };
  5278. GGML_OPT_OK = 0
  5279. GGML_OPT_DID_NOT_CONVERGE = 1
  5280. GGML_OPT_NO_CONTEXT = 2
  5281. GGML_OPT_INVALID_WOLFE = 3
  5282. GGML_OPT_FAIL = 4
  5283. GGML_OPT_CANCEL = 5
  5284. GGML_LINESEARCH_FAIL = -128
  5285. GGML_LINESEARCH_MINIMUM_STEP = -127
  5286. GGML_LINESEARCH_MAXIMUM_STEP = -126
  5287. GGML_LINESEARCH_MAXIMUM_ITERATIONS = -125
  5288. GGML_LINESEARCH_INVALID_PARAMETERS = -124
  5289. # typedef void (*ggml_opt_callback)(void * data, int accum_step, float * sched, bool * cancel);
  5290. ggml_opt_callback = ctypes.CFUNCTYPE(
  5291. None,
  5292. ctypes.c_void_p,
  5293. ctypes.c_int,
  5294. ctypes.POINTER(ctypes.c_float),
  5295. ctypes.POINTER(ctypes.c_bool),
  5296. )
  5297. # typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
  5298. ggml_log_callback = ctypes.CFUNCTYPE(
  5299. None, ctypes.c_int, ctypes.c_char_p, ctypes.c_void_p
  5300. )
  5301. # // optimization parameters
  5302. # //
  5303. # // see ggml.c (ggml_opt_default_params) for default values
  5304. # //
  5305. # struct ggml_opt_params {
  5306. # enum ggml_opt_type type;
  5307. # size_t graph_size;
  5308. # int n_threads;
  5309. # // delta-based convergence test
  5310. # //
  5311. # // if past == 0 - disabled
  5312. # // if past > 0:
  5313. # // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
  5314. # //
  5315. # int past;
  5316. # float delta;
  5317. # // maximum number of iterations without improvement
  5318. # //
  5319. # // if 0 - disabled
  5320. # // if > 0:
  5321. # // assume convergence if no cost improvement in this number of iterations
  5322. # //
  5323. # int max_no_improvement;
  5324. # bool print_forward_graph;
  5325. # bool print_backward_graph;
  5326. # int n_gradient_accumulation;
  5327. # // ADAM parameters
  5328. # struct {
  5329. # int n_iter;
  5330. # float sched; // schedule multiplier (fixed, decay or warmup)
  5331. # float decay; // weight decay for AdamW, use 0.0f to disable
  5332. # int decay_min_ndim; // minimum number of tensor dimension to apply weight decay
  5333. # float alpha; // learning rate
  5334. # float beta1;
  5335. # float beta2;
  5336. # float eps; // epsilon for numerical stability
  5337. # float eps_f; // epsilon for convergence test
  5338. # float eps_g; // epsilon for convergence test
  5339. # float gclip; // gradient clipping
  5340. # } adam;
  5341. # // LBFGS parameters
  5342. # struct {
  5343. # int m; // number of corrections to approximate the inv. Hessian
  5344. # int n_iter;
  5345. # int max_linesearch;
  5346. # float eps; // convergence tolerance
  5347. # float ftol; // line search tolerance
  5348. # float wolfe;
  5349. # float min_step;
  5350. # float max_step;
  5351. # enum ggml_linesearch linesearch;
  5352. # } lbfgs;
  5353. # };
  5354. class ggml_opt_params_adam(ctypes.Structure):
  5355. _fields_ = [
  5356. ("n_iter", ctypes.c_int),
  5357. ("sched", ctypes.c_float),
  5358. ("decay", ctypes.c_float),
  5359. ("decay_min_ndim", ctypes.c_int),
  5360. ("alpha", ctypes.c_float),
  5361. ("beta1", ctypes.c_float),
  5362. ("beta2", ctypes.c_float),
  5363. ("eps", ctypes.c_float),
  5364. ("eps_f", ctypes.c_float),
  5365. ("eps_g", ctypes.c_float),
  5366. ("gclip", ctypes.c_float),
  5367. ]
  5368. class ggml_opt_params_lbfgs(ctypes.Structure):
  5369. _fields_ = [
  5370. ("m", ctypes.c_int),
  5371. ("n_iter", ctypes.c_int),
  5372. ("max_linesearch", ctypes.c_int),
  5373. ("eps", ctypes.c_float),
  5374. ("ftol", ctypes.c_float),
  5375. ("wolfe", ctypes.c_float),
  5376. ("min_step", ctypes.c_float),
  5377. ("max_step", ctypes.c_float),
  5378. ("linesearch", ctypes.c_int),
  5379. ]
  5380. class ggml_opt_params(ctypes.Structure):
  5381. _fields_ = [
  5382. ("type", ctypes.c_int),
  5383. ("graph_size", ctypes.c_size_t),
  5384. ("n_threads", ctypes.c_int),
  5385. ("past", ctypes.c_int),
  5386. ("delta", ctypes.c_float),
  5387. ("max_no_improvement", ctypes.c_int),
  5388. ("print_forward_graph", ctypes.c_bool),
  5389. ("print_backward_graph", ctypes.c_bool),
  5390. ("n_gradient_accumulation", ctypes.c_int),
  5391. ("adam", ggml_opt_params_adam),
  5392. ("lbfgs", ggml_opt_params_lbfgs),
  5393. ]
  5394. # struct ggml_opt_context {
  5395. # struct ggml_context * ctx;
  5396. # struct ggml_opt_params params;
  5397. # int iter;
  5398. # int64_t nx; // number of parameter elements
  5399. # bool just_initialized;
  5400. # float loss_before;
  5401. # float loss_after;
  5402. # struct {
  5403. # struct ggml_tensor * g; // current gradient
  5404. # struct ggml_tensor * m; // first moment
  5405. # struct ggml_tensor * v; // second moment
  5406. # struct ggml_tensor * pf; // past function values
  5407. # float fx_best;
  5408. # float fx_prev;
  5409. # int n_no_improvement;
  5410. # } adam;
  5411. # struct {
  5412. # struct ggml_tensor * x; // current parameters
  5413. # struct ggml_tensor * xp; // previous parameters
  5414. # struct ggml_tensor * g; // current gradient
  5415. # struct ggml_tensor * gp; // previous gradient
  5416. # struct ggml_tensor * d; // search direction
  5417. # struct ggml_tensor * pf; // past function values
  5418. # struct ggml_tensor * lmal; // the L-BFGS memory alpha
  5419. # struct ggml_tensor * lmys; // the L-BFGS memory ys
  5420. # struct ggml_tensor * lms; // the L-BFGS memory s
  5421. # struct ggml_tensor * lmy; // the L-BFGS memory y
  5422. # float fx_best;
  5423. # float step;
  5424. # int j;
  5425. # int k;
  5426. # int end;
  5427. # int n_no_improvement;
  5428. # } lbfgs;
  5429. # };
  5430. class ggml_opt_context_adam(ctypes.Structure):
  5431. _fields_ = [
  5432. ("g", ctypes.POINTER(ggml_tensor)),
  5433. ("m", ctypes.POINTER(ggml_tensor)),
  5434. ("v", ctypes.POINTER(ggml_tensor)),
  5435. ("pf", ctypes.POINTER(ggml_tensor)),
  5436. ("fx_best", ctypes.c_float),
  5437. ("fx_prev", ctypes.c_float),
  5438. ("n_no_improvement", ctypes.c_int),
  5439. ]
  5440. class ggml_opt_context_lbfgs(ctypes.Structure):
  5441. _fields_ = [
  5442. ("x", ctypes.POINTER(ggml_tensor)),
  5443. ("xp", ctypes.POINTER(ggml_tensor)),
  5444. ("g", ctypes.POINTER(ggml_tensor)),
  5445. ("gp", ctypes.POINTER(ggml_tensor)),
  5446. ("d", ctypes.POINTER(ggml_tensor)),
  5447. ("pf", ctypes.POINTER(ggml_tensor)),
  5448. ("lmal", ctypes.POINTER(ggml_tensor)),
  5449. ("lmys", ctypes.POINTER(ggml_tensor)),
  5450. ("lms", ctypes.POINTER(ggml_tensor)),
  5451. ("lmy", ctypes.POINTER(ggml_tensor)),
  5452. ("fx_best", ctypes.c_float),
  5453. ("step", ctypes.c_float),
  5454. ("j", ctypes.c_int),
  5455. ("k", ctypes.c_int),
  5456. ("end", ctypes.c_int),
  5457. ("n_no_improvement", ctypes.c_int),
  5458. ]
  5459. class ggml_opt_context(ctypes.Structure):
  5460. _fields_ = [
  5461. ("ctx", ggml_context_p),
  5462. ("params", ggml_opt_params),
  5463. ("iter", ctypes.c_int),
  5464. ("nx", ctypes.c_int64),
  5465. ("just_initialized", ctypes.c_bool),
  5466. ("loss_before", ctypes.c_float),
  5467. ("loss_after", ctypes.c_float),
  5468. ("adam", ggml_opt_context_adam),
  5469. ("lbfgs", ggml_opt_context_lbfgs),
  5470. ]
  5471. ggml_opt_context_p = ctypes.POINTER(ggml_opt_context)
  5472. # GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
  5473. def ggml_opt_default_params(type: Union[ctypes.c_int, bool]) -> ggml_opt_params:
  5474. return lib.ggml_opt_default_params(type)
  5475. lib.ggml_opt_default_params.argtypes = [ctypes.c_int]
  5476. lib.ggml_opt_default_params.restype = ggml_opt_params
  5477. # // optimize the function defined by the tensor f
  5478. # GGML_API enum ggml_opt_result ggml_opt(
  5479. # struct ggml_context * ctx,
  5480. # struct ggml_opt_params params,
  5481. # struct ggml_tensor * f);
  5482. def ggml_opt(
  5483. ctx: ggml_context_p,
  5484. params: ggml_opt_params,
  5485. f: ggml_tensor_p,
  5486. ) -> int:
  5487. return lib.ggml_opt(ctx, params, f)
  5488. lib.ggml_opt.argtypes = [ggml_context_p, ggml_opt_params, ctypes.POINTER(ggml_tensor)]
  5489. lib.ggml_opt.restype = ctypes.c_int
  5490. # // initialize optimizer context
  5491. # GGML_API void ggml_opt_init(
  5492. # struct ggml_context * ctx,
  5493. # struct ggml_opt_context * opt,
  5494. # struct ggml_opt_params params,
  5495. # int64_t nx);
  5496. def ggml_opt_init(
  5497. ctx: ggml_context_p,
  5498. opt: "ctypes._Pointer[ggml_opt_context]", # type: ignore
  5499. params: ggml_opt_params,
  5500. nx: Union[ctypes.c_int64, int],
  5501. ):
  5502. return lib.ggml_opt_init(ctx, opt, params, nx)
  5503. lib.ggml_opt_init.argtypes = [
  5504. ggml_context_p,
  5505. ctypes.POINTER(ggml_opt_context),
  5506. ggml_opt_params,
  5507. ctypes.c_int64,
  5508. ]
  5509. lib.ggml_opt_init.restype = None
  5510. # // continue optimizing the function defined by the tensor f
  5511. # GGML_API enum ggml_opt_result ggml_opt_resume(
  5512. # struct ggml_context * ctx,
  5513. # struct ggml_opt_context * opt,
  5514. # struct ggml_tensor * f);
  5515. def ggml_opt_resume(
  5516. ctx: ggml_context_p,
  5517. opt: "ctypes._Pointer[ggml_opt_context]", # type: ignore
  5518. f: ggml_tensor_p,
  5519. ) -> int:
  5520. return lib.ggml_opt_resume(ctx, opt, f)
  5521. lib.ggml_opt_resume.argtypes = [
  5522. ggml_context_p,
  5523. ctypes.POINTER(ggml_opt_context),
  5524. ctypes.POINTER(ggml_tensor),
  5525. ]
  5526. lib.ggml_opt_resume.restype = ctypes.c_int
  5527. # // continue optimizing the function defined by the tensor f
  5528. # GGML_API enum ggml_opt_result ggml_opt_resume_g(
  5529. # struct ggml_context * ctx,
  5530. # struct ggml_opt_context * opt,
  5531. # struct ggml_tensor * f,
  5532. # struct ggml_cgraph * gf,
  5533. # struct ggml_cgraph * gb,
  5534. # ggml_opt_callback callback,
  5535. # void * callback_data);
  5536. def ggml_opt_resume_g(
  5537. ctx: ggml_context_p,
  5538. opt: "ctypes._Pointer[ggml_opt_context]", # type: ignore
  5539. f: ggml_tensor_p,
  5540. gf: ggml_cgraph_p,
  5541. gb: ggml_cgraph_p,
  5542. callback: "ctypes._CFuncPtr[None, ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_bool)]", # type: ignore
  5543. callback_data: ctypes.c_void_p,
  5544. ) -> int:
  5545. return lib.ggml_opt_resume_g(ctx, opt, f, gf, gb, callback, callback_data)
  5546. lib.ggml_opt_resume_g.argtypes = [
  5547. ggml_context_p,
  5548. ctypes.POINTER(ggml_opt_context),
  5549. ctypes.POINTER(ggml_tensor),
  5550. ctypes.POINTER(ggml_cgraph),
  5551. ctypes.POINTER(ggml_cgraph),
  5552. ggml_opt_callback,
  5553. ctypes.c_void_p,
  5554. ]
  5555. lib.ggml_opt_resume_g.restype = ctypes.c_int
  5556. # //
  5557. # // quantization
  5558. # //
  5559. # // TODO: these would probably get removed in favor of the more general ggml_quantize_chunk
  5560. # GGML_API size_t ggml_quantize_q4_0(const float * src, void * dst, int n, int k, int64_t * hist);
  5561. def ggml_quantize_q4_0(
  5562. src: CFloatArray,
  5563. dst: ctypes.c_void_p,
  5564. n: Union[ctypes.c_int, int],
  5565. k: Union[ctypes.c_int, int],
  5566. hist: CInt64Array,
  5567. ) -> int:
  5568. return lib.ggml_quantize_q4_0(src, dst, n, k, hist)
  5569. lib.ggml_quantize_q4_0.argtypes = [
  5570. ctypes.POINTER(ctypes.c_float),
  5571. ctypes.c_void_p,
  5572. ctypes.c_int,
  5573. ctypes.c_int,
  5574. ctypes.POINTER(ctypes.c_int64),
  5575. ]
  5576. lib.ggml_quantize_q4_0.restype = ctypes.c_size_t
  5577. # GGML_API size_t ggml_quantize_q4_1(const float * src, void * dst, int n, int k, int64_t * hist);
  5578. def ggml_quantize_q4_1(
  5579. src: CFloatArray,
  5580. dst: ctypes.c_void_p,
  5581. n: Union[ctypes.c_int, int],
  5582. k: Union[ctypes.c_int, int],
  5583. hist: CInt64Array,
  5584. ) -> int:
  5585. return lib.ggml_quantize_q4_1(src, dst, n, k, hist)
  5586. lib.ggml_quantize_q4_1.argtypes = [
  5587. ctypes.POINTER(ctypes.c_float),
  5588. ctypes.c_void_p,
  5589. ctypes.c_int,
  5590. ctypes.c_int,
  5591. ctypes.POINTER(ctypes.c_int64),
  5592. ]
  5593. lib.ggml_quantize_q4_1.restype = ctypes.c_size_t
  5594. # GGML_API size_t ggml_quantize_q5_0(const float * src, void * dst, int n, int k, int64_t * hist);
  5595. def ggml_quantize_q5_0(
  5596. src: CFloatArray,
  5597. dst: ctypes.c_void_p,
  5598. n: Union[ctypes.c_int, int],
  5599. k: Union[ctypes.c_int, int],
  5600. hist: CInt64Array,
  5601. ) -> int:
  5602. return lib.ggml_quantize_q5_0(src, dst, n, k, hist)
  5603. lib.ggml_quantize_q5_0.argtypes = [
  5604. ctypes.POINTER(ctypes.c_float),
  5605. ctypes.c_void_p,
  5606. ctypes.c_int,
  5607. ctypes.c_int,
  5608. ctypes.POINTER(ctypes.c_int64),
  5609. ]
  5610. lib.ggml_quantize_q5_0.restype = ctypes.c_size_t
  5611. # GGML_API size_t ggml_quantize_q5_1(const float * src, void * dst, int n, int k, int64_t * hist);
  5612. def ggml_quantize_q5_1(
  5613. src: CFloatArray,
  5614. dst: ctypes.c_void_p,
  5615. n: Union[ctypes.c_int, int],
  5616. k: Union[ctypes.c_int, int],
  5617. hist: CInt64Array,
  5618. ) -> int:
  5619. return lib.ggml_quantize_q5_1(src, dst, n, k, hist)
  5620. lib.ggml_quantize_q5_1.argtypes = [
  5621. ctypes.POINTER(ctypes.c_float),
  5622. ctypes.c_void_p,
  5623. ctypes.c_int,
  5624. ctypes.c_int,
  5625. ctypes.POINTER(ctypes.c_int64),
  5626. ]
  5627. lib.ggml_quantize_q5_1.restype = ctypes.c_size_t
  5628. # GGML_API size_t ggml_quantize_q8_0(const float * src, void * dst, int n, int k, int64_t * hist);
  5629. def ggml_quantize_q8_0(
  5630. src: CFloatArray,
  5631. dst: ctypes.c_void_p,
  5632. n: Union[ctypes.c_int, int],
  5633. k: Union[ctypes.c_int, int],
  5634. hist: CInt64Array,
  5635. ) -> int:
  5636. return lib.ggml_quantize_q8_0(src, dst, n, k, hist)
  5637. lib.ggml_quantize_q8_0.argtypes = [
  5638. ctypes.POINTER(ctypes.c_float),
  5639. ctypes.c_void_p,
  5640. ctypes.c_int,
  5641. ctypes.c_int,
  5642. ctypes.POINTER(ctypes.c_int64),
  5643. ]
  5644. lib.ggml_quantize_q8_0.restype = ctypes.c_size_t
  5645. # GGML_API size_t ggml_quantize_q2_K(const float * src, void * dst, int n, int k, int64_t * hist);
  5646. def ggml_quantize_q2_K(
  5647. src: CFloatArray,
  5648. dst: ctypes.c_void_p,
  5649. n: Union[ctypes.c_int, int],
  5650. k: Union[ctypes.c_int, int],
  5651. hist: CInt64Array,
  5652. ) -> int:
  5653. return lib.ggml_quantize_q2_K(src, dst, n, k, hist)
  5654. lib.ggml_quantize_q2_K.argtypes = [
  5655. ctypes.POINTER(ctypes.c_float),
  5656. ctypes.c_void_p,
  5657. ctypes.c_int,
  5658. ctypes.c_int,
  5659. ctypes.POINTER(ctypes.c_int64),
  5660. ]
  5661. lib.ggml_quantize_q2_K.restype = ctypes.c_size_t
  5662. # GGML_API size_t ggml_quantize_q3_K(const float * src, void * dst, int n, int k, int64_t * hist);
  5663. def ggml_quantize_q3_K(
  5664. src: CFloatArray,
  5665. dst: ctypes.c_void_p,
  5666. n: Union[ctypes.c_int, int],
  5667. k: Union[ctypes.c_int, int],
  5668. hist: CInt64Array,
  5669. ) -> int:
  5670. return lib.ggml_quantize_q3_K(src, dst, n, k, hist)
  5671. lib.ggml_quantize_q3_K.argtypes = [
  5672. ctypes.POINTER(ctypes.c_float),
  5673. ctypes.c_void_p,
  5674. ctypes.c_int,
  5675. ctypes.c_int,
  5676. ctypes.POINTER(ctypes.c_int64),
  5677. ]
  5678. lib.ggml_quantize_q3_K.restype = ctypes.c_size_t
  5679. # GGML_API size_t ggml_quantize_q4_K(const float * src, void * dst, int n, int k, int64_t * hist);
  5680. def ggml_quantize_q4_K(
  5681. src: CFloatArray,
  5682. dst: ctypes.c_void_p,
  5683. n: Union[ctypes.c_int, int],
  5684. k: Union[ctypes.c_int, int],
  5685. hist: CInt64Array,
  5686. ) -> int:
  5687. return lib.ggml_quantize_q4_K(src, dst, n, k, hist)
  5688. lib.ggml_quantize_q4_K.argtypes = [
  5689. ctypes.POINTER(ctypes.c_float),
  5690. ctypes.c_void_p,
  5691. ctypes.c_int,
  5692. ctypes.c_int,
  5693. ctypes.POINTER(ctypes.c_int64),
  5694. ]
  5695. lib.ggml_quantize_q4_K.restype = ctypes.c_size_t
  5696. # GGML_API size_t ggml_quantize_q5_K(const float * src, void * dst, int n, int k, int64_t * hist);
  5697. def ggml_quantize_q5_K(
  5698. src: CFloatArray,
  5699. dst: ctypes.c_void_p,
  5700. n: Union[ctypes.c_int, int],
  5701. k: Union[ctypes.c_int, int],
  5702. hist: CInt64Array,
  5703. ) -> int:
  5704. return lib.ggml_quantize_q5_K(src, dst, n, k, hist)
  5705. lib.ggml_quantize_q5_K.argtypes = [
  5706. ctypes.POINTER(ctypes.c_float),
  5707. ctypes.c_void_p,
  5708. ctypes.c_int,
  5709. ctypes.c_int,
  5710. ctypes.POINTER(ctypes.c_int64),
  5711. ]
  5712. lib.ggml_quantize_q5_K.restype = ctypes.c_size_t
  5713. # GGML_API size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist);
  5714. def ggml_quantize_q6_K(
  5715. src: CFloatArray,
  5716. dst: ctypes.c_void_p,
  5717. n: Union[ctypes.c_int, int],
  5718. k: Union[ctypes.c_int, int],
  5719. hist: CInt64Array,
  5720. ) -> int:
  5721. return lib.ggml_quantize_q6_K(src, dst, n, k, hist)
  5722. lib.ggml_quantize_q6_K.argtypes = [
  5723. ctypes.POINTER(ctypes.c_float),
  5724. ctypes.c_void_p,
  5725. ctypes.c_int,
  5726. ctypes.c_int,
  5727. ctypes.POINTER(ctypes.c_int64),
  5728. ]
  5729. lib.ggml_quantize_q6_K.restype = ctypes.c_size_t
  5730. # GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist);
  5731. def ggml_quantize_chunk(
  5732. type: Union[ctypes.c_int, int],
  5733. src: CFloatArray,
  5734. dst: ctypes.c_void_p,
  5735. start: Union[ctypes.c_int, int],
  5736. n: Union[ctypes.c_int, int],
  5737. hist: CInt64Array,
  5738. ) -> int:
  5739. return lib.ggml_quantize_chunk(type, src, dst, start, n, hist)
  5740. lib.ggml_quantize_chunk.argtypes = [
  5741. ctypes.c_int,
  5742. ctypes.POINTER(ctypes.c_float),
  5743. ctypes.c_void_p,
  5744. ctypes.c_int,
  5745. ctypes.c_int,
  5746. ctypes.POINTER(ctypes.c_int64),
  5747. ]
  5748. lib.ggml_quantize_chunk.restype = ctypes.c_size_t
  5749. # //
  5750. # // gguf
  5751. # //
  5752. # enum gguf_type {
  5753. # GGUF_TYPE_UINT8 = 0,
  5754. # GGUF_TYPE_INT8 = 1,
  5755. # GGUF_TYPE_UINT16 = 2,
  5756. # GGUF_TYPE_INT16 = 3,
  5757. # GGUF_TYPE_UINT32 = 4,
  5758. # GGUF_TYPE_INT32 = 5,
  5759. # GGUF_TYPE_FLOAT32 = 6,
  5760. # GGUF_TYPE_BOOL = 7,
  5761. # GGUF_TYPE_STRING = 8,
  5762. # GGUF_TYPE_ARRAY = 9,
  5763. # GGUF_TYPE_UINT64 = 10,
  5764. # GGUF_TYPE_INT64 = 11,
  5765. # GGUF_TYPE_FLOAT64 = 12,
  5766. # GGUF_TYPE_COUNT, // marks the end of the enum
  5767. # };
  5768. GGUF_TYPE_UINT8 = 0
  5769. GGUF_TYPE_INT8 = 1
  5770. GGUF_TYPE_UINT16 = 2
  5771. GGUF_TYPE_INT16 = 3
  5772. GGUF_TYPE_UINT32 = 4
  5773. GGUF_TYPE_INT32 = 5
  5774. GGUF_TYPE_FLOAT32 = 6
  5775. GGUF_TYPE_BOOL = 7
  5776. GGUF_TYPE_STRING = 8
  5777. GGUF_TYPE_ARRAY = 9
  5778. GGUF_TYPE_COUNT = 10
  5779. # struct gguf_context;
  5780. gguf_context_p = ctypes.c_void_p
  5781. # //
  5782. # // system info
  5783. # //
  5784. # GGML_API int ggml_cpu_has_avx (void);
  5785. def ggml_cpu_has_avx() -> int:
  5786. return lib.ggml_cpu_has_avx()
  5787. lib.ggml_cpu_has_avx.argtypes = []
  5788. lib.ggml_cpu_has_avx.restype = ctypes.c_int
  5789. # GGML_API int ggml_cpu_has_avx2 (void);
  5790. def ggml_cpu_has_avx2() -> int:
  5791. return lib.ggml_cpu_has_avx2()
  5792. lib.ggml_cpu_has_avx2.argtypes = []
  5793. lib.ggml_cpu_has_avx2.restype = ctypes.c_int
  5794. # GGML_API int ggml_cpu_has_avx512 (void);
  5795. def ggml_cpu_has_avx512() -> int:
  5796. return lib.ggml_cpu_has_avx512()
  5797. lib.ggml_cpu_has_avx512.argtypes = []
  5798. lib.ggml_cpu_has_avx512.restype = ctypes.c_int
  5799. # GGML_API int ggml_cpu_has_avx512_vbmi(void);
  5800. def ggml_cpu_has_avx512_vbmi() -> int:
  5801. return lib.ggml_cpu_has_avx512_vbmi()
  5802. lib.ggml_cpu_has_avx512_vbmi.argtypes = []
  5803. lib.ggml_cpu_has_avx512_vbmi.restype = ctypes.c_int
  5804. # GGML_API int ggml_cpu_has_avx512_vnni(void);
  5805. def ggml_cpu_has_avx512_vnni() -> int:
  5806. return lib.ggml_cpu_has_avx512_vnni()
  5807. lib.ggml_cpu_has_avx512_vnni.argtypes = []
  5808. lib.ggml_cpu_has_avx512_vnni.restype = ctypes.c_int
  5809. # GGML_API int ggml_cpu_has_fma (void);
  5810. def ggml_cpu_has_fma() -> int:
  5811. return lib.ggml_cpu_has_fma()
  5812. lib.ggml_cpu_has_fma.argtypes = []
  5813. lib.ggml_cpu_has_fma.restype = ctypes.c_int
  5814. # GGML_API int ggml_cpu_has_neon (void);
  5815. def ggml_cpu_has_neon() -> int:
  5816. return lib.ggml_cpu_has_neon()
  5817. lib.ggml_cpu_has_neon.argtypes = []
  5818. lib.ggml_cpu_has_neon.restype = ctypes.c_int
  5819. # GGML_API int ggml_cpu_has_arm_fma (void);
  5820. def ggml_cpu_has_arm_fma() -> int:
  5821. return lib.ggml_cpu_has_arm_fma()
  5822. lib.ggml_cpu_has_arm_fma.argtypes = []
  5823. lib.ggml_cpu_has_arm_fma.restype = ctypes.c_int
  5824. # GGML_API int ggml_cpu_has_metal (void);
  5825. def ggml_cpu_has_metal() -> int:
  5826. return lib.ggml_cpu_has_metal()
  5827. lib.ggml_cpu_has_metal.argtypes = []
  5828. lib.ggml_cpu_has_metal.restype = ctypes.c_int
  5829. # GGML_API int ggml_cpu_has_f16c (void);
  5830. def ggml_cpu_has_f16c() -> int:
  5831. return lib.ggml_cpu_has_f16c()
  5832. lib.ggml_cpu_has_f16c.argtypes = []
  5833. lib.ggml_cpu_has_f16c.restype = ctypes.c_int
  5834. # GGML_API int ggml_cpu_has_fp16_va (void);
  5835. def ggml_cpu_has_fp16_va() -> int:
  5836. return lib.ggml_cpu_has_fp16_va()
  5837. lib.ggml_cpu_has_fp16_va.argtypes = []
  5838. lib.ggml_cpu_has_fp16_va.restype = ctypes.c_int
  5839. # GGML_API int ggml_cpu_has_wasm_simd (void);
  5840. def ggml_cpu_has_wasm_simd() -> int:
  5841. return lib.ggml_cpu_has_wasm_simd()
  5842. lib.ggml_cpu_has_wasm_simd.argtypes = []
  5843. lib.ggml_cpu_has_wasm_simd.restype = ctypes.c_int
  5844. # GGML_API int ggml_cpu_has_blas (void);
  5845. def ggml_cpu_has_blas() -> int:
  5846. return lib.ggml_cpu_has_blas()
  5847. lib.ggml_cpu_has_blas.argtypes = []
  5848. lib.ggml_cpu_has_blas.restype = ctypes.c_int
  5849. # GGML_API int ggml_cpu_has_cublas (void);
  5850. def ggml_cpu_has_cublas() -> int:
  5851. return lib.ggml_cpu_has_cublas()
  5852. lib.ggml_cpu_has_cublas.argtypes = []
  5853. lib.ggml_cpu_has_cublas.restype = ctypes.c_int
  5854. # GGML_API int ggml_cpu_has_clblast (void);
  5855. def ggml_cpu_has_clblast() -> int:
  5856. return lib.ggml_cpu_has_clblast()
  5857. lib.ggml_cpu_has_clblast.argtypes = []
  5858. lib.ggml_cpu_has_clblast.restype = ctypes.c_int
  5859. # GGML_API int ggml_cpu_has_gpublas (void);
  5860. def ggml_cpu_has_gpublas() -> int:
  5861. return lib.ggml_cpu_has_gpublas()
  5862. lib.ggml_cpu_has_gpublas.argtypes = []
  5863. lib.ggml_cpu_has_gpublas.restype = ctypes.c_int
  5864. # GGML_API int ggml_cpu_has_sse3 (void);
  5865. def ggml_cpu_has_sse3() -> int:
  5866. return lib.ggml_cpu_has_sse3()
  5867. lib.ggml_cpu_has_sse3.argtypes = []
  5868. lib.ggml_cpu_has_sse3.restype = ctypes.c_int
  5869. # GGML_API int ggml_cpu_has_ssse3 (void);
  5870. def ggml_cpu_has_ssse3() -> int:
  5871. return lib.ggml_cpu_has_ssse3()
  5872. lib.ggml_cpu_has_ssse3.argtypes = []
  5873. lib.ggml_cpu_has_ssse3.restype = ctypes.c_int
  5874. # GGML_API int ggml_cpu_has_vsx (void);
  5875. def ggml_cpu_has_vsx() -> int:
  5876. return lib.ggml_cpu_has_vsx()
  5877. lib.ggml_cpu_has_vsx.argtypes = []
  5878. lib.ggml_cpu_has_vsx.restype = ctypes.c_int
  5879. # //
  5880. # // Internal types and functions exposed for tests and benchmarks
  5881. # //
  5882. # typedef void (*ggml_to_float_t)(const void * x, float * y, int k);
  5883. ggml_to_float_t = ctypes.CFUNCTYPE(
  5884. None, ctypes.c_void_p, ctypes.POINTER(ctypes.c_float), ctypes.c_int
  5885. )
  5886. # typedef void (*ggml_from_float_t)(const float * x, void * y, int k);
  5887. ggml_from_float_t = ctypes.CFUNCTYPE(
  5888. None, ctypes.POINTER(ctypes.c_float), ctypes.c_void_p, ctypes.c_int
  5889. )
  5890. # typedef void (*ggml_vec_dot_t)(const int n, float * s, const void * x, const void * y);
  5891. ggml_vec_dot_t = ctypes.CFUNCTYPE(
  5892. None, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.c_void_p, ctypes.c_void_p
  5893. )
  5894. # typedef struct {
  5895. # const char * type_name;
  5896. # int blck_size;
  5897. # size_t type_size;
  5898. # bool is_quantized;
  5899. # ggml_to_float_t to_float;
  5900. # ggml_from_float_t from_float;
  5901. # ggml_from_float_t from_float_reference;
  5902. # ggml_vec_dot_t vec_dot;
  5903. # enum ggml_type vec_dot_type;
  5904. # } ggml_type_traits_t;
  5905. class ggml_type_traits_t(ctypes.Structure):
  5906. _fields_ = [
  5907. ("type_name", ctypes.c_char_p),
  5908. ("blck_size", ctypes.c_int),
  5909. ("type_size", ctypes.c_size_t),
  5910. ("is_quantized", ctypes.c_bool),
  5911. ("to_float", ggml_to_float_t),
  5912. ("from_float", ggml_from_float_t),
  5913. ("from_float_reference", ggml_from_float_t),
  5914. ("vec_dot", ggml_vec_dot_t),
  5915. ("vec_dot_type", ctypes.c_int),
  5916. ]
  5917. # GGML_API ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type);
  5918. def ggml_internal_get_type_traits(type: Union[ctypes.c_int, int]) -> ggml_type_traits_t:
  5919. return lib.ggml_internal_get_type_traits(type)
  5920. lib.ggml_internal_get_type_traits.argtypes = [ctypes.c_int]
  5921. lib.ggml_internal_get_type_traits.restype = ggml_type_traits_t
  5922. #####################################################
  5923. # GGML ALLOC API
  5924. # source: ggml-alloc.h
  5925. #####################################################
  5926. # struct ggml_backend;
  5927. # struct ggml_backend_buffer;
  5928. # struct ggml_backend_buffer_type;
  5929. ggml_backend_t = ctypes.c_void_p
  5930. ggml_backend_buffer_p = ctypes.c_void_p
  5931. ggml_backend_buffer_type_p = ctypes.c_void_p
  5932. # //
  5933. # // Legacy API
  5934. # //
  5935. # typedef struct ggml_allocr * ggml_allocr_t;
  5936. ggml_allocr_t = ctypes.c_void_p
  5937. # // initialize allocator for use with CPU backend only
  5938. # GGML_API ggml_allocr_t ggml_allocr_new(void * data, size_t size, size_t alignment);
  5939. def ggml_allocr_new(
  5940. data: ctypes.c_void_p,
  5941. size: Union[ctypes.c_size_t, int],
  5942. alignment: Union[ctypes.c_size_t, int],
  5943. ) -> ggml_allocr_t:
  5944. return lib.ggml_allocr_new(data, size, alignment)
  5945. lib.ggml_allocr_new.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t]
  5946. lib.ggml_allocr_new.restype = ggml_allocr_t
  5947. # GGML_API ggml_allocr_t ggml_allocr_new_measure(size_t alignment);
  5948. def ggml_allocr_new_measure(alignment: Union[ctypes.c_size_t, int]) -> ggml_allocr_t:
  5949. return lib.ggml_allocr_new_measure(alignment)
  5950. lib.ggml_allocr_new_measure.argtypes = [ctypes.c_size_t]
  5951. lib.ggml_allocr_new_measure.restype = ggml_allocr_t
  5952. # // initialize allocator for use with ggml-backend
  5953. # GGML_API ggml_allocr_t ggml_allocr_new_from_buffer(struct ggml_backend_buffer * buffer);
  5954. def ggml_allocr_new_from_buffer(buffer: ggml_backend_buffer_p) -> ggml_allocr_t:
  5955. return lib.ggml_allocr_new_from_buffer(buffer)
  5956. lib.ggml_allocr_new_from_buffer.argtypes = [ggml_backend_buffer_p]
  5957. lib.ggml_allocr_new_from_buffer.restype = ggml_allocr_t
  5958. # GGML_API ggml_allocr_t ggml_allocr_new_from_backend(struct ggml_backend * backend, size_t size); // allocates an owned buffer
  5959. def ggml_allocr_new_from_backend(
  5960. backend: ggml_backend_t, size: Union[ctypes.c_size_t, int]
  5961. ) -> ggml_allocr_t:
  5962. return lib.ggml_allocr_new_from_backend(backend, size)
  5963. lib.ggml_allocr_new_from_backend.argtypes = [ggml_backend_t, ctypes.c_size_t]
  5964. lib.ggml_allocr_new_from_backend.restype = ggml_allocr_t
  5965. # GGML_API ggml_allocr_t ggml_allocr_new_measure_from_backend(struct ggml_backend * backend);
  5966. def ggml_allocr_new_measure_from_backend(backend: ggml_backend_t) -> ggml_allocr_t:
  5967. return lib.ggml_allocr_new_measure_from_backend(backend)
  5968. lib.ggml_allocr_new_measure_from_backend.argtypes = [ggml_backend_t]
  5969. lib.ggml_allocr_new_measure_from_backend.restype = ggml_allocr_t
  5970. # GGML_API struct ggml_backend_buffer * ggml_allocr_get_buffer(ggml_allocr_t alloc);
  5971. def ggml_allocr_get_buffer(alloc: ggml_allocr_t) -> ggml_backend_buffer_p:
  5972. return lib.ggml_allocr_get_buffer(alloc)
  5973. lib.ggml_allocr_get_buffer.argtypes = [ggml_allocr_t]
  5974. lib.ggml_allocr_get_buffer.restype = ggml_backend_buffer_p
  5975. # // tell the allocator to parse nodes following the order described in the list
  5976. # // you should call this if your graph are optimized to execute out-of-order
  5977. # GGML_API void ggml_allocr_set_parse_seq(ggml_allocr_t alloc, const int * list, int n);
  5978. def ggml_allocr_set_parse_seq(
  5979. alloc: ggml_allocr_t,
  5980. list: "ctypes._Pointer(ctypes.c_int)", # type: ignore
  5981. n: Union[ctypes.c_int, int],
  5982. ) -> None:
  5983. return lib.ggml_allocr_set_parse_seq(alloc, list, n)
  5984. lib.ggml_allocr_set_parse_seq.argtypes = [
  5985. ggml_allocr_t,
  5986. ctypes.POINTER(ctypes.c_int),
  5987. ctypes.c_int,
  5988. ]
  5989. lib.ggml_allocr_set_parse_seq.restype = None
  5990. # GGML_API void ggml_allocr_free (ggml_allocr_t alloc);
  5991. def ggml_allocr_free(alloc: ggml_allocr_t) -> None:
  5992. return lib.ggml_allocr_free(alloc)
  5993. lib.ggml_allocr_free.argtypes = [ggml_allocr_t]
  5994. lib.ggml_allocr_free.restype = None
  5995. # GGML_API bool ggml_allocr_is_measure (ggml_allocr_t alloc);
  5996. def ggml_allocr_is_measure(alloc: ggml_allocr_t) -> ctypes.c_bool:
  5997. return lib.ggml_allocr_is_measure(alloc)
  5998. lib.ggml_allocr_is_measure.argtypes = [ggml_allocr_t]
  5999. lib.ggml_allocr_is_measure.restype = ctypes.c_bool
  6000. # GGML_API void ggml_allocr_reset (ggml_allocr_t alloc);
  6001. def ggml_allocr_reset(alloc: ggml_allocr_t) -> None:
  6002. return lib.ggml_allocr_reset(alloc)
  6003. lib.ggml_allocr_reset.argtypes = [ggml_allocr_t]
  6004. lib.ggml_allocr_reset.restype = None
  6005. # GGML_API void ggml_allocr_alloc (ggml_allocr_t alloc, struct ggml_tensor * tensor);
  6006. def ggml_allocr_alloc(alloc: ggml_allocr_t, tensor: ggml_tensor_p) -> None:
  6007. return lib.ggml_allocr_alloc(alloc, tensor)
  6008. lib.ggml_allocr_alloc.argtypes = [ggml_allocr_t, ctypes.POINTER(ggml_tensor)]
  6009. lib.ggml_allocr_alloc.restype = None
  6010. # GGML_API size_t ggml_allocr_max_size (ggml_allocr_t alloc);
  6011. def ggml_allocr_max_size(alloc: ggml_allocr_t) -> Union[ctypes.c_size_t, int]:
  6012. return lib.ggml_allocr_max_size(alloc)
  6013. lib.ggml_allocr_max_size.argtypes = [ggml_allocr_t]
  6014. lib.ggml_allocr_max_size.restype = ctypes.c_size_t
  6015. # GGML_API size_t ggml_allocr_alloc_graph(ggml_allocr_t alloc, struct ggml_cgraph * graph);
  6016. def ggml_allocr_alloc_graph(alloc: ggml_allocr_t, graph: ggml_cgraph_p) -> int:
  6017. return lib.ggml_allocr_alloc_graph(alloc, graph)
  6018. lib.ggml_allocr_alloc_graph.argtypes = [ggml_allocr_t, ctypes.POINTER(ggml_cgraph)]
  6019. lib.ggml_allocr_alloc_graph.restype = ctypes.c_size_t
  6020. # //
  6021. # // ggml-backend v2 API
  6022. # //
  6023. # // Separate tensor and graph allocator objects
  6024. # // This is necessary for multi-backend allocation because the graph allocator needs to use multiple tensor allocators
  6025. # // The original API is kept as a wrapper around the new API
  6026. # // Tensor allocator
  6027. # typedef struct ggml_tallocr * ggml_tallocr_t;
  6028. ggml_tallocr_t = ctypes.c_void_p
  6029. # GGML_API ggml_tallocr_t ggml_tallocr_new(void * data, size_t size, size_t alignment);
  6030. def ggml_tallocr_new(
  6031. data: ctypes.c_void_p,
  6032. size: Union[ctypes.c_size_t, int],
  6033. alignment: Union[ctypes.c_size_t, int],
  6034. ) -> ggml_tallocr_t:
  6035. return lib.ggml_tallocr_new(data, size, alignment)
  6036. lib.ggml_tallocr_new.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t]
  6037. lib.ggml_tallocr_new.restype = ggml_tallocr_t
  6038. # GGML_API ggml_tallocr_t ggml_tallocr_new_measure(size_t alignment);
  6039. def ggml_tallocr_new_measure(alignment: Union[ctypes.c_size_t, int]) -> ggml_tallocr_t:
  6040. return lib.ggml_tallocr_new_measure(alignment)
  6041. lib.ggml_tallocr_new_measure.argtypes = [ctypes.c_size_t]
  6042. lib.ggml_tallocr_new_measure.restype = ggml_tallocr_t
  6043. # GGML_API ggml_tallocr_t ggml_tallocr_new_from_buffer(struct ggml_backend_buffer * buffer);
  6044. def ggml_tallocr_new_from_buffer(buffer: ggml_backend_buffer_p) -> ggml_tallocr_t:
  6045. return lib.ggml_tallocr_new_from_buffer(buffer)
  6046. lib.ggml_tallocr_new_from_buffer.argtypes = [ggml_backend_buffer_p]
  6047. lib.ggml_tallocr_new_from_buffer.restype = ggml_tallocr_t
  6048. # GGML_API ggml_tallocr_t ggml_tallocr_new_from_backend(struct ggml_backend * backend, size_t size); // allocates an owned buffer
  6049. def ggml_tallocr_new_from_backend(
  6050. backend: ggml_backend_t, size: Union[ctypes.c_size_t, int]
  6051. ) -> ggml_tallocr_t:
  6052. return lib.ggml_tallocr_new_from_backend(backend, size)
  6053. lib.ggml_tallocr_new_from_backend.argtypes = [ggml_backend_t, ctypes.c_size_t]
  6054. lib.ggml_tallocr_new_from_backend.restype = ggml_tallocr_t
  6055. # GGML_API ggml_tallocr_t ggml_tallocr_new_measure_from_backend(struct ggml_backend * backend);
  6056. def ggml_tallocr_new_measure_from_backend(backend: ggml_backend_t) -> ggml_tallocr_t:
  6057. return lib.ggml_tallocr_new_measure_from_backend(backend)
  6058. lib.ggml_tallocr_new_measure_from_backend.argtypes = [ggml_backend_t]
  6059. lib.ggml_tallocr_new_measure_from_backend.restype = ggml_tallocr_t
  6060. # GGML_API struct ggml_backend_buffer * ggml_tallocr_get_buffer(ggml_tallocr_t talloc);
  6061. def ggml_tallocr_get_buffer(talloc: ggml_tallocr_t) -> ggml_backend_buffer_p:
  6062. return lib.ggml_tallocr_get_buffer(talloc)
  6063. lib.ggml_tallocr_get_buffer.argtypes = [ggml_tallocr_t]
  6064. lib.ggml_tallocr_get_buffer.restype = ggml_backend_buffer_p
  6065. # GGML_API void ggml_tallocr_free (ggml_tallocr_t talloc);
  6066. def ggml_tallocr_free(talloc: ggml_tallocr_t) -> None:
  6067. return lib.ggml_tallocr_free(talloc)
  6068. lib.ggml_tallocr_free.argtypes = [ggml_tallocr_t]
  6069. lib.ggml_tallocr_free.restype = None
  6070. # GGML_API bool ggml_tallocr_is_measure (ggml_tallocr_t talloc);
  6071. def ggml_tallocr_is_measure(talloc: ggml_tallocr_t) -> bool:
  6072. return lib.ggml_tallocr_is_measure(talloc)
  6073. lib.ggml_tallocr_is_measure.argtypes = [ggml_tallocr_t]
  6074. lib.ggml_tallocr_is_measure.restype = ctypes.c_bool
  6075. # GGML_API void ggml_tallocr_reset (ggml_tallocr_t talloc);
  6076. def ggml_tallocr_reset(talloc: ggml_tallocr_t) -> None:
  6077. return lib.ggml_tallocr_reset(talloc)
  6078. lib.ggml_tallocr_reset.argtypes = [ggml_tallocr_t]
  6079. lib.ggml_tallocr_reset.restype = None
  6080. # GGML_API void ggml_tallocr_alloc (ggml_tallocr_t talloc, struct ggml_tensor * tensor);
  6081. def ggml_tallocr_alloc(talloc: ggml_tallocr_t, tensor: ggml_tensor_p) -> None:
  6082. return lib.ggml_tallocr_alloc(talloc, tensor)
  6083. lib.ggml_tallocr_alloc.argtypes = [ggml_tallocr_t, ctypes.POINTER(ggml_tensor)]
  6084. lib.ggml_tallocr_alloc.restype = None
  6085. # GGML_API size_t ggml_tallocr_max_size (ggml_tallocr_t talloc);
  6086. def ggml_tallocr_max_size(talloc: ggml_tallocr_t) -> Union[ctypes.c_size_t, int]:
  6087. return lib.ggml_tallocr_max_size(talloc)
  6088. lib.ggml_tallocr_max_size.argtypes = [ggml_tallocr_t]
  6089. lib.ggml_tallocr_max_size.restype = ctypes.c_size_t
  6090. # // Graph allocator
  6091. # typedef struct ggml_gallocr * ggml_gallocr_t;
  6092. ggml_gallocr_t = ctypes.c_void_p
  6093. # GGML_API ggml_gallocr_t ggml_gallocr_new(void);
  6094. def ggml_gallocr_new() -> ggml_gallocr_t:
  6095. return lib.ggml_gallocr_new()
  6096. lib.ggml_gallocr_new.argtypes = []
  6097. lib.ggml_gallocr_new.restype = ggml_gallocr_t
  6098. # GGML_API void ggml_gallocr_free(ggml_gallocr_t galloc);
  6099. def ggml_gallocr_free(galloc: ggml_gallocr_t) -> None:
  6100. return lib.ggml_gallocr_free(galloc)
  6101. lib.ggml_gallocr_free.argtypes = [ggml_gallocr_t]
  6102. lib.ggml_gallocr_free.restype = None
  6103. # GGML_API void ggml_gallocr_set_parse_seq(ggml_gallocr_t galloc, const int * list, int n);
  6104. def ggml_gallocr_set_parse_seq(
  6105. galloc: ggml_gallocr_t,
  6106. list: "ctypes._Pointer(ctypes.c_int)", # type: ignore
  6107. n: Union[ctypes.c_int, int],
  6108. ) -> None:
  6109. return lib.ggml_gallocr_set_parse_seq(galloc, list, n)
  6110. lib.ggml_gallocr_set_parse_seq.argtypes = [
  6111. ggml_gallocr_t,
  6112. ctypes.POINTER(ctypes.c_int),
  6113. ctypes.c_int,
  6114. ]
  6115. lib.ggml_gallocr_set_parse_seq.restype = None
  6116. # GGML_API size_t ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, ggml_tallocr_t talloc, struct ggml_cgraph * graph);
  6117. def ggml_gallocr_alloc_graph(
  6118. galloc: ggml_gallocr_t, talloc: ggml_tallocr_t, graph: ggml_cgraph_p
  6119. ) -> Union[ctypes.c_size_t, int]:
  6120. return lib.ggml_gallocr_alloc_graph(galloc, talloc, graph)
  6121. lib.ggml_gallocr_alloc_graph.argtypes = [
  6122. ggml_gallocr_t,
  6123. ggml_tallocr_t,
  6124. ctypes.POINTER(ggml_cgraph),
  6125. ]
  6126. lib.ggml_gallocr_alloc_graph.restype = ctypes.c_size_t
  6127. # // Allocate tensors from the allocators given by the hash table
  6128. # GGML_API void ggml_gallocr_alloc_graph_n(
  6129. # ggml_gallocr_t galloc,
  6130. # struct ggml_cgraph * graph,
  6131. # struct ggml_hash_set hash_set,
  6132. # ggml_tallocr_t * hash_node_talloc);
  6133. def ggml_gallocr_alloc_graph_n(
  6134. galloc: ggml_gallocr_t,
  6135. graph: ggml_cgraph_p,
  6136. hash_set: ggml_hash_set,
  6137. hash_node_talloc: "ctypes._Pointer(ggml_tallocr_t)", # type: ignore
  6138. ) -> None:
  6139. return lib.ggml_gallocr_alloc_graph_n(galloc, graph, hash_set, hash_node_talloc)
  6140. lib.ggml_gallocr_alloc_graph_n.argtypes = [
  6141. ggml_gallocr_t,
  6142. ctypes.POINTER(ggml_cgraph),
  6143. ggml_hash_set,
  6144. ctypes.POINTER(ggml_tallocr_t),
  6145. ]
  6146. lib.ggml_gallocr_alloc_graph_n.restype = None
  6147. # // Utils
  6148. # // Create a buffer and allocate all the tensors in a ggml_context
  6149. # GGML_API struct ggml_backend_buffer * ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, struct ggml_backend_buffer_type * buft);
  6150. def ggml_backend_alloc_ctx_tensors_from_buft(
  6151. ctx: ggml_context_p, buft: ggml_backend_buffer_type_p
  6152. ) -> ggml_backend_buffer_p:
  6153. return lib.ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft)
  6154. lib.ggml_backend_alloc_ctx_tensors_from_buft.argtypes = [
  6155. ggml_context_p,
  6156. ggml_backend_buffer_type_p,
  6157. ]
  6158. lib.ggml_backend_alloc_ctx_tensors_from_buft.restype = ggml_backend_buffer_p
  6159. # GGML_API struct ggml_backend_buffer * ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, struct ggml_backend * backend);
  6160. def ggml_backend_alloc_ctx_tensors(
  6161. ctx: ggml_context_p, backend: ggml_backend_t
  6162. ) -> ggml_backend_buffer_p:
  6163. return lib.ggml_backend_alloc_ctx_tensors(ctx, backend)
  6164. lib.ggml_backend_alloc_ctx_tensors.argtypes = [
  6165. ggml_context_p,
  6166. ggml_backend_t,
  6167. ]
  6168. lib.ggml_backend_alloc_ctx_tensors.restype = ggml_backend_buffer_p
  6169. #####################################################
  6170. # GGML Backend API
  6171. # source: ggml-backend.h
  6172. #####################################################
  6173. # typedef struct ggml_backend_buffer_type * ggml_backend_buffer_type_t;
  6174. # typedef struct ggml_backend_buffer * ggml_backend_buffer_t;
  6175. # typedef struct ggml_backend * ggml_backend_t;
  6176. # typedef void * ggml_backend_graph_plan_t;
  6177. ggml_backend_buffer_type_t = ctypes.c_void_p
  6178. ggml_backend_buffer_t = ctypes.c_void_p
  6179. ggml_backend_t = ctypes.c_void_p
  6180. ggml_backend_graph_plan_t = ctypes.c_void_p
  6181. # //
  6182. # // Backend buffer
  6183. # //
  6184. # // buffer type
  6185. # GGML_API ggml_backend_buffer_t ggml_backend_buft_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  6186. def ggml_backend_buft_alloc_buffer(
  6187. buft: ggml_backend_buffer_type_t, size: Union[ctypes.c_size_t, int]
  6188. ) -> ggml_backend_buffer_t:
  6189. return lib.ggml_backend_buft_alloc_buffer(buft, size)
  6190. lib.ggml_backend_buft_alloc_buffer.argtypes = [
  6191. ggml_backend_buffer_type_t,
  6192. ctypes.c_size_t,
  6193. ]
  6194. lib.ggml_backend_buft_alloc_buffer.restype = ggml_backend_buffer_t
  6195. # GGML_API size_t ggml_backend_buft_get_alignment (ggml_backend_buffer_type_t buft);
  6196. def ggml_backend_buft_get_alignment(
  6197. buft: ggml_backend_buffer_type_t,
  6198. ) -> int:
  6199. return lib.ggml_backend_buft_get_alignment(buft)
  6200. lib.ggml_backend_buft_get_alignment.argtypes = [ggml_backend_buffer_type_t]
  6201. lib.ggml_backend_buft_get_alignment.restype = ctypes.c_size_t
  6202. # GGML_API size_t ggml_backend_buft_get_alloc_size(ggml_backend_buffer_type_t buft, struct ggml_tensor * tensor);
  6203. def ggml_backend_buft_get_alloc_size(
  6204. buft: ggml_backend_buffer_type_t, tensor: ggml_tensor_p
  6205. ) -> int:
  6206. return lib.ggml_backend_buft_get_alloc_size(buft, tensor)
  6207. lib.ggml_backend_buft_get_alloc_size.argtypes = [
  6208. ggml_backend_buffer_type_t,
  6209. ctypes.POINTER(ggml_tensor),
  6210. ]
  6211. lib.ggml_backend_buft_get_alloc_size.restype = ctypes.c_size_t
  6212. # GGML_API bool ggml_backend_buft_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend);
  6213. def ggml_backend_buft_supports_backend(
  6214. buft: ggml_backend_buffer_type_t, backend: ggml_backend_t
  6215. ) -> bool:
  6216. return lib.ggml_backend_buft_supports_backend(buft, backend)
  6217. lib.ggml_backend_buft_supports_backend.argtypes = [
  6218. ggml_backend_buffer_type_t,
  6219. ggml_backend_t,
  6220. ]
  6221. lib.ggml_backend_buft_supports_backend.restype = ctypes.c_bool
  6222. # // buffer
  6223. # GGML_API void ggml_backend_buffer_free (ggml_backend_buffer_t buffer);
  6224. def ggml_backend_buffer_free(
  6225. buffer: ggml_backend_buffer_t,
  6226. ):
  6227. return lib.ggml_backend_buffer_free(buffer)
  6228. lib.ggml_backend_buffer_free.argtypes = [ggml_backend_buffer_t]
  6229. lib.ggml_backend_buffer_free.restype = None
  6230. # GGML_API void * ggml_backend_buffer_get_base (ggml_backend_buffer_t buffer);
  6231. def ggml_backend_buffer_get_base(
  6232. buffer: ggml_backend_buffer_t,
  6233. ) -> ctypes.c_void_p:
  6234. return lib.ggml_backend_buffer_get_base(buffer)
  6235. lib.ggml_backend_buffer_get_base.argtypes = [ggml_backend_buffer_t]
  6236. lib.ggml_backend_buffer_get_base.restype = ctypes.c_void_p
  6237. # GGML_API size_t ggml_backend_buffer_get_size (ggml_backend_buffer_t buffer);
  6238. def ggml_backend_buffer_get_size(
  6239. buffer: ggml_backend_buffer_t,
  6240. ) -> int:
  6241. return lib.ggml_backend_buffer_get_size(buffer)
  6242. lib.ggml_backend_buffer_get_size.argtypes = [ggml_backend_buffer_t]
  6243. lib.ggml_backend_buffer_get_size.restype = ctypes.c_size_t
  6244. # GGML_API void ggml_backend_buffer_init_tensor (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
  6245. def ggml_backend_buffer_init_tensor(
  6246. buffer: ggml_backend_buffer_t,
  6247. tensor: ggml_tensor_p,
  6248. ):
  6249. return lib.ggml_backend_buffer_init_tensor(buffer, tensor)
  6250. lib.ggml_backend_buffer_init_tensor.argtypes = [
  6251. ggml_backend_buffer_t,
  6252. ctypes.POINTER(ggml_tensor),
  6253. ]
  6254. lib.ggml_backend_buffer_init_tensor.restype = None
  6255. # GGML_API size_t ggml_backend_buffer_get_alignment (ggml_backend_buffer_t buffer);
  6256. def ggml_backend_buffer_get_alignment(
  6257. buffer: ggml_backend_buffer_t,
  6258. ) -> int:
  6259. return lib.ggml_backend_buffer_get_alignment(buffer)
  6260. lib.ggml_backend_buffer_get_alignment.argtypes = [ggml_backend_buffer_t]
  6261. lib.ggml_backend_buffer_get_alignment.restype = ctypes.c_size_t
  6262. # GGML_API size_t ggml_backend_buffer_get_alloc_size(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
  6263. def ggml_backend_buffer_get_alloc_size(
  6264. buffer: ggml_backend_buffer_t, tensor: ggml_tensor_p
  6265. ) -> int:
  6266. return lib.ggml_backend_buffer_get_alloc_size(buffer, tensor)
  6267. lib.ggml_backend_buffer_get_alloc_size.argtypes = [
  6268. ggml_backend_buffer_t,
  6269. ctypes.POINTER(ggml_tensor),
  6270. ]
  6271. lib.ggml_backend_buffer_get_alloc_size.restype = ctypes.c_size_t
  6272. # GGML_API ggml_backend_buffer_type_t ggml_backend_buffer_type(ggml_backend_buffer_t buffer);
  6273. def ggml_backend_buffer_type(
  6274. buffer: ggml_backend_buffer_t,
  6275. ) -> ggml_backend_buffer_type_t:
  6276. return lib.ggml_backend_buffer_type(buffer)
  6277. lib.ggml_backend_buffer_type.argtypes = [ggml_backend_buffer_t]
  6278. lib.ggml_backend_buffer_type.restype = ggml_backend_buffer_type_t
  6279. # //
  6280. # // Backend
  6281. # //
  6282. # GGML_API const char * ggml_backend_name(ggml_backend_t backend);
  6283. def ggml_backend_name(
  6284. backend: ggml_backend_t,
  6285. ) -> bytes:
  6286. return lib.ggml_backend_name(backend)
  6287. lib.ggml_backend_name.argtypes = [ggml_backend_t]
  6288. lib.ggml_backend_name.restype = ctypes.c_char_p
  6289. # GGML_API void ggml_backend_free(ggml_backend_t backend);
  6290. def ggml_backend_free(
  6291. backend: ggml_backend_t,
  6292. ):
  6293. return lib.ggml_backend_free(backend)
  6294. lib.ggml_backend_free.argtypes = [ggml_backend_t]
  6295. lib.ggml_backend_free.restype = None
  6296. # GGML_API ggml_backend_buffer_type_t ggml_backend_get_default_buffer_type(ggml_backend_t backend);
  6297. def ggml_backend_get_default_buffer_type(
  6298. backend: ggml_backend_t,
  6299. ) -> ggml_backend_buffer_type_t:
  6300. return lib.ggml_backend_get_default_buffer_type(backend)
  6301. lib.ggml_backend_get_default_buffer_type.argtypes = [ggml_backend_t]
  6302. lib.ggml_backend_get_default_buffer_type.restype = ggml_backend_buffer_type_t
  6303. # GGML_API ggml_backend_buffer_t ggml_backend_alloc_buffer(ggml_backend_t backend, size_t size);
  6304. def ggml_backend_alloc_buffer(
  6305. backend: ggml_backend_t,
  6306. size: Union[ctypes.c_size_t, int],
  6307. ) -> ggml_backend_buffer_t:
  6308. return lib.ggml_backend_alloc_buffer(backend, size)
  6309. lib.ggml_backend_alloc_buffer.argtypes = [ggml_backend_t, ctypes.c_size_t]
  6310. lib.ggml_backend_alloc_buffer.restype = ggml_backend_buffer_t
  6311. # GGML_API size_t ggml_backend_get_alignment(ggml_backend_t backend);
  6312. def ggml_backend_get_alignment(
  6313. backend: ggml_backend_t,
  6314. ) -> int:
  6315. return lib.ggml_backend_get_alignment(backend)
  6316. lib.ggml_backend_get_alignment.argtypes = [ggml_backend_t]
  6317. lib.ggml_backend_get_alignment.restype = ctypes.c_size_t
  6318. # GGML_API void ggml_backend_tensor_set_async(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  6319. def ggml_backend_tensor_set_async(
  6320. backend: ggml_backend_t,
  6321. tensor: ggml_tensor_p,
  6322. data: ctypes.c_void_p,
  6323. offset: Union[ctypes.c_size_t, int],
  6324. size: Union[ctypes.c_size_t, int],
  6325. ):
  6326. return lib.ggml_backend_tensor_set_async(backend, tensor, data, offset, size)
  6327. lib.ggml_backend_tensor_set_async.argtypes = [
  6328. ggml_backend_t,
  6329. ctypes.POINTER(ggml_tensor),
  6330. ctypes.c_void_p,
  6331. ctypes.c_size_t,
  6332. ctypes.c_size_t,
  6333. ]
  6334. lib.ggml_backend_tensor_set_async.restype = None
  6335. # GGML_API void ggml_backend_tensor_get_async(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  6336. def ggml_backend_tensor_get_async(
  6337. backend: ggml_backend_t,
  6338. tensor: ggml_tensor_p,
  6339. data: ctypes.c_void_p,
  6340. offset: Union[ctypes.c_size_t, int],
  6341. size: Union[ctypes.c_size_t, int],
  6342. ):
  6343. return lib.ggml_backend_tensor_get_async(backend, tensor, data, offset, size)
  6344. lib.ggml_backend_tensor_get_async.argtypes = [
  6345. ggml_backend_t,
  6346. ctypes.POINTER(ggml_tensor),
  6347. ctypes.c_void_p,
  6348. ctypes.c_size_t,
  6349. ctypes.c_size_t,
  6350. ]
  6351. lib.ggml_backend_tensor_get_async.restype = None
  6352. # GGML_API void ggml_backend_tensor_set( struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  6353. def ggml_backend_tensor_set(
  6354. tensor: ggml_tensor_p,
  6355. data: ctypes.c_void_p,
  6356. offset: Union[ctypes.c_size_t, int],
  6357. size: Union[ctypes.c_size_t, int],
  6358. ):
  6359. return lib.ggml_backend_tensor_set(tensor, data, offset, size)
  6360. lib.ggml_backend_tensor_set.argtypes = [
  6361. ctypes.POINTER(ggml_tensor),
  6362. ctypes.c_void_p,
  6363. ctypes.c_size_t,
  6364. ctypes.c_size_t,
  6365. ]
  6366. lib.ggml_backend_tensor_set.restype = None
  6367. # GGML_API void ggml_backend_tensor_get(const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  6368. def ggml_backend_tensor_get(
  6369. tensor: ggml_tensor_p,
  6370. data: ctypes.c_void_p,
  6371. offset: Union[ctypes.c_size_t, int],
  6372. size: Union[ctypes.c_size_t, int],
  6373. ):
  6374. return lib.ggml_backend_tensor_get(tensor, data, offset, size)
  6375. lib.ggml_backend_tensor_get.argtypes = [
  6376. ctypes.POINTER(ggml_tensor),
  6377. ctypes.c_void_p,
  6378. ctypes.c_size_t,
  6379. ctypes.c_size_t,
  6380. ]
  6381. lib.ggml_backend_tensor_get.restype = None
  6382. # GGML_API void ggml_backend_synchronize(ggml_backend_t backend);
  6383. def ggml_backend_synchronize(
  6384. backend: ggml_backend_t,
  6385. ):
  6386. return lib.ggml_backend_synchronize(backend)
  6387. lib.ggml_backend_synchronize.argtypes = [ggml_backend_t]
  6388. lib.ggml_backend_synchronize.restype = None
  6389. # GGML_API ggml_backend_graph_plan_t ggml_backend_graph_plan_create (ggml_backend_t backend, struct ggml_cgraph * cgraph);
  6390. def ggml_backend_graph_plan_create(
  6391. backend: ggml_backend_t,
  6392. cgraph: ggml_cgraph_p,
  6393. ) -> ggml_backend_graph_plan_t:
  6394. return lib.ggml_backend_graph_plan_create(backend, cgraph)
  6395. lib.ggml_backend_graph_plan_create.argtypes = [
  6396. ggml_backend_t,
  6397. ctypes.POINTER(ggml_cgraph),
  6398. ]
  6399. lib.ggml_backend_graph_plan_create.restype = ggml_backend_graph_plan_t
  6400. # GGML_API void ggml_backend_graph_plan_free (ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  6401. def ggml_backend_graph_plan_free(
  6402. backend: ggml_backend_t,
  6403. plan: ggml_backend_graph_plan_t,
  6404. ):
  6405. return lib.ggml_backend_graph_plan_free(backend, plan)
  6406. lib.ggml_backend_graph_plan_free.argtypes = [ggml_backend_t, ggml_backend_graph_plan_t]
  6407. lib.ggml_backend_graph_plan_free.restype = None
  6408. # GGML_API void ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  6409. def ggml_backend_graph_plan_compute(
  6410. backend: ggml_backend_t,
  6411. plan: ggml_backend_graph_plan_t,
  6412. ):
  6413. return lib.ggml_backend_graph_plan_compute(backend, plan)
  6414. lib.ggml_backend_graph_plan_compute.argtypes = [
  6415. ggml_backend_t,
  6416. ggml_backend_graph_plan_t,
  6417. ]
  6418. lib.ggml_backend_graph_plan_compute.restype = None
  6419. # GGML_API void ggml_backend_graph_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph);
  6420. def ggml_backend_graph_compute(
  6421. backend: ggml_backend_t,
  6422. cgraph: ggml_cgraph_p,
  6423. ):
  6424. return lib.ggml_backend_graph_compute(backend, cgraph)
  6425. lib.ggml_backend_graph_compute.argtypes = [ggml_backend_t, ctypes.POINTER(ggml_cgraph)]
  6426. lib.ggml_backend_graph_compute.restype = None
  6427. # GGML_API bool ggml_backend_supports_op (ggml_backend_t backend, const struct ggml_tensor * op);
  6428. def ggml_backend_supports_op(
  6429. backend: ggml_backend_t,
  6430. op: ggml_tensor_p,
  6431. ) -> Union[ctypes.c_bool, bool]:
  6432. return lib.ggml_backend_supports_op(backend, op)
  6433. lib.ggml_backend_supports_op.argtypes = [ggml_backend_t, ctypes.POINTER(ggml_tensor)]
  6434. lib.ggml_backend_supports_op.restype = ctypes.c_bool
  6435. # // tensor copy between different backends
  6436. # GGML_API void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst);
  6437. def ggml_backend_tensor_copy(
  6438. src: ggml_tensor_p,
  6439. dst: ggml_tensor_p,
  6440. ):
  6441. return lib.ggml_backend_tensor_copy(src, dst)
  6442. lib.ggml_backend_tensor_copy.argtypes = [
  6443. ctypes.POINTER(ggml_tensor),
  6444. ctypes.POINTER(ggml_tensor),
  6445. ]
  6446. lib.ggml_backend_tensor_copy.restype = None
  6447. # GGML_API void ggml_backend_tensor_copy_async(ggml_backend_t backend, struct ggml_tensor * src, struct ggml_tensor * dst); // automatic fallback to sync copy
  6448. def ggml_backend_tensor_copy_async(
  6449. backend: ggml_backend_t,
  6450. src: ggml_tensor_p,
  6451. dst: ggml_tensor_p,
  6452. ):
  6453. return lib.ggml_backend_tensor_copy_async(backend, src, dst)
  6454. # lib.ggml_backend_tensor_copy_async.argtypes = [
  6455. # ggml_backend_t,
  6456. # ctypes.POINTER(ggml_tensor),
  6457. # ctypes.POINTER(ggml_tensor),
  6458. # ]
  6459. # lib.ggml_backend_tensor_copy_async.restype = None
  6460. # //
  6461. # // CPU backend
  6462. # //
  6463. # GGML_API ggml_backend_t ggml_backend_cpu_init(void);
  6464. def ggml_backend_cpu_init() -> ggml_backend_t:
  6465. return lib.ggml_backend_cpu_init()
  6466. lib.ggml_backend_cpu_init.argtypes = []
  6467. lib.ggml_backend_cpu_init.restype = ggml_backend_t
  6468. # GGML_API bool ggml_backend_is_cpu(ggml_backend_t backend);
  6469. def ggml_backend_is_cpu(
  6470. backend: ggml_backend_t,
  6471. ) -> bool:
  6472. return lib.ggml_backend_is_cpu(backend)
  6473. lib.ggml_backend_is_cpu.argtypes = [ggml_backend_t]
  6474. lib.ggml_backend_is_cpu.restype = ctypes.c_bool
  6475. # GGML_API void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads);
  6476. def ggml_backend_cpu_set_n_threads(
  6477. backend_cpu: ggml_backend_t,
  6478. n_threads: Union[ctypes.c_int, int],
  6479. ):
  6480. return lib.ggml_backend_cpu_set_n_threads(backend_cpu, n_threads)
  6481. lib.ggml_backend_cpu_set_n_threads.argtypes = [ggml_backend_t, ctypes.c_int]
  6482. lib.ggml_backend_cpu_set_n_threads.restype = None
  6483. # // Create a backend buffer from an existing pointer
  6484. # GGML_API ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size);
  6485. def ggml_backend_cpu_buffer_from_ptr(
  6486. ptr: ctypes.c_void_p,
  6487. size: Union[ctypes.c_size_t, int],
  6488. ) -> ggml_backend_buffer_t:
  6489. return lib.ggml_backend_cpu_buffer_from_ptr(ptr, size)
  6490. lib.ggml_backend_cpu_buffer_from_ptr.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
  6491. lib.ggml_backend_cpu_buffer_from_ptr.restype = ggml_backend_buffer_t
  6492. # GGML_API ggml_backend_buffer_type_t ggml_backend_cpu_buffer_type(void);
  6493. def ggml_backend_cpu_buffer_type() -> ggml_backend_buffer_type_t:
  6494. return lib.ggml_backend_cpu_buffer_type()
  6495. lib.ggml_backend_cpu_buffer_type.argtypes = []
  6496. lib.ggml_backend_cpu_buffer_type.restype = ggml_backend_buffer_type_t
  6497. # //
  6498. # // Backend registry
  6499. # //
  6500. # // The backend registry is a registry of all the available backends, and allows initializing backends in a generic way
  6501. # GGML_API size_t ggml_backend_reg_get_count(void);
  6502. def ggml_backend_reg_get_count() -> int:
  6503. return lib.ggml_backend_reg_get_count()
  6504. lib.ggml_backend_reg_get_count.argtypes = []
  6505. lib.ggml_backend_reg_get_count.restype = ctypes.c_size_t
  6506. # GGML_API size_t ggml_backend_reg_find_by_name(const char * name);
  6507. def ggml_backend_reg_find_by_name(
  6508. name: bytes,
  6509. ) -> int:
  6510. return lib.ggml_backend_reg_find_by_name(name)
  6511. lib.ggml_backend_reg_find_by_name.argtypes = [ctypes.c_char_p]
  6512. lib.ggml_backend_reg_find_by_name.restype = ctypes.c_size_t
  6513. # GGML_API ggml_backend_t ggml_backend_reg_init_backend_from_str(const char * backend_str); // str is name[:params]
  6514. def ggml_backend_reg_init_backend_from_str(
  6515. backend_str: bytes,
  6516. ) -> ggml_backend_t:
  6517. return lib.ggml_backend_reg_init_backend_from_str(backend_str)
  6518. lib.ggml_backend_reg_init_backend_from_str.argtypes = [ctypes.c_char_p]
  6519. lib.ggml_backend_reg_init_backend_from_str.restype = ggml_backend_t
  6520. # GGML_API const char * ggml_backend_reg_get_name(size_t i);
  6521. def ggml_backend_reg_get_name(
  6522. i: Union[ctypes.c_size_t, int],
  6523. ) -> bytes:
  6524. return lib.ggml_backend_reg_get_name(i)
  6525. lib.ggml_backend_reg_get_name.argtypes = [ctypes.c_size_t]
  6526. lib.ggml_backend_reg_get_name.restype = ctypes.c_char_p
  6527. # GGML_API ggml_backend_t ggml_backend_reg_init_backend(size_t i, const char * params); // params is backend-specific
  6528. def ggml_backend_reg_init_backend(
  6529. i: Union[ctypes.c_size_t, int],
  6530. params: bytes,
  6531. ) -> ggml_backend_t:
  6532. return lib.ggml_backend_reg_init_backend(i, params)
  6533. lib.ggml_backend_reg_init_backend.argtypes = [ctypes.c_size_t, ctypes.c_char_p]
  6534. lib.ggml_backend_reg_init_backend.restype = ggml_backend_t
  6535. # GGML_API ggml_backend_buffer_type_t ggml_backend_reg_get_default_buffer_type(size_t i);
  6536. def ggml_backend_reg_get_default_buffer_type(
  6537. i: Union[ctypes.c_size_t, int],
  6538. ) -> ggml_backend_buffer_type_t:
  6539. return lib.ggml_backend_reg_get_default_buffer_type(i)
  6540. lib.ggml_backend_reg_get_default_buffer_type.argtypes = [ctypes.c_size_t]
  6541. lib.ggml_backend_reg_get_default_buffer_type.restype = ggml_backend_buffer_type_t
  6542. # GGML_API ggml_backend_buffer_t ggml_backend_reg_alloc_buffer(size_t i, size_t size);
  6543. def ggml_backend_reg_alloc_buffer(
  6544. i: Union[ctypes.c_size_t, int],
  6545. size: Union[ctypes.c_size_t, int],
  6546. ) -> ggml_backend_buffer_t:
  6547. return lib.ggml_backend_reg_alloc_buffer(i, size)
  6548. lib.ggml_backend_reg_alloc_buffer.argtypes = [ctypes.c_size_t, ctypes.c_size_t]
  6549. lib.ggml_backend_reg_alloc_buffer.restype = ggml_backend_buffer_t
  6550. # //
  6551. # // Backend scheduler
  6552. # //
  6553. # // The backend scheduler allows for multiple backends to be used together
  6554. # // Handles compute buffer allocation, assignment of tensors to backends, and copying of tensors between backends
  6555. # // The backends are selected based on:
  6556. # // - the backend that supports the operation
  6557. # // - the location of the pre-allocated tensors (e.g. the weights)
  6558. # /*
  6559. # Example usage:
  6560. # sched = ggml_backend_sched_new({backend_gpu, backend_gpu2, backend_cpu}, num_backends);
  6561. # // sched is initialized with measure allocators and cannot be used until allocated with a measure graph
  6562. # // initialize buffers from a measure graph
  6563. # measure_graph = build_graph(sched); // use the allocr to allocate inputs as needed
  6564. # // in build_graph:
  6565. # build_graph(...) {
  6566. # // allocating tensors in a specific backend (optional, recommended: pre-allocate inputs in a different buffer)
  6567. # alloc_cpu = ggml_backend_sched_get_allocr(sched, backend_cpu);
  6568. # ggml_allocr_alloc(alloc_cpu, tensor);
  6569. # // manually assigning nodes to a backend (optional, shouldn't be needed in most cases)
  6570. # struct ggml_tensor * node = ggml_mul_mat(ctx, ...);
  6571. # ggml_backend_sched_set_node_backend(sched, node, backend_gpu);
  6572. # }
  6573. # // allocate backend buffers from measure graph
  6574. # ggml_backend_sched_init_measure(sched, measure_graph);
  6575. # // the scheduler is now ready to compute graphs
  6576. # // compute
  6577. # graph = build_graph(sched);
  6578. # ggml_backend_sched_graph_compute(sched, graph);
  6579. # */
  6580. # struct ggml_backend_sched;
  6581. # typedef struct ggml_backend_sched * ggml_backend_sched_t;
  6582. ggml_backend_sched_t = ctypes.c_void_p
  6583. # // Initialize a backend scheduler
  6584. # GGML_API ggml_backend_sched_t ggml_backend_sched_new(ggml_backend_t * backends, int n_backends);
  6585. def ggml_backend_sched_new(
  6586. backends: ggml_backend_t,
  6587. n_backends: Union[ctypes.c_int, int],
  6588. ) -> ggml_backend_sched_t:
  6589. return lib.ggml_backend_sched_new(backends, n_backends)
  6590. lib.ggml_backend_sched_new.argtypes = [ggml_backend_t, ctypes.c_int]
  6591. lib.ggml_backend_sched_new.restype = ggml_backend_sched_t
  6592. # GGML_API void ggml_backend_sched_free(ggml_backend_sched_t sched);
  6593. def ggml_backend_sched_free(
  6594. sched: ggml_backend_sched_t,
  6595. ):
  6596. return lib.ggml_backend_sched_free(sched)
  6597. lib.ggml_backend_sched_free.argtypes = [ggml_backend_sched_t]
  6598. lib.ggml_backend_sched_free.restype = None
  6599. # // Initialize backend buffers from a measure graph
  6600. # GGML_API void ggml_backend_sched_init_measure(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph);
  6601. def ggml_backend_sched_init_measure(
  6602. sched: ggml_backend_sched_t,
  6603. measure_graph: ggml_cgraph_p,
  6604. ):
  6605. return lib.ggml_backend_sched_init_measure(sched, measure_graph)
  6606. lib.ggml_backend_sched_init_measure.argtypes = [
  6607. ggml_backend_sched_t,
  6608. ctypes.POINTER(ggml_cgraph),
  6609. ]
  6610. lib.ggml_backend_sched_init_measure.restype = None
  6611. # GGML_API ggml_tallocr_t ggml_backend_sched_get_tallocr(ggml_backend_sched_t sched, ggml_backend_t backend);
  6612. def ggml_backend_sched_get_tallocr(
  6613. sched: ggml_backend_sched_t,
  6614. backend: ggml_backend_t,
  6615. ) -> ggml_tallocr_t:
  6616. return lib.ggml_backend_sched_get_tallocr(sched, backend)
  6617. lib.ggml_backend_sched_get_tallocr.argtypes = [ggml_backend_sched_t, ggml_backend_t]
  6618. lib.ggml_backend_sched_get_tallocr.restype = ggml_tallocr_t
  6619. # GGML_API ggml_backend_buffer_t ggml_backend_sched_get_buffer (ggml_backend_sched_t sched, ggml_backend_t backend);
  6620. def ggml_backend_sched_get_buffer(
  6621. sched: ggml_backend_sched_t,
  6622. backend: ggml_backend_t,
  6623. ) -> ggml_backend_buffer_t:
  6624. return lib.ggml_backend_sched_get_buffer(sched, backend)
  6625. lib.ggml_backend_sched_get_buffer.argtypes = [ggml_backend_sched_t, ggml_backend_t]
  6626. lib.ggml_backend_sched_get_buffer.restype = ggml_backend_buffer_t
  6627. # GGML_API void ggml_backend_sched_set_node_backend(ggml_backend_sched_t sched, struct ggml_tensor * node, ggml_backend_t backend);
  6628. def ggml_backend_sched_set_node_backend(
  6629. sched: ggml_backend_sched_t,
  6630. node: ggml_tensor_p,
  6631. backend: ggml_backend_t,
  6632. ):
  6633. return lib.ggml_backend_sched_set_node_backend(sched, node, backend)
  6634. lib.ggml_backend_sched_set_node_backend.argtypes = [
  6635. ggml_backend_sched_t,
  6636. ctypes.POINTER(ggml_tensor),
  6637. ggml_backend_t,
  6638. ]
  6639. lib.ggml_backend_sched_set_node_backend.restype = None
  6640. # // Allocate a graph on the backend scheduler
  6641. # GGML_API void ggml_backend_sched_graph_compute(
  6642. # ggml_backend_sched_t sched,
  6643. # struct ggml_cgraph * graph);
  6644. def ggml_backend_sched_graph_compute(
  6645. sched: ggml_backend_sched_t,
  6646. graph: ggml_cgraph_p,
  6647. ):
  6648. return lib.ggml_backend_sched_graph_compute(sched, graph)
  6649. lib.ggml_backend_sched_graph_compute.argtypes = [
  6650. ggml_backend_sched_t,
  6651. ctypes.POINTER(ggml_cgraph),
  6652. ]
  6653. lib.ggml_backend_sched_graph_compute.restype = None
  6654. # //
  6655. # // Utils
  6656. # //
  6657. # struct ggml_backend_graph_copy {
  6658. # ggml_backend_buffer_t buffer;
  6659. # struct ggml_context * ctx_allocated;
  6660. # struct ggml_context * ctx_unallocated;
  6661. # struct ggml_cgraph * graph;
  6662. # };
  6663. class ggml_backend_graph_copy(ctypes.Structure):
  6664. _fields_ = [
  6665. ("buffer", ggml_backend_buffer_t),
  6666. ("ctx_allocated", ggml_context_p),
  6667. ("ctx_unallocated", ggml_context_p),
  6668. ("graph", ctypes.POINTER(ggml_cgraph)),
  6669. ]
  6670. ggml_backend_graph_copy_t = ggml_backend_graph_copy
  6671. # // Copy a graph to a different backend
  6672. # GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph);
  6673. def ggml_backend_graph_copy(
  6674. backend: ggml_backend_t,
  6675. graph: ggml_cgraph_p,
  6676. ) -> ggml_backend_graph_copy_t:
  6677. return lib.ggml_backend_graph_copy(backend, graph)
  6678. lib.ggml_backend_graph_copy.argtypes = [
  6679. ggml_backend_t,
  6680. ctypes.POINTER(ggml_cgraph),
  6681. ]
  6682. lib.ggml_backend_graph_copy.restype = ggml_backend_graph_copy_t
  6683. # GGML_API void ggml_backend_graph_copy_free(struct ggml_backend_graph_copy copy);
  6684. def ggml_backend_graph_copy_free(
  6685. copy: ggml_backend_graph_copy_t,
  6686. ):
  6687. return lib.ggml_backend_graph_copy_free(copy)
  6688. lib.ggml_backend_graph_copy_free.argtypes = [ggml_backend_graph_copy_t]
  6689. lib.ggml_backend_graph_copy_free.restype = None
  6690. # typedef bool (*ggml_backend_eval_callback)(int node_index, struct ggml_tensor * t1, struct ggml_tensor * t2, void * user_data);
  6691. ggml_backend_eval_callback = ctypes.CFUNCTYPE(
  6692. ctypes.c_bool,
  6693. ctypes.c_int,
  6694. ctypes.POINTER(ggml_tensor),
  6695. ctypes.POINTER(ggml_tensor),
  6696. ctypes.c_void_p,
  6697. )
  6698. # // Compare the output of two backends
  6699. # GGML_API void ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t backend2, struct ggml_cgraph * graph, ggml_backend_eval_callback callback, void * user_data);
  6700. def ggml_backend_compare_graph_backend(
  6701. backend1: ggml_backend_t,
  6702. backend2: ggml_backend_t,
  6703. graph: ggml_cgraph_p,
  6704. callback,
  6705. user_data: ctypes.c_void_p,
  6706. ):
  6707. return lib.ggml_backend_compare_graph_backend(
  6708. backend1, backend2, graph, callback, user_data
  6709. )
  6710. lib.ggml_backend_compare_graph_backend.argtypes = [
  6711. ggml_backend_t,
  6712. ggml_backend_t,
  6713. ctypes.POINTER(ggml_cgraph),
  6714. ggml_backend_eval_callback,
  6715. ctypes.c_void_p,
  6716. ]
  6717. lib.ggml_backend_compare_graph_backend.restype = None
  6718. # // Tensor initialization
  6719. # GGML_API void ggml_backend_tensor_alloc(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, void * addr);
  6720. def ggml_backend_tensor_alloc(
  6721. buffer: ggml_backend_buffer_t,
  6722. tensor: ggml_tensor_p,
  6723. addr: ctypes.c_void_p,
  6724. ):
  6725. return lib.ggml_backend_tensor_alloc(buffer, tensor, addr)
  6726. lib.ggml_backend_tensor_alloc.argtypes = [
  6727. ggml_backend_buffer_t,
  6728. ctypes.POINTER(ggml_tensor),
  6729. ctypes.c_void_p,
  6730. ]
  6731. lib.ggml_backend_tensor_alloc.restype = None
  6732. # GGML_API void ggml_backend_view_init(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
  6733. def ggml_backend_view_init(
  6734. buffer: ggml_backend_buffer_t,
  6735. tensor: ggml_tensor_p,
  6736. ):
  6737. return lib.ggml_backend_view_init(buffer, tensor)
  6738. lib.ggml_backend_view_init.argtypes = [
  6739. ggml_backend_buffer_t,
  6740. ctypes.POINTER(ggml_tensor),
  6741. ]
  6742. lib.ggml_backend_view_init.restype = None
  6743. #####################################################
  6744. # GGML Backend Implementation API
  6745. # source: ggml-backend-impl.h
  6746. #####################################################
  6747. # //
  6748. # // Backend buffer
  6749. # //
  6750. # // buffer type
  6751. # typedef void * ggml_backend_buffer_type_context_t;
  6752. ggml_backend_buffer_type_context_t = ctypes.c_void_p
  6753. # struct ggml_backend_buffer_type_i {
  6754. # ggml_backend_buffer_t (*alloc_buffer) (ggml_backend_buffer_type_t buft, size_t size);
  6755. # size_t (*get_alignment) (ggml_backend_buffer_type_t buft); // tensor alignment
  6756. # size_t (*get_alloc_size) (ggml_backend_buffer_type_t buft, struct ggml_tensor * tensor); // data size needed to allocate the tensor, including padding
  6757. # bool (*supports_backend)(ggml_backend_buffer_type_t buft, ggml_backend_t backend); // check if the buffer type is usable by the backend
  6758. # };
  6759. ggml_backend_buffer_i_alloc_buffer = ctypes.CFUNCTYPE(
  6760. ggml_backend_buffer_t, ggml_backend_buffer_type_t, ctypes.c_size_t
  6761. )
  6762. ggml_backend_buffer_i_get_alignment = ctypes.CFUNCTYPE(
  6763. ctypes.c_size_t, ggml_backend_buffer_type_t
  6764. )
  6765. ggml_backend_buffer_i_get_alloc_size = ctypes.CFUNCTYPE(
  6766. ctypes.c_size_t, ggml_backend_buffer_type_t, ctypes.POINTER(ggml_tensor)
  6767. )
  6768. ggml_backend_buffer_i_supports_backend = ctypes.CFUNCTYPE(
  6769. ctypes.c_bool, ggml_backend_buffer_type_t, ggml_backend_t
  6770. )
  6771. class ggml_backend_buffer_type_i(ctypes.Structure):
  6772. _fields_ = [
  6773. ("alloc_buffer", ggml_backend_buffer_i_alloc_buffer),
  6774. ("get_alignment", ggml_backend_buffer_i_get_alignment),
  6775. ("get_alloc_size", ggml_backend_buffer_i_get_alloc_size),
  6776. ("supports_backend", ggml_backend_buffer_i_supports_backend),
  6777. ]
  6778. # struct ggml_backend_buffer_type {
  6779. # struct ggml_backend_buffer_type_i iface;
  6780. # ggml_backend_buffer_type_context_t context;
  6781. # };
  6782. class ggml_backend_buffer_type(ctypes.Structure):
  6783. _fields_ = [
  6784. ("iface", ggml_backend_buffer_type_i),
  6785. ("context", ggml_backend_buffer_type_context_t),
  6786. ]
  6787. # typedef void * ggml_backend_buffer_context_t;
  6788. ggml_backend_buffer_context_t = ctypes.c_void_p
  6789. # struct ggml_backend_buffer_i {
  6790. # void (*free_buffer)(ggml_backend_buffer_t buffer);
  6791. # //void (*reset) (ggml_backend_buffer_t buffer); // reset any internal state due to tensor initialization, such as tensor extras
  6792. # void * (*get_base) (ggml_backend_buffer_t buffer);
  6793. # void (*init_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
  6794. # void (*set_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  6795. # void (*get_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  6796. # // (optional) copy tensor between different buffer-type, allow for single-copy tranfers
  6797. # void (*cpy_tensor_from)(ggml_backend_buffer_t buffer, struct ggml_tensor * src, struct ggml_tensor * dst);
  6798. # void (*cpy_tensor_to) (ggml_backend_buffer_t buffer, struct ggml_tensor * src, struct ggml_tensor * dst);
  6799. # };
  6800. ggml_backend_buffer_i_free_buffer = ctypes.CFUNCTYPE(None, ggml_backend_buffer_t)
  6801. ggml_backend_buffer_i_get_base = ctypes.CFUNCTYPE(
  6802. ctypes.c_void_p, ggml_backend_buffer_t
  6803. )
  6804. ggml_backend_buffer_i_init_tensor = ctypes.CFUNCTYPE(
  6805. None, ggml_backend_buffer_t, ctypes.POINTER(ggml_tensor)
  6806. )
  6807. ggml_backend_buffer_i_set_tensor = ctypes.CFUNCTYPE(
  6808. None,
  6809. ggml_backend_buffer_t,
  6810. ctypes.POINTER(ggml_tensor),
  6811. ctypes.c_void_p,
  6812. ctypes.c_size_t,
  6813. ctypes.c_size_t,
  6814. )
  6815. ggml_backend_buffer_i_get_tensor = ctypes.CFUNCTYPE(
  6816. None,
  6817. ggml_backend_buffer_t,
  6818. ctypes.POINTER(ggml_tensor),
  6819. ctypes.c_void_p,
  6820. ctypes.c_size_t,
  6821. ctypes.c_size_t,
  6822. )
  6823. ggml_backend_buffer_i_cpy_tensor_from = ctypes.CFUNCTYPE(
  6824. None,
  6825. ggml_backend_buffer_t,
  6826. ctypes.POINTER(ggml_tensor),
  6827. ctypes.POINTER(ggml_tensor),
  6828. )
  6829. ggml_backend_buffer_i_cpy_tensor_to = ctypes.CFUNCTYPE(
  6830. None,
  6831. ggml_backend_buffer_t,
  6832. ctypes.POINTER(ggml_tensor),
  6833. ctypes.POINTER(ggml_tensor),
  6834. )
  6835. class ggml_backend_buffer_i(ctypes.Structure):
  6836. _fields_ = [
  6837. ("free_buffer", ggml_backend_buffer_i_free_buffer),
  6838. ("get_base", ggml_backend_buffer_i_get_base),
  6839. ("init_tensor", ggml_backend_buffer_i_init_tensor),
  6840. ("set_tensor", ggml_backend_buffer_i_set_tensor),
  6841. ("get_tensor", ggml_backend_buffer_i_get_tensor),
  6842. ("cpy_tensor_from", ggml_backend_buffer_i_cpy_tensor_from),
  6843. ("cpy_tensor_to", ggml_backend_buffer_i_cpy_tensor_to),
  6844. ]
  6845. # struct ggml_backend_buffer {
  6846. # struct ggml_backend_buffer_i iface;
  6847. # ggml_backend_buffer_type_t buft;
  6848. # ggml_backend_buffer_context_t context;
  6849. # size_t size;
  6850. # };
  6851. class ggml_backend_buffer(ctypes.Structure):
  6852. _fields_ = [
  6853. ("iface", ggml_backend_buffer_i),
  6854. ("buft", ggml_backend_buffer_type_t),
  6855. ("context", ggml_backend_buffer_context_t),
  6856. ("size", ctypes.c_size_t),
  6857. ]
  6858. # ggml_backend_buffer_t ggml_backend_buffer_init(
  6859. # ggml_backend_buffer_type_t buft,
  6860. # struct ggml_backend_buffer_i iface,
  6861. # ggml_backend_buffer_context_t context,
  6862. # size_t size);
  6863. def ggml_backend_buffer_init(
  6864. buft: ggml_backend_buffer_type_t,
  6865. iface: ggml_backend_buffer_i,
  6866. context: ggml_backend_buffer_context_t,
  6867. size: Union[ctypes.c_size_t, int],
  6868. ) -> ggml_backend_buffer_t:
  6869. return lib.ggml_backend_buffer_init(buft, iface, context, size)
  6870. lib.ggml_backend_buffer_init.argtypes = [
  6871. ggml_backend_buffer_type_t,
  6872. ggml_backend_buffer_i,
  6873. ggml_backend_buffer_context_t,
  6874. ctypes.c_size_t,
  6875. ]
  6876. lib.ggml_backend_buffer_init.restype = ggml_backend_buffer_t
  6877. # //
  6878. # // Backend
  6879. # //
  6880. # typedef void * ggml_backend_context_t;
  6881. ggml_backend_context_t = ctypes.c_void_p
  6882. # struct ggml_backend_i {
  6883. # const char * (*get_name)(ggml_backend_t backend);
  6884. # void (*free)(ggml_backend_t backend);
  6885. # // buffer allocation
  6886. # ggml_backend_buffer_type_t (*get_default_buffer_type)(ggml_backend_t backend);
  6887. # // (optional) asynchroneous tensor data access
  6888. # void (*set_tensor_async)(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  6889. # void (*get_tensor_async)(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  6890. # // (optional) asynchroneous tensor copy
  6891. # void (*cpy_tensor_from_async)(ggml_backend_t backend, struct ggml_tensor * src, struct ggml_tensor * dst);
  6892. # void (*cpy_tensor_to_async) (ggml_backend_t backend, struct ggml_tensor * src, struct ggml_tensor * dst);
  6893. # void (*synchronize) (ggml_backend_t backend);
  6894. # // compute graph with a plan
  6895. # ggml_backend_graph_plan_t (*graph_plan_create) (ggml_backend_t backend, struct ggml_cgraph * cgraph);
  6896. # void (*graph_plan_free) (ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  6897. # void (*graph_plan_compute)(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  6898. # // compute graph without a plan
  6899. # void (*graph_compute)(ggml_backend_t backend, struct ggml_cgraph * cgraph);
  6900. # // check if the backend supports an operation
  6901. # bool (*supports_op)(ggml_backend_t backend, const struct ggml_tensor * op);
  6902. # };
  6903. ggml_backend_i_get_name = ctypes.CFUNCTYPE(ctypes.c_char_p, ggml_backend_t)
  6904. ggml_backend_i_free = ctypes.CFUNCTYPE(None, ggml_backend_t)
  6905. ggml_backend_i_get_default_buffer_type = ctypes.CFUNCTYPE(
  6906. ggml_backend_buffer_type_t, ggml_backend_t
  6907. )
  6908. ggml_backend_i_set_tensor_async = ctypes.CFUNCTYPE(
  6909. None,
  6910. ggml_backend_t,
  6911. ctypes.POINTER(ggml_tensor),
  6912. ctypes.c_void_p,
  6913. ctypes.c_size_t,
  6914. ctypes.c_size_t,
  6915. )
  6916. ggml_backend_i_get_tensor_async = ctypes.CFUNCTYPE(
  6917. None,
  6918. ggml_backend_t,
  6919. ctypes.POINTER(ggml_tensor),
  6920. ctypes.c_void_p,
  6921. ctypes.c_size_t,
  6922. ctypes.c_size_t,
  6923. )
  6924. ggml_backend_i_cpy_tensor_from_async = ctypes.CFUNCTYPE(
  6925. None, ggml_backend_t, ctypes.POINTER(ggml_tensor), ctypes.POINTER(ggml_tensor)
  6926. )
  6927. ggml_backend_i_cpy_tensor_to_async = ctypes.CFUNCTYPE(
  6928. None, ggml_backend_t, ctypes.POINTER(ggml_tensor), ctypes.POINTER(ggml_tensor)
  6929. )
  6930. ggml_backend_i_synchronize = ctypes.CFUNCTYPE(None, ggml_backend_t)
  6931. ggml_backend_i_graph_plan_create = ctypes.CFUNCTYPE(
  6932. ggml_backend_graph_plan_t, ggml_backend_t, ctypes.POINTER(ggml_cgraph)
  6933. )
  6934. ggml_backend_i_graph_plan_free = ctypes.CFUNCTYPE(
  6935. None, ggml_backend_t, ggml_backend_graph_plan_t
  6936. )
  6937. ggml_backend_i_graph_plan_compute = ctypes.CFUNCTYPE(
  6938. None, ggml_backend_t, ggml_backend_graph_plan_t
  6939. )
  6940. ggml_backend_i_graph_compute = ctypes.CFUNCTYPE(
  6941. None, ggml_backend_t, ctypes.POINTER(ggml_cgraph)
  6942. )
  6943. ggml_backend_i_supports_op = ctypes.CFUNCTYPE(
  6944. ctypes.c_bool, ggml_backend_t, ctypes.POINTER(ggml_tensor)
  6945. )
  6946. class ggml_backend_i(ctypes.Structure):
  6947. _fields_ = [
  6948. ("get_name", ggml_backend_i_get_name),
  6949. ("free", ggml_backend_i_free),
  6950. ("get_default_buffer_type", ggml_backend_i_get_default_buffer_type),
  6951. ("set_tensor_async", ggml_backend_i_set_tensor_async),
  6952. ("get_tensor_async", ggml_backend_i_get_tensor_async),
  6953. ("cpy_tensor_from_async", ggml_backend_i_cpy_tensor_from_async),
  6954. ("cpy_tensor_to_async", ggml_backend_i_cpy_tensor_to_async),
  6955. ("synchronize", ggml_backend_i_synchronize),
  6956. ("graph_plan_create", ggml_backend_i_graph_plan_create),
  6957. ("graph_plan_free", ggml_backend_i_graph_plan_free),
  6958. ("graph_plan_compute", ggml_backend_i_graph_plan_compute),
  6959. ("graph_compute", ggml_backend_i_graph_compute),
  6960. ("supports_op", ggml_backend_i_supports_op),
  6961. ]
  6962. # struct ggml_backend {
  6963. # struct ggml_backend_i iface;
  6964. # ggml_backend_context_t context;
  6965. # };
  6966. class ggml_backend(ctypes.Structure):
  6967. _fields_ = [
  6968. ("iface", ggml_backend_i),
  6969. ("context", ggml_backend_context_t),
  6970. ]
  6971. # //
  6972. # // Backend registry
  6973. # //
  6974. # typedef ggml_backend_t (*ggml_backend_init_fn)(const char * params, void * user_data);
  6975. ggml_backend_init_fn = ctypes.CFUNCTYPE(
  6976. ggml_backend_t, ctypes.c_char_p, ctypes.c_void_p
  6977. )
  6978. # void ggml_backend_register(const char * name, ggml_backend_init_fn init_fn, ggml_backend_buffer_type_t default_buffer_type, void * user_data);
  6979. def ggml_backend_register(
  6980. name: bytes,
  6981. init_fn,
  6982. default_buffer_type: ggml_backend_buffer_type_t,
  6983. user_data: ctypes.c_void_p,
  6984. ):
  6985. return lib.ggml_backend_register(name, init_fn, default_buffer_type, user_data)
  6986. lib.ggml_backend_register.argtypes = [
  6987. ctypes.c_char_p,
  6988. ggml_backend_init_fn,
  6989. ggml_backend_buffer_type_t,
  6990. ctypes.c_void_p,
  6991. ]
  6992. lib.ggml_backend_register.restype = None
  6993. #####################################################
  6994. # GGML CUDA API
  6995. # source: ggml-cuda.h
  6996. #####################################################
  6997. GGML_USE_CUBLAS = hasattr(lib, "ggml_init_cublas")
  6998. GGML_CUDA_MAX_DEVICES = 16
  6999. # // Always success. To check if CUDA is actually loaded, use `ggml_cublas_loaded`.
  7000. # GGML_API void ggml_init_cublas(void);
  7001. def ggml_init_cublas():
  7002. return lib.ggml_init_cublas()
  7003. if GGML_USE_CUBLAS:
  7004. lib.ggml_init_cublas.argtypes = []
  7005. lib.ggml_init_cublas.restype = None
  7006. # // Returns `true` if there are available CUDA devices and cublas loads successfully; otherwise, it returns `false`.
  7007. # GGML_API bool ggml_cublas_loaded(void);
  7008. def ggml_cublas_loaded() -> bool:
  7009. return lib.ggml_cublas_loaded()
  7010. if GGML_USE_CUBLAS:
  7011. lib.ggml_cublas_loaded.argtypes = []
  7012. lib.ggml_cublas_loaded.restype = ctypes.c_bool
  7013. # void * ggml_cuda_host_malloc(size_t size);
  7014. def ggml_cuda_host_malloc(
  7015. size: Union[ctypes.c_size_t, int],
  7016. ) -> Optional[ctypes.c_void_p]:
  7017. return lib.ggml_cuda_host_malloc(size)
  7018. if GGML_USE_CUBLAS:
  7019. lib.ggml_cuda_host_malloc.argtypes = [ctypes.c_size_t]
  7020. lib.ggml_cuda_host_malloc.restype = ctypes.c_void_p
  7021. # void ggml_cuda_host_free(void * ptr);
  7022. def ggml_cuda_host_free(
  7023. ptr: ctypes.c_void_p,
  7024. ):
  7025. return lib.ggml_cuda_host_free(ptr)
  7026. if GGML_USE_CUBLAS:
  7027. lib.ggml_cuda_host_free.argtypes = [ctypes.c_void_p]
  7028. lib.ggml_cuda_host_free.restype = None
  7029. # GGML_API bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  7030. def ggml_cuda_can_mul_mat(
  7031. src0: ggml_tensor_p,
  7032. src1: ggml_tensor_p,
  7033. dst: ggml_tensor_p,
  7034. ) -> bool:
  7035. return lib.ggml_cuda_can_mul_mat(src0, src1, dst)
  7036. if GGML_USE_CUBLAS:
  7037. lib.ggml_cuda_can_mul_mat.argtypes = [
  7038. ctypes.POINTER(ggml_tensor),
  7039. ctypes.POINTER(ggml_tensor),
  7040. ctypes.POINTER(ggml_tensor),
  7041. ]
  7042. lib.ggml_cuda_can_mul_mat.restype = ctypes.c_bool
  7043. # GGML_API void ggml_cuda_set_tensor_split(const float * tensor_split);
  7044. def ggml_cuda_set_tensor_split(
  7045. tensor_split: CFloatArray,
  7046. ):
  7047. return lib.ggml_cuda_set_tensor_split(tensor_split)
  7048. if GGML_USE_CUBLAS:
  7049. lib.ggml_cuda_set_tensor_split.argtypes = [ctypes.POINTER(ctypes.c_float)]
  7050. lib.ggml_cuda_set_tensor_split.restype = None
  7051. # void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor);
  7052. def ggml_cuda_transform_tensor(
  7053. data: ctypes.c_void_p,
  7054. tensor: ggml_tensor_p,
  7055. ):
  7056. return lib.ggml_cuda_transform_tensor(data, tensor)
  7057. if GGML_USE_CUBLAS:
  7058. lib.ggml_cuda_transform_tensor.argtypes = [
  7059. ctypes.c_void_p,
  7060. ctypes.POINTER(ggml_tensor),
  7061. ]
  7062. lib.ggml_cuda_transform_tensor.restype = None
  7063. # void ggml_cuda_free_data(struct ggml_tensor * tensor);
  7064. def ggml_cuda_free_data(
  7065. tensor: ggml_tensor_p,
  7066. ):
  7067. return lib.ggml_cuda_free_data(tensor)
  7068. if GGML_USE_CUBLAS:
  7069. lib.ggml_cuda_free_data.argtypes = [
  7070. ctypes.POINTER(ggml_tensor),
  7071. ]
  7072. lib.ggml_cuda_free_data.restype = None
  7073. # void ggml_cuda_assign_buffers(struct ggml_tensor * tensor);
  7074. def ggml_cuda_assign_buffers(
  7075. tensor: ggml_tensor_p,
  7076. ):
  7077. return lib.ggml_cuda_assign_buffers(tensor)
  7078. if GGML_USE_CUBLAS:
  7079. lib.ggml_cuda_assign_buffers.argtypes = [
  7080. ctypes.POINTER(ggml_tensor),
  7081. ]
  7082. lib.ggml_cuda_assign_buffers.restype = None
  7083. # void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  7084. def ggml_cuda_assign_buffers_no_scratch(
  7085. tensor: ggml_tensor_p,
  7086. ):
  7087. return lib.ggml_cuda_assign_buffers_no_scratch(tensor)
  7088. if GGML_USE_CUBLAS:
  7089. lib.ggml_cuda_assign_buffers_no_scratch.argtypes = [
  7090. ctypes.POINTER(ggml_tensor),
  7091. ]
  7092. lib.ggml_cuda_assign_buffers_no_scratch.restype = None
  7093. # GGML_API void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  7094. def ggml_cuda_assign_buffers_force_inplace(
  7095. tensor: ggml_tensor_p,
  7096. ):
  7097. return lib.ggml_cuda_assign_buffers_force_inplace(tensor)
  7098. if GGML_USE_CUBLAS:
  7099. lib.ggml_cuda_assign_buffers_force_inplace.argtypes = [
  7100. ctypes.POINTER(ggml_tensor),
  7101. ]
  7102. lib.ggml_cuda_assign_buffers_force_inplace.restype = None
  7103. # GGML_API void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  7104. def ggml_cuda_assign_buffers_no_alloc(
  7105. tensor: ggml_tensor_p,
  7106. ):
  7107. return lib.ggml_cuda_assign_buffers_no_alloc(tensor)
  7108. if GGML_USE_CUBLAS:
  7109. lib.ggml_cuda_assign_buffers_no_alloc.argtypes = [
  7110. ctypes.POINTER(ggml_tensor),
  7111. ]
  7112. lib.ggml_cuda_assign_buffers_no_alloc.restype = None
  7113. # GGML_API void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset);
  7114. def ggml_cuda_assign_scratch_offset(
  7115. tensor: ggml_tensor_p,
  7116. offset: Union[ctypes.c_size_t, int],
  7117. ):
  7118. return lib.ggml_cuda_assign_scratch_offset(tensor, offset)
  7119. if GGML_USE_CUBLAS:
  7120. lib.ggml_cuda_assign_scratch_offset.argtypes = [
  7121. ctypes.POINTER(ggml_tensor),
  7122. ctypes.c_size_t,
  7123. ]
  7124. lib.ggml_cuda_assign_scratch_offset.restype = None
  7125. # GGML_API void ggml_cuda_copy_to_device(struct ggml_tensor * tensor);
  7126. def ggml_cuda_copy_to_device(
  7127. tensor: ggml_tensor_p,
  7128. ):
  7129. return lib.ggml_cuda_copy_to_device(tensor)
  7130. if GGML_USE_CUBLAS:
  7131. lib.ggml_cuda_copy_to_device.argtypes = [
  7132. ctypes.POINTER(ggml_tensor),
  7133. ]
  7134. lib.ggml_cuda_copy_to_device.restype = None
  7135. # void ggml_cuda_set_main_device(int main_device);
  7136. def ggml_cuda_set_main_device(
  7137. main_device: Union[ctypes.c_int, int],
  7138. ):
  7139. return lib.ggml_cuda_set_main_device(main_device)
  7140. if GGML_USE_CUBLAS:
  7141. lib.ggml_cuda_set_main_device.argtypes = [
  7142. ctypes.c_int,
  7143. ]
  7144. lib.ggml_cuda_set_main_device.restype = None
  7145. # GGML_API void ggml_cuda_set_mul_mat_q(bool mul_mat_q);
  7146. def ggml_cuda_set_mul_mat_q(
  7147. mul_mat_q: Union[ctypes.c_bool, bool],
  7148. ):
  7149. return lib.ggml_cuda_set_mul_mat_q(mul_mat_q)
  7150. if GGML_USE_CUBLAS:
  7151. lib.ggml_cuda_set_mul_mat_q.argtypes = [
  7152. ctypes.c_bool,
  7153. ]
  7154. lib.ggml_cuda_set_mul_mat_q.restype = None
  7155. # void ggml_cuda_set_scratch_size(size_t scratch_size);
  7156. def ggml_cuda_set_scratch_size(
  7157. scratch_size: Union[ctypes.c_size_t, int],
  7158. ):
  7159. return lib.ggml_cuda_set_scratch_size(scratch_size)
  7160. if GGML_USE_CUBLAS:
  7161. lib.ggml_cuda_set_scratch_size.argtypes = [
  7162. ctypes.c_size_t,
  7163. ]
  7164. lib.ggml_cuda_set_scratch_size.restype = None
  7165. # void ggml_cuda_free_scratch(void);
  7166. def ggml_cuda_free_scratch():
  7167. return lib.ggml_cuda_free_scratch()
  7168. if GGML_USE_CUBLAS:
  7169. lib.ggml_cuda_free_scratch.argtypes = []
  7170. lib.ggml_cuda_free_scratch.restype = None
  7171. # GGML_API bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor);
  7172. def ggml_cuda_compute_forward(
  7173. params: ggml_compute_params_p,
  7174. tensor: ggml_tensor_p,
  7175. ) -> bool:
  7176. return lib.ggml_cuda_compute_forward(params, tensor)
  7177. if GGML_USE_CUBLAS:
  7178. lib.ggml_cuda_compute_forward.argtypes = [
  7179. ctypes.POINTER(ggml_compute_params),
  7180. ctypes.POINTER(ggml_tensor),
  7181. ]
  7182. lib.ggml_cuda_compute_forward.restype = ctypes.c_bool
  7183. # GGML_API int ggml_cuda_get_device_count(void);
  7184. def ggml_cuda_get_device_count() -> int:
  7185. return lib.ggml_cuda_get_device_count()
  7186. if GGML_USE_CUBLAS:
  7187. lib.ggml_cuda_get_device_count.argtypes = []
  7188. lib.ggml_cuda_get_device_count.restype = ctypes.c_int
  7189. # GGML_API void ggml_cuda_get_device_description(int device, char * description, size_t description_size);
  7190. def ggml_cuda_get_device_description(
  7191. device: Union[ctypes.c_int, int],
  7192. description: bytes,
  7193. description_size: Union[ctypes.c_size_t, int],
  7194. ):
  7195. return lib.ggml_cuda_get_device_description(device, description, description_size)
  7196. if GGML_USE_CUBLAS:
  7197. lib.ggml_cuda_get_device_description.argtypes = [
  7198. ctypes.c_int,
  7199. ctypes.c_char_p,
  7200. ctypes.c_size_t,
  7201. ]
  7202. lib.ggml_cuda_get_device_description.restype = None
  7203. # // backend API
  7204. # GGML_API ggml_backend_t ggml_backend_cuda_init(void); // TODO: take a list of devices to use
  7205. def ggml_backend_cuda_init() -> ggml_backend_t:
  7206. return lib.ggml_backend_cuda_init()
  7207. if GGML_USE_CUBLAS:
  7208. lib.ggml_backend_cuda_init.argtypes = []
  7209. lib.ggml_backend_cuda_init.restype = ggml_backend_t
  7210. # GGML_API bool ggml_backend_is_cuda(ggml_backend_t backend);
  7211. def ggml_backend_is_cuda(
  7212. backend: ggml_backend_t,
  7213. ) -> bool:
  7214. return lib.ggml_backend_is_cuda(backend)
  7215. if GGML_USE_CUBLAS:
  7216. lib.ggml_backend_is_cuda.argtypes = [ggml_backend_t]
  7217. lib.ggml_backend_is_cuda.restype = ctypes.c_bool
  7218. # GGML_API int ggml_backend_cuda_get_device(ggml_backend_t backend);
  7219. def ggml_backend_cuda_get_device(
  7220. backend: ggml_backend_t,
  7221. ) -> int:
  7222. return lib.ggml_backend_cuda_get_device(backend)
  7223. if GGML_USE_CUBLAS:
  7224. lib.ggml_backend_cuda_get_device.argtypes = [ggml_backend_t]
  7225. lib.ggml_backend_cuda_get_device.restype = ctypes.c_int
  7226. # GGML_API ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device);
  7227. def ggml_backend_cuda_buffer_type(
  7228. device: Union[ctypes.c_int, int],
  7229. ) -> ggml_backend_buffer_type_t:
  7230. return lib.ggml_backend_cuda_buffer_type(device)
  7231. if GGML_USE_CUBLAS:
  7232. lib.ggml_backend_cuda_buffer_type.argtypes = [ctypes.c_int]
  7233. lib.ggml_backend_cuda_buffer_type.restype = ggml_backend_buffer_type_t
  7234. # // pinned host buffer for use with CPU backend for faster copies between CPU and GPU
  7235. # GGML_API ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type(void);
  7236. def ggml_backend_cuda_host_buffer_type() -> ggml_backend_buffer_type_t:
  7237. return lib.ggml_backend_cuda_host_buffer_type()
  7238. if GGML_USE_CUBLAS:
  7239. lib.ggml_backend_cuda_host_buffer_type.argtypes = []
  7240. lib.ggml_backend_cuda_host_buffer_type.restype = ggml_backend_buffer_type_t
  7241. #####################################################
  7242. # GGML METAL API
  7243. # source: ggml-metal.h
  7244. #####################################################
  7245. GGML_USE_METAL = hasattr(lib, "ggml_metal_init")
  7246. # // max memory buffers that can be mapped to the device
  7247. # #define GGML_METAL_MAX_BUFFERS 64
  7248. GGML_METAL_MAX_BUFFERS = 64
  7249. # #define GGML_METAL_MAX_COMMAND_BUFFERS 32
  7250. GGML_METAL_MAX_COMMAND_BUFFERS = 32
  7251. # struct ggml_metal_context;
  7252. ggml_metal_context_p = ctypes.c_void_p
  7253. # void ggml_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
  7254. def ggml_metal_log_set_callback(
  7255. log_callback, # type: "ctypes._CFuncPtr" # type: ignore
  7256. user_data: ctypes.c_void_p,
  7257. ):
  7258. return lib.ggml_metal_log_set_callback(log_callback, user_data)
  7259. if GGML_USE_METAL:
  7260. lib.ggml_metal_log_set_callback.argtypes = [
  7261. ggml_log_callback,
  7262. ctypes.c_void_p,
  7263. ]
  7264. lib.ggml_metal_log_set_callback.restype = None
  7265. # struct ggml_metal_context * ggml_metal_init(int n_cb);
  7266. def ggml_metal_init(
  7267. n_cb: Union[ctypes.c_int, int],
  7268. ) -> ggml_metal_context_p:
  7269. return lib.ggml_metal_init(n_cb)
  7270. if GGML_USE_METAL:
  7271. lib.ggml_metal_init.argtypes = [ctypes.c_int]
  7272. lib.ggml_metal_init.restype = ggml_metal_context_p
  7273. # void ggml_metal_free(struct ggml_metal_context * ctx);
  7274. def ggml_metal_free(
  7275. ctx: ggml_metal_context_p,
  7276. ):
  7277. return lib.ggml_metal_free(ctx)
  7278. if GGML_USE_METAL:
  7279. lib.ggml_metal_free.argtypes = [ggml_metal_context_p]
  7280. lib.ggml_metal_free.restype = None
  7281. # // set the number of command buffers to use
  7282. # void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
  7283. def ggml_metal_set_n_cb(
  7284. ctx: ggml_metal_context_p,
  7285. n_cb: Union[ctypes.c_int, int],
  7286. ):
  7287. return lib.ggml_metal_set_n_cb(ctx, n_cb)
  7288. if GGML_USE_METAL:
  7289. lib.ggml_metal_set_n_cb.argtypes = [ggml_metal_context_p, ctypes.c_int]
  7290. lib.ggml_metal_set_n_cb.restype = None
  7291. # // creates a mapping between a host memory buffer and a device memory buffer
  7292. # // - make sure to map all buffers used in the graph before calling ggml_metal_graph_compute
  7293. # // - the mapping is used during computation to determine the arguments of the compute kernels
  7294. # // - you don't need to keep the host memory buffer allocated as it is never accessed by Metal
  7295. # // - max_size specifies the maximum size of a tensor and is used to create shared views such
  7296. # // that it is guaranteed that the tensor will fit in at least one of the views
  7297. # //
  7298. # bool ggml_metal_add_buffer(
  7299. # struct ggml_metal_context * ctx,
  7300. # const char * name,
  7301. # void * data,
  7302. # size_t size,
  7303. # size_t max_size);
  7304. def ggml_metal_add_buffer(
  7305. ctx: ggml_metal_context_p,
  7306. name: bytes,
  7307. data: ctypes.c_void_p,
  7308. size: Union[ctypes.c_size_t, int],
  7309. max_size: Union[ctypes.c_size_t, int],
  7310. ) -> bool:
  7311. return lib.ggml_metal_add_buffer(ctx, name, data, size, max_size)
  7312. if GGML_USE_METAL:
  7313. lib.ggml_metal_add_buffer.argtypes = [
  7314. ggml_metal_context_p,
  7315. ctypes.c_char_p,
  7316. ctypes.c_void_p,
  7317. ctypes.c_size_t,
  7318. ctypes.c_size_t,
  7319. ]
  7320. lib.ggml_metal_add_buffer.restype = ctypes.c_bool
  7321. # // set data from host memory into the device
  7322. # void ggml_metal_set_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  7323. def ggml_metal_set_tensor(
  7324. ctx: ggml_metal_context_p,
  7325. t: ggml_tensor_p,
  7326. ):
  7327. return lib.ggml_metal_set_tensor(ctx, t)
  7328. if GGML_USE_METAL:
  7329. lib.ggml_metal_set_tensor.argtypes = [
  7330. ggml_metal_context_p,
  7331. ctypes.POINTER(ggml_tensor),
  7332. ]
  7333. lib.ggml_metal_set_tensor.restype = None
  7334. # // get data from the device into host memory
  7335. # void ggml_metal_get_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  7336. def ggml_metal_get_tensor(
  7337. ctx: ggml_metal_context_p,
  7338. t: ggml_tensor_p,
  7339. ):
  7340. return lib.ggml_metal_get_tensor(ctx, t)
  7341. if GGML_USE_METAL:
  7342. lib.ggml_metal_get_tensor.argtypes = [
  7343. ggml_metal_context_p,
  7344. ctypes.POINTER(ggml_tensor),
  7345. ]
  7346. lib.ggml_metal_get_tensor.restype = None
  7347. # // try to find operations that can be run concurrently in the graph
  7348. # // you should run it again if the topology of your graph changes
  7349. # void ggml_metal_graph_find_concurrency(struct ggml_metal_context * ctx, struct ggml_cgraph * gf, bool check_mem);
  7350. def ggml_metal_graph_find_concurrency(
  7351. ctx: ggml_metal_context_p,
  7352. gf: ggml_cgraph_p,
  7353. check_mem: Union[ctypes.c_bool, bool],
  7354. ):
  7355. return lib.ggml_metal_graph_find_concurrency(ctx, gf, check_mem)
  7356. if GGML_USE_METAL:
  7357. lib.ggml_metal_graph_find_concurrency.argtypes = [
  7358. ggml_metal_context_p,
  7359. ctypes.POINTER(ggml_cgraph),
  7360. ctypes.c_bool,
  7361. ]
  7362. lib.ggml_metal_graph_find_concurrency.restype = None
  7363. # // if the graph has been optimized for concurrently dispatch, return length of the concur_list if optimized
  7364. # int ggml_metal_if_optimized(struct ggml_metal_context * ctx);
  7365. def ggml_metal_if_optimized(
  7366. ctx: ggml_metal_context_p,
  7367. ) -> int:
  7368. return lib.ggml_metal_if_optimized(ctx)
  7369. if GGML_USE_METAL:
  7370. lib.ggml_metal_if_optimized.argtypes = [
  7371. ggml_metal_context_p,
  7372. ]
  7373. lib.ggml_metal_if_optimized.restype = ctypes.c_int
  7374. # // output the concur_list for ggml_alloc
  7375. # int * ggml_metal_get_concur_list(struct ggml_metal_context * ctx);
  7376. def ggml_metal_get_concur_list(
  7377. ctx: ggml_metal_context_p,
  7378. ) -> CIntPointer:
  7379. return lib.ggml_metal_get_concur_list(ctx)
  7380. if GGML_USE_METAL:
  7381. lib.ggml_metal_get_concur_list.argtypes = [
  7382. ggml_metal_context_p,
  7383. ]
  7384. lib.ggml_metal_get_concur_list.restype = ctypes.POINTER(ctypes.c_int)
  7385. # // same as ggml_graph_compute but uses Metal
  7386. # // creates gf->n_threads command buffers in parallel
  7387. # void ggml_metal_graph_compute(struct ggml_metal_context * ctx, struct ggml_cgraph * gf);
  7388. def ggml_metal_graph_compute(
  7389. ctx: ggml_metal_context_p,
  7390. gf: ggml_cgraph_p,
  7391. ):
  7392. return lib.ggml_metal_graph_compute(ctx, gf)
  7393. if GGML_USE_METAL:
  7394. lib.ggml_metal_graph_compute.argtypes = [
  7395. ggml_metal_context_p,
  7396. ctypes.POINTER(ggml_cgraph),
  7397. ]
  7398. lib.ggml_metal_graph_compute.restype = None
  7399. # //
  7400. # // backend API
  7401. # // user-code should use only these functions
  7402. # //
  7403. # GGML_API ggml_backend_t ggml_backend_metal_init(void);
  7404. def ggml_backend_metal_init() -> ggml_backend_t:
  7405. return lib.ggml_backend_metal_init()
  7406. if GGML_USE_METAL:
  7407. lib.ggml_backend_metal_init.argtypes = []
  7408. lib.ggml_backend_metal_init.restype = ggml_backend_t
  7409. # GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
  7410. def ggml_backend_is_metal(
  7411. backend: ggml_backend_t,
  7412. ) -> bool:
  7413. return lib.ggml_backend_is_metal(backend)
  7414. if GGML_USE_METAL:
  7415. lib.ggml_backend_is_metal.argtypes = [ggml_backend_t]
  7416. lib.ggml_backend_is_metal.restype = ctypes.c_bool
  7417. # GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
  7418. def ggml_backend_metal_set_n_cb(
  7419. backend: ggml_backend_t,
  7420. n_cb: Union[ctypes.c_int, int],
  7421. ):
  7422. return lib.ggml_backend_metal_set_n_cb(backend, n_cb)
  7423. if GGML_USE_METAL:
  7424. lib.ggml_backend_metal_set_n_cb.argtypes = [ggml_backend_t, ctypes.c_int]
  7425. lib.ggml_backend_metal_set_n_cb.restype = None
  7426. # GGML_API ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  7427. def ggml_backend_metal_buffer_type() -> ggml_backend_buffer_type_t:
  7428. return lib.ggml_backend_metal_buffer_type()
  7429. if GGML_USE_METAL:
  7430. lib.ggml_backend_metal_buffer_type.argtypes = []
  7431. lib.ggml_backend_metal_buffer_type.restype = ggml_backend_buffer_type_t
  7432. # // helper to check if the device supports a specific family
  7433. # // ideally, the user code should be doing these checks
  7434. # // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  7435. # GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
  7436. def ggml_backend_metal_supports_family(
  7437. backend: ggml_backend_t,
  7438. family: Union[ctypes.c_int, int],
  7439. ) -> bool:
  7440. return lib.ggml_backend_metal_supports_family(backend, family)
  7441. if GGML_USE_METAL:
  7442. lib.ggml_backend_metal_supports_family.argtypes = [ggml_backend_t, ctypes.c_int]
  7443. lib.ggml_backend_metal_supports_family.restype = ctypes.c_bool
  7444. #####################################################
  7445. # GGML OPENCL API
  7446. # source: ggml-opencl.h
  7447. #####################################################
  7448. GGML_USE_CLBLAST = hasattr(lib, "ggml_cl_init")
  7449. # void ggml_cl_init(void);
  7450. def ggml_cl_init():
  7451. return lib.ggml_cl_init()
  7452. if GGML_USE_CLBLAST:
  7453. lib.ggml_cl_init.argtypes = []
  7454. lib.ggml_cl_init.restype = None
  7455. # void ggml_cl_mul(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  7456. def ggml_cl_mul(
  7457. src0: ggml_tensor_p,
  7458. src1: ggml_tensor_p,
  7459. dst: ggml_tensor_p,
  7460. ):
  7461. return lib.ggml_cl_mul(src0, src1, dst)
  7462. if GGML_USE_CLBLAST:
  7463. lib.ggml_cl_mul.argtypes = [
  7464. ctypes.POINTER(ggml_tensor),
  7465. ctypes.POINTER(ggml_tensor),
  7466. ctypes.POINTER(ggml_tensor),
  7467. ]
  7468. lib.ggml_cl_mul.restype = None
  7469. # bool ggml_cl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  7470. def ggml_cl_can_mul_mat(
  7471. src0: ggml_tensor_p,
  7472. src1: ggml_tensor_p,
  7473. dst: ggml_tensor_p,
  7474. ) -> bool:
  7475. return lib.ggml_cl_can_mul_mat(src0, src1, dst)
  7476. if GGML_USE_CLBLAST:
  7477. lib.ggml_cl_can_mul_mat.argtypes = [
  7478. ctypes.POINTER(ggml_tensor),
  7479. ctypes.POINTER(ggml_tensor),
  7480. ctypes.POINTER(ggml_tensor),
  7481. ]
  7482. lib.ggml_cl_can_mul_mat.restype = ctypes.c_bool
  7483. # size_t ggml_cl_mul_mat_get_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  7484. def ggml_cl_mul_mat_get_wsize(
  7485. src0: ggml_tensor_p,
  7486. src1: ggml_tensor_p,
  7487. dst: ggml_tensor_p,
  7488. ) -> int:
  7489. return lib.ggml_cl_mul_mat_get_wsize(src0, src1, dst)
  7490. if GGML_USE_CLBLAST:
  7491. lib.ggml_cl_mul_mat_get_wsize.argtypes = [
  7492. ctypes.POINTER(ggml_tensor),
  7493. ctypes.POINTER(ggml_tensor),
  7494. ctypes.POINTER(ggml_tensor),
  7495. ]
  7496. lib.ggml_cl_mul_mat_get_wsize.restype = ctypes.c_size_t
  7497. # void ggml_cl_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst, void * wdata, size_t wsize);
  7498. def ggml_cl_mul_mat(
  7499. src0: ggml_tensor_p,
  7500. src1: ggml_tensor_p,
  7501. dst: ggml_tensor_p,
  7502. wdata: ctypes.c_void_p,
  7503. wsize: Union[ctypes.c_size_t, int],
  7504. ):
  7505. return lib.ggml_cl_mul_mat(src0, src1, dst, wdata, wsize)
  7506. if GGML_USE_CLBLAST:
  7507. lib.ggml_cl_mul_mat.argtypes = [
  7508. ctypes.POINTER(ggml_tensor),
  7509. ctypes.POINTER(ggml_tensor),
  7510. ctypes.POINTER(ggml_tensor),
  7511. ctypes.c_void_p,
  7512. ctypes.c_size_t,
  7513. ]
  7514. lib.ggml_cl_mul_mat.restype = None
  7515. # NOTE: The following functions are defined in the ggml-opencl.h header file but
  7516. # are not defined in the ggml-opencl.c source file.
  7517. # void * ggml_cl_host_malloc(size_t size);
  7518. # def ggml_cl_host_malloc(
  7519. # size: Union[ctypes.c_size_t, int],
  7520. # ) -> Optional[ctypes.c_void_p]:
  7521. # return lib.ggml_cl_host_malloc(size)
  7522. # if GGML_USE_CLBLAST:
  7523. # lib.ggml_cl_host_malloc.argtypes = [
  7524. # ctypes.c_size_t,
  7525. # ]
  7526. # lib.ggml_cl_host_malloc.restype = ctypes.c_void_p
  7527. # void ggml_cl_host_free(void * ptr);
  7528. # def ggml_cl_host_free(
  7529. # ptr: ctypes.c_void_p,
  7530. # ):
  7531. # return lib.ggml_cl_host_free(ptr)
  7532. # if GGML_USE_CLBLAST:
  7533. # lib.ggml_cl_host_free.argtypes = [
  7534. # ctypes.c_void_p,
  7535. # ]
  7536. # lib.ggml_cl_host_free.restype = None
  7537. # void ggml_cl_free_data(const struct ggml_tensor* tensor);
  7538. def ggml_cl_free_data(
  7539. tensor: ggml_tensor_p,
  7540. ):
  7541. return lib.ggml_cl_free_data(tensor)
  7542. if GGML_USE_CLBLAST:
  7543. lib.ggml_cl_free_data.argtypes = [
  7544. ctypes.POINTER(ggml_tensor),
  7545. ]
  7546. lib.ggml_cl_free_data.restype = None
  7547. # void ggml_cl_transform_tensor(void * data, struct ggml_tensor * tensor);
  7548. def ggml_cl_transform_tensor(
  7549. data: ctypes.c_void_p,
  7550. tensor: ggml_tensor_p,
  7551. ):
  7552. return lib.ggml_cl_transform_tensor(data, tensor)
  7553. if GGML_USE_CLBLAST:
  7554. lib.ggml_cl_transform_tensor.argtypes = [
  7555. ctypes.c_void_p,
  7556. ctypes.POINTER(ggml_tensor),
  7557. ]
  7558. lib.ggml_cl_transform_tensor.restype = None
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...