bixverse-rs 0.7.0

A Rust library with various utilities for computational biology and single cell.
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
//! MLX HALS NMF over dense or sparse input.
//!
//! Mirrors [`crate::methods::nmf_hals::nmf_hals`] and the cubecl port in
//! `gpu::methods_gpu::nmf_gpu` iteration for iteration. Layout follows the
//! cubecl path: `W` is `[m, k]` row-major and `H` is held transposed as
//! `[n, k]`, so both Grams, both data products and both sweeps see the same
//! component-minor layout. The Grams are MLX GEMMs over lazy transposes; the
//! data products are GEMMs for dense `V` and the plain SpMM of
//! `mlx::linalg::spmm` for sparse `V`, which is uploaded in both
//! orientations so neither product needs atomics. The Gauss-Seidel sweep and
//! the normalisation scale are custom Metal kernels. One iteration stays in
//! flight: iteration `t` is submitted asynchronously before the host waits on
//! `t - 1`.

use std::borrow::Cow;

use faer::{Mat, MatRef};

use crate::core::math::pca_svd::SvdResults;
use crate::methods::nmf_hals::consensus::{
    ConsensusNmfResult, ConsensusParams, ConsensusTarget, KSweepEntry, consensus_from_restarts,
    median,
};
use crate::methods::nmf_hals::dense::DenseInput;
use crate::methods::nmf_hals::nmf_preprocessing::{
    NmfPreprocessing, ensure_csc, nmf_process_dense, nmf_process_sparse, parse_nmf_processing,
};
use crate::methods::nmf_hals::sparse::SparseInput;
use crate::methods::nmf_hals::{
    HalsOpts, NmfInit, NmfInput, NmfResult, StabilisedNmfResult, nndsvd_from_svd, random_init,
};
use crate::mlx::ffi::{
    Array, MLX_FLOAT32, MetalKernel, OutputSpec, Stream, clear_cache, eval_all,
    install_error_handler,
};
use crate::mlx::linalg::spmm::{MlxCsr, spmm_plain, spmm_plain_kernel};
use crate::prelude::*;

////////////
// Consts //
////////////

/// Largest rank the sweep is compiled for. Same bound as the cubecl sweep: at
/// `k_cap = 128` the threadgroup is down to one SIMD group of 32.
pub const NMF_MAX_RANK: usize = 128;

/// Threads per threadgroup for the flat elementwise scale kernel.
const ELEMENTWISE_TG: usize = 256;

/// Metal source of the HALS sweep. One thread per row; the row is staged in
/// threadgroup memory at `stage[s * WG + tx]` exactly as the cubecl kernel
/// does, `K_CAP * WG = 4096` floats (16 KiB) at every tier.
const SWEEP_SOURCE: &str = r#"
    uint tx = thread_position_in_threadgroup.x;
    uint row = thread_position_in_grid.x;
    threadgroup float stage[K_CAP * WG];
    if (row >= uint(N_ROWS)) {
        return;
    }
    uint base = row * uint(K);
    for (uint s = 0; s < uint(K); ++s) {
        stage[s * WG + tx] = x[base + s];
    }
    for (uint r = 0; r < uint(K); ++r) {
        float grr = g[r * K + r];
        if (grr > 0.0f) {
            float dot = 0.0f;
            for (uint t = 0; t < uint(K); ++t) {
                dot += g[r * K + t] * stage[t * WG + tx];
            }
            uint slot = r * WG + tx;
            float updated = stage[slot] + (p[base + r] - dot) / grr;
            stage[slot] = updated < eps ? eps : updated;
        }
    }
    for (uint s = 0; s < uint(K); ++s) {
        x_out[base + s] = stage[s * WG + tx];
    }
"#;

/// Metal source of the normalisation scale. Turns a per-column sum of squares
/// into a factor (the norm, or its reciprocal with `INVERT`) and scales every
/// element of a `[rows, k]` buffer by its column's factor. A collapsed column
/// gets a factor of one, as on the CPU and in the cubecl path.
const SCALE_SOURCE: &str = r#"
    uint idx = thread_position_in_grid.x;
    if (idx >= uint(TOTAL)) {
        return;
    }
    float v = sq[idx % uint(K)];
    float f = 1.0f;
    if (v > 0.0f) {
        float nrm = sqrt(v);
        f = INVERT ? 1.0f / nrm : nrm;
    }
    x_out[idx] = x[idx] * f;
"#;

/////////////
// Helpers //
/////////////

/// Rank capacity and threadgroup width for a sweep at rank `k`.
///
/// `k_cap * wg` is 4096 in every arm, so the threadgroup footprint does not
/// depend on `k`. Same tiers as the cubecl sweep.
///
/// ### Params
///
/// * `k` - Number of components
///
/// ### Returns
///
/// `Some((k_cap, wg))`, or `None` if `k` is zero or above [`NMF_MAX_RANK`].
fn sweep_tier(k: usize) -> Option<(usize, usize)> {
    match k {
        0 => None,
        1..=16 => Some((16, 256)),
        17..=32 => Some((32, 128)),
        33..=64 => Some((64, 64)),
        65..=NMF_MAX_RANK => Some((128, 32)),
        _ => None,
    }
}

/// Reject a rank the sweep is not compiled for.
///
/// ### Params
///
/// * `k` - Number of components
///
/// ### Returns
///
/// `Ok(())`, or `MlxNmfRankTooLarge`.
fn check_rank(k: usize) -> Result<(), BixverseErrors> {
    match sweep_tier(k) {
        Some(_) => Ok(()),
        None => Err(BixverseErrors::MlxNmfRankTooLarge {
            k,
            max: NMF_MAX_RANK,
        }),
    }
}

/// Convert a size to the `i32` mlx-c takes for shapes, grids and templates.
///
/// ### Params
///
/// * `x` - The size
///
/// ### Returns
///
/// `x` as `i32`. Panics past `i32::MAX`, which MLX cannot index either.
fn to_i32(x: usize) -> i32 {
    i32::try_from(x).expect("size exceeds i32, which MLX cannot address")
}

/// Borrow a column-major faer matrix as one flat buffer when it has no padding
/// between columns, otherwise copy it.
///
/// A column-major `rows x cols` buffer is `[cols, rows]` row-major, i.e. the
/// transpose, so this is how `V^T` and `H^T` reach the device.
///
/// ### Params
///
/// * `x` - The matrix
///
/// ### Returns
///
/// The column-major values, borrowed or owned.
fn col_major_flat<'a>(x: MatRef<'a, f32>) -> Cow<'a, [f32]> {
    let (rows, cols) = (x.nrows(), x.ncols());
    if let Some(cm) = x.try_as_col_major()
        && cm.col_stride() as usize == rows
    {
        // SAFETY: unit row stride from `try_as_col_major` and no padding
        // between columns, so the `rows * cols` elements are one contiguous
        // range of the allocation starting at `as_ptr()`.
        return Cow::Borrowed(unsafe { std::slice::from_raw_parts(cm.as_ptr(), rows * cols) });
    }
    let mut out = Vec::with_capacity(rows * cols);
    for j in 0..cols {
        for i in 0..rows {
            out.push(x[(i, j)]);
        }
    }
    Cow::Owned(out)
}

/// Flatten a faer `rows x cols` matrix into row-major `[rows, cols]`.
///
/// ### Params
///
/// * `x` - The matrix
///
/// ### Returns
///
/// `rows * cols` values, row `i` contiguous at `i * cols`.
fn to_row_major(x: MatRef<f32>) -> Vec<f32> {
    let (rows, cols) = (x.nrows(), x.ncols());
    let mut out = vec![0f32; rows * cols];
    for (i, row) in out.chunks_exact_mut(cols.max(1)).enumerate().take(rows) {
        for (c, slot) in row.iter_mut().enumerate() {
            *slot = x[(i, c)];
        }
    }
    out
}

/// Gram of a `[rows, k]` buffer, `X^T X`, via a lazy transpose.
///
/// ### Params
///
/// * `x` - Buffer `[rows, k]`
/// * `s` - Stream
///
/// ### Returns
///
/// The lazy `[k, k]` Gram.
fn gram(x: &Array, s: &Stream) -> Result<Array, BixverseErrors> {
    x.transpose(s)?.matmul(x, s)
}

/// Per-row dot products of two `[rows, k]` buffers, `out[i] = sum_s x[i, s] *
/// y[i, s]`. One partial per row so the host finishes the sum in f64, as the
/// cubecl objective does.
///
/// ### Params
///
/// * `x` - First buffer `[rows, k]`
/// * `y` - Second buffer `[rows, k]`
/// * `s` - Stream
///
/// ### Returns
///
/// The lazy `[rows]` partials.
fn row_dot_partials(x: &Array, y: &Array, s: &Stream) -> Result<Array, BixverseErrors> {
    x.multiply(y, s)?.sum_axis(1, s)
}

/// Evaluate the objective parts and finish `||V||^2 - 2 sum(partials) + <B,
/// D>` on the host in f64.
///
/// ### Params
///
/// * `sq_frob` - `||V||_F^2`
/// * `partials` - Per-row partials of the cross term
/// * `b` - `W^T W`, `[k, k]`
/// * `d` - `H H^T`, `[k, k]`
///
/// ### Returns
///
/// The reconstruction error, clamped at zero.
fn finish_objective(
    sq_frob: f32,
    partials: &Array,
    b: &Array,
    d: &Array,
) -> Result<f32, BixverseErrors> {
    eval_all(&[partials, b, d], false)?;
    let cross: f64 = partials.as_f32()?.iter().map(|&x| x as f64).sum();
    let bd: f64 = b
        .as_f32()?
        .iter()
        .zip(d.as_f32()?)
        .map(|(&x, &y)| x as f64 * y as f64)
        .sum();
    Ok((sq_frob as f64 - 2.0 * cross + bd).max(0.0) as f32)
}

/// The relative-tolerance denominator, floored at one.
///
/// ### Params
///
/// * `sq_frob` - `||V||_F^2`
///
/// ### Returns
///
/// `max(sq_frob, 1)`.
fn relative_denominator(sq_frob: f32) -> f32 {
    if sq_frob > 1.0 { sq_frob } else { 1.0 }
}

/////////////
// Kernels //
/////////////

/// The two custom kernels of the solve, registered once per input.
struct HalsKernels {
    /// Gauss-Seidel sweep over all components of a `[rows, k]` factor
    sweep: MetalKernel,
    /// Column scale by a norm or its reciprocal
    scale: MetalKernel,
}

impl HalsKernels {
    /// Register both kernels.
    ///
    /// ### Returns
    ///
    /// The kernel handles.
    fn new() -> Self {
        Self {
            sweep: MetalKernel::new(
                "bixverse_hals_sweep",
                &["x", "g", "p", "eps"],
                &["x_out"],
                "",
                SWEEP_SOURCE,
            ),
            scale: MetalKernel::new(
                "bixverse_hals_scale",
                &["x", "sq"],
                &["x_out"],
                "",
                SCALE_SOURCE,
            ),
        }
    }

    /// One HALS sweep, `X[i, r] = max(eps, X[i, r] + (P[i, r] - sum_s G[r, s]
    /// X[i, s]) / G[r, r])` for every `r` in order. Covers the H sweep with
    /// `X = H^T`, `G = W^T W`, `P = (W^T V)^T`, and the W sweep with `X = W`,
    /// `G = H H^T`, `P = V H^T`. A component with a non-positive Gram diagonal
    /// is skipped, as on the CPU.
    ///
    /// ### Params
    ///
    /// * `x` - Factor `[n_rows, k]`
    /// * `g` - Symmetric Gram `[k, k]`
    /// * `p` - Data product `[n_rows, k]`
    /// * `eps` - Non-negativity floor, a 0-d array
    /// * `n_rows` - Rows of `x`
    /// * `k` - Number of components
    /// * `s` - Stream
    ///
    /// ### Returns
    ///
    /// The lazy swept factor `[n_rows, k]`.
    #[allow(clippy::too_many_arguments)]
    fn sweep(
        &self,
        x: &Array,
        g: &Array,
        p: &Array,
        eps: &Array,
        n_rows: usize,
        k: usize,
        s: &Stream,
    ) -> Result<Array, BixverseErrors> {
        let (k_cap, wg) = sweep_tier(k).ok_or(BixverseErrors::MlxNmfRankTooLarge {
            k,
            max: NMF_MAX_RANK,
        })?;
        let grid = n_rows.div_ceil(wg) * wg;
        let mut out = self.sweep.apply(
            &[x, g, p, eps],
            &[OutputSpec {
                shape: &[to_i32(n_rows), to_i32(k)],
                dtype: MLX_FLOAT32,
            }],
            [to_i32(grid), 1, 1],
            [to_i32(wg), 1, 1],
            &[
                ("N_ROWS", to_i32(n_rows)),
                ("K", to_i32(k)),
                ("K_CAP", to_i32(k_cap)),
                ("WG", to_i32(wg)),
            ],
            s,
        )?;
        Ok(out.remove(0))
    }

    /// Scale each column of a `[n_rows, k]` buffer by the L2 norm behind `sq`,
    /// or by its reciprocal.
    ///
    /// ### Params
    ///
    /// * `x` - Buffer `[n_rows, k]`
    /// * `sq` - Per-column sums of squares `[k]`
    /// * `n_rows` - Rows of `x`
    /// * `k` - Number of components
    /// * `invert` - Divide by the norm instead of multiplying
    /// * `s` - Stream
    ///
    /// ### Returns
    ///
    /// The lazy scaled buffer.
    fn scale(
        &self,
        x: &Array,
        sq: &Array,
        n_rows: usize,
        k: usize,
        invert: bool,
        s: &Stream,
    ) -> Result<Array, BixverseErrors> {
        let total = n_rows * k;
        let mut out = self.scale.apply(
            &[x, sq],
            &[OutputSpec {
                shape: &[to_i32(n_rows), to_i32(k)],
                dtype: MLX_FLOAT32,
            }],
            [
                to_i32(total.div_ceil(ELEMENTWISE_TG) * ELEMENTWISE_TG),
                1,
                1,
            ],
            [to_i32(ELEMENTWISE_TG), 1, 1],
            &[
                ("TOTAL", to_i32(total)),
                ("K", to_i32(k)),
                ("INVERT", invert as i32),
            ],
            s,
        )?;
        Ok(out.remove(0))
    }
}

////////////////
// MlxNmfCore //
////////////////

/// Device state every MLX NMF input carries, whatever the layout of `V`.
///
/// Owns the stream and the registered kernels, so every solve against one
/// input reuses them. MLX streams are thread affine, so this is `!Send`.
pub struct MlxNmfCore {
    /// Logical shape `(m, n)` of `V`
    shape: (usize, usize),
    /// Cached `||V||_F^2`, accumulated in f64
    sq_frob: f32,
    /// Stream every op of every solve runs on
    stream: Stream,
    /// The custom kernels
    kernels: HalsKernels,
}

impl MlxNmfCore {
    /// Create the stream and register the kernels.
    ///
    /// ### Params
    ///
    /// * `shape` - Logical shape `(m, n)` of `V`
    /// * `sq_frob` - `||V||_F^2`
    ///
    /// ### Returns
    ///
    /// The core.
    fn new(shape: (usize, usize), sq_frob: f32) -> Self {
        install_error_handler();
        Self {
            shape,
            sq_frob,
            stream: Stream::default_gpu(),
            kernels: HalsKernels::new(),
        }
    }
}

////////////////
// MlxNmfData //
////////////////

/// Device-resident `V` for MLX HALS.
///
/// The implementors differ only in how the two data products are dispatched;
/// the solver is written against this trait as the cubecl one is written
/// against `GpuNmfData`. The initialisation SVD stays on the host so MLX and
/// CPU runs start from identical factors.
pub trait MlxNmfData {
    /// The shared device state.
    ///
    /// ### Returns
    ///
    /// The [`MlxNmfCore`].
    fn core(&self) -> &MlxNmfCore;

    /// `(W^T V)^T = V^T W`.
    ///
    /// ### Params
    ///
    /// * `w` - Left factor `[m, k]`
    /// * `k` - Number of components
    ///
    /// ### Returns
    ///
    /// The lazy `[n, k]` product.
    fn wt_v(&self, w: &Array, k: usize) -> Result<Array, BixverseErrors>;

    /// `V H^T`.
    ///
    /// ### Params
    ///
    /// * `h_t` - Right factor transposed, `[n, k]`
    /// * `k` - Number of components
    ///
    /// ### Returns
    ///
    /// The lazy `[m, k]` product.
    fn v_ht(&self, h_t: &Array, k: usize) -> Result<Array, BixverseErrors>;

    /// Top-k truncated SVD of `V` on the host, for NNDSVD initialisation.
    ///
    /// ### Params
    ///
    /// * `k` - Number of singular triplets
    ///
    /// ### Returns
    ///
    /// The top-k [`SvdResults`].
    fn top_k_svd(&self, k: usize) -> Result<SvdResults<f32>, BixverseErrors>;

    /// Shape of `V`.
    ///
    /// ### Returns
    ///
    /// `(m, n)`, samples by features.
    fn shape(&self) -> (usize, usize) {
        self.core().shape
    }

    /// The squared Frobenius norm `||V||_F^2`.
    ///
    /// ### Returns
    ///
    /// The squared Frobenius norm.
    fn sq_frob(&self) -> f32 {
        self.core().sq_frob
    }
}

/// Recompute both Grams and `W^T V`, then evaluate `||V - W H||_F^2`.
///
/// The cross term `<V, W H>` is `sum_j (V^T W)[j, :] . H^T[j, :]`, so it reuses
/// the `W^T V` product and never densifies a sparse `V`.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `w` - Left factor `[m, k]`
/// * `h_t` - Right factor transposed `[n, k]`
/// * `k` - Number of components
///
/// ### Returns
///
/// The reconstruction error, clamped at zero. Blocks.
fn objective(v: &impl MlxNmfData, w: &Array, h_t: &Array, k: usize) -> Result<f32, BixverseErrors> {
    let s = &v.core().stream;
    let b = gram(w, s)?;
    let a = v.wt_v(w, k)?;
    let d = gram(h_t, s)?;
    let partials = row_dot_partials(&a, h_t, s)?;
    finish_objective(v.sq_frob(), &partials, &b, &d)
}

/// Normalise the columns of `W` to unit L2 and absorb the norms into `H`.
///
/// ### Params
///
/// * `core` - Device state of the input
/// * `w` - Left factor `[m, k]`
/// * `h_t` - Right factor transposed `[n, k]`
/// * `k` - Number of components
///
/// ### Returns
///
/// The lazy pair `(W, H^T)` with `W H` unchanged.
fn normalise(
    core: &MlxNmfCore,
    w: &Array,
    h_t: &Array,
    k: usize,
) -> Result<(Array, Array), BixverseErrors> {
    let s = &core.stream;
    let (m, n) = core.shape;
    let sq = w.multiply(w, s)?.sum_axis(0, s)?;
    let w_new = core.kernels.scale(w, &sq, m, k, true, s)?;
    let h_new = core.kernels.scale(h_t, &sq, n, k, false, s)?;
    Ok((w_new, h_new))
}

/// One full HALS iteration: Gram, product, H sweep, Gram, product, W sweep,
/// normalise.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `w` - Left factor `[m, k]`
/// * `h_t` - Right factor transposed `[n, k]`
/// * `eps` - Non-negativity floor, a 0-d array
/// * `k` - Number of components
///
/// ### Returns
///
/// The lazy pair `(W, H^T)` after the iteration.
fn iteration(
    v: &impl MlxNmfData,
    w: &Array,
    h_t: &Array,
    eps: &Array,
    k: usize,
) -> Result<(Array, Array), BixverseErrors> {
    let core = v.core();
    let s = &core.stream;
    let (m, n) = core.shape;

    let b = gram(w, s)?;
    let a = v.wt_v(w, k)?;
    let h_t = core.kernels.sweep(h_t, &b, &a, eps, n, k, s)?;

    let d = gram(&h_t, s)?;
    let c = v.v_ht(&h_t, k)?;
    let w = core.kernels.sweep(w, &d, &c, eps, m, k, s)?;

    normalise(core, &w, &h_t, k)
}

//////////////////////
// MlxDenseNmfInput //
//////////////////////

/// Dense `V` on the MLX device, uploaded once as `V^T`.
///
/// Holds the host view alongside for the NNDSVD initialisation SVD.
pub struct MlxDenseNmfInput<'a> {
    /// `V^T` as `[n, m]` row-major
    v_t: Array,
    /// Host-side view, for the initialisation SVD only
    host: DenseInput<'a, f32>,
    /// Shape, norm, stream and kernels
    core: MlxNmfCore,
}

impl<'a> MlxDenseNmfInput<'a> {
    /// Validate `V`, upload it and cache its Frobenius norm.
    ///
    /// A contiguous column-major `m x n` buffer already is `[n, m]` row-major,
    /// so that case uploads without an intermediate host copy.
    ///
    /// ### Params
    ///
    /// * `v` - The matrix to factorise, `m x n`, samples by features
    ///
    /// ### Returns
    ///
    /// The input, or `NmfNonFinite` / `NmfNonNegativeViolated` if validation
    /// fails.
    pub fn new(v: MatRef<'a, f32>) -> Result<Self, BixverseErrors> {
        let host = DenseInput::new(v)?;
        let (m, n) = (v.nrows(), v.ncols());
        let core = MlxNmfCore::new((m, n), NmfInput::sq_frob(&host));
        let v_t = Array::from_f32(&col_major_flat(v), &[to_i32(n), to_i32(m)]);
        Ok(Self { v_t, host, core })
    }
}

impl MlxNmfData for MlxDenseNmfInput<'_> {
    fn core(&self) -> &MlxNmfCore {
        &self.core
    }

    fn wt_v(&self, w: &Array, _k: usize) -> Result<Array, BixverseErrors> {
        self.v_t.matmul(w, &self.core.stream)
    }

    fn v_ht(&self, h_t: &Array, _k: usize) -> Result<Array, BixverseErrors> {
        let s = &self.core.stream;
        self.v_t.transpose(s)?.matmul(h_t, s)
    }

    fn top_k_svd(&self, k: usize) -> Result<SvdResults<f32>, BixverseErrors> {
        NmfInput::top_k_svd(&self.host, k)
    }
}

///////////////////////
// MlxSparseNmfInput //
///////////////////////

/// Sparse `V` on the MLX device, uploaded once in both orientations.
///
/// The CSC of `V` is the CSR of `V^T`, so both data products are the same
/// row-wise SpMM, one SIMD group per output row and no atomics.
pub struct MlxSparseNmfInput {
    /// CSR of `V`, `m` rows, for `V H^T`
    v: MlxCsr,
    /// CSC of `V` read as the CSR of `V^T`, `n` rows, for `V^T W`
    v_t: MlxCsr,
    /// The plain SpMM kernel
    spmm: MetalKernel,
    /// Host-side matched pair, for the initialisation SVD only
    host: SparseInput<f32, f32>,
    /// Shape, norm, stream and kernels
    core: MlxNmfCore,
}

impl MlxSparseNmfInput {
    /// Upload a validated host pair in both orientations.
    ///
    /// ### Params
    ///
    /// * `host` - A validated [`SparseInput`] over `V`
    ///
    /// ### Returns
    ///
    /// The input, or `InvalidArgument` if the matrix exceeds what MLX can
    /// index.
    pub fn new(host: SparseInput<f32, f32>) -> Result<Self, BixverseErrors> {
        let (m, n) = NmfInput::shape(&host);
        let core = MlxNmfCore::new((m, n), NmfInput::sq_frob(&host));
        let (csr, csc) = (host.csr(), host.csc());
        let v = MlxCsr::new(&csr.indptr, &csr.indices, &csr.data, m)?;
        let v_t = MlxCsr::new(&csc.indptr, &csc.indices, &csc.data, n)?;
        Ok(Self {
            v,
            v_t,
            spmm: spmm_plain_kernel(),
            host,
            core,
        })
    }
}

impl MlxNmfData for MlxSparseNmfInput {
    fn core(&self) -> &MlxNmfCore {
        &self.core
    }

    fn wt_v(&self, w: &Array, k: usize) -> Result<Array, BixverseErrors> {
        spmm_plain(&self.spmm, &self.v_t, w, k, &self.core.stream)
    }

    fn v_ht(&self, h_t: &Array, k: usize) -> Result<Array, BixverseErrors> {
        spmm_plain(&self.spmm, &self.v, h_t, k, &self.core.stream)
    }

    fn top_k_svd(&self, k: usize) -> Result<SvdResults<f32>, BixverseErrors> {
        NmfInput::top_k_svd(&self.host, k)
    }
}

//////////////////////
// Device-side solve //
//////////////////////

/// A finished solve whose factors are still on the device.
///
/// Splits the readback from the solve so the two can be timed apart; the
/// caller normally goes straight through [`MlxNmfSolve::read`].
pub struct MlxNmfSolve {
    /// Left factor `[m, k]`
    w: Array,
    /// Right factor transposed `[n, k]`
    h_t: Array,
    /// Samples
    m: usize,
    /// Features
    n: usize,
    /// Components
    k: usize,
    /// Reconstruction loss at the last evaluated point
    final_loss: f32,
    /// Iterations performed
    n_iter: usize,
    /// Whether the tolerance was met
    converged: bool,
}

impl MlxNmfSolve {
    /// Copy the factors back into faer matrices.
    ///
    /// ### Returns
    ///
    /// The [`NmfResult`] with `W` (`m x k`) and `H` (`k x n`).
    pub fn read(self) -> Result<NmfResult<f32>, BixverseErrors> {
        eval_all(&[&self.w, &self.h_t], false)?;
        let (m, n, k) = (self.m, self.n, self.k);
        let w = self.w.as_f32()?;
        let h = self.h_t.as_f32()?;
        Ok(NmfResult {
            w: Mat::from_fn(m, k, |i, c| w[i * k + c]),
            h: Mat::from_fn(k, n, |r, j| h[j * k + r]),
            final_loss: self.final_loss,
            n_iter: self.n_iter,
            converged: self.converged,
        })
    }
}

//////////
// Main //
//////////

/// Frobenius HALS NMF on MLX, leaving the factors on the device.
///
/// Same algorithm and iteration order as
/// [`crate::methods::nmf_hals::nmf_hals`]. Initialisation runs on the host.
/// The only readback inside the loop is the objective, once per
/// `check_every` iterations, as per-row partials plus the two `[k, k]` Grams
/// finished in f64.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `k` - Number of components
/// * `opts` - Solver options; see [`HalsOpts`]
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// An [`MlxNmfSolve`]; call [`MlxNmfSolve::read`] for the factors.
///
/// ### Errors
///
/// * `MlxNmfRankTooLarge` if `k` is zero or above [`NMF_MAX_RANK`].
/// * `NmfRankTooLarge` if NNDSVD is requested and `V` has fewer than `k`
///   singular values.
/// * `MlxError` from the device path.
pub fn nmf_hals_mlx_device(
    v: &impl MlxNmfData,
    k: usize,
    opts: &HalsOpts<f32>,
    verbose: usize,
) -> Result<MlxNmfSolve, BixverseErrors> {
    check_rank(k)?;
    let (m, n) = v.shape();
    let sq_frob = v.sq_frob();
    let verbosity = parse_verbosity_level(verbose);

    let (w_host, h_host) = match &opts.init {
        NmfInit::Nndsvd => {
            let svd = v.top_k_svd(k)?;
            nndsvd_from_svd(&svd, k, m, n)?
        }
        NmfInit::Random { seed } => random_init(m, n, k, sq_frob, *seed),
    };

    let mut w = Array::from_f32(&to_row_major(w_host.as_ref()), &[to_i32(m), to_i32(k)]);
    let mut h_t = Array::from_f32(&col_major_flat(h_host.as_ref()), &[to_i32(n), to_i32(k)]);
    let eps = Array::from_f32(&[opts.eps], &[]);

    let denom = relative_denominator(sq_frob);
    let mut final_loss = f32::INFINITY;
    let mut last_loss = f32::INFINITY;
    let mut converged = false;
    let mut n_iter = 0usize;

    for iter in 0..opts.max_iter {
        n_iter = iter + 1;

        let (w_next, h_next) = iteration(v, &w, &h_t, &eps, k)?;
        eval_all(&[&w_next, &h_next], true)?;
        eval_all(&[&w, &h_t], false)?;
        w = w_next;
        h_t = h_next;

        if n_iter.is_multiple_of(opts.check_every) {
            let loss = objective(v, &w, &h_t, k)?;
            final_loss = loss;

            if n_iter > opts.check_every {
                if verbosity.normal_verbosity() {
                    println!(
                        "  NMF MLX: Iteration {} out of {} - current loss: {:.2?}",
                        iter + 1,
                        opts.max_iter,
                        loss
                    );
                }
                if (last_loss - loss).abs() / denom < opts.tol {
                    converged = true;
                    if verbosity.normal_verbosity() {
                        println!("  NMF MLX converged successfully after {} iters", iter + 1)
                    };
                    break;
                }
            }
            last_loss = loss;
        }
    }

    // `n_iter == 0` when `max_iter` is zero, and zero is a multiple of
    // everything, so without the first clause the loss stays at infinity.
    if n_iter == 0 || !n_iter.is_multiple_of(opts.check_every) {
        final_loss = objective(v, &w, &h_t, k)?;
    }
    eval_all(&[&w, &h_t], false)?;
    clear_cache()?;

    Ok(MlxNmfSolve {
        w,
        h_t,
        m,
        n,
        k,
        final_loss,
        n_iter,
        converged,
    })
}

/// Frobenius HALS NMF on MLX.
///
/// [`nmf_hals_mlx_device`] followed by the readback. Results are not
/// bit-identical to the CPU path; the factors agree to f32 GEMM-ordering
/// differences.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `k` - Number of components
/// * `opts` - Solver options; see [`HalsOpts`]
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// An [`NmfResult`] with `W` (`m x k`), `H` (`k x n`), the final loss, the
/// iteration count and whether the tolerance was met.
pub fn nmf_hals_mlx(
    v: &impl MlxNmfData,
    k: usize,
    opts: &HalsOpts<f32>,
    verbose: usize,
) -> Result<NmfResult<f32>, BixverseErrors> {
    nmf_hals_mlx_device(v, k, opts, verbose)?.read()
}

//////////////////////////
// Multiple run version //
//////////////////////////

/// Stabilised MLX NMF via random restarts.
///
/// Runs [`nmf_hals_mlx`] `n_runs` times with `base_seed + i`, reusing the
/// uploaded `V`, and column-binds the resulting `W` matrices. The `init` field
/// of `opts` is ignored and random initialisation is always used.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `k` - Number of components per run
/// * `n_runs` - Number of restarts, at least one
/// * `base_seed` - Seed offset; run `i` uses `base_seed + i`
/// * `opts` - Solver options; `init` is ignored
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// A [`StabilisedNmfResult`] with column-bound `W`, per-run `H`, per-run losses
/// and convergence flags, and the index of the lowest-loss run.
pub fn stabilised_nmf_mlx(
    v: &impl MlxNmfData,
    k: usize,
    n_runs: usize,
    base_seed: u64,
    opts: &HalsOpts<f32>,
    verbose: usize,
) -> Result<StabilisedNmfResult<f32>, BixverseErrors> {
    assert!(n_runs >= 1, "n_runs must be >= 1");
    let verbosity = parse_verbosity_level(verbose);
    let inner_verbose = verbose.saturating_sub(1);

    let mut runs: Vec<NmfResult<f32>> = Vec::with_capacity(n_runs);
    for i in 0..n_runs {
        let opts_i = HalsOpts {
            init: NmfInit::Random {
                seed: base_seed + i as u64,
            },
            ..opts.clone()
        };
        runs.push(nmf_hals_mlx(v, k, &opts_i, inner_verbose)?);
        if verbosity.normal_verbosity() {
            println!(" Finished stabilised MLX NMF run: {}", i + 1);
        }
    }

    let (m, _) = v.shape();
    let w_all = Mat::<f32>::from_fn(m, k * n_runs, |row, col| runs[col / k].w[(row, col % k)]);
    let losses: Vec<f32> = runs.iter().map(|r| r.final_loss).collect();
    let converged: Vec<bool> = runs.iter().map(|r| r.converged).collect();
    let h_per_run: Vec<Mat<f32>> = runs.into_iter().map(|r| r.h).collect();
    let best_idx = losses
        .iter()
        .enumerate()
        .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .map(|(i, _)| i)
        .unwrap_or(0);

    Ok(StabilisedNmfResult {
        w_all,
        h_per_run,
        losses,
        converged,
        best_idx,
    })
}

////////////
// Refits //
////////////

/// Refit `H` against a frozen `W`.
///
/// `W^T W` and `W^T V` are loop invariants, so `V` is touched once. `H` starts
/// at `opts.eps` and no normalisation is applied.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `w_host` - Frozen left factor `m x k`
/// * `opts` - Solver options; `init` is ignored
///
/// ### Returns
///
/// `(H of shape k x n, final ||V - W H||_F^2)`.
fn nmf_refit_h_mlx(
    v: &impl MlxNmfData,
    w_host: MatRef<f32>,
    opts: &HalsOpts<f32>,
) -> Result<(Mat<f32>, f32), BixverseErrors> {
    let (m, n) = v.shape();
    if w_host.nrows() != m {
        return Err(BixverseErrors::NmfDimensionMismatch {
            expected: m,
            found: w_host.nrows(),
        });
    }
    let k = w_host.ncols();
    check_rank(k)?;
    let s = &v.core().stream;

    let w = Array::from_f32(&to_row_major(w_host), &[to_i32(m), to_i32(k)]);
    let mut h_t = Array::from_f32(&vec![opts.eps; n * k], &[to_i32(n), to_i32(k)]);
    let eps = Array::from_f32(&[opts.eps], &[]);
    let b = gram(&w, s)?;
    let a = v.wt_v(&w, k)?;
    eval_all(&[&b, &a], false)?;

    let objective = |h_t: &Array| -> Result<f32, BixverseErrors> {
        let d = gram(h_t, s)?;
        let partials = row_dot_partials(&a, h_t, s)?;
        finish_objective(v.sq_frob(), &partials, &b, &d)
    };

    let final_loss = refit_loop(&mut h_t, &b, &a, &eps, n, k, opts, v.core(), objective)?;
    eval_all(&[&h_t], false)?;
    let h = h_t.as_f32()?;
    let out = Mat::from_fn(k, n, |r, j| h[j * k + r]);
    clear_cache()?;
    Ok((out, final_loss))
}

/// Refit `W` against a frozen `H`.
///
/// Mirror of [`nmf_refit_h_mlx`]: `H H^T` and `V H^T` are the invariants and
/// the objective pairs `W` against `V H^T`, so a changing `W` never forces
/// another pass over `V`.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `h_host` - Frozen right factor `k x n`
/// * `opts` - Solver options; `init` is ignored
///
/// ### Returns
///
/// `(W of shape m x k, final ||V - W H||_F^2)`.
fn nmf_refit_w_mlx(
    v: &impl MlxNmfData,
    h_host: MatRef<f32>,
    opts: &HalsOpts<f32>,
) -> Result<(Mat<f32>, f32), BixverseErrors> {
    let (m, n) = v.shape();
    if h_host.ncols() != n {
        return Err(BixverseErrors::NmfDimensionMismatch {
            expected: n,
            found: h_host.ncols(),
        });
    }
    let k = h_host.nrows();
    check_rank(k)?;
    let s = &v.core().stream;

    let h_t = Array::from_f32(&col_major_flat(h_host), &[to_i32(n), to_i32(k)]);
    let mut w = Array::from_f32(&vec![opts.eps; m * k], &[to_i32(m), to_i32(k)]);
    let eps = Array::from_f32(&[opts.eps], &[]);
    let d = gram(&h_t, s)?;
    let c = v.v_ht(&h_t, k)?;
    eval_all(&[&d, &c], false)?;

    let objective = |w: &Array| -> Result<f32, BixverseErrors> {
        let b = gram(w, s)?;
        let partials = row_dot_partials(w, &c, s)?;
        finish_objective(v.sq_frob(), &partials, &b, &d)
    };

    let final_loss = refit_loop(&mut w, &d, &c, &eps, m, k, opts, v.core(), objective)?;
    eval_all(&[&w], false)?;
    let wf = w.as_f32()?;
    let out = Mat::from_fn(m, k, |i, c| wf[i * k + c]);
    clear_cache()?;
    Ok((out, final_loss))
}

/// Shared loop of the two refits: sweep the free factor against fixed `G` and
/// `P` until the objective stalls.
///
/// ### Params
///
/// * `x` - Free factor `[n_rows, k]`, replaced by the result
/// * `g` - Fixed Gram `[k, k]`
/// * `p` - Fixed data product `[n_rows, k]`
/// * `eps` - Non-negativity floor, a 0-d array
/// * `n_rows` - Rows of `x`
/// * `k` - Number of components
/// * `opts` - Solver options
/// * `core` - Device state of the input, for the stream, kernels and norm
/// * `objective` - Objective of a candidate `x`
///
/// ### Returns
///
/// The final reconstruction error.
#[allow(clippy::too_many_arguments)]
fn refit_loop(
    x: &mut Array,
    g: &Array,
    p: &Array,
    eps: &Array,
    n_rows: usize,
    k: usize,
    opts: &HalsOpts<f32>,
    core: &MlxNmfCore,
    objective: impl Fn(&Array) -> Result<f32, BixverseErrors>,
) -> Result<f32, BixverseErrors> {
    let denom = relative_denominator(core.sq_frob);
    let mut final_loss = f32::INFINITY;
    let mut last_loss = f32::INFINITY;
    let mut n_iter = 0usize;

    for iter in 0..opts.max_iter {
        n_iter = iter + 1;
        let next = core.kernels.sweep(x, g, p, eps, n_rows, k, &core.stream)?;
        eval_all(&[&next], true)?;
        eval_all(&[x], false)?;
        *x = next;

        if n_iter.is_multiple_of(opts.check_every) {
            let loss = objective(x)?;
            final_loss = loss;
            if n_iter > opts.check_every && (last_loss - loss).abs() / denom < opts.tol {
                break;
            }
            last_loss = loss;
        }
    }

    if n_iter == 0 || !n_iter.is_multiple_of(opts.check_every) {
        final_loss = objective(x)?;
    }
    Ok(final_loss)
}

///////////////
// Consensus //
///////////////

/// Consensus NMF on MLX.
///
/// Restarts through [`stabilised_nmf_mlx`], the shared host-side
/// [`consensus_from_restarts`], then a refit of the partner factor against the
/// consensus. Same structure and reported quantities as
/// [`crate::methods::nmf_hals::consensus::nmf_consensus`].
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `k` - Number of components, at least two
/// * `n_runs` - Number of restarts, at least two
/// * `base_seed` - Seed offset; run `i` uses `base_seed + i`
/// * `opts` - Solver options; the `init` field is ignored by the restarts
/// * `params` - Optional [`ConsensusParams`], defaulted if `None`
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// A [`ConsensusNmfResult`]. `error` and `run_errors` are relative to
/// `||V||_F^2`.
///
/// ### Errors
///
/// * `NmfConsensusInvalidK` if `k < 2`, `NmfConsensusTooFewRuns` if
///   `n_runs < 2`, `NmfConsensusEmptyCluster` if a cluster came out empty.
/// * Anything the solve or the consensus step raises.
pub fn nmf_consensus_mlx(
    v: &impl MlxNmfData,
    k: usize,
    n_runs: usize,
    base_seed: u64,
    opts: &HalsOpts<f32>,
    params: Option<ConsensusParams<f32>>,
    verbose: usize,
) -> Result<ConsensusNmfResult<f32>, BixverseErrors> {
    let params = params.unwrap_or_default();
    if k < 2 {
        return Err(BixverseErrors::NmfConsensusInvalidK { k });
    }
    if n_runs < 2 {
        return Err(BixverseErrors::NmfConsensusTooFewRuns { n_runs });
    }

    let restarts = stabilised_nmf_mlx(v, k, n_runs, base_seed, opts, verbose)?;
    let clusters = consensus_from_restarts(&restarts, k, &params, verbose)?;
    if let Some(cluster) = clusters.sizes.iter().position(|&s| s == 0) {
        return Err(BixverseErrors::NmfConsensusEmptyCluster { cluster, k });
    }

    let (w, h, loss) = match params.target {
        ConsensusTarget::HRows => {
            let h = clusters.consensus.clone();
            let (w, loss) = nmf_refit_w_mlx(v, h.as_ref(), opts)?;
            (w, h, loss)
        }
        ConsensusTarget::WColumns => {
            let w = clusters.consensus.transpose().to_owned();
            let (h, loss) = nmf_refit_h_mlx(v, w.as_ref(), opts)?;
            (w, h, loss)
        }
    };

    let sq_frob = v.sq_frob();
    let denom = if sq_frob > 0.0 { sq_frob } else { 1.0 };
    let run_errors: Vec<f32> = restarts.losses.iter().map(|&l| l / denom).collect();

    Ok(ConsensusNmfResult {
        w,
        h,
        error: loss / denom,
        clusters,
        run_errors,
    })
}

/// Sweep k on MLX and report stability against error.
///
/// One row per rank, no refit and no factors retained. `V` is uploaded once
/// for the whole sweep and the ranks run sequentially. Mirrors
/// `gpu::methods_gpu::nmf_consensus_gpu::nmf_k_sweep_gpu`.
///
/// ### Params
///
/// * `v` - Device-resident `V`
/// * `k_range` - Ranks to evaluate, non-empty, every entry at least two
/// * `n_runs` - Restarts per rank, at least two
/// * `base_seed` - Seed offset; each rank gets a disjoint block of seeds
/// * `opts` - Solver options; the `init` field is ignored by the restarts
/// * `params` - Optional [`ConsensusParams`], defaulted if `None`
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// One [`KSweepEntry`] per entry of `k_range`, in the order given.
///
/// ### Errors
///
/// * `NmfKSweepEmptyRange` if `k_range` is empty.
/// * `NmfConsensusTooFewRuns` if `n_runs < 2`, `NmfConsensusInvalidK` if any
///   rank is below two.
/// * Anything the solve or the consensus step raises, except too few surviving
///   components, which is recorded as `consensus_failed`.
pub fn nmf_k_sweep_mlx(
    v: &impl MlxNmfData,
    k_range: &[usize],
    n_runs: usize,
    base_seed: u64,
    opts: &HalsOpts<f32>,
    params: Option<ConsensusParams<f32>>,
    verbose: usize,
) -> Result<Vec<KSweepEntry<f32>>, BixverseErrors> {
    if k_range.is_empty() {
        return Err(BixverseErrors::NmfKSweepEmptyRange);
    }
    if n_runs < 2 {
        return Err(BixverseErrors::NmfConsensusTooFewRuns { n_runs });
    }
    if let Some(&k) = k_range.iter().find(|&&k| k < 2) {
        return Err(BixverseErrors::NmfConsensusInvalidK { k });
    }

    let params = params.unwrap_or_default();
    let verbosity = parse_verbosity_level(verbose);
    let inner_verbose = verbose.saturating_sub(1);
    let sq_frob = v.sq_frob();
    let denom = if sq_frob > 0.0 { sq_frob } else { 1.0 };

    let mut out = Vec::with_capacity(k_range.len());
    for (i, &k) in k_range.iter().enumerate() {
        if verbosity.normal_verbosity() {
            println!(
                "Running MLX NMF k sweep: k = {} ({} of {})",
                k,
                i + 1,
                k_range.len()
            );
        }

        let seed = base_seed + (i * n_runs) as u64;
        let restarts = stabilised_nmf_mlx(v, k, n_runs, seed, opts, inner_verbose)?;

        let clusters = match consensus_from_restarts(&restarts, k, &params, inner_verbose) {
            Ok(clusters) => Some(clusters),
            Err(BixverseErrors::NmfConsensusTooFewComponents { n_surviving, .. }) => {
                if verbosity.normal_verbosity() {
                    println!(
                        "  k = {}: only {} components survived the density filter (need {}); stability not scored",
                        k, n_surviving, k
                    );
                }
                None
            }
            Err(e) => return Err(e),
        };

        out.push(KSweepEntry {
            k,
            stability: clusters.as_ref().map(|c| c.stability).unwrap_or(f32::NAN),
            best_error: restarts.losses[restarts.best_idx] / denom,
            median_error: median(&restarts.losses) / denom,
            consensus_failed: clusters.is_none(),
            n_dropped: clusters.as_ref().map(|c| c.n_dropped).unwrap_or(k * n_runs),
            n_empty_clusters: clusters.as_ref().map(|c| c.n_empty_clusters).unwrap_or(0),
            n_converged: restarts.converged.iter().filter(|&&c| c).count(),
        });
    }

    Ok(out)
}

//////////////////
// Entry points //
//////////////////

/// Parse the pre-processing string, warning and defaulting on unknown input,
/// as the CPU and cubecl entry points do.
///
/// ### Params
///
/// * `preprocessing` - String to parse, see [`parse_nmf_processing`]
///
/// ### Returns
///
/// The [`NmfPreprocessing`] to apply.
fn resolve_preprocessing(preprocessing: &str) -> NmfPreprocessing {
    parse_nmf_processing(preprocessing).unwrap_or_else(|| {
        println!(
            "Unknown string provided: {:?}. Using the default (No additional pre-processing)",
            preprocessing
        );
        NmfPreprocessing::default()
    })
}

/// Run MLX NMF on a dense matrix (single run).
///
/// ### Params
///
/// * `data` - The matrix to factorise, samples x features
/// * `k` - Number of components
/// * `preprocessing` - Column-scaling mode, see [`parse_nmf_processing`]
/// * `nmf_hals_params` - Optional [`HalsOpts`]
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// An [`NmfResult`], or errors from validation or the device path.
pub fn nmf_single_run_mlx(
    data: MatRef<f32>,
    k: usize,
    preprocessing: &str,
    nmf_hals_params: Option<HalsOpts<f32>>,
    verbose: usize,
) -> Result<NmfResult<f32>, BixverseErrors> {
    let opts = nmf_hals_params.unwrap_or_default();
    let processed = nmf_process_dense(data, &resolve_preprocessing(preprocessing));
    let input = MlxDenseNmfInput::new(processed.as_ref())?;
    nmf_hals_mlx(&input, k, &opts, verbose)
}

/// Run MLX consensus NMF on a dense matrix.
///
/// ### Params
///
/// * `data` - The matrix to factorise, samples x features
/// * `k` - Number of components, at least two
/// * `preprocessing` - Column-scaling mode, see [`parse_nmf_processing`]
/// * `nmf_hals_params` - Optional [`HalsOpts`]
/// * `consensus_params` - Optional [`ConsensusParams`]
/// * `n_runs` - Number of restarts, at least two
/// * `base_seed` - Seed offset
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// A [`ConsensusNmfResult`], or errors from validation or the device path.
#[allow(clippy::too_many_arguments)]
pub fn nmf_consensus_run_mlx(
    data: MatRef<f32>,
    k: usize,
    preprocessing: &str,
    nmf_hals_params: Option<HalsOpts<f32>>,
    consensus_params: Option<ConsensusParams<f32>>,
    n_runs: usize,
    base_seed: usize,
    verbose: usize,
) -> Result<ConsensusNmfResult<f32>, BixverseErrors> {
    let opts = nmf_hals_params.unwrap_or_default();
    let processed = nmf_process_dense(data, &resolve_preprocessing(preprocessing));
    let input = MlxDenseNmfInput::new(processed.as_ref())?;
    nmf_consensus_mlx(
        &input,
        k,
        n_runs,
        base_seed as u64,
        &opts,
        consensus_params,
        verbose,
    )
}

/// Pre-process a sparse matrix and upload it.
///
/// Pre-processing scales columns and needs CSC, so the caller's matrix is
/// converted first exactly as the CPU and cubecl sparse paths do.
///
/// ### Params
///
/// * `data` - Sparse `V`, samples x features
/// * `preprocessing` - Column-scaling mode
/// * `use_second_layer` - Use `data_2` (normalised counts) instead of `data`
/// * `verbose` - Verbosity level, used only for the transpose notice
///
/// ### Returns
///
/// The uploaded [`MlxSparseNmfInput`], or errors from validation or the
/// upload.
fn build_sparse_input(
    data: CompressedSparseData2<f32>,
    preprocessing: &str,
    use_second_layer: bool,
    verbose: usize,
) -> Result<MlxSparseNmfInput, BixverseErrors> {
    let csc = ensure_csc(data, parse_verbosity_level(verbose));
    let processed = nmf_process_sparse(
        &csc,
        &resolve_preprocessing(preprocessing),
        use_second_layer,
    )?;
    let host = if use_second_layer {
        SparseInput::<f32, f32>::from_secondary(&processed)?
    } else {
        SparseInput::<f32, f32>::from_primary(&processed)?
    };
    MlxSparseNmfInput::new(host)
}

/// Run MLX NMF on a sparse matrix (single run).
///
/// ### Params
///
/// * `data` - Sparse `V`, samples x features
/// * `k` - Number of components
/// * `preprocessing` - Column-scaling mode, see [`parse_nmf_processing`]
/// * `use_second_layer` - Use `data_2` (normalised counts) instead of `data`
/// * `nmf_hals_params` - Optional [`HalsOpts`]
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// An [`NmfResult`], or errors from validation or the device path.
pub fn nmf_single_run_sparse_mlx(
    data: CompressedSparseData2<f32>,
    k: usize,
    preprocessing: &str,
    use_second_layer: bool,
    nmf_hals_params: Option<HalsOpts<f32>>,
    verbose: usize,
) -> Result<NmfResult<f32>, BixverseErrors> {
    let opts = nmf_hals_params.unwrap_or_default();
    let input = build_sparse_input(data, preprocessing, use_second_layer, verbose)?;
    nmf_hals_mlx(&input, k, &opts, verbose)
}

/// Run MLX consensus NMF on a sparse matrix.
///
/// ### Params
///
/// * `data` - Sparse `V`, samples x features
/// * `k` - Number of components, at least two
/// * `preprocessing` - Column-scaling mode, see [`parse_nmf_processing`]
/// * `use_second_layer` - Use `data_2` (normalised counts) instead of `data`
/// * `nmf_hals_params` - Optional [`HalsOpts`]
/// * `consensus_params` - Optional [`ConsensusParams`]
/// * `n_runs` - Number of restarts, at least two
/// * `base_seed` - Seed offset
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// A [`ConsensusNmfResult`], or errors from validation or the device path.
#[allow(clippy::too_many_arguments)]
pub fn nmf_consensus_run_sparse_mlx(
    data: CompressedSparseData2<f32>,
    k: usize,
    preprocessing: &str,
    use_second_layer: bool,
    nmf_hals_params: Option<HalsOpts<f32>>,
    consensus_params: Option<ConsensusParams<f32>>,
    n_runs: usize,
    base_seed: usize,
    verbose: usize,
) -> Result<ConsensusNmfResult<f32>, BixverseErrors> {
    let opts = nmf_hals_params.unwrap_or_default();
    let input = build_sparse_input(data, preprocessing, use_second_layer, verbose)?;
    nmf_consensus_mlx(
        &input,
        k,
        n_runs,
        base_seed as u64,
        &opts,
        consensus_params,
        verbose,
    )
}

/// Run MLX NMF on a sparse matrix across random restarts.
///
/// ### Params
///
/// * `data` - Sparse `V`, samples x features
/// * `k` - Number of components per run
/// * `preprocessing` - Column-scaling mode, see [`parse_nmf_processing`]
/// * `use_second_layer` - Use `data_2` (normalised counts) instead of `data`
/// * `nmf_hals_params` - Optional [`HalsOpts`]; the `init` field is ignored
/// * `n_runs` - Number of restarts, at least one
/// * `base_seed` - Seed offset
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// A [`StabilisedNmfResult`], or errors from validation or the device path.
#[allow(clippy::too_many_arguments)]
pub fn nmf_multiple_run_sparse_mlx(
    data: CompressedSparseData2<f32>,
    k: usize,
    preprocessing: &str,
    use_second_layer: bool,
    nmf_hals_params: Option<HalsOpts<f32>>,
    n_runs: usize,
    base_seed: usize,
    verbose: usize,
) -> Result<StabilisedNmfResult<f32>, BixverseErrors> {
    let opts = nmf_hals_params.unwrap_or_default();
    let input = build_sparse_input(data, preprocessing, use_second_layer, verbose)?;
    stabilised_nmf_mlx(&input, k, n_runs, base_seed as u64, &opts, verbose)
}

/// Sweep k with MLX consensus NMF on a sparse matrix.
///
/// `V` uploads once and serves all `k_range.len() * n_runs` solves.
///
/// ### Params
///
/// * `data` - Sparse `V`, samples x features
/// * `k_range` - Ranks to evaluate, non-empty, every entry at least two
/// * `preprocessing` - Column-scaling mode, see [`parse_nmf_processing`]
/// * `use_second_layer` - Use `data_2` (normalised counts) instead of `data`
/// * `nmf_hals_params` - Optional [`HalsOpts`]
/// * `consensus_params` - Optional [`ConsensusParams`]
/// * `n_runs` - Restarts per rank, at least two
/// * `base_seed` - Seed offset
/// * `verbose` - `0` silent, `1` normal, `2` detailed
///
/// ### Returns
///
/// One [`KSweepEntry`] per rank, or errors from validation or the device path.
#[allow(clippy::too_many_arguments)]
pub fn nmf_k_sweep_run_sparse_mlx(
    data: CompressedSparseData2<f32>,
    k_range: &[usize],
    preprocessing: &str,
    use_second_layer: bool,
    nmf_hals_params: Option<HalsOpts<f32>>,
    consensus_params: Option<ConsensusParams<f32>>,
    n_runs: usize,
    base_seed: usize,
    verbose: usize,
) -> Result<Vec<KSweepEntry<f32>>, BixverseErrors> {
    if k_range.is_empty() {
        return Err(BixverseErrors::NmfKSweepEmptyRange);
    }
    let opts = nmf_hals_params.unwrap_or_default();
    let input = build_sparse_input(data, preprocessing, use_second_layer, verbose)?;
    nmf_k_sweep_mlx(
        &input,
        k_range,
        n_runs,
        base_seed as u64,
        &opts,
        consensus_params,
        verbose,
    )
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;
    use crate::methods::nmf_hals::{
        compute_objective, gram_h_ht, gram_wt_w, hals_sweep_cols, nmf_hals,
    };
    use approx::assert_relative_eq;

    /// Deterministic non-negative `m x n` matrix with an exact rank-`k` core.
    fn build_v(m: usize, n: usize, k: usize) -> Mat<f32> {
        let w = Mat::<f32>::from_fn(m, k, |i, c| (((i * 13 + c * 29) % 17) as f32) * 0.1 + 0.05);
        let h = Mat::<f32>::from_fn(k, n, |r, j| (((r * 31 + j * 11) % 19) as f32) * 0.1 + 0.05);
        w * h
    }

    /// Fixed `W` (`m x k`) and `H` (`k x n`) for the product tests.
    fn build_factors(m: usize, n: usize, k: usize) -> (Mat<f32>, Mat<f32>) {
        let w = Mat::<f32>::from_fn(m, k, |i, c| (((i * 5 + c * 3) % 7) as f32) * 0.2 + 0.1);
        let h = Mat::<f32>::from_fn(k, n, |r, j| (((r * 7 + j * 5) % 9) as f32) * 0.2 + 0.1);
        (w, h)
    }

    /// Upload `W` as `[m, k]` and `H` as `[n, k]`.
    fn upload(w: &Mat<f32>, h: &Mat<f32>) -> (Array, Array) {
        let (m, k) = (w.nrows(), w.ncols());
        let n = h.ncols();
        (
            Array::from_f32(&to_row_major(w.as_ref()), &[m as i32, k as i32]),
            Array::from_f32(&col_major_flat(h.as_ref()), &[n as i32, k as i32]),
        )
    }

    /// Evaluate and copy an array out.
    fn read(x: &Array) -> Vec<f32> {
        eval_all(&[x], false).unwrap();
        x.as_f32().unwrap().to_vec()
    }

    /// Relative comparison floored at one, with the all-zeros guard.
    fn assert_close(got: &[f32], want: &[f32], tol: f32) -> f32 {
        assert_eq!(got.len(), want.len(), "length mismatch");
        assert!(
            got.iter().any(|v| v.abs() > 1e-6),
            "output is all zeros, the device did no work"
        );
        let mut worst = 0f32;
        for (i, (&a, &b)) in got.iter().zip(want.iter()).enumerate() {
            let rel = (a - b).abs() / b.abs().max(1.0);
            worst = worst.max(rel);
            assert!(rel <= tol, "index {i}: got {a} want {b}");
        }
        worst
    }

    #[test]
    fn test_dense_products_match_cpu() {
        let (m, n, k) = (43usize, 29usize, 6usize);
        let v = build_v(m, n, k);
        let cpu = DenseInput::new(v.as_ref()).unwrap();
        let mlx = MlxDenseNmfInput::new(v.as_ref()).unwrap();
        assert_eq!(mlx.shape(), NmfInput::shape(&cpu));

        let (w, h) = build_factors(m, n, k);
        let (w_d, h_d) = upload(&w, &h);
        let a = mlx.wt_v(&w_d, k).unwrap();
        let c = mlx.v_ht(&h_d, k).unwrap();

        let mut wtv = Mat::<f32>::zeros(k, n);
        let mut vht = Mat::<f32>::zeros(m, k);
        cpu.wt_v(w.as_ref(), &mut wtv);
        cpu.v_ht(h.as_ref(), &mut vht);

        let d1 = assert_close(&read(&a), &to_row_major(wtv.transpose()), 1e-4);
        let d2 = assert_close(&read(&c), &to_row_major(vht.as_ref()), 1e-4);
        println!("products max rel diff: {d1:e} {d2:e}");
    }

    #[test]
    fn test_grams_match_cpu() {
        let (m, n, k) = (97usize, 61usize, 8usize);
        let (w, h) = build_factors(m, n, k);
        let (w_d, h_d) = upload(&w, &h);
        let s = Stream::default_gpu();

        let mut wtw = Mat::<f32>::zeros(k, k);
        let mut hht = Mat::<f32>::zeros(k, k);
        gram_wt_w(w.as_ref(), &mut wtw);
        gram_h_ht(h.as_ref(), &mut hht);

        let d1 = assert_close(
            &read(&gram(&w_d, &s).unwrap()),
            &to_row_major(wtw.as_ref()),
            1e-4,
        );
        let d2 = assert_close(
            &read(&gram(&h_d, &s).unwrap()),
            &to_row_major(hht.as_ref()),
            1e-4,
        );
        println!("grams max rel diff: {d1:e} {d2:e}");
    }

    // The sweep against the CPU W sweep at every tier, both edges of the
    // smallest tier and the largest rank. More rows than one threadgroup so
    // the row guard and the threadgroup offset are both exercised.
    #[test]
    fn test_sweep_matches_cpu_across_ranks() {
        let s = Stream::default_gpu();
        let kernels = HalsKernels::new();
        let rows = 300usize;
        let eps = 1e-10f32;
        for k in [1usize, 5, 16, 17, 33, 64, 65, 128] {
            let x = Mat::<f32>::from_fn(rows, k, |i, c| {
                (((i * 7 + c * 13) % 11) as f32) * 0.1 + 0.05
            });
            let y =
                Mat::<f32>::from_fn(rows, k, |i, c| (((i * 3 + c * 5) % 13) as f32) * 0.1 + 0.02);
            let mut g = Mat::<f32>::zeros(k, k);
            gram_wt_w(y.as_ref(), &mut g);
            let p = Mat::<f32>::from_fn(rows, k, |i, c| (((i * 11 + c * 2) % 7) as f32) * 0.5);

            let mut want = x.clone();
            hals_sweep_cols(&mut want, g.as_ref(), p.as_ref(), eps);

            let x_d = Array::from_f32(&to_row_major(x.as_ref()), &[rows as i32, k as i32]);
            let g_d = Array::from_f32(&to_row_major(g.as_ref()), &[k as i32, k as i32]);
            let p_d = Array::from_f32(&to_row_major(p.as_ref()), &[rows as i32, k as i32]);
            let eps_d = Array::from_f32(&[eps], &[]);
            let got = kernels
                .sweep(&x_d, &g_d, &p_d, &eps_d, rows, k, &s)
                .unwrap();
            let d = assert_close(&read(&got), &to_row_major(want.as_ref()), 1e-4);
            println!("sweep k = {k}: max rel diff {d:e}");
        }
    }

    #[test]
    fn test_objective_matches_cpu() {
        let (m, n, k) = (53usize, 41usize, 6usize);
        let v = build_v(m, n, k);
        let cpu = DenseInput::new(v.as_ref()).unwrap();
        let mlx = MlxDenseNmfInput::new(v.as_ref()).unwrap();

        let (w, h) = build_factors(m, n, k);
        let (w_d, h_d) = upload(&w, &h);
        let got = objective(&mlx, &w_d, &h_d, k).unwrap();

        let mut wtv = Mat::<f32>::zeros(k, n);
        let mut wtw = Mat::<f32>::zeros(k, k);
        let mut hht = Mat::<f32>::zeros(k, k);
        cpu.wt_v(w.as_ref(), &mut wtv);
        gram_wt_w(w.as_ref(), &mut wtw);
        gram_h_ht(h.as_ref(), &mut hht);
        let want = compute_objective(
            NmfInput::sq_frob(&cpu),
            h.as_ref(),
            wtv.as_ref(),
            wtw.as_ref(),
            hht.as_ref(),
        );

        assert!(got > 0.0, "objective is zero, the device did no work");
        let rel = (got - want).abs() / want.abs().max(1.0);
        println!("objective rel diff: {rel:e}");
        assert!(rel <= 1e-3, "got {got} want {want}");
    }

    /// One CPU and one MLX solve from the same random initialisation.
    fn solve_pair(
        m: usize,
        n: usize,
        k: usize,
        max_iter: usize,
    ) -> (NmfResult<f32>, NmfResult<f32>, f32) {
        let v = build_v(m, n, k);
        let opts = HalsOpts::<f32> {
            max_iter,
            tol: 1e-9,
            eps: 1e-10,
            check_every: 10,
            init: NmfInit::Random { seed: 7 },
        };
        let cpu_in = DenseInput::new(v.as_ref()).unwrap();
        let cpu = nmf_hals(&cpu_in, k, &opts, 0).unwrap();
        let mlx_in = MlxDenseNmfInput::new(v.as_ref()).unwrap();
        let mlx = nmf_hals_mlx(&mlx_in, k, &opts, 0).unwrap();
        (cpu, mlx, NmfInput::sq_frob(&cpu_in))
    }

    // Pins the whole loop order: Gram, product, sweep, Gram, product, sweep,
    // normalise.
    #[test]
    fn test_one_iteration_matches_cpu_elementwise() {
        let (cpu, mlx, _) = solve_pair(47, 31, 5, 1);
        assert_eq!(cpu.n_iter, 1);
        assert_eq!(mlx.n_iter, 1);
        let dw = assert_close(
            &to_row_major(mlx.w.as_ref()),
            &to_row_major(cpu.w.as_ref()),
            1e-4,
        );
        let dh = assert_close(
            &to_row_major(mlx.h.as_ref()),
            &to_row_major(cpu.h.as_ref()),
            1e-4,
        );
        println!("one iteration max rel diff: W {dw:e} H {dh:e}");
    }

    #[test]
    fn test_converged_loss_matches_cpu() {
        let (cpu, mlx, sq_frob) = solve_pair(47, 31, 5, 300);
        let cpu_rel = cpu.final_loss / sq_frob;
        let mlx_rel = mlx.final_loss / sq_frob;
        println!("converged relative losses: cpu {cpu_rel:e} mlx {mlx_rel:e}");
        assert!(
            (cpu_rel - mlx_rel).abs() < 1e-3,
            "relative losses disagree: cpu {cpu_rel} mlx {mlx_rel}"
        );
    }

    #[test]
    fn test_recovers_rank_k_dense() {
        let (m, n, k) = (60usize, 40usize, 3usize);
        let v = build_v(m, n, k);
        let opts = HalsOpts::<f32> {
            max_iter: 400,
            tol: 1e-9,
            eps: 1e-10,
            check_every: 10,
            init: NmfInit::Nndsvd,
        };
        let input = MlxDenseNmfInput::new(v.as_ref()).unwrap();
        let res = nmf_hals_mlx(&input, k, &opts, 0).unwrap();

        let rel = res.final_loss / input.sq_frob();
        println!("rank-k recovery relative loss: {rel:e}");
        assert!(rel < 1e-3, "relative loss {rel} is too high");
        assert!((0..m).all(|i| (0..k).all(|c| res.w[(i, c)] >= 0.0)));
        assert!((0..k).all(|r| (0..n).all(|j| res.h[(r, j)] >= 0.0)));
    }

    #[test]
    fn test_rejects_out_of_range_ranks() {
        let v = build_v(20, 10, 2);
        let input = MlxDenseNmfInput::new(v.as_ref()).unwrap();
        for k in [0usize, NMF_MAX_RANK + 1] {
            assert!(matches!(
                nmf_hals_mlx(&input, k, &HalsOpts::default(), 0),
                Err(BixverseErrors::MlxNmfRankTooLarge { .. })
            ));
        }
    }

    #[test]
    fn test_zero_iterations_reports_a_finite_loss() {
        let (m, n, k) = (30usize, 20usize, 3usize);
        let v = build_v(m, n, k);
        let input = MlxDenseNmfInput::new(v.as_ref()).unwrap();
        let opts = HalsOpts::<f32> {
            max_iter: 0,
            init: NmfInit::Random { seed: 1 },
            ..HalsOpts::default()
        };
        let res = nmf_hals_mlx(&input, k, &opts, 0).unwrap();
        assert_eq!(res.n_iter, 0);
        assert!(res.final_loss.is_finite());
    }

    /// Exact rank-k product with clearly separated components.
    fn rank_k_input(m: usize, n: usize, k: usize) -> Mat<f32> {
        let w = Mat::<f32>::from_fn(m, k, |i, j| if i % k == j { 5.0 } else { 0.3 });
        let h = Mat::<f32>::from_fn(k, n, |i, j| if j % k == i { 4.0 } else { 0.2 });
        w * h
    }

    // Both refit arms through the consensus entry point.
    #[test]
    fn test_consensus_returns_usable_factors() {
        let (m, n, k, n_runs) = (25usize, 18usize, 2usize, 4usize);
        let v = rank_k_input(m, n, k);
        let opts = HalsOpts::<f32>::new(200, 1e-7, 1e-10, 10, NmfInit::Nndsvd);
        for target in [ConsensusTarget::HRows, ConsensusTarget::WColumns] {
            let params = ConsensusParams::<f32>::new(target, None, None, 100, 3, 42);
            let res = nmf_consensus_run_mlx(
                v.as_ref(),
                k,
                "none",
                Some(opts.clone()),
                Some(params),
                n_runs,
                0,
                0,
            )
            .unwrap();
            assert_eq!((res.w.nrows(), res.w.ncols()), (m, k));
            assert_eq!((res.h.nrows(), res.h.ncols()), (k, n));
            assert_eq!(res.run_errors.len(), n_runs);
            assert!(
                res.error < 1e-4,
                "consensus error {} is too high",
                res.error
            );
        }
    }

    #[test]
    fn test_single_run_entry_point() {
        let v = build_v(40, 30, 3);
        let res = nmf_single_run_mlx(v.as_ref(), 3, "none", None, 0).unwrap();
        assert_eq!((res.w.nrows(), res.w.ncols()), (40, 3));
        assert_eq!((res.h.nrows(), res.h.ncols()), (3, 30));
        assert!(res.final_loss.is_finite());
    }

    ////////////
    // Sparse //
    ////////////

    /// Row left empty in every sparse fixture.
    const EMPTY_ROW: usize = 3;

    /// Column left empty in every sparse fixture.
    const EMPTY_COL: usize = 4;

    /// CSR of a dense matrix.
    fn to_csr(v: &Mat<f32>) -> CompressedSparseData2<f32> {
        CompressedSparseData2::<f32, f32>::from_dense_matrix(
            v.as_ref(),
            CompressedSparseFormat::Csr,
        )
    }

    /// Sparse `m x n` matrix with a fixed pattern, about one entry in five,
    /// with [`EMPTY_ROW`] and [`EMPTY_COL`] empty.
    fn build_sparse_v(m: usize, n: usize) -> Mat<f32> {
        Mat::<f32>::from_fn(m, n, |i, j| {
            if i != EMPTY_ROW && j != EMPTY_COL && (i * 7 + j * 3) % 5 == 0 {
                (((i * 19 + j * 23) % 13) as f32) * 0.25 + 0.5
            } else {
                0.0
            }
        })
    }

    /// Exact rank-`k` sparse matrix: `V[i, j]` is non-zero only when
    /// `i % k == j % k`, so the density is `1 / k`. [`EMPTY_ROW`] and
    /// [`EMPTY_COL`] are empty.
    fn sparse_rank_k(m: usize, n: usize, k: usize) -> Mat<f32> {
        let w = Mat::<f32>::from_fn(m, k, |i, c| {
            if i % k == c && i != EMPTY_ROW {
                1.0 + ((i * 7) % 5) as f32 * 0.2
            } else {
                0.0
            }
        });
        let h = Mat::<f32>::from_fn(k, n, |c, j| {
            if j % k == c && j != EMPTY_COL {
                1.0 + ((j * 3) % 7) as f32 * 0.1
            } else {
                0.0
            }
        });
        w * h
    }

    // Long enough CSC rows (about 40 non-zeros) for a ragged second batch of
    // lanes, and ranks on both sides of the SIMD width.
    #[test]
    fn test_sparse_products_match_cpu() {
        let (m, n) = (200usize, 90usize);
        let dense = build_sparse_v(m, n);
        let csr = to_csr(&dense);
        let cpu = SparseInput::<f32, f32>::from_primary(&csr).unwrap();
        let mlx = MlxSparseNmfInput::new(SparseInput::from_primary(&csr).unwrap()).unwrap();
        assert_eq!(mlx.shape(), (m, n));

        let dense_sq = NmfInput::sq_frob(&DenseInput::new(dense.as_ref()).unwrap());
        assert!(
            (mlx.sq_frob() - dense_sq).abs() <= 1e-4 * dense_sq,
            "sparse and dense Frobenius norms disagree"
        );

        for k in [5usize, 30, 40] {
            let (w, h) = build_factors(m, n, k);
            let (w_d, h_d) = upload(&w, &h);
            let a = read(&mlx.wt_v(&w_d, k).unwrap());
            let c = read(&mlx.v_ht(&h_d, k).unwrap());

            let mut wtv = Mat::<f32>::zeros(k, n);
            let mut vht = Mat::<f32>::zeros(m, k);
            cpu.wt_v(w.as_ref(), &mut wtv);
            cpu.v_ht(h.as_ref(), &mut vht);

            let d1 = assert_close(&a, &to_row_major(wtv.transpose()), 1e-4);
            let d2 = assert_close(&c, &to_row_major(vht.as_ref()), 1e-4);
            assert!(
                a[EMPTY_COL * k..(EMPTY_COL + 1) * k]
                    .iter()
                    .all(|&x| x == 0.0)
            );
            assert!(
                c[EMPTY_ROW * k..(EMPTY_ROW + 1) * k]
                    .iter()
                    .all(|&x| x == 0.0)
            );
            println!("sparse products k = {k}: max rel diff {d1:e} {d2:e}");
        }
    }

    #[test]
    fn test_sparse_objective_matches_cpu() {
        let (m, n, k) = (53usize, 41usize, 6usize);
        let csr = to_csr(&build_sparse_v(m, n));
        let cpu = SparseInput::<f32, f32>::from_primary(&csr).unwrap();
        let mlx = MlxSparseNmfInput::new(SparseInput::from_primary(&csr).unwrap()).unwrap();

        let (w, h) = build_factors(m, n, k);
        let (w_d, h_d) = upload(&w, &h);
        let got = objective(&mlx, &w_d, &h_d, k).unwrap();

        let mut wtv = Mat::<f32>::zeros(k, n);
        let mut wtw = Mat::<f32>::zeros(k, k);
        let mut hht = Mat::<f32>::zeros(k, k);
        cpu.wt_v(w.as_ref(), &mut wtv);
        gram_wt_w(w.as_ref(), &mut wtw);
        gram_h_ht(h.as_ref(), &mut hht);
        let want = compute_objective(
            NmfInput::sq_frob(&cpu),
            h.as_ref(),
            wtv.as_ref(),
            wtw.as_ref(),
            hht.as_ref(),
        );

        assert!(got > 0.0, "objective is zero, the device did no work");
        let rel = (got - want).abs() / want.abs().max(1.0);
        println!("sparse objective rel diff: {rel:e}");
        assert!(rel <= 1e-3, "got {got} want {want}");
    }

    // The same matrix through the dense and the sparse entry point, from the
    // same seed for a fixed number of iterations. Also covers the CSR-to-CSC
    // conversion the sparse entry point does before pre-processing.
    #[test]
    fn test_sparse_entry_point_matches_dense() {
        let (m, n, k) = (24usize, 16usize, 2usize);
        let v = sparse_rank_k(m, n, k);
        let opts = HalsOpts::<f32>::new(100, 0.0, 1e-10, 10, NmfInit::Random { seed: 5 });

        let dense = nmf_single_run_mlx(v.as_ref(), k, "none", Some(opts.clone()), 0).unwrap();
        let sparse =
            nmf_single_run_sparse_mlx(to_csr(&v), k, "none", false, Some(opts), 0).unwrap();
        assert_eq!((sparse.n_iter, dense.n_iter), (100, 100));

        let dw = assert_close(
            &to_row_major(sparse.w.as_ref()),
            &to_row_major(dense.w.as_ref()),
            1e-3,
        );
        let dh = assert_close(
            &to_row_major(sparse.h.as_ref()),
            &to_row_major(dense.h.as_ref()),
            1e-3,
        );
        let sq_frob: f32 = (0..m)
            .map(|i| (0..n).map(|j| v[(i, j)] * v[(i, j)]).sum::<f32>())
            .sum();
        let rel_dense = dense.final_loss / sq_frob;
        let rel_sparse = sparse.final_loss / sq_frob;
        println!(
            "sparse vs dense entry point: W {dw:e} H {dh:e}, rel loss {rel_dense:e} {rel_sparse:e}"
        );
        assert!(
            (rel_dense - rel_sparse).abs() < 1e-4,
            "dense {rel_dense} and sparse {rel_sparse} entry points disagree"
        );
    }

    #[test]
    fn test_recovers_rank_k_sparse() {
        let (m, n, k) = (60usize, 40usize, 3usize);
        let v = sparse_rank_k(m, n, k);
        let opts = HalsOpts::<f32> {
            max_iter: 400,
            tol: 1e-9,
            eps: 1e-10,
            check_every: 10,
            init: NmfInit::Nndsvd,
        };
        let input =
            MlxSparseNmfInput::new(SparseInput::from_primary(&to_csr(&v)).unwrap()).unwrap();
        let res = nmf_hals_mlx(&input, k, &opts, 0).unwrap();
        let rel = res.final_loss / input.sq_frob();

        let dense_in = MlxDenseNmfInput::new(v.as_ref()).unwrap();
        let rel_dense = nmf_hals_mlx(&dense_in, k, &opts, 0).unwrap().final_loss / input.sq_frob();

        println!("sparse rank-k recovery relative loss: {rel:e} (dense {rel_dense:e})");
        assert!(rel < 1e-3, "relative loss {rel} is too high");
        assert!(
            (rel - rel_dense).abs() < 1e-3,
            "sparse {rel} and dense {rel_dense} backends disagree"
        );
        assert!((0..m).all(|i| (0..k).all(|c| res.w[(i, c)] >= 0.0)));
        assert!((0..k).all(|r| (0..n).all(|j| res.h[(r, j)] >= 0.0)));
    }

    // Both refit arms through the sparse consensus entry point.
    #[test]
    fn test_sparse_consensus_returns_usable_factors() {
        let (m, n, k, n_runs) = (25usize, 18usize, 2usize, 4usize);
        let v = sparse_rank_k(m, n, k);
        let opts = HalsOpts::<f32>::new(200, 1e-7, 1e-10, 10, NmfInit::Nndsvd);
        for target in [ConsensusTarget::HRows, ConsensusTarget::WColumns] {
            let params = ConsensusParams::<f32>::new(target, None, None, 100, 3, 42);
            let res = nmf_consensus_run_sparse_mlx(
                to_csr(&v),
                k,
                "none",
                false,
                Some(opts.clone()),
                Some(params),
                n_runs,
                0,
                0,
            )
            .unwrap();
            assert_eq!((res.w.nrows(), res.w.ncols()), (m, k));
            assert_eq!((res.h.nrows(), res.h.ncols()), (k, n));
            assert_eq!(res.run_errors.len(), n_runs);
            assert!(
                res.error < 1e-4,
                "consensus error {} is too high",
                res.error
            );
        }
    }

    /// Run `i` of the multi-run entry point is the single run seeded
    /// `base_seed + i`.
    #[test]
    fn test_sparse_multiple_run_matches_seeded_single_runs() {
        let (m, n, k, n_runs, base_seed) = (25usize, 18usize, 2usize, 3usize, 7usize);
        let v = sparse_rank_k(m, n, k);
        let opts = HalsOpts::<f32>::new(50, 1e-7, 1e-10, 10, NmfInit::Nndsvd);
        let res = nmf_multiple_run_sparse_mlx(
            to_csr(&v),
            k,
            "none",
            false,
            Some(opts.clone()),
            n_runs,
            base_seed,
            0,
        )
        .unwrap();
        assert_eq!((res.w_all.nrows(), res.w_all.ncols()), (m, k * n_runs));
        assert_eq!(res.h_per_run.len(), n_runs);
        for i in 0..n_runs {
            let opts_i = HalsOpts {
                init: NmfInit::Random {
                    seed: (base_seed + i) as u64,
                },
                ..opts.clone()
            };
            let single =
                nmf_single_run_sparse_mlx(to_csr(&v), k, "none", false, Some(opts_i), 0).unwrap();
            assert_relative_eq!(res.losses[i], single.final_loss, max_relative = 1e-5);
        }
    }

    /// One row per rank in the order given, and the true rank fits.
    #[test]
    fn test_sparse_k_sweep_rows_and_errors() {
        let (m, n, k, n_runs) = (25usize, 18usize, 2usize, 4usize);
        let v = sparse_rank_k(m, n, k);
        let opts = HalsOpts::<f32>::new(200, 1e-7, 1e-10, 10, NmfInit::Nndsvd);
        let params = ConsensusParams::<f32>::new(ConsensusTarget::HRows, None, None, 100, 3, 42);
        let rows = nmf_k_sweep_run_sparse_mlx(
            to_csr(&v),
            &[3, 2],
            "none",
            false,
            Some(opts),
            Some(params),
            n_runs,
            0,
            0,
        )
        .unwrap();
        assert_eq!(rows.iter().map(|r| r.k).collect::<Vec<_>>(), vec![3, 2]);
        assert!(
            rows[1].best_error < 1e-4,
            "k = 2 error {}",
            rows[1].best_error
        );
        assert!(rows.iter().all(|r| r.median_error >= r.best_error));
    }

    /// Bad sweep inputs are rejected before any solve.
    #[test]
    fn test_sparse_k_sweep_rejects_bad_ranges() {
        let v = sparse_rank_k(10, 8, 2);
        let sweep = |range: &[usize], n_runs: usize| {
            nmf_k_sweep_run_sparse_mlx(to_csr(&v), range, "none", false, None, None, n_runs, 0, 0)
        };
        assert!(matches!(
            sweep(&[], 3),
            Err(BixverseErrors::NmfKSweepEmptyRange)
        ));
        assert!(matches!(
            sweep(&[2, 1], 3),
            Err(BixverseErrors::NmfConsensusInvalidK { k: 1 })
        ));
        assert!(matches!(
            sweep(&[2], 1),
            Err(BixverseErrors::NmfConsensusTooFewRuns { n_runs: 1 })
        ));
    }

    /// Multi-run losses and sweep errors agree with wgpu under the same seeds.
    /// The data is rank four and every fit is below that, so the losses stay
    /// well away from zero and a relative comparison means something.
    #[cfg(feature = "gpu")]
    #[test]
    fn test_sparse_multiple_run_and_sweep_match_wgpu() {
        use crate::gpu::methods_gpu::nmf_consensus_gpu::{
            nmf_k_sweep_run_sparse_gpu, nmf_multiple_run_sparse_gpu,
        };
        use cubecl::wgpu::WgpuDevice;

        let (m, n, k, n_runs) = (25usize, 18usize, 2usize, 4usize);
        let v = sparse_rank_k(m, n, 4);
        let opts = HalsOpts::<f32>::new(100, 1e-7, 1e-10, 10, NmfInit::Nndsvd);
        let params = ConsensusParams::<f32>::new(ConsensusTarget::HRows, None, None, 100, 3, 42);

        let mlx = nmf_multiple_run_sparse_mlx(
            to_csr(&v),
            k,
            "none",
            false,
            Some(opts.clone()),
            n_runs,
            3,
            0,
        )
        .unwrap();
        let gpu = nmf_multiple_run_sparse_gpu(
            to_csr(&v),
            k,
            "none",
            false,
            Some(opts.clone()),
            n_runs,
            3,
            WgpuDevice::default().into(),
            0,
        )
        .unwrap();
        for (a, b) in mlx.losses.iter().zip(gpu.losses.iter()) {
            assert_relative_eq!(*a, *b, max_relative = 1e-3, epsilon = 1e-6);
        }

        let mlx_rows = nmf_k_sweep_run_sparse_mlx(
            to_csr(&v),
            &[2, 3],
            "none",
            false,
            Some(opts.clone()),
            Some(params),
            n_runs,
            0,
            0,
        )
        .unwrap();
        let gpu_rows = nmf_k_sweep_run_sparse_gpu(
            to_csr(&v),
            &[2, 3],
            "none",
            false,
            Some(opts),
            Some(params),
            n_runs,
            0,
            WgpuDevice::default().into(),
            0,
        )
        .unwrap();
        for (a, b) in mlx_rows.iter().zip(gpu_rows.iter()) {
            assert_eq!(a.k, b.k);
            assert_relative_eq!(
                a.best_error,
                b.best_error,
                max_relative = 1e-3,
                epsilon = 1e-6
            );
            assert_relative_eq!(
                a.median_error,
                b.median_error,
                max_relative = 1e-3,
                epsilon = 1e-6
            );
        }
    }
}