-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
201 lines (180 loc) · 7.99 KB
/
Copy pathsetup.py
File metadata and controls
201 lines (180 loc) · 7.99 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
#!/usr/bin/env python3
"""
setup.py for RFX-Fuse - Breiman and Cutler's Unified ML & Similarity Engine
Original Random Forest algorithm by Leo Breiman and Adele Cutler
C++ implementation, GPU acceleration, and Python bindings by Chris Kuchar
"""
import os
import sys
import subprocess
from pathlib import Path
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the extension")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = [
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}',
f'-DPYTHON_EXECUTABLE={sys.executable}',
f'-DPython3_EXECUTABLE={sys.executable}',
f'-DPython_EXECUTABLE={sys.executable}',
'-DRFX_PORTABLE=ON',
]
# Environment-driven build flags (for wheel builds)
# Always pass both flags explicitly to avoid CMake cache stale values
cpu_only = os.environ.get('RFX_CPU_ONLY', '0') == '1'
cuda_static = os.environ.get('RFX_CUDA_STATIC', '0') == '1'
cmake_args.append(f'-DRFX_CPU_ONLY={"ON" if cpu_only else "OFF"}')
cmake_args.append(f'-DRFX_CUDA_STATIC={"ON" if cuda_static else "OFF"}')
if not cpu_only:
# CUDA 12.4+ supported architectures:
# 75 (Turing), 80 (Ampere), 86 (Ampere), 89 (Ada), 90 (Hopper)
cuda_archs = os.environ.get('CMAKE_CUDA_ARCHITECTURES', '75;80;86;89;90')
cmake_args.append(f'-DCMAKE_CUDA_ARCHITECTURES={cuda_archs}')
# Add pybind11 path if available
try:
import pybind11
pybind11_path = pybind11.get_cmake_dir()
cmake_args.append(f'-DCMAKE_PREFIX_PATH={pybind11_path}')
print(f"Using pybind11 from: {pybind11_path}")
except ImportError:
print("Warning: pybind11 not found, CMake will try to find it")
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
cmake_args += [f'-DCMAKE_BUILD_TYPE={cfg}']
# On Windows with Visual Studio generators, CMake appends Release/ or
# Debug/ to output directories. Set per-config variables to prevent this.
# .pyd files are DLLs, so RUNTIME_OUTPUT_DIRECTORY is needed on Windows.
if sys.platform == 'win32':
cmake_args += [
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}',
f'-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}',
f'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}',
]
import multiprocessing
num_cores = multiprocessing.cpu_count()
if sys.platform == 'win32':
# Find an installed Visual Studio version via vswhere
vswhere = r"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
vs_found = False
if os.path.isfile(vswhere):
try:
vs_version = subprocess.check_output(
[vswhere, '-latest', '-property', 'catalog_productLineVersion'],
text=True
).strip()
vs_generators = {'2022': 'Visual Studio 17 2022', '2019': 'Visual Studio 16 2019'}
if vs_version in vs_generators:
cmake_args += ['-G', vs_generators[vs_version], '-A', 'x64']
vs_found = True
except Exception:
pass
if not vs_found:
cmake_args += ['-G', 'Ninja']
build_args += ['--', f'/m:{num_cores}']
else:
build_args += ['--', f'-j{num_cores}']
env = os.environ.copy()
env['CXXFLAGS'] = f'{env.get("CXXFLAGS", "")} -DVERSION_INFO=\\"{self.distribution.get_version()}\\"'
# Ensure CUDA bin is on PATH (nvcc may not be on default PATH)
if not cpu_only:
cuda_paths = ['/usr/local/cuda/bin', '/usr/local/cuda-12/bin']
for cp in cuda_paths:
if os.path.isdir(cp) and cp not in env.get('PATH', ''):
env['PATH'] = cp + os.pathsep + env.get('PATH', '')
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# Always do a fresh configure to prevent stale CMake cache
# (e.g., switching between CPU-only and GPU builds)
cmake_cache = os.path.join(self.build_temp, 'CMakeCache.txt')
if os.path.exists(cmake_cache):
os.remove(cmake_cache)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
# Determine package variant from environment
is_cpu_only = os.environ.get('RFX_CPU_ONLY', '0') == '1'
pkg_name = 'rfx-fuse-cpu' if is_cpu_only else 'rfx-fuse'
pkg_description = (
"RFX-Fuse: Breiman and Cutler's Unified ML Engine (CPU-only)" if is_cpu_only
else "RFX-Fuse: Breiman and Cutler's Unified ML Engine with GPU Acceleration"
)
# Read README for long description
readme_file = 'README_PYPI_CPU.md' if is_cpu_only else 'README_PYPI.md'
long_description = ''
if os.path.exists(readme_file):
with open(readme_file, encoding='utf-8') as f:
long_description = f.read()
elif os.path.exists('README.md'):
with open('README.md', encoding='utf-8') as f:
long_description = f.read()
setup(
name=pkg_name,
version='1.1.4',
author='Chris Kuchar',
author_email='chrisjkuchar@gmail.com',
description=pkg_description,
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/chriskuchar/RFX-Fuse',
project_urls={
'Bug Reports': 'https://github.com/chriskuchar/RFX-Fuse/issues',
'Source': 'https://github.com/chriskuchar/RFX-Fuse',
'Documentation': 'https://github.com/chriskuchar/RFX-Fuse/blob/main/docs/API.md',
},
ext_modules=[CMakeExtension('RFXFuse', sourcedir='.')],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
python_requires='>=3.9',
setup_requires=[
'pybind11>=2.6.0',
],
install_requires=[
'numpy>=1.19.0',
],
extras_require={
'dev': [
'pytest>=6.0.0',
'pytest-cov>=2.10.0',
],
},
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Science/Research',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: POSIX :: Linux',
'Operating System :: Microsoft :: Windows',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Programming Language :: C++',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Scientific/Engineering :: Information Analysis',
'Topic :: Software Development :: Libraries :: Python Modules',
],
keywords='random forest, machine learning, gpu, cuda, classification, visualization, proximity',
packages=find_packages(where='python', include=['*']),
py_modules=['rfx_impute', 'rfx_fuse_impute', 'rfviz', 'categorical_helper', 'rfx_metrics'],
package_dir={'': 'python'},
include_package_data=True,
package_data={
'': [
'examples/**/*.py',
'examples/**/*.md',
],
},
)