快捷方式

從原始碼構建

ExecuTorch 使用 CMake 作為主要的構建系統。即使您不直接使用 CMake,CMake 也可以生成其他格式的指令碼,例如 Make、Ninja 或 Xcode。更多資訊請參閱 cmake-generators(7)

系統要求

作業系統

我們在以下系統上測試了這些說明,它們也應該在類似的環境中工作。

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 選項的適用於 Linux 的 Windows 子系統 (WSL)

軟體

  • conda 或其他虛擬環境管理器

    • 我們推薦使用 conda,因為它提供跨語言支援並與 pip (Python 內建的包管理器) 無縫整合

    • 或者,Python 內建的虛擬環境管理器 python venv 是一個不錯的替代方案。

  • g++ 版本 7 或更高,clang++ 版本 5 或更高,或另一個相容 C++17 的工具鏈。

  • python 版本 3.10-3.12

請注意,可交叉編譯的核心執行時程式碼支援更廣泛的工具鏈,最低可達 C++17。有關可移植性詳細資訊,請參閱執行時概覽

環境設定

克隆 ExecuTorch

# Clone the ExecuTorch repo from GitHub
git clone -b release/0.6 https://github.com/pytorch/executorch.git && cd executorch

建立虛擬環境

建立並激活 Python 虛擬環境

python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip

或者,您可以在機器上安裝 conda。然後,建立一個名為“executorch”的 Conda 環境。

conda create -yn executorch python=3.10.0 && conda activate executorch

從原始碼安裝 ExecuTorch pip 包

# 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_executorch.sh

使用--pybind 標誌安裝 pybindings 及其他後端的依賴項。

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

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

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

預設情況下,./install_executorch.sh 命令會安裝 XNNPACK 的 pybindings。要完全停用所有 pybindings

./install_executorch.sh --pybind off

對於開發模式,請使用 --editable 執行命令,這允許我們修改 Python 原始碼並立即看到更改。

./install_executorch.sh --editable [--pybind xnnpack]

# Or you can directly do the following if dependencies are already installed
# either via a previous invocation of `./install_executorch.sh` or by explicitly installing requirements via `./install_requirements.sh` first.
pip install -e .

如果修改了 C++ 檔案,您仍然需要從原始碼重新安裝 ExecuTorch。

警告: 某些模組在可編輯模式下無法直接匯入。這是一個已知問題,我們正在積極解決。要解決此問題

# This will fail
python -c "from executorch.exir import CaptureConfig"
# But this will succeed
python -c "from executorch.exir.capture import CaptureConfig"

注意: 清理構建系統

當獲取上游倉庫的新版本(透過 git fetchgit pull)時,最好清理舊的構建產物。構建系統目前對構建依賴項的變化適應性不佳。

您還應該再次更新和拉取子模組,以防它們的版本發生變化。

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

從原始碼構建 ExecuTorch C++ 執行時

ExecuTorch 的 CMake 構建系統包含了可能對嵌入式系統使用者有用的執行時部分。

  • libexecutorch.a: ExecuTorch 執行時的核心。不包含任何運算元/核心定義或後端定義。

  • libportable_kernels.a: ATen 相容運算元的實現,遵循 //kernels/portable/functions.yaml 中的簽名。

  • libportable_kernels_bindings.a: 將 libportable_kernels.a 內容註冊到執行時的生成程式碼。

    • 注意:必須使用類似 -Wl,-force_load-Wl,--whole-archive 的標誌將其連結到您的應用程式中。它包含自動註冊核心的載入時函式,但連結器通常會預設移除這些函式,因為沒有直接呼叫它們。

  • executor_runner: 一個示例工具,它使用所有 1 值作為輸入執行 .pte 程式檔案,並將輸出列印到 stdout。它與 libportable_kernels.a 連結,因此程式可以使用它實現的任何運算元。

配置 CMake 構建

在克隆或拉取上游倉庫後遵循這些步驟,因為構建依賴項可能已更改。

# cd to the root of the executorch repo
cd executorch

# Clean and configure the CMake build system. It's good practice to do this
# whenever cloning or pulling the upstream repo.
./install_executorch.sh --clean
(mkdir cmake-out && cd cmake-out && cmake ..)

完成後,除非您再次從上游倉庫拉取或修改任何與 CMake 相關的檔案,否則無需再次執行此操作。

CMake 構建選項

釋出構建提供了旨在提高效能和減小二進位制大小的最佳化。它停用程式驗證和 executorch 日誌記錄,並新增最佳化標誌。

-DCMAKE_BUILD_TYPE=Release

要進一步最佳化釋出構建以減小大小,請同時使用

-DCMAKE_BUILD_TYPE=Release \
-DOPTIMIZE_SIZE=ON

參閱 CMakeLists.txt

構建執行時元件

構建所有目標,使用

# cd to the root of the executorch repo
cd executorch

# Build using the configuration that you previously generated under the
# `cmake-out` directory.
#
# NOTE: The `-j` argument specifies how many jobs/processes to use when
# building, and tends to speed up the build significantly. It's typical to use
# "core count + 1" as the `-j` value.
cmake --build cmake-out -j9

使用示例二進位制 executor_runner 執行 .pte 檔案

首先,按照準備模型中描述的說明生成 add.pte 或其他 ExecuTorch 程式檔案。

然後,將其傳遞給命令列工具

./cmake-out/executor_runner --model_path path/to/model.pte

您應該看到訊息“模型執行成功”,後跟輸出值。

I 00:00:00.000526 executorch:executor_runner.cpp:82] Model file add.pte is loaded.
I 00:00:00.000595 executorch:executor_runner.cpp:91] Using method forward
I 00:00:00.000612 executorch:executor_runner.cpp:138] Setting up planned buffer 0, size 48.
I 00:00:00.000669 executorch:executor_runner.cpp:161] Method loaded.
I 00:00:00.000685 executorch:executor_runner.cpp:171] Inputs prepared.
I 00:00:00.000764 executorch:executor_runner.cpp:180] Model executed successfully.
I 00:00:00.000770 executorch:executor_runner.cpp:184] 1 outputs:
Output 0: tensor(sizes=[1], [2.])

交叉編譯

以下是如何針對 Android 和 iOS 執行交叉編譯的說明。

Android

構建 executor_runner shell 二進位制檔案

  • 前提條件:Android NDK,選擇以下任一方式

    • 選項 1:按照安裝 ndk 的說明下載 Android Studio。

    • 選項 2:直接從此處下載 Android NDK。

假設 Android NDK 已可用,執行

# Run the following lines from the `executorch/` folder
./install_executorch.sh --clean
mkdir cmake-android-out && cd cmake-android-out

# point -DCMAKE_TOOLCHAIN_FILE to the location where ndk is installed
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake  -DANDROID_ABI=arm64-v8a ..

cd  ..
cmake --build  cmake-android-out  -j9

adb shell mkdir -p /data/local/tmp/executorch
# push the binary to an Android device
adb push  cmake-android-out/executor_runner  /data/local/tmp/executorch
# push the model file
adb push  add.pte  /data/local/tmp/executorch

adb shell  "/data/local/tmp/executorch/executor_runner --model_path /data/local/tmp/executorch/add.pte"

從原始碼構建用於應用整合的 AAR

  • 前提條件:上一節中的 Android NDK 和 Android SDK(推薦使用 Android Studio)。

假設 Android NDK 和 SDK 已可用,執行

export ANDROID_ABIS=arm64-v8a
export BUILD_AAR_DIR=aar-out
mkdir -p $BUILD_AAR_DIR
sh scripts/build_android_library.sh

此指令碼將構建 AAR,其中包含 Java API 及其對應的 JNI 庫。有關用法,請參閱此文件

iOS

對於 iOS,我們將構建 frameworks 而非靜態庫,其中也將包含公共標頭檔案。

  1. Mac App Store 安裝 Xcode,然後使用終端安裝 Command Line Tools

xcode-select --install
  1. 構建 frameworks

./scripts/build_apple_frameworks.sh

使用 --help 標誌執行上述命令,以瞭解如何構建其他後端(例如Core MLMPS 或 XNNPACK)等。請注意,某些後端可能需要額外的依賴項以及特定版本的 Xcode 和 iOS。

  1. 將生成的 .xcframework bundle 複製到您的 Xcode 專案中,將其連結到您的 targets,並且不要忘記新增額外的連結器標誌 -all_load

檢視 iOS 演示應用教程以獲取更多資訊。

後續步驟

您已成功將 executor_runner 二進位制檔案交叉編譯到 iOS 和 Android 平臺。您可以開始探索高階功能和能力。以下是您接下來可能希望閱讀的章節列表

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源