forked from chainer/chainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainer_setup_build.py
261 lines (219 loc) · 7.89 KB
/
chainer_setup_build.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from __future__ import print_function
from distutils import ccompiler
from distutils import sysconfig
import os
from os import path
import sys
import pkg_resources
import setuptools
from install import build
from install import utils
required_cython_version = pkg_resources.parse_version('0.24.0')
MODULES = [
{
'name': 'cuda',
'file': [
'cupy.core.core',
'cupy.core.flags',
'cupy.core.internal',
'cupy.cuda.cublas',
'cupy.cuda.curand',
'cupy.cuda.device',
'cupy.cuda.driver',
'cupy.cuda.memory',
'cupy.cuda.pinned_memory',
'cupy.cuda.profiler',
'cupy.cuda.nvtx',
'cupy.cuda.function',
'cupy.cuda.runtime',
'cupy.util',
],
'include': [
'cublas_v2.h',
'cuda.h',
'cuda_profiler_api.h',
'cuda_runtime.h',
'curand.h',
'nvToolsExt.h',
],
'libraries': [
'cublas',
'cuda',
'cudart',
'curand',
'nvToolsExt',
],
'check_method': build.check_cuda_version,
},
{
'name': 'cudnn',
'file': [
'cupy.cuda.cudnn',
],
'include': [
'cudnn.h',
],
'libraries': [
'cudnn',
],
'check_method': build.check_cudnn_version,
},
{
'name': 'cusolver',
'file': [
'cupy.cuda.cusolver',
],
'include': [
'cusolverDn.h',
],
'libraries': [
'cusolver',
],
'check_method': build.check_cusolver_version,
}
]
if sys.platform == 'win32':
mod_cuda = MODULES[0]
mod_cuda['libraries'].remove('nvToolsExt')
if utils.search_on_path(['nvToolsExt64_1.dll']) is None:
mod_cuda['file'].remove('cupy.cuda.nvtx')
mod_cuda['include'].remove('nvToolsExt.h')
utils.print_warning(
'Cannot find nvToolsExt. nvtx was disabled.')
else:
mod_cuda['libraries'].append('nvToolsExt64_1')
def check_readthedocs_environment():
return os.environ.get('READTHEDOCS', None) == 'True'
def check_library(compiler, includes=(), libraries=(),
include_dirs=(), library_dirs=()):
source = ''.join(['#include <%s>\n' % header for header in includes])
source += 'int main(int argc, char* argv[]) {return 0;}'
try:
# We need to try to build a shared library because distutils
# uses different option to build an executable and a shared library.
# Especially when a user build an executable, distutils does not use
# LDFLAGS environment variable.
build.build_shlib(compiler, source, libraries,
include_dirs, library_dirs)
except Exception as e:
print(e)
return False
return True
def make_extensions(options, compiler, use_cython):
"""Produce a list of Extension instances which passed to cythonize()."""
no_cuda = options['no_cuda']
settings = build.get_compiler_setting()
include_dirs = settings['include_dirs']
settings['include_dirs'] = [
x for x in include_dirs if path.exists(x)]
settings['library_dirs'] = [
x for x in settings['library_dirs'] if path.exists(x)]
if sys.platform != 'win32':
settings['runtime_library_dirs'] = settings['library_dirs']
if sys.platform == 'darwin':
args = settings.setdefault('extra_link_args', [])
args.append(
'-Wl,' + ','.join('-rpath,' + p
for p in settings['library_dirs']))
# -rpath is only supported when targetting Mac OS X 10.5 or later
args.append('-mmacosx-version-min=10.5')
# This is a workaround for Anaconda.
# Anaconda installs libstdc++ from GCC 4.8 and it is not compatible
# with GCC 5's new ABI.
settings['define_macros'].append(('_GLIBCXX_USE_CXX11_ABI', '0'))
if options['linetrace']:
settings['define_macros'].append(('CYTHON_TRACE', '1'))
settings['define_macros'].append(('CYTHON_TRACE_NOGIL', '1'))
if no_cuda:
settings['define_macros'].append(('CUPY_NO_CUDA', '1'))
ret = []
ext = '.pyx' if use_cython else '.cpp'
for module in MODULES:
print('Include directories:', settings['include_dirs'])
print('Library directories:', settings['library_dirs'])
if not no_cuda:
if not check_library(compiler,
includes=module['include'],
include_dirs=settings['include_dirs']):
utils.print_warning(
'Include files not found: %s' % module['include'],
'Skip installing %s support' % module['name'],
'Check your CFLAGS environment variable')
continue
if not check_library(compiler,
libraries=module['libraries'],
library_dirs=settings['library_dirs']):
utils.print_warning(
'Cannot link libraries: %s' % module['libraries'],
'Skip installing %s support' % module['name'],
'Check your LDFLAGS environment variable')
continue
if 'check_method' in module and \
not module['check_method'](compiler, settings):
continue
s = settings.copy()
if not no_cuda:
s['libraries'] = module['libraries']
ret.extend([
setuptools.Extension(f, [path.join(*f.split('.')) + ext], **s)
for f in module['file']])
return ret
def parse_args():
cupy_profile = '--cupy-profile' in sys.argv
if cupy_profile:
sys.argv.remove('--cupy-profile')
cupy_coverage = '--cupy-coverage' in sys.argv
if cupy_coverage:
sys.argv.remove('--cupy-coverage')
no_cuda = '--cupy-no-cuda' in sys.argv
if no_cuda:
sys.argv.remove('--cupy-no-cuda')
arg_options = {
'profile': cupy_profile,
'linetrace': cupy_coverage,
'annotate': cupy_coverage,
'no_cuda': no_cuda,
}
if check_readthedocs_environment():
arg_options['no_cuda'] = True
return arg_options
def check_cython_version():
try:
import Cython
cython_version = pkg_resources.parse_version(Cython.__version__)
return cython_version >= required_cython_version
except ImportError:
return False
def cythonize(extensions, arg_options):
import Cython.Build
directive_keys = ('linetrace', 'profile')
directives = {key: arg_options[key] for key in directive_keys}
cythonize_option_keys = ('annotate',)
cythonize_options = {key: arg_options[key]
for key in cythonize_option_keys}
return Cython.Build.cythonize(
extensions, verbose=True,
compiler_directives=directives, **cythonize_options)
def check_extensions(extensions):
for x in extensions:
for f in x.sources:
if not path.isfile(f):
msg = ('Missing file: %s\n' % f +
'Please install Cython. ' +
'Please also check the version of Cython.\n' +
'See http://docs.chainer.org/en/stable/install.html')
raise RuntimeError(msg)
def get_ext_modules():
arg_options = parse_args()
print('Options:', arg_options)
# We need to call get_config_vars to initialize _config_vars in distutils
# see #1849
sysconfig.get_config_vars()
compiler = ccompiler.new_compiler()
sysconfig.customize_compiler(compiler)
use_cython = check_cython_version()
extensions = make_extensions(arg_options, compiler, use_cython)
if use_cython:
extensions = cythonize(extensions, arg_options)
check_extensions(extensions)
return extensions