Skip to content

Commit bca2cf5

Browse files
nil-is-allNikhil Viswanath Sivakumar
and
Nikhil Viswanath Sivakumar
authored
Added docs for setting up executorch on windows (#11273)
### Summary First documentation for setting up ExecuTorch for Windows and exporting MobileNet V2 model and executing. This version works only on Windows Powershell on admin mode. --------- Co-authored-by: Nikhil Viswanath Sivakumar <nikhilviswanath@meta.com>
1 parent 93b1a0c commit bca2cf5

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

docs/source/using-executorch-building-from-source.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,156 @@ I 00:00:00.000764 executorch:executor_runner.cpp:180] Model executed successfull
204204
I 00:00:00.000770 executorch:executor_runner.cpp:184] 1 outputs:
205205
Output 0: tensor(sizes=[1], [2.])
206206
```
207+
## Build ExecuTorch for Windows
207208
209+
This document outlines the current known working build instructions for building and validating ExecuTorch on a Windows machine.
210+
211+
This demo uses the
212+
[MobileNet v2](https://pytorch.org/vision/main/models/mobilenetv2.html) model to classify images using the [XNNPACK](https://github.com/google/XNNPACK) backend.
213+
214+
Note that all commands should be executed on Windows powershell in administrator mode.
215+
216+
### Pre-requisites
217+
218+
#### 1. Install Miniconda for Windows
219+
Install miniconda for Windows from the [official website](https://docs.conda.io/en/latest/miniconda.html).
220+
221+
#### 2. Install Git for Windows
222+
Install Git for Windows from the [official website](https://git-scm.com/download/win).
223+
224+
#### 3. Install ClangCL for Windows
225+
Install ClangCL for Windows from the [official website](https://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild?view=msvc-170).
226+
227+
228+
### Create the Conda Environment
229+
To check if conda is detected by the powershell prompt, try `conda list` or `conda --version`
230+
231+
If conda is not detected, you could run the powershell script for conda named `conda-hook.ps1`.
232+
To verify that Conda is available in the in the powershell environment, run try `conda list` or `conda --version`.
233+
If Conda is not available, run conda-hook.ps1 as follows:
234+
```bash
235+
$miniconda_dir\\shell\\condabin\\conda-hook.ps1
236+
```
237+
where `$miniconda_dir` is the directory where you installed miniconda
238+
This is `“C:\Users\<username>\AppData\Local”` by default.
239+
240+
#### Create and activate the conda environment:
241+
```bash
242+
conda create -yn et python=3.12
243+
conda activate et
244+
```
245+
246+
### Check Symlinks
247+
Set the following environment variable to enable symlinks:
248+
```bash
249+
git config --global core.symlinks true
250+
```
251+
252+
### Set up ExecuTorch
253+
Clone ExecuTorch from the [official GitHub repository](https://github.com/pytorch/executorch).
254+
255+
```bash
256+
git clone --recurse -submodules https://github.com/pytorch/executorch.git
257+
```
258+
259+
### Run the Setup Script
260+
261+
Currently, there are a lot of components that are not buildable on Windows. The below instructions install a very minimal ExecuTorch which can be used as a sanity check.
262+
263+
#### Move into the `executorch` directory
264+
```bash
265+
cd executorch
266+
```
267+
268+
#### (Optional) Run a --clean script prior to running the .bat file.
269+
```bash
270+
./install_executorch.bat --clean
271+
```
272+
273+
#### Run the setup script.
274+
You could run the .bat file or the python script.
275+
```bash
276+
./install_executorch.bat
277+
# OR
278+
# python install_executorch.py
279+
```
280+
281+
### Export MobileNet V2
282+
283+
Create the following script named export_mv2.py
284+
285+
```bash
286+
from torchvision.models import mobilenet_v2
287+
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
288+
289+
mv2 = mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT) # This is torch.nn.Module
290+
291+
import torch
292+
from executorch.exir import to_edge
293+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
294+
295+
model = mv2.eval() # turn into evaluation mode
296+
297+
example_inputs = (torch.randn((1, 3, 224, 224)),) # Necessary for exporting the model
298+
299+
exported_graph = torch.export.export(model, example_inputs) # Core Aten graph
300+
301+
edge = to_edge(exported_graph) # Edge Dialect
302+
303+
edge_delegated = edge.to_backend(XnnpackPartitioner()) # Parts of the graph are delegated to XNNPACK
304+
305+
executorch_program = edge_delegated.to_executorch() # ExecuTorch program
306+
307+
pte_path = "mv2_xnnpack.pte"
308+
309+
with open(pte_path, "wb") as file:
310+
executorch_program.write_to_file(file) # Serializing into .pte file
311+
```
312+
313+
#### Run the export script to create a `mv2_xnnpack.pte` file.
314+
315+
```bash
316+
python .\\export_mv2.py
317+
```
318+
319+
### Build and Install C++ Libraries + Binaries
320+
```bash
321+
del -Recurse -Force cmake-out; `
322+
cmake . `
323+
-DCMAKE_INSTALL_PREFIX=cmake-out `
324+
-DPYTHON_EXECUTABLE=$miniconda_dir\\envs\\et\\python.exe `
325+
-DCMAKE_PREFIX_PATH=$miniconda_dir\\envs\\et\\Lib\\site-packages `
326+
-DCMAKE_BUILD_TYPE=Release `
327+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON `
328+
-DEXECUTORCH_BUILD_FLATC=ON `
329+
-DEXECUTORCH_BUILD_PYBIND=OFF `
330+
-DEXECUTORCH_BUILD_XNNPACK=ON `
331+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON `
332+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON `
333+
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON `
334+
-DEXECUTORCH_ENABLE_LOGGING=ON `
335+
-T ClangCL `
336+
-Bcmake-out; `
337+
cmake --build cmake-out -j64 --target install --config Release
338+
```
339+
where `$miniconda_dir` is the directory where you installed miniconda
340+
This is `“C:\Users\<username>\AppData\Local”` by default.
341+
342+
### Run Mobilenet V2 model with XNNPACK delegation
343+
344+
```bash
345+
.\\cmake-out\\backends\\xnnpack\\Release\\xnn_executor_runner.exe --model_path=.\\mv2_xnnpack.pte
346+
```
347+
348+
The expected output would print a tensor of size 1x1000, containing values of class scores.
349+
350+
```bash
351+
Output 0: tensor(sizes=[1, 1000], [
352+
-0.50986, 0.30064, 0.0953904, 0.147726, 0.231205, 0.338555, 0.206892, -0.0575775, … ])
353+
```
354+
355+
Congratulations! You've successfully set up ExecuTorch on your Windows device and ran a MobileNet V2 model.
356+
Now, you can explore and enjoy the power of ExecuTorch on your own Windows device!
208357
209358
## Cross compilation
210359

0 commit comments

Comments
 (0)