Skip to content

Instantly share code, notes, and snippets.

@maxime
Last active April 13, 2023 18:49
Show Gist options
  • Save maxime/ef3fae0c2cf255fcb5c123978ec70da5 to your computer and use it in GitHub Desktop.
Save maxime/ef3fae0c2cf255fcb5c123978ec70da5 to your computer and use it in GitHub Desktop.
Install Facebook FAISS on macOS High Sierra

Those instructions are working for me as of January 2017, 2018. Using Homebrew and maOS 10.13.2

Install xCode command line tools (if you don't have it already)

xcode-select --install

Install LLVM

brew install llvm

Make sure /usr/local/opt/llvm/bin is in your PATH

Install Openblas

brew install openblas

Install swig

Go into your conda environment and run

conda install swig

which swig should show the path to your swig install in your environment.

Clone FAISS

git clone https://github.com/facebookresearch/faiss.git

Patch FAISS

Open the IndexScalarQuantizer.cpp and comment out the include <malloc.h> instruction:

--- a/IndexScalarQuantizer.cpp
+++ b/IndexScalarQuantizer.cpp
@@ -11,7 +11,7 @@
 #include <cstdio>
 #include <algorithm>

-#include <malloc.h>
+//#include <malloc.h>

 #include <omp.h>

Create makefile.inc

Create a makefile.inc in the root directory of faiss with the following content:


# -*- makefile -*-

CC=/usr/local/opt/llvm/bin/clang++
CFLAGS=-fPIC -m64 -Wall -g -O3 -msse4 -mpopcnt -fopenmp -Wno-sign-compare -I/usr/local/opt/llvm/include -std=c++11 -I/usr/local/opt/openblas/include
LDFLAGS=-g -fPIC -fopenmp -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib -L/usr/local/opt/openblas/lib

# common mac flags
SHAREDEXT=dylib
SHAREDFLAGS=-Wl,-F. -bundle -undefined dynamic_lookup
FAISSSHAREDFLAGS=-dynamiclib

BLASCFLAGS=-DFINTEGER=int
BLASLDFLAGS=/usr/local/opt/openblas/lib/libopenblas.dylib

SWIGEXEC=$(shell which swig)

PYTHON_INC=$(shell python -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())")
NUMPY_INC=$(shell python -c "import numpy ; print(numpy.get_include())")
PYTHONCFLAGS=-I${PYTHON_INC} -I${NUMPY_INC}

Test if blas is well installed & linked

Go into the faiss directory and run:

make tests/test_blas

Then run:

./tests/test_blas

Compile FAISS

Run:

make

A basic usage example is in

tests/demo_ivfpq_indexing

Run in ~10s on a 2017 Macbook Pro

Install the Python interface

Run:

make py

and test:

python -c "import faiss"

In order to use faiss in your own code, you'll have the copy the files in faiss/python to your own project. See more details here: https://github.com/facebookresearch/faiss/blob/master/INSTALL.md#python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment