mlx.go 64.2 KB
Newer Older
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
//go:build mlx

package mlx

/*
#cgo CFLAGS: -O3 -I${SRCDIR}/../../../build/_deps/mlx-c-src
#cgo LDFLAGS: -L${SRCDIR}/../../../build/lib/ollama/ -lmlxc -Wl,-rpath,${SRCDIR}/../../../build/lib/ollama/
#cgo darwin LDFLAGS: -lc++ -framework Metal -framework Foundation -framework Accelerate
#cgo linux LDFLAGS: -lstdc++ -lcuda -lcudart -lnvrtc

#include "mlx/c/mlx.h"
#include <stdlib.h>
#include <stdint.h>

// Cached default GPU stream for all ops
static mlx_stream _default_stream = {0};
static mlx_stream _cpu_stream = {0};

static inline mlx_stream default_stream() {
    if (_default_stream.ctx == NULL) {
        _default_stream = mlx_default_gpu_stream_new();
    }
    return _default_stream;
}

static inline void set_default_stream(mlx_stream s) {
    _default_stream = s;
}

// CPU stream for file loading (Load primitive only runs on CPU)
static inline mlx_stream cpu_stream() {
    if (_cpu_stream.ctx == NULL) {
        _cpu_stream = mlx_default_cpu_stream_new();
    }
    return _cpu_stream;
}

// CGO noescape/nocallback hints to reduce CGO overhead
// noescape: pointers won't escape, no heap allocation needed
// nocallback: function won't call back into Go
#cgo noescape mlx_add
#cgo nocallback mlx_add
#cgo noescape mlx_subtract
#cgo nocallback mlx_subtract
#cgo noescape mlx_multiply
#cgo nocallback mlx_multiply
#cgo noescape mlx_divide
#cgo nocallback mlx_divide
#cgo noescape mlx_negative
#cgo nocallback mlx_negative
#cgo noescape mlx_abs
#cgo nocallback mlx_abs
#cgo noescape mlx_exp
#cgo nocallback mlx_exp
#cgo noescape mlx_log
#cgo nocallback mlx_log
#cgo noescape mlx_sqrt
#cgo nocallback mlx_sqrt
#cgo noescape mlx_rsqrt
#cgo nocallback mlx_rsqrt
#cgo noescape mlx_square
#cgo nocallback mlx_square
#cgo noescape mlx_power
#cgo nocallback mlx_power
#cgo noescape mlx_erf
#cgo nocallback mlx_erf
#cgo noescape mlx_sigmoid
#cgo nocallback mlx_sigmoid
#cgo noescape mlx_tanh
#cgo nocallback mlx_tanh
#cgo noescape mlx_sin
#cgo nocallback mlx_sin
#cgo noescape mlx_cos
#cgo nocallback mlx_cos
#cgo noescape mlx_maximum
#cgo nocallback mlx_maximum
#cgo noescape mlx_minimum
#cgo nocallback mlx_minimum
#cgo noescape mlx_clip
#cgo nocallback mlx_clip
#cgo noescape mlx_sum
#cgo nocallback mlx_sum
#cgo noescape mlx_sum_axis
#cgo nocallback mlx_sum_axis
#cgo noescape mlx_mean
#cgo nocallback mlx_mean
#cgo noescape mlx_mean_axis
#cgo nocallback mlx_mean_axis
#cgo noescape mlx_var_axis
#cgo nocallback mlx_var_axis
#cgo noescape mlx_argmax
#cgo nocallback mlx_argmax
#cgo noescape mlx_argmax_axis
#cgo nocallback mlx_argmax_axis
#cgo noescape mlx_softmax_axis
#cgo nocallback mlx_softmax_axis
#cgo noescape mlx_cumsum
#cgo nocallback mlx_cumsum
#cgo noescape mlx_matmul
#cgo nocallback mlx_matmul
#cgo noescape mlx_addmm
#cgo nocallback mlx_addmm
#cgo noescape mlx_gather_mm
#cgo nocallback mlx_gather_mm
#cgo noescape mlx_gather_qmm
#cgo nocallback mlx_gather_qmm
#cgo noescape mlx_reshape
#cgo nocallback mlx_reshape
#cgo noescape mlx_transpose_axes
#cgo nocallback mlx_transpose_axes
#cgo noescape mlx_expand_dims
#cgo nocallback mlx_expand_dims
#cgo noescape mlx_squeeze_axis
#cgo nocallback mlx_squeeze_axis
#cgo noescape mlx_flatten
#cgo nocallback mlx_flatten
#cgo noescape mlx_concatenate_axis
#cgo nocallback mlx_concatenate_axis
#cgo noescape mlx_slice
#cgo nocallback mlx_slice
#cgo noescape mlx_slice_update
#cgo nocallback mlx_slice_update
#cgo noescape mlx_as_strided
#cgo nocallback mlx_as_strided
#cgo noescape mlx_view
#cgo nocallback mlx_view
#cgo noescape mlx_contiguous
#cgo nocallback mlx_contiguous
#cgo noescape mlx_pad
#cgo nocallback mlx_pad
#cgo noescape mlx_tile
#cgo nocallback mlx_tile
#cgo noescape mlx_take_axis
#cgo nocallback mlx_take_axis
#cgo noescape mlx_take_along_axis
#cgo nocallback mlx_take_along_axis
#cgo noescape mlx_put_along_axis
#cgo nocallback mlx_put_along_axis
#cgo noescape mlx_where
#cgo nocallback mlx_where
#cgo noescape mlx_argsort_axis
#cgo nocallback mlx_argsort_axis
#cgo noescape mlx_argpartition_axis
#cgo nocallback mlx_argpartition_axis
#cgo noescape mlx_topk_axis
#cgo nocallback mlx_topk_axis
#cgo noescape mlx_less
#cgo nocallback mlx_less
#cgo noescape mlx_greater_equal
#cgo nocallback mlx_greater_equal
#cgo noescape mlx_logical_and
#cgo nocallback mlx_logical_and
#cgo noescape mlx_zeros
#cgo nocallback mlx_zeros
#cgo noescape mlx_zeros_like
#cgo nocallback mlx_zeros_like
#cgo noescape mlx_ones
#cgo nocallback mlx_ones
#cgo noescape mlx_full
#cgo nocallback mlx_full
#cgo noescape mlx_arange
#cgo nocallback mlx_arange
#cgo noescape mlx_linspace
#cgo nocallback mlx_linspace
#cgo noescape mlx_tri
#cgo nocallback mlx_tri
#cgo noescape mlx_astype
#cgo nocallback mlx_astype
#cgo noescape mlx_fast_rms_norm
#cgo nocallback mlx_fast_rms_norm
#cgo noescape mlx_fast_rope
#cgo nocallback mlx_fast_rope
#cgo noescape mlx_fast_scaled_dot_product_attention
#cgo nocallback mlx_fast_scaled_dot_product_attention
#cgo noescape mlx_conv2d
#cgo nocallback mlx_conv2d
#cgo noescape mlx_conv3d
#cgo nocallback mlx_conv3d
#cgo noescape mlx_random_key
#cgo nocallback mlx_random_key
#cgo noescape mlx_random_split
#cgo nocallback mlx_random_split
#cgo noescape mlx_random_categorical_num_samples
#cgo nocallback mlx_random_categorical_num_samples
#cgo noescape mlx_random_normal
#cgo nocallback mlx_random_normal
#cgo noescape mlx_random_uniform
#cgo nocallback mlx_random_uniform
#cgo noescape mlx_array_eval
#cgo nocallback mlx_array_eval
#cgo noescape mlx_eval
#cgo nocallback mlx_eval
#cgo noescape mlx_async_eval
#cgo nocallback mlx_async_eval
#cgo noescape mlx_synchronize
#cgo nocallback mlx_synchronize
#cgo noescape mlx_array_new
#cgo nocallback mlx_array_new
#cgo noescape mlx_array_new_data
#cgo nocallback mlx_array_new_data
#cgo noescape mlx_array_new_float
#cgo nocallback mlx_array_new_float
#cgo noescape mlx_array_free
#cgo nocallback mlx_array_free
#cgo noescape mlx_array_size
#cgo nocallback mlx_array_size
#cgo noescape mlx_array_ndim
#cgo nocallback mlx_array_ndim
#cgo noescape mlx_array_dim
#cgo nocallback mlx_array_dim
#cgo noescape mlx_array_dtype
#cgo nocallback mlx_array_dtype
#cgo noescape mlx_array_item_int32
#cgo nocallback mlx_array_item_int32
#cgo noescape mlx_vector_array_new_data
#cgo nocallback mlx_vector_array_new_data
#cgo noescape mlx_vector_array_free
#cgo nocallback mlx_vector_array_free
#cgo noescape mlx_array_new_int
#cgo nocallback mlx_array_new_int
#cgo noescape mlx_stream_new_device
#cgo nocallback mlx_stream_new_device
#cgo noescape mlx_get_default_stream
#cgo nocallback mlx_get_default_stream
#cgo noescape mlx_set_default_stream
#cgo nocallback mlx_set_default_stream
*/
import "C"
import (
	"fmt"
	"reflect"
	"runtime"
	"sync"
	"sync/atomic"
	"time"
	"unsafe"
)

// Dtype represents MLX data types
type Dtype int

const (
	DtypeBool      Dtype = C.MLX_BOOL
	DtypeUint8     Dtype = C.MLX_UINT8
	DtypeUint16    Dtype = C.MLX_UINT16
	DtypeUint32    Dtype = C.MLX_UINT32
	DtypeUint64    Dtype = C.MLX_UINT64
	DtypeInt8      Dtype = C.MLX_INT8
	DtypeInt16     Dtype = C.MLX_INT16
	DtypeInt32     Dtype = C.MLX_INT32
	DtypeInt64     Dtype = C.MLX_INT64
	DtypeFloat16   Dtype = C.MLX_FLOAT16
	DtypeFloat32   Dtype = C.MLX_FLOAT32
	DtypeFloat64   Dtype = C.MLX_FLOAT64
	DtypeBFloat16  Dtype = C.MLX_BFLOAT16
	DtypeComplex64 Dtype = C.MLX_COMPLEX64
)

// String implements fmt.Stringer for Dtype
func (d Dtype) String() string {
	switch d {
	case DtypeBool:
		return "bool"
	case DtypeUint8:
		return "u8"
	case DtypeUint16:
		return "u16"
	case DtypeUint32:
		return "u32"
	case DtypeUint64:
		return "u64"
	case DtypeInt8:
		return "i8"
	case DtypeInt16:
		return "i16"
	case DtypeInt32:
		return "i32"
	case DtypeInt64:
		return "i64"
	case DtypeFloat16:
		return "f16"
	case DtypeFloat32:
		return "f32"
	case DtypeFloat64:
		return "f64"
	case DtypeBFloat16:
		return "bf16"
	case DtypeComplex64:
		return "c64"
	default:
		return "unknown"
	}
}

// Memory Management:
//
// All arrays are automatically tracked for cleanup. On Eval(), non-kept arrays are freed.
//
//	x := mlx.Matmul(input, weight)   // x is tracked for cleanup
//	mlx.Keep(x)                       // mark x as persistent
//	mlx.Eval(x)                       // eval + free non-kept arrays
//
// Use Keep() for arrays that should persist (weights, caches).
// Use Free() to mark a kept array for cleanup on next Eval().
//
// Note: Not goroutine-safe. Use from a single goroutine.

// Array wraps an MLX array handle.
// Arrays are freed via Eval() cleanup (deterministic) or GC (fallback).
type Array struct {
	c     C.mlx_array
	freed bool // Prevents double-free
	kept  bool // If true, survives Eval() cleanup
}

// arrays tracks all live arrays. On Eval(), non-kept arrays are freed.
// Not goroutine-safe.
var arrays = make([]*Array, 0, 4096)

// evalHandles is a pre-allocated slice for passing arrays to MLX eval.
var evalHandles = make([]C.mlx_array, 0, 64)

// arrayPool reduces allocations for intermediate arrays
var arrayPool = sync.Pool{
	New: func() any { return &Array{} },
}

func newArray(array C.mlx_array) *Array {
	// In compiled closures, MLX manages memory - skip Go tracking
	if InClosureCallback() {
		return &Array{c: array}
	}

	// Use pooled Array struct for efficiency
	a := arrayPool.Get().(*Array)
	a.c = array
	a.freed = false
	a.kept = false

	// Track in global list
	arrays = append(arrays, a)

	return a
}

// Collect uses reflection to find all *Array fields in a struct (recursively).
// Use this to automatically gather model weights, cache state, etc.
func Collect(v any) []*Array {
	var arrays []*Array
	seen := make(map[uintptr]bool)
	collect(reflect.ValueOf(v), &arrays, seen)
	return arrays
}

func collect(v reflect.Value, arrays *[]*Array, seen map[uintptr]bool) {
	if !v.IsValid() {
		return
	}

	// Handle pointers
	if v.Kind() == reflect.Ptr {
		if v.IsNil() {
			return
		}
		// Avoid infinite loops
		ptr := v.Pointer()
		if seen[ptr] {
			return
		}
		seen[ptr] = true

		// Check if it's *Array
		if arr, ok := v.Interface().(*Array); ok {
			if arr != nil && arr.c.ctx != nil {
				*arrays = append(*arrays, arr)
			}
			return
		}
		collect(v.Elem(), arrays, seen)
		return
	}

	// Handle structs
	if v.Kind() == reflect.Struct {
		for i := 0; i < v.NumField(); i++ {
			field := v.Field(i)
			if field.CanInterface() {
				collect(field, arrays, seen)
			}
		}
		return
	}

	// Handle slices
	if v.Kind() == reflect.Slice {
		for i := 0; i < v.Len(); i++ {
			collect(v.Index(i), arrays, seen)
		}
		return
	}

	// Handle maps
	if v.Kind() == reflect.Map {
		for _, key := range v.MapKeys() {
			collect(v.MapIndex(key), arrays, seen)
		}
		return
	}

	// Handle interfaces
	if v.Kind() == reflect.Interface {
		if !v.IsNil() {
			collect(v.Elem(), arrays, seen)
		}
		return
	}
}

// FreeStruct releases all *Array fields in a struct (recursively).
// Use this to free model weights when unloading a model.
func FreeStruct(v any) {
	for _, arr := range Collect(v) {
		arr.Free()
	}
}

// Keep marks arrays to persist across Eval() cleanup.
// Kept arrays will NOT be freed when Eval() runs cleanup.
func Keep(arrays ...*Array) {
	for _, a := range arrays {
		if a != nil {
			a.kept = true
		}
	}
}

// cleanup frees non-kept arrays and compacts the live array list.
// Returns number of arrays freed.
func cleanup() int {
	freed := 0
	n := 0
	for _, a := range arrays {
		if a.kept {
			arrays[n] = a
			n++
		} else if a.c.ctx != nil && !a.freed {
			C.mlx_array_free(a.c)
			a.c.ctx = nil
			arrayPool.Put(a)
			freed++
		}
	}
	arrays = arrays[:n]
	return freed
}

// DebugArrays prints summary info about all tracked arrays.
func DebugArrays() {
	var totalBytes int64
	var keptCount, unkeptCount int
	for _, a := range arrays {
		if a.kept {
			keptCount++
		} else {
			unkeptCount++
		}
		totalBytes += a.Nbytes()
	}
	fmt.Printf("[DEBUG] Arrays: %d kept, %d unkept, %.2f GB total\n",
		keptCount, unkeptCount, float64(totalBytes)/(1024*1024*1024))
}

// DebugArraysVerbose prints detailed info about all tracked arrays, sorted by size.
func DebugArraysVerbose(topN int) {
	type arrayInfo struct {
		shape []int32
		dtype Dtype
		bytes int64
		kept  bool
	}

	var infos []arrayInfo
	var totalBytes int64
	for _, a := range arrays {
		bytes := a.Nbytes()
		infos = append(infos, arrayInfo{
			shape: a.Shape(),
			dtype: a.Dtype(),
			bytes: bytes,
			kept:  a.kept,
		})
		totalBytes += bytes
	}

	// Sort by size descending
	for i := 0; i < len(infos)-1; i++ {
		for j := i + 1; j < len(infos); j++ {
			if infos[j].bytes > infos[i].bytes {
				infos[i], infos[j] = infos[j], infos[i]
			}
		}
	}

	fmt.Printf("[DEBUG] %d arrays, %.2f GB total:\n", len(infos), float64(totalBytes)/(1024*1024*1024))
	for i, info := range infos {
		if i >= topN {
			break
		}
		keptStr := ""
		if info.kept {
			keptStr = " [kept]"
		}
		fmt.Printf("  %3d. %8.2f MB  %v %v%s\n",
			i+1, float64(info.bytes)/(1024*1024), info.shape, info.dtype, keptStr)
	}
}

// Eval synchronously evaluates arrays and cleans up non-kept arrays.
// Outputs are automatically kept (survive cleanup). Returns them for chaining.
func Eval(outputs ...*Array) []*Array {
	// Keep outputs so cleanup doesn't free them
	for _, o := range outputs {
		if o != nil {
			o.kept = true
		}
	}

	// Cleanup non-kept arrays
	cleanup()

	// Then evaluate
	if len(outputs) > 0 {
		evalHandles = evalHandles[:0]
		for _, o := range outputs {
			if o != nil {
				evalHandles = append(evalHandles, o.c)
			}
		}
		if len(evalHandles) > 0 {
			vec := C.mlx_vector_array_new_data(&evalHandles[0], C.size_t(len(evalHandles)))
			C.mlx_eval(vec)
			C.mlx_vector_array_free(vec)
		}
	}
	return outputs
}

// AsyncEval dispatches async evaluation and cleans up non-kept arrays.
// Outputs are automatically kept (survive cleanup).
func AsyncEval(outputs ...*Array) {
	// Keep outputs so cleanup doesn't free them
	for _, o := range outputs {
		if o != nil {
			o.kept = true
		}
	}

	// Cleanup non-kept arrays
	cleanup()

	// Then dispatch async eval
	if len(outputs) > 0 {
		evalHandles = evalHandles[:0]
		for _, o := range outputs {
			if o != nil {
				evalHandles = append(evalHandles, o.c)
			}
		}
		if len(evalHandles) > 0 {
			vec := C.mlx_vector_array_new_data(&evalHandles[0], C.size_t(len(evalHandles)))
			C.mlx_async_eval(vec)
			C.mlx_vector_array_free(vec)
		}
	}
}

// Sync waits for all async operations to complete (no cleanup).
func Sync() {
	C.mlx_synchronize(C.default_stream())
}

// Free marks this array for cleanup on the next Eval().
// The array is not immediately freed - cleanup happens during Eval().
//
// Pattern for loops:
//
//	oldLatents.Free()     // mark for cleanup
//	mlx.Eval(newLatents)  // frees old, evals new
func (a *Array) Free() {
	if a != nil {
		a.kept = false
	}
}

// Eval evaluates this single array and runs cleanup.
func (a *Array) Eval() *Array {
	Eval(a)
	return a
}

// Valid returns true if the array hasn't been freed.
func (a *Array) Valid() bool {
	return a != nil && a.c.ctx != nil
}

func int32ToCInt(s []int32) *C.int {
	if len(s) == 0 {
		return nil
	}
	return (*C.int)(unsafe.Pointer(&s[0]))
}

// NewArray creates a new MLX array from float32 data
func NewArray(data []float32, shape []int32) *Array {
	handle := C.mlx_array_new_data(
		unsafe.Pointer(&data[0]),
		int32ToCInt(shape),
		C.int(len(shape)),
		C.MLX_FLOAT32,
	)
	return newArray(handle)
}

// NewArrayInt32 creates a new MLX array from int32 data
func NewArrayInt32(data []int32, shape []int32) *Array {
	handle := C.mlx_array_new_data(
		unsafe.Pointer(&data[0]),
		int32ToCInt(shape),
		C.int(len(shape)),
		C.MLX_INT32,
	)
	return newArray(handle)
}

// NewArrayFloat32 creates a new float32 array from data
func NewArrayFloat32(data []float32, shape []int32) *Array {
	return NewArray(data, shape)
}

// Zeros creates an array of zeros with optional dtype (default float32)
func Zeros(shape []int32, dtype ...Dtype) *Array {
	res := C.mlx_array_new()
	dt := DtypeFloat32
	if len(dtype) > 0 {
		dt = dtype[0]
	}
	C.mlx_zeros(&res, int32ToCInt(shape), C.size_t(len(shape)), C.mlx_dtype(dt), C.default_stream())
	return newArray(res)
}

// ZerosLike creates a zeros array with the same dtype as a.
// If shape is provided, uses that shape; otherwise uses a's shape.
func ZerosLike(a *Array, shape ...int32) *Array {
	res := C.mlx_array_new()
	if len(shape) == 0 {
		C.mlx_zeros_like(&res, a.c, C.default_stream())
	} else {
		dtype := a.Dtype()
		C.mlx_zeros(&res, int32ToCInt(shape), C.size_t(len(shape)), C.mlx_dtype(dtype), C.default_stream())
	}
	return newArray(res)
}

// Ones creates an array of ones
func Ones(shape ...int32) *Array {
	res := C.mlx_array_new()
	C.mlx_ones(&res, int32ToCInt(shape), C.size_t(len(shape)), C.MLX_FLOAT32, C.default_stream())
	return newArray(res)
}

// Full creates an array filled with a value
func Full(value float32, shape ...int32) *Array {
	vals := C.mlx_array_new_float(C.float(value))
	res := C.mlx_array_new()
	C.mlx_full(&res, int32ToCInt(shape), C.size_t(len(shape)), vals, C.MLX_FLOAT32, C.default_stream())
	C.mlx_array_free(vals)
	return newArray(res)
}

// Arange creates a range of values
func Arange(start, stop, step float32) *Array {
	res := C.mlx_array_new()
	C.mlx_arange(&res, C.double(start), C.double(stop), C.double(step), C.MLX_FLOAT32, C.default_stream())
	return newArray(res)
}

// Linspace creates evenly spaced values
func Linspace(start, stop float32, steps int32) *Array {
	res := C.mlx_array_new()
	C.mlx_linspace(&res, C.double(start), C.double(stop), C.int(steps), C.MLX_FLOAT32, C.default_stream())
	return newArray(res)
}

// ============ Math Operations ============

// Add adds two arrays element-wise
func Add(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_add(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// AddRaw is like Add - kept for API compatibility (now identical to Add)
func AddRaw(a, b *Array) *Array {
	return Add(a, b)
}

// Sub subtracts two arrays element-wise
func Sub(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_subtract(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// Mul multiplies two arrays element-wise
func Mul(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_multiply(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// Div divides two arrays element-wise
func Div(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_divide(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// Matmul performs matrix multiplication
func Matmul(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_matmul(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// AddMM computes: result = beta*c + alpha*(a @ b)
// This fuses bias addition with matmul into a single op.
func AddMM(c, a, b *Array, alpha, beta float32) *Array {
	res := C.mlx_array_new()
	C.mlx_addmm(&res, c.c, a.c, b.c, C.float(alpha), C.float(beta), C.default_stream())
	return newArray(res)
}

// Linear performs matrix multiplication: a @ weight
func Linear(a, weight *Array) *Array {
	return Matmul(a, weight)
}

// Sqrt computes element-wise square root
func Sqrt(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_sqrt(&res, a.c, C.default_stream())
	return newArray(res)
}

// RSqrt computes element-wise reciprocal square root
func RSqrt(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_rsqrt(&res, a.c, C.default_stream())
	return newArray(res)
}

// Erf computes element-wise error function
func Erf(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_erf(&res, a.c, C.default_stream())
	return newArray(res)
}

// Exp computes element-wise exponential
func Exp(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_exp(&res, a.c, C.default_stream())
	return newArray(res)
}

// Log computes element-wise natural logarithm
func Log(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_log(&res, a.c, C.default_stream())
	return newArray(res)
}

// Sin computes element-wise sine
func Sin(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_sin(&res, a.c, C.default_stream())
	return newArray(res)
}

// Cos computes element-wise cosine
func Cos(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_cos(&res, a.c, C.default_stream())
	return newArray(res)
}

// Neg negates the array
func Neg(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_negative(&res, a.c, C.default_stream())
	return newArray(res)
}

// Abs computes element-wise absolute value
func Abs(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_abs(&res, a.c, C.default_stream())
	return newArray(res)
}

// Square computes element-wise square
func Square(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_square(&res, a.c, C.default_stream())
	return newArray(res)
}

// Pow raises a to the power of b element-wise
func Pow(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_power(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// Max computes element-wise maximum
func Max(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_maximum(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// Min computes element-wise minimum
func Min(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_minimum(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// scalarWithDtype creates a scalar array matching the dtype of a (critical for graph fusion!)
func scalarWithDtype(s float32, a *Array) C.mlx_array {
	// Create float32 scalar, then cast to match input dtype
	f32 := C.mlx_array_new_float(C.float(s))
	dtype := a.Dtype()
	if dtype == DtypeFloat32 {
		return f32 // No cast needed
	}
	// Cast to match input dtype
	casted := C.mlx_array_new()
	C.mlx_astype(&casted, f32, C.mlx_dtype(dtype), C.default_stream())
	C.mlx_array_free(f32)
	return casted
}

// AddScalar adds a scalar to an array (matches dtype for graph fusion)
func AddScalar(a *Array, s float32) *Array {
	scalar := scalarWithDtype(s, a)
	res := C.mlx_array_new()
	C.mlx_add(&res, a.c, scalar, C.default_stream())
	C.mlx_array_free(scalar)
	return newArray(res)
}

// MulScalar multiplies an array by a scalar (matches dtype for graph fusion)
func MulScalar(a *Array, s float32) *Array {
	scalar := scalarWithDtype(s, a)
	res := C.mlx_array_new()
	C.mlx_multiply(&res, a.c, scalar, C.default_stream())
	C.mlx_array_free(scalar)
	return newArray(res)
}

// DivScalar divides an array by a scalar (matches dtype for graph fusion)
func DivScalar(a *Array, s float32) *Array {
	scalar := scalarWithDtype(s, a)
	res := C.mlx_array_new()
	C.mlx_divide(&res, a.c, scalar, C.default_stream())
	C.mlx_array_free(scalar)
	return newArray(res)
}

// DivScalarInt divides an int array by an int scalar (regular division, may return float)
func DivScalarInt(a *Array, s int32) *Array {
	scalar := C.mlx_array_new_int(C.int(s))
	res := C.mlx_array_new()
	C.mlx_divide(&res, a.c, scalar, C.default_stream())
	C.mlx_array_free(scalar)
	return newArray(res)
}

// FloorDivideScalar performs integer floor division (a // s), preserving int dtype
func FloorDivideScalar(a *Array, s int32) *Array {
	scalar := C.mlx_array_new_int(C.int(s))
	res := C.mlx_array_new()
	C.mlx_floor_divide(&res, a.c, scalar, C.default_stream())
	C.mlx_array_free(scalar)
	return newArray(res)
}

// ============ Reduction Operations ============

// Sum reduces along an axis
func Sum(a *Array, axis int, keepdims bool) *Array {
	res := C.mlx_array_new()
	C.mlx_sum_axis(&res, a.c, C.int(axis), C._Bool(keepdims), C.default_stream())
	return newArray(res)
}

// SumAll reduces the entire array to a scalar
func SumAll(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_sum(&res, a.c, false, C.default_stream())
	return newArray(res)
}

// Mean reduces along an axis
func Mean(a *Array, axis int, keepdims bool) *Array {
	res := C.mlx_array_new()
	C.mlx_mean_axis(&res, a.c, C.int(axis), C._Bool(keepdims), C.default_stream())
	return newArray(res)
}

// MeanAll reduces the entire array to a scalar
func MeanAll(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_mean(&res, a.c, false, C.default_stream())
	return newArray(res)
}

// Var computes variance along an axis
func Var(a *Array, axis int, keepdims bool) *Array {
	res := C.mlx_array_new()
	C.mlx_var_axis(&res, a.c, C.int(axis), C._Bool(keepdims), 0, C.default_stream())
	return newArray(res)
}

// Argmax returns indices of maximum values along an axis
func Argmax(a *Array, axis int, keepdims bool) *Array {
	res := C.mlx_array_new()
	C.mlx_argmax_axis(&res, a.c, C.int(axis), C._Bool(keepdims), C.default_stream())
	return newArray(res)
}

// ArgmaxAll returns the index of the maximum element (flattened).
// Triggers cleanup of non-kept arrays.
func ArgmaxAll(a *Array) int32 {
	cleanup()
	// Flatten, then argmax with keepdims=false
	flat := C.mlx_array_new()
	C.mlx_flatten(&flat, a.c, 0, -1, C.default_stream())
	res := C.mlx_array_new()
	C.mlx_argmax(&res, flat, false, C.default_stream())
	C.mlx_array_eval(res)
	var val C.int32_t
	C.mlx_array_item_int32(&val, res)
	C.mlx_array_free(flat)
	C.mlx_array_free(res)
	return int32(val)
}

// Reshape reshapes the array
func Reshape(a *Array, shape ...int32) *Array {
	res := C.mlx_array_new()
	C.mlx_reshape(&res, a.c, int32ToCInt(shape), C.size_t(len(shape)), C.default_stream())
	return newArray(res)
}

// Transpose permutes the dimensions
func Transpose(a *Array, axes ...int) *Array {
	cAxes := make([]C.int, len(axes))
	for i, ax := range axes {
		cAxes[i] = C.int(ax)
	}
	res := C.mlx_array_new()
	C.mlx_transpose_axes(&res, a.c, &cAxes[0], C.size_t(len(axes)), C.default_stream())
	return newArray(res)
}

// AsStrided creates a view with custom strides. Useful for fusing reshape+transpose.
func AsStrided(a *Array, shape []int32, strides []int64, offset int64) *Array {
	cShape := make([]C.int, len(shape))
	for i, s := range shape {
		cShape[i] = C.int(s)
	}
	cStrides := make([]C.int64_t, len(strides))
	for i, s := range strides {
		cStrides[i] = C.int64_t(s)
	}
	res := C.mlx_array_new()
	C.mlx_as_strided(&res, a.c, &cShape[0], C.size_t(len(shape)), &cStrides[0], C.size_t(len(strides)), C.size_t(offset), C.default_stream())
	return newArray(res)
}

// ExpandDims adds a dimension at the specified axis
func ExpandDims(a *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_expand_dims(&res, a.c, C.int(axis), C.default_stream())
	return newArray(res)
}

// Squeeze removes a dimension at the specified axis
func Squeeze(a *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_squeeze_axis(&res, a.c, C.int(axis), C.default_stream())
	return newArray(res)
}

// Flatten flattens the array to 1D
func Flatten(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_flatten(&res, a.c, 0, -1, C.default_stream())
	return newArray(res)
}

// FlattenRange flattens consecutive axes from start_axis to end_axis (intermediates)
func FlattenRange(a *Array, startAxis, endAxis int) *Array {
	res := C.mlx_array_new()
	C.mlx_flatten(&res, a.c, C.int(startAxis), C.int(endAxis), C.default_stream())
	return newArray(res)
}

// View reinterprets the array with a new dtype (no data copy)
func View(a *Array, dtype int) *Array {
	res := C.mlx_array_new()
	C.mlx_view(&res, a.c, C.mlx_dtype(dtype), C.default_stream())
	return newArray(res)
}

// Contiguous returns a contiguous copy of the array
func Contiguous(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_contiguous(&res, a.c, true, C.default_stream())
	return newArray(res)
}

// Clip clips values to [min, max]. Pass nil for no bound on that side.
func Clip(a *Array, aMin, aMax *Array) *Array {
	res := C.mlx_array_new()
	var minH, maxH C.mlx_array
	if aMin != nil {
		minH = aMin.c
	}
	if aMax != nil {
		maxH = aMax.c
	}
	C.mlx_clip(&res, a.c, minH, maxH, C.default_stream())
	return newArray(res)
}

// ClipScalar clips array values using scalar bounds (matches dtype for graph fusion)
// Pass math.NaN() or set hasMin/hasMax to false for unbounded
func ClipScalar(a *Array, minVal, maxVal float32, hasMin, hasMax bool) *Array {
	var minArr, maxArr C.mlx_array
	if hasMin {
		minArr = scalarWithDtype(minVal, a)
	}
	if hasMax {
		maxArr = scalarWithDtype(maxVal, a)
	}
	res := C.mlx_array_new()
	C.mlx_clip(&res, a.c, minArr, maxArr, C.default_stream())
	if hasMin {
		C.mlx_array_free(minArr)
	}
	if hasMax {
		C.mlx_array_free(maxArr)
	}
	return newArray(res)
}

// GreaterEqual returns element-wise a >= b
func GreaterEqual(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_greater_equal(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// LessArray returns element-wise a < b
func LessArray(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_less(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// LogicalAnd returns element-wise a && b
func LogicalAnd(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_logical_and(&res, a.c, b.c, C.default_stream())
	return newArray(res)
}

// AllClose returns true if all elements of a and b are within tolerance.
// Uses rtol (relative tolerance) and atol (absolute tolerance):
// |a - b| <= atol + rtol * |b|
func AllClose(a, b *Array, rtol, atol float64) *Array {
	res := C.mlx_array_new()
	C.mlx_allclose(&res, a.c, b.c, C.double(rtol), C.double(atol), C.bool(false), C.default_stream())
	return newArray(res)
}

// AllCloseEqualNaN is like AllClose but treats NaN as equal to NaN.
func AllCloseEqualNaN(a, b *Array, rtol, atol float64) *Array {
	res := C.mlx_array_new()
	C.mlx_allclose(&res, a.c, b.c, C.double(rtol), C.double(atol), C.bool(true), C.default_stream())
	return newArray(res)
}

// ArrayEqual returns true if arrays have same shape and all elements are equal.
func ArrayEqual(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_array_equal(&res, a.c, b.c, C.bool(false), C.default_stream())
	return newArray(res)
}

// ArrayEqualNaN is like ArrayEqual but treats NaN as equal to NaN.
func ArrayEqualNaN(a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_array_equal(&res, a.c, b.c, C.bool(true), C.default_stream())
	return newArray(res)
}

// IsClose returns element-wise bool array indicating if values are within tolerance.
// |a - b| <= atol + rtol * |b|
func IsClose(a, b *Array, rtol, atol float64) *Array {
	res := C.mlx_array_new()
	C.mlx_isclose(&res, a.c, b.c, C.double(rtol), C.double(atol), C.bool(false), C.default_stream())
	return newArray(res)
}

// IsCloseEqualNaN is like IsClose but treats NaN as equal to NaN.
func IsCloseEqualNaN(a, b *Array, rtol, atol float64) *Array {
	res := C.mlx_array_new()
	C.mlx_isclose(&res, a.c, b.c, C.double(rtol), C.double(atol), C.bool(true), C.default_stream())
	return newArray(res)
}

// ReduceMax reduces array to max value over all dimensions.
func ReduceMax(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_max(&res, a.c, C.bool(false), C.default_stream())
	return newArray(res)
}

// ArangeInt creates an array with values from start to stop with step and specified dtype
func ArangeInt(start, stop, step int32, dtype Dtype) *Array {
	res := C.mlx_array_new()
	C.mlx_arange(&res, C.double(start), C.double(stop), C.double(step), C.mlx_dtype(dtype), C.default_stream())
	return newArray(res)
}

// Concatenate concatenates arrays along an axis
func Concatenate(arrays []*Array, axis int) *Array {
	handles := make([]C.mlx_array, len(arrays))
	for i, arr := range arrays {
		handles[i] = arr.c
	}
	vec := C.mlx_vector_array_new_data(&handles[0], C.size_t(len(handles)))
	res := C.mlx_array_new()
	C.mlx_concatenate_axis(&res, vec, C.int(axis), C.default_stream())
	C.mlx_vector_array_free(vec)
	return newArray(res)
}

// Concat is a convenience function to concatenate two arrays
func Concat(a, b *Array, axis int) *Array {
	return Concatenate([]*Array{a, b}, axis)
}

// Slice slices the array
func Slice(a *Array, start, stop []int32) *Array {
	n := len(start)
	cStart := make([]C.int, n)
	cStop := make([]C.int, n)
	cStrides := make([]C.int, n)
	for i := 0; i < n; i++ {
		cStart[i] = C.int(start[i])
		cStop[i] = C.int(stop[i])
		cStrides[i] = 1 // Default stride of 1
	}
	res := C.mlx_array_new()
	C.mlx_slice(&res, a.c, &cStart[0], C.size_t(n), &cStop[0], C.size_t(n), &cStrides[0], C.size_t(n), C.default_stream())
	return newArray(res)
}

// SliceStride slices with start:stop:stride like Python a[start:stop:stride]
func SliceStride(a *Array, start, stop, strides []int32) *Array {
	cStart := make([]C.int, len(start))
	cStop := make([]C.int, len(stop))
	cStrides := make([]C.int, len(strides))
	for i := range start {
		cStart[i] = C.int(start[i])
		cStop[i] = C.int(stop[i])
		cStrides[i] = C.int(strides[i])
	}
	res := C.mlx_array_new()
	C.mlx_slice(&res, a.c, &cStart[0], C.size_t(len(start)), &cStop[0], C.size_t(len(stop)), &cStrides[0], C.size_t(len(strides)), C.default_stream())
	return newArray(res)
}

// Tile repeats the array along each dimension
func Tile(a *Array, reps []int32) *Array {
	res := C.mlx_array_new()
	C.mlx_tile(&res, a.c, int32ToCInt(reps), C.size_t(len(reps)), C.default_stream())
	return newArray(res)
}

// BroadcastTo broadcasts an array to a given shape
func BroadcastTo(a *Array, shape []int32) *Array {
	res := C.mlx_array_new()
	C.mlx_broadcast_to(&res, a.c, int32ToCInt(shape), C.size_t(len(shape)), C.default_stream())
	return newArray(res)
}

// ============ Neural Network Operations ============

// Softmax computes softmax along an axis
func Softmax(a *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_softmax_axis(&res, a.c, C.int(axis), false, C.default_stream())
	return newArray(res)
}

// Take gathers elements along an axis using indices
func Take(a *Array, indices *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_take_axis(&res, a.c, indices.c, C.int(axis), C.default_stream())
	return newArray(res)
}

// Argsort returns indices that would sort the array along an axis
func Argsort(a *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_argsort_axis(&res, a.c, C.int(axis), C.default_stream())
	return newArray(res)
}

// Sigmoid computes element-wise sigmoid
func Sigmoid(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_sigmoid(&res, a.c, C.default_stream())
	return newArray(res)
}

// ReLU computes element-wise ReLU: max(0, x)
func ReLU(a *Array) *Array {
	// ReLU = maximum(x, 0) - mlx-c doesn't have mlx_relu, but we can use maximum
	zero := C.mlx_array_new_float(0.0)
	res := C.mlx_array_new()
	C.mlx_maximum(&res, a.c, zero, C.default_stream())
	C.mlx_array_free(zero)
	return newArray(res)
}

// SiLU computes element-wise SiLU (Swish): x * sigmoid(x)
func SiLU(a *Array) *Array {
	// SiLU = x * sigmoid(x)
	sig := C.mlx_array_new()
	C.mlx_sigmoid(&sig, a.c, C.default_stream())
	res := C.mlx_array_new()
	C.mlx_multiply(&res, a.c, sig, C.default_stream())
	C.mlx_array_free(sig)
	return newArray(res)
}

// GELU computes element-wise GELU (Gaussian Error Linear Unit)
// GELU(x) = x * 0.5 * (1 + erf(x / sqrt(2)))
func GELU(a *Array) *Array {
	sqrt2 := C.mlx_array_new_float(1.4142135623730951)
	scaled := C.mlx_array_new()
	C.mlx_divide(&scaled, a.c, sqrt2, C.default_stream())
	erfd := C.mlx_array_new()
	C.mlx_erf(&erfd, scaled, C.default_stream())
	one := C.mlx_array_new_float(1.0)
	erfdPlusOne := C.mlx_array_new()
	C.mlx_add(&erfdPlusOne, erfd, one, C.default_stream())
	half := C.mlx_array_new_float(0.5)
	halfErfdPlusOne := C.mlx_array_new()
	C.mlx_multiply(&halfErfdPlusOne, half, erfdPlusOne, C.default_stream())
	res := C.mlx_array_new()
	C.mlx_multiply(&res, a.c, halfErfdPlusOne, C.default_stream())
	C.mlx_array_free(sqrt2)
	C.mlx_array_free(scaled)
	C.mlx_array_free(erfd)
	C.mlx_array_free(one)
	C.mlx_array_free(erfdPlusOne)
	C.mlx_array_free(half)
	C.mlx_array_free(halfErfdPlusOne)
	return newArray(res)
}

// Tanh computes element-wise tanh
func Tanh(a *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_tanh(&res, a.c, C.default_stream())
	return newArray(res)
}

// RMSNorm computes RMS normalization using mlx.fast
func RMSNorm(x, weight *Array, eps float32) *Array {
	res := C.mlx_array_new()
	C.mlx_fast_rms_norm(&res, x.c, weight.c, C.float(eps), C.default_stream())
	return newArray(res)
}

// RMSNormNoWeight applies RMS normalization without a weight
// x * rsqrt(mean(x^2) + eps)
// Uses mlx_fast_rms_norm with ones weight for f32 accumulation precision
func RMSNormNoWeight(x *Array, eps float32) *Array {
	// Create weight of ones matching last dimension
	lastDim := x.Shape()[len(x.Shape())-1]
	ones := AsType(Full(1.0, lastDim), x.Dtype())
	return RMSNorm(x, ones, eps)
}

// RoPE applies rotary position embeddings using mlx.fast
func RoPE(x *Array, dims int, traditional bool, base, scale float32, offset int) *Array {
	res := C.mlx_array_new()
	optBase := C.mlx_optional_float{value: C.float(base), has_value: true}
	C.mlx_fast_rope(&res, x.c, C.int(dims), C._Bool(traditional), optBase, C.float(scale), C.int(offset), C.mlx_array{}, C.default_stream())
	return newArray(res)
}

// RoPEWithFreqs applies rotary position embeddings with custom frequencies (for YaRN)
// freqs is required - use RoPE() if you don't have custom frequencies
func RoPEWithFreqs(x, freqs *Array, dims int, traditional bool, scale float32, offset int) *Array {
	res := C.mlx_array_new()
	optBase := C.mlx_optional_float{has_value: false} // No base when using freqs
	C.mlx_fast_rope(&res, x.c, C.int(dims), C._Bool(traditional), optBase, C.float(scale), C.int(offset), freqs.c, C.default_stream())
	return newArray(res)
}

// ============ Indexing ============

// EmbeddingLookup performs embedding lookup (gathers from table)
// table: [vocab_size, hidden_size], indices: [batch, seq_len]
// returns: [batch, seq_len, hidden_size]
func EmbeddingLookup(table, indices *Array) *Array {
	return Take(table, indices, 0)
}

// Gather gathers elements using indices - simplified to use take axis 0
func Gather(a, indices *Array) *Array {
	return Take(a, indices, 0)
}

// ============ Array Properties ============

// Ndim returns the number of dimensions
func (a *Array) Ndim() int {
	return int(C.mlx_array_ndim(a.c))
}

// Size returns the total number of elements
func (a *Array) Size() int {
	return int(C.mlx_array_size(a.c))
}

// IsContiguous returns whether the array's data is contiguous in memory.
// Non-contiguous arrays (e.g., from SliceStride) must call Contiguous() before Data().
func (a *Array) IsContiguous() bool {
	var res C.bool
	C._mlx_array_is_contiguous(&res, a.c)
	return bool(res)
}

// Dim returns the size of a dimension
func (a *Array) Dim(axis int) int32 {
	return int32(C.mlx_array_dim(a.c, C.int(axis)))
}

// Shape returns the shape as a slice
func (a *Array) Shape() []int32 {
	ndim := a.Ndim()
	shape := make([]int32, ndim)
	for i := 0; i < ndim; i++ {
		shape[i] = a.Dim(i)
	}
	return shape
}

// IsValid returns true if the array hasn't been freed
func (a *Array) IsValid() bool {
	return a != nil && a.c.ctx != nil
}

// Dtype returns the data type
func (a *Array) Dtype() Dtype {
	return Dtype(C.mlx_array_dtype(a.c))
}

// Nbytes returns the total size in bytes
func (a *Array) Nbytes() int64 {
	return int64(a.Size()) * a.Dtype().ItemSize()
}

// ItemSize returns the size in bytes of one element for this dtype
func (d Dtype) ItemSize() int64 {
	switch d {
	case DtypeBool, DtypeUint8, DtypeInt8:
		return 1
	case DtypeUint16, DtypeInt16, DtypeFloat16, DtypeBFloat16:
		return 2
	case DtypeUint32, DtypeInt32, DtypeFloat32:
		return 4
	case DtypeUint64, DtypeInt64, DtypeFloat64, DtypeComplex64:
		return 8
	default:
		return 4
	}
}

// ============ Data Access ============

// Data copies the float32 data out of the array.
// Note: For non-contiguous arrays (e.g., from SliceStride), call Contiguous() first.
// Note: Arrays of other dtypes (bf16, f16, etc) are automatically converted to float32.
// Note: Triggers cleanup of non-kept arrays.
func (a *Array) Data() []float32 {
	cleanup()
	size := a.Size()
	if size == 0 {
		return nil
	}

	arr := a
	if a.Dtype() != DtypeFloat32 {
		arr = AsType(a, DtypeFloat32)
		arr.Eval()
		// Cast array will be cleaned up on next Eval
	}

	ptr := C.mlx_array_data_float32(arr.c)
	if ptr == nil {
		return nil
	}
	data := make([]float32, size)
	copy(data, unsafe.Slice((*float32)(unsafe.Pointer(ptr)), size))
	return data
}

// Item returns the scalar value from a 0-dimensional array.
// Converts to float32 if necessary. Triggers cleanup.
func (a *Array) Item() float32 {
	data := a.Data() // Data() calls cleanup()
	if len(data) == 0 {
		return 0
	}
	return data[0]
}

// DataInt32 copies the int32 data out of the array.
// Note: For non-contiguous arrays (e.g., from SliceStride), call Contiguous() first.
// Note: Triggers cleanup of non-kept arrays.
func (a *Array) DataInt32() []int32 {
	cleanup()
	size := a.Size()
	if size == 0 {
		return nil
	}
	ptr := C.mlx_array_data_int32(a.c)
	if ptr == nil {
		return nil
	}
	data := make([]int32, size)
	copy(data, unsafe.Slice((*int32)(unsafe.Pointer(ptr)), size))
	return data
}

// ItemInt32 gets a single scalar value efficiently (no array copy).
// Note: Triggers cleanup of non-kept arrays.
func (a *Array) ItemInt32() int32 {
	cleanup()
	var val C.int32_t
	C.mlx_array_item_int32(&val, a.c)
	return int32(val)
}

// ============ Utility ============

// String returns a string representation
func (a *Array) String() string {
	shape := a.Shape()
	size := a.Size()
	if size <= 20 {
		data := a.Data()
		return fmt.Sprintf("Array(shape=%v, data=%v)", shape, data)
	}
	return fmt.Sprintf("Array(shape=%v, size=%d)", shape, size)
}

// ============ Safetensors Support ============

// NewArrayFromBytes creates an array from raw bytes (for safetensors)
func NewArrayFromBytes(data []byte, shape []int32, dtype Dtype) *Array {
	cData := unsafe.Pointer(&data[0])
	intShape := make([]C.int, len(shape))
	for i, s := range shape {
		intShape[i] = C.int(s)
	}
	handle := C.mlx_array_new_data(cData, &intShape[0], C.int(len(shape)), C.mlx_dtype(dtype))
	return newArray(handle)
}

// ============ Device Control ============

// SetDefaultDeviceGPU sets the default device to GPU (Metal)
func SetDefaultDeviceGPU() {
	dev := C.mlx_device_new_type(C.MLX_GPU, 0)
	C.mlx_set_default_device(dev)
	C.mlx_device_free(dev)
}

// SetDefaultDeviceCPU sets the default device to CPU
func SetDefaultDeviceCPU() {
	dev := C.mlx_device_new_type(C.MLX_CPU, 0)
	C.mlx_set_default_device(dev)
	C.mlx_device_free(dev)
}

// MetalIsAvailable returns true if Metal GPU is available
func MetalIsAvailable() bool {
	var available C._Bool
	C.mlx_metal_is_available(&available)
	return bool(available)
}

// MetalStartCapture starts a GPU trace capture to the given file path.
// The path must not already exist. Run with MTL_CAPTURE_ENABLED=1 env var.
// Open the resulting .gputrace file in Xcode for analysis.
func MetalStartCapture(path string) {
	cPath := C.CString(path)
	defer C.free(unsafe.Pointer(cPath))
	C.mlx_metal_start_capture(cPath)
}

// MetalStopCapture stops the current GPU trace capture.
func MetalStopCapture() {
	C.mlx_metal_stop_capture()
}

// GPUIsAvailable returns true if any GPU (Metal or CUDA) is available
func GPUIsAvailable() bool {
	// On Linux with CUDA build, GPU is available
	// On macOS, check Metal availability
	if MetalIsAvailable() {
		return true
	}
	// CUDA is available if we compiled with CUDA support (Linux)
	return runtime.GOOS == "linux"
}

// GetDefaultDeviceType returns the current default device (0=CPU, 1=GPU)
func GetDefaultDeviceType() int {
	var dev C.mlx_device
	C.mlx_get_default_device(&dev)
	var devType C.mlx_device_type
	C.mlx_device_get_type(&devType, dev)
	C.mlx_device_free(dev)
	return int(devType)
}

// Synchronize waits for all GPU operations to complete
func Synchronize() {
	C.mlx_synchronize(C.default_stream())
}

// ScaledDotProductAttention computes optimized attention using GPU kernel
// Q, K, V should be [batch, heads, seq, head_dim]
func ScaledDotProductAttention(q, k, v *Array, scale float32, causalMask bool) *Array {
	res := C.mlx_array_new()
	maskMode := "" // empty string for no mask
	if causalMask {
		maskMode = "causal"
	}
	cMaskMode := C.CString(maskMode)
	defer C.free(unsafe.Pointer(cMaskMode))
	C.mlx_fast_scaled_dot_product_attention(&res, q.c, k.c, v.c, C.float(scale), cMaskMode, C.mlx_array{}, C.mlx_array{}, C.default_stream())
	return newArray(res)
}

// ScaledDotProductAttentionWithSinks computes attention with sinks support
// maskMode: "causal", "sliding_window", or "" for none
// mask: optional attention mask array (nil for none)
// sinks: attention sinks array (nil for none)
func ScaledDotProductAttentionWithSinks(q, k, v *Array, scale float32, maskMode string, mask, sinks *Array) *Array {
	res := C.mlx_array_new()
	cMaskMode := C.CString(maskMode)
	defer C.free(unsafe.Pointer(cMaskMode))
	var maskH, sinksH C.mlx_array
	if mask != nil {
		maskH = mask.c
	}
	if sinks != nil {
		sinksH = sinks.c
	}
	C.mlx_fast_scaled_dot_product_attention(&res, q.c, k.c, v.c, C.float(scale), cMaskMode, maskH, sinksH, C.default_stream())
	return newArray(res)
}

// ============ Native Safetensors Loading ============

// SafetensorsFile represents a loaded safetensors file
type SafetensorsFile struct {
	arrays   C.mlx_map_string_to_array
	metadata C.mlx_map_string_to_string
}

// LoadSafetensorsNative loads a safetensors file using MLX's optimized loader
// Note: Uses CPU stream because Load primitive only runs on CPU
func LoadSafetensorsNative(path string) (*SafetensorsFile, error) {
	cPath := C.CString(path)
	defer C.free(unsafe.Pointer(cPath))

	var arrays C.mlx_map_string_to_array
	var metadata C.mlx_map_string_to_string
	if C.mlx_load_safetensors(&arrays, &metadata, cPath, C.cpu_stream()) != 0 {
		return nil, fmt.Errorf("failed to load safetensors: %s", path)
	}
	return &SafetensorsFile{arrays: arrays, metadata: metadata}, nil
}

// Get retrieves a tensor by name
func (s *SafetensorsFile) Get(name string) *Array {
	cName := C.CString(name)
	defer C.free(unsafe.Pointer(cName))

	var arr C.mlx_array
	if C.mlx_map_string_to_array_get(&arr, s.arrays, cName) != 0 {
		return nil
	}
	if arr.ctx == nil {
		return nil
	}
	return newArray(arr)
}

// Set replaces a tensor in the map (like Python's weights[k] = v)
func (s *SafetensorsFile) Set(name string, arr *Array) {
	cName := C.CString(name)
	defer C.free(unsafe.Pointer(cName))
	C.mlx_map_string_to_array_insert(s.arrays, cName, arr.c)
}

// Count returns the number of tensors (not directly available, would need iterator)
func (s *SafetensorsFile) Count() int {
	// mlx-c doesn't have a direct count - would need to iterate
	return 0
}

// Free releases the safetensors file
func (s *SafetensorsFile) Free() {
	C.mlx_map_string_to_array_free(s.arrays)
	C.mlx_map_string_to_string_free(s.metadata)
}

// ============ NPY Loading ============

// LoadNpy loads a numpy array from an npy file
// Note: Uses CPU stream because Load primitive only runs on CPU
func LoadNpy(path string) (*Array, error) {
	cPath := C.CString(path)
	defer C.free(unsafe.Pointer(cPath))

	var arr C.mlx_array
	if C.mlx_load(&arr, cPath, C.cpu_stream()) != 0 {
		return nil, fmt.Errorf("failed to load npy: %s", path)
	}
	if arr.ctx == nil {
		return nil, fmt.Errorf("failed to load npy: %s", path)
	}
	return newArray(arr), nil
}

// ============ Slice Update ============

// SliceUpdate updates a slice of the array with new values
func SliceUpdate(a, update *Array, start, stop []int32) *Array {
	n := len(start)
	cStart := make([]C.int, n)
	cStop := make([]C.int, n)
	cStrides := make([]C.int, n)
	for i := 0; i < n; i++ {
		cStart[i] = C.int(start[i])
		cStop[i] = C.int(stop[i])
		cStrides[i] = 1 // Default stride of 1
	}
	res := C.mlx_array_new()
	C.mlx_slice_update(&res, a.c, update.c, &cStart[0], C.size_t(n), &cStop[0], C.size_t(n), &cStrides[0], C.size_t(n), C.default_stream())
	return newArray(res)
}

// SliceUpdateInplace updates a slice and returns a new array.
// Note: Despite the name, this is NOT in-place - MLX arrays are immutable.
// The caller must use the returned value.
func SliceUpdateInplace(a, update *Array, start, stop []int32) *Array {
	return SliceUpdate(a, update, start, stop)
}

// ============ Optimized Operations ============

// SampleArgmax gets the last logit position and returns argmax (fused operation)
func SampleArgmax(logits *Array) int32 {
	result := Argmax(logits, -1, false)
	return result.ItemInt32()
}

// ArgmaxKeepArray returns argmax as an Array (for pipelining, no sync)
// This is like mlx-lm's sampler that returns y as an array, not .item()
func ArgmaxKeepArray(logits *Array) *Array {
	// For greedy decoding: logits shape is [1, 1, vocab]
	// We want argmax over vocab dimension, return shape []
	return Argmax(logits, -1, false)
}

// RandomState is the global PRNG state, analogous to mx.random.state in Python.
// It's a slice containing a single key array. Random functions use and update this state.
//
// Thread safety: Protected by randomStateMu, mimicking Python's GIL behavior.
// All random functions that use global state acquire this lock.
var RandomState = []*Array{nil}
var randomStateMu sync.Mutex

func init() {
	// Lock main goroutine to OS thread for CUDA context stability.
	// CUDA contexts are bound to threads; Go can migrate goroutines between threads.
	runtime.LockOSThread()
	RandomState[0] = RandomKey(uint64(time.Now().UnixMilli()))
	Keep(RandomState[0]) // Global state should persist
}

// RandomKey creates a PRNG key from a seed
func RandomKey(seed uint64) *Array {
	var res C.mlx_array
	C.mlx_random_key(&res, C.uint64_t(seed))
	return newArray(res)
}

// RandomSplit splits a PRNG key into two new keys
func RandomSplit(key *Array) (*Array, *Array) {
	var key1, key2 C.mlx_array
	C.mlx_random_split(&key1, &key2, key.c, C.default_stream())
	return newArray(key1), newArray(key2)
}

// RandomCategoricalWithKey samples from categorical distribution using provided key.
func RandomCategoricalWithKey(logits, key *Array, axis int, numSamples int) *Array {
	res := C.mlx_array_new()
	C.mlx_random_categorical_num_samples(&res, logits.c, C.int(axis), C.int(numSamples), key.c, C.default_stream())
	return newArray(res)
}

// RandomCategorical samples using global RandomState.
// For simple scripts - production code should use RandomCategoricalWithKey with explicit key management.
func RandomCategorical(logits *Array, axis int, numSamples int) *Array {
	randomStateMu.Lock()
	oldKey := RandomState[0]
	key1, key2 := RandomSplit(oldKey)
	Keep(key1) // key1 becomes the new global state
	oldKey.Free()
	RandomState[0] = key1
	randomStateMu.Unlock()
	return RandomCategoricalWithKey(logits, key2, axis, numSamples)
}

// RandomNormal creates a random normal (Gaussian) tensor
func RandomNormal(shape []int32, seed uint64) *Array {
	key := RandomKey(seed)
	res := C.mlx_array_new()
	C.mlx_random_normal(&res, int32ToCInt(shape), C.size_t(len(shape)), C.MLX_FLOAT32, 0.0, 1.0, key.c, C.default_stream())
	return newArray(res)
}

// RandomUniform generates uniform random values in [0, 1) with the given shape
func RandomUniform(shape []int32, seed uint64) *Array {
	key := RandomKey(seed)
	low := C.mlx_array_new_float(0.0)
	high := C.mlx_array_new_float(1.0)
	res := C.mlx_array_new()
	C.mlx_random_uniform(&res, low, high, int32ToCInt(shape), C.size_t(len(shape)), C.MLX_FLOAT32, key.c, C.default_stream())
	C.mlx_array_free(low)
	C.mlx_array_free(high)
	return newArray(res)
}

// Conv2d performs 2D convolution
// input: [N, H, W, C], weight: [O, kH, kW, C]  (MLX uses NHWC layout)
// Returns: [N, H', W', O]
func Conv2d(input, weight *Array, stride, padding int32) *Array {
	res := C.mlx_array_new()
	C.mlx_conv2d(&res, input.c, weight.c, C.int(stride), C.int(stride), C.int(padding), C.int(padding), 1, 1, 1, C.default_stream())
	return newArray(res)
}

// Conv3d performs 3D convolution
// input: [N, D, H, W, C], weight: [O, kD, kH, kW, C]  (MLX uses NDHWC layout)
// Returns: [N, D', H', W', O]
func Conv3d(input, weight *Array, strideD, strideH, strideW, padD, padH, padW int32) *Array {
	res := C.mlx_array_new()
	C.mlx_conv3d(&res, input.c, weight.c, C.int(strideD), C.int(strideH), C.int(strideW), C.int(padD), C.int(padH), C.int(padW), 1, 1, 1, 1, C.default_stream())
	return newArray(res)
}

// ============ Compilation Control ============

// EnableCompile enables global compilation/graph fusion
func EnableCompile() {
	C.mlx_enable_compile()
}

// DisableCompile disables global compilation
func DisableCompile() {
	C.mlx_disable_compile()
}

// SetCompileMode sets the compile mode
// 0=disabled, 1=no_simplify, 2=no_fuse, 3=enabled
func SetCompileMode(mode int) {
	C.mlx_set_compile_mode(C.mlx_compile_mode(mode))
}

// ============ Stream Control ============

// Stream represents an MLX execution stream
type Stream struct {
	c C.mlx_stream
}

// NewStream creates a new execution stream on the default device
func NewStream() *Stream {
	var dev C.mlx_device
	C.mlx_get_default_device(&dev)
	stream := C.mlx_stream_new_device(dev)
	C.mlx_device_free(dev)
	return &Stream{c: stream}
}

// Free releases the stream
func (s *Stream) Free() {
	if s.c.ctx != nil {
		C.mlx_stream_free(s.c)
		s.c.ctx = nil
	}
}

// SetDefaultStream sets the default stream for operations
func SetDefaultStream(s *Stream) {
	C.mlx_set_default_stream(s.c)
	C.set_default_stream(s.c) // Also update our cached stream
}

// GetDefaultStream returns the current default stream
func GetDefaultStream() *Stream {
	var stream C.mlx_stream
	var dev C.mlx_device
	C.mlx_get_default_device(&dev)
	C.mlx_get_default_stream(&stream, dev)
	C.mlx_device_free(dev)
	return &Stream{c: stream}
}

// SynchronizeStream waits for all operations on the stream to complete
func SynchronizeStream(s *Stream) {
	C.mlx_synchronize(s.c)
}

// ============ Metal Memory Control ============

// MetalGetCacheMemory returns the current cache memory usage in bytes
func MetalGetCacheMemory() uint64 {
	var size C.size_t
	C.mlx_get_cache_memory(&size)
	return uint64(size)
}

// MetalGetPeakMemory returns the peak memory usage in bytes
func MetalGetPeakMemory() uint64 {
	var size C.size_t
	C.mlx_get_peak_memory(&size)
	return uint64(size)
}

// MetalResetPeakMemory resets the peak memory counter
func MetalResetPeakMemory() {
	C.mlx_reset_peak_memory()
}

// MetalSetWiredLimit sets the wired memory limit and returns the previous limit
// This keeps tensors pinned in GPU memory for faster access
func MetalSetWiredLimit(limit uint64) uint64 {
	var prev C.size_t
	C.mlx_set_wired_limit(&prev, C.size_t(limit))
	return uint64(prev)
}

// MetalGetActiveMemory returns the current active memory usage in bytes
func MetalGetActiveMemory() uint64 {
	var size C.size_t
	C.mlx_get_active_memory(&size)
	return uint64(size)
}

// ClearCache clears the MLX memory cache
func ClearCache() {
	C.mlx_clear_cache()
}

// SetCacheLimit sets the free cache limit in bytes
// Setting to 0 disables caching (useful for memory-constrained generation)
// Returns the previous cache limit
func SetCacheLimit(limit uint64) uint64 {
	var prev C.size_t
	C.mlx_set_cache_limit(&prev, C.size_t(limit))
	return uint64(prev)
}

// SetMemoryLimit sets the overall memory limit in bytes
// This is a guideline for maximum memory during graph evaluation.
// When Metal is available, defaults to 1.5x the max recommended working set.
// Returns the previous memory limit
func SetMemoryLimit(limit uint64) uint64 {
	var prev C.size_t
	C.mlx_set_memory_limit(&prev, C.size_t(limit))
	return uint64(prev)
}

// GetMemoryLimit returns the current memory limit in bytes
func GetMemoryLimit() uint64 {
	var size C.size_t
	C.mlx_get_memory_limit(&size)
	return uint64(size)
}

// ============ MoE Operations ============

// GatherMM performs gather matrix multiplication for MoE
// a: input, b: weight matrices
// lhsIndices, rhsIndices: optional expert selection indices (nil for none)
func GatherMM(a, b *Array, lhsIndices, rhsIndices *Array, sortedIndices bool) *Array {
	var lhs, rhs C.mlx_array
	if lhsIndices != nil {
		lhs = lhsIndices.c
	}
	if rhsIndices != nil {
		rhs = rhsIndices.c
	}
	res := C.mlx_array_new()
	C.mlx_gather_mm(&res, a.c, b.c, lhs, rhs, C._Bool(sortedIndices), C.default_stream())
	return newArray(res)
}

// GatherQMM performs quantized gather matrix multiplication for MoE
// Used for MXFP4 and other quantized MoE inference
func GatherQMM(x, w, scales *Array, biases, lhsIndices, rhsIndices *Array, transpose bool, groupSize, bits int, mode string, sortedIndices bool) *Array {
	var b, lhs, rhs C.mlx_array
	if biases != nil {
		b = biases.c
	}
	if lhsIndices != nil {
		lhs = lhsIndices.c
	}
	if rhsIndices != nil {
		rhs = rhsIndices.c
	}
	cMode := C.CString(mode)
	defer C.free(unsafe.Pointer(cMode))
	optGroupSize := C.mlx_optional_int{value: C.int(groupSize), has_value: true}
	optBits := C.mlx_optional_int{value: C.int(bits), has_value: true}
	res := C.mlx_array_new()
	C.mlx_gather_qmm(&res, x.c, w.c, scales.c, b, lhs, rhs, C._Bool(transpose), optGroupSize, optBits, cMode, C._Bool(sortedIndices), C.default_stream())
	return newArray(res)
}

// ============ Quantization ============

// Quantize quantizes weights to specified bits per element.
// Returns (quantized_weights, scales, biases).
// groupSize: number of elements quantized together (default 64)
// bits: bits per element, 2, 4, or 8 (default 4)
// mode: "affine" (default) or "mxfp4"
func Quantize(w *Array, groupSize, bits int, mode string) (weights, scales, biases *Array) {
	cMode := C.CString(mode)
	defer C.free(unsafe.Pointer(cMode))
	optGroupSize := C.mlx_optional_int{value: C.int(groupSize), has_value: true}
	optBits := C.mlx_optional_int{value: C.int(bits), has_value: true}
	res := C.mlx_vector_array_new()
	C.mlx_quantize(&res, w.c, optGroupSize, optBits, cMode, C.default_stream())

	// Result is a vector of 3 arrays: [weights, scales, biases]
	var w0, w1, w2 C.mlx_array
	C.mlx_vector_array_get(&w0, res, 0)
	C.mlx_vector_array_get(&w1, res, 1)
	C.mlx_vector_array_get(&w2, res, 2)
	C.mlx_vector_array_free(res)

	return newArray(w0), newArray(w1), newArray(w2)
}

// Dequantize reconstructs weights from quantized form.
// groupSize: number of elements quantized together (default 64)
// bits: bits per element, 2, 4, or 8 (default 4)
// mode: "affine" (default) or "mxfp4"
func Dequantize(w, scales, biases *Array, groupSize, bits int, mode string) *Array {
	cMode := C.CString(mode)
	defer C.free(unsafe.Pointer(cMode))
	optGroupSize := C.mlx_optional_int{value: C.int(groupSize), has_value: true}
	optBits := C.mlx_optional_int{value: C.int(bits), has_value: true}
	optDtype := C.mlx_optional_dtype{has_value: false}

	var b C.mlx_array
	if biases != nil {
		b = biases.c
	}

	res := C.mlx_array_new()
	C.mlx_dequantize(&res, w.c, scales.c, b, optGroupSize, optBits, cMode, optDtype, C.default_stream())
	return newArray(res)
}

// QuantizedMatmul performs matrix multiplication with quantized weights.
// x: input tensor [batch..., in_features]
// w: quantized weights
// scales, biases: from Quantize
// transpose: if true, compute x @ w.T (typical for Linear layers)
// groupSize, bits, mode: must match what was used in Quantize
func QuantizedMatmul(x, w, scales, biases *Array, transpose bool, groupSize, bits int, mode string) *Array {
	cMode := C.CString(mode)
	defer C.free(unsafe.Pointer(cMode))
	optGroupSize := C.mlx_optional_int{value: C.int(groupSize), has_value: true}
	optBits := C.mlx_optional_int{value: C.int(bits), has_value: true}

	var b C.mlx_array
	if biases != nil {
		b = biases.c
	}

	res := C.mlx_array_new()
	C.mlx_quantized_matmul(&res, x.c, w.c, scales.c, b, C._Bool(transpose), optGroupSize, optBits, cMode, C.default_stream())
	return newArray(res)
}

// ============ Sorting and Top-K ============

// TopK returns the k largest elements along an axis
func TopK(a *Array, k int, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_topk_axis(&res, a.c, C.int(k), C.int(axis), C.default_stream())
	return newArray(res)
}

// Argpartition returns indices for partial sort (k-th smallest first)
func Argpartition(a *Array, kth int, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_argpartition_axis(&res, a.c, C.int(kth), C.int(axis), C.default_stream())
	return newArray(res)
}

// TakeAlongAxis takes elements from array using indices along axis
func TakeAlongAxis(a, indices *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_take_along_axis(&res, a.c, indices.c, C.int(axis), C.default_stream())
	return newArray(res)
}

// PutAlongAxis puts values into array at indices along axis
func PutAlongAxis(a, indices, values *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_put_along_axis(&res, a.c, indices.c, values.c, C.int(axis), C.default_stream())
	return newArray(res)
}

// Cumsum computes cumulative sum along an axis
func Cumsum(a *Array, axis int) *Array {
	res := C.mlx_array_new()
	C.mlx_cumsum(&res, a.c, C.int(axis), false, false, C.default_stream())
	return newArray(res)
}

// Where selects elements: condition ? a : b
func Where(condition, a, b *Array) *Array {
	res := C.mlx_array_new()
	C.mlx_where(&res, condition.c, a.c, b.c, C.default_stream())
	return newArray(res)
}

// LessScalar returns element-wise a < scalar
func LessScalar(a *Array, s float32) *Array {
	scalar := C.mlx_array_new_float(C.float(s))
	res := C.mlx_array_new()
	C.mlx_less(&res, a.c, scalar, C.default_stream())
	C.mlx_array_free(scalar)
	return newArray(res)
}

// FullDtype creates an array filled with a value with specific dtype
func FullDtype(value float32, dtype Dtype, shape ...int32) *Array {
	intShape := make([]C.int, len(shape))
	for i, s := range shape {
		intShape[i] = C.int(s)
	}
	vals := C.mlx_array_new_float(C.float(value))
	res := C.mlx_array_new()
	C.mlx_full(&res, &intShape[0], C.size_t(len(shape)), vals, C.mlx_dtype(dtype), C.default_stream())
	C.mlx_array_free(vals)
	return newArray(res)
}

// AsType casts an array to a different dtype
func AsType(a *Array, dtype Dtype) *Array {
	res := C.mlx_array_new()
	C.mlx_astype(&res, a.c, C.mlx_dtype(dtype), C.default_stream())
	return newArray(res)
}

// ToBFloat16 casts an array to bfloat16
func ToBFloat16(a *Array) *Array {
	return AsType(a, DtypeBFloat16)
}

// ============ VibeVoice Helper Functions ============

// NewScalarArray creates a true 0-dimensional scalar array from a float32 value
func NewScalarArray(value float32) *Array {
	return newArray(C.mlx_array_new_float(C.float(value)))
}

// Global random seed counter for RandN
var randnSeedCounter uint64 = uint64(time.Now().UnixNano())

// RandN creates an array of random samples from a standard normal distribution
func RandN(shape []int32) *Array {
	// Use incrementing seed for unique random values each call
	seed := atomic.AddUint64(&randnSeedCounter, 1)
	return RandomNormal(shape, seed)
}

// Pad pads an array with zeros
// paddings: [before_0, after_0, before_1, after_1, ...] for each dimension
func Pad(a *Array, paddings []int32) *Array {
	numAxes := len(paddings) / 2
	// Convert to low/high pairs
	lowPad := make([]C.int, numAxes)
	highPad := make([]C.int, numAxes)
	for i := 0; i < numAxes; i++ {
		lowPad[i] = C.int(paddings[i*2])
		highPad[i] = C.int(paddings[i*2+1])
	}
	zero := C.mlx_array_new_float(0.0)
	res := C.mlx_array_new()
	// mlx_pad takes axes, low, high arrays
	axes := make([]C.int, numAxes)
	for i := 0; i < numAxes; i++ {
		axes[i] = C.int(i)
	}
	cMode := C.CString("constant")
	defer C.free(unsafe.Pointer(cMode))
	C.mlx_pad(&res, a.c, &axes[0], C.size_t(numAxes), &lowPad[0], C.size_t(numAxes), &highPad[0], C.size_t(numAxes), zero, cMode, C.default_stream())
	C.mlx_array_free(zero)
	return newArray(res)
}

// Conv1d performs 1D convolution
// x: [B, L, Cin], weight: [Cout, K, Cin] (MLX uses NLC layout)
// bias: optional (nil for no bias)
func Conv1d(x, weight *Array, bias *Array, stride int32) *Array {
	res := C.mlx_array_new()
	C.mlx_conv1d(&res, x.c, weight.c, C.int(stride), C.int(0), C.int(1), 1, C.default_stream())
	// Apply bias if provided
	if bias != nil {
		biased := C.mlx_array_new()
		C.mlx_add(&biased, res, bias.c, C.default_stream())
		C.mlx_array_free(res)
		return newArray(biased)
	}
	return newArray(res)
}

// ConvTranspose1d performs transposed 1D convolution
// x: [B, L, Cin], weight: [Cout, K, Cin] (MLX uses NLC layout)
// bias: optional (nil for no bias)
func ConvTranspose1d(x, weight *Array, bias *Array, stride int32) *Array {
	res := C.mlx_array_new()
	// stride, padding, dilation, output_padding, groups
	C.mlx_conv_transpose1d(&res, x.c, weight.c, C.int(stride), 0, 1, 0, 1, C.default_stream())
	// Apply bias if provided
	if bias != nil {
		biased := C.mlx_array_new()
		C.mlx_add(&biased, res, bias.c, C.default_stream())
		C.mlx_array_free(res)
		return newArray(biased)
	}
	return newArray(res)
}

// DepthwiseConv1d performs depthwise 1D convolution (groups=Cin)
// x: [B, L, C], weight: [1, K, C] (groups = C)
// bias: optional (nil for no bias)
func DepthwiseConv1d(x, weight *Array, bias *Array) *Array {
	// Get number of input channels for groups
	shape := x.Shape()
	groups := int(shape[len(shape)-1])
	res := C.mlx_array_new()
	C.mlx_conv1d(&res, x.c, weight.c, 1, 0, 1, C.int(groups), C.default_stream())
	// Apply bias if provided
	if bias != nil {
		biased := C.mlx_array_new()
		C.mlx_add(&biased, res, bias.c, C.default_stream())
		C.mlx_array_free(res)
		return newArray(biased)
	}
	return newArray(res)
}

// SliceAxis extracts a slice along a specific axis
func SliceAxis(a *Array, axis int, start, stop int32) *Array {
	shape := a.Shape()

	// Build start and stop indices for all dimensions
	starts := make([]int32, len(shape))
	stops := make([]int32, len(shape))
	for i := range shape {
		if i == axis {
			starts[i] = start
			stops[i] = stop
		} else {
			starts[i] = 0
			stops[i] = shape[i]
		}
	}

	return Slice(a, starts, stops)
}

// Tri creates a lower triangular matrix
func Tri(n, m int32, k int) *Array {
	res := C.mlx_array_new()
	C.mlx_tri(&res, C.int(n), C.int(m), C.int(k), C.MLX_FLOAT32, C.default_stream())
	return newArray(res)
}