背景
不支持conda安装cuda9.2版本的faiss。faiss-1.5.1版本编译不成功。
步骤
- 下载faiss-1.5.0版本,解压并重命名为faiss
- 运行
./configure --prefix=/home/cyh/faiss_build/ --with-cuda=/usr/local/cuda-9.2 --with-python=python3
# prefix: build的路径
# with-cuda: 安装cuda的路径
# with-python: 指定python版本
得到makefile.inc
cuda9.0的makefile.inc
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
CXX = g++ -std=c++11
CXXCPP = g++ -std=c++11 -E
# TODO: Investigate the LAPACKE wrapper for LAPACK, which defines the correct
# type for FORTRAN integers.
CPPFLAGS = -DFINTEGER=int
CXXFLAGS = -fPIC -fopenmp -m64 -Wno-sign-compare -g -O3 -Wall -Wextra
CPUFLAGS = -msse4 -mpopcnt
LDFLAGS = -fopenmp
LIBS = -lopenblas
PYTHONCFLAGS = -I/usr/include/python3.5m -I/usr/include/python3.5m -I/home/cyh/.local/lib/python3.5/site-packages/numpy/core/include
NVCC = /usr/local/cuda-9.0/bin/nvcc
NVCCLDFLAGS = -L/usr/local/cuda-9.0/lib64
NVCCLIBS = -lcudart -lcublas -lcuda
CUDAROOT = /usr/local/cuda-9.0
CUDACFLAGS = -I/usr/local/cuda-9.0/include
NVCCFLAGS = -I $(CUDAROOT)/targets/x86_64-linux/include/ \
-Xcompiler -fPIC \
-Xcudafe --diag_suppress=unrecognized_attribute \
-gencode arch=compute_35,code="compute_35" \
-gencode arch=compute_52,code="compute_52" \
-gencode arch=compute_60,code="compute_60" \
-gencode arch=compute_61,code="compute_61" \
-lineinfo \
-ccbin $(CXX) -DFAISS_USE_FLOAT16
OS = $(shell uname -s)
SHAREDEXT = so
SHAREDFLAGS = -shared
ifeq ($(OS),Darwin)
SHAREDEXT = dylib
SHAREDFLAGS = -dynamiclib -undefined dynamic_lookup
endif
MKDIR_P = /bin/mkdir -p
PYTHON = python3
SWIG = swig
prefix ?= /home/cyh/faiss_build
exec_prefix ?= ${prefix}
libdir = ${exec_prefix}/lib
includedir = ${prefix}/include
cuda9.2的makefile.inc,可以看出NVCC和CUDA的相关变量都为空。
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
CXX = g++ -std=c++11
CXXCPP = g++ -std=c++11 -E
# TODO: Investigate the LAPACKE wrapper for LAPACK, which defines the correct
# type for FORTRAN integers.
CPPFLAGS = -DFINTEGER=int
CXXFLAGS = -fPIC -fopenmp -m64 -Wno-sign-compare -g -O3 -Wall -Wextra
CPUFLAGS = -msse4 -mpopcnt
LDFLAGS = -fopenmp
LIBS = -lopenblas
PYTHONCFLAGS = -I/usr/include/python3.5m -I/usr/include/python3.5m -I/home/cyh/.local/lib/python3.5/site-packages/numpy/core/include
NVCC =
NVCCLDFLAGS =
NVCCLIBS =
CUDAROOT =
CUDACFLAGS =
NVCCFLAGS = -I $(CUDAROOT)/targets/x86_64-linux/include/ \
-Xcompiler -fPIC \
-Xcudafe --diag_suppress=unrecognized_attribute \
-gencode arch=compute_35,code="compute_35" \
-gencode arch=compute_52,code="compute_52" \
-gencode arch=compute_60,code="compute_60" \
-gencode arch=compute_61,code="compute_61" \
-lineinfo \
-ccbin $(CXX) -DFAISS_USE_FLOAT16
OS = $(shell uname -s)
SHAREDEXT = so
SHAREDFLAGS = -shared
ifeq ($(OS),Darwin)
SHAREDEXT = dylib
SHAREDFLAGS = -dynamiclib -undefined dynamic_lookup
endif
MKDIR_P = /bin/mkdir -p
PYTHON = python3
SWIG =
prefix ?= /home/cyh/faiss_build
exec_prefix ?= ${prefix}
libdir = ${exec_prefix}/lib
includedir = ${prefix}/include
- cuda9.0直接运行
make -j
cuda9.2需要修改makefile.inc为
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
CXX = g++ -std=c++11
CXXCPP = g++ -std=c++11 -E
# TODO: Investigate the LAPACKE wrapper for LAPACK, which defines the correct
# type for FORTRAN integers.
CPPFLAGS = -DFINTEGER=int
CXXFLAGS = -fPIC -fopenmp -m64 -Wno-sign-compare -g -O3 -Wall -Wextra
CPUFLAGS = -msse4 -mpopcnt
LDFLAGS = -fopenmp
LIBS = -lopenblas
PYTHONCFLAGS = -I/usr/include/python3.5m -I/usr/include/python3.5m -I/home/cyh/faiss_build/include/faiss
NVCC = /usr/local/cuda-9.2/bin/nvcc
NVCCLDFLAGS = -L/usr/local/cuda-9.2/lib64
NVCCLIBS = -lcudart -lcublas -lcuda
CUDAROOT = /usr/local/cuda-9.2
CUDACFLAGS = -I/usr/local/cuda-9.2/include
NVCCFLAGS = -I $(CUDAROOT)/targets/x86_64-linux/include/ \
-Xcompiler -fPIC \
-Xcudafe --diag_suppress=unrecognized_attribute \
-gencode arch=compute_35,code="compute_35" \
-gencode arch=compute_52,code="compute_52" \
-gencode arch=compute_60,code="compute_60" \
-gencode arch=compute_61,code="compute_61" \
-gencode arch=compute_70,code="compute_70" \
-lineinfo \
-ccbin $(CXX) -DFAISS_USE_FLOAT16
OS = $(shell uname -s)
SHAREDEXT = so
SHAREDFLAGS = -shared
ifeq ($(OS),Darwin)
SHAREDEXT = dylib
SHAREDFLAGS = -dynamiclib -undefined dynamic_lookup
endif
MKDIR_P = /bin/mkdir -p
PYTHON = python3
SWIG =
prefix ?= /home/cyh/faiss_build
exec_prefix ?= ${prefix}
libdir = ${exec_prefix}/lib
includedir = ${prefix}/include
其中,cuda9.2需要改的部分:
NVCC = /usr/local/cuda-9.2/bin/nvcc
NVCCLDFLAGS = -L/usr/local/cuda-9.2/lib64
NVCCLIBS = -lcudart -lcublas -lcuda
CUDAROOT = /usr/local/cuda-9.2
CUDACFLAGS = -I/usr/local/cuda-9.2/include
NVCCFLAGS = -I $(CUDAROOT)/targets/x86_64-linux/include/ \
TitanV需要增加一行,增加计算能力
-gencode arch=compute_70,code="compute_70" \
然后再执行
make -j
- 执行
make install
- 安装python版本
如果需要更改python版本faiss安装路径,更改faiss/python/Makefile
install: build
$(PYTHON) setup.py install --prefix=/home/cyh/.local
# 增加 --perfix 安装路径
然后执行
make py
如果找不到xxx.h,更改makefile.inc中的PYTHONCFLAGS为
PYTHONCFLAGS = -I/usr/include/python3.5m -I/usr/include/python3.5m -I/home/cyh/faiss_build/include/faiss
# -I/home/cyh/faiss_build/include/faiss: -I$(faiss的build路径)/include/faiss
- 编译gpu相关组件
faiss/gpu/Makefile的48行少了一个\,更改为:
48 GpuIndexBinaryFlat.o \
执行
make -j
测试gpu,进入faiss/gpu/test
make demo_ivfpq_indexing_gpu
- python的gpu版本
进入faiss/python
make _swigfaiss_gpu.so
测试
python -c "import _swigfaiss_gpu"
如果编译没有报错,但是import报错了,可以关掉conda的路径试试