從原始碼構建¶
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)
環境設定¶
克隆 ExecuTorch¶
# Clone the ExecuTorch repo from GitHub
git clone -b release/0.6 https://github.com/pytorch/executorch.git && cd 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 fetch或git 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
構建執行時元件¶
構建所有目標,使用
# 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,選擇以下任一方式
假設 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 而非靜態庫,其中也將包含公共標頭檔案。
從 Mac App Store 安裝 Xcode,然後使用終端安裝 Command Line Tools
xcode-select --install
構建 frameworks
./scripts/build_apple_frameworks.sh
使用 --help 標誌執行上述命令,以瞭解如何構建其他後端(例如Core ML、MPS 或 XNNPACK)等。請注意,某些後端可能需要額外的依賴項以及特定版本的 Xcode 和 iOS。
將生成的
.xcframeworkbundle 複製到您的 Xcode 專案中,將其連結到您的 targets,並且不要忘記新增額外的連結器標誌-all_load。
檢視 iOS 演示應用教程以獲取更多資訊。
後續步驟¶
您已成功將 executor_runner 二進位制檔案交叉編譯到 iOS 和 Android 平臺。您可以開始探索高階功能和能力。以下是您接下來可能希望閱讀的章節列表
選擇性構建,構建僅連結程式使用的核心的執行時,這可以顯著節省二進位制檔案大小。
關於將應用程式部署到嵌入式裝置(例如 ARM Cortex-M/Ethos-U 和 XTensa HiFi DSP)的教程。