从源代码构建¶
ExecuTorch 使用 CMake 作为主要的构建系统。即使您不直接使用 CMake,CMake 也可以生成其他格式的脚本,例如 Make、Ninja 或 Xcode。更多信息请参阅 cmake-generators(7)。
系统要求¶
操作系统¶
我们在以下系统上测试了这些说明,它们也应该在类似的环境中工作。
Linux (x86_64)
CentOS 8+
Ubuntu 20.04.6 LTS+
RHEL 8+
macOS (x86_64/M1/M2)
Big Sur (11.0)+
Windows (x86_64)
带任意 Linux 选项的适用于 Linux 的 Windows 子系统 (WSL)
环境设置¶
克隆 ExecuTorch¶
# Clone the ExecuTorch repo from GitHub
git clone -b release/0.6 https://github.com/pytorch/executorch.git && cd executorch
从源代码安装 ExecuTorch pip 包¶
# Install ExecuTorch pip package and its dependencies, as well as
# development tools like CMake.
# If developing on a Mac, make sure to install the Xcode Command Line Tools first.
./install_executorch.sh
使用--pybind
标志安装 pybindings 及其他后端的依赖项。
./install_executorch.sh --pybind <coreml | mps | xnnpack>
# Example: pybindings with CoreML *only*
./install_executorch.sh --pybind coreml
# Example: pybinds with CoreML *and* XNNPACK
./install_executorch.sh --pybind coreml xnnpack
默认情况下,./install_executorch.sh
命令会安装 XNNPACK 的 pybindings。要完全禁用所有 pybindings
./install_executorch.sh --pybind off
对于开发模式,请使用 --editable
运行命令,这允许我们修改 Python 源代码并立即看到更改。
./install_executorch.sh --editable [--pybind xnnpack]
# Or you can directly do the following if dependencies are already installed
# either via a previous invocation of `./install_executorch.sh` or by explicitly installing requirements via `./install_requirements.sh` first.
pip install -e .
如果修改了 C++ 文件,您仍然需要从源代码重新安装 ExecuTorch。
警告: 某些模块在可编辑模式下无法直接导入。这是一个已知问题,我们正在积极解决。要解决此问题
# This will fail python -c "from executorch.exir import CaptureConfig" # But this will succeed python -c "from executorch.exir.capture import CaptureConfig"
注意: 清理构建系统
当获取上游仓库的新版本(通过
git fetch
或git pull
)时,最好清理旧的构建产物。构建系统目前对构建依赖项的变化适应性不佳。您还应该再次更新和拉取子模块,以防它们的版本发生变化。
# From the root of the executorch repo: ./install_executorch.sh --clean git submodule sync git submodule update --init --recursive
从源代码构建 ExecuTorch C++ 运行时¶
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
: 一个示例工具,它使用所有 1 值作为输入运行.pte
程序文件,并将输出打印到 stdout。它与libportable_kernels.a
链接,因此程序可以使用它实现的任何算子。
配置 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_executorch.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 文件¶
首先,按照准备模型中描述的说明生成 add.pte
或其他 ExecuTorch 程序文件。
然后,将其传递给命令行工具
./cmake-out/executor_runner --model_path path/to/model.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¶
构建 executor_runner shell 二进制文件¶
前提条件:Android NDK,选择以下任一方式
假设 Android NDK 已可用,运行
# Run the following lines from the `executorch/` folder
./install_executorch.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=$ANDROID_NDK/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"
从源代码构建用于应用集成的 AAR¶
前提条件:上一节中的 Android NDK 和 Android SDK(推荐使用 Android Studio)。
假设 Android NDK 和 SDK 已可用,运行
export ANDROID_ABIS=arm64-v8a
export BUILD_AAR_DIR=aar-out
mkdir -p $BUILD_AAR_DIR
sh scripts/build_android_library.sh
此脚本将构建 AAR,其中包含 Java API 及其对应的 JNI 库。有关用法,请参阅此文档。
iOS¶
对于 iOS,我们将构建 frameworks 而非静态库,其中也将包含公共头文件。
从 Mac App Store 安装 Xcode,然后使用终端安装 Command Line Tools
xcode-select --install
构建 frameworks
./scripts/build_apple_frameworks.sh
使用 --help
标志运行上述命令,以了解如何构建其他后端(例如Core ML、MPS 或 XNNPACK)等。请注意,某些后端可能需要额外的依赖项以及特定版本的 Xcode 和 iOS。
将生成的
.xcframework
bundle 复制到您的 Xcode 项目中,将其链接到您的 targets,并且不要忘记添加额外的链接器标志-all_load
。
查看 iOS 演示应用教程以获取更多信息。
后续步骤¶
您已成功将 executor_runner
二进制文件交叉编译到 iOS 和 Android 平台。您可以开始探索高级功能和能力。以下是您接下来可能希望阅读的章节列表
选择性构建,构建仅链接程序使用的内核的运行时,这可以显著节省二进制文件大小。
关于将应用程序部署到嵌入式设备(例如 ARM Cortex-M/Ethos-U 和 XTensa HiFi DSP)的教程。