-
Notifications
You must be signed in to change notification settings - Fork 24
/
setup.py
282 lines (241 loc) · 9.98 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
# DEBUG build with debug
#
# USE_SYSTEM_ONECCL=0
# disables use of system-wide oneCCL (we will use our submoduled
# copy in third_party/oneCCL)
import os
import sys
import pathlib
import shutil
from subprocess import check_call, check_output
import torch
from torch.utils.cpp_extension import BuildExtension, CppExtension, library_paths
from setuptools import setup
from distutils.command.clean import clean
from tools.setup.cmake import CMakeExtension
from tools.setup.env import get_compiler
# Constant known variables used throughout this file
CWD = os.path.dirname(os.path.abspath(__file__))
ONECCL_BINDINGS_FOR_PYTORCH_PATH = os.path.join(CWD, "oneccl_bindings_for_pytorch")
def _check_env_flag(name, default=''):
return os.getenv(name, default).upper() in ['ON', '1', 'YES', 'TRUE', 'Y']
def check_file(f):
if not os.path.exists(f):
print("Could not find {}".format(f))
print("Did you run 'git submodule update --init --recursive'?")
sys.exit(1)
# all the work we need to do _before_ setup runs
def create_version():
"""Create the version string for torch-ccl"""
package_name = os.getenv('CCL_PACKAGE_NAME', 'oneccl-bind-pt')
version = open('version.txt', 'r').read().strip()
sha = 'Unknown'
try:
sha = check_output(['git', 'rev-parse', 'HEAD'], cwd=CWD).decode('ascii').strip()
except Exception:
pass
if os.getenv('CCL_SHA_VERSION', False):
if sha != 'Unknown':
version += '+' + sha[:7]
if os.environ.get("COMPUTE_BACKEND") == "dpcpp":
backend = "gpu"
else:
backend = os.environ.get("ONECCL_BINDINGS_FOR_PYTORCH_BACKEND", "cpu")
if "+" not in version:
version += '+' + backend
print("Building {}-{}".format(package_name, version))
version_path = os.path.join(CWD, 'oneccl_bindings_for_pytorch', 'version.py')
with open(version_path, 'w') as f:
f.write("__version__ = '{}'\n".format(version))
f.write("git_version = {}\n".format(repr(sha)))
return version, package_name
class BuildCMakeExt(BuildExtension):
"""
Builds using cmake instead of the python setuptools implicit build
"""
def run(self):
"""
Perform build_cmake before doing the 'normal' stuff
"""
cmake_extensions = [ext for ext in self.extensions if isinstance(ext, CMakeExtension)]
for ext in cmake_extensions:
self.build_cmake(ext)
self.extensions = [ext for ext in self.extensions if not isinstance(ext, CMakeExtension)]
super(BuildCMakeExt, self).run()
build_py = self.get_finalized_command('build_py')
build_py.data_files = build_py._get_data_files()
build_py.run()
def build_cmake(self, extension: CMakeExtension):
"""
The steps required to build the extension
"""
build_dir = pathlib.Path('.'.join([self.build_temp, extension.name]))
build_dir.mkdir(parents=True, exist_ok=True)
install_dir = ONECCL_BINDINGS_FOR_PYTORCH_PATH
# Now that the necessary directories are created, build
my_env = os.environ.copy()
my_env["CMAKE_DISABLE_FIND_PACKAGE_MKL"] = "TRUE"
build_type = 'Release'
if _check_env_flag('DEBUG'):
build_type = 'Debug'
build_options = {
'CMAKE_BUILD_TYPE': build_type,
# The value cannot be easily obtained in CMakeLists.txt.
'CMAKE_PREFIX_PATH': torch.utils.cmake_prefix_path,
# skip the example and test code in oneCCL
'BUILD_EXAMPLES': 'OFF',
'BUILD_CONFIG': 'OFF',
'BUILD_FT': 'OFF'
}
compute_backend = os.getenv('COMPUTE_BACKEND', 'n/a')
runtime = 'gcc'
if compute_backend == 'dpcpp':
runtime = 'dpcpp'
build_options['COMPUTE_BACKEND'] = compute_backend
import intel_extension_for_pytorch
build_options['CMAKE_PREFIX_PATH'] += ";" + intel_extension_for_pytorch.cmake_prefix_path
if "DPCPP_GCC_INSTALL_DIR" in my_env:
exist_cflags = "CFLAGS" in my_env
cflags = ""
if exist_cflags:
cflags = my_env["CFLAGS"]
my_env["CFLAGS"] = f"--gcc-install-dir={my_env['DPCPP_GCC_INSTALL_DIR']} {cflags}"
exist_cxxflags = "CXXFLAGS" in my_env
cxxflags = ""
if exist_cxxflags:
cxxflags = my_env["CXXFLAGS"]
my_env["CXXFLAGS"] = f"--gcc-install-dir={my_env['DPCPP_GCC_INSTALL_DIR']} {cxxflags}"
exist_ldflags = "LDFLAGS" in my_env
ldflags = ""
if exist_ldflags:
ldflags = my_env["LDFLAGS"]
my_env["LDFLAGS"] = f"--gcc-install-dir={my_env['DPCPP_GCC_INSTALL_DIR']} -fuse-ld=lld -lrt -lpthread {ldflags}"
cc, cxx = get_compiler(runtime)
build_options['CMAKE_C_COMPILER'] = cc
build_options['CMAKE_CXX_COMPILER'] = cxx
extension.generate(build_options, my_env, build_dir, install_dir)
if compute_backend == 'dpcpp':
if "DPCPP_GCC_INSTALL_DIR" in my_env:
if exist_cflags:
my_env["CFLAGS"] = cflags
else:
del my_env["CFLAGS"]
if exist_cxxflags:
my_env["CXXFLAGS"] = cxxflags
else:
del my_env["CXXFLAGS"]
if exist_ldflags:
my_env["LDFLAGS"] = ldflags
else:
del my_env["LDFLAGS"]
build_args = ['-j', str(os.cpu_count())]
check_call(['make', 'oneccl_bindings_for_pytorch'] + build_args, cwd=str(build_dir))
if compute_backend == 'dpcpp':
check_call(['make', 'oneccl_bindings_for_pytorch_xpu'] + build_args, cwd=str(build_dir))
check_call(['make', 'install'], cwd=str(build_dir))
class Clean(clean):
def run(self):
import glob
import re
with open('.gitignore', 'r') as f:
ignores = f.read()
pat = re.compile(r'^#( BEGIN NOT-CLEAN-FILES )?')
for wildcard in filter(None, ignores.split('\n')):
match = pat.match(wildcard)
if match:
if match.group(1):
# Marker is found and stop reading .gitignore.
break
# Ignore lines which begin with '#'.
else:
for filename in glob.glob(wildcard):
try:
os.remove(filename)
except OSError:
shutil.rmtree(filename, ignore_errors=True)
clean.run(self)
def get_python_c_module():
main_compile_args = []
main_libraries = ['oneccl_bindings_for_pytorch']
main_link_args = []
main_sources = ["oneccl_bindings_for_pytorch/csrc/_C.cpp", "oneccl_bindings_for_pytorch/csrc/init.cpp"]
lib_path = os.path.join(ONECCL_BINDINGS_FOR_PYTORCH_PATH, "lib")
library_dirs = [lib_path]
include_path = os.path.join(CWD, "src")
include_dirs = [include_path]
extra_link_args = []
extra_compile_args = [
'-Wall',
'-Wextra',
'-Wno-strict-overflow',
'-Wno-unused-parameter',
'-Wno-missing-field-initializers',
'-Wno-write-strings',
'-Wno-unknown-pragmas',
# This is required for Python 2 declarations that are deprecated in 3.
'-Wno-deprecated-declarations',
# Python 2.6 requires -fno-strict-aliasing, see
# http://legacy.python.org/dev/peps/pep-3123/
# We also depend on it in our code (even Python 3).
'-fno-strict-aliasing',
# Clang has an unfixed bug leading to spurious missing
# braces warnings, see
# https://bugs.llvm.org/show_bug.cgi?id=21629
'-Wno-missing-braces',
]
def make_relative_rpath(path):
ret = []
ret.append('-Wl,-rpath,$ORIGIN/' + path)
if os.getenv('COMPUTE_BACKEND', 'n/a') == 'dpcpp':
ret.append('-Wl,-rpath,$ORIGIN/../../../')
ret.append('-Wl,--disable-new-dtags')
return ret
_c_module = CppExtension("oneccl_bindings_for_pytorch._C",
libraries=main_libraries,
sources=main_sources,
language='c',
extra_compile_args=main_compile_args + extra_compile_args,
include_dirs=include_dirs,
library_dirs=library_dirs,
extra_link_args=extra_link_args + main_link_args + make_relative_rpath('lib'))
return _c_module
if __name__ == '__main__':
version, package_name = create_version()
c_module = get_python_c_module()
cmake_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "CMakeLists.txt")
modules = [CMakeExtension("liboneccl_bindings_for_pytorch", cmake_file), c_module]
setup(
name=package_name,
version=version,
ext_modules=modules,
packages=['oneccl_bindings_for_pytorch'],
package_data={
'oneccl_bindings_for_pytorch': [
'*.py',
'*/*.h',
'*/*.hpp',
'lib/*.so*',
'opt/mpi/lib/*.so*',
'bin/*',
'opt/mpi/bin/*',
'env/*',
'etc/*',
'opt/mpi/etc/*',
'examples/*',
'include/native_device_api/*.h*',
'include/native_device_api/l0/*.h*',
'include/*.h*',
'opt/mpi/include/*.h*',
'lib/lib*',
'opt/mpi/libfabric/lib/lib*',
'lib/prov/lib*',
'lib/ccl/kernels/*',
'opt/mpi/libfabric/lib/prov/lib*',
'licensing/*',
'modulefiles/*',
]},
cmdclass={
'build_ext': BuildCMakeExt,
'clean': Clean,
}
)