Inferensys

Glossary

ScaNN (Scalable Nearest Neighbors)

ScaNN (Scalable Nearest Neighbors) is an open-source library from Google Research for efficient approximate nearest neighbor search, specializing in Maximum Inner Product Search (MIPS) using anisotropic vector quantization to optimize the accuracy-latency trade-off.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
RETRIEVAL LATENCY OPTIMIZATION

What is ScaNN (Scalable Nearest Neighbors)?

ScaNN is a high-performance vector similarity search library developed by Google Research, designed to efficiently solve the Maximum Inner Product Search (MIPS) problem common in recommendation and retrieval systems.

ScaNN (Scalable Nearest Neighbors) is an open-source library for approximate nearest neighbor (ANN) search that uses anisotropic vector quantization to optimize the accuracy-latency trade-off, particularly for maximum inner product search (MIPS). It partitions the vector space in a way that better preserves the geometric relationships crucial for inner product similarity, unlike isotropic methods designed for Euclidean distance. This makes it exceptionally effective for retrieving items with high dot-product similarity to a query, a core operation in systems using dense embeddings from models like BERT or Sentence Transformers.

The library's performance stems from its tree-AH (asymmetric hashing) and score-AH quantization techniques, which allow for faster distance computations with minimal loss in recall. By focusing on the anisotropic nature of real-world embedding distributions, ScaNN achieves higher accuracy at comparable speeds to libraries like Faiss for MIPS tasks. It is a key tool in retrieval-augmented generation (RAG) pipelines and recommendation engines where low-latency, high-recall retrieval from billion-scale vector databases is required.

SCALABLE NEAREST NEIGHBORS

Key Features of ScaNN

ScaNN (Scalable Nearest Neighbors) is a library from Google Research designed for efficient high-dimensional vector similarity search. Its core innovations focus on optimizing the accuracy-latency trade-off, particularly for Maximum Inner Product Search (MIPS).

01

Anisotropic Vector Quantization

ScaNN's defining technique is Anisotropic Vector Quantization (AVQ), a novel loss function for training quantization codebooks. Unlike standard quantization that minimizes mean squared error, AVQ directly optimizes for the inner product between the query and the quantized vector. This is critical because MIPS aims to maximize the inner product, not minimize Euclidean distance. By aligning the quantization error with the search objective, ScaNN achieves higher accuracy for a given compression rate and search speed.

  • Standard Quantization: Minimizes ||x - q(x)||² (reconstruction error).
  • Anisotropic Quantization (ScaNN): Minimizes 1 - (x · q(x)) / (||x|| * ||q(x)||) (inner product loss).
02

Maximum Inner Product Search (MIPS) Optimization

ScaNN is specifically engineered for the Maximum Inner Product Search (MIPS) problem, which is prevalent in recommendation systems and retrieval with certain dense embedding models (where similarity is measured by dot product). It provides a more accurate and efficient solution than converting MIPS to nearest neighbor search via Euclidean distance, which requires vector normalization and can distort the space.

  • Native MIPS Handling: Operates directly on inner product space without costly pre-processing transformations.
  • Partitioning Strategy: Employs a tree-AVQ partitioning scheme that recursively splits the dataset using anisotropic quantizers, creating an index structure optimized for inner product traversal.
03

Optimal Space Partitioning (Tree-AVQ)

For dataset partitioning, ScaNN uses a tree-AVQ structure. This involves building a tree where each node uses an anisotropic quantizer to split its data points. The quantization cells (partitions) are not isotropic spheres but are shaped to maximize the expected maximum inner product of points within them. This leads to more intelligent pruning during search.

  • Recursive Anisotropic Splitting: Each partition is created using AVQ, keeping the search objective in mind.
  • Efficient Traversal: The tree structure allows the search algorithm to quickly prune entire branches that are unlikely to contain high inner-product results, reducing the number of distance computations.
04

Highly-Tunable Accuracy-Speed Trade-off

ScaNN provides granular control over the recall-latency trade-off through key parameters, allowing engineers to dial in performance for specific production requirements.

  • Leaves to Search (leaves_to_search): Analogous to nprobe in IVF methods. Controls how many partitions (tree leaves) are examined per query. Increasing this improves recall at the cost of latency.
  • AVQ Re-ranks: The number of candidates re-scored with a more accurate distance computation after the initial fast quantization-based search.
  • Pre-Trained Configuration: Ships with pre-optimized configurations for common scenarios (e.g., high recall, low latency) on standard benchmarks like Glove and Deep1B.
05

Performance & Scalability

ScaNN is designed for billion-scale datasets. Its combination of AVQ and tree partitioning achieves state-of-the-art performance on public benchmarks, often outperforming other libraries like Faiss (IVFPQ) and HNSW on MIPS tasks, especially at high recall rates.

  • Benchmark Results: On the Glove-1.2M benchmark, ScaNN achieves ~10x faster search than Faiss-IVFPQ for the same recall.
  • Memory Efficiency: Product Quantization is used in conjunction with AVQ, providing high compression rates (e.g., 32-byte codes for 128-dimensional vectors).
  • GPU Compatibility: While primarily CPU-optimized, it supports execution on GPU hardware for accelerated batch inference.
FEATURE COMPARISON

ScaNN vs. Other ANN Libraries

A technical comparison of ScaNN against other prominent approximate nearest neighbor (ANN) libraries, focusing on core algorithms, optimization targets, and deployment characteristics relevant to retrieval latency optimization in RAG systems.

Feature / MetricScaNN (Google)Faiss (Meta)HNSW-based (e.g., hnswlib)

Primary Algorithm

Anisotropic Vector Quantization (AVQ)

Inverted File Index (IVF), Product Quantization (PQ)

Hierarchical Navigable Small World (HNSW) Graph

Optimization Target

Maximum Inner Product Search (MIPS)

L2 Distance / Cosine Similarity

L2 Distance / Cosine Similarity

Quantization Approach

Learned anisotropic partitioning

Structured (PQ) or scalar quantization

Typically none (full-precision vectors)

Memory Efficiency (for 1M 768-dim vectors)

High (compressed via AVQ)

Very High (compressed via PQ)

Low (stores full-precision vectors)

Index Build Time

Moderate to High (requires training)

Low to Moderate (IVF) / High (PQ training)

High (graph construction is computationally intensive)

Query Latency (P50) at 95% Recall

< 1 ms (on CPU, optimized for MIPS)

1-5 ms (CPU, depends on nprobe/PQ parameters)

0.5-2 ms (CPU, depends on efSearch)

GPU Acceleration

Limited (experimental)

Extensive (native GPU kernels for IVF, PQ)

None (primarily CPU-based)

Dynamic Updates (Incremental Add)

No (requires partial retrain)

Yes (IVFADD, but degrades performance)

Yes (supports adding vectors to graph)

Native Filtering Support

No

Yes (via IDSelector and range search)

No (requires pre/post-filtering wrapper)

Primary Use Case in RAG

Recommendation-style retrieval, dense retrieval with dot-product similarity

Generic semantic search, high-scale clustering

Low-latency, high-recall search for moderate-scale datasets

SCANN

Frequently Asked Questions

ScaNN (Scalable Nearest Neighbors) is a library from Google Research for efficient high-dimensional vector similarity search, crucial for low-latency retrieval in recommendation systems and RAG architectures.

ScaNN (Scalable Nearest Neighbors) is an open-source library from Google Research for efficient Approximate Nearest Neighbor (ANN) search, specifically optimized for Maximum Inner Product Search (MIPS). It works by employing anisotropic vector quantization, a technique that quantizes vectors (compresses them into discrete codes) in a way that is directionally sensitive to the query distribution. Unlike isotropic methods that treat all vector directions equally, ScaNN's quantization learns to preserve more accuracy in the directions most relevant for the inner product, leading to a superior accuracy-latency trade-off. The library provides a simple API to build an index from a dataset of embeddings and then query it to find the most similar items with high recall and low millisecond latency.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.