faiss 安裝

faiss c++ 源碼編譯

0.寫在前面

參考下述文章的編譯安裝步驟同時將自己遇到的錯誤進行梳理解決:

https://juejin.im/post/5bed7cebe51d454e5b5f2c23 十分感謝此文章~

遇到問題多去faiss github裏面的 issues中找找,往往總能找到你想要的?

https://github.com/facebookresearch/faiss

1.安裝Python環境

如果使用python接口,不需要對源碼有更深瞭解的話,可以直接用 conda 安裝,簡單方便

#下載anaconda包並安裝
wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh
chmod +x Anaconda3-5.3.0-Linux-x86_64.sh
bash Anaconda3-5.3.0-Linux-x86_64.sh

#手動添加環境變量
vi $HOME/.bashrc
export PATH="$HOME/anaconda3/bin:$PATH"
source $HOME/.bashrc

#安裝faiss 
conda install faiss-cpu -c pytorch #cpu版本

conda install faiss-gpu -c pytorch # [DEFAULT]For CUDA8.0  gpu版本
conda install faiss-gpu cuda90 -c pytorch # For CUDA9.0    gpu版本
conda install faiss-gpu cuda91 -c pytorch # For CUDA9.1    gpu版本

2.編譯源碼

2.1 C++環境

2.1.1 相關庫安裝並編譯

# 安裝gcc:
yum install gcc 
# 安裝g++:
yum install gcc-c++ 
# 查看gcc版本,如果是gcc4.8以下建議升級到gcc4.8
gcc -v

#安裝OpenBLAS
git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make FC=gfortran #如果沒有 安裝yum install gcc-gfortran
make install
ln -s /opt/OpenBLAS/lib/libopenblas.so  /usr/lib/libopenblas.so
LD_LIBRARY_PATH=/opt/OpenBLAS/lib
export LD_LIBRARY_PATH

#安裝lapack
wget http://www.netlib.org/lapack/lapack-3.4.2.tgz
tar -zxf lapack-3.4.2.tgz
cd lapack-3.4.2
cp ./INSTALL/make.inc.gfortran ./
mv make.inc.gfortran make.inc
vi Makefile # 修改如下
[#lib: lapacklib tmglib
lib: blaslib variants lapacklig tmglib]
make
cd lapacke
make  
cp include/*.h /usr/include   
cd .. 
cp *.a /usr/lib 

# 編譯Faiss源碼
git clone https://github.com/facebookresearch/faiss.git
cd faiss
cp example_makefiles/makefile.inc.Linux ./makefile.inc
./configure  # 需注意地方。請看注意事項部分
make misc/test_blas
./misc/test_blas
make
make install

2.1.2 測試

如果你正確編譯了Faiss 那麼 運行一下命令可以測試c++的接口是否可以使用(如果沒有下載相應的SIFT1M數據集的話,運行程序時會報沒有文件的錯誤)

make demos
./demo_sift1M

 

2.1.3 注意事項及問題

(1) 確保默認 python 爲python2 ,否則lapack庫編譯安裝時會出錯

Linux 修改系統默認python版本方法:

https://blog.csdn.net/white_idiot/article/details/78240298

(2) 確保python2中安裝了numpy

ps: python版本多的時候注意pip的版本哦~

安裝速度慢的話記得換源~

(3) yum安裝報錯:ImportError: No module named urlgrabber.grabber

解決辦法:

vi /usr/bin/yum
vi /usr/libexec/urlgrabber-ext-down

文件中首行python的路徑改成相同的 。urlgrabber-ext-down與yum 中一致

(4)./configure 時注意查看你缺少什麼 後面的問題很多就是由於你所缺的

如果你沒有nvcc 那麼要使用./configure --without-cuda 否則會出錯。我的錯誤是在編譯時出現諸如:

IndexScalarQuantizer.cpp:1110:56: error: ‘_mm256_cvtepu8_epi16’ was not declared in this scope
                 (_mm_loadu_si128((__m128i*)(code1 + i)));
                                                        ^
IndexScalarQuantizer.cpp:1112:56: error: ‘_mm256_cvtepu8_epi16’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
                 (_mm_loadu_si128((__m128i*)(code2 + i)));

(5) 編譯時沒有error,跑demo時出現問題

make demos 無問題

./demos/demo_sift1M 出錯

error while loading shared libraries: libfaiss.so: cannot open shared object file: No such file or directory

解決方法將libfaiss.so添加到$LD_LIBRARY_PATH中

$LD_LIBRARY_PATH
cp libfaiss.so $LD_LIBRARY_PATH

在此過程中我嘗試使用g++ 然後手加地址進行編譯

g++ -o demo_sift1M demo_sift1M.cpp -L/libfaiss.so所在地址 -lfaiss

出現了錯誤:

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

修改爲

g++ -std=c++11 -o test demo_sift1M.cpp -L/libfaiss.so所在地址 -lfaiss

然鵝 還是行不通! 還是要把libfaiss.so添加到$LD_LIBRARY_PATH中

(6) 運行./misc/test_blas 測試blas時出現下方情況是正常的,不是錯誤哦

BLAS test
errors=
 0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
 0.000 -0.000  0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000 -0.000 -0.000  0.000  0.000  0.000
 0.000  0.000  0.000  0.000  0.000 -0.000  0.000 -0.000 -0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000
 0.000  0.000  0.000  0.000  0.000 -0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
 0.000  0.000  0.000  0.000 -0.000  0.000 -0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000
 0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000 -0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000  0.000
 0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000  0.000 -0.000
 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000 -0.000  0.000  0.000  0.000  0.000
 0.000  0.000  0.000 -0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000 -0.000  0.000  0.000  0.000  0.000  0.000
 0.000  0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.000  0.000  0.000  0.000  0.000  0.000
Intentional Lapack error (appears only for 64-bit INTEGER):
info=0000064b00000000
Lapack uses 32-bit integers

2.2 python接口

2.2.1 編譯安裝

make py
cd python
python setup.py install #注意這一步,否則除python路徑下無法import faiss

2.2.2 測試

python -c 'import faiss'

2.2.3 注意事項及問題

(1) swig

要使用python接口就要使用到swig 請確認是否安裝了該包,並且確認其版本是否是3

(yum install swig 默認安裝的是版本2的,要在網站上自己下載包進行編譯安裝)

具體請參考:

https://blog.csdn.net/Nankys/article/details/86219705

make py時可能出現以下錯誤,其原因及解決辦法:

問題原因:無swig

make -C python
make[1]: Entering directory `/faiss_c++/faiss-master/python'
python -c++ -Doverride= -I../ -DSWIGWORDSIZE64 -o swigfaiss.cpp swigfaiss.swig
  File "<string>", line 1
    ++
     ^
SyntaxError: invalid syntax
make[1]: [swigfaiss.cpp] Error 1 (ignored)
g++ -std=c++11 -DFINTEGER=int  -fopenmp  -fPIC -m64 -Wno-sign-compare -g -O3 -Wall -Wextra -mpopcnt -mavx2 -mf16c -I/usr/local/ext2/anaconda2/include/python2.7 -I/usr/local/ext2/anaconda2/include/python2.7 -I/usr/local/ext2/anaconda2/lib/python2.7/site-packages/numpy/core/include \
               -I../ -c swigfaiss.cpp -o swigfaiss.o
g++: error: swigfaiss.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
make[1]: *** [swigfaiss.o] Error 4
make[1]: Leaving directory `/faiss_c++/faiss-master/python'
make: *** [py] Error 2

問題原因:swig版本不對

make -C python
make[1]: Entering directory `/faiss_c++/faiss-master/python'
swig -python -c++ -Doverride= -I../ -DSWIGWORDSIZE64 -o swigfaiss.cpp swigfaiss.swig
../Index.h:72: Error: Syntax error in input(3).
make[1]: *** [swigfaiss.cpp] Error 1
make[1]: Leaving directory `/faiss_c++/faiss-master/python'
make: *** [py] Error 2

(2) make py成功後,找不到faiss包

如果只有在python路徑下可以Import faiss 其他路徑無法import

可能你沒有執行python setup.py install 這步

faiss python 安裝

1. conda 

conda install 在線/離線

2. pip 

https://libraries.io/pypi/faiss-cpu

pip install faiss-cpu==1.6.1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章