2017-08-30 21:27:53 -07:00
[//]: # "**********************************************************"
[//]: # "** INSTALL file for Faiss (Fair AI Similarity Search ** "
[//]: # "**********************************************************"
2017-02-22 23:26:44 +01:00
2017-08-30 21:27:53 -07:00
INSTALL file for Faiss (Fair AI Similarity Search)
==================================================
2017-02-22 23:26:44 +01:00
2018-03-13 13:10:44 -04:00
Install via Conda
-----------------
2019-04-05 11:50:39 +02:00
The easiest way to install FAISS is from Anaconda. We regularly push stable releases to the pytorch conda channel.
2018-04-05 01:12:32 -07:00
2019-04-05 11:50:39 +02:00
Currently we support faiss-cpu both on Linux and OSX. We also provide faiss-gpu compiled with CUDA8/CUDA9/CUDA10 on Linux systems.
2018-04-05 01:12:32 -07:00
2018-03-13 13:10:44 -04:00
You can easily install it by
```
# CPU version only
conda install faiss-cpu -c pytorch
2019-04-05 11:50:39 +02:00
# GPU version
conda install faiss-gpu cudatoolkit=8.0 -c pytorch # For CUDA8
conda install faiss-gpu cudatoolkit=9.0 -c pytorch # For CUDA9
conda install faiss-gpu cudatoolkit=10.0 -c pytorch # For CUDA10
2018-03-13 13:10:44 -04:00
```
Compile from source
-------------------
2019-04-05 11:50:39 +02:00
The Faiss compilation works in 2 steps:
2017-02-22 23:26:44 +01:00
1. compile the C++ core and examples
2. compile the Python interface
2019-04-05 11:50:39 +02:00
Steps 2 depends on 1.
2017-03-23 18:25:27 +01:00
2018-02-26 10:23:17 +00: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-22 23:26:44 +01:00
General compilation instructions
================================
2020-08-14 15:01:43 -07:00
TL;DR:
```bash
cmake -B build .
make -C build
2019-04-05 11:50:39 +02:00
2020-08-14 15:01:43 -07:00
# For the Python interface:
2020-08-22 22:31:06 -07:00
cd build/faiss/python & & python setup.py install
2020-08-14 15:01:43 -07:00
```
1. `cmake`
2019-04-05 11:50:39 +02:00
2020-08-14 15:01:43 -07:00
This generates the system-dependent configuration/build files.
2019-04-05 11:50:39 +02:00
A few useful options:
2020-08-14 15:01:43 -07:00
- `-DFAISS_ENABLE_GPU=OFF` in order to build the CPU part only.
- `-DCUDAToolkit_ROOT=/path/to/cuda-10.1` in order to hint to the path of
2019-04-05 11:50:39 +02:00
the cudatoolkit.
2020-08-14 15:01:43 -07:00
- `-DCMAKE_CUDA_ARCHITECTURES="75;72"` for specifying which GPU architectures to build against.
- `-DPython_EXECUTABLE=/path/to/python3.7` in order to build a python
2019-04-05 11:50:39 +02:00
interface for a different python than the default one.
2. `make`
This builds the C++ library (the whole library if a suitable cuda toolkit was
found, or the CPU part only otherwise).
3. `make install` (optional)
This installs the headers and libraries.
2017-02-22 23:26:44 +01:00
2020-08-14 15:01:43 -07:00
Faiss is supported on x86_64 machines on Linux and Mac OS.
2020-03-29 12:43:10 +02:00
It has been found to run on other platforms as well, see [other platforms ](https://github.com/facebookresearch/faiss/wiki/Related-projects#bindings-to-other-languages-and-porting-to-other-platforms )
2017-02-22 23:26:44 +01:00
Faiss requires a C++ compiler that understands:
2019-04-05 11:50:39 +02:00
- the Intel intrinsics for SSE instructions,
- the GCC intrinsic for the popcount instruction,
- basic OpenMP.
2017-02-22 23:26:44 +01:00
2020-08-14 15:01:43 -07:00
There are indications for specific configurations in the
2017-03-04 23:07:29 +01:00
troubleshooting section of the wiki.
https://github.com/facebookresearch/faiss/wiki/Troubleshooting
2017-02-22 23:26:44 +01:00
Faiss comes as a .a archive, that can be linked with executables or
dynamic libraries (useful for the Python wrapper).
2017-03-04 23:07:29 +01:00
2017-02-22 23:26:44 +01: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
2020-08-14 15:01:43 -07:00
multithreaded.
2017-02-22 23:26:44 +01:00
To check that the link flags are correct, and verify whether the
implementation uses 32 or 64 bit integers, you can
2020-08-14 15:01:43 -07:00
`make test_blas`
2017-02-22 23:26:44 +01:00
and run
2018-06-02 08:35:30 +02:00
`./misc/test_blas`
2017-02-22 23:26:44 +01:00
2018-06-02 08:35:30 +02:00
Testing Faiss
-------------
2017-02-22 23:26:44 +01:00
A basic usage example is in
2018-02-23 16:44:31 +01:00
`demos/demo_ivfpq_indexing`
2017-02-22 23:26:44 +01:00
2019-04-05 11:50:39 +02:00
which you can build by calling
2020-08-14 15:01:43 -07:00
`make demo_ivfpq_indexing`
2019-04-05 11:50:39 +02:00
It makes a small index, stores it and performs some searches. A normal
2017-02-22 23:26:44 +01:00
runtime is around 20s. With a fast machine and Intel MKL's BLAS it
runs in 2.5s.
2018-06-02 08:35:30 +02:00
To run the whole test suite:
2019-04-05 11:50:39 +02:00
`make test` (for the CPU part)
2018-06-02 08:35:30 +02:00
2017-02-22 23:26:44 +01:00
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 01:35:21 -05:00
and unzip it to the subdirectory `sift1M` at the root of the source
directory for this repository.
2017-02-22 23:26:44 +01:00
2018-06-02 08:35:30 +02:00
Then compile and run the following (after ensuring you have installed faiss):
2017-02-22 23:26:44 +01:00
2017-08-30 21:27:53 -07:00
```
2018-06-02 08:35:30 +02:00
make demos
./demos/demo_sift1M
2017-08-30 21:27:53 -07:00
```
2017-02-22 23:26:44 +01: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.
2020-08-14 15:01:43 -07:00
Python Interface
2017-03-02 16:43:37 +01:00
------------
2017-02-22 23:26:44 +01:00
The Python interface is provided via SWIG (Simple Wrapper and
2020-08-07 08:45:46 -07:00
Interface Generator) and an additional level of manual wrappers (in faiss/python/faiss.py).
2017-02-22 23:26:44 +01:00
2020-08-07 08:45:46 -07:00
SWIG generates two wrapper files: a Python file (`faiss/python/swigfaiss.py` ) and a
C++ file that must be compiled to a dynamic library (`faiss/python/_swigfaiss.so` ).
2017-02-22 23:26:44 +01:00
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-30 21:27:53 -07:00
`python -c "import faiss"`
2017-02-22 23:26:44 +01:00
In case of failure, it reports the first missing symbol. To see all
missing symbols (on Linux), use
2017-08-30 21:27:53 -07:00
`ldd -r _swigfaiss.so`
2017-02-22 23:26:44 +01:00
Sometimes, problems (eg with BLAS libraries) appear only when actually
calling a BLAS function. A simple way to check this
2017-08-30 21:27:53 -07:00
```python
2017-02-22 23:26:44 +01:00
python -c "import faiss, numpy
2020-08-07 11:09:27 -07:00
faiss.Kmeans(10, 20).train(numpy.random.rand(1000, 10).astype('float32'))"
2017-08-30 21:27:53 -07:00
```
2017-02-22 23:26:44 +01:00
Real-life test
--------------
The following script extends the demo_sift1M test to several types of
2018-02-08 01:35:21 -05:00
indexes. This must be run from the root of the source directory for this
repository:
2017-08-30 21:27:53 -07:00
```
2018-02-08 01:35:21 -05:00
mkdir tmp # graphs of the output will be written here
2018-02-23 16:44:31 +01:00
PYTHONPATH=. python demos/demo_auto_tune.py
2017-08-30 21:27:53 -07:00
```
2017-02-22 23:26:44 +01: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
========================================
The GPU version is a superset of the CPU version. In addition it
2018-02-14 10:05:08 +01:00
requires the cuda compiler and related libraries (Cublas)
2017-02-22 23:26:44 +01:00
2017-03-02 22:31:32 -08:00
The nvcc-specific flags to pass to the compiler, based on your desired
2020-08-14 15:01:43 -07:00
compute capability can be customized by providing the `CMAKE_CUDA_ARCHITECTURES`
flag to CMake. Only compute capability 3.5+ is supported. For example, we enable
2019-04-05 11:50:39 +02:00
by default:
2017-02-22 23:26:44 +01:00
2017-08-30 21:27:53 -07:00
```
2019-04-05 11:50:39 +02:00
-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
-gencode=arch=compute_75,code=compute_75
2017-08-30 21:27:53 -07:00
```
2017-02-22 23:26:44 +01:00
2017-04-24 21:21:47 -04: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-22 23:26:44 +01: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.
2017-08-30 21:27:53 -07:00
You may want to add `-j 10` to use 10 threads during compile.
2017-02-22 23:26:44 +01:00
Testing the GPU implementation
------------------------------
Compile the example with
2020-08-14 15:01:43 -07:00
`make demo_ivfpq_indexing_gpu`
2017-02-22 23:26:44 +01:00
This produce the GPU code equivalent to the CPU
demo_ivfpq_indexing. It also shows how to translate indexed from/to
the GPU.
Python example with GPU support
-------------------------------
The auto-tuning example above also runs on the GPU. Edit
2018-02-23 16:44:31 +01:00
`demos/demo_auto_tune.py` at line 100 with the values
2017-08-30 21:27:53 -07:00
```python
keys_to_test = keys_gpu
use_gpu = True
```
and you can run
2017-02-22 23:26:44 +01:00
2017-08-30 21:27:53 -07:00
```
export PYTHONPATH=.
2018-02-23 16:44:31 +01:00
python demos/demo_auto_tune.py
2017-08-30 21:27:53 -07:00
```
2017-02-22 23:26:44 +01:00
2017-08-30 21:27:53 -07:00
to test the GPU code.
2017-02-22 23:26:44 +01:00
2017-03-23 18:25:27 +01: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-30 21:27:53 -07:00
`nvidia-docker build -t faiss .`
2017-03-23 18:25:27 +01:00
2017-04-19 10:22:25 +02:00
or if you don't want/need to clone the sources, just run
2017-08-30 21:27:53 -07:00
`nvidia-docker build -t faiss github.com/facebookresearch/faiss`
2017-04-19 10:22:25 +02:00
2017-03-23 18:25:27 +01: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-30 21:27:53 -07:00
`nvidia-docker run -ti --name faiss faiss bash`
2017-03-23 18:25:27 +01: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 15:31:26 +00:00
How to use Faiss in your own projects
2017-02-22 23:26:44 +01:00
=====================================
2017-03-02 22:31:32 -08:00
C++
2017-02-22 23:26:44 +01:00
---
The makefile generates a static and a dynamic library
2017-08-30 21:27:53 -07:00
```
libfaiss.a
libfaiss.so (or libfaiss.dylib)
```
2017-02-22 23:26:44 +01:00
2017-03-02 22:31:32 -08:00
the executable should be linked to one of these. If you use
2017-03-15 13:25:00 -04:00
the static version (.a), add the LDFLAGS used in the Makefile.
2017-02-22 23:26:44 +01:00
2019-04-05 11:50:39 +02:00
For binary-only distributions, the headers should be under
2018-02-14 10:05:08 +01:00
a `faiss/` directory, so that they can be included as
2017-03-21 12:14:03 +01:00
2017-08-30 21:27:53 -07:00
```c++
#include <faiss/IndexIVFPQ.h>
#include <faiss/gpu/GpuIndexFlat.h>
```
2017-03-21 12:14:03 +01:00
2017-02-22 23:26:44 +01:00
Python
------
2017-03-02 22:31:32 -08:00
To import Faiss in your own Python project, you need the files
2017-02-22 23:26:44 +01:00
2017-08-30 21:27:53 -07:00
```
2019-04-05 11:50:39 +02:00
__init__.py
swigfaiss.py
_swigfaiss.so
2017-08-30 21:27:53 -07:00
```
2019-04-05 11:50:39 +02:00
to be present in a `faiss/` directory visible in the PYTHONPATH or in the
current directory.
2017-02-22 23:26:44 +01:00
Then Faiss can be used in python with
2017-08-30 21:27:53 -07:00
```python
import faiss
```