Skip to content

Commit df3e06a

Browse files
authored
Added Windows docs
1 parent 0421eac commit df3e06a

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

0 commit comments

Comments
 (0)