-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
153 lines (133 loc) · 5.55 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
import os
import platform
import re
import shutil
import subprocess
import sys
from pathlib import Path
from pip._internal.req import parse_requirements
from setuptools import Extension, find_namespace_packages, setup
from setuptools.command.build_ext import build_ext
# A CMakeExtension needs a sourcedir instead of a file list.
# The name must be the _single_ output extension from the CMake build.
# If you need multiple extensions, see scikit-build.
class CMakeExtension(Extension):
def __init__(self, name: str, sourcedir: str = "") -> None:
super().__init__(name, sources=[])
self.sourcedir = os.fspath(Path(sourcedir).resolve())
class CMakeBuild(build_ext):
def build_extension(self, ext: CMakeExtension) -> None:
# Must be in this form due to bug in .resolve() only fixed in Python 3.10+
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name) # type: ignore[no-untyped-call]
ext_build_lib_dir = ext_fullpath.parent.resolve()
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
cfg = "Debug" if debug else "Release"
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
cmake_args = [
f"-DCMAKE_INSTALL_PREFIX={ext_build_lib_dir}/{PACKAGE_NAME}",
f"-DPython3_EXECUTABLE={sys.executable}",
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
]
torch_mlir_install_dir = os.environ.get("TORCH_MLIR_INSTALL_DIR", None)
if torch_mlir_install_dir is not None:
cmake_args.append(f"-DTORCH_MLIR_INSTALL_DIR={torch_mlir_install_dir}")
build_args = []
if "CMAKE_ARGS" in os.environ:
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
if not cmake_generator or cmake_generator == "Ninja":
try:
import ninja # noqa: F401
ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
cmake_args += [
"-GNinja",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
]
except ImportError:
pass
if sys.platform.lower().startswith("darwin") and os.environ.get(
"CMAKE_OSX_ARCHITECTURES", False
):
cmake_args += [
f"-DCMAKE_OSX_ARCHITECTURES={os.getenv('CMAKE_OSX_ARCHITECTURES')}"
]
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
# self.parallel is a Python 3 only way to set parallel jobs by hand
# using -j in the build_ext call, not supported by pip or PyPA-build.
if hasattr(self, "parallel") and self.parallel:
# CMake 3.12+ only.
build_args += [f"-j{self.parallel}"]
build_temp = Path(self.build_temp) / ext.name
if not build_temp.exists():
build_temp.mkdir(parents=True)
subprocess.run(
["cmake", ext.sourcedir] + cmake_args, cwd=build_temp, check=True
)
subprocess.run(
["cmake", "--build", ".", "--target", "install"] + build_args,
cwd=build_temp,
check=True,
)
if platform.system() == "Darwin":
shlib_ext = "dylib"
elif platform.system() == "Linux":
shlib_ext = "so"
else:
raise NotImplementedError(f"unknown platform {platform.system()}")
mlir_libs_dir = Path(f"{ext_build_lib_dir}/{PACKAGE_NAME}/mlir/_mlir_libs")
shlibs = [
# "LTO",
# "MLIR-C",
# "Remarks",
"mlir_async_runtime",
"mlir_c_runner_utils",
"mlir_float16_utils",
"mlir_runner_utils",
]
for shlib in shlibs:
shlib_name = f"lib{shlib}.{shlib_ext}"
torch_mlir_install_dir = (
Path(".").parent / "torch_mlir_install/torch_mlir_install"
).absolute()
assert torch_mlir_install_dir.exists(), f"missing {torch_mlir_install_dir=}"
torch_mlir_install_fp = (
torch_mlir_install_dir / "lib" / shlib_name
).absolute()
assert torch_mlir_install_fp.exists(), f"missing {torch_mlir_install_fp=}"
dst_path = mlir_libs_dir / shlib_name
shutil.copyfile(torch_mlir_install_fp, dst_path)
if platform.system() == "Linux":
shutil.copyfile(torch_mlir_install_fp, f"{dst_path}.17git")
subprocess.run(
["patchelf", "--set-rpath", "$ORIGIN", dst_path],
cwd=build_temp,
check=True,
)
subprocess.run(
["patchelf", "--set-rpath", "$ORIGIN", f"{dst_path}.17git"],
cwd=build_temp,
check=True,
)
PACKAGE_NAME = "pi"
packages = find_namespace_packages(
include=[
PACKAGE_NAME,
f"{PACKAGE_NAME}.*",
],
)
VERSION = "0.0.5"
if len(sys.argv) > 1 and sys.argv[1] == "--version":
print(VERSION)
else:
install_reqs = parse_requirements("requirements.txt", session="hack")
setup(
name=PACKAGE_NAME,
version=VERSION,
author="Maksim Levental",
author_email="[email protected]",
description="A lightweight MLIR Python frontend with PyTorch like syntax",
ext_modules=[CMakeExtension("_pi_mlir")],
cmdclass={"build_ext": CMakeBuild},
packages=packages,
zip_safe=False,
install_requires=[str(ir.requirement) for ir in install_reqs],
)