| 1 | # LocalInference |
| 2 | |
| 3 | LocalInference provides a local inference implementation powered by [executorch](https://github.com/pytorch/executorch/). |
| 4 | |
| 5 | Llama Stack currently supports on-device inference for iOS with Android coming soon. You can run on-device inference on Android today using [executorch](https://github.com/pytorch/executorch/tree/main/examples/demo-apps/android/LlamaDemo), PyTorch’s on-device inference library. |
| 6 | |
| 7 | ## Installation |
| 8 | |
| 9 | We're working on making LocalInference easier to set up. For now, you'll need to import it via `.xcframework`: |
| 10 | |
| 11 | 1. Clone the executorch submodule in this repo and its dependencies: `git submodule update --init --recursive` |
| 12 | 1. Install [Cmake](https://cmake.org/) for the executorch build` |
| 13 | 1. Drag `LocalInference.xcodeproj` into your project |
| 14 | 1. Add `LocalInference` as a framework in your app target |
| 15 | 1. Add a package dependency on https://github.com/pytorch/executorch (branch latest) |
| 16 | 1. Add all the kernels / backends from executorch (but not exectuorch itself!) as frameworks in your app target: |
| 17 | - backend_coreml |
| 18 | - backend_mps |
| 19 | - backend_xnnpack |
| 20 | - kernels_custom |
| 21 | - kernels_optimized |
| 22 | - kernels_portable |
| 23 | - kernels_quantized |
| 24 | 1. In "Build Settings" > "Other Linker Flags" > "Any iOS Simulator SDK", add: |
| 25 | ``` |
| 26 | -force_load |
| 27 | $(BUILT_PRODUCTS_DIR)/libkernels_optimized-simulator-release.a |
| 28 | -force_load |
| 29 | $(BUILT_PRODUCTS_DIR)/libkernels_custom-simulator-release.a |
| 30 | -force_load |
| 31 | $(BUILT_PRODUCTS_DIR)/libkernels_quantized-simulator-release.a |
| 32 | -force_load |
| 33 | $(BUILT_PRODUCTS_DIR)/libbackend_xnnpack-simulator-release.a |
| 34 | -force_load |
| 35 | $(BUILT_PRODUCTS_DIR)/libbackend_coreml-simulator-release.a |
| 36 | -force_load |
| 37 | $(BUILT_PRODUCTS_DIR)/libbackend_mps-simulator-release.a |
| 38 | ``` |
| 39 | |
| 40 | 1. In "Build Settings" > "Other Linker Flags" > "Any iOS SDK", add: |
| 41 | |
| 42 | ``` |
| 43 | -force_load |
| 44 | $(BUILT_PRODUCTS_DIR)/libkernels_optimized-simulator-release.a |
| 45 | -force_load |
| 46 | $(BUILT_PRODUCTS_DIR)/libkernels_custom-simulator-release.a |
| 47 | -force_load |
| 48 | $(BUILT_PRODUCTS_DIR)/libkernels_quantized-simulator-release.a |
| 49 | -force_load |
| 50 | $(BUILT_PRODUCTS_DIR)/libbackend_xnnpack-simulator-release.a |
| 51 | -force_load |
| 52 | $(BUILT_PRODUCTS_DIR)/libbackend_coreml-simulator-release.a |
| 53 | -force_load |
| 54 | $(BUILT_PRODUCTS_DIR)/libbackend_mps-simulator-release.a |
| 55 | ``` |
| 56 | |
| 57 | ## Preparing a model |
| 58 | |
| 59 | 1. Prepare a `.pte` file [following the executorch docs](https://github.com/pytorch/executorch/blob/main/examples/models/llama2/README.md#step-2-prepare-model) |
| 60 | 2. Bundle the `.pte` and `tokenizer.model` file into your app |
| 61 | |
| 62 | ## Using LocalInference |
| 63 | |
| 64 | 1. Instantiate LocalInference with a DispatchQueue. Optionally, pass it into your agents service: |
| 65 | |
| 66 | ```swift |
| 67 | init () { |
| 68 | runnerQueue = DispatchQueue(label: "org.meta.llamastack") |
| 69 | inferenceService = LocalInferenceService(queue: runnerQueue) |
| 70 | agentsService = LocalAgentsService(inference: inferenceService) |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | 2. Before making any inference calls, load your model from your bundle: |
| 75 | |
| 76 | ```swift |
| 77 | let mainBundle = Bundle.main |
| 78 | inferenceService.loadModel( |
| 79 | modelPath: mainBundle.url(forResource: "llama32_1b_spinquant", withExtension: "pte"), |
| 80 | tokenizerPath: mainBundle.url(forResource: "tokenizer", withExtension: "model"), |
| 81 | completion: {_ in } // use to handle load failures |
| 82 | ) |
| 83 | ``` |
| 84 | |
| 85 | 3. Make inference calls (or agents calls) as you normally would with LlamaStack: |
| 86 | |
| 87 | ``` |
| 88 | for await chunk in try await agentsService.initAndCreateTurn( |
| 89 | messages: [ |
| 90 | .UserMessage(Components.Schemas.UserMessage( |
| 91 | content: .case1("Call functions as needed to handle any actions in the following text:\n\n" + text), |
| 92 | role: .user)) |
| 93 | ] |
| 94 | ) { |
| 95 | ``` |
| 96 | |
| 97 | ## Troubleshooting |
| 98 | |
| 99 | If you receive errors like "missing package product" or "invalid checksum", try cleaning the build folder and resetting the Swift package cache: |
| 100 | |
| 101 | (Opt+Click) Product > Clean Build Folder Immediately |
| 102 | |
| 103 | ``` |
| 104 | rm -rf \ |
| 105 | ~/Library/org.swift.swiftpm \ |
| 106 | ~/Library/Caches/org.swift.swiftpm \ |
| 107 | ~/Library/Caches/com.apple.dt.Xcode \ |
| 108 | ~/Library/Developer/Xcode/DerivedData |
| 109 | ``` |