Flash-KMeans: Fast and Memory-Efficient Exact K-Means

Shuo Yang, Haocheng Xi, Yilong Zhao, Muyang Li, Xiaoze Fan, Jintao Zhang, Han Cai, Yujun Lin, Xiuyu Li, Kurt Keutzer, Song Han, Chenfeng Xu, Ion Stoica · DOI

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.

Introduction

Historically, $k$-means has been viewed as a slow, "offline" preprocessing tool. However, modern AI architectures—like Large Language Models (LLMs) and video diffusion models—increasingly use $k$-means as an "online" primitive for tasks like dynamic token routing, KV cache compression, and vector quantization.

The authors identify that current GPU implementations (including industry standards like NVIDIA cuML and FAISS) are fundamentally bottlenecked by hardware constraints rather than algorithmic complexity. The primary issues are the "Memory Wall"—where standard implementations explicitly write a massive $N \times K$ distance matrix to the GPU's High Bandwidth Memory (HBM)—and "Atomic Contention" during the centroid update phase, where thousands of threads compete to update cluster centers using atomic operations.

Methodology: Flash-KMeans Innovations

FlashAssign: Fusing Distance and Argmin

Inspired by the philosophy of FlashAttention, FlashAssign fuses distance computation with an "online argmin" operator. Instead of materializing the giant $N \times K$ distance matrix in HBM, it tiles the data and keeps the running minimum distance in fast on-chip registers and SRAM. This reduces the IO complexity from $O(NK)$ to $O(Nd + Kd)$, effectively bypassing the memory bottleneck.

FlashAssign's pipeline and the resulting reduction in time proportion compared to standard PyTorch implementations.

Sort-Inverse Update: Eliminating Contention

To solve atomic contention, Flash-KMeans moves away from traditional "scatter-style" updates. Instead, it employs a "Sort-Inverse Update" strategy. By sorting the assignment vector by cluster ID, the algorithm transforms a chaotic, high-contention write process into a "gather-style" operation. This allows threads to perform localized, segment-level reductions, which are significantly more efficient on modern GPU hardware.

Comparison between scatter-style and sort-inverse updates, showing how sorting eliminates conflicts and accelerates execution by 6.3x.

Algorithm-System Co-design

Beyond kernel-level optimizations, Flash-KMeans integrates system-level designs:

  • Chunked Stream Overlap: For datasets exceeding GPU memory, a pipelined approach overlaps CPU-to-GPU data transfers with computation.
  • Cache-Aware Heuristics: A heuristic automatically selects optimal kernel configurations, reducing tuning time by 175$\times$ while maintaining near-peak performance.

Key Results and Scalability

Flash-KMeans achieves up to 17.9× end-to-end speedup over the best existing baselines. It outperforms industry-standard libraries like cuML by 33× and FAISS by over 200×. Notably, the system is mathematically exact; it achieves these gains through efficiency rather than approximation.

The system demonstrates massive scalability, successfully processing one billion points with a 10.5× speedup compared to the most robust out-of-core baseline. The FlashAssign kernel itself is up to 21.2× faster than standard assignment, while the Sort-Inverse Update provides a 6.3× speedup over traditional methods.

Conclusion

Flash-KMeans demonstrates that "compute is cheap, but IO is expensive." By restructuring a classical algorithm to respect modern hardware constraints—specifically by minimizing HBM access and atomic contention—it transforms $k$-means into a high-performance online primitive suitable for the most demanding AI workloads.

Summary updated

BibTeX
@inproceedings{yang2026flash,
  author = {Shuo Yang and Haocheng Xi and Yilong Zhao and Muyang Li and Xiaoze Fan and Jintao Zhang and Han Cai and Yujun Lin and Xiuyu Li and Kurt Keutzer and Song Han and Chenfeng Xu and Ion Stoica},
  title = {{Flash-KMeans: Fast and Memory-Efficient Exact K-Means}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=WVswOFOoe5},
}
Figure