FAISS

FAISS (Facebook AI Similarity Search) is the most widely-used open-source library for Approximate Nearest Neighbour (ANN) search and similarity search on dense vectors, developed and maintained by Meta (Facebook). The library was open-sourced in 2017 with the Johnson, Douze, Jégou paper “Billion-scale similarity search with GPUs”; the 2024 update paper is “The FAISS library”. FAISS is not a single ANN algorithm — it is a library implementing many index types (IVF, PQ, HNSW, LSH, flat exact search, and many composites) with consistent APIs, optimized C++ implementations, and GPU acceleration. The library is the de-facto standard for vector similarity search in production at Meta, Pinterest, Spotify, and many other companies, and it is the underlying engine of several commercial vector databases. In recommender systems, FAISS is the canonical choice for indexing item embeddings produced by two-tower retrieval models and serving them at billion-item scale.

1. What FAISS Provides

FAISS provides:

  • Index types — implementations of all major ANN algorithms (Flat exact, IVF, PQ, IVFPQ, HNSW, LSH, NSG, scalar quantization, OPQ, and many composites).
  • Index factory — a string-based DSL for composing index types (e.g., "OPQ16_64,IVF1024,PQ16" describes an OPQ-rotated, IVF-partitioned, PQ-coded index).
  • GPU acceleration — most indexes have GPU implementations for both indexing and querying.
  • Distributed training — IVF clustering can be trained on many machines.
  • Serialization — indexes can be saved to disk and loaded.
  • Python and C++ bindings — both APIs are first-class.

The breadth of index types is FAISS’s main value. Different applications have different recall-latency-memory profiles, and FAISS’s index factory makes it straightforward to test multiple options.

2. Common Index Configurations

A few configurations cover most use cases.

Flat — exact search via brute-force distance computation. The reference for measuring recall against. Memory O(n · d · 4) bytes. Search O(n · d) per query. Use for small datasets (under 10⁶ vectors) or when exact search is required.

IVF{K_IVF},Flat — IVF coarse partitioning with full-precision vectors in inverted lists. Memory same as Flat. Search O((n/K_IVF) · n_probe · d). Use for moderate datasets where memory is not constrained but speed is.

IVF{K_IVF},PQ{M} — IVF coarse partitioning with Product Quantization codes. Memory O(n · M) bytes. Search O((n/K_IVF) · n_probe · M). The workhorse for billion-scale indexes.

HNSW{M} — graph-based HNSW index. Memory O(n · (d · 4 + M · 4)) bytes (graph edges add overhead). Search O(log n · ef_search). Use for moderate datasets where high recall is needed.

OPQ,IVF,PQ — adds an optimized rotation (OPQ — Optimized PQ) before IVF and PQ to make subspaces more independent. Modest accuracy gain over IVF-PQ; widely used in production.

The FAISS wiki provides detailed guidance on choosing among these.

3. The Index Factory

FAISS’s index factory allows composing indexes via string descriptions:

import faiss
 
# Build a 1024-cluster IVF + 16-byte PQ index for 256-dim vectors
index = faiss.index_factory(256, "IVF1024,PQ16")
index.train(training_vectors)        # Train k-means and PQ codebooks
index.add(database_vectors)           # Insert database vectors
distances, indices = index.search(query_vectors, k=10)

The string "IVF1024,PQ16" describes the index structure. More complex compositions:

  • "OPQ16_64,IVF1024,PQ16" — OPQ rotation (16 subspaces, 64-dim post-rotation), IVF with 1024 cells, PQ with 16 subspaces.
  • "IVF65536_HNSW32,Flat" — IVF with 65536 cells where the cell-assignment uses HNSW (with M=32) for fast cell lookup, Flat storage.

The string DSL is a major engineering productivity boost — you can experiment with many index configurations without writing custom code.

4. GPU Acceleration

FAISS provides GPU implementations for most index types. The basic GPU usage:

res = faiss.StandardGpuResources()           # GPU memory pool
gpu_index = faiss.index_cpu_to_gpu(res, 0, cpu_index)  # Move to GPU 0
distances, indices = gpu_index.search(queries, k=10)   # GPU search

GPU acceleration provides 5–50× speedup over CPU for typical workloads, particularly for large batches of queries. Building IVF clusters and PQ codebooks is also accelerated.

The GPU implementations have some constraints (limited index types, memory limited by GPU RAM) but are essential for production systems with high query loads.

5. When to Use FAISS

FAISS is appropriate when:

  • You need high-performance similarity search for dense vector embeddings.
  • The dataset is large enough (10⁵+ vectors) to need indexing infrastructure.
  • You can integrate a C++/Python library into your serving stack.

FAISS is not appropriate when:

  • You need a managed vector database with API and durability guarantees — use Pinecone, Weaviate, Qdrant, or similar (which often use FAISS or similar libraries internally).
  • You need only sparse-vector search — use Lucene-style inverted indexes.

6. Strengths

  • Comprehensive index library — every major ANN technique implemented.
  • Mature and well-tested at Meta and many other companies.
  • GPU acceleration widely available.
  • Active development — continued improvements over a decade.
  • Index factory DSL simplifies experimentation.

7. Weaknesses

  • Library, not a database — no built-in API server, persistence, replication.
  • Memory-resident — indexes must fit in RAM.
  • C++ + Python only — bindings for other languages are community-maintained.

8. See Also