Name Date Size #Lines LOC

..--

executor_runner/H25-Apr-2025-461404

scripts/H25-Apr-2025-401309

CMakeLists.txtH A D25-Apr-20253.6 KiB127102

README.mdH A D25-Apr-20252.7 KiB7053

test_mps.shH A D25-Apr-20251.9 KiB6543

README.md

1This README gives some examples on backend-specific model workflow.
2
3# MPS Backend
4
5[MPS](https://developer.apple.com/documentation/metalperformanceshaders) is a framework of highly optimized compute and graphics shaders, specially tuned to take advantage of the unique hardware characteristics of each GPU family to ensure optimal performance.
6**MPS** backend takes advantage of [MPSGraph](https://developer.apple.com/documentation/metalperformanceshadersgraph/mpsgraph?language=objc) to build, compile, and execute customized multidimensional graphs from the edge dialect ops.
7
8## Prerequisite
9
10Please finish the following tutorials:
11- [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup).
12- [Setting up MPS backend](../../../backends/apple/mps/setup.md).
13
14## Delegation to MPS backend
15
16The following command will lower the EdgeIR to MPS delegate:
17
18```bash
19# For MobileNet V2
20python3 -m examples.apple.mps.scripts.mps_example --model_name="mv2" --bundled
21```
22To see all the options when exporting a model to MPS delegate, use the following command:
23```
24python3 -m examples.apple.mps.scripts.mps_example --help
25```
26
27Once we have the model binary file, then let's run it with the ExecuTorch runtime using the `mps_executor_runner`.
28
29```bash
30# Build and install executorch
31cmake -DCMAKE_INSTALL_PREFIX=cmake-out \
32          -DCMAKE_BUILD_TYPE=Release \
33          -DEXECUTORCH_BUILD_DEVTOOLS=ON \
34          -DEXECUTORCH_ENABLE_EVENT_TRACER=ON \
35          -DEXECUTORCH_BUILD_MPS=ON \
36          -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
37          -Bcmake-out .
38cmake --build cmake-out -j9 --target install --config Release
39CMAKE_PREFIX_PATH="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags"
40# build mps_executor_runner
41rm -rf cmake-out/examples/apple/mps
42cmake \
43    -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
44    -DCMAKE_BUILD_TYPE=Release \
45    -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
46    -Bcmake-out/examples/apple/mps \
47    examples/apple/mps
48
49cmake --build cmake-out/examples/apple/mps -j9 --config Release
50
51# Run the mv2 generated model using the mps_executor_runner
52./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv2_mps_bundled.pte --bundled_program
53```
54
55## Profiling
56
57The following arguments can be used alongside the mps_executor_runner to benchmark a model:
58- `--num-runs`: Number of total iterations.
59- `--profile`: Show execution time for each iteration.
60- `--bundled_program`: Load the inputs and outputs from the bundled program (note that during export, `--bundled` flag must be passed)
61
62For example:
63```bash
64./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv2_mps_bundled.pte --profile --num_runs 10
65```
66
67## Limitation
68
691. MPS backend is currently supported from iOS 17 and macOS Sonoma and newer.
70