- 在跑gaussian_splatting的时候报错,看python代码是在
ProjectGaussians.apply
的时候报错的,
AttributeError: 'NoneType' object has no attribute 'ara'
- 点进源码一看,
C_
是从import gsplat.cuda as _C
中来的,直接在python CLI中import是可以的,直接安装gsplat whl虽然能安装,单独import gsplat
也能import,但是运行时候才会报错. - 定睛一看
gsplat.cuda
的__init__,用到了一个懒加载,也就是说import的时候不会报错只有实际调用的时候才会真正的去加载cuda模块,然后报错.地那件里面的_backend
,原来正真的编译模块一开始是没有的,当调用到的时候才会去编译模块
try:
# try to import the compiled module (via setup.py)
from gsplat import csrc as _C
except ImportError:
# if failed, try with JIT compilation
....
- 一开始尝试使用conda 安装cudatoolkit和nvcc,安装成功了但是在进入
_C = load
使用ninja编译的时候会报缺少cuda_runtime.h
头文件,报错
Installation failed cc1plus: fatal error: cuda_runtime.h
- 这个文件在conda目录下的lib中也没有,于是开始查看系统自带的cuda,终于在
/usr/local/cuda-11.6/targets/x86_64-linux/include/
中找到. 然后将其添加到linux CPATH环境变量(PS这里11.7换成自己的cuda版本)
# 查找runtime.h
ll -h "/usr/local/cuda-11.7/targets/x86_64-linux/include/" | grep runtime
# 添加环境变量
export CPATH=/usr/local/cuda-11.7/targets/x86_64-linux/include:$CPATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.7/lib64
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/cuda-11.7/lib64
CPATH 环境变量用于 C/C++ 预处理,它列出了预处理器检索 C/C++ 头文件的目录,与之相对的环境变量还有 C_INCLUDE_PATH、CPLUS_INCLUDE_PATH 和 OBJC_INCLUDE_PATH。其中无论预处理哪种语言,都会使用 CPATH 环境变量,而其余的三种环境变量仅在预处理指定的语言时才会适用。
- 完成后又发先报错缺少动态链接库
lcudart
也就是找不到libcudart.so
,查阅资料发现仅仅添加LD_LIBRARY_PATH
不行,gcc找不到,需要添加到LIBRARY_PATH
中,添加后可以编译
The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it cannot find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).
- (optional) 如果你有多个cuda版本可以写一个shell函数快速切换环境
# add below to your env bash file.
function _switch_cuda {
v=$1
export PATH=/usr/local/cuda-$v/bin:$PATH
export CUDADIR=/usr/local/cuda-$v
export LD_LIBRARY_PATH=/usr/local/cuda-$v/lib64:$LD_LIBRARY_PATH
export CPATH=/usr/local/cuda-$v/targets/x86_64-linux/include:$CPATH
nvcc --version
}
_switch_cuda 11.6
- 在此运行
<conda env路径>/lib/python3.8/site-packages/gsplat/cuda/_backend.py
,发现了没有报错即可,也可以直接跑这段代码进行测试
import glob
import os
from torch.utils.cpp_extension import _get_build_directory, load
PATH='<CONDA env path>/lib/python3.8/site-packages/gsplat/cuda'
name = "gsplat_cuda"
build_dir = _get_build_directory(name, verbose=False)
print('@build_dir',build_dir)
extra_include_paths = [os.path.join(PATH, "csrc/third_party/glm")]
extra_cflags = ["-O3"]
extra_cuda_cflags = ["-O3"]
sources = list(glob.glob(os.path.join(PATH, "csrc/*.cu"))) + list(
glob.glob(os.path.join(PATH, "csrc/*.cpp"))
)
_C = load(
name=name,
sources=sources,
extra_cflags=extra_cflags,
extra_cuda_cflags=extra_cuda_cflags,
extra_include_paths=extra_include_paths,
)
Reference
- How to manage multiple versions of Cuda and cuDNN ? | Notes by Air
- Linux 环境变量 PATH、CPATH、LIBRARY_PATH和LD_LIBRARY_PATH - Deza Blog
- 解决 :fatal error: cuda_runtime.h: No such file or directory_cuda_runtime.h: 没有那个文件或目录-CSDN博客
- /usr/bin/ld: cannot find -lxxx 的解决办法 - zhming - 博客园
- c - LD_LIBRARY_PATH is ignored by GCC - Stack Overflow
- fatal error: cuda_runtime.h: No such file or directory · Issue #131 · NVIDIA/nccl · GitHub