目录

使用 CMake 构建

ExecuTorch 使用 CMake 作为其主要构建系统。 即使您不直接使用 CMake,CMake 也可以发出其他格式的脚本 如 Make、Ninja 或 Xcode。有关信息,请参见 cmake-generators(7)。

由 CMake 构建系统构建的目标

ExecuTorch 的 CMake 构建系统涵盖了运行时的各个部分,这些部分是 可能对嵌入式系统用户有用。

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

  • libportable_kernels.a:ATen 兼容运算符的实现, 在 中的签名之后。[functions.yaml](https://github.com/pytorch/executorch/blob/release/0.4/kernels/portable/functions.yaml)

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

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

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

一次性设置以准备 CMake Build

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

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

    • pip install tomli

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

    • Run 或 .conda install cmakepip install cmake

配置 CMake 构建

在克隆或拉取上游存储库后,请按照以下步骤操作,因为构建 依赖项可能已更改。

# cd to the root of the executorch repo
cd executorch

# Clean cmake cache directory (cmake-out). It's a good practice to do this
# whenever cloning or pulling the upstream repo.
bash install_requirements.sh --clean

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

CMake 构建选项

发布版本提供了旨在提高性能和减小二进制文件大小的优化。它禁用了程序验证和 executorch 日志记录,并添加了优化标志。

CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Release"

要进一步优化发布版本的大小,请添加:

CMAKE_FLAGS="$CMAKE_FLAGS -DOPTIMIZE_SIZE=ON"

查看 CMakeLists.txt

构建运行时组件

构建所有目标

# Build using the configuration that you previously generated under the
# `cmake-out` directory.
cmake "$CMAKE_FLAGS" -Bcmake-out .

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

  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 的全面开发人员文档

查看文档

教程

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

查看教程

资源

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

查看资源