快捷方式

MPS 后端

在本教程中,我们将引导您完成为 ExecuTorch 构建 MPS 后端的设置过程,并在其上运行一个简单的模型。

MPS 后端设备将机器学习计算图和原语映射到 MPS Graph 框架以及由 MPS 提供的优化内核。

本教程将学到什么
  • 在本教程中,您将学习如何将 MobileNet V3 模型导出到 MPS 委托。

  • 您还将学习如何在 macOS 和 iOS 上编译和部署带有 MPS 委托的 ExecuTorch 运行时。

先决条件(硬件和软件)

为了能够成功构建和运行使用 ExecuTorch MPS 后端的模型,您需要以下硬件和软件组件:

硬件:

  • 一台 Mac 用于跟踪模型

软件:

设置开发环境

步骤 1. 完成《入门》中的步骤,以设置 ExecuTorch 开发环境。

您还需要 ExecuTorch 存储库的本地克隆。有关说明,请参阅从源代码构建 ExecuTorch。本文档中的所有命令都应在 executorch 存储库中运行。

构建

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_float16_bundled.pte --bundled_program
  • 您应该会看到以下结果。请注意,在此示例中不会生成任何输出文件。

I 00:00:00.003290 executorch:mps_executor_runner.mm:286] Model file mv3_mps_float16_bundled.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 运行生成的模型

  1. 确保已安装支持 MPS 的 pybind

CMAKE_ARGS="-DEXECUTORCH_BUILD_MPS=ON" ./install_executorch.sh
  1. 运行 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

性能分析:

  1. [可选] 在导出模型时生成 ETRecord

cd executorch
python3 -m examples.apple.mps.scripts.mps_example --model_name="mv3" --generate_etrecord -b
  1. 在 ExecuTorch 运行时上运行您的程序并生成 ETDump

./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv3_mps_float16_bundled.pte --bundled_program --dump-outputs
  1. 通过将从运行时获取的 ETDump 与步骤 1 中可选生成的 ETRecord 结合使用,创建 Inspector API 的实例。

python3 -m devtools.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/问题,并附上标签 **#mps**。

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源