This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
setup.py
333 lines (279 loc) · 13.3 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""Setup and install modules."""
import importlib
import os
import subprocess
import sys
import time
from io import open
from pathlib import Path
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
from setuptools_scm import get_version
result = subprocess.Popen("pip install -r requirements.txt", shell=True)
result.wait()
def is_intel_gpu_available():
import torch
import intel_extension_for_pytorch as ipex
return hasattr(torch, "xpu") and torch.xpu.is_available()
def is_habana_hpu_available():
try:
import habana_frameworks.torch.hpu as hthpu
return True
except ImportError:
return False
def check_env_flag(name: str, default: bool = False) -> bool:
if default: # if a flag meant to be true if not set / mal-formatted
return not os.getenv(name, "").upper() in ["OFF", "0", "FALSE", "NO", "N"]
else:
return os.getenv(name, "").upper() in ["ON", "1", "TRUE", "YES", "Y"]
SKIP_RUNTIME = check_env_flag("SKIP_RUNTIME", False)
"""Whether to only packaging optimization."""
RUNTIME_ONLY = check_env_flag("RUNTIME_ONLY", False)
"""Whether to only packaging backends."""
ipex_available = importlib.util.find_spec(
"intel_extension_for_pytorch") is not None
IS_INTEL_GPU = False
if ipex_available and is_intel_gpu_available():
SKIP_RUNTIME = True
RUNTIME_ONLY = False
IS_INTEL_GPU = True
elif not is_habana_hpu_available():
result = subprocess.Popen(
"pip install -r requirements-cpu.txt", shell=True)
result.wait()
if not IS_INTEL_GPU:
from cmake import CMAKE_BIN_DIR
from cpuinfo import get_cpu_info
cpu_flags = get_cpu_info()['flags']
CMAKE_BUILD_TYPE = os.environ.get("CMAKE_BUILD_TYPE", "Release")
"""Whether to build with -O0 / -O3 / -g; could be one of Debug / Release / RelWithDebInfo; default to Release."""
CMAKE_GENERATOR = os.environ.get("CMAKE_GENERATOR", "Ninja")
"""The CMake generator to be used; default to Ninja."""
CMAKE_ARGS = os.environ.get("CMAKE_ARGS", "")
"""Adding CMake arguments set as environment variable (needed e.g. to build for GPU support on conda-forge)"""
CMAKE_BUILD_PARALLEL_LEVEL = os.environ.get(
"CMAKE_BUILD_PARALLEL_LEVEL", "")
"""Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level across all generators."""
NE_WITH_AVX2 = check_env_flag("NE_WITH_AVX2", 'avx512f' not in cpu_flags)
"""Whether to limit the max ISA used to AVX2; otherwise AVX512 will be used; set to ON/OFF."""
cwd = os.path.dirname(os.path.abspath(__file__))
# define install requirements
install_requires_list = ['packaging', 'numpy', 'schema', 'pyyaml']
opt_install_requires_list = ['neural_compressor', 'transformers']
packages_list = find_packages()
install_requires_list.extend(opt_install_requires_list)
class CMakeExtension(Extension):
"""CMakeExtension class."""
def __init__(self, name, sourcedir="", lib_only=False):
"""Init a CMakeExtension object."""
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
# we only deliver shared object but not as a python extension module
self.optional = lib_only
class CMakeBuild(build_ext):
"""Extension builder."""
_copy_targes: bool = False
@staticmethod
def _is_target_file(file_name: str) -> bool:
if file_name.endswith(".dll") or file_name.endswith(".exe") or file_name.endswith(".pyd"):
return True
if file_name.endswith(".so") or ".so." in file_name:
return True
if sys.platform == "linux" and ('.' not in file_name):
return True
return False
@staticmethod
def _get_files(scope: str, repo: str):
"""Equivalent of `git ls-files --recurse-submodules -- $scope` for git-v1.x."""
files = [os.path.join(repo, f) for f in subprocess.check_output(
["git", "ls-files", "--", scope], cwd=repo
).decode("utf-8").splitlines()]
submodules = subprocess.check_output(
["git", "submodule", "--quiet", "foreach", f'echo $sm_path'], cwd=repo).decode("utf-8").splitlines()
for sm in submodules:
sm_path = os.path.join(repo, sm)
files.extend(CMakeBuild._get_files(sm_path, sm_path))
return files
def get_source_files(self):
"""The primary purpose of this function is to help populating the `sdist` with all the files necessary to build the distribution.
-- setuptools doc
"""
files = super().get_source_files()
if not os.path.isdir(os.path.join(cwd, ".git")):
return files
for ext in self.extensions:
if not isinstance(ext, CMakeExtension):
continue
files.extend(os.path.relpath(f, cwd)
for f in self._get_files(ext.sourcedir, cwd))
return files
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)
extdir = ext_fullpath.parent.resolve()
output_dir = f"{extdir}{os.sep}"
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={output_dir}",
f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={output_dir}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{CMAKE_BUILD_TYPE.upper()}={output_dir}",
f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{CMAKE_BUILD_TYPE.upper()}={output_dir}",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_BUILD_TYPE={CMAKE_BUILD_TYPE}",
f"-DNE_VERSION_STRING={self.distribution.get_version()}",
f"-DNE_WITH_AVX2={'ON' if NE_WITH_AVX2 else 'OFF'}",
f"-DNE_WITH_TESTS=OFF",
]
if sys.platform == "linux": # relative_rpath
cmake_args.append('-DCMAKE_BUILD_RPATH=$ORIGIN/')
build_args = []
my_env: dict[str, str] = os.environ.copy()
# Using Ninja-build since it a) is available as a wheel and b)
# multithreads automatically. MSVC would require all variables be
# exported for Ninja to pick it up, which is a little tricky to do.
generator = CMAKE_GENERATOR
if generator == "Ninja":
try:
import ninja
ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
cmake_args += [
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
]
except ImportError:
generator = ""
if generator:
cmake_args += [f"-G{generator}"]
if self.compiler.compiler_type == "msvc":
# Single config generators are handled "normally"
single_config = any(x in generator for x in {"NMake", "Ninja"})
# CMake allows an arch-in-generator style for backward compatibility
contains_arch = any(x in generator for x in {"Win64"})
# Specify the arch if using MSVC generator, but only if it doesn't
# contain a backward-compatibility arch spec already in the
# generator name.
PLAT_TO_CMAKE = { # Convert distutils Windows platform specifiers to CMake -A arguments
"win32": "Win32",
"win-amd64": "x64",
}
if not single_config and not contains_arch:
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
if generator == "Ninja":
# temporary solution based on that of pytorch
from distutils import _msvccompiler # type: ignore[import]
vc_env = _msvccompiler._get_vc_env("x64")
# Keys in `_get_vc_env` are always lowercase while OS environ keys are always uppercase on Windows.
# https://stackoverflow.com/a/7797329
my_env = {**my_env, **{k.upper(): v for k, v in vc_env.items()}}
# Multi-config generators have a different way to specify configs
if not single_config:
build_args += ["--config", CMAKE_BUILD_TYPE]
if CMAKE_ARGS:
cmake_args += [item for item in CMAKE_ARGS.split(" ") if item]
if not CMAKE_BUILD_PARALLEL_LEVEL:
parallel_level = getattr(self, 'parallel', '') or ''
build_args += [f"-j{parallel_level}"]
# we avoid using self.build_tmp for incremental builds
build_dir = Path("build") / ext.name.split('.')[-1]
if not build_dir.exists():
build_dir.mkdir(parents=True)
cmake_path = os.path.join(CMAKE_BIN_DIR, "cmake")
config_command = [cmake_path, *cmake_args, ext.sourcedir]
build_command = [cmake_path, "--build", ".", *build_args]
print(' '.join(config_command))
subprocess.run(config_command, cwd=build_dir, check=True, env=my_env)
print(' '.join(build_command))
subprocess.run(build_command, cwd=build_dir, check=True, env=my_env)
if (self._copy_targes):
for f in next(os.walk(output_dir))[2]:
if CMakeBuild._is_target_file(f):
self.copy_file(
os.path.join(output_dir, f),
os.path.join(cwd, *ext.name.split('.')[:-1], f)
)
def get_output_mapping(self):
mapping: dict[str, str] = getattr(super(), 'get_output_mapping')()
for ext in self.extensions:
if not isinstance(ext, CMakeExtension):
continue
build_lib = (Path(self.build_lib) /
ext.name.replace('.', os.sep)).parent
ext_dir = Path(self.get_ext_fullpath(ext.name)).parent.resolve()
for f in next(os.walk(build_lib.resolve()))[2]:
mapping[str(build_lib / f)] = str(ext_dir / f)
return mapping
def run(self) -> None:
self._copy_targes = self.inplace or \
getattr(self, 'editable_mode', False)
return super().run()
def check_submodules():
"""Check submodules information."""
if not os.path.exists(".git"):
return
try:
print(' --- Trying to initialize submodules')
start = time.time()
subprocess.check_call(
["git", "submodule", "update", "--init", "--recursive"], cwd=cwd)
end = time.time()
print(f' --- Submodule initialization took {end - start:.2f} sec')
except Exception:
print(' --- Submodule initialization failed')
print('Please run:\n\tgit submodule update --init --recursive')
sys.exit(1)
if __name__ == '__main__':
if IS_INTEL_GPU:
ext_modules = []
else:
ext_modules = [CMakeExtension(
"intel_extension_for_transformers.qbits_py", 'intel_extension_for_transformers/qbits/')]
if SKIP_RUNTIME:
subprocess.check_call(
["git", "submodule", "update", "--init", "intel_extension_for_transformers/transformers/runtime/third_party/pybind11"], cwd=cwd)
if not SKIP_RUNTIME:
check_submodules()
ext_modules.extend([
CMakeExtension("intel_extension_for_transformers.neural_engine_py",
"intel_extension_for_transformers/transformers/runtime/"),
])
cmdclass = {'build_ext': CMakeBuild}
itrex_version = get_version()
if IS_INTEL_GPU:
itrex_version = itrex_version + "-gpu"
setup(
name="intel-extension-for-transformers",
author="Intel AIA/AIPC Team",
description="Repository of Intel® Intel Extension for Transformers",
long_description=open("README.md", "r", encoding='utf-8').read(),
long_description_content_type="text/markdown",
keywords='quantization, auto-tuning, post-training static quantization, post-training dynamic quantization, quantization-aware training, tuning strategy',
license='Apache 2.0',
url="https://github.com/intel/intel-extension-for-transformers",
ext_modules=ext_modules,
packages=find_packages(),
package_dir={'': '.'},
# otherwise CMakeExtension's source files will be included in final installation
include_package_data=False,
package_data={
'': ["*.yaml", "*.mat"],
},
cmdclass=cmdclass if not IS_INTEL_GPU else {},
install_requires=install_requires_list,
entry_points={
'console_scripts': [
'neural_engine = intel_extension_for_transformers.transformers.runtime:neural_engine_bin',
'neuralchat = intel_extension_for_transformers.neural_chat.cli.cli_commands:neuralchat_execute',
'neuralchat_server = intel_extension_for_transformers.neural_chat.server.server_commands:neuralchat_server_execute',
'neuralchat_client = intel_extension_for_transformers.neural_chat.server.server_commands:neuralchat_client_execute'
]
},
python_requires='>=3.7.0',
classifiers=[
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'License :: OSI Approved :: Apache Software License',
],
setup_requires=['setuptools_scm'],
use_scm_version=True,
version=itrex_version
)