lbin/DCNv2

raise RuntimeError(message) from e RuntimeError: Error compiling objects for extension

Xavier-Lin opened this issue · 0 comments

Previously when compiling DCNv2, it always appeared:

File "/home/xxx/miniconda3/envs/open-mmlab/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 1682, in _run_ninja_build
raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension

Then I made changes to the setup.py file for the compilation problem. The changed code is as follows:

`
import glob
import os
import sys

import torch
from setuptools import find_packages, setup
from torch.utils.cpp_extension import CUDA_HOME, CppExtension, CUDAExtension

requirements = ["torch", "torchvision"]

def get_extensions():
this_dir = os.path.dirname(os.path.abspath(file))
# extensions_dir = os.path.join(this_dir, "src")
extensions_dir = os.path.abspath(
os.path.join(this_dir, "csrc"))

main_file = glob.glob(os.path.join(extensions_dir, "*.cpp"))
source_cpu = glob.glob(os.path.join(extensions_dir, "cpu", "*.cpp"))
source_cuda = glob.glob(os.path.join(extensions_dir, "cuda", "*.cu"))

os.environ["CC"] = "g++"
sources = main_file + source_cpu
extension = CppExtension
extra_compile_args = {"cxx": []}
define_macros = []

if torch.cuda.is_available() and CUDA_HOME is not None:
    extension = CUDAExtension
    sources += source_cuda
    define_macros += [("WITH_CUDA", None)]
    extra_compile_args["nvcc"] = [
        "-DCUDA_HAS_FP16=1",
        "-D__CUDA_NO_HALF_OPERATORS__",
        "-D__CUDA_NO_HALF_CONVERSIONS__",
        "-D__CUDA_NO_HALF2_OPERATORS__",
    ]
else:
    # raise NotImplementedError('Cuda is not available')
    pass

extra_compile_args['cxx'].append('-fopenmp')

sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
    extension(
        "_ext",
        sources,
        include_dirs=include_dirs,
        define_macros=define_macros,
        extra_compile_args=extra_compile_args,
    )
]
return ext_modules

setup(
name="DCNv2",
version="0.1",
author="charlesshang",
url="https://github.com/charlesshang/DCNv2",
description="deformable convolutional networks",
packages=find_packages(
exclude=(
"configs",
"tests",
)
),
# install_requires=requirements,
ext_modules=get_extensions(),
cmdclass={"--build_ext": torch.utils.cpp_extension.BuildExtension},
)
'''
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
'''
`

Then it will be compiled successfully. I hope it will be useful to you.