-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
213 lines (180 loc) · 7.03 KB
/
setup.py
File metadata and controls
213 lines (180 loc) · 7.03 KB
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
import multiprocessing
import os
import platform
import stat
import subprocess
import sys
import runpy
from pathlib import Path
from typing import Union
import torch
from setuptools import Extension, find_packages, setup
from setuptools.command.build_clib import build_clib
from setuptools.command.build_ext import build_ext
from setuptools.command.develop import develop
from packaging.version import Version
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
VERSION = "1.0.0"
detect_use_arch35 = os.environ.get('USE_ARCH35', 'false').lower() == 'true'
def which(thefile):
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
for d in path:
fname = os.path.join(d, thefile)
fnames = [fname]
if sys.platform == "win32":
exts = os.environ.get("PATHEXT", "").split(os.pathsep)
fnames += [fname + ext for ext in exts]
for name in fnames:
if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name):
return name
return None
def get_cmake_command():
def _get_version(cmd):
for line in subprocess.check_output([cmd, "--version"]).decode("utf-8").split("\n"):
if "version" in line:
return Version(line.strip().split(" ")[2])
raise RuntimeError("no version found")
"Returns cmake command."
cmake_command = "cmake"
if platform.system() == "Windows":
return cmake_command
cmake3 = which("cmake3")
cmake = which("cmake")
if cmake3 is not None and _get_version(cmake3) >= Version("3.19.0"):
cmake_command = "cmake3"
return cmake_command
elif cmake is not None and _get_version(cmake) >= Version("3.19.0"):
return cmake_command
else:
raise RuntimeError("no cmake or cmake3 with version >= 3.19.0 found")
class CPPLibBuild(build_clib):
def initialize_options(self) -> None:
super().initialize_options()
self.kernel_name = None
def run(self) -> None:
cmake = get_cmake_command()
if not cmake:
raise RuntimeError("CMake must be installed to build the libraries")
self.cmake = cmake
build_py = self.get_finalized_command("build_py")
mx_driving_dir = os.path.join(BASE_DIR, build_py.build_lib, build_py.get_package_dir("mx_driving"))
if not os.path.exists(mx_driving_dir):
os.makedirs(mx_driving_dir)
use_arch35 = "ON" if detect_use_arch35 else "OFF"
cmake_args = [
"--preset=default",
f"-DCMAKE_BUILD_TYPE={'Debug' if self.debug else 'Release'}",
"-B",
self.build_temp,
f"-DMX_DRIVING_PATH={mx_driving_dir}",
f"-DKERNEL_NAME={self.kernel_name if self.kernel_name else '*'}",
f"-DUSE_ARCH35={use_arch35}",
]
build_args = ["--build", self.build_temp, f"-j{multiprocessing.cpu_count()}"]
for stage in range(2):
subprocess.check_call(
[self.cmake, BASE_DIR] + cmake_args + ["-DBUILD_STAGE=" + str(stage)],
cwd=BASE_DIR,
env=os.environ,
)
subprocess.check_call(
[self.cmake] + build_args,
cwd=BASE_DIR,
env=os.environ,
)
class ExtBuild(build_ext):
def run(self) -> None:
cmake = get_cmake_command()
if not cmake:
raise RuntimeError("CMake must be installed to build the libraries")
self.cmake = cmake
build_py = self.get_finalized_command("build_py")
mx_driving_dir = os.path.join(BASE_DIR, build_py.build_lib, build_py.get_package_dir("mx_driving"))
if not os.path.exists(mx_driving_dir):
os.makedirs(mx_driving_dir)
ext_cxx_flags = ["-std=c++17"]
for name in ["COMPILER_TYPE", "STDLIB", "BUILD_ABI"]:
val = getattr(torch._C, f"_PYBIND11_{name}")
if val:
ext_cxx_flags.append(f"-D_PYBIND11_{name}={val}")
use_arch35 = "ON" if detect_use_arch35 else "OFF"
cmake_args = [
"--preset=default",
f"-DCMAKE_BUILD_TYPE={'Debug' if self.debug else 'Release'}",
"-B",
self.build_temp,
f"-DMX_DRIVING_PATH={mx_driving_dir}",
f"-DEXT_CXX_FLAGS={' '.join(ext_cxx_flags)}",
f"-DPython3_EXECUTABLE={sys.executable}",
f"-DUSE_ARCH35={use_arch35}",
]
if Version(torch.__version__) < Version("2.1.0"):
cmake_args.append("-DCOMPILE_WITH_XLA:BOOL=ON")
if torch.compiled_with_cxx11_abi():
cmake_args.append("-DABI=1")
else:
cmake_args.append("-DABI=0")
build_args = ["--build", self.build_temp, f"-j{multiprocessing.cpu_count()}"]
subprocess.check_call(
[self.cmake, BASE_DIR] + cmake_args + ["-DBUILD_STAGE=2"],
cwd=BASE_DIR,
env=os.environ,
)
subprocess.check_call(
[self.cmake] + build_args,
cwd=BASE_DIR,
env=os.environ,
)
class DevelopBuild(develop):
user_options = develop.user_options + [
("kernel-name=", None, "Build the single kernel with the specified name"),
("release", None, "Build the release version"),
]
def initialize_options(self) -> None:
super().initialize_options()
self.kernel_name = None
self.release = False
def install_for_development(self) -> None:
self.reinitialize_command("build_py", build_lib="")
self.reinitialize_command("build_clib", kernel_name=self.kernel_name, debug=not self.release)
self.reinitialize_command("build_ext", debug=not self.release)
if self.kernel_name:
self.run_command("build_clib")
return
self.run_command("egg_info")
self.run_command("build_clib")
self.run_command("build_ext")
if not self.dry_run:
with os.fdopen(
os.open(self.egg_link, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, stat.S_IWUSR | stat.S_IRUSR),
"w",
encoding="utf-8",
) as f:
f.write(self.egg_path + "\n" + self.setup_path)
self.process_distribution(None, self.dist, not self.no_deps)
def get_sha(pytorch_root: Union[str, Path]) -> str:
try:
return (
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=pytorch_root).decode("ascii").strip() # Compliant
)
except Exception:
return "Unknown"
sha = get_sha(BASE_DIR)
if not os.getenv("BUILD_WITHOUT_SHA"):
VERSION += "+git" + sha[:7]
setup(
name="mx_driving",
version=VERSION,
description="A Library of acceleration for autonomous driving systems on Ascend-NPU.",
keywords="mx_driving",
ext_modules=[Extension("mx_driving._C", sources=[])],
author="Ascend Contributors",
libraries=[("mx_driving", {"sources": []})],
cmdclass={
"build_clib": CPPLibBuild,
"build_ext": ExtBuild,
"develop": DevelopBuild,
},
packages=find_packages(),
include_package_data=True,
)