faiss-next
Rust bindings for Faiss (Facebook AI Similarity Search), a library for efficient similarity search and clustering of dense vectors.
Features
- Safe Rust API: Idiomatic Rust wrappers around Faiss C API
- Multiple Index Types: Support for Flat, IVF, LSH, Scalar Quantizer, and more
- Multi-Version Support: Works with Faiss 1.14.x and newer (with loose compatibility mode)
- CUDA Support: Optional GPU acceleration on Linux and Windows (feature flag:
cuda) - Serialization: Save and load indexes to/from disk
- Clustering: K-means clustering with customizable parameters
- Pairwise Operations: Efficient distance computations (L2, inner product)
Platform Support
| OS | Architecture | CPU | CUDA |
|---|---|---|---|
| macOS (Apple Silicon) | aarch64 (M1/M2/M3) | ✅ | ❌ |
| Linux | x86_64 | ✅ | ✅ |
| Windows | x86_64 | ✅ | ✅ |
Legend:
- ✅ Fully supported with pre-generated bindings
- ❌ Not supported
Notes:
- CUDA is supported on Linux x86_64 and Windows x86_64
Supported Index Types
| Index Type | Description |
|---|---|
IndexFlat |
Brute-force index (exact search) |
IndexIVFFlat |
Inverted file with flat quantizer |
IndexIVFScalarQuantizer |
IVF with scalar quantization |
IndexScalarQuantizer |
Scalar quantizer index |
IndexLSH |
Locality-sensitive hashing |
IndexIDMap / IndexIDMap2 |
Custom ID mapping wrapper |
IndexPreTransform |
Pre-transformation wrapper |
IndexRefineFlat |
Refinement with flat index |
IndexReplicas / IndexShards |
Distributed indexes |
IndexBinary |
Binary vector indexes |
IndexFlat1D |
Optimized 1D flat index |
Search Parameters
| Type | Description |
|---|---|
SearchParameters |
Basic search parameters |
SearchParametersIvf |
IVF-specific parameters (nprobe, max_codes) |
Getting Started
Prerequisites
- Install Faiss C library:
macOS (Homebrew):
Linux:
# From source (recommended)
&&
Windows:
For Windows, you need to build Faiss from source with C API enabled. A pre-configured build is available at:
&&
-DFAISS_ENABLE_GPU=ON
-DCMAKE_INSTALL_PREFIX=C:/tools/faiss
After installation, set environment variables:
set FAISS_INCLUDE_DIR=C:\tools\faiss\include
set FAISS_LIB_DIR=C:\tools\faiss\lib
Or copy faiss.dll and faiss_c.dll to your executable directory.
Note: CUDA support on Windows requires:
- NVIDIA CUDA Toolkit installed
- Faiss built with
-DFAISS_ENABLE_GPU=ON - MKL libraries (bundled with Intel oneAPI or conda)
- Ensure the library is discoverable:
- Set
FAISS_DIRenvironment variable, or - Install to standard locations (
/usr/local,/opt/homebrew)
- Set
Installation
Add to your Cargo.toml:
[]
= "0.6"
Basic Usage
use ;
Using Index Factory
use ;
Using Index Builder (Fluent API)
use ;
Custom IDs
use ;
Serialization
use ;
Pairwise Distance Computation
use ;
let d = 128;
let x: = vec!; // 10 query vectors
let y: = vec!; // 100 database vectors
// Compute pairwise L2 squared distances
let distances = pairwise_l2_sqr; // 10 * 100 elements
// Compute pairwise inner products
let products = inner_products; // 10 * 100 elements
Clustering (K-means)
use ;
Search with Parameters
For fine-grained control over search behavior, use search_with_params:
use ;
Range Search
Find all vectors within a distance threshold:
use ;
Feature Flags
| Flag | Description |
|---|---|
cuda |
Enable CUDA GPU support (Linux x86_64 and Windows x86_64) |
bindgen |
Generate bindings at compile time (requires LLVM/Clang) |
Performance
The bindings leverage Faiss's optimized SIMD implementations:
L2 Distance Benchmark (Dimension: 128)
Size (nq, nb) ndarray (ms) faiss (ms) Speedup
( 100, 1000) 12.34 0.87 14.2x
( 1000, 1000) 123.45 8.21 15.0x
( 1000, 10000) 1234.56 17.89 69.0x
Version Compatibility
- Minimum: Faiss 1.14.0
- Tested: Faiss 1.14.x
- Loose Mode: Newer versions will work with a compile-time warning
When Faiss 1.15 is released, add new bindings in the v1_15 directory.
Documentation
License
MIT License