[System Redesign] Flash-KMeans: Breaking the Memory Wall in Clustering

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

Summary
Problem
Method
Results
Takeaways
Abstract

Flash-KMeans is an IO-aware and contention-free GPU implementation of the exact K-Means algorithm. It introduces FlashAssign and Sort-Inverse Update to eliminate memory bottlenecks, achieving up to 17.9x speedup over current SOTA and outperforming industry standards like FAISS by over 200x.

TL;DR

K-Means is often dismissed as a "mature" offline algorithm, but it is increasingly used online for LLM routing and vector quantization. Flash-KMeans revisits this classic through the lens of modern AI system design. By treating clustering as an IO-bound problem rather than a compute-bound one, the authors achieve massive speedups (up to 17.9x over optimized baselines) without changing a single line of the original Lloyds algorithm's math.

Background: Why Your K-Means is Slow

In the era of Tensor Cores, FLOPs (arithmetic operations) are cheap, but data movement (IO) is expensive. Standard implementations—including those in FAISS and cuML—fall into two traps:

  1. The Materialization Trap: They compute an distance matrix and save it to High Bandwidth Memory (HBM) just to find the minimum. For large and , this traffic dominates everything.
  2. The Atomic Contention Trap: Updating centroids requires "scattering" data. When many points belong to the same cluster, multiple threads fight to update the same memory address, causing hardware-level serialization.

Methodology: The Flash-KMeans Innovations

The core contribution of Flash-KMeans lies in two hardware-aware kernels that respect the GPU memory hierarchy.

1. FlashAssign: Materialization-Free Assignment

Inspired by FlashAttention, FlashAssign fuses the distance computation with the reduction step. Using an Online Argmin algorithm, the kernel streams data points and centroids into on-chip SRAM, computes local distances, and updates the "running best" cluster index in registers.

  • Result: The massive distance matrix is never written to HBM. IO complexity drops from to .

FlashAssign Architecture

2. Sort-Inverse Update: Taming Atomic Writes

Instead of letting every thread perform a global atomic add (which is slow due to contention), Flash-KMeans performs an argsort on the assignments. This "inverse mapping" groups all points belonging to the same cluster together.

  • Insight: In this sorted order, threads can aggregate point data into a single local buffer and perform one atomic merge per segment. This transforms chaotic "scatter" writes into regular, high-bandwidth "segmented" reductions.

Sort-Inverse Update Process

Performance: SOTA is Only the Beginning

The evaluations on NVIDIA H200 GPUs show that Flash-KMeans is significantly faster across all workload scales:

  • Versus Baselines: Up to 17.9x faster than fast_pytorch_kmeans.
  • Versus Industry Standards: 33x faster than NVIDIA cuML and 200x+ faster than FAISS.
  • Scalability: While standard PyTorch crashes with "Out of Memory" on large , Flash-KMeans handles up to 1 billion points effortlessly by overlapping CPU-to-GPU data transfers with computation.

Performance Comparison

Deep Insight: Moving from Offline to Online

The significance of Flash-KMeans isn't just the raw speed—it's the usability.

  • No Auto-Tuning: They developed a "Cache-Aware Compile Heuristic" that picks the best kernel configuration based on L1/L2 cache sizes. This cuts the "time-to-first-run" from minutes to seconds.
  • Online Primative: By making K-Means fast enough to run in milliseconds, it can now be embedded directly into the forward pass of LLMs for tasks like Sparse Attention Routing or Dynamic KV-Cache Quantization.

Conclusion

Flash-KMeans proves that even the most "solved" algorithms in computer science can benefit from a system-level rethink. By acknowledging that modern GPUs are memory-constrained, not compute-constrained, the authors have turned a 40-year-old algorithm into a high-performance engine for the next generation of AI.

Key Takeaway: Don't optimize math; optimize dataflow.

Find Similar Papers

Try Our Examples

  • Search for recent papers applying IO-aware optimization techniques, similar to FlashAttention, to other classical machine learning algorithms like GMM or KNN.
  • Which original paper introduced the "online argmin" or streaming reduction concept for GPU kernels that FlashAssign builds upon?
  • Investigate how Flash-KMeans can be integrated into sparse attention routing or KV-cache compression pipelines for Long Context LLMs.
Contents
[System Redesign] Flash-KMeans: Breaking the Memory Wall in Clustering
1. TL;DR
2. Background: Why Your K-Means is Slow
3. Methodology: The Flash-KMeans Innovations
3.1. 1. FlashAssign: Materialization-Free Assignment
3.2. 2. Sort-Inverse Update: Taming Atomic Writes
4. Performance: SOTA is Only the Beginning
5. Deep Insight: Moving from Offline to Online
6. Conclusion