Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

How to build mobile inference library with minimum size #21

Merged
merged 5 commits into from
Nov 10, 2017

Conversation

hedaoyuan
Copy link
Collaborator

No description provided.

@hedaoyuan hedaoyuan requested review from NHZlX and Xreki November 1, 2017 04:02
@NHZlX
Copy link
Contributor

NHZlX commented Nov 1, 2017

Merge the updated develop branch

@@ -0,0 +1,27 @@
# Build mobile inference library with minimum size

In many mobile applications, the size of the executable program will have some requirements.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the size of the executable program will have some requirements -> there usually are some limitations to the size of libraries.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

# Build mobile inference library with minimum size

In many mobile applications, the size of the executable program will have some requirements.
Here we explore how to compile the inference library with minimum size.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with minimum size -> for minimum size

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


**Note:**
In the original PaddlePaddle, all computationally relevant code is implemented in Matrix.cpp and BaseMatrix.cu,
this causes the compiled Matrix.o and BaseMatrix.o files to be large and can not be split.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this causes large compiled Matrix.o and BaseMatrix.o files which cannot be split

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

In the original PaddlePaddle, all computationally relevant code is implemented in Matrix.cpp and BaseMatrix.cu,
this causes the compiled Matrix.o and BaseMatrix.o files to be large and 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 has some redundancy. These all will lead to the size of inference library larger.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has some redundancy -> is redundant
And I suggest to move These all will lead to the size of inference library larger. to line 7, as Three reasons lead to the large size of PaddlePaddle's inference library:, then list the three reasons.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Maybe not only three. I think we can fix this later.

## 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.

- MinSizeRel: By specify the build type(CMAKE_BUILD_TYPE) as MinSizeRel, the -Os option is used by the compiler during the build for minimum size code.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By specify the build type(CMAKE_BUILD_TYPE) as MinSizeRel -> by specifing the build type (CMAKE_BUILD_TYPE) to MinSizeRel

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- protobuf-lite: With the `--cpp_out lite` option, 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-archive: In `libpaddle_capi_layers.a` contains all the object file of the layers, need use the `--whole-archive` option to ensure that all layers be linked to the executable file.
But don't forget to use `-no-whole-archive after` after `libpaddle_capi_layers.a`, avoid this option affect other libraries.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduntant "after": -no-whole-archive after -> -no-whole-archive

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

## 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.

- MinSizeRel: By specify the build type(CMAKE_BUILD_TYPE) as MinSizeRel, the -Os option is used by the compiler during the build for minimum size code.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MinSizeRel: -> Set build type to MinSizeRel:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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.

- MinSizeRel: By specify the build type(CMAKE_BUILD_TYPE) as MinSizeRel, the -Os option is used by the compiler during the build for minimum size code.
- protobuf-lite: With the `--cpp_out lite` option, the generated proto configuration rely on MessageLite instead of Message,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protobuf-lite: -> Use protobuf-lite instead of protobuf:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- MinSizeRel: By specify the build type(CMAKE_BUILD_TYPE) as MinSizeRel, the -Os option is used by the compiler during the build for minimum size code.
- protobuf-lite: With the `--cpp_out lite` option, 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-archive: In `libpaddle_capi_layers.a` contains all the object file of the layers, need use the `--whole-archive` option to ensure that all layers be linked to the executable file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whole-archive: -> How to link static libraries:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- whole-archive: In `libpaddle_capi_layers.a` contains all the object file of the layers, need use the `--whole-archive` option to ensure that all layers be linked to the executable file.
But don't forget to use `-no-whole-archive after` after `libpaddle_capi_layers.a`, avoid this option affect other libraries.
[Here's an example](https://github.com/PaddlePaddle/Mobile/blob/develop/benchmark/tool/C/CMakeLists.txt#L41)
- Shared library: When building a shared library by `libpaddle_capi_layers.a` and `libpaddle_capi_engine.a`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared library: -> How to generate shared library use PaddlePaddle's static libraries
Perhaps it is still not precise...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@hedaoyuan hedaoyuan changed the title How to build mobile inference library with minimun size How to build mobile inference library with minimum size Nov 8, 2017
@hedaoyuan hedaoyuan merged commit 285ccba into PaddlePaddle:develop Nov 10, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants