Installing Faiss

Precautions

  • Before installing Faiss, install OpenBLAS. For details, see Installing OpenBLAS.
  • The current FeatureRetrieval is developed and released based on Faiss 1.10.0, the installation procedures of which are provided in this section as an example. You are advised to choose a proper Faiss version and install it based on the actual environment.
  • If the ARM platform is used, adapt the Faiss source code based on the GCC version before compiling and installing Faiss.
  • On the ARM platform, GCC of an earlier version (such as 4.8.5) does not support direct compilation of Faiss 1.10.0, and the compiler of an earlier version does not support the implementation of simdlib_neon.h. In this case, you need to use the default SIMD on the CPU. Though functions can run properly, the performance of some index algorithms (such as IVF and SQ algorithms) deteriorates greatly. You are advised to use GCC 7.5.0 for the compilation and installation. If the GCC version is later than 9.5.0, compatibility issues may occur.

Procedure

  1. Download the Faiss source package and decompress it.
    1
    2
    3
    # Faiss 1.10.0
    wget https://github.com/facebookresearch/faiss/archive/v1.10.0.tar.gz
    tar -xf v1.10.0.tar.gz && cd faiss-1.10.0/faiss
    
  2. Create an install_faiss_sh.sh script.
    vi install_faiss_sh.sh
  3. Add the following content to the install_faiss_sh.sh script.
    # modify source code
    # 1. Modify the Faiss source code.
    arch="$(uname -m)"
    if [ "${arch}" = "aarch64" ]; then
      gcc_version="$(gcc -dumpversion)"
      if [ "${gcc_version}" = "4.8.5" ];then
        sed -i '20i /*' utils/simdlib.h
        sed -i '24i */' utils/simdlib.h
      fi
    fi
    sed -i "149 i\\
        \\
        virtual void search_with_filter (idx_t n, const float *x, idx_t k,\\
                                         float *distances, idx_t *labels, const void *mask = nullptr) const {}\\
    " Index.h
    sed -i "49 i\\
        \\
    template <typename IndexT>\\
    IndexIDMapTemplate<IndexT>::IndexIDMapTemplate (IndexT *index, std::vector<idx_t> &ids):\\
        index (index),\\
        own_fields (false)\\
    {\\
        this->is_trained = index->is_trained;\\
        this->metric_type = index->metric_type;\\
        this->verbose = index->verbose;\\
        this->d = index->d;\\
        id_map = ids;\\
    }\\
    " IndexIDMap.cpp
    sed -i "30 i\\
        \\
        explicit IndexIDMapTemplate (IndexT *index, std::vector<idx_t> &ids);\\
    " IndexIDMap.h
    sed -i "217 i\\
      utils/sorting.h
    " CMakeLists.txt
    # modify source code end
    cd ..
    ls
    # 2. Configure Faiss compilation.
    cmake -B build . -DFAISS_ENABLE_GPU=OFF -DFAISS_ENABLE_PYTHON=OFF -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release
    # 3. Compile and install Faiss.
    cd build && make -j && make install
    cd ../.. && rm -f v1.10.0.tar.gz && rm -rf faiss-1.10.0
  4. Press Esc, type :wq!, and press Enter to save the changes and exit.
  5. Download the Faiss source package, decompress it, and install it.
    bash install_faiss_sh.sh
    • To compile Faiss 1.10.0, the CMake version must be 3.24.0 or later. If a message is displayed indicating that the CMake version is unqualified during Faiss compilation, solve this issue by referring to CMake Reports an Error During Faiss 1.10.0 Compilation.
    • The default Faiss installation directory is /usr/local/lib. To specify an installation directory, for example, install_path=/usr/local/faiss/faiss1.10.0, add -DCMAKE_INSTALL_PREFIX=${install_path} to the CMake compilation configuration.
      1
      2
      install_path=/usr/local/faiss/faiss1.10.0
      cmake -B build . -DFAISS_ENABLE_GPU=OFF -DFAISS_ENABLE_PYTHON=OFF -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${install_path}
      
  6. Configure the environment variable of the search path of the system library.

    The path of the Faiss dynamic library is required when the program with dynamic link depending on Faiss is running. Therefore, you need to add the LD_LIBRARY_PATH environment variable in the Faiss library directory.

    1
    2
    3
    4
    5
    6
    # Configure /etc/profile.
    vim /etc/profile
    # Add export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH to /etc/profile.
    # /usr/local/lib is the Faiss installation directory. If Faiss is installed in another directory, replace /usr/local/lib with the actual installation directory. In some operating systems and environments, Faiss may be installed in another directory.
    source /etc/profile
    cd ..
    
  7. Check whether the installation is successful.
    1
    cat /usr/local/share/faiss/faiss-config-version.cmake |grep 'PACKAGE_VERSION "'
    

    If the correct version information is displayed, the installation is successful.

If an error is reported after Faiss is compiled in the openEuler system, rectify the fault by referring to Message"undefined reference" Is Returned When libfaiss.so Is Linked.