报错解决: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的报错。

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

报错如下:

bash 复制代码
 ...
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可以正常编译成功,命令如下:
bash 复制代码
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>注释掉,取而代之是新的头文件,代码如下:
cpp 复制代码
//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
相关推荐
cv-player3 个月前
NLP学习与踩坑记录(持续更新版)
代码·自然语言·报错解决
懷淰メ5 个月前
python3--lxml pytoml.core.TomlError expected_equals报错解决
爬虫·python·报错解决·lxml
鱼儿也有烦恼5 个月前
OSError: Can‘t load tokenizer for ‘bert-base-chinese‘
深度学习·报错解决
颜淡慕潇5 个月前
【已解决】解决前端模块与Node.js版本不兼容问题
前端·node.js·报错解决·版本兼容问题
Arnold-FY-Chen10 个月前
解决UniAD在高版本CUDA、pytorch下运行遇到的问题
人工智能·自动驾驶·mmdetection3d·uniad·nuscenes
NiNi_suanfa1 年前
【leetcode报错】 leetcode格式问题解决:error: stray ‘\302’ in program [solution.c]
算法·leetcode·报错·报错解决
一颗小树x1 年前
3D目标检测框架 MMDetection3D环境搭建 docker篇
docker·框架·环境搭建·3d目标检测·mmdetection3d