-
Notifications
You must be signed in to change notification settings - Fork 39
/
setup.py
184 lines (147 loc) · 5.99 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
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
import os
import subprocess
from pathlib import Path
from setuptools import Command, Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
cur_path = Path(__file__).parent
def get_version():
with open(cur_path / "pyproject.toml") as f:
for line in f:
if "version" in line:
return line.split("=")[-1].strip().strip('"')
return "0.0.1"
def get_requirements():
"""Get Python package dependencies from requirements.txt."""
with open(cur_path / "requirements.txt") as f:
requirements = f.read().strip().split("\n")
requirements = [req for req in requirements if "https" not in req]
return requirements
class CMakeExtension(Extension):
""" specify the root folder of the CMake projects"""
def __init__(self, name, cmake_lists_dir=".", **kwargs):
Extension.__init__(self, name, sources=[], **kwargs)
self.cmake_lists_dir = os.path.abspath(cmake_lists_dir)
class CMakeBuildExt(build_ext):
"""launches the CMake build."""
def get_ext_filename(self, name):
return f"lib{name}.so"
def copy_extensions_to_source(self) -> None:
build_py = self.get_finalized_command("build_py")
for ext in self.extensions:
source_path = os.path.join(
self.build_lib, self.get_ext_filename(ext.name)
)
inplace_file, _ = self._get_inplace_equivalent(build_py, ext)
target_path = os.path.join(
build_py.build_lib, "vptq", "ops", inplace_file
)
# Always copy, even if source is older than destination, to ensure
# that the right extensions for the current Python/platform are
# used.
if os.path.exists(source_path) or not ext.optional:
self.copy_file(source_path, target_path, level=self.verbose)
def build_extension(self, ext: CMakeExtension) -> None:
# Ensure that CMake is present and working
try:
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("Cannot find CMake executable") from None
debug = int(
os.environ.get("DEBUG", 0)
) if self.debug is None else self.debug
cfg = "Debug" if debug else "Release"
parallel_level = os.environ.get("CMAKE_BUILD_PARALLEL_LEVEL", None)
if parallel_level is not None:
self.parallel = int(parallel_level)
else:
self.parallel = os.cpu_count()
for ext in self.extensions:
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)
cmake_args = [
"-DCMAKE_BUILD_TYPE=%s" % cfg,
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(
cfg.upper(), extdir
), "-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{}={}".format(
cfg.upper(), self.build_temp
)
]
# Adding CMake arguments set as environment variable
if "CMAKE_ARGS" in os.environ:
cmake_args += [
item for item in os.environ["CMAKE_ARGS"].split(" ") if item
]
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
build_args = []
build_args += ["--config", cfg]
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
# across all generators.
if (
"CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ and
hasattr(self, "parallel") and self.parallel
):
build_args += [f"-j{self.parallel}"]
build_temp = Path(self.build_temp) / ext.name
if not build_temp.exists():
build_temp.mkdir(parents=True)
# Config
subprocess.check_call(["cmake", ext.cmake_lists_dir] + cmake_args,
cwd=self.build_temp)
# Build
subprocess.check_call(["cmake", "--build", "."] + build_args,
cwd=self.build_temp)
print()
self.copy_extensions_to_source()
class Clean(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import glob
import re
import shutil
with open(".gitignore") 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:
# Don't remove absolute paths from the system
wildcard = wildcard.lstrip("./")
for filename in glob.glob(wildcard):
print(f"cleaning '{filename}'")
try:
os.remove(filename)
except OSError:
shutil.rmtree(filename, ignore_errors=True)
description = (
"VPTQ: Extreme Low-bit Vector Post-Training Quantization "
"for Large Language Models"
)
setup(
name="vptq",
python_requires=">=3.8",
packages=find_packages(exclude=[""]),
install_requires=get_requirements(),
version=get_version(),
description=description,
author="Wang Yang, Wen JiCheng",
ext_modules=[CMakeExtension("vptq")],
cmdclass={
"build_ext": CMakeBuildExt,
"clean": Clean,
},
)