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

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

TL;DR

This paper introduces TPU-KNN, a novel algorithm for K-Nearest Neighbor (KNN) search that achieves peak performance on Google's Tensor Processing Units (TPUs). By developing a refined performance model that accounts for both computational and memory bottlenecks, they've designed an efficient KNN search that significantly outperforms existing methods on TPUs.

What Problem Does This Paper Address?

The K-Nearest Neighbor (KNN) search problem is fundamental to many applications like image retrieval and recommendation systems. The challenge lies in finding the K closest data points to a query efficiently, especially when dealing with massive datasets. While accelerators like TPUs offer immense computational power (measured in TFLOPS), effectively utilizing this power for KNN search is difficult due to data locality, memory bandwidth, and parallelism complexities. Existing methods often struggle to achieve high utilization of these accelerators, leading to suboptimal performance.

Methods

The core of this work is a refined roofline performance model. This model extends the classic roofline model by explicitly considering the throughput differences between floating-point operations (FLOPs) and coefficient-wise operations (COPs), which are crucial for modern accelerators with specialized units like tensor cores.

Based on this model, the authors designed an approximate KNN algorithm that prioritizes computational efficiency. Instead of complex indexing structures that can be data-dependent and hard to tune, their approach focuses on optimizing the execution kernels. The algorithm consists of two main kernels:

  1. PartialReduce kernel: This kernel computes distances and partially aggregates results, aiming for high arithmetic intensity by minimizing memory writes.
  2. ExactRescoring kernel (optional): This kernel refines the top-K results. Its cost is amortized when the number of bins (L) is proportional to K.

A key aspect is the analytical guarantee of recall in expectation, which is derived from a balls-and-bins analogy, making the algorithm robust and predictable.

Datasets

The paper evaluates its approach on two standard ANN benchmarks:

  • Glove1.2M: Uses cosine distance.
  • Sift1M: Uses Euclidean distance.

These datasets are commonly used in the field and allow for direct comparison with existing methods. The authors also mention using the ANN-benchmarks framework for evaluation.

Key Insights and Ideas

  • The importance of instruction throughput: The paper highlights that simply maximizing FLOPs isn't enough. The throughput of coefficient-wise operations (COPs) can become a significant bottleneck, especially for algorithms like Euclidean distance KNN that rely heavily on them.
  • Roofline model for algorithm design: Using a performance model not just for profiling but for designing algorithms is crucial for achieving hardware-level efficiency. The refined roofline model helps identify and avoid performance pitfalls.
  • Approximate KNN with guarantees: Achieving high performance doesn't necessarily mean sacrificing accuracy. The proposed method offers an analytical guarantee on recall, making it predictable and reliable.
  • TPU-specific optimizations: The algorithm is tailored to leverage the strengths of TPUs, particularly their matrix multiplication capabilities, while mitigating potential bottlenecks.

Key Results

  • Peak FLOP/s on TPUs: The proposed TPU-KNN algorithm achieves peak FLOP/s on Google TPUs (V3 and V4) for both Maximum Inner Product Search (MIPS) and Euclidean distance KNN.
  • Significant speedup: For MIPS, the implementation achieved a 9.6x speedup over a naive baseline on a TPU V4.
  • State-of-the-art speed-recall trade-offs: On the Glove and Sift benchmarks, TPU-KNN demonstrates superior performance compared to competitive baselines (like Faiss) across various recall levels.
  • Near-optimal performance: The experiments show that the implementation aligns closely with the theoretical performance limits predicted by the refined roofline model.

Limitations and Future Work

  • Single-chip focus: The current work focuses on single-chip accelerator performance. Extending to multi-chip or distributed settings is a natural next step.
  • Dense vectors: The algorithm is designed for dense vectors. Sparse vector search, which has different memory access patterns, would require a different approach.
  • Integration with other techniques: The authors suggest that their method could be combined with existing techniques like dimension reduction, quantization, and tree search to further enhance performance.
  • Heterogeneous architectures: Exploring performance on architectures with off-HBM storage (like host-RAM or SSDs) is a promising research direction.
  • Further roofline model extensions: The paper acknowledges that roofline models are an active research area, with extensions for GPU warps, time-based analysis, and energy efficiency being relevant for future work.

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