Skip to content

Commit ca7cce2

Browse files
authored
Merge pull request #180 from kkleidal/build-cpu-and-gpu-versions
Build both cpu and gpu binaries
2 parents ab4cd9d + cc8aa66 commit ca7cce2

File tree

10 files changed

+70
-60
lines changed

10 files changed

+70
-60
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ install:
112112
- source script/torch.sh
113113
- pip install flake8
114114
- pip install codecov
115-
- pip install .[test]
115+
- travis_wait 30 pip install -e .
116116
script:
117117
- flake8 .
118118
- python setup.py test
119119
after_success:
120-
- python setup.py bdist_wheel --dist-dir=dist/torch-${TORCH_VERSION}
121-
- python script/rename_wheel.py ${IDX}
120+
- python setup.py bdist_wheel --dist-dir=dist
121+
- ls -lah dist/
122122
- codecov
123123
deploy:
124124
provider: s3
@@ -127,8 +127,8 @@ deploy:
127127
access_key_id: ${S3_ACCESS_KEY}
128128
secret_access_key: ${S3_SECRET_ACCESS_KEY}
129129
bucket: pytorch-geometric.com
130-
local_dir: dist/torch-${TORCH_VERSION}
131-
upload_dir: whl/torch-${TORCH_VERSION}
130+
local_dir: dist
131+
upload_dir: whl/torch-${TORCH_VERSION}+${IDX}
132132
acl: public_read
133133
on:
134134
all_branches: true

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.0)
22
project(torchscatter)
33
set(CMAKE_CXX_STANDARD 14)
4-
set(TORCHSCATTER_VERSION 2.0.5)
4+
set(TORCHSCATTER_VERSION 2.0.6)
55

66
option(WITH_CUDA "Enable CUDA support" OFF)
77

csrc/scatter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#endif
1010

1111
#ifdef _WIN32
12-
PyMODINIT_FUNC PyInit__scatter(void) { return NULL; }
12+
#ifdef WITH_CUDA
13+
PyMODINIT_FUNC PyInit__scatter_cuda(void) { return NULL; }
14+
#else
15+
PyMODINIT_FUNC PyInit__scatter_cpu(void) { return NULL; }
16+
#endif
1317
#endif
1418

1519
torch::Tensor broadcast(torch::Tensor src, torch::Tensor other, int64_t dim) {

csrc/segment_coo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#endif
1010

1111
#ifdef _WIN32
12-
PyMODINIT_FUNC PyInit__segment_coo(void) { return NULL; }
12+
#ifdef WITH_CUDA
13+
PyMODINIT_FUNC PyInit__segment_coo_cuda(void) { return NULL; }
14+
#else
15+
PyMODINIT_FUNC PyInit__segment_coo_cpu(void) { return NULL; }
16+
#endif
1317
#endif
1418

1519
std::tuple<torch::Tensor, torch::optional<torch::Tensor>>

csrc/segment_csr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#endif
1010

1111
#ifdef _WIN32
12-
PyMODINIT_FUNC PyInit__segment_csr(void) { return NULL; }
12+
#ifdef WITH_CUDA
13+
PyMODINIT_FUNC PyInit__segment_csr_cuda(void) { return NULL; }
14+
#else
15+
PyMODINIT_FUNC PyInit__segment_csr_cpu(void) { return NULL; }
16+
#endif
1317
#endif
1418

1519
std::tuple<torch::Tensor, torch::optional<torch::Tensor>>

csrc/version.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
#endif
77

88
#ifdef _WIN32
9-
PyMODINIT_FUNC PyInit__version(void) { return NULL; }
9+
#ifdef WITH_CUDA
10+
PyMODINIT_FUNC PyInit__version_cuda(void) { return NULL; }
11+
#else
12+
PyMODINIT_FUNC PyInit__version_cpu(void) { return NULL; }
13+
#endif
1014
#endif
1115

1216
int64_t cuda_version() {

script/cuda.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ if [ "${TRAVIS_OS_NAME}" = "osx" ] && [ "$IDX" = "cpu" ]; then
6969
fi
7070

7171
if [ "${IDX}" = "cpu" ]; then
72-
export FORCE_CPU=1
72+
export FORCE_ONLY_CPU=1
7373
else
7474
export FORCE_CUDA=1
7575
fi

script/rename_wheel.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

setup.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,69 @@
11
import os
2-
import os.path as osp
2+
import sys
33
import glob
4+
import os.path as osp
5+
from itertools import product
46
from setuptools import setup, find_packages
57

68
import torch
9+
from torch.__config__ import parallel_info
710
from torch.utils.cpp_extension import BuildExtension
811
from torch.utils.cpp_extension import CppExtension, CUDAExtension, CUDA_HOME
912

1013
WITH_CUDA = torch.cuda.is_available() and CUDA_HOME is not None
14+
suffices = ['cpu', 'cuda'] if WITH_CUDA else ['cpu']
1115
if os.getenv('FORCE_CUDA', '0') == '1':
12-
WITH_CUDA = True
13-
if os.getenv('FORCE_CPU', '0') == '1':
14-
WITH_CUDA = False
16+
suffices = ['cuda', 'cpu']
17+
if os.getenv('FORCE_ONLY_CUDA', '0') == '1':
18+
suffices = ['cuda']
19+
if os.getenv('FORCE_ONLY_CPU', '0') == '1':
20+
suffices = ['cpu']
1521

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

1824

1925
def get_extensions():
20-
Extension = CppExtension
21-
define_macros = []
22-
extra_compile_args = {'cxx': ['-O2']}
23-
extra_link_args = ['-s']
24-
25-
if WITH_CUDA:
26-
Extension = CUDAExtension
27-
define_macros += [('WITH_CUDA', None)]
28-
nvcc_flags = os.getenv('NVCC_FLAGS', '')
29-
nvcc_flags = [] if nvcc_flags == '' else nvcc_flags.split(' ')
30-
nvcc_flags += ['-arch=sm_35', '--expt-relaxed-constexpr', '-O2']
31-
extra_compile_args['nvcc'] = nvcc_flags
26+
extensions = []
3227

3328
extensions_dir = osp.join(osp.dirname(osp.abspath(__file__)), 'csrc')
3429
main_files = glob.glob(osp.join(extensions_dir, '*.cpp'))
35-
extensions = []
36-
for main in main_files:
37-
name = main.split(os.sep)[-1][:-4]
3830

31+
for main, suffix in product(main_files, suffices):
32+
define_macros = []
33+
extra_compile_args = {'cxx': ['-O2']}
34+
extra_link_args = ['-s']
35+
36+
info = parallel_info()
37+
if 'backend: OpenMP' in info and 'OpenMP not found' not in info:
38+
extra_compile_args['cxx'] += ['-DAT_PARALLEL_OPENMP']
39+
if sys.platform == 'win32':
40+
extra_compile_args['cxx'] += ['/openmp']
41+
else:
42+
extra_compile_args['cxx'] += ['-fopenmp']
43+
else:
44+
print('Compiling without OpenMP...')
45+
46+
if suffix == 'cuda':
47+
define_macros += [('WITH_CUDA', None)]
48+
nvcc_flags = os.getenv('NVCC_FLAGS', '')
49+
nvcc_flags = [] if nvcc_flags == '' else nvcc_flags.split(' ')
50+
nvcc_flags += ['-arch=sm_35', '--expt-relaxed-constexpr']
51+
extra_compile_args['nvcc'] = nvcc_flags
52+
53+
name = main.split(os.sep)[-1][:-4]
3954
sources = [main]
4055

4156
path = osp.join(extensions_dir, 'cpu', f'{name}_cpu.cpp')
4257
if osp.exists(path):
4358
sources += [path]
4459

4560
path = osp.join(extensions_dir, 'cuda', f'{name}_cuda.cu')
46-
if WITH_CUDA and osp.exists(path):
61+
if suffix == 'cuda' and osp.exists(path):
4762
sources += [path]
4863

64+
Extension = CppExtension if suffix == 'cpu' else CUDAExtension
4965
extension = Extension(
50-
'torch_scatter._' + name,
66+
f'torch_scatter._{name}_{suffix}',
5167
sources,
5268
include_dirs=[extensions_dir],
5369
define_macros=define_macros,
@@ -65,7 +81,7 @@ def get_extensions():
6581

6682
setup(
6783
name='torch_scatter',
68-
version='2.0.5',
84+
version='2.0.6',
6985
author='Matthias Fey',
7086
author_email='[email protected]',
7187
url='https://github.com/rusty1s/pytorch_scatter',

torch_scatter/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
import torch
66

7-
__version__ = '2.0.5'
7+
__version__ = '2.0.6'
8+
9+
suffix = 'cuda' if torch.cuda.is_available() else 'cpu'
810

911
try:
1012
for library in ['_version', '_scatter', '_segment_csr', '_segment_coo']:
1113
torch.ops.load_library(importlib.machinery.PathFinder().find_spec(
12-
library, [osp.dirname(__file__)]).origin)
14+
f'{library}_{suffix}', [osp.dirname(__file__)]).origin)
1315
except AttributeError as e:
1416
if os.getenv('BUILD_DOCS', '0') != '1':
1517
raise AttributeError(e)
@@ -42,7 +44,7 @@
4244
torch.ops.torch_scatter.segment_max_coo = segment_coo_arg_placeholder
4345
torch.ops.torch_scatter.gather_coo = gather_coo_placeholder
4446

45-
if torch.cuda.is_available() and torch.version.cuda: # pragma: no cover
47+
if torch.cuda.is_available(): # pragma: no cover
4648
cuda_version = torch.ops.torch_scatter.cuda_version()
4749

4850
if cuda_version == -1:

0 commit comments

Comments
 (0)