目录

通过Makefile安装

安装命令

通过cmake安装

安装命令:

遇到问题

1、安装时报错,提示找不到gtest

2、安装时报错,提示找不到openBlas

3、提示找不到头文件

4、函数引用未定义


FAISS 是 Facebook AI 研究团队开源的针对聚类和相似性搜索库,它包含一种在任意大小的向量集合中搜索直到可能不适合在 RAM 中的新算法。它还包含用于评估和参数调整的支持代码。由于公司服务器没有连接外网,无法使用conda安装,所以直接源码安装,githup网站下载地址:

https://github.com/facebookresearch/faiss

安装方法参考安装说明:

https://github.com/facebookresearch/faiss/blob/master/INSTALL.md

faiss安装可以使用make工具或者cmake工具。make可以生成c++接口和python接口, cmake编译只能生成c++接口。

通过Makefile安装

安装命令

1、编译cpu版本库函数

./configure
make
make install

configure完成后生成makefile.inc, 默认使用intel  MTK库,我用的OpenBlas开源库,这里若不修改后续会提示找不到libmkl_avx2.so or libmkl_def.so。

解决办法:修改生成makefile.inc文件,指定连接OpenBlas库函数,然后重新执行make & make install

#添加OpenBlas和lapack库文件路径
#LDFLAGS      = -fopenmp
LDFLAGS      = -fopenmp -L/usr/local/OpenBLAS/lib -L/usr/lib64/atlas
#删除链接的mkl库,添加OpenBlas和lapack库文件
#LIBS         = -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl  
LIBS         = -lopenblas -llapack -lgomp -lpthread -lm -ldl  

2、编译python接口

#根目录下编译
make py

#安装
cd python 
make install

测试pthon接口:

[root@host233186 python]# python ../tutorial/python/1-Flat.py 
True
100000
[[  0 393 363  78]
 [  1 555 277 364]
 [  2 304 101  13]
 [  3 173  18 182]
 [  4 288 370 531]]
[[ 0.          7.17517328  7.2076292   7.25116253]
 [ 0.          6.32356453  6.6845808   6.79994535]
 [ 0.          5.79640865  6.39173603  7.28151226]
 [ 0.          7.27790546  7.52798653  7.66284657]
 [ 0.          6.76380348  7.29512024  7.36881447]]
[[ 381  207  210  477]
 [ 526  911  142   72]
 [ 838  527 1290  425]
 [ 196  184  164  359]
 [ 526  377  120  425]]
[[ 9900 10500  9309  9831]
 [11055 10895 10812 11321]
 [11353 11103 10164  9787]
 [10571 10664 10632  9638]
 [ 9628  9554 10036  9582]]

3、编译gpu版库文件

GPU版库依赖libfaiss.a文件,所以需要先执行步骤1。

另外makefile.inc中compute_60和compute_61,服务器不支持,删除两行就可以了。

cd  gpu
make  -j10

4、编译gpu版本的python接口

cd python
make _swigfaiss_gpu.so
make  install

在编译gpu版python接口时,曾经提示一下错误(后来再次编译时,错误有不出现了很奇怪):

/usr/bin/ld: ../gpu/libgpufaiss.a(gpufaiss_generated_GpuIndexFlat.cu.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
../gpu/libgpufaiss.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [_swigfaiss_gpu.so] Error 1

 在网上找了一下解决方案,就是在编译libgpufaiss.a时,使用 CFLAGS='$CFLAGS -fpic' 选项,试了一下是有效的。

cd gpu
make CFLAGS='$CFLAGS -fpic' -j10

 

测试:

[root@host233186 faiss]# python tutorial/python/4-GPU.py 
100000
[[ 381  207  210  477]
 [ 526  911  142   72]
 [ 838  527 1290  425]
 [ 196  184  164  359]
 [ 526  377  120  425]]
[[ 9900 10500  9309  9831]
 [11055 10895 10812 11321]
 [11353 11103 10164  9787]
 [10571 10664 10632  9638]
 [ 9628  9554 10036  9582]]
100000
[[ 381  207  210  477]
 [ 526  911  142   72]
 [ 838  527 1290  425]
 [ 196  184  164  359]
 [ 526  377  120  425]]
[[ 9900  9309  9810 10048]
 [11055 10895 10812 11321]
 [11353 10164  9787 10719]
 [10571 10664 10632 10203]
 [ 9628  9554  9582 10304]]

通过cmake安装

安装命令:

#创建build文件夹
mkdir build
cmake  ../
make
make install

注意:使用cmake编译faiss可以生成cpu和gpu版本的库文件,但是不支持生成python接口。如果需要python接口需要按照使用Makefile编译中生成python接口的方法进行编译。

遇到问题

0、configure是报错

checking for cuInit in -lcuda... configure: error: in `/faiss':
no
configure: error: Couldn't find libcuda
See `config.log' for more details

解决办法:./configure --with-cuda=/dev/null

1、安装时报错,提示找不到gtest

原因:没有安装googletest

解决办法:

      下载地址:https://github.com/abseil/googletest

#创建build文件夹
mkdir build
cmake  ../
make
make install

2、安装时报错,提示找不到openBlas

错误提示:

-- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off

-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off

CMake Error at cmake/Modules/FindOpenBLAS.cmake:59 (MESSAGE):

Could not find OpenBLAS

原因:没有指定OpenBlas的头文件路径和库文件路径。

解决方法:服务器上OpenBlas的安装目录是/usr/local/OpenBlas,修改cmake/Modules/FindOpenBLAS.cmake添加该目录:

SET(Open_BLAS_INCLUDE_SEARCH_PATHS
  /usr/include
  /usr/include/openblas
  /usr/include/openblas-base
  /usr/local/include
  /usr/local/include/openblas
  /usr/local/include/openblas-base
  /opt/OpenBLAS/include
  /opt/local/include
  /usr/local/OpenBLAS/include        #指定OpenBlas头文件的搜索目录
  $ENV{OpenBLAS_HOME}
  $ENV{OpenBLAS_HOME}/include
)

SET(Open_BLAS_LIB_SEARCH_PATHS
        /lib/
        /lib/openblas-base
        /lib64/
        /usr/lib
        /usr/lib/openblas-base
        /usr/lib64
        /usr/local/lib
        /usr/local/lib64
        /usr/local/OpenBLAS/lib    #指定OpenBlas库文件的搜索目录
        /opt/OpenBLAS/lib
        /opt/local/lib
        $ENV{OpenBLAS}cd
        $ENV{OpenBLAS}/lib
        $ENV{OpenBLAS_HOME}
        $ENV{OpenBLAS_HOME}/lib
 )

3、提示找不到头文件

错误提示:      [ 79%] Building CXX object tutorial/cpp/CMakeFiles/1-Flat.dir/1-Flat.cpp.o
faiss-master/tutorial/cpp/1-Flat.cpp:12:29: fatal error: faiss/IndexFlat.h: No such file or directory
 #include <faiss/IndexFlat.h>
compilation terminated.b

原因:1-Flat.cpp代码中使用的是“faiss/IndexFlat.h”相对路径名,而从githup中下载源码是“faiss-master/IndexFlat.h”

解决方法:将“faiss-master”重名为“faiss”, 或者逐个修改include源代码,前者比较简单

4、函数引用未定义

提示:

undefined reference to `faiss::gpu::getNumDevices()'

原因:

tutorial/cpp/CMakeLists.txt中定义编译的时统一使用libfaiss.a库文件,没有指定libgpufaiss.a

解决办法:修改tutorial/cpp/CMakeLists.txt文件

file(GLOB srcs ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

# Build each source file independently
include_directories(../../../)  # faiss root directory

foreach(source ${srcs})
    get_filename_component(name ${source} NAME_WE)

    # 设定编译gpu代码时引用的库文件
    IF(name MATCHES ".*GPU.*")
        SET(faiss_lib gpufaiss faiss cublas)
        #指定cublas库文件的路径
        link_directories("/usr/local/cuda-7.5/lib64")   
    ENDIF(name MATCHES ".*GPU.*")

    message("****************" ${name} "libraries:" ${faiss_lib} "****************")
    add_executable(${name} ${source}) 
    target_link_libraries(${name} ${faiss_lib})

    # Install
    install(TARGETS ${name} DESTINATION bin)
endforeach(source)

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐