forked from kyamagu/faiss-wheels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
227 lines (208 loc) · 6.72 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import os
import sys
from setuptools import setup
from setuptools.command.build_py import build_py
from setuptools.extension import Extension
NAME = 'faiss-cpu'
VERSION = '1.7.4'
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. It is developed by Facebook AI Research.
"""
# CMake variables for faiss
FAISS_ROOT = os.getenv('FAISS_ROOT', 'faiss')
FAISS_INCLUDE = os.getenv('FAISS_INCLUDE', os.path.join('/usr/local/include'))
FAISS_LDFLAGS = os.getenv('FAISS_LDFLAGS')
FAISS_OPT_LEVEL = os.getenv('FAISS_OPT_LEVEL', 'generic')
FAISS_ENABLE_GPU = (
os.getenv('FAISS_ENABLE_GPU', '').lower() in ('on', 'true')
)
class get_numpy_include(object):
"""
Helper class to determine the numpy include path.
The purpose of this class is to postpone importing numpy
until it is actually installed, so that the ``get_include()``
method can be invoked.
"""
def __str__(self):
import numpy as np
return np.get_include()
# Platform-specific configurations
DEFINE_MACROS = [
('FINTEGER', 'int'),
]
INCLUDE_DIRS = [
get_numpy_include(),
FAISS_INCLUDE,
FAISS_ROOT,
]
LIBRARY_DIRS = []
EXTRA_COMPILE_ARGS = []
EXTRA_LINK_ARGS = FAISS_LDFLAGS.split() if FAISS_LDFLAGS is not None else []
SWIG_OPTS = [
'-c++',
'-Doverride=',
'-I' + FAISS_INCLUDE,
'-I' + FAISS_ROOT,
'-doxygen',
]
if sys.platform == 'win32':
EXTRA_COMPILE_ARGS += [
'/openmp',
'/std:c++17',
'/Zc:inline',
'/wd4101', # unreferenced local variable.
'/MD', # Bugfix: https://bugs.python.org/issue38597
]
EXTRA_LINK_ARGS += [
'/OPT:ICF',
'/OPT:REF',
]
if FAISS_LDFLAGS is None:
EXTRA_LINK_ARGS += [
'faiss.lib',
'openblas.lib',
]
SWIG_OPTS += ['-DSWIGWIN']
elif sys.platform == 'linux':
EXTRA_COMPILE_ARGS += [
'-std=c++11',
'-Wno-sign-compare',
'-fopenmp',
'-fdata-sections',
'-ffunction-sections',
]
EXTRA_LINK_ARGS += [
'-fopenmp',
'-lrt',
'-s',
'-Wl,--gc-sections',
]
if FAISS_LDFLAGS is None:
EXTRA_LINK_ARGS += [
'-L/usr/local/lib',
'-l:libfaiss.a',
'-l:libopenblas.a',
'-lgfortran',
]
if FAISS_ENABLE_GPU:
EXTRA_LINK_ARGS += [
'-lcublas_static',
'-lcublasLt_static',
'-lcudart_static',
'-lculibos'
]
SWIG_OPTS += ['-DSWIGWORDSIZE64']
elif sys.platform == 'darwin':
EXTRA_COMPILE_ARGS += [
'-std=c++11',
'-Wno-sign-compare',
'-Xpreprocessor',
'-fopenmp',
]
EXTRA_LINK_ARGS += [
'-Xpreprocessor',
'-fopenmp',
'-dead_strip',
]
if FAISS_LDFLAGS is None:
EXTRA_LINK_ARGS += [
'-L/usr/local/lib',
'-lfaiss',
'-lomp',
'-framework',
'Accelerate',
]
if FAISS_ENABLE_GPU:
NAME = 'faiss-gpu'
CUDA_HOME = os.getenv('CUDA_HOME', '/usr/local/cuda')
INCLUDE_DIRS += [os.path.join(CUDA_HOME, 'include')]
LIBRARY_DIRS += [os.path.join(CUDA_HOME, 'lib64')]
SWIG_OPTS += ['-I' + os.path.join(CUDA_HOME, 'include'), '-DGPU_WRAPPER']
class CustomBuildPy(build_py):
"""Run build_ext before build_py to compile swig code."""
def run(self):
self.run_command("build_ext")
return build_py.run(self)
_swigfaiss = Extension(
'faiss._swigfaiss',
sources=[
os.path.join(FAISS_ROOT, 'faiss', 'python', 'swigfaiss.i'),
os.path.join(FAISS_ROOT, 'faiss', 'python', 'python_callbacks.cpp'),
],
depends=[os.path.join(FAISS_ROOT, 'faiss', 'python', 'python_callbacks.h')],
language='c++',
define_macros=DEFINE_MACROS,
include_dirs=INCLUDE_DIRS,
library_dirs=LIBRARY_DIRS,
extra_compile_args=EXTRA_COMPILE_ARGS,
extra_link_args=EXTRA_LINK_ARGS,
swig_opts=SWIG_OPTS + ["-module", "swigfaiss"],
)
ext_modules = [_swigfaiss]
if FAISS_OPT_LEVEL == 'avx2':
# NOTE: avx2 is only available on x86_64 arch.
if sys.platform == 'win32':
EXTRA_COMPILE_ARGS_AVX2 = EXTRA_COMPILE_ARGS + ['/arch:AVX2']
else:
EXTRA_COMPILE_ARGS_AVX2 = EXTRA_COMPILE_ARGS + ['-mavx2', '-mpopcnt']
# TODO: fix this ad-hoc approach to specify avx2 lib.
EXTRA_LINK_ARGS_AVX2 = [x.replace("faiss", "faiss_avx2") for x in EXTRA_LINK_ARGS]
_swigfaiss_avx2 = Extension(
'faiss._swigfaiss_avx2',
sources=[
os.path.join(FAISS_ROOT, 'faiss', 'python', 'swigfaiss.i'),
os.path.join(FAISS_ROOT, 'faiss', 'python', 'python_callbacks.cpp'),
],
depends=[os.path.join(FAISS_ROOT, 'faiss', 'python', 'python_callbacks.h')],
language='c++',
define_macros=DEFINE_MACROS,
include_dirs=INCLUDE_DIRS,
library_dirs=LIBRARY_DIRS,
extra_compile_args=EXTRA_COMPILE_ARGS_AVX2,
extra_link_args=EXTRA_LINK_ARGS_AVX2,
swig_opts=SWIG_OPTS + ["-module", "swigfaiss_avx2"],
)
ext_modules.append(_swigfaiss_avx2)
setup(
name=NAME,
version=VERSION,
description=(
'A library for efficient similarity search and clustering of dense '
'vectors.'
),
long_description=LONG_DESCRIPTION,
url='https://github.com/kyamagu/faiss-wheels',
author='Kota Yamaguchi',
author_email='[email protected]',
license='MIT',
keywords='search nearest neighbors',
install_requires=['numpy'],
setup_requires=['numpy'],
packages=['faiss', 'faiss.contrib'],
package_dir={
'faiss': os.path.join(FAISS_ROOT, 'faiss', 'python'),
'faiss.contrib': os.path.join(FAISS_ROOT, 'contrib'),
},
package_data={'faiss': ['*.i', '*.h']},
ext_modules=ext_modules,
cmdclass={'build_py': CustomBuildPy},
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
],
)