Build PaddlePaddle for iOS¶
This tutorial will walk you through cross compiling the PaddlePaddle library for iOS from the source in MacOS.
Apple provides Xcode for cross-compiling and IDE for iOS development. Download from App store or here. To verify your installation, run command as follows
$ xcodebuild -version Xcode 9.0 Build version 9A235
PaddlePaddle provides cross-compiling toolchain configuration documentation cmake/cross_compiling/ios.cmake, which has some default settings for frequently used compilers.
There are some mandatory environment variables need to be set before cross compiling PaddlePaddle for iOS:
CMAKE_SYSTEM_NAME, CMake compiling target platform name, has to be
iOS. PaddlePaddle CMake will compile all the third party dependencies and enforce some parameters (
WITH_RDMA=OFF) when this variable is set with value
WITH_C_API, Whether to compile inference C-API library, has to be
ON, since C-API is the only supported interface for inferencing in iOS.
WITH_SWIG_PY, has to be
OFF. It’s not supported to inference or train via swig in iOS.
Optional environment variables for iOS are:
OS, build targets ARM-based physical devices like iPhone or iPad.
SIMULATOR, build targets x86 architecture simulators.
IOS_ARCH, target architecture. By default, all architecture types will be compiled. If you need to specify the architecture to compile for, please find valid values for different
IOS_PLATFORMsettings from the table below:
IOS_PLATFORM IOS_ARCH OS armv7, armv7s, arm64 SIMULATOR i386, x86_64
IOS_DEPLOYMENT_TARGET, minimum iOS version to deployment,
IOS_ENABLE_BITCODE, whether to enable Bitcode, values can be
IOS_USE_VECLIB_FOR_BLAS, whether to use vecLib framework for BLAS computing. values can be
IOS_DEVELOPMENT_ROOT, the path to
Developerdirectory, can be explicitly set with your
/path/to/platform/Developer. If left blank, PaddlePaddle will automatically pick the Xcode corresponding
Developerdirectory based on your
IOS_SDK_ROOT, the path to
SDKroot, can be explicitly set with your
/path/to/platform/Developer/SDKs/SDK. if left black, PaddlePaddle will pick the latest SDK in the directory of
USE_EIGEN_FOR_BLAS, whether to use Eigen for matrix computing. effective when
IOS_USE_VECLIB_FOR_BLAS=OFF. Values can be
HOST_C/CXX_COMPILER, host C/C++ compiler. Uses value from environment variable
CC/CXXby default or
some typical cmake configurations:
cmake -DCMAKE_SYSTEM_NAME=iOS \ -DIOS_PLATFORM=OS \ -DIOS_ARCH="armv7;arm64" \ -DIOS_ENABLE_BITCODE=ON \ -DIOS_USE_VECLIB_FOR_BLAS=ON \ -DCMAKE_INSTALL_PREFIX=your/path/to/install \ -DWITH_C_API=ON \ -DWITH_TESTING=OFF \ -DWITH_SWIG_PY=OFF \ ..
cmake -DCMAKE_SYSTEM_NAME=iOS \ -DIOS_PLATFORM=SIMULATOR \ -DIOS_ARCH="x86_64" \ -DIOS_USE_VECLIB_FOR_BLAS=ON \ -DCMAKE_INSTALL_PREFIX=your/path/to/install \ -DWITH_C_API=ON \ -DWITH_TESTING=OFF \ -DWITH_SWIG_PY=OFF \ ..
You can set other compiling parameters for your own need. I.E. if you are trying to minimize the library size, set
MinSizeRel; or if the performance is your concern, set
Release. You can even manipulate the PaddlePaddle compiling procedure by manually set
TIPS for a better performance:
Build and install¶
After CMake, run following commands, PaddlePaddle will download the compile 3rd party dependencies, compile and install PaddlePaddle inference library.
$ make $ make install
Please Note: if you compiled PaddlePaddle in the source directory for other platforms, do remove
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
include, contains all the C-API header files.
lib, contains PaddlePaddle C-API static library.
third_partycontains all the 3rd party libraries.
Please note: if PaddlePaddle library need to support both physical devices and simulators, you will need to compile correspondingly, then merge fat library with
Now you will have PaddlePaddle library compiled and installed, the fat library can be used in deep learning related iOS APPs. Please refer to C-API documentation for usage guides.