目录

使用 CMake 构建

虽然 buck2 是 ExecuTorch 项目的主要构建系统,但它也是 可以使用 CMake 构建运行时的核心部分,以便更轻松地与其他构建系统集成。即使您不使用 CMake 直接,CMake 可以发出其他格式的脚本,如 Make、Ninja 或 Xcode。 有关信息,请参见 cmake-generators(7)。

由 CMake 构建系统构建的目标

ExecuTorch 的 CMake 构建系统并未涵盖 buck2 构建的所有内容 系统盖板。它只能构建运行时中可能 对嵌入式系统用户有用。

  • libexecutorch.a:ExecuTorch 运行时的核心。不包含任何 operator/kernel 定义或后端定义。

  • libportable_kernels.a:ATen 兼容运算符的实现, 在 中的签名之后。//kernels/portable/functions.yaml

  • libportable_kernels_bindings.a:注册内容的生成代码 的与运行时。libportable_kernels.a

    • 注意:这必须通过类似 或 的标志链接到您的应用程序中。它包含加载时间函数 会自动注册内核,但链接器通常会修剪这些内核 函数,因为没有直接调用它们。-Wl,-force_load-Wl,--whole-archive

  • executor_runner:一个示例工具,它使用所有值作为输入来运行程序文件,并将输出打印到 stdout。它与 链接,因此程序可以使用它的任何运算符 实现。.pte1libportable_kernels.a

一次性设置以准备 CMake Build

请按照以下步骤准备好工具,然后再使用 CMake 在您的机器上进行构建。

  1. 克隆存储库并安装 buck2,如 “构建运行时” 部分所述 设置 ExecuTorch

    • buck2是必需的,因为 CMake 构建系统运行命令 从主构建系统中提取源列表。这将是可能的 来配置 CMake 系统以避免调用 。buck2buck2

  2. 如果您的系统的 python3 版本低于 3.11:

    • pip install tomli

    • 这提供了 CMake 构建系统所需的脚本所需的导入 调用 以从 中提取源列表。请考虑在 Python 或 Conda 虚拟环境中执行此操作(如果您创建了 一个已经通过遵循设置 ExecuTorch 来实现。buck2pip install

  3. 安装 CMake 版本 3.19 或更高版本:

    • Run 或 .conda install cmakepip 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.
#
# NOTE: If your `buck2` binary is not on the PATH, you can change this line to
# say something like `-DBUCK2=/tmp/buck2` to point directly to the tool.
(rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake -DBUCK2=buck2 ..)

完成此操作后,您无需再次执行此操作,直到您再次从上游存储库拉取,或者修改了任何与 CMake 相关的文件。

构建运行时组件

构建所有目标

# 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

使用示例应用程序执行 .pte 文件executor_runner

首先,使用 说明,如设置 ExecuTorch 中所述。add.pte

然后,将其传递给命令行工具:

./cmake-out/executor_runner --model_path path/to/add.pte

如果它有效,您应该会看到消息 “Model executed successfully” 按输出值。

I 00:00:00.002052 executorch:executor_runner.cpp:75] Model file add.pte is loaded.
I 00:00:00.002086 executorch:executor_runner.cpp:85] Running method forward
I 00:00:00.002092 executorch:executor_runner.cpp:140] Setting up non-const buffer 1, size 48.
I 00:00:00.002149 executorch:executor_runner.cpp:181] Method loaded.
I 00:00:00.002154 executorch:util.h:105] input already initialized, refilling.
I 00:00:00.002157 executorch:util.h:105] input already initialized, refilling.
I 00:00:00.002159 executorch:executor_runner.cpp:186] Inputs prepared.
I 00:00:00.011684 executorch:executor_runner.cpp:195] Model executed successfully.
I 00:00:00.011709 executorch:executor_runner.cpp:210] 8.000000

交叉编译

以下是有关如何为 Android 和 iOS 执行交叉编译的说明。

人造人

假设 Android NDK 可用,请运行:

# Run the following lines from the `executorch/` folder
rm -rf cmake-android-out && mkdir cmake-android-out && cd cmake-android-out

# point -DCMAKE_TOOLCHAIN_FILE to the location where ndk is installed
# Run `which buck2`, if it returns empty (meaning the system doesn't know where buck2 is installed), pass in pass in this flag `-DBUCK2=/path/to/buck2` pointing to buck2
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

# 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,我们将构建框架而不是静态库,静态库也将包含公共标头。

  1. Mac App Store 安装 Xcode,然后安装 使用终端的命令行工具:

xcode-select --install
  1. 构建框架:

./build/build_apple_frameworks.sh

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

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

有关详细信息,请查看 iOS 演示应用程序教程。

后续步骤

您已成功将二进制文件交叉编译到 iOS 和 Android 平台。您可以开始探索高级特性和功能。以下是您接下来可能想要阅读的部分列表:executor_runner

  • 选择性构建,用于构建仅链接到程序使用的内核的运行时,这可以显著节省二进制大小。

  • 有关构建 AndroidiOS 演示应用程序的教程。

  • 有关将应用程序部署到嵌入式设备(如 ARM Cortex-M/Ethos-UXTensa HiFi DSP)的教程。

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

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

查看教程

资源

查找开发资源并解答您的问题

查看资源