目录

使用 MPS 后端构建和运行 ExecuTorch

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

MPS 后端设备将机器学习计算图和基元映射到 MPS 提供的 MPS Graph 框架和调优内核上。

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

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

先决条件(硬件和软件)

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

硬件:

  • 用于跟踪模型的 mac

软件:

设置 Developer Environment

步骤 1.请完成教程 设置 ExecuTorch

步骤 2。安装降低 MPS 委托所需的依赖项:

./backends/apple/mps/install_requirements.sh

AOT(预先)组件

为 MPS 委托编译模型

  • 在此步骤中,您将生成一个简单的 ExecuTorch 程序,该程序将 MobileNetV3 模型降低到 MPS 委托。然后,您将在运行时传递此程序(文件),以使用 MPS 后端运行它。.pte

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 直接运行生成的模型

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

./install_requirements.sh --pybind mps
  1. 运行脚本以跟踪模型并直接从 python 运行它:mps_example

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_bundled_fp16.pte --bundled_program --dump-outputs
  1. 通过传入您从运行时获取的 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-outexecutorch.xcframeworkportable_delegate.xcframework

cd cmake-out && ls

步骤 2。将框架链接到您的 XCode 项目中: 转到项目 Target 的 - ,单击 + 号并添加 frameworks: files 位于文件夹中。Build PhasesLink Binaries With LibrariesRelease

  • executorch.xcframework

  • portable_delegate.xcframework

  • mps_delegate.xcframework

在同一页面中,包括 MPS 委托所需的库:

  • MetalPerformanceShaders.framework

  • MetalPerformanceShadersGraph.framework

  • Metal.framework

在本教程中,您学习了如何通过 MPS 委托构建mps_executor_runner模型并运行降低的模型,或通过 MPS 委托或直接在设备上使用 MPS 委托静态库运行降低的模型。

经常遇到的错误和解决方法。

如果您在本教程中遇到任何错误或问题,请在 ExecuTorch 存储库上提交错误/问题,并带有 #mps 标签。

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

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

查看资源