-
Notifications
You must be signed in to change notification settings - Fork 84
/
setup.py
executable file
·179 lines (144 loc) · 4.93 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Oliver Borchers
# For License information, see corresponding LICENSE file.
"""Template setup.py Read more on
https://docs.python.org/3.7/distutils/setupscript.html."""
import distutils
import itertools
import os
import platform
import shutil
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
NAME = "fse"
VERSION = "1.0.0"
DESCRIPTION = "Fast Sentence Embeddings for Gensim"
AUTHOR = "Oliver Borchers"
AUTHOR_EMAIL = "o.borchers@oxolo.com"
URL = "https://github.com/oborchers/Fast_Sentence_Embeddings"
LICENSE = "GPL-3.0"
REQUIRES_PYTHON = ">=3.6"
NUMPY_STR = "numpy >= 1.11.3"
CYTHON_STR = "Cython==0.29.23"
INSTALL_REQUIRES = [
NUMPY_STR,
"scipy >= 0.18.1",
"smart_open >= 1.5.0",
"scikit-learn >= 0.19.1",
"gensim>=4",
"wordfreq >= 2.2.1",
"huggingface-hub",
"psutil",
"dataclasses; python_version < '3.7'",
]
SETUP_REQUIRES = [NUMPY_STR]
c_extensions = {
"fse.models.average_inner": "fse/models/average_inner.c",
}
cpp_extensions = {}
def need_cython():
"""Return True if we need Cython to translate any of the extensions.
If the extensions have already been translated to C/C++, then we don"t need to
install Cython and perform the translation.
"""
expected = list(c_extensions.values()) + list(cpp_extensions.values())
return any([not os.path.isfile(f) for f in expected])
def make_c_ext(use_cython=False):
for module, source in c_extensions.items():
if use_cython:
source = source.replace(".c", ".pyx")
extra_args = []
# extra_args.extend(["-g", "-O0"]) # uncomment if optimization limiting crash info
yield Extension(
module,
sources=[source],
language="c",
extra_compile_args=extra_args,
)
def make_cpp_ext(use_cython=False):
extra_args = []
system = platform.system()
if system == "Linux":
extra_args.append("-std=c++11")
elif system == "Darwin":
extra_args.extend(["-stdlib=libc++", "-std=c++11"])
# extra_args.extend(["-g", "-O0"]) # uncomment if
# optimization limiting crash info
for module, source in cpp_extensions.items():
if use_cython:
source = source.replace(".cpp", ".pyx")
yield Extension(
module,
sources=[source],
language="c++",
extra_compile_args=extra_args,
extra_link_args=extra_args,
)
#
# We use use_cython=False here for two reasons:
#
# 1. Cython may not be available at this stage
# 2. The actual translation from Cython to C/C++ happens inside CustomBuildExt
#
ext_modules = list(
itertools.chain(make_c_ext(use_cython=False), make_cpp_ext(use_cython=False))
)
class CustomBuildExt(build_ext):
"""Custom build_ext action with bootstrapping.
We need this in order to use numpy and Cython in this script without importing them
at module level, because they may not be available yet.
"""
#
# http://stackoverflow.com/questions/19919905/how-to-bootstrap-numpy-installation-in-setup-py
#
def finalize_options(self):
build_ext.finalize_options(self)
# Prevent numpy from thinking it is still in its setup process:
# https://docs.python.org/2/library/__builtin__.html#module-__builtin__
__builtins__.__NUMPY_SETUP__ = False
import numpy
self.include_dirs.append(numpy.get_include())
if need_cython():
import Cython.Build
Cython.Build.cythonize(list(make_c_ext(use_cython=True)))
Cython.Build.cythonize(list(make_cpp_ext(use_cython=True)))
class CleanExt(distutils.cmd.Command):
description = "Remove C sources, C++ sources and binaries for gensim extensions"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
for root, dirs, files in os.walk("gensim"):
files = [
os.path.join(root, f)
for f in files
if os.path.splitext(f)[1] in (".c", ".cpp", ".so")
]
for f in files:
self.announce("removing %s" % f, level=distutils.log.INFO)
os.unlink(f)
if os.path.isdir("build"):
self.announce("recursively removing build", level=distutils.log.INFO)
shutil.rmtree("build")
cmdclass = {"build_ext": CustomBuildExt, "clean_ext": CleanExt}
if need_cython():
INSTALL_REQUIRES.append(CYTHON_STR)
SETUP_REQUIRES.append(CYTHON_STR)
setup(
name=NAME,
version=VERSION,
description=DESCRIPTION,
author=AUTHOR,
author_email=AUTHOR_EMAIL,
packages=find_packages(),
requires_python=REQUIRES_PYTHON,
install_requires=INSTALL_REQUIRES,
setup_requires=SETUP_REQUIRES,
ext_modules=ext_modules,
cmdclass=cmdclass,
zip_safe=False,
include_package_data=True,
)