Skip to content

Commit 1658c03

Browse files
committed
Added docs for setting up executorch on windows
1 parent ceaf147 commit 1658c03

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# How to Build ExecuTorch for Windows
2+
3+
This document outlines the current known working build instructions for building and validating ExecuTorch on a Windows machine.
4+
5+
This demo uses the
6+
[MobileNet v2](https://pytorch.org/vision/main/models/mobilenetv2.html) model to
7+
process live camera images leveraging the
8+
[XNNPACK](https://github.com/google/XNNPACK) backend.
9+
10+
Note that all commands should be executed on Windows powershell in administrator mode.
11+
12+
## Pre-requisites
13+
14+
### 1. Install Miniconda for Windows
15+
Install miniconda for Windows from the [official website](https://docs.conda.io/en/latest/miniconda.html).
16+
17+
### 2. Install Git for Windows
18+
Install Git for Windows from the [official website](https://git-scm.com/download/win).
19+
20+
### 3. Install ClangCL for Windows
21+
Install ClangCL for Windows from the [official website](https://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild?view=msvc-170).
22+
23+
24+
## Create the Conda Environment
25+
To check if conda is detected by the powershell prompt, try `conda list` or `conda --version`
26+
27+
If conda is not detected, you could run the powershell script for conda named `conda-hook.ps1`.
28+
29+
```bash
30+
$miniconda_dir\\shell\\condabin\\conda-hook.ps1
31+
```
32+
where `$miniconda_dir` is the directory where you installed miniconda
33+
This is `“C:\Users\<username>\AppData\Local”` by default.
34+
35+
### Create and activate the conda environment:
36+
```bash
37+
conda create -yn et python=3.12
38+
conda activate et
39+
```
40+
41+
## Check Symlinks
42+
Set the following environment variable to enable symlinks:
43+
```bash
44+
git config --global core.symlinks true
45+
```
46+
47+
## Set up ExecuTorch
48+
Clone ExecuTorch from the [official GitHub repository](https://github.com/pytorch/executorch).
49+
50+
```bash
51+
git clone --recurse -submodules https://github.com/pytorch/executorch.git
52+
```
53+
54+
## Run the Setup Script
55+
56+
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.
57+
58+
#### Move into the `executorch` directory
59+
```bash
60+
cd executorch
61+
```
62+
63+
#### (Optional) Run a --clean script prior to running the .bat file.
64+
```bash
65+
./install_executorch.bat --clean
66+
```
67+
68+
#### Run the setup script.
69+
You could run the .bat file or the python script.
70+
```bash
71+
./install_executorch.bat
72+
# OR
73+
# python install_executorch.py
74+
```
75+
76+
## Export MobileNet V2
77+
78+
Create the following script named export_mv2.py
79+
80+
```bash
81+
from torchvision.models import mobilenet_v2
82+
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
83+
84+
mv2 = mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT) # This is torch.nn.Module
85+
86+
import torch
87+
from executorch.exir import to_edge
88+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
89+
90+
model = mv2.eval() # turn into evaluation mode
91+
92+
example_inputs = (torch.randn((1, 3, 224, 224)),) # Necessary for exporting the model
93+
94+
exported_graph = torch.export.export(model, example_inputs) # Core Aten graph
95+
96+
edge = to_edge(exported_graph) # Edge Dialect
97+
98+
edge_delegated = edge.to_backend(XnnpackPartitioner()) # Parts of the graph are delegated to XNNPACK
99+
100+
executorch_program = edge_delegated.to_executorch() # ExecuTorch program
101+
102+
pte_path = "mv2_xnnpack.pte"
103+
104+
with open(pte_path, "wb") as file:
105+
executorch_program.write_to_file(file) # Serializing into .pte file
106+
```
107+
108+
### Run the export script to create a `mv2_xnnpack.pte` file.
109+
110+
```bash
111+
python .\\export_mv2.py
112+
```
113+
114+
## Build and Install C++ Libraries + Binaries
115+
```bash
116+
del -Recurse -Force cmake-out; `
117+
cmake . `
118+
-DCMAKE_INSTALL_PREFIX=cmake-out `
119+
-DPYTHON_EXECUTABLE=C:\Users\nikhi\miniconda3\envs\et\python.exe `
120+
-DCMAKE_PREFIX_PATH=C:\Users\nikhi\miniconda3\envs\et\Lib\site-packages `
121+
-DCMAKE_BUILD_TYPE=Release `
122+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON `
123+
-DEXECUTORCH_BUILD_FLATC=ON `
124+
-DEXECUTORCH_BUILD_PYBIND=OFF `
125+
-DEXECUTORCH_BUILD_XNNPACK=ON `
126+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON `
127+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON `
128+
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON `
129+
-DEXECUTORCH_ENABLE_LOGGING=ON `
130+
-T ClangCL `
131+
-Bcmake-out; `
132+
cmake --build cmake-out -j64 --target install --config Release
133+
```
134+
135+
## Run Mobilenet V2 model with XNNPACK delegation
136+
137+
```bash
138+
.\cmake-out\backends\xnnpack\Release\xnn_executor_runner.exe --model_path=.\mv2_xnnpack.pte
139+
```
140+
141+
The expected output would print a tensor of size 1x1000.
142+
143+
```bash
144+
Output 0: tensor(sizes=[1, 1000], [
145+
-0.50986, 0.30064, 0.0953904, 0.147726, 0.231205, 0.338555, 0.206892, -0.0575775, … ])
146+
```
147+
148+
Congratulations! You've successfully set up ExecuTorch on your Windows device and ran a MobileNet V2 model.
149+
Now, you can explore and enjoy the power of ExecuTorch on your own Windows device!
150+

0 commit comments

Comments
 (0)