Building MindSpore Lite
Windows
macOS
Linux
iOS
Android
Environment Preparation
Intermediate
Expert
This chapter introduces how to quickly compile MindSpore Lite, which includes the following modules:
Modules in MindSpore Lite:
Module |
Support Platform |
Description |
---|---|---|
converter |
Linux, Windows |
Model Conversion Tool |
runtime(cpp, java) |
Linux, Windows, Android, iOS |
Model Inference Framework(Windows platform does not support java version runtime) |
benchmark |
Linux, Windows, Android |
Benchmarking Tool |
benchmark_train |
Linux, Android |
Performance and Accuracy Validation |
cropper |
Linux |
Static library crop tool for libmindspore-lite.a |
minddata |
Linux, Android |
Image Processing Library |
codegen |
Linux |
Model inference code generation tool |
obfuscator |
Linux |
Model Obfuscation Tool |
Linux Environment Compilation
Environment Requirements
The compilation environment supports Linux x86_64 only. Ubuntu 18.04.02 LTS is recommended.
Compilation dependencies of cpp:
GCC >= 7.3.0
CMake >= 3.18.3
Git >= 2.28.0
Android_NDK >= r20
Configure environment variables:
export ANDROID_NDK=NDK path
.
Additional compilation dependencies of Java:
Gradle >= 6.6.1
Configure environment variables:
export GRADLE_HOME=GRADLE path
.Add the bin directory to the PATH:
export PATH=${GRADLE_HOME}/bin:$PATH
.
OpenJDK >= 1.8
Configure environment variables:
export JAVA_HOME=JDK path
.Add the bin directory to the PATH:
export PATH=${JAVA_HOME}/bin:$PATH
.
-
Create a new directory, configure environment variables
export ANDROID_SDK_ROOT=new directory
.Download
SDK Tools
, create SDK throughsdkmanager
:./sdkmanager --sdk_root=${ANDROID_SDK_ROOT} "cmdline-tools;latest"
.Accept the license through
sdkmanager
under the${ANDROID_SDK_ROOT}
directory:yes | ./sdkmanager --licenses
.
You can also directly use the Docker compilation image that has been configured with the above dependencies.
Download the docker image:
docker pull swr.cn-south-1.myhuaweicloud.com/mindspore-build/mindspore-lite:ubuntu18.04.2-20210530
Create a container:
docker run -tid --net=host --name=docker01 swr.cn-south-1.myhuaweicloud.com/mindspore-build/mindspore-lite:ubuntu18.04.2-20210530
Enter the container:
docker exec -ti -u 0 docker01 bash
Compilation Options
The script build.sh
in the root directory of MindSpore can be used to compile MindSpore Lite.
Instructions for parameters of build.sh
Parameter |
Parameter Description |
Value Range |
Defaults |
---|---|---|---|
-I |
Selects an applicable architecture. |
arm64, arm32, or x86_64 |
None |
-A |
Compile AAR package (including arm32 and arm64). |
on, off |
off |
-d |
If this parameter is set, the debug version is compiled. Otherwise, the release version is compiled. |
None |
None |
-i |
If this parameter is set, incremental compilation is performed. Otherwise, full compilation is performed. |
None |
None |
-j[n] |
Sets the number of threads used during compilation. Otherwise, the number of threads is set to 8 by default. |
Integer |
8 |
-a |
Whether to enable AddressSanitizer |
on, off |
off |
When compiling the x86_64 version, if the JAVA_HOME environment variable is configured and Gradle is installed, the JAR package will be compiled at the same time.
When the
-I
parameter changes, such as-I x86_64
is converted to-I arm64
, adding-i
for parameter compilation does not take effect.When compiling the AAR package, the
-A on
parameter must be added, and there is no need to add the-I
parameter.
Module build compilation options
The construction of modules is controlled by environment variables. Users can control the modules built by compiling by declaring relevant environment variables. After the compilation option is modified, in order to make the option effective, the -i
parameter cannot be added for incremental compilation when compiling with the build.sh
script.
General module compilation options
Option |
Parameter Description |
Value Range |
Defaults |
---|---|---|---|
MSLITE_GPU_BACKEND |
Set the GPU backend, only opencl is valid when |
opencl, tensorrt, off |
opencl when |
MSLITE_ENABLE_NPU |
Whether to compile NPU operator, only valid when |
on, off |
off |
MSLITE_ENABLE_TRAIN |
Whether to compile the training version |
on, off |
on |
MSLITE_ENABLE_SSE |
Whether to enable SSE instruction set, only valid when |
on, off |
off |
MSLITE_ENABLE_AVX |
Whether to enable AVX instruction set, only valid when |
on, off |
off |
MSLITE_ENABLE_CONVERTER |
Whether to compile the model conversion tool, only valid when |
on, off |
on |
MSLITE_ENABLE_TOOLS |
Whether to compile supporting tools |
on, off |
on |
MSLITE_ENABLE_TESTCASES |
Whether to compile test cases |
on, off |
off |
For TensorRT and NPU compilation environment configuration, refer to Application Specific Integrated Circuit Integration Instructions.
Runtime feature compilation options
If the user is sensitive to the package size of the framework, the following options can be configured to reduce the package size by reducing the function of the runtime model reasoning framework. Then, the user can further reduce the package size by operator reduction through the reduction tool or download.
Option |
Parameter Description |
Value Range |
Defaults |
---|---|---|---|
MSLITE_ENABLE_STRING_KERNEL |
Whether to support string data reasoning model, such as smart_reply.tflite |
on,off |
on |
MSLITE_ENABLE_CONTROLFLOW |
Whether to support control flow model |
on,off |
on |
MSLITE_ENABLE_WEIGHT_DECODE |
Whether to support weight quantitative model |
on,off |
on |
MSLITE_ENABLE_CUSTOM_KERNEL |
Whether to support southbound operator registration |
on,off |
on |
MSLITE_ENABLE_DELEGATE |
Whether to support Delegate mechanism |
on,off |
on |
MSLITE_ENABLE_V0 |
Whether to compatible with models exported before 1.1.0 |
on,off |
on |
Since the implementation of NPU and TensorRT depends on the Delegate mechanism, the Delegate mechanism cannot be turned off when using NPU or TensorRT. If the Delegate mechanism is turned off, the related functions must also be turned off.
Compilation Example
First, download source code from the MindSpore code repository.
git clone https://gitee.com/mindspore/mindspore.git -b r1.5
Then, run the following commands in the root directory of the source code to compile MindSpore Lite of different versions:
Compile the x86_64 architecture version and set the number of threads at the same time.
bash build.sh -I x86_64 -j32
Compile the ARM64 architecture version without compiling training-related code.
export MSLITE_ENABLE_TRAIN=off bash build.sh -I arm64 -j32
Or modify
mindspore/lite/CMakeLists.txt
to set MSLITE_ENABLE_TRAIN to off and execute the command:bash build.sh -I arm64 -j32
Compile the AAR package containing aarch64 and aarch32.
bash build.sh -A on -j32
Finally, the following files will be generated in the output/
directory:
mindspore-lite-{version}-{os}-{arch}.tar.gz
: Contains runtime, and related tools.mindspore-lite-maven-{version}.zip
: The AAR package which contains runtime (java).
version: Version of the output, consistent with that of the MindSpore.
os: Operating system on which the output will be deployed.
arch: System architecture on which the output will be deployed.
Directory Structure
When the compilation option is
-I x86_64
:mindspore-lite-{version}-linux-x64 ├── runtime │ ├── include │ ├── lib │ │ ├── libminddata-lite.a # Static library of image processing │ │ ├── libminddata-lite.so # Dynamic library of image processing │ │ ├── libmindspore-lite.a # Static library of inference framework in MindSpore Lite │ │ ├── libmindspore-lite-jni.so # Dynamic library of inference framework jni in MindSpore Lite │ │ ├── libmindspore-lite.so # Dynamic library of inference framework in MindSpore Lite │ │ ├── libmindspore-lite-train.a # Static library of training framework in MindSpore Lite │ │ ├── libmindspore-lite-train.so # Dynamic library of training framework in MindSpore Lite │ │ ├── libmsdeobfuscator-lite.so # The files of obfuscated model loading dynamic library, need to open the `ENABLE_MODEL_OBF` option. │ │ └── mindspore-lite-java.jar # Jar of inference framework in MindSpore Lite │ └── third_party │ └── libjpeg-turbo └── tools ├── benchmark # Benchmarking tool ├── benchmark_train # Training model benchmark tool ├── codegen # Code generation tool ├── converter # Model conversion tool ├── obfuscator # Model obfuscation tool └── cropper # Static library crop tool
When the compilation option is
-I arm64
or-I arm32
:mindspore-lite-{version}-android-{arch} ├── runtime │ ├── include │ ├── lib │ │ ├── libminddata-lite.a # Static library of image processing │ │ ├── libminddata-lite.so # Dynamic library of image processing │ │ ├── libmindspore-lite.a # Static library of inference framework in MindSpore Lite │ │ ├── libmindspore-lite.so # Dynamic library of inference framework in MindSpore Lite │ │ ├── libmindspore-lite-train.a # Static library of training framework in MindSpore Lite │ │ └── libmindspore-lite-train.so # Dynamic library of training framework in MindSpore Lite │ │ └── libmsdeobfuscator-lite.so # The files of obfuscated model loading dynamic library, need to open the `ENABLE_MODEL_OBF` option. │ └── third_party │ ├── hiai_ddk │ └── libjpeg-turbo └── tools ├── benchmark # Benchmarking tool ├── benchmark_train # Training model benchmark tool └── codegen # Code generation tool
When the compilation option is
-A on
:mindspore-lite-maven-{version} └── mindspore └── mindspore-lite └── {version} └── mindspore-lite-{version}.aar # MindSpore Lite runtime aar
Windows Environment Compilation
Environment Requirements
System environment: Windows 7, Windows 10; 64-bit.
MinGW compilation dependencies:
CMake >= 3.18.3
Compile 64-bit: MinGW-W64 x86_64 = GCC-7.3.0
Compile 32-bit: MinGW-W64 i686 = GCC-7.3.0
Visual Studio compilation dependencies:
Visual Studio = 2017, cmake is included.
Compile 64-bit: Enter the start menu, click “x64 Native Tools Command Prompt for VS 2017”, or open the cmd window and execute
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Profession\VC\Auxiliary\Build\vcvars64.bat"
.Compile 32-bit: Enter the start menu, click “x64_x86 Cross Tools Command Prompt for VS 2017”, or open the cmd window and execute
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Profession\VC\Auxiliary\Build\vcvarsamd64_x86.bat"
.
Compilation Options
The script build.bat
in the root directory of MindSpore can be used to compile MindSpore Lite.
The compilation parameter of build.bat
Parameter |
Parameter Description |
Mandatory or Not |
---|---|---|
lite |
Set this parameter to compile the MindSpore Lite project. |
Yes |
[n] |
Set the number of threads used during compilation, otherwise the default is set to 6 threads. |
No |
The options of mindspore/lite/CMakeLists.txt
Option |
Parameter Description |
Value Range |
Defaults |
---|---|---|---|
MSLITE_ENABLE_SSE |
Whether to enable SSE instruction set |
on, off |
off |
MSLITE_ENABLE_AVX |
Whether to enable AVX instruction set (This option does not currently support the Visual Studio compiler) |
on, off |
off |
MSLITE_ENABLE_CONVERTER |
Whether to compile the model conversion tool (This option does not currently support the Visual Studio compiler) |
on, off |
on |
MSLITE_ENABLE_TOOLS |
Whether to compile supporting tools |
on, off |
on |
MSLITE_ENABLE_TESTCASES |
Whether to compile test cases |
on, off |
off |
The above options can be modified by setting the environment variable with the same name or the file
mindspore/lite/CMakeLists.txt
.
Compilation Example
First, use the git tool to download the source code from the MindSpore code repository.
git clone https://gitee.com/mindspore/mindspore.git -b r1.5
Then, use the cmd tool to compile MindSpore Lite in the root directory of the source code and execute the following commands.
Turn on SSE instruction set optimization and compile with 8 threads.
set MSLITE_ENABLE_SSE=on
call build.bat lite 8
Finally, the following files will be generated in the output/
directory:
mindspore-lite-{version}-win-x64.zip
: Contains model inference framework and related tool.
version: Version of the output, consistent with that of the MindSpore.
Directory Structure
When the compiler is MinGW:
mindspore-lite-{version}-win-x64 ├── runtime │ ├── include │ └── lib │ ├── libgcc_s_seh-1.dll # Dynamic library of MinGW │ ├── libmindspore-lite.a # Static library of inference framework in MindSpore Lite │ ├── libmindspore-lite.dll # Dynamic library of inference framework in MindSpore Lite │ ├── libmindspore-lite.dll.a # Link file of dynamic library of inference framework in MindSpore Lite │ ├── libssp-0.dll # Dynamic library of MinGW │ ├── libstdc++-6.dll # Dynamic library of MinGW │ └── libwinpthread-1.dll # Dynamic library of MinGW └── tools ├── benchmark # Benchmarking tool └── converter # Model conversion tool
When the compiler is Visual Studio:
mindspore-lite-{version}-win-x64 ├── runtime │ ├── include │ └── lib │ ├── libmindspore-lite.dll # Dynamic library of inference framework in MindSpore Lite │ ├── libmindspore-lite.dll.lib # Import library of dynamic library of inference framework in MindSpore Lite │ └── libmindspore-lite.lib # Static library of inference framework in MindSpore Lite └── tools └── benchmark # Benchmarking tool
When linking the static library compiled by MinGW, you need to add
-Wl, --whole-archive mindspore-lite -Wl, --no-whole-archive
in the link options.When linking the static library compiled by Visual Studio, you need to add
/WHOLEARCHIVE:libmindspore-lite.lib
in “Property Pages -> Linker -> Command Line -> Additional Options”.When using the Visual Studio compiler, std::ios::binary must be added to read the model stream, otherwise the problem of incomplete reading of the model file will occur.
Currently, MindSpore Lite is not supported on Windows.
macOS Environment Compilation
Environment Requirements
System environment: macOS 10.15.4 and above ; 64-bit.
Compilation dependencies are:
The compilation script will execute
git clone
to obtain the code of the third-party dependent libraries.
Compilation Options
The script build.sh
in the root directory of MindSpore can be used to compile MindSpore Lite.
The compilation parameter of build.sh
Parameter |
Parameter Description |
Value Range |
Defaults |
---|---|---|---|
-I |
Selects an applicable architecture. |
arm64, arm32 |
None |
-j[n] |
Sets the number of threads used during compilation. Otherwise, the number of threads is set to 8 by default. |
Integer |
8 |
Compilation Example
First, use the git tool to download the source code from the MindSpore code repository.
git clone https://gitee.com/mindspore/mindspore.git -b r1.5
Then, use the cmd tool to compile MindSpore Lite in the root directory of the source code and execute the following commands.
Compile the ARM64 architecture version
bash build.sh -I arm64 -j8
Compile the ARM32 architecture version
bash build.sh -I arm32 -j8
Finally, the following files will be generated in the output/
directory:
mindspore-lite-{version}-{os}-{arch}.tar.gz
: Contains model inference framework.
version: Version of the output, consistent with that of the MindSpore.
os: Operating system on which the output will be deployed.
arch: System architecture on which the output will be deployed.
Directory Structure
mindspore-lite.framework
└── runtime
├── Headers # 推理框架头文件
├── Info.plist # 配置文件
└── mindspore-lite # 静态库
Currently, MindSpore Lite Train and converter are not supported on macOS.