MPS 後端¶
在本教程中,我們將引導您完成為 ExecuTorch 構建 MPS 後端並執行簡單模型的過程。
MPS 後端裝置將機器學習計算圖和原語對映到 MPS Graph 框架和 MPS 提供的調優核心上。
在本教程中,您將學習如何將 MobileNet V3 模型匯出到 MPS 委託。
您還將學習如何在 macOS 和 iOS 上編譯和部署包含 MPS 委託的 ExecuTorch 執行時。
前置條件(硬體和軟體)¶
為了能夠成功構建並使用 ExecuTorch 的 MPS 後端執行模型,您需要以下硬體和軟體元件
構建¶
AOT(提前)元件¶
為 MPS 委託編譯模型:
在此步驟中,您將生成一個簡單的 ExecuTorch 程式,該程式將 MobileNetV3 模型降級到 MPS 委託。然後,您將在執行時傳遞此程式(
.pte檔案)以使用 MPS 後端執行它。
cd executorch
# Note: `mps_example` script uses by default the MPSPartitioner for ops that are not yet supported by the MPS delegate. To turn it off, pass `--no-use_partitioner`.
python3 -m examples.apple.mps.scripts.mps_example --model_name="mv3" --bundled --use_fp16
# To see all options, run following command:
python3 -m examples.apple.mps.scripts.mps_example --help
執行時¶
構建 MPS 執行器執行器
# In this step, you'll be building the `mps_executor_runner` that is able to run MPS lowered modules:
cd executorch
./examples/apple/mps/scripts/build_mps_executor_runner.sh
使用 mps_executor_runner 執行生成的 mv3 模型¶
./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv3_mps_bundled_fp16.pte --bundled_program
您應該看到以下結果。請注意,此示例不會生成輸出檔案
I 00:00:00.003290 executorch:mps_executor_runner.mm:286] Model file mv3_mps_bundled_fp16.pte is loaded.
I 00:00:00.003306 executorch:mps_executor_runner.mm:292] Program methods: 1
I 00:00:00.003308 executorch:mps_executor_runner.mm:294] Running method forward
I 00:00:00.003311 executorch:mps_executor_runner.mm:349] Setting up non-const buffer 1, size 606112.
I 00:00:00.003374 executorch:mps_executor_runner.mm:376] Setting up memory manager
I 00:00:00.003376 executorch:mps_executor_runner.mm:392] Loading method name from plan
I 00:00:00.018942 executorch:mps_executor_runner.mm:399] Method loaded.
I 00:00:00.018944 executorch:mps_executor_runner.mm:404] Loading bundled program...
I 00:00:00.018980 executorch:mps_executor_runner.mm:421] Inputs prepared.
I 00:00:00.118731 executorch:mps_executor_runner.mm:438] Model executed successfully.
I 00:00:00.122615 executorch:mps_executor_runner.mm:501] Model verified successfully.
[可選] 直接使用 pybind 執行生成的模型¶
確保已安裝
pybind的 MPS 支援
./install_executorch.sh --pybind mps
執行
mps_example指令碼以追蹤模型並直接從 python 執行它
cd executorch
# Check correctness between PyTorch eager forward pass and ExecuTorch MPS delegate forward pass
python3 -m examples.apple.mps.scripts.mps_example --model_name="mv3" --no-use_fp16 --check_correctness
# You should see following output: `Results between ExecuTorch forward pass with MPS backend and PyTorch forward pass for mv3_mps are matching!`
# Check performance between PyTorch MPS forward pass and ExecuTorch MPS forward pass
python3 -m examples.apple.mps.scripts.mps_example --model_name="mv3" --no-use_fp16 --bench_pytorch
效能分析:¶
[可選] 在匯出模型時生成一個 ETRecord。
cd executorch
python3 -m examples.apple.mps.scripts.mps_example --model_name="mv3" --generate_etrecord -b
在 ExecuTorch 執行時上執行您的程式並生成一個 ETDump。
./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv3_mps_bundled_fp16.pte --bundled_program --dump-outputs
透過傳入從執行時獲取的 ETDump 以及步驟 1 中可選生成的 ETRecord 來建立一個 Inspector API 例項。
python3 -m sdk.inspector.inspector_cli --etdump_path etdump.etdp --etrecord_path etrecord.bin
在裝置上部署和執行¶
步驟 1. 建立 ExecuTorch 核心和 MPS 委託框架以連結到 iOS 上
cd executorch
./scripts/build_apple_frameworks.sh --mps
mps_delegate.xcframework 將位於 cmake-out 資料夾中,以及 executorch.xcframework 和 portable_delegate.xcframework
cd cmake-out && ls
步驟 2. 將框架連結到您的 XCode 專案中:轉到專案 Target 的 Build Phases - Link Binaries With Libraries,點選 **+** 號並新增框架:位於 Release 資料夾中的檔案。
executorch.xcframeworkportable_delegate.xcframeworkmps_delegate.xcframework
在同一頁面,包含 MPS 委託所需的庫
MetalPerformanceShaders.frameworkMetalPerformanceShadersGraph.frameworkMetal.framework
在本教程中,您學習瞭如何將模型降級到 MPS 委託,構建 mps_executor_runner,並透過 MPS 委託或直接在裝置上使用 MPS 委託靜態庫執行降級後的模型。
常見錯誤與解決方案。¶
如果您在本教程中遇到任何 bug 或問題,請在 ExecuTorch 倉庫上提交 bug/issue,並帶上標籤 #mps。