|
| 1 | +# **Local LLM sample application running Phi3-mini on iOS** |
| 2 | + |
| 3 | +## **Steps** |
| 4 | + |
| 5 | +### General prerequisites |
| 6 | + |
| 7 | +See the general prerequisites [here](../../../../../README.md#General-Prerequisites). |
| 8 | + |
| 9 | +For this application, the following prerequisites are preferred: |
| 10 | + |
| 11 | +1. macOS 14+ |
| 12 | + |
| 13 | +2. Xcode 15+ (latest Xcode version perferred.) |
| 14 | + |
| 15 | +3. iOS SDK 16.x + (iPhone 14 or iPhone 15 powered by a A16 or A17 preferred) |
| 16 | + |
| 17 | +**Note**: |
| 18 | + The current Xcode project contains a built .dylib for ORT and ORT GenAI. The following steps `A, B, C` under `step 1.` for building from source for the libraries are optional. |
| 19 | + However if you want to build from source to include the latest updates, please use the `step 1.` as a reference. |
| 20 | + |
| 21 | +### 1. Steps to build from source for ONNX Runtime and Generative AI libraries [Optional] |
| 22 | + |
| 23 | +#### **A. Preparation** |
| 24 | + |
| 25 | + - Install Python 3.10+ |
| 26 | + |
| 27 | + - Install flatbuffers |
| 28 | + ``` |
| 29 | + pip3 install flatbuffers |
| 30 | + ``` |
| 31 | +
|
| 32 | + - Install [CMake](https://cmake.org/download/) |
| 33 | +
|
| 34 | +#### **B. Compiling ONNX Runtime for iOS** |
| 35 | +
|
| 36 | +```bash |
| 37 | +
|
| 38 | +git clone https://github.com/microsoft/onnxruntime.git |
| 39 | +
|
| 40 | +cd onnxruntime |
| 41 | +
|
| 42 | +./build.sh --build_shared_lib --skip_tests --parallel --build_dir ./build_ios --ios --apple_sysroot iphoneos --osx_arch arm64 --apple_deploy_target 16.6 --cmake_generator Xcode --config Release |
| 43 | +
|
| 44 | +``` |
| 45 | + |
| 46 | +***Notice*** |
| 47 | + |
| 48 | + 1. Before compiling, you must ensure that Xcode is configured correctly and set it on the terminal |
| 49 | + |
| 50 | +```bash |
| 51 | + |
| 52 | +sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer |
| 53 | + |
| 54 | +``` |
| 55 | + |
| 56 | + 2. ONNX Runtime needs to be compiled based on different platforms. For iOS, you can compile for arm64 or x86_64 based on needs. If you are running an iOS simulator on an Intel mac, compile for x86_64. Use arm64 for an ARM based mac to run the simulator, and to run on an iPhone. |
| 57 | + |
| 58 | + 3. It is recommended to directly use the latest iOS SDK for compilation. Of course, you can also lower the version to be compatible with past SDKs. |
| 59 | + |
| 60 | +#### **C. Compiling Generative AI with ONNX Runtime for iOS** |
| 61 | + |
| 62 | +```bash |
| 63 | + |
| 64 | +git clone https://github.com/microsoft/onnxruntime-genai |
| 65 | + |
| 66 | +cd onnxruntime-genai |
| 67 | + |
| 68 | +python3 build.py --parallel --build_dir ./build_iphoneos --ios --ios_sysroot iphoneos --ios_arch arm64 --ios_deployment_target 16.6 --cmake_generator Xcode |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +#### **D. Copy over latest header files and required .dylibs built from source** |
| 73 | + |
| 74 | +If you build from source and get the latest .dylibs for ORT and ORT GenAI, please copy the .dylibs over to `mobile\examples\phi-3\ios\LocalLLM\LocalLLM\lib` and copy the latest header files over to `mobile\examples\phi-3\ios\LocalLLM\LocalLLM\header` |
| 75 | + |
| 76 | +The build output path for libonnxruntime.dylib is `<ORT_PROJECT_ROOT>/build/intermediates/<platform>_<arch>/<build_config>/<build_config-platform>/libonnxruntime.dylib` |
| 77 | +The build output path for libonnxruntime-genai.dylib is `<ORT_GENAI_PROJECT_ROOT>/build/<build_config-platform>/libonnxruntime-genai.dylib`. |
| 78 | + |
| 79 | +For example: |
| 80 | +- `onnxruntime/build/intermediates/iphoneos_arm64/Release/Release-iphoneos/libonnxruntime.1.19.0.dylib` |
| 81 | +- `onnxruntime-genai/build/Release/Release-iphoneos/libonnxruntime-genai.dylib`. |
| 82 | + |
| 83 | +Note that you will need to build and copy the correct dylib for the target architecture you wish to run the app on. |
| 84 | +e.g. |
| 85 | +if you want to run on the iOS simulator on an Intel mac, you must build both onnxruntime and onnxruntime-genai for x86_64 and copy the dylibs to the app's `lib` directory. |
| 86 | +if you want to run on an iPhone, you must build both onnxruntime and onnxruntime-genai for arm64 and copy the dylibs to the app's `lib` directory. |
| 87 | + |
| 88 | +The header files to copy are: |
| 89 | +`<ORT_MAIN_SOURCE_REPO>/onnxruntime/core/session/onnxruntime_c_api.h`, |
| 90 | +`<ORT_GENAI_MAIN_SOURCE_REPO>/src/ort_genai.h`, |
| 91 | +`<ORT_GENAI_MAIN_SOURCE_REPO>/src/ort_genai_c.h`. |
| 92 | + |
| 93 | +### 2. Create/Open the iOS application in Xcode |
| 94 | + |
| 95 | +The app uses Objective-C/C++ since using Generative AI with ONNX Runtime C++ API, Objective-C has better compatiblility. |
| 96 | + |
| 97 | +### 3. Copy the ONNX quantized INT4 model to the App application project |
| 98 | + |
| 99 | +Download from hf repo: <https://huggingface.co/microsoft/Phi-3-mini-128k-instruct-onnx/tree/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4> |
| 100 | + |
| 101 | +After downloading completes, you need to copy files over to the `Resources` directory in the `Destination` column of `Target-LocalLLM`->`Build Phases`-> `New Copy File Phases` -> `Copy Files`. |
| 102 | + |
| 103 | +Upon app launching, Xcode will automatically copy and install the model files from Resources folder and directly download to the iOS device. |
| 104 | + |
| 105 | +### 4. Run the app and checkout the streaming output token results |
| 106 | + |
| 107 | +**Note**: The current app only sets up with a simple initial prompt question, you can adjust/try your own or refine the UI based on requirements. |
| 108 | + |
| 109 | +***Notice:*** The current Xcode project runs on iOS 16.6, feel free to adjust latest iOS/build for lates iOS versions accordingly. |
0 commit comments