Build mobile inference library with minimum size
In the mobile application, there usually are some limitations to the size of an executable file. Here we explore how to compile the inference library for minimum size.
In the original PaddlePaddle, all computationally relevant code is implemented in Matrix.cpp and BaseMatrix.cu, this causes the large compiled Matrix.o and BaseMatrix.o files which can not be split. The module of Layer can be split, but the Layer forward and backward computing is included in the same file. The configuration definition in proto is redundant. These all will lead to the size of inference library larger.
The new PaddlePaddle is being refactored, the calculation is based on Operator, the configuration description in proto is simplified. These will bring a good optimization to the inference library size.
How to build with minimum size
Here we mainly introduce some optimization in the compilation process to reduce the size of the inference library. These methods are also used in the refactored PaddlePaddle.
- Compile the code with
-Osoption: By specifying the build type(CMAKE_BUILD_TYPE) to MinSizeRel, the
-Osoption is used by the compiler during the build for minimum size code.
- Use protobuf-lite instead of protobuf: With the
--cpp_out liteoption, the generated proto configuration rely on MessageLite instead of Message, so only libprotobuf-lite is needed for the link, and the size of the final executable file can be reduced.
--whole-archivelink option: In
libpaddle_capi_layers.acontains all the object file of the layers, need use the
--whole-archiveoption to ensure that all layers be linked to the executable file. But don't forget to use
libpaddle_capi_layers.a, avoid this option affect other libraries. Here's an example
- Remove useless symbols in the shared library: When building a shared library by
libpaddle_capi_engine.a, you can remove the useless export symbols with the
--version-scriptoption to reduce the size of the
.dynstrsections. Here's an example