What is FAISS?FAISS (Facebook AI Similarity Search) is a library developed by Facebook AI Research for efficient similarity search and clustering of dense vectors. It provides algorithms that search for points in a set of vectors that are closest to a given query vector. This is a fundamental operation in many applications, especially in artificial intelligence and machine learning, where data points are often represented as high-dimensional vectors. 
Its primary strength lies in handling large datasets—it can search through billions of vectors, even on systems with limited RAM, by employing various indexing strategies. FAISS is written in C++ and has Python bindings, making it accessible to a wide range of developers and researchers. It supports both CPU and GPU implementations for various index types, allowing for highly optimized performance. Common use cases for FAISS include: - Recommendation Systems: Finding items similar to those a user has interacted with.
- Image/Video Search: Retrieving images or videos similar to a query image/video.
- Natural Language Processing: Identifying semantically similar documents, paragraphs, or words based on their vector embeddings.
- Duplicate Detection: Finding near-duplicate items in large datasets.
- Clustering: Grouping similar data points together.

How to Install FAISS on CentOS 7Installing FAISS on CentOS 7 can be done via pip for pre-built CPU wheels, or by building from source for more control, especially for GPU support or specific optimizations. CentOS 7 comes with older packages by default, so some steps involve upgrading tools like CMake and ensuring Python 3 is properly set up. PrerequisitesBefore installing FAISS, ensure you have the following on your CentOS 7 system: - Development Tools: C++ compiler (GCC), make.
- Python 3 and pip: CentOS 7 often defaults to Python 2. You'll need Python 3 (3.6+ recommended) and its package installer,
pip. - Git: For cloning the FAISS repository if building from source.
- CMake: Version 3.10 or higher. CentOS 7's default is often too old (2.8.x).
- BLAS/LAPACK Library: For high-performance linear algebra operations (e.g., OpenBLAS, MKL, ATLAS).
- NumPy and SWIG: Python libraries required for building FAISS Python bindings.
- NVIDIA CUDA Toolkit (Optional): If you plan to use FAISS with GPU support. This requires a compatible NVIDIA GPU and drivers.
Method 1: Using Pip (Recommended for CPU-only, pre-built wheels)This is the easiest method if a pre-built wheel is available for your Python version and CentOS 7 architecture. These wheels are typically CPU-only. 1. Ensure Python 3 and pip are installed: sudo yum install epel-release -y
sudo yum install python3 python3-pip -y
# Ensure pip refers to python3's pip
python3 -m pip install --upgrade pip
2. Install FAISS using pip:
python3 -m pip install faiss-cpu # For CPU-only version
# OR for GPU support (if pre-built wheel exists, rare for CentOS 7, often needs CUDA installed separately)
# python3 -m pip install faiss-gpu
- Note: If
faiss-cpu or faiss-gpu wheels are not available for your specific Python version or CentOS 7, pip will try to build from source, which might fail if dependencies are not met. In such cases, Method 2 is necessary.
Method 2: Building from Source (For GPU support, specific optimizations, or if pip fails)Building from source gives you full control over the FAISS configuration, including enabling GPU support and linking to specific BLAS libraries. Step 1: Install System Dependencies1. Update system and install basic development tools: sudo yum update -y
sudo yum groupinstall "Development Tools" -y
sudo yum install git wget unzip -y
2. Install Python 3, pip, and required Python packages:
sudo yum install python3 python3-pip -y
python3 -m pip install --upgrade pip
python3 -m pip install numpy swig
3. Install a BLAS/LAPACK library (OpenBLAS recommended):
sudo yum install openblas-devel -y
# Note: For MKL, you would need to download and install Intel MKL separately.
4. Upgrade CMake (Crucial for CentOS 7): CentOS 7's default CMake (2.8.x) is too old. You need CMake 3.10 or newer.
# Download a newer CMake version (e.g., 3.27.x - check cmake.org for latest)
wget https://github.com/Kitware/CMake/releases/download/v3.27.0/cmake-3.27.0-linux-x86_64.tar.gz
sudo tar -zxvf cmake-3.27.0-linux-x86_64.tar.gz -C /opt
# Create a symlink to make the new CMake available in your PATH
sudo ln -sf /opt/cmake-3.27.0-linux-x86_64/bin/cmake /usr/local/bin/cmake
sudo ln -sf /opt/cmake-3.27.0-linux-x86_64/bin/ctest /usr/local/bin/ctest
sudo ln -sf /opt/cmake-3.27.0-linux-x86_64/bin/cpack /usr/local/bin/cpack
# Verify CMake version
cmake --version
5. Install CUDA Toolkit (if building with GPU support): Follow NVIDIA's instructions to install the CUDA Toolkit for CentOS 7. This is a complex step involving driver installation, nvcc, cudnn, etc.
Step 2: Clone FAISS Repositorygit clone https://github.com/facebookresearch/faiss.git
cd faiss
Step 3: Build FAISS Core Library1. Create a build directory: mkdir build && cd build
2. Run CMake with desired options:
- CPU-only build (recommended for most CentOS 7 users): Adjust
BLAS_LIBRARIES path if your OpenBLAS installation is different. You can find it with find /usr -name "libopenblasp*so".
cmake .. \
-DFAISS_ENABLE_GPU=OFF \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_DEMOS=OFF \
-DBUILD_TESTING=OFF \
-DFAISS_ENABLE_PYTHON=ON \
-DBLAS_LIBRARIES="/usr/lib64/libopenblasp-r0.3.20.so" # Adjust path to your OpenBLAS library
- GPU build (requires CUDA Toolkit installed):
cmake .. \
-DFAISS_ENABLE_GPU=ON \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_DEMOS=OFF \
-DBUILD_TESTING=OFF \
-DFAISS_ENABLE_PYTHON=ON \
-DBLAS_LIBRARIES="/usr/lib64/libopenblasp-r0.3.20.so" \ # Or MKL, etc.
-DCUDA_ARCHITECTURES="All" # Or specific arch, e.g., "75" for RTX 20 series
3. Compile FAISS:
make -j $(nproc) # Compiles using all available CPU cores
4. Optional: Install C++ libraries system-wide:
sudo make install
Step 4: Install Python Bindings1. Navigate back to the FAISS root directory: cd ..
2. Install Python bindings:
python3 setup.py install
# Alternatively, you can use pip:
# python3 -m pip install .
Step 5: Verify InstallationOpen a Python 3 interpreter and try to import FAISS: python3
import faiss
print(faiss.__version__)
If the import is successful and prints a version number, FAISS has been successfully installed.
|