Metadata-Version: 2.2
Name: superkmeans
Version: 0.3.0
Summary: A Super fast K-Means library for High-Dimensional vectors on CPUs (x86, ARM) and GPUs
Author-Email: Leonardo Kuffo <lxkr@cwi.nl>
License: MIT
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Project-URL: Homepage, https://github.com/cwida/superkmeans
Project-URL: Repository, https://github.com/cwida/superkmeans
Requires-Python: >=3.9
Requires-Dist: numpy>=1.20.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: scikit-learn>=1.0; extra == "test"
Description-Content-Type: text/markdown

<p align="center">
        <img src="./benchmarks/results/plots/superkmeans-logo.png" alt="SuperKMeans: A super-fast clustering library for high-dimensional vector embeddings">
</p>

<div align="center">
    <a href="https://arxiv.org/pdf/2603.20009"><img src="https://img.shields.io/badge/Paper%20I-SuperKMeans-red" alt="Paper" /></a>
    <a href="https://arxiv.org/pdf/2608.14648"><img src="https://img.shields.io/badge/Paper%20II-Quantized%20Clustering-red" alt="Paper" /></a>
</div>
<div align="center">
    <a href="https://pypi.org/project/superkmeans/"><img src="https://img.shields.io/pypi/pyversions/superkmeans.svg" alt="PyPI" /></a>
    <img src="https://github.com/cwida/SuperKMeans/actions/workflows/ci.yml/badge.svg?cacheSeconds=3600" alt="License" />
    <a href="https://github.com/cwida/SuperKMeans/blob/main/LICENSE"><img src="https://img.shields.io/github/license/cwida/SuperKMeans?cacheSeconds=3600" alt="License" /></a>
    <a href="https://github.com/cwida/SuperKMeans/stargazers"><img src="https://img.shields.io/github/stars/cwida/SuperKMeans" alt="GitHub stars" /></a>
</div>
<!-- <h3 align="center">
  A super-fast clustering library for high-dimensional vector embeddings
</h3> -->

<p align="center">
        <img src="./benchmarks/results/plots/github_1.png" height=260 alt="SuperKMeans vs FAISS and Scikit Learn" style="{max-height: 250px}">
</p>

## Why Super K-Means?
- **Faster clustering** of vector embeddings (Cohere, OpenAI, MXBAI, CLIP, MiniLM) than FAISS.
- Index 10M embeddings of 1024 dimensions [**in less than a minute**](https://www.lkuffo.com/superkmeans/) on a single CPU.
- Faster **without compromising clustering quality**.
- Support for [**quantized clustering**](#quantized-clustering) (8-bit Scalar Quantization, LVQ, and RaBitQ)
- Efficient on **CPUs** (ARM and x86) and **GPUs**.
- **Lightweight**: no external dependencies.
- **Portable**: Linux (x86, ARM), macOS, Windows (x86, ARM), Wasm.

## Our secret sauce
- Carefully interleaving GEMM routines and pruning kernels that **prune dimensions** efficiently
- In the benchmarks you see in the cover image, **all algorithms are clustering the same data**: No dimensionality reduction, no sampling, no early-termination.

## Usage
```py
from superkmeans import SuperKMeans

data = ... # Numpy 2D matrix
k = 1000
d = 768

kmeans = SuperKMeans(
    n_clusters=k,
    dimensionality=d
)

# Run the clustering
centroids = kmeans.train(data) # 2D array with centroids (k x d) 

# Get assignments
assignments = kmeans.assign(data)
```

Then, you can use the `centroids` to create an IVF index for Vector Search, for example, in FAISS.

<details>

<summary>Usage in C++</summary>

```c++
#include <vector>
#include <cstddef>
#include "superkmeans/superkmeans.h"
#include "superkmeans/hierarchical_superkmeans.h"

int main(int argc, char* argv[]) {
    std::vector<float> data; // Fill
    size_t n = 1000000;
    size_t k = 10000;
    size_t d = 768;

    auto kmeans = skmeans::SuperKMeans(k, d);

    // Or Hierarchical Super K-Means for extreme performance:
    // auto kmeans = skmeans::HierarchicalSuperKMeans(k, d);
    
    // Run the clustering
    std::vector<float> centroids = kmeans.Train(data.data(), n);
    
    // Assign points
    std::vector<uint32_t> assignments = kmeans.Assign(data.data(), centroids.data(), n, k);
}

```

</details>

Check our [examples](./examples/) for fully working examples in Python and C++.

## Clustering Variants
- **Lloyd's**: Classical k-means, accelerated by pruning. Check [our example](/examples/simple_clustering.cpp). 
- **Hierarchical**: A coarse k-means to create $\sqrt{k}$ meso clusters. Then, within each meso cluster, we run k-means again to create fine clusters that will serve as the final centroids. This creates more balanced clusters and scales to billions of points. Both phases are accelerated by pruning. Check [our example](/examples/hierarchical_clustering.cpp). 
- [**e-means**](https://e-means.github.io/): Reduces k-means iterations from 10 to 2, accelerated by pruning. Check [our example](/examples/emeans_clustering.py). 

## Quantized Clustering
SuperKMeans supports **clustering quantized vectors**, substantially accelerating clustering and barely affecting clustering quality. We support 8-bit scalar quantization, [LVQ](https://arxiv.org/pdf/2304.04759), and [RabitQ](https://github.com/VectorDB-NTU/RaBitQ-Library). You give us `float32` vectors and we handle the rest:

```py
kmeans = SuperKMeans(
    n_clusters=k,
    dimensionality=d,
    quantizer='rabitq' # or: sq8, lvq4
)

centroids = kmeans.train(data) # We return float32 centroids
assignments = kmeans.quantized_assign(data, centroids)
```

> [!TIP]
> **Does quantized clustering hurt the index quality?** Usually, NO: in most embedding datasets, quantized clustering will get you right where full precision will (+ faster indexing). Read [our research paper](https://arxiv.org/pdf/2608.14648) for the full analysis.



Check our fully working examples in [Python](./examples/quantized_clustering.py) or [C++](./examples/quantized_clustering.cpp). Note that all clustering variants support quantized clustering.

## Documentation
Check [our wiki](https://github.com/cwida/SuperKMeans/wiki/Documentation) for advanced usage and API reference.

## Installation

### Python
```sh
pip install superkmeans
```

### C++
With CMake `FetchContent`. There are no dependencies to install: CMake fetches and builds everything it needs.

```cmake
FetchContent_Declare(
    superkmeans
    GIT_REPOSITORY https://github.com/cwida/superkmeans
)
FetchContent_MakeAvailable(superkmeans)

target_link_libraries(myapp PRIVATE superkmeans)
```

<details>

<summary>Compiling Python Bindings from source</summary>

### Prerequisites
- Clang 17 or GCC 13
- CMake 3.26
- Python 3 (only for Python bindings)

```bash
git clone https://github.com/cwida/SuperKMeans.git
cd SuperKMeans
pip install .

# Run plug-and-play example
python ./examples/simple_clustering.py

# Set a value for n, d and k
python ./examples/simple_clustering.py 200000 1536 1000
```

</details> 

<details>

<summary>Compiling C++ library from source</summary>

### Prerequisites
- Clang 17 or GCC 13
- CMake 3.26

```bash
git clone https://github.com/cwida/SuperKMeans.git
cd SuperKMeans

# Set proper path to clang if needed
export CXX="/usr/bin/clang++-18" 

# Compile
cmake .
make examples

# Run plug-and-play example
cd examples
./simple_clustering.out

# Set a value for n, d and k
./simple_clustering.out 100000 1536 1000
```
</details> 


For a more comprehensive installation and compilation guide, check [INSTALL.md](./INSTALL.md).

## Roadmap
We are actively developing Super K-Means and accepting contributions! Check [CONTRIBUTING.md](./CONTRIBUTING.md).


## Benchmarking
To run our benchmark suite in C++, refer to [BENCHMARKING.md](./BENCHMARKING.md).

## Multithreading
SuperKMeans uses [ForkUnion](https://github.com/ashvardanian/ForkUnion) for threading (also when doing GEMM). You can plug in OpenMP or your own threading library by inheriting our [ParallelExecutor class](./include/superkmeans/executor.h) and passing it in our config.

## Memory Management
You can inject your own memory allocator by inheriting our [MemoryAllocator class](./include/superkmeans/allocator.h) and passing it in our config. Your memory allocator must be thread-safe and outlive the `SuperKMeans` object.

## BLAS
An efficient GEMM is key for performance. We use [Eigen](https://gitlab.com/libeigen/eigen) by default. In our benchmarks, Eigen is on par with OpenBLAS and BLIS, and 5–10% slower than Intel MKL end to end. You can also use your own BLAS (see [INSTALL.md](INSTALL.md#using-an-external-blas-optional)). However, it **must be single-threaded and thread-safe**.

## Adoption
SuperKMeans' ideas have been adopted in:
- [FAISS](https://github.com/facebookresearch/faiss/pull/5168)
- [Zilliz](https://github.com/zilliztech/knowhere/pull/1783)
- [Elastic](https://github.com/elastic/elasticsearch/pull/144599)
- [ParadeDB](https://github.com/paradedb/superkmeans-rs) |  [Rust implementation](https://github.com/paradedb/superkmeans-rs)
- [RaBitQ-Library](https://github.com/VectorDB-NTU/RaBitQ-Library/pull/120)
- [DuckIR](https://github.com/Noorts/PDXearch): DuckDB extension for vector search

## Research behind SuperKMeans

**[A Super Fast K-means for Indexing Vector Embeddings](https://arxiv.org/pdf/2603.20009)**   

```bibtex
@article{kuffo2026super,
  title={A Super Fast K-means for Indexing Vector Embeddings},
  author={Kuffo, Leonardo and Hepkema, Sven and Boncz, Peter},
  journal={arXiv preprint arXiv:2603.20009},
  year={2026}
}
```

**[Stop Indexing at Full Precision: Revisiting Clustering for Vector Embeddings](https://arxiv.org/pdf/2608.14648)**   

```bibtex
@article{kuffo2026superquantized,
  title={Stop Indexing at Full Precision: Revisiting Clustering for Vector Embeddings},
  author={Kuffo, Leonardo and Boncz, Peter},
  journal={VLDB 2026 Workshop: The 2nd Workshop on Vector Databases},
  year={2026}
}
```