hermes-core 1.8.102

Core async search engine library with WASM support
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
//! Binary IVF: inverted-file index for packed-bit vectors under Hamming distance.
//!
//! Coarse centroids are trained with k-majority clustering (the Hamming-space
//! analog of k-means: assignment by Hamming distance, centroid update by
//! per-bit majority vote). Cluster payloads store the *exact* packed codes in
//! one contiguous buffer per cluster, so within-cluster scanning uses the same
//! SIMD Hamming kernel as brute force and scanned candidates have exact
//! distances — the only approximation is which clusters get probed (`nprobe`).
//!
//! Brute-force Hamming is fast (~10-50M vectors/s/core), so this index pays
//! off for segments past a few million vectors, or when many binary fields
//! are queried concurrently.

use rand::prelude::*;
use serde::{Deserialize, Serialize};
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::io;

use crate::dsl::IvfRoutingMode;
use crate::structures::simd::{HammingKernel, scores_from_hamming};
#[cfg(test)]
use crate::structures::simd::{batch_hamming_scores, hamming_distance};
use crate::structures::vector::ivf::routing::{
    HIERARCHICAL_TRAINING_THRESHOLD, HnswRoutingGraph, IvfProbePlan, IvfRoutingTopology,
    PairDistance, QueryDistance, allocate_child_clusters, binary_probe_fingerprint,
    effective_binary_routing_mode, routing_parent_count, select_best, select_best_candidates,
    select_parent_beam, select_parent_beam_for_build,
};
use crate::structures::vector::progress::PhaseProgress;

/// Hamming distance from one query code to graph nodes backed by a packed
/// centroid matrix.
///
/// Routing visits scattered centroid rows, so the batched form gathers a whole
/// neighbour list through one SIMD dispatch instead of one call per node.
struct BinaryCentroidDistance<'a> {
    query: &'a [u8],
    centroids: &'a [u8],
    byte_len: usize,
    kernel: HammingKernel,
}

impl<'a> BinaryCentroidDistance<'a> {
    #[inline]
    fn new(query: &'a [u8], centroids: &'a [u8], byte_len: usize) -> Self {
        Self {
            query,
            centroids,
            byte_len,
            kernel: HammingKernel::resolve(),
        }
    }
}

impl QueryDistance for BinaryCentroidDistance<'_> {
    #[inline]
    fn distance(&self, node: u32) -> f32 {
        let offset = node as usize * self.byte_len;
        self.kernel
            .distance(self.query, &self.centroids[offset..offset + self.byte_len]) as f32
    }

    fn distances(&self, nodes: &[u32], out: &mut [f32]) {
        let mut distances = [0u32; ROUTING_GATHER_BLOCK];
        for (block, scores) in nodes
            .chunks(ROUTING_GATHER_BLOCK)
            .zip(out.chunks_mut(ROUTING_GATHER_BLOCK))
        {
            let gathered = &mut distances[..block.len()];
            self.kernel.gather_distances(
                self.query,
                self.centroids,
                self.byte_len,
                block,
                gathered,
            );
            for (slot, &distance) in scores.iter_mut().zip(gathered.iter()) {
                *slot = distance as f32;
            }
        }
    }
}

/// Pairwise centroid distance used while building the routing graph.
struct BinaryCentroidPairDistance<'a> {
    centroids: &'a [u8],
    byte_len: usize,
    kernel: HammingKernel,
}

impl PairDistance for BinaryCentroidPairDistance<'_> {
    #[inline]
    fn distance(&self, left: u32, right: u32) -> f32 {
        let left_offset = left as usize * self.byte_len;
        let right_offset = right as usize * self.byte_len;
        self.kernel.distance(
            &self.centroids[left_offset..left_offset + self.byte_len],
            &self.centroids[right_offset..right_offset + self.byte_len],
        ) as f32
    }

    fn distances_from(&self, left: u32, rights: &[u32], out: &mut [f32]) {
        let offset = left as usize * self.byte_len;
        BinaryCentroidDistance {
            query: &self.centroids[offset..offset + self.byte_len],
            centroids: self.centroids,
            byte_len: self.byte_len,
            kernel: self.kernel,
        }
        .distances(rights, out);
    }
}

/// Rows gathered per kernel dispatch while routing. Covers a full level-0
/// neighbour list (`2 * HNSW_M`) without touching the allocator.
const ROUTING_GATHER_BLOCK: usize = 128;

/// Nearest centroid and its exact Hamming distance.
///
/// Ranking stays integral: the winner's distance falls out of the same scan
/// rather than costing a second full-width distance, and equal distances keep
/// the lowest cluster ID to match query-time centroid ordering.
#[inline]
fn nearest_binary_centroid_with_distance(
    kernel: HammingKernel,
    code: &[u8],
    centroids: &[u8],
    byte_len: usize,
    distances: &mut [u32],
) -> (u32, u32) {
    kernel.distances(code, centroids, byte_len, distances);
    let mut best = (u32::MAX, 0u32);
    for (cluster, &distance) in distances.iter().enumerate() {
        if distance < best.0 {
            best = (distance, cluster as u32);
        }
    }
    (best.1, best.0)
}

const MAX_BINARY_IVF_CLUSTERS: usize = 1_048_576;
#[cfg(test)]
const BINARY_IVF_SCORE_BATCH: usize = 8_192;
const BUILD_ASSIGNMENT_CANDIDATES: usize = 128;

/// A code with no set bits carries no information: its Hamming distance to any
/// query is a constant `popcount(query)`, so it scores mid-range against
/// *everything* while matching nothing.
///
/// It is also a latency cliff. Equal distances resolve to the lowest cluster
/// ID, so every zero code lands in the same leaf: one production field
/// accumulated 31% of its vectors — 20.1M codes, 6.0 GiB — in leaf 0, and any
/// query probing that leaf scanned all of it.
///
/// They are still indexed, because the byte-copy merge path requires an ANN
/// payload to hold exactly as many vectors as flat storage; withholding them
/// needs a versioned header field and is deliberately not done here. What the
/// build does instead is count and report them, so a producer regression is
/// loud rather than a silent scan cliff.
#[inline]
fn is_zero_code(code: &[u8]) -> bool {
    code.iter().all(|&byte| byte == 0)
}

/// The saturated twin of [`is_zero_code`]: every bit set. Produced by packers
/// that route NaN through a signbit test (`~signbit(NaN)` is true), where the
/// `x > 0` convention produces zeros instead. One production corpus carried
/// this face for two years — 36% of a field's vectors were the identical
/// all-ones code, with a codebook centroid trained to exactly 0xFF — so it
/// gets the same count-and-report treatment. `dim_bits` is validated to be a
/// multiple of 8, so a byte-level check is exact (no pad bits).
#[inline]
fn is_ones_code(code: &[u8]) -> bool {
    code.iter().all(|&byte| byte == 0xff)
}

/// A leaf this many times larger than the average is a scan cliff regardless of
/// what produced it, so segment builds report it.
#[cfg(feature = "native")]
const LEAF_SKEW_WARN_RATIO: usize = 100;
/// Below this many vectors a skewed leaf cannot cost enough to be worth a line.
#[cfg(feature = "native")]
const LEAF_SKEW_WARN_MINIMUM: usize = 10_000;

/// Global Hamming coarse quantizer shared by every segment of a field.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BinaryCoarseQuantizer {
    pub dim_bits: usize,
    pub num_clusters: u32,
    /// Packed leaf centroids (`num_clusters × byte_len`).
    centroids: Vec<u8>,
    pub version: u64,
    routing_index: Option<BinaryCentroidRouter>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum BinaryCentroidRouter {
    TwoLevel {
        parent_centroids: Vec<u8>,
        topology: IvfRoutingTopology,
    },
    Hnsw(HnswRoutingGraph),
}

impl BinaryCoarseQuantizer {
    pub fn train(
        mut config: BinaryIvfConfig,
        codes: &[u8],
        num_vectors: usize,
        index_label: &str,
    ) -> io::Result<Self> {
        config
            .validate()
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
        let expected = num_vectors.checked_mul(config.byte_len()).ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "binary training size overflow")
        })?;
        if num_vectors == 0 || codes.len() != expected {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "binary coarse training requires a non-empty, contiguous code matrix",
            ));
        }
        config.num_clusters = config.num_clusters.clamp(1, num_vectors);
        let (centroids, routing_index) =
            match effective_binary_routing_mode(config.routing, config.num_clusters) {
                IvfRoutingMode::TwoLevel => {
                    let (leaves, router) =
                        train_k_majority_hierarchical(&config, codes, num_vectors, index_label);
                    (leaves, Some(router))
                }
                IvfRoutingMode::Hnsw => {
                    let leaves = if config.num_clusters >= HIERARCHICAL_TRAINING_THRESHOLD {
                        train_k_majority_hierarchical(&config, codes, num_vectors, index_label).0
                    } else {
                        train_k_majority(&config, codes, num_vectors, index_label)
                    };
                    let byte_len = config.byte_len();
                    let graph = HnswRoutingGraph::build(
                        config.num_clusters,
                        BinaryCentroidPairDistance {
                            centroids: &leaves,
                            byte_len,
                            kernel: HammingKernel::resolve(),
                        },
                        config.seed,
                        index_label,
                    );
                    (leaves, Some(BinaryCentroidRouter::Hnsw(graph)))
                }
                IvfRoutingMode::Flat | IvfRoutingMode::Auto => (
                    train_k_majority(&config, codes, num_vectors, index_label),
                    None,
                ),
            };
        let version = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos() as u64;
        Ok(Self {
            dim_bits: config.dim_bits,
            num_clusters: config.num_clusters as u32,
            centroids,
            version,
            routing_index,
        })
    }

    #[inline]
    pub fn byte_len(&self) -> usize {
        self.dim_bits.div_ceil(8)
    }

    pub fn validate(&self) -> Result<(), String> {
        let expected = (self.num_clusters as usize)
            .checked_mul(self.byte_len())
            .ok_or_else(|| "binary coarse centroid size overflow".to_string())?;
        if self.dim_bits == 0
            || !self.dim_bits.is_multiple_of(8)
            || self.num_clusters == 0
            || self.centroids.len() != expected
        {
            return Err("invalid binary coarse quantizer shape".to_string());
        }
        if let Some(router) = &self.routing_index {
            match router {
                BinaryCentroidRouter::TwoLevel {
                    parent_centroids,
                    topology,
                } => {
                    let parent_count = topology.parent_count();
                    if parent_count == 0
                        || parent_centroids.len() != parent_count.saturating_mul(self.byte_len())
                        || !topology.validate(self.num_clusters as usize)
                    {
                        return Err("invalid binary two-level routing index".to_string());
                    }
                }
                BinaryCentroidRouter::Hnsw(graph)
                    if !graph.validate(self.num_clusters as usize) =>
                {
                    return Err("invalid binary HNSW routing graph".to_string());
                }
                BinaryCentroidRouter::Hnsw(_) => {}
            }
        }
        Ok(())
    }

    pub fn validate_routing(&self, mode: IvfRoutingMode) -> Result<(), String> {
        match effective_binary_routing_mode(mode, self.num_clusters as usize) {
            IvfRoutingMode::Flat | IvfRoutingMode::Auto => {}
            IvfRoutingMode::TwoLevel
                if !matches!(
                    self.routing_index,
                    Some(BinaryCentroidRouter::TwoLevel { .. })
                ) =>
            {
                return Err(
                    "two-level IVF routing was requested but the global binary quantizer has no matching router"
                        .to_string(),
                );
            }
            IvfRoutingMode::Hnsw
                if !matches!(self.routing_index, Some(BinaryCentroidRouter::Hnsw(_))) =>
            {
                return Err(
                    "HNSW IVF routing was requested but the global binary quantizer has no HNSW graph"
                        .to_string(),
                );
            }
            IvfRoutingMode::TwoLevel | IvfRoutingMode::Hnsw => {}
        }
        self.validate()
    }

    /// Visit compact routing topology and parent arrays before the potentially
    /// much larger leaf centroid matrix.
    #[cfg(feature = "native")]
    pub(crate) fn visit_routing_regions(&self, visit: &mut dyn FnMut(&'static str, &[u8])) {
        if let Some(router) = &self.routing_index {
            match router {
                BinaryCentroidRouter::TwoLevel {
                    parent_centroids,
                    topology,
                } => {
                    topology.visit_resident_regions(visit);
                    visit("binary parent centroids", parent_centroids);
                }
                BinaryCentroidRouter::Hnsw(graph) => graph.visit_resident_regions(visit),
            }
        }
    }

    #[cfg(feature = "native")]
    pub(crate) fn visit_leaf_centroid_region(&self, visit: &mut dyn FnMut(&'static str, &[u8])) {
        visit("binary leaf centroids", &self.centroids);
    }

    pub fn probe(&self, query: &[u8], k: usize, mode: IvfRoutingMode) -> IvfProbePlan {
        let take = k.clamp(1, self.num_clusters as usize);
        let cluster_ids = match effective_binary_routing_mode(mode, self.num_clusters as usize) {
            IvfRoutingMode::Flat | IvfRoutingMode::Auto => self.find_k_nearest(query, take),
            IvfRoutingMode::TwoLevel => self.find_k_nearest_two_level(query, take),
            IvfRoutingMode::Hnsw => self.find_k_nearest_hnsw(query, take),
        };
        IvfProbePlan::new(
            self.version,
            binary_probe_fingerprint(query, take, mode),
            cluster_ids,
        )
    }

    /// Assign one code to its leaf.
    ///
    /// Segment construction routes every vector through here, so this path must
    /// stay allocation-free.
    pub fn assign(&self, code: &[u8], mode: IvfRoutingMode) -> u32 {
        match effective_binary_routing_mode(mode, self.num_clusters as usize) {
            IvfRoutingMode::Hnsw => self.find_nearest_hnsw_for_build(code).unwrap_or(0),
            IvfRoutingMode::TwoLevel => self
                .find_k_nearest_two_level_for_build(
                    code,
                    BUILD_ASSIGNMENT_CANDIDATES.min(self.num_clusters as usize),
                )
                .first()
                .copied()
                .unwrap_or(0),
            IvfRoutingMode::Flat | IvfRoutingMode::Auto => self.find_nearest(code),
        }
    }

    fn find_nearest(&self, query: &[u8]) -> u32 {
        let byte_len = self.byte_len();
        if query.len() != byte_len {
            return 0;
        }
        let kernel = HammingKernel::resolve();
        let mut distances = [0u32; BINARY_CENTROID_SCAN_BLOCK];
        let mut best = (u32::MAX, 0u32);
        for (block_index, block) in self
            .centroids
            .chunks(BINARY_CENTROID_SCAN_BLOCK * byte_len)
            .enumerate()
        {
            let rows = block.len() / byte_len;
            let scored = &mut distances[..rows];
            kernel.distances(query, block, byte_len, scored);
            for (row, &distance) in scored.iter().enumerate() {
                // Ties keep the lowest cluster ID, matching query-time ordering.
                if distance < best.0 {
                    best = (
                        distance,
                        (block_index * BINARY_CENTROID_SCAN_BLOCK + row) as u32,
                    );
                }
            }
        }
        best.1
    }

    fn find_k_nearest(&self, query: &[u8], k: usize) -> Vec<u32> {
        if query.len() != self.byte_len() {
            return Vec::new();
        }
        // A flat probe scores every centroid; at production cluster counts the
        // score buffer is hundreds of KiB, so it comes from reusable scratch.
        with_centroid_score_scratch(self.num_clusters as usize, |scores| {
            scores_from_hamming(
                HammingKernel::resolve(),
                query,
                &self.centroids,
                self.byte_len(),
                self.dim_bits,
                scores,
            );
            select_best::<true>(scores, k)
        })
    }

    fn find_k_nearest_two_level(&self, query: &[u8], k: usize) -> Vec<u32> {
        self.find_k_nearest_two_level_impl::<false>(query, k)
    }

    fn find_k_nearest_two_level_for_build(&self, query: &[u8], k: usize) -> Vec<u32> {
        self.find_k_nearest_two_level_impl::<true>(query, k)
    }

    fn find_k_nearest_two_level_impl<const FOR_BUILD: bool>(
        &self,
        query: &[u8],
        k: usize,
    ) -> Vec<u32> {
        let Some(BinaryCentroidRouter::TwoLevel {
            parent_centroids,
            topology,
        }) = self.routing_index.as_ref()
        else {
            return self.find_k_nearest(query, k);
        };
        if topology.parent_count() <= 1 {
            return self.find_k_nearest(query, k);
        }
        let byte_len = self.byte_len();
        let kernel = HammingKernel::resolve();
        let mut parent_scores = vec![0.0; topology.parent_count()];
        scores_from_hamming(
            kernel,
            query,
            parent_centroids,
            byte_len,
            self.dim_bits,
            &mut parent_scores,
        );
        let parents = if FOR_BUILD {
            select_parent_beam_for_build::<true>(&parent_scores, topology, k)
        } else {
            select_parent_beam::<true>(&parent_scores, topology, k)
        };
        let candidate_count = parents
            .iter()
            .map(|&parent| topology.children(parent as usize).len())
            .sum();
        let mut candidates = Vec::with_capacity(candidate_count);
        // Each parent owns a contiguous leaf run, so its children are one
        // batched pass over the centroid matrix rather than one kernel call
        // (and one runtime feature detection) per leaf.
        let mut leaf_scores = vec![0.0f32; 0];
        for parent in parents {
            let children = topology.children(parent as usize);
            match topology.children_run(parent as usize) {
                Some((first_leaf, count)) => {
                    leaf_scores.clear();
                    leaf_scores.resize(count, 0.0);
                    let start = first_leaf as usize * byte_len;
                    scores_from_hamming(
                        kernel,
                        query,
                        &self.centroids[start..start + count * byte_len],
                        byte_len,
                        self.dim_bits,
                        &mut leaf_scores,
                    );
                    candidates.extend(
                        (first_leaf..first_leaf + count as u32).zip(leaf_scores.iter().copied()),
                    );
                }
                None => {
                    for &leaf in children {
                        let offset = leaf as usize * byte_len;
                        let distance =
                            kernel.distance(query, &self.centroids[offset..offset + byte_len]);
                        candidates.push((leaf, 1.0 - distance as f32 / self.dim_bits as f32));
                    }
                }
            }
        }
        select_best_candidates::<true>(&mut candidates, k)
    }

    fn find_k_nearest_hnsw(&self, query: &[u8], k: usize) -> Vec<u32> {
        let Some(BinaryCentroidRouter::Hnsw(graph)) = self.routing_index.as_ref() else {
            return self.find_k_nearest(query, k);
        };
        graph.search(
            BinaryCentroidDistance::new(query, &self.centroids, self.byte_len()),
            k,
        )
    }

    fn find_nearest_hnsw_for_build(&self, query: &[u8]) -> Option<u32> {
        let Some(BinaryCentroidRouter::Hnsw(graph)) = self.routing_index.as_ref() else {
            return Some(self.find_nearest(query));
        };
        graph.search_best_for_build(BinaryCentroidDistance::new(
            query,
            &self.centroids,
            self.byte_len(),
        ))
    }
}

/// Centroid rows scored per stack block during a flat scan.
const BINARY_CENTROID_SCAN_BLOCK: usize = 64;

thread_local! {
    /// Flat probing scores every centroid; at production cluster counts that
    /// buffer is hundreds of KiB per query, so it is retained per thread.
    static CENTROID_SCORE_SCRATCH: std::cell::RefCell<Vec<f32>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

fn with_centroid_score_scratch<T>(len: usize, scope: impl FnOnce(&mut [f32]) -> T) -> T {
    CENTROID_SCORE_SCRATCH.with(|scratch| {
        let mut scores = scratch.borrow_mut();
        scores.clear();
        scores.resize(len, 0.0);
        scope(&mut scores)
    })
}

fn default_max_train_samples() -> usize {
    100_000
}

/// Configuration for a binary IVF index.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BinaryIvfConfig {
    /// Number of bits per vector (must be a multiple of 8)
    pub dim_bits: usize,
    /// Number of clusters
    pub num_clusters: usize,
    /// Flat, two-level, or HNSW coarse routing. Auto chooses from the leaf count.
    pub routing: IvfRoutingMode,
    /// k-majority training iterations
    pub train_iters: usize,
    /// Cap on vectors used for centroid training (assignment still covers
    /// all vectors). Bounds merge-time training cost on huge segments.
    #[serde(default = "default_max_train_samples")]
    pub max_train_samples: usize,
    /// RNG seed for centroid initialization (deterministic builds)
    pub seed: u64,
}

impl BinaryIvfConfig {
    pub fn new(dim_bits: usize, num_clusters: usize) -> Self {
        Self {
            dim_bits,
            num_clusters,
            routing: IvfRoutingMode::Auto,
            train_iters: 10,
            max_train_samples: default_max_train_samples(),
            seed: 42,
        }
    }

    #[inline]
    pub fn byte_len(&self) -> usize {
        self.dim_bits.div_ceil(8)
    }

    fn validate(&self) -> Result<(), String> {
        if self.dim_bits == 0 || !self.dim_bits.is_multiple_of(8) {
            return Err(format!(
                "binary IVF dimension must be a positive multiple of 8, got {}",
                self.dim_bits
            ));
        }
        if !(1..=MAX_BINARY_IVF_CLUSTERS).contains(&self.num_clusters) {
            return Err(format!(
                "binary IVF cluster count must be in 1..={MAX_BINARY_IVF_CLUSTERS}, got {}",
                self.num_clusters
            ));
        }
        self.num_clusters
            .checked_mul(self.byte_len())
            .ok_or_else(|| "binary IVF centroid size overflow".to_string())?;
        self.num_clusters
            .checked_mul(self.dim_bits)
            .ok_or_else(|| "binary IVF training scratch size overflow".to_string())?;
        Ok(())
    }
}

/// One cluster: SoA layout with contiguous packed codes for SIMD scanning.
#[derive(Debug, Clone, Default)]
pub(crate) struct BinaryCluster {
    pub(crate) doc_ids: Vec<u32>,
    pub(crate) ordinals: Vec<u16>,
    /// Packed codes, `byte_len` bytes per entry, contiguous
    pub(crate) codes: Vec<u8>,
}

#[cfg(test)]
fn visit_binary_cluster(
    cluster: &BinaryCluster,
    dim_bits: usize,
    query: &[u8],
    scores: &mut [f32],
    visit: &mut impl FnMut(u32, u16, f32),
) {
    let byte_len = dim_bits.div_ceil(8);
    let count = cluster.doc_ids.len();
    for batch_start in (0..count).step_by(BINARY_IVF_SCORE_BATCH) {
        let batch_count = BINARY_IVF_SCORE_BATCH.min(count - batch_start);
        let code_start = batch_start * byte_len;
        let code_end = (batch_start + batch_count) * byte_len;
        batch_hamming_scores(
            query,
            &cluster.codes[code_start..code_end],
            byte_len,
            dim_bits,
            &mut scores[..batch_count],
        );
        for (batch_idx, &score) in scores.iter().enumerate().take(batch_count) {
            let i = batch_start + batch_idx;
            visit(cluster.doc_ids[i], cluster.ordinals[i], score);
        }
    }
}

/// Centroid-free binary IVF payload for one segment. The global quantizer is
/// loaded once at index scope; segments only retain exact codes partitioned by
/// leaf ID, making compatible merges O(number of non-empty clusters).
#[derive(Debug, Clone)]
pub struct BinaryIvfIndex {
    pub dim_bits: usize,
    pub quantizer_version: u64,
    pub num_clusters: u32,
    /// Sorted non-empty `(leaf_id, payload)` pairs. Empty cells cost no
    /// per-segment heap memory even when the global codebook has millions of
    /// leaves.
    pub(crate) clusters: Vec<(u32, BinaryCluster)>,
    len: usize,
    /// Indexed codes that carry no information (all bits clear).
    zero_codes: usize,
    /// Indexed codes that carry no information (all bits set).
    ones_codes: usize,
}

/// Streaming build state used by vector-generation rewrites. Only the exact
/// compressed output plus one bounded assignment batch is resident; source
/// flat vectors are never accumulated in memory.
pub(crate) struct BinaryIvfBuilder {
    dim_bits: usize,
    quantizer_version: u64,
    num_clusters: u32,
    routing: IvfRoutingMode,
    clusters: rustc_hash::FxHashMap<u32, BinaryCluster>,
    len: usize,
    zero_codes: usize,
    ones_codes: usize,
}

impl BinaryIvfBuilder {
    pub(crate) fn new(
        quantizer: &BinaryCoarseQuantizer,
        routing: IvfRoutingMode,
    ) -> io::Result<Self> {
        quantizer
            .validate_routing(routing)
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
        Ok(Self {
            dim_bits: quantizer.dim_bits,
            quantizer_version: quantizer.version,
            num_clusters: quantizer.num_clusters,
            routing,
            clusters: rustc_hash::FxHashMap::default(),
            len: 0,
            zero_codes: 0,
            ones_codes: 0,
        })
    }

    pub(crate) fn add_batch(
        &mut self,
        quantizer: &BinaryCoarseQuantizer,
        codes: &[u8],
        doc_id_ordinals: &[(u32, u16)],
    ) -> io::Result<()> {
        if quantizer.dim_bits != self.dim_bits
            || quantizer.version != self.quantizer_version
            || quantizer.num_clusters != self.num_clusters
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "binary IVF build batch uses a different quantizer generation",
            ));
        }
        let byte_len = quantizer.byte_len();
        let expected = doc_id_ordinals.len().checked_mul(byte_len).ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "binary IVF batch overflows")
        })?;
        if codes.len() != expected {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "binary IVF code/label batch is inconsistent",
            ));
        }
        #[cfg(feature = "native")]
        let assignments: Vec<u32> = {
            use rayon::prelude::*;
            codes
                .par_chunks_exact(byte_len)
                .map(|code| quantizer.assign(code, self.routing))
                .collect()
        };
        #[cfg(not(feature = "native"))]
        let assignments: Vec<u32> = codes
            .chunks_exact(byte_len)
            .map(|code| quantizer.assign(code, self.routing))
            .collect();

        // Insert grouped by leaf: one map lookup and one reservation per
        // distinct leaf in the batch instead of per code. Sorting by
        // `(cluster, index)` keeps each leaf's entries in ascending batch order,
        // so the serialized payload is byte-identical to per-code insertion.
        for code in codes.chunks_exact(byte_len) {
            if is_zero_code(code) {
                self.zero_codes += 1;
            } else if is_ones_code(code) {
                self.ones_codes += 1;
            }
        }
        let mut order: Vec<u32> = (0..assignments.len() as u32).collect();
        order.sort_unstable_by_key(|&index| (assignments[index as usize], index));
        let mut run_start = 0usize;
        while run_start < order.len() {
            let cluster_id = assignments[order[run_start] as usize];
            let mut run_end = run_start + 1;
            while run_end < order.len() && assignments[order[run_end] as usize] == cluster_id {
                run_end += 1;
            }
            let run = &order[run_start..run_end];
            let cluster = self.clusters.entry(cluster_id).or_default();
            cluster.doc_ids.reserve(run.len());
            cluster.ordinals.reserve(run.len());
            cluster.codes.reserve(run.len() * byte_len);
            for &index in run {
                let index = index as usize;
                let (doc_id, ordinal) = doc_id_ordinals[index];
                cluster.doc_ids.push(doc_id);
                cluster.ordinals.push(ordinal);
                cluster
                    .codes
                    .extend_from_slice(&codes[index * byte_len..(index + 1) * byte_len]);
            }
            run_start = run_end;
        }
        self.len = self.len.checked_add(order.len()).ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "binary IVF count overflows")
        })?;
        Ok(())
    }

    pub(crate) fn finish(self) -> io::Result<BinaryIvfIndex> {
        let mut clusters: Vec<_> = self.clusters.into_iter().collect();
        clusters.sort_unstable_by_key(|(cluster_id, _)| *cluster_id);
        let index = BinaryIvfIndex {
            dim_bits: self.dim_bits,
            quantizer_version: self.quantizer_version,
            num_clusters: self.num_clusters,
            clusters,
            len: self.len,
            zero_codes: self.zero_codes,
            ones_codes: self.ones_codes,
        };
        index
            .validate()
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
        Ok(index)
    }
}

impl BinaryIvfIndex {
    pub fn build(
        quantizer: &BinaryCoarseQuantizer,
        routing: IvfRoutingMode,
        codes: &[u8],
        doc_id_ordinals: &[(u32, u16)],
    ) -> io::Result<Self> {
        let mut builder = BinaryIvfBuilder::new(quantizer, routing)?;
        builder.add_batch(quantizer, codes, doc_id_ordinals)?;
        builder.finish()
    }

    fn validate(&self) -> Result<(), String> {
        if self.dim_bits == 0 || !self.dim_bits.is_multiple_of(8) || self.num_clusters == 0 {
            return Err("invalid global binary IVF metadata".to_string());
        }
        let byte_len = self.dim_bits.div_ceil(8);
        let mut total = 0usize;
        let mut previous = None;
        for (cluster_id, cluster) in &self.clusters {
            if *cluster_id >= self.num_clusters || previous.is_some_and(|id| id >= *cluster_id) {
                return Err("global binary IVF cluster IDs are invalid or unsorted".to_string());
            }
            previous = Some(*cluster_id);
            let count = cluster.doc_ids.len();
            if cluster.ordinals.len() != count
                || cluster.codes.len() != count.saturating_mul(byte_len)
            {
                return Err("global binary IVF cluster columns are inconsistent".to_string());
            }
            total = total
                .checked_add(count)
                .ok_or_else(|| "global binary IVF vector count overflow".to_string())?;
        }
        if total != self.len {
            return Err("global binary IVF vector count is inconsistent".to_string());
        }
        Ok(())
    }

    /// Score the requested clusters of the in-memory build product.
    ///
    /// Queries never reach this: they scan the mmap-backed `AnnDiskIndex` this
    /// structure serialises into. It exists so build-side tests can assert the
    /// partitioning directly, and is therefore test-only — production scanning
    /// lives in `ann_disk::score_binary_cluster_runs`.
    #[cfg(test)]
    pub fn search_in_clusters(
        &self,
        query: &[u8],
        k: usize,
        cluster_ids: &[u32],
    ) -> Vec<(u32, u16, f32)> {
        let mut collector = super::BoundedAnnCollector::<false, true>::new(k);
        if query.len() == self.dim_bits.div_ceil(8) && self.len > 0 {
            let mut scores = vec![0.0; BINARY_IVF_SCORE_BATCH.min(self.len)];
            for &cluster_id in cluster_ids {
                if let Ok(position) = self
                    .clusters
                    .binary_search_by_key(&cluster_id, |(id, _)| *id)
                {
                    visit_binary_cluster(
                        &self.clusters[position].1,
                        self.dim_bits,
                        query,
                        &mut scores,
                        &mut |doc_id, ordinal, score| collector.insert(doc_id, ordinal, score),
                    );
                }
            }
        }
        collector.into_sorted_results()
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Indexed codes that were all-zero.
    pub fn zero_codes(&self) -> usize {
        self.zero_codes
    }

    /// Indexed codes that were all-ones.
    pub fn ones_codes(&self) -> usize {
        self.ones_codes
    }

    /// Largest leaf as `(cluster_id, count)`, for skew reporting.
    pub fn largest_cluster(&self) -> Option<(u32, usize)> {
        self.clusters
            .iter()
            .map(|(cluster_id, cluster)| (*cluster_id, cluster.doc_ids.len()))
            .max_by_key(|&(cluster_id, count)| (count, std::cmp::Reverse(cluster_id)))
    }

    pub fn estimated_memory_bytes(&self) -> usize {
        self.clusters
            .iter()
            .map(|(_, cluster)| cluster.codes.len() + cluster.doc_ids.len() * 6)
            .sum()
    }
}

/// Report build-quality problems for one segment's binary payload.
///
/// Both are silent-degradation modes that cost search latency and recall long
/// before anything looks broken, so they are surfaced at every build, merge and
/// rewrite rather than left to be discovered from disk.
#[cfg(feature = "native")]
pub(crate) fn report_binary_build_quality(
    index_label: &str,
    field_id: u32,
    index: &BinaryIvfIndex,
) {
    let indexed = index.len();
    let zero = index.zero_codes();
    if zero > 0 {
        log::warn!(
            "[binary_ivf] index={index_label} field={field_id}: {zero} of {indexed} indexed \
             vectors ({:.1}%) are all-zero — they match nothing, collapse into a single leaf, \
             and every query probing that leaf scans them; check the embedding producer",
            100.0 * zero as f64 / indexed.max(1) as f64,
        );
    }
    let ones = index.ones_codes();
    if ones > 0 {
        log::warn!(
            "[binary_ivf] index={index_label} field={field_id}: {ones} of {indexed} indexed \
             vectors ({:.1}%) are all-ones — the saturated twin of the zero code (NaN packed \
             through a signbit test): they match nothing, collapse into a single leaf, and \
             every query probing that leaf scans them; check the embedding producer",
            100.0 * ones as f64 / indexed.max(1) as f64,
        );
    }
    if let Some((cluster_id, count)) = index.largest_cluster()
        && count >= LEAF_SKEW_WARN_MINIMUM
        && index.num_clusters > 1
        && count.saturating_mul(index.num_clusters as usize)
            > indexed.saturating_mul(LEAF_SKEW_WARN_RATIO)
    {
        log::warn!(
            "[binary_ivf] index={index_label} field={field_id}: leaf {cluster_id} holds {count}              of {indexed} vectors ({:.1}%, {:.0}x the average) — every query probing it scans              that leaf in full",
            100.0 * count as f64 / indexed.max(1) as f64,
            count as f64 * index.num_clusters as f64 / indexed.max(1) as f64,
        );
    }
}

/// Lloyd-style k-majority clustering in Hamming space.
fn train_k_majority(
    config: &BinaryIvfConfig,
    codes: &[u8],
    n: usize,
    index_label: &str,
) -> Vec<u8> {
    train_k_majority_reporting(config, codes, n, index_label, true)
}

/// As [`train_k_majority`], with progress reporting suppressed for the hundreds
/// of small child codebooks a hierarchical build trains.
fn train_k_majority_reporting(
    config: &BinaryIvfConfig,
    codes: &[u8],
    n: usize,
    index_label: &str,
    report: bool,
) -> Vec<u8> {
    let byte_len = config.byte_len();
    let k = config.num_clusters;
    let mut rng = rand::rngs::StdRng::seed_from_u64(config.seed);
    // Seeding and Lloyd assignment together dominate training; resolve the
    // architecture kernel once instead of re-detecting CPU features for every
    // one of the O(N*K) code pairs.
    let kernel = HammingKernel::resolve();

    // Bound training cost: iterate over a sample, assign everything later. Do
    // not materialize a random permutation when the complete input is used:
    // the indirection destroys locality for the billion-scale global training
    // sample without changing which points participate.
    let sample_len = config.max_train_samples.max(k).min(n);
    let sample =
        (sample_len < n).then(|| rand::seq::index::sample(&mut rng, n, sample_len).into_vec());
    let n = sample_len;
    let vec_at = |i: usize| -> &[u8] {
        let vi = sample.as_ref().map_or(i, |sample| sample[i]);
        &codes[vi * byte_len..(vi + 1) * byte_len]
    };

    let mut centroids = vec![0u8; k * byte_len];
    // k-means++ seeding in Hamming space. Random-first-k is especially prone
    // to duplicate/near-duplicate cells on skewed embedding distributions.
    let first = rng.random_range(0..n);
    centroids[..byte_len].copy_from_slice(vec_at(first));
    let mut minimum_weights = vec![f64::INFINITY; n];
    // Seeding is one full pass over the sample per centroid, so it is O(N*K)
    // and by far the longest silent stretch of a large codebook.
    let mut seeding = PhaseProgress::start_if(
        report,
        index_label,
        "k-majority seeding",
        format!("{k} centroids over {n} samples"),
        k,
    );
    for centroid_id in 1..k {
        seeding.advance(centroid_id);
        let previous = &centroids[(centroid_id - 1) * byte_len..centroid_id * byte_len];
        #[cfg(feature = "native")]
        {
            use rayon::prelude::*;
            minimum_weights
                .par_iter_mut()
                .enumerate()
                .for_each(|(index, minimum_weight)| {
                    let distance = kernel.distance(vec_at(index), previous);
                    // Hamming distance is already squared Euclidean distance
                    // for vectors in {0,1}^d, so D² sampling uses it directly.
                    *minimum_weight = minimum_weight.min(distance as f64);
                });
        }
        #[cfg(not(feature = "native"))]
        for (index, minimum_weight) in minimum_weights.iter_mut().enumerate() {
            let distance = kernel.distance(vec_at(index), previous);
            *minimum_weight = minimum_weight.min(distance as f64);
        }

        let chosen = if let Some(chosen) = crate::structures::vector::kmeans::weighted_sample_index(
            &minimum_weights,
            rng.random::<f64>(),
        ) {
            chosen
        } else {
            rng.random_range(0..n)
        };
        centroids[centroid_id * byte_len..(centroid_id + 1) * byte_len]
            .copy_from_slice(vec_at(chosen));
    }

    seeding.finish();

    let mut assignment = vec![u32::MAX; n];
    let mut assignment_distances = vec![u32::MAX; n];
    let mut members: Vec<Vec<usize>> = (0..k).map(|_| Vec::new()).collect();

    let mut lloyd = PhaseProgress::start_if(
        report,
        index_label,
        "k-majority refinement",
        format!(
            "{k} centroids, {n} samples, <= {} iters",
            config.train_iters
        ),
        config.train_iters,
    );
    for _iter in 0..config.train_iters {
        lloyd.advance(_iter);
        // Assignment is point-independent. Give every rayon worker its own
        // score buffer so the O(N*K) scan uses all available training CPUs
        // without synchronization or per-point allocation.
        #[cfg(feature = "native")]
        let changed: usize = {
            use rayon::prelude::*;
            assignment
                .par_iter_mut()
                .zip(assignment_distances.par_iter_mut())
                .enumerate()
                .map_init(
                    || vec![0u32; k],
                    |distances, (i, (slot, assignment_distance))| {
                        let (best, distance) = nearest_binary_centroid_with_distance(
                            kernel,
                            vec_at(i),
                            &centroids,
                            byte_len,
                            distances,
                        );
                        *assignment_distance = distance;
                        usize::from(std::mem::replace(slot, best) != best)
                    },
                )
                .sum()
        };
        #[cfg(not(feature = "native"))]
        let changed: usize = {
            let mut distances = vec![0u32; k];
            assignment
                .iter_mut()
                .zip(&mut assignment_distances)
                .enumerate()
                .map(|(i, (slot, assignment_distance))| {
                    let (best, distance) = nearest_binary_centroid_with_distance(
                        kernel,
                        vec_at(i),
                        &centroids,
                        byte_len,
                        &mut distances,
                    );
                    *assignment_distance = distance;
                    usize::from(std::mem::replace(slot, best) != best)
                })
                .sum()
        };
        if changed == 0 {
            break;
        }

        // Group point IDs once, in input order. Updating one centroid per task
        // is both deterministic and much more cache-friendly than writing a
        // K*D counter matrix at a data-dependent cluster offset for every bit.
        for cluster_members in &mut members {
            cluster_members.clear();
        }
        for (i, &slot) in assignment.iter().enumerate().take(n) {
            members[slot as usize].push(i);
        }

        reseed_empty_binary_centroids(
            &mut centroids,
            &mut members,
            &mut assignment,
            &mut assignment_distances,
            sample.as_deref(),
            codes,
            byte_len,
        );

        #[cfg(feature = "native")]
        {
            use rayon::prelude::*;
            centroids
                .par_chunks_mut(byte_len)
                .zip(members.par_iter())
                .filter(|(_, cluster_members)| !cluster_members.is_empty())
                .for_each_init(
                    BinaryMajorityScratch::default,
                    |scratch, (centroid, cluster_members)| {
                        update_binary_centroid(
                            centroid,
                            cluster_members,
                            sample.as_deref(),
                            codes,
                            byte_len,
                            scratch,
                        );
                    },
                );
        }
        #[cfg(not(feature = "native"))]
        {
            let mut scratch = BinaryMajorityScratch::default();
            for (centroid, cluster_members) in centroids.chunks_mut(byte_len).zip(&members) {
                if !cluster_members.is_empty() {
                    update_binary_centroid(
                        centroid,
                        cluster_members,
                        sample.as_deref(),
                        codes,
                        byte_len,
                        &mut scratch,
                    );
                }
            }
        }
    }
    lloyd.finish();

    centroids
}

/// Re-seed preference: farthest assignment distance first, then lowest point ID.
type ReseedPriority = (u32, Reverse<usize>);

/// Re-seed empty cells from the represented points with the largest assignment
/// error. One closest point is retained in every donor cell, so each selected
/// point is unique and re-assignment cannot create a new empty cell. Empty IDs
/// and equal-distance candidates are resolved in ascending order.
fn reseed_empty_binary_centroids(
    centroids: &mut [u8],
    members: &mut [Vec<usize>],
    assignment: &mut [u32],
    assignment_distances: &mut [u32],
    sample: Option<&[usize]>,
    codes: &[u8],
    byte_len: usize,
) {
    let empty_clusters: Vec<usize> = members
        .iter()
        .enumerate()
        .filter_map(|(cluster, cluster_members)| cluster_members.is_empty().then_some(cluster))
        .collect();
    if empty_clusters.is_empty() {
        return;
    }

    // Retain the closest (then lowest-index) represented point in each donor
    // cell. Every other point is safe to move without emptying its donor.
    //
    // Only `empty_clusters.len()` of those points are ever used, so selection
    // runs through a bounded heap: the previous full candidate list was an
    // allocation proportional to the whole training sample, on every Lloyd
    // iteration that had even one empty cell.
    let wanted = empty_clusters.len();
    let priority =
        |point: usize| -> ReseedPriority { (assignment_distances[point], Reverse(point)) };
    // The heap keeps the *least* preferred survivor on top, so a bounded push/pop
    // retains exactly the `wanted` most preferred points.
    let mut selected: BinaryHeap<Reverse<ReseedPriority>> = BinaryHeap::with_capacity(wanted + 1);
    for cluster_members in members.iter().filter(|members| !members.is_empty()) {
        let keeper = *cluster_members
            .iter()
            .min_by_key(|&&point| (assignment_distances[point], point))
            .expect("non-empty binary cluster must have a represented point");
        for &point in cluster_members.iter().filter(|&&point| point != keeper) {
            selected.push(Reverse(priority(point)));
            if selected.len() > wanted {
                selected.pop();
            }
        }
    }
    debug_assert!(selected.len() >= wanted.min(selected.capacity()));

    let mut candidates: Vec<usize> = selected
        .into_iter()
        .map(|Reverse((_, Reverse(point)))| point)
        .collect();
    candidates.sort_unstable_by_key(|&point| Reverse(priority(point)));

    for (&empty_cluster, &point) in empty_clusters.iter().zip(candidates.iter()) {
        let donor = assignment[point] as usize;
        assignment[point] = empty_cluster as u32;
        assignment_distances[point] = 0;
        let vector_index = sample.map_or(point, |sample| sample[point]);
        centroids[empty_cluster * byte_len..(empty_cluster + 1) * byte_len]
            .copy_from_slice(&codes[vector_index * byte_len..(vector_index + 1) * byte_len]);

        // Keep the membership view consistent with the assignments before the
        // centroid-update phase: a donor cell must not retain a point that
        // moved into a newly seeded cell. Only moved points are touched, so
        // this costs one donor scan each instead of rebuilding every cell.
        if let Some(position) = members[donor].iter().position(|&member| member == point) {
            members[donor].remove(position);
        }
        members[empty_cluster].push(point);
    }
}

/// Per-worker counters for Hamming-majority centroid updates.
///
/// Counts live as eight bit-planes of one byte-counter per code byte, so the
/// accumulation loop is a flat `u8` add over a contiguous plane and vectorises.
/// The narrow planes are folded into wide counters before they can overflow,
/// which keeps one 20 KiB-scale allocation per worker instead of one per
/// centroid per Lloyd iteration.
#[derive(Default)]
struct BinaryMajorityScratch {
    planes: Vec<u8>,
    counts: Vec<u32>,
}

/// Members accumulated into `u8` planes before folding. A plane slot counts one
/// bit per member, so 255 members is the overflow bound.
const MAJORITY_PLANE_FLUSH: usize = 255;

impl BinaryMajorityScratch {
    fn reset(&mut self, byte_len: usize) {
        let slots = byte_len * 8;
        self.planes.clear();
        self.planes.resize(slots, 0);
        self.counts.clear();
        self.counts.resize(slots, 0);
    }

    #[inline]
    fn accumulate(&mut self, vector: &[u8], byte_len: usize) {
        for bit in 0..8 {
            let plane = &mut self.planes[bit * byte_len..(bit + 1) * byte_len];
            for (slot, &byte) in plane.iter_mut().zip(vector) {
                *slot += (byte >> bit) & 1;
            }
        }
    }

    #[inline]
    fn fold(&mut self) {
        for (count, plane) in self.counts.iter_mut().zip(self.planes.iter_mut()) {
            *count += u32::from(*plane);
            *plane = 0;
        }
    }

    #[inline]
    fn count(&self, bit: usize, byte_index: usize, byte_len: usize) -> u32 {
        self.counts[bit * byte_len + byte_index]
    }
}

/// Compute one Hamming-space centroid using a per-byte bit histogram. Member
/// IDs arrive in input order, so the result is deterministic regardless of
/// which centroids rayon updates concurrently.
fn update_binary_centroid(
    centroid: &mut [u8],
    members: &[usize],
    sample: Option<&[usize]>,
    codes: &[u8],
    byte_len: usize,
    scratch: &mut BinaryMajorityScratch,
) {
    scratch.reset(byte_len);
    for (position, &member) in members.iter().enumerate() {
        let vector_index = sample.map_or(member, |sample| sample[member]);
        let vector = &codes[vector_index * byte_len..(vector_index + 1) * byte_len];
        scratch.accumulate(vector, byte_len);
        if (position + 1).is_multiple_of(MAJORITY_PLANE_FLUSH) {
            scratch.fold();
        }
    }
    scratch.fold();

    let total = members.len() as u32;
    for (byte_index, byte) in centroid.iter_mut().enumerate() {
        let previous = *byte;
        let mut packed = 0u8;
        for bit in 0..8 {
            let ones = scratch.count(bit, byte_index, byte_len);
            let value = match ones.cmp(&(total - ones)) {
                std::cmp::Ordering::Greater => 1,
                std::cmp::Ordering::Less => 0,
                // Majority ties keep the previous bit rather than biasing to 0.
                std::cmp::Ordering::Equal => (previous >> bit) & 1,
            };
            packed |= value << bit;
        }
        *byte = packed;
    }
}

/// Hierarchical k-majority training keeps large global codebooks tractable:
/// train sqrt(K) parent cells, partition the sample once, then train each
/// child codebook independently. Complexity is O(N·sqrt(K)) rather than
/// O(N·K), while leaf centroids remain ordinary Hamming-majority centroids.
fn train_k_majority_hierarchical(
    config: &BinaryIvfConfig,
    codes: &[u8],
    n: usize,
    index_label: &str,
) -> (Vec<u8>, BinaryCentroidRouter) {
    let byte_len = config.byte_len();
    let parent_count = routing_parent_count(config.num_clusters).min(n);
    let mut parent_config = config.clone();
    parent_config.num_clusters = parent_count;
    parent_config.max_train_samples = config.max_train_samples.min(n);
    let parents = train_k_majority(&parent_config, codes, n, index_label);

    let mut assignments = vec![0u32; n];
    let mut group_sizes = vec![0usize; parent_count];
    let kernel = HammingKernel::resolve();
    #[cfg(feature = "native")]
    {
        use rayon::prelude::*;
        assignments.par_iter_mut().enumerate().for_each_init(
            || vec![0u32; parent_count],
            |distances, (index, assignment)| {
                *assignment = nearest_binary_centroid_with_distance(
                    kernel,
                    &codes[index * byte_len..(index + 1) * byte_len],
                    &parents,
                    byte_len,
                    distances,
                )
                .0;
            },
        );
    }
    #[cfg(not(feature = "native"))]
    {
        let mut distances = vec![0u32; parent_count];
        for (index, assignment) in assignments.iter_mut().enumerate() {
            *assignment = nearest_binary_centroid_with_distance(
                kernel,
                &codes[index * byte_len..(index + 1) * byte_len],
                &parents,
                byte_len,
                &mut distances,
            )
            .0;
        }
    }
    for &assignment in &assignments {
        group_sizes[assignment as usize] += 1;
    }
    let child_counts = allocate_child_clusters(&group_sizes, config.num_clusters);
    let mut groups: Vec<Vec<u8>> = group_sizes
        .iter()
        .map(|&size| Vec::with_capacity(size.saturating_mul(byte_len)))
        .collect();
    for (index, &assignment) in assignments.iter().enumerate() {
        groups[assignment as usize]
            .extend_from_slice(&codes[index * byte_len..(index + 1) * byte_len]);
    }
    drop(assignments);

    // Child codebooks share nothing: each trains from its own parent's group
    // with its own derived seed. Training them concurrently is the level that
    // actually fills the machine — one child covers only a few tens of
    // thousands of codes, far too little work for its internal rayon loops to
    // saturate a 48-core box, so a sequential loop over parents left most of
    // the machine idle for the bulk of a large codebook's build.
    let populated = child_counts.iter().filter(|&&count| count > 0).count();
    let child_phase = PhaseProgress::start(
        index_label,
        "child codebooks",
        format!("{populated} parents -> {} leaves", config.num_clusters),
        populated,
    );
    let shared = child_phase.shared();
    let train_child = |(parent, (group, &child_count)): (usize, (&Vec<u8>, &usize))| -> Vec<u8> {
        if child_count == 0 {
            return Vec::new();
        }
        let mut child_config = config.clone();
        child_config.num_clusters = child_count;
        child_config.max_train_samples = group.len() / byte_len;
        child_config.seed = config.seed ^ (parent as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15);
        let trained = train_k_majority_reporting(
            &child_config,
            group,
            group.len() / byte_len,
            index_label,
            false,
        );
        shared.complete_one();
        trained
    };
    #[cfg(feature = "native")]
    let trained_children: Vec<Vec<u8>> = {
        use rayon::prelude::*;
        groups
            .par_iter()
            .zip(child_counts.par_iter())
            .enumerate()
            .map(train_child)
            .collect()
    };
    #[cfg(not(feature = "native"))]
    let trained_children: Vec<Vec<u8>> = groups
        .iter()
        .zip(child_counts.iter())
        .enumerate()
        .map(train_child)
        .collect();
    child_phase.finish();

    // Assemble in parent order, so the leaf matrix is byte-identical to the
    // sequential build and every parent still owns one contiguous leaf run.
    let mut leaves = Vec::with_capacity(config.num_clusters.saturating_mul(byte_len));
    let mut children = vec![Vec::new(); parent_count];
    for (parent, (trained, &child_count)) in trained_children.iter().zip(&child_counts).enumerate()
    {
        if child_count == 0 {
            continue;
        }
        let first_leaf = leaves.len() / byte_len;
        leaves.extend_from_slice(trained);
        children[parent].extend((first_leaf..first_leaf + child_count).map(|leaf| leaf as u32));
    }
    debug_assert_eq!(leaves.len(), config.num_clusters * byte_len);
    (
        leaves,
        BinaryCentroidRouter::TwoLevel {
            parent_centroids: parents,
            topology: IvfRoutingTopology::from_children(&children),
        },
    )
}

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

    fn trained_index(
        dim_bits: usize,
        clusters: usize,
        codes: &[u8],
        labels: &[(u32, u16)],
    ) -> (BinaryCoarseQuantizer, BinaryIvfIndex) {
        let mut config = BinaryIvfConfig::new(dim_bits, clusters);
        config.train_iters = 4;
        config.max_train_samples = labels.len();
        let quantizer = BinaryCoarseQuantizer::train(config, codes, labels.len(), "test").unwrap();
        let index = BinaryIvfIndex::build(&quantizer, IvfRoutingMode::Flat, codes, labels).unwrap();
        (quantizer, index)
    }

    #[test]
    fn full_probe_matches_exact_hamming_and_preserves_ties() {
        let dim = 64;
        let byte_len = dim / 8;
        let n = 300;
        let mut rng = rand::rngs::StdRng::seed_from_u64(11);
        let codes: Vec<u8> = (0..n * byte_len).map(|_| rng.random()).collect();
        let labels: Vec<_> = (0..n as u32).map(|doc_id| (doc_id, 0)).collect();
        let query: Vec<u8> = (0..byte_len).map(|_| rng.random()).collect();
        let (quantizer, index) = trained_index(dim, 8, &codes, &labels);
        let plan = quantizer.probe(&query, 8, IvfRoutingMode::Flat);
        let actual = index.search_in_clusters(&query, 20, &plan.cluster_ids);

        let mut scores = vec![0.0; n];
        batch_hamming_scores(&query, &codes, byte_len, dim, &mut scores);
        let mut expected: Vec<_> = scores
            .into_iter()
            .enumerate()
            .map(|(doc_id, score)| (doc_id as u32, 0, score))
            .collect();
        expected.sort_unstable_by(|left, right| {
            right
                .2
                .total_cmp(&left.2)
                .then_with(|| left.0.cmp(&right.0))
        });
        expected.truncate(20);
        assert_eq!(actual, expected);
    }

    /// Build a clustered binary corpus: `groups` random anchors, each with
    /// `per_group` codes at a small Hamming radius. Real embedding corpora are
    /// clustered, and uniform noise would make every routing budget look alike.
    fn clustered_binary_codes(
        byte_len: usize,
        groups: usize,
        per_group: usize,
        flips: usize,
        seed: u64,
    ) -> Vec<u8> {
        let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
        let mut codes = Vec::with_capacity(groups * per_group * byte_len);
        for _ in 0..groups {
            let mut anchor = vec![0u8; byte_len];
            rng.fill_bytes(&mut anchor);
            for _ in 0..per_group {
                let mut code = anchor.clone();
                for _ in 0..flips {
                    let bit = rng.random_range(0..byte_len * 8);
                    code[bit / 8] ^= 1 << (bit % 8);
                }
                codes.extend_from_slice(&code);
            }
        }
        codes
    }

    /// The construction beam is a *floor* paid by every assigned vector, so its
    /// value has to be justified by assignment quality rather than by "build can
    /// afford more". Assignment recall against exact centroid scan saturates
    /// well below the floor: widening past it only multiplies the distance work
    /// each rebuilt segment pays per vector.
    ///
    /// Referenced by `HNSW_MIN_EF_BUILD` in `ivf/routing.rs`.
    #[cfg(feature = "native")]
    #[test]
    fn hnsw_build_beam_recall_saturates_before_the_floor() {
        use crate::structures::vector::ivf::routing::HNSW_MIN_EF_BUILD;

        let dim_bits = 256;
        let byte_len = dim_bits / 8;
        let codes = clustered_binary_codes(byte_len, 512, 24, 12, 0x5eed_1234);
        let points = codes.len() / byte_len;

        let mut config = BinaryIvfConfig::new(dim_bits, 4_096);
        config.train_iters = 3;
        config.max_train_samples = points;
        config.routing = IvfRoutingMode::Hnsw;
        let quantizer = BinaryCoarseQuantizer::train(config, &codes, points, "test").unwrap();
        let Some(BinaryCentroidRouter::Hnsw(graph)) = quantizer.routing_index.as_ref() else {
            panic!("HNSW routing expected at 4096 clusters");
        };

        let probes = clustered_binary_codes(byte_len, 64, 4, 14, 0x9ab_cdef);
        let exact: Vec<u32> = probes
            .chunks_exact(byte_len)
            .map(|code| quantizer.find_nearest(code))
            .collect();

        let recall_at = |ef: usize| -> f64 {
            let hits = probes
                .chunks_exact(byte_len)
                .zip(exact.iter())
                .filter(|&(code, &want)| {
                    graph.search_best_with_ef(
                        BinaryCentroidDistance::new(code, &quantizer.centroids, byte_len),
                        ef,
                    ) == Some(want)
                })
                .count();
            hits as f64 / exact.len() as f64
        };

        let (narrow, floor, wide) = (recall_at(32), recall_at(HNSW_MIN_EF_BUILD), recall_at(512));

        // Cost side of the same comparison: count centroid distance
        // evaluations, which is deterministic where wall-clock is not.
        struct CountingDistance<'a> {
            inner: BinaryCentroidDistance<'a>,
            calls: &'a std::cell::Cell<usize>,
        }
        impl QueryDistance for CountingDistance<'_> {
            fn distance(&self, node: u32) -> f32 {
                self.calls.set(self.calls.get() + 1);
                self.inner.distance(node)
            }

            fn distances(&self, nodes: &[u32], out: &mut [f32]) {
                self.calls.set(self.calls.get() + nodes.len());
                self.inner.distances(nodes, out);
            }
        }
        let work_at = |ef: usize| -> usize {
            let calls = std::cell::Cell::new(0usize);
            for code in probes.chunks_exact(byte_len) {
                graph.search_best_with_ef(
                    CountingDistance {
                        inner: BinaryCentroidDistance::new(code, &quantizer.centroids, byte_len),
                        calls: &calls,
                    },
                    ef,
                );
            }
            calls.get()
        };
        let (floor_work, wide_work) = (work_at(HNSW_MIN_EF_BUILD), work_at(512));
        println!(
            "assignment recall@1: ef=32 {narrow:.4}, ef={HNSW_MIN_EF_BUILD} {floor:.4}, ef=512 {wide:.4}\n\
             assignment distance evaluations: ef={HNSW_MIN_EF_BUILD} {floor_work}, ef=512 {wide_work} \
             ({:.2}x)",
            wide_work as f64 / floor_work as f64
        );
        assert!(
            wide_work > floor_work,
            "a wider beam must cost more work: {wide_work} vs {floor_work}"
        );

        // The floor must be worth paying for over a clearly narrow beam...
        assert!(
            floor >= narrow,
            "floor beam {HNSW_MIN_EF_BUILD} must not route worse than ef=32: {floor:.4} vs {narrow:.4}"
        );
        // ...and 4x more work than the floor must not be buying recall, which is
        // what makes the previous 512 floor pure overhead.
        assert!(
            wide - floor <= 0.005,
            "ef=512 bought {:.4} recall over ef={HNSW_MIN_EF_BUILD}; the floor is too low",
            wide - floor
        );
    }

    /// Child codebooks train concurrently, so the leaf matrix and topology must
    /// still be byte-identical whatever the pool width — the quantizer is a
    /// shared artifact and every segment's postings are keyed to its leaf IDs.
    #[cfg(feature = "native")]
    #[test]
    fn hierarchical_training_is_deterministic_across_thread_counts() {
        let dim_bits = 64;
        let byte_len = dim_bits / 8;
        let points = 4_000;
        let mut rng = rand::rngs::StdRng::seed_from_u64(0x1234_5678);
        let mut codes = vec![0u8; points * byte_len];
        rng.fill_bytes(&mut codes);

        let mut config = BinaryIvfConfig::new(dim_bits, 96);
        config.train_iters = 3;
        config.max_train_samples = points;

        let train = |threads: usize| {
            rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .unwrap()
                .install(|| train_k_majority_hierarchical(&config, &codes, points, "test"))
        };
        let (one_leaves, one_router) = train(1);
        let (eight_leaves, eight_router) = train(8);

        assert_eq!(one_leaves, eight_leaves, "leaf centroids diverged");
        let (
            BinaryCentroidRouter::TwoLevel { topology: one, .. },
            BinaryCentroidRouter::TwoLevel {
                topology: eight, ..
            },
        ) = (&one_router, &eight_router)
        else {
            panic!("hierarchical training must produce a two-level router");
        };
        assert_eq!(one, eight, "parent topology diverged");
        assert!(one.validate(config.num_clusters), "topology invalid");
    }

    #[cfg(feature = "native")]
    #[test]
    fn k_majority_is_deterministic_across_thread_counts() {
        let dim_bits = 128;
        let byte_len = dim_bits / 8;
        let points = 1_024;
        let mut rng = rand::rngs::StdRng::seed_from_u64(0xfeed_cafe);
        let mut codes = vec![0u8; points * byte_len];
        rng.fill_bytes(&mut codes);

        let mut config = BinaryIvfConfig::new(dim_bits, 32);
        config.train_iters = 4;
        config.max_train_samples = points;
        let one_thread = rayon::ThreadPoolBuilder::new()
            .num_threads(1)
            .build()
            .unwrap()
            .install(|| train_k_majority(&config, &codes, points, "test"));
        let four_threads = rayon::ThreadPoolBuilder::new()
            .num_threads(4)
            .build()
            .unwrap()
            .install(|| train_k_majority(&config, &codes, points, "test"));

        assert_eq!(one_thread, four_threads);
    }

    #[test]
    fn k_majority_seeding_uses_raw_hamming_d2_weights() {
        let codes = [0x00, 0x01, 0x03, 0x0f, 0x3f, 0x7f, 0xff];
        let (seed, first, raw_choice, squared_choice) = (0u64..10_000)
            .find_map(|seed| {
                let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
                let first = rng.random_range(0..codes.len());
                let draw = rng.random::<f64>();
                let raw_weights: Vec<f64> = codes
                    .iter()
                    .map(|code| {
                        f64::from(hamming_distance(
                            std::slice::from_ref(code),
                            std::slice::from_ref(&codes[first]),
                        ))
                    })
                    .collect();
                let squared_weights: Vec<f64> =
                    raw_weights.iter().map(|weight| weight * weight).collect();
                let raw_choice =
                    crate::structures::vector::kmeans::weighted_sample_index(&raw_weights, draw)?;
                let squared_choice = crate::structures::vector::kmeans::weighted_sample_index(
                    &squared_weights,
                    draw,
                )?;
                (raw_choice != squared_choice).then_some((seed, first, raw_choice, squared_choice))
            })
            .expect("test data must distinguish Hamming from Hamming-squared sampling");

        let mut config = BinaryIvfConfig::new(8, 2);
        config.seed = seed;
        config.train_iters = 0;
        config.max_train_samples = codes.len();
        let centroids = train_k_majority(&config, &codes, codes.len(), "test");

        assert_eq!(centroids[0], codes[first]);
        assert_eq!(centroids[1], codes[raw_choice]);
        assert_ne!(centroids[1], codes[squared_choice]);
    }

    #[test]
    fn empty_binary_centroids_take_farthest_movable_points_deterministically() {
        let codes = [0x0f, 0x01, 0xf0, 0xf1, 0xfe];
        let mut centroids = vec![0x00, 0xff, 0x55, 0xaa];
        let mut members = vec![vec![0, 1], vec![2, 3, 4], vec![], vec![]];
        let mut assignment = vec![0, 0, 1, 1, 1];
        let mut assignment_distances = vec![4, 1, 4, 3, 1];

        reseed_empty_binary_centroids(
            &mut centroids,
            &mut members,
            &mut assignment,
            &mut assignment_distances,
            None,
            &codes,
            1,
        );

        // Points 0 and 2 have the largest movable distance. The equal-distance
        // tie and empty cluster IDs are both resolved from lowest to highest.
        assert_eq!(centroids, vec![0x00, 0xff, 0x0f, 0xf0]);
        assert_eq!(assignment, vec![2, 0, 3, 1, 1]);
        assert_eq!(assignment_distances, vec![0, 1, 0, 3, 1]);
        assert_eq!(members, vec![vec![1], vec![3, 4], vec![0], vec![2]]);
    }

    #[test]
    fn binary_centroid_majority_ties_keep_previous_bits() {
        let mut scratch = BinaryMajorityScratch::default();
        let codes = [0b1010_1010, 0b0101_0101];
        let mut centroid = [0b1100_0011];
        update_binary_centroid(&mut centroid, &[0, 1], None, &codes, 1, &mut scratch);
        assert_eq!(centroid, [0b1100_0011]);

        let decisive_codes = [0b1111_0000, 0b1111_0000, 0b0000_1111];
        update_binary_centroid(
            &mut centroid,
            &[0, 1, 2],
            None,
            &decisive_codes,
            1,
            &mut scratch,
        );
        assert_eq!(centroid, [0b1111_0000]);
    }

    /// Bit counts accumulate in `u8` planes that must be folded into the wide
    /// counters before 256 members can overflow them, and the scratch is reused
    /// across centroids so it must not carry counts between calls.
    #[test]
    fn binary_centroid_majority_survives_plane_overflow_and_scratch_reuse() {
        let byte_len = 3;
        let members: Vec<usize> = (0..MAJORITY_PLANE_FLUSH * 2 + 7).collect();
        // Every member is all-ones, so the majority must be all-ones however
        // many times the planes were folded.
        let ones = vec![0xffu8; members.len() * byte_len];
        let mut scratch = BinaryMajorityScratch::default();
        let mut centroid = vec![0x00u8; byte_len];
        update_binary_centroid(&mut centroid, &members, None, &ones, byte_len, &mut scratch);
        assert_eq!(centroid, vec![0xff; byte_len]);

        // Reusing the same scratch for an all-zero cluster must yield zeros.
        let zeros = vec![0x00u8; members.len() * byte_len];
        let mut next = vec![0xffu8; byte_len];
        update_binary_centroid(&mut next, &members, None, &zeros, byte_len, &mut scratch);
        assert_eq!(next, vec![0x00; byte_len]);

        // A per-bit split just past the fold boundary resolves by true majority.
        let split_members: Vec<usize> = (0..MAJORITY_PLANE_FLUSH + 3).collect();
        let mut split_codes = vec![0x00u8; split_members.len() * byte_len];
        let ones_majority = MAJORITY_PLANE_FLUSH / 2 + 3;
        for (position, chunk) in split_codes.chunks_mut(byte_len).enumerate() {
            if position < ones_majority {
                chunk.fill(0b0000_1111);
            }
        }
        assert!(ones_majority * 2 > split_members.len());
        let mut split = vec![0x00u8; byte_len];
        update_binary_centroid(
            &mut split,
            &split_members,
            None,
            &split_codes,
            byte_len,
            &mut scratch,
        );
        assert_eq!(split, vec![0b0000_1111; byte_len]);
    }

    #[test]
    fn streaming_builder_appends_batches_without_retaining_inputs() {
        let codes = [0x00, 0x01, 0x02, 0xf0, 0xf1, 0xf2];
        let labels = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0)];
        let config = BinaryIvfConfig::new(8, 2);
        let quantizer = BinaryCoarseQuantizer::train(config, &codes, codes.len(), "test").unwrap();
        let mut builder = BinaryIvfBuilder::new(&quantizer, IvfRoutingMode::Flat).unwrap();
        builder
            .add_batch(&quantizer, &codes[..3], &labels[..3])
            .unwrap();
        builder
            .add_batch(&quantizer, &codes[3..], &labels[3..])
            .unwrap();
        let index = builder.finish().unwrap();
        assert_eq!(index.len(), 6);
        let plan = quantizer.probe(&[0xf0], 2, IvfRoutingMode::Flat);
        assert_eq!(
            index.search_in_clusters(&[0xf0], 1, &plan.cluster_ids)[0].0,
            3
        );

        // Batched inserts group by leaf, so pin the payload layout: one batch
        // must produce the same columns as two, and each leaf must keep its
        // entries in ascending input order.
        let single =
            BinaryIvfIndex::build(&quantizer, IvfRoutingMode::Flat, &codes, &labels).unwrap();
        assert_eq!(index.clusters.len(), single.clusters.len());
        for ((left_id, left), (right_id, right)) in index.clusters.iter().zip(&single.clusters) {
            assert_eq!(left_id, right_id);
            assert_eq!(left.doc_ids, right.doc_ids);
            assert_eq!(left.ordinals, right.ordinals);
            assert_eq!(left.codes, right.codes);
            assert!(
                left.doc_ids.windows(2).all(|pair| pair[0] < pair[1]),
                "leaf {left_id} lost input order: {:?}",
                left.doc_ids
            );
        }
    }

    /// All-zero codes are counted and reported. They are still indexed —
    /// the byte-copy merge requires the payload to hold exactly as many
    /// vectors as flat storage — so the guard is detection, not removal.
    #[test]
    fn all_zero_codes_are_counted_and_reported() {
        let codes = [0x00u8, 0xf0, 0x00, 0x00, 0x0f, 0x00];
        let labels = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0)];
        let mut config = BinaryIvfConfig::new(8, 2);
        config.train_iters = 2;
        config.max_train_samples = labels.len();
        let quantizer = BinaryCoarseQuantizer::train(config, &codes, labels.len(), "test").unwrap();
        let index =
            BinaryIvfIndex::build(&quantizer, IvfRoutingMode::Flat, &codes, &labels).unwrap();

        assert_eq!(index.zero_codes(), 4, "four zero codes must be counted");
        assert_eq!(index.len(), labels.len(), "every vector stays indexed");
        // Ties resolve to the lowest cluster ID, so the zeros share one leaf:
        // this is exactly the collapse the report warns about.
        let (_, largest) = index.largest_cluster().expect("a populated leaf");
        assert!(
            largest >= 4,
            "all-zero codes should share one leaf, got {largest}"
        );
        report_binary_build_quality("test-index", 7, &index);
    }

    /// All-ones codes — signbit-packed NaN — are counted and reported the
    /// same way as zeros, and separately from them: the two faces attribute
    /// to different producer revisions.
    #[test]
    fn all_ones_codes_are_counted_and_reported() {
        let codes = [0xffu8, 0xf0, 0xff, 0xff, 0x0f, 0x00];
        let labels = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0)];
        let mut config = BinaryIvfConfig::new(8, 2);
        config.train_iters = 2;
        config.max_train_samples = labels.len();
        let quantizer = BinaryCoarseQuantizer::train(config, &codes, labels.len(), "test").unwrap();
        let index =
            BinaryIvfIndex::build(&quantizer, IvfRoutingMode::Flat, &codes, &labels).unwrap();

        assert_eq!(
            index.ones_codes(),
            3,
            "three all-ones codes must be counted"
        );
        assert_eq!(index.zero_codes(), 1, "the zero code is counted on its own");
        assert_eq!(index.len(), labels.len(), "every vector stays indexed");
        // Identical codes share a nearest centroid, so the constant collapses
        // into one leaf: the same topology the ann_health warning names.
        let (_, largest) = index.largest_cluster().expect("a populated leaf");
        assert!(
            largest >= 3,
            "all-ones codes should share one leaf, got {largest}"
        );
        report_binary_build_quality("test-index", 7, &index);
    }

    /// A field that is entirely zero still produces a payload, so merges that
    /// require one keep working.
    #[test]
    fn an_entirely_zero_field_still_produces_a_payload() {
        let codes = [0x00u8; 4];
        let labels = [(0, 0), (1, 0), (2, 0), (3, 0)];
        let mut config = BinaryIvfConfig::new(8, 2);
        config.train_iters = 1;
        config.max_train_samples = labels.len();
        let quantizer = BinaryCoarseQuantizer::train(config, &codes, labels.len(), "test").unwrap();
        let index =
            BinaryIvfIndex::build(&quantizer, IvfRoutingMode::Flat, &codes, &labels).unwrap();
        assert!(!index.is_empty(), "payload must cover every flat vector");
        assert_eq!(index.len(), labels.len());
        assert_eq!(index.zero_codes(), 4);
    }

    #[test]
    fn largest_cluster_reports_the_dominant_leaf() {
        let codes = [0x0fu8, 0x0f, 0x0e, 0x0f, 0x0d, 0x0f, 0xf0];
        let labels = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0)];
        let mut config = BinaryIvfConfig::new(8, 2);
        config.train_iters = 4;
        config.max_train_samples = labels.len();
        let quantizer = BinaryCoarseQuantizer::train(config, &codes, labels.len(), "test").unwrap();
        let index =
            BinaryIvfIndex::build(&quantizer, IvfRoutingMode::Flat, &codes, &labels).unwrap();
        let (_, count) = index.largest_cluster().expect("a populated leaf");
        assert_eq!(count, 6, "the dominant leaf holds every near-duplicate");
    }

    #[test]
    fn sparse_payload_does_not_allocate_empty_leaf_columns() {
        let codes = [0x00, 0xff];
        let labels = [(0, 0), (1, 0)];
        let (quantizer, index) = trained_index(8, 2, &codes, &labels);
        assert!(index.clusters.len() <= 2);
        assert_eq!(index.quantizer_version, quantizer.version);
    }

    #[test]
    fn child_allocation_is_exact_and_never_exceeds_group_size() {
        let sizes = [100, 30, 0, 7];
        let allocation = allocate_child_clusters(&sizes, 64);
        assert_eq!(allocation.iter().sum::<usize>(), 64);
        assert!(
            allocation
                .iter()
                .zip(sizes)
                .all(|(&cells, size)| cells <= size)
        );
        assert_eq!(allocation[2], 0);
    }

    #[test]
    fn binary_two_level_build_assignment_checks_four_parents() {
        let leaves_per_parent = 512;
        let children: Vec<Vec<u32>> = (0..4)
            .map(|parent| {
                let first = parent * leaves_per_parent;
                (first..first + leaves_per_parent)
                    .map(|leaf| leaf as u32)
                    .collect()
            })
            .collect();
        let mut leaf_centroids = vec![0x01; 4 * leaves_per_parent];
        let best_leaf = 3 * leaves_per_parent;
        leaf_centroids[best_leaf] = 0x00;
        let quantizer = BinaryCoarseQuantizer {
            dim_bits: 8,
            num_clusters: leaf_centroids.len() as u32,
            centroids: leaf_centroids,
            version: 1,
            routing_index: Some(BinaryCentroidRouter::TwoLevel {
                parent_centroids: vec![0x00, 0xff, 0xff, 0xff],
                topology: IvfRoutingTopology::from_children(&children),
            }),
        };

        assert_eq!(
            &*quantizer
                .probe(&[0x00], 1, IvfRoutingMode::TwoLevel)
                .cluster_ids,
            &[0]
        );
        assert_eq!(
            quantizer.assign(&[0x00], IvfRoutingMode::TwoLevel),
            best_leaf as u32
        );
    }

    #[test]
    fn persisted_binary_hnsw_and_two_level_routers_are_valid() {
        let mut rng = rand::rngs::StdRng::seed_from_u64(1234);
        let codes: Vec<u8> = (0..256 * 8).map(|_| rng.random()).collect();
        for routing in [IvfRoutingMode::Hnsw, IvfRoutingMode::TwoLevel] {
            let mut config = BinaryIvfConfig::new(64, 16);
            config.routing = routing;
            config.train_iters = 3;
            config.max_train_samples = 256;
            let quantizer = BinaryCoarseQuantizer::train(config, &codes, 256, "test").unwrap();
            quantizer.validate_routing(routing).unwrap();
            let plan = quantizer.probe(&codes[..8], 8, routing);
            assert_eq!(plan.cluster_ids.len(), 8);
            assert!(
                plan.cluster_ids
                    .iter()
                    .all(|&cluster| cluster < quantizer.num_clusters)
            );

            let bytes =
                bincode::serde::encode_to_vec(&quantizer, bincode::config::standard()).unwrap();
            let (loaded, consumed): (BinaryCoarseQuantizer, usize) =
                bincode::serde::decode_from_slice(&bytes, bincode::config::standard()).unwrap();
            assert_eq!(consumed, bytes.len());
            loaded.validate_routing(routing).unwrap();
        }
    }
}