-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
124 lines (105 loc) · 3.91 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
#!/usr/bin/env python
import codecs
import os
import subprocess
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
HERE = os.path.dirname(os.path.realpath(__file__))
def read(*parts):
with codecs.open(os.path.join(HERE, *parts), "rb", "utf-8") as f:
return f.read()
# This custom class for building the extensions uses CMake to compile. You
# don't have to use CMake for this task, but I found it to be the easiest when
# compiling ops with GPU support since setuptools doesn't have great CUDA
# support.
class CMakeBuildExt(build_ext):
def build_extensions(self):
# First: configure CMake build
import platform
import sys
import distutils.sysconfig
import pybind11
# Work out the relevant Python paths to pass to CMake, adapted from the
# PyTorch build system
if platform.system() == "Windows":
cmake_python_library = "{}/libs/python{}.lib".format(
distutils.sysconfig.get_config_var("prefix"),
distutils.sysconfig.get_config_var("VERSION"),
)
if not os.path.exists(cmake_python_library):
cmake_python_library = "{}/libs/python{}.lib".format(
sys.base_prefix,
distutils.sysconfig.get_config_var("VERSION"),
)
else:
cmake_python_library = "{}/{}".format(
distutils.sysconfig.get_config_var("LIBDIR"),
distutils.sysconfig.get_config_var("INSTSONAME"),
)
cmake_python_include_dir = distutils.sysconfig.get_python_inc()
install_dir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath("dummy"))
)
os.makedirs(install_dir, exist_ok=True)
cmake_args = [
"-DCMAKE_INSTALL_PREFIX={}".format(install_dir),
"-DPython_EXECUTABLE={}".format(sys.executable),
"-DPython_LIBRARIES={}".format(cmake_python_library),
"-DPython_INCLUDE_DIRS={}".format(cmake_python_include_dir),
"-DCMAKE_BUILD_TYPE={}".format(
"Debug" if self.debug else "Release"
),
"-DCMAKE_PREFIX_PATH={}".format(pybind11.get_cmake_dir()),
]
if os.environ.get("SUMCUMPROD_JAX_CUDA", "no").lower() == "yes":
cmake_args.append("-DSUMCUMPROD_JAX_CUDA=yes")
os.makedirs(self.build_temp, exist_ok=True)
subprocess.check_call(
["cmake", HERE] + cmake_args, cwd=self.build_temp
)
# Build all the extensions
super().build_extensions()
# Finally run install
subprocess.check_call(
["cmake", "--build", ".", "--target", "install"],
cwd=self.build_temp,
)
def build_extension(self, ext):
target_name = ext.name.split(".")[-1]
subprocess.check_call(
["cmake", "--build", ".", "--target", target_name],
cwd=self.build_temp,
)
extensions = [
Extension(
"sumcumprod_jax.cpu_ops",
["src/sumcumprod_jax/src/cpu_ops.cc"],
),
]
if os.environ.get("SUMCUMPROD_JAX_CUDA", "no").lower() == "yes":
extensions.append(
Extension(
"sumcumprod_jax.gpu_ops",
[
"src/sumcumprod_jax/src/gpu_ops.cc",
"src/sumcumprod_jax/src/cuda_kernels.cc.cu",
],
)
)
setup(
name="sumcumprod_jax",
license="MIT",
description=(
"A simple demonstration of how you can extend JAX with custom C++ and "
"CUDA ops"
),
long_description=read("README.md"),
long_description_content_type="text/markdown",
packages=find_packages("src"),
package_dir={"": "src"},
include_package_data=True,
install_requires=["jax", "jaxlib"],
extras_require={"test": "pytest"},
ext_modules=extensions,
cmdclass={"build_ext": CMakeBuildExt},
)