|
| 1 | +# iOS Demo App: Executorch Setup |
| 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. |
| 6 | + |
| 7 | +## Pre-setup |
| 8 | + |
| 9 | +1. Install Xcode and Command Line Tools: |
| 10 | + |
| 11 | + ```bash |
| 12 | + xcode-select --install |
| 13 | + ``` |
| 14 | + |
| 15 | +2. Install `buck2` binary (using MacOS Apple Silicon build as example): |
| 16 | + |
| 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`: |
| 25 | + |
| 26 | + ```bash |
| 27 | + ln -s /Applications/CMake.app/Contents/bin/cmake /usr/bin/cmake |
| 28 | + ``` |
| 29 | + |
| 30 | +4. Clone Executorch repository and update submodules: |
| 31 | + |
| 32 | + ```bash |
| 33 | + git clone https://github.com/pytorch/executorch.git |
| 34 | + cd executorch |
| 35 | + git submodule sync |
| 36 | + git submodule update --init |
| 37 | + ``` |
| 38 | + |
| 39 | +5. Verify Python 3.10+ (standard since MacOS 13.5) and `pip3` installation: |
| 40 | + |
| 41 | + ```bash |
| 42 | + which python3 pip |
| 43 | + python3 --version |
| 44 | + ``` |
| 45 | + |
| 46 | +6. Install PyTorch dependencies: |
| 47 | + |
| 48 | + ```bash |
| 49 | + ./install_requirements.sh |
| 50 | + ``` |
| 51 | + |
| 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_XNNPACK=ON` to build XNNPACK backend libraries. |
| 82 | + |
| 83 | +## Building and Copying Libraries |
| 84 | + |
| 85 | +1. Build the libraries: |
| 86 | + |
| 87 | +```bash |
| 88 | +cmake --build . --config Release |
| 89 | +``` |
| 90 | + |
| 91 | +2. Copy the libraries to the appropriate location to link against them: |
| 92 | + |
| 93 | +Navigate to the build artifacts directory: |
| 94 | + |
| 95 | +```bash |
| 96 | +cd Release |
| 97 | +mkdir -p ../../examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/executorch/ |
| 98 | +``` |
| 99 | + |
| 100 | +Copy the core libraries: |
| 101 | + |
| 102 | +```bash |
| 103 | +cp libexecutorch.a \ |
| 104 | + libextension_data_loader.a \ |
| 105 | + ../../examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/executorch/ |
| 106 | +``` |
| 107 | + |
| 108 | +For Portable or Benchmark operators, copy additional libraries: |
| 109 | + |
| 110 | +```bash |
| 111 | +cp libportable_kernels.a \ |
| 112 | + libportable_ops_lib.a \ |
| 113 | + ../../examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/executorch/ |
| 114 | +``` |
| 115 | + |
| 116 | +For XNNPACK backend, copy additional libraries: |
| 117 | + |
| 118 | +```bash |
| 119 | +cp libclog.a \ |
| 120 | + libcpuinfo.a \ |
| 121 | + libpthreadpool.a \ |
| 122 | + libxnnpack_backend.a \ |
| 123 | + libXNNPACK.a \ |
| 124 | + ../../examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Frameworks/executorch/ |
| 125 | +``` |
| 126 | + |
| 127 | +Then return to the `executorch` directory: |
| 128 | + |
| 129 | +```bash |
| 130 | +cd ../.. |
| 131 | +``` |
| 132 | + |
| 133 | +## Model Download and Bundling |
| 134 | + |
| 135 | +1. Download MobileNet model labels and bundle them with the app: |
| 136 | + |
| 137 | +```bash |
| 138 | +mkdir -p examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ |
| 139 | +curl https://github.com/raw/pytorch/hub/master/imagenet_classes.txt -o examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/imagenet_classes.txt |
| 140 | +``` |
| 141 | + |
| 142 | +2. Export a MobileNet v3 model backed with XNNPACK delegate and bundle it with |
| 143 | + the app: |
| 144 | + |
| 145 | +```bash |
| 146 | +export FLATC_EXECUTABLE=$(realpath third-party/flatbuffers/cmake-out/flatc) |
| 147 | +python3 -m examples.export.export_example --model_name="mv3" |
| 148 | +python3 -m examples.backend.xnnpack_examples --model_name="mv3" --delegate |
| 149 | +cp mv3.pte mv3_xnnpack_fp32.pte examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/ |
| 150 | +``` |
| 151 | + |
| 152 | +## Final Steps |
| 153 | + |
| 154 | +1. Open the project with Xcode: |
| 155 | + |
| 156 | +```bash |
| 157 | +open executorch/examples/apple/ios/ExecuTorchDemo/ExecuTorchDemo.xcodeproj |
| 158 | +``` |
| 159 | + |
| 160 | +2. Set the Header Search Paths for `MobileNetClassifier` target to the directory |
| 161 | + containing the `executorch` folder. |
| 162 | + |
| 163 | +3. Run the app (Cmd + R) and tests (Cmd + U). |
0 commit comments