-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
265 lines (206 loc) · 7.54 KB
/
setup.py
File metadata and controls
265 lines (206 loc) · 7.54 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
#!/usr/bin/env python3
"""
Setup script for building MLKV Plus PyTorch Extension
"""
import os
import sys
import subprocess
from pathlib import Path
from setuptools import setup
import torch
from torch.utils.cpp_extension import (
CppExtension,
CUDAExtension,
BuildExtension,
CUDA_HOME,
)
if torch.__version__ >= "2.6.0":
py_limited_api = True
else:
py_limited_api = False
# Get the directory containing this script
PROJECT_ROOT_DIR = Path(__file__).parent.absolute()
BUILD_DIR = PROJECT_ROOT_DIR / "build"
CONDA_PREFIX = Path(os.getenv("CONDA_PREFIX"))
PROJECT_NAME = "mlkv_plus"
CPP_STANDARD = "c++20"
def rel(p: Path) -> str:
return os.path.relpath(str(p), start=str(PROJECT_ROOT_DIR)).replace(os.sep, "/")
def get_cuda_architectures():
"""Get CUDA architectures from environment variable or default to 86"""
sm_env = os.environ.get("CUDA_SM", "86")
return [arch.strip() for arch in sm_env.split(",") if arch.strip()]
def run_cmake_build():
"""Run CMake configure and build before Python extension build"""
print("Running CMake build...")
# Get CUDA architectures
cuda_archs = get_cuda_architectures()
print(f"Using CUDA architectures: {cuda_archs}")
# Ensure build directory exists
BUILD_DIR.mkdir(exist_ok=True)
# Configure CMake with CUDA architectures
cmake_configure_cmd = [
"cmake",
"-S", str(PROJECT_ROOT_DIR),
"-B", str(BUILD_DIR),
f"-Dsm={';'.join(cuda_archs)}",
"-DCMAKE_BUILD_TYPE=Debug"
]
print(f"CMake configure command: {' '.join(cmake_configure_cmd)}")
try:
subprocess.run(cmake_configure_cmd, check=True, cwd=str(PROJECT_ROOT_DIR))
print("CMake configuration completed successfully")
except subprocess.CalledProcessError as e:
print(f"CMake configuration failed: {e}")
sys.exit(1)
# Build with CMake
cmake_build_cmd = [
"cmake",
"--build", str(BUILD_DIR),
"--parallel", os.environ.get("MAX_JOBS", 8)
]
print(f"CMake build command: {' '.join(cmake_build_cmd)}")
try:
subprocess.run(cmake_build_cmd, check=True, cwd=str(PROJECT_ROOT_DIR))
print("CMake build completed successfully")
except subprocess.CalledProcessError as e:
print(f"CMake build failed: {e}")
sys.exit(1)
# Install CMake targets (especially python_binding component)
cmake_install_cmd = [
"cmake",
"--install", str(BUILD_DIR),
"--component", "gycsb_python_binding"
]
print(f"CMake install command: {' '.join(cmake_install_cmd)}")
try:
subprocess.run(cmake_install_cmd, check=True, cwd=str(PROJECT_ROOT_DIR))
print("CMake install completed successfully")
except subprocess.CalledProcessError as e:
print(f"CMake install failed: {e}")
# Note: Install failure is not always critical, continue with Python extension build
print("Warning: CMake install failed, but continuing with Python extension build")
def mlkv_plus_torch_binding():
cxx_args = [
"-std=c++20",
"-g",
"-fdiagnostics-color=always",
"-DPy_LIMITED_API=0x03120000", # min CPython version 3.12
]
# Get CUDA architectures from environment
arch = get_cuda_architectures()
nvcc_args = [
f"-std={CPP_STANDARD}",
"-g",
]
for a in arch:
nvcc_args.append(f"-gencode=arch=compute_{a},code=sm_{a}")
source_files = [
rel(PROJECT_ROOT_DIR / "libmlkvplus" / "torch_binding" / "dummy_var_handle.cu"),
rel(PROJECT_ROOT_DIR / "libmlkvplus" / "torch_binding" / "dummy_var_ops.cu"),
rel(PROJECT_ROOT_DIR / "libmlkvplus" / "torch_binding" / "dummy_var.cu"),
rel(PROJECT_ROOT_DIR / "libmlkvplus" / "torch_binding" / "register.cc"),
]
include_dirs = [
str(PROJECT_ROOT_DIR / "libmlkvplus"),
str(PROJECT_ROOT_DIR / "libmlkvplus" / "include"),
str(PROJECT_ROOT_DIR / "libmlkvplus" / "torch_binding"),
str(PROJECT_ROOT_DIR / "third_party" / "HierarchicalKV" / "include"),
str(PROJECT_ROOT_DIR / "libmlkvplus" / "rocksdb" / "include"), # match priority with original rocksdb
str(PROJECT_ROOT_DIR / "third_party" / "rocksdb" / "include"),
]
library_dirs = [
str(BUILD_DIR),
]
print(f"Building MLKV Plus PyTorch Extension...")
print(f"Include dirs: {include_dirs}")
print(f"Source files: {source_files}")
rpaths = []
rpaths.append(str(BUILD_DIR))
extra_link_args = []
if rpaths:
extra_link_args.append(f"-Wl,-rpath,{','.join(rpaths)}")
# Create the extension
ext_module = CUDAExtension(
name=f"{PROJECT_NAME}.libmlkvplus_torch",
sources=source_files,
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=["mlkv_plus"],
extra_compile_args={
"cxx": cxx_args,
"nvcc": nvcc_args,
},
extra_link_args=extra_link_args,
py_limited_api=py_limited_api,
)
return ext_module
def sok():
base_dir = PROJECT_ROOT_DIR / "third_party" / "HugeCTR" / "sparse_operation_kit"
source_files = [
rel(base_dir / "kit_src" / "py_init.cc"),
rel(base_dir / "kit_src" / "lookup" / "binding" / "select.cu"),
rel(base_dir / "kit_src" / "lookup" / "binding" / "reorder.cu"),
rel(base_dir / "kit_src" / "lookup" / "impl" / "reorder_kernel.cu"),
rel(base_dir / "kit_src" / "lookup" / "impl" / "select_kernel.cu"),
]
include_dirs = [
str(base_dir / "kit_src"),
]
cxx_args = [
f"-std={CPP_STANDARD}",
"-g",
"-fdiagnostics-color=always",
"-DPy_LIMITED_API=0x03120000", # min CPython version 3.12
]
# Get CUDA architectures from environment
arch = get_cuda_architectures()
nvcc_args = [
"-std=c++20",
"-g",
]
for a in arch:
nvcc_args.append(f"-gencode=arch=compute_{a},code=sm_{a}")
# Create the extension
ext_module = CUDAExtension(
name=f"{PROJECT_NAME}.sok",
sources=source_files,
include_dirs=include_dirs,
extra_compile_args={
"cxx": cxx_args,
"nvcc": nvcc_args,
},
py_limited_api=py_limited_api,
)
return ext_module
def main():
import os
# Check if we're installing benchmark-only mode
benchmark_only = os.environ.get("gYCSB_ONLY", "false").lower() == "true"
if benchmark_only:
print("Installing gYCSB only...")
# Still need to call setup() for metadata generation, but without extensions
setup(
ext_modules=[],
cmdclass={}
)
return
else:
print("Installing MLKV+ (PyTorch + libmlkvplus) and gYCSB...")
# Check if CUDA is available
if not torch.cuda.is_available():
print("Warning: CUDA not available, but SOK requires CUDA")
print("Please install CUDA and PyTorch with CUDA support")
sys.exit(1)
# Run CMake build first
print("Step 1: Running CMake build...")
run_cmake_build()
# Then build Python extensions
print("Step 2: Building Python extensions...")
ext_modules = [sok(), mlkv_plus_torch_binding()]
setup(
ext_modules=ext_modules,
cmdclass={"build_ext": BuildExtension}
)
if __name__ == "__main__":
main()