Skip to content

Commit

Permalink
Merge pull request rusty1s#180 from kkleidal/build-cpu-and-gpu-versions
Browse files Browse the repository at this point in the history
Build both cpu and gpu binaries
  • Loading branch information
rusty1s authored Mar 2, 2021
2 parents ab4cd9d + cc8aa66 commit ca7cce2
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 60 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ install:
- source script/torch.sh
- pip install flake8
- pip install codecov
- pip install .[test]
- travis_wait 30 pip install -e .
script:
- flake8 .
- python setup.py test
after_success:
- python setup.py bdist_wheel --dist-dir=dist/torch-${TORCH_VERSION}
- python script/rename_wheel.py ${IDX}
- python setup.py bdist_wheel --dist-dir=dist
- ls -lah dist/
- codecov
deploy:
provider: s3
Expand All @@ -127,8 +127,8 @@ deploy:
access_key_id: ${S3_ACCESS_KEY}
secret_access_key: ${S3_SECRET_ACCESS_KEY}
bucket: pytorch-geometric.com
local_dir: dist/torch-${TORCH_VERSION}
upload_dir: whl/torch-${TORCH_VERSION}
local_dir: dist
upload_dir: whl/torch-${TORCH_VERSION}+${IDX}
acl: public_read
on:
all_branches: true
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(torchscatter)
set(CMAKE_CXX_STANDARD 14)
set(TORCHSCATTER_VERSION 2.0.5)
set(TORCHSCATTER_VERSION 2.0.6)

option(WITH_CUDA "Enable CUDA support" OFF)

Expand Down
6 changes: 5 additions & 1 deletion csrc/scatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#endif

#ifdef _WIN32
PyMODINIT_FUNC PyInit__scatter(void) { return NULL; }
#ifdef WITH_CUDA
PyMODINIT_FUNC PyInit__scatter_cuda(void) { return NULL; }
#else
PyMODINIT_FUNC PyInit__scatter_cpu(void) { return NULL; }
#endif
#endif

torch::Tensor broadcast(torch::Tensor src, torch::Tensor other, int64_t dim) {
Expand Down
6 changes: 5 additions & 1 deletion csrc/segment_coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#endif

#ifdef _WIN32
PyMODINIT_FUNC PyInit__segment_coo(void) { return NULL; }
#ifdef WITH_CUDA
PyMODINIT_FUNC PyInit__segment_coo_cuda(void) { return NULL; }
#else
PyMODINIT_FUNC PyInit__segment_coo_cpu(void) { return NULL; }
#endif
#endif

std::tuple<torch::Tensor, torch::optional<torch::Tensor>>
Expand Down
6 changes: 5 additions & 1 deletion csrc/segment_csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#endif

#ifdef _WIN32
PyMODINIT_FUNC PyInit__segment_csr(void) { return NULL; }
#ifdef WITH_CUDA
PyMODINIT_FUNC PyInit__segment_csr_cuda(void) { return NULL; }
#else
PyMODINIT_FUNC PyInit__segment_csr_cpu(void) { return NULL; }
#endif
#endif

std::tuple<torch::Tensor, torch::optional<torch::Tensor>>
Expand Down
6 changes: 5 additions & 1 deletion csrc/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#endif

#ifdef _WIN32
PyMODINIT_FUNC PyInit__version(void) { return NULL; }
#ifdef WITH_CUDA
PyMODINIT_FUNC PyInit__version_cuda(void) { return NULL; }
#else
PyMODINIT_FUNC PyInit__version_cpu(void) { return NULL; }
#endif
#endif

int64_t cuda_version() {
Expand Down
2 changes: 1 addition & 1 deletion script/cuda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ if [ "${TRAVIS_OS_NAME}" = "osx" ] && [ "$IDX" = "cpu" ]; then
fi

if [ "${IDX}" = "cpu" ]; then
export FORCE_CPU=1
export FORCE_ONLY_CPU=1
else
export FORCE_CUDA=1
fi
Expand Down
24 changes: 0 additions & 24 deletions script/rename_wheel.py

This file was deleted.

60 changes: 38 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,69 @@
import os
import os.path as osp
import sys
import glob
import os.path as osp
from itertools import product
from setuptools import setup, find_packages

import torch
from torch.__config__ import parallel_info
from torch.utils.cpp_extension import BuildExtension
from torch.utils.cpp_extension import CppExtension, CUDAExtension, CUDA_HOME

WITH_CUDA = torch.cuda.is_available() and CUDA_HOME is not None
suffices = ['cpu', 'cuda'] if WITH_CUDA else ['cpu']
if os.getenv('FORCE_CUDA', '0') == '1':
WITH_CUDA = True
if os.getenv('FORCE_CPU', '0') == '1':
WITH_CUDA = False
suffices = ['cuda', 'cpu']
if os.getenv('FORCE_ONLY_CUDA', '0') == '1':
suffices = ['cuda']
if os.getenv('FORCE_ONLY_CPU', '0') == '1':
suffices = ['cpu']

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


def get_extensions():
Extension = CppExtension
define_macros = []
extra_compile_args = {'cxx': ['-O2']}
extra_link_args = ['-s']

if WITH_CUDA:
Extension = CUDAExtension
define_macros += [('WITH_CUDA', None)]
nvcc_flags = os.getenv('NVCC_FLAGS', '')
nvcc_flags = [] if nvcc_flags == '' else nvcc_flags.split(' ')
nvcc_flags += ['-arch=sm_35', '--expt-relaxed-constexpr', '-O2']
extra_compile_args['nvcc'] = nvcc_flags
extensions = []

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

for main, suffix in product(main_files, suffices):
define_macros = []
extra_compile_args = {'cxx': ['-O2']}
extra_link_args = ['-s']

info = parallel_info()
if 'backend: OpenMP' in info and 'OpenMP not found' not in info:
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...')

if suffix == 'cuda':
define_macros += [('WITH_CUDA', None)]
nvcc_flags = os.getenv('NVCC_FLAGS', '')
nvcc_flags = [] if nvcc_flags == '' else nvcc_flags.split(' ')
nvcc_flags += ['-arch=sm_35', '--expt-relaxed-constexpr']
extra_compile_args['nvcc'] = nvcc_flags

name = main.split(os.sep)[-1][:-4]
sources = [main]

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

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

Extension = CppExtension if suffix == 'cpu' else CUDAExtension
extension = Extension(
'torch_scatter._' + name,
f'torch_scatter._{name}_{suffix}',
sources,
include_dirs=[extensions_dir],
define_macros=define_macros,
Expand All @@ -65,7 +81,7 @@ def get_extensions():

setup(
name='torch_scatter',
version='2.0.5',
version='2.0.6',
author='Matthias Fey',
author_email='[email protected]',
url='https://github.com/rusty1s/pytorch_scatter',
Expand Down
8 changes: 5 additions & 3 deletions torch_scatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import torch

__version__ = '2.0.5'
__version__ = '2.0.6'

suffix = 'cuda' if torch.cuda.is_available() else 'cpu'

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

if torch.cuda.is_available() and torch.version.cuda: # pragma: no cover
if torch.cuda.is_available(): # pragma: no cover
cuda_version = torch.ops.torch_scatter.cuda_version()

if cuda_version == -1:
Expand Down

0 comments on commit ca7cce2

Please sign in to comment.