diff --git a/Makefile b/Makefile index cf605bfa3..4c800c0f6 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,8 @@ depend: test: libfaiss.a py make -C tests run - PYTHONPATH=./python $(PYTHON) -m unittest discover tests/ -v + PYTHONPATH=./python/build/`ls python/build | grep lib` \ + $(PYTHON) -m unittest discover tests/ -v ############################# @@ -85,7 +86,7 @@ misc/test_blas: misc/test_blas.cpp # Python py: - $(MAKE) -C python + $(MAKE) -C python build .PHONY: all clean default demos install installdirs py test uninstall diff --git a/python/Makefile b/python/Makefile index 991a23060..c50f2874a 100644 --- a/python/Makefile +++ b/python/Makefile @@ -8,7 +8,7 @@ HEADERS = $(wildcard ../*.h) -all: cpu +all: cpu build ############################# # CPU @@ -43,9 +43,15 @@ _swigfaiss_gpu.so: swigfaiss_gpu_wrap.o ../gpu/libgpufaiss.a ../libfaiss.a $(CXX) $(SHAREDFLAGS) $(NVCCLDFLAGS) -o $@ $^ $(NVCCLIBS) +build: cpu + $(PYTHON) setup.py build + +install: build + $(PYTHON) setup.py install + clean: rm -f swigfaiss_wrap.cpp swigfaiss_gpu_wrap.cpp rm -f swigfaiss.py swigfaiss_gpu.py rm -f _swigfaiss.so _swigfaiss_gpu.so -.PHONY: all clean cpu gpu +.PHONY: all build clean cpu gpu install diff --git a/python/faiss.py b/python/faiss.py index 6cf833d12..b38cd4408 100644 --- a/python/faiss.py +++ b/python/faiss.py @@ -18,14 +18,14 @@ import pdb # we import * so that the symbol X can be accessed as faiss.X try: - from swigfaiss_gpu import * + from .swigfaiss_gpu import * except ImportError as e: if 'No module named' not in e.args[0]: # swigfaiss_gpu is there but failed to load: Warn user about it. sys.stderr.write("Failed to load GPU Faiss: %s\n" % e.args[0]) sys.stderr.write("Faiss falling back to CPU-only.\n") - from swigfaiss import * + from .swigfaiss import * ################################################################## diff --git a/python/setup.py b/python/setup.py new file mode 100644 index 000000000..304f9c73b --- /dev/null +++ b/python/setup.py @@ -0,0 +1,51 @@ +from __future__ import print_function +from setuptools import setup, find_packages +import os +import shutil + +here = os.path.abspath(os.path.dirname(__file__)) + +check_fpath = os.path.join("_swigfaiss.so") +if not os.path.exists(check_fpath): + print("Could not find {}".format(check_fpath)) + print("Have you run `make` and `make py` " + "(and optionally `cd gpu && make && make py && cd ..`)?") + +# make the faiss python package dir +shutil.rmtree("faiss", ignore_errors=True) +os.mkdir("faiss") +shutil.copyfile("faiss.py", "faiss/__init__.py") +shutil.copyfile("swigfaiss.py", "faiss/swigfaiss.py") +shutil.copyfile("_swigfaiss.so", "faiss/_swigfaiss.so") +try: + shutil.copyfile("_swigfaiss_gpu.so", "faiss/_swigfaiss_gpu.so") + shutil.copyfile("swigfaiss_gpu.py", "faiss/swigfaiss_gpu.py") +except: + pass + +long_description=""" +Faiss is a library for efficient similarity search and clustering of dense +vectors. It contains algorithms that search in sets of vectors of any size, + up to ones that possibly do not fit in RAM. It also contains supporting +code for evaluation and parameter tuning. Faiss is written in C++ with +complete wrappers for Python/numpy. Some of the most useful algorithms +are implemented on the GPU. It is developed by Facebook AI Research. +""" +setup( + name='faiss', + version='0.1', + description='A library for efficient similarity search and clustering of dense vectors', + long_description=long_description, + url='https://github.com/facebookresearch/faiss', + author='Matthijs Douze, Jeff Johnson, Herve Jegou', + author_email='matthijs@fb.com', + license='BSD', + keywords='search nearest neighbors', + + install_requires=['numpy'], + packages=['faiss'], + package_data={ + 'faiss': ['*.so'], + }, + +)