|
| 1 | +# Getting Started with ExecuTorch |
| 2 | +This section is intended to describe the necessary steps to take PyTorch model and run it using ExecuTorch. To use the framework, you will typically need to take the following steps: |
| 3 | +- Install the ExecuTorch python package and runtime libraries. |
| 4 | +- Export the PyTorch model for the target hardware configuration. |
| 5 | +- Run the model using the ExecuTorch runtime APIs on your development platform. |
| 6 | +- Deploy the model to the target platform using the ExecuTorch runtime. |
| 7 | + |
| 8 | +## Installation |
| 9 | +To use ExecuTorch, you will need to install both the Python package and the appropriate platform-specific runtime libraries. |
| 10 | + |
| 11 | +Pip is the recommended way to install the ExecuTorch python package. This package includes the dependencies needed to export a PyTorch model, as well as Python runtime bindings for model testing and evaluation. It is common to install the package within a Python virtual environment, in order to meet the Python and dependency version requirements. |
| 12 | + |
| 13 | +``` |
| 14 | +pip install executorch |
| 15 | +``` |
| 16 | + |
| 17 | +To build the framework from source, see [Building From Source](TODO). |
| 18 | + |
| 19 | +Backend delegates may require additional dependencies. See the appropriate backend documentation for more information. |
| 20 | + |
| 21 | +#### System Requirements |
| 22 | +The following are required to install the ExecuTorch host libraries, needed to export models and run from Python. Requirements for target end-user devices are backend dependent. See the appropriate backend documentation for more information. |
| 23 | + |
| 24 | +- Python 3.10 - 3.12 |
| 25 | +- g++ version 7 or higher, clang++ version 5 or higher, or another C++17-compatible toolchain. |
| 26 | +- Linux or MacOS operating system (Arm or x86). |
| 27 | + - Windows is supported via WSL. |
| 28 | + |
| 29 | +<hr/> |
| 30 | + |
| 31 | +## Preparing the Model |
| 32 | +Exporting is the process of taking a PyTorch model and converting it to the .pte file format used by the ExecuTorch runtime. This is done using Python APIs. PTE files for common models can be found on HuggingFace (TODO add link). |
| 33 | + |
| 34 | +### Requirements |
| 35 | +- A PyTorch model. |
| 36 | +- Example model inputs, typically as PyTorch tensors. You should be able to successfully run the PyTorch model with these inputs. |
| 37 | +- One or more target hardware backends. |
| 38 | + |
| 39 | +### Selecting a Backend |
| 40 | +ExecuTorch provides hardware acceleration for a wide variety of hardware. The most commonly used backends are XNNPACK, for Arm and x86 CPU, Core ML (for iOS), Vulkan (for Android GPUs), and Qualcomm (for Qualcomm-powered Android phones). |
| 41 | + |
| 42 | +For mobile use cases, consider using XNNPACK for Android and Core ML or XNNPACK for iOS as a first step. See [Delegates](/TODO.md) for a description of available backends. |
| 43 | + |
| 44 | +### Exporting |
| 45 | +Exporting is done using Python APIs. ExecuTorch provides a high degree of customization during the export process, but the typical flow is as follows: |
| 46 | +```python |
| 47 | +import executorch |
| 48 | + |
| 49 | +model = MyModel() # The PyTorch model to export |
| 50 | +example_inputs = (torch.randn(1,3,64,64),) # A tuple of inputs |
| 51 | + |
| 52 | +et_program = |
| 53 | + executorch.exir.to_edge_transform_and_lower( |
| 54 | + torch.export.export(model, example_inputs) |
| 55 | +partitioner=[XnnpackPartitioner()] |
| 56 | +).to_executorch() |
| 57 | + |
| 58 | +with open(“model.pte”, “wb”) as f: |
| 59 | + f.write(et_program.buffer) |
| 60 | +``` |
| 61 | + |
| 62 | +If the model requires varying input sizes, you will need to specify the varying dimensions and bounds as part of the `export` call. See [Exporting a Model for ExecuTorch](/TODO.md) for more information. |
| 63 | + |
| 64 | +The hardware backend to target is controlled by the partitioner parameter to to\_edge\_transform\_and\_lower. In this example, the XnnpackPartitioner is used to target mobile CPUs. See the delegate-specific documentation for a full description of the partitioner and available options. |
| 65 | + |
| 66 | +Quantization can also be done at this stage to reduce model size and runtime. Quantization is backend-specific. See the documentation for the target backend for a full description of supported quantization schemes. |
| 67 | + |
| 68 | +### Testing the Model |
| 69 | + |
| 70 | +After successfully generating a .pte file, it is common to use the Python runtime APIs to validate the model on the development platform. This can be used to evaluate model accuracy before running on-device. |
| 71 | + |
| 72 | +Inference can be run as follows: |
| 73 | +```python |
| 74 | +from executorch.runtime import Runtime |
| 75 | + |
| 76 | +runtime = Runtime.get() |
| 77 | + |
| 78 | +input_tensor = torch.randn(1,3,128,128) |
| 79 | +program = runtime.load_program("/path/to/mode.pte") |
| 80 | +method = program.load_method("forward") |
| 81 | +outputs = method.execute([input_tensor]) |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +<hr/> |
| 86 | + |
| 87 | +## Running on Device |
| 88 | +ExecuTorch provides runtime APIs in Java, Objective-C, and C++. |
| 89 | + |
| 90 | +Quick Links: |
| 91 | +- [Android](#android) |
| 92 | +- [iOS](#ios) |
| 93 | +- [C++](#c) |
| 94 | + |
| 95 | +### Android |
| 96 | + |
| 97 | +#### Installation |
| 98 | +ExecuTorch provides Java bindings for Android usage, which can be consumed from both Java and Kotlin. |
| 99 | +To add the library to your app, download the AAR, and add it to the gradle build rule. TODO Replace with Maven/Gradle package management when available. |
| 100 | + |
| 101 | +``` |
| 102 | +mkdir -p app/libs |
| 103 | +curl https://ossci-android.s3.amazonaws.com/executorch/release/executorch-241002/executorch.aar -o app/libs/executorch.aar |
| 104 | +``` |
| 105 | +And in gradle, |
| 106 | +``` |
| 107 | +# app/build.gradle.kts |
| 108 | +dependencies { |
| 109 | + implementation(files("libs/executorch.aar")) |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +#### Runtime APIs |
| 114 | +Models can be loaded and run using the `Module` class: |
| 115 | +```java |
| 116 | +import org.pytorch.executorch.EValue |
| 117 | +import org.pytorch.executorch.Module |
| 118 | +import org.pytorch.executorch.Tensor |
| 119 | + |
| 120 | +// … |
| 121 | + |
| 122 | +Module model = Module.load(“/path/to/model.pte”) |
| 123 | +// TODO Add input setup |
| 124 | +EValue output = model.forward(input_evalue); |
| 125 | +``` |
| 126 | + |
| 127 | +For more information on Android development, including building from source, a full description of the Java APIs, and information on using ExecuTorch from Android native code, see [Using ExecuTorch on Android](/TODO.md). |
| 128 | + |
| 129 | +### iOS |
| 130 | + |
| 131 | +#### Installation |
| 132 | +ExecuTorch supports both iOS and MacOS via C++ and Objective-C bindings, as well as hardware backends for CoreML, MPS, and CPU. The iOS runtime library is provided as a collection of .xcframework targets and are made available as a Swift PM package. |
| 133 | + |
| 134 | +To get started with Xcode, go to File > Add Package Dependencies. Paste the URL of the ExecuTorch repo into the search bar and select it. Make sure to change the branch name to the desired ExecuTorch version in format “swiftpm-”, (e.g. “swiftpm-0.5.0”). The ExecuTorch dependency can also be added to the package file manually. See [Using ExecuTorch on iOS](/TODO.md) for more information. |
| 135 | + |
| 136 | +#### Runtime APIs |
| 137 | +Models can be loaded and run from Swift as follows: |
| 138 | +```swift |
| 139 | +// TODO Code sample |
| 140 | +``` |
| 141 | + |
| 142 | +For more information on iOS integration, including an API reference, logging setup, and building from source, see [Using ExecuTorch on iOS](/TODO.md). |
| 143 | + |
| 144 | +### C++ |
| 145 | +ExecuTorch provides C++ APIs, which can be used to target embedded or mobile devices. The C++ APIs provide a greater level of control compared to other language bindings, allowing for advanced memory management, data loading, and platform integration. |
| 146 | + |
| 147 | +#### Installation |
| 148 | +CMake is the preferred build system for the ExecuTorch C++ runtime. To use with CMake, clone the ExecuTorch repository as a subdirectory of your project, and use CMake's `add_subdirectory("executorch")` to include the dependency. The `executorch` target, as well as kernel and backend targets will be made available to link against. The runtime can also be built standalone to support diverse toolchains. See [Using ExecuTorch with C++](/TODO.md) for a detailed description of build integration, targets, and cross compilation. |
| 149 | + |
| 150 | +``` |
| 151 | +git clone -b release/0.5 https://github.com/pytorch/executorch.git |
| 152 | +``` |
| 153 | +```python |
| 154 | +# CMakeLists.txt |
| 155 | +add_subdirectory("executorch") |
| 156 | +... |
| 157 | +target_link_libraries( |
| 158 | + my_target |
| 159 | + PRIVATE executorch |
| 160 | + executorch_module_static |
| 161 | + executorch_tensor |
| 162 | + optimized_native_cpu_ops_lib |
| 163 | + xnnpack_backend) |
| 164 | +``` |
| 165 | + |
| 166 | +#### Runtime APIs |
| 167 | +Both high-level and low-level C++ APIs are provided. The low-level APIs are platform independent, do not dynamically allocate memory, and are most suitable for resource-constrained embedded systems. The high-level APIs are provided as a convenience wrapper around the lower-level APIs, and make use of dynamic memory allocation and standard library constructs to reduce verbosity. |
| 168 | + |
| 169 | +ExecuTorch uses CMake for native builds. Integration is typically done by cloning the ExecuTorch repository and using CMake add_subdirectory to add the dependency. |
| 170 | + |
| 171 | +Loading and running a model using the high-level API can be done as follows: |
| 172 | +```cpp |
| 173 | +#include <executorch/extension/module/module.h> |
| 174 | +#include <executorch/extension/tensor/tensor.h> |
| 175 | + |
| 176 | +using namespace ::executorch::extension; |
| 177 | + |
| 178 | +// Load the model. |
| 179 | +Module module("/path/to/model.pte"); |
| 180 | + |
| 181 | +// Create an input tensor. |
| 182 | +float input[1 * 3 * 256 * 256]; |
| 183 | +auto tensor = from_blob(input, {1, 3, 256, 256}); |
| 184 | + |
| 185 | +// Perform an inference. |
| 186 | +const auto result = module.forward(tensor); |
| 187 | + |
| 188 | +if (result.ok()) { |
| 189 | + // Retrieve the output data. |
| 190 | + const auto output = result->at(0).toTensor().const_data_ptr<float>(); |
| 191 | +} |
| 192 | +``` |
| 193 | +
|
| 194 | +For more information on the C++ APIs, see [Running an ExecuTorch Model Using the Module Extension in C++](https://pytorch.org/executorch/stable/extension-module.html) and [Managing Tensor Memory in C++](https://pytorch.org/executorch/stable/extension-tensor.html). |
| 195 | +
|
| 196 | +<hr/> |
| 197 | +
|
| 198 | +## Next Steps |
| 199 | +ExecuTorch provides a high-degree of customizability to support diverse hardware targets. Depending on your use cases, consider exploring one or more of the following pages: |
| 200 | +
|
| 201 | +- [Exporting a Model to ExecuTorch](/TODO.md) for advanced model conversion options. |
| 202 | +- [Delegates](/TODO.md) for available backends and configuration options. |
| 203 | +- [Using ExecuTorch on Android](/TODO.md) and [Using ExecuTorch on iOS](TODO.md) for mobile runtime integration. |
| 204 | +- [Using ExecuTorch with C++](/TODO.md) for embedded and mobile native development. |
| 205 | +- [Troubleshooting, Profiling, and Optimization](/TODO.md) for developer tooling and debugging. |
| 206 | +- [API Reference](/TODO.md) for a full description of available APIs. |
| 207 | +- [Examples](https://github.com/pytorch/executorch/tree/main/examples) for demo apps and example code. |
0 commit comments