使用 MPS 后端构建和运行 ExecuTorch¶
在本教程中,我们将引导您完成设置过程,以构建 ExecuTorch 的 MPS 后端并在其上运行一个简单的模型。
MPS 后端设备将机器学习计算图和原语映射到 MPS Graph 框架和 MPS 提供的调优内核上。
在本教程中,您将学习如何将 MobileNet V3 模型导出到 MPS 委托。
您还将学习如何在 macOS 和 iOS 上编译和部署带有 MPS 委托的 ExecuTorch 运行时。
先决条件(硬件和软件)¶
为了能够成功构建和运行使用 ExecuTorch 的 MPS 后端的模型,您需要以下硬件和软件组件
设置开发者环境¶
步骤 1. 请完成教程 设置 ExecuTorch。
步骤 2. 安装降低 MPS 委托所需的依赖项
./backends/apple/mps/install_requirements.sh
构建¶
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_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。