设置 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)
带有任何 Linux 选项的 Windows Linux 子系统 (WSL)
快速设置: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
git clone --branch release/0.5 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>
设置好环境后,您就可以将 PyTorch 程序转换为 ExecuTorch 了。
注意: 清理构建系统
当获取上游仓库的新版本时(通过
git fetch
或git 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 nn.Module
编译为 ExecuTorch 运行时使用的 .pte
二进制文件。
将结果另存为
.pte
二进制文件,供 ExecuTorch 运行时使用。
让我们尝试使用一个简单的 PyTorch 模型,该模型将其输入相加。
在 ExecuTorch 仓库外部的新目录中创建 export_add.py
。
注意:重要的是,此文件不应位于 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
方法。
构建工具设置¶
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
注意: 清理构建系统
当获取上游仓库的新版本时(通过
git fetch
或git 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 的 torch.Tensor
。executor_runner
将所有输入值设置为 torch.ones
张量,因此当 x=[1]
且 y=[1]
时,我们得到 [1]+[1]=[2]
示例输出
Output 0: tensor(sizes=[1], [2.])
要了解如何构建类似的程序,请访问运行时 API 教程。
下一步¶
恭喜!您已成功导出、构建和运行您的第一个 ExecuTorch 程序。现在您已经对 ExecuTorch 有了基本的了解,请探索其高级功能和特性,如下所示。
了解有关导出过程的更多信息
深入了解用于复杂导出工作流程的导出中间表示 (EXIR)