• 文件 >
  • 使用 CMake 建置
捷徑

使用 CMake 建置

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

CMake 建置系統建置的目標

ExecuTorch 的 CMake 建置系統涵蓋了 runtime 中可能對嵌入式系統使用者有用的部分。

  • 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 建置的一次性設定

請按照以下步驟操作,在您使用 CMake 在您的機器上進行建置之前,先準備好工具。

  1. 如果您的系統的 python3 版本低於 3.11

    • 執行 pip install tomli

  2. 安裝 CMake 3.19 或更高版本

    • 執行 conda install cmakepip install cmake

設定 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_requirements.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 檔案

首先,使用 設定 ExecuTorch 中描述的指示,產生一個 add.pte 或其他 ExecuTorch 程式檔案。

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

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

如果一切正常,您應該會看到訊息「Model executed successfully」,後面接著輸出值。

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

  • 先決條件:Android NDK,選擇以下其中一個

    • 選項 1:依照 安裝 ndk 的指示下載 Android Studio。

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

假設 Android NDK 可用,請執行

# Run the following lines from the `executorch/` folder
./install_requirements.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=/Users/{user_name}/Library/Android/sdk/ndk/25.2.9519653/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"

iOS

對於 iOS,我們將建置 frameworks 而不是靜態函式庫,其中也將包含公用標頭。

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

xcode-select --install
  1. 建置 frameworks

./build/build_apple_frameworks.sh

使用 --help 標記執行上述命令,以了解有關如何建置其他後端(例如 Core MLMPS 或 XNNPACK)的更多資訊。請注意,某些後端可能需要其他相依性和特定版本的 Xcode 和 iOS。

  1. 將產生的 .xcframework 組合複製到您的 Xcode 專案中,將它們連結到您的目標,並且不要忘記新增額外的連結器標記 -all_load

請查看 iOS Demo App 教學課程以獲取更多資訊。

下一步

您已成功將 executor_runner 二進位檔交叉編譯到 iOS 和 Android 平台。您可以開始探索進階功能和能力。以下是您可能想要接下來閱讀的章節清單

  • 選擇性建置,以建置僅連結到程式使用的核心的執行階段,這可以顯著節省二進位檔大小。

  • 有關建置 AndroidiOS 示範應用程式的教學課程。

  • 有關將應用程式部署到嵌入式裝置(例如 ARM Cortex-M/Ethos-UXTensa HiFi DSP)的教學課程。

文件

存取 PyTorch 的全面開發人員文件

檢視文件

教學課程

取得針對初學者和進階開發人員的深入教學課程

檢視教學課程

資源

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

檢視資源