使用 CMake 构建¶
ExecuTorch 使用 CMake 作为其主要构建系统。 即使您不直接使用 CMake,CMake 也可以生成其他格式的脚本,例如 Make、Ninja 或 Xcode。有关详细信息,请参阅 cmake-generators(7)。
CMake 构建系统生成的目标¶
ExecuTorch 的 CMake 构建系统涵盖了嵌入式系统用户可能用到的运行时组件。
libexecutorch.a: ExecuTorch运行时的核心部分。不包含任何操作符/内核定义或后端定义。libportable_kernels.a: ATen兼容操作符的实现,遵循//kernels/portable/functions.yaml中的签名。libportable_kernels_bindings.a: 生成的代码用于将libportable_kernels.a的内容注册到运行时。注意:必须使用类似
-Wl,-force_load或-Wl,--whole-archive的标志将其链接到您的应用程序中。它包含在加载时自动注册内核的功能,但链接器通常会默认修剪这些功能,因为它们没有直接调用。
executor_runner: 一个运行.pte程序文件的示例工具,使用所有1值作为输入,并将输出打印到标准输出。它与libportable_kernels.a链接,因此程序可以使用它实现的任何操作符。
一次设置以准备CMake构建¶
在使用CMake在您的机器上进行构建之前,请按照以下步骤准备工具。
如果您系统的 python3 版本低于 3.11:
运行
pip install tomli
安装 CMake 版本 3.19 或更高版本:
运行
conda install cmake或pip install cmake。
配置CMake构建¶
克隆或拉取上游仓库后,请按照这些步骤操作,因为构建依赖可能已经更改。
# cd to the root of the executorch repo
cd executorch
# Clean and configure the CMake build system. It's good practice to do this
# whenever cloning or pulling the upstream repo.
./install_requirements.sh --clean
(mkdir cmake-out && cd cmake-out && cmake ..)
一旦完成这一步,除非您再次从上游仓库拉取代码或修改任何与CMake相关的文件,否则不需要再重复进行。
CMake 构建选项¶
发布版构建提供了旨在提高性能并减少二进制文件大小的优化。它禁用了程序验证和executorch日志记录,并添加了优化标志。
-DCMAKE_BUILD_TYPE=Release
为了进一步优化发布版本的大小,请同时使用:
-DCMAKE_BUILD_TYPE=Release \
-DOPTIMIZE_SIZE=ON
构建运行时组件¶
构建所有目标
# cd to the root of the executorch repo
cd executorch
# Build using the configuration that you previously generated under the
# `cmake-out` directory.
#
# NOTE: The `-j` argument specifies how many jobs/processes to use when
# building, and tends to speed up the build significantly. It's typical to use
# "core count + 1" as the `-j` value.
cmake --build cmake-out -j9
使用示例应用 executor_runner 执行一个 .pte 文件¶
首先,使用设置 ExecuTorch中描述的说明生成一个add.pte或其他 ExecuTorch 程序文件。
然后将其传递给命令行工具:
./cmake-out/executor_runner --model_path path/to/add.pte
如果运行成功,你应该会看到消息“模型执行成功”,随后是输出值。
I 00:00:00.000526 executorch:executor_runner.cpp:82] Model file add.pte is loaded.
I 00:00:00.000595 executorch:executor_runner.cpp:91] Using method forward
I 00:00:00.000612 executorch:executor_runner.cpp:138] Setting up planned buffer 0, size 48.
I 00:00:00.000669 executorch:executor_runner.cpp:161] Method loaded.
I 00:00:00.000685 executorch:executor_runner.cpp:171] Inputs prepared.
I 00:00:00.000764 executorch:executor_runner.cpp:180] Model executed successfully.
I 00:00:00.000770 executorch:executor_runner.cpp:184] 1 outputs:
Output 0: tensor(sizes=[1], [2.])
交叉编译¶
以下是如何为Android和iOS进行交叉编译的指令。
Android¶
先决条件: Android NDK,请选择以下之一:
假设已安装Android NDK,请运行:
# Run the following lines from the `executorch/` folder
./install_requirements.sh --clean
mkdir cmake-android-out && cd cmake-android-out
# point -DCMAKE_TOOLCHAIN_FILE to the location where ndk is installed
cmake -DCMAKE_TOOLCHAIN_FILE=/Users/{user_name}/Library/Android/sdk/ndk/25.2.9519653/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a ..
cd ..
cmake --build cmake-android-out -j9
adb shell mkdir -p /data/local/tmp/executorch
# push the binary to an Android device
adb push cmake-android-out/executor_runner /data/local/tmp/executorch
# push the model file
adb push add.pte /data/local/tmp/executorch
adb shell "/data/local/tmp/executorch/executor_runner --model_path /data/local/tmp/executorch/add.pte"
iOS¶
对于iOS,我们将构建框架而不是静态库,这些框架也将包含内部的公共头文件。
安装 Xcode 从 Mac App Store,然后使用终端安装 命令行工具:
xcode-select --install
构建框架:
./build/build_apple_frameworks.sh
运行上述命令并带上 --help 标志以了解如何构建其他后端(如 Core ML,MPS 或 XNNPACK),等等。
注意,某些后端可能需要额外的依赖项以及特定版本的 Xcode 和 iOS。
Copy over the generated
.xcframework包到您的 Xcode 项目,链接到您的目标,并不要忘记添加额外的链接标志-all_load。
查看iOS 演示应用教程以获取更多信息。
下一步¶
您已成功跨编译了executor_runner个二进制文件到iOS和Android平台。您可以开始探索高级功能和能力。以下是一些您可能想阅读的部分列表:
选择性构建 以构建仅链接到程序所使用内核的运行时,这可以显著节省二进制文件大小。
Tutorials on deploying applications to embedded devices such as ARM Cortex-M/Ethos-U 和 XTensa HiFi DSP。