注意
在深入研究之前,请确保您了解 ExecuTorch 概述 中的概念
设置 ExecuTorch¶
在本节中,我们将学习如何
设置一个用于处理 ExecuTorch 的环境
生成一个 ExecuTorch 示例程序
使用 ExecuTorch 运行时构建和运行程序
系统要求¶
操作系统¶
我们在以下系统上测试了这些说明,尽管它们也应该在类似的环境中工作。
CentOS 8+
Ubuntu 20.04.6 LTS+
RHEL 8+
Big Sur (11.0)+
Windows Subsystem for Linux (WSL) 以及任何 Linux 选项
环境设置¶
创建虚拟环境¶
在您的机器上安装 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
二进制文件。
将结果保存为
.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.Tensor
。 executor_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 的基本知识,请探索以下高级功能和特性。
详细了解 导出过程
深入了解 导出中间表示 (EXIR) 以进行复杂的导出工作流程