2021-02-16 22:51:33 +08:00
# Installing Faiss via conda
2024-01-17 17:47:46 +08:00
The supported way to install Faiss is through [conda ](https://docs.conda.io ).
2021-02-16 22:51:33 +08:00
Stable releases are pushed regularly to the pytorch conda channel, as well as
pre-release nightly builds.
2017-02-23 06:26:44 +08:00
2024-10-09 23:26:22 +08:00
- The CPU-only faiss-cpu conda package is currently available on Linux (x86-64 and aarch64), OSX (arm64 only), and Windows (x86-64)
- faiss-gpu, containing both CPU and GPU indices, is available on Linux (x86-64 only) for CUDA 11.4 and 12.1
- faiss-gpu-raft containing both CPU and GPU indices provided by NVIDIA RAFT, is available on Linux (x86-64 only) for CUDA 11.8 and 12.1.
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
To install the latest stable release:
2018-03-14 01:10:44 +08:00
2021-02-16 22:51:33 +08:00
``` shell
# CPU-only version
2024-10-09 23:26:22 +08:00
$ conda install -c pytorch faiss-cpu=1.9.0
2018-04-05 16:12:32 +08:00
2021-02-16 22:51:33 +08:00
# GPU(+CPU) version
2024-10-09 23:26:22 +08:00
$ conda install -c pytorch -c nvidia faiss-gpu=1.9.0
2024-03-04 19:07:49 +08:00
# GPU(+CPU) version with NVIDIA RAFT
2024-10-09 23:26:22 +08:00
$ conda install -c pytorch -c nvidia -c rapidsai -c conda-forge faiss-gpu-raft=1.9.0
2024-09-17 08:10:53 +08:00
# GPU(+CPU) version using AMD ROCm not yet available
2018-03-14 01:10:44 +08:00
```
2019-04-05 17:50:39 +08:00
2024-03-04 19:07:49 +08:00
For faiss-gpu, the nvidia channel is required for CUDA, which is not
2023-06-20 02:47:31 +08:00
published in the main anaconda channel.
2024-03-04 19:07:49 +08:00
For faiss-gpu-raft, the nvidia, rapidsai and conda-forge channels are required.
2023-06-20 02:47:31 +08:00
2024-03-04 19:07:49 +08:00
Nightly pre-release packages can be installed as follows:
2021-02-16 22:51:33 +08:00
``` shell
# CPU-only version
$ conda install -c pytorch/label/nightly faiss-cpu
2018-03-14 01:10:44 +08:00
2021-03-08 16:31:08 +08:00
# GPU(+CPU) version
2024-10-09 23:26:22 +08:00
$ conda install -c pytorch/label/nightly -c nvidia faiss-gpu=1.9.0
2024-03-04 19:07:49 +08:00
# GPU(+CPU) version with NVIDIA RAFT
2024-10-09 23:26:22 +08:00
conda install -c pytorch -c nvidia -c rapidsai -c conda-forge faiss-gpu-raft=1.9.0 pytorch pytorch-cuda numpy
2024-09-17 08:10:53 +08:00
# GPU(+CPU) version using AMD ROCm not yet available
2021-02-16 22:51:33 +08:00
```
2024-03-04 19:07:49 +08:00
In the above commands, pytorch-cuda=11 or pytorch-cuda=12 would select a specific CUDA version, if it’ s required.
2018-03-14 01:10:44 +08:00
2024-05-15 21:24:23 +08:00
A combination of versions that installs GPU Faiss with CUDA and Pytorch (as of 2024-05-15):
2022-12-01 00:25:30 +08:00
```
2024-03-04 19:07:49 +08:00
conda create --name faiss_1.8.0
conda activate faiss_1.8.0
2024-05-15 21:24:23 +08:00
conda install -c pytorch -c nvidia faiss-gpu=1.8.0 pytorch=*=*cuda* pytorch-cuda=11 numpy
2022-12-01 00:25:30 +08:00
```
2021-03-08 16:31:08 +08:00
## Installing from conda-forge
Faiss is also being packaged by [conda-forge ](https://conda-forge.org/ ), the
community-driven packaging ecosystem for conda. The packaging effort is
collaborating with the Faiss team to ensure high-quality package builds.
Due to the comprehensive infrastructure of conda-forge, it may even happen that
certain build combinations are supported in conda-forge that are not available
through the pytorch channel. To install, use
``` shell
# CPU version
$ conda install -c conda-forge faiss-cpu
# GPU version
$ conda install -c conda-forge faiss-gpu
2024-09-17 08:10:53 +08:00
# AMD ROCm version not yet available
2021-03-08 16:31:08 +08:00
```
You can tell which channel your conda packages come from by using `conda list` .
If you are having problems using a package built by conda-forge, please raise
an [issue ](https://github.com/conda-forge/faiss-split-feedstock/issues ) on the
conda-forge package "feedstock".
2021-02-16 22:51:33 +08:00
# Building from source
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
Faiss can be built from source using CMake.
2017-02-23 06:26:44 +08:00
2024-10-09 23:26:22 +08:00
Faiss is supported on x86-64 machines on Linux, OSX, and Windows. It has been
2021-02-16 22:51:33 +08:00
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-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
The basic requirements are:
2023-06-14 20:06:15 +08:00
- a C++17 compiler (with support for OpenMP support version 2 or higher),
2024-01-17 17:47:46 +08:00
- a BLAS implementation (on Intel machines we strongly recommend using Intel MKL for best
2021-02-16 22:51:33 +08:00
performance).
2017-03-24 01:25:27 +08:00
2021-02-16 22:51:33 +08:00
The optional requirements are:
- for GPU indices:
- nvcc,
- the CUDA toolkit,
2024-09-17 08:10:53 +08:00
- for AMD GPUs:
- AMD ROCm,
2021-02-16 22:51:33 +08:00
- for the python bindings:
- python 3,
- numpy,
- and swig.
2018-02-26 18:23:17 +08:00
2021-02-16 22:51:33 +08:00
Indications for specific configurations are available in the [troubleshooting
section of the wiki](https://github.com/facebookresearch/faiss/wiki/Troubleshooting).
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
## Step 1: invoking CMake
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ cmake -B build .
2020-08-15 06:01:43 +08:00
```
2021-02-16 22:51:33 +08:00
This generates the system-dependent configuration/build files in the `build/`
subdirectory.
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
Several options can be passed to CMake, among which:
- general options:
- `-DFAISS_ENABLE_GPU=OFF` in order to disable building GPU indices (possible
values are `ON` and `OFF` ),
- `-DFAISS_ENABLE_PYTHON=OFF` in order to disable building python bindings
(possible values are `ON` and `OFF` ),
2024-11-15 03:30:16 +08:00
- `-DFAISS_ENABLE_CUVS=ON` in order to enable building the cuVS implementations
2023-05-04 04:13:45 +08:00
of the IVF-Flat and IVF-PQ GPU-accelerated indices (default is `OFF` , possible
values are `ON` and `OFF` )
2021-02-16 22:51:33 +08:00
- `-DBUILD_TESTING=OFF` in order to disable building C++ tests,
- `-DBUILD_SHARED_LIBS=ON` in order to build a shared library (possible values
are `ON` and `OFF` ),
2023-03-24 23:16:47 +08:00
- `-DFAISS_ENABLE_C_API=ON` in order to enable building [C API ](c_api/INSTALL.md ) (possible values
2023-06-20 02:47:31 +08:00
are `ON` and `OFF` ),
2021-02-16 22:51:33 +08:00
- optimization-related options:
- `-DCMAKE_BUILD_TYPE=Release` in order to enable generic compiler
optimization options (enables `-O3` on gcc for instance),
- `-DFAISS_OPT_LEVEL=avx2` in order to enable the required compiler flags to
2024-10-16 00:21:28 +08:00
generate code using optimized SIMD/Vector instructions. Possible values are below:
- On x86-64, `generic` , `avx2` and `avx512` , by increasing order of optimization,
- On aarch64, `generic` and `sve` , by increasing order of optimization,
2024-08-28 04:36:53 +08:00
- `-DFAISS_USE_LTO=ON` in order to enable [Link-Time Optimization ](https://en.wikipedia.org/wiki/Link-time_optimization ) (default is `OFF` , possible values are `ON` and `OFF` ).
2021-02-16 22:51:33 +08:00
- BLAS-related options:
- `-DBLA_VENDOR=Intel10_64_dyn -DMKL_LIBRARIES=/path/to/mkl/libs` to use the
Intel MKL BLAS implementation, which is significantly faster than OpenBLAS
(more information about the values for the `BLA_VENDOR` option can be found in
the [CMake docs ](https://cmake.org/cmake/help/latest/module/FindBLAS.html )),
- GPU-related options:
- `-DCUDAToolkit_ROOT=/path/to/cuda-10.1` in order to hint to the path of
the CUDA toolkit (for more information, see
[CMake docs ](https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html )),
- `-DCMAKE_CUDA_ARCHITECTURES="75;72"` for specifying which GPU architectures
to build against (see [CUDA docs ](https://developer.nvidia.com/cuda-gpus ) to
determine which architecture(s) you should pick),
2024-09-17 08:10:53 +08:00
- `-DFAISS_ENABLE_ROCM=ON` in order to enable building GPU indices for AMD GPUs.
2024-10-19 06:42:10 +08:00
`-DFAISS_ENABLE_GPU` must be `ON` when using this option. (possible values are `ON` and `OFF` ),
2021-02-16 22:51:33 +08:00
- python-related options:
- `-DPython_EXECUTABLE=/path/to/python3.7` in order to build a python
interface for a different python than the default one (see
[CMake docs ](https://cmake.org/cmake/help/latest/module/FindPython.html )).
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
## Step 2: Invoking Make
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ make -C build -j faiss
```
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
This builds the C++ library (`libfaiss.a` by default, and `libfaiss.so` if
`-DBUILD_SHARED_LIBS=ON` was passed to CMake).
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
The `-j` option enables parallel compilation of multiple units, leading to a
faster build, but increasing the chances of running out of memory, in which case
it is recommended to set the `-j` option to a fixed value (such as `-j4` ).
2019-04-05 17:50:39 +08:00
2024-10-01 02:35:36 +08:00
If making use of optimization options, build the correct target before swigfaiss.
For AVX2:
``` shell
$ make -C build -j faiss_avx2
```
For AVX512:
``` shell
$ make -C build -j faiss_avx512
```
This will ensure the creation of neccesary files when building and installing the python package.
2021-02-16 22:51:33 +08:00
## Step 3: Building the python bindings (optional)
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ make -C build -j swigfaiss
$ (cd build/faiss/python & & python setup.py install)
```
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
The first command builds the python bindings for Faiss, while the second one
generates and installs the python package.
2017-02-23 06:26:44 +08:00
2024-10-01 02:35:36 +08:00
2021-02-16 22:51:33 +08:00
## Step 4: Installing the C++ library and headers (optional)
2017-03-05 06:07:29 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ make -C build install
```
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
This will make the compiled library (either `libfaiss.a` or `libfaiss.so` on
Linux) available system-wide, as well as the C++ headers. This step is not
needed to install the python package only.
2017-02-23 06:26:44 +08:00
2017-03-05 06:07:29 +08:00
2021-02-16 22:51:33 +08:00
## Step 5: Testing (optional)
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
### Running the C++ test suite
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
To run the whole test suite, make sure that `cmake` was invoked with
`-DBUILD_TESTING=ON` , and run:
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ make -C build test
```
2018-06-02 14:35:30 +08:00
2021-02-16 22:51:33 +08:00
### Running the python test suite
2018-06-02 14:35:30 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ (cd build/faiss/python & & python setup.py build)
$ PYTHONPATH="$(ls -d ./build/faiss/python/build/lib*/)" pytest tests/test_*.py
```
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
### Basic example
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
A basic usage example is available in
2021-09-02 00:13:29 +08:00
[`demos/demo_ivfpq_indexing.cpp` ](https://github.com/facebookresearch/faiss/blob/main/demos/demo_ivfpq_indexing.cpp ).
2020-12-16 06:58:53 +08:00
2021-02-16 22:51:33 +08:00
It creates 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.
It can be built with
``` shell
$ make -C build demo_ivfpq_indexing
2020-12-16 06:58:53 +08:00
```
2021-02-16 22:51:33 +08:00
and subsequently ran with
``` shell
$ ./build/demos/demo_ivfpq_indexing
2020-12-16 06:58:53 +08:00
```
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
### Basic GPU example
2019-04-05 17:50:39 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ make -C build demo_ivfpq_indexing_gpu
$ ./build/demos/demo_ivfpq_indexing_gpu
2020-12-16 06:58:53 +08:00
```
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
This produce the GPU code equivalent to the CPU `demo_ivfpq_indexing` . It also
shows how to translate indexes from/to a GPU.
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
### A real-life benchmark
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
A 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
2018-06-02 14:35:30 +08:00
Then compile and run the following (after ensuring you have installed faiss):
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ make -C build demo_sift1M
$ ./build/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.
2021-02-16 22:51:33 +08:00
### Real-life test
2017-02-23 06:26:44 +08:00
The following script extends the demo_sift1M test to several types of
2021-02-16 22:51:33 +08:00
indexes. This must be run from the root of the source directory for this
2018-02-08 14:35:21 +08:00
repository:
2017-08-31 12:27:53 +08:00
2021-02-16 22:51:33 +08:00
``` shell
$ mkdir tmp # graphs of the output will be written here
$ 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.
2021-02-16 22:51:33 +08:00
### Real-life test on GPU
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
The example above also runs on GPU. Edit `demos/demo_auto_tune.py` at line 100
with the values
2017-02-23 06:26:44 +08:00
2021-02-16 22:51:33 +08:00
``` python
2017-08-31 12:27:53 +08:00
keys_to_test = keys_gpu
use_gpu = True
```
and you can run
2021-02-16 22:51:33 +08:00
``` shell
$ python demos/demo_auto_tune.py
2017-08-31 12:27:53 +08:00
```
to test the GPU code.