forked from benfred/implicit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
165 lines (136 loc) · 5.44 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
import glob
import io
import logging
import os.path
import platform
import sys
from setuptools import Extension, setup, find_packages
from cuda_setup import CUDA, build_ext
NAME = 'implicit'
VERSION = "0.4.0"
try:
from Cython.Build import cythonize
use_cython = True
except ImportError:
use_cython = False
is_dev = 'dev' in VERSION
if is_dev and not use_cython:
raise RuntimeError('Cython required to build dev version of %s.' % NAME)
use_openmp = True
def define_extensions(use_cython=False):
if sys.platform.startswith("win"):
# compile args from
# https://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx
compile_args = ['/O2', '/openmp']
link_args = []
else:
gcc = extract_gcc_binaries()
if gcc is not None:
rpath = '/usr/local/opt/gcc/lib/gcc/' + gcc[-1] + '/'
link_args = ['-Wl,-rpath,' + rpath]
else:
link_args = []
compile_args = ['-Wno-unused-function', '-Wno-maybe-uninitialized', '-O3', '-ffast-math']
if use_openmp:
compile_args.append("-fopenmp")
link_args.append("-fopenmp")
compile_args.append("-std=c++11")
link_args.append("-std=c++11")
src_ext = '.pyx' if use_cython else '.cpp'
modules = [Extension("implicit." + name,
[os.path.join("implicit", name + src_ext)],
language='c++',
extra_compile_args=compile_args, extra_link_args=link_args)
for name in ['_als', '_nearest_neighbours', 'bpr', 'lmf', 'evaluation']]
modules.append(Extension("implicit." + 'recommender_base',
[os.path.join("implicit", 'recommender_base' + src_ext),
os.path.join("implicit", 'topnc.cpp')],
language='c++',
extra_compile_args=compile_args, extra_link_args=link_args))
if CUDA:
modules.append(Extension("implicit.cuda._cuda",
[os.path.join("implicit", "cuda", "_cuda" + src_ext),
os.path.join("implicit", "cuda", "als.cu"),
os.path.join("implicit", "cuda", "bpr.cu"),
os.path.join("implicit", "cuda", "matrix.cu")],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
library_dirs=[CUDA['lib64']],
libraries=['cudart', 'cublas', 'curand'],
include_dirs=[CUDA['include'], '.']))
else:
print("Failed to find CUDA toolkit. Building without GPU acceleration.")
if use_cython:
return cythonize(modules)
else:
return modules
# set_gcc copied from glove-python project
# https://github.com/maciejkula/glove-python
def extract_gcc_binaries():
"""Try to find GCC on OSX for OpenMP support."""
patterns = ['/opt/local/bin/g++-mp-[0-9].[0-9]',
'/opt/local/bin/g++-mp-[0-9]',
'/usr/local/bin/g++-[0-9].[0-9]',
'/usr/local/bin/g++-[0-9]']
if platform.system() == 'Darwin':
gcc_binaries = []
for pattern in patterns:
gcc_binaries += glob.glob(pattern)
gcc_binaries.sort()
if gcc_binaries:
_, gcc = os.path.split(gcc_binaries[-1])
return gcc
else:
return None
else:
return None
def set_gcc():
"""Try to use GCC on OSX for OpenMP support."""
# For macports and homebrew
if platform.system() == 'Darwin':
gcc = extract_gcc_binaries()
if gcc is not None:
os.environ["CC"] = gcc
os.environ["CXX"] = gcc
else:
global use_openmp
use_openmp = False
logging.warning('No GCC available. Install gcc from Homebrew '
'using brew install gcc.')
set_gcc()
def read(file_name):
"""Read a text file and return the content as a string."""
file_path = os.path.join(os.path.dirname(__file__), file_name)
with io.open(file_path, encoding="utf-8") as f:
return f.read()
setup(
name=NAME,
version=VERSION,
description='Collaborative Filtering for Implicit Datasets',
long_description=read("README.md"),
long_description_content_type="text/markdown",
url='http://github.com/benfred/implicit/',
author='Ben Frederickson',
author_email='ben@benfrederickson.com',
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',
'Natural Language :: English',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Programming Language :: Cython',
'Operating System :: OS Independent',
'Topic :: Software Development :: Libraries :: Python Modules'],
keywords='Matrix Factorization, Implicit Alternating Least Squares, '
'Collaborative Filtering, Recommender Systems',
packages=find_packages(),
install_requires=['numpy', 'scipy>=0.16', 'tqdm>=4.27'],
setup_requires=["Cython>=0.24"],
ext_modules=define_extensions(use_cython),
cmdclass={'build_ext': build_ext},
test_suite="tests",
)