快捷方式
1
2
3
4

注意

在深入研究之前,请确保您了解 ExecuTorch 概述 中的概念

设置 ExecuTorch

在本节中,我们将学习如何

  • 设置一个用于处理 ExecuTorch 的环境

  • 生成一个 ExecuTorch 示例程序

  • 使用 ExecuTorch 运行时构建和运行程序

系统要求

操作系统

我们在以下系统上测试了这些说明,尽管它们也应该在类似的环境中工作。

Linux (x86_64)
  • CentOS 8+

  • Ubuntu 20.04.6 LTS+

  • RHEL 8+

macOS (x86_64/M1/M2)
  • Big Sur (11.0)+

Windows (x86_64)
  • Windows Subsystem for Linux (WSL) 以及任何 Linux 选项

软件

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

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

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

  • g++ 版本 8 或更高,clang++ 版本 8 或更高,或其他支持 GNU C 样式 语句表达式 (({ ... }) 语法) 的 C++17 兼容工具链。

请注意,可跨编译的核心运行时代码支持更广泛的工具链,低至 C++11。有关可移植性的详细信息,请参见 运行时概述

环境设置

创建虚拟环境

在您的机器上安装 conda。然后,创建一个虚拟环境来管理我们的依赖项。

# Create and activate a conda environment named "executorch"
conda create -yn executorch python=3.10.0
conda activate executorch

克隆并安装 ExecuTorch 要求

# Clone the ExecuTorch repo from GitHub
git clone --branch v0.2.0 https://github.com/pytorch/executorch.git
cd executorch

# Update and pull submodules
git submodule sync
git submodule update --init

# Install ExecuTorch pip package and its dependencies, as well as
# development tools like CMake.
./install_requirements.sh

使用 --pybind 标志 安装 pybindings 和其他后端的依赖项。

./install_requirements.sh --pybind <coreml | mps | xnnpack>

设置好环境后,您就可以将 PyTorch 程序转换为 ExecuTorch 了。

创建 ExecuTorch 程序

设置好环境后,您就可以将 PyTorch 程序转换为 ExecuTorch 了。

导出程序

ExecuTorch 提供 API 来将 PyTorch nn.Module 编译为 ExecuTorch 运行时使用的 .pte 二进制文件。

  1. torch.export

  2. exir.to_edge

  3. exir.to_executorch

  4. 将结果保存为 .pte 二进制文件,供 ExecuTorch 运行时使用。

让我们尝试使用一个简单的 PyTorch 模型来添加其输入。创建一个名为 export_add.py 的文件,其中包含以下代码

import torch
from torch.export import export
from executorch.exir import to_edge

# Start with a PyTorch model that adds two input tensors (matrices)
class Add(torch.nn.Module):
  def __init__(self):
    super(Add, self).__init__()

  def forward(self, x: torch.Tensor, y: torch.Tensor):
      return x + y

# 1. torch.export: Defines the program with the ATen operator set.
aten_dialect = export(Add(), (torch.ones(1), torch.ones(1)))

# 2. to_edge: Make optimizations for Edge devices
edge_program = to_edge(aten_dialect)

# 3. to_executorch: Convert the graph to an ExecuTorch program
executorch_program = edge_program.to_executorch()

# 4. Save the compiled .pte program
with open("add.pte", "wb") as file:
    file.write(executorch_program.buffer)

然后,从您的终端执行它。

python3 export_add.py

查看 ExecuTorch 导出教程,以了解有关导出过程的更多信息。

构建和运行

创建程序后,我们可以使用 ExecuTorch 运行时来执行它。

现在,让我们使用 executor_runner,这是一个使用 ExecuTorch 运行时在您的程序上运行 forward 方法的示例。

构建工具设置

ExecuTorch 仓库使用 CMake 来构建其 C++ 代码。在这里,我们将配置它来构建 executor_runner 工具,以便在我们的桌面操作系统上运行它。

# Clean and configure the CMake build system. Compiled programs will appear in the executorch/cmake-out directory we create here.
(rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake ..)

# Build the executor_runner target
cmake --build cmake-out --target executor_runner -j9

运行您的程序

现在我们已经导出了一个程序并构建了运行时,让我们执行它!

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

我们的输出是一个大小为 1 的 torch.Tensorexecutor_runner 将所有输入值设置为 torch.ones 张量,因此当 x=[1]y=[1] 时,我们得到 [1]+[1]=[2]

示例输出
Output 0: tensor(sizes=[1], [2.])

要了解如何构建类似的程序,请访问 ExecuTorch C++ 教程

[可选] 设置 Buck2

Buck2 是一个开源构建系统,我们的一些示例目前使用它来构建和运行。

但是,请注意,安装 Buck2 对于使用 ExecuTorch 是可选的,我们正在逐步放弃 Buck2 并将所有相关部分迁移到 cmake。一旦迁移完成,本节将被删除。

要设置 Buck2,您需要以下先决条件:

  • zstd 命令行工具 - 通过运行以下命令安装

    pip3 install zstd
    
  • buck2 命令行工具的 2024-02-15 版本 - 您可以在 Buck2 仓库 中下载适合您系统的预构建存档。请注意,版本很重要,较新或较旧的版本可能无法与 ExecuTorch 仓库使用的 buck2 prelude 版本兼容。

通过以下命令解压缩来配置 Buck2(文件名取决于您的系统)

# For example, buck2-x86_64-unknown-linux-musl.zst or buck2-aarch64-apple-darwin.zst
zstd -cdq buck2-DOWNLOADED_FILENAME.zst > /tmp/buck2 && chmod +x /tmp/buck2

您可能希望将 buck2 二进制文件复制到您的 $PATH 中,以便您可以将其作为 buck2 运行。

安装完成后,您可以按照 buck2 命令运行 add.pte 程序

/tmp/buck2 run //examples/portable/executor_runner:executor_runner -- --model_path add.pte

下一步

恭喜!您已成功导出、构建和运行您的第一个 ExecuTorch 程序。现在您已经了解了 ExecuTorch 的基本知识,请探索以下高级功能和特性。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源