-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·239 lines (212 loc) · 8.65 KB
/
setup.py
File metadata and controls
executable file
·239 lines (212 loc) · 8.65 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
import os
import shutil
import sys
from setuptools import find_packages, setup
from setuptools.command.build_ext import build_ext
from torch.utils import cpp_extension
def detect_cuda_arch():
"""Auto-detect GPU compute capability. Returns a semicolon-separated arch list.
Falls back to a safe default when no GPU is available."""
try:
import torch
if torch.cuda.is_available():
archs = set()
for i in range(torch.cuda.device_count()):
major, minor = torch.cuda.get_device_capability(i)
archs.add(f"{major}.{minor}")
if archs:
arch_list = ";".join(sorted(archs))
print(f"Auto-detected GPU architectures: {arch_list}")
return arch_list
except Exception as e:
print(f"GPU architecture auto-detection failed: {e}")
# Fallback: common architectures (Ampere + Hopper)
fallback = "8.0;8.6;9.0"
print(f"No GPU detected, using fallback architectures: {fallback}")
return fallback
def get_version():
with open(os.path.join(os.path.dirname(__file__), "VERSION")) as f:
return f.read().strip()
build_dir = "build"
os.makedirs(build_dir, exist_ok=True)
# Check if we're in debug mode using environment variable
debug = os.environ.get("FLEXKV_DEBUG") == "1"
if debug:
print("Running in debug mode - Cython compilation disabled")
enable_cfs = os.environ.get("FLEXKV_ENABLE_CFS", "0") == "1"
enable_gds = os.environ.get("FLEXKV_ENABLE_GDS", "0") == "1"
enable_p2p = os.environ.get("FLEXKV_ENABLE_P2P", "0") == "1"
enable_cputest = os.environ.get("FLEXKV_ENABLE_CPUTEST", "0") == "1"
# FLEXKV_ENABLE_METRICS=0: build without Prometheus (no prometheus-cpp dependency)
enable_metrics = os.environ.get("FLEXKV_ENABLE_METRICS", "0") == "1"
# Define C++ extensions (base: no dist/Redis)
cpp_sources = [
"csrc/bindings.cpp",
"csrc/transfer.cu", # Skip CUDA file for now
"csrc/hash.cpp",
"csrc/tp_transfer_thread_group.cpp",
"csrc/transfer_ssd.cpp",
"csrc/radix_tree.cpp",
"csrc/eviction_strategy.cpp",
"csrc/monitoring/metrics_manager.cpp", # Monitoring support
]
hpp_sources = [
"csrc/cache_utils.h",
"csrc/tp_transfer_thread_group.h",
"csrc/transfer_ssd.h",
"csrc/radix_tree.h",
"csrc/eviction_strategy.h",
"csrc/monitoring/metrics_manager.h", # Monitoring support
]
# extra_link_args: dist/Redis (libhiredis) only when FLEXKV_ENABLE_P2P=1
extra_link_args = ["-lcuda", "-lxxhash", "-lpthread", "-lrt", "-luring"]
if enable_p2p:
extra_link_args.append("-lhiredis")
if enable_cputest:
extra_link_args.remove("-lcuda")
# Set TORCH_CUDA_ARCH_LIST to avoid IndexError when no GPU is available
os.environ["TORCH_CUDA_ARCH_LIST"] = "7.0;7.5;8.0;8.6;9.0"
# Prometheus libraries only when metrics enabled
if enable_metrics:
extra_link_args.extend(["-lprometheus-cpp-pull", "-lprometheus-cpp-core"])
else:
print("FLEXKV_ENABLE_METRICS=0: building without Prometheus monitoring")
# Auto-detect GPU architecture if TORCH_CUDA_ARCH_LIST is not explicitly set
if not os.environ.get("TORCH_CUDA_ARCH_LIST"):
os.environ["TORCH_CUDA_ARCH_LIST"] = detect_cuda_arch()
print(f"TORCH_CUDA_ARCH_LIST = {os.environ['TORCH_CUDA_ARCH_LIST']}")
extra_compile_args = ["-std=c++17", "-O3"]
if enable_metrics:
extra_compile_args.append("-DFLEXKV_ENABLE_MONITORING")
include_dirs = [os.path.abspath(os.path.join(build_dir, "include"))]
# Add rpath to find libraries at runtime
lib_dir = os.path.join(build_dir, "lib")
if os.path.exists(lib_dir):
extra_link_args.extend([f"-Wl,-rpath,{lib_dir}", "-Wl,-rpath,$ORIGIN"])
# Also add the current package directory to rpath for installed libraries
extra_link_args.append("-Wl,-rpath,$ORIGIN/../lib")
if enable_cfs:
print("ENABLE_CFS = true: compiling and link cfs related content")
cpp_sources.append("csrc/pcfs/pcfs.cpp")
hpp_sources.append("csrc/pcfs/pcfs.h")
extra_link_args.append("-lhifs_client_sdk")
extra_compile_args.append("-DFLEXKV_ENABLE_CFS")
extra_compile_args.append("-DCUDA_AVAILABLE")
nvcc_compile_args = ["-O3"]
if enable_metrics:
nvcc_compile_args.append("-DFLEXKV_ENABLE_MONITORING")
if enable_gds:
print("ENABLE_GDS = true: Compiling and linking GDS content")
cpp_sources.extend([
"csrc/gds/gds_manager.cpp",
"csrc/gds/tp_gds_transfer_thread_group.cpp",
"csrc/gds/layout_transform.cu",
])
hpp_sources.extend([
"csrc/gds/gds_manager.h",
"csrc/gds/tp_gds_transfer_thread_group.h",
"csrc/gds/layout_transform.cuh",
])
extra_link_args.append("-lcufile")
extra_compile_args.append("-DFLEXKV_ENABLE_GDS")
nvcc_compile_args.append("-DFLEXKV_ENABLE_GDS")
if enable_p2p:
print("ENABLE_P2P = true: Compiling and linking distributed (P2P/Redis) content")
cpp_sources.extend([
"csrc/dist/distributed_radix_tree.cpp",
"csrc/dist/local_radix_tree.cpp",
"csrc/dist/redis_meta_channel.cpp",
"csrc/dist/lease_meta_mempool.cpp",
])
extra_compile_args.append("-DFLEXKV_ENABLE_P2P")
if not enable_gds:
print("ENABLE_GDS = false: Skipping GDS code")
if not enable_p2p:
print("ENABLE_P2P = false: Skipping distributed (P2P/Redis) code; no libhiredis or Redis deps required")
cpp_extensions = [
cpp_extension.CUDAExtension(
name="flexkv.c_ext",
sources=cpp_sources,
library_dirs=[os.path.join(build_dir, "lib")],
include_dirs=include_dirs,
depends=hpp_sources,
extra_compile_args={"nvcc": nvcc_compile_args, "cxx": extra_compile_args},
extra_link_args=extra_link_args,
),
]
# Initialize ext_modules with C++ extensions
ext_modules = cpp_extensions
# Only use Cython in release mode
if not debug:
# Compile Python modules with cythonize
# Exclude __init__.py files and test files
python_files = ["flexkv/**/*.py"]
excluded_files = ["flexkv/**/__init__.py",
"flexkv/**/test_*.py",
"flexkv/**/benchmark_*.py",
"flexkv/benchmark/**/*.py",
"flexkv/benchmark/test_kvmanager.py"]
# Import cython when debug is turned off.
from Cython.Build import cythonize
cythonized_modules = cythonize(
python_files,
exclude=excluded_files,
compiler_directives={
"language_level": 3,
"boundscheck": False,
"wraparound": False,
"initializedcheck": False,
"profile": True,
},
build_dir=build_dir, # Direct Cython to use the build directory
)
# Add Cython modules to ext_modules
ext_modules.extend(cythonized_modules)
print("Release mode: Including Cython compilation")
else:
print("Debug mode: Skipping Cython compilation")
class CustomBuildExt(cpp_extension.BuildExtension):
def run(self):
super().run()
# Copy required shared libraries to the package directory after building
self.copy_shared_libraries()
def copy_shared_libraries(self):
"""Copy shared libraries to the package lib directory"""
source_lib_dir = os.path.join(build_dir, "lib")
if not os.path.exists(source_lib_dir):
print(f"Warning: Source library directory {source_lib_dir} does not exist")
return
# Create lib directory in the package
package_lib_dir = os.path.join("flexkv", "lib")
os.makedirs(package_lib_dir, exist_ok=True)
# Copy all .so files
for file in os.listdir(source_lib_dir):
if file.endswith(".so") or file.endswith(".so.*"):
source_file = os.path.join(source_lib_dir, file)
dest_file = os.path.join(package_lib_dir, file)
if os.path.isfile(source_file):
shutil.copy2(source_file, dest_file)
print(f"Copied {source_file} to {dest_file}")
with open("requirements.txt") as f:
install_requires = f.read().splitlines()
setup(
name="flexkv",
description="A global KV-Cache manager for LLM inference",
version=get_version(),
packages=find_packages(exclude=("benchmarks", "csrc", "examples", "tests")),
package_data={
"flexkv": ["*.so", "lib/*.so", "lib/*.so.*"],
},
include_package_data=True,
install_requires=install_requires,
ext_modules=ext_modules, # Now contains both C++ and Cython modules as needed
cmdclass={
"build_ext": CustomBuildExt.with_options(
include_dirs=os.path.join(build_dir, "include"), # Include directory for xxhash
no_python_abi_suffix=True,
build_temp=os.path.join(build_dir, "temp"), # Temporary build files
)
},
#python_requires=">=3.8",
python_requires=">=3.6",
)