-
Notifications
You must be signed in to change notification settings - Fork 0
/
cupy_setup_build.py
1015 lines (874 loc) · 33.6 KB
/
cupy_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import copy
from distutils import ccompiler
from distutils import errors
from distutils import msvccompiler
from distutils import sysconfig
from distutils import unixccompiler
import os
from os import path
import shutil
import sys
import pkg_resources
import setuptools
from setuptools.command import build_ext
from setuptools.command import sdist
from install import build
from install.build import PLATFORM_DARWIN
from install.build import PLATFORM_LINUX
from install.build import PLATFORM_WIN32
required_cython_version = pkg_resources.parse_version('0.28.0')
ignore_cython_versions = [
]
use_hip = bool(int(os.environ.get('CUPY_INSTALL_USE_HIP', '0')))
# The value of the key 'file' is a list that contains extension names
# or tuples of an extension name and a list of other souces files
# required to build the extension such as .cpp files and .cu files.
#
# <extension name> | (<extension name>, a list of <other source>)
#
# The extension name is also interpreted as the name of the Cython
# source file required to build the extension with appending '.pyx'
# file extension.
MODULES = []
cuda_files = [
'cupy_backends.cuda.api.driver',
'cupy_backends.cuda.api.runtime',
'cupy_backends.cuda.libs.cublas',
'cupy_backends.cuda.libs.curand',
'cupy_backends.cuda.libs.cusparse',
'cupy_backends.cuda.libs.nvrtc',
'cupy_backends.cuda.libs.profiler',
'cupy_backends.cuda.stream',
'cupy.core._accelerator',
'cupy.core._carray',
'cupy.core._cub_reduction',
'cupy.core._dtype',
'cupy.core._fusion_kernel',
'cupy.core._fusion_thread_local',
'cupy.core._fusion_trace',
'cupy.core._fusion_variable',
'cupy.core._kernel',
'cupy.core._memory_range',
'cupy.core._optimize_config',
'cupy.core._reduction',
'cupy.core._routines_binary',
'cupy.core._routines_indexing',
'cupy.core._routines_logic',
'cupy.core._routines_manipulation',
'cupy.core._routines_math',
'cupy.core._routines_sorting',
'cupy.core._routines_statistics',
'cupy.core._scalar',
'cupy.core.core',
'cupy.core.dlpack',
'cupy.core.flags',
'cupy.core.internal',
'cupy.core.fusion',
'cupy.core.new_fusion',
'cupy.core.raw',
'cupy.cuda.common',
'cupy.cuda.cufft',
'cupy.cuda.device',
'cupy.cuda.memory',
'cupy.cuda.memory_hook',
'cupy.cuda.pinned_memory',
'cupy.cuda.function',
'cupy.cuda.stream',
'cupy.cuda.texture',
'cupy.fft._cache',
'cupy.lib.polynomial',
'cupy._util'
]
if use_hip:
# We handle nvtx (and likely any other future support) here, because
# the HIP stubs (cupy_hip.h/cupy_hip_common.h) would cause many symbols
# to leak into all these modules even if unused. It's easier for all of
# them to link to the same set of shared libraries.
MODULES.append({
'name': 'cuda',
'file': cuda_files + [
'cupy.cuda.nvtx',
'cupy_backends.cuda.libs.cusolver',
],
'include': [
'hip/hip_runtime_api.h',
'hip/hiprtc.h',
'hipblas.h',
'hiprand/hiprand.h',
'hipfft.h',
'roctx.h',
'rocsolver.h',
],
'libraries': [
'hiprtc',
'hip_hcc',
'hipblas',
'hiprand',
'rocfft',
'roctx64',
'rocblas',
'rocsolver',
],
})
else:
MODULES.append({
'name': 'cuda',
'file': cuda_files,
'include': [
'cublas_v2.h',
'cuda.h',
'cuda_profiler_api.h',
'cuda_runtime.h',
'cufft.h',
'curand.h',
'cusparse.h',
'nvrtc.h',
],
'libraries': [
'cublas',
'cuda',
'cudart',
'cufft',
'curand',
'cusparse',
'nvrtc',
],
'check_method': build.check_cuda_version,
'version_method': build.get_cuda_version,
})
if not use_hip:
MODULES.append({
'name': 'cusolver',
'file': [
'cupy_backends.cuda.libs.cusolver',
],
'include': [
'cusolverDn.h',
],
'libraries': [
'cusolver',
],
'check_method': build.check_cuda_version,
})
if not use_hip:
MODULES.append({
'name': 'cudnn',
'file': [
'cupy_backends.cuda.libs.cudnn',
'cupy.cudnn',
],
'include': [
'cudnn.h',
],
'libraries': [
'cudnn',
],
'check_method': build.check_cudnn_version,
'version_method': build.get_cudnn_version,
})
MODULES.append({
'name': 'nccl',
'file': [
'cupy.cuda.nccl',
],
'include': [
'nccl.h',
],
'libraries': [
'nccl',
],
'check_method': build.check_nccl_version,
'version_method': build.get_nccl_version,
})
MODULES.append({
'name': 'nvtx',
'file': [
'cupy.cuda.nvtx',
],
'include': [
'nvToolsExt.h',
],
'libraries': [
'nvToolsExt' if not PLATFORM_WIN32 else 'nvToolsExt64_1',
],
'check_method': build.check_nvtx,
})
MODULES.append({
'name': 'cutensor',
'file': [
'cupy_backends.cuda.libs.cutensor',
'cupy.cutensor',
],
'include': [
'cutensor.h',
],
'libraries': [
'cutensor',
'cublas',
],
'check_method': build.check_cutensor_version,
'version_method': build.get_cutensor_version,
})
MODULES.append({
'name': 'cub',
'file': [
('cupy.cuda.cub', ['cupy/cuda/cupy_cub.cu']),
],
'include': [
'cub/util_namespace.cuh', # dummy
],
'libraries': [
'cudart',
],
'check_method': build.check_cub_version,
'version_method': build.get_cub_version,
})
if bool(int(os.environ.get('CUPY_SETUP_ENABLE_THRUST', 1))):
if use_hip:
MODULES.append({
'name': 'thrust',
'file': [
('cupy.cuda.thrust', ['cupy/cuda/cupy_thrust.cu']),
],
'include': [
'thrust/version.h',
],
'libraries': [
'hiprtc',
'hip_hcc',
],
})
else:
MODULES.append({
'name': 'thrust',
'file': [
('cupy.cuda.thrust', ['cupy/cuda/cupy_thrust.cu']),
],
'include': [
'thrust/device_ptr.h',
'thrust/sequence.h',
'thrust/sort.h',
],
'libraries': [
'cudart',
],
'check_method': build.check_thrust_version,
'version_method': build.get_thrust_version,
})
def ensure_module_file(file):
if isinstance(file, tuple):
return file
else:
return file, []
def module_extension_name(file):
return ensure_module_file(file)[0]
def module_extension_sources(file, use_cython, no_cuda):
pyx, others = ensure_module_file(file)
base = path.join(*pyx.split('.'))
if use_cython:
pyx = base + '.pyx'
if not os.path.exists(pyx):
use_cython = False
print(
'NOTICE: Skipping cythonize as {} does not exist.'.format(pyx))
if not use_cython:
pyx = base + '.cpp'
# If CUDA SDK is not available, remove CUDA C files from extension sources
# and use stubs defined in header files.
if no_cuda:
others1 = []
for source in others:
base, ext = os.path.splitext(source)
if ext == '.cu':
continue
others1.append(source)
others = others1
return [pyx] + others
def check_readthedocs_environment():
return os.environ.get('READTHEDOCS', None) == 'True'
def check_library(compiler, includes=(), libraries=(),
include_dirs=(), library_dirs=(), define_macros=None,
extra_compile_args=()):
source = ''.join(['#include <%s>\n' % header for header in includes])
source += 'int main() {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, define_macros,
extra_compile_args)
except Exception as e:
print(e)
sys.stdout.flush()
return False
return True
def preconfigure_modules(compiler, settings):
"""Returns a list of modules buildable in given environment and settings.
For each module in MODULES list, this function checks if the module
can be built in the current environment and reports it.
Returns a list of module names available.
"""
nvcc_path = build.get_nvcc_path()
summary = [
'',
'************************************************************',
'* CuPy Configuration Summary *',
'************************************************************',
'',
'Build Environment:',
' Include directories: {}'.format(str(settings['include_dirs'])),
' Library directories: {}'.format(str(settings['library_dirs'])),
' nvcc command : {}'.format(
nvcc_path if nvcc_path else '(not found)'),
'',
'Environment Variables:',
]
for key in ['CFLAGS', 'LDFLAGS', 'LIBRARY_PATH',
'CUDA_PATH', 'NVTOOLSEXT_PATH', 'NVCC',
'ROCM_HOME']:
summary += [' {:<16}: {}'.format(key, os.environ.get(key, '(none)'))]
summary += [
'',
'Modules:',
]
ret = []
for module in MODULES:
installed = False
status = 'No'
errmsg = []
if module['name'] == 'cutensor':
cuda_version = build.get_cuda_version()
cuda_version = str(cuda_version // 1000) + '.' + \
str((cuda_version // 10) % 100)
cutensor_path = os.environ.get('CUTENSOR_PATH', '')
inc_path = os.path.join(cutensor_path, 'include')
if os.path.exists(inc_path):
settings['include_dirs'].append(inc_path)
lib_path = os.path.join(cutensor_path, 'lib', cuda_version)
if os.path.exists(lib_path):
settings['library_dirs'].append(lib_path)
print('')
print('-------- Configuring Module: {} --------'.format(
module['name']))
sys.stdout.flush()
if not check_library(
compiler,
includes=module['include'],
include_dirs=settings['include_dirs'],
define_macros=settings['define_macros'],
extra_compile_args=settings['extra_compile_args']):
errmsg = ['Include files not found: %s' % module['include'],
'Check your CFLAGS environment variable.']
elif not check_library(
compiler,
libraries=module['libraries'],
library_dirs=settings['library_dirs'],
define_macros=settings['define_macros'],
extra_compile_args=settings['extra_compile_args']):
errmsg = ['Cannot link libraries: %s' % module['libraries'],
'Check your LDFLAGS environment variable.']
elif ('check_method' in module and
not module['check_method'](compiler, settings)):
# Fail on per-library condition check (version requirements etc.)
installed = True
errmsg = ['The library is installed but not supported.']
elif module['name'] in ('thrust', 'cub') and nvcc_path is None:
installed = True
errmsg = ['nvcc command could not be found in PATH.',
'Check your PATH environment variable.']
else:
installed = True
status = 'Yes'
ret.append(module['name'])
if installed and 'version_method' in module:
status += ' (version {})'.format(module['version_method'](True))
summary += [
' {:<10}: {}'.format(module['name'], status)
]
# If error message exists...
if len(errmsg) != 0:
summary += [' -> {}'.format(m) for m in errmsg]
# Skip checking other modules when CUDA is unavailable.
if module['name'] == 'cuda':
break
if len(ret) != len(MODULES):
if 'cuda' in ret:
lines = [
'WARNING: Some modules could not be configured.',
'CuPy will be installed without these modules.',
]
else:
lines = [
'ERROR: CUDA could not be found on your system.',
]
summary += [
'',
] + lines + [
'Please refer to the Installation Guide for details:',
'https://docs.cupy.dev/en/stable/install.html',
'',
]
summary += [
'************************************************************',
'',
]
print('\n'.join(summary))
return ret, settings
def _rpath_base():
if PLATFORM_LINUX:
return '$ORIGIN'
elif PLATFORM_DARWIN:
return '@loader_path'
else:
raise Exception('not supported on this platform')
def make_extensions(options, compiler, use_cython):
"""Produce a list of Extension instances which passed to cythonize()."""
no_cuda = options['no_cuda']
use_hip = not no_cuda and options['use_hip']
settings = build.get_compiler_setting(use_hip)
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)]
# Adjust rpath to use CUDA libraries in `cupy/.data/lib/*.so`) from CuPy.
use_wheel_libs_rpath = (
0 < len(options['wheel_libs']) and not PLATFORM_WIN32)
# In the environment with CUDA 7.5 on Ubuntu 16.04, gcc5.3 does not
# automatically deal with memcpy because string.h header file has
# been changed. This is a workaround for that environment.
# See details in the below discussions:
# https://github.com/BVLC/caffe/issues/4046
# https://groups.google.com/forum/#!topic/theano-users/3ihQYiTRG4E
settings['define_macros'].append(('_FORCE_INLINES', '1'))
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'))
if use_hip:
settings['define_macros'].append(('CUPY_USE_HIP', '1'))
settings['define_macros'].append(('__HIP_PLATFORM_HCC__', '1'))
available_modules = []
if no_cuda:
available_modules = [m['name'] for m in MODULES]
else:
available_modules, settings = preconfigure_modules(compiler, settings)
if 'cuda' not in available_modules:
raise Exception('Your CUDA environment is invalid. '
'Please check above error log.')
ret = []
for module in MODULES:
if module['name'] not in available_modules:
continue
s = settings.copy()
if not no_cuda:
s['libraries'] = module['libraries']
compile_args = s.setdefault('extra_compile_args', [])
link_args = s.setdefault('extra_link_args', [])
if module['name'] == 'cusolver':
compile_args = s.setdefault('extra_compile_args', [])
link_args = s.setdefault('extra_link_args', [])
# openmp is required for cusolver
if use_hip:
pass
elif compiler.compiler_type == 'unix' and not PLATFORM_DARWIN:
# In mac environment, openmp is not required.
compile_args.append('-fopenmp')
link_args.append('-fopenmp')
elif compiler.compiler_type == 'msvc':
compile_args.append('/openmp')
original_s = s
for f in module['file']:
s = copy.deepcopy(original_s)
name = module_extension_name(f)
rpath = []
if not options['no_rpath']:
# Add library directories (e.g., `/usr/local/cuda/lib64`) to
# RPATH.
rpath += s['library_dirs']
if use_wheel_libs_rpath:
# Add `cupy/.data/lib` (where shared libraries included in
# wheels reside) to RPATH.
# The path is resolved relative to the module, e.g., use
# `$ORIGIN/../cupy/.data/lib` for `cupy/cudnn.so` and
# `$ORIGIN/../../../cupy/.data/lib` for
# `cupy_backends/cuda/libs/cudnn.so`.
depth = name.count('.')
rpath.append(
'{}{}/cupy/.data/lib'.format(_rpath_base(), '/..' * depth))
if not PLATFORM_WIN32 and not PLATFORM_LINUX:
s['runtime_library_dirs'] = rpath
if (PLATFORM_LINUX and s['library_dirs']) or PLATFORM_DARWIN:
ldflag = '-Wl,'
if PLATFORM_LINUX:
ldflag += '--disable-new-dtags,'
ldflag += ','.join('-rpath,' + p for p in rpath)
args = s.setdefault('extra_link_args', [])
args.append(ldflag)
if PLATFORM_DARWIN:
# -rpath is only supported when targeting Mac OS X 10.5 or
# later
args.append('-mmacosx-version-min=10.5')
sources = module_extension_sources(f, use_cython, no_cuda)
extension = setuptools.Extension(name, sources, **s)
ret.append(extension)
return ret
# TODO(oktua): use enviriment variable
def parse_args():
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
'--cupy-package-name', type=str, default='cupy',
help='alternate package name')
parser.add_argument(
'--cupy-long-description', type=str, default=None,
help='path to the long description file')
parser.add_argument(
'--cupy-wheel-lib', type=str, action='append', default=[],
help='shared library to copy into the wheel '
'(can be specified for multiple times)')
parser.add_argument(
'--cupy-wheel-include', type=str, action='append', default=[],
help='An include file to copy into the wheel. '
'Delimited by a colon. '
'The former part is a full path of the source include file and '
'the latter is the relative path within cupy wheel. '
'(can be specified for multiple times)')
parser.add_argument(
'--cupy-wheel-metadata', type=str, default=None,
help='wheel metadata (cupy/.data/_wheel.json)')
parser.add_argument(
'--cupy-no-rpath', action='store_true', default=False,
help='disable adding default library directories to RPATH')
parser.add_argument(
'--cupy-profile', action='store_true', default=False,
help='enable profiling for Cython code')
parser.add_argument(
'--cupy-coverage', action='store_true', default=False,
help='enable coverage for Cython code')
parser.add_argument(
'--cupy-no-cuda', action='store_true', default=False,
help='build CuPy with stub header file')
# parser.add_argument(
# '--cupy-use-hip', action='store_true', default=False,
# help='build CuPy with HIP')
opts, sys.argv = parser.parse_known_args(sys.argv)
arg_options = {
'package_name': opts.cupy_package_name,
'long_description': opts.cupy_long_description,
'wheel_libs': opts.cupy_wheel_lib, # list
'wheel_includes': opts.cupy_wheel_include, # list
'wheel_metadata': opts.cupy_wheel_metadata,
'no_rpath': opts.cupy_no_rpath,
'profile': opts.cupy_profile,
'linetrace': opts.cupy_coverage,
'annotate': opts.cupy_coverage,
'no_cuda': opts.cupy_no_cuda,
'use_hip': use_hip # opts.cupy_use_hip,
}
if check_readthedocs_environment():
arg_options['no_cuda'] = True
return arg_options
cupy_setup_options = parse_args()
print('Options:', cupy_setup_options)
def get_package_name():
return cupy_setup_options['package_name']
def get_long_description():
path = cupy_setup_options['long_description']
if path is None:
return None
with open(path) as f:
return f.read()
def prepare_wheel_libs():
"""Prepare shared libraries and include files for wheels.
Shared libraries are placed under `cupy/.data/lib` and
RUNPATH will be set to this directory later (Linux only).
Include files are placed under `cupy/.data/include`.
Returns the list of files (path relative to `cupy` module) to add to
the sdist/wheel distribution.
"""
data_dir = os.path.abspath(os.path.join('cupy', '.data'))
if os.path.exists(data_dir):
print('Removing directory: {}'.format(data_dir))
shutil.rmtree(data_dir)
# Generate list files to copy
# tuple of (src_path, dst_path)
files_to_copy = []
# Library files
for srcpath in cupy_setup_options['wheel_libs']:
relpath = os.path.basename(srcpath)
dstpath = os.path.join(data_dir, 'lib', relpath)
files_to_copy.append((srcpath, dstpath))
# Include files
for include_path_spec in cupy_setup_options['wheel_includes']:
srcpath, relpath = include_path_spec.rsplit(':', 1)
dstpath = os.path.join(data_dir, 'include', relpath)
files_to_copy.append((srcpath, dstpath))
# Wheel meta data
wheel_metadata = cupy_setup_options['wheel_metadata']
if wheel_metadata:
files_to_copy.append(
(wheel_metadata, os.path.join(data_dir, '_wheel.json')))
# Copy
for srcpath, dstpath in files_to_copy:
# Note: symlink is resolved by shutil.copy2.
print('Copying file for wheel: {}'.format(srcpath))
dirpath = os.path.dirname(dstpath)
if not os.path.isdir(dirpath):
os.makedirs(dirpath)
shutil.copy2(srcpath, dstpath)
return [os.path.relpath(x[1], 'cupy') for x in files_to_copy]
try:
import Cython
import Cython.Build
cython_version = pkg_resources.parse_version(Cython.__version__)
cython_available = (
cython_version >= required_cython_version and
cython_version not in ignore_cython_versions)
except ImportError:
cython_available = False
def cythonize(extensions, arg_options):
directive_keys = ('linetrace', 'profile')
directives = {key: arg_options[key] for key in directive_keys}
# Embed signatures for Sphinx documentation.
directives['embedsignature'] = True
cythonize_option_keys = ('annotate',)
cythonize_options = {key: arg_options[key]
for key in cythonize_option_keys}
# Compile-time constants to be used in Cython code
compile_time_env = cythonize_options.get('compile_time_env')
if compile_time_env is None:
compile_time_env = {}
cythonize_options['compile_time_env'] = compile_time_env
compile_time_env['use_hip'] = arg_options['use_hip']
if use_hip or arg_options['no_cuda']:
compile_time_env['CUDA_VERSION'] = 0
else:
compile_time_env['CUDA_VERSION'] = build.get_cuda_version()
return Cython.Build.cythonize(
extensions, verbose=True, language_level=3,
compiler_directives=directives, **cythonize_options)
def check_extensions(extensions):
for x in extensions:
for f in x.sources:
if not path.isfile(f):
raise RuntimeError('''\
Missing file: {}
Please install Cython {} or later. Please also check the version of Cython.
See https://docs.cupy.dev/en/stable/install.html for details.
'''.format(f, required_cython_version))
def get_ext_modules(use_cython=False):
arg_options = cupy_setup_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)
extensions = make_extensions(arg_options, compiler, use_cython)
return extensions
def _nvcc_gencode_options(cuda_version):
"""Returns NVCC GPU code generation options."""
if sys.argv == ['setup.py', 'develop']:
return []
envcfg = os.getenv('CUPY_NVCC_GENERATE_CODE', None)
if envcfg:
return ['--generate-code={}'.format(arch)
for arch in envcfg.split(';') if len(arch) > 0]
# The arch_list specifies virtual architectures, such as 'compute_61', and
# real architectures, such as 'sm_61', for which the CUDA input files are
# to be compiled.
#
# The syntax of an entry of the list is
#
# entry ::= virtual_arch | (virtual_arch, real_arch)
#
# where virtual_arch is a string which means a virtual architecture and
# real_arch is a string which means a real architecture.
#
# If a virtual architecture is supplied, NVCC generates a PTX code for the
# virtual architecture. If a pair of a virtual architecture and a real
# architecture is supplied, NVCC generates a PTX code for the virtual
# architecture as well as a cubin code for the real architecture.
#
# For example, making NVCC generate a PTX code for 'compute_60' virtual
# architecture, the arch_list has an entry of 'compute_60'.
#
# arch_list = ['compute_60']
#
# For another, making NVCC generate a PTX code for 'compute_61' virtual
# architecture and a cubin code for 'sm_61' real architecture, the
# arch_list has an entry of ('compute_61', 'sm_61').
#
# arch_list = [('compute_61', 'sm_61')]
if cuda_version >= 11000:
arch_list = ['compute_50',
('compute_60', 'sm_60'),
('compute_61', 'sm_61'),
('compute_70', 'sm_70'),
('compute_75', 'sm_75'),
('compute_80', 'sm_80'),
'compute_80']
elif cuda_version >= 10000:
arch_list = ['compute_30',
'compute_50',
('compute_60', 'sm_60'),
('compute_61', 'sm_61'),
('compute_70', 'sm_70'),
('compute_75', 'sm_75'),
'compute_70']
elif cuda_version >= 9000:
arch_list = ['compute_30',
'compute_50',
('compute_60', 'sm_60'),
('compute_61', 'sm_61'),
('compute_70', 'sm_70'),
'compute_70']
else:
# This should not happen.
assert False
options = []
for arch in arch_list:
if type(arch) is tuple:
virtual_arch, real_arch = arch
options.append('--generate-code=arch={},code={}'.format(
virtual_arch, real_arch))
else:
options.append('--generate-code=arch={},code={}'.format(
arch, arch))
return options
class _UnixCCompiler(unixccompiler.UnixCCompiler):
src_extensions = list(unixccompiler.UnixCCompiler.src_extensions)
src_extensions.append('.cu')
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
# For sources other than CUDA C ones, just call the super class method.
if os.path.splitext(src)[1] != '.cu':
return unixccompiler.UnixCCompiler._compile(
self, obj, src, ext, cc_args, extra_postargs, pp_opts)
if use_hip:
return self._comiple_unix_hipcc(
obj, src, ext, cc_args, extra_postargs, pp_opts)
# For CUDA C source files, compile them with NVCC.
_compiler_so = self.compiler_so
try:
nvcc_path = build.get_nvcc_path()
base_opts = build.get_compiler_base_options()
self.set_executable('compiler_so', nvcc_path)
cuda_version = build.get_cuda_version()
postargs = _nvcc_gencode_options(cuda_version) + [
'-O2', '--compiler-options="-fPIC"', '--std=c++11']
print('NVCC options:', postargs)
return unixccompiler.UnixCCompiler._compile(
self, obj, src, ext, base_opts + cc_args, postargs, pp_opts)
finally:
self.compiler_so = _compiler_so
def _comiple_unix_hipcc(self,
obj, src, ext, cc_args, extra_postargs, pp_opts):
# For CUDA C source files, compile them with HIPCC.
_compiler_so = self.compiler_so
try:
rcom_path = build.get_hipcc_path()
base_opts = build.get_compiler_base_options()
self.set_executable('compiler_so', rcom_path)
postargs = ['-O2', '-fPIC', '--include', 'hip_runtime.h']
print('HIPCC options:', postargs)
return unixccompiler.UnixCCompiler._compile(
self, obj, src, ext, base_opts + cc_args, postargs, pp_opts)
finally:
self.compiler_so = _compiler_so
def link(self, target_desc, objects, output_filename, *args):
use_hipcc = False
if use_hip:
for i in objects:
if 'cupy_thrust.o' in i:
use_hipcc = True
if use_hipcc:
_compiler_cxx = self.compiler_cxx
try:
rcom_path = build.get_hipcc_path()
self.set_executable('compiler_cxx', rcom_path)
return unixccompiler.UnixCCompiler.link(
self, target_desc, objects, output_filename, *args)
finally:
self.compiler_cxx = _compiler_cxx
else:
return unixccompiler.UnixCCompiler.link(
self, target_desc, objects, output_filename, *args)
class _MSVCCompiler(msvccompiler.MSVCCompiler):
_cu_extensions = ['.cu']
src_extensions = list(unixccompiler.UnixCCompiler.src_extensions)
src_extensions.extend(_cu_extensions)
def _compile_cu(self, sources, output_dir=None, macros=None,
include_dirs=None, debug=0, extra_preargs=None,
extra_postargs=None, depends=None):
# Compile CUDA C files, mainly derived from UnixCCompiler._compile().
macros, objects, extra_postargs, pp_opts, _build = \
self._setup_compile(output_dir, macros, include_dirs, sources,
depends, extra_postargs)
compiler_so = build.get_nvcc_path()
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
cuda_version = build.get_cuda_version()
postargs = _nvcc_gencode_options(cuda_version) + ['-O2']
postargs += ['-Xcompiler', '/MD']
print('NVCC options:', postargs)
for obj in objects:
try:
src, ext = _build[obj]
except KeyError:
continue
try:
self.spawn(compiler_so + cc_args + [src, '-o', obj] + postargs)
except errors.DistutilsExecError as e:
raise errors.CompileError(str(e))
return objects
def compile(self, sources, **kwargs):
# Split CUDA C sources and others.
cu_sources = []
other_sources = []
for source in sources:
if os.path.splitext(source)[1] == '.cu':
cu_sources.append(source)
else:
other_sources.append(source)
# Compile source files other than CUDA C ones.
other_objects = msvccompiler.MSVCCompiler.compile(
self, other_sources, **kwargs)
# Compile CUDA C sources.
cu_objects = self._compile_cu(cu_sources, **kwargs)
# Return compiled object filenames.
return other_objects + cu_objects
class sdist_with_cython(sdist.sdist):
"""Custom `sdist` command with cyhonizing."""
def __init__(self, *args, **kwargs):
if not cython_available:
raise RuntimeError('Cython is required to make sdist.')
ext_modules = get_ext_modules(True) # get .pyx modules
cythonize(ext_modules, cupy_setup_options)
sdist.sdist.__init__(self, *args, **kwargs)
class custom_build_ext(build_ext.build_ext):
"""Custom `build_ext` command to include CUDA C source files."""
def run(self):
if build.get_nvcc_path() is not None:
def wrap_new_compiler(func):
def _wrap_new_compiler(*args, **kwargs):
try:
return func(*args, **kwargs)
except errors.DistutilsPlatformError:
if not PLATFORM_WIN32: