Hnsw
FAISS: Facebook AI Similarity Search
Overview
FAISS is a library developed by Facebook AI Research (FAIR) for efficient similarity search and clustering of dense vectors. It is the core engine behind most vector databases.
Key Concepts
1. The Index
The core object in FAISS. You add vectors to an Index, and search against it.
- IndexFlatL2: Exact search (brute force). Perfect accuracy, slow at scale.
- IndexIVFFlat: Inverted File Index. Faster, slightly less accurate.
- IndexHNSW: Graph-based. Fastest, but uses more RAM.
2. Search
import faiss
import numpy as np
d = 64 # dimension
nb = 100000 # database size
xb = np.random.random((nb, d)).astype('float32')
index = faiss.IndexFlatL2(d)
index.add(xb)
# Search
xq = np.random.random((1, d)).astype('float32')
D, I = index.search(xq, k=5) # search 5 nearest neighborsGPU Acceleration
FAISS can run on NVIDIA GPUs, which is 5-10x faster than CPU.
When to use?
Use FAISS if you want raw speed and are building a custom search engine. Use a Vector Database (Pinecone, Chroma) if you want a managed service with an API.