-
Notifications
You must be signed in to change notification settings - Fork 179
/
setup.py
231 lines (208 loc) · 7.48 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
228
229
230
231
#!/usr/bin/env python
#
# Copyright (c) 2020 Idiap Research Institute, http://www.idiap.ch/
# Written by Angelos Katharopoulos <[email protected]>,
# Apoorv Vyas <[email protected]>
#
"""Setup fast transformers"""
from functools import lru_cache
from itertools import dropwhile
import os
from os import path
from setuptools import find_packages, setup
from subprocess import DEVNULL, call
import sys
try:
import torch
from torch.utils.cpp_extension import BuildExtension, CppExtension
except ImportError as e:
raise ImportError(
("PyTorch is required to install pytorch-fast-transformers. Please "
"install your favorite version of PyTorch, we support 1.3.1, 1.5.0 "
"and >=1.6"),
name=e.name,
path=e.path
) from e
@lru_cache(None)
def cuda_toolkit_available():
try:
call(["nvcc"], stdout=DEVNULL, stderr=DEVNULL)
return True
except FileNotFoundError:
return False
def collect_docstring(lines):
"""Return document docstring if it exists"""
lines = dropwhile(lambda x: not x.startswith('"""'), lines)
doc = ""
for line in lines:
doc += line
if doc.endswith('"""\n'):
break
return doc[3:-4].replace("\r", "").replace("\n", " ")
def collect_metadata():
meta = {}
with open(path.join("fast_transformers", "__init__.py")) as f:
lines = iter(f)
meta["description"] = collect_docstring(lines)
for line in lines:
if line.startswith("__"):
key, value = map(lambda x: x.strip(), line.split("="))
meta[key[2:-2]] = value[1:-1]
return meta
@lru_cache()
def _get_cpu_extra_compile_args():
base_args = ["-fopenmp", "-ffast-math"]
if sys.platform == "darwin":
return ["-Xpreprocessor"] + base_args
else:
return base_args
@lru_cache()
def _get_gpu_extra_compile_args():
if torch.cuda.is_available():
return []
else:
return ["-arch=compute_60"]
def get_extensions():
extensions = [
CppExtension(
"fast_transformers.hashing.hash_cpu",
sources=[
"fast_transformers/hashing/hash_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
),
CppExtension(
"fast_transformers.aggregate.aggregate_cpu",
sources=[
"fast_transformers/aggregate/aggregate_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
),
CppExtension(
"fast_transformers.clustering.hamming.cluster_cpu",
sources=[
"fast_transformers/clustering/hamming/cluster_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
),
CppExtension(
"fast_transformers.sparse_product.sparse_product_cpu",
sources=[
"fast_transformers/sparse_product/sparse_product_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
),
CppExtension(
"fast_transformers.sparse_product.clustered_sparse_product_cpu",
sources=[
"fast_transformers/sparse_product/clustered_sparse_product_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
),
CppExtension(
"fast_transformers.causal_product.causal_product_cpu",
sources=[
"fast_transformers/causal_product/causal_product_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
),
CppExtension(
"fast_transformers.local_product.local_product_cpu",
sources=[
"fast_transformers/local_product/local_product_cpu.cpp"
],
extra_compile_args=_get_cpu_extra_compile_args()
)
]
if cuda_toolkit_available():
from torch.utils.cpp_extension import CUDAExtension
extensions += [
CUDAExtension(
"fast_transformers.hashing.hash_cuda",
sources=[
"fast_transformers/hashing/hash_cuda.cu",
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.aggregate.aggregate_cuda",
sources=[
"fast_transformers/aggregate/aggregate_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.aggregate.clustered_aggregate_cuda",
sources=[
"fast_transformers/aggregate/clustered_aggregate_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.clustering.hamming.cluster_cuda",
sources=[
"fast_transformers/clustering/hamming/cluster_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.sparse_product.sparse_product_cuda",
sources=[
"fast_transformers/sparse_product/sparse_product_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.sparse_product.clustered_sparse_product_cuda",
sources=[
"fast_transformers/sparse_product/clustered_sparse_product_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.causal_product.causal_product_cuda",
sources=[
"fast_transformers/causal_product/causal_product_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
),
CUDAExtension(
"fast_transformers.local_product.local_product_cuda",
sources=[
"fast_transformers/local_product/local_product_cuda.cu"
],
extra_compile_args=_get_gpu_extra_compile_args()
)
]
return extensions
def setup_package():
with open("README.rst") as f:
long_description = f.read()
meta = collect_metadata()
version_suffix = os.getenv("FAST_TRANSFORMERS_VERSION_SUFFIX", "")
setup(
name="pytorch-fast-transformers",
version=meta["version"] + version_suffix,
description=meta["description"],
long_description=long_description,
long_description_content_type="text/x-rst",
maintainer=meta["maintainer"],
maintainer_email=meta["email"],
url=meta["url"],
license=meta["license"],
classifiers=[
"Intended Audience :: Science/Research",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Topic :: Scientific/Engineering",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
],
packages=find_packages(exclude=["docs", "tests", "scripts", "examples"]),
ext_modules=get_extensions(),
cmdclass={"build_ext": BuildExtension},
install_requires=["torch"]
)
if __name__ == "__main__":
setup_package()