FAISS GPU Installation

Step 1: Install Dependencies

Run the following commands to install the necessary dependencies:


sudo apt-get update
sudo apt-get install -y cmake libopenblas-dev libomp-dev libgtest-dev gcc-10 g++-10
    

Step 2: Install CUDA

If CUDA is not installed, follow these steps to install it:


sudo apt-get install cuda-12-2
    

Set up environment variables for CUDA:


export CUDA_HOME=/usr/local/cuda-12.2
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
    

Step 3: Clone FAISS Repository


git clone https://github.com/facebookresearch/faiss.git
cd faiss
    

Step 4: File Editing - Modify CMakeLists.txt

Edit the CMakeLists.txt file to ensure proper configuration:


vi faiss/CMakeLists.txt
    

Add the following lines at the top:


cmake_minimum_required(VERSION 3.27)
project(Faiss VERSION 1.7.0 LANGUAGES C CXX)
    

Fix the path for `faiss-config.cmake.in` by editing the line around line 410 in `CMakeLists.txt`:


configure_file(${CMAKE_SOURCE_DIR}/cmake/faiss-config.cmake.in
               ${CMAKE_BINARY_DIR}/faiss-config.cmake @ONLY)
    

Step 5: Build FAISS with GPU Support


cmake -B build -DFAISS_ENABLE_GPU=ON -DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.2/bin/nvcc -DCMAKE_CUDA_ARCHITECTURES=86 -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
    

Step 6: Install FAISS (Optional)


sudo make install
    

Step 7: Verify FAISS Installation

Run the following test script to verify FAISS installation:


import faiss
import numpy as np

# Create random vectors
d = 128  # dimension
nb = 1000  # number of vectors
xb = np.random.random((nb, d)).astype('float32')

# Create an index for L2 distance
index = faiss.IndexFlatL2(d)

# Add vectors
index.add(xb)

# Search for nearest neighbors
D, I = index.search(xb[:1], 5)
print(f"Distances: {D}")
print(f"Indices: {I}")
    

Step 8: Install SWIG (if not installed)

If you encounter issues with Python bindings, install SWIG:


sudo apt-get install swig
    

Step 9: Set PYTHONPATH (Optional)

If FAISS Python bindings aren't being picked up, set the PYTHONPATH:


export PYTHONPATH=/home/open-webui/faiss/build/faiss/python:$PYTHONPATH