-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathsetup.py
More file actions
346 lines (291 loc) · 10.9 KB
/
setup.py
File metadata and controls
346 lines (291 loc) · 10.9 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
import os
import shutil
import sys
from setuptools import Distribution, setup
from setuptools.command.build_ext import build_ext
this_dir = os.path.dirname(os.path.abspath(__file__))
PACKAGE_NAME = "amd-aiter"
BUILD_TARGET = os.environ.get("BUILD_TARGET", "auto")
PREBUILD_KERNELS = int(os.environ.get("PREBUILD_KERNELS", 0))
ENABLE_CK = int(os.environ.get("ENABLE_CK", "1"))
def getMaxJobs():
# calculate the maximum allowed NUM_JOBS based on cores
max_num_jobs_cores = max(1, os.cpu_count() * 0.8)
try:
import psutil
# calculate the maximum allowed NUM_JOBS based on free memory
free_memory_gb = psutil.virtual_memory().available / (1024**3)
max_num_jobs_memory = int(free_memory_gb / 0.5) # assuming 0.5 GB per job
except ImportError:
# psutil may not be available during metadata extraction
max_num_jobs_memory = max_num_jobs_cores
# pick lower value of jobs based on cores vs memory metric to minimize oom and swap usage during compilation
max_jobs = int(max(1, min(max_num_jobs_cores, max_num_jobs_memory)))
return max_jobs
def is_develop_mode():
for arg in sys.argv:
if arg == "develop":
return True
# pip install -e
elif "editable" in arg:
return True
return False
def write_install_mode():
"""Write install_mode so core.py uses aiter_meta/ (install) vs repo root (develop).
Called here so the file exists when setuptools resolves package_data,
and again in build_ext.run() to ensure it's written for develop mode too.
"""
mode = "develop" if is_develop_mode() else "install"
with open("./aiter/install_mode", "w") as f:
f.write(mode)
def prepare_packaging():
"""Copy source directories and create package metadata for non-editable installs."""
if os.path.exists("aiter_meta") and os.path.isdir("aiter_meta"):
shutil.rmtree("aiter_meta")
if ENABLE_CK:
shutil.copytree("3rdparty", "aiter_meta/3rdparty")
else:
os.makedirs("aiter_meta/3rdparty", exist_ok=True)
shutil.copytree("hsa", "aiter_meta/hsa")
shutil.copytree("gradlib", "aiter_meta/gradlib")
shutil.copytree("csrc", "aiter_meta/csrc")
open("aiter_meta/__init__.py", "w").close()
write_install_mode()
if is_develop_mode():
packages = ["aiter"]
write_install_mode()
else:
prepare_packaging()
packages = ["aiter_meta", "aiter"]
def _is_metadata_only():
_skip = frozenset(
{
"egg_info",
"dist_info",
"clean",
"--version",
"--name",
"--fullname",
"--author",
"--author-email",
"--url",
"--license",
"--classifiers",
}
)
return len(sys.argv) < 2 or sys.argv[1] in _skip
# Defer heavy imports until build time
if not _is_metadata_only():
import json
from concurrent.futures import ThreadPoolExecutor
sys.path.insert(0, f"{this_dir}/aiter/")
from jit import core
from jit.utils.cpp_extension import IS_HIP_EXTENSION
# Determine build target
if BUILD_TARGET == "auto":
IS_ROCM = IS_HIP_EXTENSION
elif BUILD_TARGET == "rocm":
IS_ROCM = True
else:
IS_ROCM = False
if not IS_ROCM:
raise NotImplementedError("Only ROCM is supported")
ck_dir = os.environ.get("CK_DIR", f"{this_dir}/3rdparty/composable_kernel")
if ENABLE_CK:
assert os.path.exists(ck_dir), (
"CK is needed by aiter, please make sure clone by "
'"git clone --recursive https://github.com/ROCm/aiter.git" or '
'"git submodule sync ; git submodule update --init --recursive"'
)
def _load_modules_from_config():
cfg_path = os.path.join(this_dir, "aiter", "jit", "optCompilerConfig.json")
try:
with open(cfg_path, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
return []
if isinstance(data, dict):
return list(data.keys())
return []
def get_exclude_ops():
all_modules = _load_modules_from_config()
exclude_ops = []
# When CK is disabled, exclude all CK-dependent modules
if not ENABLE_CK:
exclude_ops.extend(sorted(core._get_ck_exclude_modules()))
return exclude_ops
for module in all_modules:
if PREBUILD_KERNELS == 1:
if "_tune" in module or module == "module_gemm_mi350_a8w8_blockscale_asm":
exclude_ops.append(module)
if "mha" in module and module not in [
"module_fmha_v3_fwd",
"module_fmha_v3_varlen_fwd",
]:
exclude_ops.append(module)
elif PREBUILD_KERNELS == 2:
# Exclude _bwd, _tune, and specific module
if (
"_bwd" in module
or "_tune" in module
or module == "module_gemm_mi350_a8w8_blockscale_asm"
):
exclude_ops.append(module)
elif PREBUILD_KERNELS == 3:
# Keep only module_fmha_v3* and module_aiter_enum
if not (
module.startswith("module_fmha_v3")
or module == "module_aiter_enum"
or module == "module_gemm_mi350_a8w8_blockscale_asm"
):
exclude_ops.append(module)
else:
# Default behavior: exclude tunes and specific mi350 module
if "_tune" in module or module == "module_gemm_mi350_a8w8_blockscale_asm":
exclude_ops.append(module)
return exclude_ops
if PREBUILD_KERNELS != 0:
has_torch = True
try:
import torch as _
except Exception:
has_torch = False
if not has_torch:
print(
"[aiter] PREBUILD_KERNELS set but torch not installed, "
"skip precompilation in this environment"
)
else:
from jit.utils.mha_recipes import (
get_mha_varlen_prebuild_variants_by_names,
)
import glob
exclude_ops = get_exclude_ops()
all_opts_args_build, _ = core.get_args_of_build("all", exclude=exclude_ops)
if PREBUILD_KERNELS == 1 and ENABLE_CK:
extra_args_build = []
req_md_names = [
"mha_varlen_fwd_bf16_nlogits_nbias_mask_nlse_ndropout_nskip_nqscale",
"mha_varlen_fwd_bf16_nlogits_nbias_nmask_lse_ndropout_nskip_nqscale",
]
variants = get_mha_varlen_prebuild_variants_by_names(req_md_names, ck_dir)
base_args = core.get_args_of_build("module_mha_varlen_fwd")
for v in variants:
if not isinstance(base_args, dict) or not base_args.get("srcs"):
continue
extra_args_build.append(
{
"md_name": v["md_name"],
"srcs": base_args["srcs"],
"flags_extra_cc": base_args["flags_extra_cc"],
"flags_extra_hip": base_args["flags_extra_hip"],
"extra_include": base_args["extra_include"],
"blob_gen_cmd": v["blob_gen_cmd"],
"third_party": base_args["third_party"],
}
)
all_opts_args_build.extend(extra_args_build)
bd = f"{core.get_user_jit_dir()}/build"
shutil.rmtree(bd, ignore_errors=True)
for f in glob.glob(f"{core.get_user_jit_dir()}/*.so"):
try:
os.remove(f)
except Exception:
pass
def build_one_module(one_opt_args):
flags_cc = list(one_opt_args["flags_extra_cc"]) + [
f"-DPREBUILD_KERNELS={PREBUILD_KERNELS}"
]
flags_hip = list(one_opt_args["flags_extra_hip"]) + [
f"-DPREBUILD_KERNELS={PREBUILD_KERNELS}"
]
core.build_module(
md_name=one_opt_args["md_name"],
srcs=one_opt_args["srcs"],
flags_extra_cc=flags_cc,
flags_extra_hip=flags_hip,
blob_gen_cmd=one_opt_args["blob_gen_cmd"],
extra_include=one_opt_args["extra_include"],
extra_ldflags=None,
verbose=False,
is_python_module=True,
is_standalone=False,
torch_exclude=False,
third_party=one_opt_args["third_party"],
)
prebuid_thread_num = 5
max_jobs = os.environ.get("MAX_JOBS")
if max_jobs is not None and max_jobs.isdigit() and int(max_jobs) > 0:
prebuid_thread_num = min(prebuid_thread_num, int(max_jobs))
else:
prebuid_thread_num = min(prebuid_thread_num, getMaxJobs())
os.environ["PREBUILD_THREAD_NUM"] = str(prebuid_thread_num)
with ThreadPoolExecutor(max_workers=prebuid_thread_num) as executor:
list(executor.map(build_one_module, all_opts_args_build))
class NinjaBuildExtension(build_ext):
"""Custom build_ext that defers expensive operations until run() is called."""
def run(self):
# Set MAX_JOBS for ninja
max_jobs_env = os.environ.get("MAX_JOBS")
if max_jobs_env is None:
max_jobs = getMaxJobs()
os.environ["MAX_JOBS"] = str(max_jobs)
else:
try:
if int(max_jobs_env) <= 0:
raise ValueError("MAX_JOBS must be a positive integer")
except ValueError:
max_jobs = getMaxJobs()
os.environ["MAX_JOBS"] = str(max_jobs)
# Run the actual build
super().run()
setup_requires = [
"packaging",
"psutil",
"ninja",
"setuptools_scm",
]
if PREBUILD_KERNELS != 0:
setup_requires.append("pandas")
class ForcePlatlibDistribution(Distribution):
def has_ext_modules(self):
return True
setup(
name=PACKAGE_NAME,
use_scm_version=True,
packages=packages,
include_package_data=True,
package_data={
"": ["*"],
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: BSD License",
"Operating System :: Unix",
],
cmdclass={"build_ext": NinjaBuildExtension},
python_requires=">=3.8",
install_requires=[
"pybind11>=3.0.1",
"ninja",
"pandas",
"einops",
"psutil",
"packaging",
],
extras_require={
# Triton-based communication using Iris
# Note: Iris is not available on PyPI and must be installed separately
# Install with: pip install -r requirements-triton-comms.txt
# (See requirements-triton-comms.txt for pinned Iris version)
"triton_comms": [],
# Install all optional dependencies
"all": [],
},
setup_requires=setup_requires,
distclass=ForcePlatlibDistribution,
)
if os.path.exists("aiter_meta") and os.path.isdir("aiter_meta"):
shutil.rmtree("aiter_meta")