编译Faiss-gpu【InterMKL】C++ 按步骤操作 基本不会有问题的 python原理相同。
使用Faiss版本【1.7.4】该项目依赖于BLAS 组件 使用InterMKL 编译的c++ faiss
编译Faiss-gpu C++
基本介绍
使用Faiss版本【1.7.4】
该项目依赖于BLAS 组件 OpenBLAS 和 IntelMKL BLAS 【官方支持】
IntelMKL 会比 OpenBLAS 快的多。 【来自官方结论】
本机环境
Cuda :11.1
Cuda-Driver: 515
InterMKL: 2021.2.0
Faiss :1.7.4
注意:faiss仅适用于 CPU 的faiss-cpuconda 软件包目前可在 Linux、OSX 和 Windows 上使用。包含faiss-gpuCPU 和 GPU 索引,可在 Linux 系统上使用,适用于各种版本的 CUDA。【来自官方】
1. 安装InterMKL
使用apt安装
# 添加密钥
wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null
# 添加源
echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list
# 更新源
sudo apt update
# 查找想要安装的版本
apt list -a intel-basekit
# 安装InterMKL 2021版本
apt-get install intel-basekit=2021.2.0-2883
# 配置MKL-ROOT 环境变量
export MKL_ROOT=/opt/intel/oneapi/mkl/2021.0
# 配置InterMKL运行的环境变量
source /opt/intel/oneapi/setvars.sh intel64
2.执行Cmake
执行Cmake 命令,python 请将 FAISS_ENABLE_PYTHON改为ON
cmake -B build
-DFAISS_ENABLE_GPU=ON
-DFAISS_ENABLE_PYTHON=OFF
-DBUILD_TESTING=OFF
-DFAISS_ENABLE_C_API=ON
-DCMAKE_BUILD_TYPE=Release
-DCUDAToolkit_ROOT=/usr/local/cuda-11.1
-DCMAKE_CUDA_ARCHITECTURES="75"
-DBUILD_SHARED_LIBS=ON
-DFAISS_OPT_LEVEL=avx2
基本参数介绍:
-DFAISS_ENABLE_GPU=ON *是否构建GPU支持
-DFAISS_ENABLE_PYTHON=OFF 是否构建Python 支持
-DBUILD_TESTING=ON 是否编译Testing 【依赖于googletest】
-DFAISS_ENABLE_C_API=ON 是否需要提高CAPI支持
-DBLA_VENDOR=Intel10_64_dyn 使用的IntelMKL 的编译类型
-DCMAKE_BUILD_TYPE=Release 编译类型
-DCMAKE_CUDA_ARCHITECTURES="75" *显卡算力
-DBUILD_SHARED_LIBS=ON 是否生成动态库
2.进行Make
make -C build -j 64 faiss
3: 安装
make -C build install
4: 测试
CPU 编译测试
make -C build demo_ivfpq_indexing
./build/demos/demo_ivfpq_indexing
GPU 编译测试:
- 需要复制faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp
cp faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp ./demos
vim CMakeLists.txt
- 添加如下内容:
add_executable(demo_ivfpq_indexing_gpu EXCLUDE_FROM_ALL demo_ivfpq_indexing_gpu.cpp)
target_link_libraries(demo_ivfpq_indexing_gpu PRIVATE faiss)
- 编译测试:
make -C build demo_ivfpq_indexing_gpu
./build/demos/demo_ivfpq_indexing_gpu
测试成功输出的内容:
CPU 与 GPU 测试案例相同,输出内容一致。只是是否使用gpu,cpu的区别
[3.495 s] Generating 100000 vectors in 128D for training
[3.779 s] Training the index
Training IVF quantizer on 100000 vectors in 128D
Clustering 100000 points in 128D to 1788 clusters, redo 1 times, 10 iterations
Preprocessing in 0.01 s
Iteration 9 (0.33 s, search 0.28 s): objective=930821 imbalance=1.244 nsplit=0
Input training set too big (max size is 65536), sampling 65536 / 100000 vectors
computing residuals
training 4 x 256 product quantizer on 65536 vectors in 128D
[4.698 s] storing the pre-trained index to /tmp/index_trained.faissindex
[4.701 s] Building a dataset of 200000 vectors to index
[5.433 s] Adding the vectors to the index
[5.512 s] done
[5.519 s] Searching the 5 nearest neighbors of 9 vectors in the index
[5.519 s] Query results (vector ids, then distances):
query 0: 1234 13437 77560 49555 8957
dis: 7.45062 10.1945 10.2946 10.4764 10.5604
query 1: 1235 50589 12589 165633 62664
dis: 8.38056 10.6488 11.1207 11.2538 11.6082
query 2: 1236 20610 126158 34606 79636
dis: 8.16755 10.3637 10.7037 10.9845 11.3439
query 3: 1237 115788 35526 135502 43453
dis: 8.15274 9.97176 10.5219 10.6314 10.7012
query 4: 1238 191682 27898 42576 56261
dis: 6.71965 9.48385 9.54837 9.5551 9.60369
query 5: 1239 53329 149098 157864 165313
dis: 8.09581 9.82387 10.499 10.5193 10.6242
query 6: 1240 56902 66408 197456 112100
dis: 8.48092 10.9467 11.1208 11.3602 11.6329
query 7: 1241 177324 184128 6840 77244
dis: 7.59753 10.286 11.0136 11.0341 11.1539
query 8: 1242 12357 73940 87457 7067
dis: 7.73664 10.6835 11.0734 11.1224 11.1892
note that the nearest neighbor is not at distance 0 due to quantization errors
问题
问题1:CMP0079 政策限制
原报错:
CMake Error at faiss/gpu/CMakeLists.txt:285 (target_link_libraries):
Attempt to add link library "CUDA::cudart" to target "faiss" which is not
built in this directory.
This is allowed only when policy CMP0079 is set to NEW.
解决:
CMakeList中加入
cmake_policy(SET CMP0079 NEW)
问题2:Cannot find source file GpuCloner.cpp
原报错:
CMake Error at faiss/CMakeLists.txt:232 (add_library):
Cannot find source file:
GpuCloner.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
.hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
解决:
GpuCloner.cpp 所属位置在faiss/gpu下
cmake没有找到目录 ,重建拉取faiss 1.7.3版本
问题3:undefined reference to `dgemm_’
原报错:
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `dgemm_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `dgetri_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `dgetrf_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `ssyrk_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sgetrf_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sgelsd_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sgemm_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `dgesvd_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `dsyev_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sgeqrf_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sorgqr_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sgesvd_'
/usr/bin/ld: /usr/local/lib/libfaiss.so: undefined reference to `sgetri_'
解决:
mkl 配置问题 说明在编译时就没有找到,
1. 未安装
按照上面教程安装
2. 安装了
1. 请看是否出现了问题4的情况。
2. 请重点检查mkl环境配置 或 根据查看官方的findmkl.cmake是怎么样的查找方式联合判断
3. source /opt/intel/oneapi/setvars.sh intel64 试试这个命令上面有提到
问题4: Targets may link only to libraries
原报错:
WARNING: Target "faiss_avx2" requests linking to directory "/opt/intel/mkl/lib/intel64". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "faiss" requests linking to directory "/opt/intel/mkl/lib/intel64". Targets may link only to libraries. CMake is dropping the item.
解决:
cmake时没有连接到应用目录下方的动态库。 官方bug 此错误可能会引起 错误3
请设置环境变量
export MKL_ROOT=/opt/intel/oneapi/mkl/2021.0
问题5:not found #include “driver_types.h”
原报错:
/usr/include/cublas_api.h:72:10: fatal error: driver_types.h: 没有那个文件或目录
72 | #include "driver_types.h"
| ^~~~~~~~~~~~~~~~
解决:
此头文件为cuda的头文件,需要将cuda添加到系统变量里。
export CPLUS_INCLUDE_PATH=/usr/local/cuda-11.1/targets/x86_64-linux/include:$CPLUS_INCLUDE_PATH
问题6: stack smashing detected
原报错:
[3.099 s] Generating 100000 vectors in 128D for training
[3.383 s] Training the index
*** stack smashing detected ***: terminated
已放弃 (核心已转储)
解决:
来自官方1.7.3版本的bug 新版本已修复。
更多推荐


所有评论(0)