Inferensys

Glossary

ScaNN

ScaNN (Scalable Nearest Neighbors) is a quantization-based approximate nearest neighbor search algorithm developed by Google that optimizes the recall-speed tradeoff through anisotropic vector quantization, prioritizing the preservation of inner product scores critical for Maximum Inner Product Search.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
SCALABLE NEAREST NEIGHBORS

What is ScaNN?

ScaNN is a vector similarity search library developed by Google Research that optimizes the trade-off between search accuracy and speed for Maximum Inner Product Search (MIPS).

ScaNN (Scalable Nearest Neighbors) is a quantization-based approximate nearest neighbor (ANN) algorithm that introduces anisotropic vector quantization to prioritize the preservation of inner product scores critical for Maximum Inner Product Search (MIPS). Unlike isotropic quantizers that minimize Euclidean reconstruction error uniformly, ScaNN's loss function is specifically designed to weight quantization error by how much it impacts the dot product, yielding superior recall for recommendation and attention models.

The algorithm compresses database vectors by learning a codebook where the quantization granularity is higher along dimensions with greater variance, directly optimizing the recall@k versus throughput tradeoff. ScaNN achieves state-of-the-art performance by combining this anisotropic quantization with a fast, SIMD-optimized scoring kernel, making it a leading choice for production systems requiring low-latency, high-recall retrieval over billion-scale embedding datasets.

SCALABLE NEAREST NEIGHBORS

Key Features of ScaNN

ScaNN (Scalable Nearest Neighbors) is Google's vector similarity search library that optimizes the recall-speed tradeoff through anisotropic vector quantization, specifically designed to preserve inner product scores critical for Maximum Inner Product Search (MIPS).

01

Anisotropic Vector Quantization

Unlike traditional Product Quantization (PQ) which minimizes reconstruction error uniformly, ScaNN's anisotropic quantization explicitly optimizes for the preservation of inner product scores. It learns a quantization codebook that allocates more precision to vector components with higher variance, recognizing that not all dimensions contribute equally to the dot product. This is critical for MIPS workloads where the magnitude of the query-database interaction matters. The technique reduces quantization error along dimensions that most impact the inner product, yielding higher recall at equivalent compression ratios compared to isotropic methods.

2x
Recall improvement vs. PQ at same speed
02

Score-Aware Quantization Loss

ScaNN introduces a novel score-aware quantization loss function that directly penalizes errors in the approximated inner product rather than Euclidean distance. The loss is formulated as:

  • Minimizing the difference between the true dot product and the quantized dot product
  • Weighting the loss by the query distribution to focus on high-probability query regions
  • Jointly optimizing the coarse and fine quantization codebooks end-to-end This design ensures that the compressed vectors maintain the correct ranking order when scored against typical queries, directly optimizing for the Recall@K metric used in production retrieval systems.
03

Two-Level Tree-Quantization Index

ScaNN combines an Inverted File Index (IVF) for coarse partitioning with anisotropic quantization for fine-grained compression in a two-level structure:

  • Level 1 (Coarse Quantizer): Partitions the vector space into Voronoi cells using K-means clustering, assigning each database vector to its nearest centroid
  • Level 2 (Fine Quantizer): Applies anisotropic quantization to the residual vectors (the difference between the original vector and its assigned coarse centroid)
  • Asymmetric Distance Computation (ADC): Queries remain in full precision while only database vectors are compressed, yielding higher accuracy than symmetric approaches This architecture enables sub-linear search complexity while maintaining high fidelity for inner product ranking.
04

MIPS-Optimized Search Pruning

Standard ANN algorithms optimized for Euclidean distance or cosine similarity perform poorly on Maximum Inner Product Search (MIPS) because inner product is not a metric—it violates the triangle inequality. ScaNN addresses this with:

  • Query-dependent pruning: Dynamically adjusts search radii based on the query vector's magnitude, since high-magnitude queries require exploring more partitions
  • Inner product upper bound estimation: Computes tight theoretical upper bounds on the maximum possible inner product within each Voronoi cell to safely prune unpromising partitions
  • Adaptive candidate selection: Varies the number of coarse partitions searched based on the query's norm, balancing speed and recall per-query This makes ScaNN particularly effective for attention mechanisms and matrix factorization models where MIPS is the core operation.
05

SIMD-Accelerated Distance Computations

ScaNN leverages SIMD (Single Instruction, Multiple Data) instructions extensively to parallelize the inner product computations that dominate search latency:

  • Uses AVX2 and AVX-512 instruction sets on x86 processors for 8-way or 16-way parallel floating-point operations
  • Implements cache-friendly memory layouts that store quantized codes contiguously to maximize cache line utilization
  • Batches distance computations across multiple query vectors simultaneously for amortized throughput gains
  • Achieves sub-millisecond latency per query on billion-scale datasets when deployed on optimized hardware These low-level optimizations are critical for production deployments requiring high queries-per-second throughput.
< 1 ms
Query latency on billion-scale
06

Dynamic Re-Ranking Pipeline Integration

ScaNN is designed to serve as the fast candidate generation stage in a two-stage retrieval pipeline:

  • Stage 1: ScaNN retrieves a candidate set (e.g., top-1000) using quantized vectors with high recall
  • Stage 2: A more expensive re-ranking step computes exact inner products on the full-precision vectors for the candidate set only
  • The anisotropic quantization ensures that the true top-K results are included in the candidate set with high probability, minimizing recall loss
  • Configurable tradeoff: Adjust the number of coarse partitions searched and the candidate set size to balance latency against Recall@K targets This architecture is the standard pattern for production semantic search systems requiring both speed and accuracy.
SCANN DEEP DIVE

Frequently Asked Questions

Technical answers to common questions about Google's ScaNN algorithm, its anisotropic quantization mechanism, and its performance characteristics in production vector search systems.

ScaNN (Scalable Nearest Neighbors) is a quantization-based approximate nearest neighbor search algorithm developed by Google Research that optimizes the recall-speed tradeoff through anisotropic vector quantization. Unlike traditional product quantization which minimizes reconstruction error uniformly, ScaNN prioritizes the preservation of inner product scores critical for Maximum Inner Product Search (MIPS). It works by learning a quantization codebook that allocates more precision to vector components with higher variance—those dimensions that disproportionately influence the dot product. The algorithm introduces a novel anisotropic quantization loss function that weights the quantization error by the query distribution's covariance structure, ensuring that the compressed vectors maintain accurate inner product rankings even at aggressive compression ratios. This makes ScaNN particularly effective for recommendation systems and attention mechanisms where the magnitude of the dot product directly determines relevance scores.

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.