|
| 1 | +import platform |
| 2 | + |
| 3 | +from setuptools import setup |
| 4 | +from setuptools.errors import CCompilerError, PackageDiscoveryError |
| 5 | +from torch.utils.cpp_extension import BuildExtension |
| 6 | + |
| 7 | + |
| 8 | +class MyBuildExtension(BuildExtension): |
| 9 | + def run(self): |
| 10 | + try: |
| 11 | + super(MyBuildExtension, self).run() |
| 12 | + except FileNotFoundError: |
| 13 | + raise Exception("File not found. Could not compile C extension") |
| 14 | + |
| 15 | + def build_extension(self, ext): |
| 16 | + # common settings |
| 17 | + for e in self.extensions: |
| 18 | + pass |
| 19 | + |
| 20 | + # OS specific settings |
| 21 | + if platform.system() == "Darwin": |
| 22 | + for e in self.extensions: |
| 23 | + e.extra_compile_args.extend([ |
| 24 | + "-Xpreprocessor", |
| 25 | + "-fopenmp", |
| 26 | + "-mmacosx-version-min=10.15", |
| 27 | + ]) |
| 28 | + e.extra_link_args.extend([ |
| 29 | + "-lomp", |
| 30 | + ]) |
| 31 | + |
| 32 | + elif platform.system() == "Linux": |
| 33 | + for e in self.extensions: |
| 34 | + e.extra_compile_args.extend([ |
| 35 | + "-fopenmp", |
| 36 | + ]) |
| 37 | + e.extra_link_args.extend([ |
| 38 | + "-fopenmp", |
| 39 | + ]) |
| 40 | + |
| 41 | + # compiler specific settings |
| 42 | + if self.compiler.compiler_type == "unix": |
| 43 | + for e in self.extensions: |
| 44 | + e.extra_compile_args.extend([ |
| 45 | + "-std=c++17", |
| 46 | + "-pthread", |
| 47 | + ]) |
| 48 | + |
| 49 | + elif self.compiler.compiler_type == "msvc": |
| 50 | + for e in self.extensions: |
| 51 | + e.extra_compile_args.extend(["/utf-8", "/std:c++17", "/openmp"]) |
| 52 | + e.define_macros.extend([ |
| 53 | + ("_CRT_SECURE_NO_WARNINGS", 1), |
| 54 | + ("_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING", 1), |
| 55 | + ]) |
| 56 | + |
| 57 | + # building |
| 58 | + try: |
| 59 | + super(MyBuildExtension, self).build_extension(ext) |
| 60 | + except (CCompilerError, PackageDiscoveryError, ValueError): |
| 61 | + raise Exception("Could not compile C extension") |
| 62 | + |
| 63 | + |
| 64 | +def build(setup_kwargs): |
| 65 | + from torch.utils.cpp_extension import CUDAExtension |
| 66 | + |
| 67 | + try: |
| 68 | + setup_kwargs.update({ |
| 69 | + "ext_modules": [ |
| 70 | + CUDAExtension( |
| 71 | + 'torchmcubes_module', |
| 72 | + [ |
| 73 | + 'cxx/pscan.cu', |
| 74 | + 'cxx/mcubes.cpp', |
| 75 | + 'cxx/mcubes_cpu.cpp', |
| 76 | + 'cxx/mcubes_cuda.cu', |
| 77 | + 'cxx/grid_interp_cpu.cpp', |
| 78 | + 'cxx/grid_interp_cuda.cu', |
| 79 | + ], |
| 80 | + extra_compile_args=['-DWITH_CUDA'], |
| 81 | + ) |
| 82 | + ], |
| 83 | + "cmdclass": { |
| 84 | + "build_ext": BuildExtension |
| 85 | + } |
| 86 | + }) |
| 87 | + except: |
| 88 | + from torch.utils.cpp_extension import CppExtension |
| 89 | + |
| 90 | + print('CUDA environment was not successfully loaded!') |
| 91 | + print('Build only CPU module!') |
| 92 | + |
| 93 | + setup_kwargs.update({ |
| 94 | + 'ext_modules': [ |
| 95 | + CppExtension('torchmcubes_module', [ |
| 96 | + 'cxx/mcubes.cpp', |
| 97 | + 'cxx/mcubes_cpu.cpp', |
| 98 | + 'cxx/grid_interp_cpu.cpp', |
| 99 | + ]) |
| 100 | + ], |
| 101 | + 'cmdclass': { |
| 102 | + 'build_ext': MyBuildExtension |
| 103 | + } |
| 104 | + }) |
0 commit comments