Welcome to the tutorial on setting up the ExecuTorch iOS Demo App!
This app uses the MobileNet v3 model to process live camera images leveraging three different backends: XNNPACK, Core ML and Metal Performance Shaders (MPS) (Xcode 15+ and iOS 17+ only).
Before we start, make sure you have the following tools installed:
Install Xcode 15+ from the Mac App Store and then install the Command Line Tools using the terminal:
xcode-select --install
Python 3.10 or above, along with pip3
, should be pre-installed on MacOS 13.5+.
If needed, download Python and
install it. Verify the Python and pip versions using these commands:
which python3 pip3
python3 --version
pip3 --version
The prebuilt ExecuTorch runtime, backend, and kernels are available as a Swift PM package. Ensure the latest SwiftPM version is linked to your Xcode installation. The steps to add the necessary package dependency are available in the Using ExecuTorch for iOS documentation.
# Create a virtual environment if needed:
# python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip
pip install executorch
Alternatively, clone ExecuTorch and set up the environment as explained in the Building from Source tutorial:
git clone https://github.com/pytorch-labs/executorch-examples.git && cd executorch-examples
Now, let's move on to exporting and bundling the MobileNet v3 model.
Export the MobileNet v3 model using the command line with Core ML, MPS and XNNPACK backends
python3 mv3/python/export.py
Move the exported model to a specific location where the Demo App will pick them up:
mkdir -p mv3/apple/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/
mv *.pte mv3/apple/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/
Download the MobileNet model labels required for image classification:
curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \
-o mv3/apple/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/imagenet_classes.txt
We're almost done! Now, we just need to open the project in Xcode, run the tests, and finally run the app.
Double-click on the project file under mv3/apple/ExecuTorchDemo/ExecuTorchDemo
to openit with Xcode, or run the command:
open mv3/apple/ExecuTorchDemo/ExecuTorchDemo.xcodeproj
You can run tests on Simulaltor directly in Xcode with Cmd + U
or use the
command line:
xcrun simctl create executorch "iPhone 15"
xcodebuild clean test \
-project mv3/apple/ExecuTorchDemo/ExecuTorchDemo.xcodeproj \
-scheme App \
-destination name=executorch
xcrun simctl delete executorch
Finally, connect the device, set up Code Signing in Xcode, and then run the app
using Cmd + R
. Try installing a Release build for better performance.
Congratulations! You've successfully set up the ExecuTorch iOS Demo App. Now, you can explore and enjoy the power of ExecuTorch on your iOS device!
Learn more about integrating and running ExecuTorch on Apple platforms.
For specific examples, take a look at this GitHub repository.