forked from mpi4jax/mpi4jax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
293 lines (233 loc) · 8.12 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
import os
import sys
import shlex
import logging
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
#logging.basicConfig(level=logging.INFO)
here = os.path.abspath(os.path.dirname(__file__))
# ensure vendored versioneer is on path
sys.path.insert(0, here)
import versioneer # noqa: E402
try:
from Cython.Build import cythonize
except ImportError:
HAS_CYTHON = False
else:
HAS_CYTHON = True
try:
import mpi4py
except ImportError:
HAS_MPI4PY = False
else:
HAS_MPI4PY = True
##############
# Requirements
JAX_MINIMUM_VERSION = "0.4.5"
BASE_DEPENDENCIES = ["mpi4py>=3.0.1", "numpy", f"jax>={JAX_MINIMUM_VERSION}"]
DEV_DEPENDENCIES = [
"pytest>=6",
"pytest-cov>=2.10.1",
"coverage[toml]>=5",
"pre-commit",
"black==23.9; python_version >= '3.8.0'",
"flake8==3.9.2",
"tqdm>=4.52",
]
CYTHON_SUBMODULE_NAME = "mpi4jax._src.xla_bridge"
CYTHON_SUBMODULE_PATH = "mpi4jax/_src/xla_bridge"
#######
# Utils
def search_on_path(filenames):
path_dirs = os.getenv("PATH", "").split(os.pathsep)
for p in path_dirs:
for filename in filenames:
full_path = os.path.join(p, filename)
if os.path.exists(full_path):
return os.path.abspath(full_path)
return None
def print_warning(*lines):
print("**************************************************")
for line in lines:
print("*** WARNING: %s" % line)
print("**************************************************")
# /end Utils
############
class custom_build_ext(build_ext):
def build_extensions(self):
config = mpi4py.get_config()
mpi_compiler = os.environ.get("MPI4JAX_BUILD_MPICC", config["mpicc"])
mpi_cmd = shlex.split(mpi_compiler)
for exe in ("compiler", "compiler_so", "compiler_cxx", "linker_so"):
# peel off compiler executable but keep flags
current_flags = getattr(self.compiler, exe)[1:]
self.compiler.set_executable(exe, [*mpi_cmd, *current_flags])
build_ext.build_extensions(self)
################
# Cuda detection
# Taken from CUPY (MIT License)
def get_cuda_path():
nvcc_path = search_on_path(("nvcc", "nvcc.exe"))
cuda_path_default = None
if nvcc_path is not None:
cuda_path_default = os.path.normpath(
os.path.join(os.path.dirname(nvcc_path), "..")
)
cuda_path = os.getenv("CUDA_PATH", "") # Nvidia default on Windows
if len(cuda_path) == 0:
cuda_path = os.getenv("CUDA_ROOT", "") # Nvidia default on Windows
if len(cuda_path) > 0 and cuda_path != cuda_path_default:
print_warning(
"nvcc path != CUDA_PATH",
"nvcc path: %s" % cuda_path_default,
"CUDA_PATH: %s" % cuda_path,
)
if os.path.exists(cuda_path):
_cuda_path = cuda_path
elif cuda_path_default is not None:
_cuda_path = cuda_path_default
elif os.path.exists("/usr/local/cuda"):
_cuda_path = "/usr/local/cuda"
else:
_cuda_path = None
return _cuda_path
def get_sycl_path():
basekit = os.environ.get('ONEAPI_ROOT')
logging.info("ONEAPI_ROOT={basekit}")
return basekit
def get_sycl_info():
sycl_info = {"compile": [], "libdirs": [], "libs": []}
sycl_path = get_sycl_path()
if not sycl_path:
return sycl_info
include_suffixes = [
"compiler/latest/linux/include/",
"compiler/latest/linux/include/sycl",
"compiler/latest/include/",
"compiler/latest/include/sycl",
]
for inc_suffix in include_suffixes:
incdir = os.path.join(sycl_path, inc_suffix)
if os.path.isdir(incdir):
sycl_info["compile"].append(incdir)
logging.info("Adding include={incdir}")
libdir_suffixes = [
"compiler/latest/linux/lib/",
"compiler/latest/lib/",
]
for libdir_suffix in libdir_suffixes:
lib_dir = os.path.join(sycl_path,libdir_suffix)
if os.path.isdir(lib_dir):
sycl_info["libdirs"].append(lib_dir)
logging.info("Adding lib dir={lib_dir}")
sycl_info["libs"].append("sycl")
return sycl_info
sycl_info = get_sycl_info()
def get_cuda_info():
cuda_info = {"compile": [], "libdirs": [], "libs": []}
cuda_path = get_cuda_path()
if not cuda_path:
return cuda_info
incdir = os.path.join(cuda_path, "include")
if os.path.isdir(incdir):
cuda_info["compile"].append(incdir)
for libdir in ("lib64", "lib"):
full_dir = os.path.join(cuda_path, libdir)
if os.path.isdir(full_dir):
cuda_info["libdirs"].append(full_dir)
cuda_info["libs"].append("cudart")
return cuda_info
cuda_info = get_cuda_info()
# /end Cuda detection
#####################
def get_extensions():
cmd = sys.argv[1]
require_extensions = any(
cmd.startswith(subcmd) for subcmd in ("install", "build", "bdist", "develop")
)
if not HAS_MPI4PY or not HAS_CYTHON:
# this should only happen when using python setup.py
# or pip install --no-build-isolation
if require_extensions:
raise RuntimeError("Building mpi4jax requires Cython and mpi4py")
else:
return []
extensions = [
Extension(
name=f"{CYTHON_SUBMODULE_NAME}.{mod}",
sources=[f"{CYTHON_SUBMODULE_PATH}/{mod}.pyx"],
)
for mod in ("mpi_xla_bridge", "mpi_xla_bridge_cpu")
]
if sycl_info["compile"] and sycl_info["libdirs"]:
extensions.append(
Extension(
name=f"{CYTHON_SUBMODULE_NAME}.mpi_xla_bridge_xpu",
sources=[f"{CYTHON_SUBMODULE_PATH}/mpi_xla_bridge_xpu.pyx"],
include_dirs=sycl_info["compile"],
library_dirs=sycl_info["libdirs"],
libraries=sycl_info["libs"],
language="c++",
)
)
else:
print_warning("SYCL (Intel Basekit) path not found. Did you call {you basekit dir}/setvars.sh? You can use env var ONEAPI_ROOT to point Basekit directory where sycl is", "(XPU extensions will not be built)")
if cuda_info["compile"] and cuda_info["libdirs"]:
extensions.append(
Extension(
name=f"{CYTHON_SUBMODULE_NAME}.mpi_xla_bridge_gpu",
sources=[f"{CYTHON_SUBMODULE_PATH}/mpi_xla_bridge_gpu.pyx"],
include_dirs=cuda_info["compile"],
library_dirs=cuda_info["libdirs"],
libraries=cuda_info["libs"],
)
)
else:
print_warning("CUDA path not found", "(GPU extensions will not be built)")
if HAS_CYTHON:
extensions = cythonize(
extensions,
language_level=3,
)
return extensions
# get the long description from the README file
with open(os.path.join(here, "README.rst"), encoding="utf-8") as f:
long_description = f.read()
cmdclass = versioneer.get_cmdclass()
cmdclass.update(build_ext=custom_build_ext)
setup(
name="mpi4jax",
author="Filippo Vicentini",
author_email="[email protected]",
description=(
"Zero-copy MPI communication of JAX arrays, "
"for turbo-charged HPC applications in Python ⚡"
),
long_description=long_description,
url="https://github.com/mpi4jax/mpi4jax",
license="MIT",
version=versioneer.get_version(),
cmdclass=cmdclass,
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Cython",
"Development Status :: 3 - Alpha",
"Environment :: GPU :: NVIDIA CUDA",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX :: Linux",
"Operating System :: Unix",
"Topic :: Scientific/Engineering",
],
packages=find_packages(),
ext_modules=get_extensions(),
python_requires=">=3.8",
install_requires=BASE_DEPENDENCIES,
extras_require={
"dev": DEV_DEPENDENCIES,
},
package_data={"mpi4jax": ["_src/_latest_jax_version.txt"]},
zip_safe=False,
)