报错解决:RuntimeError: Error compiling objects for extension和nvcc fatal: Unsupported gpu architecture

报错解决:RuntimeError: Error compiling objects for extension和nvcc fatal: Unsupported gpu architecture

  • 报错
  • 原因与解决
  • 参考文献

报错

博主在配置mmdetection3d环境时,运行pip install -v -e .会有如下报错:

nvcc fatal : Unsupported gpu architecture 'compute_86'
error: command '/usr/bin/nvcc' failed with exit code 1
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/torch/utils/cpp_extension.py", line 1717, in _run_ninja_build
subprocess.run(
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/subprocess.py", line 516, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "setup.py", line 197, in
setup(
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/init.py", line 155, in setup
return distutils.core.setup(**attrs)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/core.py", line 148, in setup
return run_commands(dist)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/core.py", line 163, in run_commands
dist.run_commands()
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 967, in run_commands
self.run_command(cmd)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
cmd_obj.run()
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/command/develop.py", line 34, in run
self.install_for_development()
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/command/develop.py", line 114, in install_for_development
self.run_command('build_ext')
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
cmd_obj.run()
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/command/build_ext.py", line 79, in run
_build_ext.run(self)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/Cython/Distutils/old_build_ext.py", line 186, in run
_build_ext.build_ext.run(self)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 339, in run
self.build_extensions()
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/torch/utils/cpp_extension.py", line 735, in build_extensions
build_ext.build_extensions(self)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/Cython/Distutils/old_build_ext.py", line 195, in build_extensions
_build_ext.build_ext.build_extensions(self)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 448, in build_extensions
self._build_extensions_serial()
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 473, in _build_extensions_serial
self.build_extension(ext)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/command/build_ext.py", line 202, in build_extension
_build_ext.build_extension(self, ext)
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 528, in build_extension
objects = self.compiler.compile(sources,
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/torch/utils/cpp_extension.py", line 556, in unix_wrap_ninja_compile
_write_ninja_file_and_compile_objects(
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/torch/utils/cpp_extension.py", line 1399, in _write_ninja_file_and_compile_objects
_run_ninja_build(
File "/home/user/anaconda3/envs/open_mmlab/lib/python3.8/site-packages/torch/utils/cpp_extension.py", line 1733, in _run_ninja_build
raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension

原因与解决

大概率的原因是安装的pytorch和cuda版本不匹配,解决办法是安装想匹配的cuda或pytorch。

首先查看cuda版本

nvcc -V

输出如下,可以看出当前cuda版本是10.2:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:24:38_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89

接着查看pytorch版本

python -c "import torch; print(torch.__version__)"

输出1.10.0+cu102

查看pytorch对应的cuda版本

python -c "import torch; print(torch.version.cuda)"

输出10.2

注意,上述输出的 cuda 的版本并不一定是 Pytorch 在实际系统上运行时使用的 cuda 版本,而是编译该 Pytorch release 版本时使用的 cuda 版本。

想要查看 Pytorch 实际使用的运行时的 cuda 目录,可以直接输出 cpp_extension.py 中的 CUDA_HOME 变量。

python -c "import torch; import torch.utils; import torch.utils.cpp_extension;  print(torch.utils.cpp_extension.CUDA_HOME)"

输出/usr/local/cuda-11.3

一般最好pytorch对应的cuda版本与安装版本相同,若不相同,版本相差比较大,就有可能出现上述报错,解决方法就是安装相匹配的cuda驱动,安装教程可以查看博主的另一篇博客博客,若已安装多版本CUDA,可以在bashrc文件中,添加以下代码指定CUDA版本:

# 根据自身情况修改cuda版本
export PATH=/usr/local/cuda-10.2/bin:$PATH
export CUDA_PATH=/usr/local/cuda-10.2
export CUDA_HOME=/usr/local/cuda-10.2
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:$LD_LIBRARY_PATH

参考文献

Pytorch与CUDA的版本

你可能感兴趣的:(Linux,python,CUDA,Pytorch,Ubuntu)