-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
178 lines (153 loc) · 6.36 KB
/
Copy pathsetup.py
File metadata and controls
178 lines (153 loc) · 6.36 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
from __future__ import annotations
import os
import platform
import shutil
import sys
import warnings
from setuptools import setup
_METADATA_COMMANDS = {"egg_info", "dist_info", "sdist"}
_MIN_TORCH_VERSION = (2, 6)
def _metadata_only_command() -> bool:
return any(arg in _METADATA_COMMANDS for arg in sys.argv[1:])
def _cuda_extensions():
if _metadata_only_command():
return [], {}
mode = os.environ.get("ARENO_BUILD_EXT", "1").lower()
if mode in {"0", "false", "no", "off"}:
return [], {}
_check_supported_build_platform()
torch = _require_torch()
_check_torch_version(torch)
_check_cuda_torch(torch)
try:
import psutil # noqa: F401
except ImportError as exc:
raise RuntimeError(
"AReno build-time setup failed: missing dependency `psutil`.\n"
"Why: AReno builds CUDA extensions with `--no-build-isolation`, and PyTorch's CUDA extension builder "
"imports psutil while sizing parallel compile jobs.\n"
"Next steps: run `pip install psutil`, then retry `pip install -e . --no-build-isolation`."
) from exc
_set_default_cuda_arch_list()
from torch.utils.cpp_extension import CUDA_HOME, BuildExtension, CUDAExtension
if CUDA_HOME is None:
raise RuntimeError(
"AReno build-time setup failed: CUDA_HOME is not set.\n"
"Why: building areno.accel requires the CUDA toolkit, not just a PyTorch CUDA wheel.\n"
"Next steps: install the CUDA toolkit and export CUDA_HOME=/usr/local/cuda, or set "
"ARENO_BUILD_EXT=0 only for docs/metadata installs that will not train or serve."
)
nvcc = shutil.which("nvcc") or shutil.which(str(os.path.join(CUDA_HOME, "bin", "nvcc")))
if nvcc is None:
raise RuntimeError(
"AReno build-time setup failed: nvcc was not found.\n"
"Why: building areno.accel requires CUDA's compiler in PATH or under CUDA_HOME/bin.\n"
"Next steps: add CUDA's bin directory to PATH (`export PATH=$CUDA_HOME/bin:$PATH`) and retry."
)
return [
CUDAExtension(
"areno.accel._areno_accel",
sources=[
"areno/accel/csrc/extension.cpp",
"areno/accel/csrc/activation.cu",
"areno/accel/csrc/attention.cu",
"areno/accel/csrc/conv.cu",
"areno/accel/csrc/embedding.cu",
"areno/accel/csrc/linear.cu",
"areno/accel/csrc/moe_align_kernel.cu",
"areno/accel/csrc/moe_permute.cu",
"areno/accel/csrc/normalization.cu",
"areno/accel/csrc/router.cu",
"areno/accel/csrc/topk.cu",
],
extra_compile_args={
"cxx": ["-O3", "-Wno-deprecated-declarations"],
"nvcc": ["-O3", "--use_fast_math", "-Xcompiler", "-Wno-deprecated-declarations"],
},
)
], {"build_ext": BuildExtension}
def _check_supported_build_platform() -> None:
system = platform.system()
machine = platform.machine().lower()
if system == "Linux" and machine in {"x86_64", "amd64", "aarch64", "arm64"}:
return
raise RuntimeError(
f"AReno runtime install is not supported on this platform: {system} {platform.machine()}.\n"
"Why: AReno training/serving requires Linux with NVIDIA CUDA.\n"
"Next steps: use Linux x86_64/aarch64 with an NVIDIA GPU, Windows WSL2, or set ARENO_BUILD_EXT=0 "
"for docs/metadata-only installs on unsupported platforms."
)
def _require_torch():
try:
import torch
except ImportError as exc:
raise RuntimeError(
"AReno build-time setup failed: PyTorch is not installed.\n"
"Why: areno.accel is compiled with PyTorch's CUDA extension tooling.\n"
"Next steps: install CUDA-enabled PyTorch >= 2.6 first, then retry with `--no-build-isolation`."
) from exc
return torch
def _check_torch_version(torch) -> None:
version = getattr(torch, "__version__", "")
if _version_at_least(version, _MIN_TORCH_VERSION):
return
raise RuntimeError(
f"AReno build-time setup failed: unsupported PyTorch version {version or '<unknown>'}.\n"
"Why: AReno requires PyTorch >= 2.6 for its local runtime and CUDA extension ABI.\n"
"Next steps: upgrade to CUDA-enabled PyTorch >= 2.6, then retry the AReno install."
)
def _check_cuda_torch(torch) -> None:
cuda_build = getattr(getattr(torch, "version", None), "cuda", None)
if cuda_build:
return
raise RuntimeError(
"AReno build-time setup failed: this PyTorch install is CPU-only.\n"
"Why: AReno training/serving requires a CUDA-enabled PyTorch build and NVIDIA CUDA extensions.\n"
"Next steps: install a CUDA-enabled PyTorch wheel matching your CUDA toolkit, then retry."
)
def _set_default_cuda_arch_list() -> None:
"""Default CUDA extension builds to the visible GPU architectures."""
if os.environ.get("TORCH_CUDA_ARCH_LIST"):
return
try:
import torch
except ImportError:
return
if not torch.cuda.is_available():
return
archs: set[str] = set()
try:
for idx in range(torch.cuda.device_count()):
major, minor = torch.cuda.get_device_capability(idx)
archs.add(f"{major}.{minor}")
except Exception:
return
if not archs:
return
arch_list = ";".join(sorted(archs))
os.environ["TORCH_CUDA_ARCH_LIST"] = arch_list
warnings.warn(
"TORCH_CUDA_ARCH_LIST is not set; building areno.accel only for "
f"visible CUDA architecture(s): {arch_list}. Set TORCH_CUDA_ARCH_LIST "
"explicitly to build for other GPUs.",
RuntimeWarning,
stacklevel=2,
)
def _version_at_least(version: str | None, minimum: tuple[int, int]) -> bool:
if not version:
return False
parts: list[int] = []
for piece in version.split("+", 1)[0].split("."):
digits = ""
for char in piece:
if not char.isdigit():
break
digits += char
if not digits:
break
parts.append(int(digits))
while len(parts) < len(minimum):
parts.append(0)
return tuple(parts[: len(minimum)]) >= minimum
ext_modules, cmdclass = _cuda_extensions()
setup(ext_modules=ext_modules, cmdclass=cmdclass)