|
25 | 25 | import os |
26 | 26 | import subprocess |
27 | 27 | import sys |
28 | | -import sysconfig |
29 | | -from glob import glob |
30 | 28 |
|
31 | | -import pybind11 |
32 | | -import torch |
33 | | -import torch.utils.cpp_extension |
34 | 29 | from setuptools import Extension, find_packages, setup |
35 | 30 | from setuptools.command.build_ext import build_ext |
36 | 31 |
|
37 | 32 | ROOT_DIR = os.path.abspath(os.path.dirname(__file__)) |
38 | 33 | PLATFORM = os.getenv("PLATFORM") |
39 | | - |
40 | 34 | ENABLE_SPARSE = os.getenv("ENABLE_SPARSE") |
41 | 35 |
|
42 | 36 |
|
43 | 37 | def _enable_sparse() -> bool: |
44 | 38 | return ENABLE_SPARSE is not None and ENABLE_SPARSE.lower() == "true" |
45 | 39 |
|
46 | 40 |
|
47 | | -def _is_cuda() -> bool: |
48 | | - return PLATFORM == "cuda" |
49 | | - |
50 | | - |
51 | | -def _is_npu() -> bool: |
52 | | - return PLATFORM == "ascend" |
53 | | - |
54 | | - |
55 | | -def _is_musa() -> bool: |
56 | | - return PLATFORM == "musa" |
57 | | - |
58 | | - |
59 | | -def _is_maca() -> bool: |
60 | | - return PLATFORM == "maca" |
61 | | - |
62 | | - |
63 | 41 | class CMakeExtension(Extension): |
64 | | - def __init__(self, name: str, sourcedir: str = ""): |
| 42 | + def __init__(self, name: str, source_dir: str = ""): |
65 | 43 | super().__init__(name, sources=[]) |
66 | | - self.sourcedir = os.path.abspath(sourcedir) |
| 44 | + self.cmake_file_path = os.path.abspath(source_dir) |
67 | 45 |
|
68 | 46 |
|
69 | 47 | class CMakeBuild(build_ext): |
70 | 48 | def run(self): |
| 49 | + build_dir = os.path.abspath(self.build_temp) |
| 50 | + os.makedirs(build_dir, exist_ok=True) |
| 51 | + |
71 | 52 | for ext in self.extensions: |
72 | 53 | self.build_cmake(ext) |
73 | 54 |
|
74 | 55 | def build_cmake(self, ext: CMakeExtension): |
75 | | - build_dir = self.build_temp |
76 | | - os.makedirs(build_dir, exist_ok=True) |
| 56 | + build_dir = os.path.abspath(self.build_temp) |
| 57 | + install_dir = os.path.abspath(self.build_lib) |
77 | 58 |
|
78 | 59 | cmake_args = [ |
79 | | - "cmake", |
80 | 60 | "-DCMAKE_BUILD_TYPE=Release", |
81 | 61 | f"-DPYTHON_EXECUTABLE={sys.executable}", |
| 62 | + f"-DCMAKE_INSTALL_PREFIX={install_dir}", |
82 | 63 | ] |
83 | 64 |
|
84 | | - torch_cmake_prefix = torch.utils.cmake_prefix_path |
85 | | - pybind11_cmake_dir = pybind11.get_cmake_dir() |
86 | | - |
87 | | - cmake_prefix_paths = [torch_cmake_prefix, pybind11_cmake_dir] |
88 | | - cmake_args.append(f"-DCMAKE_PREFIX_PATH={';'.join(cmake_prefix_paths)}") |
89 | | - |
90 | | - torch_includes = torch.utils.cpp_extension.include_paths() |
91 | | - python_include = sysconfig.get_path("include") |
92 | | - pybind11_include = pybind11.get_include() |
93 | | - |
94 | | - all_includes = torch_includes + [python_include, pybind11_include] |
95 | | - cmake_include_string = ";".join(all_includes) |
96 | | - cmake_args.append(f"-DEXTERNAL_INCLUDE_DIRS={cmake_include_string}") |
97 | | - |
98 | | - if _is_cuda(): |
99 | | - cmake_args.append("-DRUNTIME_ENVIRONMENT=cuda") |
100 | | - elif _is_npu(): |
101 | | - cmake_args.append("-DRUNTIME_ENVIRONMENT=ascend") |
102 | | - elif _is_musa(): |
103 | | - cmake_args.append("-DRUNTIME_ENVIRONMENT=musa") |
104 | | - elif _is_maca(): |
105 | | - cmake_args.append("-DRUNTIME_ENVIRONMENT=maca") |
106 | | - cmake_args.append("-DBUILD_UCM_SPARSE=OFF") |
107 | | - else: |
108 | | - raise RuntimeError( |
109 | | - "No supported accelerator found. " |
110 | | - "Please ensure either CUDA/MUSA or NPU is available." |
111 | | - ) |
112 | | - |
113 | 65 | if _enable_sparse(): |
114 | | - cmake_args.append("-DBUILD_UCM_SPARSE=ON") |
115 | | - |
116 | | - cmake_args.append(ext.sourcedir) |
| 66 | + cmake_args += ["-DBUILD_UCM_SPARSE=ON"] |
| 67 | + |
| 68 | + match PLATFORM: |
| 69 | + case "cuda": |
| 70 | + cmake_args += ["-DRUNTIME_ENVIRONMENT=cuda"] |
| 71 | + case "ascend": |
| 72 | + cmake_args += ["-DRUNTIME_ENVIRONMENT=ascend"] |
| 73 | + case "musa": |
| 74 | + cmake_args += ["-DRUNTIME_ENVIRONMENT=musa"] |
| 75 | + case "maca": |
| 76 | + cmake_args += ["-DRUNTIME_ENVIRONMENT=maca"] |
| 77 | + cmake_args += ["-DBUILD_UCM_SPARSE=OFF"] |
| 78 | + case _: |
| 79 | + cmake_args += ["-DRUNTIME_ENVIRONMENT=simu"] |
| 80 | + cmake_args += ["-DBUILD_UCM_SPARSE=OFF"] |
117 | 81 |
|
118 | | - print(f"[INFO] Building {ext.name} module with CMake") |
119 | | - print(f"[INFO] Source directory: {ext.sourcedir}") |
120 | | - print(f"[INFO] Build directory: {build_dir}") |
121 | | - print(f"[INFO] CMake command: {' '.join(cmake_args)}") |
122 | | - |
123 | | - subprocess.check_call(cmake_args, cwd=build_dir) |
| 82 | + subprocess.check_call( |
| 83 | + ["cmake", *cmake_args, ext.cmake_file_path], cwd=build_dir |
| 84 | + ) |
124 | 85 | subprocess.check_call( |
125 | 86 | ["cmake", "--build", ".", "--config", "Release", "--", "-j8"], |
126 | 87 | cwd=build_dir, |
127 | 88 | ) |
128 | 89 |
|
| 90 | + subprocess.check_call( |
| 91 | + ["cmake", "--install", ".", "--config", "Release", "--component", "ucm"], |
| 92 | + cwd=build_dir, |
| 93 | + ) |
129 | 94 |
|
130 | | -def _get_packages(): |
131 | | - """Discover Python packages, optionally filtering out sparse-related ones.""" |
132 | | - packages = find_packages() |
133 | | - if not _enable_sparse(): |
134 | | - packages = [pkg for pkg in packages if not pkg.startswith("ucm.sparse")] |
135 | | - return packages |
136 | | - |
137 | | - |
138 | | -def _get_package_data_with_so(packages=None): |
139 | | - """Automatically discover all packages and include .so files.""" |
140 | | - if packages is None: |
141 | | - packages = _get_packages() |
142 | | - package_data = {} |
143 | | - |
144 | | - for package in packages: |
145 | | - # Convert package name to directory path |
146 | | - package_dir = os.path.join(ROOT_DIR, package.replace(".", os.sep)) |
147 | | - |
148 | | - # Check if this package directory contains .so files |
149 | | - so_files = glob(os.path.join(package_dir, "*.so")) |
150 | | - if so_files: |
151 | | - package_data[package] = ["*.so"] |
152 | | - print(f"[INFO] Including .so files for package: {package}") |
153 | | - |
154 | | - print(f"[INFO] Package data: {package_data}") |
155 | | - return package_data |
156 | | - |
157 | | - |
158 | | -ext_modules = [] |
159 | | -ext_modules.append(CMakeExtension(name="ucm", sourcedir=ROOT_DIR)) |
160 | | - |
161 | | -packages = _get_packages() |
162 | 95 |
|
163 | 96 | setup( |
164 | 97 | name="uc-manager", |
165 | | - version="0.1.1", |
| 98 | + version="0.1.2", |
166 | 99 | description="Unified Cache Management", |
167 | 100 | author="Unified Cache Team", |
168 | | - packages=packages, |
| 101 | + packages=find_packages(), |
169 | 102 | python_requires=">=3.10", |
170 | | - ext_modules=ext_modules, |
| 103 | + ext_modules=[CMakeExtension(name="ucm", source_dir=ROOT_DIR)], |
171 | 104 | cmdclass={"build_ext": CMakeBuild}, |
172 | | - package_data=_get_package_data_with_so(packages), |
173 | 105 | zip_safe=False, |
| 106 | + include_package_data=False, |
174 | 107 | ) |
0 commit comments