CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/DOTA_devkit/poly_nms_gpu/setup.py
Views: 475
1
"""
2
setup.py file for SWIG example
3
"""
4
import os
5
from os.path import join as pjoin
6
from setuptools import setup
7
from distutils.extension import Extension
8
from Cython.Distutils import build_ext
9
import subprocess
10
import numpy as np
11
12
def find_in_path(name, path):
13
"Find a file in a search path"
14
# Adapted fom
15
# http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/
16
for dir in path.split(os.pathsep):
17
binpath = pjoin(dir, name)
18
if os.path.exists(binpath):
19
return os.path.abspath(binpath)
20
return None
21
22
23
def locate_cuda():
24
"""Locate the CUDA environment on the system
25
26
Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
27
and values giving the absolute path to each directory.
28
29
Starts by looking for the CUDAHOME env variable. If not found, everything
30
is based on finding 'nvcc' in the PATH.
31
"""
32
33
# first check if the CUDAHOME env variable is in use
34
if 'CUDAHOME' in os.environ:
35
home = os.environ['CUDAHOME']
36
nvcc = pjoin(home, 'bin', 'nvcc')
37
else:
38
# otherwise, search the PATH for NVCC
39
default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')
40
nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)
41
if nvcc is None:
42
raise EnvironmentError('The nvcc binary could not be '
43
'located in your $PATH. Either add it to your path, or set $CUDAHOME')
44
home = os.path.dirname(os.path.dirname(nvcc))
45
46
cudaconfig = {'home':home, 'nvcc':nvcc,
47
'include': pjoin(home, 'include'),
48
'lib64': pjoin(home, 'lib64')}
49
try:
50
for k, v in cudaconfig.iteritems():
51
if not os.path.exists(v):
52
raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
53
except:
54
for k, v in cudaconfig.items():
55
if not os.path.exists(v):
56
raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
57
return cudaconfig
58
CUDA = locate_cuda()
59
60
61
# Obtain the numpy include directory. This logic works across numpy versions.
62
try:
63
numpy_include = np.get_include()
64
except AttributeError:
65
numpy_include = np.get_numpy_include()
66
67
def customize_compiler_for_nvcc(self):
68
"""inject deep into distutils to customize how the dispatch
69
to gcc/nvcc works.
70
71
If you subclass UnixCCompiler, it's not trivial to get your subclass
72
injected in, and still have the right customizations (i.e.
73
distutils.sysconfig.customize_compiler) run on it. So instead of going
74
the OO route, I have this. Note, it's kindof like a wierd functional
75
subclassing going on."""
76
77
# tell the compiler it can processes .cu
78
self.src_extensions.append('.cu')
79
80
# save references to the default compiler_so and _comple methods
81
default_compiler_so = self.compiler_so
82
super = self._compile
83
84
# now redefine the _compile method. This gets executed for each
85
# object but distutils doesn't have the ability to change compilers
86
# based on source extension: we add it.
87
def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
88
if os.path.splitext(src)[1] == '.cu':
89
# use the cuda for .cu files
90
self.set_executable('compiler_so', CUDA['nvcc'])
91
# use only a subset of the extra_postargs, which are 1-1 translated
92
# from the extra_compile_args in the Extension class
93
postargs = extra_postargs['nvcc']
94
else:
95
postargs = extra_postargs['gcc']
96
97
super(obj, src, ext, cc_args, postargs, pp_opts)
98
# reset the default compiler_so, which we might have changed for cuda
99
self.compiler_so = default_compiler_so
100
101
# inject our redefined _compile method into the class
102
self._compile = _compile
103
104
105
# run the customize_compiler
106
class custom_build_ext(build_ext):
107
def build_extensions(self):
108
customize_compiler_for_nvcc(self.compiler)
109
build_ext.build_extensions(self)
110
111
ext_modules = [
112
Extension('poly_nms',
113
['poly_nms_kernel.cu', 'poly_nms.pyx'],
114
library_dirs=[CUDA['lib64']],
115
libraries=['cudart'],
116
language='c++',
117
runtime_library_dirs=[CUDA['lib64']],
118
# this syntax is specific to this build system
119
# we're only going to use certain compiler args with nvcc and not with
120
# gcc the implementation of this trick is in customize_compiler() below
121
extra_compile_args={'gcc': ["-Wno-unused-function"],
122
'nvcc': ['-arch=sm_35',
123
'--ptxas-options=-v',
124
'-c',
125
'--compiler-options',
126
"'-fPIC'"]},
127
include_dirs=[numpy_include, CUDA['include']]
128
),
129
Extension('poly_overlaps',
130
['poly_overlaps_kernel.cu', 'poly_overlaps.pyx'],
131
library_dirs=[CUDA['lib64']],
132
libraries=['cudart'],
133
language='c++',
134
runtime_library_dirs=[CUDA['lib64']],
135
# this syntax is specific to this build system
136
# we're only going to use certain compiler args with nvcc and not with
137
# gcc the implementation of this trick is in customize_compiler() below
138
extra_compile_args={'gcc': ["-Wno-unused-function"],
139
'nvcc': ['-arch=sm_35',
140
'--ptxas-options=-v',
141
'-c',
142
'--compiler-options',
143
"'-fPIC'"]},
144
include_dirs=[numpy_include, CUDA['include']]
145
),
146
]
147
setup(
148
name='rotation',
149
ext_modules=ext_modules,
150
# inject our custom trigger
151
cmdclass={'build_ext': custom_build_ext},
152
)
153
154