-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
69 lines (57 loc) · 1.95 KB
/
setup.py
File metadata and controls
69 lines (57 loc) · 1.95 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
import os
from setuptools import find_packages, setup
import torch
from torch.utils.cpp_extension import BuildExtension, CUDA_HOME, CUDAExtension, CppExtension
def should_build_with_cuda() -> bool:
if os.environ.get('ATTR_GRAPH_FORCE_CPU', '0') == '1':
return False
if os.environ.get('ATTR_GRAPH_FORCE_CUDA', '0') == '1':
return True
return CUDA_HOME is not None and bool(torch.cuda.is_available())
class OptionalBuildExtension(BuildExtension):
def run(self):
try:
super().run()
except Exception as exc:
self._warn(exc)
def build_extension(self, ext):
try:
super().build_extension(ext)
except Exception as exc:
self._warn(exc)
@staticmethod
def _warn(exc):
banner = '=' * 79
print(banner)
print('WARNING: optional native extension build failed; pure PyTorch fallback remains available.')
print(str(exc))
print(banner)
extra_compile_args = {'cxx': ['-O3']}
sources = ['csrc/compact_topk.cpp']
define_macros = []
extension_cls = CppExtension
if should_build_with_cuda():
extension_cls = CUDAExtension
define_macros.append(('WITH_CUDA', None))
sources.append('csrc/compact_topk_cuda.cu')
extra_compile_args['nvcc'] = ['-O3']
ext_modules = [
extension_cls(
name='attribution_graph_optimization._native',
sources=sources,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
setup(
name='attribution-graph-optimization',
version='0.1.0',
description='Attribution graph generation with an optional custom C++/CUDA extension',
packages=find_packages(exclude=('tests', 'tests.*')),
include_package_data=True,
python_requires='>=3.9,<3.11',
install_requires=['numpy>=1.24,<2.0', 'torch>=1.13,<2.0'],
extras_require={'dev': ['pytest>=8.0']},
ext_modules=ext_modules,
cmdclass={'build_ext': OptionalBuildExtension},
)