2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
[//]: # "**********************************************************"
|
|
|
|
[//]: # "** INSTALL file for Faiss (Fair AI Similarity Search **"
|
|
|
|
[//]: # "**********************************************************"
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
INSTALL file for Faiss (Fair AI Similarity Search)
|
|
|
|
==================================================
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
The Faiss installation works in 3 steps, from easiest to most
|
|
|
|
involved:
|
|
|
|
|
|
|
|
1. compile the C++ core and examples
|
|
|
|
|
|
|
|
2. compile the Python interface
|
|
|
|
|
|
|
|
3. compile GPU part
|
|
|
|
|
|
|
|
Steps 2 and 3 depend on 1, but they are otherwise independent.
|
|
|
|
|
2018-02-23 23:44:31 +08:00
|
|
|
Alternatively, all 3 steps above can be run by building a Docker image (see
|
2018-02-14 17:05:08 +08:00
|
|
|
section "Docker instructions" below).
|
|
|
|
|
|
|
|
Alternatively, steps 1 and 3 can be built via the cmake scripts (see below).
|
|
|
|
|
2017-03-24 01:25:27 +08:00
|
|
|
|
2018-02-26 18:23:17 +08:00
|
|
|
It is also possible to build a pure C interface. This optional process is
|
|
|
|
described separately (please see the [C interface installation file](c_api/INSTALL.md))
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
General compilation instructions
|
|
|
|
================================
|
|
|
|
|
|
|
|
Faiss has been tested only on x86_64 machines on Linux and Mac OS.
|
|
|
|
|
|
|
|
Faiss is compiled via a Makefile. The system-dependent configuration
|
|
|
|
of the Makefile is in an include file, makefile.inc. The variables in
|
|
|
|
makefile.inc must be set by hand.
|
|
|
|
|
|
|
|
Faiss requires a C++ compiler that understands:
|
|
|
|
- the Intel intrinsics for SSE instructions
|
|
|
|
- the GCC intrinsic for the popcount instruction
|
|
|
|
- basic OpenMP
|
|
|
|
|
|
|
|
There are a few models for makefile.inc in the example_makefiles/
|
|
|
|
subdirectory. Copy the relevant one for your system and adjust to your
|
2017-08-31 12:27:53 +08:00
|
|
|
needs. There are also indications for specific configurations in the
|
2017-03-05 06:07:29 +08:00
|
|
|
troubleshooting section of the wiki.
|
|
|
|
|
|
|
|
https://github.com/facebookresearch/faiss/wiki/Troubleshooting
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Faiss comes as a .a archive, that can be linked with executables or
|
|
|
|
dynamic libraries (useful for the Python wrapper).
|
|
|
|
|
2017-03-05 06:07:29 +08:00
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
Step 1: Compiling the C++ Faiss
|
|
|
|
===============================
|
|
|
|
|
2018-02-14 17:05:08 +08:00
|
|
|
The CPU version of Faiss is written in C++11.
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
BLAS/Lapack
|
|
|
|
-----------
|
|
|
|
|
|
|
|
The only variables that need to be configured for the C++ Faiss are
|
|
|
|
the BLAS/Lapack flags (a linear aglebra software package). It needs a
|
|
|
|
flag telling whether BLAS/Lapack uses 32 or 64 bit integers and the
|
|
|
|
linking flags. Faiss uses the Fortran 77 interface of BLAS/Lapack and
|
|
|
|
thus does not need an include path.
|
|
|
|
|
|
|
|
There are several BLAS implementations, depending on the OS and
|
|
|
|
machine. To have reasonable performance, the BLAS library should be
|
|
|
|
multithreaded. See the example makefile.inc's for hints and examples
|
|
|
|
on how to set the flags.
|
|
|
|
|
|
|
|
To check that the link flags are correct, and verify whether the
|
|
|
|
implementation uses 32 or 64 bit integers, you can
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`make tests/test_blas`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
and run
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`./tests/test_blas`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Testing Faiss
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Once the proper BLAS flags are set, the library should compile
|
|
|
|
smoothly by running
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`make`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
A basic usage example is in
|
|
|
|
|
2018-02-23 23:44:31 +08:00
|
|
|
`demos/demo_ivfpq_indexing`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
it makes a small index, stores it and performs some searches. A normal
|
|
|
|
runtime is around 20s. With a fast machine and Intel MKL's BLAS it
|
|
|
|
runs in 2.5s.
|
|
|
|
|
|
|
|
A real-life benchmark
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
A bit longer example runs and evaluates Faiss on the SIFT1M
|
|
|
|
dataset. To run it, please download the ANN_SIFT1M dataset from
|
|
|
|
|
|
|
|
http://corpus-texmex.irisa.fr/
|
|
|
|
|
2018-02-08 14:35:21 +08:00
|
|
|
and unzip it to the subdirectory `sift1M` at the root of the source
|
|
|
|
directory for this repository.
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Then compile and run
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
2018-02-23 23:44:31 +08:00
|
|
|
make demos/demo_sift1M
|
|
|
|
demos/demo_sift1M
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
This is a demonstration of the high-level auto-tuning API. You can try
|
|
|
|
setting a different index_key to find the indexing structure that
|
|
|
|
gives the best performance.
|
|
|
|
|
|
|
|
|
|
|
|
Step 2: Compiling the Python interface
|
|
|
|
======================================
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
The Python interface is compiled with
|
2017-03-02 23:43:37 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`make py`
|
|
|
|
|
|
|
|
If you want to compile it for another python version than the default
|
|
|
|
Python 2.7, in particular Python 3, the PYTHONCFLAGS must be adjusted in
|
2017-03-03 14:43:55 +08:00
|
|
|
makefile.inc, see the examples.
|
|
|
|
|
2017-03-02 23:43:37 +08:00
|
|
|
How it works
|
|
|
|
------------
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
The Python interface is provided via SWIG (Simple Wrapper and
|
|
|
|
Interface Generator) and an additional level of manual wrappers (in faiss.py).
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
SWIG generates two wrapper files: a Python file (`swigfaiss.py`) and a
|
|
|
|
C++ file that must be compiled to a dynamic library (`_swigfaiss.so`). These
|
2017-03-02 23:53:44 +08:00
|
|
|
files are included in the repository, so running swig is only required when
|
|
|
|
the C++ headers of Faiss are changed.
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
The C++ compilation to the dynamic library requires to set:
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
- `SHAREDFLAGS`: system-specific flags to generate a dynamic library
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
- `PYTHONCFLAGS`: include flags for Python
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
See the example makefile.inc's on how to set the flags.
|
|
|
|
|
|
|
|
|
|
|
|
Testing the Python wrapper
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
Often, a successful compile does not mean that the library works,
|
|
|
|
because missing symbols are detected only at runtime. You should be
|
|
|
|
able to load the Faiss dynamic library:
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`python -c "import faiss"`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
In case of failure, it reports the first missing symbol. To see all
|
|
|
|
missing symbols (on Linux), use
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`ldd -r _swigfaiss.so`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Sometimes, problems (eg with BLAS libraries) appear only when actually
|
|
|
|
calling a BLAS function. A simple way to check this
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```python
|
2017-02-23 06:26:44 +08:00
|
|
|
python -c "import faiss, numpy
|
|
|
|
faiss.Kmeans(10, 20).train(numpy.random.rand(1000, 10).astype('float32'))"
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
Real-life test
|
|
|
|
--------------
|
|
|
|
|
|
|
|
The following script extends the demo_sift1M test to several types of
|
2018-02-08 14:35:21 +08:00
|
|
|
indexes. This must be run from the root of the source directory for this
|
|
|
|
repository:
|
2017-08-31 12:27:53 +08:00
|
|
|
|
|
|
|
```
|
2018-02-08 14:35:21 +08:00
|
|
|
mkdir tmp # graphs of the output will be written here
|
2018-02-23 23:44:31 +08:00
|
|
|
PYTHONPATH=. python demos/demo_auto_tune.py
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
It will cycle through a few types of indexes and find optimal
|
|
|
|
operating points. You can play around with the types of indexes.
|
|
|
|
|
|
|
|
|
|
|
|
Step 3: Compiling the GPU implementation
|
|
|
|
========================================
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
There is a GPU-specific Makefile in the `gpu/` directory. It depends on
|
2017-02-23 06:26:44 +08:00
|
|
|
the same ../makefile.inc for system-specific variables. You need
|
|
|
|
libfaiss.a from Step 1 for this to work.
|
|
|
|
|
|
|
|
The GPU version is a superset of the CPU version. In addition it
|
2018-02-14 17:05:08 +08:00
|
|
|
requires the cuda compiler and related libraries (Cublas)
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
See the example makefile on how to set the flags.
|
|
|
|
|
2017-03-03 14:31:32 +08:00
|
|
|
The nvcc-specific flags to pass to the compiler, based on your desired
|
|
|
|
compute capability. Only compute capability 3.5+ is supported. For
|
2017-04-25 09:21:47 +08:00
|
|
|
example, we enable by default:
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
|
|
|
-gencode arch=compute_35,code="compute_35"
|
|
|
|
-gencode arch=compute_52,code="compute_52"
|
|
|
|
-gencode arch=compute_60,code="compute_60"
|
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-04-25 09:21:47 +08:00
|
|
|
However, look at https://developer.nvidia.com/cuda-gpus to determine
|
|
|
|
what compute capability you need to use, and replace our gencode
|
|
|
|
specifications with the one(s) you need.
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
Most other flags are related to the C++11 compiler used by nvcc to
|
|
|
|
complile the actual C++ code. They are normally just transmitted by
|
|
|
|
nvcc, except some of them that are not recognized and that should be
|
|
|
|
escaped by prefixing them with -Xcompiler. Also link flags that are
|
|
|
|
prefixed with -Wl, should be passed with -Xlinker.
|
|
|
|
|
|
|
|
Then compile with
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`cd gpu; make`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
You may want to add `-j 10` to use 10 threads during compile.
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Testing the GPU implementation
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
Compile the example with
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`cd gpu; make test/demo_ivfpq_indexing_gpu`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
This produce the GPU code equivalent to the CPU
|
|
|
|
demo_ivfpq_indexing. It also shows how to translate indexed from/to
|
|
|
|
the GPU.
|
|
|
|
|
|
|
|
Compiling the Python interface with GPU support
|
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
Given step 2, adding support of the GPU from Python is quite
|
|
|
|
straightforward. Run
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`cd gpu; make py`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
The import is the same for the GPU version and the CPU-only
|
|
|
|
version.
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`python -c "import faiss"`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Faiss tries to load the GPU version first, and in case of failure,
|
|
|
|
loads the CPU-only version. To investigate more closely the cause of
|
|
|
|
a failure, you can run:
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`python -c "import _swigfaiss_gpu"`
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
Python example with GPU support
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
The auto-tuning example above also runs on the GPU. Edit
|
2018-02-23 23:44:31 +08:00
|
|
|
`demos/demo_auto_tune.py` at line 100 with the values
|
2017-08-31 12:27:53 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
keys_to_test = keys_gpu
|
|
|
|
use_gpu = True
|
|
|
|
```
|
|
|
|
|
|
|
|
and you can run
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
|
|
|
export PYTHONPATH=.
|
2018-02-23 23:44:31 +08:00
|
|
|
python demos/demo_auto_tune.py
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
to test the GPU code.
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-03-24 01:25:27 +08:00
|
|
|
|
|
|
|
Docker instructions
|
|
|
|
===================
|
|
|
|
|
|
|
|
For using GPU capabilities of Faiss, you'll need to run "nvidia-docker"
|
|
|
|
rather than "docker". Make sure that docker
|
|
|
|
(https://docs.docker.com/engine/installation/) and nvidia-docker
|
|
|
|
(https://github.com/NVIDIA/nvidia-docker) are installed on your system
|
|
|
|
|
|
|
|
To build the "faiss" image, run
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`nvidia-docker build -t faiss .`
|
2017-03-24 01:25:27 +08:00
|
|
|
|
2017-04-19 16:22:25 +08:00
|
|
|
or if you don't want/need to clone the sources, just run
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`nvidia-docker build -t faiss github.com/facebookresearch/faiss`
|
2017-04-19 16:22:25 +08:00
|
|
|
|
2017-03-24 01:25:27 +08:00
|
|
|
If you want to run the tests during the docker build, uncomment the
|
|
|
|
last 3 "RUN" steps in the Dockerfile. But you might want to run the
|
|
|
|
tests by yourself, so just run
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`nvidia-docker run -ti --name faiss faiss bash`
|
2017-03-24 01:25:27 +08:00
|
|
|
|
|
|
|
and run what you want. If you need a dataset (like sift1M), download it
|
|
|
|
inside the created container, or better, mount a directory from the host
|
|
|
|
|
|
|
|
nvidia-docker run -ti --name faiss -v /my/host/data/folder/ann_dataset/sift/:/opt/faiss/sift1M faiss bash
|
|
|
|
|
|
|
|
|
2018-01-22 23:31:26 +08:00
|
|
|
How to use Faiss in your own projects
|
2017-02-23 06:26:44 +08:00
|
|
|
=====================================
|
|
|
|
|
2017-03-03 14:31:32 +08:00
|
|
|
C++
|
2017-02-23 06:26:44 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
The makefile generates a static and a dynamic library
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
|
|
|
libfaiss.a
|
|
|
|
libfaiss.so (or libfaiss.dylib)
|
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-03-03 14:31:32 +08:00
|
|
|
the executable should be linked to one of these. If you use
|
2017-03-16 01:25:00 +08:00
|
|
|
the static version (.a), add the LDFLAGS used in the Makefile.
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
For binary-only distributions, the include files should be under
|
2018-02-14 17:05:08 +08:00
|
|
|
a `faiss/` directory, so that they can be included as
|
2017-03-21 19:14:03 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```c++
|
|
|
|
#include <faiss/IndexIVFPQ.h>
|
|
|
|
#include <faiss/gpu/GpuIndexFlat.h>
|
|
|
|
```
|
2017-03-21 19:14:03 +08:00
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
Python
|
|
|
|
------
|
|
|
|
|
2017-03-03 14:31:32 +08:00
|
|
|
To import Faiss in your own Python project, you need the files
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
|
|
|
faiss.py
|
|
|
|
swigfaiss.py / swigfaiss_gpu.py
|
|
|
|
_swigfaiss.so / _swigfaiss_gpu.so
|
|
|
|
```
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2017-03-03 14:31:32 +08:00
|
|
|
to be visible in the PYTHONPATH or in the current directory.
|
2017-02-23 06:26:44 +08:00
|
|
|
Then Faiss can be used in python with
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```python
|
|
|
|
import faiss
|
|
|
|
```
|
2017-05-02 17:04:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
CMake build instructions:
|
|
|
|
=========================
|
|
|
|
Alternatively, Faiss can be built via the experimental cmake scripts.
|
2017-08-31 12:27:53 +08:00
|
|
|
The installation process is similar to using Makefiles. After installing
|
2017-05-02 17:04:50 +08:00
|
|
|
the necessary dependencies (OpenBLAS, OpenMP, and CUDA, if BUILD_WITH_GPU
|
|
|
|
is enabled), the build process can be done by the following commands:
|
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
|
|
|
mkdir build
|
2018-01-11 16:21:48 +08:00
|
|
|
cd build
|
2017-08-31 12:27:53 +08:00
|
|
|
cmake ..
|
|
|
|
make # use -j to enable parallel build
|
|
|
|
```
|
2017-05-02 17:04:50 +08:00
|
|
|
|
|
|
|
Notes for build on Mac: The native compiler on Mac does not support OpenMP.
|
2017-08-31 12:27:53 +08:00
|
|
|
So to make it work on Mac, you have to install a new compiler using either
|
|
|
|
Macports or Homebrew. For example, after installing the compiler `g++-mp-6`
|
|
|
|
from Macports (`port install g++-mp-6`), you need to set the two flags
|
|
|
|
`CMAKE_CXX_COMPILER` and `CMAKE_C_COMPILER`:
|
2017-05-02 17:04:50 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
`cmake -DCMAKE_CXX_COMPILER=/opt/local/bin/g++-mp-6 -DCMAKE_C_COMPILER=/opt/local/bin/gcc-mp-6 ..`
|
2017-05-02 17:04:50 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
Similarly, you can use Homebrew to install clang++ (`brew install llvm`) and
|
|
|
|
then set the two flags to `/usr/local/opt/llvm/bin/clang++`.
|
2017-05-02 17:04:50 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
CMake supports the OpenBLAS and MKL implementations. CMake limitations: the python interface is
|
|
|
|
NOT supported at this point.
|
2017-05-02 17:04:50 +08:00
|
|
|
|
|
|
|
Use Faiss as a 3rd-party library: Using Faiss as a 3rd-party lib via CMake is easy.
|
2017-08-31 12:27:53 +08:00
|
|
|
If the parental project is also build via CMake, just add a line `add_subdirectory(faiss)`
|
|
|
|
in CMake where faiss is the sub-folder name. To link Faiss to your application, use
|
2017-05-02 17:04:50 +08:00
|
|
|
|
2017-08-31 12:27:53 +08:00
|
|
|
```
|
|
|
|
add_executable(my_app my_app.cpp)
|
|
|
|
target_link_libraries(my_app gpufaise faiss)
|
|
|
|
```
|