-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
94 lines (77 loc) · 2.99 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
import os
import subprocess
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
ROOT = os.path.realpath(os.path.dirname(__file__))
class cmake_ext(build_ext):
"""Build the native library using cmake"""
def run(self):
import torch
source_dir = ROOT
build_dir = os.path.join(ROOT, "build", "cmake-build")
install_dir = os.path.join(
os.path.realpath(self.build_lib), "metatensor_lj_test"
)
os.makedirs(build_dir, exist_ok=True)
cmake_options = [
"-DCMAKE_BUILD_TYPE=Release",
f"-DCMAKE_INSTALL_PREFIX={install_dir}",
f"-DCMAKE_PREFIX_PATH={torch.utils.cmake_prefix_path}",
]
# ==================================================================== #
# HACK: Torch cmake build system has a hard time finding CuDNN, so we
# help it by pointing it to the right files
# First try using the `nvidia.cudnn` package (dependency of torch on PyPI)
try:
import nvidia.cudnn
cudnn_root = os.path.dirname(nvidia.cudnn.__file__)
except ImportError:
# Otherwise try to find CuDNN inside PyTorch itself
cudnn_root = os.path.join(torch.utils.cmake_prefix_path, "..", "..")
cudnn_version = os.path.join(cudnn_root, "include", "cudnn_version.h")
if not os.path.exists(cudnn_version):
# create a minimal cudnn_version.h (with a made-up version),
# because it is not bundled together with the CuDNN shared
# library in PyTorch conda distribution, see
# https://github.com/pytorch/pytorch/issues/47743
with open(cudnn_version, "w") as fd:
fd.write("#define CUDNN_MAJOR 8\n")
fd.write("#define CUDNN_MINOR 5\n")
fd.write("#define CUDNN_PATCHLEVEL 0\n")
cmake_options.append(f"-DCUDNN_INCLUDE_DIR={cudnn_root}/include")
cmake_options.append(f"-DCUDNN_LIBRARY={cudnn_root}/lib")
# do not warn if the two variables above aren't used
cmake_options.append("--no-warn-unused-cli")
# end of HACK
# ==================================================================== #
subprocess.run(
["cmake", source_dir, *cmake_options],
cwd=build_dir,
check=True,
)
subprocess.run(
[
"cmake",
"--build",
build_dir,
"--config",
"Release",
"--target",
"install",
],
check=True,
)
if __name__ == "__main__":
setup(
ext_modules=[
Extension(name="metatensor_lj_test", sources=[]),
],
cmdclass={
"build_ext": cmake_ext,
},
package_data={
"metatensor_lj_test": [
"metatensor_lj_test/lib/*",
]
},
)