目录
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)

  • Big Sur (11.0)+

Windows (x86_64)

  • Windows Subsystem for Linux (WSL) 与任何 Linux 发行版

软件

  • conda 或另一个虚拟环境管理器

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

    • 否则,Python 自带的虚拟环境管理器 python venv 是一个很好的替代方案。

  • g++ 版本 7 或更高版本,clang++ 版本 5 或更高版本,或另一个 C++17 兼容的工具链。

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

快速设置:Colab/Jupyter Notebook 原型

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

或者,如果您想快速轻松地尝试ExecuTorch,我们推荐使用以下 colab notebook 进行原型设计。您可以直接通过 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

使用 --pybind 标志 安装带有 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

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

./install_requirements.sh --pybind off

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

NOTE: Cleaning the build system

When fetching a new version of the upstream repo (via git fetch or git pull) it is a good idea to clean the old build artifacts. The build system does not currently adapt well to changes in build dependencies.

You should also update and pull the submodules again, in case their versions have changed.

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

创建一个ExecuTorch程序

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

导出一个程序

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

  1. torch.export

  2. exir.to_edge

  3. exir.to_executorch

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

让我们尝试使用一个简单的 PyTorch 模型来实现这一功能,该模型会对输入进行相加。

创建 export_add.py 在 ExecuTorch 仓库外部的新目录中。

注意:这个文件不应该位于 executorch 目录的父目录中。我们需要 Python 从 site-packages 导入,而不是从仓库本身导入。

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

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

构建工具配置

The ExecuTorch repo uses CMake to build its C++ code. Here, we’ll configure it to build the executor_runner tool to run it on our desktop OS.

# 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

NOTE: Cleaning the build system

When fetching a new version of the upstream repo (via git fetch or git pull) it is a good idea to clean the old build artifacts. The build system does not currently adapt well to changes in build dependencies.

You should also update and pull the submodules again, in case their versions have changed.

# 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

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

Sample Output
Output 0: tensor(sizes=[1], [2.])

要学习如何构建类似的程序,请访问运行时API教程

下一步

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

文档

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

查看文档

教程

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

查看教程

资源

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

查看资源