报错解决:Fatal error: ‘THC/THC.h’: No such file or directory

报错解决:Fatal error: 'THC/THC.h': No such file or directory

报错

博主的软硬件环境(供参考):

  • Linux
  • NVIDIA GeForce RTX 3090
  • CUDA 11.6
  • gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
  • Pytorch:1.12.0+cu116

博主在安装mmdetection3d的时候,遇到了Fatal error: 'THC/THC.h': No such file or directory的报错。

# 下载mmdetection3d
git clone https://github.com/open-mmlab/mmdetection3d.git
# 切换目录
cd mmdetection3d
# 由于代码需要,切换到指定分支
git checkout v0.17.1
# 编译安装
pip install -v -e .

报错如下:

 ...
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    mmdet3d/ops/ball_query/src/ball_query.cpp:4:10: fatal error: THC/THC.h: No such file or directory
     #include <THC/THC.h>
              ^~~~~~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
ERROR: Command errored out with exit status 1: 
... 
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.
The above exception was the direct cause of the following exception:
...
RuntimeError: Error compiling objects for extension

完整报错如下图所示:
在这里插入图片描述

原因

报错的原因是THC方法目前在最新版本的 Pytorch 中已被弃用,并被 ATen API 取代,因此在高版本的Pytorch(版本在1.11.0及以上)编译安装mmdet3d的时候就会遇到无法找到THC/THC.h的报错。

解决方法

解决方法有两种:

  1. 安装低版本的Pytorch,再安装mmdet3d。
    例如,经亲测1.9.1版本的Pytorch可以正常编译成功,命令如下:
conda create -n test python=3.8 -y
conda activate test
pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio==0.9.1 -f https://download.pytorch.org/whl/torch_stable.html
# 安装mmcv、mmdetection和mmsegmentation
# 安装mmdetection3d
  1. 根据报错的反馈,把所有包含<THE/THC.h>头文件的#include <THE/THC.h>注释掉,取而代之是新的头文件,代码如下:
//Comment Out
//#include <THE/THC.h>
//extern THCState *state;
//cudaStream_t stream = THCState_getCurrentStream(state);

//Replace with
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/CUDAEvent.h>
cudaStream_t stream = at::cuda::getCurrentCUDAStream();

总结

在安装mmcv、mmdet、mmseg和mmdet3d的时候,首先一定要注意各版本之间的依赖关系,以及软硬件版本,例如Pytorch版本、显卡驱动版本和CUDA版本等等。其次,注意环境的依赖项,如遇到ModuleNotFoundError: No module named ‘XXX’的问题,那就根据报错提示,进行安装相关依赖:pip install XXX,也可参考博主的另一片博客:报错解决:ModuleNotFoundError: No module named ‘XXX’。最后,如果依赖和版本都没有问题,那就依据报错信息,逐一排查其他问题,将BUG解决。

参考文献

  1. https://stackoverflow.com/questions/72988735/replacing-thc-thc-h-module-to-aten-aten-h-module
  2. https://github.com/open-mmlab/mmdetection3d/issues/1332

猜你喜欢

转载自blog.csdn.net/weixin_43603658/article/details/134832070