使用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 (提前编译) 组件¶
为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运行生成的模型¶
请确保已安装
pybindMPS 支持:
./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
Profiling:¶
[可选] 在导出模型时生成一个 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 项目:
前往项目的 Target 的 Build Phases - Link Binaries With Libraries,点击 + 符号并添加以下框架:位于 Release 文件夹中的文件。
executorch.xcframeworkportable_delegate.xcframeworkmps_delegate.xcframework
从同一页面中,包含用于 MPS 委托所需的库:
MetalPerformanceShaders.frameworkMetalPerformanceShadersGraph.frameworkMetal.framework
在本教程中,你已经学习了如何将模型降低到 MPS 委托,构建 mps_executor_runner 并通过 MPS 委托运行降低后的模型,或者直接使用 MPS 委托静态库在设备上运行。
常见错误及解决方法。¶
如果您在遵循本教程时遇到任何错误或问题,请在 ExecuTorch 仓库中提交错误/问题,并附上标签 #mps。