快捷方式

从源代码构建

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)

软件

  • conda 或其他虚拟环境管理器

    • 我们推荐使用 conda,因为它提供跨语言支持并与 pip (Python 内置的包管理器) 无缝集成

    • 或者,Python 内置的虚拟环境管理器 python venv 是一个不错的替代方案。

  • g++ 版本 7 或更高,clang++ 版本 5 或更高,或另一个兼容 C++17 的工具链。

  • python 版本 3.10-3.12

请注意,可交叉编译的核心运行时代码支持更广泛的工具链,最低可达 C++17。有关可移植性详细信息,请参阅运行时概览

环境设置

克隆 ExecuTorch

# Clone the ExecuTorch repo from GitHub
git clone -b release/0.6 https://github.com/pytorch/executorch.git && cd executorch

创建虚拟环境

创建并激活 Python 虚拟环境

python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip

或者,您可以在机器上安装 conda。然后,创建一个名为“executorch”的 Conda 环境。

conda create -yn executorch python=3.10.0 && conda activate 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 fetchgit 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

参阅 CMakeLists.txt

构建运行时组件

构建所有目标,使用

# 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,选择以下任一方式

    • 选项 1:按照安装 ndk 的说明下载 Android Studio。

    • 选项 2:直接从此处下载 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 而非静态库,其中也将包含公共头文件。

  1. Mac App Store 安装 Xcode,然后使用终端安装 Command Line Tools

xcode-select --install
  1. 构建 frameworks

./scripts/build_apple_frameworks.sh

使用 --help 标志运行上述命令,以了解如何构建其他后端(例如Core MLMPS 或 XNNPACK)等。请注意,某些后端可能需要额外的依赖项以及特定版本的 Xcode 和 iOS。

  1. 将生成的 .xcframework bundle 复制到您的 Xcode 项目中,将其链接到您的 targets,并且不要忘记添加额外的链接器标志 -all_load

查看 iOS 演示应用教程以获取更多信息。

后续步骤

您已成功将 executor_runner 二进制文件交叉编译到 iOS 和 Android 平台。您可以开始探索高级功能和能力。以下是您接下来可能希望阅读的章节列表

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源