We can run the Docker image we just created to build the inference library of PaddlePaddle for Android using the command below:
...
...
@@ -47,7 +56,7 @@ The Docker image accepts two arguments `ANDROID_ABI` and `ANDROID_API`:
</tr>
<tr class="row-odd">
<td>ANDROID_API</td>
<td>>= 21</td>
<td>>= 16</td>
<td>21</td>
</tr>
</tbody>
...
...
@@ -93,15 +102,13 @@ Android NDK includes everything we need to build the [*standalone toolchain*](ht
The generated standalone toolchain will be in `your/path/to/arm64_standalone_toolchain`.
**Please be aware that the minimum level of Android API required by PaddlePaddle is 21.**
### Cross-Compiling Arguments
CMake supports [choosing the toolchain](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling). PaddlePaddle provides [`android.cmake`](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake), which configures the Android cross-compiling toolchain for CMake. `android.cmake` is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses [the official version](https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling).
Some other CMake arguments you need to know:
- `CMAKE_SYSTEM_NAME` must be `Android`. This tells PaddlePaddle's CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like `WITH_GPU=OFF`, `WITH_AVX=OFF`, `WITH_PYTHON=OFF`, and `WITH_RDMA=OFF`.
- `CMAKE_SYSTEM_NAME` must be `Android`. This tells PaddlePaddle's CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like `WITH_GPU=OFF`, `WITH_AVX=OFF`, `WITH_PYTHON=OFF`, `WITH_RDMA=OFF`, `WITH_MKL=OFF` and `WITH_GOLANG=OFF`.
- `WITH_C_API` must be `ON`, to build the C-based inference library for Android.
- `WITH_SWIG_PY` must be `OFF` because the Android platform doesn't support SWIG-based API.
...
...
@@ -123,7 +130,7 @@ Some Android-specific arguments:
Other useful arguments:
- `USE_EIGEN_FOR_BLAS`: indicates if using Eigen. Could be `ON` or `OFF`, defaults to `OFF`.
- `HOST_C/CXX_COMPILER`: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable `CC`, or `cc`.
- `HOST_C/CXX_COMPILER`: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable `CC/C++`, or `cc/c++`.
Some frequent configurations for your reference:
...
...
@@ -158,6 +165,7 @@ There are some other arguments you might want to configure.
- `CMAKE_BUILD_TYPE-Release` optimizes the runtime performance.
Our own tip for performance optimization to use clang and Eigen or OpenBLAS:
- `CMAKE_BUILD_TYPE=Release`
- `ANDROID_TOOLCHAIN=clang`
- `USE_EIGEN_BLAS=ON` for `armeabi-v7a`, or `USE_EIGEN_FOR_BLAS=OFF` for `arm64-v8a`.
This tutorial will walk you through cross compiling the PaddlePaddle library for iOS from the source in MacOS.
...
...
@@ -98,7 +98,7 @@ You can set other compiling parameters for your own need. I.E. if you are trying
- set `CMAKE_BUILD_TYPE` with `Release`
- set `IOS_USE_VECLIB_FOR_BLAS` with `ON`
## Compile and install
## Build and install
After CMake, run following commands, PaddlePaddle will download the compile 3rd party dependencies, compile and install PaddlePaddle inference library.
...
...
@@ -109,7 +109,7 @@ $ make install
Please Note: if you compiled PaddlePaddle in the source directory for other platforms, do remove `third_party` and `build` directory within the source with `rm -rf` to ensure that all the 3rd party libraries dependencies and PaddlePaddle is newly compiled with current CMake configuration.
`your/path/to/install` directory will have following directories after `compile` and `install`:
`your/path/to/install` directory will have following directories after `make install`:
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_android_en.html">Build PaddlePaddle for Android</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_ios_en.html">PaddlePaddle Compiling Guide for iOS</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_ios_en.html">Build PaddlePaddle for iOS</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_raspberry_en.html">Build PaddlePaddle for Raspberry Pi</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_android_en.html">Build PaddlePaddle for Android</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_ios_en.html">PaddlePaddle Compiling Guide for iOS</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_ios_en.html">Build PaddlePaddle for iOS</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../../../../../mobile/cross_compiling_for_raspberry_en.html">Build PaddlePaddle for Raspberry Pi</a></li>
<spanid="build-paddlepaddle-for-android"></span><h1>Build PaddlePaddle for Android<aclass="headerlink"href="#build-paddlepaddle-for-android"title="Permalink to this headline">¶</a></h1>
<p>There are two approaches to build PaddlePaddle for Android: using Docker and on Linux without Docker.</p>
<p>There are two approaches to build PaddlePaddle for Android:</p>
<ulclass="simple">
<li><aclass="reference external"href="#cross-compiling-using-docker">Cross-Compiling Using Docker</a></li>
<li><aclass="reference external"href="#cross-compiling-on-linux">Cross-Compiling on Linux</a></li>
<spanid="cross-compiling-using-docker"></span><h2>Cross-Compiling Using Docker<aclass="headerlink"href="#cross-compiling-using-docker"title="Permalink to this headline">¶</a></h2>
<p>Docker-based cross-compiling is the recommended approach because Docker runs on all major operating systems, including Linux, Mac OS X, and Windows.</p>
<spanid="build-the-inference-library"></span><h3>Build the Inference Library<aclass="headerlink"href="#build-the-inference-library"title="Permalink to this headline">¶</a></h3>
<p>The generated standalone toolchain will be in <codeclass="docutils literal"><spanclass="pre">your/path/to/arm64_standalone_toolchain</span></code>.</p>
</li>
</ul>
<p><strong>Please be aware that the minimum level of Android API required by PaddlePaddle is 21.</strong></p>
<spanid="cross-compiling-arguments"></span><h3>Cross-Compiling Arguments<aclass="headerlink"href="#cross-compiling-arguments"title="Permalink to this headline">¶</a></h3>
<p>CMake supports <aclass="reference external"href="https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling">choosing the toolchain</a>. PaddlePaddle provides <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake"><codeclass="docutils literal"><spanclass="pre">android.cmake</span></code></a>, which configures the Android cross-compiling toolchain for CMake. <codeclass="docutils literal"><spanclass="pre">android.cmake</span></code> is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses <aclass="reference external"href="https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling">the official version</a>.</p>
<p>Some other CMake arguments you need to know:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">CMAKE_SYSTEM_NAME</span></code> must be <codeclass="docutils literal"><spanclass="pre">Android</span></code>. This tells PaddlePaddle’s CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like <codeclass="docutils literal"><spanclass="pre">WITH_GPU=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_AVX=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_PYTHON=OFF</span></code>, and <codeclass="docutils literal"><spanclass="pre">WITH_RDMA=OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">CMAKE_SYSTEM_NAME</span></code> must be <codeclass="docutils literal"><spanclass="pre">Android</span></code>. This tells PaddlePaddle’s CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like <codeclass="docutils literal"><spanclass="pre">WITH_GPU=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_AVX=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_PYTHON=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_RDMA=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_MKL=OFF</span></code> and <codeclass="docutils literal"><spanclass="pre">WITH_GOLANG=OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">WITH_C_API</span></code> must be <codeclass="docutils literal"><spanclass="pre">ON</span></code>, to build the C-based inference library for Android.</li>
<li><codeclass="docutils literal"><spanclass="pre">WITH_SWIG_PY</span></code> must be <codeclass="docutils literal"><spanclass="pre">OFF</span></code> because the Android platform doesn’t support SWIG-based API.</li>
<li><codeclass="docutils literal"><spanclass="pre">USE_EIGEN_FOR_BLAS</span></code>: indicates if using Eigen. Could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, defaults to <codeclass="docutils literal"><spanclass="pre">OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">HOST_C/CXX_COMPILER</span></code>: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable <codeclass="docutils literal"><spanclass="pre">CC</span></code>, or <codeclass="docutils literal"><spanclass="pre">cc</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">HOST_C/CXX_COMPILER</span></code>: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable <codeclass="docutils literal"><spanclass="pre">CC/C++</span></code>, or <codeclass="docutils literal"><spanclass="pre">cc/c++</span></code>.</li>
</ul>
<p>Some frequent configurations for your reference:</p>
<spanid="paddlepaddle-compiling-guide-for-ios"></span><h1>PaddlePaddle Compiling Guide for iOS<aclass="headerlink"href="#paddlepaddle-compiling-guide-for-ios"title="Permalink to this headline">¶</a></h1>
<spanid="build-paddlepaddle-for-ios"></span><h1>Build PaddlePaddle for iOS<aclass="headerlink"href="#build-paddlepaddle-for-ios"title="Permalink to this headline">¶</a></h1>
<p>This tutorial will walk you through cross compiling the PaddlePaddle library for iOS from the source in MacOS.</p>
<divclass="section"id="preparation">
<spanid="preparation"></span><h2>Preparation<aclass="headerlink"href="#preparation"title="Permalink to this headline">¶</a></h2>
...
...
@@ -312,15 +312,15 @@ Build version 9A235
<li>set <codeclass="docutils literal"><spanclass="pre">IOS_USE_VECLIB_FOR_BLAS</span></code> with <codeclass="docutils literal"><spanclass="pre">ON</span></code></li>
</ul>
</div>
<divclass="section"id="compile-and-install">
<spanid="compile-and-install"></span><h2>Compile and install<aclass="headerlink"href="#compile-and-install"title="Permalink to this headline">¶</a></h2>
<divclass="section"id="build-and-install">
<spanid="build-and-install"></span><h2>Build and install<aclass="headerlink"href="#build-and-install"title="Permalink to this headline">¶</a></h2>
<p>After CMake, run following commands, PaddlePaddle will download the compile 3rd party dependencies, compile and install PaddlePaddle inference library.</p>
<divclass="highlight-default"><divclass="highlight"><pre><span></span>$ make
$ make install
</pre></div>
</div>
<p>Please Note: if you compiled PaddlePaddle in the source directory for other platforms, do remove <codeclass="docutils literal"><spanclass="pre">third_party</span></code> and <codeclass="docutils literal"><spanclass="pre">build</span></code> directory within the source with <codeclass="docutils literal"><spanclass="pre">rm</span><spanclass="pre">-rf</span></code> to ensure that all the 3rd party libraries dependencies and PaddlePaddle is newly compiled with current CMake configuration.</p>
<p><codeclass="docutils literal"><spanclass="pre">your/path/to/install</span></code> directory will have following directories after <codeclass="docutils literal"><spanclass="pre">compile</span></code> and <codeclass="docutils literal"><spanclass="pre">install</span></code>:</p>
<p><codeclass="docutils literal"><spanclass="pre">your/path/to/install</span></code> directory will have following directories after <codeclass="docutils literal"><spanclass="pre">make</span><spanclass="pre">install</span></code>:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">include</span></code>, contains all the C-API header files.</li>
执行上述`docker run`命令时,容器默认执行[paddle/scripts/docker/build_android.sh](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh)脚本。该脚本中记录了交叉编译Android版PaddlePaddle库常用的CMake配置,并且会根据`ANDROID_ABI`和`ANDROID_API`自动构建独立工具链、进行编译和安装。由于arm64架构要求Android API不小于21。因此当`ANDROID_ABI=arm64-v8a`,`ANDROID_API<21`时,Docker容器中将默认使用`Android API 21`的编译工具链。用户可以参考下文**配置交叉编译参数**章节,根据个人的需求修改定制Docker容器所执行的脚本。编译安装结束之后,PaddlePaddle的C-API库将被安装到`$PWD/install_android`目录,所依赖的第三方库同时也被安装到`$PWD/install_android/third_party`目录。
执行上述`docker run`命令时,容器默认执行[paddle/scripts/docker/build_android.sh](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh)脚本。该脚本中记录了交叉编译Android版PaddlePaddle库常用的CMake配置,并且会根据`ANDROID_ABI`和`ANDROID_API`自动构建独立工具链、进行编译和安装。由于arm64架构要求Android API不小于21。因此当`ANDROID_ABI=arm64-v8a`,`ANDROID_API<21`时,Docker容器中将默认使用`Android API 21`的编译工具链。用户可以参考下文[配置交叉编译参数](#配置交叉编译参数)章节,根据个人的需求修改定制Docker容器所执行的脚本。编译安装结束之后,PaddlePaddle的C-API库将被安装到`$PWD/install_android`目录,所依赖的第三方库同时也被安装到`$PWD/install_android/third_party`目录。