1# Building an ExecuTorch iOS Demo App 2 3Welcome to the tutorial on setting up the ExecuTorch iOS Demo App! 4 5This app uses the 6[MobileNet v3](https://pytorch.org/vision/main/models/mobilenetv3.html) model to 7process live camera images leveraging three different backends: 8[XNNPACK](https://github.com/google/XNNPACK), 9[Core ML](https://developer.apple.com/documentation/coreml) and 10[Metal Performance Shaders (MPS)](https://developer.apple.com/documentation/metalperformanceshaders) 11(Xcode 15+ and iOS 17+ only). 12 13 14 15## Prerequisites 16 17Before we start, make sure you have the following tools installed: 18 19### 1. Xcode 15 and Command Line Tools 20 21Install Xcode 15 from the 22[Mac App Store](https://apps.apple.com/app/xcode/id497799835) and then install 23the Command Line Tools using the terminal: 24 25```bash 26xcode-select --install 27``` 28 29### 2. Python 3.10+ 30 31Python 3.10 or above, along with `pip`, should be pre-installed on MacOS 13.5+. 32If needed, [download Python](https://www.python.org/downloads/macos/) and 33install it. Verify the Python and pip versions using these commands: 34 35```bash 36which python3 pip 37python3 --version 38pip --version 39``` 40 41### 3. Getting Started Tutorial 42 43Follow the [Setting Up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) 44tutorial to configure the basic environment: 45 46```bash 47git clone https://github.com/pytorch/executorch.git --depth 1 --recurse-submodules --shallow-submodules 48cd executorch 49 50python3 -m venv .venv && source .venv/bin/activate 51 52pip install --upgrade cmake pip setuptools wheel 53 54./install_requirements.sh --pybind coreml mps xnnpack 55``` 56 57### 4. Backend Dependencies 58 59Also, follow the corresponding sections from [Core ML](https://pytorch.org/executorch/stable/build-run-coreml) and 60[MPS](https://pytorch.org/executorch/stable/build-run-mps) tutorials to install additional dependencies for those 61backends: 62 63```bash 64./backends/apple/coreml/scripts/install_requirements.sh 65 66./backends/apple/mps/install_requirements.sh 67``` 68 69## Models and Labels 70 71Now, let's move on to exporting and bundling the MobileNet v3 model. 72 73### 1. Export Model 74 75Export the MobileNet v3 model with Core ML, MPS and XNNPACK backends, and move 76the exported model to a specific location where the Demo App will pick them up: 77 78```bash 79MODEL_NAME="mv3" 80 81python3 -m examples.portable.scripts.export --model_name="$MODEL_NAME" 82python3 -m examples.apple.coreml.scripts.export --model_name="$MODEL_NAME" 83python3 -m examples.apple.mps.scripts.mps_example --model_name="$MODEL_NAME" 84python3 -m examples.xnnpack.aot_compiler --model_name="$MODEL_NAME" --delegate 85 86mkdir -p examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ 87mv "$MODEL_NAME*.pte" examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ 88``` 89 90### 2. Download Labels 91 92Download the MobileNet model labels required for image classification: 93 94```bash 95curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \ 96 -o examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/imagenet_classes.txt 97``` 98 99## Final Steps 100 101We're almost done! Now, we just need to open the project in Xcode, run the 102tests, and finally run the app. 103 104### 1. Open Project in Xcode 105 106Double-click on the project file under 107`examples/demo-apps/apple_ios/ExecuTorchDemo` or run the command: 108 109```bash 110open examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo.xcodeproj 111``` 112 113### 2. Run Tests 114 115You can run tests on Simulaltor directly in Xcode with `Cmd + U` or use the 116command line: 117 118```bash 119xcrun simctl create executorch "iPhone 15" 120xcodebuild clean test \ 121 -project examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo.xcodeproj \ 122 -scheme App \ 123 -destination name=executorch 124xcrun simctl delete executorch 125``` 126 127### 3. Run App 128 129Finally, connect the device, set up Code Signing in Xcode, and then run the app 130using `Cmd + R`. Try installing a Release build for better performance. 131 132Congratulations! You've successfully set up the ExecuTorch iOS Demo App. Now, 133you can explore and enjoy the power of ExecuTorch on your iOS device! 134 135Learn more about integrating and running [ExecuTorch on Apple](https://pytorch.org/executorch/stable/apple-runtime) platforms. 136 137 138