|
1 |
| -<!---- DO NOT MODIFY Progress Bar Start ---> |
2 |
| -<div class="progress-bar-wrapper"> |
3 |
| - <div class="progress-bar-item"> |
4 |
| - <div class="step-number" id="step-1">1</div> |
5 |
| - <span class="step-caption" id="caption-1"></span> |
6 |
| - </div> |
7 |
| - <div class="progress-bar-item"> |
8 |
| - <div class="step-number" id="step-2">2</div> |
9 |
| - <span class="step-caption" id="caption-2"></span> |
10 |
| - </div> |
11 |
| - <div class="progress-bar-item"> |
12 |
| - <div class="step-number" id="step-3">3</div> |
13 |
| - <span class="step-caption" id="caption-3"></span> |
14 |
| - </div> |
15 |
| - <div class="progress-bar-item"> |
16 |
| - <div class="step-number" id="step-4">4</div> |
17 |
| - <span class="step-caption" id="caption-4"></span> |
18 |
| - </div> |
19 |
| -</div> |
20 |
| -<!---- DO NOT MODIFY Progress Bar End---> |
21 |
| - |
22 | 1 | # Setting Up ExecuTorch
|
23 |
| -In this section, we'll learn how to |
24 |
| -* Set up an environment to work on ExecuTorch |
25 |
| -* Generate a sample ExecuTorch program |
26 |
| -* Build and run a program with the ExecuTorch runtime |
27 |
| - |
28 |
| -## System Requirements |
29 |
| -### Operating System |
30 |
| - |
31 |
| -We've tested these instructions on the following systems, although they should |
32 |
| -also work in similar environments. |
33 |
| - |
34 |
| - |
35 |
| -Linux (x86_64) |
36 |
| -- CentOS 8+ |
37 |
| -- Ubuntu 20.04.6 LTS+ |
38 |
| -- RHEL 8+ |
39 |
| - |
40 |
| -macOS (x86_64/M1/M2) |
41 |
| -- Big Sur (11.0)+ |
42 |
| - |
43 |
| -Windows (x86_64) |
44 |
| -- Windows Subsystem for Linux (WSL) with any of the Linux options |
45 |
| - |
46 |
| -### Software |
47 |
| -* `conda` or another virtual environment manager |
48 |
| - - We recommend `conda` as it provides cross-language |
49 |
| - support and integrates smoothly with `pip` (Python's built-in package manager) |
50 |
| - - Otherwise, Python's built-in virtual environment manager `python venv` is a good alternative. |
51 |
| -* `g++` version 7 or higher, `clang++` version 5 or higher, or another |
52 |
| - C++17-compatible toolchain. |
53 |
| - |
54 |
| -Note that the cross-compilable core runtime code supports a wider range of |
55 |
| -toolchains, down to C++17. See the [Runtime Overview](./runtime-overview.md) for |
56 |
| -portability details. |
57 |
| - |
58 |
| -## Quick Setup: Colab/Jupyter Notebook Prototype |
59 |
| - |
60 |
| -To utilize ExecuTorch to its fullest extent, please follow the setup instructions provided below to install from source. |
61 |
| - |
62 |
| -Alternatively, if you would like to experiment with ExecuTorch quickly and easily, we recommend using the following [colab notebook](https://colab.research.google.com/drive/1qpxrXC3YdJQzly3mRg-4ayYiOjC6rue3?usp=sharing) for prototyping purposes. You can install directly via `pip` for basic functionality. |
63 |
| - ```bash |
64 |
| - pip install executorch |
65 |
| - ``` |
66 |
| - |
67 |
| - |
68 |
| -## Environment Setup |
69 |
| - |
70 |
| -### Create a Virtual Environment |
71 |
| - |
72 |
| -[Install conda on your machine](https://conda.io/projects/conda/en/latest/user-guide/install/index.html). Then, create a virtual environment to manage our dependencies. |
73 |
| - ```bash |
74 |
| - # Create and activate a conda environment named "executorch" |
75 |
| - conda create -yn executorch python=3.10.0 |
76 |
| - conda activate executorch |
77 |
| - ``` |
78 |
| - |
79 |
| -### Clone and install ExecuTorch requirements |
80 |
| - |
81 |
| - ```bash |
82 |
| - # Clone the ExecuTorch repo from GitHub |
83 |
| - # 'main' branch is the primary development branch where you see the latest changes. |
84 |
| - # 'viable/strict' contains all of the commits on main that pass all of the necessary CI checks. |
85 |
| - git clone --branch viable/strict https://github.com/pytorch/executorch.git |
86 |
| - cd executorch |
87 |
| - |
88 |
| - # Update and pull submodules |
89 |
| - git submodule sync |
90 |
| - git submodule update --init |
91 |
| - |
92 |
| - # Install ExecuTorch pip package and its dependencies, as well as |
93 |
| - # development tools like CMake. |
94 |
| - # If developing on a Mac, make sure to install the Xcode Command Line Tools first. |
95 |
| - ./install_executorch.sh |
96 |
| - ``` |
97 |
| - |
98 |
| - Use the [`--pybind` flag](https://github.com/pytorch/executorch/blob/main/install_executorch.sh#L26-L29) to install with pybindings and dependencies for other backends. |
99 |
| - ```bash |
100 |
| - ./install_executorch.sh --pybind <coreml | mps | xnnpack> |
101 |
| - |
102 |
| - # Example: pybindings with CoreML *only* |
103 |
| - ./install_executorch.sh --pybind coreml |
104 |
| - |
105 |
| - # Example: pybinds with CoreML *and* XNNPACK |
106 |
| - ./install_executorch.sh --pybind coreml xnnpack |
107 |
| - ``` |
108 |
| - |
109 |
| - By default, `./install_executorch.sh` command installs pybindings for XNNPACK. To disable any pybindings altogether: |
110 |
| - ```bash |
111 |
| - ./install_executorch.sh --pybind off |
112 |
| - ``` |
113 |
| - |
114 |
| -After setting up your environment, you are ready to convert your PyTorch programs |
115 |
| -to ExecuTorch. |
116 |
| - |
117 |
| -> **_NOTE:_** Cleaning the build system |
118 |
| -> |
119 |
| -> When fetching a new version of the upstream repo (via `git fetch` or `git |
120 |
| -> pull`) it is a good idea to clean the old build artifacts. The build system |
121 |
| -> does not currently adapt well to changes in build dependencies. |
122 |
| -> |
123 |
| -> You should also update and pull the submodules again, in case their versions |
124 |
| -> have changed. |
125 |
| -> |
126 |
| -> ```bash |
127 |
| -> # From the root of the executorch repo: |
128 |
| -> ./install_executorch.sh --clean |
129 |
| -> git submodule sync |
130 |
| -> git submodule update --init |
131 |
| -> ``` |
132 |
| -
|
133 |
| -## Create an ExecuTorch program |
134 |
| -
|
135 |
| -After setting up your environment, you are ready to convert your PyTorch programs |
136 |
| -to ExecuTorch. |
137 |
| -
|
138 |
| -### Export a Program |
139 |
| -ExecuTorch provides APIs to compile a PyTorch [`nn.Module`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html) to a `.pte` binary consumed by the ExecuTorch runtime. |
140 |
| -1. [`torch.export`](https://pytorch.org/docs/stable/export.html) |
141 |
| -1. [`exir.to_edge`](export-to-executorch-api-reference.md#exir.to_edge) |
142 |
| -1. [`exir.to_executorch`](ir-exir.md) |
143 |
| -1. Save the result as a [`.pte` binary](pte-file-format.md) to be consumed by the ExecuTorch runtime. |
144 |
| -
|
145 |
| -
|
146 |
| -Let's try this using with a simple PyTorch model that adds its inputs. |
147 |
| -
|
148 |
| -Create `export_add.py` in a new directory outside of the ExecuTorch repo. |
149 |
| -
|
150 |
| -**Note: It's important that this file does does not live in the directory that's a parent of the `executorch` directory. We need python to import from site-packages, not from the repo itself.** |
151 |
| -
|
152 |
| -``` |
153 |
| -mkdir -p ../example_files |
154 |
| -cd ../example_files |
155 |
| -touch export_add.py |
156 |
| -``` |
157 |
| -
|
158 |
| -Add the following code to `export_add.py`: |
159 |
| -```python |
160 |
| -import torch |
161 |
| -from torch.export import export |
162 |
| -from executorch.exir import to_edge |
163 |
| -
|
164 |
| -# Start with a PyTorch model that adds two input tensors (matrices) |
165 |
| -class Add(torch.nn.Module): |
166 |
| - def __init__(self): |
167 |
| - super(Add, self).__init__() |
168 |
| -
|
169 |
| - def forward(self, x: torch.Tensor, y: torch.Tensor): |
170 |
| - return x + y |
171 |
| -
|
172 |
| -# 1. torch.export: Defines the program with the ATen operator set. |
173 |
| -aten_dialect = export(Add(), (torch.ones(1), torch.ones(1))) |
174 |
| -
|
175 |
| -# 2. to_edge: Make optimizations for Edge devices |
176 |
| -edge_program = to_edge(aten_dialect) |
177 |
| -
|
178 |
| -# 3. to_executorch: Convert the graph to an ExecuTorch program |
179 |
| -executorch_program = edge_program.to_executorch() |
180 |
| -
|
181 |
| -# 4. Save the compiled .pte program |
182 |
| -with open("add.pte", "wb") as file: |
183 |
| - file.write(executorch_program.buffer) |
184 |
| -
|
185 |
| -``` |
186 |
| -
|
187 |
| -Then, execute it from your terminal. |
188 |
| -```bash |
189 |
| -python3 export_add.py |
190 |
| -``` |
191 |
| -
|
192 |
| -If it worked you'll see `add.pte` in that directory |
193 |
| -
|
194 |
| -See the [ExecuTorch export tutorial](tutorials_source/export-to-executorch-tutorial.py) to learn more about the export process. |
195 |
| -
|
196 |
| -
|
197 |
| -## Build & Run |
198 |
| -
|
199 |
| -After creating a program go back to the executorch directory to execute it using the ExecuTorch runtime. |
200 |
| -``` |
201 |
| -cd ../executorch |
202 |
| -``` |
203 |
| -
|
204 |
| -For now, let's use [`executor_runner`](https://github.com/pytorch/executorch/blob/main/examples/portable/executor_runner/executor_runner.cpp), an example that runs the `forward` method on your program using the ExecuTorch runtime. |
205 |
| - |
206 |
| -### Build Tooling Setup |
207 |
| -The ExecuTorch repo uses CMake to build its C++ code. Here, we'll configure it to build the `executor_runner` tool to run it on our desktop OS. |
208 |
| - ```bash |
209 |
| - # Clean and configure the CMake build system. Compiled programs will |
210 |
| - # appear in the executorch/cmake-out directory we create here. |
211 |
| - ./install_executorch.sh --clean |
212 |
| - (mkdir cmake-out && cd cmake-out && cmake ..) |
213 |
| - |
214 |
| - # Go to work directory. |
215 |
| - cd .. |
216 |
| - |
217 |
| - # Build the executor_runner target |
218 |
| - cmake --build cmake-out --target executor_runner -j9 |
219 |
| - ``` |
220 |
| - |
221 |
| -> **_NOTE:_** Cleaning the build system |
222 |
| -> |
223 |
| -> When fetching a new version of the upstream repo (via `git fetch` or `git |
224 |
| -> pull`) it is a good idea to clean the old build artifacts. The build system |
225 |
| -> does not currently adapt well to changes in build dependencies. |
226 |
| -> |
227 |
| -> You should also update and pull the submodules again, in case their versions |
228 |
| -> have changed. |
229 |
| -> |
230 |
| -> ```bash |
231 |
| -> # From the root of the executorch repo: |
232 |
| -> ./install_executorch.sh --clean |
233 |
| -> git submodule sync |
234 |
| -> git submodule update --init |
235 |
| -> ``` |
236 |
| -
|
237 |
| -### Run Your Program |
238 |
| -
|
239 |
| -Now that we've exported a program and built the runtime, let's execute it! |
240 |
| -
|
241 |
| - ```bash |
242 |
| - ./cmake-out/executor_runner --model_path ../example_files/add.pte |
243 |
| - ``` |
244 |
| -Our output is a `torch.Tensor` with a size of 1. The `executor_runner` sets all input values to a [`torch.ones`](https://pytorch.org/docs/stable/generated/torch.ones.html) tensor, so when `x=[1]` and `y=[1]`, we get `[1]+[1]=[2]` |
245 |
| - :::{dropdown} Sample Output |
246 |
| - |
247 |
| - ``` |
248 |
| -Output 0: tensor(sizes=[1], [2.]) |
249 |
| - ``` |
250 |
| - ::: |
251 |
| - |
252 |
| -To learn how to build a similar program, visit the [Runtime APIs Tutorial](extension-module.md). |
253 |
| - |
254 |
| -## Next Steps |
255 | 2 |
|
256 |
| -Congratulations! You have successfully exported, built, and run your first |
257 |
| -ExecuTorch program. Now that you have a basic understanding of ExecuTorch, |
258 |
| -explore its advanced features and capabilities below. |
| 3 | +This page is re-organized into the following pages: |
259 | 4 |
|
260 |
| -* Build an [Android](demo-apps-android.md) or [iOS](demo-apps-ios.md) demo app |
261 |
| -* Learn more about the [export process](export-overview.md) |
262 |
| -* Dive deeper into the [Export Intermediate Representation (EXIR)](ir-exir.md) for complex export workflows |
263 |
| -* Refer to [advanced examples in executorch/examples](https://github.com/pytorch/executorch/tree/main/examples) |
| 5 | +* [Getting Started with ExecuTorch](getting-started.md) |
| 6 | +* [Building from Source](using-executorch-building-from-source.md) |
0 commit comments