使用 MPS 後端建置和執行 ExecuTorch¶
在本教學中,我們將引導您完成設定,以建置 ExecuTorch 的 MPS 後端,並在其上執行簡單模型。
MPS 後端裝置將機器學習計算圖和基本元素對應到 MPS Graph 框架和由 MPS 提供的調整核心。
在本教學中,您將學習如何將 MobileNet V3 模型匯出到 MPS 委派。
您也將學習如何在 macOS 和 iOS 上編譯和部署帶有 MPS 委派的 ExecuTorch 執行階段。
先決條件(硬體和軟體)¶
為了能夠成功使用 MPS 後端建構和執行 ExecuTorch 模型,您需要以下硬體和軟體組件
設定開發者環境¶
步驟 1. 請完成教學 設定 ExecuTorch。
步驟 2. 安裝降低 MPS 委派所需的依賴項
./backends/apple/mps/install_requirements.sh
建置¶
AOT (Ahead-of-time) 組件¶
編譯 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_requirements.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
./build/build_apple_frameworks.sh --mps
mps_delegate.xcframework
將位於 cmake-out
資料夾中,以及 executorch.xcframework
和 portable_delegate.xcframework
cd cmake-out && ls
步驟 2. 將框架連結到您的 XCode 專案中:前往專案目標的 Build Phases
- Link Binaries With Libraries
,按一下 + 符號並新增框架:位於 Release
資料夾中的檔案。
executorch.xcframework
portable_delegate.xcframework
mps_delegate.xcframework
從同一個頁面,包含 MPS 委派所需的程式庫
MetalPerformanceShaders.framework
MetalPerformanceShadersGraph.framework
Metal.framework
在本教學課程中,您已學習如何將模型降低到 MPS 委派,建置 mps_executor_runner 並透過 MPS 委派執行降低的模型,或使用 MPS 委派靜態程式庫直接在裝置上執行。
常見錯誤和解決方法。¶
如果您在遵循本教學課程時遇到任何錯誤或問題,請在 ExecuTorch 儲存庫上提交錯誤/問題,並加上標籤 #mps。