forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
338 lines (287 loc) · 11.8 KB
/
setup.py
File metadata and controls
338 lines (287 loc) · 11.8 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
"""
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# 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 glob
import os
import re
import subprocess
import sys
from functools import lru_cache
from pathlib import Path
import paddle
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
from wheel.bdist_wheel import bdist_wheel
long_description = "FastDeploy: Large Language Model Serving.\n\n"
long_description += "GitHub: https://github.com/PaddlePaddle/FastDeploy\n"
long_description += "Email: dltp@baidu.com"
# Platform to CMake mapping
PLAT_TO_CMAKE = {
"win32": "Win32",
"win-amd64": "x64",
"win-arm32": "ARM",
"win-arm64": "ARM64",
}
class CustomBdistWheel(bdist_wheel):
"""Custom wheel builder for pure Python packages."""
def finalize_options(self):
"""Configure wheel as pure Python and platform-independent."""
super().finalize_options()
self.root_is_pure = True
self.python_tag = "py3"
self.abi_tag = "none"
self.plat_name_supplied = True
self.plat_name = "any"
class CMakeExtension(Extension):
"""A setuptools Extension for CMake-based builds."""
def __init__(self, name: str, sourcedir: str = "", version: str = None) -> None:
"""
Initialize CMake extension.
Args:
name (str): Name of the extension.
sourcedir (str): Source directory path.
version (str): Optional version string (set to None to disable version info)
"""
super().__init__(name, sources=[])
self.sourcedir = os.fspath(Path(sourcedir).resolve())
self.version = version
class CMakeBuild(build_ext):
"""Custom build_ext command using CMake."""
def get_ext_filename(self, ext_name):
"""Remove Python version tag from extension filename"""
return ext_name.split(".")[0] + ".so"
def build_extension(self, ext: CMakeExtension) -> None:
"""
Build the CMake extension.
Args:
ext (CMakeExtension): The extension to build.
"""
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve()
cfg = "Debug" if int(os.environ.get("DEBUG", 0)) else "Release"
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_BUILD_TYPE={cfg}",
"-DVERSION_INFO=",
"-DPYBIND11_PYTHON_VERSION=",
"-DPYTHON_VERSION=",
f"-DPYTHON_INCLUDE_DIR={sys.prefix}/include/python{sys.version_info.major}.{sys.version_info.minor}",
f"-DPYTHON_LIBRARY={sys.prefix}/lib/libpython{sys.version_info.major}.{sys.version_info.minor}.so",
]
build_args = []
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
if self.compiler.compiler_type != "msvc":
if not cmake_generator or cmake_generator == "Ninja":
try:
import ninja
ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
cmake_args += [
"-GNinja",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
]
except ImportError:
pass
else:
if "NMake" not in cmake_generator and "Ninja" not in cmake_generator:
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
if "NMake" not in cmake_generator and "Ninja" not in cmake_generator:
cmake_args += [f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"]
build_args += ["--config", cfg]
if sys.platform.startswith("darwin"):
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
if archs:
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
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
build_temp.mkdir(parents=True, exist_ok=True)
subprocess.run(["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True)
subprocess.run(["cmake", "--build", ".", *build_args], cwd=build_temp, check=True)
class PostInstallCommand(install):
"""在标准安装完成后执行自定义命令"""
def run(self):
# 先执行标准安装步骤
install.run(self)
# 执行自定义命令
subprocess.check_call(["opentelemetry-bootstrap", "-a", "install"])
def load_requirements():
"""Load dependencies from requirements.txt"""
requirements_file_name = "requirements.txt"
if paddle.is_compiled_with_custom_device("iluvatar_gpu"):
requirements_file_name = "requirements_iluvatar.txt"
elif paddle.is_compiled_with_rocm():
requirements_file_name = "requirements_dcu.txt"
elif paddle.device.is_compiled_with_custom_device("metax_gpu"):
requirements_file_name = "requirements_metaxgpu.txt"
requirements_path = os.path.join(os.path.dirname(__file__), requirements_file_name)
with open(requirements_path, "r") as f:
return [line.strip() for line in f if line.strip() and not line.startswith("#")]
def get_device_type():
"""Get the device type (rocm/gpu/xpu/npu/cpu/metax-gpu) that paddle is compiled with."""
if paddle.is_compiled_with_rocm():
return "rocm"
elif paddle.is_compiled_with_cuda():
return "gpu"
elif paddle.is_compiled_with_xpu():
return "xpu"
elif paddle.is_compiled_with_custom_device("npu"):
return "npu"
elif paddle.is_compiled_with_custom_device("iluvatar_gpu"):
return "iluvatar-gpu"
elif paddle.is_compiled_with_custom_device("gcu"):
return "gcu"
elif paddle.device.is_compiled_with_custom_device("metax_gpu"):
return "metax-gpu"
elif paddle.is_compiled_with_custom_device("intel_hpu"):
return "intel-hpu"
else:
return "cpu"
def check_header(header_path):
return os.path.exists(header_path)
def check_library(lib_name):
# search /usr/lib /usr/lib64 /lib /lib64 .etc
paths = [
"/usr/lib",
"/usr/lib32",
"/usr/lib64",
"/usr/lib/x86_64-linux-gnu",
"/lib",
"/lib32",
"/lib64",
"/usr/local/lib",
"/usr/local/lib64",
]
for p in paths:
if glob.glob(os.path.join(p, lib_name)):
return True
return False
def check_rdma_packages():
results = {}
# libibverbs-dev
results["libibverbs header"] = check_header("/usr/include/infiniband/verbs.h")
results["libibverbs library"] = check_library("libibverbs.so*") or check_library("libibverbs.so")
# librdmacm-dev
results["librdmacm header"] = check_header("/usr/include/rdma/rdma_cma.h")
results["librdmacm library"] = check_library("librdmacm.so*") or check_library("librdmacm.so")
print("===== RDMA Library Check Results =====")
for k, v in results.items():
status = "FOUND" if v else "NOT FOUND"
print(f"{k:25}: {status}")
print("\n== Summary ==")
if all(results.values()):
print("All required RDMA libraries are installed.")
return True
else:
print("Some RDMA libraries are missing. Suggested commands:")
print("\nUbuntu/Debian:")
print(" sudo apt-get install -y libibverbs-dev librdmacm-dev")
print("\nCentOS/RHEL:")
print(" sudo yum install -y libibverbs-devel librdmacm-devel")
return False
@lru_cache(maxsize=1)
def rdma_comm_supported():
supported = (
get_device_type() in ["gpu", "xpu"]
and check_rdma_packages()
and os.getenv("FD_ENABLE_RDMA_COMPILE", "1") == "1"
)
return supported
def get_name():
"""get package name"""
return "fastdeploy-" + get_device_type()
cmdclass_dict = {"bdist_wheel": CustomBdistWheel}
cmdclass_dict["build_ext"] = CMakeBuild
FASTDEPLOY_VERSION = os.environ.get("FASTDEPLOY_VERSION", "2.5.0-dev")
cmdclass_dict["build_optl"] = PostInstallCommand
def write_version_to_file():
current_dir = os.path.dirname(os.path.abspath(__file__))
version_file_path = os.path.join(current_dir, "fastdeploy/version.txt")
with open(version_file_path, "a") as f:
f.write(f"fastdeploy version: {FASTDEPLOY_VERSION}\n")
write_version_to_file()
setup(
name=get_name(),
version=FASTDEPLOY_VERSION,
author="PaddlePaddle",
author_email="dltp@baidu.com",
description="FastDeploy: Large Language Model Serving.",
long_description=long_description,
long_description_content_type="text/plain",
url="https://github.com/PaddlePaddle/FastDeploy",
packages=find_packages(),
package_dir={"fastdeploy": "fastdeploy/"},
# For deprecated method (egg-based installation), `.so` files are placed in the `model_executor/ops/XXX` directory.
# For modern method (PEP 517/518-based installation), `.so` files are placed in the `model_executor/ops/XXX/fastdeploy_ops` directory.
# Therefore, the `fastdeploy_ops` directory should be included for modern Python packaging.
package_data={
"fastdeploy": [
"model_executor/ops/gpu/*",
"model_executor/ops/gpu/fastdeploy_ops/*",
"model_executor/ops/gpu/deep_gemm/include/**/*",
"model_executor/ops/gpu/fastdeploy_ops_*/*",
"model_executor/ops/gpu/fastdeploy_ops_*/fastdeploy_ops/*",
"model_executor/ops/gpu/fastdeploy_ops_*/deep_gemm/include/*/*",
"model_executor/ops/cpu/*",
"model_executor/ops/cpu/fastdeploy_cpu_ops/*",
"model_executor/ops/xpu/*",
"model_executor/ops/xpu/fastdeploy_ops/*",
"model_executor/ops/xpu/libs/*",
"model_executor/ops/xpu/fastdeploy_ops/libs/*",
"model_executor/ops/npu/*",
"model_executor/ops/npu/fastdeploy_ops/*",
"model_executor/ops/base/*",
"model_executor/ops/base/fastdeploy_ops/*",
"model_executor/ops/iluvatar/*",
"model_executor/ops/iluvatar/fastdeploy_ops/*",
"model_executor/models/*",
"model_executor/layers/*",
"input/ernie4_5_vl_processor/utils/*",
"model_executor/ops/gcu/*",
"model_executor/ops/gcu/fastdeploy_ops/*",
"cache_manager/transfer_factory/get_rdma_nics.sh",
"version.txt",
]
},
install_requires=load_requirements(),
ext_modules=(
[
CMakeExtension(
"rdma_comm",
sourcedir="fastdeploy/cache_manager/transfer_factory/kvcache_transfer",
version=None,
)
]
if rdma_comm_supported()
else []
),
cmdclass=cmdclass_dict if rdma_comm_supported() else {},
zip_safe=False,
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
license="Apache 2.0",
python_requires=">=3.7",
extras_require={
"test": ["pytest>=6.0"],
"eval": ["lm-eval==0.4.9.1"],
},
entry_points={
"console_scripts": ["fastdeploy=fastdeploy.entrypoints.cli.main:main"],
},
)