Faiss GPU
Faiss GPU (Facebook AI Similarity Search) is a library for efficient similarity search and clustering of dense vectors, with a strong focus on high performance through GPU acceleration. The `faiss-gpu` package on PyPI provides Python bindings designed for NVIDIA GPUs. While the upstream Faiss library and its associated wheel-building projects have progressed to significantly newer versions (e.g., v1.9.x or v1.13.x), the current version of `faiss-gpu` directly available on PyPI is `1.7.2` (as of early 2022). This implies that many recent features, bug fixes, and performance improvements from more current Faiss releases are not present in the PyPI `faiss-gpu` package.
Warnings
- breaking The `faiss-gpu` PyPI package (v1.7.2, released early 2022) is significantly outdated compared to the upstream Faiss library (latest official v1.9.1; `faiss-wheels` targets v1.13.x). This means many new features, bug fixes, and performance optimizations from more recent Faiss versions will not be available, and API signatures may differ from current documentation. For newer Faiss versions with GPU support, users often need to compile from source or seek alternative, potentially unofficial, wheel distributions.
- gotcha Using `faiss-gpu` requires a properly installed NVIDIA CUDA toolkit, compatible NVIDIA drivers, and potentially cuBLAS/cuDNN libraries. The Faiss build is linked against specific CUDA versions. Missing or incompatible GPU dependencies will lead to runtime errors (e.g., `FaissError` during index creation or `ModuleNotFoundError` if GPU-specific components cannot load).
- gotcha While the PyPI package name is `faiss-gpu`, the Python module is imported as `faiss`. Attempting `import faiss_gpu` will result in an `ImportError`. GPU-specific functionality is then accessed through methods and classes within the `faiss` module (e.g., `faiss.GpuIndexFlatL2`).
- gotcha Compatibility with NumPy 2.0 (released mid-2024) is a known issue for many C-extension libraries, including Faiss. Older `faiss-gpu` versions (like 1.7.2) are unlikely to be compatible without recompilation or patches. The `faiss-wheels` project, which typically builds these wheels, explicitly pins NumPy versions to `<2.0` in its build configurations.
Install
-
pip install faiss-gpu
Imports
- faiss
import faiss_gpu
import faiss
Quickstart
import faiss
import numpy as np
# Check for GPU availability
if not faiss.get_num_gpus():
print("Warning: No GPUs detected. Faiss GPU functionality will not be available.")
print("Ensure NVIDIA drivers and CUDA toolkit are correctly installed.")
# In a real application, you might exit or switch to faiss-cpu here.
# For this quickstart, we will proceed assuming GPU context is desired if available.
# Define parameters
d = 128 # vector dimension
nb = 100000 # database size
nq = 1000 # number of query vectors
k = 4 # number of nearest neighbors to search for
# Generate random data
np.random.seed(1234)
xb = np.random.random((nb, d)).astype('float32')
xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.
print(f"Database vectors shape: {xb.shape}")
print(f"Query vectors shape: {xq.shape}")
# Initialize GPU resources
# A single StandardGpuResources object handles memory management on a specific GPU.
res = faiss.StandardGpuResources()
# Create a GPU index (e.g., IndexFlatL2 for brute-force L2 distance)
# 'res' links the index to the GPU resources, 'd' is the vector dimension.
index_flat = faiss.GpuIndexFlatL2(res, d)
# Add vectors to the index
print("Adding vectors to GPU index...")
index_flat.add(xb)
print(f"Number of vectors in the index: {index_flat.ntotal}")
# Search for nearest neighbors
print(f"Searching for {k} nearest neighbors for {nq} queries...")
D, I = index_flat.search(xq, k) # D: distances, I: indices
print("\nFirst 5 query results (indices of nearest neighbors):")
print(I[:5])
print("\nFirst 5 query results (distances):")
print(D[:5])