FAISS
Facebook AI Research Similarity Search. C++ library with Python bindings for efficient similarity search and clustering of dense vectors. Supports flat (exact), IVF (approximate), HNSW, PQ, and many other index types. CPU-only via PyPI (faiss-cpu); GPU support requires conda or building from source. Officially maintained by Meta/Facebook AI Research; PyPI wheel builds maintained by the community (faiss-wheels project). Import is always 'import faiss' regardless of which package is installed.
Warnings
- breaking faiss-gpu on PyPI is frozen at 1.7.2 (2021) and discontinued. pip install faiss-gpu installs a 3-year-old build supporting only Python <= 3.10 and CUDA 11. Will not import on Python 3.11+.
- breaking FAISS indices saved in one architecture (e.g., x86_64) are not always loadable in another (e.g., arm64). Cross-platform index portability is not guaranteed. Loading an incompatible index raises a segfault or corrupted data error.
- breaking Indices built with the AVX2 SIMD extension are not compatible with the generic extension. faiss-cpu PyPI wheels include both generic and AVX2 variants and select at runtime. In containers where CPU features are misdetected, this causes segfaults or wrong results.
- breaking All vectors passed to FAISS must be numpy float32 (dtype='float32'). Passing float64, int, or Python lists causes TypeError at the C++ binding layer or silent wrong results.
- gotcha Approximate indices (IndexIVFFlat, IndexIVFPQ, IndexHNSW variants) require index.train(vectors) before index.add(). Calling add() on an untrained IVF index raises a 'not trained' assertion error. IndexFlat* are the only types that are pre-trained.
- gotcha FAISS is a library, not a database. It has no persistence by default. Indices exist only in memory. faiss.write_index() / faiss.read_index() must be called explicitly to save/load. There is no automatic recovery on crash.
- gotcha faiss.IndexFlatL2 performs exact exhaustive search — O(n) per query. For large datasets (>100k vectors), query time is prohibitively slow. Many tutorials use IndexFlatL2 for simplicity without noting this.
- gotcha pip install faiss-cpu from source (not wheel) requires SWIG, CMake, and a BLAS library to be present on the system. Occurs when no pre-built wheel is available for the Python/OS/arch combination. Fails with 'swig not found' or BLAS linking errors.
Install
-
pip install faiss-cpu -
conda install -c pytorch -c conda-forge faiss-cpu=1.13.2 -
conda install -c pytorch -c nvidia -c conda-forge faiss-gpu=1.13.2
Imports
- faiss
import faiss
- faiss-gpu (PyPI, discontinued)
conda install -c pytorch -c nvidia faiss-gpu
Quickstart
import faiss
import numpy as np
dim = 128
n_vectors = 10000
# All vectors MUST be float32
vectors = np.random.random((n_vectors, dim)).astype('float32')
# Flat (exact) index
index = faiss.IndexFlatL2(dim)
print(index.is_trained) # True (flat indices don't need training)
index.add(vectors)
print(index.ntotal) # 10000
# Search: returns (distances, indices)
query = np.random.random((5, dim)).astype('float32')
D, I = index.search(query, k=10)
print(I.shape) # (5, 10)
# Save/load index
faiss.write_index(index, 'my_index.faiss')
index2 = faiss.read_index('my_index.faiss')