-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
127 lines (108 loc) · 4.28 KB
/
Copy pathsetup.py
File metadata and controls
127 lines (108 loc) · 4.28 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
import os
import setuptools
import shutil
import subprocess
import torch
from setuptools import find_packages, setup
from setuptools.command.build_py import build_py
from torch.utils.cpp_extension import (
BuildExtension,
CppExtension
)
# _abs_file_ = os.path.abspath(__file__)
# source_root_dir = os.path.dirname(_abs_file_)
current_dir = os.path.dirname(os.path.realpath(__file__))
cxx_flags = ['-std=c++17', '-O3', '-fPIC', '-Wno-psabi', '-Wno-deprecated-declarations']
sources = ['csrc/python_api.cpp']
def get_pkg_install_dir(pkg_name: str) -> str:
import subprocess
import sys
import os
result = subprocess.run(
[sys.executable, "-m", "pip", "show", pkg_name], capture_output=True, text=True
)
for line in result.stdout.splitlines():
if line.startswith("Location:"):
install_location = line.split("Location: ")[1]
return os.path.join(install_location, pkg_name)
torch_install_dir = get_pkg_install_dir("torch")
torch_gcu_install_dir = get_pkg_install_dir("torch_gcu")
print(torch_install_dir)
print(torch_gcu_install_dir)
torch_include_dir = os.path.join(torch_install_dir, "include")
torch_gcu_include_dir = os.path.join(torch_gcu_install_dir, "include")
top_runtime_include_dir = "/opt/tops/include"
top_aten_include_dir = "/usr/include/gcu"
torch_lib_dir = os.path.join(torch_install_dir, "lib")
torch_gcu_lib_dir = os.path.join(torch_gcu_install_dir, "lib")
build_include_dirs = [
'/usr/include',
torch_include_dir,
torch_gcu_include_dir,
top_runtime_include_dir,
top_aten_include_dir
]
build_libraries = ['topsaten', 'torch', 'torch_python', 'torch_gcu']
build_library_dirs = [
'/usr/lib',
torch_lib_dir,
torch_gcu_lib_dir
]
# Use runtime API
if int(os.environ.get('DG_JIT_USE_RUNTIME_API', '0')):
cxx_flags.append('-DDG_JIT_USE_RUNTIME_API')
class CustomBuildPy(build_py):
def run(self):
# First, prepare the include directories
self.prepare_includes()
# Second, make clusters' cache setting default into `envs.py`
self.generate_default_envs()
# Finally, run the regular build
build_py.run(self)
def generate_default_envs(self):
code = '# Pre-installed environment variables\n'
code += 'persistent_envs = dict()\n'
for name in ('DG_JIT_CACHE_DIR', 'DG_JIT_PRINT_COMPILER_COMMAND', 'DG_JIT_CPP_STANDARD'):
code += f"persistent_envs['{name}'] = '{os.environ[name]}'\n" if name in os.environ else ''
with open(os.path.join(self.build_lib, 'deep_gemm', 'envs.py'), 'w') as f:
f.write(code)
def prepare_includes(self):
# Create temporary build directory instead of modifying package directory
build_include_dir = os.path.join(self.build_lib, 'deep_gemm/include')
os.makedirs(build_include_dir, exist_ok=True)
def get_tops_version(version_file_path):
tops_version = "unknown"
with open(version_file_path, 'r') as file:
tops_version = file.read().strip()
return tops_version
def get_package_version():
TOPS_VERSION = "4.0"
version_file_path = f"{current_dir}/.version"
if os.path.exists(version_file_path):
TOPS_VERSION = get_tops_version(version_file_path)
PACKAGE_VERSION = os.getenv('PACKAGE_VERSION', default='')
if PACKAGE_VERSION == '' or PACKAGE_VERSION == '123.456':
PACKAGE_VERSION = TOPS_VERSION + "." + subprocess.check_output(
["git", "show", "-s", "--date=format:'%Y%m%d'", "--format=%cd"],
cwd=current_dir).decode("ascii").strip().replace("'", "")
return PACKAGE_VERSION
extra_link_args = []
if __name__ == '__main__':
setup(
name="deep_gemm",
version=get_package_version(),
packages=find_packages('.'),
# package_dir={"": "python"},
ext_modules=[
CppExtension(name='deep_gemm_cpp',
sources=sources,
include_dirs=build_include_dirs,
libraries=build_libraries,
library_dirs=build_library_dirs,
extra_compile_args=cxx_flags,
extra_link_args=extra_link_args + [f'-Wl,-rpath,$ORIGIN/torch/lib:$ORIGIN/torch_gcu/lib'])
],
cmdclass={
"build_py": CustomBuildPy,
},
)