-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsetup.py
342 lines (286 loc) · 12.9 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
334
335
336
337
338
339
340
341
342
# Copyright 2024-2025 ModelCloud.ai
# Copyright 2024-2025 qubitium@modelcloud.ai
# Contact: qubitium@modelcloud.ai, x.com/qubitium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import urllib
import urllib.error
import urllib.request
from pathlib import Path
from setuptools import find_packages, setup
try:
from setuptools.command.bdist_wheel import bdist_wheel as _bdist_wheel
except BaseException:
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
except BaseException:
sys.exit("Both latest setuptools and wheel package are not found. Please upgrade to latest setuptools: `pip install -U setuptools`")
RELEASE_MODE = os.environ.get("RELEASE_MODE", None)
TORCH_CUDA_ARCH_LIST = os.environ.get("TORCH_CUDA_ARCH_LIST")
ROCM_VERSION = os.environ.get('ROCM_VERSION', None)
SKIP_ROCM_VERSION_CHECK = os.environ.get('SKIP_ROCM_VERSION_CHECK', None)
if ROCM_VERSION is not None and float(ROCM_VERSION) < 6.2 and not SKIP_ROCM_VERSION_CHECK:
sys.exit(
"GPTQModel's compatibility with ROCM versions below 6.2 has not been verified. If you wish to proceed, please set the SKIP_ROCM_VERSION_CHECK environment."
)
if TORCH_CUDA_ARCH_LIST:
arch_list = " ".join([arch for arch in TORCH_CUDA_ARCH_LIST.split() if float(arch.split('+')[0]) >= 6.0 or print(f"we do not support this compute arch: {arch}, skipped.") is not None])
if arch_list != TORCH_CUDA_ARCH_LIST:
os.environ["TORCH_CUDA_ARCH_LIST"] = arch_list
print(f"TORCH_CUDA_ARCH_LIST has been updated to '{arch_list}'")
version_vars = {}
exec("exec(open('gptqmodel/version.py').read()); version=__version__", {}, version_vars)
gptqmodel_version = version_vars['version']
BASE_WHEEL_URL = (
"https://github.com/ModelCloud/GPTQModel/releases/download/{tag_name}/{wheel_name}"
)
BUILD_CUDA_EXT = sys.platform != "darwin"
if os.environ.get("GPTQMODEL_FORCE_BUILD", None):
FORCE_BUILD = True
else:
FORCE_BUILD = False
extensions = []
common_setup_kwargs = {
"version": gptqmodel_version,
"name": "gptqmodel",
"author": "ModelCloud",
"author_email": "qubitium@modelcloud.ai",
"description": "A LLM quantization package with user-friendly apis. Based on GPTQ algorithm.",
"long_description": (Path(__file__).parent / "README.md").read_text(encoding="UTF-8"),
"long_description_content_type": "text/markdown",
"url": "https://github.com/ModelCloud/GPTQModel",
"project_urls": {
"Homepage": "https://github.com/ModelCloud/GPTQModel",
},
"keywords": ["gptq", "quantization", "large-language-models", "transformers", "4bit", "llm"],
"platforms": ["linux", "windows", "darwin"],
"classifiers": [
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: C++",
"Intended Audience :: Developers",
"Intended Audience :: Education",
"Intended Audience :: Science/Research",
"Intended Audience :: Information Technology",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Scientific/Engineering :: Information Analysis",
],
}
def get_version_tag() -> str:
if not BUILD_CUDA_EXT:
return "cpu"
import torch
if ROCM_VERSION:
return f"rocm{ROCM_VERSION}"
cuda_version = os.environ.get("CUDA_VERSION", torch.version.cuda)
if not cuda_version or not cuda_version.split("."):
print(
f"Trying to compile GPTQModel for CUDA, but Pytorch {torch.__version__} "
"is installed without CUDA support."
)
sys.exit(1)
CUDA_VERSION = "".join(cuda_version.split("."))
# For the PyPI release, the version is simply x.x.x to comply with PEP 440.
return f"cu{CUDA_VERSION[:3]}torch{'.'.join(torch.version.__version__.split('.')[:2])}"
requirements = []
if not os.getenv("CI"):
with open('requirements.txt') as f:
requirements = [line.strip() for line in f if line.strip()]
#subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
import torch # noqa: E402
if TORCH_CUDA_ARCH_LIST is None:
HAS_CUDA_V8 = any(torch.cuda.get_device_capability(i)[0] >= 8 for i in range(torch.cuda.device_count()))
got_cuda_v6 = any(torch.cuda.get_device_capability(i)[0] >= 6 for i in range(torch.cuda.device_count()))
got_cuda_between_v6_and_v8 = any(6 <= torch.cuda.get_device_capability(i)[0] < 8 for i in range(torch.cuda.device_count()))
# not validated for compute < 6
if not got_cuda_v6 and not torch.version.hip:
BUILD_CUDA_EXT = False
if sys.platform == "win32" and 'cu+' not in torch.__version__:
print("No CUDA device detected: avoid installing torch from PyPi which may not have bundle CUDA support for Windows.\nInstall via PyTorch: `https://pytorch.org/get-started/locally/`")
# if cuda compute is < 8.0, always force build since we only compile cached wheels for >= 8.0
if BUILD_CUDA_EXT and not FORCE_BUILD:
if got_cuda_between_v6_and_v8:
FORCE_BUILD = True
else:
HAS_CUDA_V8 = not ROCM_VERSION and len([arch for arch in TORCH_CUDA_ARCH_LIST.split() if float(arch.split('+')[0]) >= 8]) > 0
if RELEASE_MODE == "1":
common_setup_kwargs["version"] += f"+{get_version_tag()}"
additional_setup_kwargs = {}
include_dirs = ["gptqmodel_cuda"]
extensions = []
if BUILD_CUDA_EXT:
from distutils.sysconfig import get_python_lib
from torch.utils import cpp_extension as cpp_ext
conda_cuda_include_dir = os.path.join(get_python_lib(), "nvidia/cuda_runtime/include")
print("conda_cuda_include_dir", conda_cuda_include_dir)
if os.path.isdir(conda_cuda_include_dir):
include_dirs.append(conda_cuda_include_dir)
print(f"appending conda cuda include dir {conda_cuda_include_dir}")
extra_link_args = []
extra_compile_args = {
"cxx": [
"-O3",
"-std=c++17",
"-fopenmp",
"-lgomp",
"-DENABLE_BF16",
],
"nvcc": [
"-O3",
"-std=c++17",
"-DENABLE_BF16",
"-U__CUDA_NO_HALF_OPERATORS__",
"-U__CUDA_NO_HALF_CONVERSIONS__",
"-U__CUDA_NO_BFLOAT16_OPERATORS__",
"-U__CUDA_NO_BFLOAT16_CONVERSIONS__",
"-U__CUDA_NO_BFLOAT162_OPERATORS__",
"-U__CUDA_NO_BFLOAT162_CONVERSIONS__",
"-diag-suppress=179,39,186",
],
}
# torch >= 2.6.0 may require extensions to be build with CX11_ABI=1
CXX11_ABI = 1 if torch._C._GLIBCXX_USE_CXX11_ABI else 0
extra_compile_args["cxx"] += [f"-D_GLIBCXX_USE_CXX11_ABI={CXX11_ABI}"]
extra_compile_args["nvcc"] += [ f"-D_GLIBCXX_USE_CXX11_ABI={CXX11_ABI}" ]
# nvidia (nvcc) only compile flags that rocm doesn't support
if not ROCM_VERSION:
extra_compile_args["nvcc"] += [
"--threads",
"4",
"-Xfatbin",
"-compress-all",
"--expt-relaxed-constexpr",
"--expt-extended-lambda",
"--use_fast_math",
]
extensions = [
cpp_ext.CUDAExtension(
"gptqmodel_cuda_64",
[
"gptqmodel_ext/cuda_64/gptqmodel_cuda_64.cpp",
"gptqmodel_ext/cuda_64/gptqmodel_cuda_kernel_64.cu"
],
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
),
cpp_ext.CUDAExtension(
"gptqmodel_cuda_256",
[
"gptqmodel_ext/cuda_256/gptqmodel_cuda_256.cpp",
"gptqmodel_ext/cuda_256/gptqmodel_cuda_kernel_256.cu"
],
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
),
]
if sys.platform != "win32":# TODO: VC++: fatal error C1061: compiler limit : blocks nested too deeply
# https://rocm.docs.amd.com/projects/HIPIFY/en/docs-6.1.0/tables/CUDA_Device_API_supported_by_HIP.html
# nv_bfloat16 and nv_bfloat162 (2x bf16) missing replacement in ROCm
if HAS_CUDA_V8 and not ROCM_VERSION:
marlin_kernel = cpp_ext.CUDAExtension(
"gptqmodel_marlin_kernels",
[
"gptqmodel_ext/marlin/marlin_cuda.cpp",
"gptqmodel_ext/marlin/marlin_cuda_kernel.cu",
"gptqmodel_ext/marlin/marlin_repack.cu",
],
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
)
extensions.append(marlin_kernel)
elif not HAS_CUDA_V8:
print("marlin kernel only supports compute capability >= 8.0, there's no such cuda device, skipped.")
extensions += [
# TODO: VC++: error lnk2001 unresolved external symbol cublasHgemm
cpp_ext.CUDAExtension(
"gptqmodel_exllama_kernels",
[
"gptqmodel_ext/exllama/exllama_ext.cpp",
"gptqmodel_ext/exllama/cuda_buffers.cu",
"gptqmodel_ext/exllama/cuda_func/column_remap.cu",
"gptqmodel_ext/exllama/cuda_func/q4_matmul.cu",
"gptqmodel_ext/exllama/cuda_func/q4_matrix.cu",
],
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
),
# TODO: VC++: error lnk2001 unresolved external symbol cublasHgemm
cpp_ext.CUDAExtension(
"gptqmodel_exllamav2_kernels",
[
"gptqmodel_ext/exllamav2/ext.cpp",
"gptqmodel_ext/exllamav2/cuda/q_matrix.cu",
"gptqmodel_ext/exllamav2/cuda/q_gemm.cu",
],
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
)
]
additional_setup_kwargs = {"ext_modules": extensions, "cmdclass": {"build_ext": cpp_ext.BuildExtension}}
class CachedWheelsCommand(_bdist_wheel):
def run(self):
if FORCE_BUILD or torch.xpu.is_available():
return super().run()
python_version = f"cp{sys.version_info.major}{sys.version_info.minor}"
wheel_filename = f"{common_setup_kwargs['name']}-{gptqmodel_version}+{get_version_tag()}-{python_version}-{python_version}-linux_x86_64.whl"
wheel_url = BASE_WHEEL_URL.format(tag_name=f"v{gptqmodel_version}", wheel_name=wheel_filename)
print(f"Guessing wheel URL: {wheel_url}\nwheel name={wheel_filename}")
try:
urllib.request.urlretrieve(wheel_url, wheel_filename)
if not os.path.exists(self.dist_dir):
os.makedirs(self.dist_dir)
impl_tag, abi_tag, plat_tag = self.get_tag()
archive_basename = f"{common_setup_kwargs['name']}-{gptqmodel_version}+{get_version_tag()}-{impl_tag}-{abi_tag}-{plat_tag}"
wheel_path = os.path.join(self.dist_dir, archive_basename + ".whl")
print("Raw wheel path", wheel_path)
os.rename(wheel_filename, wheel_path)
except BaseException:
print(f"Precompiled wheel not found in url: {wheel_url}. Building from source...")
# If the wheel could not be downloaded, build from source
super().run()
setup(
packages=find_packages(),
install_requires=requirements,
extras_require={
"test": ["pytest>=8.2.2", "parameterized"],
"quality": ["ruff==0.4.9", "isort==5.13.2"],
'vllm': ["vllm>=0.6.4", "flashinfer==0.1.6"],
'sglang': ["sglang>=0.3.2", "flashinfer==0.1.6"],
'bitblas': ["bitblas==0.0.1-dev13"],
'hf': ["optimum>=1.21.2"],
'ipex': ["intel_extension_for_pytorch>=2.5.0"],
'auto_round': ["auto_round>=0.3"],
'logger': ["clearml", "random_word", "plotly"],
'eval': ["lm_eval>=0.4.7", "evalplus>=0.3.1"],
'triton': ["triton>=2.0.0"],
'openai': ["uvicorn", "fastapi", "pydantic"],
'mlx': ["mlx_lm>=0.20.6"]
},
include_dirs=include_dirs,
python_requires=">=3.9.0",
cmdclass={"bdist_wheel": CachedWheelsCommand, "build_ext": cpp_ext.BuildExtension}
if BUILD_CUDA_EXT
else {
"bdist_wheel": CachedWheelsCommand,
},
ext_modules=extensions,
license="Apache 2.0",
**common_setup_kwargs
)