setup.py 4.82 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import glob
rusty1s's avatar
rusty1s committed
2
import os
rusty1s's avatar
rusty1s committed
3
import os.path as osp
Amit Aflalo's avatar
Amit Aflalo committed
4
import platform
rusty1s's avatar
rusty1s committed
5
6
import sys
from itertools import product
rusty1s's avatar
rusty1s committed
7

rusty1s's avatar
rusty1s committed
8
import torch
rusty1s's avatar
rusty1s committed
9
from setuptools import find_packages, setup
rusty1s's avatar
rusty1s committed
10
from torch.__config__ import parallel_info
rusty1s's avatar
rusty1s committed
11
12
13
from torch.utils.cpp_extension import (CUDA_HOME, BuildExtension, CppExtension,
                                       CUDAExtension)

Matthias Fey's avatar
Matthias Fey committed
14
__version__ = '0.7.0'
rusty1s's avatar
rusty1s committed
15
URL = 'https://github.com/rusty1s/pytorch_sparse'
rusty1s's avatar
rusty1s committed
16

rusty1s's avatar
rusty1s committed
17
WITH_CUDA = torch.cuda.is_available() and CUDA_HOME is not None
rusty1s's avatar
rusty1s committed
18
suffices = ['cpu', 'cuda'] if WITH_CUDA else ['cpu']
rusty1s's avatar
rusty1s committed
19
if os.getenv('FORCE_CUDA', '0') == '1':
rusty1s's avatar
rusty1s committed
20
21
22
23
24
    suffices = ['cuda', 'cpu']
if os.getenv('FORCE_ONLY_CUDA', '0') == '1':
    suffices = ['cuda']
if os.getenv('FORCE_ONLY_CPU', '0') == '1':
    suffices = ['cpu']
rusty1s's avatar
rusty1s committed
25
26
27

BUILD_DOCS = os.getenv('BUILD_DOCS', '0') == '1'

rusty1s's avatar
rusty1s committed
28
29
WITH_METIS = True if os.getenv('WITH_METIS', '0') == '1' else False
WITH_MTMETIS = True if os.getenv('WITH_MTMETIS', '0') == '1' else False
rusty1s's avatar
rusty1s committed
30

31
32
WITH_SYMBOLS = True if os.getenv('WITH_SYMBOLS', '0') == '1' else False

rusty1s's avatar
rusty1s committed
33
34

def get_extensions():
rusty1s's avatar
rusty1s committed
35
    extensions = []
rusty1s's avatar
rusty1s committed
36

rusty1s's avatar
rusty1s committed
37
    extensions_dir = osp.join('csrc')
rusty1s's avatar
rusty1s committed
38
39
    main_files = glob.glob(osp.join(extensions_dir, '*.cpp'))

rusty1s's avatar
rusty1s committed
40
    for main, suffix in product(main_files, suffices):
41
        define_macros = [('WITH_PYTHON', None)]
Daniel Falbel's avatar
Daniel Falbel committed
42
43
44
45

        if sys.platform == 'win32':
            define_macros += [('torchsparse_EXPORTS', None)]

rusty1s's avatar
rusty1s committed
46
47
48
49
50
51
52
53
54
55
56
        libraries = []
        if WITH_METIS:
            define_macros += [('WITH_METIS', None)]
            libraries += ['metis']
        if WITH_MTMETIS:
            define_macros += [('WITH_MTMETIS', None)]
            define_macros += [('MTMETIS_64BIT_VERTICES', None)]
            define_macros += [('MTMETIS_64BIT_EDGES', None)]
            define_macros += [('MTMETIS_64BIT_WEIGHTS', None)]
            define_macros += [('MTMETIS_64BIT_PARTITIONS', None)]
            libraries += ['mtmetis', 'wildriver']
rusty1s's avatar
rusty1s committed
57

rusty1s's avatar
rusty1s committed
58
        extra_compile_args = {'cxx': ['-O2']}
rusty1s's avatar
rusty1s committed
59
60
        if not os.name == 'nt':  # Not on Windows:
            extra_compile_args['cxx'] += ['-Wno-sign-compare']
61
        extra_link_args = [] if WITH_SYMBOLS else ['-s']
rusty1s's avatar
rusty1s committed
62
63

        info = parallel_info()
rusty1s's avatar
rusty1s committed
64
65
        if ('backend: OpenMP' in info and 'OpenMP not found' not in info
                and sys.platform != 'darwin'):
rusty1s's avatar
rusty1s committed
66
67
68
69
70
71
72
73
            extra_compile_args['cxx'] += ['-DAT_PARALLEL_OPENMP']
            if sys.platform == 'win32':
                extra_compile_args['cxx'] += ['/openmp']
            else:
                extra_compile_args['cxx'] += ['-fopenmp']
        else:
            print('Compiling without OpenMP...')

Amit Aflalo's avatar
Amit Aflalo committed
74
75
76
77
78
        # Compile for mac arm64
        if (sys.platform == 'darwin' and platform.machine() == 'arm64'):
            extra_compile_args['cxx'] += ['-arch', 'arm64']
            extra_link_args += ['-arch', 'arm64']

rusty1s's avatar
rusty1s committed
79
80
81
82
        if suffix == 'cuda':
            define_macros += [('WITH_CUDA', None)]
            nvcc_flags = os.getenv('NVCC_FLAGS', '')
            nvcc_flags = [] if nvcc_flags == '' else nvcc_flags.split(' ')
83
            nvcc_flags += ['--expt-relaxed-constexpr', '-O2']
rusty1s's avatar
rusty1s committed
84
85
86
87
88
89
90
91
            extra_compile_args['nvcc'] = nvcc_flags

            if sys.platform == 'win32':
                extra_link_args += ['cusparse.lib']
            else:
                extra_link_args += ['-lcusparse', '-l', 'cusparse']

        name = main.split(os.sep)[-1][:-4]
rusty1s's avatar
rusty1s committed
92
93
        sources = [main]

rusty1s's avatar
rusty1s committed
94
        path = osp.join(extensions_dir, 'cpu', f'{name}_cpu.cpp')
rusty1s's avatar
rusty1s committed
95
96
97
        if osp.exists(path):
            sources += [path]

rusty1s's avatar
rusty1s committed
98
        path = osp.join(extensions_dir, 'cuda', f'{name}_cuda.cu')
rusty1s's avatar
rusty1s committed
99
        if suffix == 'cuda' and osp.exists(path):
rusty1s's avatar
rusty1s committed
100
            sources += [path]
rusty1s's avatar
rusty1s committed
101

rusty1s's avatar
rusty1s committed
102
        Extension = CppExtension if suffix == 'cpu' else CUDAExtension
rusty1s's avatar
rusty1s committed
103
        extension = Extension(
rusty1s's avatar
rusty1s committed
104
            f'torch_sparse._{name}_{suffix}',
rusty1s's avatar
rusty1s committed
105
106
107
108
            sources,
            include_dirs=[extensions_dir],
            define_macros=define_macros,
            extra_compile_args=extra_compile_args,
109
            extra_link_args=extra_link_args,
rusty1s's avatar
rusty1s committed
110
            libraries=libraries,
rusty1s's avatar
rusty1s committed
111
112
113
114
115
        )
        extensions += [extension]

    return extensions

rusty1s's avatar
rusty1s committed
116

rusty1s's avatar
rusty1s committed
117
118
119
120
121
122
123
124
install_requires = [
    'scipy',
]

test_requires = [
    'pytest',
    'pytest-cov',
]
rusty1s's avatar
rusty1s committed
125
126
127

setup(
    name='torch_sparse',
rusty1s's avatar
rusty1s committed
128
    version=__version__,
rusty1s's avatar
rusty1s committed
129
130
    description=('PyTorch Extension Library of Optimized Autograd Sparse '
                 'Matrix Operations'),
rusty1s's avatar
rusty1s committed
131
132
133
134
135
136
137
138
139
140
141
    author='Matthias Fey',
    author_email='matthias.fey@tu-dortmund.de',
    url=URL,
    download_url=f'{URL}/archive/{__version__}.tar.gz',
    keywords=[
        'pytorch',
        'sparse',
        'sparse-matrices',
        'autograd',
    ],
    python_requires='>=3.7',
rusty1s's avatar
rusty1s committed
142
    install_requires=install_requires,
rusty1s's avatar
rusty1s committed
143
144
145
    extras_require={
        'test': test_requires,
    },
rusty1s's avatar
rusty1s committed
146
147
    ext_modules=get_extensions() if not BUILD_DOCS else [],
    cmdclass={
rusty1s's avatar
rusty1s committed
148
149
        'build_ext':
        BuildExtension.with_options(no_python_abi_suffix=True, use_ninja=False)
rusty1s's avatar
rusty1s committed
150
    },
151
    packages=find_packages(),
rusty1s's avatar
rusty1s committed
152
    include_package_data=True,
153
)