|
1 |
| -# iOS Demo App: ExecuTorch Setup |
| 1 | +# ExecuTorch: iOS Demo App Setup |
2 | 2 |
|
3 |
| -This guide explains how to setup ExecuTorch for iOS using a demo app. The app |
4 |
| -employs a MobileNet v3 model (exported to ExecuTorch) to process live camera |
5 |
| -images. |
| 3 | +This README guides you through the setup of ExecuTorch on iOS using a demo app. |
| 4 | +This app utilizes the MobileNet v3 model to process live camera images. |
6 | 5 |
|
7 |
| -## Pre-setup |
| 6 | +## Prerequisites |
8 | 7 |
|
9 |
| -1. Install Xcode and Command Line Tools: |
| 8 | +1. **Install Xcode 15 and Command Line Tools** |
10 | 9 |
|
11 | 10 | ```bash
|
12 | 11 | xcode-select --install
|
13 | 12 | ```
|
14 | 13 |
|
15 |
| -2. Install `buck2` binary (using MacOS Apple Silicon build as example): |
| 14 | +2. **Python 3.10+ and `pip`** (Pre-installed from MacOS 13.5+) |
16 | 15 |
|
17 |
| - ```bash |
18 |
| - curl -L -O https://github.com/facebook/buck2/releases/download/2023-07-18/buck2-aarch64-apple-darwin.zst |
19 |
| - pip3 install zstd |
20 |
| - zstd -cdq buck2-aarch64-apple-darwin.zst > /tmp/buck2 && chmod +x /tmp/buck2 |
21 |
| - ``` |
22 |
| - |
23 |
| -3. Install [Cmake](cmake.org/download) and link it in a system directory or |
24 |
| - `$PATH`: |
| 16 | + [Download](https://www.python.org/downloads/macos/) and install Python 3.10 |
| 17 | + or 3.11, if needed, and verify the versions: |
25 | 18 |
|
26 | 19 | ```bash
|
27 |
| - ln -s /Applications/CMake.app/Contents/bin/cmake /usr/bin/cmake |
| 20 | + which python3 pip |
| 21 | + python3 --version |
| 22 | + pip --version |
28 | 23 | ```
|
29 | 24 |
|
30 |
| -4. Clone ExecuTorch repository and update submodules: |
| 25 | +3. **Follow the [Getting Started](../../../docs/source/getting-started-setup.md) |
| 26 | + Tutorial** |
31 | 27 |
|
32 |
| - ```bash |
33 |
| - git clone https://github.com/pytorch/executorch.git |
34 |
| - cd executorch |
35 |
| - git submodule sync |
36 |
| - git submodule update --init |
37 |
| - ``` |
| 28 | +4. **Backend Dependency Installation** |
38 | 29 |
|
39 |
| -5. Verify Python 3.10+ (standard since MacOS 13.5) and `pip3` installation: |
| 30 | + Install additional dependencies for **CoreML**: |
40 | 31 |
|
41 | 32 | ```bash
|
42 |
| - which python3 pip |
43 |
| - python3 --version |
| 33 | + ./backends/apple/coreml/scripts/install_requirements.sh |
44 | 34 | ```
|
45 | 35 |
|
46 |
| -6. Install PyTorch dependencies: |
| 36 | + And **Metal Performance Shaders**: |
47 | 37 |
|
48 | 38 | ```bash
|
49 |
| - ./install_requirements.sh |
| 39 | + ./backends/apple/mps/install_requirements.sh |
50 | 40 | ```
|
51 | 41 |
|
52 |
| -## Flatbuffers Compiler Setup |
53 |
| - |
54 |
| -Run the following in the `flatbuffers` directory: |
55 |
| - |
56 |
| -```bash |
57 |
| -cd third-party/flatbuffers |
58 |
| -rm -rf cmake-out && mkdir cmake-out && cd cmake-out |
59 |
| -cmake .. && cmake --build . --target flatc |
60 |
| -cd ../../.. |
61 |
| -``` |
62 |
| - |
63 |
| -## ExecuTorch Configuration |
64 |
| - |
65 |
| -Configure the libraries for iOS: |
66 |
| - |
67 |
| -```bash |
68 |
| -rm -rf cmake-out && mkdir cmake-out && cd cmake-out |
69 |
| -cmake .. -G Xcode \ |
70 |
| - -DCMAKE_TOOLCHAIN_FILE=../third-party/pytorch/cmake/iOS.cmake \ |
71 |
| - -DBUCK2=/tmp/buck2 \ |
72 |
| - -DPYTHON_EXECUTABLE=$(which python3) \ |
73 |
| - -DFLATC_EXECUTABLE=$(realpath ../third-party/flatbuffers/cmake-out/flatc) \ |
74 |
| - -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ |
75 |
| - -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=$(pwd) |
76 |
| -``` |
77 |
| - |
78 |
| -Append `-DIOS_PLATFORM=SIMULATOR` for Simulator configuration to build libraries |
79 |
| -for `x86` architecture instead of `arm64`, which is the default. |
80 |
| - |
81 |
| -Append `-DEXECUTORCH_BUILD_COREML_DELGATE=ON` to build CoreML backend libraries. |
82 |
| - |
83 |
| -Append `-DEXECUTORCH_BUILD_XNNPACK=ON` to build XNNPACK backend libraries. |
84 |
| - |
85 |
| -## Building and Copying Libraries |
86 |
| - |
87 |
| -1. Build the libraries: |
88 |
| - |
89 |
| -```bash |
90 |
| -cmake --build . --config Release |
91 |
| -``` |
92 |
| - |
93 |
| -2. Copy the libraries to the appropriate location to link against them: |
| 42 | +## Model Export & Bundling |
94 | 43 |
|
95 |
| -Navigate to the build artifacts directory: |
| 44 | +1. **Export MobileNet v3 model with CoreML, MPS and XNNPACK delegates** |
96 | 45 |
|
97 |
| -```bash |
98 |
| -cd Release |
99 |
| -``` |
100 |
| - |
101 |
| -Copy the core libraries: |
102 |
| - |
103 |
| -```bash |
104 |
| -mkdir -p ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/executorch/ |
105 |
| -cp libexecutorch.a \ |
106 |
| - libextension_data_loader.a \ |
107 |
| - ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/executorch/ |
108 |
| -``` |
109 |
| - |
110 |
| -For Portable CPU operators, copy additional libraries: |
111 |
| - |
112 |
| -```bash |
113 |
| -mkdir -p ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/portable/ |
114 |
| -cp libportable_kernels.a \ |
115 |
| - libportable_ops_lib.a \ |
116 |
| - ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/portable/ |
117 |
| -``` |
118 |
| - |
119 |
| -For CoreML delegate backend, copy additional libraries: |
120 |
| - |
121 |
| -```bash |
122 |
| -mkdir -p ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/coreml/ |
123 |
| -cp libcoremldelegate.a \ |
124 |
| - ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/coreml/ |
125 |
| -``` |
126 |
| - |
127 |
| -For XNNPACK delegate backend, copy additional libraries: |
| 46 | + ```bash |
| 47 | + python3 -m examples.portable.scripts.export --model_name="mv3" |
| 48 | + python3 -m examples.xnnpack.aot_compiler --delegate --model_name="mv3" |
| 49 | + python3 -m examples.apple.coreml.scripts.export_and_delegate --model_name="mv3" |
| 50 | + python3 -m examples.apple.mps.scripts.mps_example --model_name="mv3" |
128 | 51 |
|
129 |
| -```bash |
130 |
| -mkdir -p ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/xnnpack/ |
131 |
| -cp libclog.a \ |
132 |
| - libcpuinfo.a \ |
133 |
| - libpthreadpool.a \ |
134 |
| - libxnnpack_backend.a \ |
135 |
| - libXNNPACK.a \ |
136 |
| - ../../examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/xnnpack/ |
137 |
| -``` |
| 52 | + mkdir -p examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ |
| 53 | + mv mv3*.pte examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ |
| 54 | + ``` |
138 | 55 |
|
139 |
| -Then return to the `executorch` directory: |
| 56 | +2. **Download MobileNet model labels** |
140 | 57 |
|
141 |
| -```bash |
142 |
| -cd ../.. |
143 |
| -``` |
| 58 | + ```bash |
| 59 | + curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \ |
| 60 | + -o examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/imagenet_classes.txt |
| 61 | + ``` |
144 | 62 |
|
145 |
| -## Model Download and Bundling |
| 63 | +## ExecuTorch & Backend Building |
146 | 64 |
|
147 |
| -1. Download MobileNet model labels and bundle them with the app: |
| 65 | +1. **Build frameworks** |
148 | 66 |
|
149 |
| -```bash |
150 |
| -mkdir -p examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ |
151 |
| -curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt -o examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/imagenet_classes.txt |
152 |
| -``` |
| 67 | + ```bash |
| 68 | + ./build/build_apple_frameworks.sh --Release --coreml --mps --xnnpack |
| 69 | + ``` |
153 | 70 |
|
154 |
| -2. Export a MobileNet v3 model backed with XNNPACK delegate and bundle it with |
155 |
| - the app: |
| 71 | +2. **Move frameworks for app linking** |
156 | 72 |
|
157 |
| -```bash |
158 |
| -export FLATC_EXECUTABLE=$(realpath third-party/flatbuffers/cmake-out/flatc) |
159 |
| -python3 -m examples.portable.scripts.export --model_name="mv3" |
160 |
| -python3 -m examples.xnnpack.aot_compiler --model_name="mv3" --delegate |
161 |
| -python3 -m examples.export.coreml_export_and_delegate -m "mv3" |
162 |
| -cp mv3.pte mv3_coreml.pte mv3_xnnpack_fp32.pte examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ |
163 |
| -``` |
| 73 | + ```bash |
| 74 | + mv cmake-out examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks |
| 75 | + ``` |
164 | 76 |
|
165 | 77 | ## Final Steps
|
166 | 78 |
|
167 |
| -1. Open the project with Xcode: |
| 79 | +1. **Open project in Xcode** |
168 | 80 |
|
169 |
| -```bash |
170 |
| -open executorch/examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo.xcodeproj |
171 |
| -``` |
| 81 | + ```bash |
| 82 | + open examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo.xcodeproj |
| 83 | + ``` |
| 84 | + |
| 85 | +2. **Run tests in Xcode** (Cmd + U) or command line: |
172 | 86 |
|
173 |
| -2. Set the Header Search Paths for `MobileNetClassifier` target to the directory |
174 |
| - containing the `executorch` folder. |
| 87 | + ```bash |
| 88 | + xcrun simctl create executorch "iPhone 15" |
| 89 | + xcodebuild clean test \ |
| 90 | + -project examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo.xcodeproj \ |
| 91 | + -scheme App \ |
| 92 | + -destination name=executorch |
| 93 | + xcrun simctl delete executorch |
| 94 | + ``` |
175 | 95 |
|
176 |
| -3. Run the app (Cmd + R) and tests (Cmd + U). |
| 96 | +3. **Setup Code Signing and run app** (Cmd + R). |
0 commit comments