在Google Colab用setup.py装库遇C++17编译器兼容错误的解决咨询
解决Google Colab中setup.py安装库时的C++版本不兼容问题
在Google Colab环境下使用setup.py安装OpenFold时,出现编译失败错误,核心原因是PyTorch和ATen要求使用C17或更高版本的编译器,但当前nvcc编译时采用了C14标准,触发以下错误提示:
#error C17 or later compatible compiler is required to use PyTorch.
#error You need C17 to compile PyTorch
#error C++17 or later compatible compiler is required to use ATen.
完整错误日志如下:
!! check.warn(importable) running build_ext /opt/conda/lib/python3.12/site-packages/torch/utils/cpp_extension.py:415: UserWarning: The detected CUDA version (12.2) has a minor version mismatch with the version that was used to compile PyTorch (12.1). Most likely this shouldn't be a problem. warnings.warn(CUDA_MISMATCH_WARN.format(cuda_str_version, torch.version.cuda)) /opt/conda/lib/python3.12/site-packages/torch/utils/cpp_extension.py:425: UserWarning: There are no g++ version bounds defined for CUDA version 12.2 warnings.warn(f'There are no {compiler_name} version bounds defined for CUDA version {cuda_str_version}') building 'attn_core_inplace_cuda' extension gcc -pthread -B /opt/conda/compiler_compat -fno-strict-overflow -DNDEBUG -O2 -Wall -fPIC -O2 -isystem /opt/conda/include -fPIC -O2 -isystem /opt/conda/include -fPIC -I/content/openfold/openfold/utils/kernel/csrc/ -I/opt/conda/lib/python3.12/site-packages/torch/include -I/opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include -I/opt/conda/lib/python3.12/site-packages/torch/include/TH -I/opt/conda/lib/python3.12/site-packages/torch/include/THC -I/usr/local/cuda/include -I/opt/conda/include/python3.12 -c openfold/utils/kernel/csrc/softmax_cuda.cpp -o build/temp.linux-x86_64-cpython-312/openfold/utils/kernel/csrc/softmax_cuda.o -O3 -DVERSION_GE_1_1 -DVERSION_GE_1_3 -DVERSION_GE_1_5 -DTORCH_API_INCLUDE_EXTENSION_H -DPYBIND11_COMPILER_TYPE=\"_gcc\" -DPYBIND11_STDLIB=\"_libstdcpp\" -DPYBIND11_BUILD_ABI=\"_cxxabi1011\" -DTORCH_EXTENSION_NAME=attn_core_inplace_cuda -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++17 /usr/local/cuda/bin/nvcc -I/content/openfold/openfold/utils/kernel/csrc/ -I/opt/conda/lib/python3.12/site-packages/torch/include -I/opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include -I/opt/conda/lib/python3.12/site-packages/torch/include/TH -I/opt/conda/lib/python3.12/site-packages/torch/include/THC -I/usr/local/cuda/include -I/opt/conda/include/python3.12 -c openfold/utils/kernel/csrc/softmax_cuda_kernel.cu -o build/temp.linux-x86_64-cpython-312/openfold/utils/kernel/csrc/softmax_cuda_kernel.o -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr --compiler-options '-fPIC' -O3 --use_fast_math -DVERSION_GE_1_1 -DVERSION_GE_1_3 -DVERSION_GE_1_5 -std=c++14 -maxrregcount=50 -U__CUDA_NO_HALF_OPERATORS__ -U__CUDA_NO_HALF_CONVERSIONS__ --expt-relaxed-constexpr --expt-extended-lambda -gencode arch=compute_75,code=sm_75 -DTORCH_API_INCLUDE_EXTENSION_H -DPYBIND11_COMPILER_TYPE=\"_gcc\" -DPYBIND11_STDLIB=\"_libstdcpp\" -DPYBIND11_BUILD_ABI=\"_cxxabi1011\" -DTORCH_EXTENSION_NAME=attn_core_inplace_cuda -D_GLIBCXX_USE_CXX11_ABI=0 In file included from /opt/conda/lib/python3.12/site-packages/torch/include/torch/extension.h:5, from openfold/utils/kernel/csrc/softmax_cuda_kernel.cu:18: /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/all.h:4:2: error: #error C++17 or later compatible compiler is required to use PyTorch. 4 | #error C++17 or later compatible compiler is required to use PyTorch. | ^~~~~ In file included from /opt/conda/lib/python3.12/site-packages/torch/include/c10/util/TypeList.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/c10/util/Metaprogramming.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/c10/core/DispatchKeySet.h:4, from /opt/conda/lib/python3.12/site-packages/torch/include/c10/core/Backend.h:5, from /opt/conda/lib/python3.12/site-packages/torch/include/c10/core/Layout.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/ATen/core/TensorBody.h:12, from /opt/conda/lib/python3.12/site-packages/torch/include/ATen/core/Tensor.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/ATen/Tensor.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/autograd/function_hook.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/autograd/cpp_hook.h:2, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/autograd/variable.h:6, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/autograd/autograd.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/autograd.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/all.h:7, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/extension.h:5, from openfold/utils/kernel/csrc/softmax_cuda_kernel.cu:18: /opt/conda/lib/python3.12/site-packages/torch/include/c10/util/C++17.h:27:2: error: #error You need C++17 to compile PyTorch 27 | #error You need C++17 to compile PyTorch | ^~~~~ In file included from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:4, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/data.h:3, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/csrc/api/include/torch/all.h:9, from /opt/conda/lib/python3.12/site-packages/torch/include/torch/extension.h:5, from openfold/utils/kernel/csrc/softmax_cuda_kernel.cu:18: /opt/conda/lib/python3.12/site-packages/torch/include/ATen/ATen.h:4:2: error: #error C++17 or later compatible compiler is required to use ATen. 4 | #error C++17 or later compatible compiler is required to use ATen. | ^~~~~ error: command '/usr/local/cuda/bin/nvcc' failed with exit code 1
解决方案
1. 强制nvcc使用C++17编译(优先推荐)
方法一:通过环境变量指定
在运行setup.py前,先执行以下命令设置环境变量,强制编译器使用C++17标准:
export CXXFLAGS="-std=c++17" export CUDAFLAGS="-std=c++17" export TORCH_CUDA_ARCH_LIST="7.5" # 匹配Colab默认GPU架构(Tesla T4为7.5)
然后执行安装命令:
python setup.py install
方法二:修改setup.py的编译参数
找到setup.py中定义CUDAExtension的部分,添加extra_compile_args参数指定C++17标准:
from torch.utils import cpp_extension # 找到对应扩展定义代码,添加extra_compile_args extension = cpp_extension.CUDAExtension( name='attn_core_inplace_cuda', sources=[ 'openfold/utils/kernel/csrc/softmax_cuda.cpp', 'openfold/utils/kernel/csrc/softmax_cuda_kernel.cu' ], extra_compile_args={ 'cxx': ['-std=c++17'], 'nvcc': ['-std=c++17'] } )
修改后重新运行python setup.py install
2. 尝试预编译包安装(避免源码编译)
如果库提供了预编译pip包,直接通过pip安装跳过源码编译:
pip install openfold
也可查找社区维护的预编译wheel包进行安装。
3. 确认并升级编译器版本(Colab环境一般无需额外操作)
Colab默认gcc版本为9.x及以上,已支持C++17,可通过以下命令确认:
gcc --version
若版本过低,执行以下命令升级:
sudo apt update sudo apt install gcc-10 g++-10 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100
内容的提问来源于stack exchange,提问作者Lib
相关产品推荐
相关产品推荐

