目录
1
2
3
4

设置 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++版本 7 或更高版本、版本 5 或更高版本或其他版本 兼容 C++17 的工具链。clang++

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

快速设置:Colab/Jupyter Notebook 原型

要充分利用 ExecuTorch,请按照下面提供的设置说明从源代码进行安装。

或者,如果您想快速轻松地试用 ExecuTorch,我们建议使用以下 colab 笔记本进行原型设计。您可以直接通过 安装以获得基本功能。pip

pip install executorch

环境设置

创建虚拟环境

在您的计算机上安装 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
# 'main' branch is the primary development branch where you see the latest changes.
# 'viable/strict' contains all of the commits on main that pass all of the necessary CI checks.
git clone --branch viable/strict 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>

# Example: pybindings with CoreML *only*
./install_requirements.sh --pybind coreml

# Example: pybinds with CoreML *and* XNNPACK
./install_requirements.sh --pybind coreml xnnpack

默认情况下,command 会为 XNNPACK 安装 pybindings。要完全禁用任何 pybindings:./install_requirements.sh

./install_requirements.sh --pybind off

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

注意:清理构建系统

在获取上游 repo 的新版本时(通过 或 ),最好清理旧的构建工件。构建系统 目前不能很好地适应构建依赖项的变化。git fetchgit pull

您还应该再次更新并拉取子模块,以防它们的版本 已经改变。

# From the root of the executorch repo:
./install_requirements.sh --clean
git submodule sync
git submodule update --init

创建 ExecuTorch 程序

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

导出程序

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

  1. torch.export

  2. exir.to_edge

  3. exir.to_executorch

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

让我们使用一个简单的 PyTorch 模型来尝试一下,该模型添加了其输入。

在 ExecuTorch 存储库之外的新目录中创建。export_add.py

注意:此文件不位于作为目录的父目录中,这一点很重要。我们需要 python 从 site-packages 导入,而不是从 repo 本身导入。

mkdir -p ../example_files
cd ../example_files
touch export_add.py

将以下代码添加到 :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

如果它有效,您将在该目录中看到add.pte

请参阅 ExecuTorch 导出教程,了解有关导出过程的更多信息。

构建并运行

创建程序后,返回 executorch 目录,使用 ExecuTorch 运行时执行该程序。

cd ../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.
./install_requirements.sh --clean
(mkdir cmake-out && cd cmake-out && cmake ..)

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

注意:清理构建系统

在获取上游 repo 的新版本时(通过 或 ),最好清理旧的构建工件。构建系统 目前不能很好地适应构建依赖项的变化。git fetchgit pull

您还应该再次更新并拉取子模块,以防它们的版本 已经改变。

# From the root of the executorch repo:
./install_requirements.sh --clean
git submodule sync
git submodule update --init

运行您的程序

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

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

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

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

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

后续步骤

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

文档

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

查看文档

教程

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

查看教程

资源

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

查看资源