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
|
import os from os.path import join as pjoin import numpy as np from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext
def find_in_path(name, path): "Find a file in a search path" for dir in path.split(os.pathsep): binpath = pjoin(dir, name) if os.path.exists(binpath): return os.path.abspath(binpath) return None
def locate_cuda(): """Locate the CUDA environment on the system Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64' and values giving the absolute path to each directory. Starts by looking for the CUDAHOME env variable. If not found, everything is based on finding 'nvcc' in the PATH. """
if 'CUDAHOME' in os.environ: home = os.environ['CUDAHOME'] nvcc = pjoin(home, 'bin', 'nvcc.exe') else: nvcc = find_in_path('nvcc.exe', os.environ['PATH']) if nvcc is None: raise EnvironmentError('The nvcc binary could not be ' 'located in your $PATH. Either add it to your path, or set $CUDAHOME') home = os.path.dirname(os.path.dirname(nvcc))
cudaconfig = {'home':home, 'nvcc':nvcc, 'include': pjoin(home, 'include'), 'lib64': pjoin(home, 'lib', 'x64')} for k, v in iter(cudaconfig.items()): if not os.path.exists(v): raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
return cudaconfig CUDA = locate_cuda()
try: numpy_include = np.get_include() except AttributeError: numpy_include = np.get_numpy_include()
def customize_compiler_for_nvcc(self): import os import shutil import stat import subprocess import winreg
from distutils.errors import DistutilsExecError, DistutilsPlatformError, \ CompileError, LibError, LinkError from distutils.ccompiler import CCompiler, gen_lib_options from distutils import log from distutils.util import get_platform
from itertools import count
super = self.compile self.src_extensions.append('.cu') import sys py_dir = sys.executable.replace('\\', '/').split('/')[:-1] py_include = pjoin('/'.join(py_dir), 'include')
def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
if not self.initialized: self.initialize() compile_info = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs) macros, objects, extra_postargs, pp_opts, build = compile_info
compile_opts = extra_preargs or [] compile_opts.append('/c') if debug: compile_opts.extend(self.compile_options_debug) else: compile_opts.extend(self.compile_options)
add_cpp_opts = False
for obj in objects: try: src, ext = build[obj] except KeyError: continue if debug: src = os.path.abspath(src)
if ext in self._c_extensions: input_opt = "/Tc" + src elif ext in self._cpp_extensions: input_opt = "/Tp" + src add_cpp_opts = True elif ext in self._rc_extensions: input_opt = src output_opt = "/fo" + obj try: self.spawn([self.rc] + pp_opts + [output_opt, input_opt]) except DistutilsExecError as msg: raise CompileError(msg) continue elif ext in self._mc_extensions: h_dir = os.path.dirname(src) rc_dir = os.path.dirname(obj) try: self.spawn([self.mc, '-h', h_dir, '-r', rc_dir, src]) base, _ = os.path.splitext(os.path.basename(src)) rc_file = os.path.join(rc_dir, base + '.rc') self.spawn([self.rc, "/fo" + obj, rc_file])
except DistutilsExecError as msg: raise CompileError(msg) continue elif ext == '.cu': try: postargs = extra_postargs['nvcc'] arg = [CUDA['nvcc']] + sources + ['-odir', pjoin(output_dir, 'nms')] for include_dir in include_dirs: arg.append('-I') arg.append(include_dir) arg += ['-I', py_include] arg += ['-Xcompiler', '/EHsc,/W3,/nologo,/Ox,/MD'] arg += postargs self.spawn(arg) continue except DistutilsExecError as msg: continue else: raise CompileError("Don't know how to compile {} to {}" .format(src, obj))
args = [self.cc] + compile_opts + pp_opts if add_cpp_opts: args.append('/EHsc') args.append(input_opt) args.append("/Fo" + obj) args.extend(extra_postargs)
try: self.spawn(args) except DistutilsExecError as msg: raise CompileError(msg)
return objects
self.compile = compile
class custom_build_ext(build_ext): def build_extensions(self): customize_compiler_for_nvcc(self.compiler) build_ext.build_extensions(self)
ext_modules = [ Extension( "utils.cython_bbox", ["utils/bbox.pyx"], extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, include_dirs = [numpy_include] ), Extension( "utils.cython_nms", ["utils/nms.pyx"], extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, include_dirs = [numpy_include] ), Extension( "nms.cpu_nms", ["nms/cpu_nms.pyx"], extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, include_dirs = [numpy_include] ), Extension('nms.gpu_nms', ['nms/nms_kernel.cu', 'nms/gpu_nms.cpp'], library_dirs=[CUDA['lib64']], libraries=['cudart'], language='c++', extra_compile_args={'gcc': ["-Wno-unused-function"], 'nvcc': ['-arch=sm_35', '--ptxas-options=-v', '-c', '--compiler-options', '-fPIC']}, include_dirs = [numpy_include, CUDA['include']] ) ]
setup( name='tf_faster_rcnn', ext_modules=ext_modules, cmdclass={'build_ext': custom_build_ext}, )
|