ubuntu 16.04 环境安装faiss( CPU和GPU)

主要是基于官网教程,在安装过程中有一些注意事项,在这里做一下说明(强烈推荐参考github给出的官方教程,非常详细,一步一步执行即可,链接https://github.com/facebookresearch/faiss/blob/master/INSTALL.md)

环境:ubunt 16.04,anaconda2(python2.7),gtx1050ti,cuda8.0 ,cudnn 6.0

首先是安装cuda8.0 和cudnn6.0,和其他所有的深度学习框架需要的一样,参考本博客其他教程

1,安装openblas

conda install openblas

2, 下载faiss,修改makefile.inc文件

  1. # 下载FAISS源码  
  2. git clone https://github.com/facebookresearch/faiss.git  
  3. # 进入FAISS源码目录  
  4. cd faiss  
  5. # 根据系统配置编译环境  
  6. cp example_makefiles/makefile.inc.Linux ./makefile.inc  

这里需要进到 faiss目录下的makefile.inc里手动修改,文件中默认使用的是centos,需要把centos部分注释掉,并取消ubuntu16.04的注释

这里给出 修改后的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.

# -*- makefile -*-
# tested on CentOS 7, Ubuntu 16 and Ubuntu 14, see below to adjust flags to distribution.

CC=gcc
CXX=g++

CFLAGS=-fPIC -m64 -Wall -g -O3 -mavx -msse4 -mpopcnt -fopenmp -Wno-sign-compare -fopenmp
CXXFLAGS=$(CFLAGS) -std=c++11
LDFLAGS=-g -fPIC  -fopenmp

# common linux flags
SHAREDEXT=so
SHAREDFLAGS=-shared
FAISSSHAREDFLAGS=-shared

##########################################################################
# Uncomment one of the 4 BLAS/Lapack implementation options
# below. They are sorted # from fastest to slowest (in our
# experiments).
##########################################################################

//这里可选intel MKL或者openblas,由于intel mkl安装比较复杂,这里我们选择使用openblas

#
# 1. Intel MKL  不做修改
#
# This is the fastest BLAS implementation we tested. Unfortunately it
# is not open-source and determining the correct linking flags is a
# nightmare. See
#
#   https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor
#
# The latest tested version is MLK 2017.0.098 (2017 Initial Release) and can
# be downloaded here:
#
#   https://registrationcenter.intel.com/en/forms/?productid=2558&licensetype=2
#
# The following settings are working if MLK is installed on its default folder:
#
# MKLROOT=/opt/intel/compilers_and_libraries/linux/mkl/
#
# BLASLDFLAGS=-Wl,--no-as-needed -L$(MKLROOT)/lib/intel64   -lmkl_intel_ilp64 \
# -lmkl_core -lmkl_gnu_thread -ldl -lpthread
#
# BLASCFLAGS=-DFINTEGER=long
#
# you may have to set the LD_LIBRARY_PATH=$MKLROOT/lib/intel64 at runtime.
# If at runtime you get the error:
#   Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.
# You may add set
#   LD_PRELOAD=$MKLROOT/lib/intel64/libmkl_core.so:$MKLROOT/lib/intel64/libmkl_sequential.so
# at runtime as well.


#
# 2. Openblas   这里只需要安装openblas即可
#
# The library contains both BLAS and Lapack. About 30% slower than MKL. Please see 
#   https://github.com/facebookresearch/faiss/wiki/Troubleshooting#slow-brute-force-search-with-openblas
# to fix performance problemes with OpenBLAS 
BLASCFLAGS=-DFINTEGER=int


# This is for Centos:注释掉centos部分
#BLASLDFLAGS?=/usr/lib64/libopenblas.so.0

#ubuntu16部分打开注释,下面第一句话在本文件中执行的时候会出错,我是直接拷贝出来执行的,在这里注释掉了

# for Ubuntu 16:  

#sudo apt-get install libopenblas-dev python-numpy python-dev
BLASLDFLAGS?=/usr/lib/libopenblas.so.0


# for Ubuntu 14:
# sudo apt-get install libopenblas-dev liblapack3 python-numpy python-dev
# BLASLDFLAGS?=/usr/lib/libopenblas.so.0 /usr/lib/lapack/liblapack.so.3.0
#
# 3. Atlas
#
# Automatically tuned linear algebra package. As the name indicates,
# it is tuned automatically for a give architecture, and in Linux
# distributions, it the architecture is typically indicated by the
# directory name, eg. atlas-sse3 = optimized for SSE3 architecture.
#
# BLASCFLAGS=-DFINTEGER=int
# BLASLDFLAGS=/usr/lib64/atlas-sse3/libptf77blas.so.3 /usr/lib64/atlas-sse3/liblapack.so
#
# 4. reference implementation
#
# This is just a compiled version of the reference BLAS
# implementation, that is not optimized at all.
#
# BLASCFLAGS=-DFINTEGER=int
# BLASLDFLAGS=/usr/lib64/libblas.so.3 /usr/lib64/liblapack.so.3.2
#

##########################################################################
# SWIG and Python flags
##########################################################################
# SWIG executable. This should be at least version 3.x
SWIGEXEC=swig  #这里需要安装swig,  sudo  apt-get install swig

# The Python include directories for a given python executable can
# typically be found with
#
# python -c "import distutils.sysconfig; print distutils.sysconfig.get_python_inc()"
# python -c "import numpy ; print numpy.get_include()"
#
# or, for Python 3, with
#
# python3 -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())"
# python3 -c "import numpy ; print(numpy.get_include())"
#

#这里根据你的anaconda2安装路径进行修改
PYTHONCFLAGS=-I/home/mm/anaconda2/include/python2.7/ -I/home/mm/anaconda2/lib/python2.7/site-packages/numpy/core/include/


###########################################################################
# Cuda GPU flags
###########################################################################


# root of the cuda 8 installation
CUDAROOT=/usr/local/cuda-8.0/

CUDACFLAGS=-I$(CUDAROOT)/include

NVCC=$(CUDAROOT)/bin/nvcc

NVCCFLAGS= $(CUDAFLAGS) \
   -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" \
   --std c++11 -lineinfo \
   -ccbin $(CXX) -DFAISS_USE_FLOAT16


# BLAS LD flags for nvcc (used to generate an executable)
# if BLASLDFLAGS contains several flags, each one may
# need to be prepended with -Xlinker
BLASLDFLAGSNVCC=-Xlinker $(BLASLDFLAGS)

# Same, but to generate a .so
BLASLDFLAGSSONVCC=-Xlinker  $(BLASLDFLAGS)


Step 1: Compiling the C++ Faiss

make tests/test_blas

./tests/test_blas

Testing Faiss

make

demos/demo_ivfpq_indexing

A real-life benchmark

  1. #编译安装  
  2. make  
  3. #下载数据集  
  4. wget ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz  
  5. tar -xzvf sift.tar.gz  
  6. # 转移数据集。  
  7. mv sift sift1M  
  8. # 编译运行  
  9. make tests/demo_sift1M  
  10. ./tests/demo_sift1M 

Step 2: Compiling the Python interface

make py


Testing the Python wrapper

python -c "import faiss"

ldd -r _swigfaiss.so

python -c "import faiss, numpy
faiss.Kmeans(10, 20).train(numpy.random.rand(1000, 10).astype('float32'))"

。。。。。



你可能感兴趣的:(ubuntu 16.04 环境安装faiss( CPU和GPU))