目录
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)
  • 大苏尔 (11.0)+

窗户 (x86_64)
  • 具有任何 Linux 选项的适用于 Linux 的 Windows 子系统 (WSL)

软件

  • conda或其他 Virtual Environment Manager

    • 我们建议使用,因为它提供跨语言 支持并与 (Python 的内置包管理器) 顺利集成condapip

    • 否则,Python 的内置虚拟环境管理器是一个不错的选择。python venv

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

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

快速设置:Colab/Jupyter Notebook 原型

要充分利用 ExecuTorch,请按照下面提供的设置说明进行操作。或者,如果您想快速轻松地试用 ExecuTorch,我们建议使用以下 colab 笔记本进行原型设计。

环境设置

创建虚拟环境

在您的计算机上安装 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.1 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.
# If developing on a Mac, make sure to install the Xcode Command Line Tools first.
./install_requirements.sh

使用标志与其他后端的 pybindings 和依赖项一起安装。

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

设置环境后,您就可以转换 PyTorch 程序了 添加到 ExecuTorch。

创建 ExecuTorch 程序

设置环境后,您就可以转换 PyTorch 程序了 添加到 ExecuTorch。

导出程序

ExecuTorch 提供了 API,用于将 PyTorch 编译为 ExecuTorch 运行时使用的二进制文件。.pte

  1. torch.export

  2. exir.to_edge

  3. exir.to_executorch

  4. 将结果保存为二进制文件,以供 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 运行时来执行它。

现在,让我们使用 ,一个使用 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 的 a。它将所有输入值设置为一个张量,因此当 和 时,我们得到torch.Tensorexecutor_runnerx=[1]y=[1][1]+[1]=[2]

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

要了解如何构建类似的程序,请访问 Runtime API 教程

[可选]设置 Buck2

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

但是,请注意,使用 ExecuTorch 时,安装 是可选的,我们正在从所有相关部分过渡并迁移到 。完成迁移后,此部分将被删除。Buck2Buck2cmake

要设置 ,您需要满足此部分的以下先决条件:Buck2

  • 命令行工具 — 通过运行zstd

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

使用以下命令进行解压缩来配置 Buck2(filename 取决于 ,并且二进制文件的位置可能不同):

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

您可能希望将二进制文件复制到您的 S 中,以便运行它 如。buck2$PATHbuck2

安装完成后,您可以通过以下命令运行该程序:add.ptebuck2

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

请注意,第一次运行可能需要一段时间,因为它必须编译来自源的内核

后续步骤

祝贺!您已成功导出、构建并运行您的第一个 ExecuTorch 程序。现在您已经对 ExecuTorch 有了基本的了解, 在下面探索其高级特性和功能。

文档

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

查看文档

教程

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

查看教程

资源

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

查看资源