Paper 2603.09229
Flash-KMeans: Fast and Memory-Efficient Exact K-Means
- Published
- Mar 2026
- Research lab
- Independent
- Citations
- 5
- GitHub
- 704 stars
01 In brief
Summary
Flash-KMeans is a GPU implementation of exact k-means that addresses performance bottlenecks in modern AI workloads.
The paper identifies two main kernel-level issues: the assignment stage suffers from an IO bottleneck due to materializing the N×K distance matrix in HBM, and the centroid update stage suffers from atomic write contention due to scatter-style token aggregations.
Flash-KMeans introduces FlashAssign, which fuses distance computation with an online argmin to avoid materializing the distance matrix, and Sort-Inverse Update, which sorts assignments by cluster ID to transform atomic scatters into segment-level reductions.
It also includes system-level co-designs: chunked stream overlap for out-of-core execution and a cache-aware compile heuristic for fast configuration.
On NVIDIA H200 GPUs, Flash-KMeans achieves up to 17.9× end-to-end speedup over baselines, 33× over cuML, and over 200× over FAISS.
Kernel-level speedups reach 21.2× for assignment and 6.3× for update.
It scales to one billion points with a 10.5× speedup and reduces configuration tuning overhead by 175× with negligible performance loss.
02 From the paper
Abstract
$k$-means has historically been positioned primarily as an offline processing primitive, typically used for dataset organization or embedding preprocessing rather than as a first-class component in online systems. In this work, we revisit this classical algorithm under the lens of modern AI system design and enable $k$-means as an online primitive. We point out that existing GPU implementations of $k$-means remain fundamentally bottlenecked by low-level system constraints rather than theoretical algorithmic complexity. Specifically, the assignment stage suffers from a severe IO bottleneck due to the massive explicit materialization of the $N \times K$ distance matrix in High Bandwidth Memory (HBM). Simultaneously, the centroid update stage is heavily penalized by hardware-level atomic write contention caused by irregular, scatter-style token aggregations. To bridge this performance gap, we propose flash-kmeans, an IO-aware and contention-free $k$-means implementation for modern GPU workloads. Flash-kmeans introduces two core kernel-level innovations: (1) FlashAssign, which fuses distance computation with an online argmin to completely bypass intermediate memory materialization; (2) sort-inverse update, which explicitly constructs an inverse mapping to transform high-contention atomic scatters into high-bandwidth, segment-level localized reductions. Furthermore, we integrate algorithm-system co-designs, including chunked-stream overlap and cache-aware compile heuristics, to ensure practical deployability. Extensive evaluations on NVIDIA H200 GPUs demonstrate that flash-kmeans achieves up to 17.9$\times$ end-to-end speedup over best baselines, while outperforming industry-standard libraries like cuML and FAISS by 33$\times$ and over 200$\times$, respectively. Our code is open-sourced at https://github.com/svg-project/flash-kmeans.