Clustering più veloce per gli indici vettoriali con e-means
I post del blog sono scritti solo in inglese.
Many of our indexes start with the all-too-well-known k-means. Vectorium trains the codebooks of Product Quantization (PQ)1 with it, kANNolo2 uses it to find the coarse centroids of its IVF indexes, and TACHIOM3 runs it to cluster token vectors in its Token-Aware Clustering (TAC). Until now, all these tools used Lloyd's algorithm4, the classic k-means solver.
e-means5 is a recent alternative from the FAISS6 team at Meta: a mini-batch k-means7 that smooths the centroid updates with an exponential moving average. With our pull request (#3), e-means lands in Vectorium 0.12, and with it in kANNolo 0.17 and TACHIOM 0.7.
At our default of 2 epochs, e-means trains k-means 1.5× to 5× faster than Lloyd at Vectorium's default of 10 epochs, and reaches the same or lower clustering error in almost every setting. On LoTTE8, with 354.6 million token vectors, TAC clustering drops from 809 to 286 seconds at the same retrieval quality.
From Lloyd to e-means
Lloyd's algorithm4 alternates two steps: it assigns every training vector to its nearest centroid, then moves each centroid to the mean of the vectors assigned to it. The centroids change once per pass over the data. Inspired by VQ-VAEs'9 exponential moving-average (EMA) approach to codebook learning over a moving distribution, and mini-batch k-means, e-means updates its centroids with an EMA interpolation step every mini-batch, so a single pass already performs many updates, and the moving average keeps them stable. The e-means paper reports a lower error than Lloyd in a tenth of the passes, and a greater ability to escape local minima from poor initialization.
Budgets in our libraries are given in epochs. One epoch assigns every training vector once and costs about the same for both solvers. The defaults are 10 epochs for Lloyd and 2 for e-means, and all the comparisons below use these values.
Usage
Lloyd is still the default solver in all three libraries, and e-means is one option away:
ivf_build -d dataset.npy -o index.ivf --n-clusters 4096 --kmeans-solver emeans
tachiom_build ... --tac-solver emeans --pq-solver emeansimport tachiom
tachiom.Tachiom.build(vectors, token_ids, doclens, tac_solver="emeans", pq_solver="emeans")Matching FAISS
When porting e-means to our tools, we first checked it against FAISS (faiss-cpu 1.16.0, with e-means from
FAISS's main branch). Both implementations read the same training sample and the same 20,000
held-out vectors, start from the same k = 4,096 random training points, and run on one socket
(48 threads) of a Xeon Gold 6252N. FAISS is restricted to its own AVX-512 kernels, without BLAS,
to match Vectorium's SIMD kernels.
The collections are SIFT1M1 and the MS MARCO10 passages embedded with Contriever11, DRAGON12 and Snowflake13. MSE is the clustering error on the held-out vectors, i.e. the mean squared distance between each vector and its nearest centroid (lower is better; its scale depends on the embedding, so compare it only within a dataset). Time is the wall-clock training time in seconds. SIFT1M numbers are the mean of 3 seeds.
| Dataset | Solver | MSE Vectorium | MSE FAISS | Time (s) Vectorium | Time (s) FAISS |
|---|---|---|---|---|---|
| SIFT1M (200k train, d = 128) | Lloyd, 10 epochs | 46548 | 46529 | 6.1 | 6.2 |
| e-means, 2 epochs | 46452 | 46437 | 1.3 | 1.5 | |
| Contriever (500k train, d = 768) | Lloyd, 10 epochs | 1.2266 | 1.2261 | 101 | 107 |
| e-means, 2 epochs | 1.2138 | 1.2137 | 19.2 | 21.5 | |
| DRAGON (500k train, d = 768) | Lloyd, 10 epochs | 25.766 | 25.753 | 113 | 118 |
| e-means, 2 epochs | 25.622 | 25.630 | 19.3 | 21.5 | |
| Snowflake (500k train, d = 1024) | Lloyd, 10 epochs | 0.6644 | 0.6637 | 156 | 146 |
| e-means, 2 epochs | 0.6615 | 0.6616 | 30.9 | 28.9 |
Vectorium's e-means matches FAISS within seed noise on SIFT1M and to 3–4 significant digits on the MS MARCO embeddings, and the same holds at k = 1,024. Speed is on par: from 10% slower to 20% faster up to d = 768, and 3–15% slower at d = 1,024. FAISS's default path, which computes distances with BLAS matrix multiplications, is faster than both: 2× on SIFT1M and 3–4× on MS MARCO for Lloyd. On every dataset, e-means at 2 epochs reaches a lower error than Lloyd at 10, in a fifth of the time.
Results in our libraries
We ran each comparison alone on a Xeon Gold 6252N, with 96 threads over two NUMA nodes, and timed search on a single thread. The collections are SIFT1M (1M vectors, d = 128, L2), Contriever on MS MARCO (8.8M vectors, d = 768, inner product) and FiQA14 encoded with ColBERTv28 (6.05M token vectors). The LoTTE numbers come from the pull request, on an AMD EPYC 7713.
The PQ speedup is smaller, but still consistent, on SIFT1M: with 128 dimensions and 256 centroids per subspace, the mini-batches are small, and the cost of each update limits the speedup at 96 threads. The pull request measured 2.5× on SIFT1M with 40 threads and 4.5× on 4,096-dimensional embeddings. For sparse IVF, it reports 4.4–5× faster training at equal recall on NQ15 and on three MS MARCO collections encoded with SPLADE16 models.
The quality is the same or better, except for PQ with many subspaces. The first table gives the change from Lloyd to e-means in clustering error (negative is better) and, for PQ, in R@10 (positive is better). For PQ, the error is the reconstruction MSE and R@10 is the 10-recall@10 of exhaustive search with asymmetric distances.
| Dataset | Component | Error change | R@10 change |
|---|---|---|---|
| SIFT1M | PQ, M = 16 | −2.8% | +0.2 pts |
| PQ, M = 32 | −10.0% | +1.5 pts | |
| PQ, M = 64 | +5.1% | −0.3 pts | |
| Contriever | PQ, M = 96 | −1.1% | −0.3 pts |
| FiQA | TAC, 65,536 centroids | −0.3% | |
| LoTTE | TAC, 2,097,152 centroids | −0.4% |
The second table gives the search quality of the indexes built on top, with Lloyd and with e-means (higher is better):
| Dataset | Index | Metric | Lloyd | e-means |
|---|---|---|---|---|
| SIFT1M | IVF, 4,096 clusters | R@10, nprobe 4 | 0.575 | 0.577 |
| R@10, nprobe 16 | 0.843 | 0.845 | ||
| R@10, nprobe 64 | 0.974 | 0.974 | ||
| Contriever | IVF, 4,096 clusters | R@10, nprobe 16 | 0.829 | 0.845 |
| FiQA | Full TACHIOM index | R@10 | 0.931 | 0.939 |
| nDCG@10 | 0.3425 | 0.3435 | ||
| LoTTE | Full TACHIOM index | nDCG@10 | 0.5725 | 0.5735 |
| R@100 | 0.7380 | 0.7397 | ||
| Success@5 | 0.8062 | 0.8035 |
On FiQA, R@10 is measured against exact MaxSim search, and nDCG@10 with exact search is 0.347. The Contriever IVF trains on a 1M-vector sample. End to end, the LoTTE build takes 1,223 seconds with e-means instead of 1,672.
Faster assignment with an index over the centroids
With many centroids, most of the cost of k-means goes into finding the nearest centroid of each
vector. kANNolo's IVF and TACHIOM's global k-means now search the centroids through an ANN index,
turned on automatically from 4,096 centroids (--kmeans-ann auto|on|off). On SIFT1M with 4,096
clusters, this makes k-means 3–4× faster with the same IVF recall. TAC does not use it yet: it
runs a separate k-means for each token type, and on FiQA none of them gets more than a few
hundred centroids.
The index has to be refreshed every time the centroids move. Lloyd moves them once per epoch, so search speed matters most, and it uses an HNSW17 graph. e-means moves them after every mini-batch, tens of times per epoch, and rebuilding an HNSW graph that often costs more than the clustering itself. It uses an IVF over the centroids instead: its coarse quantizer is trained once, and each refresh only re-files the centroids that moved, as in FAISS.
| k-means training time | Lloyd 10 + HNSW | e-means 2 + HNSW | e-means 2 + IVF |
|---|---|---|---|
| SIFT1M, 32,768 clusters (kANNolo) | 10.6 s | 28.0 s | 5.1 s |
| FiQA, 65,536 centroids (TACHIOM global k-means) | 28.7 s | 51.0 s | 18.4 s |
Retrieval quality is the same in all three cases: on FiQA, TACHIOM's R@10 is 0.922, 0.917 and 0.920.
References
-
Hervé Jégou, Matthijs Douze, Cordelia Schmid. Product Quantization for Nearest Neighbor Search. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2011. doi:10.1109/TPAMI.2010.57 ↩ ↩2
-
Leonardo Delfino, Domenico Erriquez, Silvio Martinico, Franco Maria Nardini, Cosimo Rulli, Rossano Venturini. kANNolo: Sweet and Smooth Approximate k-Nearest Neighbors Search. ECIR, 2025. doi:10.1007/978-3-031-88717-8_29 ↩
-
Silvio Martinico, Franco Maria Nardini, Cosimo Rulli, Rossano Venturini. Efficient Multivector Retrieval with Token-Aware Clustering and Hierarchical Indexing. SIGIR, 2026. doi:10.1145/3805712.3809927 ↩
-
Stuart P. Lloyd. Least Squares Quantization in PCM. IEEE Transactions on Information Theory, 1982. doi:10.1109/TIT.1982.1056489 ↩ ↩2
-
Swann Bessa, Pierre-Emmanuel Mazaré, Gergely Szilvasy, Hugo Touvron, David Picard, Matthijs Douze, Hervé Jégou. e-means: Outperforming k-means (in One-Tenth the Epochs). arXiv, 2026. arXiv:2611.09501 ↩
-
Matthijs Douze, Alexandr Guzhva, Chengqi Deng, Jeff Johnson, Gergely Szilvasy, Pierre-Emmanuel Mazaré, Maria Lomeli, Lucas Hosseini, Hervé Jégou. The Faiss Library. arXiv, 2024. arXiv:2401.08281 ↩
-
D. Sculley. Web-Scale K-Means Clustering. WWW, 2010. doi:10.1145/1772690.1772862 ↩
-
Keshav Santhanam, Omar Khattab, Jon Saad-Falcon, Christopher Potts, Matei Zaharia. ColBERTv2: Effective and Efficient Retrieval via Lightweight Late Interaction. NAACL, 2022. doi:10.18653/v1/2022.naacl-main.272 ↩ ↩2
-
Aaron van den Oord, Oriol Vinyals, Koray Kavukcuoglu. Neural Discrete Representation Learning. NeurIPS, 2017. arXiv:1711.00937 ↩
-
Tri Nguyen, Mir Rosenberg, Xia Song, Jianfeng Gao, Saurabh Tiwary, Rangan Majumder, Li Deng. MS MARCO: A Human Generated MAchine Reading COmprehension Dataset. CoCo Workshop at NeurIPS, 2016. ceur-ws.org ↩
-
Gautier Izacard, Mathilde Caron, Lucas Hosseini, Sebastian Riedel, Piotr Bojanowski, Armand Joulin, Edouard Grave. Unsupervised Dense Information Retrieval with Contrastive Learning. arXiv, 2021. arXiv:2112.09118 ↩
-
Sheng-Chieh Lin, Akari Asai, Minghan Li, Barlas Oguz, Jimmy Lin, Yashar Mehdad, Wen-tau Yih, Xilun Chen. How to Train Your Dragon: Diverse Augmentation Towards Generalizable Dense Retrieval. Findings of EMNLP, 2023. aclanthology.org ↩
-
Puxuan Yu, Luke Merrick, Gaurav Nuti, Daniel Campos. Arctic-Embed 2.0: Multilingual Retrieval Without Compromise. arXiv, 2024. arXiv:2412.04506 ↩
-
Nandan Thakur, Nils Reimers, Andreas Rücklé, Abhishek Srivastava, Iryna Gurevych. BEIR: A Heterogeneous Benchmark for Zero-shot Evaluation of Information Retrieval Models. NeurIPS Datasets and Benchmarks Track, 2021. openreview.net ↩
-
Tom Kwiatkowski, Jennimaria Palomaki, Olivia Redfield, et al. Natural Questions: A Benchmark for Question Answering Research. Transactions of the Association for Computational Linguistics, 2019. doi:10.1162/tacl_a_00276 ↩
-
Thibault Formal, Benjamin Piwowarski, Stéphane Clinchant. SPLADE: Sparse Lexical and Expansion Model for First Stage Ranking. SIGIR, 2021. doi:10.1145/3404835.3463098 ↩
-
Yu A. Malkov, D. A. Yashunin. Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2020. doi:10.1109/TPAMI.2018.2889473 ↩