Linux下源码编译Libtorch
在部署AI模型的时候用到了libtorch
库,直接从官网下载后发现与现有环境不兼容,执行strings /usr/lib64/libstdc++.so.6 | grep GLIBC
发现缺失相应的GLIBC版本,因此选择下载源码重新编译libtorch。这里记录一下编译过程。
参照官网编译教程,主要分为以下几步:
一、安装anconda3
anaconda3的安装教程可以参考: 安装anconda3
安装完后创建一个环境,我这里采用的python版本是3.7.4,所以执行以下命令
conda create -n Test374 python=3.7.4
创建完成后,进入该环境,conda activate Test374
二、安装环境
安装相关依赖
conda install astunparse numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses
三、下载
git clone --recursive https://github.com/pytorch/pytorch # --recursive表示下载子模块,但有些模块很难下载下来,可以先用 git clone https://github.com.cnpmjs.org/pytorch/pytorch
cd pytorch
四、切换分支
克隆下来的是最新版本,执行git tag
查看分支,然后git checkout branch_name
切换到想要的分支,
git checkout v1.2.0
# if you are updating an existing checkout
git submodule sync
git submodule update --init --recursive
注意:如果下载比较慢或者报错,可以在pytorch目录下查看.gitmodules
文件, 切换分支后先把里面网址替换为github加速插件的地址, 然后再执行git submodule sync 和 后面的命令
- 加速
以asmjit
库为例
# 原始网址:
git@github.com:asmjit/asmjit.git
# 加速:
https://github.com.cnpmjs.org/asmjit/asmjit.git
https://hub.fastgit.org/asmjit/asmjit.git
https://github.91chi.fun//https://github.com/asmjit/asmjit.git
五、安装
用export或者cmake-gui设置编译选项,如:
export USE_CUDA=False
export BUILD_TEST=False
export USE_NINJA=OFF
在分支目录下新建一个build文件夹,进入文件夹后执行以下命令,完成后在build目录下可以找到生成的结果。
mkdir build && cd build
python ../tools/build_libtorch.py
六、整理
新建libtorch
文件夹
- 复制pytorch下的torch/include 到libtorch 目录
- 复制build/lib下编译好的库文件到 libtorch/lib
七、修改编译依赖项
比如使用已有的MKL库,则可以修改pytorch/cmake
目录下的Dependencies.cmake
文件,在里面指定你的库文件。
如果BLAS编译的时候设置为Eigen(默认为MKL),需要进行以下修改, 更多细节参考#32407
diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake
index 1b3917c..efe9c5d 100644
--- a/cmake/Dependencies.cmake
+++ b/cmake/Dependencies.cmake
@@ -112,6 +112,7 @@ message(STATUS "Trying to find preferred BLAS backend of choice: " ${BLAS})
if(BLAS STREQUAL "Eigen")
# Eigen is header-only and we do not have any dependent libraries
+ find_package(Eigen3 REQUIRED)
+ # set(Eigen3_FOUND True)
+ # set(Eigen3_INCLUDE_DIR "/root/eigen3/include")
set(CAFFE2_USE_EIGEN_FOR_BLAS ON)
elseif(BLAS STREQUAL "ATLAS")
find_package(Atlas REQUIRED)
@@ -155,15 +156,20 @@ endif()
if (NOT INTERN_BUILD_MOBILE)
set(AT_MKL_ENABLED 0)
set(AT_MKL_MT 0)
- set(USE_BLAS 1)
- if(NOT (ATLAS_FOUND OR OpenBLAS_FOUND OR MKL_FOUND OR VECLIB_FOUND))
+ if(CAFFE2_USE_EIGEN_FOR_BLAS)
+ message(WARNING "Using EIGEN for blas for Caffe2, disabling blas in libpytorch")
+ set(USE_BLAS 0)
+ elseif(ATLAS_FOUND OR OpenBLAS_FOUND OR MKL_FOUND OR VECLIB_FOUND)
+ set(USE_BLAS 1)
+ else()
message(WARNING "Preferred BLAS (" ${BLAS} ") cannot be found, now searching for a general BLAS library")
find_package(BLAS)
if (NOT BLAS_FOUND)
set(USE_BLAS 0)
set(BLAS "" CACHE STRING "Selected BLAS library")
else()
- set(BLAS BLAS_INFO CACHE STRING "Selected BLAS library")
+ set(USE_BLAS 1)
+ set(BLAS ${BLAS_INFO} CACHE STRING "Selected BLAS library")
endif()
endif()
@@ -1359,9 +1291,11 @@ if (NOT INTERN_BUILD_MOBILE)
CACHE BOOL "Copy the required BLAS DLLs into the TH install dirs")
ENDIF()
- FIND_PACKAGE(LAPACK)
- IF (LAPACK_FOUND)
- SET(USE_LAPACK 1)
+ IF (NOT CAFFE2_USE_EIGEN_FOR_BLAS)
+ FIND_PACKAGE(LAPACK)
+ IF (LAPACK_FOUND)
+ SET(USE_LAPACK 1)
+ ENDIF()
ENDIF()
if (NOT USE_CUDA)