Compile Pytorch and Torchvision from source code with CUDA enabled.


每次debug之后,重新编译

python setup.py clean
python setup.py install

背景

项目需要python、c++混合编程,c++中需要用到libtorch包。使用pybind11编译c++代码得到python包一切正常,但是在python中import c++拓展总是报各种错

undefined symbol: _ZN3c106detail23torchInternalAssertFailEPKcS2_jS2_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

TypeError: test(): incompatible function arguments

查阅相关链接:github issue , pytorch discuss
初步定位是cxx11 ABI的问题,cpp extend和pytorch中的_GLIBCXX_USE_CXX11_ABI设置不一致,在pip/conda安装的pytorch中,_GLIBCXX_USE_CXX11_ABI=0,在cpp中,默认是1。
所以在一般情况下,在编译c++时设置_GLIBCXX_USE_CXX11_ABI=0就能解决问题。
但是在我的项目中,c++还用到了其他用CXX11_ABI编译的包,所以需要自己从源码编译pytorch。

Ubuntu下源码编译Pytorch

  1. conda中新建环境并进入
conda create -n Pytorch python=3.8.3

conda activate Pytorch
  1. 下载pytorch源码
git clone https://github.com/pytorch/pytorch
cd pytorch

git tag # 查看分支
git checkout v1.11.0 # 切换到想要的版本

# if you are updating an existing checkout
git submodule sync
git submodule update --init --recursive

如果下载太慢,可以参照这里

  1. 安装依赖
    参照pytorch github readme
conda install astunparse numpy ninja pyyaml setuptools cmake cffi typing_extensions future six requests dataclasses

conda install mkl mkl-include
# CUDA only: Add LAPACK support for the GPU if needed
conda install -c pytorch magma-cuda110  # or the magma-cuda* that matches your CUDA version from https://anaconda.org/pytorch/repo
  1. 开始编译
# 指定cuda版本,使用cuda 11.7时遇到错误/usr/local/cuda/lib64/libcublas.so: undefined reference to `cublasLtGetStatusString@libcublasLt.so.11',使用cuda 11.2时顺利编译

export PATH=/usr/local/cuda-11.2/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64:$LD_LIBRARY_PATH 

export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
python setup.py install

Bug shotting

nvlink error

在这里插入图片描述

应该是nccl的问题,这个issue 提出要重新安装nccl,参照官网安装,
在这里插入图片描述
然后

sudo dpkg -i nccl-repo-ubuntu1604-2.2.13-ga-cuda8.0_1-1_amd64.deb
sudo apt-get install libnccl2
sudo apt-get install libnccl-dev
export USE_SYSTEM_NCCL=ON

又出现问题,安装的时候提示“The public CUDA GPG key does not appear to be installed.”,按照建议指令“sudo cp /var/cuda-repo-wsl-ubuntu-11-7-local/cuda-B81839D3-keyring.gpg /usr/share/keyrings/”之后问题依然存在。在这里得到了解决办法,
在这里插入图片描述

Ubuntu下源码编译Torchvision

直接使用pip install torchvision==x.x.x / conda install torchvision=x.x.x -c pytorch都会重新安装pytorch,所以只能从源码编译。

  1. 下载torchvision源码
    git clone https://github.com/pytorch/vision
    
  2. 查看需要的版本
    git tag
    
    在这里插入图片描述
  3. 切换到所需版本
    git checkout v0.12.0
    
  4. 开始编译
    # 指定cuda版本,与pytorch一致
    
    export PATH=/usr/local/cuda-11.2/bin:$PATH
    export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64:$LD_LIBRARY_PATH 
    
    export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
    python setup.py install
    

Optional

我没有用到但是可能有用的设置

export CC=/usr/bin/gcc
export CXX=/usr/bin/g++
export USE_CUDA="True"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值