setup.py 4.21 KB
Newer Older
1
import os
2
import glob
3
import subprocess
4
from setuptools import setup, find_packages
5
6
7
from distutils import log
import sys

Jinze Xue's avatar
Jinze Xue committed
8
9
10
11
12
13
BUILD_CUAEV_ALL_SM = '--cuaev-all-sms' in sys.argv
if BUILD_CUAEV_ALL_SM:
    sys.argv.remove('--cuaev-all-sms')

FAST_BUILD_CUAEV = '--cuaev' in sys.argv
if FAST_BUILD_CUAEV:
14
15
    sys.argv.remove('--cuaev')

Jinze Xue's avatar
Jinze Xue committed
16
if not BUILD_CUAEV_ALL_SM and not FAST_BUILD_CUAEV:
Gao, Xiang's avatar
Gao, Xiang committed
17
    log.warn("Will not install cuaev")  # type: ignore
Xiang Gao's avatar
Xiang Gao committed
18

19
20
21
with open("README.md", "r") as fh:
    long_description = fh.read()

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

def maybe_download_cub():
    import torch
    dirs = torch.utils.cpp_extension.include_paths(cuda=True)
    for d in dirs:
        cubdir = os.path.join(d, 'cub')
        log.info(f'Searching for cub at {cubdir}...')
        if os.path.isdir(cubdir):
            log.info(f'Found cub in {cubdir}')
            return []
    # if no cub, download it to include dir from github
    if not os.path.isdir('./include/cub'):
        if not os.path.exists('./include'):
            os.makedirs('include')
        commands = """
        echo "Downloading CUB library";
        wget -q https://github.com/NVIDIA/cub/archive/main.zip;
        unzip -q main.zip -d include;
        mv include/cub-main/cub include;
        echo "Removing unnecessary files";
        rm main.zip;
        rm -rf include/cub-main;
        """
        subprocess.run(commands, shell=True, check=True, universal_newlines=True)
    return [os.path.abspath("./include")]


Jinze Xue's avatar
Jinze Xue committed
49
def cuda_extension(build_all=False):
50
51
    import torch
    from torch.utils.cpp_extension import CUDAExtension
Jinze Xue's avatar
Jinze Xue committed
52
    SMs = None
53
    print('-' * 75)
Jinze Xue's avatar
Jinze Xue committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    if not build_all:
        SMs = []
        devices = torch.cuda.device_count()
        print('FAST_BUILD_CUAEV: ON')
        print('This build will only support the following devices or the devices with same cuda capability: ')
        for i in range(devices):
            d = 'cuda:{}'.format(i)
            sm = torch.cuda.get_device_capability(i)
            sm = int(f'{sm[0]}{sm[1]}')
            if sm >= 50:
                print('{}: {}'.format(i, torch.cuda.get_device_name(d)))
                print('   {}'.format(torch.cuda.get_device_properties(i)))
            if sm not in SMs and sm >= 50:
                SMs.append(sm)
68

Jinze Xue's avatar
Jinze Xue committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    nvcc_args = ["-Xptxas=-v", '--expt-extended-lambda', '-use_fast_math']
    if SMs:
        for sm in SMs:
            nvcc_args.append(f"-gencode=arch=compute_{sm},code=sm_{sm}")
    else:
        nvcc_args.append("-gencode=arch=compute_60,code=sm_60")
        nvcc_args.append("-gencode=arch=compute_61,code=sm_61")
        nvcc_args.append("-gencode=arch=compute_70,code=sm_70")
        cuda_version = float(torch.version.cuda)
        if cuda_version >= 10:
            nvcc_args.append("-gencode=arch=compute_75,code=sm_75")
        if cuda_version >= 11:
            nvcc_args.append("-gencode=arch=compute_80,code=sm_80")
        if cuda_version >= 11.1:
            nvcc_args.append("-gencode=arch=compute_86,code=sm_86")
    print("nvcc_args: ", nvcc_args)
85
    print('-' * 75)
86
    return CUDAExtension(
87
88
        name='torchani.cuaev',
        pkg='torchani.cuaev',
89
        sources=glob.glob('torchani/cuaev/*.cu'),
90
        include_dirs=maybe_download_cub(),
91
        extra_compile_args={'cxx': ['-std=c++17'], 'nvcc': nvcc_args})
92
93
94


def cuaev_kwargs():
Jinze Xue's avatar
Jinze Xue committed
95
    if not BUILD_CUAEV_ALL_SM and not FAST_BUILD_CUAEV:
96
97
98
99
100
101
102
103
104
105
        return dict(
            provides=['torchani']
        )
    from torch.utils.cpp_extension import BuildExtension
    kwargs = dict(
        provides=[
            'torchani',
            'torchani.cuaev',
        ],
        ext_modules=[
Jinze Xue's avatar
Jinze Xue committed
106
            cuda_extension(BUILD_CUAEV_ALL_SM)
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        ],
        cmdclass={
            'build_ext': BuildExtension,
        })
    return kwargs


setup(
    name='torchani',
    description='PyTorch implementation of ANI',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url='https://github.com/aiqm/torchani',
    author='Xiang Gao',
    author_email='qasdfgtyuiop@gmail.com',
    license='MIT',
    packages=find_packages(),
    include_package_data=True,
    use_scm_version=True,
    setup_requires=['setuptools_scm'],
    install_requires=[
128
        'torch',
129
        'lark-parser',
130
131
        'requests',
        'importlib_metadata',
132
    ],
133
134
    **cuaev_kwargs()
)