捷徑
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)

  • 適用於 Linux 的 Windows 子系統 (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

Clone 並安裝 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 flag 安裝 pybindings 和其他後端的依賴項。

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

設定好您的環境後,您就可以將您的 PyTorch 程式轉換為 ExecuTorch 了。

注意: 清理建置系統

當獲取上游儲存庫的新版本時(透過 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 nn.Module 編譯為可由 ExecuTorch 運行時環境使用的 .pte 二進位檔。

  1. torch.export

  2. exir.to_edge

  3. exir.to_executorch

  4. 將結果儲存為 .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 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 的 torch.Tensorexecutor_runner 將所有輸入值設定為 torch.ones 張量,所以當 x=[1]y=[1] 時,我們會得到 [1]+[1]=[2]

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

要了解如何建置類似的程式,請造訪Runtime APIs Tutorial

下一步

恭喜!您已成功匯出、建置和運行您的第一個 ExecuTorch 程式。現在您已經對 ExecuTorch 有了基本的了解,請探索以下的高級功能和特性。

文件

存取 PyTorch 的完整開發人員文件

查看文件

教學

獲取適合初學者和進階開發人員的深入教學課程

查看教學課程

資源

尋找開發資源並獲得您的問題解答

查看資源