def test_cpu(self): create_extension( name='test_extensions.cpulib', headers=[test_dir + '/ffi/src/cpu/lib.h'], sources=[ test_dir + '/ffi/src/cpu/lib1.c', test_dir + '/ffi/src/cpu/lib2.c', ], verbose=False, ).build() from test_extensions import cpulib tensor = torch.ones(2, 2).float() cpulib.good_func(tensor, 2, 1.5) self.assertEqual(tensor, torch.ones(2, 2) * 2 + 1.5) new_tensor = cpulib.new_tensor(4) self.assertEqual(new_tensor, torch.ones(4, 4) * 4) f = cpulib.int_to_float(5) self.assertIs(type(f), float) self.assertRaises(TypeError, lambda: cpulib.good_func(tensor.double(), 2, 1.5)) self.assertRaises(torch.FatalError, lambda: cpulib.bad_func(tensor, 2, 1.5))
def build_torch_extension(build_ext, options, abi_compile_flags): check_torch_import() have_cuda = is_torch_cuda() if not have_cuda and check_macro(options['MACROS'], 'HAVE_CUDA'): raise DistutilsPlatformError( 'Horovod build with GPU support was requested, but this PyTorch ' 'installation does not support CUDA.') # Update HAVE_CUDA to mean that PyTorch supports CUDA. Internally, we will be checking # HOROVOD_GPU_(ALLREDUCE|ALLGATHER|BROADCAST) to decide whether we should use GPU # version or transfer tensors to CPU memory for those operations. updated_macros = set_macro( options['MACROS'], 'HAVE_CUDA', str(int(have_cuda))) # Create_extension overwrites these files which are customized, we need to protect them. with protect_files('horovod/torch/mpi_lib/__init__.py', 'horovod/torch/mpi_lib_impl/__init__.py'): from torch.utils.ffi import create_extension ffi_iface = create_extension( name='horovod.torch.mpi_lib', headers=['horovod/torch/interface.h'] + (['horovod/torch/interface_cuda.h'] if have_cuda else []), with_cuda=have_cuda, language='c', package=True, sources=[], extra_compile_args=['-std=c11', '-fPIC', '-O2'] ) ffi_impl = create_extension( name='horovod.torch.mpi_lib_impl', headers=[], with_cuda=have_cuda, language='c++', package=True, source_extension='.cc', define_macros=updated_macros, include_dirs=options['INCLUDES'], sources=options['SOURCES'] + ['horovod/torch/mpi_ops.cc', 'horovod/torch/handle_manager.cc', 'horovod/torch/ready_event.cc', 'horovod/torch/tensor_util.cc', 'horovod/torch/cuda_util.cc', 'horovod/torch/adapter.cc'], extra_compile_args=options['COMPILE_FLAGS'] + abi_compile_flags, extra_link_args=options['LINK_FLAGS'], library_dirs=options['LIBRARY_DIRS'], libraries=options['LIBRARIES'] ) for ffi, setuptools_ext in [(ffi_iface, torch_mpi_lib), (ffi_impl, torch_mpi_lib_impl)]: ffi_ext = ffi.distutils_extension() # ffi_ext is distutils Extension, not setuptools Extension for k, v in ffi_ext.__dict__.items(): setuptools_ext.__dict__[k] = v build_ext.build_extension(setuptools_ext)
def is_torch_cuda(): try: from torch.utils.ffi import create_extension cuda_test_ext = create_extension( name='horovod.torch.test_cuda', headers=['horovod/torch/dummy.h'], sources=[], with_cuda=True, extra_compile_args=['-std=c11', '-fPIC', '-O2'] ) cuda_test_ext.build() return True except: print('INFO: Above error indicates that this PyTorch installation does not support CUDA.') return False
def test_gpu(self): create_extension( name='gpulib', headers=[test_dir + '/ffi/src/cuda/cudalib.h'], sources=[ test_dir + '/ffi/src/cuda/cudalib.c', ], with_cuda=True, verbose=False, ).build() import gpulib tensor = torch.ones(2, 2).float() gpulib.good_func(tensor, 2, 1.5) self.assertEqual(tensor, torch.ones(2, 2) * 2 + 1.5) ctensor = tensor.cuda().fill_(1) gpulib.cuda_func(ctensor, 2, 1.5) self.assertEqual(ctensor, torch.ones(2, 2) * 2 + 1.5) self.assertRaises(TypeError, lambda: gpulib.cuda_func(tensor, 2, 1.5)) self.assertRaises(TypeError, lambda: gpulib.cuda_func(ctensor.storage(), 2, 1.5))
import os from torch.utils.ffi import create_extension sources = ['src/lib_cffi.cpp'] headers = ['src/lib_cffi.h'] extra_objects = ['src/bn.o'] with_cuda = True this_file = os.path.dirname(os.path.realpath(__file__)) extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension( '_ext', headers=headers, sources=sources, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=["-std=c++11"] ) if __name__ == '__main__': ffi.build()
import glob import torch from os import path as osp from torch.utils.ffi import create_extension abs_path = osp.dirname(osp.realpath(__file__)) sources = [] headers = [] sources += ['src/binop_cpu.c'] sources += ['src/binop_cpu_kernel.c'] headers += ['include/binop_cpu.h'] headers += ['include/binop_cpu_kernel.h'] ffi = create_extension( 'binop_cpu', headers=headers, sources=sources, relative_to=__file__, include_dirs=[osp.join(abs_path, 'include')], extra_compile_args=["-std=c99", "-Ofast", "-fopenmp", "-mtune=native", "-march=x86-64"] ) if __name__ == '__main__': ffi.build()
reqs = f.read() bleu = Extension( 'fairseq.libbleu', sources=[ 'fairseq/clib/libbleu/libbleu.cpp', 'fairseq/clib/libbleu/module.cpp', ], extra_compile_args=['-std=c++11'], ) conv_tbc = create_extension( 'fairseq.temporal_convolution_tbc', relative_to='fairseq', headers=['fairseq/clib/temporal_convolution_tbc/temporal_convolution_tbc.h'], sources=['fairseq/clib/temporal_convolution_tbc/temporal_convolution_tbc.cpp'], define_macros=[('WITH_CUDA', None)], with_cuda=True, extra_compile_args=['-std=c++11'], source_extension='.cpp', ) class build_py_hook(build_py): def run(self): conv_tbc.build() build_py.run(self) setup( name='fairseq', version='0.2.0',
with_cuda = False extra_objects = [] if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/crop_and_resize_gpu.c'] headers += ['src/crop_and_resize_gpu.h'] defines += [('WITH_CUDA', None)] extra_objects += ['src/crop_and_resize_kernel.cu.o'] with_cuda = True extra_compile_args = ['-fopenmp', '-std=c99'] this_file = os.path.dirname(os.path.realpath(__file__)) print(this_file) sources = [os.path.join(this_file, fname) for fname in sources] headers = [os.path.join(this_file, fname) for fname in headers] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension('extension', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=extra_compile_args) if __name__ == '__main__': ffi.build()
this_file = os.path.abspath(__file__) warp_root = os.path.dirname(os.path.dirname(this_file)) sources = ['src/warp_ctc.c'] headers = ['src/warp_ctc.h'] include_dirs = [os.path.join(warp_root, 'include')] libraries = ['warpctc'] library_dirs = [os.path.join(warp_root, 'build')] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/warp_ctc_cuda.c'] headers += ['src/warp_ctc_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension('_ext.ctc', headers=headers, sources=sources, include_dirs=include_dirs, relative_to=__file__, define_macros=defines, with_cuda=with_cuda, libraries=libraries, library_dirs=library_dirs) if __name__ == '__main__': ffi.build()
headers = [] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/roi_align_cuda.c'] headers += ['src/roi_align_cuda.h'] # headers += ['src/cuda/roi_align_kernel.h'] defines += [('WITH_CUDA', None)] with_cuda = True else: raise ValueError("only cuda version support") extra_objects = ['src/cuda/roi_align_kernel.cu.o'] # absolute path extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension( '_ext.roi_align', # _ext/roi_align headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects) if __name__ == '__main__': ffi.build()
import torch from torch.utils.ffi import create_extension sources = [] headers = [] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/rpsroi_pooling_cuda.c'] headers += ['src/rpsroi_pooling_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True this_file = os.path.dirname(os.path.realpath(__file__)) print(this_file) extra_objects = ['src/cuda/rpsroi_pooling.cu.o'] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension('_ext.rpsroi_pooling', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects) if __name__ == '__main__': ffi.build()
import glob import torch from os import path as osp from torch.utils.ffi import create_extension abs_path = osp.dirname(osp.realpath(__file__)) extra_objects = [osp.join(abs_path, 'build/libsift.so')] extra_objects += glob.glob('/usr/local/cuda-9.0/lib64/*.a') ffi = create_extension( 'libsift', headers=['include/PointSift.h'], sources=['src/PointSift.c'], define_macros=[('WITH_CUDA', None)], relative_to=__file__, with_cuda=True, extra_objects=extra_objects, include_dirs=[osp.join(abs_path, 'include'),"/opt/cuda/include"] ) if __name__ == '__main__': print("COMPILING C ") assert torch.cuda.is_available(), 'Please install CUDA for GPU support.' ffi.build()
import torch from torch.utils.ffi import create_extension this_file = os.path.dirname(__file__) sources = [] headers = [] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['pytorch_fft/src/th_fft_cuda.c'] headers += ['pytorch_fft/src/th_fft_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension('pytorch_fft._ext.th_fft', package=False, headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, include_dirs=[os.getcwd() + '/pytorch_fft/src'], library_dirs=['/usr/local/cuda/lib64'], libraries=['cufft']) if __name__ == '__main__': ffi.build()
cu_sources = glob.glob("src/**/*_cuda.c") cu_headers = glob.glob("src/**/*_cuda.h") sources = list(set(all_sources) - set(cu_sources)) headers = list(set(all_headers) - set(cu_headers)) defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') # sources += ['src/new_lib_cuda.c'] sources += cu_sources # headers += ['src/new_lib_cuda.h'] headers += cu_headers defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension('_ext.new_lib', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_compile_args=["-std=c99"]) if __name__ == '__main__': ffi.build()
import torch from torch.utils.ffi import create_extension this_file = os.path.dirname(__file__) sources = [] headers = [] defines = [] with_cuda = False abs_path = os.path.dirname(os.path.realpath(__file__)) extra_objects = [os.path.join(abs_path, 'internel/batchnormp_kernel.so')] if torch.cuda.is_available(): print('Including CUDA code.') sources += ['batchnorm_binding.c'] headers += ['batchnorm_binding.h'] defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension('batchnorm_utils.pytorch', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects) if __name__ == '__main__': ffi.build()
project_dir = pathlib.Path(__file__).resolve().parent finished_file = project_dir / 'finished_compiling.txt' src = project_dir / 'retinanet/lib/nms/src' if not finished_file.exists(): with Sultan.load(cwd=project_dir / 'retinanet/lib/nms/src/cuda') as s: cmd = ('/usr/local/cuda/bin/nvcc -c -o ' 'nms_kernel.cu.o nms_kernel.cu -x ' 'cu -Xcompiler -fPIC ' + CUDA_ARCH) s.bash(f'-c "{cmd}"').run() sources = [src / 'nms.c', src / 'nms_cuda.c'] headers = [src / 'nms.h', src / 'nms_cuda.h'] defines = [('WITH_CUDA', None)] with_cuda = True extra_objects = [src / 'cuda/nms_kernel.cu.o'] ffi = create_extension('retinanet.lib.nms._ext.nms', headers=[str(x) for x in headers], sources=[str(x) for x in sources], define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=[str(x) for x in extra_objects], extra_compile_args=['-std=c99']) ffi.build() finished_file.touch() setup(name='retinanet', packages=find_packages())
import os.path as path import torch from torch.utils.ffi import create_extension sources = ['lib/deform_conv3d_cuda.cpp'] headers = ['lib/deform_conv3d_cuda.h'] defines = [('WITH_CUDA', None)] with_cuda = True this_file = path.dirname(path.realpath(__file__)) print(this_file) extra_objects = ['lib/deform_conv3d_cuda_kernel.cu.o'] extra_objects = [path.join(this_file, fname) for fname in extra_objects] ffi = create_extension('deform_conv3dl_op', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects) if __name__ == '__main__': assert torch.cuda.is_available(), 'Please install CUDA for GPU support.' ffi.build()
headers += ['src/functions_cuda.h'] defines.append(('WITH_CUDA', None)) libraries += ["cudart", "cudadevrt"] extra_objects = ['src/functions.link.cu.o', 'src/internals.cu.o', 'src/functions_cuda_kernel.cu.o'] library_dirs = [args.cuda_path, os.path.join(this_file, 'src/')] include_dirs = ['/usr/local/cuda-9.0/include/'] else: library_dirs = [os.path.join(this_file, 'src/')] include_dirs = [] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] print(torch.cuda.is_available() and args.cuda) ffi = create_extension( '_ext.functions', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=(torch.cuda.is_available() and args.cuda), library_dirs=library_dirs, include_dirs=include_dirs, libraries=libraries, extra_compile_args=["-std=gnu11"], extra_objects=extra_objects ) if __name__ == '__main__': ffi.build()
# pylint: disable=invalid-name import os import torch from torch.utils.ffi import create_extension if not torch.cuda.is_available(): raise Exception('HighwayLSTM can only be compiled with CUDA') sources = ['src/highway_lstm_cuda.c'] headers = ['src/highway_lstm_cuda.h'] defines = [('WITH_CUDA', None)] with_cuda = True this_file = os.path.dirname(os.path.realpath(__file__)) extra_objects = ['src/highway_lstm_kernel.cu.o'] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension( '_ext.highway_lstm_layer', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects ) if __name__ == '__main__': ffi.build()
def build_modules(): parser = argparse.ArgumentParser( description="Install script for torchsyncbn package") parser.add_argument("--cuda-path", default=Path("/usr/local/cuda"), type=Path, metavar="PATH", help="CUDA install path") parser.add_argument("-v", "--verbose", action="store_true", help="verbose output") args = parser.parse_args() script_dir = Path(__file__).absolute().parent # CUDA building cuda_path = args.cuda_path cuda_include_dir = cuda_path / "include" nvcc = args.cuda_path / "bin" / "nvcc" nvcc_opt = "-std=c++11 -x cu --expt-extended-lambda -O3 -Xcompiler -fPIC".split( ) gen_code = "-gencode arch=compute_61,code=sm_61 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52".split( ) extension_dir = script_dir / "torchsyncbn" / "functional" / "_syncbn" / "src" # check if NVCC exists try: subprocess.call([nvcc, "-V"]) except FileNotFoundError: raise RuntimeError("NVCC is not found: {}".format(nvcc)) # build CUDA kernel print("=> building CUDA kernel") if args.verbose: result = subprocess.call( [nvcc, "-v", "-c", "-o", "syncbn.cu.o", "syncbn.cu"] + ["-I", cuda_include_dir] + nvcc_opt + gen_code, cwd=extension_dir) else: result = subprocess.call( [nvcc, "-c", "-o", "syncbn.cu.o", "syncbn.cu"] + ["-I", cuda_include_dir] + nvcc_opt + gen_code, cwd=extension_dir) if result != 0: raise RuntimeError("building syncbn.cu has been failed") # create PyTorch extension print("=> creating PyTorch extension") sources = [str(extension_dir / "syncbn.cpp")] headers = [str(extension_dir / "syncbn.h")] extra_objects = [str(extension_dir / "syncbn.cu.o")] os.environ["C_INCLUDE_PATH"] = str(cuda_include_dir) os.environ["CPLUS_INCLUDE_PATH"] = str(cuda_include_dir) ffi = create_extension("_ext.syncbn", headers=headers, sources=sources, relative_to=extension_dir, with_cuda=True, extra_objects=extra_objects, extra_compile_args=["-std=c++11"], verbose=args.verbose) ffi.build() print("=> Please set PYTHONPATH as follows:") print("") print("export PYTHONPATH=\"{}:$PYTHONPATH\"".format(script_dir)) print("")
extra_objects += ['src/cuda/crop_and_resize_kernel.cu.o'] with_cuda = True extra_compile_args = ['-fopenmp', ] this_file = os.path.dirname(os.path.realpath(__file__)) print(this_file) sources = [os.path.join(this_file, fname) for fname in sources] headers = [os.path.join(this_file, fname) for fname in headers] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] library_dirs=[r'D:\pylibs\pytorch-scripts\pytorch\torch\lib','D:\dnnLibs\pytorch-mask-rcnn\nms\src\cuda',] libraries=['_C','caffe2', 'caffe2_gpu','cudart'] ffi = create_extension( '_ext.crop_and_resize', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=extra_compile_args, libraries = libraries, ) if __name__ == '__main__': ffi.build()
reqs = f.read() bleu = Extension( 'fairseq.libbleu', sources=[ 'fairseq/clib/libbleu/libbleu.cpp', 'fairseq/clib/libbleu/module.cpp', ], extra_compile_args=['-std=c++11'], ) conv_tbc = create_extension( 'fairseq.temporal_convolution_tbc', relative_to='fairseq', headers=['fairseq/clib/temporal_convolution_tbc/temporal_convolution_tbc.h'], sources=['fairseq/clib/temporal_convolution_tbc/temporal_convolution_tbc.cpp'], define_macros=[('WITH_CUDA', None)], with_cuda=True, extra_compile_args=['-std=c++11'], source_extension='.cpp', ) class build_py_hook(build_py): def run(self): conv_tbc.build() build_py.run(self) setup( name='fairseq', version='0.3.1',
if "WARP_CTC_PATH" in os.environ: warp_ctc_path = os.environ["WARP_CTC_PATH"] if not os.path.exists(os.path.join(warp_ctc_path, "libwarpctc" + lib_ext)): print(("Could not find libwarpctc.so in {}.\n" "Build warp-ctc and set WARP_CTC_PATH to the location of" " libwarpctc.so (default is '../build')").format(warp_ctc_path)) sys.exit(1) include_dirs = [os.path.realpath('../include')] ffi = create_extension( name='warpctc_pytorch._warp_ctc', package=True, language='c++', headers=headers, sources=['src/binding.cpp'], with_cuda=enable_gpu, include_dirs=include_dirs, library_dirs=[os.path.realpath(warp_ctc_path)], libraries=['warpctc'], extra_link_args=['-Wl,-rpath,' + os.path.realpath(warp_ctc_path)], extra_compile_args=extra_compile_args) ffi = ffi.distutils_extension() setup( name="warpctc_pytorch", version="0.1", description="PyTorch wrapper for warp-ctc", url="https://github.com/baidu-research/warp-ctc", author="Jared Casper, Sean Naren", author_email= "[email protected], [email protected]", license="Apache",
import os from torch.utils.ffi import create_extension sources = ['src/lib_cffi.cpp'] headers = ['src/lib_cffi.h'] extra_objects = ['src/ca.o'] with_cuda = True this_file = os.path.dirname(os.path.realpath(__file__)) extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension('_ext', headers=headers, sources=sources, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=["-std=c++11"]) if __name__ == '__main__': ffi.build()
#!/usr/bin/env python3 from torch.utils.ffi import create_extension ffi = create_extension('torchwordemb._torchwordemb', headers='torchwordemb/src/loadwordemb.h', sources=['torchwordemb/src/loadwordemb.c'], with_cuda=False, package=True, relative_to=__file__, extra_compile_args=["--std=c99", "-Wall"]) if __name__ == '__main__': ffi.build()
import glob import torch from os import path as osp from torch.utils.ffi import create_extension abs_path = osp.dirname(osp.realpath(__file__)) extra_objects = [osp.join(abs_path, 'build/gabor_layer_cuda_kernel.so')] extra_objects += glob.glob('/usr/local/cuda/lib64/*.a') ffi = create_extension('gabor_layer', headers=['include/gabor_layer_cuda.h'], sources=['src/gabor_layer_cuda.c'], define_macros=[('WITH_CUDA', None)], relative_to=__file__, with_cuda=True, extra_objects=extra_objects, include_dirs=[osp.join(abs_path, 'include')]) if __name__ == '__main__': assert torch.cuda.is_available(), 'Please install CUDA for GPU support.' ffi.build()
import glob import torch from os import path as osp from torch.utils.ffi import create_extension abs_path = osp.dirname(osp.realpath(__file__)) extra_objects = [osp.join(abs_path, 'build/scatter_cuda_kernel.so')] extra_objects += glob.glob('/usr/local/cuda/lib64/*.a') ffi = create_extension('scatter_utils', headers=['include/scatter_utils.h'], sources=['scatter_utils.c'], verbose=True, define_macros=[('WITH_CUDA', None)], relative_to=__file__, with_cuda=True, extra_objects=extra_objects, include_dirs=[osp.join(abs_path, 'include')]) if __name__ == '__main__': assert torch.cuda.is_available(), 'Please install CUDA for GPU support.' ffi.build()
extra_objects = [] if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/crop_and_resize_gpu.c'] headers += ['src/crop_and_resize_gpu.h'] defines += [('WITH_CUDA', None)] extra_objects += ['src/cuda/crop_and_resize_kernel.cu.o'] with_cuda = True extra_compile_args = ['-fopenmp', '-std=c99'] this_file = os.path.dirname(os.path.realpath(__file__)) print(this_file) sources = [os.path.join(this_file, fname) for fname in sources] headers = [os.path.join(this_file, fname) for fname in headers] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension( '_ext.crop_and_resize', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=extra_compile_args ) if __name__ == '__main__': ffi.build()
abs_path = osp.dirname(osp.realpath(__file__)) sources = ['src/andor.c'] headers = ['include/andor.h'] defines = [] with_cuda = False extra_objects = [osp.join(abs_path, 'build/andor_cuda_kernel.so')] extra_objects += glob.glob('/usr/local/cuda/lib64/*.a') if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/andor_cuda.c'] headers += ['include/andor_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension( '_ext.andor', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, # extra_compile_args=["-std=c99"], extra_objects=extra_objects, include_dirs=[osp.join(abs_path, 'include')]) if __name__ == '__main__': ffi.build()
else: lib_ext = ".so" if "WARP_CTC_PATH" in os.environ: warp_ctc_path = os.environ["WARP_CTC_PATH"] if not os.path.exists(os.path.join(warp_ctc_path, "libwarpctc" + lib_ext)): print(("Could not find libwarpctc.so in {}.\n" "Build warp-ctc and set WARP_CTC_PATH to the location of" " libwarpctc.so (default is '../build')").format(warp_ctc_path)) sys.exit(1) include_dirs = [os.path.realpath('../include')] ffi = create_extension(name='warp_ctc', language='c++', headers=['src/binding.h'], sources=['src/binding.cpp'], with_cuda=True, include_dirs=include_dirs, library_dirs=[os.path.realpath(warp_ctc_path)], runtime_library_dirs=[os.path.realpath(warp_ctc_path)], libraries=['warpctc'], extra_compile_args=extra_compile_args) ffi = ffi.distutils_extension() ffi.name = 'warpctc_pytorch._warp_ctc' setup( name="warpctc_pytorch", version="0.1", packages=["warpctc_pytorch"], ext_modules=[ffi], )
if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/roi_crop_cuda.cpp'] headers += ['src/roi_crop_cuda.h'] include_dirs += [os.path.join(cuda_root,"include"), os.path.join(torch_root,'include')] defines += [('WITH_CUDA', None)] with_cuda = True extra_objects = ['src/roi_crop_cuda_kernel.lib'] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] extra_objects += [os.path.join(torch_root,'ATen.lib'), os.path.join(cuda_root,'lib/x64/cudart.lib'), os.path.join(torch_root,'_C.lib')] print(extra_objects) ffi = create_extension( '_ext.roi_crop', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, include_dirs=include_dirs ) if __name__ == '__main__': ffi.build()
import os.path as path import torch from torch.utils.ffi import create_extension sources = ['lib/conv2d_cuda.cpp'] headers = ['lib/conv2d_cuda.h'] # defines = [('WITH_CUDA', None), ('TH_DOUBLE', None)] defines = [('WITH_CUDA', None)] this_file = path.dirname(path.realpath(__file__)) extra_objects = ['lib/conv2d_cuda_kernel.cu.o'] extra_objects = [path.join(this_file, fname) for fname in extra_objects] print(extra_objects) ffi = create_extension('conv2d_op', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=True, extra_objects=extra_objects) if __name__ == '__main__': assert torch.cuda.is_available(), 'Please install CUDA for GPU support.' ffi.build()
import os from torch.utils.ffi import create_extension ffi = create_extension(name='_ext.nms', headers=['src/nms.h'], sources=['src/nms.c'], extra_objects=[ os.path.join( os.path.dirname(os.path.abspath(__file__)), it) for it in ['src/nms_cuda.o'] ], relative_to=__file__, with_cuda=True) if __name__ == '__main__': ffi.build()
import os import torch from torch.utils.ffi import create_extension this_file = os.path.dirname(__file__) sources = ['my_package/src/my_lib.c'] headers = ['my_package/src/my_lib.h'] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['my_package/src/my_lib_cuda.c'] headers += ['my_package/src/my_lib_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension('my_package._ext.my_lib', package=True, headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda) if __name__ == '__main__': ffi.build()
+ '-gencode arch=compute_52,code=sm_52 ' + '-gencode arch=compute_50,code=sm_50 ' + '-gencode arch=compute_30,code=sm_30 ' + '-DNVCC ' + '-I/usr/local/cuda/include ' + '-I' + torch_dir + '/lib/include ' + '-I' + torch_dir + '/lib/include/TH ' + '-I' + torch_dir + '/lib/include/THC ' + '-I.') assert r == 0 ffi = create_extension( 'sparseconvnet.SCN', headers=[ 'sparseconvnet/SCN/header_cpu.h', 'sparseconvnet/SCN/header_gpu.h'], sources=[], extra_objects=[ this_dir + '/sparseconvnet/SCN/init.cu.o'], relative_to=__file__, with_cuda=True) else: r = os.system( 'cd sparseconvnet/SCN; g++ -std=c++11 -fPIC -c init.cpp -o init.cpp.o -I' + torch_dir + '/lib/include -I' + torch_dir + '/lib/include/TH -I.') assert r == 0 ffi = create_extension( 'sparseconvnet.SCN',
import os from torch.utils.ffi import create_extension sources = ['src/lib_mpl.cpp'] headers = ['src/lib_mpl.h'] with_cuda = False this_file = os.path.dirname(os.path.realpath(__file__)) ffi = create_extension('_mpl', headers=headers, sources=sources, relative_to=__file__, with_cuda=with_cuda) if __name__ == '__main__': ffi.build()
from torch.utils.ffi import create_extension ffi_plan_cufft = create_extension('s2cnn.ops.gpu.lib_cufft', headers=['s2cnn/ops/gpu/plan_cufft.h'], package=True, sources=['s2cnn/ops/gpu/plan_cufft.c'], define_macros=[('WITH_CUDA', None)], relative_to=__file__, libraries=['cufft'], with_cuda=True) if __name__ == '__main__': print("build CUDA dependencies") ffi_plan_cufft.build()
ext_libs.append('bz2') if compile_test('lzma.h', 'lzma'): compile_args.append('-DHAVE_XZLIB') ext_libs.append('lzma') third_party_libs = ["kenlm", "openfst-1.6.7/src/include", "ThreadPool", "boost_1_63_0", "utf8"] compile_args.extend(['-DINCLUDE_KENLM', '-DKENLM_MAX_ORDER=6']) lib_sources = glob.glob('third_party/kenlm/util/*.cc') + glob.glob('third_party/kenlm/lm/*.cc') + glob.glob( 'third_party/kenlm/util/double-conversion/*.cc') + glob.glob('third_party/openfst-1.6.7/src/lib/*.cc') lib_sources = [fn for fn in lib_sources if not (fn.endswith('main.cc') or fn.endswith('test.cc'))] third_party_includes = [os.path.realpath(os.path.join("third_party", lib)) for lib in third_party_libs] ctc_sources = glob.glob('ctcdecode/src/*.cpp') ctc_headers = ['ctcdecode/src/binding.h', ] ffi = create_extension( name='ctcdecode._ext.ctc_decode', package=True, language='c++', headers=ctc_headers, sources=ctc_sources + lib_sources, include_dirs=third_party_includes, with_cuda=False, libraries=ext_libs, extra_compile_args=compile_args ) if __name__ == '__main__': ffi.build()
from torch.utils.ffi import create_extension this_file = os.path.dirname(__file__) sources = ['src/batchnormp.c'] headers = ['src/batchnormp.h'] defines = [] with_cuda = False abs_path = os.path.dirname(os.path.realpath(__file__)) extra_objects = [os.path.join(abs_path, 'dense/batchnormp_kernel.so')] extra_objects += glob.glob('/usr/local/cuda/lib64/*.a') if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/batchnormp_cuda.c'] headers += ['src/batchnormp_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True ffi = create_extension( 'dense.batch_norm', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects) if __name__ == '__main__': ffi.build()
headers = [] extra_objects = [] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['rotation_feature/src/rotationFeature.c'] headers += ['rotation_feature/src/rotationFeature.h'] extra_objects += ['rotation_feature/src/rotationFeatureKernel.cu.o'] defines += [('WITH_CUDA', None)] with_cuda = True extra_compile_args = None if sys.platform == 'darwin' else [ '-fopenmp' ] # MacOS does not support 'fopenmp' ffi = create_extension( 'rotation_feature._ext.rm', package=True, headers=headers, sources=sources, define_macros=defines, relative_to=__file__, include_dirs=['rotation_feature/src'], with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=extra_compile_args, ) if __name__ == '__main__': ffi.build()
sources = ['src/roi_pooling.c'] headers = ['src/roi_pooling.h'] defines = [] with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/roi_pooling_cuda.c'] headers += ['src/roi_pooling_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True this_file = os.path.dirname(os.path.realpath(__file__)) print(this_file) extra_objects = ['src/cuda/roi_pooling.cu.o'] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension( '_ext.roi_pooling', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects ) if __name__ == '__main__': ffi.build()
with_cuda = False if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/roi_align_forward_cuda.c','src/roi_align_backward_cuda.c'] headers += ['src/roi_align_forward_cuda.h','src/roi_align_backward_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True this_file = os.path.dirname(os.path.realpath(__file__)) print(this_file) extra_objects = ['src/cpp/roi_align_cpu_loop.o', 'src/cuda/roi_align_forward_cuda_kernel.cu.o', 'src/cuda/roi_align_backward_cuda_kernel.cu.o'] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects] ffi = create_extension( 'roialign', headers=headers, sources=sources, define_macros=defines, relative_to=__file__, with_cuda=with_cuda, extra_objects=extra_objects, extra_compile_args=['-std=c11'] ) if __name__ == '__main__': ffi.build()