TPU-KNN: K Nearest Neighbor Search at Peak FLOP/s

Felix Chern, Blake Hechtman, Andy Davis, Ruiqi Guo, David Majnemer, Sanjiv Kumar

Overview

TPU-KNN introduces a novel nearest neighbor search algorithm designed to hit the theoretical "peak FLOP/s" of Google’s Tensor Processing Units (TPUs). By ditching complex indexing in favor of a hardware-optimized "PartialReduce" approach, the authors outperform state-of-the-art GPU libraries like Faiss in high-recall scenarios while providing analytical guarantees on accuracy.

The Problem: The Accelerator Performance Gap

The $K$-nearest neighbor ($K$-NN) search is a cornerstone of modern AI, used in everything from image retrieval to recommendation systems and "memory" for Large Language Models. However, there is a massive gap between the theoretical power of hardware accelerators (TPUs and GPUs) and the actual performance of $K$-NN algorithms.

Traditional Approximate Nearest Neighbor (ANN) methods rely on complex data structures like trees, graphs, or inverted files to prune the search space. While these methods reduce the number of calculations, they create two new problems for accelerators:

  1. Memory Bandwidth Wall: They involve "scattered" memory access patterns that prevent the chip from reading data at full speed.
  2. Instruction Bottleneck: The logic required to navigate these structures (comparisons, branches) is much slower on a TPU/GPU than the raw matrix math used for calculating distances.
Performance of various operations across different hardware architectures, highlighting the gap between theoretical peak and actual performance for traditional K-NN operations.

The authors set out to design an algorithm that is "compute-bound" rather than "memory-bound," essentially trying to make $K$-NN as fast as a standard matrix multiplication.

Methods: Rethinking the Bottleneck

The paper introduces two major contributions: a refined performance model to understand accelerator limits, and a new algorithm designed to exploit them.

1. The Instruction-Aware Roofline Model

Standard "roofline models" compare a chip's peak math performance against its memory bandwidth. The authors extend this by adding COPs (Coefficient-wise Operations). They demonstrate that on modern accelerators, non-matrix operations (like if statements or comparisons) are a hidden bottleneck. For example, on a TPU V4, you can only afford about 4 "if" statements for every dot product before the hardware slows down.

Comparison of the traditional memory bandwidth roofline model versus the authors' novel instruction bandwidth roofline model for TPU V3 and V4.

2. The PartialReduce Algorithm

Instead of trying to skip calculations, the authors embrace "brute force" but optimize the aggregation.

  • The Kernel: They compute all distances between queries and the database using high-speed matrix units.
  • Partial Reduction: To avoid the "memory wall" of writing millions of distances back to main memory, they perform a "partial reduction" inside the hardware's local registers. They divide the database into $L$ bins and only keep the single best result from each bin.
  • Final Sort: A small, final "ExactRescoring" step sorts these $L$ candidates to find the true top-$K$.

Key Insights

  • Brute Force is the New Fast: On modern hardware, doing more math in a predictable, linear way is often faster than doing less math with complex, branching logic.
  • The Birthday Problem Connection: The authors prove the accuracy of their "PartialReduce" method using the logic of the "Birthday Problem." They show that if you throw $K$ "balls" (the true top results) into $L$ "bins," the probability of them not colliding (and thus being preserved by the top-1-per-bin rule) is high and mathematically predictable.
  • Aggregation is the Real Bottleneck: The "aha moment" is realizing that calculating distances isn't what slows down $K$-NN on accelerators; it's the sorting and selection of the results. By moving selection into the compute kernel, they bypass the memory bottleneck.

Results and Performance

The authors evaluated their approach on standard ANN benchmarks (Glove, Sift, NYTimes, Lastfm, and Yandex Deep for scalability up to 10 million vectors).

  • Peak Performance: The algorithm reaches near-peak theoretical TFLOPS on TPU V3 and V4, effectively "saturating" the hardware.
  • Superiority over GPUs: In high-recall regimes (where accuracy is paramount), TPU-KNN significantly outperforms the industry-standard Faiss library running on high-end Nvidia V100 and A100 GPUs.
  • Linear Scalability: The performance scales perfectly with hardware. Doubling the number of TPU chips exactly doubles the Queries Per Second (QPS).
  • No Tuning Required: Unlike graph-based or tree-based methods that require complex parameter tuning, TPU-KNN only requires choosing the number of bins ($L$), which is directly tied to the desired recall level.

Limitations and Future Work

While highly effective, the approach has constraints. Because this is a "brute force" style approach, the entire dataset (or a shard of it) must fit into the accelerator's HBM (High Bandwidth Memory). This makes it less suitable for trillion-scale datasets without massive hardware clusters. Additionally, at very low recall targets, traditional compressed-domain methods (like IVF-PQ) may still be faster because they skip the vast majority of the data. Finally, achieving peak performance requires low-level access to compiler internals (like XLA) and assembly-level scheduling, which may be difficult to replicate on other hardware platforms.

Summary updated

BibTeX
@inproceedings{chern2022tpu,
  author = {Felix Chern and Blake Hechtman and Andy Davis and Ruiqi Guo and David Majnemer and Sanjiv Kumar},
  title = {{TPU-KNN: K Nearest Neighbor Search at Peak FLOP/s}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=OoNmOfYVhEU},
}
Figure