-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsetup.py
639 lines (551 loc) · 20.7 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import platform
import sys
import os
import tempfile
import setuptools
import subprocess
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import distutils.sysconfig
import distutils.spawn
__version__ = "4.0.4"
# TODO(@motiwari): Move this to a separate file
GHA = "GITHUB_ACTIONS"
class get_pybind_include(object):
"""
Helper class to determine the pybind11 include path.
The purpose of this class is to postpone importing pybind11
until it is actually installed via setup's setup_requires arg,
so that the ``get_include()`` method can be invoked.
"""
def __str__(self):
import pybind11
return pybind11.get_include()
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 via setup's setup_requires arg,
so that the ``get_include()`` method can be invoked.
"""
def __str__(self):
import numpy
return numpy.get_include()
def compiler_check():
"""
This is necessary because setuptools will use the compiler that compiled
python for some of the compilation process, even if the user specifies
another one!
"""
try:
return (
"clang"
if "clang" in distutils.sysconfig.get_config_vars()["CC"]
else "gcc"
)
except KeyError:
# The 'CC' environment variable hasn't been set.
# In this case, search for compilers that we can use.
# Borrowed from https://github.com/clab/dynet/blob/master/setup.py
if distutils.spawn.find_executable("cl") is not None:
return "msvc"
elif distutils.spawn.find_executable("clang") is not None:
return "clang"
elif distutils.spawn.find_executable("gcc") is not None:
return "gcc"
raise Exception(
"No C++ compiler was found. Please ensure you have "
"MSVC, LLVM clang, or GCC."
)
def has_flag(compiler: str, flagname: str):
"""
Return a boolean indicating whether a flag name is supported on the
specified compiler.
"""
with tempfile.NamedTemporaryFile("w", suffix=".cpp", delete=False) as f:
f.write("int main (int argc, char **argv) { return 0; }")
fname = f.name
try:
compiler.compile([fname], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
finally:
try:
os.remove(fname)
except OSError:
print("Warning: Received an OSError")
return True
def cpp_flag(compiler: str):
"""
Return the -std=c++[11/14/17] compiler flag.
The newer version is prefered over c++11 (when it is available).
"""
compiler_name = compiler_check()
if compiler_name == "clang" or compiler_name == "gcc":
flags = ["-std=c++17"]
else:
# Assume msvc
flags = ["/std:c++17"] # required for std::optional
for flag in flags:
if has_flag(compiler, flag):
return flag
raise RuntimeError(
"Unsupported compiler -- \
at least C++11 support is needed!"
)
def check_brew_package(pkg_name: str):
brew_cmd = ["brew", "--prefix", pkg_name]
process = subprocess.Popen(brew_cmd, stdout=subprocess.PIPE)
output, _error = process.communicate()
if output.decode() == "":
raise Exception(
"Error: Need to install %s via homebrew! \
Please run `brew install %s`"
% (pkg_name, pkg_name)
)
return output.decode().strip()
def check_brew_installation():
brew_cmd = ["which", "brew"]
process = subprocess.Popen(brew_cmd, stdout=subprocess.PIPE)
output, _error = process.communicate()
if output.decode() == "":
raise Exception(
"Error: Need to install homebrew! \
Please see https://brew.sh"
)
def check_numpy_installation():
try:
import numpy # noqa: F401
except ModuleNotFoundError:
raise Exception("Need to install numpy!")
def install_check_mac():
# Make sure homebrew is installed
check_brew_installation()
# Make sure numpy is installed
check_numpy_installation()
# Check that libomp, and armadillo are installed
check_brew_package("libomp")
check_brew_package("armadillo")
# Because we don't want to force M1 users to compile from source,
# we build wheels on Github Runners via Github Actions. In
# doing so, we need to use Apple's compiler to cross-compile
# for M1 Macs and should NOT use clang.
# If we are building from source on a non-M1 Mac, we should use
# LLVM clang to support multithreading via OpenMP
# TODO(@motiwari): Check if the arm64 wheels are not multithreaded
# TODO(@motiwari): Check if the universal2 wheels are not multithreaded
# when installed on Intel Mac
if not os.environ.get(GHA, False):
# If we are NOT running inside a Github action,
# check that LLVM clang is installed
llvm_loc = check_brew_package(
"llvm"
) # We need to use LLVM clang since Apple's doesn't support OpenMP
# Set compiler to LLVM clang on Mac for OpenMP support
distutils.sysconfig.get_config_vars()["CC"] = os.path.join(
llvm_loc, "bin", "clang"
)
def check_omp_install_linux():
# TODO: Need to get exact compiler name and version to check this
# Check compiler version is gcc>=6.0.0 or clang>=X.X.X
pass
def check_armadillo_install_linux():
# Since armadillo is a C++ extension, just check if it exists
cmd = ["find", "/", "-iname", "armadillo"]
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
output, _error = process.communicate()
if output.decode() == "":
print(
"Warning: Armadillo may not be installed. \
Please build it from",
os.path.join(
"BanditPAM",
"headers",
"carma",
"third_party",
"armadillo-code",
),
)
return output.decode().strip()
def check_linux_package_installation(pkg_name: str):
cmd = ["dpkg", "-s", pkg_name]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output, _error = process.communicate()
if output.decode() == "":
raise Exception(
"Error: Need to install %s! \
Please ensure all dependencies are installed \
via your package manager (apt, yum, etc.): \
build-essential checkinstall \
libncursesw5-dev libssl-dev libsqlite3-dev tk-dev \
libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev"
% (pkg_name)
)
return output.decode().strip()
def install_ubuntu_pkgs():
# TODO: Remove dangerous os.system() calls
# See https://stackoverflow.com/a/51329156
os.system("apt update")
os.system(
"apt install -y \
build-essential \
checkinstall \
libncursesw5-dev \
libssl-dev \
libsqlite3-dev \
libgdbm-dev \
libc6-dev \
libbz2-dev \
libffi-dev \
zlib1g-dev"
) # noqa: E124
def setup_colab(delete_source=False):
# If we're in Google Colab, we need to manually copy over
# the prebuilt armadillo libraries
# NOTE: This only works for Colab instances with Ubuntu 18.04 Runtimes!
try:
import google.colab # noqa: F401
in_colab = True
except ModuleNotFoundError:
in_colab = False
if in_colab:
# TODO: Remove dangerous os.system() calls
# See https://stackoverflow.com/a/51329156
install_ubuntu_pkgs()
# TODO(@motiwari): Make this a randomly-named directory
# and set delete_source=True always
repo_location = os.path.join("/", "content", "BanditPAM")
# Note the space after the git URL to separate the source and target
os.system(
"git clone https://github.com/motiwari/BanditPAM.git "
+ repo_location
)
os.system(
repo_location
+ os.path.join(
"scripts",
"colab_files",
"colab_install_armadillo.sh",
)
)
if delete_source:
os.system("rm -rf " + repo_location)
def setup_paperspace_gradient():
# Unfortunately, Paperspace Gradient does not make it easy to tell
# whether you're inside a Gradient instance. For this reason, we
# determine whether python is running inside a Gradient instance
# by checking for a /notebooks directory
# TODO: Remove dangerous os.system() calls
# See https://stackoverflow.com/a/51329156
in_paperspace_gradient = os.path.exists("/notebooks")
if in_paperspace_gradient:
install_ubuntu_pkgs()
os.system(
"DEBIAN_FRONTEND=noninteractive TZ=America/NewYork \
apt install -y tk-dev"
)
os.system(
"git clone \
https://gitlab.com/conradsnicta/armadillo-code.git"
)
os.system("cd armadillo-code && cmake . && make install")
def install_check_ubuntu():
# Make sure linux packages are installed
dependencies = [
"build-essential",
"checkinstall",
"libncursesw5-dev",
"libssl-dev",
"libsqlite3-dev",
"tk-dev",
"libgdbm-dev",
"libc6-dev",
"libbz2-dev",
"libffi-dev",
"zlib1g-dev",
]
setup_colab(delete_source=False)
setup_paperspace_gradient()
for dep in dependencies:
check_linux_package_installation(dep)
# Make sure numpy is installed
check_numpy_installation()
# Check openMP is installed
check_omp_install_linux()
# Check armadillo is installed
check_armadillo_install_linux()
def is_ubuntu():
cmd = ["cat", os.path.join("/", "etc", "issue")]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output, _error = process.communicate()
return "Ubuntu" in output.decode()
class BuildExt(build_ext):
"""
A custom build extension for adding compiler-specific options.
"""
c_opts = {"msvc": ["/EHsc"], "unix": []}
l_opts = {"msvc": [], "unix": []}
if sys.platform == "darwin":
install_check_mac()
# Verify that we're either compiling with clang or
# inside a Github Action
assert compiler_check() == "clang" or os.environ.get(
GHA, False
), "Need to install LLVM clang!"
darwin_opts = [
"-stdlib=libc++",
"-mmacosx-version-min=10.14",
"-O3",
]
c_opts["unix"] += darwin_opts
l_opts["unix"] += darwin_opts
elif sys.platform == "linux" or sys.platform == "linux2":
if is_ubuntu():
install_check_ubuntu()
linux_opts = ["-O3"]
c_opts["unix"] += linux_opts
l_opts["unix"] += linux_opts
# Currently necessary (unsure why)
elif sys.platform == "win32":
c_opts["msvc"] += ["/fsanitize=address"]
def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
link_opts = self.l_opts.get(ct, [])
# TODO(@motiwari): on Windows, these flags are unrecognized
opts.append(cpp_flag(self.compiler))
opts.append("-O3")
if sys.platform == "darwin" and os.environ.get(GHA, False):
opts.append("-Xpreprocessor") # NEEDS TO BE WITH NEXT LINE
opts.append("-fopenmp") # NEEDS TO BE WITH PREVIOUS LINE
opts.append("-lomp") # Potentially unused?
opts.append("-I/usr/local/opt/libomp/include")
opts.append("-L/usr/local/opt/libomp/lib") # Unused?
elif sys.platform != "win32":
opts.append("-fopenmp")
compiler_name = compiler_check()
if sys.platform == "darwin" and os.environ.get(GHA, False):
link_opts.append("-lomp") # Potentially unused?
link_opts.append("-I/usr/local/opt/libomp/include")
link_opts.append("-L/usr/local/opt/libomp/lib") # Unused?
elif sys.platform != "win32":
if compiler_name == "clang":
link_opts.append("-lomp")
else: # gcc
link_opts.append("-lgomp")
if ct == "unix":
if has_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")
for ext in self.extensions:
ext.define_macros = [
(
"VERSION_INFO",
'"{}"'.format(self.distribution.get_version()),
)
]
ext.extra_compile_args = opts
ext.extra_compile_args += [] # []["-arch", "x86_64"]
ext.extra_link_args = link_opts
ext.extra_link_args += [
"-v",
] # "-arch", "x86_64"]
build_ext.build_extensions(self)
def main():
if sys.platform == "linux" or sys.platform == "linux2":
include_dirs = [
get_pybind_include(),
get_numpy_include(),
"headers",
os.path.join("headers", "algorithms"),
os.path.join("headers", "python_bindings"),
os.path.join("headers", "carma", "include"),
os.path.join("headers", "carma", "include", "carma_bits"),
os.path.join("/", "usr", "local", "include"),
]
elif sys.platform == "darwin": # OSX
include_dirs = [
get_pybind_include(),
get_numpy_include(),
"headers",
os.path.join("headers", "algorithms"),
os.path.join("headers", "python_bindings"),
os.path.join("headers", "carma", "include"),
os.path.join("headers", "carma", "include", "carma"),
os.path.join("headers", "carma", "include", "carma", "carma"),
# To include carma when the BanditPAM repo hasnt been initialized
os.path.join("/", "usr", "local", "include"),
os.path.join("/", "usr", "local", "include", "carma"),
os.path.join(
"/", "usr", "local", "include", "carma", "carma_bits"
),
# When building from source on M1 Macs, may need these dirs
# Currently, we should never be building from source on an M1 Mac,
# Only cross-compiling from an Intel Mac
# TODO(@motiwari): Remove extraneous directories
os.path.join("/", "opt", "homebrew"),
os.path.join("/", "opt", "homebrew", "bin"),
os.path.join("/", "opt", "homebrew", "include"),
os.path.join("/", "opt", "homebrew", "lib"),
os.path.join("/", "opt", "homebrew", "opt"),
os.path.join("/", "opt", "homebrew", "opt", "armadillo"),
os.path.join(
"/", "opt", "homebrew", "opt", "armadillo", "include"
),
os.path.join(
"/",
"opt",
"homebrew",
"opt",
"armadillo",
"include",
"armadillo_bits",
),
# Needed for Mac Github Runners
# for macos-10.15
os.path.join(
"/", "usr", "local", "Cellar", "libomp", "15.0.2", "include"
),
# for macos-latest
os.path.join(
"/", "usr", "local", "Cellar", "libomp", "15.0.7", "include"
),
]
elif sys.platform == "win32": # WIN32
include_dirs = [
get_pybind_include(),
get_numpy_include(),
"headers",
os.path.join("headers", "algorithms"),
os.path.join("headers", "python_bindings"),
os.path.join("headers", "carma", "include"),
os.path.join("headers", "carma", "include", "carma_bits"),
os.path.join("headers", "armadillo", "include"),
os.path.join("headers", "armadillo", "include", "armadillo_bits"),
]
else:
raise Exception("Unrecognized platform")
compiler_name = compiler_check()
if sys.platform == "darwin" and os.environ.get(GHA, False):
# On Mac Github Runners, we should NOT include gomp or omp here
# due to build errors.
libraries = ["armadillo", "omp"]
elif sys.platform == "win32":
libraries = ["libopenblas"]
else:
if compiler_name == "clang":
libraries = ["armadillo", "omp"]
else: # gcc
libraries = ["armadillo", "gomp"]
cpp_args = None
if sys.platform == "win32":
cpp_args = ["/std:c++17"]
library_dirs = [
# for windows
os.path.join(os.getcwd(), r"headers\armadillo\examples\lib_win64"),
]
else:
cpp_args = [
"-static-libstdc++"
] # TODO(@motiwari): Modify this based on gcc or clang
library_dirs = [
os.path.join("/", "usr", "local", "lib"),
os.path.join(
"/", "usr", "local", "Cellar", "libomp", "15.0.2", "lib"
),
os.path.join(
"/", "usr", "local", "Cellar", "libomp", "15.0.7", "lib"
),
]
if sys.platform == "darwin" and platform.processor() == "arm": # M1
library_dirs.append(
os.path.join("/", "opt", "homebrew", "opt", "armadillo", "lib")
)
library_dirs.append(
os.path.join("/", "opt", "homebrew", "opt", "libomp", "lib")
)
ext_modules = [
Extension(
"banditpam",
[
os.path.join("src", "algorithms", "kmedoids_algorithm.cpp"),
os.path.join("src", "algorithms", "pam.cpp"),
os.path.join("src", "algorithms", "banditpam.cpp"),
os.path.join("src", "algorithms", "banditpam_orig.cpp"),
os.path.join("src", "algorithms", "fastpam1.cpp"),
os.path.join(
"src", "python_bindings", "kmedoids_pywrapper.cpp"
),
os.path.join("src", "python_bindings", "medoids_python.cpp"),
os.path.join(
"src", "python_bindings", "build_medoids_python.cpp"
),
os.path.join("src", "python_bindings", "fit_python.cpp"),
os.path.join("src", "python_bindings", "labels_python.cpp"),
os.path.join("src", "python_bindings", "steps_python.cpp"),
os.path.join("src", "python_bindings", "loss_python.cpp"),
os.path.join("src", "python_bindings", "cache_python.cpp"),
os.path.join(
"src", "python_bindings", "swap_times_python.cpp"
),
],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
language="c++1z", # TODO: modify this based on cpp_flag(compiler)
extra_compile_args=cpp_args,
extra_link_args=[], # Wrong pass (BuildExt sets extra_link_args)
)
]
with open(os.path.join("docs", "long_desc.rst"), encoding="utf-8") as f:
long_description = f.read()
my_data_files = [("docs", [os.path.join("docs", "long_desc.rst")])]
if sys.platform == "win32":
my_data_files.append(
(
"",
[
os.path.join(
os.getcwd(),
r"headers\armadillo\examples\lib_win64"
+ r"\libopenblas.dll",
)
],
)
)
setup(
name="banditpam",
version=__version__,
author="Mo Tiwari",
maintainer="Mo Tiwari",
author_email="motiwari@stanford.edu",
url="https://github.com/motiwari/BanditPAM",
description="BanditPAM: A state-of-the-art, \
high-performance k-medoids algorithm.",
long_description=long_description,
ext_modules=ext_modules,
setup_requires=["pybind11>=2.5.0", "numpy>=1.18"],
data_files=my_data_files,
include_package_data=True,
cmdclass={"build_ext": BuildExt},
zip_safe=False,
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
headers=[
os.path.join("headers", "algorithms", "kmedoids_algorithm.hpp"),
os.path.join("headers", "algorithms", "banditpam.hpp"),
os.path.join("headers", "algorithms", "fastpam1.hpp"),
os.path.join("headers", "algorithms", "pam.hpp"),
os.path.join(
"headers", "python_bindings", "kmedoids_pywrapper.hpp"
),
],
)
if __name__ == "__main__":
main()