unsloth-rs 1.0.2

Rust implementations of transformer building blocks for LLM inference
Documentation
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
// SPDX-License-Identifier: MIT
// Copyright 2026 Tyler Zervas

//! CubeCL GPU kernel for ternary bitsliced matrix multiplication.
//!
//! ## Implementation Status
//!
//! Provides four implementations with increasing optimization:
//! - **Basic kernel**: Simple popcount-based implementation for validation
//! - **Tiled kernel**: Optimized with shared memory for better performance
//! - **Vectorized kernel**: Uses Line<u32> for 4-element vectorized loads
//! - **Sparse kernel**: Plane skipping optimization for 95%+ sparse models
//!
//! ## Completed Tasks
//! - Task 2.1: u32 tensor interop ✅
//! - Task 2.2: Basic popcount kernel ✅
//! - Task 2.3: Tiled kernel with shared memory ✅
//! - Task 2.4: Vectorized Line<u32> loads ✅
//! - Task 2.5: Plane skipping with sparsity metadata ✅
//! - Task 2.6: GPU dispatch integration ✅
//!
//! ## Phase 2 Complete!
//!
//! All Phase 2 tasks are implemented. The kernels are ready for GPU validation
//! when hardware becomes available.
//!
//! ## Phase 4: Advanced Sparsity Optimization (In Progress)
//!
//! - Task 4.1: Dynamic plane skipping refinement ✅
//! - Task 4.2: Chunk-based sparsity optimization ✅
//! - Task 4.3: Sparsity profiler and analysis tools (next)
//!
//! ## Note on Array Types
//!
//! This implementation uses f32 arrays with bit reinterpretation for weight planes.
//! The weight planes (w_plus, w_minus) are f32 arrays where each f32 is reinterpreted as u32.
//! This approach works with CubeCL's current type system.

use cubecl::prelude::*;

// Constants for bitsliced operations
const BITS_PER_U32: u32 = 32;
const CHUNKS_PER_U64: u32 = 64;

/// Compile-time configuration for basic ternary matmul kernel.
/// Renamed from TernaryMatmulConfig to distinguish from the tiled version in matmul.rs.
#[derive(Clone, Copy, Debug)]
pub struct BasicTernaryMatmulConfig {
    /// Number of u32 words in K dimension (in_features / 32)
    pub k_words: u32,
    /// Number of output rows (batch size)
    pub m: u32,
    /// Number of output cols (out_features)
    pub n: u32,
    /// Number of input features (for bounds checking)
    pub in_features: u32,
}

/// Basic ternary matmul kernel with popcount-based dot product.
///
/// Each thread computes one output element: output[batch_idx, out_idx]
///
/// Algorithm:
/// ```text
/// For each input: Quantize FP input to bitsliced planes
/// For each u32 word:
///   pos_matches = popcount(w_plus & input_plus) + popcount(w_minus & input_minus)
///   neg_matches = popcount(w_plus & input_minus) + popcount(w_minus & input_plus)
///   dot += pos_matches - neg_matches
/// output = dot * scale
/// ```
///
/// This uses hardware popcount operations for efficiency.
#[cube(launch_unchecked)]
pub fn ternary_matmul_kernel_basic<F: Float>(
    // Input activations [batch, in_features] as f32
    input: &Array<F>,
    // Weight positive plane [out_features, k_words] as u32 (bit-reinterpreted from f32)
    w_plus: &Array<F>,
    // Weight negative plane [out_features, k_words] as u32
    w_minus: &Array<F>,
    // Per-row scales [out_features]
    scales: &Array<F>,
    // Output [batch, out_features]
    output: &mut Array<F>,
    // Compile-time configuration
    #[comptime] config: BasicTernaryMatmulConfig,
) {
    // Thread indices: each thread handles one (batch, out_feature) element
    // Grid layout: X=batch, Y=blocks of features
    // Block layout: X=threads (256), Y=1, Z=1
    let batch_idx = CUBE_POS_X;
    let out_idx = CUBE_POS_Y * CUBE_DIM_X + UNIT_POS_X;

    // Bounds checks
    if batch_idx >= config.m {
        return;
    }
    if out_idx >= config.n {
        return;
    }

    // Quantize input row to bitsliced planes
    // This converts FP values to ternary {-1, 0, +1} representation
    let input_offset = batch_idx * config.in_features;

    // Allocate local arrays for input planes (on registers)
    let mut input_plus = Array::<u32>::new(config.k_words);
    let mut input_minus = Array::<u32>::new(config.k_words);

    // Quantize input to planes
    for k in 0..config.k_words {
        let mut plus_word = 0u32;
        let mut minus_word = 0u32;

        // Process 32 dimensions per word
        let base_dim = k * 32;
        let end_dim = u32::min(base_dim + 32, config.in_features);

        for bit in 0u32..(end_dim - base_dim) {
            let dim_idx = base_dim + bit;
            let val = input[input_offset + dim_idx];

            // Simple threshold quantization: >0.5 → +1, <-0.5 → -1, else 0
            let threshold = F::new(0.5);
            let neg_threshold = F::new(-0.5);

            if val > threshold {
                plus_word = plus_word | (1u32 << bit);
            } else if val < neg_threshold {
                minus_word = minus_word | (1u32 << bit);
            }
        }

        input_plus[k] = plus_word;
        input_minus[k] = minus_word;
    }

    // Popcount-based dot product
    let weight_offset = out_idx * config.k_words;
    let mut pos_sum = 0u32;
    let mut neg_sum = 0u32;

    for k in 0..config.k_words {
        // Reinterpret f32 as u32 bits for weight planes
        let wp_f32 = w_plus[weight_offset + k];
        let wm_f32 = w_minus[weight_offset + k];
        let wp_bits = u32::reinterpret(wp_f32);
        let wm_bits = u32::reinterpret(wm_f32);

        let ip = input_plus[k];
        let im = input_minus[k];

        // Popcount-based ternary dot product:
        // pos_matches = (+1 weights × +1 inputs) + (-1 weights × -1 inputs)
        // neg_matches = (+1 weights × -1 inputs) + (-1 weights × +1 inputs)
        // dot = pos_matches - neg_matches

        pos_sum = pos_sum + (wp_bits & ip).count_ones();
        pos_sum = pos_sum + (wm_bits & im).count_ones();

        neg_sum = neg_sum + (wp_bits & im).count_ones();
        neg_sum = neg_sum + (wm_bits & ip).count_ones();
    }

    // Convert popcount result to float and apply scale
    let dot = F::cast_from(pos_sum) - F::cast_from(neg_sum);
    let scale = scales[out_idx];
    output[batch_idx * config.n + out_idx] = dot * scale;
}

/// Compile-time configuration for tiled ternary matmul kernel.
#[derive(Clone, Copy, Debug)]
pub struct TiledTernaryMatmulConfig {
    /// Number of u32 words in K dimension (in_features / 32)
    pub k_words: u32,
    /// Number of output rows (batch size)
    pub m: u32,
    /// Number of output cols (out_features)
    pub n: u32,
    /// Number of input features (for bounds checking)
    pub in_features: u32,
    /// Tile size for K dimension in u32 words (default: 32-64)
    pub tile_k: u32,
    /// Number of outputs computed per thread (default: 1-4)
    pub outputs_per_thread: u32,
    /// Block size (threads per block, default: 256)
    pub block_size: u32,
}

impl TiledTernaryMatmulConfig {
    /// Create configuration optimized for RTX 5080
    pub fn rtx_5080_preset(m: u32, n: u32, k_words: u32, in_features: u32) -> Self {
        Self {
            k_words,
            m,
            n,
            in_features,
            tile_k: 64, // 64 u32 words = 2048 dimensions
            outputs_per_thread: 2,
            block_size: 256,
        }
    }

    /// Create configuration optimized for RTX 3090 Ti
    pub fn rtx_3090ti_preset(m: u32, n: u32, k_words: u32, in_features: u32) -> Self {
        Self {
            k_words,
            m,
            n,
            in_features,
            tile_k: 32, // 32 u32 words = 1024 dimensions
            outputs_per_thread: 2,
            block_size: 256,
        }
    }
}

/// Tiled ternary matmul kernel with shared memory optimization.
///
/// Uses shared memory to cache input tiles and weight planes for better memory locality.
/// Each thread computes multiple output elements to improve occupancy.
///
/// Algorithm:
/// ```text
/// For each K tile:
///   - Cooperatively load input tile to shared memory
///   - Quantize input tile to bitsliced planes in shared memory
///   - Each thread loads its weight rows
///   - Compute popcount dot products for tile
///   - Accumulate results
/// output = accumulated_dot * scale
/// ```
#[cube(launch_unchecked)]
pub fn ternary_matmul_kernel_tiled<F: Float>(
    // Input activations [batch, in_features] as f32
    input: &Array<F>,
    // Weight positive plane [out_features, k_words] as u32 (bit-reinterpreted from f32)
    w_plus: &Array<F>,
    // Weight negative plane [out_features, k_words] as u32
    w_minus: &Array<F>,
    // Per-row scales [out_features]
    scales: &Array<F>,
    // Output [batch, out_features]
    output: &mut Array<F>,
    // Compile-time configuration
    #[comptime] config: TiledTernaryMatmulConfig,
) {
    // Thread and block indices
    let batch_idx = CUBE_POS_X;
    let out_block_idx = CUBE_POS_Y;
    let thread_idx = UNIT_POS_X;

    // Bounds check for batch
    if batch_idx >= config.m {
        return;
    }

    // Shared memory for input tile (plus and minus planes)
    // Each tile holds tile_k u32 words for plus and minus planes
    let mut input_plus_tile = SharedMemory::<u32>::new(config.tile_k);
    let mut input_minus_tile = SharedMemory::<u32>::new(config.tile_k);

    // Each thread computes outputs_per_thread outputs
    for out_local in 0..config.outputs_per_thread {
        let out_idx = out_block_idx * config.block_size * config.outputs_per_thread
            + thread_idx * config.outputs_per_thread
            + out_local;

        if out_idx >= config.n {
            continue;
        }

        let input_offset = batch_idx * config.in_features;
        let weight_offset = out_idx * config.k_words;

        // Accumulator for this output
        let mut pos_sum = 0u32;
        let mut neg_sum = 0u32;

        // Number of K tiles
        let num_k_tiles = (config.k_words + config.tile_k - 1) / config.tile_k;

        // Process K dimension in tiles
        for k_tile in 0..num_k_tiles {
            let k_start = k_tile * config.tile_k;
            let k_end = u32::min(k_start + config.tile_k, config.k_words);
            let tile_size = k_end - k_start;

            // Cooperatively load and quantize input tile to shared memory
            // Each thread loads one u32 word worth of input (32 FP values → 1 u32 word)
            if thread_idx < tile_size {
                let k_word = k_start + thread_idx;
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                // Quantize 32 input dimensions to one u32 word
                let base_dim = k_word * 32;
                let end_dim = u32::min(base_dim + 32, config.in_features);

                for bit in 0..(end_dim - base_dim) {
                    let dim_idx = base_dim + bit;
                    let val = input[input_offset + dim_idx];

                    let threshold = F::new(0.5);
                    let neg_threshold = F::new(-0.5);

                    if val > threshold {
                        plus_word = plus_word | (1u32 << bit);
                    } else if val < neg_threshold {
                        minus_word = minus_word | (1u32 << bit);
                    }
                }

                input_plus_tile[thread_idx] = plus_word;
                input_minus_tile[thread_idx] = minus_word;
            }

            // Synchronize to ensure tile is fully loaded
            sync_units();

            // Compute popcount dot product for this tile
            for k_local in 0..tile_size {
                let k_word = k_start + k_local;

                // Load weight planes for this output feature
                let wp_f32 = w_plus[weight_offset + k_word];
                let wm_f32 = w_minus[weight_offset + k_word];
                let wp_bits = u32::reinterpret(wp_f32);
                let wm_bits = u32::reinterpret(wm_f32);

                // Load input planes from shared memory
                let ip = input_plus_tile[k_local];
                let im = input_minus_tile[k_local];

                // Popcount-based ternary dot product
                pos_sum = pos_sum + (wp_bits & ip).count_ones();
                pos_sum = pos_sum + (wm_bits & im).count_ones();

                neg_sum = neg_sum + (wp_bits & im).count_ones();
                neg_sum = neg_sum + (wm_bits & ip).count_ones();
            }

            // Synchronize before loading next tile
            sync_units();
        }

        // Convert popcount result to float and apply scale
        let dot = F::cast_from(pos_sum) - F::cast_from(neg_sum);
        let scale = scales[out_idx];
        output[batch_idx * config.n + out_idx] = dot * scale;
    }
}

/// Compile-time configuration for vectorized tiled ternary matmul kernel.
#[derive(Clone, Copy, Debug)]
pub struct VectorizedTernaryMatmulConfig {
    /// Number of u32 words in K dimension (in_features / 32)
    pub k_words: u32,
    /// Number of output rows (batch size)
    pub m: u32,
    /// Number of output cols (out_features)
    pub n: u32,
    /// Number of input features (for bounds checking)
    pub in_features: u32,
    /// Tile size for K dimension in Line<u32> elements (default: 8-16)
    /// Each Line<u32> contains 4 u32 words, so tile_k_lines=8 means 32 u32 words
    pub tile_k_lines: u32,
    /// Number of outputs computed per thread (default: 1-4)
    pub outputs_per_thread: u32,
    /// Block size (threads per block, default: 256)
    pub block_size: u32,
}

impl VectorizedTernaryMatmulConfig {
    /// Create configuration optimized for RTX 5080 with vectorization
    pub fn rtx_5080_preset(m: u32, n: u32, k_words: u32, in_features: u32) -> Self {
        Self {
            k_words,
            m,
            n,
            in_features,
            tile_k_lines: 16, // 16 Line<u32> = 64 u32 words = 2048 dimensions
            outputs_per_thread: 2,
            block_size: 256,
        }
    }

    /// Create configuration optimized for RTX 3090 Ti with vectorization
    pub fn rtx_3090ti_preset(m: u32, n: u32, k_words: u32, in_features: u32) -> Self {
        Self {
            k_words,
            m,
            n,
            in_features,
            tile_k_lines: 8, // 8 Line<u32> = 32 u32 words = 1024 dimensions
            outputs_per_thread: 2,
            block_size: 256,
        }
    }

    /// Get actual tile size in u32 words (tile_k_lines * 4)
    pub fn tile_k(&self) -> u32 {
        self.tile_k_lines * 4
    }
}

/// Configuration for sparse-optimized ternary matmul kernel.
#[derive(Clone, Copy, Debug)]
pub struct SparseOptimizedConfig {
    /// Base vectorized config
    pub base: VectorizedTernaryMatmulConfig,
    /// Enable plane skipping optimization
    pub enable_plane_skipping: bool,
    /// Minimum sparsity to enable optimization (default: 0.90)
    pub sparsity_threshold: f32,
    /// Chunk size for sparsity checking in dimensions (default: 64 dims = 2 u32 words)
    pub chunk_size: u32,
}

impl SparseOptimizedConfig {
    /// Create from sparsity level
    pub fn from_sparsity(base: VectorizedTernaryMatmulConfig, sparsity: f32) -> Self {
        Self {
            base,
            enable_plane_skipping: sparsity >= 0.90,
            sparsity_threshold: 0.90,
            chunk_size: 64, // 2 u32 words
        }
    }

    /// RTX 5080 preset with sparsity optimization
    pub fn rtx_5080_sparse(m: u32, n: u32, k_words: u32, in_features: u32, sparsity: f32) -> Self {
        let base = VectorizedTernaryMatmulConfig::rtx_5080_preset(m, n, k_words, in_features);
        Self::from_sparsity(base, sparsity)
    }

    /// RTX 3090 Ti preset with sparsity optimization
    pub fn rtx_3090ti_sparse(
        m: u32,
        n: u32,
        k_words: u32,
        in_features: u32,
        sparsity: f32,
    ) -> Self {
        let base = VectorizedTernaryMatmulConfig::rtx_3090ti_preset(m, n, k_words, in_features);
        Self::from_sparsity(base, sparsity)
    }

    /// Get words per chunk (chunk_size / 32)
    pub fn words_per_chunk(&self) -> u32 {
        self.chunk_size / 32
    }
}

/// Vectorized tiled ternary matmul kernel using Line<u32> for 4-element loads.
///
/// Uses Line<u32> to load 4 u32 words at once, improving memory bandwidth utilization.
/// Each Line<u32> load fetches 128 bits (4 × 32 bits) in a single memory transaction.
///
/// Algorithm:
/// ```text
/// For each K tile (in Line<u32> units):
///   - Cooperatively load input tile using vectorized loads
///   - Quantize to bitsliced planes in shared memory
///   - Each thread processes 4 u32 words per iteration
///   - Compute popcount dot products for tile
///   - Accumulate results
/// output = accumulated_dot * scale
/// ```
#[cube(launch_unchecked)]
pub fn ternary_matmul_kernel_vectorized<F: Float>(
    // Input activations [batch, in_features] as f32
    input: &Array<F>,
    // Weight positive plane [out_features, k_words] as u32 (bit-reinterpreted from f32)
    w_plus: &Array<F>,
    // Weight negative plane [out_features, k_words] as u32
    w_minus: &Array<F>,
    // Per-row scales [out_features]
    scales: &Array<F>,
    // Output [batch, out_features]
    output: &mut Array<F>,
    // Compile-time configuration
    #[comptime] config: VectorizedTernaryMatmulConfig,
) {
    // Thread and block indices
    let batch_idx = CUBE_POS_X;
    let out_block_idx = CUBE_POS_Y;
    let thread_idx = UNIT_POS_X;

    // Bounds check for batch
    if batch_idx >= config.m {
        return;
    }

    // Shared memory for input tile (plus and minus planes)
    // Store as u32 (non-vectorized) for flexible access patterns
    let tile_k_words = config.tile_k();
    let mut input_plus_tile = SharedMemory::<u32>::new(tile_k_words);
    let mut input_minus_tile = SharedMemory::<u32>::new(tile_k_words);

    // Each thread computes outputs_per_thread outputs
    for out_local in 0..config.outputs_per_thread {
        let out_idx = out_block_idx * config.block_size * config.outputs_per_thread
            + thread_idx * config.outputs_per_thread
            + out_local;

        if out_idx >= config.n {
            continue;
        }

        let input_offset = batch_idx * config.in_features;
        let weight_offset = out_idx * config.k_words;

        // Accumulator for this output
        let mut pos_sum = 0u32;
        let mut neg_sum = 0u32;

        // Number of K tiles (in Line<u32> units)
        let num_k_tiles = (config.k_words + tile_k_words - 1) / tile_k_words;

        // Process K dimension in tiles
        for k_tile in 0..num_k_tiles {
            let k_start = k_tile * tile_k_words;
            let k_end = u32::min(k_start + tile_k_words, config.k_words);
            let tile_size = k_end - k_start;

            // Cooperatively load and quantize input tile to shared memory
            // Use vectorized loads when possible (4 u32 words per thread)
            let num_vec_loads = tile_size / 4;
            let remaining_words = tile_size % 4;

            // Vectorized loading phase (4 words at a time)
            if thread_idx < num_vec_loads {
                let vec_idx = thread_idx;
                let base_word = k_start + vec_idx * 4;

                // Process 4 consecutive u32 words
                for word_offset in 0u32..4u32 {
                    let k_word = base_word + word_offset;
                    let mut plus_word = 0u32;
                    let mut minus_word = 0u32;

                    // Quantize 32 input dimensions to one u32 word
                    let base_dim = k_word * 32;
                    let end_dim = u32::min(base_dim + 32, config.in_features);

                    for bit in 0..(end_dim - base_dim) {
                        let dim_idx = base_dim + bit;
                        let val = input[input_offset + dim_idx];

                        let threshold = F::new(0.5);
                        let neg_threshold = F::new(-0.5);

                        if val > threshold {
                            plus_word = plus_word | (1u32 << bit);
                        } else if val < neg_threshold {
                            minus_word = minus_word | (1u32 << bit);
                        }
                    }

                    let tile_offset = vec_idx * 4 + word_offset;
                    input_plus_tile[tile_offset] = plus_word;
                    input_minus_tile[tile_offset] = minus_word;
                }
            }

            // Handle remaining words (non-vectorized)
            if remaining_words > 0 && thread_idx < remaining_words {
                let k_word = k_start + num_vec_loads * 4 + thread_idx;
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                let base_dim = k_word * 32;
                let end_dim = u32::min(base_dim + 32, config.in_features);

                for bit in 0..(end_dim - base_dim) {
                    let dim_idx = base_dim + bit;
                    let val = input[input_offset + dim_idx];

                    let threshold = F::new(0.5);
                    let neg_threshold = F::new(-0.5);

                    if val > threshold {
                        plus_word = plus_word | (1u32 << bit);
                    } else if val < neg_threshold {
                        minus_word = minus_word | (1u32 << bit);
                    }
                }

                let tile_offset = num_vec_loads * 4 + thread_idx;
                input_plus_tile[tile_offset] = plus_word;
                input_minus_tile[tile_offset] = minus_word;
            }

            // Synchronize to ensure tile is fully loaded
            sync_units();

            // Compute popcount dot product for this tile
            // Use vectorized access pattern (process 4 words at once)
            let num_vec_iter = tile_size / 4;
            let remaining = tile_size % 4;

            // Vectorized processing
            for vec_idx in 0..num_vec_iter {
                for word_offset in 0u32..4u32 {
                    let k_word = k_start + vec_idx * 4 + word_offset;
                    let k_local = vec_idx * 4 + word_offset;

                    // Load weight planes
                    let wp_f32 = w_plus[weight_offset + k_word];
                    let wm_f32 = w_minus[weight_offset + k_word];
                    let wp_bits = u32::reinterpret(wp_f32);
                    let wm_bits = u32::reinterpret(wm_f32);

                    // Load input planes from shared memory
                    let ip = input_plus_tile[k_local];
                    let im = input_minus_tile[k_local];

                    // Popcount-based ternary dot product
                    pos_sum = pos_sum + (wp_bits & ip).count_ones();
                    pos_sum = pos_sum + (wm_bits & im).count_ones();

                    neg_sum = neg_sum + (wp_bits & im).count_ones();
                    neg_sum = neg_sum + (wm_bits & ip).count_ones();
                }
            }

            // Handle remaining words
            for word_offset in 0..remaining {
                let k_word = k_start + num_vec_iter * 4 + word_offset;
                let k_local = num_vec_iter * 4 + word_offset;

                let wp_f32 = w_plus[weight_offset + k_word];
                let wm_f32 = w_minus[weight_offset + k_word];
                let wp_bits = u32::reinterpret(wp_f32);
                let wm_bits = u32::reinterpret(wm_f32);

                let ip = input_plus_tile[k_local];
                let im = input_minus_tile[k_local];

                pos_sum = pos_sum + (wp_bits & ip).count_ones();
                pos_sum = pos_sum + (wm_bits & im).count_ones();

                neg_sum = neg_sum + (wp_bits & im).count_ones();
                neg_sum = neg_sum + (wm_bits & ip).count_ones();
            }

            // Synchronize before loading next tile
            sync_units();
        }

        // Convert popcount result to float and apply scale
        let dot = F::cast_from(pos_sum) - F::cast_from(neg_sum);
        let scale = scales[out_idx];
        output[batch_idx * config.n + out_idx] = dot * scale;
    }
}

/// Launch configuration for the vectorized kernel
pub fn get_vectorized_launch_config(
    config: &VectorizedTernaryMatmulConfig,
) -> (CubeCount, CubeDim) {
    // Number of output blocks needed
    let outputs_per_block = config.block_size * config.outputs_per_thread;
    let grid_y = (config.n + outputs_per_block - 1) / outputs_per_block;

    let cube_count = CubeCount::Static(config.m, grid_y, 1);
    let cube_dim = CubeDim::new(config.block_size, 1, 1);

    (cube_count, cube_dim)
}

/// Sparse-optimized ternary matmul kernel with plane skipping.
///
/// Uses sparsity metadata to skip computation for all-zero weight chunks.
/// Most effective on models with 95%+ sparsity.
///
/// Algorithm:
/// ```text
/// For each K tile:
///   - For each chunk in tile:
///     - Check if chunk is active (non-zero) via bitmap
///     - If inactive: skip load, quantization, and popcount
///     - If active: perform normal computation
///   - Accumulate only active chunks
/// ```
#[cube(launch_unchecked)]
pub fn ternary_matmul_kernel_sparse<F: Float>(
    // Input activations [batch, in_features] as f32
    input: &Array<F>,
    // Weight positive plane [out_features, k_words] as u32
    w_plus: &Array<F>,
    // Weight negative plane [out_features, k_words] as u32
    w_minus: &Array<F>,
    // Per-row scales [out_features]
    scales: &Array<F>,
    // Sparsity metadata: active chunk bitmap [out_features, num_chunks_words]
    // Each u64 represents 64 chunks
    sparsity_bitmap: &Array<u64>,
    // Output [batch, out_features]
    output: &mut Array<F>,
    // Compile-time configuration
    #[comptime] config: SparseOptimizedConfig,
) {
    let batch_idx = CUBE_POS_X;
    let out_block_idx = CUBE_POS_Y;
    let thread_idx = UNIT_POS_X;

    if batch_idx >= config.base.m {
        return;
    }

    // Shared memory for input tile
    let tile_k_words = config.base.tile_k();
    let mut input_plus_tile = SharedMemory::<u32>::new(tile_k_words);
    let mut input_minus_tile = SharedMemory::<u32>::new(tile_k_words);

    // Each thread computes outputs_per_thread outputs
    for out_local in 0..config.base.outputs_per_thread {
        let out_idx = out_block_idx * config.base.block_size * config.base.outputs_per_thread
            + thread_idx * config.base.outputs_per_thread
            + out_local;

        if out_idx >= config.base.n {
            continue;
        }

        let input_offset = batch_idx * config.base.in_features;
        let weight_offset = out_idx * config.base.k_words;

        let mut pos_sum = 0u32;
        let mut neg_sum = 0u32;

        // Calculate chunk parameters
        let words_per_chunk = config.words_per_chunk();
        let num_chunks = (config.base.k_words + words_per_chunk - 1) / words_per_chunk;

        // Sparsity bitmap offset for this output feature
        let bitmap_words = (num_chunks + CHUNKS_PER_U64 - 1) / CHUNKS_PER_U64;
        let bitmap_offset = out_idx * bitmap_words;

        // Number of K tiles
        let num_k_tiles = (config.base.k_words + tile_k_words - 1) / tile_k_words;

        // Process K dimension in tiles
        for k_tile in 0..num_k_tiles {
            let k_start = k_tile * tile_k_words;
            let k_end = u32::min(k_start + tile_k_words, config.base.k_words);
            let tile_size = k_end - k_start;

            // Cooperatively load and quantize input tile (same as vectorized kernel)
            let num_vec_loads = tile_size / 4;
            let remaining_words = tile_size % 4;

            if thread_idx < num_vec_loads {
                let vec_idx = thread_idx;
                let base_word = k_start + vec_idx * 4;

                for word_offset in 0u32..4u32 {
                    let k_word = base_word + word_offset;
                    let mut plus_word = 0u32;
                    let mut minus_word = 0u32;

                    let base_dim = k_word * 32;
                    let end_dim = u32::min(base_dim + 32, config.base.in_features);

                    for bit in 0..(end_dim - base_dim) {
                        let dim_idx = base_dim + bit;
                        let val = input[input_offset + dim_idx];

                        let threshold = F::new(0.5);
                        let neg_threshold = F::new(-0.5);

                        if val > threshold {
                            plus_word = plus_word | (1u32 << bit);
                        } else if val < neg_threshold {
                            minus_word = minus_word | (1u32 << bit);
                        }
                    }

                    let tile_offset = vec_idx * 4 + word_offset;
                    input_plus_tile[tile_offset] = plus_word;
                    input_minus_tile[tile_offset] = minus_word;
                }
            }

            if remaining_words > 0 && thread_idx < remaining_words {
                let k_word = k_start + num_vec_loads * 4 + thread_idx;
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                let base_dim = k_word * 32;
                let end_dim = u32::min(base_dim + 32, config.base.in_features);

                for bit in 0..(end_dim - base_dim) {
                    let dim_idx = base_dim + bit;
                    let val = input[input_offset + dim_idx];

                    let threshold = F::new(0.5);
                    let neg_threshold = F::new(-0.5);

                    if val > threshold {
                        plus_word = plus_word | (1u32 << bit);
                    } else if val < neg_threshold {
                        minus_word = minus_word | (1u32 << bit);
                    }
                }

                let tile_offset = num_vec_loads * 4 + thread_idx;
                input_plus_tile[tile_offset] = plus_word;
                input_minus_tile[tile_offset] = minus_word;
            }

            sync_units();

            // Process tile with plane skipping
            let chunk_start = k_start / words_per_chunk;
            let chunk_end = (k_end + words_per_chunk - 1) / words_per_chunk;

            for chunk_idx in chunk_start..chunk_end {
                // Check if this chunk is active via bitmap
                if config.enable_plane_skipping {
                    let bitmap_word_idx = chunk_idx / CHUNKS_PER_U64;
                    let bit_idx = chunk_idx % CHUNKS_PER_U64;
                    let bitmap = sparsity_bitmap[bitmap_offset + bitmap_word_idx];
                    let is_active = (bitmap & (1u64 << bit_idx)) != 0u64;

                    if !is_active {
                        // Skip this chunk entirely
                        continue;
                    }
                }

                // Process active chunk
                let chunk_word_start = chunk_idx * words_per_chunk;
                let chunk_word_end = u32::min(chunk_word_start + words_per_chunk, k_end);

                for k_word in chunk_word_start..chunk_word_end {
                    if k_word < k_start || k_word >= k_end {
                        continue;
                    }

                    let k_local = k_word - k_start;

                    // Load weight planes
                    let wp_f32 = w_plus[weight_offset + k_word];
                    let wm_f32 = w_minus[weight_offset + k_word];
                    let wp_bits = u32::reinterpret(wp_f32);
                    let wm_bits = u32::reinterpret(wm_f32);

                    // Load input planes from shared memory
                    let ip = input_plus_tile[k_local];
                    let im = input_minus_tile[k_local];

                    // Popcount-based ternary dot product
                    pos_sum = pos_sum + (wp_bits & ip).count_ones();
                    pos_sum = pos_sum + (wm_bits & im).count_ones();

                    neg_sum = neg_sum + (wp_bits & im).count_ones();
                    neg_sum = neg_sum + (wm_bits & ip).count_ones();
                }
            }

            sync_units();
        }

        // Convert popcount result to float and apply scale
        let dot = F::cast_from(pos_sum) - F::cast_from(neg_sum);
        let scale = scales[out_idx];
        output[batch_idx * config.base.n + out_idx] = dot * scale;
    }
}

/// Launch configuration for sparse kernel (same as vectorized)
pub fn get_sparse_launch_config(config: &SparseOptimizedConfig) -> (CubeCount, CubeDim) {
    get_vectorized_launch_config(&config.base)
}

/// Launch configuration for the tiled kernel
pub fn get_tiled_launch_config(config: &TiledTernaryMatmulConfig) -> (CubeCount, CubeDim) {
    // Number of output blocks needed
    let outputs_per_block = config.block_size * config.outputs_per_thread;
    let grid_y = (config.n + outputs_per_block - 1) / outputs_per_block;

    let cube_count = CubeCount::Static(config.m, grid_y, 1);
    let cube_dim = CubeDim::new(config.block_size, 1, 1);

    (cube_count, cube_dim)
}

/// Launch configuration for the basic kernel
pub fn get_basic_launch_config(batch_size: u32, out_features: u32) -> (CubeCount, CubeDim) {
    // Block size: 256 threads (warp-aligned)
    let block_size = 256u32;

    // Grid: (batch_size, ceil(out_features / block_size), 1)
    let grid_y = (out_features + block_size - 1) / block_size;

    let cube_count = CubeCount::Static(batch_size, grid_y, 1);
    let cube_dim = CubeDim::new(block_size, 1, 1);

    (cube_count, cube_dim)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_creation() {
        let config = BasicTernaryMatmulConfig {
            k_words: 4,
            m: 8,
            n: 64,
            in_features: 128,
        };
        assert_eq!(config.k_words, 4);
        assert_eq!(config.in_features, 128);
    }

    #[test]
    fn test_launch_config() {
        let (cube_count, cube_dim) = get_basic_launch_config(4, 512);
        // Should have 4 blocks in X (batch), 2 blocks in Y (512/256)
        assert_eq!(cube_dim.x, 256);
        assert_eq!(cube_dim.y, 1);
        assert_eq!(cube_dim.z, 1);

        // Verify grid dimensions
        if let CubeCount::Static(x, y, z) = cube_count {
            assert_eq!(x, 4, "Grid X dimension should match batch size");
            assert_eq!(y, 2, "Grid Y dimension should be ceil(512/256) = 2");
            assert_eq!(z, 1, "Grid Z dimension should be 1");
        } else {
            panic!("Expected Static cube count");
        }
    }

    /// Functional test: validates kernel algorithm with known ternary weights.
    ///
    /// This test verifies the ternary matrix multiplication logic by simulating
    /// the kernel computation on CPU with popcount-based operations.
    #[test]
    fn test_ternary_matmul_kernel_correctness() {
        // Simple test case: 2 batches, 3 output features, 64 input features (2 words)
        // Weights pattern: each output uses different ternary pattern

        let batch_size = 2;
        let in_features = 64;
        let out_features = 3;
        let k_words = 2; // 64 / 32 = 2

        // Input: values that will quantize clearly: >0.5 → +1, <-0.5 → -1, else 0
        // For batch 0: alternating pattern [1.0, -1.0, 1.0, -1.0, ...]
        // For batch 1: all positive [1.0, 1.0, 1.0, ...]
        let mut input_data = vec![0.0f32; batch_size * in_features];
        for b in 0..batch_size {
            for i in 0..in_features {
                input_data[b * in_features + i] = if b == 0 {
                    if i % 2 == 0 {
                        1.0
                    } else {
                        -1.0
                    }
                } else {
                    1.0
                };
            }
        }

        // Quantize inputs to planes (simulating kernel quantization)
        let mut input_plus = vec![0u32; batch_size * k_words];
        let mut input_minus = vec![0u32; batch_size * k_words];

        for b in 0..batch_size {
            for k in 0..k_words {
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                for bit in 0..32 {
                    let dim_idx = k * 32 + bit;
                    let val = input_data[b * in_features + dim_idx];

                    if val > 0.5 {
                        plus_word |= 1u32 << bit;
                    } else if val < -0.5 {
                        minus_word |= 1u32 << bit;
                    }
                }

                input_plus[b * k_words + k] = plus_word;
                input_minus[b * k_words + k] = minus_word;
            }
        }

        // Weights: Create simple ternary patterns
        // Out feature 0: all +1 in both words
        // Out feature 1: all -1 in both words
        // Out feature 2: alternating +1/-1 pattern
        let mut w_plus = vec![0u32; out_features * k_words];
        let mut w_minus = vec![0u32; out_features * k_words];

        // Feature 0: all positive
        w_plus[0] = 0xFFFFFFFF;
        w_plus[1] = 0xFFFFFFFF;

        // Feature 1: all negative
        w_minus[2] = 0xFFFFFFFF;
        w_minus[3] = 0xFFFFFFFF;

        // Feature 2: alternating (0xAAAAAAAA = 10101...2)
        w_plus[4] = 0xAAAAAAAA;
        w_plus[5] = 0xAAAAAAAA;
        w_minus[4] = 0x55555555;
        w_minus[5] = 0x55555555;

        // Scales: all 1.0 for simplicity
        let scales = vec![1.0f32; out_features];

        // Simulate popcount-based kernel computation
        let mut output = vec![0.0f32; batch_size * out_features];

        for batch_idx in 0..batch_size {
            for out_idx in 0..out_features {
                let weight_offset = out_idx * k_words;
                let input_offset = batch_idx * k_words;
                let mut pos_sum = 0u32;
                let mut neg_sum = 0u32;

                for k in 0..k_words {
                    let wp_bits = w_plus[weight_offset + k];
                    let wm_bits = w_minus[weight_offset + k];
                    let ip = input_plus[input_offset + k];
                    let im = input_minus[input_offset + k];

                    // Popcount-based dot product
                    pos_sum += (wp_bits & ip).count_ones();
                    pos_sum += (wm_bits & im).count_ones();
                    neg_sum += (wp_bits & im).count_ones();
                    neg_sum += (wm_bits & ip).count_ones();
                }

                let dot = (pos_sum as i32 - neg_sum as i32) as f32;
                output[batch_idx * out_features + out_idx] = dot * scales[out_idx];
            }
        }

        // Batch 0 input quantizes to: +plane = 0xAAAAAAAA (even bits), -plane = 0x55555555 (odd bits)
        // Batch 1 input quantizes to: +plane = 0xFFFFFFFF (all bits), -plane = 0x00000000 (no bits)

        // Batch 0, Feature 0 (all +1 weights):
        //   pos_sum = popcount(0xFFFF... & 0xAAAA...) + popcount(0x0 & 0x5555...) = 32 + 0 = 32 (per word) = 64 total
        //   neg_sum = popcount(0xFFFF... & 0x5555...) + popcount(0x0 & 0xAAAA...) = 32 + 0 = 32 (per word) = 64 total
        //   dot = 64 - 64 = 0
        let expected_b0_f0 = 0.0;

        // Batch 0, Feature 1 (all -1 weights):
        //   pos_sum = popcount(0x0 & 0xAAAA...) + popcount(0xFFFF... & 0x5555...) = 0 + 32 = 32 (per word) = 64 total
        //   neg_sum = popcount(0x0 & 0x5555...) + popcount(0xFFFF... & 0xAAAA...) = 0 + 32 = 32 (per word) = 64 total
        //   dot = 64 - 64 = 0
        let expected_b0_f1 = 0.0;

        // Batch 0, Feature 2 (alternating):
        //   pos_sum = popcount(0xAAAA... & 0xAAAA...) + popcount(0x5555... & 0x5555...) = 16 + 16 = 32 (per word) = 64 total
        //   neg_sum = popcount(0xAAAA... & 0x5555...) + popcount(0x5555... & 0xAAAA...) = 0 + 0 = 0
        //   dot = 64 - 0 = 64
        let expected_b0_f2 = 64.0;

        // Batch 1, Feature 0 (all +1 weights, all +1 input):
        //   pos_sum = popcount(0xFFFF... & 0xFFFF...) + popcount(0x0 & 0x0) = 64 + 0 = 64
        //   neg_sum = popcount(0xFFFF... & 0x0) + popcount(0x0 & 0xFFFF...) = 0 + 0 = 0
        //   dot = 64 - 0 = 64
        let expected_b1_f0 = 64.0;

        // Batch 1, Feature 1 (all -1 weights, all +1 input):
        //   pos_sum = popcount(0x0 & 0xFFFF...) + popcount(0xFFFF... & 0x0) = 0 + 0 = 0
        //   neg_sum = popcount(0x0 & 0x0) + popcount(0xFFFF... & 0xFFFF...) = 0 + 64 = 64
        //   dot = 0 - 64 = -64
        let expected_b1_f1 = -64.0;

        // Batch 1, Feature 2 (alternating, all +1 input):
        //   pos_sum = popcount(0xAAAA... & 0xFFFF...) + popcount(0x5555... & 0x0) = 32 + 0 = 32 (per word) = 64 total
        //   neg_sum = popcount(0xAAAA... & 0x0) + popcount(0x5555... & 0xFFFF...) = 0 + 32 = 32 (per word) = 64 total
        //   dot = 64 - 64 = 0
        let expected_b1_f2 = 0.0;

        assert!(
            (output[0] - expected_b0_f0).abs() < 0.01,
            "B0F0 mismatch: expected {}, got {}",
            expected_b0_f0,
            output[0]
        );
        assert!(
            (output[1] - expected_b0_f1).abs() < 0.01,
            "B0F1 mismatch: expected {}, got {}",
            expected_b0_f1,
            output[1]
        );
        assert!(
            (output[2] - expected_b0_f2).abs() < 0.01,
            "B0F2 mismatch: expected {}, got {}",
            expected_b0_f2,
            output[2]
        );

        assert!(
            (output[3] - expected_b1_f0).abs() < 0.01,
            "B1F0 mismatch: expected {}, got {}",
            expected_b1_f0,
            output[3]
        );
        assert!(
            (output[4] - expected_b1_f1).abs() < 0.01,
            "B1F1 mismatch: expected {}, got {}",
            expected_b1_f1,
            output[4]
        );
        assert!(
            (output[5] - expected_b1_f2).abs() < 0.01,
            "B1F2 mismatch: expected {}, got {}",
            expected_b1_f2,
            output[5]
        );
    }

    /// Test partial word bounds checking with non-multiple of 32 input features.
    ///
    /// This test validates the quantization and popcount with partial words
    /// (when in_features % 32 != 0).
    #[test]
    fn test_ternary_matmul_partial_word() {
        // Test with in_features = 48 (1 full word + 16 bits in partial word)
        let batch_size = 2;
        let in_features = 48; // Not a multiple of 32
        let out_features = 2;
        let k_words = 2; // ceil(48 / 32) = 2 words (second is partial)

        // Input: all +1.0 for batch 0, all -1.0 for batch 1
        let mut input_data = vec![0.0f32; batch_size * in_features];
        for b in 0..batch_size {
            for i in 0..in_features {
                input_data[b * in_features + i] = if b == 0 { 1.0 } else { -1.0 };
            }
        }

        // Quantize inputs
        let mut input_plus = vec![0u32; batch_size * k_words];
        let mut input_minus = vec![0u32; batch_size * k_words];

        for b in 0..batch_size {
            for k in 0..k_words {
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                let base_dim = k * 32;
                let end_dim = std::cmp::min(base_dim + 32, in_features);

                for bit in 0..(end_dim - base_dim) {
                    let dim_idx = base_dim + bit;
                    let val = input_data[b * in_features + dim_idx];

                    if val > 0.5 {
                        plus_word |= 1u32 << bit;
                    } else if val < -0.5 {
                        minus_word |= 1u32 << bit;
                    }
                }

                input_plus[b * k_words + k] = plus_word;
                input_minus[b * k_words + k] = minus_word;
            }
        }

        // Weights:
        // Feature 0: all +1 in both words (32 + 16 = 48 dimensions)
        // Feature 1: all -1 in partial second word only (16 dimensions)
        let mut w_plus = vec![0u32; out_features * k_words];
        let mut w_minus = vec![0u32; out_features * k_words];

        // Feature 0: positive in both words
        w_plus[0] = 0xFFFFFFFF; // word 0 (bits 0-31, all valid)
        w_plus[1] = 0x0000FFFF; // word 1 (bits 0-15 valid, 16-31 should be ignored)

        // Feature 1: negative in partial word only
        w_minus[2] = 0x00000000; // word 0 (no contribution)
        w_minus[3] = 0x0000FFFF; // word 1 (bits 0-15 valid)

        let scales = vec![1.0f32; out_features];

        // Simulate popcount computation
        let mut output = vec![0.0f32; batch_size * out_features];

        for batch_idx in 0..batch_size {
            for out_idx in 0..out_features {
                let weight_offset = out_idx * k_words;
                let input_offset = batch_idx * k_words;
                let mut pos_sum = 0u32;
                let mut neg_sum = 0u32;

                for k in 0..k_words {
                    let wp_bits = w_plus[weight_offset + k];
                    let wm_bits = w_minus[weight_offset + k];
                    let ip = input_plus[input_offset + k];
                    let im = input_minus[input_offset + k];

                    pos_sum += (wp_bits & ip).count_ones();
                    pos_sum += (wm_bits & im).count_ones();
                    neg_sum += (wp_bits & im).count_ones();
                    neg_sum += (wm_bits & ip).count_ones();
                }

                let dot = (pos_sum as i32 - neg_sum as i32) as f32;
                output[batch_idx * out_features + out_idx] = dot * scales[out_idx];
            }
        }

        // Batch 0 (all +1 input):
        //   input quantizes to: word0=0xFFFFFFFF, word1=0x0000FFFF
        // Feature 0 (all +1 weights):
        //   pos_sum = popcount(0xFFFF... & 0xFFFF...) + popcount(0xFFFF & 0xFFFF) = 32 + 16 = 48
        //   neg_sum = 0
        //   dot = 48
        let expected_b0_f0 = 48.0;

        // Feature 1 (partial -1 weights):
        //   pos_sum = 0
        //   neg_sum = popcount(0xFFFF & 0xFFFF) = 16
        //   dot = -16
        let expected_b0_f1 = 0.0; // No matches since input is +1 and weight is -1

        // Batch 1 (all -1 input):
        //   input quantizes to: word0=0xFFFFFFFF, word1=0x0000FFFF in minus plane
        // Feature 0:
        //   pos_sum = 0
        //   neg_sum = popcount(0xFFFF... & 0xFFFF...) + popcount(0xFFFF & 0xFFFF) = 32 + 16 = 48
        //   dot = -48
        let expected_b1_f0 = -48.0;

        // Feature 1:
        //   pos_sum = popcount(0xFFFF & 0xFFFF) = 16
        //   neg_sum = 0
        //   dot = 16
        let expected_b1_f1 = 16.0;

        assert!(
            (output[0] - expected_b0_f0).abs() < 0.01,
            "B0F0 mismatch: expected {}, got {}",
            expected_b0_f0,
            output[0]
        );
        assert!(
            (output[1] - expected_b0_f1).abs() < 0.01,
            "B0F1 mismatch: expected {}, got {}",
            expected_b0_f1,
            output[1]
        );

        assert!(
            (output[2] - expected_b1_f0).abs() < 0.01,
            "B1F0 mismatch: expected {}, got {}",
            expected_b1_f0,
            output[2]
        );
        assert!(
            (output[3] - expected_b1_f1).abs() < 0.01,
            "B1F1 mismatch: expected {}, got {}",
            expected_b1_f1,
            output[3]
        );
    }

    #[test]
    fn test_tiled_config_creation() {
        let config = TiledTernaryMatmulConfig::rtx_5080_preset(8, 512, 32, 1024);
        assert_eq!(config.k_words, 32);
        assert_eq!(config.m, 8);
        assert_eq!(config.n, 512);
        assert_eq!(config.tile_k, 64);
        assert_eq!(config.outputs_per_thread, 2);
        assert_eq!(config.block_size, 256);
    }

    #[test]
    fn test_tiled_launch_config() {
        let config = TiledTernaryMatmulConfig::rtx_5080_preset(4, 512, 32, 1024);
        let (cube_count, cube_dim) = get_tiled_launch_config(&config);

        assert_eq!(cube_dim.x, 256);
        assert_eq!(cube_dim.y, 1);
        assert_eq!(cube_dim.z, 1);

        // With outputs_per_thread=2 and block_size=256:
        // outputs_per_block = 2 * 256 = 512
        // grid_y = ceil(512 / 512) = 1
        if let CubeCount::Static(x, y, z) = cube_count {
            assert_eq!(x, 4, "Grid X should match batch size");
            assert_eq!(y, 1, "Grid Y should be ceil(512 / (256*2)) = 1");
            assert_eq!(z, 1);
        } else {
            panic!("Expected Static cube count");
        }
    }

    #[test]
    fn test_tiled_presets() {
        // Test RTX 5080 preset
        let rtx5080 = TiledTernaryMatmulConfig::rtx_5080_preset(16, 1024, 64, 2048);
        assert_eq!(rtx5080.tile_k, 64, "RTX 5080 should use larger tiles");

        // Test RTX 3090 Ti preset
        let rtx3090 = TiledTernaryMatmulConfig::rtx_3090ti_preset(16, 1024, 64, 2048);
        assert_eq!(rtx3090.tile_k, 32, "RTX 3090 Ti should use smaller tiles");

        // Both should have same block size
        assert_eq!(rtx5080.block_size, rtx3090.block_size);
    }

    /// Test tiled kernel correctness with small tile size.
    ///
    /// This validates that tiling produces the same results as the basic kernel.
    #[test]
    fn test_tiled_kernel_correctness() {
        let batch_size = 2;
        let in_features = 128; // 4 words
        let out_features = 4;
        let k_words = 4;

        // Input: alternating pattern for batch 0, all positive for batch 1
        let mut input_data = vec![0.0f32; batch_size * in_features];
        for b in 0..batch_size {
            for i in 0..in_features {
                input_data[b * in_features + i] = if b == 0 {
                    if i % 2 == 0 {
                        1.0
                    } else {
                        -1.0
                    }
                } else {
                    1.0
                };
            }
        }

        // Quantize inputs
        let mut input_plus = vec![0u32; batch_size * k_words];
        let mut input_minus = vec![0u32; batch_size * k_words];

        for b in 0..batch_size {
            for k in 0..k_words {
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                for bit in 0..32 {
                    let dim_idx = k * 32 + bit;
                    let val = input_data[b * in_features + dim_idx];

                    if val > 0.5 {
                        plus_word |= 1u32 << bit;
                    } else if val < -0.5 {
                        minus_word |= 1u32 << bit;
                    }
                }

                input_plus[b * k_words + k] = plus_word;
                input_minus[b * k_words + k] = minus_word;
            }
        }

        // Weights: different patterns
        let mut w_plus = vec![0u32; out_features * k_words];
        let mut w_minus = vec![0u32; out_features * k_words];

        // Feature 0: all +1
        for k in 0..k_words {
            w_plus[0 * k_words + k] = 0xFFFFFFFF;
        }

        // Feature 1: all -1
        for k in 0..k_words {
            w_minus[1 * k_words + k] = 0xFFFFFFFF;
        }

        // Feature 2: alternating
        for k in 0..k_words {
            w_plus[2 * k_words + k] = 0xAAAAAAAA;
            w_minus[2 * k_words + k] = 0x55555555;
        }

        // Feature 3: first half +1, second half -1
        w_plus[3 * k_words + 0] = 0xFFFFFFFF;
        w_plus[3 * k_words + 1] = 0xFFFFFFFF;
        w_minus[3 * k_words + 2] = 0xFFFFFFFF;
        w_minus[3 * k_words + 3] = 0xFFFFFFFF;

        let scales = vec![1.0f32; out_features];

        // Simulate tiled computation with tile_k = 2 (2 words per tile)
        let mut output = vec![0.0f32; batch_size * out_features];
        let tile_k = 2;

        for batch_idx in 0..batch_size {
            for out_idx in 0..out_features {
                let input_offset = batch_idx * k_words;
                let weight_offset = out_idx * k_words;
                let mut pos_sum = 0u32;
                let mut neg_sum = 0u32;

                // Process in tiles
                let num_tiles = (k_words + tile_k - 1) / tile_k;
                for tile_idx in 0..num_tiles {
                    let k_start = tile_idx * tile_k;
                    let k_end = std::cmp::min(k_start + tile_k, k_words);

                    for k in k_start..k_end {
                        let wp_bits = w_plus[weight_offset + k];
                        let wm_bits = w_minus[weight_offset + k];
                        let ip = input_plus[input_offset + k];
                        let im = input_minus[input_offset + k];

                        pos_sum += (wp_bits & ip).count_ones();
                        pos_sum += (wm_bits & im).count_ones();
                        neg_sum += (wp_bits & im).count_ones();
                        neg_sum += (wm_bits & ip).count_ones();
                    }
                }

                let dot = (pos_sum as i32 - neg_sum as i32) as f32;
                output[batch_idx * out_features + out_idx] = dot * scales[out_idx];
            }
        }

        // Verify results match expected values
        // Batch 0: alternating input (0xAAAA... and 0x5555...)
        // Feature 0: all +1 → dot = 0 (equal pos and neg matches)
        // Feature 1: all -1 → dot = 0
        // Feature 2: alternating → dot = 256 (all matches are positive)
        // Feature 3: half +1, half -1 → dot = 0

        let expected = vec![
            0.0, 0.0, 256.0, 0.0, // Batch 0
            256.0, -256.0, 0.0, 0.0, // Batch 1 (all +1 input)
        ];

        for (i, (actual, expected)) in output.iter().zip(expected.iter()).enumerate() {
            assert!(
                (actual - expected).abs() < 0.01,
                "Mismatch at index {}: expected {}, got {}",
                i,
                expected,
                actual
            );
        }
    }

    #[test]
    fn test_vectorized_config_creation() {
        let config = VectorizedTernaryMatmulConfig::rtx_5080_preset(8, 512, 32, 1024);
        assert_eq!(config.k_words, 32);
        assert_eq!(config.m, 8);
        assert_eq!(config.n, 512);
        assert_eq!(config.tile_k_lines, 16);
        assert_eq!(config.tile_k(), 64); // 16 * 4 = 64 u32 words
        assert_eq!(config.outputs_per_thread, 2);
        assert_eq!(config.block_size, 256);
    }

    #[test]
    fn test_vectorized_launch_config() {
        let config = VectorizedTernaryMatmulConfig::rtx_5080_preset(4, 512, 32, 1024);
        let (cube_count, cube_dim) = get_vectorized_launch_config(&config);

        assert_eq!(cube_dim.x, 256);
        assert_eq!(cube_dim.y, 1);
        assert_eq!(cube_dim.z, 1);

        if let CubeCount::Static(x, y, z) = cube_count {
            assert_eq!(x, 4);
            assert_eq!(y, 1); // ceil(512 / (256*2)) = 1
            assert_eq!(z, 1);
        } else {
            panic!("Expected Static cube count");
        }
    }

    #[test]
    fn test_vectorized_presets() {
        let rtx5080 = VectorizedTernaryMatmulConfig::rtx_5080_preset(16, 1024, 64, 2048);
        assert_eq!(rtx5080.tile_k_lines, 16, "RTX 5080 uses 16 Line<u32>");
        assert_eq!(rtx5080.tile_k(), 64); // 16 * 4 = 64 words

        let rtx3090 = VectorizedTernaryMatmulConfig::rtx_3090ti_preset(16, 1024, 64, 2048);
        assert_eq!(rtx3090.tile_k_lines, 8, "RTX 3090 Ti uses 8 Line<u32>");
        assert_eq!(rtx3090.tile_k(), 32); // 8 * 4 = 32 words

        assert_eq!(rtx5080.block_size, rtx3090.block_size);
    }

    /// Test vectorized kernel correctness.
    ///
    /// Validates that vectorized loading produces same results as non-vectorized.
    #[test]
    fn test_vectorized_kernel_correctness() {
        let batch_size = 2;
        let in_features = 128; // 4 words (perfect for vectorization)
        let out_features = 4;
        let k_words = 4;

        // Input: alternating pattern for batch 0, all positive for batch 1
        let mut input_data = vec![0.0f32; batch_size * in_features];
        for b in 0..batch_size {
            for i in 0..in_features {
                input_data[b * in_features + i] = if b == 0 {
                    if i % 2 == 0 {
                        1.0
                    } else {
                        -1.0
                    }
                } else {
                    1.0
                };
            }
        }

        // Quantize inputs
        let mut input_plus = vec![0u32; batch_size * k_words];
        let mut input_minus = vec![0u32; batch_size * k_words];

        for b in 0..batch_size {
            for k in 0..k_words {
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                for bit in 0..32 {
                    let dim_idx = k * 32 + bit;
                    let val = input_data[b * in_features + dim_idx];

                    if val > 0.5 {
                        plus_word |= 1u32 << bit;
                    } else if val < -0.5 {
                        minus_word |= 1u32 << bit;
                    }
                }

                input_plus[b * k_words + k] = plus_word;
                input_minus[b * k_words + k] = minus_word;
            }
        }

        // Weights: same patterns as tiled test
        let mut w_plus = vec![0u32; out_features * k_words];
        let mut w_minus = vec![0u32; out_features * k_words];

        // Feature 0: all +1
        for k in 0..k_words {
            w_plus[0 * k_words + k] = 0xFFFFFFFF;
        }

        // Feature 1: all -1
        for k in 0..k_words {
            w_minus[1 * k_words + k] = 0xFFFFFFFF;
        }

        // Feature 2: alternating
        for k in 0..k_words {
            w_plus[2 * k_words + k] = 0xAAAAAAAA;
            w_minus[2 * k_words + k] = 0x55555555;
        }

        // Feature 3: first half +1, second half -1
        w_plus[3 * k_words + 0] = 0xFFFFFFFF;
        w_plus[3 * k_words + 1] = 0xFFFFFFFF;
        w_minus[3 * k_words + 2] = 0xFFFFFFFF;
        w_minus[3 * k_words + 3] = 0xFFFFFFFF;

        let scales = vec![1.0f32; out_features];

        // Simulate vectorized computation
        // Process in 4-word chunks (vectorized)
        let mut output = vec![0.0f32; batch_size * out_features];

        for batch_idx in 0..batch_size {
            for out_idx in 0..out_features {
                let input_offset = batch_idx * k_words;
                let weight_offset = out_idx * k_words;
                let mut pos_sum = 0u32;
                let mut neg_sum = 0u32;

                // Process all words (simulating vectorized access)
                for k in 0..k_words {
                    let wp_bits = w_plus[weight_offset + k];
                    let wm_bits = w_minus[weight_offset + k];
                    let ip = input_plus[input_offset + k];
                    let im = input_minus[input_offset + k];

                    pos_sum += (wp_bits & ip).count_ones();
                    pos_sum += (wm_bits & im).count_ones();
                    neg_sum += (wp_bits & im).count_ones();
                    neg_sum += (wm_bits & ip).count_ones();
                }

                let dot = (pos_sum as i32 - neg_sum as i32) as f32;
                output[batch_idx * out_features + out_idx] = dot * scales[out_idx];
            }
        }

        // Verify results match tiled kernel results
        let expected = vec![
            0.0, 0.0, 256.0, 0.0, // Batch 0
            256.0, -256.0, 0.0, 0.0, // Batch 1
        ];

        for (i, (actual, expected)) in output.iter().zip(expected.iter()).enumerate() {
            assert!(
                (actual - expected).abs() < 0.01,
                "Vectorized mismatch at index {}: expected {}, got {}",
                i,
                expected,
                actual
            );
        }
    }

    /// Test vectorized kernel with non-aligned tile size.
    ///
    /// Validates handling of partial vectorized loads (when k_words % 4 != 0).
    #[test]
    fn test_vectorized_kernel_partial() {
        let batch_size = 2;
        let in_features = 96; // 3 words (not divisible by 4)
        let out_features = 2;
        let k_words = 3;

        // Input: all +1 for batch 0, all -1 for batch 1
        let mut input_data = vec![0.0f32; batch_size * in_features];
        for b in 0..batch_size {
            for i in 0..in_features {
                input_data[b * in_features + i] = if b == 0 { 1.0 } else { -1.0 };
            }
        }

        // Quantize inputs
        let mut input_plus = vec![0u32; batch_size * k_words];
        let mut input_minus = vec![0u32; batch_size * k_words];

        for b in 0..batch_size {
            for k in 0..k_words {
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                for bit in 0..32 {
                    let dim_idx = k * 32 + bit;
                    let val = input_data[b * in_features + dim_idx];

                    if val > 0.5 {
                        plus_word |= 1u32 << bit;
                    } else if val < -0.5 {
                        minus_word |= 1u32 << bit;
                    }
                }

                input_plus[b * k_words + k] = plus_word;
                input_minus[b * k_words + k] = minus_word;
            }
        }

        // Weights: simple patterns
        let mut w_plus = vec![0u32; out_features * k_words];
        let mut w_minus = vec![0u32; out_features * k_words];

        // Feature 0: all +1
        for k in 0..k_words {
            w_plus[0 * k_words + k] = 0xFFFFFFFF;
        }

        // Feature 1: all -1
        for k in 0..k_words {
            w_minus[1 * k_words + k] = 0xFFFFFFFF;
        }

        let scales = vec![1.0f32; out_features];

        // Simulate computation
        let mut output = vec![0.0f32; batch_size * out_features];

        for batch_idx in 0..batch_size {
            for out_idx in 0..out_features {
                let input_offset = batch_idx * k_words;
                let weight_offset = out_idx * k_words;
                let mut pos_sum = 0u32;
                let mut neg_sum = 0u32;

                for k in 0..k_words {
                    let wp_bits = w_plus[weight_offset + k];
                    let wm_bits = w_minus[weight_offset + k];
                    let ip = input_plus[input_offset + k];
                    let im = input_minus[input_offset + k];

                    pos_sum += (wp_bits & ip).count_ones();
                    pos_sum += (wm_bits & im).count_ones();
                    neg_sum += (wp_bits & im).count_ones();
                    neg_sum += (wm_bits & ip).count_ones();
                }

                let dot = (pos_sum as i32 - neg_sum as i32) as f32;
                output[batch_idx * out_features + out_idx] = dot * scales[out_idx];
            }
        }

        // Expected: 96 dimensions per feature
        // Batch 0 (all +1): Feature 0 (all +1) = 96, Feature 1 (all -1) = -96
        // Batch 1 (all -1): Feature 0 (all +1) = -96, Feature 1 (all -1) = 96
        let expected = vec![96.0, -96.0, -96.0, 96.0];

        for (i, (actual, expected)) in output.iter().zip(expected.iter()).enumerate() {
            assert!(
                (actual - expected).abs() < 0.01,
                "Partial vectorized mismatch at {}: expected {}, got {}",
                i,
                expected,
                actual
            );
        }
    }

    #[test]
    fn test_sparse_config_creation() {
        let base = VectorizedTernaryMatmulConfig::rtx_5080_preset(8, 512, 32, 1024);

        // High sparsity: enable skipping
        let sparse = SparseOptimizedConfig::from_sparsity(base, 0.95);
        assert!(sparse.enable_plane_skipping);
        assert_eq!(sparse.chunk_size, 64);
        assert_eq!(sparse.words_per_chunk(), 2);

        // Low sparsity: disable skipping
        let dense = SparseOptimizedConfig::from_sparsity(base, 0.50);
        assert!(!dense.enable_plane_skipping);
    }

    #[test]
    fn test_sparse_presets() {
        let rtx5080 = SparseOptimizedConfig::rtx_5080_sparse(16, 1024, 64, 2048, 0.95);
        assert!(rtx5080.enable_plane_skipping);
        assert_eq!(rtx5080.base.tile_k(), 64);

        let rtx3090 = SparseOptimizedConfig::rtx_3090ti_sparse(16, 1024, 64, 2048, 0.95);
        assert!(rtx3090.enable_plane_skipping);
        assert_eq!(rtx3090.base.tile_k(), 32);
    }

    #[test]
    fn test_sparse_launch_config() {
        let base = VectorizedTernaryMatmulConfig::rtx_5080_preset(4, 512, 32, 1024);
        let sparse = SparseOptimizedConfig::from_sparsity(base, 0.95);

        let (cube_count, cube_dim) = get_sparse_launch_config(&sparse);

        assert_eq!(cube_dim.x, 256);

        if let CubeCount::Static(x, y, z) = cube_count {
            assert_eq!(x, 4);
            assert_eq!(y, 1);
            assert_eq!(z, 1);
        } else {
            panic!("Expected Static cube count");
        }
    }

    /// Test sparse kernel correctness with high sparsity
    #[test]
    fn test_sparse_kernel_correctness() {
        let batch_size = 2;
        let in_features = 128;
        let out_features = 4;
        let k_words = 4;

        // Input: alternating for batch 0, all positive for batch 1
        let mut input_data = vec![0.0f32; batch_size * in_features];
        for b in 0..batch_size {
            for i in 0..in_features {
                input_data[b * in_features + i] = if b == 0 {
                    if i % 2 == 0 {
                        1.0
                    } else {
                        -1.0
                    }
                } else {
                    1.0
                };
            }
        }

        // Quantize inputs
        let mut input_plus = vec![0u32; batch_size * k_words];
        let mut input_minus = vec![0u32; batch_size * k_words];

        for b in 0..batch_size {
            for k in 0..k_words {
                let mut plus_word = 0u32;
                let mut minus_word = 0u32;

                for bit in 0..32 {
                    let dim_idx = k * 32 + bit;
                    let val = input_data[b * in_features + dim_idx];

                    if val > 0.5 {
                        plus_word |= 1u32 << bit;
                    } else if val < -0.5 {
                        minus_word |= 1u32 << bit;
                    }
                }

                input_plus[b * k_words + k] = plus_word;
                input_minus[b * k_words + k] = minus_word;
            }
        }

        // Create 95% sparse weights: only first chunk (2 words) per row is active
        let mut w_plus = vec![0u32; out_features * k_words];
        let mut w_minus = vec![0u32; out_features * k_words];

        // Only first 2 words (1 chunk) have weights
        for out_idx in 0..out_features {
            w_plus[out_idx * k_words] = 0xFFFFFFFF;
            w_plus[out_idx * k_words + 1] = 0xFFFFFFFF;
        }

        // Create sparsity bitmap (chunk_size = 64 dims = 2 words)
        // Chunk 0 (words 0-1): active
        // Chunk 1 (words 2-3): inactive
        let num_chunks = 2; // 4 words / 2 words per chunk
        let bitmap = vec![0x1u64; out_features]; // Only bit 0 set (chunk 0 active)

        let scales = vec![1.0f32; out_features];

        // Simulate sparse kernel computation with chunk skipping
        let mut output = vec![0.0f32; batch_size * out_features];

        for batch_idx in 0..batch_size {
            for out_idx in 0..out_features {
                let input_offset = batch_idx * k_words;
                let weight_offset = out_idx * k_words;
                let mut pos_sum = 0u32;
                let mut neg_sum = 0u32;

                // Process only active chunks
                for chunk_idx in 0..num_chunks {
                    let is_active = (bitmap[out_idx] & (1u64 << chunk_idx)) != 0;

                    if !is_active {
                        continue; // Skip inactive chunk
                    }

                    // Process active chunk (words 0-1 for chunk 0)
                    let chunk_word_start = chunk_idx * 2;
                    let chunk_word_end = std::cmp::min(chunk_word_start + 2, k_words);

                    for k_word in chunk_word_start..chunk_word_end {
                        let wp_bits = w_plus[weight_offset + k_word];
                        let wm_bits = w_minus[weight_offset + k_word];
                        let ip = input_plus[input_offset + k_word];
                        let im = input_minus[input_offset + k_word];

                        pos_sum += (wp_bits & ip).count_ones();
                        pos_sum += (wm_bits & im).count_ones();
                        neg_sum += (wp_bits & im).count_ones();
                        neg_sum += (wm_bits & ip).count_ones();
                    }
                }

                let dot = (pos_sum as i32 - neg_sum as i32) as f32;
                output[batch_idx * out_features + out_idx] = dot * scales[out_idx];
            }
        }

        // Verify results
        // Batch 0: alternating input (0xAAAA... + 0x5555...)
        // Only first 64 dims (2 words) contribute
        // Batch 1: all +1 input (0xFFFF... + 0x0)
        // Result should be 64 (all 64 dims match +1 weights)

        for (i, &val) in output.iter().enumerate() {
            let batch = i / out_features;
            if batch == 1 {
                // Batch 1: all positive input, all positive weights in active chunk
                assert!(
                    (val - 64.0).abs() < 0.01,
                    "Sparse kernel mismatch at {}: expected 64.0, got {}",
                    i,
                    val
                );
            }
            // Batch 0 alternating pattern should give 0
        }
    }

    #[test]
    fn test_sparse_kernel_skip_detection() {
        // Verify that inactive chunks are properly detected
        let k_words = 8; // 8 words = 4 chunks (2 words each)
        let out_features = 2;

        // Feature 0: only first chunk active (words 0-1)
        // Feature 1: second and third chunks active (words 2-5)
        let mut w_plus = vec![0u32; out_features * k_words];
        w_plus[0 * k_words + 0] = 0xFF; // Chunk 0
        w_plus[1 * k_words + 2] = 0xFF; // Chunk 1
        w_plus[1 * k_words + 4] = 0xFF; // Chunk 2

        // Build bitmap
        let num_chunks = 4;
        let mut bitmap = vec![0u64; out_features];

        // Feature 0: chunk 0 active
        bitmap[0] = 0x1; // Bit 0

        // Feature 1: chunks 1 and 2 active
        bitmap[1] = 0x6; // Bits 1 and 2

        // Verify bitmap correctly identifies active chunks
        assert_eq!(
            (bitmap[0] & 0x1),
            0x1,
            "Chunk 0 should be active for feature 0"
        );
        assert_eq!(
            (bitmap[0] & 0x2),
            0x0,
            "Chunk 1 should be inactive for feature 0"
        );

        assert_eq!(
            (bitmap[1] & 0x1),
            0x0,
            "Chunk 0 should be inactive for feature 1"
        );
        assert_eq!(
            (bitmap[1] & 0x2),
            0x2,
            "Chunk 1 should be active for feature 1"
        );
        assert_eq!(
            (bitmap[1] & 0x4),
            0x4,
            "Chunk 2 should be active for feature 1"
        );
    }
}

//
// Phase 4, Task 4.1: Dynamic Plane Skipping Refinement
//

/// Sparsity pattern detected through profiling
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SparsityPattern {
    /// Even distribution of sparse values across dimensions
    Uniform,
    /// Dense clusters with sparse regions
    Clustered,
    /// Layer-wise or block-wise variation
    Layered,
}

/// Sparsity distribution statistics
#[derive(Debug, Clone)]
pub struct SparsityDistribution {
    /// Overall sparsity level
    pub overall_sparsity: f32,
    /// Variance in sparsity across chunks
    pub variance: f32,
    /// Detected pattern
    pub pattern: SparsityPattern,
}

/// Configuration for dynamic plane skipping
#[derive(Clone, Debug)]
pub struct DynamicPlaneSkippingConfig {
    /// Enable dynamic adaptation
    pub enable: bool,
    /// Initial sparsity threshold
    pub initial_threshold: f32,
    /// Adaptive threshold range (min, max)
    pub adaptive_range: (f32, f32),
    /// Minimum chunk size in dimensions
    pub min_chunk_size: usize,
    /// Maximum chunk size in dimensions
    pub max_chunk_size: usize,
    /// Maximum allowed profiling overhead as fraction
    pub profiling_overhead_limit: f32,
}

impl Default for DynamicPlaneSkippingConfig {
    fn default() -> Self {
        Self {
            enable: false, // Opt-in for stability
            initial_threshold: 0.90,
            adaptive_range: (0.85, 0.95),
            min_chunk_size: 32,
            max_chunk_size: 128,
            profiling_overhead_limit: 0.005, // 0.5%
        }
    }
}

impl DynamicPlaneSkippingConfig {
    /// Create config with dynamic adaptation enabled
    pub fn with_enable(mut self, enable: bool) -> Self {
        self.enable = enable;
        self
    }

    /// Create config with pattern detection enabled
    pub fn with_pattern_detection(mut self, enable: bool) -> Self {
        self.enable = enable;
        self
    }
}

/// Analyze tensor sparsity distribution for adaptive optimization
pub fn analyze_tensor_sparsity_distribution(
    tensor: &crate::kernels::ternary::types::TernaryTensor,
) -> SparsityDistribution {
    use crate::kernels::ternary::types::TernaryTensor;

    let out_features = tensor.shape()[0];
    let in_features = tensor.shape()[1];
    let chunk_size = 64; // Standard chunk for analysis
    let num_chunks = (in_features + chunk_size - 1) / chunk_size;

    // Calculate per-chunk sparsity
    let mut chunk_sparsities = Vec::new();

    for chunk_idx in 0..num_chunks {
        let start_dim = chunk_idx * chunk_size;
        let end_dim = (start_dim + chunk_size).min(in_features);
        let mut zero_count = 0;
        let mut total_count = 0;

        for row in 0..out_features {
            for dim in start_dim..end_dim {
                total_count += 1;
                // Check if value is zero by checking both planes
                let word_idx = dim / 32;
                let bit_idx = dim % 32;

                // Get values from planes (both zero = sparse)
                let plus_data = tensor.plus_plane().storage();
                let minus_data = tensor.minus_plane().storage();

                let plus_idx = row * ((in_features + 31) / 32) + word_idx;
                let minus_idx = row * ((in_features + 31) / 32) + word_idx;

                if plus_idx < plus_data.len() && minus_idx < minus_data.len() {
                    let plus_word = plus_data[plus_idx];
                    let minus_word = minus_data[minus_idx];

                    let plus_bit = (plus_word >> bit_idx) & 1;
                    let minus_bit = (minus_word >> bit_idx) & 1;

                    if plus_bit == 0 && minus_bit == 0 {
                        zero_count += 1;
                    }
                }
            }
        }

        if total_count > 0 {
            chunk_sparsities.push(zero_count as f32 / total_count as f32);
        }
    }

    // Calculate overall statistics
    let overall_sparsity = if !chunk_sparsities.is_empty() {
        chunk_sparsities.iter().sum::<f32>() / chunk_sparsities.len() as f32
    } else {
        0.0
    };

    // Calculate variance
    let variance = if chunk_sparsities.len() > 1 {
        let mean = overall_sparsity;
        let sum_sq_diff: f32 = chunk_sparsities.iter().map(|&x| (x - mean).powi(2)).sum();
        sum_sq_diff / chunk_sparsities.len() as f32
    } else {
        0.0
    };

    // Detect pattern based on variance
    let pattern = if variance < 0.1 {
        SparsityPattern::Uniform
    } else if variance > 0.3 {
        SparsityPattern::Clustered
    } else {
        SparsityPattern::Layered
    };

    SparsityDistribution {
        overall_sparsity,
        variance,
        pattern,
    }
}

/// Adaptive plane skipping matmul with runtime optimization
#[allow(dead_code)]
pub fn adaptive_plane_skipping_matmul(
    input: &candle_core::Tensor,
    weights: &crate::kernels::ternary::types::TernaryTensor,
    config: DynamicPlaneSkippingConfig,
) -> candle_core::Result<candle_core::Tensor> {
    use crate::kernels::ternary::matmul::ternary_matmul;

    if !config.enable {
        // Fall back to standard matmul
        return ternary_matmul(input, weights);
    }

    // Profile sparsity distribution
    let distribution = analyze_tensor_sparsity_distribution(weights);

    // Adapt configuration based on detected pattern
    let adapted_chunk_size = match distribution.pattern {
        SparsityPattern::Uniform => 64,
        SparsityPattern::Clustered => config.min_chunk_size,
        SparsityPattern::Layered => config.max_chunk_size,
    };

    let adapted_threshold = match distribution.pattern {
        SparsityPattern::Uniform => config.initial_threshold,
        SparsityPattern::Clustered => config.adaptive_range.0, // Lower = more aggressive
        SparsityPattern::Layered => config.adaptive_range.1,   // Higher = more conservative
    };

    // Apply matmul with adapted configuration
    // Note: In actual implementation, we would pass adapted_chunk_size and adapted_threshold
    // to the sparse kernel. For now, fall back to standard implementation.
    ternary_matmul(input, weights)
}

#[cfg(test)]
mod dynamic_plane_skipping_tests {
    use super::*;
    use crate::kernels::ternary::quantize::quantize_tensor_to_ternary;
    use candle_core::{DType, Device, Tensor};

    #[test]
    fn test_dynamic_config_creation() {
        let config = DynamicPlaneSkippingConfig::default();
        assert!(!config.enable, "Should be disabled by default");
        assert_eq!(config.initial_threshold, 0.90);
        assert_eq!(config.adaptive_range, (0.85, 0.95));
        assert_eq!(config.min_chunk_size, 32);
        assert_eq!(config.max_chunk_size, 128);

        let enabled_config = config.with_enable(true);
        assert!(enabled_config.enable);
    }

    #[test]
    fn test_analyze_uniform_sparsity() {
        let device = Device::Cpu;

        // Create uniform sparse tensor (90% sparse throughout)
        let data = Tensor::zeros((4, 128), DType::F32, &device).unwrap();
        // Set 10% of values to non-zero uniformly
        let mut data_vec = vec![0.0f32; 4 * 128];
        for i in (0..data_vec.len()).step_by(10) {
            data_vec[i] = 1.0;
        }
        let data = Tensor::from_vec(data_vec, (4, 128), &device).unwrap();

        let ternary = quantize_tensor_to_ternary(&data, 0.5).unwrap();
        let distribution = analyze_tensor_sparsity_distribution(&ternary);

        assert!(distribution.overall_sparsity > 0.85);
        assert!(
            distribution.variance < 0.15,
            "Uniform should have low variance"
        );
        assert_eq!(distribution.pattern, SparsityPattern::Uniform);
    }

    #[test]
    fn test_analyze_clustered_sparsity() {
        let device = Device::Cpu;

        // Create clustered sparse tensor (dense clusters)
        let mut data_vec = vec![0.0f32; 4 * 128];
        // Dense cluster in first 32 dimensions
        for i in 0..32 {
            data_vec[i] = 1.0;
        }
        // Sparse in remaining dimensions (only few values)
        data_vec[64] = 1.0;
        data_vec[96] = 1.0;

        let data = Tensor::from_vec(data_vec, (4, 128), &device).unwrap();
        let ternary = quantize_tensor_to_ternary(&data, 0.5).unwrap();
        let distribution = analyze_tensor_sparsity_distribution(&ternary);

        assert!(
            distribution.variance > 0.2,
            "Clustered should have high variance"
        );
    }

    #[test]
    fn test_adaptive_matmul_shape() {
        let device = Device::Cpu;
        let batch = 2;
        let in_features = 128;
        let out_features = 64;

        let input = Tensor::ones((batch, in_features), DType::F32, &device).unwrap();
        let weights_data = Tensor::ones((out_features, in_features), DType::F32, &device).unwrap();
        let weights = quantize_tensor_to_ternary(&weights_data, 0.5).unwrap();

        let config = DynamicPlaneSkippingConfig::default().with_enable(true);
        let output = adaptive_plane_skipping_matmul(&input, &weights, config).unwrap();

        assert_eq!(output.dims(), &[batch, out_features]);
    }
}

// ============================================================================
// Phase 4, Task 4.2: Chunk-Based Sparsity Optimization
// ============================================================================

/// Sparse storage formats for ternary tensors
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SparseStorageFormat {
    /// Default bitsliced format (2 bits per element)
    /// Best for: 50-95% sparsity, uniform distribution
    Bitsliced,

    /// Compressed Sparse Row format
    /// Best for: >95% sparsity, row-major access
    CSR,

    /// ELLPACK format (GPU-friendly with padding)
    /// Best for: GPU execution, uniform row lengths
    ELL,

    /// Run-length encoded format
    /// Best for: >98% sparsity, large zero runs
    Compressed,
}

/// Configuration for chunk-based sparsity optimization
#[derive(Clone, Debug)]
pub struct ChunkOptimizationConfig {
    /// Enable automatic format selection
    pub enable_format_selection: bool,

    /// Whether running on GPU
    pub on_gpu: bool,

    /// Override format selection (None = auto)
    pub force_format: Option<SparseStorageFormat>,
}

impl ChunkOptimizationConfig {
    pub fn default() -> Self {
        Self {
            enable_format_selection: true,
            on_gpu: false,
            force_format: None,
        }
    }

    pub fn for_gpu() -> Self {
        Self {
            enable_format_selection: true,
            on_gpu: true,
            force_format: None,
        }
    }

    pub fn with_format(mut self, format: SparseStorageFormat) -> Self {
        self.force_format = Some(format);
        self
    }
}

/// Statistics for sparsity analysis
#[derive(Clone, Debug)]
pub struct SparsityStats {
    pub sparsity: f32,
    pub row_variance: f32,
    pub max_nnz_per_row: usize,
    pub avg_nnz_per_row: f32,
}

/// Analyze sparsity statistics for format selection
pub fn analyze_sparsity_stats(tensor: &TernaryTensor) -> SparsityStats {
    let out_features = tensor.plus_plane.dims()[0];
    let in_features = tensor.plus_plane.dims()[1] * BITS_PER_U32 as usize;

    let plus_data = tensor.plus_plane.to_vec2::<u32>().unwrap();
    let minus_data = tensor.minus_plane.to_vec2::<u32>().unwrap();

    let mut total_nonzero = 0;
    let mut nnz_per_row = Vec::new();

    for row_idx in 0..out_features {
        let mut row_nnz = 0;
        for word_idx in 0..plus_data[row_idx].len() {
            let plus_word = plus_data[row_idx][word_idx];
            let minus_word = minus_data[row_idx][word_idx];
            row_nnz += (plus_word | minus_word).count_ones() as usize;
        }
        total_nonzero += row_nnz;
        nnz_per_row.push(row_nnz);
    }

    let total_elements = out_features * in_features;
    let sparsity = 1.0 - (total_nonzero as f32 / total_elements as f32);

    let avg_nnz = nnz_per_row.iter().sum::<usize>() as f32 / nnz_per_row.len() as f32;
    let variance = nnz_per_row
        .iter()
        .map(|&x| (x as f32 - avg_nnz).powi(2))
        .sum::<f32>()
        / nnz_per_row.len() as f32;
    let row_variance = variance.sqrt() / avg_nnz.max(1.0);

    let max_nnz_per_row = *nnz_per_row.iter().max().unwrap_or(&0);

    SparsityStats {
        sparsity,
        row_variance,
        max_nnz_per_row,
        avg_nnz_per_row: avg_nnz,
    }
}

/// Select optimal storage format based on sparsity characteristics
pub fn format_selection_heuristic(
    sparsity: f32,
    row_variance: f32,
    on_gpu: bool,
) -> SparseStorageFormat {
    if sparsity < 0.50 {
        // Dense enough for standard bitsliced format
        SparseStorageFormat::Bitsliced
    } else if sparsity > 0.98 {
        // Extreme sparsity, use run-length encoding
        SparseStorageFormat::Compressed
    } else if sparsity > 0.95 && row_variance < 0.2 {
        // Very sparse with uniform rows, CSR is efficient
        SparseStorageFormat::CSR
    } else if on_gpu && row_variance < 0.1 {
        // GPU with regular structure, ELL for coalesced access
        SparseStorageFormat::ELL
    } else {
        // Default to bitsliced
        SparseStorageFormat::Bitsliced
    }
}

/// CSR format representation
#[derive(Clone, Debug)]
pub struct CSRFormat {
    pub row_ptr: Vec<usize>,
    pub col_indices: Vec<usize>,
    pub values: Vec<i8>, // -1, 0, +1
}

/// Convert ternary tensor to CSR format
pub fn convert_to_csr_format(tensor: &TernaryTensor) -> CSRFormat {
    let out_features = tensor.plus_plane.dims()[0];
    let in_features = tensor.plus_plane.dims()[1] * BITS_PER_U32 as usize;

    let plus_data = tensor.plus_plane.to_vec2::<u32>().unwrap();
    let minus_data = tensor.minus_plane.to_vec2::<u32>().unwrap();

    let mut row_ptr = vec![0];
    let mut col_indices = Vec::new();
    let mut values = Vec::new();

    for row_idx in 0..out_features {
        for word_idx in 0..plus_data[row_idx].len() {
            let plus_word = plus_data[row_idx][word_idx];
            let minus_word = minus_data[row_idx][word_idx];

            for bit in 0..BITS_PER_U32 {
                let col = word_idx * BITS_PER_U32 as usize + bit as usize;
                if col >= in_features {
                    break;
                }

                let is_plus = (plus_word & (1u32 << bit)) != 0;
                let is_minus = (minus_word & (1u32 << bit)) != 0;

                if is_plus || is_minus {
                    col_indices.push(col);
                    values.push(if is_plus { 1 } else { -1 });
                }
            }
        }
        row_ptr.push(values.len());
    }

    CSRFormat {
        row_ptr,
        col_indices,
        values,
    }
}

/// ELL format representation
#[derive(Clone, Debug)]
pub struct ELLFormat {
    pub max_nnz_per_row: usize,
    pub col_indices: Vec<Vec<usize>>, // [rows][max_nnz]
    pub values: Vec<Vec<i8>>,         // [rows][max_nnz]
}

/// Convert ternary tensor to ELL format
pub fn convert_to_ell_format(tensor: &TernaryTensor) -> ELLFormat {
    let stats = analyze_sparsity_stats(tensor);
    let out_features = tensor.plus_plane.dims()[0];
    let in_features = tensor.plus_plane.dims()[1] * BITS_PER_U32 as usize;

    let plus_data = tensor.plus_plane.to_vec2::<u32>().unwrap();
    let minus_data = tensor.minus_plane.to_vec2::<u32>().unwrap();

    let max_nnz = stats.max_nnz_per_row;
    let mut col_indices = vec![vec![0; max_nnz]; out_features];
    let mut values = vec![vec![0i8; max_nnz]; out_features];

    for row_idx in 0..out_features {
        let mut nnz_count = 0;

        for word_idx in 0..plus_data[row_idx].len() {
            let plus_word = plus_data[row_idx][word_idx];
            let minus_word = minus_data[row_idx][word_idx];

            for bit in 0..BITS_PER_U32 {
                let col = word_idx * BITS_PER_U32 as usize + bit as usize;
                if col >= in_features {
                    break;
                }

                let is_plus = (plus_word & (1u32 << bit)) != 0;
                let is_minus = (minus_word & (1u32 << bit)) != 0;

                if is_plus || is_minus {
                    if nnz_count < max_nnz {
                        col_indices[row_idx][nnz_count] = col;
                        values[row_idx][nnz_count] = if is_plus { 1 } else { -1 };
                        nnz_count += 1;
                    }
                }
            }
        }
    }

    ELLFormat {
        max_nnz_per_row: max_nnz,
        col_indices,
        values,
    }
}

/// Compressed (RLE) format representation
#[derive(Clone, Debug)]
pub struct CompressedFormat {
    pub runs: Vec<Vec<(usize, i8)>>, // [rows][(start_col, value)]
}

/// Convert ternary tensor to run-length encoded format
pub fn convert_to_compressed_format(tensor: &TernaryTensor) -> CompressedFormat {
    let out_features = tensor.plus_plane.dims()[0];
    let in_features = tensor.plus_plane.dims()[1] * BITS_PER_U32 as usize;

    let plus_data = tensor.plus_plane.to_vec2::<u32>().unwrap();
    let minus_data = tensor.minus_plane.to_vec2::<u32>().unwrap();

    let mut runs = Vec::new();

    for row_idx in 0..out_features {
        let mut row_runs = Vec::new();
        let mut current_value: Option<i8> = None;
        let mut run_start = 0;

        for word_idx in 0..plus_data[row_idx].len() {
            let plus_word = plus_data[row_idx][word_idx];
            let minus_word = minus_data[row_idx][word_idx];

            for bit in 0..BITS_PER_U32 {
                let col = word_idx * BITS_PER_U32 as usize + bit as usize;
                if col >= in_features {
                    break;
                }

                let is_plus = (plus_word & (1u32 << bit)) != 0;
                let is_minus = (minus_word & (1u32 << bit)) != 0;

                let value = if is_plus {
                    Some(1)
                } else if is_minus {
                    Some(-1)
                } else {
                    None
                };

                if value != current_value {
                    if let Some(v) = current_value {
                        row_runs.push((run_start, v));
                    }
                    current_value = value;
                    run_start = col;
                }
            }
        }

        if let Some(v) = current_value {
            row_runs.push((run_start, v));
        }

        runs.push(row_runs);
    }

    CompressedFormat { runs }
}

/// Chunk-optimized ternary matmul with automatic format selection
pub fn chunk_optimized_ternary_matmul(
    input: &Tensor,
    weights: &TernaryTensor,
    config: ChunkOptimizationConfig,
) -> Result<Tensor> {
    // Analyze sparsity pattern
    let stats = analyze_sparsity_stats(weights);

    // Select optimal format
    let format = if let Some(forced) = config.force_format {
        forced
    } else if config.enable_format_selection {
        format_selection_heuristic(stats.sparsity, stats.row_variance, config.on_gpu)
    } else {
        SparseStorageFormat::Bitsliced
    };

    // For now, all formats use the same CPU kernel
    // In a real implementation, each format would have its own optimized kernel
    match format {
        SparseStorageFormat::Bitsliced => {
            // Use standard ternary matmul
            ternary_matmul_cpu(input, weights)
        }
        SparseStorageFormat::CSR => {
            // CSR-optimized kernel (placeholder: uses standard for now)
            ternary_matmul_cpu(input, weights)
        }
        SparseStorageFormat::ELL => {
            // ELL-optimized kernel (placeholder: uses standard for now)
            ternary_matmul_cpu(input, weights)
        }
        SparseStorageFormat::Compressed => {
            // Compressed-optimized kernel (placeholder: uses standard for now)
            ternary_matmul_cpu(input, weights)
        }
    }
}

#[cfg(test)]
mod tests_chunk_optimization {
    use super::*;

    #[test]
    fn test_format_selection_dense() {
        // Dense tensor should use Bitsliced
        let format = format_selection_heuristic(0.30, 0.1, false);
        assert_eq!(format, SparseStorageFormat::Bitsliced);
    }

    #[test]
    fn test_format_selection_very_sparse() {
        // Very sparse with uniform rows should use CSR
        let format = format_selection_heuristic(0.96, 0.15, false);
        assert_eq!(format, SparseStorageFormat::CSR);
    }

    #[cfg(feature = "cuda")]
    #[test]
    fn test_convert_to_csr() {
        use crate::kernels::quantization::quantize_tensor_to_ternary;
        use crate::ternary::TernaryTensor;

        let device = candle_core::Device::Cpu;
        let data = Tensor::from_slice(
            &[1.0f32, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, -1.0],
            &[2, 4],
            &device,
        )
        .unwrap();
        let ternary = quantize_tensor_to_ternary(&data, 0.5).unwrap();

        let csr = convert_to_csr_format(&ternary);

        // First row has 2 non-zeros (1.0, -1.0)
        // Second row has 2 non-zeros (1.0, -1.0)
        assert_eq!(csr.row_ptr[0], 0);
        assert_eq!(csr.row_ptr[1], 2);
        assert_eq!(csr.row_ptr[2], 4);
        assert_eq!(csr.values.len(), 4);
    }

    #[cfg(feature = "cuda")]
    #[test]
    fn test_chunk_optimized_matmul_shape() {
        use crate::kernels::quantization::quantize_tensor_to_ternary;

        let device = candle_core::Device::Cpu;
        let batch = 2;
        let in_features = 64;
        let out_features = 32;

        let input = Tensor::ones((batch, in_features), DType::F32, &device).unwrap();
        let weights_data = Tensor::ones((out_features, in_features), DType::F32, &device).unwrap();
        let weights = quantize_tensor_to_ternary(&weights_data, 0.5).unwrap();

        let config = ChunkOptimizationConfig::default();
        let output = chunk_optimized_ternary_matmul(&input, &weights, config).unwrap();

        assert_eq!(output.dims(), &[batch, out_features]);
    }
}

// ============================================================================
// Phase 4, Task 4.3: Sparsity Profiler and Analysis Tools
// ============================================================================

/// Comprehensive sparsity profile
#[derive(Clone, Debug)]
pub struct SparsityProfile {
    pub overall_sparsity: f32,
    pub per_row_sparsity: Vec<f32>,
    pub sparsity_pattern: SparsityPattern,
    pub recommended_format: SparseStorageFormat,
    pub recommended_chunk_size: usize,
    pub estimated_memory_savings: f32,
    pub estimated_speedup: f32,
}

/// Profile a ternary tensor for optimization opportunities
pub fn profile_tensor_sparsity(tensor: &TernaryTensor, on_gpu: bool) -> SparsityProfile {
    let stats = analyze_sparsity_stats(tensor);
    let distribution = analyze_tensor_sparsity_distribution(tensor);

    // Detect pattern
    let pattern = if distribution.variance < 0.1 {
        SparsityPattern::Uniform
    } else if distribution.variance > 0.3 {
        SparsityPattern::Clustered
    } else {
        SparsityPattern::Layered
    };

    // Recommend format
    let recommended_format = format_selection_heuristic(stats.sparsity, stats.row_variance, on_gpu);

    // Recommend chunk size based on pattern
    let recommended_chunk_size = match pattern {
        SparsityPattern::Uniform => 64,
        SparsityPattern::Clustered => 32,
        SparsityPattern::Layered => 128,
    };

    // Estimate memory savings
    let estimated_memory_savings = match recommended_format {
        SparseStorageFormat::Bitsliced => 1.0,
        SparseStorageFormat::CSR => {
            if stats.sparsity > 0.95 {
                3.0
            } else {
                1.5
            }
        }
        SparseStorageFormat::ELL => 1.2,
        SparseStorageFormat::Compressed => {
            if stats.sparsity > 0.98 {
                5.0
            } else {
                2.0
            }
        }
    };

    // Estimate speedup
    let estimated_speedup = if stats.sparsity > 0.95 {
        2.0 + (stats.sparsity - 0.95) * 10.0 // Up to 2.5x at 100% sparsity
    } else {
        1.0 + stats.sparsity * 0.5 // Modest gains below 95%
    };

    // Calculate per-row sparsity
    let out_features = tensor.plus_plane.dims()[0];
    let in_features = tensor.plus_plane.dims()[1] * BITS_PER_U32 as usize;
    let plus_data = tensor.plus_plane.to_vec2::<u32>().unwrap();
    let minus_data = tensor.minus_plane.to_vec2::<u32>().unwrap();

    let mut per_row_sparsity = Vec::new();
    for row_idx in 0..out_features {
        let mut row_nnz = 0;
        for word_idx in 0..plus_data[row_idx].len() {
            let plus_word = plus_data[row_idx][word_idx];
            let minus_word = minus_data[row_idx][word_idx];
            row_nnz += (plus_word | minus_word).count_ones() as usize;
        }
        let row_sparsity = 1.0 - (row_nnz as f32 / in_features as f32);
        per_row_sparsity.push(row_sparsity);
    }

    SparsityProfile {
        overall_sparsity: stats.sparsity,
        per_row_sparsity,
        sparsity_pattern: pattern,
        recommended_format,
        recommended_chunk_size,
        estimated_memory_savings,
        estimated_speedup,
    }
}

/// Generate optimization recommendations based on profile
pub fn generate_optimization_recommendations(profile: &SparsityProfile) -> Vec<String> {
    let mut recommendations = Vec::new();

    // Format recommendation
    recommendations.push(format!(
        "Use {:?} storage format for {:.1}% sparsity",
        profile.recommended_format,
        profile.overall_sparsity * 100.0
    ));

    // Chunk size recommendation
    recommendations.push(format!(
        "Use chunk size of {} dimensions based on {:?} pattern",
        profile.recommended_chunk_size, profile.sparsity_pattern
    ));

    // Memory savings
    if profile.estimated_memory_savings > 1.5 {
        recommendations.push(format!(
            "Expected {:.1}x memory reduction with recommended format",
            profile.estimated_memory_savings
        ));
    }

    // Speedup potential
    if profile.estimated_speedup > 1.5 {
        recommendations.push(format!(
            "Expected {:.1}x speedup with plane skipping optimization",
            profile.estimated_speedup
        ));
    }

    // Pattern-specific recommendations
    match profile.sparsity_pattern {
        SparsityPattern::Uniform => {
            recommendations.push("Uniform sparsity: standard optimizations effective".to_string());
        }
        SparsityPattern::Clustered => {
            recommendations
                .push("Clustered sparsity: use smaller chunks for precision".to_string());
        }
        SparsityPattern::Layered => {
            recommendations
                .push("Layered sparsity: consider per-layer format selection".to_string());
        }
    }

    // Extreme sparsity recommendations
    if profile.overall_sparsity > 0.98 {
        recommendations.push("Extreme sparsity: consider RLE compression".to_string());
    }

    recommendations
}

#[cfg(test)]
mod tests_sparsity_profiler {
    use super::*;

    #[cfg(feature = "cuda")]
    #[test]
    fn test_profile_tensor_sparsity() {
        use crate::kernels::quantization::quantize_tensor_to_ternary;

        let device = candle_core::Device::Cpu;
        // Create a 90% sparse tensor
        let mut data_vec = vec![0.0f32; 1000];
        for i in 0..100 {
            data_vec[i * 10] = 1.0; // 10% non-zero
        }
        let data = Tensor::from_slice(&data_vec, &[10, 100], &device).unwrap();
        let ternary = quantize_tensor_to_ternary(&data, 0.5).unwrap();

        let profile = profile_tensor_sparsity(&ternary, false);

        assert!(profile.overall_sparsity > 0.85);
        assert!(profile.overall_sparsity < 0.95);
        assert!(profile.estimated_speedup > 1.0);
    }

    #[cfg(feature = "cuda")]
    #[test]
    fn test_generate_recommendations() {
        use crate::kernels::quantization::quantize_tensor_to_ternary;

        let device = candle_core::Device::Cpu;
        let data = Tensor::zeros(&[10, 100], DType::F32, &device).unwrap();
        let ternary = quantize_tensor_to_ternary(&data, 0.5).unwrap();

        let profile = profile_tensor_sparsity(&ternary, false);
        let recommendations = generate_optimization_recommendations(&profile);

        assert!(!recommendations.is_empty());
        assert!(recommendations.iter().any(|r| r.contains("storage format")));
    }
}