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.xcframework
portable_delegate.xcframework
mps_delegate.xcframework
在同一页面,包含 MPS 委托所需的库
MetalPerformanceShaders.framework
MetalPerformanceShadersGraph.framework
Metal.framework
在本教程中,您学习了如何将模型降级到 MPS 委托,构建 mps_executor_runner,并通过 MPS 委托或直接在设备上使用 MPS 委托静态库运行降级后的模型。
常见错误与解决方案。¶
如果您在本教程中遇到任何 bug 或问题,请在 ExecuTorch 仓库上提交 bug/issue,并带上标签 #mps。