pytorch c++ 编译

最近在搞pytorch c++ inference,需要编译pytorch lib库。

折腾了几次,终于搞定,这里记录下来。

clone源码

git clone https://github.com/pytorch/pytorch.git

git checkout tags/v1.3.1

git checkout -b v1.3.1

git submodule sync

git submodule update –init –recursive

准备环境,miniconda、cmake(3.5+)、cuda 9.0+

miniconda安装

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

sh Miniconda3-latest-Linux-x86_64.sh

cmake安装

curl -O https://github.com/Kitware/CMake/releases/download/v3.15.5/cmake-3.15.5.tar.gz

./configure –prefix=install_dir

cuda使用yum安装

安装devtoolset-7,pytorch编译对gcc有要求,不然会出现编译错误,参考https://github.com/pytorch/builder/blob/master/update_compiler.sh

yum install -y -q devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-gcc-gfortran devtoolset-7-binutils

source /opt/rh/devtoolset-7/enable

源码编译,可以根目录直接make,这里选择手动安装到指定目录

进入torch根目录,

mkdir build

cmake .. -DPYTHON_EXECUTABLE:FILEPATH=/search/privateData/tools/miniconda3/bin/python -DPYTHON_INCLUDE_DIR=/search/privateData/tools/miniconda3/include/python3.7m -DCMAKE_INSTALL_PREFIX=/search/privateData/tools/libtorch-1.3.0

Leave a Reply

Your email address will not be published. Required fields are marked *