1*523fa7a6SAndroid Build Coastguard WorkerThis README gives some examples on backend-specific model workflow. 2*523fa7a6SAndroid Build Coastguard Worker 3*523fa7a6SAndroid Build Coastguard Worker# MPS Backend 4*523fa7a6SAndroid Build Coastguard Worker 5*523fa7a6SAndroid Build Coastguard Worker[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*523fa7a6SAndroid Build Coastguard Worker**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*523fa7a6SAndroid Build Coastguard Worker 8*523fa7a6SAndroid Build Coastguard Worker## Prerequisite 9*523fa7a6SAndroid Build Coastguard Worker 10*523fa7a6SAndroid Build Coastguard WorkerPlease finish the following tutorials: 11*523fa7a6SAndroid Build Coastguard Worker- [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup). 12*523fa7a6SAndroid Build Coastguard Worker- [Setting up MPS backend](../../../backends/apple/mps/setup.md). 13*523fa7a6SAndroid Build Coastguard Worker 14*523fa7a6SAndroid Build Coastguard Worker## Delegation to MPS backend 15*523fa7a6SAndroid Build Coastguard Worker 16*523fa7a6SAndroid Build Coastguard WorkerThe following command will lower the EdgeIR to MPS delegate: 17*523fa7a6SAndroid Build Coastguard Worker 18*523fa7a6SAndroid Build Coastguard Worker```bash 19*523fa7a6SAndroid Build Coastguard Worker# For MobileNet V2 20*523fa7a6SAndroid Build Coastguard Workerpython3 -m examples.apple.mps.scripts.mps_example --model_name="mv2" --bundled 21*523fa7a6SAndroid Build Coastguard Worker``` 22*523fa7a6SAndroid Build Coastguard WorkerTo see all the options when exporting a model to MPS delegate, use the following command: 23*523fa7a6SAndroid Build Coastguard Worker``` 24*523fa7a6SAndroid Build Coastguard Workerpython3 -m examples.apple.mps.scripts.mps_example --help 25*523fa7a6SAndroid Build Coastguard Worker``` 26*523fa7a6SAndroid Build Coastguard Worker 27*523fa7a6SAndroid Build Coastguard WorkerOnce we have the model binary file, then let's run it with the ExecuTorch runtime using the `mps_executor_runner`. 28*523fa7a6SAndroid Build Coastguard Worker 29*523fa7a6SAndroid Build Coastguard Worker```bash 30*523fa7a6SAndroid Build Coastguard Worker# Build and install executorch 31*523fa7a6SAndroid Build Coastguard Workercmake -DCMAKE_INSTALL_PREFIX=cmake-out \ 32*523fa7a6SAndroid Build Coastguard Worker -DCMAKE_BUILD_TYPE=Release \ 33*523fa7a6SAndroid Build Coastguard Worker -DEXECUTORCH_BUILD_DEVTOOLS=ON \ 34*523fa7a6SAndroid Build Coastguard Worker -DEXECUTORCH_ENABLE_EVENT_TRACER=ON \ 35*523fa7a6SAndroid Build Coastguard Worker -DEXECUTORCH_BUILD_MPS=ON \ 36*523fa7a6SAndroid Build Coastguard Worker -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ 37*523fa7a6SAndroid Build Coastguard Worker -Bcmake-out . 38*523fa7a6SAndroid Build Coastguard Workercmake --build cmake-out -j9 --target install --config Release 39*523fa7a6SAndroid Build Coastguard WorkerCMAKE_PREFIX_PATH="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags" 40*523fa7a6SAndroid Build Coastguard Worker# build mps_executor_runner 41*523fa7a6SAndroid Build Coastguard Workerrm -rf cmake-out/examples/apple/mps 42*523fa7a6SAndroid Build Coastguard Workercmake \ 43*523fa7a6SAndroid Build Coastguard Worker -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ 44*523fa7a6SAndroid Build Coastguard Worker -DCMAKE_BUILD_TYPE=Release \ 45*523fa7a6SAndroid Build Coastguard Worker -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ 46*523fa7a6SAndroid Build Coastguard Worker -Bcmake-out/examples/apple/mps \ 47*523fa7a6SAndroid Build Coastguard Worker examples/apple/mps 48*523fa7a6SAndroid Build Coastguard Worker 49*523fa7a6SAndroid Build Coastguard Workercmake --build cmake-out/examples/apple/mps -j9 --config Release 50*523fa7a6SAndroid Build Coastguard Worker 51*523fa7a6SAndroid Build Coastguard Worker# Run the mv2 generated model using the mps_executor_runner 52*523fa7a6SAndroid Build Coastguard Worker./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv2_mps_bundled.pte --bundled_program 53*523fa7a6SAndroid Build Coastguard Worker``` 54*523fa7a6SAndroid Build Coastguard Worker 55*523fa7a6SAndroid Build Coastguard Worker## Profiling 56*523fa7a6SAndroid Build Coastguard Worker 57*523fa7a6SAndroid Build Coastguard WorkerThe following arguments can be used alongside the mps_executor_runner to benchmark a model: 58*523fa7a6SAndroid Build Coastguard Worker- `--num-runs`: Number of total iterations. 59*523fa7a6SAndroid Build Coastguard Worker- `--profile`: Show execution time for each iteration. 60*523fa7a6SAndroid Build Coastguard Worker- `--bundled_program`: Load the inputs and outputs from the bundled program (note that during export, `--bundled` flag must be passed) 61*523fa7a6SAndroid Build Coastguard Worker 62*523fa7a6SAndroid Build Coastguard WorkerFor example: 63*523fa7a6SAndroid Build Coastguard Worker```bash 64*523fa7a6SAndroid Build Coastguard Worker./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv2_mps_bundled.pte --profile --num_runs 10 65*523fa7a6SAndroid Build Coastguard Worker``` 66*523fa7a6SAndroid Build Coastguard Worker 67*523fa7a6SAndroid Build Coastguard Worker## Limitation 68*523fa7a6SAndroid Build Coastguard Worker 69*523fa7a6SAndroid Build Coastguard Worker1. MPS backend is currently supported from iOS 17 and macOS Sonoma and newer. 70