做研发,总是要不停装新机器和新系统。今天尝试了一下编译Paddle2.2.2。

先说结果:

(1)Ubuntu20.04 + Python3.8.10 + gcc-8

(2)cuda和cudnn的版本分别是:
cuda_11.4.3_470.82.01_linux.run
cudnn-11.4-linux-x64-v8.2.4.15.tgz

下面介绍过程和碰到的问题,首先参考Paddle官网的说明,

源码编译 — Paddle-Inference documentation

找到那段“基于 Ubuntu 18.04”的说明,我们一步步根据这个说明来编译安装。

一:环境准备

首先是安装必须的工具,官网上是说

sudo apt-get install gcc g++ make cmake git vim unrar python3 python3-dev python3-pip swig wget patchelf libopencv-dev
pip3 install numpy protobuf wheel setuptools

我的系统里已经有python3.8.10这个默认的版本了,就没有再安装python3这些。opencv我昨天已经编译安装了opencv4.5.5这个版本的,所以也不需要重复安装,

Ubuntu20.04编译安装Opencv4.5.5_高精度计算机视觉的博客-CSDN博客

如果你没有自己编译安装opencv,那么就可以在这里选择安装libopencv-dev。patchefl这些都是必须的,没装的话无法编译通过,不嫌麻烦的话也可以在编译报错了之后再一步步安装。

cuda和cudnn的版本分别是:
cuda_11.4.3_470.82.01_linux.run
cudnn-11.4-linux-x64-v8.2.4.15.tgz

官方有这么一段话,

编译飞桨过程中可能会打开很多文件,Ubuntu 18.04 默认设置最多同时打开的文件数是1024(参见 ulimit -a),需要更改这个设定值。

在 /etc/security/limits.conf 文件中添加两行。

* hard noopen 102400
* soft noopen 102400

重启计算机,重启后执行以下指令,请将${user}切换成当前用户名。

su ${user}
ulimit -n 102400

 我都照做了。值得说明的是,如果你碰到下面这样的错误,

/usr/bin/ld: 找不到 ../operators/math/libsampler.a: Too many open files
/usr/bin/ld: 找不到 ../framework/libgenerator.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libgru_compute.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libmaxouting.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libpooling.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libsequence2batch.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libsequence_pooling.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libsoftmax.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libbeam_search.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libfc.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/liblapack_function.a: Too many open files
/usr/bin/ld: 找不到 ../platform/dynload/libdynload_lapack.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libmatrix_bit_code.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libunpooling.a: Too many open files
/usr/bin/ld: 找不到 ../operators/math/libvol2col.a: Too many open files
....

通常是因为你的"ulimit -n 102400"设置没起作用(比如你重启了机器,打开了新的终端等),所以重新设置一下就好了。我实际用的指令是

ulimit -n 20480

二:编译命令

使用 Git 将飞桨代码克隆到本地,并进入目录,切换到稳定版本(git tag显示的标签名,如 release/2.0)。 飞桨使用 develop 分支进行最新特性的开发,使用 release 分支发布稳定版本。在 GitHub 的 Releases 选项卡中,可以看到飞桨版本的发布记录。

git clone https://github/PaddlePaddle/Paddle.git
cd Paddle
git checkout release/2.2

下面以 GPU 版本为例说明编译命令。其他环境可以参考“CMake编译选项表”修改对应的cmake选项。比如,若编译 CPU 版本,请将 WITH_GPU 设置为 OFF。

# 创建并进入 build 目录
~$ mkdir build_cuda && cd build_cuda
# 执行cmake指令
~$ cmake .. -DPY_VERSION=3 \
        -DWITH_TESTING=OFF \
        -DWITH_MKL=ON \
        -DWITH_GPU=ON \
        -DON_INFER=ON \
        ..
~$ nproc
12 

使用make编译

make -j12

编译成功后可在dist目录找到生成的.whl包

比如我的编译生成包在这里,
~/ocr/01/Paddle/build_cuda/python/dist/paddlepaddle_gpu-0.0.0-cp38-cp38-linux_x86_64.whl

官方给出的版本是(前面官方明明说是GPU版本,可看上去很像CPU版本),
pip3 install python/dist/paddlepaddle-2.0.0-cp38-cp38-linux_x86_64.whl

不清楚为什么会有这样的版本区别。

预测库编译

make inference_lib_dist -j4

cmake编译环境表

以下介绍的编译方法都是通用步骤,根据环境对应修改cmake选项即可。

:编译时碰到的问题

系统默认的是gcc-9.4.0,编译时碰到的问题我列举在下面,

(1)file failed to open for reading (No such file or directory)  /home/mc/ocr/Paddle/NCCL_INCLUDE_DIR-NOTFOUND/nccl.h

参考:
https://github/PaddlePaddle/Paddle/pull/8540
https://github/PaddlePaddle/Paddle/issues/5035
https://docs.nvidia/deeplearning/nccl/install-guide/index.html

解决办法,安装libnccl

In the following commands, please replace <architecture> with your CPU architecture: x86_64, ppc64le, or sbsa, and replace <distro> with the Ubuntu version, for example ubuntu1604, ubuntu1804, or ubuntu2004.

说明:<distro>=ubunt2004<architecture>=x86_64

(1). Install the keys.
When installing using the network repo for Ubuntu 20.04/18.04:
---> sudo apt-key adv --fetch-keys https://developer.download.nvidia/compute/cuda/repos/<distro>/<architecture>/7fa2af80.pub

(2). Install the repository.
or the local NCCL repository:
---> sudo dpkg -i nccl-repo-<version>.deb
For the network repository:
---> sudo add-apt-repository "deb https://developer.download.nvidia/compute/cuda/repos/<distro>/<architecture>/ /"

(3). Update the APT database:
sudo apt update

(4). Install the libnccl2 package with APT. Additionally, if you need to compile applications with NCCL, you can install the libnccl-dev package as well:
Note: If you are using the network repository, the following command will upgrade CUDA to the latest version.
---> sudo apt install libnccl2 libnccl-dev
If you prefer to keep an older version of CUDA, specify a specific version, for example:
---> sudo apt install libnccl2=2.4.8-1+cuda11.4 libnccl-dev=2.4.8-1+cuda11.4
---> sudo apt install libnccl2=2.4.8-1+cuda10.0 libnccl-dev=2.4.8-1+cuda10.0
Refer to the download page for exact package versions.

https://developer.download.nvidia/compute/cuda/repos/ubuntu2004/x86_64/libnccl2_2.11.4-1+cuda11.4_amd64.deb

https://developer.download.nvidia/compute/cuda/repos/ubuntu2004/x86_64/libnccl-dev_2.11.4-1+cuda11.4_amd64.deb

具体有哪些包你可以直接到nvidia的官网上去查,找到合适你的cuda版本的那个包就可以了。

(2)Could NOT find PY_google.protobuf (missing: PY_GOOGLE.PROTOBUF) 

-- Could NOT find PY_google.protobuf (missing: PY_GOOGLE.PROTOBUF) 
CMake Error at cmake/python_module.cmake:27 (message):
  python module google.protobuf is not found
Call Stack (most recent call first):
  cmake/external/python.cmake:71 (find_python_module)
  cmake/third_party.cmake:243 (include)
  CMakeLists.txt:334 (include)

这是一个小问题,主要是我的系统上有多个版本的python,关闭掉那些不用的版本就可以了,比如,使用anaconda的话,就

~$ conda deactivate

(3)error: moving a local object in a return statement prevents copy elision [-Werror=pessimizing-move]

/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h:54:23: note: remove 'std::move' call
/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h: In instantiation of 'std::string paddle::string::format_string(const char*, ARGS&& ...) [with ARGS = {const char*}; std::string =
std::__cxx11::basic_string<char>]':
/opt/Paddle-1.8.3/paddle/fluid/framework/io/fs:115:71:   required from here
/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h:54:23: error: moving a local object in a return statement prevents copy elision [-Werror=pessimizing-move]
/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h:54:23: note: remove 'std::move' call
/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h: In instantiation of 'std::string paddle::string::format_string(const char*, ARGS&& ...) [with ARGS = {const char*, const char*, co
nst char*}; std::string = std::__cxx11::basic_string<char>]':
/opt/Paddle-1.8.3/paddle/fluid/framework/io/fs:345:78:   required from here
/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h:54:23: error: moving a local object in a return statement prevents copy elision [-Werror=pessimizing-move]
/opt/Paddle-1.8.3/paddle/fluid/string/string_helper.h:54:23: note: remove 'std::move' call
[  5%] Built target data_feed_proto

说明,这个问题和后面那个一样,是由编译器引起的,所以如果换编译器的话,下面的信息就不用理会。

如果只是针对这个问题,解决办法请参考这里,

std::move报错 · Issue #26878 · PaddlePaddle/Paddle · GitHub

我使用的这个指令,

find SRCROOT -type f -name CMakeLists.txt -exec sed -i -e '$aset(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=pessimizing-move")' {} \;

 (4)avx512fintrin.h(9146): error: identifier "__builtin_ia32_rndscaless_round" is undefined

[ 10%] Building CXX object paddle/fluid/operators/math/CMakeFiles/pooling.dir/pooling.o
/usr/lib/gcc/x86_64-linux-gnu/9/include/avx512fintrin.h(9146): error: identifier "__builtin_ia32_rndscaless_round" is undefined

/usr/lib/gcc/x86_64-linux-gnu/9/include/avx512fintrin.h(9155): error: identifier "__builtin_ia32_rndscalesd_round" is undefined

/usr/lib/gcc/x86_64-linux-gnu/9/include/avx512fintrin.h(14797): error: identifier "__builtin_ia32_rndscaless_round" is undefined

/usr/lib/gcc/x86_64-linux-gnu/9/include/avx512fintrin.h(14806): error: identifier "__builtin_ia32_rndscalesd_round" is undefined

/usr/lib/gcc/x86_64-linux-gnu/9/include/avx512dqintrin.h(1365): error: identifier "__builtin_ia32_fpclassss" is undefined

/usr/lib/gcc/x86_64-linux-gnu/9/include/avx512dqintrin.h(1372): error: identifier "__builtin_ia32_fpclasssd" is undefined

 说明:这个问题是直接导致我放弃gcc-9.4.0的主要原因。因为这些builtin函数我们无法自己定义,也不能在编译器外找到他们,加上我对编译器并不了解,所以也没敢去修改源码,最后直接换成了gcc-8.4.0才编译成功。

如何更换gcc编译版本请参考这里,

Linux中如何安装特定的gcc版本_高精度计算机视觉的博客-CSDN博客

本文结束 

更多推荐

Ubuntu 20.04 源码编译Paddle2.2.2