Faiss Installation Reference

  • Before installing the Faiss, install the OpenBLAS. For details, see OpenBLAS Installation Reference.
  • The current FeatureRetrieval is developed and released based on Faiss v1.7.1, the installation procedures of which are provided in this section as an example. You are advised to use the actual Faiss version and environment and modify Faiss as follows after performing Step 2.
    • If the ARM platform is used, adapt the Faiss source code based on the GCC version before compiling and installing the Faiss.
    • On the ARM platform, GCC of an earlier version (such as 4.8.5) does not support direct build of Faiss v1.7.1, and 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 of Faiss 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 build and installation. If the GCC version is later than 9.5.0, compatibility issues may occur.
  1. Download and decompress the Faiss source package. (To compile Faiss, the CMake version must be 3.17 or later.)
    wget https://github.com/facebookresearch/faiss/archive/refs/tags/v1.7.1.tar.gz
    tar -xf v1.7.1.tar.gz
  2. Go to the Faiss directory.
    cd faiss-1.7.1
    • Add the following content to line 118 (after the declaration of the search API) in faiss/Index.h:
      virtual void search_with_filter (
      			idx_t n,
      			const float *x,
      			idx_t k,
      			float *distances,
      			idx_t *lables,
      			const void *mask = nullptr) const {}
      
    • Add the following content to line 33 (after the declaration of the IndexIdMapTemplate interface) in faiss/MetaIndexes.h:
      explicit IndexIDMapTemplate (IndexT *index, std::vector<idx_t> &ids);
    • Add the following content to line 39 (after the definition of the IndexIDMapTemplate interface) in faiss/MetaIndexes.cpp:
      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;
      }
  3. Build and configure Faiss.
    cmake -B build  -DFAISS_ENABLE_GPU=OFF -DFAISS_ENABLE_PYTHON=OFF -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release
  4. Perform build and installation.
    cd build
    make -j && make install
  5. Configure the search path for the system library and return to the upper-level directory.

    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 Faiss library directory to the LD_LIBRARY_PATH environment variable.

    # 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 ..