FAISS
What it is
FAISS (Facebook AI Similarity Search) is an open-source library published by Johnson et al. (2017) implementing optimized ANN algorithms for similarity search on CPUs and GPUs. It supports composite indexing (IVF, PQ, HNSW combinations), flat brute-force search, and GPU acceleration, making it the de facto standard for research and production dense retrieval systems.
[illustrate: FAISS index types (flat, IVF, PQ, HNSW); memory usage vs. speed tradeoff curve for different configurations]
How it works
FAISS provides modular index components that can be composed:
-
Index types:
IndexFlatL2/IndexFlatIP: exhaustive search (baseline)IndexIVFFlat: IVF with flat storageIndexIVFPQ: IVF + product quantization (memory-efficient)IndexHNSW: HNSW graph indexIndexPreTransform: preprocessing (rotation, dimensionality reduction)
-
GPU support:
GpuIndexFlatL2,GpuIndexIVFFlat: GPU-accelerated indexes- Batch processing for throughput
-
Python API:
index = faiss.IndexIVFPQ(d, nlist, m, nbits) index.train(vectors) index.add(vectors) distances, indices = index.search(queries, k=10)
Example
import faiss
import numpy as np
# Vectors: 1M docs × 768-dim embeddings
vectors = np.random.random((1000000, 768)).astype('float32')
queries = np.random.random((100, 768)).astype('float32')
# Create IVF+PQ index
index = faiss.IndexIVFPQ(768, 1024, 8, 8)
index.train(vectors)
index.add(vectors)
# Search: find top-10 nearest neighbours
distances, indices = index.search(queries, k=10)
# indices: [100, 10] → top-10 doc IDs per query
Variants and history
FAISS emerged in 2017 as Facebook’s internal search library. It became widely adopted in academia and industry due to comprehensive features, GPU support, and active maintenance. Modern competitors include Hnswlib (HNSW focus), Weaviate (full vector DB), and RAPIDS (GPU-accelerated). FAISS remains the reference implementation for ANN research.
When to use it
Use FAISS when:
- Implementing dense retrieval research or prototypes
- You need flexibility in index composition
- GPU acceleration is available
- You want battle-tested, production-ready code
- Batch processing and throughput matter
FAISS is a library, not a full database. For production systems with persistence, sharding, and multi-tenancy, consider vector databases built on or inspired by FAISS.